]> git.pld-linux.org Git - packages/kernel.git/blob - kernel-aufs5.patch
Die on failures.
[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 a6313a969bc5f..aca4b89d41a14 100644
6 --- a/fs/Kconfig
7 +++ b/fs/Kconfig
8 @@ -312,6 +312,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 84c5e4cdfee5a..b4fcdad8412ed 100644
18 --- a/fs/Makefile
19 +++ b/fs/Makefile
20 @@ -138,3 +138,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 a4a0c2baaf274..cc2119bc60fd1 100644
30 --- a/MAINTAINERS
31 +++ b/MAINTAINERS
32 @@ -3122,6 +3122,19 @@ F:       include/uapi/linux/audit.h
33  F:     kernel/audit*
34  F:     lib/*audit.c
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 +S:     Supported
41 +W:     http://aufs.sourceforge.net
42 +T:     git://github.com/sfjro/aufs4-linux.git
43 +F:     Documentation/ABI/testing/debugfs-aufs
44 +F:     Documentation/ABI/testing/sysfs-aufs
45 +F:     Documentation/filesystems/aufs/
46 +F:     fs/aufs/
47 +F:     include/uapi/linux/aufs_type.h
48 +
49  AUXILIARY DISPLAY DRIVERS
50  M:     Miguel Ojeda <ojeda@kernel.org>
51  S:     Maintained
52 diff --git a/drivers/block/loop.c b/drivers/block/loop.c
53 index 7bf4686af774e..a62b132cbd551 100644
54 --- a/drivers/block/loop.c
55 +++ b/drivers/block/loop.c
56 @@ -811,6 +811,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 cf871a81f4fdc..bc5095b734f58 100644
83 --- a/fs/dcache.c
84 +++ b/fs/dcache.c
85 @@ -1320,7 +1320,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 9c6c6a3e2de51..02382fa9bd341 100644
96 --- a/fs/fcntl.c
97 +++ b/fs/fcntl.c
98 @@ -33,7 +33,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 @@ -64,6 +64,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 ed0cab8a32db1..7e53985e62e5e 100644
118 --- a/fs/inode.c
119 +++ b/fs/inode.c
120 @@ -1782,7 +1782,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 659a8f39c61af..1283670737826 100644
131 --- a/fs/namespace.c
132 +++ b/fs/namespace.c
133 @@ -808,6 +808,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/splice.c b/fs/splice.c
147 index 5dbce4dcc1a7d..3e6ba363b7775 100644
148 --- a/fs/splice.c
149 +++ b/fs/splice.c
150 @@ -759,8 +759,8 @@ static int warn_unsupported(struct file *file, const char *op)
151  /*
152   * Attempt to initiate a splice from pipe to file.
153   */
154 -static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
155 -                          loff_t *ppos, size_t len, unsigned int flags)
156 +long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
157 +                   loff_t *ppos, size_t len, unsigned int flags)
158  {
159         if (unlikely(!out->f_op->splice_write))
160                 return warn_unsupported(out, "write");
161 @@ -770,9 +770,9 @@ static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
162  /*
163   * Attempt to initiate a splice from a file to a pipe.
164   */
165 -static long do_splice_to(struct file *in, loff_t *ppos,
166 -                        struct pipe_inode_info *pipe, size_t len,
167 -                        unsigned int flags)
168 +long do_splice_to(struct file *in, loff_t *ppos,
169 +                 struct pipe_inode_info *pipe, size_t len,
170 +                 unsigned int flags)
171  {
172         unsigned int p_space;
173         int ret;
174 diff --git a/fs/sync.c b/fs/sync.c
175 index 1373a610dc784..b7b5a0a0df6ff 100644
176 --- a/fs/sync.c
177 +++ b/fs/sync.c
178 @@ -28,7 +28,7 @@
179   * wait == 1 case since in that case write_inode() functions do
180   * sync_dirty_buffer() and thus effectively write one block at a time.
181   */
182 -static int __sync_filesystem(struct super_block *sb, int wait)
183 +int __sync_filesystem(struct super_block *sb, int wait)
184  {
185         if (wait)
186                 sync_inodes_sb(sb);
187 diff --git a/include/linux/fs.h b/include/linux/fs.h
188 index e7a633353fd20..376ddf1965aa2 100644
189 --- a/include/linux/fs.h
190 +++ b/include/linux/fs.h
191 @@ -1381,6 +1381,7 @@ extern void fasync_free(struct fasync_struct *);
192  /* can be called from interrupts */
193  extern void kill_fasync(struct fasync_struct **, int, int);
194  
195 +extern int setfl(int fd, struct file *filp, unsigned long arg);
196  extern void __f_setown(struct file *filp, struct pid *, enum pid_type, int force);
197  extern int f_setown(struct file *filp, unsigned long arg, int force);
198  extern void f_delown(struct file *filp);
199 @@ -2092,6 +2093,7 @@ struct file_operations {
200         ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int);
201         unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
202         int (*check_flags)(int);
203 +       int (*setfl)(struct file *, unsigned long);
204         int (*flock) (struct file *, int, struct file_lock *);
205         ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, loff_t *, size_t, unsigned int);
206         ssize_t (*splice_read)(struct file *, loff_t *, struct pipe_inode_info *, size_t, unsigned int);
207 @@ -2613,6 +2615,7 @@ extern int current_umask(void);
208  extern void ihold(struct inode * inode);
209  extern void iput(struct inode *);
210  extern int generic_update_time(struct inode *, struct timespec64 *, int);
211 +extern int update_time(struct inode *, struct timespec64 *, int);
212  
213  /* /sys/fs */
214  extern struct kobject *fs_kobj;
215 @@ -2776,6 +2779,7 @@ static inline bool sb_is_blkdev_sb(struct super_block *sb)
216  }
217  
218  void emergency_thaw_all(void);
219 +extern int __sync_filesystem(struct super_block *, int);
220  extern int sync_filesystem(struct super_block *);
221  extern const struct file_operations def_blk_fops;
222  extern const struct file_operations def_chr_fops;
223 diff --git a/include/linux/lockdep.h b/include/linux/lockdep.h
224 index 9fe165beb0f9e..e47f7e15eeaf7 100644
225 --- a/include/linux/lockdep.h
226 +++ b/include/linux/lockdep.h
227 @@ -248,6 +248,8 @@ static inline int lockdep_match_key(struct lockdep_map *lock,
228         return lock->key == key;
229  }
230  
231 +struct lock_class *lockdep_hlock_class(struct held_lock *hlock);
232 +
233  /*
234   * Acquire a lock.
235   *
236 diff --git a/include/linux/mnt_namespace.h b/include/linux/mnt_namespace.h
237 index 8f882f5881e87..6b9808f098435 100644
238 --- a/include/linux/mnt_namespace.h
239 +++ b/include/linux/mnt_namespace.h
240 @@ -7,12 +7,15 @@ struct mnt_namespace;
241  struct fs_struct;
242  struct user_namespace;
243  struct ns_common;
244 +struct vfsmount;
245  
246  extern struct mnt_namespace *copy_mnt_ns(unsigned long, struct mnt_namespace *,
247                 struct user_namespace *, struct fs_struct *);
248  extern void put_mnt_ns(struct mnt_namespace *ns);
249  extern struct ns_common *from_mnt_ns(struct mnt_namespace *);
250  
251 +extern int is_current_mnt_ns(struct vfsmount *mnt);
252 +
253  extern const struct file_operations proc_mounts_operations;
254  extern const struct file_operations proc_mountinfo_operations;
255  extern const struct file_operations proc_mountstats_operations;
256 diff --git a/include/linux/splice.h b/include/linux/splice.h
257 index a55179fd60fc3..8e21c53cf8831 100644
258 --- a/include/linux/splice.h
259 +++ b/include/linux/splice.h
260 @@ -93,4 +93,10 @@ extern void splice_shrink_spd(struct splice_pipe_desc *);
261  
262  extern const struct pipe_buf_operations page_cache_pipe_buf_ops;
263  extern const struct pipe_buf_operations default_pipe_buf_ops;
264 +
265 +extern long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
266 +                          loff_t *ppos, size_t len, unsigned int flags);
267 +extern long do_splice_to(struct file *in, loff_t *ppos,
268 +                        struct pipe_inode_info *pipe, size_t len,
269 +                        unsigned int flags);
270  #endif
271 diff --git a/kernel/locking/lockdep.c b/kernel/locking/lockdep.c
272 index bf1c00c881e48..e0aaf34974df5 100644
273 --- a/kernel/locking/lockdep.c
274 +++ b/kernel/locking/lockdep.c
275 @@ -189,7 +189,7 @@ static
276  struct lock_class lock_classes[MAX_LOCKDEP_KEYS];
277  static DECLARE_BITMAP(lock_classes_in_use, MAX_LOCKDEP_KEYS);
278  
279 -static inline struct lock_class *hlock_class(struct held_lock *hlock)
280 +inline struct lock_class *lockdep_hlock_class(struct held_lock *hlock)
281  {
282         unsigned int class_idx = hlock->class_idx;
283  
284 @@ -210,6 +210,7 @@ static inline struct lock_class *hlock_class(struct held_lock *hlock)
285          */
286         return lock_classes + class_idx;
287  }
288 +#define hlock_class(hlock) lockdep_hlock_class(hlock)
289  
290  #ifdef CONFIG_LOCK_STAT
291  static DEFINE_PER_CPU(struct lock_class_stats[MAX_LOCKDEP_KEYS], cpu_lock_stats);
292 SPDX-License-Identifier: GPL-2.0
293 aufs5.x-rcN mmap patch
294
295 diff --git a/fs/proc/base.c b/fs/proc/base.c
296 index 533d5836eb9a4..f5de60c5327f5 100644
297 --- a/fs/proc/base.c
298 +++ b/fs/proc/base.c
299 @@ -2186,7 +2186,7 @@ static int map_files_get_link(struct dentry *dentry, struct path *path)
300         rc = -ENOENT;
301         vma = find_exact_vma(mm, vm_start, vm_end);
302         if (vma && vma->vm_file) {
303 -               *path = vma->vm_file->f_path;
304 +               *path = vma_pr_or_file(vma)->f_path;
305                 path_get(path);
306                 rc = 0;
307         }
308 diff --git a/fs/proc/nommu.c b/fs/proc/nommu.c
309 index 13452b32e2bd5..38acccfef9d49 100644
310 --- a/fs/proc/nommu.c
311 +++ b/fs/proc/nommu.c
312 @@ -40,7 +40,10 @@ static int nommu_region_show(struct seq_file *m, struct vm_region *region)
313         file = region->vm_file;
314  
315         if (file) {
316 -               struct inode *inode = file_inode(region->vm_file);
317 +               struct inode *inode;
318 +
319 +               file = vmr_pr_or_file(region);
320 +               inode = file_inode(file);
321                 dev = inode->i_sb->s_dev;
322                 ino = inode->i_ino;
323         }
324 diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c
325 index cf25be3e03212..70af1c25ffa22 100644
326 --- a/fs/proc/task_mmu.c
327 +++ b/fs/proc/task_mmu.c
328 @@ -280,7 +280,10 @@ show_map_vma(struct seq_file *m, struct vm_area_struct *vma)
329         const char *name = NULL;
330  
331         if (file) {
332 -               struct inode *inode = file_inode(vma->vm_file);
333 +               struct inode *inode;
334 +
335 +               file = vma_pr_or_file(vma);
336 +               inode = file_inode(file);
337                 dev = inode->i_sb->s_dev;
338                 ino = inode->i_ino;
339                 pgoff = ((loff_t)vma->vm_pgoff) << PAGE_SHIFT;
340 @@ -1865,7 +1868,7 @@ static int show_numa_map(struct seq_file *m, void *v)
341         struct proc_maps_private *proc_priv = &numa_priv->proc_maps;
342         struct vm_area_struct *vma = v;
343         struct numa_maps *md = &numa_priv->md;
344 -       struct file *file = vma->vm_file;
345 +       struct file *file = vma_pr_or_file(vma);
346         struct mm_struct *mm = vma->vm_mm;
347         struct mempolicy *pol;
348         char buffer[64];
349 diff --git a/fs/proc/task_nommu.c b/fs/proc/task_nommu.c
350 index a6d21fc0033c6..02c2de31196e0 100644
351 --- a/fs/proc/task_nommu.c
352 +++ b/fs/proc/task_nommu.c
353 @@ -155,7 +155,10 @@ static int nommu_vma_show(struct seq_file *m, struct vm_area_struct *vma)
354         file = vma->vm_file;
355  
356         if (file) {
357 -               struct inode *inode = file_inode(vma->vm_file);
358 +               struct inode *inode;
359 +
360 +               file = vma_pr_or_file(vma);
361 +               inode = file_inode(file);
362                 dev = inode->i_sb->s_dev;
363                 ino = inode->i_ino;
364                 pgoff = (loff_t)vma->vm_pgoff << PAGE_SHIFT;
365 diff --git a/include/linux/mm.h b/include/linux/mm.h
366 index 73a52aba448f9..5dd42acf0707b 100644
367 --- a/include/linux/mm.h
368 +++ b/include/linux/mm.h
369 @@ -1806,6 +1806,28 @@ static inline void unmap_shared_mapping_range(struct address_space *mapping,
370         unmap_mapping_range(mapping, holebegin, holelen, 0);
371  }
372  
373 +extern void vma_do_file_update_time(struct vm_area_struct *, const char[], int);
374 +extern struct file *vma_do_pr_or_file(struct vm_area_struct *, const char[],
375 +                                     int);
376 +extern void vma_do_get_file(struct vm_area_struct *, const char[], int);
377 +extern void vma_do_fput(struct vm_area_struct *, const char[], int);
378 +
379 +#define vma_file_update_time(vma)      vma_do_file_update_time(vma, __func__, \
380 +                                                               __LINE__)
381 +#define vma_pr_or_file(vma)            vma_do_pr_or_file(vma, __func__, \
382 +                                                         __LINE__)
383 +#define vma_get_file(vma)              vma_do_get_file(vma, __func__, __LINE__)
384 +#define vma_fput(vma)                  vma_do_fput(vma, __func__, __LINE__)
385 +
386 +#ifndef CONFIG_MMU
387 +extern struct file *vmr_do_pr_or_file(struct vm_region *, const char[], int);
388 +extern void vmr_do_fput(struct vm_region *, const char[], int);
389 +
390 +#define vmr_pr_or_file(region)         vmr_do_pr_or_file(region, __func__, \
391 +                                                         __LINE__)
392 +#define vmr_fput(region)               vmr_do_fput(region, __func__, __LINE__)
393 +#endif /* !CONFIG_MMU */
394 +
395  extern int access_process_vm(struct task_struct *tsk, unsigned long addr,
396                 void *buf, int len, unsigned int gup_flags);
397  extern int access_remote_vm(struct mm_struct *mm, unsigned long addr,
398 diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h
399 index 7f8ee09c711f4..3a9a798a4ae1f 100644
400 --- a/include/linux/mm_types.h
401 +++ b/include/linux/mm_types.h
402 @@ -294,6 +294,7 @@ struct vm_region {
403         unsigned long   vm_top;         /* region allocated to here */
404         unsigned long   vm_pgoff;       /* the offset in vm_file corresponding to vm_start */
405         struct file     *vm_file;       /* the backing file or NULL */
406 +       struct file     *vm_prfile;     /* the virtual backing file or NULL */
407  
408         int             vm_usage;       /* region usage count (access under nommu_region_sem) */
409         bool            vm_icache_flushed : 1; /* true if the icache has been flushed for
410 @@ -373,6 +374,7 @@ struct vm_area_struct {
411         unsigned long vm_pgoff;         /* Offset (within vm_file) in PAGE_SIZE
412                                            units */
413         struct file * vm_file;          /* File we map to (can be NULL). */
414 +       struct file *vm_prfile;         /* shadow of vm_file */
415         void * vm_private_data;         /* was vm_pte (shared mem) */
416  
417  #ifdef CONFIG_SWAP
418 diff --git a/kernel/fork.c b/kernel/fork.c
419 index 38681ad44c76b..95de8e9843192 100644
420 --- a/kernel/fork.c
421 +++ b/kernel/fork.c
422 @@ -573,7 +573,7 @@ static __latent_entropy int dup_mmap(struct mm_struct *mm,
423                 if (file) {
424                         struct address_space *mapping = file->f_mapping;
425  
426 -                       get_file(file);
427 +                       vma_get_file(tmp);
428                         i_mmap_lock_write(mapping);
429                         if (tmp->vm_flags & VM_SHARED)
430                                 mapping_allow_writable(mapping);
431 diff --git a/mm/Makefile b/mm/Makefile
432 index fc60a40ce954b..c715b0138237b 100644
433 --- a/mm/Makefile
434 +++ b/mm/Makefile
435 @@ -52,7 +52,7 @@ obj-y                 := filemap.o mempool.o oom_kill.o fadvise.o \
436                            mm_init.o percpu.o slab_common.o \
437                            compaction.o vmacache.o \
438                            interval_tree.o list_lru.o workingset.o \
439 -                          debug.o gup.o mmap_lock.o $(mmu-y)
440 +                          prfile.o debug.o gup.o mmap_lock.o $(mmu-y)
441  
442  # Give 'page_alloc' its own module-parameter namespace
443  page-alloc-y := page_alloc.o
444 diff --git a/mm/filemap.c b/mm/filemap.c
445 index dae481293b5d9..52bf3bf87b757 100644
446 --- a/mm/filemap.c
447 +++ b/mm/filemap.c
448 @@ -3350,7 +3350,7 @@ vm_fault_t filemap_page_mkwrite(struct vm_fault *vmf)
449         vm_fault_t ret = VM_FAULT_LOCKED;
450  
451         sb_start_pagefault(mapping->host->i_sb);
452 -       file_update_time(vmf->vma->vm_file);
453 +       vma_file_update_time(vmf->vma);
454         lock_page(page);
455         if (page->mapping != mapping) {
456                 unlock_page(page);
457 diff --git a/mm/mmap.c b/mm/mmap.c
458 index 88dcc5c252255..6c276614ca962 100644
459 --- a/mm/mmap.c
460 +++ b/mm/mmap.c
461 @@ -183,7 +183,7 @@ static struct vm_area_struct *remove_vma(struct vm_area_struct *vma)
462         if (vma->vm_ops && vma->vm_ops->close)
463                 vma->vm_ops->close(vma);
464         if (vma->vm_file)
465 -               fput(vma->vm_file);
466 +               vma_fput(vma);
467         mpol_put(vma_policy(vma));
468         vm_area_free(vma);
469         return next;
470 @@ -952,7 +952,7 @@ int __vma_adjust(struct vm_area_struct *vma, unsigned long start,
471         if (remove_next) {
472                 if (file) {
473                         uprobe_munmap(next, next->vm_start, next->vm_end);
474 -                       fput(file);
475 +                       vma_fput(vma);
476                 }
477                 if (next->anon_vma)
478                         anon_vma_merge(vma, next);
479 @@ -1873,7 +1873,7 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
480         return addr;
481  
482  unmap_and_free_vma:
483 -       fput(vma->vm_file);
484 +       vma_fput(vma);
485         vma->vm_file = NULL;
486  
487         /* Undo any partial mapping done by a device driver. */
488 @@ -2731,7 +2731,7 @@ int __split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
489                 goto out_free_mpol;
490  
491         if (new->vm_file)
492 -               get_file(new->vm_file);
493 +               vma_get_file(new);
494  
495         if (new->vm_ops && new->vm_ops->open)
496                 new->vm_ops->open(new);
497 @@ -2750,7 +2750,7 @@ int __split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
498         if (new->vm_ops && new->vm_ops->close)
499                 new->vm_ops->close(new);
500         if (new->vm_file)
501 -               fput(new->vm_file);
502 +               vma_fput(new);
503         unlink_anon_vmas(new);
504   out_free_mpol:
505         mpol_put(vma_policy(new));
506 @@ -2945,7 +2945,7 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
507         struct vm_area_struct *vma;
508         unsigned long populate = 0;
509         unsigned long ret = -EINVAL;
510 -       struct file *file;
511 +       struct file *file, *prfile;
512  
513         pr_warn_once("%s (%d) uses deprecated remap_file_pages() syscall. See Documentation/vm/remap_file_pages.rst.\n",
514                      current->comm, current->pid);
515 @@ -3001,10 +3001,27 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
516         if (vma->vm_flags & VM_LOCKED)
517                 flags |= MAP_LOCKED;
518  
519 -       file = get_file(vma->vm_file);
520 +       vma_get_file(vma);
521 +       file = vma->vm_file;
522 +       prfile = vma->vm_prfile;
523         ret = do_mmap(vma->vm_file, start, size,
524                         prot, flags, pgoff, &populate, NULL);
525 +       if (!IS_ERR_VALUE(ret) && file && prfile) {
526 +               struct vm_area_struct *new_vma;
527 +
528 +               new_vma = find_vma(mm, ret);
529 +               if (!new_vma->vm_prfile)
530 +                       new_vma->vm_prfile = prfile;
531 +               if (new_vma != vma)
532 +                       get_file(prfile);
533 +       }
534 +       /*
535 +        * two fput()s instead of vma_fput(vma),
536 +        * coz vma may not be available anymore.
537 +        */
538         fput(file);
539 +       if (prfile)
540 +               fput(prfile);
541  out:
542         mmap_write_unlock(mm);
543         if (populate)
544 @@ -3285,7 +3302,7 @@ struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,
545                 if (anon_vma_clone(new_vma, vma))
546                         goto out_free_mempol;
547                 if (new_vma->vm_file)
548 -                       get_file(new_vma->vm_file);
549 +                       vma_get_file(new_vma);
550                 if (new_vma->vm_ops && new_vma->vm_ops->open)
551                         new_vma->vm_ops->open(new_vma);
552                 vma_link(mm, new_vma, prev, rb_link, rb_parent);
553 diff --git a/mm/nommu.c b/mm/nommu.c
554 index 02d2427b8f9e8..a7419302ab4ed 100644
555 --- a/mm/nommu.c
556 +++ b/mm/nommu.c
557 @@ -523,7 +523,7 @@ static void __put_nommu_region(struct vm_region *region)
558                 up_write(&nommu_region_sem);
559  
560                 if (region->vm_file)
561 -                       fput(region->vm_file);
562 +                       vmr_fput(region);
563  
564                 /* IO memory and memory shared directly out of the pagecache
565                  * from ramfs/tmpfs mustn't be released here */
566 @@ -655,7 +655,7 @@ static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma)
567         if (vma->vm_ops && vma->vm_ops->close)
568                 vma->vm_ops->close(vma);
569         if (vma->vm_file)
570 -               fput(vma->vm_file);
571 +               vma_fput(vma);
572         put_nommu_region(vma->vm_region);
573         vm_area_free(vma);
574  }
575 @@ -1175,7 +1175,7 @@ unsigned long do_mmap(struct file *file,
576                                         goto error_just_free;
577                                 }
578                         }
579 -                       fput(region->vm_file);
580 +                       vmr_fput(region);
581                         kmem_cache_free(vm_region_jar, region);
582                         region = pregion;
583                         result = start;
584 @@ -1252,10 +1252,10 @@ unsigned long do_mmap(struct file *file,
585         up_write(&nommu_region_sem);
586  error:
587         if (region->vm_file)
588 -               fput(region->vm_file);
589 +               vmr_fput(region);
590         kmem_cache_free(vm_region_jar, region);
591         if (vma->vm_file)
592 -               fput(vma->vm_file);
593 +               vma_fput(vma);
594         vm_area_free(vma);
595         return ret;
596  
597 diff --git a/mm/prfile.c b/mm/prfile.c
598 new file mode 100644
599 index 0000000000000..511543ab1b418
600 --- /dev/null
601 +++ b/mm/prfile.c
602 @@ -0,0 +1,86 @@
603 +// SPDX-License-Identifier: GPL-2.0
604 +/*
605 + * Mainly for aufs which mmap(2) different file and wants to print different
606 + * path in /proc/PID/maps.
607 + * Call these functions via macros defined in linux/mm.h.
608 + *
609 + * See Documentation/filesystems/aufs/design/06mmap.txt
610 + *
611 + * Copyright (c) 2014-2021 Junjro R. Okajima
612 + * Copyright (c) 2014 Ian Campbell
613 + */
614 +
615 +#include <linux/mm.h>
616 +#include <linux/file.h>
617 +#include <linux/fs.h>
618 +
619 +/* #define PRFILE_TRACE */
620 +static inline void prfile_trace(struct file *f, struct file *pr,
621 +                             const char func[], int line, const char func2[])
622 +{
623 +#ifdef PRFILE_TRACE
624 +       if (pr)
625 +               pr_info("%s:%d: %s, %pD2\n", func, line, func2, f);
626 +#endif
627 +}
628 +
629 +void vma_do_file_update_time(struct vm_area_struct *vma, const char func[],
630 +                            int line)
631 +{
632 +       struct file *f = vma->vm_file, *pr = vma->vm_prfile;
633 +
634 +       prfile_trace(f, pr, func, line, __func__);
635 +       file_update_time(f);
636 +       if (f && pr)
637 +               file_update_time(pr);
638 +}
639 +
640 +struct file *vma_do_pr_or_file(struct vm_area_struct *vma, const char func[],
641 +                              int line)
642 +{
643 +       struct file *f = vma->vm_file, *pr = vma->vm_prfile;
644 +
645 +       prfile_trace(f, pr, func, line, __func__);
646 +       return (f && pr) ? pr : f;
647 +}
648 +
649 +void vma_do_get_file(struct vm_area_struct *vma, const char func[], int line)
650 +{
651 +       struct file *f = vma->vm_file, *pr = vma->vm_prfile;
652 +
653 +       prfile_trace(f, pr, func, line, __func__);
654 +       get_file(f);
655 +       if (f && pr)
656 +               get_file(pr);
657 +}
658 +
659 +void vma_do_fput(struct vm_area_struct *vma, const char func[], int line)
660 +{
661 +       struct file *f = vma->vm_file, *pr = vma->vm_prfile;
662 +
663 +       prfile_trace(f, pr, func, line, __func__);
664 +       fput(f);
665 +       if (f && pr)
666 +               fput(pr);
667 +}
668 +
669 +#ifndef CONFIG_MMU
670 +struct file *vmr_do_pr_or_file(struct vm_region *region, const char func[],
671 +                              int line)
672 +{
673 +       struct file *f = region->vm_file, *pr = region->vm_prfile;
674 +
675 +       prfile_trace(f, pr, func, line, __func__);
676 +       return (f && pr) ? pr : f;
677 +}
678 +
679 +void vmr_do_fput(struct vm_region *region, const char func[], int line)
680 +{
681 +       struct file *f = region->vm_file, *pr = region->vm_prfile;
682 +
683 +       prfile_trace(f, pr, func, line, __func__);
684 +       fput(f);
685 +       if (f && pr)
686 +               fput(pr);
687 +}
688 +#endif /* !CONFIG_MMU */
689 SPDX-License-Identifier: GPL-2.0
690 aufs5.x-rcN standalone patch
691
692 diff --git a/fs/dcache.c b/fs/dcache.c
693 index bc5095b734f58..9508bd57a3bc0 100644
694 --- a/fs/dcache.c
695 +++ b/fs/dcache.c
696 @@ -1425,6 +1425,7 @@ void d_walk(struct dentry *parent, void *data,
697         seq = 1;
698         goto again;
699  }
700 +EXPORT_SYMBOL_GPL(d_walk);
701  
702  struct check_mount {
703         struct vfsmount *mnt;
704 @@ -2970,6 +2971,7 @@ void d_exchange(struct dentry *dentry1, struct dentry *dentry2)
705  
706         write_sequnlock(&rename_lock);
707  }
708 +EXPORT_SYMBOL_GPL(d_exchange);
709  
710  /**
711   * d_ancestor - search for an ancestor
712 diff --git a/fs/exec.c b/fs/exec.c
713 index a098c133d8d74..7b6a249fd4778 100644
714 --- a/fs/exec.c
715 +++ b/fs/exec.c
716 @@ -111,6 +111,7 @@ bool path_noexec(const struct path *path)
717         return (path->mnt->mnt_flags & MNT_NOEXEC) ||
718                (path->mnt->mnt_sb->s_iflags & SB_I_NOEXEC);
719  }
720 +EXPORT_SYMBOL_GPL(path_noexec);
721  
722  #ifdef CONFIG_USELIB
723  /*
724 diff --git a/fs/fcntl.c b/fs/fcntl.c
725 index 02382fa9bd341..3418c60b90146 100644
726 --- a/fs/fcntl.c
727 +++ b/fs/fcntl.c
728 @@ -86,6 +86,7 @@ int setfl(int fd, struct file *filp, unsigned long arg)
729   out:
730         return error;
731  }
732 +EXPORT_SYMBOL_GPL(setfl);
733  
734  static void f_modown(struct file *filp, struct pid *pid, enum pid_type type,
735                       int force)
736 diff --git a/fs/file_table.c b/fs/file_table.c
737 index 45437f8e1003e..786af52904fcf 100644
738 --- a/fs/file_table.c
739 +++ b/fs/file_table.c
740 @@ -161,6 +161,7 @@ struct file *alloc_empty_file(int flags, const struct cred *cred)
741         }
742         return ERR_PTR(-ENFILE);
743  }
744 +EXPORT_SYMBOL_GPL(alloc_empty_file);
745  
746  /*
747   * Variant of alloc_empty_file() that doesn't check and modify nr_files.
748 @@ -375,6 +376,7 @@ void __fput_sync(struct file *file)
749  }
750  
751  EXPORT_SYMBOL(fput);
752 +EXPORT_SYMBOL_GPL(__fput_sync);
753  
754  void __init files_init(void)
755  {
756 diff --git a/fs/inode.c b/fs/inode.c
757 index 7e53985e62e5e..e36c5b2f96df7 100644
758 --- a/fs/inode.c
759 +++ b/fs/inode.c
760 @@ -1788,6 +1788,7 @@ int update_time(struct inode *inode, struct timespec64 *time, int flags)
761                 return inode->i_op->update_time(inode, time, flags);
762         return generic_update_time(inode, time, flags);
763  }
764 +EXPORT_SYMBOL_GPL(update_time);
765  
766  /**
767   *     atime_needs_update      -       update the access time
768 diff --git a/fs/namespace.c b/fs/namespace.c
769 index 1283670737826..db99365620118 100644
770 --- a/fs/namespace.c
771 +++ b/fs/namespace.c
772 @@ -439,6 +439,7 @@ void __mnt_drop_write(struct vfsmount *mnt)
773         mnt_dec_writers(real_mount(mnt));
774         preempt_enable();
775  }
776 +EXPORT_SYMBOL_GPL(__mnt_drop_write);
777  
778  /**
779   * mnt_drop_write - give up write access to a mount
780 @@ -813,6 +814,7 @@ int is_current_mnt_ns(struct vfsmount *mnt)
781  {
782         return check_mnt(real_mount(mnt));
783  }
784 +EXPORT_SYMBOL_GPL(is_current_mnt_ns);
785  
786  /*
787   * vfsmount lock must be held for write
788 @@ -2011,6 +2013,7 @@ int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg,
789         }
790         return 0;
791  }
792 +EXPORT_SYMBOL_GPL(iterate_mounts);
793  
794  static void lock_mnt_tree(struct mount *mnt)
795  {
796 diff --git a/fs/notify/group.c b/fs/notify/group.c
797 index fb89c351295d6..460ad19c2570a 100644
798 --- a/fs/notify/group.c
799 +++ b/fs/notify/group.c
800 @@ -100,6 +100,7 @@ void fsnotify_get_group(struct fsnotify_group *group)
801  {
802         refcount_inc(&group->refcnt);
803  }
804 +EXPORT_SYMBOL_GPL(fsnotify_get_group);
805  
806  /*
807   * Drop a reference to a group.  Free it if it's through.
808 diff --git a/fs/open.c b/fs/open.c
809 index daa324606a41f..0d8bb2f1922a3 100644
810 --- a/fs/open.c
811 +++ b/fs/open.c
812 @@ -65,6 +65,7 @@ int do_truncate(struct user_namespace *mnt_userns, struct dentry *dentry,
813         inode_unlock(dentry->d_inode);
814         return ret;
815  }
816 +EXPORT_SYMBOL_GPL(do_truncate);
817  
818  long vfs_truncate(const struct path *path, loff_t length)
819  {
820 diff --git a/fs/read_write.c b/fs/read_write.c
821 index af057c57bdc64..76017f8331fbf 100644
822 --- a/fs/read_write.c
823 +++ b/fs/read_write.c
824 @@ -492,6 +492,7 @@ ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos)
825         inc_syscr(current);
826         return ret;
827  }
828 +EXPORT_SYMBOL_GPL(vfs_read);
829  
830  static ssize_t new_sync_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos)
831  {
832 @@ -602,6 +603,7 @@ ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_
833         file_end_write(file);
834         return ret;
835  }
836 +EXPORT_SYMBOL_GPL(vfs_write);
837  
838  /* file_ppos returns &file->f_pos or NULL if file is stream */
839  static inline loff_t *file_ppos(struct file *file)
840 diff --git a/fs/splice.c b/fs/splice.c
841 index 3e6ba363b7775..7c1be373eb7cd 100644
842 --- a/fs/splice.c
843 +++ b/fs/splice.c
844 @@ -766,6 +766,7 @@ long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
845                 return warn_unsupported(out, "write");
846         return out->f_op->splice_write(pipe, out, ppos, len, flags);
847  }
848 +EXPORT_SYMBOL_GPL(do_splice_from);
849  
850  /*
851   * Attempt to initiate a splice from a file to a pipe.
852 @@ -795,6 +796,7 @@ long do_splice_to(struct file *in, loff_t *ppos,
853                 return warn_unsupported(in, "read");
854         return in->f_op->splice_read(in, ppos, pipe, len, flags);
855  }
856 +EXPORT_SYMBOL_GPL(do_splice_to);
857  
858  /**
859   * splice_direct_to_actor - splices data directly between two non-pipes
860 diff --git a/fs/sync.c b/fs/sync.c
861 index b7b5a0a0df6ff..fa5c7fba7f1ba 100644
862 --- a/fs/sync.c
863 +++ b/fs/sync.c
864 @@ -39,6 +39,7 @@ int __sync_filesystem(struct super_block *sb, int wait)
865                 sb->s_op->sync_fs(sb, wait);
866         return __sync_blockdev(sb->s_bdev, wait);
867  }
868 +EXPORT_SYMBOL_GPL(__sync_filesystem);
869  
870  /*
871   * Write out and wait upon all dirty data associated with this
872 diff --git a/fs/xattr.c b/fs/xattr.c
873 index 5c8c5175b385c..ff7e9ff774b73 100644
874 --- a/fs/xattr.c
875 +++ b/fs/xattr.c
876 @@ -384,6 +384,7 @@ vfs_getxattr_alloc(struct user_namespace *mnt_userns, struct dentry *dentry,
877         *xattr_value = value;
878         return error;
879  }
880 +EXPORT_SYMBOL_GPL(vfs_getxattr_alloc);
881  
882  ssize_t
883  __vfs_getxattr(struct dentry *dentry, struct inode *inode, const char *name,
884 diff --git a/kernel/locking/lockdep.c b/kernel/locking/lockdep.c
885 index e0aaf34974df5..d7b737731d4ef 100644
886 --- a/kernel/locking/lockdep.c
887 +++ b/kernel/locking/lockdep.c
888 @@ -210,6 +210,7 @@ inline struct lock_class *lockdep_hlock_class(struct held_lock *hlock)
889          */
890         return lock_classes + class_idx;
891  }
892 +EXPORT_SYMBOL_GPL(lockdep_hlock_class);
893  #define hlock_class(hlock) lockdep_hlock_class(hlock)
894  
895  #ifdef CONFIG_LOCK_STAT
896 diff --git a/kernel/task_work.c b/kernel/task_work.c
897 index 1698fbe6f0e13..081b05acadf82 100644
898 --- a/kernel/task_work.c
899 +++ b/kernel/task_work.c
900 @@ -167,3 +167,4 @@ void task_work_run(void)
901                 } while (work);
902         }
903  }
904 +EXPORT_SYMBOL_GPL(task_work_run);
905 diff --git a/security/security.c b/security/security.c
906 index 9ffa9e9c5c554..83b533a3fb8e8 100644
907 --- a/security/security.c
908 +++ b/security/security.c
909 @@ -1147,6 +1147,7 @@ int security_path_rmdir(const struct path *dir, struct dentry *dentry)
910                 return 0;
911         return call_int_hook(path_rmdir, 0, dir, dentry);
912  }
913 +EXPORT_SYMBOL_GPL(security_path_rmdir);
914  
915  int security_path_unlink(const struct path *dir, struct dentry *dentry)
916  {
917 @@ -1163,6 +1164,7 @@ int security_path_symlink(const struct path *dir, struct dentry *dentry,
918                 return 0;
919         return call_int_hook(path_symlink, 0, dir, dentry, old_name);
920  }
921 +EXPORT_SYMBOL_GPL(security_path_symlink);
922  
923  int security_path_link(struct dentry *old_dentry, const struct path *new_dir,
924                        struct dentry *new_dentry)
925 @@ -1171,6 +1173,7 @@ int security_path_link(struct dentry *old_dentry, const struct path *new_dir,
926                 return 0;
927         return call_int_hook(path_link, 0, old_dentry, new_dir, new_dentry);
928  }
929 +EXPORT_SYMBOL_GPL(security_path_link);
930  
931  int security_path_rename(const struct path *old_dir, struct dentry *old_dentry,
932                          const struct path *new_dir, struct dentry *new_dentry,
933 @@ -1198,6 +1201,7 @@ int security_path_truncate(const struct path *path)
934                 return 0;
935         return call_int_hook(path_truncate, 0, path);
936  }
937 +EXPORT_SYMBOL_GPL(security_path_truncate);
938  
939  int security_path_chmod(const struct path *path, umode_t mode)
940  {
941 @@ -1205,6 +1209,7 @@ int security_path_chmod(const struct path *path, umode_t mode)
942                 return 0;
943         return call_int_hook(path_chmod, 0, path, mode);
944  }
945 +EXPORT_SYMBOL_GPL(security_path_chmod);
946  
947  int security_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
948  {
949 @@ -1212,6 +1217,7 @@ int security_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
950                 return 0;
951         return call_int_hook(path_chown, 0, path, uid, gid);
952  }
953 +EXPORT_SYMBOL_GPL(security_path_chown);
954  
955  int security_path_chroot(const struct path *path)
956  {
957 @@ -1312,6 +1318,7 @@ int security_inode_permission(struct inode *inode, int mask)
958                 return 0;
959         return call_int_hook(inode_permission, 0, inode, mask);
960  }
961 +EXPORT_SYMBOL_GPL(security_inode_permission);
962  
963  int security_inode_setattr(struct dentry *dentry, struct iattr *attr)
964  {
965 @@ -1509,6 +1516,7 @@ int security_file_permission(struct file *file, int mask)
966  
967         return fsnotify_perm(file, mask);
968  }
969 +EXPORT_SYMBOL_GPL(security_file_permission);
970  
971  int security_file_alloc(struct file *file)
972  {
973 diff -urN /usr/share/empty/Documentation/ABI/testing/debugfs-aufs linux/Documentation/ABI/testing/debugfs-aufs
974 --- /usr/share/empty/Documentation/ABI/testing/debugfs-aufs     1970-01-01 01:00:00.000000000 +0100
975 +++ linux/Documentation/ABI/testing/debugfs-aufs        2021-11-01 23:48:34.199692595 +0100
976 @@ -0,0 +1,55 @@
977 +What:          /debug/aufs/si_<id>/
978 +Date:          March 2009
979 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
980 +Description:
981 +               Under /debug/aufs, a directory named si_<id> is created
982 +               per aufs mount, where <id> is a unique id generated
983 +               internally.
984 +
985 +What:          /debug/aufs/si_<id>/plink
986 +Date:          Apr 2013
987 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
988 +Description:
989 +               It has three lines and shows the information about the
990 +               pseudo-link. The first line is a single number
991 +               representing a number of buckets. The second line is a
992 +               number of pseudo-links per buckets (separated by a
993 +               blank). The last line is a single number representing a
994 +               total number of psedo-links.
995 +               When the aufs mount option 'noplink' is specified, it
996 +               will show "1\n0\n0\n".
997 +
998 +What:          /debug/aufs/si_<id>/xib
999 +Date:          March 2009
1000 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1001 +Description:
1002 +               It shows the consumed blocks by xib (External Inode Number
1003 +               Bitmap), its block size and file size.
1004 +               When the aufs mount option 'noxino' is specified, it
1005 +               will be empty. About XINO files, see the aufs manual.
1006 +
1007 +What:          /debug/aufs/si_<id>/xi<branch-index>
1008 +Date:          March 2009
1009 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1010 +Description:
1011 +               It shows the consumed blocks by xino (External Inode Number
1012 +               Translation Table), its link count, block size and file
1013 +               size.
1014 +               Due to the file size limit, there may exist multiple
1015 +               xino files per branch.  In this case, "-N" is added to
1016 +               the filename and it corresponds to the index of the
1017 +               internal xino array.  "-0" is omitted.
1018 +               When the aufs mount option 'noxino' is specified, Those
1019 +               entries won't exist.  About XINO files, see the aufs
1020 +               manual.
1021 +
1022 +What:          /debug/aufs/si_<id>/xigen
1023 +Date:          March 2009
1024 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1025 +Description:
1026 +               It shows the consumed blocks by xigen (External Inode
1027 +               Generation Table), its block size and file size.
1028 +               If CONFIG_AUFS_EXPORT is disabled, this entry will not
1029 +               be created.
1030 +               When the aufs mount option 'noxino' is specified, it
1031 +               will be empty. About XINO files, see the aufs manual.
1032 diff -urN /usr/share/empty/Documentation/ABI/testing/sysfs-aufs linux/Documentation/ABI/testing/sysfs-aufs
1033 --- /usr/share/empty/Documentation/ABI/testing/sysfs-aufs       1970-01-01 01:00:00.000000000 +0100
1034 +++ linux/Documentation/ABI/testing/sysfs-aufs  2021-11-01 23:48:34.199692595 +0100
1035 @@ -0,0 +1,31 @@
1036 +What:          /sys/fs/aufs/si_<id>/
1037 +Date:          March 2009
1038 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1039 +Description:
1040 +               Under /sys/fs/aufs, a directory named si_<id> is created
1041 +               per aufs mount, where <id> is a unique id generated
1042 +               internally.
1043 +
1044 +What:          /sys/fs/aufs/si_<id>/br<idx>
1045 +Date:          March 2009
1046 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1047 +Description:
1048 +               It shows the abolute path of a member directory (which
1049 +               is called branch) in aufs, and its permission.
1050 +
1051 +What:          /sys/fs/aufs/si_<id>/brid<idx>
1052 +Date:          July 2013
1053 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1054 +Description:
1055 +               It shows the id of a member directory (which is called
1056 +               branch) in aufs.
1057 +
1058 +What:          /sys/fs/aufs/si_<id>/xi_path
1059 +Date:          March 2009
1060 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1061 +Description:
1062 +               It shows the abolute path of XINO (External Inode Number
1063 +               Bitmap, Translation Table and Generation Table) file
1064 +               even if it is the default path.
1065 +               When the aufs mount option 'noxino' is specified, it
1066 +               will be empty. About XINO files, see the aufs manual.
1067 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/01intro.txt linux/Documentation/filesystems/aufs/design/01intro.txt
1068 --- /usr/share/empty/Documentation/filesystems/aufs/design/01intro.txt  1970-01-01 01:00:00.000000000 +0100
1069 +++ linux/Documentation/filesystems/aufs/design/01intro.txt     2021-11-01 23:48:34.199692595 +0100
1070 @@ -0,0 +1,171 @@
1071 +
1072 +# Copyright (C) 2005-2021 Junjiro R. Okajima
1073 +# 
1074 +# This program is free software; you can redistribute it and/or modify
1075 +# it under the terms of the GNU General Public License as published by
1076 +# the Free Software Foundation; either version 2 of the License, or
1077 +# (at your option) any later version.
1078 +# 
1079 +# This program is distributed in the hope that it will be useful,
1080 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1081 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1082 +# GNU General Public License for more details.
1083 +# 
1084 +# You should have received a copy of the GNU General Public License
1085 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1086 +
1087 +Introduction
1088 +----------------------------------------
1089 +
1090 +aufs [ei ju: ef es] | /ey-yoo-ef-es/ | [a u f s]
1091 +1. abbrev. for "advanced multi-layered unification filesystem".
1092 +2. abbrev. for "another unionfs".
1093 +3. abbrev. for "auf das" in German which means "on the" in English.
1094 +   Ex. "Butter aufs Brot"(G) means "butter onto bread"(E).
1095 +   But "Filesystem aufs Filesystem" is hard to understand.
1096 +4. abbrev. for "African Urban Fashion Show".
1097 +
1098 +AUFS is a filesystem with features:
1099 +- multi layered stackable unification filesystem, the member directory
1100 +  is called as a branch.
1101 +- branch permission and attribute, 'readonly', 'real-readonly',
1102 +  'readwrite', 'whiteout-able', 'link-able whiteout', etc. and their
1103 +  combination.
1104 +- internal "file copy-on-write".
1105 +- logical deletion, whiteout.
1106 +- dynamic branch manipulation, adding, deleting and changing permission.
1107 +- allow bypassing aufs, user's direct branch access.
1108 +- external inode number translation table and bitmap which maintains the
1109 +  persistent aufs inode number.
1110 +- seekable directory, including NFS readdir.
1111 +- file mapping, mmap and sharing pages.
1112 +- pseudo-link, hardlink over branches.
1113 +- loopback mounted filesystem as a branch.
1114 +- several policies to select one among multiple writable branches.
1115 +- revert a single systemcall when an error occurs in aufs.
1116 +- and more...
1117 +
1118 +
1119 +Multi Layered Stackable Unification Filesystem
1120 +----------------------------------------------------------------------
1121 +Most people already knows what it is.
1122 +It is a filesystem which unifies several directories and provides a
1123 +merged single directory. When users access a file, the access will be
1124 +passed/re-directed/converted (sorry, I am not sure which English word is
1125 +correct) to the real file on the member filesystem. The member
1126 +filesystem is called 'lower filesystem' or 'branch' and has a mode
1127 +'readonly' and 'readwrite.' And the deletion for a file on the lower
1128 +readonly branch is handled by creating 'whiteout' on the upper writable
1129 +branch.
1130 +
1131 +On LKML, there have been discussions about UnionMount (Jan Blunck,
1132 +Bharata B Rao and Valerie Aurora) and Unionfs (Erez Zadok). They took
1133 +different approaches to implement the merged-view.
1134 +The former tries putting it into VFS, and the latter implements as a
1135 +separate filesystem.
1136 +(If I misunderstand about these implementations, please let me know and
1137 +I shall correct it. Because it is a long time ago when I read their
1138 +source files last time).
1139 +
1140 +UnionMount's approach will be able to small, but may be hard to share
1141 +branches between several UnionMount since the whiteout in it is
1142 +implemented in the inode on branch filesystem and always
1143 +shared. According to Bharata's post, readdir does not seems to be
1144 +finished yet.
1145 +There are several missing features known in this implementations such as
1146 +- for users, the inode number may change silently. eg. copy-up.
1147 +- link(2) may break by copy-up.
1148 +- read(2) may get an obsoleted filedata (fstat(2) too).
1149 +- fcntl(F_SETLK) may be broken by copy-up.
1150 +- unnecessary copy-up may happen, for example mmap(MAP_PRIVATE) after
1151 +  open(O_RDWR).
1152 +
1153 +In linux-3.18, "overlay" filesystem (formerly known as "overlayfs") was
1154 +merged into mainline. This is another implementation of UnionMount as a
1155 +separated filesystem. All the limitations and known problems which
1156 +UnionMount are equally inherited to "overlay" filesystem.
1157 +
1158 +Unionfs has a longer history. When I started implementing a stackable
1159 +filesystem (Aug 2005), it already existed. It has virtual super_block,
1160 +inode, dentry and file objects and they have an array pointing lower
1161 +same kind objects. After contributing many patches for Unionfs, I
1162 +re-started my project AUFS (Jun 2006).
1163 +
1164 +In AUFS, the structure of filesystem resembles to Unionfs, but I
1165 +implemented my own ideas, approaches and enhancements and it became
1166 +totally different one.
1167 +
1168 +Comparing DM snapshot and fs based implementation
1169 +- the number of bytes to be copied between devices is much smaller.
1170 +- the type of filesystem must be one and only.
1171 +- the fs must be writable, no readonly fs, even for the lower original
1172 +  device. so the compression fs will not be usable. but if we use
1173 +  loopback mount, we may address this issue.
1174 +  for instance,
1175 +       mount /cdrom/squashfs.img /sq
1176 +       losetup /sq/ext2.img
1177 +       losetup /somewhere/cow
1178 +       dmsetup "snapshot /dev/loop0 /dev/loop1 ..."
1179 +- it will be difficult (or needs more operations) to extract the
1180 +  difference between the original device and COW.
1181 +- DM snapshot-merge may help a lot when users try merging. in the
1182 +  fs-layer union, users will use rsync(1).
1183 +
1184 +You may want to read my old paper "Filesystems in LiveCD"
1185 +(http://aufs.sourceforge.net/aufs2/report/sq/sq.pdf).
1186 +
1187 +
1188 +Several characters/aspects/persona of aufs
1189 +----------------------------------------------------------------------
1190 +
1191 +Aufs has several characters, aspects or persona.
1192 +1. a filesystem, callee of VFS helper
1193 +2. sub-VFS, caller of VFS helper for branches
1194 +3. a virtual filesystem which maintains persistent inode number
1195 +4. reader/writer of files on branches such like an application
1196 +
1197 +1. Callee of VFS Helper
1198 +As an ordinary linux filesystem, aufs is a callee of VFS. For instance,
1199 +unlink(2) from an application reaches sys_unlink() kernel function and
1200 +then vfs_unlink() is called. vfs_unlink() is one of VFS helper and it
1201 +calls filesystem specific unlink operation. Actually aufs implements the
1202 +unlink operation but it behaves like a redirector.
1203 +
1204 +2. Caller of VFS Helper for Branches
1205 +aufs_unlink() passes the unlink request to the branch filesystem as if
1206 +it were called from VFS. So the called unlink operation of the branch
1207 +filesystem acts as usual. As a caller of VFS helper, aufs should handle
1208 +every necessary pre/post operation for the branch filesystem.
1209 +- acquire the lock for the parent dir on a branch
1210 +- lookup in a branch
1211 +- revalidate dentry on a branch
1212 +- mnt_want_write() for a branch
1213 +- vfs_unlink() for a branch
1214 +- mnt_drop_write() for a branch
1215 +- release the lock on a branch
1216 +
1217 +3. Persistent Inode Number
1218 +One of the most important issue for a filesystem is to maintain inode
1219 +numbers. This is particularly important to support exporting a
1220 +filesystem via NFS. Aufs is a virtual filesystem which doesn't have a
1221 +backend block device for its own. But some storage is necessary to
1222 +keep and maintain the inode numbers. It may be a large space and may not
1223 +suit to keep in memory. Aufs rents some space from its first writable
1224 +branch filesystem (by default) and creates file(s) on it. These files
1225 +are created by aufs internally and removed soon (currently) keeping
1226 +opened.
1227 +Note: Because these files are removed, they are totally gone after
1228 +      unmounting aufs. It means the inode numbers are not persistent
1229 +      across unmount or reboot. I have a plan to make them really
1230 +      persistent which will be important for aufs on NFS server.
1231 +
1232 +4. Read/Write Files Internally (copy-on-write)
1233 +Because a branch can be readonly, when you write a file on it, aufs will
1234 +"copy-up" it to the upper writable branch internally. And then write the
1235 +originally requested thing to the file. Generally kernel doesn't
1236 +open/read/write file actively. In aufs, even a single write may cause a
1237 +internal "file copy". This behaviour is very similar to cp(1) command.
1238 +
1239 +Some people may think it is better to pass such work to user space
1240 +helper, instead of doing in kernel space. Actually I am still thinking
1241 +about it. But currently I have implemented it in kernel space.
1242 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/02struct.txt linux/Documentation/filesystems/aufs/design/02struct.txt
1243 --- /usr/share/empty/Documentation/filesystems/aufs/design/02struct.txt 1970-01-01 01:00:00.000000000 +0100
1244 +++ linux/Documentation/filesystems/aufs/design/02struct.txt    2021-11-01 23:48:34.199692595 +0100
1245 @@ -0,0 +1,258 @@
1246 +
1247 +# Copyright (C) 2005-2021 Junjiro R. Okajima
1248 +# 
1249 +# This program is free software; you can redistribute it and/or modify
1250 +# it under the terms of the GNU General Public License as published by
1251 +# the Free Software Foundation; either version 2 of the License, or
1252 +# (at your option) any later version.
1253 +# 
1254 +# This program is distributed in the hope that it will be useful,
1255 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1256 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1257 +# GNU General Public License for more details.
1258 +# 
1259 +# You should have received a copy of the GNU General Public License
1260 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1261 +
1262 +Basic Aufs Internal Structure
1263 +
1264 +Superblock/Inode/Dentry/File Objects
1265 +----------------------------------------------------------------------
1266 +As like an ordinary filesystem, aufs has its own
1267 +superblock/inode/dentry/file objects. All these objects have a
1268 +dynamically allocated array and store the same kind of pointers to the
1269 +lower filesystem, branch.
1270 +For example, when you build a union with one readwrite branch and one
1271 +readonly, mounted /au, /rw and /ro respectively.
1272 +- /au = /rw + /ro
1273 +- /ro/fileA exists but /rw/fileA
1274 +
1275 +Aufs lookup operation finds /ro/fileA and gets dentry for that. These
1276 +pointers are stored in a aufs dentry. The array in aufs dentry will be,
1277 +- [0] = NULL (because /rw/fileA doesn't exist)
1278 +- [1] = /ro/fileA
1279 +
1280 +This style of an array is essentially same to the aufs
1281 +superblock/inode/dentry/file objects.
1282 +
1283 +Because aufs supports manipulating branches, ie. add/delete/change
1284 +branches dynamically, these objects has its own generation. When
1285 +branches are changed, the generation in aufs superblock is
1286 +incremented. And a generation in other object are compared when it is
1287 +accessed. When a generation in other objects are obsoleted, aufs
1288 +refreshes the internal array.
1289 +
1290 +
1291 +Superblock
1292 +----------------------------------------------------------------------
1293 +Additionally aufs superblock has some data for policies to select one
1294 +among multiple writable branches, XIB files, pseudo-links and kobject.
1295 +See below in detail.
1296 +About the policies which supports copy-down a directory, see
1297 +wbr_policy.txt too.
1298 +
1299 +
1300 +Branch and XINO(External Inode Number Translation Table)
1301 +----------------------------------------------------------------------
1302 +Every branch has its own xino (external inode number translation table)
1303 +file. The xino file is created and unlinked by aufs internally. When two
1304 +members of a union exist on the same filesystem, they share the single
1305 +xino file.
1306 +The struct of a xino file is simple, just a sequence of aufs inode
1307 +numbers which is indexed by the lower inode number.
1308 +In the above sample, assume the inode number of /ro/fileA is i111 and
1309 +aufs assigns the inode number i999 for fileA. Then aufs writes 999 as
1310 +4(8) bytes at 111 * 4(8) bytes offset in the xino file.
1311 +
1312 +When the inode numbers are not contiguous, the xino file will be sparse
1313 +which has a hole in it and doesn't consume as much disk space as it
1314 +might appear. If your branch filesystem consumes disk space for such
1315 +holes, then you should specify 'xino=' option at mounting aufs.
1316 +
1317 +Aufs has a mount option to free the disk blocks for such holes in XINO
1318 +files on tmpfs or ramdisk. But it is not so effective actually. If you
1319 +meet a problem of disk shortage due to XINO files, then you should try
1320 +"tmpfs-ino.patch" (and "vfs-ino.patch" too) in aufs4-standalone.git.
1321 +The patch localizes the assignment inumbers per tmpfs-mount and avoid
1322 +the holes in XINO files.
1323 +
1324 +Also a writable branch has three kinds of "whiteout bases". All these
1325 +are existed when the branch is joined to aufs, and their names are
1326 +whiteout-ed doubly, so that users will never see their names in aufs
1327 +hierarchy.
1328 +1. a regular file which will be hardlinked to all whiteouts.
1329 +2. a directory to store a pseudo-link.
1330 +3. a directory to store an "orphan"-ed file temporary.
1331 +
1332 +1. Whiteout Base
1333 +   When you remove a file on a readonly branch, aufs handles it as a
1334 +   logical deletion and creates a whiteout on the upper writable branch
1335 +   as a hardlink of this file in order not to consume inode on the
1336 +   writable branch.
1337 +2. Pseudo-link Dir
1338 +   See below, Pseudo-link.
1339 +3. Step-Parent Dir
1340 +   When "fileC" exists on the lower readonly branch only and it is
1341 +   opened and removed with its parent dir, and then user writes
1342 +   something into it, then aufs copies-up fileC to this
1343 +   directory. Because there is no other dir to store fileC. After
1344 +   creating a file under this dir, the file is unlinked.
1345 +
1346 +Because aufs supports manipulating branches, ie. add/delete/change
1347 +dynamically, a branch has its own id. When the branch order changes,
1348 +aufs finds the new index by searching the branch id.
1349 +
1350 +
1351 +Pseudo-link
1352 +----------------------------------------------------------------------
1353 +Assume "fileA" exists on the lower readonly branch only and it is
1354 +hardlinked to "fileB" on the branch. When you write something to fileA,
1355 +aufs copies-up it to the upper writable branch. Additionally aufs
1356 +creates a hardlink under the Pseudo-link Directory of the writable
1357 +branch. The inode of a pseudo-link is kept in aufs super_block as a
1358 +simple list. If fileB is read after unlinking fileA, aufs returns
1359 +filedata from the pseudo-link instead of the lower readonly
1360 +branch. Because the pseudo-link is based upon the inode, to keep the
1361 +inode number by xino (see above) is essentially necessary.
1362 +
1363 +All the hardlinks under the Pseudo-link Directory of the writable branch
1364 +should be restored in a proper location later. Aufs provides a utility
1365 +to do this. The userspace helpers executed at remounting and unmounting
1366 +aufs by default.
1367 +During this utility is running, it puts aufs into the pseudo-link
1368 +maintenance mode. In this mode, only the process which began the
1369 +maintenance mode (and its child processes) is allowed to operate in
1370 +aufs. Some other processes which are not related to the pseudo-link will
1371 +be allowed to run too, but the rest have to return an error or wait
1372 +until the maintenance mode ends. If a process already acquires an inode
1373 +mutex (in VFS), it has to return an error.
1374 +
1375 +
1376 +XIB(external inode number bitmap)
1377 +----------------------------------------------------------------------
1378 +Addition to the xino file per a branch, aufs has an external inode number
1379 +bitmap in a superblock object. It is also an internal file such like a
1380 +xino file.
1381 +It is a simple bitmap to mark whether the aufs inode number is in-use or
1382 +not.
1383 +To reduce the file I/O, aufs prepares a single memory page to cache xib.
1384 +
1385 +As well as XINO files, aufs has a feature to truncate/refresh XIB to
1386 +reduce the number of consumed disk blocks for these files.
1387 +
1388 +
1389 +Virtual or Vertical Dir, and Readdir in Userspace
1390 +----------------------------------------------------------------------
1391 +In order to support multiple layers (branches), aufs readdir operation
1392 +constructs a virtual dir block on memory. For readdir, aufs calls
1393 +vfs_readdir() internally for each dir on branches, merges their entries
1394 +with eliminating the whiteout-ed ones, and sets it to file (dir)
1395 +object. So the file object has its entry list until it is closed. The
1396 +entry list will be updated when the file position is zero and becomes
1397 +obsoleted. This decision is made in aufs automatically.
1398 +
1399 +The dynamically allocated memory block for the name of entries has a
1400 +unit of 512 bytes (by default) and stores the names contiguously (no
1401 +padding). Another block for each entry is handled by kmem_cache too.
1402 +During building dir blocks, aufs creates hash list and judging whether
1403 +the entry is whiteouted by its upper branch or already listed.
1404 +The merged result is cached in the corresponding inode object and
1405 +maintained by a customizable life-time option.
1406 +
1407 +Some people may call it can be a security hole or invite DoS attack
1408 +since the opened and once readdir-ed dir (file object) holds its entry
1409 +list and becomes a pressure for system memory. But I'd say it is similar
1410 +to files under /proc or /sys. The virtual files in them also holds a
1411 +memory page (generally) while they are opened. When an idea to reduce
1412 +memory for them is introduced, it will be applied to aufs too.
1413 +For those who really hate this situation, I've developed readdir(3)
1414 +library which operates this merging in userspace. You just need to set
1415 +LD_PRELOAD environment variable, and aufs will not consume no memory in
1416 +kernel space for readdir(3).
1417 +
1418 +
1419 +Workqueue
1420 +----------------------------------------------------------------------
1421 +Aufs sometimes requires privilege access to a branch. For instance,
1422 +in copy-up/down operation. When a user process is going to make changes
1423 +to a file which exists in the lower readonly branch only, and the mode
1424 +of one of ancestor directories may not be writable by a user
1425 +process. Here aufs copy-up the file with its ancestors and they may
1426 +require privilege to set its owner/group/mode/etc.
1427 +This is a typical case of a application character of aufs (see
1428 +Introduction).
1429 +
1430 +Aufs uses workqueue synchronously for this case. It creates its own
1431 +workqueue. The workqueue is a kernel thread and has privilege. Aufs
1432 +passes the request to call mkdir or write (for example), and wait for
1433 +its completion. This approach solves a problem of a signal handler
1434 +simply.
1435 +If aufs didn't adopt the workqueue and changed the privilege of the
1436 +process, then the process may receive the unexpected SIGXFSZ or other
1437 +signals.
1438 +
1439 +Also aufs uses the system global workqueue ("events" kernel thread) too
1440 +for asynchronous tasks, such like handling inotify/fsnotify, re-creating a
1441 +whiteout base and etc. This is unrelated to a privilege.
1442 +Most of aufs operation tries acquiring a rw_semaphore for aufs
1443 +superblock at the beginning, at the same time waits for the completion
1444 +of all queued asynchronous tasks.
1445 +
1446 +
1447 +Whiteout
1448 +----------------------------------------------------------------------
1449 +The whiteout in aufs is very similar to Unionfs's. That is represented
1450 +by its filename. UnionMount takes an approach of a file mode, but I am
1451 +afraid several utilities (find(1) or something) will have to support it.
1452 +
1453 +Basically the whiteout represents "logical deletion" which stops aufs to
1454 +lookup further, but also it represents "dir is opaque" which also stop
1455 +further lookup.
1456 +
1457 +In aufs, rmdir(2) and rename(2) for dir uses whiteout alternatively.
1458 +In order to make several functions in a single systemcall to be
1459 +revertible, aufs adopts an approach to rename a directory to a temporary
1460 +unique whiteouted name.
1461 +For example, in rename(2) dir where the target dir already existed, aufs
1462 +renames the target dir to a temporary unique whiteouted name before the
1463 +actual rename on a branch, and then handles other actions (make it opaque,
1464 +update the attributes, etc). If an error happens in these actions, aufs
1465 +simply renames the whiteouted name back and returns an error. If all are
1466 +succeeded, aufs registers a function to remove the whiteouted unique
1467 +temporary name completely and asynchronously to the system global
1468 +workqueue.
1469 +
1470 +
1471 +Copy-up
1472 +----------------------------------------------------------------------
1473 +It is a well-known feature or concept.
1474 +When user modifies a file on a readonly branch, aufs operate "copy-up"
1475 +internally and makes change to the new file on the upper writable branch.
1476 +When the trigger systemcall does not update the timestamps of the parent
1477 +dir, aufs reverts it after copy-up.
1478 +
1479 +
1480 +Move-down (aufs3.9 and later)
1481 +----------------------------------------------------------------------
1482 +"Copy-up" is one of the essential feature in aufs. It copies a file from
1483 +the lower readonly branch to the upper writable branch when a user
1484 +changes something about the file.
1485 +"Move-down" is an opposite action of copy-up. Basically this action is
1486 +ran manually instead of automatically and internally.
1487 +For desgin and implementation, aufs has to consider these issues.
1488 +- whiteout for the file may exist on the lower branch.
1489 +- ancestor directories may not exist on the lower branch.
1490 +- diropq for the ancestor directories may exist on the upper branch.
1491 +- free space on the lower branch will reduce.
1492 +- another access to the file may happen during moving-down, including
1493 +  UDBA (see "Revalidate Dentry and UDBA").
1494 +- the file should not be hard-linked nor pseudo-linked. they should be
1495 +  handled by auplink utility later.
1496 +
1497 +Sometimes users want to move-down a file from the upper writable branch
1498 +to the lower readonly or writable branch. For instance,
1499 +- the free space of the upper writable branch is going to run out.
1500 +- create a new intermediate branch between the upper and lower branch.
1501 +- etc.
1502 +
1503 +For this purpose, use "aumvdown" command in aufs-util.git.
1504 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/03atomic_open.txt linux/Documentation/filesystems/aufs/design/03atomic_open.txt
1505 --- /usr/share/empty/Documentation/filesystems/aufs/design/03atomic_open.txt    1970-01-01 01:00:00.000000000 +0100
1506 +++ linux/Documentation/filesystems/aufs/design/03atomic_open.txt       2021-11-01 23:48:34.203025928 +0100
1507 @@ -0,0 +1,85 @@
1508 +
1509 +# Copyright (C) 2015-2021 Junjiro R. Okajima
1510 +# 
1511 +# This program is free software; you can redistribute it and/or modify
1512 +# it under the terms of the GNU General Public License as published by
1513 +# the Free Software Foundation; either version 2 of the License, or
1514 +# (at your option) any later version.
1515 +# 
1516 +# This program is distributed in the hope that it will be useful,
1517 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1518 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1519 +# GNU General Public License for more details.
1520 +# 
1521 +# You should have received a copy of the GNU General Public License
1522 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1523 +
1524 +Support for a branch who has its ->atomic_open()
1525 +----------------------------------------------------------------------
1526 +The filesystems who implement its ->atomic_open() are not majority. For
1527 +example NFSv4 does, and aufs should call NFSv4 ->atomic_open,
1528 +particularly for open(O_CREAT|O_EXCL, 0400) case. Other than
1529 +->atomic_open(), NFSv4 returns an error for this open(2). While I am not
1530 +sure whether all filesystems who have ->atomic_open() behave like this,
1531 +but NFSv4 surely returns the error.
1532 +
1533 +In order to support ->atomic_open() for aufs, there are a few
1534 +approaches.
1535 +
1536 +A. Introduce aufs_atomic_open()
1537 +   - calls one of VFS:do_last(), lookup_open() or atomic_open() for
1538 +     branch fs.
1539 +B. Introduce aufs_atomic_open() calling create, open and chmod. this is
1540 +   an aufs user Pip Cet's approach
1541 +   - calls aufs_create(), VFS finish_open() and notify_change().
1542 +   - pass fake-mode to finish_open(), and then correct the mode by
1543 +     notify_change().
1544 +C. Extend aufs_open() to call branch fs's ->atomic_open()
1545 +   - no aufs_atomic_open().
1546 +   - aufs_lookup() registers the TID to an aufs internal object.
1547 +   - aufs_create() does nothing when the matching TID is registered, but
1548 +     registers the mode.
1549 +   - aufs_open() calls branch fs's ->atomic_open() when the matching
1550 +     TID is registered.
1551 +D. Extend aufs_open() to re-try branch fs's ->open() with superuser's
1552 +   credential
1553 +   - no aufs_atomic_open().
1554 +   - aufs_create() registers the TID to an internal object. this info
1555 +     represents "this process created this file just now."
1556 +   - when aufs gets EACCES from branch fs's ->open(), then confirm the
1557 +     registered TID and re-try open() with superuser's credential.
1558 +
1559 +Pros and cons for each approach.
1560 +
1561 +A.
1562 +   - straightforward but highly depends upon VFS internal.
1563 +   - the atomic behavaiour is kept.
1564 +   - some of parameters such as nameidata are hard to reproduce for
1565 +     branch fs.
1566 +   - large overhead.
1567 +B.
1568 +   - easy to implement.
1569 +   - the atomic behavaiour is lost.
1570 +C.
1571 +   - the atomic behavaiour is kept.
1572 +   - dirty and tricky.
1573 +   - VFS checks whether the file is created correctly after calling
1574 +     ->create(), which means this approach doesn't work.
1575 +D.
1576 +   - easy to implement.
1577 +   - the atomic behavaiour is lost.
1578 +   - to open a file with superuser's credential and give it to a user
1579 +     process is a bad idea, since the file object keeps the credential
1580 +     in it. It may affect LSM or something. This approach doesn't work
1581 +     either.
1582 +
1583 +The approach A is ideal, but it hard to implement. So here is a
1584 +variation of A, which is to be implemented.
1585 +
1586 +A-1. Introduce aufs_atomic_open()
1587 +     - calls branch fs ->atomic_open() if exists. otherwise calls
1588 +       vfs_create() and finish_open().
1589 +     - the demerit is that the several checks after branch fs
1590 +       ->atomic_open() are lost. in the ordinary case, the checks are
1591 +       done by VFS:do_last(), lookup_open() and atomic_open(). some can
1592 +       be implemented in aufs, but not all I am afraid.
1593 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/03lookup.txt linux/Documentation/filesystems/aufs/design/03lookup.txt
1594 --- /usr/share/empty/Documentation/filesystems/aufs/design/03lookup.txt 1970-01-01 01:00:00.000000000 +0100
1595 +++ linux/Documentation/filesystems/aufs/design/03lookup.txt    2021-11-01 23:48:34.203025928 +0100
1596 @@ -0,0 +1,113 @@
1597 +
1598 +# Copyright (C) 2005-2021 Junjiro R. Okajima
1599 +# 
1600 +# This program is free software; you can redistribute it and/or modify
1601 +# it under the terms of the GNU General Public License as published by
1602 +# the Free Software Foundation; either version 2 of the License, or
1603 +# (at your option) any later version.
1604 +# 
1605 +# This program is distributed in the hope that it will be useful,
1606 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1607 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1608 +# GNU General Public License for more details.
1609 +# 
1610 +# You should have received a copy of the GNU General Public License
1611 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1612 +
1613 +Lookup in a Branch
1614 +----------------------------------------------------------------------
1615 +Since aufs has a character of sub-VFS (see Introduction), it operates
1616 +lookup for branches as VFS does. It may be a heavy work. But almost all
1617 +lookup operation in aufs is the simplest case, ie. lookup only an entry
1618 +directly connected to its parent. Digging down the directory hierarchy
1619 +is unnecessary. VFS has a function lookup_one_len() for that use, and
1620 +aufs calls it.
1621 +
1622 +When a branch is a remote filesystem, aufs basically relies upon its
1623 +->d_revalidate(), also aufs forces the hardest revalidate tests for
1624 +them.
1625 +For d_revalidate, aufs implements three levels of revalidate tests. See
1626 +"Revalidate Dentry and UDBA" in detail.
1627 +
1628 +
1629 +Test Only the Highest One for the Directory Permission (dirperm1 option)
1630 +----------------------------------------------------------------------
1631 +Let's try case study.
1632 +- aufs has two branches, upper readwrite and lower readonly.
1633 +  /au = /rw + /ro
1634 +- "dirA" exists under /ro, but /rw. and its mode is 0700.
1635 +- user invoked "chmod a+rx /au/dirA"
1636 +- the internal copy-up is activated and "/rw/dirA" is created and its
1637 +  permission bits are set to world readable.
1638 +- then "/au/dirA" becomes world readable?
1639 +
1640 +In this case, /ro/dirA is still 0700 since it exists in readonly branch,
1641 +or it may be a natively readonly filesystem. If aufs respects the lower
1642 +branch, it should not respond readdir request from other users. But user
1643 +allowed it by chmod. Should really aufs rejects showing the entries
1644 +under /ro/dirA?
1645 +
1646 +To be honest, I don't have a good solution for this case. So aufs
1647 +implements 'dirperm1' and 'nodirperm1' mount options, and leave it to
1648 +users.
1649 +When dirperm1 is specified, aufs checks only the highest one for the
1650 +directory permission, and shows the entries. Otherwise, as usual, checks
1651 +every dir existing on all branches and rejects the request.
1652 +
1653 +As a side effect, dirperm1 option improves the performance of aufs
1654 +because the number of permission check is reduced when the number of
1655 +branch is many.
1656 +
1657 +
1658 +Revalidate Dentry and UDBA (User's Direct Branch Access)
1659 +----------------------------------------------------------------------
1660 +Generally VFS helpers re-validate a dentry as a part of lookup.
1661 +0. digging down the directory hierarchy.
1662 +1. lock the parent dir by its i_mutex.
1663 +2. lookup the final (child) entry.
1664 +3. revalidate it.
1665 +4. call the actual operation (create, unlink, etc.)
1666 +5. unlock the parent dir
1667 +
1668 +If the filesystem implements its ->d_revalidate() (step 3), then it is
1669 +called. Actually aufs implements it and checks the dentry on a branch is
1670 +still valid.
1671 +But it is not enough. Because aufs has to release the lock for the
1672 +parent dir on a branch at the end of ->lookup() (step 2) and
1673 +->d_revalidate() (step 3) while the i_mutex of the aufs dir is still
1674 +held by VFS.
1675 +If the file on a branch is changed directly, eg. bypassing aufs, after
1676 +aufs released the lock, then the subsequent operation may cause
1677 +something unpleasant result.
1678 +
1679 +This situation is a result of VFS architecture, ->lookup() and
1680 +->d_revalidate() is separated. But I never say it is wrong. It is a good
1681 +design from VFS's point of view. It is just not suitable for sub-VFS
1682 +character in aufs.
1683 +
1684 +Aufs supports such case by three level of revalidation which is
1685 +selectable by user.
1686 +1. Simple Revalidate
1687 +   Addition to the native flow in VFS's, confirm the child-parent
1688 +   relationship on the branch just after locking the parent dir on the
1689 +   branch in the "actual operation" (step 4). When this validation
1690 +   fails, aufs returns EBUSY. ->d_revalidate() (step 3) in aufs still
1691 +   checks the validation of the dentry on branches.
1692 +2. Monitor Changes Internally by Inotify/Fsnotify
1693 +   Addition to above, in the "actual operation" (step 4) aufs re-lookup
1694 +   the dentry on the branch, and returns EBUSY if it finds different
1695 +   dentry.
1696 +   Additionally, aufs sets the inotify/fsnotify watch for every dir on branches
1697 +   during it is in cache. When the event is notified, aufs registers a
1698 +   function to kernel 'events' thread by schedule_work(). And the
1699 +   function sets some special status to the cached aufs dentry and inode
1700 +   private data. If they are not cached, then aufs has nothing to
1701 +   do. When the same file is accessed through aufs (step 0-3) later,
1702 +   aufs will detect the status and refresh all necessary data.
1703 +   In this mode, aufs has to ignore the event which is fired by aufs
1704 +   itself.
1705 +3. No Extra Validation
1706 +   This is the simplest test and doesn't add any additional revalidation
1707 +   test, and skip the revalidation in step 4. It is useful and improves
1708 +   aufs performance when system surely hide the aufs branches from user,
1709 +   by over-mounting something (or another method).
1710 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/04branch.txt linux/Documentation/filesystems/aufs/design/04branch.txt
1711 --- /usr/share/empty/Documentation/filesystems/aufs/design/04branch.txt 1970-01-01 01:00:00.000000000 +0100
1712 +++ linux/Documentation/filesystems/aufs/design/04branch.txt    2021-11-01 23:48:34.203025928 +0100
1713 @@ -0,0 +1,74 @@
1714 +
1715 +# Copyright (C) 2005-2021 Junjiro R. Okajima
1716 +# 
1717 +# This program is free software; you can redistribute it and/or modify
1718 +# it under the terms of the GNU General Public License as published by
1719 +# the Free Software Foundation; either version 2 of the License, or
1720 +# (at your option) any later version.
1721 +# 
1722 +# This program is distributed in the hope that it will be useful,
1723 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1724 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1725 +# GNU General Public License for more details.
1726 +# 
1727 +# You should have received a copy of the GNU General Public License
1728 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1729 +
1730 +Branch Manipulation
1731 +
1732 +Since aufs supports dynamic branch manipulation, ie. add/remove a branch
1733 +and changing its permission/attribute, there are a lot of works to do.
1734 +
1735 +
1736 +Add a Branch
1737 +----------------------------------------------------------------------
1738 +o Confirm the adding dir exists outside of aufs, including loopback
1739 +  mount, and its various attributes.
1740 +o Initialize the xino file and whiteout bases if necessary.
1741 +  See struct.txt.
1742 +
1743 +o Check the owner/group/mode of the directory
1744 +  When the owner/group/mode of the adding directory differs from the
1745 +  existing branch, aufs issues a warning because it may impose a
1746 +  security risk.
1747 +  For example, when a upper writable branch has a world writable empty
1748 +  top directory, a malicious user can create any files on the writable
1749 +  branch directly, like copy-up and modify manually. If something like
1750 +  /etc/{passwd,shadow} exists on the lower readonly branch but the upper
1751 +  writable branch, and the writable branch is world-writable, then a
1752 +  malicious guy may create /etc/passwd on the writable branch directly
1753 +  and the infected file will be valid in aufs.
1754 +  I am afraid it can be a security issue, but aufs can do nothing except
1755 +  producing a warning.
1756 +
1757 +
1758 +Delete a Branch
1759 +----------------------------------------------------------------------
1760 +o Confirm the deleting branch is not busy
1761 +  To be general, there is one merit to adopt "remount" interface to
1762 +  manipulate branches. It is to discard caches. At deleting a branch,
1763 +  aufs checks the still cached (and connected) dentries and inodes. If
1764 +  there are any, then they are all in-use. An inode without its
1765 +  corresponding dentry can be alive alone (for example, inotify/fsnotify case).
1766 +
1767 +  For the cached one, aufs checks whether the same named entry exists on
1768 +  other branches.
1769 +  If the cached one is a directory, because aufs provides a merged view
1770 +  to users, as long as one dir is left on any branch aufs can show the
1771 +  dir to users. In this case, the branch can be removed from aufs.
1772 +  Otherwise aufs rejects deleting the branch.
1773 +
1774 +  If any file on the deleting branch is opened by aufs, then aufs
1775 +  rejects deleting.
1776 +
1777 +
1778 +Modify the Permission of a Branch
1779 +----------------------------------------------------------------------
1780 +o Re-initialize or remove the xino file and whiteout bases if necessary.
1781 +  See struct.txt.
1782 +
1783 +o rw --> ro: Confirm the modifying branch is not busy
1784 +  Aufs rejects the request if any of these conditions are true.
1785 +  - a file on the branch is mmap-ed.
1786 +  - a regular file on the branch is opened for write and there is no
1787 +    same named entry on the upper branch.
1788 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/05wbr_policy.txt linux/Documentation/filesystems/aufs/design/05wbr_policy.txt
1789 --- /usr/share/empty/Documentation/filesystems/aufs/design/05wbr_policy.txt     1970-01-01 01:00:00.000000000 +0100
1790 +++ linux/Documentation/filesystems/aufs/design/05wbr_policy.txt        2021-11-01 23:48:34.203025928 +0100
1791 @@ -0,0 +1,64 @@
1792 +
1793 +# Copyright (C) 2005-2021 Junjiro R. Okajima
1794 +# 
1795 +# This program is free software; you can redistribute it and/or modify
1796 +# it under the terms of the GNU General Public License as published by
1797 +# the Free Software Foundation; either version 2 of the License, or
1798 +# (at your option) any later version.
1799 +# 
1800 +# This program is distributed in the hope that it will be useful,
1801 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1802 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1803 +# GNU General Public License for more details.
1804 +# 
1805 +# You should have received a copy of the GNU General Public License
1806 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1807 +
1808 +Policies to Select One among Multiple Writable Branches
1809 +----------------------------------------------------------------------
1810 +When the number of writable branch is more than one, aufs has to decide
1811 +the target branch for file creation or copy-up. By default, the highest
1812 +writable branch which has the parent (or ancestor) dir of the target
1813 +file is chosen (top-down-parent policy).
1814 +By user's request, aufs implements some other policies to select the
1815 +writable branch, for file creation several policies, round-robin,
1816 +most-free-space, and other policies. For copy-up, top-down-parent,
1817 +bottom-up-parent, bottom-up and others.
1818 +
1819 +As expected, the round-robin policy selects the branch in circular. When
1820 +you have two writable branches and creates 10 new files, 5 files will be
1821 +created for each branch. mkdir(2) systemcall is an exception. When you
1822 +create 10 new directories, all will be created on the same branch.
1823 +And the most-free-space policy selects the one which has most free
1824 +space among the writable branches. The amount of free space will be
1825 +checked by aufs internally, and users can specify its time interval.
1826 +
1827 +The policies for copy-up is more simple,
1828 +top-down-parent is equivalent to the same named on in create policy,
1829 +bottom-up-parent selects the writable branch where the parent dir
1830 +exists and the nearest upper one from the copyup-source,
1831 +bottom-up selects the nearest upper writable branch from the
1832 +copyup-source, regardless the existence of the parent dir.
1833 +
1834 +There are some rules or exceptions to apply these policies.
1835 +- If there is a readonly branch above the policy-selected branch and
1836 +  the parent dir is marked as opaque (a variation of whiteout), or the
1837 +  target (creating) file is whiteout-ed on the upper readonly branch,
1838 +  then the result of the policy is ignored and the target file will be
1839 +  created on the nearest upper writable branch than the readonly branch.
1840 +- If there is a writable branch above the policy-selected branch and
1841 +  the parent dir is marked as opaque or the target file is whiteouted
1842 +  on the branch, then the result of the policy is ignored and the target
1843 +  file will be created on the highest one among the upper writable
1844 +  branches who has diropq or whiteout. In case of whiteout, aufs removes
1845 +  it as usual.
1846 +- link(2) and rename(2) systemcalls are exceptions in every policy.
1847 +  They try selecting the branch where the source exists as possible
1848 +  since copyup a large file will take long time. If it can't be,
1849 +  ie. the branch where the source exists is readonly, then they will
1850 +  follow the copyup policy.
1851 +- There is an exception for rename(2) when the target exists.
1852 +  If the rename target exists, aufs compares the index of the branches
1853 +  where the source and the target exists and selects the higher
1854 +  one. If the selected branch is readonly, then aufs follows the
1855 +  copyup policy.
1856 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.dot linux/Documentation/filesystems/aufs/design/06dirren.dot
1857 --- /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.dot 1970-01-01 01:00:00.000000000 +0100
1858 +++ linux/Documentation/filesystems/aufs/design/06dirren.dot    2021-11-01 23:48:34.203025928 +0100
1859 @@ -0,0 +1,31 @@
1860 +
1861 +// to view this graph, run dot(1) command in GRAPHVIZ.
1862 +
1863 +digraph G {
1864 +node [shape=box];
1865 +whinfo [label="detailed info file\n(lower_brid_root-hinum, h_inum, namelen, old name)"];
1866 +
1867 +node [shape=oval];
1868 +
1869 +aufs_rename -> whinfo [label="store/remove"];
1870 +
1871 +node [shape=oval];
1872 +inode_list [label="h_inum list in branch\ncache"];
1873 +
1874 +node [shape=box];
1875 +whinode [label="h_inum list file"];
1876 +
1877 +node [shape=oval];
1878 +brmgmt [label="br_add/del/mod/umount"];
1879 +
1880 +brmgmt -> inode_list [label="create/remove"];
1881 +brmgmt -> whinode [label="load/store"];
1882 +
1883 +inode_list -> whinode [style=dashed,dir=both];
1884 +
1885 +aufs_rename -> inode_list [label="add/del"];
1886 +
1887 +aufs_lookup -> inode_list [label="search"];
1888 +
1889 +aufs_lookup -> whinfo [label="load/remove"];
1890 +}
1891 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.txt linux/Documentation/filesystems/aufs/design/06dirren.txt
1892 --- /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.txt 1970-01-01 01:00:00.000000000 +0100
1893 +++ linux/Documentation/filesystems/aufs/design/06dirren.txt    2021-11-01 23:48:34.203025928 +0100
1894 @@ -0,0 +1,102 @@
1895 +
1896 +# Copyright (C) 2017-2021 Junjiro R. Okajima
1897 +#
1898 +# This program is free software; you can redistribute it and/or modify
1899 +# it under the terms of the GNU General Public License as published by
1900 +# the Free Software Foundation; either version 2 of the License, or
1901 +# (at your option) any later version.
1902 +#
1903 +# This program is distributed in the hope that it will be useful,
1904 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1905 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1906 +# GNU General Public License for more details.
1907 +#
1908 +# You should have received a copy of the GNU General Public License
1909 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1910 +
1911 +Special handling for renaming a directory (DIRREN)
1912 +----------------------------------------------------------------------
1913 +First, let's assume we have a simple usecase.
1914 +
1915 +- /u = /rw + /ro
1916 +- /rw/dirA exists
1917 +- /ro/dirA and /ro/dirA/file exist too
1918 +- there is no dirB on both branches
1919 +- a user issues rename("dirA", "dirB")
1920 +
1921 +Now, what should aufs behave against this rename(2)?
1922 +There are a few possible cases.
1923 +
1924 +A. returns EROFS.
1925 +   since dirA exists on a readonly branch which cannot be renamed.
1926 +B. returns EXDEV.
1927 +   it is possible to copy-up dirA (only the dir itself), but the child
1928 +   entries ("file" in this case) should not be. it must be a bad
1929 +   approach to copy-up recursively.
1930 +C. returns a success.
1931 +   even the branch /ro is readonly, aufs tries renaming it. Obviously it
1932 +   is a violation of aufs' policy.
1933 +D. construct an extra information which indicates that /ro/dirA should
1934 +   be handled as the name of dirB.
1935 +   overlayfs has a similar feature called REDIRECT.
1936 +
1937 +Until now, aufs implements the case B only which returns EXDEV, and
1938 +expects the userspace application behaves like mv(1) which tries
1939 +issueing rename(2) recursively.
1940 +
1941 +A new aufs feature called DIRREN is introduced which implements the case
1942 +D. There are several "extra information" added.
1943 +
1944 +1. detailed info per renamed directory
1945 +   path: /rw/dirB/$AUFS_WH_DR_INFO_PFX.<lower branch-id>
1946 +2. the inode-number list of directories on a branch
1947 +   path: /rw/dirB/$AUFS_WH_DR_BRHINO
1948 +
1949 +The filename of "detailed info per directory" represents the lower
1950 +branch, and its format is
1951 +- a type of the branch id
1952 +  one of these.
1953 +  + uuid (not implemented yet)
1954 +  + fsid
1955 +  + dev
1956 +- the inode-number of the branch root dir
1957 +
1958 +And it contains these info in a single regular file.
1959 +- magic number
1960 +- branch's inode-number of the logically renamed dir
1961 +- the name of the before-renamed dir
1962 +
1963 +The "detailed info per directory" file is created in aufs rename(2), and
1964 +loaded in any lookup.
1965 +The info is considered in lookup for the matching case only. Here
1966 +"matching" means that the root of branch (in the info filename) is same
1967 +to the current looking-up branch. After looking-up the before-renamed
1968 +name, the inode-number is compared. And the matched dentry is used.
1969 +
1970 +The "inode-number list of directories" is a regular file which contains
1971 +simply the inode-numbers on the branch. The file is created or updated
1972 +in removing the branch, and loaded in adding the branch. Its lifetime is
1973 +equal to the branch.
1974 +The list is refered in lookup, and when the current target inode is
1975 +found in the list, the aufs tries loading the "detailed info per
1976 +directory" and get the changed and valid name of the dir.
1977 +
1978 +Theoretically these "extra informaiton" may be able to be put into XATTR
1979 +in the dir inode. But aufs doesn't choose this way because
1980 +1. XATTR may not be supported by the branch (or its configuration)
1981 +2. XATTR may have its size limit.
1982 +3. XATTR may be less easy to convert than a regular file, when the
1983 +   format of the info is changed in the future.
1984 +At the same time, I agree that the regular file approach is much slower
1985 +than XATTR approach. So, in the future, aufs may take the XATTR or other
1986 +better approach.
1987 +
1988 +This DIRREN feature is enabled by aufs configuration, and is activated
1989 +by a new mount option.
1990 +
1991 +For the more complicated case, there is a work with UDBA option, which
1992 +is to dected the direct access to the branches (by-passing aufs) and to
1993 +maintain the cashes in aufs. Since a single cached aufs dentry may
1994 +contains two names, before- and after-rename, the name comparision in
1995 +UDBA handler may not work correctly. In this case, the behaviour will be
1996 +equivalen to udba=reval case.
1997 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06fhsm.txt linux/Documentation/filesystems/aufs/design/06fhsm.txt
1998 --- /usr/share/empty/Documentation/filesystems/aufs/design/06fhsm.txt   1970-01-01 01:00:00.000000000 +0100
1999 +++ linux/Documentation/filesystems/aufs/design/06fhsm.txt      2021-11-01 23:48:34.203025928 +0100
2000 @@ -0,0 +1,120 @@
2001 +
2002 +# Copyright (C) 2011-2021 Junjiro R. Okajima
2003 +# 
2004 +# This program is free software; you can redistribute it and/or modify
2005 +# it under the terms of the GNU General Public License as published by
2006 +# the Free Software Foundation; either version 2 of the License, or
2007 +# (at your option) any later version.
2008 +# 
2009 +# This program is distributed in the hope that it will be useful,
2010 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2011 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2012 +# GNU General Public License for more details.
2013 +# 
2014 +# You should have received a copy of the GNU General Public License
2015 +# along with this program; if not, write to the Free Software
2016 +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
2017 +
2018 +
2019 +File-based Hierarchical Storage Management (FHSM)
2020 +----------------------------------------------------------------------
2021 +Hierarchical Storage Management (or HSM) is a well-known feature in the
2022 +storage world. Aufs provides this feature as file-based with multiple
2023 +writable branches, based upon the principle of "Colder, the Lower".
2024 +Here the word "colder" means that the less used files, and "lower" means
2025 +that the position in the order of the stacked branches vertically.
2026 +These multiple writable branches are prioritized, ie. the topmost one
2027 +should be the fastest drive and be used heavily.
2028 +
2029 +o Characters in aufs FHSM story
2030 +- aufs itself and a new branch attribute.
2031 +- a new ioctl interface to move-down and to establish a connection with
2032 +  the daemon ("move-down" is a converse of "copy-up").
2033 +- userspace tool and daemon.
2034 +
2035 +The userspace daemon establishes a connection with aufs and waits for
2036 +the notification. The notified information is very similar to struct
2037 +statfs containing the number of consumed blocks and inodes.
2038 +When the consumed blocks/inodes of a branch exceeds the user-specified
2039 +upper watermark, the daemon activates its move-down process until the
2040 +consumed blocks/inodes reaches the user-specified lower watermark.
2041 +
2042 +The actual move-down is done by aufs based upon the request from
2043 +user-space since we need to maintain the inode number and the internal
2044 +pointer arrays in aufs.
2045 +
2046 +Currently aufs FHSM handles the regular files only. Additionally they
2047 +must not be hard-linked nor pseudo-linked.
2048 +
2049 +
2050 +o Cowork of aufs and the user-space daemon
2051 +  During the userspace daemon established the connection, aufs sends a
2052 +  small notification to it whenever aufs writes something into the
2053 +  writable branch. But it may cost high since aufs issues statfs(2)
2054 +  internally. So user can specify a new option to cache the
2055 +  info. Actually the notification is controlled by these factors.
2056 +  + the specified cache time.
2057 +  + classified as "force" by aufs internally.
2058 +  Until the specified time expires, aufs doesn't send the info
2059 +  except the forced cases. When aufs decide forcing, the info is always
2060 +  notified to userspace.
2061 +  For example, the number of free inodes is generally large enough and
2062 +  the shortage of it happens rarely. So aufs doesn't force the
2063 +  notification when creating a new file, directory and others. This is
2064 +  the typical case which aufs doesn't force.
2065 +  When aufs writes the actual filedata and the files consumes any of new
2066 +  blocks, the aufs forces notifying.
2067 +
2068 +
2069 +o Interfaces in aufs
2070 +- New branch attribute.
2071 +  + fhsm
2072 +    Specifies that the branch is managed by FHSM feature. In other word,
2073 +    participant in the FHSM.
2074 +    When nofhsm is set to the branch, it will not be the source/target
2075 +    branch of the move-down operation. This attribute is set
2076 +    independently from coo and moo attributes, and if you want full
2077 +    FHSM, you should specify them as well.
2078 +- New mount option.
2079 +  + fhsm_sec
2080 +    Specifies a second to suppress many less important info to be
2081 +    notified.
2082 +- New ioctl.
2083 +  + AUFS_CTL_FHSM_FD
2084 +    create a new file descriptor which userspace can read the notification
2085 +    (a subset of struct statfs) from aufs.
2086 +- Module parameter 'brs'
2087 +  It has to be set to 1. Otherwise the new mount option 'fhsm' will not
2088 +  be set.
2089 +- mount helpers /sbin/mount.aufs and /sbin/umount.aufs
2090 +  When there are two or more branches with fhsm attributes,
2091 +  /sbin/mount.aufs invokes the user-space daemon and /sbin/umount.aufs
2092 +  terminates it. As a result of remounting and branch-manipulation, the
2093 +  number of branches with fhsm attribute can be one. In this case,
2094 +  /sbin/mount.aufs will terminate the user-space daemon.
2095 +
2096 +
2097 +Finally the operation is done as these steps in kernel-space.
2098 +- make sure that,
2099 +  + no one else is using the file.
2100 +  + the file is not hard-linked.
2101 +  + the file is not pseudo-linked.
2102 +  + the file is a regular file.
2103 +  + the parent dir is not opaqued.
2104 +- find the target writable branch.
2105 +- make sure the file is not whiteout-ed by the upper (than the target)
2106 +  branch.
2107 +- make the parent dir on the target branch.
2108 +- mutex lock the inode on the branch.
2109 +- unlink the whiteout on the target branch (if exists).
2110 +- lookup and create the whiteout-ed temporary name on the target branch.
2111 +- copy the file as the whiteout-ed temporary name on the target branch.
2112 +- rename the whiteout-ed temporary name to the original name.
2113 +- unlink the file on the source branch.
2114 +- maintain the internal pointer array and the external inode number
2115 +  table (XINO).
2116 +- maintain the timestamps and other attributes of the parent dir and the
2117 +  file.
2118 +
2119 +And of course, in every step, an error may happen. So the operation
2120 +should restore the original file state after an error happens.
2121 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06mmap.txt linux/Documentation/filesystems/aufs/design/06mmap.txt
2122 --- /usr/share/empty/Documentation/filesystems/aufs/design/06mmap.txt   1970-01-01 01:00:00.000000000 +0100
2123 +++ linux/Documentation/filesystems/aufs/design/06mmap.txt      2021-11-01 23:48:34.203025928 +0100
2124 @@ -0,0 +1,72 @@
2125 +
2126 +# Copyright (C) 2005-2021 Junjiro R. Okajima
2127 +# 
2128 +# This program is free software; you can redistribute it and/or modify
2129 +# it under the terms of the GNU General Public License as published by
2130 +# the Free Software Foundation; either version 2 of the License, or
2131 +# (at your option) any later version.
2132 +# 
2133 +# This program is distributed in the hope that it will be useful,
2134 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2135 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2136 +# GNU General Public License for more details.
2137 +# 
2138 +# You should have received a copy of the GNU General Public License
2139 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2140 +
2141 +mmap(2) -- File Memory Mapping
2142 +----------------------------------------------------------------------
2143 +In aufs, the file-mapped pages are handled by a branch fs directly, no
2144 +interaction with aufs. It means aufs_mmap() calls the branch fs's
2145 +->mmap().
2146 +This approach is simple and good, but there is one problem.
2147 +Under /proc, several entries show the mmapped files by its path (with
2148 +device and inode number), and the printed path will be the path on the
2149 +branch fs's instead of virtual aufs's.
2150 +This is not a problem in most cases, but some utilities lsof(1) (and its
2151 +user) may expect the path on aufs.
2152 +
2153 +To address this issue, aufs adds a new member called vm_prfile in struct
2154 +vm_area_struct (and struct vm_region). The original vm_file points to
2155 +the file on the branch fs in order to handle everything correctly as
2156 +usual. The new vm_prfile points to a virtual file in aufs, and the
2157 +show-functions in procfs refers to vm_prfile if it is set.
2158 +Also we need to maintain several other places where touching vm_file
2159 +such like
2160 +- fork()/clone() copies vma and the reference count of vm_file is
2161 +  incremented.
2162 +- merging vma maintains the ref count too.
2163 +
2164 +This is not a good approach. It just fakes the printed path. But it
2165 +leaves all behaviour around f_mapping unchanged. This is surely an
2166 +advantage.
2167 +Actually aufs had adopted another complicated approach which calls
2168 +generic_file_mmap() and handles struct vm_operations_struct. In this
2169 +approach, aufs met a hard problem and I could not solve it without
2170 +switching the approach.
2171 +
2172 +There may be one more another approach which is
2173 +- bind-mount the branch-root onto the aufs-root internally
2174 +- grab the new vfsmount (ie. struct mount)
2175 +- lazy-umount the branch-root internally
2176 +- in open(2) the aufs-file, open the branch-file with the hidden
2177 +  vfsmount (instead of the original branch's vfsmount)
2178 +- ideally this "bind-mount and lazy-umount" should be done atomically,
2179 +  but it may be possible from userspace by the mount helper.
2180 +
2181 +Adding the internal hidden vfsmount and using it in opening a file, the
2182 +file path under /proc will be printed correctly. This approach looks
2183 +smarter, but is not possible I am afraid.
2184 +- aufs-root may be bind-mount later. when it happens, another hidden
2185 +  vfsmount will be required.
2186 +- it is hard to get the chance to bind-mount and lazy-umount
2187 +  + in kernel-space, FS can have vfsmount in open(2) via
2188 +    file->f_path, and aufs can know its vfsmount. But several locks are
2189 +    already acquired, and if aufs tries to bind-mount and lazy-umount
2190 +    here, then it may cause a deadlock.
2191 +  + in user-space, bind-mount doesn't invoke the mount helper.
2192 +- since /proc shows dev and ino, aufs has to give vma these info. it
2193 +  means a new member vm_prinode will be necessary. this is essentially
2194 +  equivalent to vm_prfile described above.
2195 +
2196 +I have to give up this "looks-smater" approach.
2197 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06xattr.txt linux/Documentation/filesystems/aufs/design/06xattr.txt
2198 --- /usr/share/empty/Documentation/filesystems/aufs/design/06xattr.txt  1970-01-01 01:00:00.000000000 +0100
2199 +++ linux/Documentation/filesystems/aufs/design/06xattr.txt     2021-11-01 23:48:34.203025928 +0100
2200 @@ -0,0 +1,96 @@
2201 +
2202 +# Copyright (C) 2014-2021 Junjiro R. Okajima
2203 +#
2204 +# This program is free software; you can redistribute it and/or modify
2205 +# it under the terms of the GNU General Public License as published by
2206 +# the Free Software Foundation; either version 2 of the License, or
2207 +# (at your option) any later version.
2208 +#
2209 +# This program is distributed in the hope that it will be useful,
2210 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2211 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2212 +# GNU General Public License for more details.
2213 +#
2214 +# You should have received a copy of the GNU General Public License
2215 +# along with this program; if not, write to the Free Software
2216 +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
2217 +
2218 +
2219 +Listing XATTR/EA and getting the value
2220 +----------------------------------------------------------------------
2221 +For the inode standard attributes (owner, group, timestamps, etc.), aufs
2222 +shows the values from the topmost existing file. This behaviour is good
2223 +for the non-dir entries since the bahaviour exactly matches the shown
2224 +information. But for the directories, aufs considers all the same named
2225 +entries on the lower branches. Which means, if one of the lower entry
2226 +rejects readdir call, then aufs returns an error even if the topmost
2227 +entry allows it. This behaviour is necessary to respect the branch fs's
2228 +security, but can make users confused since the user-visible standard
2229 +attributes don't match the behaviour.
2230 +To address this issue, aufs has a mount option called dirperm1 which
2231 +checks the permission for the topmost entry only, and ignores the lower
2232 +entry's permission.
2233 +
2234 +A similar issue can happen around XATTR.
2235 +getxattr(2) and listxattr(2) families behave as if dirperm1 option is
2236 +always set. Otherwise these very unpleasant situation would happen.
2237 +- listxattr(2) may return the duplicated entries.
2238 +- users may not be able to remove or reset the XATTR forever,
2239 +
2240 +
2241 +XATTR/EA support in the internal (copy,move)-(up,down)
2242 +----------------------------------------------------------------------
2243 +Generally the extended attributes of inode are categorized as these.
2244 +- "security" for LSM and capability.
2245 +- "system" for posix ACL, 'acl' mount option is required for the branch
2246 +  fs generally.
2247 +- "trusted" for userspace, CAP_SYS_ADMIN is required.
2248 +- "user" for userspace, 'user_xattr' mount option is required for the
2249 +  branch fs generally.
2250 +
2251 +Moreover there are some other categories. Aufs handles these rather
2252 +unpopular categories as the ordinary ones, ie. there is no special
2253 +condition nor exception.
2254 +
2255 +In copy-up, the support for XATTR on the dst branch may differ from the
2256 +src branch. In this case, the copy-up operation will get an error and
2257 +the original user operation which triggered the copy-up will fail. It
2258 +can happen that even all copy-up will fail.
2259 +When both of src and dst branches support XATTR and if an error occurs
2260 +during copying XATTR, then the copy-up should fail obviously. That is a
2261 +good reason and aufs should return an error to userspace. But when only
2262 +the src branch support that XATTR, aufs should not return an error.
2263 +For example, the src branch supports ACL but the dst branch doesn't
2264 +because the dst branch may natively un-support it or temporary
2265 +un-support it due to "noacl" mount option. Of course, the dst branch fs
2266 +may NOT return an error even if the XATTR is not supported. It is
2267 +totally up to the branch fs.
2268 +
2269 +Anyway when the aufs internal copy-up gets an error from the dst branch
2270 +fs, then aufs tries removing the just copied entry and returns the error
2271 +to the userspace. The worst case of this situation will be all copy-up
2272 +will fail.
2273 +
2274 +For the copy-up operation, there two basic approaches.
2275 +- copy the specified XATTR only (by category above), and return the
2276 +  error unconditionally if it happens.
2277 +- copy all XATTR, and ignore the error on the specified category only.
2278 +
2279 +In order to support XATTR and to implement the correct behaviour, aufs
2280 +chooses the latter approach and introduces some new branch attributes,
2281 +"icexsec", "icexsys", "icextr", "icexusr", and "icexoth".
2282 +They correspond to the XATTR namespaces (see above). Additionally, to be
2283 +convenient, "icex" is also provided which means all "icex*" attributes
2284 +are set (here the word "icex" stands for "ignore copy-error on XATTR").
2285 +
2286 +The meaning of these attributes is to ignore the error from setting
2287 +XATTR on that branch.
2288 +Note that aufs tries copying all XATTR unconditionally, and ignores the
2289 +error from the dst branch according to the specified attributes.
2290 +
2291 +Some XATTR may have its default value. The default value may come from
2292 +the parent dir or the environment. If the default value is set at the
2293 +file creating-time, it will be overwritten by copy-up.
2294 +Some contradiction may happen I am afraid.
2295 +Do we need another attribute to stop copying XATTR? I am unsure. For
2296 +now, aufs implements the branch attributes to ignore the error.
2297 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/07export.txt linux/Documentation/filesystems/aufs/design/07export.txt
2298 --- /usr/share/empty/Documentation/filesystems/aufs/design/07export.txt 1970-01-01 01:00:00.000000000 +0100
2299 +++ linux/Documentation/filesystems/aufs/design/07export.txt    2021-11-01 23:48:34.203025928 +0100
2300 @@ -0,0 +1,58 @@
2301 +
2302 +# Copyright (C) 2005-2021 Junjiro R. Okajima
2303 +# 
2304 +# This program is free software; you can redistribute it and/or modify
2305 +# it under the terms of the GNU General Public License as published by
2306 +# the Free Software Foundation; either version 2 of the License, or
2307 +# (at your option) any later version.
2308 +# 
2309 +# This program is distributed in the hope that it will be useful,
2310 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2311 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2312 +# GNU General Public License for more details.
2313 +# 
2314 +# You should have received a copy of the GNU General Public License
2315 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2316 +
2317 +Export Aufs via NFS
2318 +----------------------------------------------------------------------
2319 +Here is an approach.
2320 +- like xino/xib, add a new file 'xigen' which stores aufs inode
2321 +  generation.
2322 +- iget_locked(): initialize aufs inode generation for a new inode, and
2323 +  store it in xigen file.
2324 +- destroy_inode(): increment aufs inode generation and store it in xigen
2325 +  file. it is necessary even if it is not unlinked, because any data of
2326 +  inode may be changed by UDBA.
2327 +- encode_fh(): for a root dir, simply return FILEID_ROOT. otherwise
2328 +  build file handle by
2329 +  + branch id (4 bytes)
2330 +  + superblock generation (4 bytes)
2331 +  + inode number (4 or 8 bytes)
2332 +  + parent dir inode number (4 or 8 bytes)
2333 +  + inode generation (4 bytes))
2334 +  + return value of exportfs_encode_fh() for the parent on a branch (4
2335 +    bytes)
2336 +  + file handle for a branch (by exportfs_encode_fh())
2337 +- fh_to_dentry():
2338 +  + find the index of a branch from its id in handle, and check it is
2339 +    still exist in aufs.
2340 +  + 1st level: get the inode number from handle and search it in cache.
2341 +  + 2nd level: if not found in cache, get the parent inode number from
2342 +    the handle and search it in cache. and then open the found parent
2343 +    dir, find the matching inode number by vfs_readdir() and get its
2344 +    name, and call lookup_one_len() for the target dentry.
2345 +  + 3rd level: if the parent dir is not cached, call
2346 +    exportfs_decode_fh() for a branch and get the parent on a branch,
2347 +    build a pathname of it, convert it a pathname in aufs, call
2348 +    path_lookup(). now aufs gets a parent dir dentry, then handle it as
2349 +    the 2nd level.
2350 +  + to open the dir, aufs needs struct vfsmount. aufs keeps vfsmount
2351 +    for every branch, but not itself. to get this, (currently) aufs
2352 +    searches in current->nsproxy->mnt_ns list. it may not be a good
2353 +    idea, but I didn't get other approach.
2354 +  + test the generation of the gotten inode.
2355 +- every inode operation: they may get EBUSY due to UDBA. in this case,
2356 +  convert it into ESTALE for NFSD.
2357 +- readdir(): call lockdep_on/off() because filldir in NFSD calls
2358 +  lookup_one_len(), vfs_getattr(), encode_fh() and others.
2359 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/08shwh.txt linux/Documentation/filesystems/aufs/design/08shwh.txt
2360 --- /usr/share/empty/Documentation/filesystems/aufs/design/08shwh.txt   1970-01-01 01:00:00.000000000 +0100
2361 +++ linux/Documentation/filesystems/aufs/design/08shwh.txt      2021-11-01 23:48:34.203025928 +0100
2362 @@ -0,0 +1,52 @@
2363 +
2364 +# Copyright (C) 2005-2021 Junjiro R. Okajima
2365 +# 
2366 +# This program is free software; you can redistribute it and/or modify
2367 +# it under the terms of the GNU General Public License as published by
2368 +# the Free Software Foundation; either version 2 of the License, or
2369 +# (at your option) any later version.
2370 +# 
2371 +# This program is distributed in the hope that it will be useful,
2372 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2373 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2374 +# GNU General Public License for more details.
2375 +# 
2376 +# You should have received a copy of the GNU General Public License
2377 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2378 +
2379 +Show Whiteout Mode (shwh)
2380 +----------------------------------------------------------------------
2381 +Generally aufs hides the name of whiteouts. But in some cases, to show
2382 +them is very useful for users. For instance, creating a new middle layer
2383 +(branch) by merging existing layers.
2384 +
2385 +(borrowing aufs1 HOW-TO from a user, Michael Towers)
2386 +When you have three branches,
2387 +- Bottom: 'system', squashfs (underlying base system), read-only
2388 +- Middle: 'mods', squashfs, read-only
2389 +- Top: 'overlay', ram (tmpfs), read-write
2390 +
2391 +The top layer is loaded at boot time and saved at shutdown, to preserve
2392 +the changes made to the system during the session.
2393 +When larger changes have been made, or smaller changes have accumulated,
2394 +the size of the saved top layer data grows. At this point, it would be
2395 +nice to be able to merge the two overlay branches ('mods' and 'overlay')
2396 +and rewrite the 'mods' squashfs, clearing the top layer and thus
2397 +restoring save and load speed.
2398 +
2399 +This merging is simplified by the use of another aufs mount, of just the
2400 +two overlay branches using the 'shwh' option.
2401 +# mount -t aufs -o ro,shwh,br:/livesys/overlay=ro+wh:/livesys/mods=rr+wh \
2402 +       aufs /livesys/merge_union
2403 +
2404 +A merged view of these two branches is then available at
2405 +/livesys/merge_union, and the new feature is that the whiteouts are
2406 +visible!
2407 +Note that in 'shwh' mode the aufs mount must be 'ro', which will disable
2408 +writing to all branches. Also the default mode for all branches is 'ro'.
2409 +It is now possible to save the combined contents of the two overlay
2410 +branches to a new squashfs, e.g.:
2411 +# mksquashfs /livesys/merge_union /path/to/newmods.squash
2412 +
2413 +This new squashfs archive can be stored on the boot device and the
2414 +initramfs will use it to replace the old one at the next boot.
2415 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/10dynop.txt linux/Documentation/filesystems/aufs/design/10dynop.txt
2416 --- /usr/share/empty/Documentation/filesystems/aufs/design/10dynop.txt  1970-01-01 01:00:00.000000000 +0100
2417 +++ linux/Documentation/filesystems/aufs/design/10dynop.txt     2021-11-01 23:48:34.203025928 +0100
2418 @@ -0,0 +1,47 @@
2419 +
2420 +# Copyright (C) 2010-2021 Junjiro R. Okajima
2421 +#
2422 +# This program is free software; you can redistribute it and/or modify
2423 +# it under the terms of the GNU General Public License as published by
2424 +# the Free Software Foundation; either version 2 of the License, or
2425 +# (at your option) any later version.
2426 +#
2427 +# This program is distributed in the hope that it will be useful,
2428 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2429 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2430 +# GNU General Public License for more details.
2431 +#
2432 +# You should have received a copy of the GNU General Public License
2433 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2434 +
2435 +Dynamically customizable FS operations
2436 +----------------------------------------------------------------------
2437 +Generally FS operations (struct inode_operations, struct
2438 +address_space_operations, struct file_operations, etc.) are defined as
2439 +"static const", but it never means that FS have only one set of
2440 +operation. Some FS have multiple sets of them. For instance, ext2 has
2441 +three sets, one for XIP, for NOBH, and for normal.
2442 +Since aufs overrides and redirects these operations, sometimes aufs has
2443 +to change its behaviour according to the branch FS type. More importantly
2444 +VFS acts differently if a function (member in the struct) is set or
2445 +not. It means aufs should have several sets of operations and select one
2446 +among them according to the branch FS definition.
2447 +
2448 +In order to solve this problem and not to affect the behaviour of VFS,
2449 +aufs defines these operations dynamically. For instance, aufs defines
2450 +dummy direct_IO function for struct address_space_operations, but it may
2451 +not be set to the address_space_operations actually. When the branch FS
2452 +doesn't have it, aufs doesn't set it to its address_space_operations
2453 +while the function definition itself is still alive. So the behaviour
2454 +itself will not change, and it will return an error when direct_IO is
2455 +not set.
2456 +
2457 +The lifetime of these dynamically generated operation object is
2458 +maintained by aufs branch object. When the branch is removed from aufs,
2459 +the reference counter of the object is decremented. When it reaches
2460 +zero, the dynamically generated operation object will be freed.
2461 +
2462 +This approach is designed to support AIO (io_submit), Direct I/O and
2463 +XIP (DAX) mainly.
2464 +Currently this approach is applied to address_space_operations for
2465 +regular files only.
2466 diff -urN /usr/share/empty/Documentation/filesystems/aufs/README linux/Documentation/filesystems/aufs/README
2467 --- /usr/share/empty/Documentation/filesystems/aufs/README      1970-01-01 01:00:00.000000000 +0100
2468 +++ linux/Documentation/filesystems/aufs/README 2021-11-01 23:48:34.199692595 +0100
2469 @@ -0,0 +1,396 @@
2470 +
2471 +Aufs5 -- advanced multi layered unification filesystem version 5.x
2472 +http://aufs.sf.net
2473 +Junjiro R. Okajima
2474 +
2475 +
2476 +0. Introduction
2477 +----------------------------------------
2478 +In the early days, aufs was entirely re-designed and re-implemented
2479 +Unionfs Version 1.x series. Adding many original ideas, approaches,
2480 +improvements and implementations, it became totally different from
2481 +Unionfs while keeping the basic features.
2482 +Later, Unionfs Version 2.x series began taking some of the same
2483 +approaches to aufs1's.
2484 +Unionfs was being developed by Professor Erez Zadok at Stony Brook
2485 +University and his team.
2486 +
2487 +Aufs5 supports linux-v5.0 and later, If you want older kernel version
2488 +support,
2489 +- for linux-v4.x series, try aufs4-linux.git or aufs4-standalone.git
2490 +- for linux-v3.x series, try aufs3-linux.git or aufs3-standalone.git
2491 +- for linux-v2.6.16 and later, try aufs2-2.6.git, aufs2-standalone.git
2492 +  or aufs1 from CVS on SourceForge.
2493 +
2494 +Note: it becomes clear that "Aufs was rejected. Let's give it up."
2495 +      According to Christoph Hellwig, linux rejects all union-type
2496 +      filesystems but UnionMount.
2497 +<http://marc.info/?l=linux-kernel&m=123938533724484&w=2>
2498 +
2499 +PS. Al Viro seems have a plan to merge aufs as well as overlayfs and
2500 +    UnionMount, and he pointed out an issue around a directory mutex
2501 +    lock and aufs addressed it. But it is still unsure whether aufs will
2502 +    be merged (or any other union solution).
2503 +<http://marc.info/?l=linux-kernel&m=136312705029295&w=1>
2504 +
2505 +
2506 +1. Features
2507 +----------------------------------------
2508 +- unite several directories into a single virtual filesystem. The member
2509 +  directory is called as a branch.
2510 +- you can specify the permission flags to the branch, which are 'readonly',
2511 +  'readwrite' and 'whiteout-able.'
2512 +- by upper writable branch, internal copyup and whiteout, files/dirs on
2513 +  readonly branch are modifiable logically.
2514 +- dynamic branch manipulation, add, del.
2515 +- etc...
2516 +
2517 +Also there are many enhancements in aufs, such as:
2518 +- test only the highest one for the directory permission (dirperm1)
2519 +- copyup on open (coo=)
2520 +- 'move' policy for copy-up between two writable branches, after
2521 +  checking free space.
2522 +- xattr, acl
2523 +- readdir(3) in userspace.
2524 +- keep inode number by external inode number table
2525 +- keep the timestamps of file/dir in internal copyup operation
2526 +- seekable directory, supporting NFS readdir.
2527 +- whiteout is hardlinked in order to reduce the consumption of inodes
2528 +  on branch
2529 +- do not copyup, nor create a whiteout when it is unnecessary
2530 +- revert a single systemcall when an error occurs in aufs
2531 +- remount interface instead of ioctl
2532 +- maintain /etc/mtab by an external command, /sbin/mount.aufs.
2533 +- loopback mounted filesystem as a branch
2534 +- kernel thread for removing the dir who has a plenty of whiteouts
2535 +- support copyup sparse file (a file which has a 'hole' in it)
2536 +- default permission flags for branches
2537 +- selectable permission flags for ro branch, whether whiteout can
2538 +  exist or not
2539 +- export via NFS.
2540 +- support <sysfs>/fs/aufs and <debugfs>/aufs.
2541 +- support multiple writable branches, some policies to select one
2542 +  among multiple writable branches.
2543 +- a new semantics for link(2) and rename(2) to support multiple
2544 +  writable branches.
2545 +- no glibc changes are required.
2546 +- pseudo hardlink (hardlink over branches)
2547 +- allow a direct access manually to a file on branch, e.g. bypassing aufs.
2548 +  including NFS or remote filesystem branch.
2549 +- userspace wrapper for pathconf(3)/fpathconf(3) with _PC_LINK_MAX.
2550 +- and more...
2551 +
2552 +Currently these features are dropped temporary from aufs5.
2553 +See design/08plan.txt in detail.
2554 +- nested mount, i.e. aufs as readonly no-whiteout branch of another aufs
2555 +  (robr)
2556 +- statistics of aufs thread (/sys/fs/aufs/stat)
2557 +
2558 +Features or just an idea in the future (see also design/*.txt),
2559 +- reorder the branch index without del/re-add.
2560 +- permanent xino files for NFSD
2561 +- an option for refreshing the opened files after add/del branches
2562 +- light version, without branch manipulation. (unnecessary?)
2563 +- copyup in userspace
2564 +- inotify in userspace
2565 +- readv/writev
2566 +
2567 +
2568 +2. Download
2569 +----------------------------------------
2570 +There are three GIT trees for aufs5, aufs5-linux.git,
2571 +aufs5-standalone.git, and aufs-util.git. Note that there is no "5" in
2572 +"aufs-util.git."
2573 +While the aufs-util is always necessary, you need either of aufs5-linux
2574 +or aufs5-standalone.
2575 +
2576 +The aufs5-linux tree includes the whole linux mainline GIT tree,
2577 +git://git.kernel.org/.../torvalds/linux.git.
2578 +And you cannot select CONFIG_AUFS_FS=m for this version, eg. you cannot
2579 +build aufs5 as an external kernel module.
2580 +Several extra patches are not included in this tree. Only
2581 +aufs5-standalone tree contains them. They are described in the later
2582 +section "Configuration and Compilation."
2583 +
2584 +On the other hand, the aufs5-standalone tree has only aufs source files
2585 +and necessary patches, and you can select CONFIG_AUFS_FS=m.
2586 +But you need to apply all aufs patches manually.
2587 +
2588 +You will find GIT branches whose name is in form of "aufs5.x" where "x"
2589 +represents the linux kernel version, "linux-5.x". For instance,
2590 +"aufs5.0" is for linux-5.0. For latest "linux-5.x-rcN", use
2591 +"aufs5.x-rcN" branch.
2592 +
2593 +o aufs5-linux tree
2594 +$ git clone --reference /your/linux/git/tree \
2595 +       git://github.com/sfjro/aufs5-linux.git aufs5-linux.git
2596 +- if you don't have linux GIT tree, then remove "--reference ..."
2597 +$ cd aufs5-linux.git
2598 +$ git checkout origin/aufs5.0
2599 +
2600 +Or You may want to directly git-pull aufs into your linux GIT tree, and
2601 +leave the patch-work to GIT.
2602 +$ cd /your/linux/git/tree
2603 +$ git remote add aufs5 git://github.com/sfjro/aufs5-linux.git
2604 +$ git fetch aufs5
2605 +$ git checkout -b my5.0 v5.0
2606 +$ (add your local change...)
2607 +$ git pull aufs5 aufs5.0
2608 +- now you have v5.0 + your_changes + aufs5.0 in you my5.0 branch.
2609 +- you may need to solve some conflicts between your_changes and
2610 +  aufs5.0. in this case, git-rerere is recommended so that you can
2611 +  solve the similar conflicts automatically when you upgrade to 5.1 or
2612 +  later in the future.
2613 +
2614 +o aufs5-standalone tree
2615 +$ git clone git://github.com/sfjro/aufs5-standalone.git aufs5-standalone.git
2616 +$ cd aufs5-standalone.git
2617 +$ git checkout origin/aufs5.0
2618 +
2619 +o aufs-util tree
2620 +$ git clone git://git.code.sf.net/p/aufs/aufs-util aufs-util.git
2621 +- note that the public aufs-util.git is on SourceForge instead of
2622 +  GitHUB.
2623 +$ cd aufs-util.git
2624 +$ git checkout origin/aufs5.0
2625 +
2626 +Note: The 5.x-rcN branch is to be used with `rc' kernel versions ONLY.
2627 +The minor version number, 'x' in '5.x', of aufs may not always
2628 +follow the minor version number of the kernel.
2629 +Because changes in the kernel that cause the use of a new
2630 +minor version number do not always require changes to aufs-util.
2631 +
2632 +Since aufs-util has its own minor version number, you may not be
2633 +able to find a GIT branch in aufs-util for your kernel's
2634 +exact minor version number.
2635 +In this case, you should git-checkout the branch for the
2636 +nearest lower number.
2637 +
2638 +For (an unreleased) example:
2639 +If you are using "linux-5.10" and the "aufs5.10" branch
2640 +does not exist in aufs-util repository, then "aufs5.9", "aufs5.8"
2641 +or something numerically smaller is the branch for your kernel.
2642 +
2643 +Also you can view all branches by
2644 +       $ git branch -a
2645 +
2646 +
2647 +3. Configuration and Compilation
2648 +----------------------------------------
2649 +Make sure you have git-checkout'ed the correct branch.
2650 +
2651 +For aufs5-linux tree,
2652 +- enable CONFIG_AUFS_FS.
2653 +- set other aufs configurations if necessary.
2654 +- for aufs5.13 and later
2655 +  Because aufs is not only an ordinary filesystem (callee of VFS), but
2656 +  also a caller of VFS functions for branch filesystems, subclassing of
2657 +  the internal locks for LOCKDEP is necessary. LOCKDEP is a debugging
2658 +  feature of linux kernel. If you enable CONFIG_LOCKDEP, then you will
2659 +  need to customize some LOCKDEP numbers. Here are what I use on my
2660 +  test environment.
2661 +       CONFIG_LOCKDEP_BITS=21
2662 +       CONFIG_LOCKDEP_CHAINS_BITS=21
2663 +       CONFIG_LOCKDEP_STACK_TRACE_BITS=24
2664 +
2665 +For aufs5-standalone tree,
2666 +There are several ways to build.
2667 +
2668 +1.
2669 +- apply ./aufs5-kbuild.patch to your kernel source files.
2670 +- apply ./aufs5-base.patch too.
2671 +- apply ./aufs5-mmap.patch too.
2672 +- apply ./aufs5-standalone.patch too, if you have a plan to set
2673 +  CONFIG_AUFS_FS=m. otherwise you don't need ./aufs5-standalone.patch.
2674 +- copy ./{Documentation,fs,include/uapi/linux/aufs_type.h} files to your
2675 +  kernel source tree. Never copy $PWD/include/uapi/linux/Kbuild.
2676 +- enable CONFIG_AUFS_FS, you can select either
2677 +  =m or =y.
2678 +- and build your kernel as usual.
2679 +- install the built kernel.
2680 +- install the header files too by "make headers_install" to the
2681 +  directory where you specify. By default, it is $PWD/usr.
2682 +  "make help" shows a brief note for headers_install.
2683 +- and reboot your system.
2684 +
2685 +2.
2686 +- module only (CONFIG_AUFS_FS=m).
2687 +- apply ./aufs5-base.patch to your kernel source files.
2688 +- apply ./aufs5-mmap.patch too.
2689 +- apply ./aufs5-standalone.patch too.
2690 +- build your kernel, don't forget "make headers_install", and reboot.
2691 +- edit ./config.mk and set other aufs configurations if necessary.
2692 +  Note: You should read $PWD/fs/aufs/Kconfig carefully which describes
2693 +  every aufs configurations.
2694 +- build the module by simple "make".
2695 +- you can specify ${KDIR} make variable which points to your kernel
2696 +  source tree.
2697 +- install the files
2698 +  + run "make install" to install the aufs module, or copy the built
2699 +    $PWD/aufs.ko to /lib/modules/... and run depmod -a (or reboot simply).
2700 +  + run "make install_headers" (instead of headers_install) to install
2701 +    the modified aufs header file (you can specify DESTDIR which is
2702 +    available in aufs standalone version's Makefile only), or copy
2703 +    $PWD/usr/include/linux/aufs_type.h to /usr/include/linux or wherever
2704 +    you like manually. By default, the target directory is $PWD/usr.
2705 +- no need to apply aufs5-kbuild.patch, nor copying source files to your
2706 +  kernel source tree.
2707 +
2708 +Note: The header file aufs_type.h is necessary to build aufs-util
2709 +      as well as "make headers_install" in the kernel source tree.
2710 +      headers_install is subject to be forgotten, but it is essentially
2711 +      necessary, not only for building aufs-util.
2712 +      You may not meet problems without headers_install in some older
2713 +      version though.
2714 +
2715 +And then,
2716 +- read README in aufs-util, build and install it
2717 +- note that your distribution may contain an obsoleted version of
2718 +  aufs_type.h in /usr/include/linux or something. When you build aufs
2719 +  utilities, make sure that your compiler refers the correct aufs header
2720 +  file which is built by "make headers_install."
2721 +- if you want to use readdir(3) in userspace or pathconf(3) wrapper,
2722 +  then run "make install_ulib" too. And refer to the aufs manual in
2723 +  detail.
2724 +
2725 +There several other patches in aufs5-standalone.git. They are all
2726 +optional. When you meet some problems, they will help you.
2727 +- aufs5-loopback.patch
2728 +  Supports a nested loopback mount in a branch-fs. This patch is
2729 +  unnecessary until aufs produces a message like "you may want to try
2730 +  another patch for loopback file".
2731 +- vfs-ino.patch
2732 +  Modifies a system global kernel internal function get_next_ino() in
2733 +  order to stop assigning 0 for an inode-number. Not directly related to
2734 +  aufs, but recommended generally.
2735 +- tmpfs-idr.patch
2736 +  Keeps the tmpfs inode number as the lowest value. Effective to reduce
2737 +  the size of aufs XINO files for tmpfs branch. Also it prevents the
2738 +  duplication of inode number, which is important for backup tools and
2739 +  other utilities. When you find aufs XINO files for tmpfs branch
2740 +  growing too much, try this patch.
2741 +
2742 +
2743 +4. Usage
2744 +----------------------------------------
2745 +At first, make sure aufs-util are installed, and please read the aufs
2746 +manual, aufs.5 in aufs-util.git tree.
2747 +$ man -l aufs.5
2748 +
2749 +And then,
2750 +$ mkdir /tmp/rw /tmp/aufs
2751 +# mount -t aufs -o br=/tmp/rw:${HOME} none /tmp/aufs
2752 +
2753 +Here is another example. The result is equivalent.
2754 +# mount -t aufs -o br=/tmp/rw=rw:${HOME}=ro none /tmp/aufs
2755 +  Or
2756 +# mount -t aufs -o br:/tmp/rw none /tmp/aufs
2757 +# mount -o remount,append:${HOME} /tmp/aufs
2758 +
2759 +Then, you can see whole tree of your home dir through /tmp/aufs. If
2760 +you modify a file under /tmp/aufs, the one on your home directory is
2761 +not affected, instead the same named file will be newly created under
2762 +/tmp/rw. And all of your modification to a file will be applied to
2763 +the one under /tmp/rw. This is called the file based Copy on Write
2764 +(COW) method.
2765 +Aufs mount options are described in aufs.5.
2766 +If you run chroot or something and make your aufs as a root directory,
2767 +then you need to customize the shutdown script. See the aufs manual in
2768 +detail.
2769 +
2770 +Additionally, there are some sample usages of aufs which are a
2771 +diskless system with network booting, and LiveCD over NFS.
2772 +See sample dir in CVS tree on SourceForge.
2773 +
2774 +
2775 +5. Contact
2776 +----------------------------------------
2777 +When you have any problems or strange behaviour in aufs, please let me
2778 +know with:
2779 +- /proc/mounts (instead of the output of mount(8))
2780 +- /sys/module/aufs/*
2781 +- /sys/fs/aufs/* (if you have them)
2782 +- /debug/aufs/* (if you have them)
2783 +- linux kernel version
2784 +  if your kernel is not plain, for example modified by distributor,
2785 +  the url where i can download its source is necessary too.
2786 +- aufs version which was printed at loading the module or booting the
2787 +  system, instead of the date you downloaded.
2788 +- configuration (define/undefine CONFIG_AUFS_xxx)
2789 +- kernel configuration or /proc/config.gz (if you have it)
2790 +- LSM (linux security module, if you are using)
2791 +- behaviour which you think to be incorrect
2792 +- actual operation, reproducible one is better
2793 +- mailto: aufs-users at lists.sourceforge.net
2794 +
2795 +Usually, I don't watch the Public Areas(Bugs, Support Requests, Patches,
2796 +and Feature Requests) on SourceForge. Please join and write to
2797 +aufs-users ML.
2798 +
2799 +
2800 +6. Acknowledgements
2801 +----------------------------------------
2802 +Thanks to everyone who have tried and are using aufs, whoever
2803 +have reported a bug or any feedback.
2804 +
2805 +Especially donators:
2806 +Tomas Matejicek(slax.org) made a donation (much more than once).
2807 +       Since Apr 2010, Tomas M (the author of Slax and Linux Live
2808 +       scripts) is making "doubling" donations.
2809 +       Unfortunately I cannot list all of the donators, but I really
2810 +       appreciate.
2811 +       It ends Aug 2010, but the ordinary donation URL is still available.
2812 +       <http://sourceforge.net/donate/index.php?group_id=167503>
2813 +Dai Itasaka made a donation (2007/8).
2814 +Chuck Smith made a donation (2008/4, 10 and 12).
2815 +Henk Schoneveld made a donation (2008/9).
2816 +Chih-Wei Huang, ASUS, CTC donated Eee PC 4G (2008/10).
2817 +Francois Dupoux made a donation (2008/11).
2818 +Bruno Cesar Ribas and Luis Carlos Erpen de Bona, C3SL serves public
2819 +       aufs2 GIT tree (2009/2).
2820 +William Grant made a donation (2009/3).
2821 +Patrick Lane made a donation (2009/4).
2822 +The Mail Archive (mail-archive.com) made donations (2009/5).
2823 +Nippy Networks (Ed Wildgoose) made a donation (2009/7).
2824 +New Dream Network, LLC (www.dreamhost.com) made a donation (2009/11).
2825 +Pavel Pronskiy made a donation (2011/2).
2826 +Iridium and Inmarsat satellite phone retailer (www.mailasail.com), Nippy
2827 +       Networks (Ed Wildgoose) made a donation for hardware (2011/3).
2828 +Max Lekomcev (DOM-TV project) made a donation (2011/7, 12, 2012/3, 6 and
2829 +11).
2830 +Sam Liddicott made a donation (2011/9).
2831 +Era Scarecrow made a donation (2013/4).
2832 +Bor Ratajc made a donation (2013/4).
2833 +Alessandro Gorreta made a donation (2013/4).
2834 +POIRETTE Marc made a donation (2013/4).
2835 +Alessandro Gorreta made a donation (2013/4).
2836 +lauri kasvandik made a donation (2013/5).
2837 +"pemasu from Finland" made a donation (2013/7).
2838 +The Parted Magic Project made a donation (2013/9 and 11).
2839 +Pavel Barta made a donation (2013/10).
2840 +Nikolay Pertsev made a donation (2014/5).
2841 +James B made a donation (2014/7 and 2015/7).
2842 +Stefano Di Biase made a donation (2014/8).
2843 +Daniel Epellei made a donation (2015/1).
2844 +OmegaPhil made a donation (2016/1, 2018/4).
2845 +Tomasz Szewczyk made a donation (2016/4).
2846 +James Burry made a donation (2016/12).
2847 +Carsten Rose made a donation (2018/9).
2848 +Porteus Kiosk made a donation (2018/10).
2849 +
2850 +Thank you very much.
2851 +Donations are always, including future donations, very important and
2852 +helpful for me to keep on developing aufs.
2853 +
2854 +
2855 +7.
2856 +----------------------------------------
2857 +If you are an experienced user, no explanation is needed. Aufs is
2858 +just a linux filesystem.
2859 +
2860 +
2861 +Enjoy!
2862 +
2863 +# Local variables: ;
2864 +# mode: text;
2865 +# End: ;
2866 diff -urN /usr/share/empty/fs/aufs/aufs.h linux/fs/aufs/aufs.h
2867 --- /usr/share/empty/fs/aufs/aufs.h     1970-01-01 01:00:00.000000000 +0100
2868 +++ linux/fs/aufs/aufs.h        2021-11-01 23:48:34.203025928 +0100
2869 @@ -0,0 +1,62 @@
2870 +/* SPDX-License-Identifier: GPL-2.0 */
2871 +/*
2872 + * Copyright (C) 2005-2021 Junjiro R. Okajima
2873 + *
2874 + * This program, aufs is free software; you can redistribute it and/or modify
2875 + * it under the terms of the GNU General Public License as published by
2876 + * the Free Software Foundation; either version 2 of the License, or
2877 + * (at your option) any later version.
2878 + *
2879 + * This program is distributed in the hope that it will be useful,
2880 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2881 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2882 + * GNU General Public License for more details.
2883 + *
2884 + * You should have received a copy of the GNU General Public License
2885 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
2886 + */
2887 +
2888 +/*
2889 + * all header files
2890 + */
2891 +
2892 +#ifndef __AUFS_H__
2893 +#define __AUFS_H__
2894 +
2895 +#ifdef __KERNEL__
2896 +
2897 +#define AuStub(type, name, body, ...) \
2898 +       static inline type name(__VA_ARGS__) { body; }
2899 +
2900 +#define AuStubVoid(name, ...) \
2901 +       AuStub(void, name, , __VA_ARGS__)
2902 +#define AuStubInt0(name, ...) \
2903 +       AuStub(int, name, return 0, __VA_ARGS__)
2904 +
2905 +#include "debug.h"
2906 +
2907 +#include "branch.h"
2908 +#include "cpup.h"
2909 +#include "dcsub.h"
2910 +#include "dbgaufs.h"
2911 +#include "dentry.h"
2912 +#include "dir.h"
2913 +#include "dirren.h"
2914 +#include "dynop.h"
2915 +#include "file.h"
2916 +#include "fstype.h"
2917 +#include "hbl.h"
2918 +#include "inode.h"
2919 +#include "lcnt.h"
2920 +#include "loop.h"
2921 +#include "module.h"
2922 +#include "opts.h"
2923 +#include "rwsem.h"
2924 +#include "super.h"
2925 +#include "sysaufs.h"
2926 +#include "vfsub.h"
2927 +#include "whout.h"
2928 +#include "wkq.h"
2929 +
2930 +#endif /* __KERNEL__ */
2931 +#endif /* __AUFS_H__ */
2932 diff -urN /usr/share/empty/fs/aufs/branch.c linux/fs/aufs/branch.c
2933 --- /usr/share/empty/fs/aufs/branch.c   1970-01-01 01:00:00.000000000 +0100
2934 +++ linux/fs/aufs/branch.c      2021-11-01 23:48:34.203025928 +0100
2935 @@ -0,0 +1,1427 @@
2936 +// SPDX-License-Identifier: GPL-2.0
2937 +/*
2938 + * Copyright (C) 2005-2021 Junjiro R. Okajima
2939 + *
2940 + * This program, aufs is free software; you can redistribute it and/or modify
2941 + * it under the terms of the GNU General Public License as published by
2942 + * the Free Software Foundation; either version 2 of the License, or
2943 + * (at your option) any later version.
2944 + *
2945 + * This program is distributed in the hope that it will be useful,
2946 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2947 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2948 + * GNU General Public License for more details.
2949 + *
2950 + * You should have received a copy of the GNU General Public License
2951 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
2952 + */
2953 +
2954 +/*
2955 + * branch management
2956 + */
2957 +
2958 +#include <linux/compat.h>
2959 +#include <linux/statfs.h>
2960 +#include "aufs.h"
2961 +
2962 +/*
2963 + * free a single branch
2964 + */
2965 +static void au_br_do_free(struct au_branch *br)
2966 +{
2967 +       int i;
2968 +       struct au_wbr *wbr;
2969 +       struct au_dykey **key;
2970 +
2971 +       au_hnotify_fin_br(br);
2972 +       /* always, regardless the mount option */
2973 +       au_dr_hino_free(&br->br_dirren);
2974 +       au_xino_put(br);
2975 +
2976 +       AuLCntZero(au_lcnt_read(&br->br_nfiles, /*do_rev*/0));
2977 +       au_lcnt_fin(&br->br_nfiles, /*do_sync*/0);
2978 +       AuLCntZero(au_lcnt_read(&br->br_count, /*do_rev*/0));
2979 +       au_lcnt_fin(&br->br_count, /*do_sync*/0);
2980 +
2981 +       wbr = br->br_wbr;
2982 +       if (wbr) {
2983 +               for (i = 0; i < AuBrWh_Last; i++)
2984 +                       dput(wbr->wbr_wh[i]);
2985 +               AuDebugOn(atomic_read(&wbr->wbr_wh_running));
2986 +               AuRwDestroy(&wbr->wbr_wh_rwsem);
2987 +       }
2988 +
2989 +       if (br->br_fhsm) {
2990 +               au_br_fhsm_fin(br->br_fhsm);
2991 +               au_kfree_try_rcu(br->br_fhsm);
2992 +       }
2993 +
2994 +       key = br->br_dykey;
2995 +       for (i = 0; i < AuBrDynOp; i++, key++)
2996 +               if (*key)
2997 +                       au_dy_put(*key);
2998 +               else
2999 +                       break;
3000 +
3001 +       /* recursive lock, s_umount of branch's */
3002 +       /* synchronize_rcu(); */ /* why? */
3003 +       lockdep_off();
3004 +       path_put(&br->br_path);
3005 +       lockdep_on();
3006 +       au_kfree_rcu(wbr);
3007 +       au_lcnt_wait_for_fin(&br->br_nfiles);
3008 +       au_lcnt_wait_for_fin(&br->br_count);
3009 +       /* I don't know why, but percpu_refcount requires this */
3010 +       /* synchronize_rcu(); */
3011 +       au_kfree_rcu(br);
3012 +}
3013 +
3014 +/*
3015 + * frees all branches
3016 + */
3017 +void au_br_free(struct au_sbinfo *sbinfo)
3018 +{
3019 +       aufs_bindex_t bmax;
3020 +       struct au_branch **br;
3021 +
3022 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
3023 +
3024 +       bmax = sbinfo->si_bbot + 1;
3025 +       br = sbinfo->si_branch;
3026 +       while (bmax--)
3027 +               au_br_do_free(*br++);
3028 +}
3029 +
3030 +/*
3031 + * find the index of a branch which is specified by @br_id.
3032 + */
3033 +int au_br_index(struct super_block *sb, aufs_bindex_t br_id)
3034 +{
3035 +       aufs_bindex_t bindex, bbot;
3036 +
3037 +       bbot = au_sbbot(sb);
3038 +       for (bindex = 0; bindex <= bbot; bindex++)
3039 +               if (au_sbr_id(sb, bindex) == br_id)
3040 +                       return bindex;
3041 +       return -1;
3042 +}
3043 +
3044 +/* ---------------------------------------------------------------------- */
3045 +
3046 +/*
3047 + * add a branch
3048 + */
3049 +
3050 +static int test_overlap(struct super_block *sb, struct dentry *h_adding,
3051 +                       struct dentry *h_root)
3052 +{
3053 +       if (unlikely(h_adding == h_root
3054 +                    || au_test_loopback_overlap(sb, h_adding)))
3055 +               return 1;
3056 +       if (h_adding->d_sb != h_root->d_sb)
3057 +               return 0;
3058 +       return au_test_subdir(h_adding, h_root)
3059 +               || au_test_subdir(h_root, h_adding);
3060 +}
3061 +
3062 +/*
3063 + * returns a newly allocated branch. @new_nbranch is a number of branches
3064 + * after adding a branch.
3065 + */
3066 +static struct au_branch *au_br_alloc(struct super_block *sb, int new_nbranch,
3067 +                                    int perm)
3068 +{
3069 +       struct au_branch *add_branch;
3070 +       struct dentry *root;
3071 +       struct inode *inode;
3072 +       int err;
3073 +
3074 +       err = -ENOMEM;
3075 +       add_branch = kzalloc(sizeof(*add_branch), GFP_NOFS);
3076 +       if (unlikely(!add_branch))
3077 +               goto out;
3078 +       add_branch->br_xino = au_xino_alloc(/*nfile*/1);
3079 +       if (unlikely(!add_branch->br_xino))
3080 +               goto out_br;
3081 +       err = au_hnotify_init_br(add_branch, perm);
3082 +       if (unlikely(err))
3083 +               goto out_xino;
3084 +
3085 +       if (au_br_writable(perm)) {
3086 +               /* may be freed separately at changing the branch permission */
3087 +               add_branch->br_wbr = kzalloc(sizeof(*add_branch->br_wbr),
3088 +                                            GFP_NOFS);
3089 +               if (unlikely(!add_branch->br_wbr))
3090 +                       goto out_hnotify;
3091 +       }
3092 +
3093 +       if (au_br_fhsm(perm)) {
3094 +               err = au_fhsm_br_alloc(add_branch);
3095 +               if (unlikely(err))
3096 +                       goto out_wbr;
3097 +       }
3098 +
3099 +       root = sb->s_root;
3100 +       err = au_sbr_realloc(au_sbi(sb), new_nbranch, /*may_shrink*/0);
3101 +       if (!err)
3102 +               err = au_di_realloc(au_di(root), new_nbranch, /*may_shrink*/0);
3103 +       if (!err) {
3104 +               inode = d_inode(root);
3105 +               err = au_hinode_realloc(au_ii(inode), new_nbranch,
3106 +                                       /*may_shrink*/0);
3107 +       }
3108 +       if (!err)
3109 +               return add_branch; /* success */
3110 +
3111 +out_wbr:
3112 +       au_kfree_rcu(add_branch->br_wbr);
3113 +out_hnotify:
3114 +       au_hnotify_fin_br(add_branch);
3115 +out_xino:
3116 +       au_xino_put(add_branch);
3117 +out_br:
3118 +       au_kfree_rcu(add_branch);
3119 +out:
3120 +       return ERR_PTR(err);
3121 +}
3122 +
3123 +/*
3124 + * test if the branch permission is legal or not.
3125 + */
3126 +static int test_br(struct inode *inode, int brperm, char *path)
3127 +{
3128 +       int err;
3129 +
3130 +       err = (au_br_writable(brperm) && IS_RDONLY(inode));
3131 +       if (!err)
3132 +               goto out;
3133 +
3134 +       err = -EINVAL;
3135 +       pr_err("write permission for readonly mount or inode, %s\n", path);
3136 +
3137 +out:
3138 +       return err;
3139 +}
3140 +
3141 +/*
3142 + * returns:
3143 + * 0: success, the caller will add it
3144 + * plus: success, it is already unified, the caller should ignore it
3145 + * minus: error
3146 + */
3147 +static int test_add(struct super_block *sb, struct au_opt_add *add, int remount)
3148 +{
3149 +       int err;
3150 +       aufs_bindex_t bbot, bindex;
3151 +       struct dentry *root, *h_dentry;
3152 +       struct inode *inode, *h_inode;
3153 +
3154 +       root = sb->s_root;
3155 +       bbot = au_sbbot(sb);
3156 +       if (unlikely(bbot >= 0
3157 +                    && au_find_dbindex(root, add->path.dentry) >= 0)) {
3158 +               err = 1;
3159 +               if (!remount) {
3160 +                       err = -EINVAL;
3161 +                       pr_err("%s duplicated\n", add->pathname);
3162 +               }
3163 +               goto out;
3164 +       }
3165 +
3166 +       err = -ENOSPC; /* -E2BIG; */
3167 +       if (unlikely(AUFS_BRANCH_MAX <= add->bindex
3168 +                    || AUFS_BRANCH_MAX - 1 <= bbot)) {
3169 +               pr_err("number of branches exceeded %s\n", add->pathname);
3170 +               goto out;
3171 +       }
3172 +
3173 +       err = -EDOM;
3174 +       if (unlikely(add->bindex < 0 || bbot + 1 < add->bindex)) {
3175 +               pr_err("bad index %d\n", add->bindex);
3176 +               goto out;
3177 +       }
3178 +
3179 +       inode = d_inode(add->path.dentry);
3180 +       err = -ENOENT;
3181 +       if (unlikely(!inode->i_nlink)) {
3182 +               pr_err("no existence %s\n", add->pathname);
3183 +               goto out;
3184 +       }
3185 +
3186 +       err = -EINVAL;
3187 +       if (unlikely(inode->i_sb == sb)) {
3188 +               pr_err("%s must be outside\n", add->pathname);
3189 +               goto out;
3190 +       }
3191 +
3192 +       if (unlikely(au_test_fs_unsuppoted(inode->i_sb))) {
3193 +               pr_err("unsupported filesystem, %s (%s)\n",
3194 +                      add->pathname, au_sbtype(inode->i_sb));
3195 +               goto out;
3196 +       }
3197 +
3198 +       if (unlikely(inode->i_sb->s_stack_depth)) {
3199 +               pr_err("already stacked, %s (%s)\n",
3200 +                      add->pathname, au_sbtype(inode->i_sb));
3201 +               goto out;
3202 +       }
3203 +
3204 +       err = test_br(d_inode(add->path.dentry), add->perm, add->pathname);
3205 +       if (unlikely(err))
3206 +               goto out;
3207 +
3208 +       if (bbot < 0)
3209 +               return 0; /* success */
3210 +
3211 +       err = -EINVAL;
3212 +       for (bindex = 0; bindex <= bbot; bindex++)
3213 +               if (unlikely(test_overlap(sb, add->path.dentry,
3214 +                                         au_h_dptr(root, bindex)))) {
3215 +                       pr_err("%s is overlapped\n", add->pathname);
3216 +                       goto out;
3217 +               }
3218 +
3219 +       err = 0;
3220 +       if (au_opt_test(au_mntflags(sb), WARN_PERM)) {
3221 +               h_dentry = au_h_dptr(root, 0);
3222 +               h_inode = d_inode(h_dentry);
3223 +               if ((h_inode->i_mode & S_IALLUGO) != (inode->i_mode & S_IALLUGO)
3224 +                   || !uid_eq(h_inode->i_uid, inode->i_uid)
3225 +                   || !gid_eq(h_inode->i_gid, inode->i_gid))
3226 +                       pr_warn("uid/gid/perm %s %u/%u/0%o, %u/%u/0%o\n",
3227 +                               add->pathname,
3228 +                               i_uid_read(inode), i_gid_read(inode),
3229 +                               (inode->i_mode & S_IALLUGO),
3230 +                               i_uid_read(h_inode), i_gid_read(h_inode),
3231 +                               (h_inode->i_mode & S_IALLUGO));
3232 +       }
3233 +
3234 +out:
3235 +       return err;
3236 +}
3237 +
3238 +/*
3239 + * initialize or clean the whiteouts for an adding branch
3240 + */
3241 +static int au_br_init_wh(struct super_block *sb, struct au_branch *br,
3242 +                        int new_perm)
3243 +{
3244 +       int err, old_perm;
3245 +       aufs_bindex_t bindex;
3246 +       struct inode *h_inode;
3247 +       struct au_wbr *wbr;
3248 +       struct au_hinode *hdir;
3249 +       struct dentry *h_dentry;
3250 +
3251 +       err = vfsub_mnt_want_write(au_br_mnt(br));
3252 +       if (unlikely(err))
3253 +               goto out;
3254 +
3255 +       wbr = br->br_wbr;
3256 +       old_perm = br->br_perm;
3257 +       br->br_perm = new_perm;
3258 +       hdir = NULL;
3259 +       h_inode = NULL;
3260 +       bindex = au_br_index(sb, br->br_id);
3261 +       if (0 <= bindex) {
3262 +               hdir = au_hi(d_inode(sb->s_root), bindex);
3263 +               au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
3264 +       } else {
3265 +               h_dentry = au_br_dentry(br);
3266 +               h_inode = d_inode(h_dentry);
3267 +               inode_lock_nested(h_inode, AuLsc_I_PARENT);
3268 +       }
3269 +       if (!wbr)
3270 +               err = au_wh_init(br, sb);
3271 +       else {
3272 +               wbr_wh_write_lock(wbr);
3273 +               err = au_wh_init(br, sb);
3274 +               wbr_wh_write_unlock(wbr);
3275 +       }
3276 +       if (hdir)
3277 +               au_hn_inode_unlock(hdir);
3278 +       else
3279 +               inode_unlock(h_inode);
3280 +       vfsub_mnt_drop_write(au_br_mnt(br));
3281 +       br->br_perm = old_perm;
3282 +
3283 +       if (!err && wbr && !au_br_writable(new_perm)) {
3284 +               au_kfree_rcu(wbr);
3285 +               br->br_wbr = NULL;
3286 +       }
3287 +
3288 +out:
3289 +       return err;
3290 +}
3291 +
3292 +static int au_wbr_init(struct au_branch *br, struct super_block *sb,
3293 +                      int perm)
3294 +{
3295 +       int err;
3296 +       struct kstatfs kst;
3297 +       struct au_wbr *wbr;
3298 +
3299 +       wbr = br->br_wbr;
3300 +       au_rw_init(&wbr->wbr_wh_rwsem);
3301 +       atomic_set(&wbr->wbr_wh_running, 0);
3302 +
3303 +       /*
3304 +        * a limit for rmdir/rename a dir
3305 +        * cf. AUFS_MAX_NAMELEN in include/uapi/linux/aufs_type.h
3306 +        */
3307 +       err = vfs_statfs(&br->br_path, &kst);
3308 +       if (unlikely(err))
3309 +               goto out;
3310 +       err = -EINVAL;
3311 +       if (kst.f_namelen >= NAME_MAX)
3312 +               err = au_br_init_wh(sb, br, perm);
3313 +       else
3314 +               pr_err("%pd(%s), unsupported namelen %ld\n",
3315 +                      au_br_dentry(br),
3316 +                      au_sbtype(au_br_dentry(br)->d_sb), kst.f_namelen);
3317 +
3318 +out:
3319 +       return err;
3320 +}
3321 +
3322 +/* initialize a new branch */
3323 +static int au_br_init(struct au_branch *br, struct super_block *sb,
3324 +                     struct au_opt_add *add)
3325 +{
3326 +       int err;
3327 +       struct au_branch *brbase;
3328 +       struct file *xf;
3329 +       struct inode *h_inode;
3330 +
3331 +       err = 0;
3332 +       br->br_perm = add->perm;
3333 +       br->br_path = add->path; /* set first, path_get() later */
3334 +       spin_lock_init(&br->br_dykey_lock);
3335 +       au_lcnt_init(&br->br_nfiles, /*release*/NULL);
3336 +       au_lcnt_init(&br->br_count, /*release*/NULL);
3337 +       br->br_id = au_new_br_id(sb);
3338 +       AuDebugOn(br->br_id < 0);
3339 +
3340 +       /* always, regardless the given option */
3341 +       err = au_dr_br_init(sb, br, &add->path);
3342 +       if (unlikely(err))
3343 +               goto out_err;
3344 +
3345 +       if (au_br_writable(add->perm)) {
3346 +               err = au_wbr_init(br, sb, add->perm);
3347 +               if (unlikely(err))
3348 +                       goto out_err;
3349 +       }
3350 +
3351 +       if (au_opt_test(au_mntflags(sb), XINO)) {
3352 +               brbase = au_sbr(sb, 0);
3353 +               xf = au_xino_file(brbase->br_xino, /*idx*/-1);
3354 +               AuDebugOn(!xf);
3355 +               h_inode = d_inode(add->path.dentry);
3356 +               err = au_xino_init_br(sb, br, h_inode->i_ino, &xf->f_path);
3357 +               if (unlikely(err)) {
3358 +                       AuDebugOn(au_xino_file(br->br_xino, /*idx*/-1));
3359 +                       goto out_err;
3360 +               }
3361 +       }
3362 +
3363 +       sysaufs_br_init(br);
3364 +       path_get(&br->br_path);
3365 +       goto out; /* success */
3366 +
3367 +out_err:
3368 +       memset(&br->br_path, 0, sizeof(br->br_path));
3369 +out:
3370 +       return err;
3371 +}
3372 +
3373 +static void au_br_do_add_brp(struct au_sbinfo *sbinfo, aufs_bindex_t bindex,
3374 +                            struct au_branch *br, aufs_bindex_t bbot,
3375 +                            aufs_bindex_t amount)
3376 +{
3377 +       struct au_branch **brp;
3378 +
3379 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
3380 +
3381 +       brp = sbinfo->si_branch + bindex;
3382 +       memmove(brp + 1, brp, sizeof(*brp) * amount);
3383 +       *brp = br;
3384 +       sbinfo->si_bbot++;
3385 +       if (unlikely(bbot < 0))
3386 +               sbinfo->si_bbot = 0;
3387 +}
3388 +
3389 +static void au_br_do_add_hdp(struct au_dinfo *dinfo, aufs_bindex_t bindex,
3390 +                            aufs_bindex_t bbot, aufs_bindex_t amount)
3391 +{
3392 +       struct au_hdentry *hdp;
3393 +
3394 +       AuRwMustWriteLock(&dinfo->di_rwsem);
3395 +
3396 +       hdp = au_hdentry(dinfo, bindex);
3397 +       memmove(hdp + 1, hdp, sizeof(*hdp) * amount);
3398 +       au_h_dentry_init(hdp);
3399 +       dinfo->di_bbot++;
3400 +       if (unlikely(bbot < 0))
3401 +               dinfo->di_btop = 0;
3402 +}
3403 +
3404 +static void au_br_do_add_hip(struct au_iinfo *iinfo, aufs_bindex_t bindex,
3405 +                            aufs_bindex_t bbot, aufs_bindex_t amount)
3406 +{
3407 +       struct au_hinode *hip;
3408 +
3409 +       AuRwMustWriteLock(&iinfo->ii_rwsem);
3410 +
3411 +       hip = au_hinode(iinfo, bindex);
3412 +       memmove(hip + 1, hip, sizeof(*hip) * amount);
3413 +       au_hinode_init(hip);
3414 +       iinfo->ii_bbot++;
3415 +       if (unlikely(bbot < 0))
3416 +               iinfo->ii_btop = 0;
3417 +}
3418 +
3419 +static void au_br_do_add(struct super_block *sb, struct au_branch *br,
3420 +                        aufs_bindex_t bindex)
3421 +{
3422 +       struct dentry *root, *h_dentry;
3423 +       struct inode *root_inode, *h_inode;
3424 +       aufs_bindex_t bbot, amount;
3425 +
3426 +       root = sb->s_root;
3427 +       root_inode = d_inode(root);
3428 +       bbot = au_sbbot(sb);
3429 +       amount = bbot + 1 - bindex;
3430 +       h_dentry = au_br_dentry(br);
3431 +       au_sbilist_lock();
3432 +       au_br_do_add_brp(au_sbi(sb), bindex, br, bbot, amount);
3433 +       au_br_do_add_hdp(au_di(root), bindex, bbot, amount);
3434 +       au_br_do_add_hip(au_ii(root_inode), bindex, bbot, amount);
3435 +       au_set_h_dptr(root, bindex, dget(h_dentry));
3436 +       h_inode = d_inode(h_dentry);
3437 +       au_set_h_iptr(root_inode, bindex, au_igrab(h_inode), /*flags*/0);
3438 +       au_sbilist_unlock();
3439 +}
3440 +
3441 +int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount)
3442 +{
3443 +       int err;
3444 +       aufs_bindex_t bbot, add_bindex;
3445 +       struct dentry *root, *h_dentry;
3446 +       struct inode *root_inode;
3447 +       struct au_branch *add_branch;
3448 +
3449 +       root = sb->s_root;
3450 +       root_inode = d_inode(root);
3451 +       IMustLock(root_inode);
3452 +       IiMustWriteLock(root_inode);
3453 +       err = test_add(sb, add, remount);
3454 +       if (unlikely(err < 0))
3455 +               goto out;
3456 +       if (err) {
3457 +               err = 0;
3458 +               goto out; /* success */
3459 +       }
3460 +
3461 +       bbot = au_sbbot(sb);
3462 +       add_branch = au_br_alloc(sb, bbot + 2, add->perm);
3463 +       err = PTR_ERR(add_branch);
3464 +       if (IS_ERR(add_branch))
3465 +               goto out;
3466 +
3467 +       err = au_br_init(add_branch, sb, add);
3468 +       if (unlikely(err)) {
3469 +               au_br_do_free(add_branch);
3470 +               goto out;
3471 +       }
3472 +
3473 +       add_bindex = add->bindex;
3474 +       sysaufs_brs_del(sb, add_bindex);        /* remove successors */
3475 +       au_br_do_add(sb, add_branch, add_bindex);
3476 +       sysaufs_brs_add(sb, add_bindex);        /* append successors */
3477 +       dbgaufs_brs_add(sb, add_bindex, /*topdown*/0);  /* rename successors */
3478 +
3479 +       h_dentry = add->path.dentry;
3480 +       if (!add_bindex) {
3481 +               au_cpup_attr_all(root_inode, /*force*/1);
3482 +               sb->s_maxbytes = h_dentry->d_sb->s_maxbytes;
3483 +       } else
3484 +               au_add_nlink(root_inode, d_inode(h_dentry));
3485 +
3486 +out:
3487 +       return err;
3488 +}
3489 +
3490 +/* ---------------------------------------------------------------------- */
3491 +
3492 +static unsigned long long au_farray_cb(struct super_block *sb, void *a,
3493 +                                      unsigned long long max __maybe_unused,
3494 +                                      void *arg)
3495 +{
3496 +       unsigned long long n;
3497 +       struct file **p, *f;
3498 +       struct hlist_bl_head *files;
3499 +       struct hlist_bl_node *pos;
3500 +       struct au_finfo *finfo;
3501 +
3502 +       n = 0;
3503 +       p = a;
3504 +       files = &au_sbi(sb)->si_files;
3505 +       hlist_bl_lock(files);
3506 +       hlist_bl_for_each_entry(finfo, pos, files, fi_hlist) {
3507 +               f = finfo->fi_file;
3508 +               if (file_count(f)
3509 +                   && !special_file(file_inode(f)->i_mode)) {
3510 +                       get_file(f);
3511 +                       *p++ = f;
3512 +                       n++;
3513 +                       AuDebugOn(n > max);
3514 +               }
3515 +       }
3516 +       hlist_bl_unlock(files);
3517 +
3518 +       return n;
3519 +}
3520 +
3521 +static struct file **au_farray_alloc(struct super_block *sb,
3522 +                                    unsigned long long *max)
3523 +{
3524 +       struct au_sbinfo *sbi;
3525 +
3526 +       sbi = au_sbi(sb);
3527 +       *max = au_lcnt_read(&sbi->si_nfiles, /*do_rev*/1);
3528 +       return au_array_alloc(max, au_farray_cb, sb, /*arg*/NULL);
3529 +}
3530 +
3531 +static void au_farray_free(struct file **a, unsigned long long max)
3532 +{
3533 +       unsigned long long ull;
3534 +
3535 +       for (ull = 0; ull < max; ull++)
3536 +               if (a[ull])
3537 +                       fput(a[ull]);
3538 +       kvfree(a);
3539 +}
3540 +
3541 +/* ---------------------------------------------------------------------- */
3542 +
3543 +/*
3544 + * delete a branch
3545 + */
3546 +
3547 +/* to show the line number, do not make it inlined function */
3548 +#define AuVerbose(do_info, fmt, ...) do { \
3549 +       if (do_info) \
3550 +               pr_info(fmt, ##__VA_ARGS__); \
3551 +} while (0)
3552 +
3553 +static int au_test_ibusy(struct inode *inode, aufs_bindex_t btop,
3554 +                        aufs_bindex_t bbot)
3555 +{
3556 +       return (inode && !S_ISDIR(inode->i_mode)) || btop == bbot;
3557 +}
3558 +
3559 +static int au_test_dbusy(struct dentry *dentry, aufs_bindex_t btop,
3560 +                        aufs_bindex_t bbot)
3561 +{
3562 +       return au_test_ibusy(d_inode(dentry), btop, bbot);
3563 +}
3564 +
3565 +/*
3566 + * test if the branch is deletable or not.
3567 + */
3568 +static int test_dentry_busy(struct dentry *root, aufs_bindex_t bindex,
3569 +                           unsigned int sigen, const unsigned int verbose)
3570 +{
3571 +       int err, i, j, ndentry;
3572 +       aufs_bindex_t btop, bbot;
3573 +       struct au_dcsub_pages dpages;
3574 +       struct au_dpage *dpage;
3575 +       struct dentry *d;
3576 +
3577 +       err = au_dpages_init(&dpages, GFP_NOFS);
3578 +       if (unlikely(err))
3579 +               goto out;
3580 +       err = au_dcsub_pages(&dpages, root, NULL, NULL);
3581 +       if (unlikely(err))
3582 +               goto out_dpages;
3583 +
3584 +       for (i = 0; !err && i < dpages.ndpage; i++) {
3585 +               dpage = dpages.dpages + i;
3586 +               ndentry = dpage->ndentry;
3587 +               for (j = 0; !err && j < ndentry; j++) {
3588 +                       d = dpage->dentries[j];
3589 +                       AuDebugOn(au_dcount(d) <= 0);
3590 +                       if (!au_digen_test(d, sigen)) {
3591 +                               di_read_lock_child(d, AuLock_IR);
3592 +                               if (unlikely(au_dbrange_test(d))) {
3593 +                                       di_read_unlock(d, AuLock_IR);
3594 +                                       continue;
3595 +                               }
3596 +                       } else {
3597 +                               di_write_lock_child(d);
3598 +                               if (unlikely(au_dbrange_test(d))) {
3599 +                                       di_write_unlock(d);
3600 +                                       continue;
3601 +                               }
3602 +                               err = au_reval_dpath(d, sigen);
3603 +                               if (!err)
3604 +                                       di_downgrade_lock(d, AuLock_IR);
3605 +                               else {
3606 +                                       di_write_unlock(d);
3607 +                                       break;
3608 +                               }
3609 +                       }
3610 +
3611 +                       /* AuDbgDentry(d); */
3612 +                       btop = au_dbtop(d);
3613 +                       bbot = au_dbbot(d);
3614 +                       if (btop <= bindex
3615 +                           && bindex <= bbot
3616 +                           && au_h_dptr(d, bindex)
3617 +                           && au_test_dbusy(d, btop, bbot)) {
3618 +                               err = -EBUSY;
3619 +                               AuVerbose(verbose, "busy %pd\n", d);
3620 +                               AuDbgDentry(d);
3621 +                       }
3622 +                       di_read_unlock(d, AuLock_IR);
3623 +               }
3624 +       }
3625 +
3626 +out_dpages:
3627 +       au_dpages_free(&dpages);
3628 +out:
3629 +       return err;
3630 +}
3631 +
3632 +static int test_inode_busy(struct super_block *sb, aufs_bindex_t bindex,
3633 +                          unsigned int sigen, const unsigned int verbose)
3634 +{
3635 +       int err;
3636 +       unsigned long long max, ull;
3637 +       struct inode *i, **array;
3638 +       aufs_bindex_t btop, bbot;
3639 +
3640 +       array = au_iarray_alloc(sb, &max);
3641 +       err = PTR_ERR(array);
3642 +       if (IS_ERR(array))
3643 +               goto out;
3644 +
3645 +       err = 0;
3646 +       AuDbg("b%d\n", bindex);
3647 +       for (ull = 0; !err && ull < max; ull++) {
3648 +               i = array[ull];
3649 +               if (unlikely(!i))
3650 +                       break;
3651 +               if (i->i_ino == AUFS_ROOT_INO)
3652 +                       continue;
3653 +
3654 +               /* AuDbgInode(i); */
3655 +               if (au_iigen(i, NULL) == sigen)
3656 +                       ii_read_lock_child(i);
3657 +               else {
3658 +                       ii_write_lock_child(i);
3659 +                       err = au_refresh_hinode_self(i);
3660 +                       au_iigen_dec(i);
3661 +                       if (!err)
3662 +                               ii_downgrade_lock(i);
3663 +                       else {
3664 +                               ii_write_unlock(i);
3665 +                               break;
3666 +                       }
3667 +               }
3668 +
3669 +               btop = au_ibtop(i);
3670 +               bbot = au_ibbot(i);
3671 +               if (btop <= bindex
3672 +                   && bindex <= bbot
3673 +                   && au_h_iptr(i, bindex)
3674 +                   && au_test_ibusy(i, btop, bbot)) {
3675 +                       err = -EBUSY;
3676 +                       AuVerbose(verbose, "busy i%lu\n", i->i_ino);
3677 +                       AuDbgInode(i);
3678 +               }
3679 +               ii_read_unlock(i);
3680 +       }
3681 +       au_iarray_free(array, max);
3682 +
3683 +out:
3684 +       return err;
3685 +}
3686 +
3687 +static int test_children_busy(struct dentry *root, aufs_bindex_t bindex,
3688 +                             const unsigned int verbose)
3689 +{
3690 +       int err;
3691 +       unsigned int sigen;
3692 +
3693 +       sigen = au_sigen(root->d_sb);
3694 +       DiMustNoWaiters(root);
3695 +       IiMustNoWaiters(d_inode(root));
3696 +       di_write_unlock(root);
3697 +       err = test_dentry_busy(root, bindex, sigen, verbose);
3698 +       if (!err)
3699 +               err = test_inode_busy(root->d_sb, bindex, sigen, verbose);
3700 +       di_write_lock_child(root); /* aufs_write_lock() calls ..._child() */
3701 +
3702 +       return err;
3703 +}
3704 +
3705 +static int test_dir_busy(struct file *file, aufs_bindex_t br_id,
3706 +                        struct file **to_free, int *idx)
3707 +{
3708 +       int err;
3709 +       unsigned char matched, root;
3710 +       aufs_bindex_t bindex, bbot;
3711 +       struct au_fidir *fidir;
3712 +       struct au_hfile *hfile;
3713 +
3714 +       err = 0;
3715 +       root = IS_ROOT(file->f_path.dentry);
3716 +       if (root) {
3717 +               get_file(file);
3718 +               to_free[*idx] = file;
3719 +               (*idx)++;
3720 +               goto out;
3721 +       }
3722 +
3723 +       matched = 0;
3724 +       fidir = au_fi(file)->fi_hdir;
3725 +       AuDebugOn(!fidir);
3726 +       bbot = au_fbbot_dir(file);
3727 +       for (bindex = au_fbtop(file); bindex <= bbot; bindex++) {
3728 +               hfile = fidir->fd_hfile + bindex;
3729 +               if (!hfile->hf_file)
3730 +                       continue;
3731 +
3732 +               if (hfile->hf_br->br_id == br_id) {
3733 +                       matched = 1;
3734 +                       break;
3735 +               }
3736 +       }
3737 +       if (matched)
3738 +               err = -EBUSY;
3739 +
3740 +out:
3741 +       return err;
3742 +}
3743 +
3744 +static int test_file_busy(struct super_block *sb, aufs_bindex_t br_id,
3745 +                         struct file **to_free, int opened)
3746 +{
3747 +       int err, idx;
3748 +       unsigned long long ull, max;
3749 +       aufs_bindex_t btop;
3750 +       struct file *file, **array;
3751 +       struct dentry *root;
3752 +       struct au_hfile *hfile;
3753 +
3754 +       array = au_farray_alloc(sb, &max);
3755 +       err = PTR_ERR(array);
3756 +       if (IS_ERR(array))
3757 +               goto out;
3758 +
3759 +       err = 0;
3760 +       idx = 0;
3761 +       root = sb->s_root;
3762 +       di_write_unlock(root);
3763 +       for (ull = 0; ull < max; ull++) {
3764 +               file = array[ull];
3765 +               if (unlikely(!file))
3766 +                       break;
3767 +
3768 +               /* AuDbg("%pD\n", file); */
3769 +               fi_read_lock(file);
3770 +               btop = au_fbtop(file);
3771 +               if (!d_is_dir(file->f_path.dentry)) {
3772 +                       hfile = &au_fi(file)->fi_htop;
3773 +                       if (hfile->hf_br->br_id == br_id)
3774 +                               err = -EBUSY;
3775 +               } else
3776 +                       err = test_dir_busy(file, br_id, to_free, &idx);
3777 +               fi_read_unlock(file);
3778 +               if (unlikely(err))
3779 +                       break;
3780 +       }
3781 +       di_write_lock_child(root);
3782 +       au_farray_free(array, max);
3783 +       AuDebugOn(idx > opened);
3784 +
3785 +out:
3786 +       return err;
3787 +}
3788 +
3789 +static void br_del_file(struct file **to_free, unsigned long long opened,
3790 +                       aufs_bindex_t br_id)
3791 +{
3792 +       unsigned long long ull;
3793 +       aufs_bindex_t bindex, btop, bbot, bfound;
3794 +       struct file *file;
3795 +       struct au_fidir *fidir;
3796 +       struct au_hfile *hfile;
3797 +
3798 +       for (ull = 0; ull < opened; ull++) {
3799 +               file = to_free[ull];
3800 +               if (unlikely(!file))
3801 +                       break;
3802 +
3803 +               /* AuDbg("%pD\n", file); */
3804 +               AuDebugOn(!d_is_dir(file->f_path.dentry));
3805 +               bfound = -1;
3806 +               fidir = au_fi(file)->fi_hdir;
3807 +               AuDebugOn(!fidir);
3808 +               fi_write_lock(file);
3809 +               btop = au_fbtop(file);
3810 +               bbot = au_fbbot_dir(file);
3811 +               for (bindex = btop; bindex <= bbot; bindex++) {
3812 +                       hfile = fidir->fd_hfile + bindex;
3813 +                       if (!hfile->hf_file)
3814 +                               continue;
3815 +
3816 +                       if (hfile->hf_br->br_id == br_id) {
3817 +                               bfound = bindex;
3818 +                               break;
3819 +                       }
3820 +               }
3821 +               AuDebugOn(bfound < 0);
3822 +               au_set_h_fptr(file, bfound, NULL);
3823 +               if (bfound == btop) {
3824 +                       for (btop++; btop <= bbot; btop++)
3825 +                               if (au_hf_dir(file, btop)) {
3826 +                                       au_set_fbtop(file, btop);
3827 +                                       break;
3828 +                               }
3829 +               }
3830 +               fi_write_unlock(file);
3831 +       }
3832 +}
3833 +
3834 +static void au_br_do_del_brp(struct au_sbinfo *sbinfo,
3835 +                            const aufs_bindex_t bindex,
3836 +                            const aufs_bindex_t bbot)
3837 +{
3838 +       struct au_branch **brp, **p;
3839 +
3840 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
3841 +
3842 +       brp = sbinfo->si_branch + bindex;
3843 +       if (bindex < bbot)
3844 +               memmove(brp, brp + 1, sizeof(*brp) * (bbot - bindex));
3845 +       sbinfo->si_branch[0 + bbot] = NULL;
3846 +       sbinfo->si_bbot--;
3847 +
3848 +       p = au_krealloc(sbinfo->si_branch, sizeof(*p) * bbot, AuGFP_SBILIST,
3849 +                       /*may_shrink*/1);
3850 +       if (p)
3851 +               sbinfo->si_branch = p;
3852 +       /* harmless error */
3853 +}
3854 +
3855 +static void au_br_do_del_hdp(struct au_dinfo *dinfo, const aufs_bindex_t bindex,
3856 +                            const aufs_bindex_t bbot)
3857 +{
3858 +       struct au_hdentry *hdp, *p;
3859 +
3860 +       AuRwMustWriteLock(&dinfo->di_rwsem);
3861 +
3862 +       hdp = au_hdentry(dinfo, bindex);
3863 +       if (bindex < bbot)
3864 +               memmove(hdp, hdp + 1, sizeof(*hdp) * (bbot - bindex));
3865 +       /* au_h_dentry_init(au_hdentry(dinfo, bbot); */
3866 +       dinfo->di_bbot--;
3867 +
3868 +       p = au_krealloc(dinfo->di_hdentry, sizeof(*p) * bbot, AuGFP_SBILIST,
3869 +                       /*may_shrink*/1);
3870 +       if (p)
3871 +               dinfo->di_hdentry = p;
3872 +       /* harmless error */
3873 +}
3874 +
3875 +static void au_br_do_del_hip(struct au_iinfo *iinfo, const aufs_bindex_t bindex,
3876 +                            const aufs_bindex_t bbot)
3877 +{
3878 +       struct au_hinode *hip, *p;
3879 +
3880 +       AuRwMustWriteLock(&iinfo->ii_rwsem);
3881 +
3882 +       hip = au_hinode(iinfo, bindex);
3883 +       if (bindex < bbot)
3884 +               memmove(hip, hip + 1, sizeof(*hip) * (bbot - bindex));
3885 +       /* au_hinode_init(au_hinode(iinfo, bbot)); */
3886 +       iinfo->ii_bbot--;
3887 +
3888 +       p = au_krealloc(iinfo->ii_hinode, sizeof(*p) * bbot, AuGFP_SBILIST,
3889 +                       /*may_shrink*/1);
3890 +       if (p)
3891 +               iinfo->ii_hinode = p;
3892 +       /* harmless error */
3893 +}
3894 +
3895 +static void au_br_do_del(struct super_block *sb, aufs_bindex_t bindex,
3896 +                        struct au_branch *br)
3897 +{
3898 +       aufs_bindex_t bbot;
3899 +       struct au_sbinfo *sbinfo;
3900 +       struct dentry *root, *h_root;
3901 +       struct inode *inode, *h_inode;
3902 +       struct au_hinode *hinode;
3903 +
3904 +       SiMustWriteLock(sb);
3905 +
3906 +       root = sb->s_root;
3907 +       inode = d_inode(root);
3908 +       sbinfo = au_sbi(sb);
3909 +       bbot = sbinfo->si_bbot;
3910 +
3911 +       h_root = au_h_dptr(root, bindex);
3912 +       hinode = au_hi(inode, bindex);
3913 +       h_inode = au_igrab(hinode->hi_inode);
3914 +       au_hiput(hinode);
3915 +
3916 +       au_sbilist_lock();
3917 +       au_br_do_del_brp(sbinfo, bindex, bbot);
3918 +       au_br_do_del_hdp(au_di(root), bindex, bbot);
3919 +       au_br_do_del_hip(au_ii(inode), bindex, bbot);
3920 +       au_sbilist_unlock();
3921 +
3922 +       /* ignore an error */
3923 +       au_dr_br_fin(sb, br); /* always, regardless the mount option */
3924 +
3925 +       dput(h_root);
3926 +       iput(h_inode);
3927 +       au_br_do_free(br);
3928 +}
3929 +
3930 +static unsigned long long empty_cb(struct super_block *sb, void *array,
3931 +                                  unsigned long long max, void *arg)
3932 +{
3933 +       return max;
3934 +}
3935 +
3936 +int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount)
3937 +{
3938 +       int err, rerr, i;
3939 +       unsigned long long opened;
3940 +       unsigned int mnt_flags;
3941 +       aufs_bindex_t bindex, bbot, br_id;
3942 +       unsigned char do_wh, verbose;
3943 +       struct au_branch *br;
3944 +       struct au_wbr *wbr;
3945 +       struct dentry *root;
3946 +       struct file **to_free;
3947 +
3948 +       err = 0;
3949 +       opened = 0;
3950 +       to_free = NULL;
3951 +       root = sb->s_root;
3952 +       bindex = au_find_dbindex(root, del->h_path.dentry);
3953 +       if (bindex < 0) {
3954 +               if (remount)
3955 +                       goto out; /* success */
3956 +               err = -ENOENT;
3957 +               pr_err("%s no such branch\n", del->pathname);
3958 +               goto out;
3959 +       }
3960 +       AuDbg("bindex b%d\n", bindex);
3961 +
3962 +       err = -EBUSY;
3963 +       mnt_flags = au_mntflags(sb);
3964 +       verbose = !!au_opt_test(mnt_flags, VERBOSE);
3965 +       bbot = au_sbbot(sb);
3966 +       if (unlikely(!bbot)) {
3967 +               AuVerbose(verbose, "no more branches left\n");
3968 +               goto out;
3969 +       }
3970 +
3971 +       br = au_sbr(sb, bindex);
3972 +       AuDebugOn(!path_equal(&br->br_path, &del->h_path));
3973 +       if (unlikely(au_lcnt_read(&br->br_count, /*do_rev*/1))) {
3974 +               AuVerbose(verbose, "br %pd2 is busy now\n", del->h_path.dentry);
3975 +               goto out;
3976 +       }
3977 +
3978 +       br_id = br->br_id;
3979 +       opened = au_lcnt_read(&br->br_nfiles, /*do_rev*/1);
3980 +       if (unlikely(opened)) {
3981 +               to_free = au_array_alloc(&opened, empty_cb, sb, NULL);
3982 +               err = PTR_ERR(to_free);
3983 +               if (IS_ERR(to_free))
3984 +                       goto out;
3985 +
3986 +               err = test_file_busy(sb, br_id, to_free, opened);
3987 +               if (unlikely(err)) {
3988 +                       AuVerbose(verbose, "%llu file(s) opened\n", opened);
3989 +                       goto out;
3990 +               }
3991 +       }
3992 +
3993 +       wbr = br->br_wbr;
3994 +       do_wh = wbr && (wbr->wbr_whbase || wbr->wbr_plink || wbr->wbr_orph);
3995 +       if (do_wh) {
3996 +               /* instead of WbrWhMustWriteLock(wbr) */
3997 +               SiMustWriteLock(sb);
3998 +               for (i = 0; i < AuBrWh_Last; i++) {
3999 +                       dput(wbr->wbr_wh[i]);
4000 +                       wbr->wbr_wh[i] = NULL;
4001 +               }
4002 +       }
4003 +
4004 +       err = test_children_busy(root, bindex, verbose);
4005 +       if (unlikely(err)) {
4006 +               if (do_wh)
4007 +                       goto out_wh;
4008 +               goto out;
4009 +       }
4010 +
4011 +       err = 0;
4012 +       if (to_free) {
4013 +               /*
4014 +                * now we confirmed the branch is deletable.
4015 +                * let's free the remaining opened dirs on the branch.
4016 +                */
4017 +               di_write_unlock(root);
4018 +               br_del_file(to_free, opened, br_id);
4019 +               di_write_lock_child(root);
4020 +       }
4021 +
4022 +       sysaufs_brs_del(sb, bindex);    /* remove successors */
4023 +       dbgaufs_xino_del(br);           /* remove one */
4024 +       au_br_do_del(sb, bindex, br);
4025 +       sysaufs_brs_add(sb, bindex);    /* append successors */
4026 +       dbgaufs_brs_add(sb, bindex, /*topdown*/1);      /* rename successors */
4027 +
4028 +       if (!bindex) {
4029 +               au_cpup_attr_all(d_inode(root), /*force*/1);
4030 +               sb->s_maxbytes = au_sbr_sb(sb, 0)->s_maxbytes;
4031 +       } else
4032 +               au_sub_nlink(d_inode(root), d_inode(del->h_path.dentry));
4033 +       if (au_opt_test(mnt_flags, PLINK))
4034 +               au_plink_half_refresh(sb, br_id);
4035 +
4036 +       goto out; /* success */
4037 +
4038 +out_wh:
4039 +       /* revert */
4040 +       rerr = au_br_init_wh(sb, br, br->br_perm);
4041 +       if (rerr)
4042 +               pr_warn("failed re-creating base whiteout, %s. (%d)\n",
4043 +                       del->pathname, rerr);
4044 +out:
4045 +       if (to_free)
4046 +               au_farray_free(to_free, opened);
4047 +       return err;
4048 +}
4049 +
4050 +/* ---------------------------------------------------------------------- */
4051 +
4052 +static int au_ibusy(struct super_block *sb, struct aufs_ibusy __user *arg)
4053 +{
4054 +       int err;
4055 +       aufs_bindex_t btop, bbot;
4056 +       struct aufs_ibusy ibusy;
4057 +       struct inode *inode, *h_inode;
4058 +
4059 +       err = -EPERM;
4060 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
4061 +               goto out;
4062 +
4063 +       err = copy_from_user(&ibusy, arg, sizeof(ibusy));
4064 +       if (!err)
4065 +               /* VERIFY_WRITE */
4066 +               err = !access_ok(&arg->h_ino, sizeof(arg->h_ino));
4067 +       if (unlikely(err)) {
4068 +               err = -EFAULT;
4069 +               AuTraceErr(err);
4070 +               goto out;
4071 +       }
4072 +
4073 +       err = -EINVAL;
4074 +       si_read_lock(sb, AuLock_FLUSH);
4075 +       if (unlikely(ibusy.bindex < 0 || ibusy.bindex > au_sbbot(sb)))
4076 +               goto out_unlock;
4077 +
4078 +       err = 0;
4079 +       ibusy.h_ino = 0; /* invalid */
4080 +       inode = ilookup(sb, ibusy.ino);
4081 +       if (!inode
4082 +           || inode->i_ino == AUFS_ROOT_INO
4083 +           || au_is_bad_inode(inode))
4084 +               goto out_unlock;
4085 +
4086 +       ii_read_lock_child(inode);
4087 +       btop = au_ibtop(inode);
4088 +       bbot = au_ibbot(inode);
4089 +       if (btop <= ibusy.bindex && ibusy.bindex <= bbot) {
4090 +               h_inode = au_h_iptr(inode, ibusy.bindex);
4091 +               if (h_inode && au_test_ibusy(inode, btop, bbot))
4092 +                       ibusy.h_ino = h_inode->i_ino;
4093 +       }
4094 +       ii_read_unlock(inode);
4095 +       iput(inode);
4096 +
4097 +out_unlock:
4098 +       si_read_unlock(sb);
4099 +       if (!err) {
4100 +               err = __put_user(ibusy.h_ino, &arg->h_ino);
4101 +               if (unlikely(err)) {
4102 +                       err = -EFAULT;
4103 +                       AuTraceErr(err);
4104 +               }
4105 +       }
4106 +out:
4107 +       return err;
4108 +}
4109 +
4110 +long au_ibusy_ioctl(struct file *file, unsigned long arg)
4111 +{
4112 +       return au_ibusy(file->f_path.dentry->d_sb, (void __user *)arg);
4113 +}
4114 +
4115 +#ifdef CONFIG_COMPAT
4116 +long au_ibusy_compat_ioctl(struct file *file, unsigned long arg)
4117 +{
4118 +       return au_ibusy(file->f_path.dentry->d_sb, compat_ptr(arg));
4119 +}
4120 +#endif
4121 +
4122 +/* ---------------------------------------------------------------------- */
4123 +
4124 +/*
4125 + * change a branch permission
4126 + */
4127 +
4128 +static void au_warn_ima(void)
4129 +{
4130 +#ifdef CONFIG_IMA
4131 +       /* since it doesn't support mark_files_ro() */
4132 +       AuWarn1("RW -> RO makes IMA to produce wrong message\n");
4133 +#endif
4134 +}
4135 +
4136 +static int do_need_sigen_inc(int a, int b)
4137 +{
4138 +       return au_br_whable(a) && !au_br_whable(b);
4139 +}
4140 +
4141 +static int need_sigen_inc(int old, int new)
4142 +{
4143 +       return do_need_sigen_inc(old, new)
4144 +               || do_need_sigen_inc(new, old);
4145 +}
4146 +
4147 +static int au_br_mod_files_ro(struct super_block *sb, aufs_bindex_t bindex)
4148 +{
4149 +       int err, do_warn;
4150 +       unsigned int mnt_flags;
4151 +       unsigned long long ull, max;
4152 +       aufs_bindex_t br_id;
4153 +       unsigned char verbose, writer;
4154 +       struct file *file, *hf, **array;
4155 +       struct au_hfile *hfile;
4156 +       struct inode *h_inode;
4157 +
4158 +       mnt_flags = au_mntflags(sb);
4159 +       verbose = !!au_opt_test(mnt_flags, VERBOSE);
4160 +
4161 +       array = au_farray_alloc(sb, &max);
4162 +       err = PTR_ERR(array);
4163 +       if (IS_ERR(array))
4164 +               goto out;
4165 +
4166 +       do_warn = 0;
4167 +       br_id = au_sbr_id(sb, bindex);
4168 +       for (ull = 0; ull < max; ull++) {
4169 +               file = array[ull];
4170 +               if (unlikely(!file))
4171 +                       break;
4172 +
4173 +               /* AuDbg("%pD\n", file); */
4174 +               fi_read_lock(file);
4175 +               if (unlikely(au_test_mmapped(file))) {
4176 +                       err = -EBUSY;
4177 +                       AuVerbose(verbose, "mmapped %pD\n", file);
4178 +                       AuDbgFile(file);
4179 +                       FiMustNoWaiters(file);
4180 +                       fi_read_unlock(file);
4181 +                       goto out_array;
4182 +               }
4183 +
4184 +               hfile = &au_fi(file)->fi_htop;
4185 +               hf = hfile->hf_file;
4186 +               if (!d_is_reg(file->f_path.dentry)
4187 +                   || !(file->f_mode & FMODE_WRITE)
4188 +                   || hfile->hf_br->br_id != br_id
4189 +                   || !(hf->f_mode & FMODE_WRITE))
4190 +                       array[ull] = NULL;
4191 +               else {
4192 +                       do_warn = 1;
4193 +                       get_file(file);
4194 +               }
4195 +
4196 +               FiMustNoWaiters(file);
4197 +               fi_read_unlock(file);
4198 +               fput(file);
4199 +       }
4200 +
4201 +       err = 0;
4202 +       if (do_warn)
4203 +               au_warn_ima();
4204 +
4205 +       for (ull = 0; ull < max; ull++) {
4206 +               file = array[ull];
4207 +               if (!file)
4208 +                       continue;
4209 +
4210 +               /* todo: already flushed? */
4211 +               /*
4212 +                * fs/super.c:mark_files_ro() is gone, but aufs keeps its
4213 +                * approach which resets f_mode and calls mnt_drop_write() and
4214 +                * file_release_write() for each file, because the branch
4215 +                * attribute in aufs world is totally different from the native
4216 +                * fs rw/ro mode.
4217 +               */
4218 +               /* fi_read_lock(file); */
4219 +               hfile = &au_fi(file)->fi_htop;
4220 +               hf = hfile->hf_file;
4221 +               /* fi_read_unlock(file); */
4222 +               spin_lock(&hf->f_lock);
4223 +               writer = !!(hf->f_mode & FMODE_WRITER);
4224 +               hf->f_mode &= ~(FMODE_WRITE | FMODE_WRITER);
4225 +               spin_unlock(&hf->f_lock);
4226 +               if (writer) {
4227 +                       h_inode = file_inode(hf);
4228 +                       if (hf->f_mode & FMODE_READ)
4229 +                               i_readcount_inc(h_inode);
4230 +                       put_write_access(h_inode);
4231 +                       __mnt_drop_write(hf->f_path.mnt);
4232 +               }
4233 +       }
4234 +
4235 +out_array:
4236 +       au_farray_free(array, max);
4237 +out:
4238 +       AuTraceErr(err);
4239 +       return err;
4240 +}
4241 +
4242 +int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
4243 +             int *do_refresh)
4244 +{
4245 +       int err, rerr;
4246 +       aufs_bindex_t bindex;
4247 +       struct dentry *root;
4248 +       struct au_branch *br;
4249 +       struct au_br_fhsm *bf;
4250 +
4251 +       root = sb->s_root;
4252 +       bindex = au_find_dbindex(root, mod->h_root);
4253 +       if (bindex < 0) {
4254 +               if (remount)
4255 +                       return 0; /* success */
4256 +               err = -ENOENT;
4257 +               pr_err("%s no such branch\n", mod->path);
4258 +               goto out;
4259 +       }
4260 +       AuDbg("bindex b%d\n", bindex);
4261 +
4262 +       err = test_br(d_inode(mod->h_root), mod->perm, mod->path);
4263 +       if (unlikely(err))
4264 +               goto out;
4265 +
4266 +       br = au_sbr(sb, bindex);
4267 +       AuDebugOn(mod->h_root != au_br_dentry(br));
4268 +       if (br->br_perm == mod->perm)
4269 +               return 0; /* success */
4270 +
4271 +       /* pre-allocate for non-fhsm --> fhsm */
4272 +       bf = NULL;
4273 +       if (!au_br_fhsm(br->br_perm) && au_br_fhsm(mod->perm)) {
4274 +               err = au_fhsm_br_alloc(br);
4275 +               if (unlikely(err))
4276 +                       goto out;
4277 +               bf = br->br_fhsm;
4278 +               br->br_fhsm = NULL;
4279 +       }
4280 +
4281 +       if (au_br_writable(br->br_perm)) {
4282 +               /* remove whiteout base */
4283 +               err = au_br_init_wh(sb, br, mod->perm);
4284 +               if (unlikely(err))
4285 +                       goto out_bf;
4286 +
4287 +               if (!au_br_writable(mod->perm)) {
4288 +                       /* rw --> ro, file might be mmapped */
4289 +                       DiMustNoWaiters(root);
4290 +                       IiMustNoWaiters(d_inode(root));
4291 +                       di_write_unlock(root);
4292 +                       err = au_br_mod_files_ro(sb, bindex);
4293 +                       /* aufs_write_lock() calls ..._child() */
4294 +                       di_write_lock_child(root);
4295 +
4296 +                       if (unlikely(err)) {
4297 +                               rerr = -ENOMEM;
4298 +                               br->br_wbr = kzalloc(sizeof(*br->br_wbr),
4299 +                                                    GFP_NOFS);
4300 +                               if (br->br_wbr)
4301 +                                       rerr = au_wbr_init(br, sb, br->br_perm);
4302 +                               if (unlikely(rerr)) {
4303 +                                       AuIOErr("nested error %d (%d)\n",
4304 +                                               rerr, err);
4305 +                                       br->br_perm = mod->perm;
4306 +                               }
4307 +                       }
4308 +               }
4309 +       } else if (au_br_writable(mod->perm)) {
4310 +               /* ro --> rw */
4311 +               err = -ENOMEM;
4312 +               br->br_wbr = kzalloc(sizeof(*br->br_wbr), GFP_NOFS);
4313 +               if (br->br_wbr) {
4314 +                       err = au_wbr_init(br, sb, mod->perm);
4315 +                       if (unlikely(err)) {
4316 +                               au_kfree_rcu(br->br_wbr);
4317 +                               br->br_wbr = NULL;
4318 +                       }
4319 +               }
4320 +       }
4321 +       if (unlikely(err))
4322 +               goto out_bf;
4323 +
4324 +       if (au_br_fhsm(br->br_perm)) {
4325 +               if (!au_br_fhsm(mod->perm)) {
4326 +                       /* fhsm --> non-fhsm */
4327 +                       au_br_fhsm_fin(br->br_fhsm);
4328 +                       au_kfree_rcu(br->br_fhsm);
4329 +                       br->br_fhsm = NULL;
4330 +               }
4331 +       } else if (au_br_fhsm(mod->perm))
4332 +               /* non-fhsm --> fhsm */
4333 +               br->br_fhsm = bf;
4334 +
4335 +       *do_refresh |= need_sigen_inc(br->br_perm, mod->perm);
4336 +       br->br_perm = mod->perm;
4337 +       goto out; /* success */
4338 +
4339 +out_bf:
4340 +       au_kfree_try_rcu(bf);
4341 +out:
4342 +       AuTraceErr(err);
4343 +       return err;
4344 +}
4345 +
4346 +/* ---------------------------------------------------------------------- */
4347 +
4348 +int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs)
4349 +{
4350 +       int err;
4351 +       struct kstatfs kstfs;
4352 +
4353 +       err = vfs_statfs(&br->br_path, &kstfs);
4354 +       if (!err) {
4355 +               stfs->f_blocks = kstfs.f_blocks;
4356 +               stfs->f_bavail = kstfs.f_bavail;
4357 +               stfs->f_files = kstfs.f_files;
4358 +               stfs->f_ffree = kstfs.f_ffree;
4359 +       }
4360 +
4361 +       return err;
4362 +}
4363 diff -urN /usr/share/empty/fs/aufs/branch.h linux/fs/aufs/branch.h
4364 --- /usr/share/empty/fs/aufs/branch.h   1970-01-01 01:00:00.000000000 +0100
4365 +++ linux/fs/aufs/branch.h      2021-11-01 23:48:34.203025928 +0100
4366 @@ -0,0 +1,375 @@
4367 +/* SPDX-License-Identifier: GPL-2.0 */
4368 +/*
4369 + * Copyright (C) 2005-2021 Junjiro R. Okajima
4370 + *
4371 + * This program, aufs is free software; you can redistribute it and/or modify
4372 + * it under the terms of the GNU General Public License as published by
4373 + * the Free Software Foundation; either version 2 of the License, or
4374 + * (at your option) any later version.
4375 + *
4376 + * This program is distributed in the hope that it will be useful,
4377 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
4378 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
4379 + * GNU General Public License for more details.
4380 + *
4381 + * You should have received a copy of the GNU General Public License
4382 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
4383 + */
4384 +
4385 +/*
4386 + * branch filesystems and xino for them
4387 + */
4388 +
4389 +#ifndef __AUFS_BRANCH_H__
4390 +#define __AUFS_BRANCH_H__
4391 +
4392 +#ifdef __KERNEL__
4393 +
4394 +#include <linux/mount.h>
4395 +#include "dirren.h"
4396 +#include "dynop.h"
4397 +#include "lcnt.h"
4398 +#include "rwsem.h"
4399 +#include "super.h"
4400 +
4401 +/* ---------------------------------------------------------------------- */
4402 +
4403 +/* a xino file */
4404 +struct au_xino {
4405 +       struct file             **xi_file;
4406 +       unsigned int            xi_nfile;
4407 +
4408 +       struct {
4409 +               spinlock_t              spin;
4410 +               ino_t                   *array;
4411 +               int                     total;
4412 +               /* reserved for future use */
4413 +               /* unsigned long        *bitmap; */
4414 +               wait_queue_head_t       wqh;
4415 +       } xi_nondir;
4416 +
4417 +       struct mutex            xi_mtx; /* protects xi_file array */
4418 +       struct hlist_bl_head    xi_writing;
4419 +
4420 +       atomic_t                xi_truncating;
4421 +
4422 +       struct kref             xi_kref;
4423 +};
4424 +
4425 +/* File-based Hierarchical Storage Management */
4426 +struct au_br_fhsm {
4427 +#ifdef CONFIG_AUFS_FHSM
4428 +       struct mutex            bf_lock;
4429 +       unsigned long           bf_jiffy;
4430 +       struct aufs_stfs        bf_stfs;
4431 +       int                     bf_readable;
4432 +#endif
4433 +};
4434 +
4435 +/* members for writable branch only */
4436 +enum {AuBrWh_BASE, AuBrWh_PLINK, AuBrWh_ORPH, AuBrWh_Last};
4437 +struct au_wbr {
4438 +       struct au_rwsem         wbr_wh_rwsem;
4439 +       struct dentry           *wbr_wh[AuBrWh_Last];
4440 +       atomic_t                wbr_wh_running;
4441 +#define wbr_whbase             wbr_wh[AuBrWh_BASE]     /* whiteout base */
4442 +#define wbr_plink              wbr_wh[AuBrWh_PLINK]    /* pseudo-link dir */
4443 +#define wbr_orph               wbr_wh[AuBrWh_ORPH]     /* dir for orphans */
4444 +
4445 +       /* mfs mode */
4446 +       unsigned long long      wbr_bytes;
4447 +};
4448 +
4449 +/* ext2 has 3 types of operations at least, ext3 has 4 */
4450 +#define AuBrDynOp (AuDyLast * 4)
4451 +
4452 +#ifdef CONFIG_AUFS_HFSNOTIFY
4453 +/* support for asynchronous destruction */
4454 +struct au_br_hfsnotify {
4455 +       struct fsnotify_group   *hfsn_group;
4456 +};
4457 +#endif
4458 +
4459 +/* sysfs entries */
4460 +struct au_brsysfs {
4461 +       char                    name[16];
4462 +       struct attribute        attr;
4463 +};
4464 +
4465 +enum {
4466 +       AuBrSysfs_BR,
4467 +       AuBrSysfs_BRID,
4468 +       AuBrSysfs_Last
4469 +};
4470 +
4471 +/* protected by superblock rwsem */
4472 +struct au_branch {
4473 +       struct au_xino          *br_xino;
4474 +
4475 +       aufs_bindex_t           br_id;
4476 +
4477 +       int                     br_perm;
4478 +       struct path             br_path;
4479 +       spinlock_t              br_dykey_lock;
4480 +       struct au_dykey         *br_dykey[AuBrDynOp];
4481 +       au_lcnt_t               br_nfiles;      /* opened files */
4482 +       au_lcnt_t               br_count;       /* in-use for other */
4483 +
4484 +       struct au_wbr           *br_wbr;
4485 +       struct au_br_fhsm       *br_fhsm;
4486 +
4487 +#ifdef CONFIG_AUFS_HFSNOTIFY
4488 +       struct au_br_hfsnotify  *br_hfsn;
4489 +#endif
4490 +
4491 +#ifdef CONFIG_SYSFS
4492 +       /* entries under sysfs per mount-point */
4493 +       struct au_brsysfs       br_sysfs[AuBrSysfs_Last];
4494 +#endif
4495 +
4496 +#ifdef CONFIG_DEBUG_FS
4497 +       struct dentry            *br_dbgaufs; /* xino */
4498 +#endif
4499 +
4500 +       struct au_dr_br         br_dirren;
4501 +};
4502 +
4503 +/* ---------------------------------------------------------------------- */
4504 +
4505 +static inline struct vfsmount *au_br_mnt(struct au_branch *br)
4506 +{
4507 +       return br->br_path.mnt;
4508 +}
4509 +
4510 +static inline struct dentry *au_br_dentry(struct au_branch *br)
4511 +{
4512 +       return br->br_path.dentry;
4513 +}
4514 +
4515 +static inline struct user_namespace *au_br_userns(struct au_branch *br)
4516 +{
4517 +       return mnt_user_ns(br->br_path.mnt);
4518 +}
4519 +
4520 +static inline struct super_block *au_br_sb(struct au_branch *br)
4521 +{
4522 +       return au_br_mnt(br)->mnt_sb;
4523 +}
4524 +
4525 +static inline int au_br_rdonly(struct au_branch *br)
4526 +{
4527 +       return (sb_rdonly(au_br_sb(br))
4528 +               || !au_br_writable(br->br_perm))
4529 +               ? -EROFS : 0;
4530 +}
4531 +
4532 +static inline int au_br_hnotifyable(int brperm __maybe_unused)
4533 +{
4534 +#ifdef CONFIG_AUFS_HNOTIFY
4535 +       return !(brperm & AuBrPerm_RR);
4536 +#else
4537 +       return 0;
4538 +#endif
4539 +}
4540 +
4541 +static inline int au_br_test_oflag(int oflag, struct au_branch *br)
4542 +{
4543 +       int err, exec_flag;
4544 +
4545 +       err = 0;
4546 +       exec_flag = oflag & __FMODE_EXEC;
4547 +       if (unlikely(exec_flag && path_noexec(&br->br_path)))
4548 +               err = -EACCES;
4549 +
4550 +       return err;
4551 +}
4552 +
4553 +static inline void au_xino_get(struct au_branch *br)
4554 +{
4555 +       struct au_xino *xi;
4556 +
4557 +       xi = br->br_xino;
4558 +       if (xi)
4559 +               kref_get(&xi->xi_kref);
4560 +}
4561 +
4562 +static inline int au_xino_count(struct au_branch *br)
4563 +{
4564 +       int v;
4565 +       struct au_xino *xi;
4566 +
4567 +       v = 0;
4568 +       xi = br->br_xino;
4569 +       if (xi)
4570 +               v = kref_read(&xi->xi_kref);
4571 +
4572 +       return v;
4573 +}
4574 +
4575 +/* ---------------------------------------------------------------------- */
4576 +
4577 +/* branch.c */
4578 +struct au_sbinfo;
4579 +void au_br_free(struct au_sbinfo *sinfo);
4580 +int au_br_index(struct super_block *sb, aufs_bindex_t br_id);
4581 +struct au_opt_add;
4582 +int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount);
4583 +struct au_opt_del;
4584 +int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount);
4585 +long au_ibusy_ioctl(struct file *file, unsigned long arg);
4586 +#ifdef CONFIG_COMPAT
4587 +long au_ibusy_compat_ioctl(struct file *file, unsigned long arg);
4588 +#endif
4589 +struct au_opt_mod;
4590 +int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
4591 +             int *do_refresh);
4592 +struct aufs_stfs;
4593 +int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs);
4594 +
4595 +/* xino.c */
4596 +static const loff_t au_loff_max = LLONG_MAX;
4597 +
4598 +aufs_bindex_t au_xi_root(struct super_block *sb, struct dentry *dentry);
4599 +struct file *au_xino_create(struct super_block *sb, char *fpath, int silent,
4600 +                           int wbrtop);
4601 +struct file *au_xino_create2(struct super_block *sb, struct path *base,
4602 +                            struct file *copy_src);
4603 +struct au_xi_new {
4604 +       struct au_xino *xi;     /* switch between xino and xigen */
4605 +       int idx;
4606 +       struct path *base;
4607 +       struct file *copy_src;
4608 +};
4609 +struct file *au_xi_new(struct super_block *sb, struct au_xi_new *xinew);
4610 +
4611 +int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4612 +                ino_t *ino);
4613 +int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4614 +                 ino_t ino);
4615 +ssize_t xino_fread(struct file *file, void *buf, size_t size, loff_t *pos);
4616 +ssize_t xino_fwrite(struct file *file, void *buf, size_t size, loff_t *pos);
4617 +
4618 +int au_xib_trunc(struct super_block *sb);
4619 +int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex, int idx_begin);
4620 +
4621 +struct au_xino *au_xino_alloc(unsigned int nfile);
4622 +int au_xino_put(struct au_branch *br);
4623 +struct file *au_xino_file1(struct au_xino *xi);
4624 +
4625 +struct au_opt_xino;
4626 +void au_xino_clr(struct super_block *sb);
4627 +int au_xino_set(struct super_block *sb, struct au_opt_xino *xiopt, int remount);
4628 +struct file *au_xino_def(struct super_block *sb);
4629 +int au_xino_init_br(struct super_block *sb, struct au_branch *br, ino_t hino,
4630 +                   struct path *base);
4631 +
4632 +ino_t au_xino_new_ino(struct super_block *sb);
4633 +void au_xino_delete_inode(struct inode *inode, const int unlinked);
4634 +
4635 +void au_xinondir_leave(struct super_block *sb, aufs_bindex_t bindex,
4636 +                      ino_t h_ino, int idx);
4637 +int au_xinondir_enter(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4638 +                     int *idx);
4639 +
4640 +int au_xino_path(struct seq_file *seq, struct file *file);
4641 +
4642 +/* ---------------------------------------------------------------------- */
4643 +
4644 +/* @idx is signed to accept -1 meaning the first file */
4645 +static inline struct file *au_xino_file(struct au_xino *xi, int idx)
4646 +{
4647 +       struct file *file;
4648 +
4649 +       file = NULL;
4650 +       if (!xi)
4651 +               goto out;
4652 +
4653 +       if (idx >= 0) {
4654 +               if (idx < xi->xi_nfile)
4655 +                       file = xi->xi_file[idx];
4656 +       } else
4657 +               file = au_xino_file1(xi);
4658 +
4659 +out:
4660 +       return file;
4661 +}
4662 +
4663 +/* ---------------------------------------------------------------------- */
4664 +
4665 +/* Superblock to branch */
4666 +static inline
4667 +aufs_bindex_t au_sbr_id(struct super_block *sb, aufs_bindex_t bindex)
4668 +{
4669 +       return au_sbr(sb, bindex)->br_id;
4670 +}
4671 +
4672 +static inline
4673 +struct vfsmount *au_sbr_mnt(struct super_block *sb, aufs_bindex_t bindex)
4674 +{
4675 +       return au_br_mnt(au_sbr(sb, bindex));
4676 +}
4677 +
4678 +static inline
4679 +struct user_namespace *au_sbr_userns(struct super_block *sb, aufs_bindex_t bindex)
4680 +{
4681 +       return au_br_userns(au_sbr(sb, bindex));
4682 +}
4683 +
4684 +static inline
4685 +struct super_block *au_sbr_sb(struct super_block *sb, aufs_bindex_t bindex)
4686 +{
4687 +       return au_br_sb(au_sbr(sb, bindex));
4688 +}
4689 +
4690 +static inline int au_sbr_perm(struct super_block *sb, aufs_bindex_t bindex)
4691 +{
4692 +       return au_sbr(sb, bindex)->br_perm;
4693 +}
4694 +
4695 +static inline int au_sbr_whable(struct super_block *sb, aufs_bindex_t bindex)
4696 +{
4697 +       return au_br_whable(au_sbr_perm(sb, bindex));
4698 +}
4699 +
4700 +/* ---------------------------------------------------------------------- */
4701 +
4702 +#define wbr_wh_read_lock(wbr)  au_rw_read_lock(&(wbr)->wbr_wh_rwsem)
4703 +#define wbr_wh_write_lock(wbr) au_rw_write_lock(&(wbr)->wbr_wh_rwsem)
4704 +#define wbr_wh_read_trylock(wbr)       au_rw_read_trylock(&(wbr)->wbr_wh_rwsem)
4705 +#define wbr_wh_write_trylock(wbr) au_rw_write_trylock(&(wbr)->wbr_wh_rwsem)
4706 +/*
4707 +#define wbr_wh_read_trylock_nested(wbr) \
4708 +       au_rw_read_trylock_nested(&(wbr)->wbr_wh_rwsem)
4709 +#define wbr_wh_write_trylock_nested(wbr) \
4710 +       au_rw_write_trylock_nested(&(wbr)->wbr_wh_rwsem)
4711 +*/
4712 +
4713 +#define wbr_wh_read_unlock(wbr)        au_rw_read_unlock(&(wbr)->wbr_wh_rwsem)
4714 +#define wbr_wh_write_unlock(wbr)       au_rw_write_unlock(&(wbr)->wbr_wh_rwsem)
4715 +#define wbr_wh_downgrade_lock(wbr)     au_rw_dgrade_lock(&(wbr)->wbr_wh_rwsem)
4716 +
4717 +#define WbrWhMustNoWaiters(wbr)        AuRwMustNoWaiters(&(wbr)->wbr_wh_rwsem)
4718 +#define WbrWhMustAnyLock(wbr)  AuRwMustAnyLock(&(wbr)->wbr_wh_rwsem)
4719 +#define WbrWhMustWriteLock(wbr)        AuRwMustWriteLock(&(wbr)->wbr_wh_rwsem)
4720 +
4721 +/* ---------------------------------------------------------------------- */
4722 +
4723 +#ifdef CONFIG_AUFS_FHSM
4724 +static inline void au_br_fhsm_init(struct au_br_fhsm *brfhsm)
4725 +{
4726 +       mutex_init(&brfhsm->bf_lock);
4727 +       brfhsm->bf_jiffy = 0;
4728 +       brfhsm->bf_readable = 0;
4729 +}
4730 +
4731 +static inline void au_br_fhsm_fin(struct au_br_fhsm *brfhsm)
4732 +{
4733 +       mutex_destroy(&brfhsm->bf_lock);
4734 +}
4735 +#else
4736 +AuStubVoid(au_br_fhsm_init, struct au_br_fhsm *brfhsm)
4737 +AuStubVoid(au_br_fhsm_fin, struct au_br_fhsm *brfhsm)
4738 +#endif
4739 +
4740 +#endif /* __KERNEL__ */
4741 +#endif /* __AUFS_BRANCH_H__ */
4742 diff -urN /usr/share/empty/fs/aufs/conf.mk linux/fs/aufs/conf.mk
4743 --- /usr/share/empty/fs/aufs/conf.mk    1970-01-01 01:00:00.000000000 +0100
4744 +++ linux/fs/aufs/conf.mk       2021-11-01 23:48:34.203025928 +0100
4745 @@ -0,0 +1,40 @@
4746 +# SPDX-License-Identifier: GPL-2.0
4747 +
4748 +AuConfStr = CONFIG_AUFS_FS=${CONFIG_AUFS_FS}
4749 +
4750 +define AuConf
4751 +ifdef ${1}
4752 +AuConfStr += ${1}=${${1}}
4753 +endif
4754 +endef
4755 +
4756 +AuConfAll = BRANCH_MAX_127 BRANCH_MAX_511 BRANCH_MAX_1023 BRANCH_MAX_32767 \
4757 +       SBILIST \
4758 +       HNOTIFY HFSNOTIFY \
4759 +       EXPORT INO_T_64 \
4760 +       XATTR \
4761 +       FHSM \
4762 +       RDU \
4763 +       DIRREN \
4764 +       SHWH \
4765 +       BR_RAMFS \
4766 +       BR_FUSE POLL \
4767 +       BR_HFSPLUS \
4768 +       BDEV_LOOP \
4769 +       DEBUG MAGIC_SYSRQ
4770 +$(foreach i, ${AuConfAll}, \
4771 +       $(eval $(call AuConf,CONFIG_AUFS_${i})))
4772 +
4773 +AuConfName = ${obj}/conf.str
4774 +${AuConfName}.tmp: FORCE
4775 +       @echo ${AuConfStr} | tr ' ' '\n' | sed -e 's/^/"/' -e 's/$$/\\n"/' > $@
4776 +${AuConfName}: ${AuConfName}.tmp
4777 +       @diff -q $< $@ > /dev/null 2>&1 || { \
4778 +       echo '  GEN    ' $@; \
4779 +       cp -p $< $@; \
4780 +       }
4781 +FORCE:
4782 +clean-files += ${AuConfName} ${AuConfName}.tmp
4783 +${obj}/sysfs.o: ${AuConfName}
4784 +
4785 +-include ${srctree}/${src}/conf_priv.mk
4786 diff -urN /usr/share/empty/fs/aufs/cpup.c linux/fs/aufs/cpup.c
4787 --- /usr/share/empty/fs/aufs/cpup.c     1970-01-01 01:00:00.000000000 +0100
4788 +++ linux/fs/aufs/cpup.c        2021-11-01 23:48:34.203025928 +0100
4789 @@ -0,0 +1,1459 @@
4790 +// SPDX-License-Identifier: GPL-2.0
4791 +/*
4792 + * Copyright (C) 2005-2021 Junjiro R. Okajima
4793 + *
4794 + * This program, aufs is free software; you can redistribute it and/or modify
4795 + * it under the terms of the GNU General Public License as published by
4796 + * the Free Software Foundation; either version 2 of the License, or
4797 + * (at your option) any later version.
4798 + *
4799 + * This program is distributed in the hope that it will be useful,
4800 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
4801 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
4802 + * GNU General Public License for more details.
4803 + *
4804 + * You should have received a copy of the GNU General Public License
4805 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
4806 + */
4807 +
4808 +/*
4809 + * copy-up functions, see wbr_policy.c for copy-down
4810 + */
4811 +
4812 +#include <linux/fs_stack.h>
4813 +#include <linux/mm.h>
4814 +#include <linux/task_work.h>
4815 +#include "aufs.h"
4816 +
4817 +void au_cpup_attr_flags(struct inode *dst, unsigned int iflags)
4818 +{
4819 +       const unsigned int mask = S_DEAD | S_SWAPFILE | S_PRIVATE
4820 +               | S_NOATIME | S_NOCMTIME | S_AUTOMOUNT;
4821 +
4822 +       BUILD_BUG_ON(sizeof(iflags) != sizeof(dst->i_flags));
4823 +
4824 +       dst->i_flags |= iflags & ~mask;
4825 +       if (au_test_fs_notime(dst->i_sb))
4826 +               dst->i_flags |= S_NOATIME | S_NOCMTIME;
4827 +}
4828 +
4829 +void au_cpup_attr_timesizes(struct inode *inode)
4830 +{
4831 +       struct inode *h_inode;
4832 +
4833 +       h_inode = au_h_iptr(inode, au_ibtop(inode));
4834 +       fsstack_copy_attr_times(inode, h_inode);
4835 +       fsstack_copy_inode_size(inode, h_inode);
4836 +}
4837 +
4838 +void au_cpup_attr_nlink(struct inode *inode, int force)
4839 +{
4840 +       struct inode *h_inode;
4841 +       struct super_block *sb;
4842 +       aufs_bindex_t bindex, bbot;
4843 +
4844 +       sb = inode->i_sb;
4845 +       bindex = au_ibtop(inode);
4846 +       h_inode = au_h_iptr(inode, bindex);
4847 +       if (!force
4848 +           && !S_ISDIR(h_inode->i_mode)
4849 +           && au_opt_test(au_mntflags(sb), PLINK)
4850 +           && au_plink_test(inode))
4851 +               return;
4852 +
4853 +       /*
4854 +        * 0 can happen in revalidating.
4855 +        * h_inode->i_mutex may not be held here, but it is harmless since once
4856 +        * i_nlink reaches 0, it will never become positive except O_TMPFILE
4857 +        * case.
4858 +        * todo: O_TMPFILE+linkat(AT_SYMLINK_FOLLOW) bypassing aufs may cause
4859 +        *       the incorrect link count.
4860 +        */
4861 +       set_nlink(inode, h_inode->i_nlink);
4862 +
4863 +       /*
4864 +        * fewer nlink makes find(1) noisy, but larger nlink doesn't.
4865 +        * it may includes whplink directory.
4866 +        */
4867 +       if (S_ISDIR(h_inode->i_mode)) {
4868 +               bbot = au_ibbot(inode);
4869 +               for (bindex++; bindex <= bbot; bindex++) {
4870 +                       h_inode = au_h_iptr(inode, bindex);
4871 +                       if (h_inode)
4872 +                               au_add_nlink(inode, h_inode);
4873 +               }
4874 +       }
4875 +}
4876 +
4877 +void au_cpup_attr_changeable(struct inode *inode)
4878 +{
4879 +       struct inode *h_inode;
4880 +
4881 +       h_inode = au_h_iptr(inode, au_ibtop(inode));
4882 +       inode->i_mode = h_inode->i_mode;
4883 +       inode->i_uid = h_inode->i_uid;
4884 +       inode->i_gid = h_inode->i_gid;
4885 +       au_cpup_attr_timesizes(inode);
4886 +       au_cpup_attr_flags(inode, h_inode->i_flags);
4887 +}
4888 +
4889 +void au_cpup_igen(struct inode *inode, struct inode *h_inode)
4890 +{
4891 +       struct au_iinfo *iinfo = au_ii(inode);
4892 +
4893 +       IiMustWriteLock(inode);
4894 +
4895 +       iinfo->ii_higen = h_inode->i_generation;
4896 +       iinfo->ii_hsb1 = h_inode->i_sb;
4897 +}
4898 +
4899 +void au_cpup_attr_all(struct inode *inode, int force)
4900 +{
4901 +       struct inode *h_inode;
4902 +
4903 +       h_inode = au_h_iptr(inode, au_ibtop(inode));
4904 +       au_cpup_attr_changeable(inode);
4905 +       if (inode->i_nlink > 0)
4906 +               au_cpup_attr_nlink(inode, force);
4907 +       inode->i_rdev = h_inode->i_rdev;
4908 +       inode->i_blkbits = h_inode->i_blkbits;
4909 +       au_cpup_igen(inode, h_inode);
4910 +}
4911 +
4912 +/* ---------------------------------------------------------------------- */
4913 +
4914 +/* Note: dt_dentry and dt_h_dentry are not dget/dput-ed */
4915 +
4916 +/* keep the timestamps of the parent dir when cpup */
4917 +void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
4918 +                   struct path *h_path)
4919 +{
4920 +       struct inode *h_inode;
4921 +
4922 +       dt->dt_dentry = dentry;
4923 +       dt->dt_h_path = *h_path;
4924 +       h_inode = d_inode(h_path->dentry);
4925 +       dt->dt_atime = h_inode->i_atime;
4926 +       dt->dt_mtime = h_inode->i_mtime;
4927 +       /* smp_mb(); */
4928 +}
4929 +
4930 +void au_dtime_revert(struct au_dtime *dt)
4931 +{
4932 +       struct iattr attr;
4933 +       int err;
4934 +
4935 +       attr.ia_atime = dt->dt_atime;
4936 +       attr.ia_mtime = dt->dt_mtime;
4937 +       attr.ia_valid = ATTR_FORCE | ATTR_MTIME | ATTR_MTIME_SET
4938 +               | ATTR_ATIME | ATTR_ATIME_SET;
4939 +
4940 +       /* no delegation since this is a directory */
4941 +       err = vfsub_notify_change(&dt->dt_h_path, &attr, /*delegated*/NULL);
4942 +       if (unlikely(err))
4943 +               pr_warn("restoring timestamps failed(%d). ignored\n", err);
4944 +}
4945 +
4946 +/* ---------------------------------------------------------------------- */
4947 +
4948 +/* internal use only */
4949 +struct au_cpup_reg_attr {
4950 +       int             valid;
4951 +       struct kstat    st;
4952 +       unsigned int    iflags; /* inode->i_flags */
4953 +};
4954 +
4955 +static noinline_for_stack
4956 +int cpup_iattr(struct dentry *dst, aufs_bindex_t bindex, struct path *h_src,
4957 +              struct au_cpup_reg_attr *h_src_attr)
4958 +{
4959 +       int err, sbits, icex;
4960 +       unsigned int mnt_flags;
4961 +       unsigned char verbose;
4962 +       struct iattr ia;
4963 +       struct path h_path;
4964 +       struct inode *h_isrc, *h_idst;
4965 +       struct kstat *h_st;
4966 +       struct au_branch *br;
4967 +
4968 +       br = au_sbr(dst->d_sb, bindex);
4969 +       h_path.mnt = au_br_mnt(br);
4970 +       h_path.dentry = au_h_dptr(dst, bindex);
4971 +       h_idst = d_inode(h_path.dentry);
4972 +       h_isrc = d_inode(h_src->dentry);
4973 +       ia.ia_valid = ATTR_FORCE | ATTR_UID | ATTR_GID
4974 +               | ATTR_ATIME | ATTR_MTIME
4975 +               | ATTR_ATIME_SET | ATTR_MTIME_SET;
4976 +       if (h_src_attr && h_src_attr->valid) {
4977 +               h_st = &h_src_attr->st;
4978 +               ia.ia_uid = h_st->uid;
4979 +               ia.ia_gid = h_st->gid;
4980 +               ia.ia_atime = h_st->atime;
4981 +               ia.ia_mtime = h_st->mtime;
4982 +               if (h_idst->i_mode != h_st->mode
4983 +                   && !S_ISLNK(h_idst->i_mode)) {
4984 +                       ia.ia_valid |= ATTR_MODE;
4985 +                       ia.ia_mode = h_st->mode;
4986 +               }
4987 +               sbits = !!(h_st->mode & (S_ISUID | S_ISGID));
4988 +               au_cpup_attr_flags(h_idst, h_src_attr->iflags);
4989 +       } else {
4990 +               ia.ia_uid = h_isrc->i_uid;
4991 +               ia.ia_gid = h_isrc->i_gid;
4992 +               ia.ia_atime = h_isrc->i_atime;
4993 +               ia.ia_mtime = h_isrc->i_mtime;
4994 +               if (h_idst->i_mode != h_isrc->i_mode
4995 +                   && !S_ISLNK(h_idst->i_mode)) {
4996 +                       ia.ia_valid |= ATTR_MODE;
4997 +                       ia.ia_mode = h_isrc->i_mode;
4998 +               }
4999 +               sbits = !!(h_isrc->i_mode & (S_ISUID | S_ISGID));
5000 +               au_cpup_attr_flags(h_idst, h_isrc->i_flags);
5001 +       }
5002 +       /* no delegation since it is just created */
5003 +       err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
5004 +
5005 +       /* is this nfs only? */
5006 +       if (!err && sbits && au_test_nfs(h_path.dentry->d_sb)) {
5007 +               ia.ia_valid = ATTR_FORCE | ATTR_MODE;
5008 +               ia.ia_mode = h_isrc->i_mode;
5009 +               err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
5010 +       }
5011 +
5012 +       icex = br->br_perm & AuBrAttr_ICEX;
5013 +       if (!err) {
5014 +               mnt_flags = au_mntflags(dst->d_sb);
5015 +               verbose = !!au_opt_test(mnt_flags, VERBOSE);
5016 +               err = au_cpup_xattr(&h_path, h_src, icex, verbose);
5017 +       }
5018 +
5019 +       return err;
5020 +}
5021 +
5022 +/* ---------------------------------------------------------------------- */
5023 +
5024 +static int au_do_copy_file(struct file *dst, struct file *src, loff_t len,
5025 +                          char *buf, unsigned long blksize)
5026 +{
5027 +       int err;
5028 +       size_t sz, rbytes, wbytes;
5029 +       unsigned char all_zero;
5030 +       char *p, *zp;
5031 +       struct inode *h_inode;
5032 +       /* reduce stack usage */
5033 +       struct iattr *ia;
5034 +
5035 +       zp = page_address(ZERO_PAGE(0));
5036 +       if (unlikely(!zp))
5037 +               return -ENOMEM; /* possible? */
5038 +
5039 +       err = 0;
5040 +       all_zero = 0;
5041 +       while (len) {
5042 +               AuDbg("len %lld\n", len);
5043 +               sz = blksize;
5044 +               if (len < blksize)
5045 +                       sz = len;
5046 +
5047 +               rbytes = 0;
5048 +               /* todo: signal_pending? */
5049 +               while (!rbytes || err == -EAGAIN || err == -EINTR) {
5050 +                       rbytes = vfsub_read_k(src, buf, sz, &src->f_pos);
5051 +                       err = rbytes;
5052 +               }
5053 +               if (unlikely(err < 0))
5054 +                       break;
5055 +
5056 +               all_zero = 0;
5057 +               if (len >= rbytes && rbytes == blksize)
5058 +                       all_zero = !memcmp(buf, zp, rbytes);
5059 +               if (!all_zero) {
5060 +                       wbytes = rbytes;
5061 +                       p = buf;
5062 +                       while (wbytes) {
5063 +                               size_t b;
5064 +
5065 +                               b = vfsub_write_k(dst, p, wbytes, &dst->f_pos);
5066 +                               err = b;
5067 +                               /* todo: signal_pending? */
5068 +                               if (unlikely(err == -EAGAIN || err == -EINTR))
5069 +                                       continue;
5070 +                               if (unlikely(err < 0))
5071 +                                       break;
5072 +                               wbytes -= b;
5073 +                               p += b;
5074 +                       }
5075 +                       if (unlikely(err < 0))
5076 +                               break;
5077 +               } else {
5078 +                       loff_t res;
5079 +
5080 +                       AuLabel(hole);
5081 +                       res = vfsub_llseek(dst, rbytes, SEEK_CUR);
5082 +                       err = res;
5083 +                       if (unlikely(res < 0))
5084 +                               break;
5085 +               }
5086 +               len -= rbytes;
5087 +               err = 0;
5088 +       }
5089 +
5090 +       /* the last block may be a hole */
5091 +       if (!err && all_zero) {
5092 +               AuLabel(last hole);
5093 +
5094 +               err = 1;
5095 +               if (au_test_nfs(dst->f_path.dentry->d_sb)) {
5096 +                       /* nfs requires this step to make last hole */
5097 +                       /* is this only nfs? */
5098 +                       do {
5099 +                               /* todo: signal_pending? */
5100 +                               err = vfsub_write_k(dst, "\0", 1, &dst->f_pos);
5101 +                       } while (err == -EAGAIN || err == -EINTR);
5102 +                       if (err == 1)
5103 +                               dst->f_pos--;
5104 +               }
5105 +
5106 +               if (err == 1) {
5107 +                       ia = (void *)buf;
5108 +                       ia->ia_size = dst->f_pos;
5109 +                       ia->ia_valid = ATTR_SIZE | ATTR_FILE;
5110 +                       ia->ia_file = dst;
5111 +                       h_inode = file_inode(dst);
5112 +                       inode_lock_nested(h_inode, AuLsc_I_CHILD2);
5113 +                       /* no delegation since it is just created */
5114 +                       err = vfsub_notify_change(&dst->f_path, ia,
5115 +                                                 /*delegated*/NULL);
5116 +                       inode_unlock(h_inode);
5117 +               }
5118 +       }
5119 +
5120 +       return err;
5121 +}
5122 +
5123 +int au_copy_file(struct file *dst, struct file *src, loff_t len)
5124 +{
5125 +       int err;
5126 +       unsigned long blksize;
5127 +       unsigned char do_kfree;
5128 +       char *buf;
5129 +       struct super_block *h_sb;
5130 +
5131 +       err = -ENOMEM;
5132 +       h_sb = file_inode(dst)->i_sb;
5133 +       blksize = h_sb->s_blocksize;
5134 +       if (!blksize || PAGE_SIZE < blksize)
5135 +               blksize = PAGE_SIZE;
5136 +       AuDbg("blksize %lu\n", blksize);
5137 +       do_kfree = (blksize != PAGE_SIZE && blksize >= sizeof(struct iattr *));
5138 +       if (do_kfree)
5139 +               buf = kmalloc(blksize, GFP_NOFS);
5140 +       else
5141 +               buf = (void *)__get_free_page(GFP_NOFS);
5142 +       if (unlikely(!buf))
5143 +               goto out;
5144 +
5145 +       if (len > (1 << 22))
5146 +               AuDbg("copying a large file %lld\n", (long long)len);
5147 +
5148 +       src->f_pos = 0;
5149 +       dst->f_pos = 0;
5150 +       err = au_do_copy_file(dst, src, len, buf, blksize);
5151 +       if (do_kfree) {
5152 +               AuDebugOn(!au_kfree_do_sz_test(blksize));
5153 +               au_kfree_do_rcu(buf);
5154 +       } else
5155 +               free_page((unsigned long)buf);
5156 +
5157 +out:
5158 +       return err;
5159 +}
5160 +
5161 +static int au_do_copy(struct file *dst, struct file *src, loff_t len)
5162 +{
5163 +       int err;
5164 +       struct super_block *h_src_sb;
5165 +       struct inode *h_src_inode;
5166 +
5167 +       h_src_inode = file_inode(src);
5168 +       h_src_sb = h_src_inode->i_sb;
5169 +
5170 +       /* XFS acquires inode_lock */
5171 +       if (!au_test_xfs(h_src_sb))
5172 +               err = au_copy_file(dst, src, len);
5173 +       else {
5174 +               inode_unlock_shared(h_src_inode);
5175 +               err = au_copy_file(dst, src, len);
5176 +               inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5177 +       }
5178 +
5179 +       return err;
5180 +}
5181 +
5182 +static int au_clone_or_copy(struct file *dst, struct file *src, loff_t len)
5183 +{
5184 +       int err;
5185 +       loff_t lo;
5186 +       struct super_block *h_src_sb;
5187 +       struct inode *h_src_inode;
5188 +
5189 +       h_src_inode = file_inode(src);
5190 +       h_src_sb = h_src_inode->i_sb;
5191 +       if (h_src_sb != file_inode(dst)->i_sb
5192 +           || !dst->f_op->remap_file_range) {
5193 +               err = au_do_copy(dst, src, len);
5194 +               goto out;
5195 +       }
5196 +
5197 +       if (!au_test_nfs(h_src_sb)) {
5198 +               inode_unlock_shared(h_src_inode);
5199 +               lo = vfsub_clone_file_range(src, dst, len);
5200 +               inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5201 +       } else
5202 +               lo = vfsub_clone_file_range(src, dst, len);
5203 +       if (lo == len) {
5204 +               err = 0;
5205 +               goto out; /* success */
5206 +       } else if (lo >= 0)
5207 +               /* todo: possible? */
5208 +               /* paritially succeeded */
5209 +               AuDbg("lo %lld, len %lld. Retrying.\n", lo, len);
5210 +       else if (lo != -EOPNOTSUPP) {
5211 +               /* older XFS has a condition in cloning */
5212 +               err = lo;
5213 +               goto out;
5214 +       }
5215 +
5216 +       /* the backend fs on NFS may not support cloning */
5217 +       err = au_do_copy(dst, src, len);
5218 +
5219 +out:
5220 +       AuTraceErr(err);
5221 +       return err;
5222 +}
5223 +
5224 +/*
5225 + * to support a sparse file which is opened with O_APPEND,
5226 + * we need to close the file.
5227 + */
5228 +static int au_cp_regular(struct au_cp_generic *cpg)
5229 +{
5230 +       int err, i;
5231 +       enum { SRC, DST };
5232 +       struct {
5233 +               aufs_bindex_t bindex;
5234 +               unsigned int flags;
5235 +               struct dentry *dentry;
5236 +               int force_wr;
5237 +               struct file *file;
5238 +       } *f, file[] = {
5239 +               {
5240 +                       .bindex = cpg->bsrc,
5241 +                       .flags = O_RDONLY | O_NOATIME | O_LARGEFILE,
5242 +               },
5243 +               {
5244 +                       .bindex = cpg->bdst,
5245 +                       .flags = O_WRONLY | O_NOATIME | O_LARGEFILE,
5246 +                       .force_wr = !!au_ftest_cpup(cpg->flags, RWDST),
5247 +               }
5248 +       };
5249 +       struct au_branch *br;
5250 +       struct super_block *sb, *h_src_sb;
5251 +       struct inode *h_src_inode;
5252 +       struct task_struct *tsk = current;
5253 +
5254 +       /* bsrc branch can be ro/rw. */
5255 +       sb = cpg->dentry->d_sb;
5256 +       f = file;
5257 +       for (i = 0; i < 2; i++, f++) {
5258 +               f->dentry = au_h_dptr(cpg->dentry, f->bindex);
5259 +               f->file = au_h_open(cpg->dentry, f->bindex, f->flags,
5260 +                                   /*file*/NULL, f->force_wr);
5261 +               if (IS_ERR(f->file)) {
5262 +                       err = PTR_ERR(f->file);
5263 +                       if (i == SRC)
5264 +                               goto out;
5265 +                       else
5266 +                               goto out_src;
5267 +               }
5268 +       }
5269 +
5270 +       /* try stopping to update while we copyup */
5271 +       h_src_inode = d_inode(file[SRC].dentry);
5272 +       h_src_sb = h_src_inode->i_sb;
5273 +       if (!au_test_nfs(h_src_sb))
5274 +               IMustLock(h_src_inode);
5275 +       err = au_clone_or_copy(file[DST].file, file[SRC].file, cpg->len);
5276 +
5277 +       /* i wonder if we had O_NO_DELAY_FPUT flag */
5278 +       if (tsk->flags & PF_KTHREAD)
5279 +               __fput_sync(file[DST].file);
5280 +       else {
5281 +               /* it happened actually */
5282 +               fput(file[DST].file);
5283 +               /*
5284 +                * too bad.
5285 +                * we have to call both since we don't know which place the file
5286 +                * was added to.
5287 +                */
5288 +               task_work_run();
5289 +               flush_delayed_fput();
5290 +       }
5291 +       br = au_sbr(sb, file[DST].bindex);
5292 +       au_lcnt_dec(&br->br_nfiles);
5293 +
5294 +out_src:
5295 +       fput(file[SRC].file);
5296 +       br = au_sbr(sb, file[SRC].bindex);
5297 +       au_lcnt_dec(&br->br_nfiles);
5298 +out:
5299 +       return err;
5300 +}
5301 +
5302 +static int au_do_cpup_regular(struct au_cp_generic *cpg,
5303 +                             struct au_cpup_reg_attr *h_src_attr)
5304 +{
5305 +       int err, rerr;
5306 +       loff_t l;
5307 +       struct path h_path;
5308 +       struct inode *h_src_inode, *h_dst_inode;
5309 +
5310 +       err = 0;
5311 +       h_src_inode = au_h_iptr(d_inode(cpg->dentry), cpg->bsrc);
5312 +       l = i_size_read(h_src_inode);
5313 +       if (cpg->len == -1 || l < cpg->len)
5314 +               cpg->len = l;
5315 +       if (cpg->len) {
5316 +               /* try stopping to update while we are referencing */
5317 +               inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5318 +               au_pin_hdir_unlock(cpg->pin);
5319 +
5320 +               h_path.dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
5321 +               h_path.mnt = au_sbr_mnt(cpg->dentry->d_sb, cpg->bsrc);
5322 +               h_src_attr->iflags = h_src_inode->i_flags;
5323 +               if (!au_test_nfs(h_src_inode->i_sb))
5324 +                       err = vfsub_getattr(&h_path, &h_src_attr->st);
5325 +               else {
5326 +                       inode_unlock_shared(h_src_inode);
5327 +                       err = vfsub_getattr(&h_path, &h_src_attr->st);
5328 +                       inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5329 +               }
5330 +               if (unlikely(err)) {
5331 +                       inode_unlock_shared(h_src_inode);
5332 +                       goto out;
5333 +               }
5334 +               h_src_attr->valid = 1;
5335 +               if (!au_test_nfs(h_src_inode->i_sb)) {
5336 +                       err = au_cp_regular(cpg);
5337 +                       inode_unlock_shared(h_src_inode);
5338 +               } else {
5339 +                       inode_unlock_shared(h_src_inode);
5340 +                       err = au_cp_regular(cpg);
5341 +               }
5342 +               rerr = au_pin_hdir_relock(cpg->pin);
5343 +               if (!err && rerr)
5344 +                       err = rerr;
5345 +       }
5346 +       if (!err && (h_src_inode->i_state & I_LINKABLE)) {
5347 +               h_path.dentry = au_h_dptr(cpg->dentry, cpg->bdst);
5348 +               h_dst_inode = d_inode(h_path.dentry);
5349 +               spin_lock(&h_dst_inode->i_lock);
5350 +               h_dst_inode->i_state |= I_LINKABLE;
5351 +               spin_unlock(&h_dst_inode->i_lock);
5352 +       }
5353 +
5354 +out:
5355 +       return err;
5356 +}
5357 +
5358 +static int au_do_cpup_symlink(struct path *h_path, struct dentry *h_src,
5359 +                             struct inode *h_dir)
5360 +{
5361 +       int err;
5362 +       DEFINE_DELAYED_CALL(done);
5363 +       const char *sym;
5364 +
5365 +       sym = vfs_get_link(h_src, &done);
5366 +       err = PTR_ERR(sym);
5367 +       if (IS_ERR(sym))
5368 +               goto out;
5369 +
5370 +       err = vfsub_symlink(h_dir, h_path, sym);
5371 +
5372 +out:
5373 +       do_delayed_call(&done);
5374 +       return err;
5375 +}
5376 +
5377 +/*
5378 + * regardless 'acl' option, reset all ACL.
5379 + * All ACL will be copied up later from the original entry on the lower branch.
5380 + */
5381 +static int au_reset_acl(struct inode *h_dir, struct path *h_path, umode_t mode)
5382 +{
5383 +       int err;
5384 +       struct dentry *h_dentry;
5385 +       struct inode *h_inode;
5386 +       struct user_namespace *h_userns;
5387 +
5388 +       h_userns = mnt_user_ns(h_path->mnt);
5389 +       h_dentry = h_path->dentry;
5390 +       h_inode = d_inode(h_dentry);
5391 +       /* forget_all_cached_acls(h_inode)); */
5392 +       err = vfsub_removexattr(h_userns, h_dentry, XATTR_NAME_POSIX_ACL_ACCESS);
5393 +       AuTraceErr(err);
5394 +       if (err == -EOPNOTSUPP)
5395 +               err = 0;
5396 +       if (!err)
5397 +               err = vfsub_acl_chmod(h_userns, h_inode, mode);
5398 +
5399 +       AuTraceErr(err);
5400 +       return err;
5401 +}
5402 +
5403 +static int au_do_cpup_dir(struct au_cp_generic *cpg, struct dentry *dst_parent,
5404 +                         struct inode *h_dir, struct path *h_path)
5405 +{
5406 +       int err;
5407 +       struct inode *dir, *inode;
5408 +       struct user_namespace *h_userns;
5409 +
5410 +       h_userns = mnt_user_ns(h_path->mnt);
5411 +       err = vfsub_removexattr(h_userns, h_path->dentry,
5412 +                               XATTR_NAME_POSIX_ACL_DEFAULT);
5413 +       AuTraceErr(err);
5414 +       if (err == -EOPNOTSUPP)
5415 +               err = 0;
5416 +       if (unlikely(err))
5417 +               goto out;
5418 +
5419 +       /*
5420 +        * strange behaviour from the users view,
5421 +        * particularly setattr case
5422 +        */
5423 +       dir = d_inode(dst_parent);
5424 +       if (au_ibtop(dir) == cpg->bdst)
5425 +               au_cpup_attr_nlink(dir, /*force*/1);
5426 +       inode = d_inode(cpg->dentry);
5427 +       au_cpup_attr_nlink(inode, /*force*/1);
5428 +
5429 +out:
5430 +       return err;
5431 +}
5432 +
5433 +static noinline_for_stack
5434 +int cpup_entry(struct au_cp_generic *cpg, struct dentry *dst_parent,
5435 +              struct au_cpup_reg_attr *h_src_attr)
5436 +{
5437 +       int err;
5438 +       umode_t mode;
5439 +       unsigned int mnt_flags;
5440 +       unsigned char isdir, isreg, force;
5441 +       const unsigned char do_dt = !!au_ftest_cpup(cpg->flags, DTIME);
5442 +       struct au_dtime dt;
5443 +       struct path h_path;
5444 +       struct dentry *h_src, *h_dst, *h_parent;
5445 +       struct inode *h_inode, *h_dir;
5446 +       struct super_block *sb;
5447 +
5448 +       /* bsrc branch can be ro/rw. */
5449 +       h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
5450 +       h_inode = d_inode(h_src);
5451 +       AuDebugOn(h_inode != au_h_iptr(d_inode(cpg->dentry), cpg->bsrc));
5452 +
5453 +       /* try stopping to be referenced while we are creating */
5454 +       h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
5455 +       if (au_ftest_cpup(cpg->flags, RENAME))
5456 +               AuDebugOn(strncmp(h_dst->d_name.name, AUFS_WH_PFX,
5457 +                                 AUFS_WH_PFX_LEN));
5458 +       h_parent = h_dst->d_parent; /* dir inode is locked */
5459 +       h_dir = d_inode(h_parent);
5460 +       IMustLock(h_dir);
5461 +       AuDebugOn(h_parent != h_dst->d_parent);
5462 +
5463 +       sb = cpg->dentry->d_sb;
5464 +       h_path.mnt = au_sbr_mnt(sb, cpg->bdst);
5465 +       if (do_dt) {
5466 +               h_path.dentry = h_parent;
5467 +               au_dtime_store(&dt, dst_parent, &h_path);
5468 +       }
5469 +       h_path.dentry = h_dst;
5470 +
5471 +       isreg = 0;
5472 +       isdir = 0;
5473 +       mode = h_inode->i_mode;
5474 +       switch (mode & S_IFMT) {
5475 +       case S_IFREG:
5476 +               isreg = 1;
5477 +               err = vfsub_create(h_dir, &h_path, 0600, /*want_excl*/true);
5478 +               if (!err)
5479 +                       err = au_do_cpup_regular(cpg, h_src_attr);
5480 +               break;
5481 +       case S_IFDIR:
5482 +               isdir = 1;
5483 +               err = vfsub_mkdir(h_dir, &h_path, mode);
5484 +               if (!err)
5485 +                       err = au_do_cpup_dir(cpg, dst_parent, h_dir, &h_path);
5486 +               break;
5487 +       case S_IFLNK:
5488 +               err = au_do_cpup_symlink(&h_path, h_src, h_dir);
5489 +               break;
5490 +       case S_IFCHR:
5491 +       case S_IFBLK:
5492 +               AuDebugOn(!capable(CAP_MKNOD));
5493 +               fallthrough;
5494 +       case S_IFIFO:
5495 +       case S_IFSOCK:
5496 +               err = vfsub_mknod(h_dir, &h_path, mode, h_inode->i_rdev);
5497 +               break;
5498 +       default:
5499 +               AuIOErr("Unknown inode type 0%o\n", mode);
5500 +               err = -EIO;
5501 +       }
5502 +       if (!err)
5503 +               err = au_reset_acl(h_dir, &h_path, mode);
5504 +
5505 +       mnt_flags = au_mntflags(sb);
5506 +       if (!au_opt_test(mnt_flags, UDBA_NONE)
5507 +           && !isdir
5508 +           && au_opt_test(mnt_flags, XINO)
5509 +           && (h_inode->i_nlink == 1
5510 +               || (h_inode->i_state & I_LINKABLE))
5511 +           /* todo: unnecessary? */
5512 +           /* && d_inode(cpg->dentry)->i_nlink == 1 */
5513 +           && cpg->bdst < cpg->bsrc
5514 +           && !au_ftest_cpup(cpg->flags, KEEPLINO))
5515 +               au_xino_write(sb, cpg->bsrc, h_inode->i_ino, /*ino*/0);
5516 +               /* ignore this error */
5517 +
5518 +       if (!err) {
5519 +               force = 0;
5520 +               if (isreg) {
5521 +                       force = !!cpg->len;
5522 +                       if (cpg->len == -1)
5523 +                               force = !!i_size_read(h_inode);
5524 +               }
5525 +               au_fhsm_wrote(sb, cpg->bdst, force);
5526 +       }
5527 +
5528 +       if (do_dt)
5529 +               au_dtime_revert(&dt);
5530 +       return err;
5531 +}
5532 +
5533 +static int au_do_ren_after_cpup(struct au_cp_generic *cpg, struct path *h_path)
5534 +{
5535 +       int err;
5536 +       struct dentry *dentry, *h_dentry, *h_parent, *parent;
5537 +       struct path h_ppath;
5538 +       struct inode *h_dir;
5539 +       aufs_bindex_t bdst;
5540 +
5541 +       dentry = cpg->dentry;
5542 +       bdst = cpg->bdst;
5543 +       h_ppath.mnt = au_sbr_mnt(dentry->d_sb, bdst);
5544 +       h_dentry = au_h_dptr(dentry, bdst);
5545 +       if (!au_ftest_cpup(cpg->flags, OVERWRITE)) {
5546 +               dget(h_dentry);
5547 +               au_set_h_dptr(dentry, bdst, NULL);
5548 +               err = au_lkup_neg(dentry, bdst, /*wh*/0);
5549 +               if (!err)
5550 +                       h_path->dentry = dget(au_h_dptr(dentry, bdst));
5551 +               au_set_h_dptr(dentry, bdst, h_dentry);
5552 +       } else {
5553 +               err = 0;
5554 +               parent = dget_parent(dentry);
5555 +               h_ppath.dentry = au_h_dptr(parent, bdst);
5556 +               dput(parent);
5557 +               h_path->dentry = vfsub_lkup_one(&dentry->d_name, &h_ppath);
5558 +               if (IS_ERR(h_path->dentry))
5559 +                       err = PTR_ERR(h_path->dentry);
5560 +       }
5561 +       if (unlikely(err))
5562 +               goto out;
5563 +
5564 +       h_parent = h_dentry->d_parent; /* dir inode is locked */
5565 +       h_dir = d_inode(h_parent);
5566 +       IMustLock(h_dir);
5567 +       AuDbg("%pd %pd\n", h_dentry, h_path->dentry);
5568 +       /* no delegation since it is just created */
5569 +       err = vfsub_rename(h_dir, h_dentry, h_dir, h_path, /*delegated*/NULL,
5570 +                          /*flags*/0);
5571 +       dput(h_path->dentry);
5572 +
5573 +out:
5574 +       return err;
5575 +}
5576 +
5577 +/*
5578 + * copyup the @dentry from @bsrc to @bdst.
5579 + * the caller must set the both of lower dentries.
5580 + * @len is for truncating when it is -1 copyup the entire file.
5581 + * in link/rename cases, @dst_parent may be different from the real one.
5582 + * basic->bsrc can be larger than basic->bdst.
5583 + * aufs doesn't touch the credential so
5584 + * security_inode_copy_up{,_xattr}() are unnecessary.
5585 + */
5586 +static int au_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
5587 +{
5588 +       int err, rerr;
5589 +       aufs_bindex_t old_ibtop;
5590 +       unsigned char isdir, plink;
5591 +       struct dentry *h_src, *h_dst, *h_parent;
5592 +       struct inode *dst_inode, *h_dir, *inode, *delegated, *src_inode;
5593 +       struct super_block *sb;
5594 +       struct au_branch *br;
5595 +       struct path h_src_path;
5596 +       /* to reduce stack size */
5597 +       struct {
5598 +               struct au_dtime dt;
5599 +               struct path h_path;
5600 +               struct au_cpup_reg_attr h_src_attr;
5601 +       } *a;
5602 +
5603 +       err = -ENOMEM;
5604 +       a = kmalloc(sizeof(*a), GFP_NOFS);
5605 +       if (unlikely(!a))
5606 +               goto out;
5607 +       a->h_src_attr.valid = 0;
5608 +
5609 +       sb = cpg->dentry->d_sb;
5610 +       br = au_sbr(sb, cpg->bdst);
5611 +       a->h_path.mnt = au_br_mnt(br);
5612 +       h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
5613 +       h_parent = h_dst->d_parent; /* dir inode is locked */
5614 +       h_dir = d_inode(h_parent);
5615 +       IMustLock(h_dir);
5616 +
5617 +       h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
5618 +       inode = d_inode(cpg->dentry);
5619 +
5620 +       if (!dst_parent)
5621 +               dst_parent = dget_parent(cpg->dentry);
5622 +       else
5623 +               dget(dst_parent);
5624 +
5625 +       plink = !!au_opt_test(au_mntflags(sb), PLINK);
5626 +       dst_inode = au_h_iptr(inode, cpg->bdst);
5627 +       if (dst_inode) {
5628 +               if (unlikely(!plink)) {
5629 +                       err = -EIO;
5630 +                       AuIOErr("hi%lu(i%lu) exists on b%d "
5631 +                               "but plink is disabled\n",
5632 +                               dst_inode->i_ino, inode->i_ino, cpg->bdst);
5633 +                       goto out_parent;
5634 +               }
5635 +
5636 +               if (dst_inode->i_nlink) {
5637 +                       const int do_dt = au_ftest_cpup(cpg->flags, DTIME);
5638 +
5639 +                       h_src = au_plink_lkup(inode, cpg->bdst);
5640 +                       err = PTR_ERR(h_src);
5641 +                       if (IS_ERR(h_src))
5642 +                               goto out_parent;
5643 +                       if (unlikely(d_is_negative(h_src))) {
5644 +                               err = -EIO;
5645 +                               AuIOErr("i%lu exists on b%d "
5646 +                                       "but not pseudo-linked\n",
5647 +                                       inode->i_ino, cpg->bdst);
5648 +                               dput(h_src);
5649 +                               goto out_parent;
5650 +                       }
5651 +
5652 +                       if (do_dt) {
5653 +                               a->h_path.dentry = h_parent;
5654 +                               au_dtime_store(&a->dt, dst_parent, &a->h_path);
5655 +                       }
5656 +
5657 +                       a->h_path.dentry = h_dst;
5658 +                       delegated = NULL;
5659 +                       err = vfsub_link(h_src, h_dir, &a->h_path, &delegated);
5660 +                       if (!err && au_ftest_cpup(cpg->flags, RENAME))
5661 +                               err = au_do_ren_after_cpup(cpg, &a->h_path);
5662 +                       if (do_dt)
5663 +                               au_dtime_revert(&a->dt);
5664 +                       if (unlikely(err == -EWOULDBLOCK)) {
5665 +                               pr_warn("cannot retry for NFSv4 delegation"
5666 +                                       " for an internal link\n");
5667 +                               iput(delegated);
5668 +                       }
5669 +                       dput(h_src);
5670 +                       goto out_parent;
5671 +               } else
5672 +                       /* todo: cpup_wh_file? */
5673 +                       /* udba work */
5674 +                       au_update_ibrange(inode, /*do_put_zero*/1);
5675 +       }
5676 +
5677 +       isdir = S_ISDIR(inode->i_mode);
5678 +       old_ibtop = au_ibtop(inode);
5679 +       err = cpup_entry(cpg, dst_parent, &a->h_src_attr);
5680 +       if (unlikely(err))
5681 +               goto out_rev;
5682 +       dst_inode = d_inode(h_dst);
5683 +       inode_lock_nested(dst_inode, AuLsc_I_CHILD2);
5684 +       /* todo: necessary? */
5685 +       /* au_pin_hdir_unlock(cpg->pin); */
5686 +
5687 +       h_src_path.dentry = h_src;
5688 +       h_src_path.mnt = au_sbr_mnt(sb, cpg->bsrc);
5689 +       err = cpup_iattr(cpg->dentry, cpg->bdst, &h_src_path, &a->h_src_attr);
5690 +       if (unlikely(err)) {
5691 +               /* todo: necessary? */
5692 +               /* au_pin_hdir_relock(cpg->pin); */ /* ignore an error */
5693 +               inode_unlock(dst_inode);
5694 +               goto out_rev;
5695 +       }
5696 +
5697 +       if (cpg->bdst < old_ibtop) {
5698 +               if (S_ISREG(inode->i_mode)) {
5699 +                       err = au_dy_iaop(inode, cpg->bdst, dst_inode);
5700 +                       if (unlikely(err)) {
5701 +                               /* ignore an error */
5702 +                               /* au_pin_hdir_relock(cpg->pin); */
5703 +                               inode_unlock(dst_inode);
5704 +                               goto out_rev;
5705 +                       }
5706 +               }
5707 +               au_set_ibtop(inode, cpg->bdst);
5708 +       } else
5709 +               au_set_ibbot(inode, cpg->bdst);
5710 +       au_set_h_iptr(inode, cpg->bdst, au_igrab(dst_inode),
5711 +                     au_hi_flags(inode, isdir));
5712 +
5713 +       /* todo: necessary? */
5714 +       /* err = au_pin_hdir_relock(cpg->pin); */
5715 +       inode_unlock(dst_inode);
5716 +       if (unlikely(err))
5717 +               goto out_rev;
5718 +
5719 +       src_inode = d_inode(h_src);
5720 +       if (!isdir
5721 +           && (src_inode->i_nlink > 1
5722 +               || src_inode->i_state & I_LINKABLE)
5723 +           && plink)
5724 +               au_plink_append(inode, cpg->bdst, h_dst);
5725 +
5726 +       if (au_ftest_cpup(cpg->flags, RENAME)) {
5727 +               a->h_path.dentry = h_dst;
5728 +               err = au_do_ren_after_cpup(cpg, &a->h_path);
5729 +       }
5730 +       if (!err)
5731 +               goto out_parent; /* success */
5732 +
5733 +       /* revert */
5734 +out_rev:
5735 +       a->h_path.dentry = h_parent;
5736 +       au_dtime_store(&a->dt, dst_parent, &a->h_path);
5737 +       a->h_path.dentry = h_dst;
5738 +       rerr = 0;
5739 +       if (d_is_positive(h_dst)) {
5740 +               if (!isdir) {
5741 +                       /* no delegation since it is just created */
5742 +                       rerr = vfsub_unlink(h_dir, &a->h_path,
5743 +                                           /*delegated*/NULL, /*force*/0);
5744 +               } else
5745 +                       rerr = vfsub_rmdir(h_dir, &a->h_path);
5746 +       }
5747 +       au_dtime_revert(&a->dt);
5748 +       if (rerr) {
5749 +               AuIOErr("failed removing broken entry(%d, %d)\n", err, rerr);
5750 +               err = -EIO;
5751 +       }
5752 +out_parent:
5753 +       dput(dst_parent);
5754 +       au_kfree_rcu(a);
5755 +out:
5756 +       return err;
5757 +}
5758 +
5759 +#if 0 /* reserved */
5760 +struct au_cpup_single_args {
5761 +       int *errp;
5762 +       struct au_cp_generic *cpg;
5763 +       struct dentry *dst_parent;
5764 +};
5765 +
5766 +static void au_call_cpup_single(void *args)
5767 +{
5768 +       struct au_cpup_single_args *a = args;
5769 +
5770 +       au_pin_hdir_acquire_nest(a->cpg->pin);
5771 +       *a->errp = au_cpup_single(a->cpg, a->dst_parent);
5772 +       au_pin_hdir_release(a->cpg->pin);
5773 +}
5774 +#endif
5775 +
5776 +/*
5777 + * prevent SIGXFSZ in copy-up.
5778 + * testing CAP_MKNOD is for generic fs,
5779 + * but CAP_FSETID is for xfs only, currently.
5780 + */
5781 +static int au_cpup_sio_test(struct au_pin *pin, umode_t mode)
5782 +{
5783 +       int do_sio;
5784 +       struct super_block *sb;
5785 +       struct inode *h_dir;
5786 +
5787 +       do_sio = 0;
5788 +       sb = au_pinned_parent(pin)->d_sb;
5789 +       if (!au_wkq_test()
5790 +           && (!au_sbi(sb)->si_plink_maint_pid
5791 +               || au_plink_maint(sb, AuLock_NOPLM))) {
5792 +               switch (mode & S_IFMT) {
5793 +               case S_IFREG:
5794 +                       /* no condition about RLIMIT_FSIZE and the file size */
5795 +                       do_sio = 1;
5796 +                       break;
5797 +               case S_IFCHR:
5798 +               case S_IFBLK:
5799 +                       do_sio = !capable(CAP_MKNOD);
5800 +                       break;
5801 +               }
5802 +               if (!do_sio)
5803 +                       do_sio = ((mode & (S_ISUID | S_ISGID))
5804 +                                 && !capable(CAP_FSETID));
5805 +               /* this workaround may be removed in the future */
5806 +               if (!do_sio) {
5807 +                       h_dir = au_pinned_h_dir(pin);
5808 +                       do_sio = h_dir->i_mode & S_ISVTX;
5809 +               }
5810 +       }
5811 +
5812 +       return do_sio;
5813 +}
5814 +
5815 +#if 0 /* reserved */
5816 +int au_sio_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
5817 +{
5818 +       int err, wkq_err;
5819 +       struct dentry *h_dentry;
5820 +
5821 +       h_dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
5822 +       if (!au_cpup_sio_test(pin, d_inode(h_dentry)->i_mode))
5823 +               err = au_cpup_single(cpg, dst_parent);
5824 +       else {
5825 +               struct au_cpup_single_args args = {
5826 +                       .errp           = &err,
5827 +                       .cpg            = cpg,
5828 +                       .dst_parent     = dst_parent
5829 +               };
5830 +               wkq_err = au_wkq_wait(au_call_cpup_single, &args);
5831 +               if (unlikely(wkq_err))
5832 +                       err = wkq_err;
5833 +       }
5834 +
5835 +       return err;
5836 +}
5837 +#endif
5838 +
5839 +/*
5840 + * copyup the @dentry from the first active lower branch to @bdst,
5841 + * using au_cpup_single().
5842 + */
5843 +static int au_cpup_simple(struct au_cp_generic *cpg)
5844 +{
5845 +       int err;
5846 +       unsigned int flags_orig;
5847 +       struct dentry *dentry;
5848 +
5849 +       AuDebugOn(cpg->bsrc < 0);
5850 +
5851 +       dentry = cpg->dentry;
5852 +       DiMustWriteLock(dentry);
5853 +
5854 +       err = au_lkup_neg(dentry, cpg->bdst, /*wh*/1);
5855 +       if (!err) {
5856 +               flags_orig = cpg->flags;
5857 +               au_fset_cpup(cpg->flags, RENAME);
5858 +               err = au_cpup_single(cpg, NULL);
5859 +               cpg->flags = flags_orig;
5860 +               if (!err)
5861 +                       return 0; /* success */
5862 +
5863 +               /* revert */
5864 +               au_set_h_dptr(dentry, cpg->bdst, NULL);
5865 +               au_set_dbtop(dentry, cpg->bsrc);
5866 +       }
5867 +
5868 +       return err;
5869 +}
5870 +
5871 +struct au_cpup_simple_args {
5872 +       int *errp;
5873 +       struct au_cp_generic *cpg;
5874 +};
5875 +
5876 +static void au_call_cpup_simple(void *args)
5877 +{
5878 +       struct au_cpup_simple_args *a = args;
5879 +
5880 +       au_pin_hdir_acquire_nest(a->cpg->pin);
5881 +       *a->errp = au_cpup_simple(a->cpg);
5882 +       au_pin_hdir_release(a->cpg->pin);
5883 +}
5884 +
5885 +static int au_do_sio_cpup_simple(struct au_cp_generic *cpg)
5886 +{
5887 +       int err, wkq_err;
5888 +       struct dentry *dentry, *parent;
5889 +       struct file *h_file;
5890 +       struct inode *h_dir;
5891 +       struct user_namespace *h_userns;
5892 +
5893 +       dentry = cpg->dentry;
5894 +       h_file = NULL;
5895 +       if (au_ftest_cpup(cpg->flags, HOPEN)) {
5896 +               AuDebugOn(cpg->bsrc < 0);
5897 +               h_file = au_h_open_pre(dentry, cpg->bsrc, /*force_wr*/0);
5898 +               err = PTR_ERR(h_file);
5899 +               if (IS_ERR(h_file))
5900 +                       goto out;
5901 +       }
5902 +
5903 +       parent = dget_parent(dentry);
5904 +       h_dir = au_h_iptr(d_inode(parent), cpg->bdst);
5905 +       h_userns = au_sbr_userns(dentry->d_sb, cpg->bdst);
5906 +       if (!au_test_h_perm_sio(h_userns, h_dir, MAY_EXEC | MAY_WRITE)
5907 +           && !au_cpup_sio_test(cpg->pin, d_inode(dentry)->i_mode))
5908 +               err = au_cpup_simple(cpg);
5909 +       else {
5910 +               struct au_cpup_simple_args args = {
5911 +                       .errp           = &err,
5912 +                       .cpg            = cpg
5913 +               };
5914 +               wkq_err = au_wkq_wait(au_call_cpup_simple, &args);
5915 +               if (unlikely(wkq_err))
5916 +                       err = wkq_err;
5917 +       }
5918 +
5919 +       dput(parent);
5920 +       if (h_file)
5921 +               au_h_open_post(dentry, cpg->bsrc, h_file);
5922 +
5923 +out:
5924 +       return err;
5925 +}
5926 +
5927 +int au_sio_cpup_simple(struct au_cp_generic *cpg)
5928 +{
5929 +       aufs_bindex_t bsrc, bbot;
5930 +       struct dentry *dentry, *h_dentry;
5931 +
5932 +       if (cpg->bsrc < 0) {
5933 +               dentry = cpg->dentry;
5934 +               bbot = au_dbbot(dentry);
5935 +               for (bsrc = cpg->bdst + 1; bsrc <= bbot; bsrc++) {
5936 +                       h_dentry = au_h_dptr(dentry, bsrc);
5937 +                       if (h_dentry) {
5938 +                               AuDebugOn(d_is_negative(h_dentry));
5939 +                               break;
5940 +                       }
5941 +               }
5942 +               AuDebugOn(bsrc > bbot);
5943 +               cpg->bsrc = bsrc;
5944 +       }
5945 +       AuDebugOn(cpg->bsrc <= cpg->bdst);
5946 +       return au_do_sio_cpup_simple(cpg);
5947 +}
5948 +
5949 +int au_sio_cpdown_simple(struct au_cp_generic *cpg)
5950 +{
5951 +       AuDebugOn(cpg->bdst <= cpg->bsrc);
5952 +       return au_do_sio_cpup_simple(cpg);
5953 +}
5954 +
5955 +/* ---------------------------------------------------------------------- */
5956 +
5957 +/*
5958 + * copyup the deleted file for writing.
5959 + */
5960 +static int au_do_cpup_wh(struct au_cp_generic *cpg, struct dentry *wh_dentry,
5961 +                        struct file *file)
5962 +{
5963 +       int err;
5964 +       unsigned int flags_orig;
5965 +       aufs_bindex_t bsrc_orig;
5966 +       struct au_dinfo *dinfo;
5967 +       struct {
5968 +               struct au_hdentry *hd;
5969 +               struct dentry *h_dentry;
5970 +       } hdst, hsrc;
5971 +
5972 +       dinfo = au_di(cpg->dentry);
5973 +       AuRwMustWriteLock(&dinfo->di_rwsem);
5974 +
5975 +       bsrc_orig = cpg->bsrc;
5976 +       cpg->bsrc = dinfo->di_btop;
5977 +       hdst.hd = au_hdentry(dinfo, cpg->bdst);
5978 +       hdst.h_dentry = hdst.hd->hd_dentry;
5979 +       hdst.hd->hd_dentry = wh_dentry;
5980 +       dinfo->di_btop = cpg->bdst;
5981 +
5982 +       hsrc.h_dentry = NULL;
5983 +       if (file) {
5984 +               hsrc.hd = au_hdentry(dinfo, cpg->bsrc);
5985 +               hsrc.h_dentry = hsrc.hd->hd_dentry;
5986 +               hsrc.hd->hd_dentry = au_hf_top(file)->f_path.dentry;
5987 +       }
5988 +       flags_orig = cpg->flags;
5989 +       cpg->flags = !AuCpup_DTIME;
5990 +       err = au_cpup_single(cpg, /*h_parent*/NULL);
5991 +       cpg->flags = flags_orig;
5992 +       if (file) {
5993 +               if (!err)
5994 +                       err = au_reopen_nondir(file);
5995 +               hsrc.hd->hd_dentry = hsrc.h_dentry;
5996 +       }
5997 +       hdst.hd->hd_dentry = hdst.h_dentry;
5998 +       dinfo->di_btop = cpg->bsrc;
5999 +       cpg->bsrc = bsrc_orig;
6000 +
6001 +       return err;
6002 +}
6003 +
6004 +static int au_cpup_wh(struct au_cp_generic *cpg, struct file *file)
6005 +{
6006 +       int err;
6007 +       aufs_bindex_t bdst;
6008 +       struct au_dtime dt;
6009 +       struct dentry *dentry, *parent, *h_parent, *wh_dentry;
6010 +       struct au_branch *br;
6011 +       struct path h_path;
6012 +
6013 +       dentry = cpg->dentry;
6014 +       bdst = cpg->bdst;
6015 +       br = au_sbr(dentry->d_sb, bdst);
6016 +       parent = dget_parent(dentry);
6017 +       h_parent = au_h_dptr(parent, bdst);
6018 +       wh_dentry = au_whtmp_lkup(h_parent, br, &dentry->d_name);
6019 +       err = PTR_ERR(wh_dentry);
6020 +       if (IS_ERR(wh_dentry))
6021 +               goto out;
6022 +
6023 +       h_path.dentry = h_parent;
6024 +       h_path.mnt = au_br_mnt(br);
6025 +       au_dtime_store(&dt, parent, &h_path);
6026 +       err = au_do_cpup_wh(cpg, wh_dentry, file);
6027 +       if (unlikely(err))
6028 +               goto out_wh;
6029 +
6030 +       dget(wh_dentry);
6031 +       h_path.dentry = wh_dentry;
6032 +       if (!d_is_dir(wh_dentry)) {
6033 +               /* no delegation since it is just created */
6034 +               err = vfsub_unlink(d_inode(h_parent), &h_path,
6035 +                                  /*delegated*/NULL, /*force*/0);
6036 +       } else
6037 +               err = vfsub_rmdir(d_inode(h_parent), &h_path);
6038 +       if (unlikely(err)) {
6039 +               AuIOErr("failed remove copied-up tmp file %pd(%d)\n",
6040 +                       wh_dentry, err);
6041 +               err = -EIO;
6042 +       }
6043 +       au_dtime_revert(&dt);
6044 +       au_set_hi_wh(d_inode(dentry), bdst, wh_dentry);
6045 +
6046 +out_wh:
6047 +       dput(wh_dentry);
6048 +out:
6049 +       dput(parent);
6050 +       return err;
6051 +}
6052 +
6053 +struct au_cpup_wh_args {
6054 +       int *errp;
6055 +       struct au_cp_generic *cpg;
6056 +       struct file *file;
6057 +};
6058 +
6059 +static void au_call_cpup_wh(void *args)
6060 +{
6061 +       struct au_cpup_wh_args *a = args;
6062 +
6063 +       au_pin_hdir_acquire_nest(a->cpg->pin);
6064 +       *a->errp = au_cpup_wh(a->cpg, a->file);
6065 +       au_pin_hdir_release(a->cpg->pin);
6066 +}
6067 +
6068 +int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file)
6069 +{
6070 +       int err, wkq_err;
6071 +       aufs_bindex_t bdst;
6072 +       struct dentry *dentry, *parent, *h_orph, *h_parent;
6073 +       struct inode *dir, *h_dir, *h_tmpdir;
6074 +       struct au_wbr *wbr;
6075 +       struct au_pin wh_pin, *pin_orig;
6076 +       struct user_namespace *h_userns;
6077 +
6078 +       dentry = cpg->dentry;
6079 +       bdst = cpg->bdst;
6080 +       parent = dget_parent(dentry);
6081 +       dir = d_inode(parent);
6082 +       h_orph = NULL;
6083 +       h_parent = NULL;
6084 +       h_dir = au_igrab(au_h_iptr(dir, bdst));
6085 +       h_tmpdir = h_dir;
6086 +       pin_orig = NULL;
6087 +       if (!h_dir->i_nlink) {
6088 +               wbr = au_sbr(dentry->d_sb, bdst)->br_wbr;
6089 +               h_orph = wbr->wbr_orph;
6090 +
6091 +               h_parent = dget(au_h_dptr(parent, bdst));
6092 +               au_set_h_dptr(parent, bdst, dget(h_orph));
6093 +               h_tmpdir = d_inode(h_orph);
6094 +               au_set_h_iptr(dir, bdst, au_igrab(h_tmpdir), /*flags*/0);
6095 +
6096 +               inode_lock_nested(h_tmpdir, AuLsc_I_PARENT3);
6097 +               /* todo: au_h_open_pre()? */
6098 +
6099 +               pin_orig = cpg->pin;
6100 +               au_pin_init(&wh_pin, dentry, bdst, AuLsc_DI_PARENT,
6101 +                           AuLsc_I_PARENT3, cpg->pin->udba, AuPin_DI_LOCKED);
6102 +               cpg->pin = &wh_pin;
6103 +       }
6104 +
6105 +       h_userns = au_sbr_userns(dentry->d_sb, bdst);
6106 +       if (!au_test_h_perm_sio(h_userns, h_tmpdir, MAY_EXEC | MAY_WRITE)
6107 +           && !au_cpup_sio_test(cpg->pin, d_inode(dentry)->i_mode))
6108 +               err = au_cpup_wh(cpg, file);
6109 +       else {
6110 +               struct au_cpup_wh_args args = {
6111 +                       .errp   = &err,
6112 +                       .cpg    = cpg,
6113 +                       .file   = file
6114 +               };
6115 +               wkq_err = au_wkq_wait(au_call_cpup_wh, &args);
6116 +               if (unlikely(wkq_err))
6117 +                       err = wkq_err;
6118 +       }
6119 +
6120 +       if (h_orph) {
6121 +               inode_unlock(h_tmpdir);
6122 +               /* todo: au_h_open_post()? */
6123 +               au_set_h_iptr(dir, bdst, au_igrab(h_dir), /*flags*/0);
6124 +               au_set_h_dptr(parent, bdst, h_parent);
6125 +               AuDebugOn(!pin_orig);
6126 +               cpg->pin = pin_orig;
6127 +       }
6128 +       iput(h_dir);
6129 +       dput(parent);
6130 +
6131 +       return err;
6132 +}
6133 +
6134 +/* ---------------------------------------------------------------------- */
6135 +
6136 +/*
6137 + * generic routine for both of copy-up and copy-down.
6138 + */
6139 +/* cf. revalidate function in file.c */
6140 +int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
6141 +              int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
6142 +                        struct au_pin *pin,
6143 +                        struct dentry *h_parent, void *arg),
6144 +              void *arg)
6145 +{
6146 +       int err;
6147 +       struct au_pin pin;
6148 +       struct dentry *d, *parent, *h_parent, *real_parent, *h_dentry;
6149 +
6150 +       err = 0;
6151 +       parent = dget_parent(dentry);
6152 +       if (IS_ROOT(parent))
6153 +               goto out;
6154 +
6155 +       au_pin_init(&pin, dentry, bdst, AuLsc_DI_PARENT2, AuLsc_I_PARENT2,
6156 +                   au_opt_udba(dentry->d_sb), AuPin_MNT_WRITE);
6157 +
6158 +       /* do not use au_dpage */
6159 +       real_parent = parent;
6160 +       while (1) {
6161 +               dput(parent);
6162 +               parent = dget_parent(dentry);
6163 +               h_parent = au_h_dptr(parent, bdst);
6164 +               if (h_parent)
6165 +                       goto out; /* success */
6166 +
6167 +               /* find top dir which is necessary to cpup */
6168 +               do {
6169 +                       d = parent;
6170 +                       dput(parent);
6171 +                       parent = dget_parent(d);
6172 +                       di_read_lock_parent3(parent, !AuLock_IR);
6173 +                       h_parent = au_h_dptr(parent, bdst);
6174 +                       di_read_unlock(parent, !AuLock_IR);
6175 +               } while (!h_parent);
6176 +
6177 +               if (d != real_parent)
6178 +                       di_write_lock_child3(d);
6179 +
6180 +               /* somebody else might create while we were sleeping */
6181 +               h_dentry = au_h_dptr(d, bdst);
6182 +               if (!h_dentry || d_is_negative(h_dentry)) {
6183 +                       if (h_dentry)
6184 +                               au_update_dbtop(d);
6185 +
6186 +                       au_pin_set_dentry(&pin, d);
6187 +                       err = au_do_pin(&pin);
6188 +                       if (!err) {
6189 +                               err = cp(d, bdst, &pin, h_parent, arg);
6190 +                               au_unpin(&pin);
6191 +                       }
6192 +               }
6193 +
6194 +               if (d != real_parent)
6195 +                       di_write_unlock(d);
6196 +               if (unlikely(err))
6197 +                       break;
6198 +       }
6199 +
6200 +out:
6201 +       dput(parent);
6202 +       return err;
6203 +}
6204 +
6205 +static int au_cpup_dir(struct dentry *dentry, aufs_bindex_t bdst,
6206 +                      struct au_pin *pin,
6207 +                      struct dentry *h_parent __maybe_unused,
6208 +                      void *arg __maybe_unused)
6209 +{
6210 +       struct au_cp_generic cpg = {
6211 +               .dentry = dentry,
6212 +               .bdst   = bdst,
6213 +               .bsrc   = -1,
6214 +               .len    = 0,
6215 +               .pin    = pin,
6216 +               .flags  = AuCpup_DTIME
6217 +       };
6218 +       return au_sio_cpup_simple(&cpg);
6219 +}
6220 +
6221 +int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
6222 +{
6223 +       return au_cp_dirs(dentry, bdst, au_cpup_dir, NULL);
6224 +}
6225 +
6226 +int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
6227 +{
6228 +       int err;
6229 +       struct dentry *parent;
6230 +       struct inode *dir;
6231 +
6232 +       parent = dget_parent(dentry);
6233 +       dir = d_inode(parent);
6234 +       err = 0;
6235 +       if (au_h_iptr(dir, bdst))
6236 +               goto out;
6237 +
6238 +       di_read_unlock(parent, AuLock_IR);
6239 +       di_write_lock_parent(parent);
6240 +       /* someone else might change our inode while we were sleeping */
6241 +       if (!au_h_iptr(dir, bdst))
6242 +               err = au_cpup_dirs(dentry, bdst);
6243 +       di_downgrade_lock(parent, AuLock_IR);
6244 +
6245 +out:
6246 +       dput(parent);
6247 +       return err;
6248 +}
6249 diff -urN /usr/share/empty/fs/aufs/cpup.h linux/fs/aufs/cpup.h
6250 --- /usr/share/empty/fs/aufs/cpup.h     1970-01-01 01:00:00.000000000 +0100
6251 +++ linux/fs/aufs/cpup.h        2021-11-01 23:48:34.203025928 +0100
6252 @@ -0,0 +1,100 @@
6253 +/* SPDX-License-Identifier: GPL-2.0 */
6254 +/*
6255 + * Copyright (C) 2005-2021 Junjiro R. Okajima
6256 + *
6257 + * This program, aufs is free software; you can redistribute it and/or modify
6258 + * it under the terms of the GNU General Public License as published by
6259 + * the Free Software Foundation; either version 2 of the License, or
6260 + * (at your option) any later version.
6261 + *
6262 + * This program is distributed in the hope that it will be useful,
6263 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6264 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6265 + * GNU General Public License for more details.
6266 + *
6267 + * You should have received a copy of the GNU General Public License
6268 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6269 + */
6270 +
6271 +/*
6272 + * copy-up/down functions
6273 + */
6274 +
6275 +#ifndef __AUFS_CPUP_H__
6276 +#define __AUFS_CPUP_H__
6277 +
6278 +#ifdef __KERNEL__
6279 +
6280 +#include <linux/path.h>
6281 +
6282 +struct inode;
6283 +struct file;
6284 +struct au_pin;
6285 +
6286 +void au_cpup_attr_flags(struct inode *dst, unsigned int iflags);
6287 +void au_cpup_attr_timesizes(struct inode *inode);
6288 +void au_cpup_attr_nlink(struct inode *inode, int force);
6289 +void au_cpup_attr_changeable(struct inode *inode);
6290 +void au_cpup_igen(struct inode *inode, struct inode *h_inode);
6291 +void au_cpup_attr_all(struct inode *inode, int force);
6292 +
6293 +/* ---------------------------------------------------------------------- */
6294 +
6295 +struct au_cp_generic {
6296 +       struct dentry   *dentry;
6297 +       aufs_bindex_t   bdst, bsrc;
6298 +       loff_t          len;
6299 +       struct au_pin   *pin;
6300 +       unsigned int    flags;
6301 +};
6302 +
6303 +/* cpup flags */
6304 +#define AuCpup_DTIME           1               /* do dtime_store/revert */
6305 +#define AuCpup_KEEPLINO                (1 << 1)        /* do not clear the lower xino,
6306 +                                                  for link(2) */
6307 +#define AuCpup_RENAME          (1 << 2)        /* rename after cpup */
6308 +#define AuCpup_HOPEN           (1 << 3)        /* call h_open_pre/post() in
6309 +                                                  cpup */
6310 +#define AuCpup_OVERWRITE       (1 << 4)        /* allow overwriting the
6311 +                                                  existing entry */
6312 +#define AuCpup_RWDST           (1 << 5)        /* force write target even if
6313 +                                                  the branch is marked as RO */
6314 +
6315 +#ifndef CONFIG_AUFS_BR_HFSPLUS
6316 +#undef AuCpup_HOPEN
6317 +#define AuCpup_HOPEN           0
6318 +#endif
6319 +
6320 +#define au_ftest_cpup(flags, name)     ((flags) & AuCpup_##name)
6321 +#define au_fset_cpup(flags, name) \
6322 +       do { (flags) |= AuCpup_##name; } while (0)
6323 +#define au_fclr_cpup(flags, name) \
6324 +       do { (flags) &= ~AuCpup_##name; } while (0)
6325 +
6326 +int au_copy_file(struct file *dst, struct file *src, loff_t len);
6327 +int au_sio_cpup_simple(struct au_cp_generic *cpg);
6328 +int au_sio_cpdown_simple(struct au_cp_generic *cpg);
6329 +int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file);
6330 +
6331 +int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
6332 +              int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
6333 +                        struct au_pin *pin,
6334 +                        struct dentry *h_parent, void *arg),
6335 +              void *arg);
6336 +int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
6337 +int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
6338 +
6339 +/* ---------------------------------------------------------------------- */
6340 +
6341 +/* keep timestamps when copyup */
6342 +struct au_dtime {
6343 +       struct dentry *dt_dentry;
6344 +       struct path dt_h_path;
6345 +       struct timespec64 dt_atime, dt_mtime;
6346 +};
6347 +void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
6348 +                   struct path *h_path);
6349 +void au_dtime_revert(struct au_dtime *dt);
6350 +
6351 +#endif /* __KERNEL__ */
6352 +#endif /* __AUFS_CPUP_H__ */
6353 diff -urN /usr/share/empty/fs/aufs/dbgaufs.c linux/fs/aufs/dbgaufs.c
6354 --- /usr/share/empty/fs/aufs/dbgaufs.c  1970-01-01 01:00:00.000000000 +0100
6355 +++ linux/fs/aufs/dbgaufs.c     2021-11-01 23:48:34.203025928 +0100
6356 @@ -0,0 +1,526 @@
6357 +// SPDX-License-Identifier: GPL-2.0
6358 +/*
6359 + * Copyright (C) 2005-2021 Junjiro R. Okajima
6360 + *
6361 + * This program, aufs is free software; you can redistribute it and/or modify
6362 + * it under the terms of the GNU General Public License as published by
6363 + * the Free Software Foundation; either version 2 of the License, or
6364 + * (at your option) any later version.
6365 + *
6366 + * This program is distributed in the hope that it will be useful,
6367 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6368 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6369 + * GNU General Public License for more details.
6370 + *
6371 + * You should have received a copy of the GNU General Public License
6372 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6373 + */
6374 +
6375 +/*
6376 + * debugfs interface
6377 + */
6378 +
6379 +#include <linux/debugfs.h>
6380 +#include "aufs.h"
6381 +
6382 +#ifndef CONFIG_SYSFS
6383 +#error DEBUG_FS depends upon SYSFS
6384 +#endif
6385 +
6386 +static struct dentry *dbgaufs;
6387 +static const mode_t dbgaufs_mode = 0444;
6388 +
6389 +/* 20 is max digits length of ulong 64 */
6390 +struct dbgaufs_arg {
6391 +       int n;
6392 +       char a[20 * 4];
6393 +};
6394 +
6395 +/*
6396 + * common function for all XINO files
6397 + */
6398 +static int dbgaufs_xi_release(struct inode *inode __maybe_unused,
6399 +                             struct file *file)
6400 +{
6401 +       void *p;
6402 +
6403 +       p = file->private_data;
6404 +       if (p) {
6405 +               /* this is struct dbgaufs_arg */
6406 +               AuDebugOn(!au_kfree_sz_test(p));
6407 +               au_kfree_do_rcu(p);
6408 +       }
6409 +       return 0;
6410 +}
6411 +
6412 +static int dbgaufs_xi_open(struct file *xf, struct file *file, int do_fcnt,
6413 +                          int cnt)
6414 +{
6415 +       int err;
6416 +       struct kstat st;
6417 +       struct dbgaufs_arg *p;
6418 +
6419 +       err = -ENOMEM;
6420 +       p = kmalloc(sizeof(*p), GFP_NOFS);
6421 +       if (unlikely(!p))
6422 +               goto out;
6423 +
6424 +       err = 0;
6425 +       p->n = 0;
6426 +       file->private_data = p;
6427 +       if (!xf)
6428 +               goto out;
6429 +
6430 +       err = vfsub_getattr(&xf->f_path, &st);
6431 +       if (!err) {
6432 +               if (do_fcnt)
6433 +                       p->n = snprintf
6434 +                               (p->a, sizeof(p->a), "%d, %llux%u %lld\n",
6435 +                                cnt, st.blocks, st.blksize,
6436 +                                (long long)st.size);
6437 +               else
6438 +                       p->n = snprintf(p->a, sizeof(p->a), "%llux%u %lld\n",
6439 +                                       st.blocks, st.blksize,
6440 +                                       (long long)st.size);
6441 +               AuDebugOn(p->n >= sizeof(p->a));
6442 +       } else {
6443 +               p->n = snprintf(p->a, sizeof(p->a), "err %d\n", err);
6444 +               err = 0;
6445 +       }
6446 +
6447 +out:
6448 +       return err;
6449 +}
6450 +
6451 +static ssize_t dbgaufs_xi_read(struct file *file, char __user *buf,
6452 +                              size_t count, loff_t *ppos)
6453 +{
6454 +       struct dbgaufs_arg *p;
6455 +
6456 +       p = file->private_data;
6457 +       return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
6458 +}
6459 +
6460 +/* ---------------------------------------------------------------------- */
6461 +
6462 +struct dbgaufs_plink_arg {
6463 +       int n;
6464 +       char a[];
6465 +};
6466 +
6467 +static int dbgaufs_plink_release(struct inode *inode __maybe_unused,
6468 +                                struct file *file)
6469 +{
6470 +       free_page((unsigned long)file->private_data);
6471 +       return 0;
6472 +}
6473 +
6474 +static int dbgaufs_plink_open(struct inode *inode, struct file *file)
6475 +{
6476 +       int err, i, limit;
6477 +       unsigned long n, sum;
6478 +       struct dbgaufs_plink_arg *p;
6479 +       struct au_sbinfo *sbinfo;
6480 +       struct super_block *sb;
6481 +       struct hlist_bl_head *hbl;
6482 +
6483 +       err = -ENOMEM;
6484 +       p = (void *)get_zeroed_page(GFP_NOFS);
6485 +       if (unlikely(!p))
6486 +               goto out;
6487 +
6488 +       err = -EFBIG;
6489 +       sbinfo = inode->i_private;
6490 +       sb = sbinfo->si_sb;
6491 +       si_noflush_read_lock(sb);
6492 +       if (au_opt_test(au_mntflags(sb), PLINK)) {
6493 +               limit = PAGE_SIZE - sizeof(p->n);
6494 +
6495 +               /* the number of buckets */
6496 +               n = snprintf(p->a + p->n, limit, "%d\n", AuPlink_NHASH);
6497 +               p->n += n;
6498 +               limit -= n;
6499 +
6500 +               sum = 0;
6501 +               for (i = 0, hbl = sbinfo->si_plink; i < AuPlink_NHASH;
6502 +                    i++, hbl++) {
6503 +                       n = au_hbl_count(hbl);
6504 +                       sum += n;
6505 +
6506 +                       n = snprintf(p->a + p->n, limit, "%lu ", n);
6507 +                       p->n += n;
6508 +                       limit -= n;
6509 +                       if (unlikely(limit <= 0))
6510 +                               goto out_free;
6511 +               }
6512 +               p->a[p->n - 1] = '\n';
6513 +
6514 +               /* the sum of plinks */
6515 +               n = snprintf(p->a + p->n, limit, "%lu\n", sum);
6516 +               p->n += n;
6517 +               limit -= n;
6518 +               if (unlikely(limit <= 0))
6519 +                       goto out_free;
6520 +       } else {
6521 +#define str "1\n0\n0\n"
6522 +               p->n = sizeof(str) - 1;
6523 +               strcpy(p->a, str);
6524 +#undef str
6525 +       }
6526 +       si_read_unlock(sb);
6527 +
6528 +       err = 0;
6529 +       file->private_data = p;
6530 +       goto out; /* success */
6531 +
6532 +out_free:
6533 +       free_page((unsigned long)p);
6534 +out:
6535 +       return err;
6536 +}
6537 +
6538 +static ssize_t dbgaufs_plink_read(struct file *file, char __user *buf,
6539 +                                 size_t count, loff_t *ppos)
6540 +{
6541 +       struct dbgaufs_plink_arg *p;
6542 +
6543 +       p = file->private_data;
6544 +       return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
6545 +}
6546 +
6547 +static const struct file_operations dbgaufs_plink_fop = {
6548 +       .owner          = THIS_MODULE,
6549 +       .open           = dbgaufs_plink_open,
6550 +       .release        = dbgaufs_plink_release,
6551 +       .read           = dbgaufs_plink_read
6552 +};
6553 +
6554 +/* ---------------------------------------------------------------------- */
6555 +
6556 +static int dbgaufs_xib_open(struct inode *inode, struct file *file)
6557 +{
6558 +       int err;
6559 +       struct au_sbinfo *sbinfo;
6560 +       struct super_block *sb;
6561 +
6562 +       sbinfo = inode->i_private;
6563 +       sb = sbinfo->si_sb;
6564 +       si_noflush_read_lock(sb);
6565 +       err = dbgaufs_xi_open(sbinfo->si_xib, file, /*do_fcnt*/0, /*cnt*/0);
6566 +       si_read_unlock(sb);
6567 +       return err;
6568 +}
6569 +
6570 +static const struct file_operations dbgaufs_xib_fop = {
6571 +       .owner          = THIS_MODULE,
6572 +       .open           = dbgaufs_xib_open,
6573 +       .release        = dbgaufs_xi_release,
6574 +       .read           = dbgaufs_xi_read
6575 +};
6576 +
6577 +/* ---------------------------------------------------------------------- */
6578 +
6579 +#define DbgaufsXi_PREFIX "xi"
6580 +
6581 +static int dbgaufs_xino_open(struct inode *inode, struct file *file)
6582 +{
6583 +       int err, idx;
6584 +       long l;
6585 +       aufs_bindex_t bindex;
6586 +       char *p, a[sizeof(DbgaufsXi_PREFIX) + 8];
6587 +       struct au_sbinfo *sbinfo;
6588 +       struct super_block *sb;
6589 +       struct au_xino *xi;
6590 +       struct file *xf;
6591 +       struct qstr *name;
6592 +       struct au_branch *br;
6593 +
6594 +       err = -ENOENT;
6595 +       name = &file->f_path.dentry->d_name;
6596 +       if (unlikely(name->len < sizeof(DbgaufsXi_PREFIX)
6597 +                    || memcmp(name->name, DbgaufsXi_PREFIX,
6598 +                              sizeof(DbgaufsXi_PREFIX) - 1)))
6599 +               goto out;
6600 +
6601 +       AuDebugOn(name->len >= sizeof(a));
6602 +       memcpy(a, name->name, name->len);
6603 +       a[name->len] = '\0';
6604 +       p = strchr(a, '-');
6605 +       if (p)
6606 +               *p = '\0';
6607 +       err = kstrtol(a + sizeof(DbgaufsXi_PREFIX) - 1, 10, &l);
6608 +       if (unlikely(err))
6609 +               goto out;
6610 +       bindex = l;
6611 +       idx = 0;
6612 +       if (p) {
6613 +               err = kstrtol(p + 1, 10, &l);
6614 +               if (unlikely(err))
6615 +                       goto out;
6616 +               idx = l;
6617 +       }
6618 +
6619 +       err = -ENOENT;
6620 +       sbinfo = inode->i_private;
6621 +       sb = sbinfo->si_sb;
6622 +       si_noflush_read_lock(sb);
6623 +       if (unlikely(bindex < 0 || bindex > au_sbbot(sb)))
6624 +               goto out_si;
6625 +       br = au_sbr(sb, bindex);
6626 +       xi = br->br_xino;
6627 +       if (unlikely(idx >= xi->xi_nfile))
6628 +               goto out_si;
6629 +       xf = au_xino_file(xi, idx);
6630 +       if (xf)
6631 +               err = dbgaufs_xi_open(xf, file, /*do_fcnt*/1,
6632 +                                     au_xino_count(br));
6633 +
6634 +out_si:
6635 +       si_read_unlock(sb);
6636 +out:
6637 +       AuTraceErr(err);
6638 +       return err;
6639 +}
6640 +
6641 +static const struct file_operations dbgaufs_xino_fop = {
6642 +       .owner          = THIS_MODULE,
6643 +       .open           = dbgaufs_xino_open,
6644 +       .release        = dbgaufs_xi_release,
6645 +       .read           = dbgaufs_xi_read
6646 +};
6647 +
6648 +void dbgaufs_xino_del(struct au_branch *br)
6649 +{
6650 +       struct dentry *dbgaufs;
6651 +
6652 +       dbgaufs = br->br_dbgaufs;
6653 +       if (!dbgaufs)
6654 +               return;
6655 +
6656 +       br->br_dbgaufs = NULL;
6657 +       /* debugfs acquires the parent i_mutex */
6658 +       lockdep_off();
6659 +       debugfs_remove(dbgaufs);
6660 +       lockdep_on();
6661 +}
6662 +
6663 +void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
6664 +{
6665 +       aufs_bindex_t bbot;
6666 +       struct au_branch *br;
6667 +
6668 +       if (!au_sbi(sb)->si_dbgaufs)
6669 +               return;
6670 +
6671 +       bbot = au_sbbot(sb);
6672 +       for (; bindex <= bbot; bindex++) {
6673 +               br = au_sbr(sb, bindex);
6674 +               dbgaufs_xino_del(br);
6675 +       }
6676 +}
6677 +
6678 +static void dbgaufs_br_do_add(struct super_block *sb, aufs_bindex_t bindex,
6679 +                             unsigned int idx, struct dentry *parent,
6680 +                             struct au_sbinfo *sbinfo)
6681 +{
6682 +       struct au_branch *br;
6683 +       struct dentry *d;
6684 +       /* "xi" bindex(5) "-" idx(2) NULL */
6685 +       char name[sizeof(DbgaufsXi_PREFIX) + 8];
6686 +
6687 +       if (!idx)
6688 +               snprintf(name, sizeof(name), DbgaufsXi_PREFIX "%d", bindex);
6689 +       else
6690 +               snprintf(name, sizeof(name), DbgaufsXi_PREFIX "%d-%u",
6691 +                        bindex, idx);
6692 +       br = au_sbr(sb, bindex);
6693 +       if (br->br_dbgaufs) {
6694 +               struct qstr qstr = QSTR_INIT(name, strlen(name));
6695 +
6696 +               if (!au_qstreq(&br->br_dbgaufs->d_name, &qstr)) {
6697 +                       /* debugfs acquires the parent i_mutex */
6698 +                       lockdep_off();
6699 +                       d = debugfs_rename(parent, br->br_dbgaufs, parent,
6700 +                                          name);
6701 +                       lockdep_on();
6702 +                       if (unlikely(!d))
6703 +                               pr_warn("failed renaming %pd/%s, ignored.\n",
6704 +                                       parent, name);
6705 +               }
6706 +       } else {
6707 +               lockdep_off();
6708 +               br->br_dbgaufs = debugfs_create_file(name, dbgaufs_mode, parent,
6709 +                                                    sbinfo, &dbgaufs_xino_fop);
6710 +               lockdep_on();
6711 +               if (unlikely(!br->br_dbgaufs))
6712 +                       pr_warn("failed creating %pd/%s, ignored.\n",
6713 +                               parent, name);
6714 +       }
6715 +}
6716 +
6717 +static void dbgaufs_br_add(struct super_block *sb, aufs_bindex_t bindex,
6718 +                          struct dentry *parent, struct au_sbinfo *sbinfo)
6719 +{
6720 +       struct au_branch *br;
6721 +       struct au_xino *xi;
6722 +       unsigned int u;
6723 +
6724 +       br = au_sbr(sb, bindex);
6725 +       xi = br->br_xino;
6726 +       for (u = 0; u < xi->xi_nfile; u++)
6727 +               dbgaufs_br_do_add(sb, bindex, u, parent, sbinfo);
6728 +}
6729 +
6730 +void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex, int topdown)
6731 +{
6732 +       struct au_sbinfo *sbinfo;
6733 +       struct dentry *parent;
6734 +       aufs_bindex_t bbot;
6735 +
6736 +       if (!au_opt_test(au_mntflags(sb), XINO))
6737 +               return;
6738 +
6739 +       sbinfo = au_sbi(sb);
6740 +       parent = sbinfo->si_dbgaufs;
6741 +       if (!parent)
6742 +               return;
6743 +
6744 +       bbot = au_sbbot(sb);
6745 +       if (topdown)
6746 +               for (; bindex <= bbot; bindex++)
6747 +                       dbgaufs_br_add(sb, bindex, parent, sbinfo);
6748 +       else
6749 +               for (; bbot >= bindex; bbot--)
6750 +                       dbgaufs_br_add(sb, bbot, parent, sbinfo);
6751 +}
6752 +
6753 +/* ---------------------------------------------------------------------- */
6754 +
6755 +#ifdef CONFIG_AUFS_EXPORT
6756 +static int dbgaufs_xigen_open(struct inode *inode, struct file *file)
6757 +{
6758 +       int err;
6759 +       struct au_sbinfo *sbinfo;
6760 +       struct super_block *sb;
6761 +
6762 +       sbinfo = inode->i_private;
6763 +       sb = sbinfo->si_sb;
6764 +       si_noflush_read_lock(sb);
6765 +       err = dbgaufs_xi_open(sbinfo->si_xigen, file, /*do_fcnt*/0, /*cnt*/0);
6766 +       si_read_unlock(sb);
6767 +       return err;
6768 +}
6769 +
6770 +static const struct file_operations dbgaufs_xigen_fop = {
6771 +       .owner          = THIS_MODULE,
6772 +       .open           = dbgaufs_xigen_open,
6773 +       .release        = dbgaufs_xi_release,
6774 +       .read           = dbgaufs_xi_read
6775 +};
6776 +
6777 +static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
6778 +{
6779 +       int err;
6780 +
6781 +       /*
6782 +        * This function is a dynamic '__init' function actually,
6783 +        * so the tiny check for si_rwsem is unnecessary.
6784 +        */
6785 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6786 +
6787 +       err = -EIO;
6788 +       sbinfo->si_dbgaufs_xigen = debugfs_create_file
6789 +               ("xigen", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6790 +                &dbgaufs_xigen_fop);
6791 +       if (sbinfo->si_dbgaufs_xigen)
6792 +               err = 0;
6793 +
6794 +       return err;
6795 +}
6796 +#else
6797 +static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
6798 +{
6799 +       return 0;
6800 +}
6801 +#endif /* CONFIG_AUFS_EXPORT */
6802 +
6803 +/* ---------------------------------------------------------------------- */
6804 +
6805 +void dbgaufs_si_fin(struct au_sbinfo *sbinfo)
6806 +{
6807 +       /*
6808 +        * This function is a dynamic '__fin' function actually,
6809 +        * so the tiny check for si_rwsem is unnecessary.
6810 +        */
6811 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6812 +
6813 +       debugfs_remove_recursive(sbinfo->si_dbgaufs);
6814 +       sbinfo->si_dbgaufs = NULL;
6815 +}
6816 +
6817 +int dbgaufs_si_init(struct au_sbinfo *sbinfo)
6818 +{
6819 +       int err;
6820 +       char name[SysaufsSiNameLen];
6821 +
6822 +       /*
6823 +        * This function is a dynamic '__init' function actually,
6824 +        * so the tiny check for si_rwsem is unnecessary.
6825 +        */
6826 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6827 +
6828 +       err = -ENOENT;
6829 +       if (!dbgaufs) {
6830 +               AuErr1("/debug/aufs is uninitialized\n");
6831 +               goto out;
6832 +       }
6833 +
6834 +       err = -EIO;
6835 +       sysaufs_name(sbinfo, name);
6836 +       sbinfo->si_dbgaufs = debugfs_create_dir(name, dbgaufs);
6837 +       if (unlikely(!sbinfo->si_dbgaufs))
6838 +               goto out;
6839 +
6840 +       /* regardless plink/noplink option */
6841 +       sbinfo->si_dbgaufs_plink = debugfs_create_file
6842 +               ("plink", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6843 +                &dbgaufs_plink_fop);
6844 +       if (unlikely(!sbinfo->si_dbgaufs_plink))
6845 +               goto out_dir;
6846 +
6847 +       /* regardless xino/noxino option */
6848 +       sbinfo->si_dbgaufs_xib = debugfs_create_file
6849 +               ("xib", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6850 +                &dbgaufs_xib_fop);
6851 +       if (unlikely(!sbinfo->si_dbgaufs_xib))
6852 +               goto out_dir;
6853 +
6854 +       err = dbgaufs_xigen_init(sbinfo);
6855 +       if (!err)
6856 +               goto out; /* success */
6857 +
6858 +out_dir:
6859 +       dbgaufs_si_fin(sbinfo);
6860 +out:
6861 +       if (unlikely(err))
6862 +               pr_err("debugfs/aufs failed\n");
6863 +       return err;
6864 +}
6865 +
6866 +/* ---------------------------------------------------------------------- */
6867 +
6868 +void dbgaufs_fin(void)
6869 +{
6870 +       debugfs_remove(dbgaufs);
6871 +}
6872 +
6873 +int __init dbgaufs_init(void)
6874 +{
6875 +       int err;
6876 +
6877 +       err = -EIO;
6878 +       dbgaufs = debugfs_create_dir(AUFS_NAME, NULL);
6879 +       if (dbgaufs)
6880 +               err = 0;
6881 +       return err;
6882 +}
6883 diff -urN /usr/share/empty/fs/aufs/dbgaufs.h linux/fs/aufs/dbgaufs.h
6884 --- /usr/share/empty/fs/aufs/dbgaufs.h  1970-01-01 01:00:00.000000000 +0100
6885 +++ linux/fs/aufs/dbgaufs.h     2021-11-01 23:48:34.203025928 +0100
6886 @@ -0,0 +1,53 @@
6887 +/* SPDX-License-Identifier: GPL-2.0 */
6888 +/*
6889 + * Copyright (C) 2005-2021 Junjiro R. Okajima
6890 + *
6891 + * This program, aufs is free software; you can redistribute it and/or modify
6892 + * it under the terms of the GNU General Public License as published by
6893 + * the Free Software Foundation; either version 2 of the License, or
6894 + * (at your option) any later version.
6895 + *
6896 + * This program is distributed in the hope that it will be useful,
6897 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6898 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6899 + * GNU General Public License for more details.
6900 + *
6901 + * You should have received a copy of the GNU General Public License
6902 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6903 + */
6904 +
6905 +/*
6906 + * debugfs interface
6907 + */
6908 +
6909 +#ifndef __DBGAUFS_H__
6910 +#define __DBGAUFS_H__
6911 +
6912 +#ifdef __KERNEL__
6913 +
6914 +struct super_block;
6915 +struct au_sbinfo;
6916 +struct au_branch;
6917 +
6918 +#ifdef CONFIG_DEBUG_FS
6919 +/* dbgaufs.c */
6920 +void dbgaufs_xino_del(struct au_branch *br);
6921 +void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
6922 +void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex, int topdown);
6923 +void dbgaufs_si_fin(struct au_sbinfo *sbinfo);
6924 +int dbgaufs_si_init(struct au_sbinfo *sbinfo);
6925 +void dbgaufs_fin(void);
6926 +int __init dbgaufs_init(void);
6927 +#else
6928 +AuStubVoid(dbgaufs_xino_del, struct au_branch *br)
6929 +AuStubVoid(dbgaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
6930 +AuStubVoid(dbgaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex,
6931 +          int topdown)
6932 +AuStubVoid(dbgaufs_si_fin, struct au_sbinfo *sbinfo)
6933 +AuStubInt0(dbgaufs_si_init, struct au_sbinfo *sbinfo)
6934 +AuStubVoid(dbgaufs_fin, void)
6935 +AuStubInt0(__init dbgaufs_init, void)
6936 +#endif /* CONFIG_DEBUG_FS */
6937 +
6938 +#endif /* __KERNEL__ */
6939 +#endif /* __DBGAUFS_H__ */
6940 diff -urN /usr/share/empty/fs/aufs/dcsub.c linux/fs/aufs/dcsub.c
6941 --- /usr/share/empty/fs/aufs/dcsub.c    1970-01-01 01:00:00.000000000 +0100
6942 +++ linux/fs/aufs/dcsub.c       2021-11-01 23:48:34.206359262 +0100
6943 @@ -0,0 +1,225 @@
6944 +// SPDX-License-Identifier: GPL-2.0
6945 +/*
6946 + * Copyright (C) 2005-2021 Junjiro R. Okajima
6947 + *
6948 + * This program, aufs is free software; you can redistribute it and/or modify
6949 + * it under the terms of the GNU General Public License as published by
6950 + * the Free Software Foundation; either version 2 of the License, or
6951 + * (at your option) any later version.
6952 + *
6953 + * This program is distributed in the hope that it will be useful,
6954 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6955 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6956 + * GNU General Public License for more details.
6957 + *
6958 + * You should have received a copy of the GNU General Public License
6959 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6960 + */
6961 +
6962 +/*
6963 + * sub-routines for dentry cache
6964 + */
6965 +
6966 +#include "aufs.h"
6967 +
6968 +static void au_dpage_free(struct au_dpage *dpage)
6969 +{
6970 +       int i;
6971 +       struct dentry **p;
6972 +
6973 +       p = dpage->dentries;
6974 +       for (i = 0; i < dpage->ndentry; i++)
6975 +               dput(*p++);
6976 +       free_page((unsigned long)dpage->dentries);
6977 +}
6978 +
6979 +int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp)
6980 +{
6981 +       int err;
6982 +       void *p;
6983 +
6984 +       err = -ENOMEM;
6985 +       dpages->dpages = kmalloc(sizeof(*dpages->dpages), gfp);
6986 +       if (unlikely(!dpages->dpages))
6987 +               goto out;
6988 +
6989 +       p = (void *)__get_free_page(gfp);
6990 +       if (unlikely(!p))
6991 +               goto out_dpages;
6992 +
6993 +       dpages->dpages[0].ndentry = 0;
6994 +       dpages->dpages[0].dentries = p;
6995 +       dpages->ndpage = 1;
6996 +       return 0; /* success */
6997 +
6998 +out_dpages:
6999 +       au_kfree_try_rcu(dpages->dpages);
7000 +out:
7001 +       return err;
7002 +}
7003 +
7004 +void au_dpages_free(struct au_dcsub_pages *dpages)
7005 +{
7006 +       int i;
7007 +       struct au_dpage *p;
7008 +
7009 +       p = dpages->dpages;
7010 +       for (i = 0; i < dpages->ndpage; i++)
7011 +               au_dpage_free(p++);
7012 +       au_kfree_try_rcu(dpages->dpages);
7013 +}
7014 +
7015 +static int au_dpages_append(struct au_dcsub_pages *dpages,
7016 +                           struct dentry *dentry, gfp_t gfp)
7017 +{
7018 +       int err, sz;
7019 +       struct au_dpage *dpage;
7020 +       void *p;
7021 +
7022 +       dpage = dpages->dpages + dpages->ndpage - 1;
7023 +       sz = PAGE_SIZE / sizeof(dentry);
7024 +       if (unlikely(dpage->ndentry >= sz)) {
7025 +               AuLabel(new dpage);
7026 +               err = -ENOMEM;
7027 +               sz = dpages->ndpage * sizeof(*dpages->dpages);
7028 +               p = au_kzrealloc(dpages->dpages, sz,
7029 +                                sz + sizeof(*dpages->dpages), gfp,
7030 +                                /*may_shrink*/0);
7031 +               if (unlikely(!p))
7032 +                       goto out;
7033 +
7034 +               dpages->dpages = p;
7035 +               dpage = dpages->dpages + dpages->ndpage;
7036 +               p = (void *)__get_free_page(gfp);
7037 +               if (unlikely(!p))
7038 +                       goto out;
7039 +
7040 +               dpage->ndentry = 0;
7041 +               dpage->dentries = p;
7042 +               dpages->ndpage++;
7043 +       }
7044 +
7045 +       AuDebugOn(au_dcount(dentry) <= 0);
7046 +       dpage->dentries[dpage->ndentry++] = dget_dlock(dentry);
7047 +       return 0; /* success */
7048 +
7049 +out:
7050 +       return err;
7051 +}
7052 +
7053 +/* todo: BAD approach */
7054 +/* copied from linux/fs/dcache.c */
7055 +enum d_walk_ret {
7056 +       D_WALK_CONTINUE,
7057 +       D_WALK_QUIT,
7058 +       D_WALK_NORETRY,
7059 +       D_WALK_SKIP,
7060 +};
7061 +
7062 +extern void d_walk(struct dentry *parent, void *data,
7063 +                  enum d_walk_ret (*enter)(void *, struct dentry *));
7064 +
7065 +struct ac_dpages_arg {
7066 +       int err;
7067 +       struct au_dcsub_pages *dpages;
7068 +       struct super_block *sb;
7069 +       au_dpages_test test;
7070 +       void *arg;
7071 +};
7072 +
7073 +static enum d_walk_ret au_call_dpages_append(void *_arg, struct dentry *dentry)
7074 +{
7075 +       enum d_walk_ret ret;
7076 +       struct ac_dpages_arg *arg = _arg;
7077 +
7078 +       ret = D_WALK_CONTINUE;
7079 +       if (dentry->d_sb == arg->sb
7080 +           && !IS_ROOT(dentry)
7081 +           && au_dcount(dentry) > 0
7082 +           && au_di(dentry)
7083 +           && (!arg->test || arg->test(dentry, arg->arg))) {
7084 +               arg->err = au_dpages_append(arg->dpages, dentry, GFP_ATOMIC);
7085 +               if (unlikely(arg->err))
7086 +                       ret = D_WALK_QUIT;
7087 +       }
7088 +
7089 +       return ret;
7090 +}
7091 +
7092 +int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
7093 +                  au_dpages_test test, void *arg)
7094 +{
7095 +       struct ac_dpages_arg args = {
7096 +               .err    = 0,
7097 +               .dpages = dpages,
7098 +               .sb     = root->d_sb,
7099 +               .test   = test,
7100 +               .arg    = arg
7101 +       };
7102 +
7103 +       d_walk(root, &args, au_call_dpages_append);
7104 +
7105 +       return args.err;
7106 +}
7107 +
7108 +int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
7109 +                      int do_include, au_dpages_test test, void *arg)
7110 +{
7111 +       int err;
7112 +
7113 +       err = 0;
7114 +       write_seqlock(&rename_lock);
7115 +       spin_lock(&dentry->d_lock);
7116 +       if (do_include
7117 +           && au_dcount(dentry) > 0
7118 +           && (!test || test(dentry, arg)))
7119 +               err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
7120 +       spin_unlock(&dentry->d_lock);
7121 +       if (unlikely(err))
7122 +               goto out;
7123 +
7124 +       /*
7125 +        * RCU for vfsmount is unnecessary since this is a traverse in a single
7126 +        * mount
7127 +        */
7128 +       while (!IS_ROOT(dentry)) {
7129 +               dentry = dentry->d_parent; /* rename_lock is locked */
7130 +               spin_lock(&dentry->d_lock);
7131 +               if (au_dcount(dentry) > 0
7132 +                   && (!test || test(dentry, arg)))
7133 +                       err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
7134 +               spin_unlock(&dentry->d_lock);
7135 +               if (unlikely(err))
7136 +                       break;
7137 +       }
7138 +
7139 +out:
7140 +       write_sequnlock(&rename_lock);
7141 +       return err;
7142 +}
7143 +
7144 +static inline int au_dcsub_dpages_aufs(struct dentry *dentry, void *arg)
7145 +{
7146 +       return au_di(dentry) && dentry->d_sb == arg;
7147 +}
7148 +
7149 +int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
7150 +                           struct dentry *dentry, int do_include)
7151 +{
7152 +       return au_dcsub_pages_rev(dpages, dentry, do_include,
7153 +                                 au_dcsub_dpages_aufs, dentry->d_sb);
7154 +}
7155 +
7156 +int au_test_subdir(struct dentry *d1, struct dentry *d2)
7157 +{
7158 +       struct path path[2] = {
7159 +               {
7160 +                       .dentry = d1
7161 +               },
7162 +               {
7163 +                       .dentry = d2
7164 +               }
7165 +       };
7166 +
7167 +       return path_is_under(path + 0, path + 1);
7168 +}
7169 diff -urN /usr/share/empty/fs/aufs/dcsub.h linux/fs/aufs/dcsub.h
7170 --- /usr/share/empty/fs/aufs/dcsub.h    1970-01-01 01:00:00.000000000 +0100
7171 +++ linux/fs/aufs/dcsub.h       2021-11-01 23:48:34.206359262 +0100
7172 @@ -0,0 +1,137 @@
7173 +/* SPDX-License-Identifier: GPL-2.0 */
7174 +/*
7175 + * Copyright (C) 2005-2021 Junjiro R. Okajima
7176 + *
7177 + * This program, aufs is free software; you can redistribute it and/or modify
7178 + * it under the terms of the GNU General Public License as published by
7179 + * the Free Software Foundation; either version 2 of the License, or
7180 + * (at your option) any later version.
7181 + *
7182 + * This program is distributed in the hope that it will be useful,
7183 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7184 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7185 + * GNU General Public License for more details.
7186 + *
7187 + * You should have received a copy of the GNU General Public License
7188 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
7189 + */
7190 +
7191 +/*
7192 + * sub-routines for dentry cache
7193 + */
7194 +
7195 +#ifndef __AUFS_DCSUB_H__
7196 +#define __AUFS_DCSUB_H__
7197 +
7198 +#ifdef __KERNEL__
7199 +
7200 +#include <linux/dcache.h>
7201 +#include <linux/fs.h>
7202 +
7203 +struct au_dpage {
7204 +       int ndentry;
7205 +       struct dentry **dentries;
7206 +};
7207 +
7208 +struct au_dcsub_pages {
7209 +       int ndpage;
7210 +       struct au_dpage *dpages;
7211 +};
7212 +
7213 +/* ---------------------------------------------------------------------- */
7214 +
7215 +/* dcsub.c */
7216 +int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp);
7217 +void au_dpages_free(struct au_dcsub_pages *dpages);
7218 +typedef int (*au_dpages_test)(struct dentry *dentry, void *arg);
7219 +int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
7220 +                  au_dpages_test test, void *arg);
7221 +int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
7222 +                      int do_include, au_dpages_test test, void *arg);
7223 +int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
7224 +                           struct dentry *dentry, int do_include);
7225 +int au_test_subdir(struct dentry *d1, struct dentry *d2);
7226 +
7227 +/* ---------------------------------------------------------------------- */
7228 +
7229 +/*
7230 + * todo: in linux-3.13, several similar (but faster) helpers are added to
7231 + * include/linux/dcache.h. Try them (in the future).
7232 + */
7233 +
7234 +static inline int au_d_hashed_positive(struct dentry *d)
7235 +{
7236 +       int err;
7237 +       struct inode *inode = d_inode(d);
7238 +
7239 +       err = 0;
7240 +       if (unlikely(d_unhashed(d)
7241 +                    || d_is_negative(d)
7242 +                    || !inode->i_nlink))
7243 +               err = -ENOENT;
7244 +       return err;
7245 +}
7246 +
7247 +static inline int au_d_linkable(struct dentry *d)
7248 +{
7249 +       int err;
7250 +       struct inode *inode = d_inode(d);
7251 +
7252 +       err = au_d_hashed_positive(d);
7253 +       if (err
7254 +           && d_is_positive(d)
7255 +           && (inode->i_state & I_LINKABLE))
7256 +               err = 0;
7257 +       return err;
7258 +}
7259 +
7260 +static inline int au_d_alive(struct dentry *d)
7261 +{
7262 +       int err;
7263 +       struct inode *inode;
7264 +
7265 +       err = 0;
7266 +       if (!IS_ROOT(d))
7267 +               err = au_d_hashed_positive(d);
7268 +       else {
7269 +               inode = d_inode(d);
7270 +               if (unlikely(d_unlinked(d)
7271 +                            || d_is_negative(d)
7272 +                            || !inode->i_nlink))
7273 +                       err = -ENOENT;
7274 +       }
7275 +       return err;
7276 +}
7277 +
7278 +static inline int au_alive_dir(struct dentry *d)
7279 +{
7280 +       int err;
7281 +
7282 +       err = au_d_alive(d);
7283 +       if (unlikely(err || IS_DEADDIR(d_inode(d))))
7284 +               err = -ENOENT;
7285 +       return err;
7286 +}
7287 +
7288 +static inline int au_qstreq(struct qstr *a, struct qstr *b)
7289 +{
7290 +       return a->len == b->len
7291 +               && !memcmp(a->name, b->name, a->len);
7292 +}
7293 +
7294 +/*
7295 + * by the commit
7296 + * 360f547 2015-01-25 dcache: let the dentry count go down to zero without
7297 + *                     taking d_lock
7298 + * the type of d_lockref.count became int, but the inlined function d_count()
7299 + * still returns unsigned int.
7300 + * I don't know why. Maybe it is for every d_count() users?
7301 + * Anyway au_dcount() lives on.
7302 + */
7303 +static inline int au_dcount(struct dentry *d)
7304 +{
7305 +       return (int)d_count(d);
7306 +}
7307 +
7308 +#endif /* __KERNEL__ */
7309 +#endif /* __AUFS_DCSUB_H__ */
7310 diff -urN /usr/share/empty/fs/aufs/debug.c linux/fs/aufs/debug.c
7311 --- /usr/share/empty/fs/aufs/debug.c    1970-01-01 01:00:00.000000000 +0100
7312 +++ linux/fs/aufs/debug.c       2021-11-01 23:48:34.206359262 +0100
7313 @@ -0,0 +1,444 @@
7314 +// SPDX-License-Identifier: GPL-2.0
7315 +/*
7316 + * Copyright (C) 2005-2021 Junjiro R. Okajima
7317 + *
7318 + * This program, aufs is free software; you can redistribute it and/or modify
7319 + * it under the terms of the GNU General Public License as published by
7320 + * the Free Software Foundation; either version 2 of the License, or
7321 + * (at your option) any later version.
7322 + *
7323 + * This program is distributed in the hope that it will be useful,
7324 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7325 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7326 + * GNU General Public License for more details.
7327 + *
7328 + * You should have received a copy of the GNU General Public License
7329 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
7330 + */
7331 +
7332 +/*
7333 + * debug print functions
7334 + */
7335 +
7336 +#include <linux/iversion.h>
7337 +#include "aufs.h"
7338 +
7339 +/* Returns 0, or -errno.  arg is in kp->arg. */
7340 +static int param_atomic_t_set(const char *val, const struct kernel_param *kp)
7341 +{
7342 +       int err, n;
7343 +
7344 +       err = kstrtoint(val, 0, &n);
7345 +       if (!err) {
7346 +               if (n > 0)
7347 +                       au_debug_on();
7348 +               else
7349 +                       au_debug_off();
7350 +       }
7351 +       return err;
7352 +}
7353 +
7354 +/* Returns length written or -errno.  Buffer is 4k (ie. be short!) */
7355 +static int param_atomic_t_get(char *buffer, const struct kernel_param *kp)
7356 +{
7357 +       atomic_t *a;
7358 +
7359 +       a = kp->arg;
7360 +       return sprintf(buffer, "%d", atomic_read(a));
7361 +}
7362 +
7363 +static struct kernel_param_ops param_ops_atomic_t = {
7364 +       .set = param_atomic_t_set,
7365 +       .get = param_atomic_t_get
7366 +       /* void (*free)(void *arg) */
7367 +};
7368 +
7369 +atomic_t aufs_debug = ATOMIC_INIT(0);
7370 +MODULE_PARM_DESC(debug, "debug print");
7371 +module_param_named(debug, aufs_debug, atomic_t, 0664);
7372 +
7373 +DEFINE_MUTEX(au_dbg_mtx);      /* just to serialize the dbg msgs */
7374 +char *au_plevel = KERN_DEBUG;
7375 +#define dpri(fmt, ...) do {                                    \
7376 +       if ((au_plevel                                          \
7377 +            && strcmp(au_plevel, KERN_DEBUG))                  \
7378 +           || au_debug_test())                                 \
7379 +               printk("%s" fmt, au_plevel, ##__VA_ARGS__);     \
7380 +} while (0)
7381 +
7382 +/* ---------------------------------------------------------------------- */
7383 +
7384 +void au_dpri_whlist(struct au_nhash *whlist)
7385 +{
7386 +       unsigned long ul, n;
7387 +       struct hlist_head *head;
7388 +       struct au_vdir_wh *pos;
7389 +
7390 +       n = whlist->nh_num;
7391 +       head = whlist->nh_head;
7392 +       for (ul = 0; ul < n; ul++) {
7393 +               hlist_for_each_entry(pos, head, wh_hash)
7394 +                       dpri("b%d, %.*s, %d\n",
7395 +                            pos->wh_bindex,
7396 +                            pos->wh_str.len, pos->wh_str.name,
7397 +                            pos->wh_str.len);
7398 +               head++;
7399 +       }
7400 +}
7401 +
7402 +void au_dpri_vdir(struct au_vdir *vdir)
7403 +{
7404 +       unsigned long ul;
7405 +       union au_vdir_deblk_p p;
7406 +       unsigned char *o;
7407 +
7408 +       if (!vdir || IS_ERR(vdir)) {
7409 +               dpri("err %ld\n", PTR_ERR(vdir));
7410 +               return;
7411 +       }
7412 +
7413 +       dpri("deblk %u, nblk %lu, deblk %p, last{%lu, %p}, ver %llu\n",
7414 +            vdir->vd_deblk_sz, vdir->vd_nblk, vdir->vd_deblk,
7415 +            vdir->vd_last.ul, vdir->vd_last.p.deblk, vdir->vd_version);
7416 +       for (ul = 0; ul < vdir->vd_nblk; ul++) {
7417 +               p.deblk = vdir->vd_deblk[ul];
7418 +               o = p.deblk;
7419 +               dpri("[%lu]: %p\n", ul, o);
7420 +       }
7421 +}
7422 +
7423 +static int do_pri_inode(aufs_bindex_t bindex, struct inode *inode, int hn,
7424 +                       struct dentry *wh)
7425 +{
7426 +       char *n = NULL;
7427 +       int l = 0;
7428 +
7429 +       if (!inode || IS_ERR(inode)) {
7430 +               dpri("i%d: err %ld\n", bindex, PTR_ERR(inode));
7431 +               return -1;
7432 +       }
7433 +
7434 +       /* the type of i_blocks depends upon CONFIG_LBDAF */
7435 +       BUILD_BUG_ON(sizeof(inode->i_blocks) != sizeof(unsigned long)
7436 +                    && sizeof(inode->i_blocks) != sizeof(u64));
7437 +       if (wh) {
7438 +               n = (void *)wh->d_name.name;
7439 +               l = wh->d_name.len;
7440 +       }
7441 +
7442 +       dpri("i%d: %p, i%lu, %s, cnt %d, nl %u, 0%o, sz %llu, blk %llu,"
7443 +            " hn %d, ct %lld, np %lu, st 0x%lx, f 0x%x, v %llu, g %x%s%.*s\n",
7444 +            bindex, inode,
7445 +            inode->i_ino, inode->i_sb ? au_sbtype(inode->i_sb) : "??",
7446 +            atomic_read(&inode->i_count), inode->i_nlink, inode->i_mode,
7447 +            i_size_read(inode), (unsigned long long)inode->i_blocks,
7448 +            hn, (long long)timespec64_to_ns(&inode->i_ctime) & 0x0ffff,
7449 +            inode->i_mapping ? inode->i_mapping->nrpages : 0,
7450 +            inode->i_state, inode->i_flags, inode_peek_iversion(inode),
7451 +            inode->i_generation,
7452 +            l ? ", wh " : "", l, n);
7453 +       return 0;
7454 +}
7455 +
7456 +void au_dpri_inode(struct inode *inode)
7457 +{
7458 +       struct au_iinfo *iinfo;
7459 +       struct au_hinode *hi;
7460 +       aufs_bindex_t bindex;
7461 +       int err, hn;
7462 +
7463 +       err = do_pri_inode(-1, inode, -1, NULL);
7464 +       if (err || !au_test_aufs(inode->i_sb) || au_is_bad_inode(inode))
7465 +               return;
7466 +
7467 +       iinfo = au_ii(inode);
7468 +       dpri("i-1: btop %d, bbot %d, gen %d\n",
7469 +            iinfo->ii_btop, iinfo->ii_bbot, au_iigen(inode, NULL));
7470 +       if (iinfo->ii_btop < 0)
7471 +               return;
7472 +       hn = 0;
7473 +       for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot; bindex++) {
7474 +               hi = au_hinode(iinfo, bindex);
7475 +               hn = !!au_hn(hi);
7476 +               do_pri_inode(bindex, hi->hi_inode, hn, hi->hi_whdentry);
7477 +       }
7478 +}
7479 +
7480 +void au_dpri_dalias(struct inode *inode)
7481 +{
7482 +       struct dentry *d;
7483 +
7484 +       spin_lock(&inode->i_lock);
7485 +       hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias)
7486 +               au_dpri_dentry(d);
7487 +       spin_unlock(&inode->i_lock);
7488 +}
7489 +
7490 +static int do_pri_dentry(aufs_bindex_t bindex, struct dentry *dentry)
7491 +{
7492 +       struct dentry *wh = NULL;
7493 +       int hn;
7494 +       struct inode *inode;
7495 +       struct au_iinfo *iinfo;
7496 +       struct au_hinode *hi;
7497 +
7498 +       if (!dentry || IS_ERR(dentry)) {
7499 +               dpri("d%d: err %ld\n", bindex, PTR_ERR(dentry));
7500 +               return -1;
7501 +       }
7502 +       /* do not call dget_parent() here */
7503 +       /* note: access d_xxx without d_lock */
7504 +       dpri("d%d: %p, %pd2?, %s, cnt %d, flags 0x%x, %shashed\n",
7505 +            bindex, dentry, dentry,
7506 +            dentry->d_sb ? au_sbtype(dentry->d_sb) : "??",
7507 +            au_dcount(dentry), dentry->d_flags,
7508 +            d_unhashed(dentry) ? "un" : "");
7509 +       hn = -1;
7510 +       inode = NULL;
7511 +       if (d_is_positive(dentry))
7512 +               inode = d_inode(dentry);
7513 +       if (inode
7514 +           && au_test_aufs(dentry->d_sb)
7515 +           && bindex >= 0
7516 +           && !au_is_bad_inode(inode)) {
7517 +               iinfo = au_ii(inode);
7518 +               hi = au_hinode(iinfo, bindex);
7519 +               hn = !!au_hn(hi);
7520 +               wh = hi->hi_whdentry;
7521 +       }
7522 +       do_pri_inode(bindex, inode, hn, wh);
7523 +       return 0;
7524 +}
7525 +
7526 +void au_dpri_dentry(struct dentry *dentry)
7527 +{
7528 +       struct au_dinfo *dinfo;
7529 +       aufs_bindex_t bindex;
7530 +       int err;
7531 +
7532 +       err = do_pri_dentry(-1, dentry);
7533 +       if (err || !au_test_aufs(dentry->d_sb))
7534 +               return;
7535 +
7536 +       dinfo = au_di(dentry);
7537 +       if (!dinfo)
7538 +               return;
7539 +       dpri("d-1: btop %d, bbot %d, bwh %d, bdiropq %d, gen %d, tmp %d\n",
7540 +            dinfo->di_btop, dinfo->di_bbot,
7541 +            dinfo->di_bwh, dinfo->di_bdiropq, au_digen(dentry),
7542 +            dinfo->di_tmpfile);
7543 +       if (dinfo->di_btop < 0)
7544 +               return;
7545 +       for (bindex = dinfo->di_btop; bindex <= dinfo->di_bbot; bindex++)
7546 +               do_pri_dentry(bindex, au_hdentry(dinfo, bindex)->hd_dentry);
7547 +}
7548 +
7549 +static int do_pri_file(aufs_bindex_t bindex, struct file *file)
7550 +{
7551 +       char a[32];
7552 +
7553 +       if (!file || IS_ERR(file)) {
7554 +               dpri("f%d: err %ld\n", bindex, PTR_ERR(file));
7555 +               return -1;
7556 +       }
7557 +       a[0] = 0;
7558 +       if (bindex < 0
7559 +           && !IS_ERR_OR_NULL(file->f_path.dentry)
7560 +           && au_test_aufs(file->f_path.dentry->d_sb)
7561 +           && au_fi(file))
7562 +               snprintf(a, sizeof(a), ", gen %d, mmapped %d",
7563 +                        au_figen(file), atomic_read(&au_fi(file)->fi_mmapped));
7564 +       dpri("f%d: mode 0x%x, flags 0%o, cnt %ld, v %llu, pos %llu%s\n",
7565 +            bindex, file->f_mode, file->f_flags, (long)file_count(file),
7566 +            file->f_version, file->f_pos, a);
7567 +       if (!IS_ERR_OR_NULL(file->f_path.dentry))
7568 +               do_pri_dentry(bindex, file->f_path.dentry);
7569 +       return 0;
7570 +}
7571 +
7572 +void au_dpri_file(struct file *file)
7573 +{
7574 +       struct au_finfo *finfo;
7575 +       struct au_fidir *fidir;
7576 +       struct au_hfile *hfile;
7577 +       aufs_bindex_t bindex;
7578 +       int err;
7579 +
7580 +       err = do_pri_file(-1, file);
7581 +       if (err
7582 +           || IS_ERR_OR_NULL(file->f_path.dentry)
7583 +           || !au_test_aufs(file->f_path.dentry->d_sb))
7584 +               return;
7585 +
7586 +       finfo = au_fi(file);
7587 +       if (!finfo)
7588 +               return;
7589 +       if (finfo->fi_btop < 0)
7590 +               return;
7591 +       fidir = finfo->fi_hdir;
7592 +       if (!fidir)
7593 +               do_pri_file(finfo->fi_btop, finfo->fi_htop.hf_file);
7594 +       else
7595 +               for (bindex = finfo->fi_btop;
7596 +                    bindex >= 0 && bindex <= fidir->fd_bbot;
7597 +                    bindex++) {
7598 +                       hfile = fidir->fd_hfile + bindex;
7599 +                       do_pri_file(bindex, hfile ? hfile->hf_file : NULL);
7600 +               }
7601 +}
7602 +
7603 +static int do_pri_br(aufs_bindex_t bindex, struct au_branch *br)
7604 +{
7605 +       struct vfsmount *mnt;
7606 +       struct super_block *sb;
7607 +
7608 +       if (!br || IS_ERR(br))
7609 +               goto out;
7610 +       mnt = au_br_mnt(br);
7611 +       if (!mnt || IS_ERR(mnt))
7612 +               goto out;
7613 +       sb = mnt->mnt_sb;
7614 +       if (!sb || IS_ERR(sb))
7615 +               goto out;
7616 +
7617 +       dpri("s%d: {perm 0x%x, id %d, wbr %p}, "
7618 +            "%s, dev 0x%02x%02x, flags 0x%lx, cnt %d, active %d, "
7619 +            "xino %d\n",
7620 +            bindex, br->br_perm, br->br_id, br->br_wbr,
7621 +            au_sbtype(sb), MAJOR(sb->s_dev), MINOR(sb->s_dev),
7622 +            sb->s_flags, sb->s_count,
7623 +            atomic_read(&sb->s_active),
7624 +            !!au_xino_file(br->br_xino, /*idx*/-1));
7625 +       return 0;
7626 +
7627 +out:
7628 +       dpri("s%d: err %ld\n", bindex, PTR_ERR(br));
7629 +       return -1;
7630 +}
7631 +
7632 +void au_dpri_sb(struct super_block *sb)
7633 +{
7634 +       struct au_sbinfo *sbinfo;
7635 +       aufs_bindex_t bindex;
7636 +       int err;
7637 +       /* to reduce stack size */
7638 +       struct {
7639 +               struct vfsmount mnt;
7640 +               struct au_branch fake;
7641 +       } *a;
7642 +
7643 +       /* this function can be called from magic sysrq */
7644 +       a = kzalloc(sizeof(*a), GFP_ATOMIC);
7645 +       if (unlikely(!a)) {
7646 +               dpri("no memory\n");
7647 +               return;
7648 +       }
7649 +
7650 +       a->mnt.mnt_sb = sb;
7651 +       a->fake.br_path.mnt = &a->mnt;
7652 +       err = do_pri_br(-1, &a->fake);
7653 +       au_kfree_rcu(a);
7654 +       dpri("dev 0x%x\n", sb->s_dev);
7655 +       if (err || !au_test_aufs(sb))
7656 +               return;
7657 +
7658 +       sbinfo = au_sbi(sb);
7659 +       if (!sbinfo)
7660 +               return;
7661 +       dpri("nw %d, gen %u, kobj %d\n",
7662 +            atomic_read(&sbinfo->si_nowait.nw_len), sbinfo->si_generation,
7663 +            kref_read(&sbinfo->si_kobj.kref));
7664 +       for (bindex = 0; bindex <= sbinfo->si_bbot; bindex++)
7665 +               do_pri_br(bindex, sbinfo->si_branch[0 + bindex]);
7666 +}
7667 +
7668 +/* ---------------------------------------------------------------------- */
7669 +
7670 +void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line)
7671 +{
7672 +       struct inode *h_inode, *inode = d_inode(dentry);
7673 +       struct dentry *h_dentry;
7674 +       aufs_bindex_t bindex, bbot, bi;
7675 +
7676 +       if (!inode /* || au_di(dentry)->di_lsc == AuLsc_DI_TMP */)
7677 +               return;
7678 +
7679 +       bbot = au_dbbot(dentry);
7680 +       bi = au_ibbot(inode);
7681 +       if (bi < bbot)
7682 +               bbot = bi;
7683 +       bindex = au_dbtop(dentry);
7684 +       bi = au_ibtop(inode);
7685 +       if (bi > bindex)
7686 +               bindex = bi;
7687 +
7688 +       for (; bindex <= bbot; bindex++) {
7689 +               h_dentry = au_h_dptr(dentry, bindex);
7690 +               if (!h_dentry)
7691 +                       continue;
7692 +               h_inode = au_h_iptr(inode, bindex);
7693 +               if (unlikely(h_inode != d_inode(h_dentry))) {
7694 +                       au_debug_on();
7695 +                       AuDbg("b%d, %s:%d\n", bindex, func, line);
7696 +                       AuDbgDentry(dentry);
7697 +                       AuDbgInode(inode);
7698 +                       au_debug_off();
7699 +                       if (au_test_fuse(h_inode->i_sb))
7700 +                               WARN_ON_ONCE(1);
7701 +                       else
7702 +                               BUG();
7703 +               }
7704 +       }
7705 +}
7706 +
7707 +void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen)
7708 +{
7709 +       int err, i, j;
7710 +       struct au_dcsub_pages dpages;
7711 +       struct au_dpage *dpage;
7712 +       struct dentry **dentries;
7713 +
7714 +       err = au_dpages_init(&dpages, GFP_NOFS);
7715 +       AuDebugOn(err);
7716 +       err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/1);
7717 +       AuDebugOn(err);
7718 +       for (i = dpages.ndpage - 1; !err && i >= 0; i--) {
7719 +               dpage = dpages.dpages + i;
7720 +               dentries = dpage->dentries;
7721 +               for (j = dpage->ndentry - 1; !err && j >= 0; j--)
7722 +                       AuDebugOn(au_digen_test(dentries[j], sigen));
7723 +       }
7724 +       au_dpages_free(&dpages);
7725 +}
7726 +
7727 +void au_dbg_verify_kthread(void)
7728 +{
7729 +       if (au_wkq_test()) {
7730 +               au_dbg_blocked();
7731 +               /*
7732 +                * It may be recursive, but udba=notify between two aufs mounts,
7733 +                * where a single ro branch is shared, is not a problem.
7734 +                */
7735 +               /* WARN_ON(1); */
7736 +       }
7737 +}
7738 +
7739 +/* ---------------------------------------------------------------------- */
7740 +
7741 +int __init au_debug_init(void)
7742 +{
7743 +       aufs_bindex_t bindex;
7744 +       struct au_vdir_destr destr;
7745 +
7746 +       bindex = -1;
7747 +       AuDebugOn(bindex >= 0);
7748 +
7749 +       destr.len = -1;
7750 +       AuDebugOn(destr.len < NAME_MAX);
7751 +
7752 +#ifdef CONFIG_4KSTACKS
7753 +       pr_warn("CONFIG_4KSTACKS is defined.\n");
7754 +#endif
7755 +
7756 +       return 0;
7757 +}
7758 diff -urN /usr/share/empty/fs/aufs/debug.h linux/fs/aufs/debug.h
7759 --- /usr/share/empty/fs/aufs/debug.h    1970-01-01 01:00:00.000000000 +0100
7760 +++ linux/fs/aufs/debug.h       2021-11-01 23:48:34.206359262 +0100
7761 @@ -0,0 +1,226 @@
7762 +/* SPDX-License-Identifier: GPL-2.0 */
7763 +/*
7764 + * Copyright (C) 2005-2021 Junjiro R. Okajima
7765 + *
7766 + * This program, aufs is free software; you can redistribute it and/or modify
7767 + * it under the terms of the GNU General Public License as published by
7768 + * the Free Software Foundation; either version 2 of the License, or
7769 + * (at your option) any later version.
7770 + *
7771 + * This program is distributed in the hope that it will be useful,
7772 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7773 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7774 + * GNU General Public License for more details.
7775 + *
7776 + * You should have received a copy of the GNU General Public License
7777 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
7778 + */
7779 +
7780 +/*
7781 + * debug print functions
7782 + */
7783 +
7784 +#ifndef __AUFS_DEBUG_H__
7785 +#define __AUFS_DEBUG_H__
7786 +
7787 +#ifdef __KERNEL__
7788 +
7789 +#include <linux/atomic.h>
7790 +#include <linux/module.h>
7791 +#include <linux/kallsyms.h>
7792 +#include <linux/sysrq.h>
7793 +
7794 +#ifdef CONFIG_AUFS_DEBUG
7795 +#define AuDebugOn(a)           BUG_ON(a)
7796 +
7797 +/* module parameter */
7798 +extern atomic_t aufs_debug;
7799 +static inline void au_debug_on(void)
7800 +{
7801 +       atomic_inc(&aufs_debug);
7802 +}
7803 +static inline void au_debug_off(void)
7804 +{
7805 +       atomic_dec_if_positive(&aufs_debug);
7806 +}
7807 +
7808 +static inline int au_debug_test(void)
7809 +{
7810 +       return atomic_read(&aufs_debug) > 0;
7811 +}
7812 +#else
7813 +#define AuDebugOn(a)           do {} while (0)
7814 +AuStubVoid(au_debug_on, void)
7815 +AuStubVoid(au_debug_off, void)
7816 +AuStubInt0(au_debug_test, void)
7817 +#endif /* CONFIG_AUFS_DEBUG */
7818 +
7819 +#define param_check_atomic_t(name, p) __param_check(name, p, atomic_t)
7820 +
7821 +/* ---------------------------------------------------------------------- */
7822 +
7823 +/* debug print */
7824 +
7825 +#define AuDbg(fmt, ...) do { \
7826 +       if (au_debug_test()) \
7827 +               pr_debug("DEBUG: " fmt, ##__VA_ARGS__); \
7828 +} while (0)
7829 +#define AuLabel(l)             AuDbg(#l "\n")
7830 +#define AuIOErr(fmt, ...)      pr_err("I/O Error, " fmt, ##__VA_ARGS__)
7831 +#define AuWarn1(fmt, ...) do { \
7832 +       static unsigned char _c; \
7833 +       if (!_c++) \
7834 +               pr_warn(fmt, ##__VA_ARGS__); \
7835 +} while (0)
7836 +
7837 +#define AuErr1(fmt, ...) do { \
7838 +       static unsigned char _c; \
7839 +       if (!_c++) \
7840 +               pr_err(fmt, ##__VA_ARGS__); \
7841 +} while (0)
7842 +
7843 +#define AuIOErr1(fmt, ...) do { \
7844 +       static unsigned char _c; \
7845 +       if (!_c++) \
7846 +               AuIOErr(fmt, ##__VA_ARGS__); \
7847 +} while (0)
7848 +
7849 +#define AuUnsupportMsg "This operation is not supported." \
7850 +                       " Please report this application to aufs-users ML."
7851 +#define AuUnsupport(fmt, ...) do { \
7852 +       pr_err(AuUnsupportMsg "\n" fmt, ##__VA_ARGS__); \
7853 +       dump_stack(); \
7854 +} while (0)
7855 +
7856 +#define AuTraceErr(e) do { \
7857 +       if (unlikely((e) < 0)) \
7858 +               AuDbg("err %d\n", (int)(e)); \
7859 +} while (0)
7860 +
7861 +#define AuTraceErrPtr(p) do { \
7862 +       if (IS_ERR(p)) \
7863 +               AuDbg("err %ld\n", PTR_ERR(p)); \
7864 +} while (0)
7865 +
7866 +/* dirty macros for debug print, use with "%.*s" and caution */
7867 +#define AuLNPair(qstr)         (qstr)->len, (qstr)->name
7868 +
7869 +/* ---------------------------------------------------------------------- */
7870 +
7871 +struct dentry;
7872 +#ifdef CONFIG_AUFS_DEBUG
7873 +extern struct mutex au_dbg_mtx;
7874 +extern char *au_plevel;
7875 +struct au_nhash;
7876 +void au_dpri_whlist(struct au_nhash *whlist);
7877 +struct au_vdir;
7878 +void au_dpri_vdir(struct au_vdir *vdir);
7879 +struct inode;
7880 +void au_dpri_inode(struct inode *inode);
7881 +void au_dpri_dalias(struct inode *inode);
7882 +void au_dpri_dentry(struct dentry *dentry);
7883 +struct file;
7884 +void au_dpri_file(struct file *filp);
7885 +struct super_block;
7886 +void au_dpri_sb(struct super_block *sb);
7887 +
7888 +#define au_dbg_verify_dinode(d) __au_dbg_verify_dinode(d, __func__, __LINE__)
7889 +void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line);
7890 +void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen);
7891 +void au_dbg_verify_kthread(void);
7892 +
7893 +int __init au_debug_init(void);
7894 +
7895 +#define AuDbgWhlist(w) do { \
7896 +       mutex_lock(&au_dbg_mtx); \
7897 +       AuDbg(#w "\n"); \
7898 +       au_dpri_whlist(w); \
7899 +       mutex_unlock(&au_dbg_mtx); \
7900 +} while (0)
7901 +
7902 +#define AuDbgVdir(v) do { \
7903 +       mutex_lock(&au_dbg_mtx); \
7904 +       AuDbg(#v "\n"); \
7905 +       au_dpri_vdir(v); \
7906 +       mutex_unlock(&au_dbg_mtx); \
7907 +} while (0)
7908 +
7909 +#define AuDbgInode(i) do { \
7910 +       mutex_lock(&au_dbg_mtx); \
7911 +       AuDbg(#i "\n"); \
7912 +       au_dpri_inode(i); \
7913 +       mutex_unlock(&au_dbg_mtx); \
7914 +} while (0)
7915 +
7916 +#define AuDbgDAlias(i) do { \
7917 +       mutex_lock(&au_dbg_mtx); \
7918 +       AuDbg(#i "\n"); \
7919 +       au_dpri_dalias(i); \
7920 +       mutex_unlock(&au_dbg_mtx); \
7921 +} while (0)
7922 +
7923 +#define AuDbgDentry(d) do { \
7924 +       mutex_lock(&au_dbg_mtx); \
7925 +       AuDbg(#d "\n"); \
7926 +       au_dpri_dentry(d); \
7927 +       mutex_unlock(&au_dbg_mtx); \
7928 +} while (0)
7929 +
7930 +#define AuDbgFile(f) do { \
7931 +       mutex_lock(&au_dbg_mtx); \
7932 +       AuDbg(#f "\n"); \
7933 +       au_dpri_file(f); \
7934 +       mutex_unlock(&au_dbg_mtx); \
7935 +} while (0)
7936 +
7937 +#define AuDbgSb(sb) do { \
7938 +       mutex_lock(&au_dbg_mtx); \
7939 +       AuDbg(#sb "\n"); \
7940 +       au_dpri_sb(sb); \
7941 +       mutex_unlock(&au_dbg_mtx); \
7942 +} while (0)
7943 +
7944 +#define AuDbgSym(addr) do {                            \
7945 +       char sym[KSYM_SYMBOL_LEN];                      \
7946 +       sprint_symbol(sym, (unsigned long)addr);        \
7947 +       AuDbg("%s\n", sym);                             \
7948 +} while (0)
7949 +#else
7950 +AuStubVoid(au_dbg_verify_dinode, struct dentry *dentry)
7951 +AuStubVoid(au_dbg_verify_gen, struct dentry *parent, unsigned int sigen)
7952 +AuStubVoid(au_dbg_verify_kthread, void)
7953 +AuStubInt0(__init au_debug_init, void)
7954 +
7955 +#define AuDbgWhlist(w)         do {} while (0)
7956 +#define AuDbgVdir(v)           do {} while (0)
7957 +#define AuDbgInode(i)          do {} while (0)
7958 +#define AuDbgDAlias(i)         do {} while (0)
7959 +#define AuDbgDentry(d)         do {} while (0)
7960 +#define AuDbgFile(f)           do {} while (0)
7961 +#define AuDbgSb(sb)            do {} while (0)
7962 +#define AuDbgSym(addr)         do {} while (0)
7963 +#endif /* CONFIG_AUFS_DEBUG */
7964 +
7965 +/* ---------------------------------------------------------------------- */
7966 +
7967 +#ifdef CONFIG_AUFS_MAGIC_SYSRQ
7968 +int __init au_sysrq_init(void);
7969 +void au_sysrq_fin(void);
7970 +
7971 +#ifdef CONFIG_HW_CONSOLE
7972 +#define au_dbg_blocked() do { \
7973 +       WARN_ON(1); \
7974 +       handle_sysrq('w'); \
7975 +} while (0)
7976 +#else
7977 +AuStubVoid(au_dbg_blocked, void)
7978 +#endif
7979 +
7980 +#else
7981 +AuStubInt0(__init au_sysrq_init, void)
7982 +AuStubVoid(au_sysrq_fin, void)
7983 +AuStubVoid(au_dbg_blocked, void)
7984 +#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
7985 +
7986 +#endif /* __KERNEL__ */
7987 +#endif /* __AUFS_DEBUG_H__ */
7988 diff -urN /usr/share/empty/fs/aufs/dentry.c linux/fs/aufs/dentry.c
7989 --- /usr/share/empty/fs/aufs/dentry.c   1970-01-01 01:00:00.000000000 +0100
7990 +++ linux/fs/aufs/dentry.c      2021-11-01 23:48:34.206359262 +0100
7991 @@ -0,0 +1,1169 @@
7992 +// SPDX-License-Identifier: GPL-2.0
7993 +/*
7994 + * Copyright (C) 2005-2021 Junjiro R. Okajima
7995 + *
7996 + * This program, aufs is free software; you can redistribute it and/or modify
7997 + * it under the terms of the GNU General Public License as published by
7998 + * the Free Software Foundation; either version 2 of the License, or
7999 + * (at your option) any later version.
8000 + *
8001 + * This program is distributed in the hope that it will be useful,
8002 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
8003 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
8004 + * GNU General Public License for more details.
8005 + *
8006 + * You should have received a copy of the GNU General Public License
8007 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
8008 + */
8009 +
8010 +/*
8011 + * lookup and dentry operations
8012 + */
8013 +
8014 +#include <linux/iversion.h>
8015 +#include <linux/namei.h>
8016 +#include "aufs.h"
8017 +
8018 +/*
8019 + * returns positive/negative dentry, NULL or an error.
8020 + * NULL means whiteout-ed or not-found.
8021 + */
8022 +static struct dentry*
8023 +au_do_lookup(struct dentry *h_parent, struct dentry *dentry,
8024 +            aufs_bindex_t bindex, struct au_do_lookup_args *args)
8025 +{
8026 +       struct dentry *h_dentry;
8027 +       struct inode *h_inode;
8028 +       struct au_branch *br;
8029 +       struct user_namespace *h_userns;
8030 +       struct path h_path;
8031 +       int wh_found, opq;
8032 +       unsigned char wh_able;
8033 +       const unsigned char allow_neg = !!au_ftest_lkup(args->flags, ALLOW_NEG);
8034 +       const unsigned char ignore_perm = !!au_ftest_lkup(args->flags,
8035 +                                                         IGNORE_PERM);
8036 +
8037 +       wh_found = 0;
8038 +       br = au_sbr(dentry->d_sb, bindex);
8039 +       h_path.dentry = h_parent;
8040 +       h_path.mnt = au_br_mnt(br);
8041 +       h_userns = au_br_userns(br);
8042 +       wh_able = !!au_br_whable(br->br_perm);
8043 +       if (wh_able)
8044 +               wh_found = au_wh_test(h_userns, &h_path, &args->whname,
8045 +                                     ignore_perm);
8046 +       h_dentry = ERR_PTR(wh_found);
8047 +       if (!wh_found)
8048 +               goto real_lookup;
8049 +       if (unlikely(wh_found < 0))
8050 +               goto out;
8051 +
8052 +       /* We found a whiteout */
8053 +       /* au_set_dbbot(dentry, bindex); */
8054 +       au_set_dbwh(dentry, bindex);
8055 +       if (!allow_neg)
8056 +               return NULL; /* success */
8057 +
8058 +real_lookup:
8059 +       if (!ignore_perm)
8060 +               h_dentry = vfsub_lkup_one(args->name, &h_path);
8061 +       else
8062 +               h_dentry = au_sio_lkup_one(h_userns, args->name, &h_path);
8063 +       if (IS_ERR(h_dentry)) {
8064 +               if (PTR_ERR(h_dentry) == -ENAMETOOLONG
8065 +                   && !allow_neg)
8066 +                       h_dentry = NULL;
8067 +               goto out;
8068 +       }
8069 +
8070 +       h_inode = d_inode(h_dentry);
8071 +       if (d_is_negative(h_dentry)) {
8072 +               if (!allow_neg)
8073 +                       goto out_neg;
8074 +       } else if (wh_found
8075 +                  || (args->type && args->type != (h_inode->i_mode & S_IFMT)))
8076 +               goto out_neg;
8077 +       else if (au_ftest_lkup(args->flags, DIRREN)
8078 +                /* && h_inode */
8079 +                && !au_dr_lkup_h_ino(args, bindex, h_inode->i_ino)) {
8080 +               AuDbg("b%d %pd ignored hi%llu\n", bindex, h_dentry,
8081 +                     (unsigned long long)h_inode->i_ino);
8082 +               goto out_neg;
8083 +       }
8084 +
8085 +       if (au_dbbot(dentry) <= bindex)
8086 +               au_set_dbbot(dentry, bindex);
8087 +       if (au_dbtop(dentry) < 0 || bindex < au_dbtop(dentry))
8088 +               au_set_dbtop(dentry, bindex);
8089 +       au_set_h_dptr(dentry, bindex, h_dentry);
8090 +
8091 +       if (!d_is_dir(h_dentry)
8092 +           || !wh_able
8093 +           || (d_really_is_positive(dentry) && !d_is_dir(dentry)))
8094 +               goto out; /* success */
8095 +
8096 +       h_path.dentry = h_dentry;
8097 +       inode_lock_shared_nested(h_inode, AuLsc_I_CHILD);
8098 +       opq = au_diropq_test(h_userns, &h_path);
8099 +       inode_unlock_shared(h_inode);
8100 +       if (opq > 0)
8101 +               au_set_dbdiropq(dentry, bindex);
8102 +       else if (unlikely(opq < 0)) {
8103 +               au_set_h_dptr(dentry, bindex, NULL);
8104 +               h_dentry = ERR_PTR(opq);
8105 +       }
8106 +       goto out;
8107 +
8108 +out_neg:
8109 +       dput(h_dentry);
8110 +       h_dentry = NULL;
8111 +out:
8112 +       return h_dentry;
8113 +}
8114 +
8115 +static int au_test_shwh(struct super_block *sb, const struct qstr *name)
8116 +{
8117 +       if (unlikely(!au_opt_test(au_mntflags(sb), SHWH)
8118 +                    && !strncmp(name->name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)))
8119 +               return -EPERM;
8120 +       return 0;
8121 +}
8122 +
8123 +/*
8124 + * returns the number of lower positive dentries,
8125 + * otherwise an error.
8126 + * can be called at unlinking with @type is zero.
8127 + */
8128 +int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t btop,
8129 +                  unsigned int flags)
8130 +{
8131 +       int npositive, err;
8132 +       aufs_bindex_t bindex, btail, bdiropq;
8133 +       unsigned char isdir, dirperm1, dirren;
8134 +       struct au_do_lookup_args args = {
8135 +               .flags          = flags,
8136 +               .name           = &dentry->d_name
8137 +       };
8138 +       struct dentry *parent;
8139 +       struct super_block *sb;
8140 +
8141 +       sb = dentry->d_sb;
8142 +       err = au_test_shwh(sb, args.name);
8143 +       if (unlikely(err))
8144 +               goto out;
8145 +
8146 +       err = au_wh_name_alloc(&args.whname, args.name);
8147 +       if (unlikely(err))
8148 +               goto out;
8149 +
8150 +       isdir = !!d_is_dir(dentry);
8151 +       dirperm1 = !!au_opt_test(au_mntflags(sb), DIRPERM1);
8152 +       dirren = !!au_opt_test(au_mntflags(sb), DIRREN);
8153 +       if (dirren)
8154 +               au_fset_lkup(args.flags, DIRREN);
8155 +
8156 +       npositive = 0;
8157 +       parent = dget_parent(dentry);
8158 +       btail = au_dbtaildir(parent);
8159 +       for (bindex = btop; bindex <= btail; bindex++) {
8160 +               struct dentry *h_parent, *h_dentry;
8161 +               struct inode *h_inode, *h_dir;
8162 +               struct au_branch *br;
8163 +
8164 +               h_dentry = au_h_dptr(dentry, bindex);
8165 +               if (h_dentry) {
8166 +                       if (d_is_positive(h_dentry))
8167 +                               npositive++;
8168 +                       break;
8169 +               }
8170 +               h_parent = au_h_dptr(parent, bindex);
8171 +               if (!h_parent || !d_is_dir(h_parent))
8172 +                       continue;
8173 +
8174 +               if (dirren) {
8175 +                       /* if the inum matches, then use the prepared name */
8176 +                       err = au_dr_lkup_name(&args, bindex);
8177 +                       if (unlikely(err))
8178 +                               goto out_parent;
8179 +               }
8180 +
8181 +               h_dir = d_inode(h_parent);
8182 +               inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
8183 +               h_dentry = au_do_lookup(h_parent, dentry, bindex, &args);
8184 +               inode_unlock_shared(h_dir);
8185 +               err = PTR_ERR(h_dentry);
8186 +               if (IS_ERR(h_dentry))
8187 +                       goto out_parent;
8188 +               if (h_dentry)
8189 +                       au_fclr_lkup(args.flags, ALLOW_NEG);
8190 +               if (dirperm1)
8191 +                       au_fset_lkup(args.flags, IGNORE_PERM);
8192 +
8193 +               if (au_dbwh(dentry) == bindex)
8194 +                       break;
8195 +               if (!h_dentry)
8196 +                       continue;
8197 +               if (d_is_negative(h_dentry))
8198 +                       continue;
8199 +               h_inode = d_inode(h_dentry);
8200 +               npositive++;
8201 +               if (!args.type)
8202 +                       args.type = h_inode->i_mode & S_IFMT;
8203 +               if (args.type != S_IFDIR)
8204 +                       break;
8205 +               else if (isdir) {
8206 +                       /* the type of lower may be different */
8207 +                       bdiropq = au_dbdiropq(dentry);
8208 +                       if (bdiropq >= 0 && bdiropq <= bindex)
8209 +                               break;
8210 +               }
8211 +               br = au_sbr(sb, bindex);
8212 +               if (dirren
8213 +                   && au_dr_hino_test_add(&br->br_dirren, h_inode->i_ino,
8214 +                                          /*add_ent*/NULL)) {
8215 +                       /* prepare next name to lookup */
8216 +                       err = au_dr_lkup(&args, dentry, bindex);
8217 +                       if (unlikely(err))
8218 +                               goto out_parent;
8219 +               }
8220 +       }
8221 +
8222 +       if (npositive) {
8223 +               AuLabel(positive);
8224 +               au_update_dbtop(dentry);
8225 +       }
8226 +       err = npositive;
8227 +       if (unlikely(!au_opt_test(au_mntflags(sb), UDBA_NONE)
8228 +                    && au_dbtop(dentry) < 0)) {
8229 +               err = -EIO;
8230 +               AuIOErr("both of real entry and whiteout found, %pd, err %d\n",
8231 +                       dentry, err);
8232 +       }
8233 +
8234 +out_parent:
8235 +       dput(parent);
8236 +       au_kfree_try_rcu(args.whname.name);
8237 +       if (dirren)
8238 +               au_dr_lkup_fin(&args);
8239 +out:
8240 +       return err;
8241 +}
8242 +
8243 +struct dentry *au_sio_lkup_one(struct user_namespace *userns, struct qstr *name,
8244 +                              struct path *ppath)
8245 +{
8246 +       struct dentry *dentry;
8247 +       int wkq_err;
8248 +
8249 +       if (!au_test_h_perm_sio(userns, d_inode(ppath->dentry), MAY_EXEC))
8250 +               dentry = vfsub_lkup_one(name, ppath);
8251 +       else {
8252 +               struct vfsub_lkup_one_args args = {
8253 +                       .errp   = &dentry,
8254 +                       .name   = name,
8255 +                       .ppath  = ppath
8256 +               };
8257 +
8258 +               wkq_err = au_wkq_wait(vfsub_call_lkup_one, &args);
8259 +               if (unlikely(wkq_err))
8260 +                       dentry = ERR_PTR(wkq_err);
8261 +       }
8262 +
8263 +       return dentry;
8264 +}
8265 +
8266 +/*
8267 + * lookup @dentry on @bindex which should be negative.
8268 + */
8269 +int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh)
8270 +{
8271 +       int err;
8272 +       struct dentry *parent, *h_dentry;
8273 +       struct au_branch *br;
8274 +       struct user_namespace *h_userns;
8275 +       struct path h_ppath;
8276 +
8277 +       parent = dget_parent(dentry);
8278 +       br = au_sbr(dentry->d_sb, bindex);
8279 +       h_ppath.dentry = au_h_dptr(parent, bindex);
8280 +       h_ppath.mnt = au_br_mnt(br);
8281 +       h_userns = au_br_userns(br);
8282 +       if (wh)
8283 +               h_dentry = au_whtmp_lkup(h_ppath.dentry, br, &dentry->d_name);
8284 +       else
8285 +               h_dentry = au_sio_lkup_one(h_userns, &dentry->d_name, &h_ppath);
8286 +       err = PTR_ERR(h_dentry);
8287 +       if (IS_ERR(h_dentry))
8288 +               goto out;
8289 +       if (unlikely(d_is_positive(h_dentry))) {
8290 +               err = -EIO;
8291 +               AuIOErr("%pd should be negative on b%d.\n", h_dentry, bindex);
8292 +               dput(h_dentry);
8293 +               goto out;
8294 +       }
8295 +
8296 +       err = 0;
8297 +       if (bindex < au_dbtop(dentry))
8298 +               au_set_dbtop(dentry, bindex);
8299 +       if (au_dbbot(dentry) < bindex)
8300 +               au_set_dbbot(dentry, bindex);
8301 +       au_set_h_dptr(dentry, bindex, h_dentry);
8302 +
8303 +out:
8304 +       dput(parent);
8305 +       return err;
8306 +}
8307 +
8308 +/* ---------------------------------------------------------------------- */
8309 +
8310 +/* subset of struct inode */
8311 +struct au_iattr {
8312 +       unsigned long           i_ino;
8313 +       /* unsigned int         i_nlink; */
8314 +       kuid_t                  i_uid;
8315 +       kgid_t                  i_gid;
8316 +       u64                     i_version;
8317 +/*
8318 +       loff_t                  i_size;
8319 +       blkcnt_t                i_blocks;
8320 +*/
8321 +       umode_t                 i_mode;
8322 +};
8323 +
8324 +static void au_iattr_save(struct au_iattr *ia, struct inode *h_inode)
8325 +{
8326 +       ia->i_ino = h_inode->i_ino;
8327 +       /* ia->i_nlink = h_inode->i_nlink; */
8328 +       ia->i_uid = h_inode->i_uid;
8329 +       ia->i_gid = h_inode->i_gid;
8330 +       ia->i_version = inode_query_iversion(h_inode);
8331 +/*
8332 +       ia->i_size = h_inode->i_size;
8333 +       ia->i_blocks = h_inode->i_blocks;
8334 +*/
8335 +       ia->i_mode = (h_inode->i_mode & S_IFMT);
8336 +}
8337 +
8338 +static int au_iattr_test(struct au_iattr *ia, struct inode *h_inode)
8339 +{
8340 +       return ia->i_ino != h_inode->i_ino
8341 +               /* || ia->i_nlink != h_inode->i_nlink */
8342 +               || !uid_eq(ia->i_uid, h_inode->i_uid)
8343 +               || !gid_eq(ia->i_gid, h_inode->i_gid)
8344 +               || !inode_eq_iversion(h_inode, ia->i_version)
8345 +/*
8346 +               || ia->i_size != h_inode->i_size
8347 +               || ia->i_blocks != h_inode->i_blocks
8348 +*/
8349 +               || ia->i_mode != (h_inode->i_mode & S_IFMT);
8350 +}
8351 +
8352 +static int au_h_verify_dentry(struct dentry *h_dentry, struct dentry *h_parent,
8353 +                             struct au_branch *br)
8354 +{
8355 +       int err;
8356 +       struct au_iattr ia;
8357 +       struct inode *h_inode;
8358 +       struct dentry *h_d;
8359 +       struct super_block *h_sb;
8360 +       struct path h_ppath;
8361 +
8362 +       err = 0;
8363 +       memset(&ia, -1, sizeof(ia));
8364 +       h_sb = h_dentry->d_sb;
8365 +       h_inode = NULL;
8366 +       if (d_is_positive(h_dentry)) {
8367 +               h_inode = d_inode(h_dentry);
8368 +               au_iattr_save(&ia, h_inode);
8369 +       } else if (au_test_nfs(h_sb) || au_test_fuse(h_sb))
8370 +               /* nfs d_revalidate may return 0 for negative dentry */
8371 +               /* fuse d_revalidate always return 0 for negative dentry */
8372 +               goto out;
8373 +
8374 +       /* main purpose is namei.c:cached_lookup() and d_revalidate */
8375 +       h_ppath.dentry = h_parent;
8376 +       h_ppath.mnt = au_br_mnt(br);
8377 +       h_d = vfsub_lkup_one(&h_dentry->d_name, &h_ppath);
8378 +       err = PTR_ERR(h_d);
8379 +       if (IS_ERR(h_d))
8380 +               goto out;
8381 +
8382 +       err = 0;
8383 +       if (unlikely(h_d != h_dentry
8384 +                    || d_inode(h_d) != h_inode
8385 +                    || (h_inode && au_iattr_test(&ia, h_inode))))
8386 +               err = au_busy_or_stale();
8387 +       dput(h_d);
8388 +
8389 +out:
8390 +       AuTraceErr(err);
8391 +       return err;
8392 +}
8393 +
8394 +int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
8395 +               struct dentry *h_parent, struct au_branch *br)
8396 +{
8397 +       int err;
8398 +
8399 +       err = 0;
8400 +       if (udba == AuOpt_UDBA_REVAL
8401 +           && !au_test_fs_remote(h_dentry->d_sb)) {
8402 +               IMustLock(h_dir);
8403 +               err = (d_inode(h_dentry->d_parent) != h_dir);
8404 +       } else if (udba != AuOpt_UDBA_NONE)
8405 +               err = au_h_verify_dentry(h_dentry, h_parent, br);
8406 +
8407 +       return err;
8408 +}
8409 +
8410 +/* ---------------------------------------------------------------------- */
8411 +
8412 +static int au_do_refresh_hdentry(struct dentry *dentry, struct dentry *parent)
8413 +{
8414 +       int err;
8415 +       aufs_bindex_t new_bindex, bindex, bbot, bwh, bdiropq;
8416 +       struct au_hdentry tmp, *p, *q;
8417 +       struct au_dinfo *dinfo;
8418 +       struct super_block *sb;
8419 +
8420 +       DiMustWriteLock(dentry);
8421 +
8422 +       sb = dentry->d_sb;
8423 +       dinfo = au_di(dentry);
8424 +       bbot = dinfo->di_bbot;
8425 +       bwh = dinfo->di_bwh;
8426 +       bdiropq = dinfo->di_bdiropq;
8427 +       bindex = dinfo->di_btop;
8428 +       p = au_hdentry(dinfo, bindex);
8429 +       for (; bindex <= bbot; bindex++, p++) {
8430 +               if (!p->hd_dentry)
8431 +                       continue;
8432 +
8433 +               new_bindex = au_br_index(sb, p->hd_id);
8434 +               if (new_bindex == bindex)
8435 +                       continue;
8436 +
8437 +               if (dinfo->di_bwh == bindex)
8438 +                       bwh = new_bindex;
8439 +               if (dinfo->di_bdiropq == bindex)
8440 +                       bdiropq = new_bindex;
8441 +               if (new_bindex < 0) {
8442 +                       au_hdput(p);
8443 +                       p->hd_dentry = NULL;
8444 +                       continue;
8445 +               }
8446 +
8447 +               /* swap two lower dentries, and loop again */
8448 +               q = au_hdentry(dinfo, new_bindex);
8449 +               tmp = *q;
8450 +               *q = *p;
8451 +               *p = tmp;
8452 +               if (tmp.hd_dentry) {
8453 +                       bindex--;
8454 +                       p--;
8455 +               }
8456 +       }
8457 +
8458 +       dinfo->di_bwh = -1;
8459 +       if (bwh >= 0 && bwh <= au_sbbot(sb) && au_sbr_whable(sb, bwh))
8460 +               dinfo->di_bwh = bwh;
8461 +
8462 +       dinfo->di_bdiropq = -1;
8463 +       if (bdiropq >= 0
8464 +           && bdiropq <= au_sbbot(sb)
8465 +           && au_sbr_whable(sb, bdiropq))
8466 +               dinfo->di_bdiropq = bdiropq;
8467 +
8468 +       err = -EIO;
8469 +       dinfo->di_btop = -1;
8470 +       dinfo->di_bbot = -1;
8471 +       bbot = au_dbbot(parent);
8472 +       bindex = 0;
8473 +       p = au_hdentry(dinfo, bindex);
8474 +       for (; bindex <= bbot; bindex++, p++)
8475 +               if (p->hd_dentry) {
8476 +                       dinfo->di_btop = bindex;
8477 +                       break;
8478 +               }
8479 +
8480 +       if (dinfo->di_btop >= 0) {
8481 +               bindex = bbot;
8482 +               p = au_hdentry(dinfo, bindex);
8483 +               for (; bindex >= 0; bindex--, p--)
8484 +                       if (p->hd_dentry) {
8485 +                               dinfo->di_bbot = bindex;
8486 +                               err = 0;
8487 +                               break;
8488 +                       }
8489 +       }
8490 +
8491 +       return err;
8492 +}
8493 +
8494 +static void au_do_hide(struct dentry *dentry)
8495 +{
8496 +       struct inode *inode;
8497 +
8498 +       if (d_really_is_positive(dentry)) {
8499 +               inode = d_inode(dentry);
8500 +               if (!d_is_dir(dentry)) {
8501 +                       if (inode->i_nlink && !d_unhashed(dentry))
8502 +                               drop_nlink(inode);
8503 +               } else {
8504 +                       clear_nlink(inode);
8505 +                       /* stop next lookup */
8506 +                       inode->i_flags |= S_DEAD;
8507 +               }
8508 +               smp_mb(); /* necessary? */
8509 +       }
8510 +       d_drop(dentry);
8511 +}
8512 +
8513 +static int au_hide_children(struct dentry *parent)
8514 +{
8515 +       int err, i, j, ndentry;
8516 +       struct au_dcsub_pages dpages;
8517 +       struct au_dpage *dpage;
8518 +       struct dentry *dentry;
8519 +
8520 +       err = au_dpages_init(&dpages, GFP_NOFS);
8521 +       if (unlikely(err))
8522 +               goto out;
8523 +       err = au_dcsub_pages(&dpages, parent, NULL, NULL);
8524 +       if (unlikely(err))
8525 +               goto out_dpages;
8526 +
8527 +       /* in reverse order */
8528 +       for (i = dpages.ndpage - 1; i >= 0; i--) {
8529 +               dpage = dpages.dpages + i;
8530 +               ndentry = dpage->ndentry;
8531 +               for (j = ndentry - 1; j >= 0; j--) {
8532 +                       dentry = dpage->dentries[j];
8533 +                       if (dentry != parent)
8534 +                               au_do_hide(dentry);
8535 +               }
8536 +       }
8537 +
8538 +out_dpages:
8539 +       au_dpages_free(&dpages);
8540 +out:
8541 +       return err;
8542 +}
8543 +
8544 +static void au_hide(struct dentry *dentry)
8545 +{
8546 +       int err;
8547 +
8548 +       AuDbgDentry(dentry);
8549 +       if (d_is_dir(dentry)) {
8550 +               /* shrink_dcache_parent(dentry); */
8551 +               err = au_hide_children(dentry);
8552 +               if (unlikely(err))
8553 +                       AuIOErr("%pd, failed hiding children, ignored %d\n",
8554 +                               dentry, err);
8555 +       }
8556 +       au_do_hide(dentry);
8557 +}
8558 +
8559 +/*
8560 + * By adding a dirty branch, a cached dentry may be affected in various ways.
8561 + *
8562 + * a dirty branch is added
8563 + * - on the top of layers
8564 + * - in the middle of layers
8565 + * - to the bottom of layers
8566 + *
8567 + * on the added branch there exists
8568 + * - a whiteout
8569 + * - a diropq
8570 + * - a same named entry
8571 + *   + exist
8572 + *     * negative --> positive
8573 + *     * positive --> positive
8574 + *      - type is unchanged
8575 + *      - type is changed
8576 + *   + doesn't exist
8577 + *     * negative --> negative
8578 + *     * positive --> negative (rejected by au_br_del() for non-dir case)
8579 + * - none
8580 + */
8581 +static int au_refresh_by_dinfo(struct dentry *dentry, struct au_dinfo *dinfo,
8582 +                              struct au_dinfo *tmp)
8583 +{
8584 +       int err;
8585 +       aufs_bindex_t bindex, bbot;
8586 +       struct {
8587 +               struct dentry *dentry;
8588 +               struct inode *inode;
8589 +               mode_t mode;
8590 +       } orig_h, tmp_h = {
8591 +               .dentry = NULL
8592 +       };
8593 +       struct au_hdentry *hd;
8594 +       struct inode *inode, *h_inode;
8595 +       struct dentry *h_dentry;
8596 +
8597 +       err = 0;
8598 +       AuDebugOn(dinfo->di_btop < 0);
8599 +       orig_h.mode = 0;
8600 +       orig_h.dentry = au_hdentry(dinfo, dinfo->di_btop)->hd_dentry;
8601 +       orig_h.inode = NULL;
8602 +       if (d_is_positive(orig_h.dentry)) {
8603 +               orig_h.inode = d_inode(orig_h.dentry);
8604 +               orig_h.mode = orig_h.inode->i_mode & S_IFMT;
8605 +       }
8606 +       if (tmp->di_btop >= 0) {
8607 +               tmp_h.dentry = au_hdentry(tmp, tmp->di_btop)->hd_dentry;
8608 +               if (d_is_positive(tmp_h.dentry)) {
8609 +                       tmp_h.inode = d_inode(tmp_h.dentry);
8610 +                       tmp_h.mode = tmp_h.inode->i_mode & S_IFMT;
8611 +               }
8612 +       }
8613 +
8614 +       inode = NULL;
8615 +       if (d_really_is_positive(dentry))
8616 +               inode = d_inode(dentry);
8617 +       if (!orig_h.inode) {
8618 +               AuDbg("negative originally\n");
8619 +               if (inode) {
8620 +                       au_hide(dentry);
8621 +                       goto out;
8622 +               }
8623 +               AuDebugOn(inode);
8624 +               AuDebugOn(dinfo->di_btop != dinfo->di_bbot);
8625 +               AuDebugOn(dinfo->di_bdiropq != -1);
8626 +
8627 +               if (!tmp_h.inode) {
8628 +                       AuDbg("negative --> negative\n");
8629 +                       /* should have only one negative lower */
8630 +                       if (tmp->di_btop >= 0
8631 +                           && tmp->di_btop < dinfo->di_btop) {
8632 +                               AuDebugOn(tmp->di_btop != tmp->di_bbot);
8633 +                               AuDebugOn(dinfo->di_btop != dinfo->di_bbot);
8634 +                               au_set_h_dptr(dentry, dinfo->di_btop, NULL);
8635 +                               au_di_cp(dinfo, tmp);
8636 +                               hd = au_hdentry(tmp, tmp->di_btop);
8637 +                               au_set_h_dptr(dentry, tmp->di_btop,
8638 +                                             dget(hd->hd_dentry));
8639 +                       }
8640 +                       au_dbg_verify_dinode(dentry);
8641 +               } else {
8642 +                       AuDbg("negative --> positive\n");
8643 +                       /*
8644 +                        * similar to the behaviour of creating with bypassing
8645 +                        * aufs.
8646 +                        * unhash it in order to force an error in the
8647 +                        * succeeding create operation.
8648 +                        * we should not set S_DEAD here.
8649 +                        */
8650 +                       d_drop(dentry);
8651 +                       /* au_di_swap(tmp, dinfo); */
8652 +                       au_dbg_verify_dinode(dentry);
8653 +               }
8654 +       } else {
8655 +               AuDbg("positive originally\n");
8656 +               /* inode may be NULL */
8657 +               AuDebugOn(inode && (inode->i_mode & S_IFMT) != orig_h.mode);
8658 +               if (!tmp_h.inode) {
8659 +                       AuDbg("positive --> negative\n");
8660 +                       /* or bypassing aufs */
8661 +                       au_hide(dentry);
8662 +                       if (tmp->di_bwh >= 0 && tmp->di_bwh <= dinfo->di_btop)
8663 +                               dinfo->di_bwh = tmp->di_bwh;
8664 +                       if (inode)
8665 +                               err = au_refresh_hinode_self(inode);
8666 +                       au_dbg_verify_dinode(dentry);
8667 +               } else if (orig_h.mode == tmp_h.mode) {
8668 +                       AuDbg("positive --> positive, same type\n");
8669 +                       if (!S_ISDIR(orig_h.mode)
8670 +                           && dinfo->di_btop > tmp->di_btop) {
8671 +                               /*
8672 +                                * similar to the behaviour of removing and
8673 +                                * creating.
8674 +                                */
8675 +                               au_hide(dentry);
8676 +                               if (inode)
8677 +                                       err = au_refresh_hinode_self(inode);
8678 +                               au_dbg_verify_dinode(dentry);
8679 +                       } else {
8680 +                               /* fill empty slots */
8681 +                               if (dinfo->di_btop > tmp->di_btop)
8682 +                                       dinfo->di_btop = tmp->di_btop;
8683 +                               if (dinfo->di_bbot < tmp->di_bbot)
8684 +                                       dinfo->di_bbot = tmp->di_bbot;
8685 +                               dinfo->di_bwh = tmp->di_bwh;
8686 +                               dinfo->di_bdiropq = tmp->di_bdiropq;
8687 +                               bbot = dinfo->di_bbot;
8688 +                               bindex = tmp->di_btop;
8689 +                               hd = au_hdentry(tmp, bindex);
8690 +                               for (; bindex <= bbot; bindex++, hd++) {
8691 +                                       if (au_h_dptr(dentry, bindex))
8692 +                                               continue;
8693 +                                       h_dentry = hd->hd_dentry;
8694 +                                       if (!h_dentry)
8695 +                                               continue;
8696 +                                       AuDebugOn(d_is_negative(h_dentry));
8697 +                                       h_inode = d_inode(h_dentry);
8698 +                                       AuDebugOn(orig_h.mode
8699 +                                                 != (h_inode->i_mode
8700 +                                                     & S_IFMT));
8701 +                                       au_set_h_dptr(dentry, bindex,
8702 +                                                     dget(h_dentry));
8703 +                               }
8704 +                               if (inode)
8705 +                                       err = au_refresh_hinode(inode, dentry);
8706 +                               au_dbg_verify_dinode(dentry);
8707 +                       }
8708 +               } else {
8709 +                       AuDbg("positive --> positive, different type\n");
8710 +                       /* similar to the behaviour of removing and creating */
8711 +                       au_hide(dentry);
8712 +                       if (inode)
8713 +                               err = au_refresh_hinode_self(inode);
8714 +                       au_dbg_verify_dinode(dentry);
8715 +               }
8716 +       }
8717 +
8718 +out:
8719 +       return err;
8720 +}
8721 +
8722 +void au_refresh_dop(struct dentry *dentry, int force_reval)
8723 +{
8724 +       const struct dentry_operations *dop
8725 +               = force_reval ? &aufs_dop : dentry->d_sb->s_d_op;
8726 +       static const unsigned int mask
8727 +               = DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE;
8728 +
8729 +       BUILD_BUG_ON(sizeof(mask) != sizeof(dentry->d_flags));
8730 +
8731 +       if (dentry->d_op == dop)
8732 +               return;
8733 +
8734 +       AuDbg("%pd\n", dentry);
8735 +       spin_lock(&dentry->d_lock);
8736 +       if (dop == &aufs_dop)
8737 +               dentry->d_flags |= mask;
8738 +       else
8739 +               dentry->d_flags &= ~mask;
8740 +       dentry->d_op = dop;
8741 +       spin_unlock(&dentry->d_lock);
8742 +}
8743 +
8744 +int au_refresh_dentry(struct dentry *dentry, struct dentry *parent)
8745 +{
8746 +       int err, ebrange, nbr;
8747 +       unsigned int sigen;
8748 +       struct au_dinfo *dinfo, *tmp;
8749 +       struct super_block *sb;
8750 +       struct inode *inode;
8751 +
8752 +       DiMustWriteLock(dentry);
8753 +       AuDebugOn(IS_ROOT(dentry));
8754 +       AuDebugOn(d_really_is_negative(parent));
8755 +
8756 +       sb = dentry->d_sb;
8757 +       sigen = au_sigen(sb);
8758 +       err = au_digen_test(parent, sigen);
8759 +       if (unlikely(err))
8760 +               goto out;
8761 +
8762 +       nbr = au_sbbot(sb) + 1;
8763 +       dinfo = au_di(dentry);
8764 +       err = au_di_realloc(dinfo, nbr, /*may_shrink*/0);
8765 +       if (unlikely(err))
8766 +               goto out;
8767 +       ebrange = au_dbrange_test(dentry);
8768 +       if (!ebrange)
8769 +               ebrange = au_do_refresh_hdentry(dentry, parent);
8770 +
8771 +       if (d_unhashed(dentry) || ebrange /* || dinfo->di_tmpfile */) {
8772 +               AuDebugOn(au_dbtop(dentry) < 0 && au_dbbot(dentry) >= 0);
8773 +               if (d_really_is_positive(dentry)) {
8774 +                       inode = d_inode(dentry);
8775 +                       err = au_refresh_hinode_self(inode);
8776 +               }
8777 +               au_dbg_verify_dinode(dentry);
8778 +               if (!err)
8779 +                       goto out_dgen; /* success */
8780 +               goto out;
8781 +       }
8782 +
8783 +       /* temporary dinfo */
8784 +       AuDbgDentry(dentry);
8785 +       err = -ENOMEM;
8786 +       tmp = au_di_alloc(sb, AuLsc_DI_TMP);
8787 +       if (unlikely(!tmp))
8788 +               goto out;
8789 +       au_di_swap(tmp, dinfo);
8790 +       /* returns the number of positive dentries */
8791 +       /*
8792 +        * if current working dir is removed, it returns an error.
8793 +        * but the dentry is legal.
8794 +        */
8795 +       err = au_lkup_dentry(dentry, /*btop*/0, AuLkup_ALLOW_NEG);
8796 +       AuDbgDentry(dentry);
8797 +       au_di_swap(tmp, dinfo);
8798 +       if (err == -ENOENT)
8799 +               err = 0;
8800 +       if (err >= 0) {
8801 +               /* compare/refresh by dinfo */
8802 +               AuDbgDentry(dentry);
8803 +               err = au_refresh_by_dinfo(dentry, dinfo, tmp);
8804 +               au_dbg_verify_dinode(dentry);
8805 +               AuTraceErr(err);
8806 +       }
8807 +       au_di_realloc(dinfo, nbr, /*may_shrink*/1); /* harmless if err */
8808 +       au_rw_write_unlock(&tmp->di_rwsem);
8809 +       au_di_free(tmp);
8810 +       if (unlikely(err))
8811 +               goto out;
8812 +
8813 +out_dgen:
8814 +       au_update_digen(dentry);
8815 +out:
8816 +       if (unlikely(err && !(dentry->d_flags & DCACHE_NFSFS_RENAMED))) {
8817 +               AuIOErr("failed refreshing %pd, %d\n", dentry, err);
8818 +               AuDbgDentry(dentry);
8819 +       }
8820 +       AuTraceErr(err);
8821 +       return err;
8822 +}
8823 +
8824 +static int au_do_h_d_reval(struct dentry *h_dentry, unsigned int flags,
8825 +                          struct dentry *dentry, aufs_bindex_t bindex)
8826 +{
8827 +       int err, valid;
8828 +
8829 +       err = 0;
8830 +       if (!(h_dentry->d_flags & DCACHE_OP_REVALIDATE))
8831 +               goto out;
8832 +
8833 +       AuDbg("b%d\n", bindex);
8834 +       /*
8835 +        * gave up supporting LOOKUP_CREATE/OPEN for lower fs,
8836 +        * due to whiteout and branch permission.
8837 +        */
8838 +       flags &= ~(/*LOOKUP_PARENT |*/ LOOKUP_OPEN | LOOKUP_CREATE
8839 +                  | LOOKUP_FOLLOW | LOOKUP_EXCL);
8840 +       /* it may return tri-state */
8841 +       valid = h_dentry->d_op->d_revalidate(h_dentry, flags);
8842 +
8843 +       if (unlikely(valid < 0))
8844 +               err = valid;
8845 +       else if (!valid)
8846 +               err = -EINVAL;
8847 +
8848 +out:
8849 +       AuTraceErr(err);
8850 +       return err;
8851 +}
8852 +
8853 +/* todo: remove this */
8854 +static int h_d_revalidate(struct dentry *dentry, struct inode *inode,
8855 +                         unsigned int flags, int do_udba, int dirren)
8856 +{
8857 +       int err;
8858 +       umode_t mode, h_mode;
8859 +       aufs_bindex_t bindex, btail, btop, ibs, ibe;
8860 +       unsigned char plus, unhashed, is_root, h_plus, h_nfs, tmpfile;
8861 +       struct inode *h_inode, *h_cached_inode;
8862 +       struct dentry *h_dentry;
8863 +       struct qstr *name, *h_name;
8864 +
8865 +       err = 0;
8866 +       plus = 0;
8867 +       mode = 0;
8868 +       ibs = -1;
8869 +       ibe = -1;
8870 +       unhashed = !!d_unhashed(dentry);
8871 +       is_root = !!IS_ROOT(dentry);
8872 +       name = &dentry->d_name;
8873 +       tmpfile = au_di(dentry)->di_tmpfile;
8874 +
8875 +       /*
8876 +        * Theoretically, REVAL test should be unnecessary in case of
8877 +        * {FS,I}NOTIFY.
8878 +        * But {fs,i}notify doesn't fire some necessary events,
8879 +        *      IN_ATTRIB for atime/nlink/pageio
8880 +        * Let's do REVAL test too.
8881 +        */
8882 +       if (do_udba && inode) {
8883 +               mode = (inode->i_mode & S_IFMT);
8884 +               plus = (inode->i_nlink > 0);
8885 +               ibs = au_ibtop(inode);
8886 +               ibe = au_ibbot(inode);
8887 +       }
8888 +
8889 +       btop = au_dbtop(dentry);
8890 +       btail = btop;
8891 +       if (inode && S_ISDIR(inode->i_mode))
8892 +               btail = au_dbtaildir(dentry);
8893 +       for (bindex = btop; bindex <= btail; bindex++) {
8894 +               h_dentry = au_h_dptr(dentry, bindex);
8895 +               if (!h_dentry)
8896 +                       continue;
8897 +
8898 +               AuDbg("b%d, %pd\n", bindex, h_dentry);
8899 +               h_nfs = !!au_test_nfs(h_dentry->d_sb);
8900 +               spin_lock(&h_dentry->d_lock);
8901 +               h_name = &h_dentry->d_name;
8902 +               if (unlikely(do_udba
8903 +                            && !is_root
8904 +                            && ((!h_nfs
8905 +                                 && (unhashed != !!d_unhashed(h_dentry)
8906 +                                     || (!tmpfile && !dirren
8907 +                                         && !au_qstreq(name, h_name))
8908 +                                         ))
8909 +                                || (h_nfs
8910 +                                    && !(flags & LOOKUP_OPEN)
8911 +                                    && (h_dentry->d_flags
8912 +                                        & DCACHE_NFSFS_RENAMED)))
8913 +                           )) {
8914 +                       int h_unhashed;
8915 +
8916 +                       h_unhashed = d_unhashed(h_dentry);
8917 +                       spin_unlock(&h_dentry->d_lock);
8918 +                       AuDbg("unhash 0x%x 0x%x, %pd %pd\n",
8919 +                             unhashed, h_unhashed, dentry, h_dentry);
8920 +                       goto err;
8921 +               }
8922 +               spin_unlock(&h_dentry->d_lock);
8923 +
8924 +               err = au_do_h_d_reval(h_dentry, flags, dentry, bindex);
8925 +               if (unlikely(err))
8926 +                       /* do not goto err, to keep the errno */
8927 +                       break;
8928 +
8929 +               /* todo: plink too? */
8930 +               if (!do_udba)
8931 +                       continue;
8932 +
8933 +               /* UDBA tests */
8934 +               if (unlikely(!!inode != d_is_positive(h_dentry)))
8935 +                       goto err;
8936 +
8937 +               h_inode = NULL;
8938 +               if (d_is_positive(h_dentry))
8939 +                       h_inode = d_inode(h_dentry);
8940 +               h_plus = plus;
8941 +               h_mode = mode;
8942 +               h_cached_inode = h_inode;
8943 +               if (h_inode) {
8944 +                       h_mode = (h_inode->i_mode & S_IFMT);
8945 +                       h_plus = (h_inode->i_nlink > 0);
8946 +               }
8947 +               if (inode && ibs <= bindex && bindex <= ibe)
8948 +                       h_cached_inode = au_h_iptr(inode, bindex);
8949 +
8950 +               if (!h_nfs) {
8951 +                       if (unlikely(plus != h_plus && !tmpfile))
8952 +                               goto err;
8953 +               } else {
8954 +                       if (unlikely(!(h_dentry->d_flags & DCACHE_NFSFS_RENAMED)
8955 +                                    && !is_root
8956 +                                    && !IS_ROOT(h_dentry)
8957 +                                    && unhashed != d_unhashed(h_dentry)))
8958 +                               goto err;
8959 +               }
8960 +               if (unlikely(mode != h_mode
8961 +                            || h_cached_inode != h_inode))
8962 +                       goto err;
8963 +               continue;
8964 +
8965 +err:
8966 +               err = -EINVAL;
8967 +               break;
8968 +       }
8969 +
8970 +       AuTraceErr(err);
8971 +       return err;
8972 +}
8973 +
8974 +/* todo: consolidate with do_refresh() and au_reval_for_attr() */
8975 +static int simple_reval_dpath(struct dentry *dentry, unsigned int sigen)
8976 +{
8977 +       int err;
8978 +       struct dentry *parent;
8979 +
8980 +       if (!au_digen_test(dentry, sigen))
8981 +               return 0;
8982 +
8983 +       parent = dget_parent(dentry);
8984 +       di_read_lock_parent(parent, AuLock_IR);
8985 +       AuDebugOn(au_digen_test(parent, sigen));
8986 +       au_dbg_verify_gen(parent, sigen);
8987 +       err = au_refresh_dentry(dentry, parent);
8988 +       di_read_unlock(parent, AuLock_IR);
8989 +       dput(parent);
8990 +       AuTraceErr(err);
8991 +       return err;
8992 +}
8993 +
8994 +int au_reval_dpath(struct dentry *dentry, unsigned int sigen)
8995 +{
8996 +       int err;
8997 +       struct dentry *d, *parent;
8998 +
8999 +       if (!au_ftest_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR))
9000 +               return simple_reval_dpath(dentry, sigen);
9001 +
9002 +       /* slow loop, keep it simple and stupid */
9003 +       /* cf: au_cpup_dirs() */
9004 +       err = 0;
9005 +       parent = NULL;
9006 +       while (au_digen_test(dentry, sigen)) {
9007 +               d = dentry;
9008 +               while (1) {
9009 +                       dput(parent);
9010 +                       parent = dget_parent(d);
9011 +                       if (!au_digen_test(parent, sigen))
9012 +                               break;
9013 +                       d = parent;
9014 +               }
9015 +
9016 +               if (d != dentry)
9017 +                       di_write_lock_child2(d);
9018 +
9019 +               /* someone might update our dentry while we were sleeping */
9020 +               if (au_digen_test(d, sigen)) {
9021 +                       /*
9022 +                        * todo: consolidate with simple_reval_dpath(),
9023 +                        * do_refresh() and au_reval_for_attr().
9024 +                        */
9025 +                       di_read_lock_parent(parent, AuLock_IR);
9026 +                       err = au_refresh_dentry(d, parent);
9027 +                       di_read_unlock(parent, AuLock_IR);
9028 +               }
9029 +
9030 +               if (d != dentry)
9031 +                       di_write_unlock(d);
9032 +               dput(parent);
9033 +               if (unlikely(err))
9034 +                       break;
9035 +       }
9036 +
9037 +       return err;
9038 +}
9039 +
9040 +/*
9041 + * if valid returns 1, otherwise 0.
9042 + */
9043 +static int aufs_d_revalidate(struct dentry *dentry, unsigned int flags)
9044 +{
9045 +       int valid, err;
9046 +       unsigned int sigen;
9047 +       unsigned char do_udba, dirren;
9048 +       struct super_block *sb;
9049 +       struct inode *inode;
9050 +
9051 +       /* todo: support rcu-walk? */
9052 +       if (flags & LOOKUP_RCU)
9053 +               return -ECHILD;
9054 +
9055 +       valid = 0;
9056 +       if (unlikely(!au_di(dentry)))
9057 +               goto out;
9058 +
9059 +       valid = 1;
9060 +       sb = dentry->d_sb;
9061 +       /*
9062 +        * todo: very ugly
9063 +        * i_mutex of parent dir may be held,
9064 +        * but we should not return 'invalid' due to busy.
9065 +        */
9066 +       err = aufs_read_lock(dentry, AuLock_FLUSH | AuLock_DW | AuLock_NOPLM);
9067 +       if (unlikely(err)) {
9068 +               valid = err;
9069 +               AuTraceErr(err);
9070 +               goto out;
9071 +       }
9072 +       inode = NULL;
9073 +       if (d_really_is_positive(dentry))
9074 +               inode = d_inode(dentry);
9075 +       if (unlikely(inode && au_is_bad_inode(inode))) {
9076 +               err = -EINVAL;
9077 +               AuTraceErr(err);
9078 +               goto out_dgrade;
9079 +       }
9080 +       if (unlikely(au_dbrange_test(dentry))) {
9081 +               err = -EINVAL;
9082 +               AuTraceErr(err);
9083 +               goto out_dgrade;
9084 +       }
9085 +
9086 +       sigen = au_sigen(sb);
9087 +       if (au_digen_test(dentry, sigen)) {
9088 +               AuDebugOn(IS_ROOT(dentry));
9089 +               err = au_reval_dpath(dentry, sigen);
9090 +               if (unlikely(err)) {
9091 +                       AuTraceErr(err);
9092 +                       goto out_dgrade;
9093 +               }
9094 +       }
9095 +       di_downgrade_lock(dentry, AuLock_IR);
9096 +
9097 +       err = -EINVAL;
9098 +       if (!(flags & (LOOKUP_OPEN | LOOKUP_EMPTY))
9099 +           && inode
9100 +           && !(inode->i_state && I_LINKABLE)
9101 +           && (IS_DEADDIR(inode) || !inode->i_nlink)) {
9102 +               AuTraceErr(err);
9103 +               goto out_inval;
9104 +       }
9105 +
9106 +       do_udba = !au_opt_test(au_mntflags(sb), UDBA_NONE);
9107 +       if (do_udba && inode) {
9108 +               aufs_bindex_t btop = au_ibtop(inode);
9109 +               struct inode *h_inode;
9110 +
9111 +               if (btop >= 0) {
9112 +                       h_inode = au_h_iptr(inode, btop);
9113 +                       if (h_inode && au_test_higen(inode, h_inode)) {
9114 +                               AuTraceErr(err);
9115 +                               goto out_inval;
9116 +                       }
9117 +               }
9118 +       }
9119 +
9120 +       dirren = !!au_opt_test(au_mntflags(sb), DIRREN);
9121 +       err = h_d_revalidate(dentry, inode, flags, do_udba, dirren);
9122 +       if (unlikely(!err && do_udba && au_dbtop(dentry) < 0)) {
9123 +               err = -EIO;
9124 +               AuDbg("both of real entry and whiteout found, %p, err %d\n",
9125 +                     dentry, err);
9126 +       }
9127 +       goto out_inval;
9128 +
9129 +out_dgrade:
9130 +       di_downgrade_lock(dentry, AuLock_IR);
9131 +out_inval:
9132 +       aufs_read_unlock(dentry, AuLock_IR);
9133 +       AuTraceErr(err);
9134 +       valid = !err;
9135 +out:
9136 +       if (!valid) {
9137 +               AuDbg("%pd invalid, %d\n", dentry, valid);
9138 +               d_drop(dentry);
9139 +       }
9140 +       return valid;
9141 +}
9142 +
9143 +static void aufs_d_release(struct dentry *dentry)
9144 +{
9145 +       if (au_di(dentry)) {
9146 +               au_di_fin(dentry);
9147 +               au_hn_di_reinit(dentry);
9148 +       }
9149 +}
9150 +
9151 +const struct dentry_operations aufs_dop = {
9152 +       .d_revalidate           = aufs_d_revalidate,
9153 +       .d_weak_revalidate      = aufs_d_revalidate,
9154 +       .d_release              = aufs_d_release
9155 +};
9156 +
9157 +/* aufs_dop without d_revalidate */
9158 +const struct dentry_operations aufs_dop_noreval = {
9159 +       .d_release              = aufs_d_release
9160 +};
9161 diff -urN /usr/share/empty/fs/aufs/dentry.h linux/fs/aufs/dentry.h
9162 --- /usr/share/empty/fs/aufs/dentry.h   1970-01-01 01:00:00.000000000 +0100
9163 +++ linux/fs/aufs/dentry.h      2021-11-01 23:48:34.206359262 +0100
9164 @@ -0,0 +1,269 @@
9165 +/* SPDX-License-Identifier: GPL-2.0 */
9166 +/*
9167 + * Copyright (C) 2005-2021 Junjiro R. Okajima
9168 + *
9169 + * This program, aufs is free software; you can redistribute it and/or modify
9170 + * it under the terms of the GNU General Public License as published by
9171 + * the Free Software Foundation; either version 2 of the License, or
9172 + * (at your option) any later version.
9173 + *
9174 + * This program is distributed in the hope that it will be useful,
9175 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
9176 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9177 + * GNU General Public License for more details.
9178 + *
9179 + * You should have received a copy of the GNU General Public License
9180 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
9181 + */
9182 +
9183 +/*
9184 + * lookup and dentry operations
9185 + */
9186 +
9187 +#ifndef __AUFS_DENTRY_H__
9188 +#define __AUFS_DENTRY_H__
9189 +
9190 +#ifdef __KERNEL__
9191 +
9192 +#include <linux/dcache.h>
9193 +#include "dirren.h"
9194 +#include "rwsem.h"
9195 +
9196 +struct au_hdentry {
9197 +       struct dentry           *hd_dentry;
9198 +       aufs_bindex_t           hd_id;
9199 +};
9200 +
9201 +struct au_dinfo {
9202 +       atomic_t                di_generation;
9203 +
9204 +       struct au_rwsem         di_rwsem;
9205 +       aufs_bindex_t           di_btop, di_bbot, di_bwh, di_bdiropq;
9206 +       unsigned char           di_tmpfile; /* to allow the different name */
9207 +       struct au_hdentry       *di_hdentry;
9208 +       struct rcu_head         rcu;
9209 +} ____cacheline_aligned_in_smp;
9210 +
9211 +/* ---------------------------------------------------------------------- */
9212 +
9213 +/* flags for au_lkup_dentry() */
9214 +#define AuLkup_ALLOW_NEG       1
9215 +#define AuLkup_IGNORE_PERM     (1 << 1)
9216 +#define AuLkup_DIRREN          (1 << 2)
9217 +#define au_ftest_lkup(flags, name)     ((flags) & AuLkup_##name)
9218 +#define au_fset_lkup(flags, name) \
9219 +       do { (flags) |= AuLkup_##name; } while (0)
9220 +#define au_fclr_lkup(flags, name) \
9221 +       do { (flags) &= ~AuLkup_##name; } while (0)
9222 +
9223 +#ifndef CONFIG_AUFS_DIRREN
9224 +#undef AuLkup_DIRREN
9225 +#define AuLkup_DIRREN 0
9226 +#endif
9227 +
9228 +struct au_do_lookup_args {
9229 +       unsigned int            flags;
9230 +       mode_t                  type;
9231 +       struct qstr             whname, *name;
9232 +       struct au_dr_lookup     dirren;
9233 +};
9234 +
9235 +/* ---------------------------------------------------------------------- */
9236 +
9237 +/* dentry.c */
9238 +extern const struct dentry_operations aufs_dop, aufs_dop_noreval;
9239 +struct au_branch;
9240 +struct dentry *au_sio_lkup_one(struct user_namespace *userns, struct qstr *name,
9241 +                              struct path *ppath);
9242 +int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
9243 +               struct dentry *h_parent, struct au_branch *br);
9244 +
9245 +int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t btop,
9246 +                  unsigned int flags);
9247 +int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh);
9248 +int au_refresh_dentry(struct dentry *dentry, struct dentry *parent);
9249 +int au_reval_dpath(struct dentry *dentry, unsigned int sigen);
9250 +void au_refresh_dop(struct dentry *dentry, int force_reval);
9251 +
9252 +/* dinfo.c */
9253 +void au_di_init_once(void *_di);
9254 +struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc);
9255 +void au_di_free(struct au_dinfo *dinfo);
9256 +void au_di_swap(struct au_dinfo *a, struct au_dinfo *b);
9257 +void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src);
9258 +int au_di_init(struct dentry *dentry);
9259 +void au_di_fin(struct dentry *dentry);
9260 +int au_di_realloc(struct au_dinfo *dinfo, int nbr, int may_shrink);
9261 +
9262 +void di_read_lock(struct dentry *d, int flags, unsigned int lsc);
9263 +void di_read_unlock(struct dentry *d, int flags);
9264 +void di_downgrade_lock(struct dentry *d, int flags);
9265 +void di_write_lock(struct dentry *d, unsigned int lsc);
9266 +void di_write_unlock(struct dentry *d);
9267 +void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir);
9268 +void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir);
9269 +void di_write_unlock2(struct dentry *d1, struct dentry *d2);
9270 +
9271 +struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex);
9272 +struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex);
9273 +aufs_bindex_t au_dbtail(struct dentry *dentry);
9274 +aufs_bindex_t au_dbtaildir(struct dentry *dentry);
9275 +
9276 +void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
9277 +                  struct dentry *h_dentry);
9278 +int au_digen_test(struct dentry *dentry, unsigned int sigen);
9279 +int au_dbrange_test(struct dentry *dentry);
9280 +void au_update_digen(struct dentry *dentry);
9281 +void au_update_dbrange(struct dentry *dentry, int do_put_zero);
9282 +void au_update_dbtop(struct dentry *dentry);
9283 +void au_update_dbbot(struct dentry *dentry);
9284 +int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry);
9285 +
9286 +/* ---------------------------------------------------------------------- */
9287 +
9288 +static inline struct au_dinfo *au_di(struct dentry *dentry)
9289 +{
9290 +       return dentry->d_fsdata;
9291 +}
9292 +
9293 +/* ---------------------------------------------------------------------- */
9294 +
9295 +/* lock subclass for dinfo */
9296 +enum {
9297 +       AuLsc_DI_CHILD,         /* child first */
9298 +       AuLsc_DI_CHILD2,        /* rename(2), link(2), and cpup at hnotify */
9299 +       AuLsc_DI_CHILD3,        /* copyup dirs */
9300 +       AuLsc_DI_PARENT,
9301 +       AuLsc_DI_PARENT2,
9302 +       AuLsc_DI_PARENT3,
9303 +       AuLsc_DI_TMP            /* temp for replacing dinfo */
9304 +};
9305 +
9306 +/*
9307 + * di_read_lock_child, di_write_lock_child,
9308 + * di_read_lock_child2, di_write_lock_child2,
9309 + * di_read_lock_child3, di_write_lock_child3,
9310 + * di_read_lock_parent, di_write_lock_parent,
9311 + * di_read_lock_parent2, di_write_lock_parent2,
9312 + * di_read_lock_parent3, di_write_lock_parent3,
9313 + */
9314 +#define AuReadLockFunc(name, lsc) \
9315 +static inline void di_read_lock_##name(struct dentry *d, int flags) \
9316 +{ di_read_lock(d, flags, AuLsc_DI_##lsc); }
9317 +
9318 +#define AuWriteLockFunc(name, lsc) \
9319 +static inline void di_write_lock_##name(struct dentry *d) \
9320 +{ di_write_lock(d, AuLsc_DI_##lsc); }
9321 +
9322 +#define AuRWLockFuncs(name, lsc) \
9323 +       AuReadLockFunc(name, lsc) \
9324 +       AuWriteLockFunc(name, lsc)
9325 +
9326 +AuRWLockFuncs(child, CHILD);
9327 +AuRWLockFuncs(child2, CHILD2);
9328 +AuRWLockFuncs(child3, CHILD3);
9329 +AuRWLockFuncs(parent, PARENT);
9330 +AuRWLockFuncs(parent2, PARENT2);
9331 +AuRWLockFuncs(parent3, PARENT3);
9332 +
9333 +#undef AuReadLockFunc
9334 +#undef AuWriteLockFunc
9335 +#undef AuRWLockFuncs
9336 +
9337 +#define DiMustNoWaiters(d)     AuRwMustNoWaiters(&au_di(d)->di_rwsem)
9338 +#define DiMustAnyLock(d)       AuRwMustAnyLock(&au_di(d)->di_rwsem)
9339 +#define DiMustWriteLock(d)     AuRwMustWriteLock(&au_di(d)->di_rwsem)
9340 +
9341 +/* ---------------------------------------------------------------------- */
9342 +
9343 +/* todo: memory barrier? */
9344 +static inline unsigned int au_digen(struct dentry *d)
9345 +{
9346 +       return atomic_read(&au_di(d)->di_generation);
9347 +}
9348 +
9349 +static inline void au_h_dentry_init(struct au_hdentry *hdentry)
9350 +{
9351 +       hdentry->hd_dentry = NULL;
9352 +}
9353 +
9354 +static inline struct au_hdentry *au_hdentry(struct au_dinfo *di,
9355 +                                           aufs_bindex_t bindex)
9356 +{
9357 +       return di->di_hdentry + bindex;
9358 +}
9359 +
9360 +static inline void au_hdput(struct au_hdentry *hd)
9361 +{
9362 +       if (hd)
9363 +               dput(hd->hd_dentry);
9364 +}
9365 +
9366 +static inline aufs_bindex_t au_dbtop(struct dentry *dentry)
9367 +{
9368 +       DiMustAnyLock(dentry);
9369 +       return au_di(dentry)->di_btop;
9370 +}
9371 +
9372 +static inline aufs_bindex_t au_dbbot(struct dentry *dentry)
9373 +{
9374 +       DiMustAnyLock(dentry);
9375 +       return au_di(dentry)->di_bbot;
9376 +}
9377 +
9378 +static inline aufs_bindex_t au_dbwh(struct dentry *dentry)
9379 +{
9380 +       DiMustAnyLock(dentry);
9381 +       return au_di(dentry)->di_bwh;
9382 +}
9383 +
9384 +static inline aufs_bindex_t au_dbdiropq(struct dentry *dentry)
9385 +{
9386 +       DiMustAnyLock(dentry);
9387 +       return au_di(dentry)->di_bdiropq;
9388 +}
9389 +
9390 +/* todo: hard/soft set? */
9391 +static inline void au_set_dbtop(struct dentry *dentry, aufs_bindex_t bindex)
9392 +{
9393 +       DiMustWriteLock(dentry);
9394 +       au_di(dentry)->di_btop = bindex;
9395 +}
9396 +
9397 +static inline void au_set_dbbot(struct dentry *dentry, aufs_bindex_t bindex)
9398 +{
9399 +       DiMustWriteLock(dentry);
9400 +       au_di(dentry)->di_bbot = bindex;
9401 +}
9402 +
9403 +static inline void au_set_dbwh(struct dentry *dentry, aufs_bindex_t bindex)
9404 +{
9405 +       DiMustWriteLock(dentry);
9406 +       /* dbwh can be outside of btop - bbot range */
9407 +       au_di(dentry)->di_bwh = bindex;
9408 +}
9409 +
9410 +static inline void au_set_dbdiropq(struct dentry *dentry, aufs_bindex_t bindex)
9411 +{
9412 +       DiMustWriteLock(dentry);
9413 +       au_di(dentry)->di_bdiropq = bindex;
9414 +}
9415 +
9416 +/* ---------------------------------------------------------------------- */
9417 +
9418 +#ifdef CONFIG_AUFS_HNOTIFY
9419 +static inline void au_digen_dec(struct dentry *d)
9420 +{
9421 +       atomic_dec(&au_di(d)->di_generation);
9422 +}
9423 +
9424 +static inline void au_hn_di_reinit(struct dentry *dentry)
9425 +{
9426 +       dentry->d_fsdata = NULL;
9427 +}
9428 +#else
9429 +AuStubVoid(au_hn_di_reinit, struct dentry *dentry __maybe_unused)
9430 +#endif /* CONFIG_AUFS_HNOTIFY */
9431 +
9432 +#endif /* __KERNEL__ */
9433 +#endif /* __AUFS_DENTRY_H__ */
9434 diff -urN /usr/share/empty/fs/aufs/dinfo.c linux/fs/aufs/dinfo.c
9435 --- /usr/share/empty/fs/aufs/dinfo.c    1970-01-01 01:00:00.000000000 +0100
9436 +++ linux/fs/aufs/dinfo.c       2021-11-01 23:48:34.206359262 +0100
9437 @@ -0,0 +1,554 @@
9438 +// SPDX-License-Identifier: GPL-2.0
9439 +/*
9440 + * Copyright (C) 2005-2021 Junjiro R. Okajima
9441 + *
9442 + * This program, aufs is free software; you can redistribute it and/or modify
9443 + * it under the terms of the GNU General Public License as published by
9444 + * the Free Software Foundation; either version 2 of the License, or
9445 + * (at your option) any later version.
9446 + *
9447 + * This program is distributed in the hope that it will be useful,
9448 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
9449 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9450 + * GNU General Public License for more details.
9451 + *
9452 + * You should have received a copy of the GNU General Public License
9453 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
9454 + */
9455 +
9456 +/*
9457 + * dentry private data
9458 + */
9459 +
9460 +#include "aufs.h"
9461 +
9462 +void au_di_init_once(void *_dinfo)
9463 +{
9464 +       struct au_dinfo *dinfo = _dinfo;
9465 +
9466 +       au_rw_init(&dinfo->di_rwsem);
9467 +}
9468 +
9469 +struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc)
9470 +{
9471 +       struct au_dinfo *dinfo;
9472 +       int nbr, i;
9473 +
9474 +       dinfo = au_cache_alloc_dinfo();
9475 +       if (unlikely(!dinfo))
9476 +               goto out;
9477 +
9478 +       nbr = au_sbbot(sb) + 1;
9479 +       if (nbr <= 0)
9480 +               nbr = 1;
9481 +       dinfo->di_hdentry = kcalloc(nbr, sizeof(*dinfo->di_hdentry), GFP_NOFS);
9482 +       if (dinfo->di_hdentry) {
9483 +               au_rw_write_lock_nested(&dinfo->di_rwsem, lsc);
9484 +               dinfo->di_btop = -1;
9485 +               dinfo->di_bbot = -1;
9486 +               dinfo->di_bwh = -1;
9487 +               dinfo->di_bdiropq = -1;
9488 +               dinfo->di_tmpfile = 0;
9489 +               for (i = 0; i < nbr; i++)
9490 +                       dinfo->di_hdentry[i].hd_id = -1;
9491 +               goto out;
9492 +       }
9493 +
9494 +       au_cache_free_dinfo(dinfo);
9495 +       dinfo = NULL;
9496 +
9497 +out:
9498 +       return dinfo;
9499 +}
9500 +
9501 +void au_di_free(struct au_dinfo *dinfo)
9502 +{
9503 +       struct au_hdentry *p;
9504 +       aufs_bindex_t bbot, bindex;
9505 +
9506 +       /* dentry may not be revalidated */
9507 +       bindex = dinfo->di_btop;
9508 +       if (bindex >= 0) {
9509 +               bbot = dinfo->di_bbot;
9510 +               p = au_hdentry(dinfo, bindex);
9511 +               while (bindex++ <= bbot)
9512 +                       au_hdput(p++);
9513 +       }
9514 +       au_kfree_try_rcu(dinfo->di_hdentry);
9515 +       au_cache_free_dinfo(dinfo);
9516 +}
9517 +
9518 +void au_di_swap(struct au_dinfo *a, struct au_dinfo *b)
9519 +{
9520 +       struct au_hdentry *p;
9521 +       aufs_bindex_t bi;
9522 +
9523 +       AuRwMustWriteLock(&a->di_rwsem);
9524 +       AuRwMustWriteLock(&b->di_rwsem);
9525 +
9526 +#define DiSwap(v, name)                                \
9527 +       do {                                    \
9528 +               v = a->di_##name;               \
9529 +               a->di_##name = b->di_##name;    \
9530 +               b->di_##name = v;               \
9531 +       } while (0)
9532 +
9533 +       DiSwap(p, hdentry);
9534 +       DiSwap(bi, btop);
9535 +       DiSwap(bi, bbot);
9536 +       DiSwap(bi, bwh);
9537 +       DiSwap(bi, bdiropq);
9538 +       /* smp_mb(); */
9539 +
9540 +#undef DiSwap
9541 +}
9542 +
9543 +void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src)
9544 +{
9545 +       AuRwMustWriteLock(&dst->di_rwsem);
9546 +       AuRwMustWriteLock(&src->di_rwsem);
9547 +
9548 +       dst->di_btop = src->di_btop;
9549 +       dst->di_bbot = src->di_bbot;
9550 +       dst->di_bwh = src->di_bwh;
9551 +       dst->di_bdiropq = src->di_bdiropq;
9552 +       /* smp_mb(); */
9553 +}
9554 +
9555 +int au_di_init(struct dentry *dentry)
9556 +{
9557 +       int err;
9558 +       struct super_block *sb;
9559 +       struct au_dinfo *dinfo;
9560 +
9561 +       err = 0;
9562 +       sb = dentry->d_sb;
9563 +       dinfo = au_di_alloc(sb, AuLsc_DI_CHILD);
9564 +       if (dinfo) {
9565 +               atomic_set(&dinfo->di_generation, au_sigen(sb));
9566 +               /* smp_mb(); */ /* atomic_set */
9567 +               dentry->d_fsdata = dinfo;
9568 +       } else
9569 +               err = -ENOMEM;
9570 +
9571 +       return err;
9572 +}
9573 +
9574 +void au_di_fin(struct dentry *dentry)
9575 +{
9576 +       struct au_dinfo *dinfo;
9577 +
9578 +       dinfo = au_di(dentry);
9579 +       AuRwDestroy(&dinfo->di_rwsem);
9580 +       au_di_free(dinfo);
9581 +}
9582 +
9583 +int au_di_realloc(struct au_dinfo *dinfo, int nbr, int may_shrink)
9584 +{
9585 +       int err, sz;
9586 +       struct au_hdentry *hdp;
9587 +
9588 +       AuRwMustWriteLock(&dinfo->di_rwsem);
9589 +
9590 +       err = -ENOMEM;
9591 +       sz = sizeof(*hdp) * (dinfo->di_bbot + 1);
9592 +       if (!sz)
9593 +               sz = sizeof(*hdp);
9594 +       hdp = au_kzrealloc(dinfo->di_hdentry, sz, sizeof(*hdp) * nbr, GFP_NOFS,
9595 +                          may_shrink);
9596 +       if (hdp) {
9597 +               dinfo->di_hdentry = hdp;
9598 +               err = 0;
9599 +       }
9600 +
9601 +       return err;
9602 +}
9603 +
9604 +/* ---------------------------------------------------------------------- */
9605 +
9606 +static void do_ii_write_lock(struct inode *inode, unsigned int lsc)
9607 +{
9608 +       switch (lsc) {
9609 +       case AuLsc_DI_CHILD:
9610 +               ii_write_lock_child(inode);
9611 +               break;
9612 +       case AuLsc_DI_CHILD2:
9613 +               ii_write_lock_child2(inode);
9614 +               break;
9615 +       case AuLsc_DI_CHILD3:
9616 +               ii_write_lock_child3(inode);
9617 +               break;
9618 +       case AuLsc_DI_PARENT:
9619 +               ii_write_lock_parent(inode);
9620 +               break;
9621 +       case AuLsc_DI_PARENT2:
9622 +               ii_write_lock_parent2(inode);
9623 +               break;
9624 +       case AuLsc_DI_PARENT3:
9625 +               ii_write_lock_parent3(inode);
9626 +               break;
9627 +       default:
9628 +               BUG();
9629 +       }
9630 +}
9631 +
9632 +static void do_ii_read_lock(struct inode *inode, unsigned int lsc)
9633 +{
9634 +       switch (lsc) {
9635 +       case AuLsc_DI_CHILD:
9636 +               ii_read_lock_child(inode);
9637 +               break;
9638 +       case AuLsc_DI_CHILD2:
9639 +               ii_read_lock_child2(inode);
9640 +               break;
9641 +       case AuLsc_DI_CHILD3:
9642 +               ii_read_lock_child3(inode);
9643 +               break;
9644 +       case AuLsc_DI_PARENT:
9645 +               ii_read_lock_parent(inode);
9646 +               break;
9647 +       case AuLsc_DI_PARENT2:
9648 +               ii_read_lock_parent2(inode);
9649 +               break;
9650 +       case AuLsc_DI_PARENT3:
9651 +               ii_read_lock_parent3(inode);
9652 +               break;
9653 +       default:
9654 +               BUG();
9655 +       }
9656 +}
9657 +
9658 +void di_read_lock(struct dentry *d, int flags, unsigned int lsc)
9659 +{
9660 +       struct inode *inode;
9661 +
9662 +       au_rw_read_lock_nested(&au_di(d)->di_rwsem, lsc);
9663 +       if (d_really_is_positive(d)) {
9664 +               inode = d_inode(d);
9665 +               if (au_ftest_lock(flags, IW))
9666 +                       do_ii_write_lock(inode, lsc);
9667 +               else if (au_ftest_lock(flags, IR))
9668 +                       do_ii_read_lock(inode, lsc);
9669 +       }
9670 +}
9671 +
9672 +void di_read_unlock(struct dentry *d, int flags)
9673 +{
9674 +       struct inode *inode;
9675 +
9676 +       if (d_really_is_positive(d)) {
9677 +               inode = d_inode(d);
9678 +               if (au_ftest_lock(flags, IW)) {
9679 +                       au_dbg_verify_dinode(d);
9680 +                       ii_write_unlock(inode);
9681 +               } else if (au_ftest_lock(flags, IR)) {
9682 +                       au_dbg_verify_dinode(d);
9683 +                       ii_read_unlock(inode);
9684 +               }
9685 +       }
9686 +       au_rw_read_unlock(&au_di(d)->di_rwsem);
9687 +}
9688 +
9689 +void di_downgrade_lock(struct dentry *d, int flags)
9690 +{
9691 +       if (d_really_is_positive(d) && au_ftest_lock(flags, IR))
9692 +               ii_downgrade_lock(d_inode(d));
9693 +       au_rw_dgrade_lock(&au_di(d)->di_rwsem);
9694 +}
9695 +
9696 +void di_write_lock(struct dentry *d, unsigned int lsc)
9697 +{
9698 +       au_rw_write_lock_nested(&au_di(d)->di_rwsem, lsc);
9699 +       if (d_really_is_positive(d))
9700 +               do_ii_write_lock(d_inode(d), lsc);
9701 +}
9702 +
9703 +void di_write_unlock(struct dentry *d)
9704 +{
9705 +       au_dbg_verify_dinode(d);
9706 +       if (d_really_is_positive(d))
9707 +               ii_write_unlock(d_inode(d));
9708 +       au_rw_write_unlock(&au_di(d)->di_rwsem);
9709 +}
9710 +
9711 +void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir)
9712 +{
9713 +       AuDebugOn(d1 == d2
9714 +                 || d_inode(d1) == d_inode(d2)
9715 +                 || d1->d_sb != d2->d_sb);
9716 +
9717 +       if ((isdir && au_test_subdir(d1, d2))
9718 +           || d1 < d2) {
9719 +               di_write_lock_child(d1);
9720 +               di_write_lock_child2(d2);
9721 +       } else {
9722 +               di_write_lock_child(d2);
9723 +               di_write_lock_child2(d1);
9724 +       }
9725 +}
9726 +
9727 +void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir)
9728 +{
9729 +       AuDebugOn(d1 == d2
9730 +                 || d_inode(d1) == d_inode(d2)
9731 +                 || d1->d_sb != d2->d_sb);
9732 +
9733 +       if ((isdir && au_test_subdir(d1, d2))
9734 +           || d1 < d2) {
9735 +               di_write_lock_parent(d1);
9736 +               di_write_lock_parent2(d2);
9737 +       } else {
9738 +               di_write_lock_parent(d2);
9739 +               di_write_lock_parent2(d1);
9740 +       }
9741 +}
9742 +
9743 +void di_write_unlock2(struct dentry *d1, struct dentry *d2)
9744 +{
9745 +       di_write_unlock(d1);
9746 +       if (d_inode(d1) == d_inode(d2))
9747 +               au_rw_write_unlock(&au_di(d2)->di_rwsem);
9748 +       else
9749 +               di_write_unlock(d2);
9750 +}
9751 +
9752 +/* ---------------------------------------------------------------------- */
9753 +
9754 +struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex)
9755 +{
9756 +       struct dentry *d;
9757 +
9758 +       DiMustAnyLock(dentry);
9759 +
9760 +       if (au_dbtop(dentry) < 0 || bindex < au_dbtop(dentry))
9761 +               return NULL;
9762 +       AuDebugOn(bindex < 0);
9763 +       d = au_hdentry(au_di(dentry), bindex)->hd_dentry;
9764 +       AuDebugOn(d && au_dcount(d) <= 0);
9765 +       return d;
9766 +}
9767 +
9768 +/*
9769 + * extended version of au_h_dptr().
9770 + * returns a hashed and positive (or linkable) h_dentry in bindex, NULL, or
9771 + * error.
9772 + */
9773 +struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex)
9774 +{
9775 +       struct dentry *h_dentry;
9776 +       struct inode *inode, *h_inode;
9777 +
9778 +       AuDebugOn(d_really_is_negative(dentry));
9779 +
9780 +       h_dentry = NULL;
9781 +       if (au_dbtop(dentry) <= bindex
9782 +           && bindex <= au_dbbot(dentry))
9783 +               h_dentry = au_h_dptr(dentry, bindex);
9784 +       if (h_dentry && !au_d_linkable(h_dentry)) {
9785 +               dget(h_dentry);
9786 +               goto out; /* success */
9787 +       }
9788 +
9789 +       inode = d_inode(dentry);
9790 +       AuDebugOn(bindex < au_ibtop(inode));
9791 +       AuDebugOn(au_ibbot(inode) < bindex);
9792 +       h_inode = au_h_iptr(inode, bindex);
9793 +       h_dentry = d_find_alias(h_inode);
9794 +       if (h_dentry) {
9795 +               if (!IS_ERR(h_dentry)) {
9796 +                       if (!au_d_linkable(h_dentry))
9797 +                               goto out; /* success */
9798 +                       dput(h_dentry);
9799 +               } else
9800 +                       goto out;
9801 +       }
9802 +
9803 +       if (au_opt_test(au_mntflags(dentry->d_sb), PLINK)) {
9804 +               h_dentry = au_plink_lkup(inode, bindex);
9805 +               AuDebugOn(!h_dentry);
9806 +               if (!IS_ERR(h_dentry)) {
9807 +                       if (!au_d_hashed_positive(h_dentry))
9808 +                               goto out; /* success */
9809 +                       dput(h_dentry);
9810 +                       h_dentry = NULL;
9811 +               }
9812 +       }
9813 +
9814 +out:
9815 +       AuDbgDentry(h_dentry);
9816 +       return h_dentry;
9817 +}
9818 +
9819 +aufs_bindex_t au_dbtail(struct dentry *dentry)
9820 +{
9821 +       aufs_bindex_t bbot, bwh;
9822 +
9823 +       bbot = au_dbbot(dentry);
9824 +       if (0 <= bbot) {
9825 +               bwh = au_dbwh(dentry);
9826 +               if (!bwh)
9827 +                       return bwh;
9828 +               if (0 < bwh && bwh < bbot)
9829 +                       return bwh - 1;
9830 +       }
9831 +       return bbot;
9832 +}
9833 +
9834 +aufs_bindex_t au_dbtaildir(struct dentry *dentry)
9835 +{
9836 +       aufs_bindex_t bbot, bopq;
9837 +
9838 +       bbot = au_dbtail(dentry);
9839 +       if (0 <= bbot) {
9840 +               bopq = au_dbdiropq(dentry);
9841 +               if (0 <= bopq && bopq < bbot)
9842 +                       bbot = bopq;
9843 +       }
9844 +       return bbot;
9845 +}
9846 +
9847 +/* ---------------------------------------------------------------------- */
9848 +
9849 +void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
9850 +                  struct dentry *h_dentry)
9851 +{
9852 +       struct au_dinfo *dinfo;
9853 +       struct au_hdentry *hd;
9854 +       struct au_branch *br;
9855 +
9856 +       DiMustWriteLock(dentry);
9857 +
9858 +       dinfo = au_di(dentry);
9859 +       hd = au_hdentry(dinfo, bindex);
9860 +       au_hdput(hd);
9861 +       hd->hd_dentry = h_dentry;
9862 +       if (h_dentry) {
9863 +               br = au_sbr(dentry->d_sb, bindex);
9864 +               hd->hd_id = br->br_id;
9865 +       }
9866 +}
9867 +
9868 +int au_dbrange_test(struct dentry *dentry)
9869 +{
9870 +       int err;
9871 +       aufs_bindex_t btop, bbot;
9872 +
9873 +       err = 0;
9874 +       btop = au_dbtop(dentry);
9875 +       bbot = au_dbbot(dentry);
9876 +       if (btop >= 0)
9877 +               AuDebugOn(bbot < 0 && btop > bbot);
9878 +       else {
9879 +               err = -EIO;
9880 +               AuDebugOn(bbot >= 0);
9881 +       }
9882 +
9883 +       return err;
9884 +}
9885 +
9886 +int au_digen_test(struct dentry *dentry, unsigned int sigen)
9887 +{
9888 +       int err;
9889 +
9890 +       err = 0;
9891 +       if (unlikely(au_digen(dentry) != sigen
9892 +                    || au_iigen_test(d_inode(dentry), sigen)))
9893 +               err = -EIO;
9894 +
9895 +       return err;
9896 +}
9897 +
9898 +void au_update_digen(struct dentry *dentry)
9899 +{
9900 +       atomic_set(&au_di(dentry)->di_generation, au_sigen(dentry->d_sb));
9901 +       /* smp_mb(); */ /* atomic_set */
9902 +}
9903 +
9904 +void au_update_dbrange(struct dentry *dentry, int do_put_zero)
9905 +{
9906 +       struct au_dinfo *dinfo;
9907 +       struct dentry *h_d;
9908 +       struct au_hdentry *hdp;
9909 +       aufs_bindex_t bindex, bbot;
9910 +
9911 +       DiMustWriteLock(dentry);
9912 +
9913 +       dinfo = au_di(dentry);
9914 +       if (!dinfo || dinfo->di_btop < 0)
9915 +               return;
9916 +
9917 +       if (do_put_zero) {
9918 +               bbot = dinfo->di_bbot;
9919 +               bindex = dinfo->di_btop;
9920 +               hdp = au_hdentry(dinfo, bindex);
9921 +               for (; bindex <= bbot; bindex++, hdp++) {
9922 +                       h_d = hdp->hd_dentry;
9923 +                       if (h_d && d_is_negative(h_d))
9924 +                               au_set_h_dptr(dentry, bindex, NULL);
9925 +               }
9926 +       }
9927 +
9928 +       dinfo->di_btop = 0;
9929 +       hdp = au_hdentry(dinfo, dinfo->di_btop);
9930 +       for (; dinfo->di_btop <= dinfo->di_bbot; dinfo->di_btop++, hdp++)
9931 +               if (hdp->hd_dentry)
9932 +                       break;
9933 +       if (dinfo->di_btop > dinfo->di_bbot) {
9934 +               dinfo->di_btop = -1;
9935 +               dinfo->di_bbot = -1;
9936 +               return;
9937 +       }
9938 +
9939 +       hdp = au_hdentry(dinfo, dinfo->di_bbot);
9940 +       for (; dinfo->di_bbot >= 0; dinfo->di_bbot--, hdp--)
9941 +               if (hdp->hd_dentry)
9942 +                       break;
9943 +       AuDebugOn(dinfo->di_btop > dinfo->di_bbot || dinfo->di_bbot < 0);
9944 +}
9945 +
9946 +void au_update_dbtop(struct dentry *dentry)
9947 +{
9948 +       aufs_bindex_t bindex, bbot;
9949 +       struct dentry *h_dentry;
9950 +
9951 +       bbot = au_dbbot(dentry);
9952 +       for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++) {
9953 +               h_dentry = au_h_dptr(dentry, bindex);
9954 +               if (!h_dentry)
9955 +                       continue;
9956 +               if (d_is_positive(h_dentry)) {
9957 +                       au_set_dbtop(dentry, bindex);
9958 +                       return;
9959 +               }
9960 +               au_set_h_dptr(dentry, bindex, NULL);
9961 +       }
9962 +}
9963 +
9964 +void au_update_dbbot(struct dentry *dentry)
9965 +{
9966 +       aufs_bindex_t bindex, btop;
9967 +       struct dentry *h_dentry;
9968 +
9969 +       btop = au_dbtop(dentry);
9970 +       for (bindex = au_dbbot(dentry); bindex >= btop; bindex--) {
9971 +               h_dentry = au_h_dptr(dentry, bindex);
9972 +               if (!h_dentry)
9973 +                       continue;
9974 +               if (d_is_positive(h_dentry)) {
9975 +                       au_set_dbbot(dentry, bindex);
9976 +                       return;
9977 +               }
9978 +               au_set_h_dptr(dentry, bindex, NULL);
9979 +       }
9980 +}
9981 +
9982 +int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry)
9983 +{
9984 +       aufs_bindex_t bindex, bbot;
9985 +
9986 +       bbot = au_dbbot(dentry);
9987 +       for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++)
9988 +               if (au_h_dptr(dentry, bindex) == h_dentry)
9989 +                       return bindex;
9990 +       return -1;
9991 +}
9992 diff -urN /usr/share/empty/fs/aufs/dir.c linux/fs/aufs/dir.c
9993 --- /usr/share/empty/fs/aufs/dir.c      1970-01-01 01:00:00.000000000 +0100
9994 +++ linux/fs/aufs/dir.c 2021-11-01 23:48:34.206359262 +0100
9995 @@ -0,0 +1,765 @@
9996 +// SPDX-License-Identifier: GPL-2.0
9997 +/*
9998 + * Copyright (C) 2005-2021 Junjiro R. Okajima
9999 + *
10000 + * This program, aufs is free software; you can redistribute it and/or modify
10001 + * it under the terms of the GNU General Public License as published by
10002 + * the Free Software Foundation; either version 2 of the License, or
10003 + * (at your option) any later version.
10004 + *
10005 + * This program is distributed in the hope that it will be useful,
10006 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
10007 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10008 + * GNU General Public License for more details.
10009 + *
10010 + * You should have received a copy of the GNU General Public License
10011 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
10012 + */
10013 +
10014 +/*
10015 + * directory operations
10016 + */
10017 +
10018 +#include <linux/fs_stack.h>
10019 +#include <linux/iversion.h>
10020 +#include "aufs.h"
10021 +
10022 +void au_add_nlink(struct inode *dir, struct inode *h_dir)
10023 +{
10024 +       unsigned int nlink;
10025 +
10026 +       AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
10027 +
10028 +       nlink = dir->i_nlink;
10029 +       nlink += h_dir->i_nlink - 2;
10030 +       if (h_dir->i_nlink < 2)
10031 +               nlink += 2;
10032 +       smp_mb(); /* for i_nlink */
10033 +       /* 0 can happen in revaliding */
10034 +       set_nlink(dir, nlink);
10035 +}
10036 +
10037 +void au_sub_nlink(struct inode *dir, struct inode *h_dir)
10038 +{
10039 +       unsigned int nlink;
10040 +
10041 +       AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
10042 +
10043 +       nlink = dir->i_nlink;
10044 +       nlink -= h_dir->i_nlink - 2;
10045 +       if (h_dir->i_nlink < 2)
10046 +               nlink -= 2;
10047 +       smp_mb(); /* for i_nlink */
10048 +       /* nlink == 0 means the branch-fs is broken */
10049 +       set_nlink(dir, nlink);
10050 +}
10051 +
10052 +loff_t au_dir_size(struct file *file, struct dentry *dentry)
10053 +{
10054 +       loff_t sz;
10055 +       aufs_bindex_t bindex, bbot;
10056 +       struct file *h_file;
10057 +       struct dentry *h_dentry;
10058 +
10059 +       sz = 0;
10060 +       if (file) {
10061 +               AuDebugOn(!d_is_dir(file->f_path.dentry));
10062 +
10063 +               bbot = au_fbbot_dir(file);
10064 +               for (bindex = au_fbtop(file);
10065 +                    bindex <= bbot && sz < KMALLOC_MAX_SIZE;
10066 +                    bindex++) {
10067 +                       h_file = au_hf_dir(file, bindex);
10068 +                       if (h_file && file_inode(h_file))
10069 +                               sz += vfsub_f_size_read(h_file);
10070 +               }
10071 +       } else {
10072 +               AuDebugOn(!dentry);
10073 +               AuDebugOn(!d_is_dir(dentry));
10074 +
10075 +               bbot = au_dbtaildir(dentry);
10076 +               for (bindex = au_dbtop(dentry);
10077 +                    bindex <= bbot && sz < KMALLOC_MAX_SIZE;
10078 +                    bindex++) {
10079 +                       h_dentry = au_h_dptr(dentry, bindex);
10080 +                       if (h_dentry && d_is_positive(h_dentry))
10081 +                               sz += i_size_read(d_inode(h_dentry));
10082 +               }
10083 +       }
10084 +       if (sz < KMALLOC_MAX_SIZE)
10085 +               sz = roundup_pow_of_two(sz);
10086 +       if (sz > KMALLOC_MAX_SIZE)
10087 +               sz = KMALLOC_MAX_SIZE;
10088 +       else if (sz < NAME_MAX) {
10089 +               BUILD_BUG_ON(AUFS_RDBLK_DEF < NAME_MAX);
10090 +               sz = AUFS_RDBLK_DEF;
10091 +       }
10092 +       return sz;
10093 +}
10094 +
10095 +struct au_dir_ts_arg {
10096 +       struct dentry *dentry;
10097 +       aufs_bindex_t brid;
10098 +};
10099 +
10100 +static void au_do_dir_ts(void *arg)
10101 +{
10102 +       struct au_dir_ts_arg *a = arg;
10103 +       struct au_dtime dt;
10104 +       struct path h_path;
10105 +       struct inode *dir, *h_dir;
10106 +       struct super_block *sb;
10107 +       struct au_branch *br;
10108 +       struct au_hinode *hdir;
10109 +       int err;
10110 +       aufs_bindex_t btop, bindex;
10111 +
10112 +       sb = a->dentry->d_sb;
10113 +       if (d_really_is_negative(a->dentry))
10114 +               goto out;
10115 +       /* no dir->i_mutex lock */
10116 +       aufs_read_lock(a->dentry, AuLock_DW); /* noflush */
10117 +
10118 +       dir = d_inode(a->dentry);
10119 +       btop = au_ibtop(dir);
10120 +       bindex = au_br_index(sb, a->brid);
10121 +       if (bindex < btop)
10122 +               goto out_unlock;
10123 +
10124 +       br = au_sbr(sb, bindex);
10125 +       h_path.dentry = au_h_dptr(a->dentry, bindex);
10126 +       if (!h_path.dentry)
10127 +               goto out_unlock;
10128 +       h_path.mnt = au_br_mnt(br);
10129 +       au_dtime_store(&dt, a->dentry, &h_path);
10130 +
10131 +       br = au_sbr(sb, btop);
10132 +       if (!au_br_writable(br->br_perm))
10133 +               goto out_unlock;
10134 +       h_path.dentry = au_h_dptr(a->dentry, btop);
10135 +       h_path.mnt = au_br_mnt(br);
10136 +       err = vfsub_mnt_want_write(h_path.mnt);
10137 +       if (err)
10138 +               goto out_unlock;
10139 +       hdir = au_hi(dir, btop);
10140 +       au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
10141 +       h_dir = au_h_iptr(dir, btop);
10142 +       if (h_dir->i_nlink
10143 +           && timespec64_compare(&h_dir->i_mtime, &dt.dt_mtime) < 0) {
10144 +               dt.dt_h_path = h_path;
10145 +               au_dtime_revert(&dt);
10146 +       }
10147 +       au_hn_inode_unlock(hdir);
10148 +       vfsub_mnt_drop_write(h_path.mnt);
10149 +       au_cpup_attr_timesizes(dir);
10150 +
10151 +out_unlock:
10152 +       aufs_read_unlock(a->dentry, AuLock_DW);
10153 +out:
10154 +       dput(a->dentry);
10155 +       au_nwt_done(&au_sbi(sb)->si_nowait);
10156 +       au_kfree_try_rcu(arg);
10157 +}
10158 +
10159 +void au_dir_ts(struct inode *dir, aufs_bindex_t bindex)
10160 +{
10161 +       int perm, wkq_err;
10162 +       aufs_bindex_t btop;
10163 +       struct au_dir_ts_arg *arg;
10164 +       struct dentry *dentry;
10165 +       struct super_block *sb;
10166 +
10167 +       IMustLock(dir);
10168 +
10169 +       dentry = d_find_any_alias(dir);
10170 +       AuDebugOn(!dentry);
10171 +       sb = dentry->d_sb;
10172 +       btop = au_ibtop(dir);
10173 +       if (btop == bindex) {
10174 +               au_cpup_attr_timesizes(dir);
10175 +               goto out;
10176 +       }
10177 +
10178 +       perm = au_sbr_perm(sb, btop);
10179 +       if (!au_br_writable(perm))
10180 +               goto out;
10181 +
10182 +       arg = kmalloc(sizeof(*arg), GFP_NOFS);
10183 +       if (!arg)
10184 +               goto out;
10185 +
10186 +       arg->dentry = dget(dentry); /* will be dput-ted by au_do_dir_ts() */
10187 +       arg->brid = au_sbr_id(sb, bindex);
10188 +       wkq_err = au_wkq_nowait(au_do_dir_ts, arg, sb, /*flags*/0);
10189 +       if (unlikely(wkq_err)) {
10190 +               pr_err("wkq %d\n", wkq_err);
10191 +               dput(dentry);
10192 +               au_kfree_try_rcu(arg);
10193 +       }
10194 +
10195 +out:
10196 +       dput(dentry);
10197 +}
10198 +
10199 +/* ---------------------------------------------------------------------- */
10200 +
10201 +static int reopen_dir(struct file *file)
10202 +{
10203 +       int err;
10204 +       unsigned int flags;
10205 +       aufs_bindex_t bindex, btail, btop;
10206 +       struct dentry *dentry, *h_dentry;
10207 +       struct file *h_file;
10208 +
10209 +       /* open all lower dirs */
10210 +       dentry = file->f_path.dentry;
10211 +       btop = au_dbtop(dentry);
10212 +       for (bindex = au_fbtop(file); bindex < btop; bindex++)
10213 +               au_set_h_fptr(file, bindex, NULL);
10214 +       au_set_fbtop(file, btop);
10215 +
10216 +       btail = au_dbtaildir(dentry);
10217 +       for (bindex = au_fbbot_dir(file); btail < bindex; bindex--)
10218 +               au_set_h_fptr(file, bindex, NULL);
10219 +       au_set_fbbot_dir(file, btail);
10220 +
10221 +       flags = vfsub_file_flags(file);
10222 +       for (bindex = btop; bindex <= btail; bindex++) {
10223 +               h_dentry = au_h_dptr(dentry, bindex);
10224 +               if (!h_dentry)
10225 +                       continue;
10226 +               h_file = au_hf_dir(file, bindex);
10227 +               if (h_file)
10228 +                       continue;
10229 +
10230 +               h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
10231 +               err = PTR_ERR(h_file);
10232 +               if (IS_ERR(h_file))
10233 +                       goto out; /* close all? */
10234 +               au_set_h_fptr(file, bindex, h_file);
10235 +       }
10236 +       au_update_figen(file);
10237 +       /* todo: necessary? */
10238 +       /* file->f_ra = h_file->f_ra; */
10239 +       err = 0;
10240 +
10241 +out:
10242 +       return err;
10243 +}
10244 +
10245 +static int do_open_dir(struct file *file, int flags, struct file *h_file)
10246 +{
10247 +       int err;
10248 +       aufs_bindex_t bindex, btail;
10249 +       struct dentry *dentry, *h_dentry;
10250 +       struct vfsmount *mnt;
10251 +
10252 +       FiMustWriteLock(file);
10253 +       AuDebugOn(h_file);
10254 +
10255 +       err = 0;
10256 +       mnt = file->f_path.mnt;
10257 +       dentry = file->f_path.dentry;
10258 +       file->f_version = inode_query_iversion(d_inode(dentry));
10259 +       bindex = au_dbtop(dentry);
10260 +       au_set_fbtop(file, bindex);
10261 +       btail = au_dbtaildir(dentry);
10262 +       au_set_fbbot_dir(file, btail);
10263 +       for (; !err && bindex <= btail; bindex++) {
10264 +               h_dentry = au_h_dptr(dentry, bindex);
10265 +               if (!h_dentry)
10266 +                       continue;
10267 +
10268 +               err = vfsub_test_mntns(mnt, h_dentry->d_sb);
10269 +               if (unlikely(err))
10270 +                       break;
10271 +               h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
10272 +               if (IS_ERR(h_file)) {
10273 +                       err = PTR_ERR(h_file);
10274 +                       break;
10275 +               }
10276 +               au_set_h_fptr(file, bindex, h_file);
10277 +       }
10278 +       au_update_figen(file);
10279 +       /* todo: necessary? */
10280 +       /* file->f_ra = h_file->f_ra; */
10281 +       if (!err)
10282 +               return 0; /* success */
10283 +
10284 +       /* close all */
10285 +       for (bindex = au_fbtop(file); bindex <= btail; bindex++)
10286 +               au_set_h_fptr(file, bindex, NULL);
10287 +       au_set_fbtop(file, -1);
10288 +       au_set_fbbot_dir(file, -1);
10289 +
10290 +       return err;
10291 +}
10292 +
10293 +static int aufs_open_dir(struct inode *inode __maybe_unused,
10294 +                        struct file *file)
10295 +{
10296 +       int err;
10297 +       struct super_block *sb;
10298 +       struct au_fidir *fidir;
10299 +
10300 +       err = -ENOMEM;
10301 +       sb = file->f_path.dentry->d_sb;
10302 +       si_read_lock(sb, AuLock_FLUSH);
10303 +       fidir = au_fidir_alloc(sb);
10304 +       if (fidir) {
10305 +               struct au_do_open_args args = {
10306 +                       .open   = do_open_dir,
10307 +                       .fidir  = fidir
10308 +               };
10309 +               err = au_do_open(file, &args);
10310 +               if (unlikely(err))
10311 +                       au_kfree_rcu(fidir);
10312 +       }
10313 +       si_read_unlock(sb);
10314 +       return err;
10315 +}
10316 +
10317 +static int aufs_release_dir(struct inode *inode __maybe_unused,
10318 +                           struct file *file)
10319 +{
10320 +       struct au_vdir *vdir_cache;
10321 +       struct au_finfo *finfo;
10322 +       struct au_fidir *fidir;
10323 +       struct au_hfile *hf;
10324 +       aufs_bindex_t bindex, bbot;
10325 +
10326 +       finfo = au_fi(file);
10327 +       fidir = finfo->fi_hdir;
10328 +       if (fidir) {
10329 +               au_hbl_del(&finfo->fi_hlist,
10330 +                          &au_sbi(file->f_path.dentry->d_sb)->si_files);
10331 +               vdir_cache = fidir->fd_vdir_cache; /* lock-free */
10332 +               if (vdir_cache)
10333 +                       au_vdir_free(vdir_cache);
10334 +
10335 +               bindex = finfo->fi_btop;
10336 +               if (bindex >= 0) {
10337 +                       hf = fidir->fd_hfile + bindex;
10338 +                       /*
10339 +                        * calls fput() instead of filp_close(),
10340 +                        * since no dnotify or lock for the lower file.
10341 +                        */
10342 +                       bbot = fidir->fd_bbot;
10343 +                       for (; bindex <= bbot; bindex++, hf++)
10344 +                               if (hf->hf_file)
10345 +                                       au_hfput(hf, /*execed*/0);
10346 +               }
10347 +               au_kfree_rcu(fidir);
10348 +               finfo->fi_hdir = NULL;
10349 +       }
10350 +       au_finfo_fin(file);
10351 +       return 0;
10352 +}
10353 +
10354 +/* ---------------------------------------------------------------------- */
10355 +
10356 +static int au_do_flush_dir(struct file *file, fl_owner_t id)
10357 +{
10358 +       int err;
10359 +       aufs_bindex_t bindex, bbot;
10360 +       struct file *h_file;
10361 +
10362 +       err = 0;
10363 +       bbot = au_fbbot_dir(file);
10364 +       for (bindex = au_fbtop(file); !err && bindex <= bbot; bindex++) {
10365 +               h_file = au_hf_dir(file, bindex);
10366 +               if (h_file)
10367 +                       err = vfsub_flush(h_file, id);
10368 +       }
10369 +       return err;
10370 +}
10371 +
10372 +static int aufs_flush_dir(struct file *file, fl_owner_t id)
10373 +{
10374 +       return au_do_flush(file, id, au_do_flush_dir);
10375 +}
10376 +
10377 +/* ---------------------------------------------------------------------- */
10378 +
10379 +static int au_do_fsync_dir_no_file(struct dentry *dentry, int datasync)
10380 +{
10381 +       int err;
10382 +       aufs_bindex_t bbot, bindex;
10383 +       struct inode *inode;
10384 +       struct super_block *sb;
10385 +
10386 +       err = 0;
10387 +       sb = dentry->d_sb;
10388 +       inode = d_inode(dentry);
10389 +       IMustLock(inode);
10390 +       bbot = au_dbbot(dentry);
10391 +       for (bindex = au_dbtop(dentry); !err && bindex <= bbot; bindex++) {
10392 +               struct path h_path;
10393 +
10394 +               if (au_test_ro(sb, bindex, inode))
10395 +                       continue;
10396 +               h_path.dentry = au_h_dptr(dentry, bindex);
10397 +               if (!h_path.dentry)
10398 +                       continue;
10399 +
10400 +               h_path.mnt = au_sbr_mnt(sb, bindex);
10401 +               err = vfsub_fsync(NULL, &h_path, datasync);
10402 +       }
10403 +
10404 +       return err;
10405 +}
10406 +
10407 +static int au_do_fsync_dir(struct file *file, int datasync)
10408 +{
10409 +       int err;
10410 +       aufs_bindex_t bbot, bindex;
10411 +       struct file *h_file;
10412 +       struct super_block *sb;
10413 +       struct inode *inode;
10414 +
10415 +       err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1, /*fi_lsc*/0);
10416 +       if (unlikely(err))
10417 +               goto out;
10418 +
10419 +       inode = file_inode(file);
10420 +       sb = inode->i_sb;
10421 +       bbot = au_fbbot_dir(file);
10422 +       for (bindex = au_fbtop(file); !err && bindex <= bbot; bindex++) {
10423 +               h_file = au_hf_dir(file, bindex);
10424 +               if (!h_file || au_test_ro(sb, bindex, inode))
10425 +                       continue;
10426 +
10427 +               err = vfsub_fsync(h_file, &h_file->f_path, datasync);
10428 +       }
10429 +
10430 +out:
10431 +       return err;
10432 +}
10433 +
10434 +/*
10435 + * @file may be NULL
10436 + */
10437 +static int aufs_fsync_dir(struct file *file, loff_t start, loff_t end,
10438 +                         int datasync)
10439 +{
10440 +       int err;
10441 +       struct dentry *dentry;
10442 +       struct inode *inode;
10443 +       struct super_block *sb;
10444 +
10445 +       err = 0;
10446 +       dentry = file->f_path.dentry;
10447 +       inode = d_inode(dentry);
10448 +       inode_lock(inode);
10449 +       sb = dentry->d_sb;
10450 +       si_noflush_read_lock(sb);
10451 +       if (file)
10452 +               err = au_do_fsync_dir(file, datasync);
10453 +       else {
10454 +               di_write_lock_child(dentry);
10455 +               err = au_do_fsync_dir_no_file(dentry, datasync);
10456 +       }
10457 +       au_cpup_attr_timesizes(inode);
10458 +       di_write_unlock(dentry);
10459 +       if (file)
10460 +               fi_write_unlock(file);
10461 +
10462 +       si_read_unlock(sb);
10463 +       inode_unlock(inode);
10464 +       return err;
10465 +}
10466 +
10467 +/* ---------------------------------------------------------------------- */
10468 +
10469 +static int aufs_iterate_shared(struct file *file, struct dir_context *ctx)
10470 +{
10471 +       int err;
10472 +       struct dentry *dentry;
10473 +       struct inode *inode, *h_inode;
10474 +       struct super_block *sb;
10475 +
10476 +       AuDbg("%pD, ctx{%ps, %llu}\n", file, ctx->actor, ctx->pos);
10477 +
10478 +       dentry = file->f_path.dentry;
10479 +       inode = d_inode(dentry);
10480 +       IMustLock(inode);
10481 +
10482 +       sb = dentry->d_sb;
10483 +       si_read_lock(sb, AuLock_FLUSH);
10484 +       err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1, /*fi_lsc*/0);
10485 +       if (unlikely(err))
10486 +               goto out;
10487 +       err = au_alive_dir(dentry);
10488 +       if (!err)
10489 +               err = au_vdir_init(file);
10490 +       di_downgrade_lock(dentry, AuLock_IR);
10491 +       if (unlikely(err))
10492 +               goto out_unlock;
10493 +
10494 +       h_inode = au_h_iptr(inode, au_ibtop(inode));
10495 +       if (!au_test_nfsd()) {
10496 +               err = au_vdir_fill_de(file, ctx);
10497 +               fsstack_copy_attr_atime(inode, h_inode);
10498 +       } else {
10499 +               /*
10500 +                * nfsd filldir may call lookup_one_len(), vfs_getattr(),
10501 +                * encode_fh() and others.
10502 +                */
10503 +               atomic_inc(&h_inode->i_count);
10504 +               di_read_unlock(dentry, AuLock_IR);
10505 +               si_read_unlock(sb);
10506 +               err = au_vdir_fill_de(file, ctx);
10507 +               fsstack_copy_attr_atime(inode, h_inode);
10508 +               fi_write_unlock(file);
10509 +               iput(h_inode);
10510 +
10511 +               AuTraceErr(err);
10512 +               return err;
10513 +       }
10514 +
10515 +out_unlock:
10516 +       di_read_unlock(dentry, AuLock_IR);
10517 +       fi_write_unlock(file);
10518 +out:
10519 +       si_read_unlock(sb);
10520 +       return err;
10521 +}
10522 +
10523 +/* ---------------------------------------------------------------------- */
10524 +
10525 +#define AuTestEmpty_WHONLY     1
10526 +#define AuTestEmpty_CALLED     (1 << 1)
10527 +#define AuTestEmpty_SHWH       (1 << 2)
10528 +#define au_ftest_testempty(flags, name)        ((flags) & AuTestEmpty_##name)
10529 +#define au_fset_testempty(flags, name) \
10530 +       do { (flags) |= AuTestEmpty_##name; } while (0)
10531 +#define au_fclr_testempty(flags, name) \
10532 +       do { (flags) &= ~AuTestEmpty_##name; } while (0)
10533 +
10534 +#ifndef CONFIG_AUFS_SHWH
10535 +#undef AuTestEmpty_SHWH
10536 +#define AuTestEmpty_SHWH       0
10537 +#endif
10538 +
10539 +struct test_empty_arg {
10540 +       struct dir_context ctx;
10541 +       struct au_nhash *whlist;
10542 +       unsigned int flags;
10543 +       int err;
10544 +       aufs_bindex_t bindex;
10545 +};
10546 +
10547 +static int test_empty_cb(struct dir_context *ctx, const char *__name,
10548 +                        int namelen, loff_t offset __maybe_unused, u64 ino,
10549 +                        unsigned int d_type)
10550 +{
10551 +       struct test_empty_arg *arg = container_of(ctx, struct test_empty_arg,
10552 +                                                 ctx);
10553 +       char *name = (void *)__name;
10554 +
10555 +       arg->err = 0;
10556 +       au_fset_testempty(arg->flags, CALLED);
10557 +       /* smp_mb(); */
10558 +       if (name[0] == '.'
10559 +           && (namelen == 1 || (name[1] == '.' && namelen == 2)))
10560 +               goto out; /* success */
10561 +
10562 +       if (namelen <= AUFS_WH_PFX_LEN
10563 +           || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
10564 +               if (au_ftest_testempty(arg->flags, WHONLY)
10565 +                   && !au_nhash_test_known_wh(arg->whlist, name, namelen))
10566 +                       arg->err = -ENOTEMPTY;
10567 +               goto out;
10568 +       }
10569 +
10570 +       name += AUFS_WH_PFX_LEN;
10571 +       namelen -= AUFS_WH_PFX_LEN;
10572 +       if (!au_nhash_test_known_wh(arg->whlist, name, namelen))
10573 +               arg->err = au_nhash_append_wh
10574 +                       (arg->whlist, name, namelen, ino, d_type, arg->bindex,
10575 +                        au_ftest_testempty(arg->flags, SHWH));
10576 +
10577 +out:
10578 +       /* smp_mb(); */
10579 +       AuTraceErr(arg->err);
10580 +       return arg->err;
10581 +}
10582 +
10583 +static int do_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
10584 +{
10585 +       int err;
10586 +       struct file *h_file;
10587 +       struct au_branch *br;
10588 +
10589 +       h_file = au_h_open(dentry, arg->bindex,
10590 +                          O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_LARGEFILE,
10591 +                          /*file*/NULL, /*force_wr*/0);
10592 +       err = PTR_ERR(h_file);
10593 +       if (IS_ERR(h_file))
10594 +               goto out;
10595 +
10596 +       err = 0;
10597 +       if (!au_opt_test(au_mntflags(dentry->d_sb), UDBA_NONE)
10598 +           && !file_inode(h_file)->i_nlink)
10599 +               goto out_put;
10600 +
10601 +       do {
10602 +               arg->err = 0;
10603 +               au_fclr_testempty(arg->flags, CALLED);
10604 +               /* smp_mb(); */
10605 +               err = vfsub_iterate_dir(h_file, &arg->ctx);
10606 +               if (err >= 0)
10607 +                       err = arg->err;
10608 +       } while (!err && au_ftest_testempty(arg->flags, CALLED));
10609 +
10610 +out_put:
10611 +       fput(h_file);
10612 +       br = au_sbr(dentry->d_sb, arg->bindex);
10613 +       au_lcnt_dec(&br->br_nfiles);
10614 +out:
10615 +       return err;
10616 +}
10617 +
10618 +struct do_test_empty_args {
10619 +       int *errp;
10620 +       struct dentry *dentry;
10621 +       struct test_empty_arg *arg;
10622 +};
10623 +
10624 +static void call_do_test_empty(void *args)
10625 +{
10626 +       struct do_test_empty_args *a = args;
10627 +       *a->errp = do_test_empty(a->dentry, a->arg);
10628 +}
10629 +
10630 +static int sio_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
10631 +{
10632 +       int err, wkq_err;
10633 +       struct dentry *h_dentry;
10634 +       struct inode *h_inode;
10635 +       struct user_namespace *h_userns;
10636 +
10637 +       h_userns = au_sbr_userns(dentry->d_sb, arg->bindex);
10638 +       h_dentry = au_h_dptr(dentry, arg->bindex);
10639 +       h_inode = d_inode(h_dentry);
10640 +       /* todo: i_mode changes anytime? */
10641 +       inode_lock_shared_nested(h_inode, AuLsc_I_CHILD);
10642 +       err = au_test_h_perm_sio(h_userns, h_inode, MAY_EXEC | MAY_READ);
10643 +       inode_unlock_shared(h_inode);
10644 +       if (!err)
10645 +               err = do_test_empty(dentry, arg);
10646 +       else {
10647 +               struct do_test_empty_args args = {
10648 +                       .errp   = &err,
10649 +                       .dentry = dentry,
10650 +                       .arg    = arg
10651 +               };
10652 +               unsigned int flags = arg->flags;
10653 +
10654 +               wkq_err = au_wkq_wait(call_do_test_empty, &args);
10655 +               if (unlikely(wkq_err))
10656 +                       err = wkq_err;
10657 +               arg->flags = flags;
10658 +       }
10659 +
10660 +       return err;
10661 +}
10662 +
10663 +int au_test_empty_lower(struct dentry *dentry)
10664 +{
10665 +       int err;
10666 +       unsigned int rdhash;
10667 +       aufs_bindex_t bindex, btop, btail;
10668 +       struct au_nhash whlist;
10669 +       struct test_empty_arg arg = {
10670 +               .ctx = {
10671 +                       .actor = test_empty_cb
10672 +               }
10673 +       };
10674 +       int (*test_empty)(struct dentry *dentry, struct test_empty_arg *arg);
10675 +
10676 +       SiMustAnyLock(dentry->d_sb);
10677 +
10678 +       rdhash = au_sbi(dentry->d_sb)->si_rdhash;
10679 +       if (!rdhash)
10680 +               rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, dentry));
10681 +       err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
10682 +       if (unlikely(err))
10683 +               goto out;
10684 +
10685 +       arg.flags = 0;
10686 +       arg.whlist = &whlist;
10687 +       btop = au_dbtop(dentry);
10688 +       if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
10689 +               au_fset_testempty(arg.flags, SHWH);
10690 +       test_empty = do_test_empty;
10691 +       if (au_opt_test(au_mntflags(dentry->d_sb), DIRPERM1))
10692 +               test_empty = sio_test_empty;
10693 +       arg.bindex = btop;
10694 +       err = test_empty(dentry, &arg);
10695 +       if (unlikely(err))
10696 +               goto out_whlist;
10697 +
10698 +       au_fset_testempty(arg.flags, WHONLY);
10699 +       btail = au_dbtaildir(dentry);
10700 +       for (bindex = btop + 1; !err && bindex <= btail; bindex++) {
10701 +               struct dentry *h_dentry;
10702 +
10703 +               h_dentry = au_h_dptr(dentry, bindex);
10704 +               if (h_dentry && d_is_positive(h_dentry)) {
10705 +                       arg.bindex = bindex;
10706 +                       err = test_empty(dentry, &arg);
10707 +               }
10708 +       }
10709 +
10710 +out_whlist:
10711 +       au_nhash_wh_free(&whlist);
10712 +out:
10713 +       return err;
10714 +}
10715 +
10716 +int au_test_empty(struct dentry *dentry, struct au_nhash *whlist)
10717 +{
10718 +       int err;
10719 +       struct test_empty_arg arg = {
10720 +               .ctx = {
10721 +                       .actor = test_empty_cb
10722 +               }
10723 +       };
10724 +       aufs_bindex_t bindex, btail;
10725 +
10726 +       err = 0;
10727 +       arg.whlist = whlist;
10728 +       arg.flags = AuTestEmpty_WHONLY;
10729 +       if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
10730 +               au_fset_testempty(arg.flags, SHWH);
10731 +       btail = au_dbtaildir(dentry);
10732 +       for (bindex = au_dbtop(dentry); !err && bindex <= btail; bindex++) {
10733 +               struct dentry *h_dentry;
10734 +
10735 +               h_dentry = au_h_dptr(dentry, bindex);
10736 +               if (h_dentry && d_is_positive(h_dentry)) {
10737 +                       arg.bindex = bindex;
10738 +                       err = sio_test_empty(dentry, &arg);
10739 +               }
10740 +       }
10741 +
10742 +       return err;
10743 +}
10744 +
10745 +/* ---------------------------------------------------------------------- */
10746 +
10747 +const struct file_operations aufs_dir_fop = {
10748 +       .owner          = THIS_MODULE,
10749 +       .llseek         = default_llseek,
10750 +       .read           = generic_read_dir,
10751 +       .iterate_shared = aufs_iterate_shared,
10752 +       .unlocked_ioctl = aufs_ioctl_dir,
10753 +#ifdef CONFIG_COMPAT
10754 +       .compat_ioctl   = aufs_compat_ioctl_dir,
10755 +#endif
10756 +       .open           = aufs_open_dir,
10757 +       .release        = aufs_release_dir,
10758 +       .flush          = aufs_flush_dir,
10759 +       .fsync          = aufs_fsync_dir
10760 +};
10761 diff -urN /usr/share/empty/fs/aufs/dir.h linux/fs/aufs/dir.h
10762 --- /usr/share/empty/fs/aufs/dir.h      1970-01-01 01:00:00.000000000 +0100
10763 +++ linux/fs/aufs/dir.h 2021-11-01 23:48:34.206359262 +0100
10764 @@ -0,0 +1,134 @@
10765 +/* SPDX-License-Identifier: GPL-2.0 */
10766 +/*
10767 + * Copyright (C) 2005-2021 Junjiro R. Okajima
10768 + *
10769 + * This program, aufs is free software; you can redistribute it and/or modify
10770 + * it under the terms of the GNU General Public License as published by
10771 + * the Free Software Foundation; either version 2 of the License, or
10772 + * (at your option) any later version.
10773 + *
10774 + * This program is distributed in the hope that it will be useful,
10775 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
10776 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10777 + * GNU General Public License for more details.
10778 + *
10779 + * You should have received a copy of the GNU General Public License
10780 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
10781 + */
10782 +
10783 +/*
10784 + * directory operations
10785 + */
10786 +
10787 +#ifndef __AUFS_DIR_H__
10788 +#define __AUFS_DIR_H__
10789 +
10790 +#ifdef __KERNEL__
10791 +
10792 +#include <linux/fs.h>
10793 +
10794 +/* ---------------------------------------------------------------------- */
10795 +
10796 +/* need to be faster and smaller */
10797 +
10798 +struct au_nhash {
10799 +       unsigned int            nh_num;
10800 +       struct hlist_head       *nh_head;
10801 +};
10802 +
10803 +struct au_vdir_destr {
10804 +       unsigned char   len;
10805 +       unsigned char   name[];
10806 +} __packed;
10807 +
10808 +struct au_vdir_dehstr {
10809 +       struct hlist_node       hash;
10810 +       struct au_vdir_destr    *str;
10811 +       struct rcu_head         rcu;
10812 +} ____cacheline_aligned_in_smp;
10813 +
10814 +struct au_vdir_de {
10815 +       ino_t                   de_ino;
10816 +       unsigned char           de_type;
10817 +       /* caution: packed */
10818 +       struct au_vdir_destr    de_str;
10819 +} __packed;
10820 +
10821 +struct au_vdir_wh {
10822 +       struct hlist_node       wh_hash;
10823 +#ifdef CONFIG_AUFS_SHWH
10824 +       ino_t                   wh_ino;
10825 +       aufs_bindex_t           wh_bindex;
10826 +       unsigned char           wh_type;
10827 +#else
10828 +       aufs_bindex_t           wh_bindex;
10829 +#endif
10830 +       /* caution: packed */
10831 +       struct au_vdir_destr    wh_str;
10832 +} __packed;
10833 +
10834 +union au_vdir_deblk_p {
10835 +       unsigned char           *deblk;
10836 +       struct au_vdir_de       *de;
10837 +};
10838 +
10839 +struct au_vdir {
10840 +       unsigned char   **vd_deblk;
10841 +       unsigned long   vd_nblk;
10842 +       struct {
10843 +               unsigned long           ul;
10844 +               union au_vdir_deblk_p   p;
10845 +       } vd_last;
10846 +
10847 +       u64             vd_version;
10848 +       unsigned int    vd_deblk_sz;
10849 +       unsigned long   vd_jiffy;
10850 +       struct rcu_head rcu;
10851 +} ____cacheline_aligned_in_smp;
10852 +
10853 +/* ---------------------------------------------------------------------- */
10854 +
10855 +/* dir.c */
10856 +extern const struct file_operations aufs_dir_fop;
10857 +void au_add_nlink(struct inode *dir, struct inode *h_dir);
10858 +void au_sub_nlink(struct inode *dir, struct inode *h_dir);
10859 +loff_t au_dir_size(struct file *file, struct dentry *dentry);
10860 +void au_dir_ts(struct inode *dir, aufs_bindex_t bsrc);
10861 +int au_test_empty_lower(struct dentry *dentry);
10862 +int au_test_empty(struct dentry *dentry, struct au_nhash *whlist);
10863 +
10864 +/* vdir.c */
10865 +unsigned int au_rdhash_est(loff_t sz);
10866 +int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp);
10867 +void au_nhash_wh_free(struct au_nhash *whlist);
10868 +int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
10869 +                           int limit);
10870 +int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen);
10871 +int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
10872 +                      unsigned int d_type, aufs_bindex_t bindex,
10873 +                      unsigned char shwh);
10874 +void au_vdir_free(struct au_vdir *vdir);
10875 +int au_vdir_init(struct file *file);
10876 +int au_vdir_fill_de(struct file *file, struct dir_context *ctx);
10877 +
10878 +/* ioctl.c */
10879 +long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg);
10880 +
10881 +#ifdef CONFIG_AUFS_RDU
10882 +/* rdu.c */
10883 +long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
10884 +#ifdef CONFIG_COMPAT
10885 +long au_rdu_compat_ioctl(struct file *file, unsigned int cmd,
10886 +                        unsigned long arg);
10887 +#endif
10888 +#else
10889 +AuStub(long, au_rdu_ioctl, return -EINVAL, struct file *file,
10890 +       unsigned int cmd, unsigned long arg)
10891 +#ifdef CONFIG_COMPAT
10892 +AuStub(long, au_rdu_compat_ioctl, return -EINVAL, struct file *file,
10893 +       unsigned int cmd, unsigned long arg)
10894 +#endif
10895 +#endif
10896 +
10897 +#endif /* __KERNEL__ */
10898 +#endif /* __AUFS_DIR_H__ */
10899 diff -urN /usr/share/empty/fs/aufs/dirren.c linux/fs/aufs/dirren.c
10900 --- /usr/share/empty/fs/aufs/dirren.c   1970-01-01 01:00:00.000000000 +0100
10901 +++ linux/fs/aufs/dirren.c      2021-11-01 23:48:34.206359262 +0100
10902 @@ -0,0 +1,1315 @@
10903 +// SPDX-License-Identifier: GPL-2.0
10904 +/*
10905 + * Copyright (C) 2017-2021 Junjiro R. Okajima
10906 + *
10907 + * This program, aufs is free software; you can redistribute it and/or modify
10908 + * it under the terms of the GNU General Public License as published by
10909 + * the Free Software Foundation; either version 2 of the License, or
10910 + * (at your option) any later version.
10911 + *
10912 + * This program is distributed in the hope that it will be useful,
10913 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
10914 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10915 + * GNU General Public License for more details.
10916 + *
10917 + * You should have received a copy of the GNU General Public License
10918 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
10919 + */
10920 +
10921 +/*
10922 + * special handling in renaming a directory
10923 + * in order to support looking-up the before-renamed name on the lower readonly
10924 + * branches
10925 + */
10926 +
10927 +#include <linux/byteorder/generic.h>
10928 +#include "aufs.h"
10929 +
10930 +static void au_dr_hino_del(struct au_dr_br *dr, struct au_dr_hino *ent)
10931 +{
10932 +       int idx;
10933 +
10934 +       idx = au_dr_ihash(ent->dr_h_ino);
10935 +       au_hbl_del(&ent->dr_hnode, dr->dr_h_ino + idx);
10936 +}
10937 +
10938 +static int au_dr_hino_test_empty(struct au_dr_br *dr)
10939 +{
10940 +       int ret, i;
10941 +       struct hlist_bl_head *hbl;
10942 +
10943 +       ret = 1;
10944 +       for (i = 0; ret && i < AuDirren_NHASH; i++) {
10945 +               hbl = dr->dr_h_ino + i;
10946 +               hlist_bl_lock(hbl);
10947 +               ret &= hlist_bl_empty(hbl);
10948 +               hlist_bl_unlock(hbl);
10949 +       }
10950 +
10951 +       return ret;
10952 +}
10953 +
10954 +static struct au_dr_hino *au_dr_hino_find(struct au_dr_br *dr, ino_t ino)
10955 +{
10956 +       struct au_dr_hino *found, *ent;
10957 +       struct hlist_bl_head *hbl;
10958 +       struct hlist_bl_node *pos;
10959 +       int idx;
10960 +
10961 +       found = NULL;
10962 +       idx = au_dr_ihash(ino);
10963 +       hbl = dr->dr_h_ino + idx;
10964 +       hlist_bl_lock(hbl);
10965 +       hlist_bl_for_each_entry(ent, pos, hbl, dr_hnode)
10966 +               if (ent->dr_h_ino == ino) {
10967 +                       found = ent;
10968 +                       break;
10969 +               }
10970 +       hlist_bl_unlock(hbl);
10971 +
10972 +       return found;
10973 +}
10974 +
10975 +int au_dr_hino_test_add(struct au_dr_br *dr, ino_t ino,
10976 +                       struct au_dr_hino *add_ent)
10977 +{
10978 +       int found, idx;
10979 +       struct hlist_bl_head *hbl;
10980 +       struct hlist_bl_node *pos;
10981 +       struct au_dr_hino *ent;
10982 +
10983 +       found = 0;
10984 +       idx = au_dr_ihash(ino);
10985 +       hbl = dr->dr_h_ino + idx;
10986 +#if 0 /* debug print */
10987 +       {
10988 +               struct hlist_bl_node *tmp;
10989 +
10990 +               hlist_bl_for_each_entry_safe(ent, pos, tmp, hbl, dr_hnode)
10991 +                       AuDbg("hi%llu\n", (unsigned long long)ent->dr_h_ino);
10992 +       }
10993 +#endif
10994 +       hlist_bl_lock(hbl);
10995 +       hlist_bl_for_each_entry(ent, pos, hbl, dr_hnode)
10996 +               if (ent->dr_h_ino == ino) {
10997 +                       found = 1;
10998 +                       break;
10999 +               }
11000 +       if (!found && add_ent)
11001 +               hlist_bl_add_head(&add_ent->dr_hnode, hbl);
11002 +       hlist_bl_unlock(hbl);
11003 +
11004 +       if (!found && add_ent)
11005 +               AuDbg("i%llu added\n", (unsigned long long)add_ent->dr_h_ino);
11006 +
11007 +       return found;
11008 +}
11009 +
11010 +void au_dr_hino_free(struct au_dr_br *dr)
11011 +{
11012 +       int i;
11013 +       struct hlist_bl_head *hbl;
11014 +       struct hlist_bl_node *pos, *tmp;
11015 +       struct au_dr_hino *ent;
11016 +
11017 +       /* SiMustWriteLock(sb); */
11018 +
11019 +       for (i = 0; i < AuDirren_NHASH; i++) {
11020 +               hbl = dr->dr_h_ino + i;
11021 +               /* no spinlock since sbinfo must be write-locked */
11022 +               hlist_bl_for_each_entry_safe(ent, pos, tmp, hbl, dr_hnode)
11023 +                       au_kfree_rcu(ent);
11024 +               INIT_HLIST_BL_HEAD(hbl);
11025 +       }
11026 +}
11027 +
11028 +/* returns the number of inodes or an error */
11029 +static int au_dr_hino_store(struct super_block *sb, struct au_branch *br,
11030 +                           struct file *hinofile)
11031 +{
11032 +       int err, i;
11033 +       ssize_t ssz;
11034 +       loff_t pos, oldsize;
11035 +       __be64 u64;
11036 +       struct inode *hinoinode;
11037 +       struct hlist_bl_head *hbl;
11038 +       struct hlist_bl_node *n1, *n2;
11039 +       struct au_dr_hino *ent;
11040 +
11041 +       SiMustWriteLock(sb);
11042 +       AuDebugOn(!au_br_writable(br->br_perm));
11043 +
11044 +       hinoinode = file_inode(hinofile);
11045 +       oldsize = i_size_read(hinoinode);
11046 +
11047 +       err = 0;
11048 +       pos = 0;
11049 +       hbl = br->br_dirren.dr_h_ino;
11050 +       for (i = 0; !err && i < AuDirren_NHASH; i++, hbl++) {
11051 +               /* no bit-lock since sbinfo must be write-locked */
11052 +               hlist_bl_for_each_entry_safe(ent, n1, n2, hbl, dr_hnode) {
11053 +                       AuDbg("hi%llu, %pD2\n",
11054 +                             (unsigned long long)ent->dr_h_ino, hinofile);
11055 +                       u64 = cpu_to_be64(ent->dr_h_ino);
11056 +                       ssz = vfsub_write_k(hinofile, &u64, sizeof(u64), &pos);
11057 +                       if (ssz == sizeof(u64))
11058 +                               continue;
11059 +
11060 +                       /* write error */
11061 +                       pr_err("ssz %zd, %pD2\n", ssz, hinofile);
11062 +                       err = -ENOSPC;
11063 +                       if (ssz < 0)
11064 +                               err = ssz;
11065 +                       break;
11066 +               }
11067 +       }
11068 +       /* regardless the error */
11069 +       if (pos < oldsize) {
11070 +               err = vfsub_trunc(&hinofile->f_path, pos, /*attr*/0, hinofile);
11071 +               AuTraceErr(err);
11072 +       }
11073 +
11074 +       AuTraceErr(err);
11075 +       return err;
11076 +}
11077 +
11078 +static int au_dr_hino_load(struct au_dr_br *dr, struct file *hinofile)
11079 +{
11080 +       int err, hidx;
11081 +       ssize_t ssz;
11082 +       size_t sz, n;
11083 +       loff_t pos;
11084 +       uint64_t u64;
11085 +       struct au_dr_hino *ent;
11086 +       struct inode *hinoinode;
11087 +       struct hlist_bl_head *hbl;
11088 +
11089 +       err = 0;
11090 +       pos = 0;
11091 +       hbl = dr->dr_h_ino;
11092 +       hinoinode = file_inode(hinofile);
11093 +       sz = i_size_read(hinoinode);
11094 +       AuDebugOn(sz % sizeof(u64));
11095 +       n = sz / sizeof(u64);
11096 +       while (n--) {
11097 +               ssz = vfsub_read_k(hinofile, &u64, sizeof(u64), &pos);
11098 +               if (unlikely(ssz != sizeof(u64))) {
11099 +                       pr_err("ssz %zd, %pD2\n", ssz, hinofile);
11100 +                       err = -EINVAL;
11101 +                       if (ssz < 0)
11102 +                               err = ssz;
11103 +                       goto out_free;
11104 +               }
11105 +
11106 +               ent = kmalloc(sizeof(*ent), GFP_NOFS);
11107 +               if (!ent) {
11108 +                       err = -ENOMEM;
11109 +                       AuTraceErr(err);
11110 +                       goto out_free;
11111 +               }
11112 +               ent->dr_h_ino = be64_to_cpu((__force __be64)u64);
11113 +               AuDbg("hi%llu, %pD2\n",
11114 +                     (unsigned long long)ent->dr_h_ino, hinofile);
11115 +               hidx = au_dr_ihash(ent->dr_h_ino);
11116 +               au_hbl_add(&ent->dr_hnode, hbl + hidx);
11117 +       }
11118 +       goto out; /* success */
11119 +
11120 +out_free:
11121 +       au_dr_hino_free(dr);
11122 +out:
11123 +       AuTraceErr(err);
11124 +       return err;
11125 +}
11126 +
11127 +/*
11128 + * @bindex/@br is a switch to distinguish whether suspending hnotify or not.
11129 + * @path is a switch to distinguish load and store.
11130 + */
11131 +static int au_dr_hino(struct super_block *sb, aufs_bindex_t bindex,
11132 +                     struct au_branch *br, const struct path *path)
11133 +{
11134 +       int err, flags;
11135 +       unsigned char load, suspend;
11136 +       struct file *hinofile;
11137 +       struct au_hinode *hdir;
11138 +       struct inode *dir, *delegated;
11139 +       struct path hinopath;
11140 +       struct qstr hinoname = QSTR_INIT(AUFS_WH_DR_BRHINO,
11141 +                                        sizeof(AUFS_WH_DR_BRHINO) - 1);
11142 +
11143 +       AuDebugOn(bindex < 0 && !br);
11144 +       AuDebugOn(bindex >= 0 && br);
11145 +
11146 +       err = -EINVAL;
11147 +       suspend = !br;
11148 +       if (suspend)
11149 +               br = au_sbr(sb, bindex);
11150 +       load = !!path;
11151 +       if (!load) {
11152 +               path = &br->br_path;
11153 +               AuDebugOn(!au_br_writable(br->br_perm));
11154 +               if (unlikely(!au_br_writable(br->br_perm)))
11155 +                       goto out;
11156 +       }
11157 +
11158 +       hdir = NULL;
11159 +       if (suspend) {
11160 +               dir = d_inode(sb->s_root);
11161 +               hdir = au_hinode(au_ii(dir), bindex);
11162 +               dir = hdir->hi_inode;
11163 +               au_hn_inode_lock_nested(hdir, AuLsc_I_CHILD);
11164 +       } else {
11165 +               dir = d_inode(path->dentry);
11166 +               inode_lock_nested(dir, AuLsc_I_CHILD);
11167 +       }
11168 +       hinopath.mnt = path->mnt;
11169 +       hinopath.dentry = vfsub_lkup_one(&hinoname, (struct path *)path);
11170 +       err = PTR_ERR(hinopath.dentry);
11171 +       if (IS_ERR(hinopath.dentry))
11172 +               goto out_unlock;
11173 +
11174 +       err = 0;
11175 +       flags = O_RDONLY;
11176 +       if (load) {
11177 +               if (d_is_negative(hinopath.dentry))
11178 +                       goto out_dput; /* success */
11179 +       } else {
11180 +               if (au_dr_hino_test_empty(&br->br_dirren)) {
11181 +                       if (d_is_positive(hinopath.dentry)) {
11182 +                               delegated = NULL;
11183 +                               err = vfsub_unlink(dir, &hinopath, &delegated,
11184 +                                                  /*force*/0);
11185 +                               AuTraceErr(err);
11186 +                               if (unlikely(err))
11187 +                                       pr_err("ignored err %d, %pd2\n",
11188 +                                              err, hinopath.dentry);
11189 +                               if (unlikely(err == -EWOULDBLOCK))
11190 +                                       iput(delegated);
11191 +                               err = 0;
11192 +                       }
11193 +                       goto out_dput;
11194 +               } else if (!d_is_positive(hinopath.dentry)) {
11195 +                       err = vfsub_create(dir, &hinopath, 0600,
11196 +                                          /*want_excl*/false);
11197 +                       AuTraceErr(err);
11198 +                       if (unlikely(err))
11199 +                               goto out_dput;
11200 +               }
11201 +               flags = O_WRONLY;
11202 +       }
11203 +       hinofile = vfsub_dentry_open(&hinopath, flags);
11204 +       if (suspend)
11205 +               au_hn_inode_unlock(hdir);
11206 +       else
11207 +               inode_unlock(dir);
11208 +       dput(hinopath.dentry);
11209 +       AuTraceErrPtr(hinofile);
11210 +       if (IS_ERR(hinofile)) {
11211 +               err = PTR_ERR(hinofile);
11212 +               goto out;
11213 +       }
11214 +
11215 +       if (load)
11216 +               err = au_dr_hino_load(&br->br_dirren, hinofile);
11217 +       else
11218 +               err = au_dr_hino_store(sb, br, hinofile);
11219 +       fput(hinofile);
11220 +       goto out;
11221 +
11222 +out_dput:
11223 +       dput(hinopath.dentry);
11224 +out_unlock:
11225 +       if (suspend)
11226 +               au_hn_inode_unlock(hdir);
11227 +       else
11228 +               inode_unlock(dir);
11229 +out:
11230 +       AuTraceErr(err);
11231 +       return err;
11232 +}
11233 +
11234 +/* ---------------------------------------------------------------------- */
11235 +
11236 +static int au_dr_brid_init(struct au_dr_brid *brid, const struct path *path)
11237 +{
11238 +       int err;
11239 +       struct kstatfs kstfs;
11240 +       dev_t dev;
11241 +       struct dentry *dentry;
11242 +       struct super_block *sb;
11243 +
11244 +       err = vfs_statfs((void *)path, &kstfs);
11245 +       AuTraceErr(err);
11246 +       if (unlikely(err))
11247 +               goto out;
11248 +
11249 +       /* todo: support for UUID */
11250 +
11251 +       if (kstfs.f_fsid.val[0] || kstfs.f_fsid.val[1]) {
11252 +               brid->type = AuBrid_FSID;
11253 +               brid->fsid = kstfs.f_fsid;
11254 +       } else {
11255 +               dentry = path->dentry;
11256 +               sb = dentry->d_sb;
11257 +               dev = sb->s_dev;
11258 +               if (dev) {
11259 +                       brid->type = AuBrid_DEV;
11260 +                       brid->dev = dev;
11261 +               }
11262 +       }
11263 +
11264 +out:
11265 +       return err;
11266 +}
11267 +
11268 +int au_dr_br_init(struct super_block *sb, struct au_branch *br,
11269 +                 const struct path *path)
11270 +{
11271 +       int err, i;
11272 +       struct au_dr_br *dr;
11273 +       struct hlist_bl_head *hbl;
11274 +
11275 +       dr = &br->br_dirren;
11276 +       hbl = dr->dr_h_ino;
11277 +       for (i = 0; i < AuDirren_NHASH; i++, hbl++)
11278 +               INIT_HLIST_BL_HEAD(hbl);
11279 +
11280 +       err = au_dr_brid_init(&dr->dr_brid, path);
11281 +       if (unlikely(err))
11282 +               goto out;
11283 +
11284 +       if (au_opt_test(au_mntflags(sb), DIRREN))
11285 +               err = au_dr_hino(sb, /*bindex*/-1, br, path);
11286 +
11287 +out:
11288 +       AuTraceErr(err);
11289 +       return err;
11290 +}
11291 +
11292 +int au_dr_br_fin(struct super_block *sb, struct au_branch *br)
11293 +{
11294 +       int err;
11295 +
11296 +       err = 0;
11297 +       if (au_br_writable(br->br_perm))
11298 +               err = au_dr_hino(sb, /*bindex*/-1, br, /*path*/NULL);
11299 +       if (!err)
11300 +               au_dr_hino_free(&br->br_dirren);
11301 +
11302 +       return err;
11303 +}
11304 +
11305 +/* ---------------------------------------------------------------------- */
11306 +
11307 +static int au_brid_str(struct au_dr_brid *brid, struct inode *h_inode,
11308 +                      char *buf, size_t sz)
11309 +{
11310 +       int err;
11311 +       unsigned int major, minor;
11312 +       char *p;
11313 +
11314 +       p = buf;
11315 +       err = snprintf(p, sz, "%d_", brid->type);
11316 +       AuDebugOn(err > sz);
11317 +       p += err;
11318 +       sz -= err;
11319 +       switch (brid->type) {
11320 +       case AuBrid_Unset:
11321 +               return -EINVAL;
11322 +       case AuBrid_UUID:
11323 +               err = snprintf(p, sz, "%pU", brid->uuid.b);
11324 +               break;
11325 +       case AuBrid_FSID:
11326 +               err = snprintf(p, sz, "%08x-%08x",
11327 +                              brid->fsid.val[0], brid->fsid.val[1]);
11328 +               break;
11329 +       case AuBrid_DEV:
11330 +               major = MAJOR(brid->dev);
11331 +               minor = MINOR(brid->dev);
11332 +               if (major <= 0xff && minor <= 0xff)
11333 +                       err = snprintf(p, sz, "%02x%02x", major, minor);
11334 +               else
11335 +                       err = snprintf(p, sz, "%03x:%05x", major, minor);
11336 +               break;
11337 +       }
11338 +       AuDebugOn(err > sz);
11339 +       p += err;
11340 +       sz -= err;
11341 +       err = snprintf(p, sz, "_%llu", (unsigned long long)h_inode->i_ino);
11342 +       AuDebugOn(err > sz);
11343 +       p += err;
11344 +       sz -= err;
11345 +
11346 +       return p - buf;
11347 +}
11348 +
11349 +static int au_drinfo_name(struct au_branch *br, char *name, int len)
11350 +{
11351 +       int rlen;
11352 +       struct dentry *br_dentry;
11353 +       struct inode *br_inode;
11354 +
11355 +       br_dentry = au_br_dentry(br);
11356 +       br_inode = d_inode(br_dentry);
11357 +       rlen = au_brid_str(&br->br_dirren.dr_brid, br_inode, name, len);
11358 +       AuDebugOn(rlen >= AUFS_DIRREN_ENV_VAL_SZ);
11359 +       AuDebugOn(rlen > len);
11360 +
11361 +       return rlen;
11362 +}
11363 +
11364 +/* ---------------------------------------------------------------------- */
11365 +
11366 +/*
11367 + * from the given @h_dentry, construct drinfo at @*fdata.
11368 + * when the size of @*fdata is not enough, reallocate and return new @fdata and
11369 + * @allocated.
11370 + */
11371 +static int au_drinfo_construct(struct au_drinfo_fdata **fdata,
11372 +                              struct dentry *h_dentry,
11373 +                              unsigned char *allocated)
11374 +{
11375 +       int err, v;
11376 +       struct au_drinfo_fdata *f, *p;
11377 +       struct au_drinfo *drinfo;
11378 +       struct inode *h_inode;
11379 +       struct qstr *qname;
11380 +
11381 +       err = 0;
11382 +       f = *fdata;
11383 +       h_inode = d_inode(h_dentry);
11384 +       qname = &h_dentry->d_name;
11385 +       drinfo = &f->drinfo;
11386 +       drinfo->ino = (__force uint64_t)cpu_to_be64(h_inode->i_ino);
11387 +       drinfo->oldnamelen = qname->len;
11388 +       if (*allocated < sizeof(*f) + qname->len) {
11389 +               v = roundup_pow_of_two(*allocated + qname->len);
11390 +               p = au_krealloc(f, v, GFP_NOFS, /*may_shrink*/0);
11391 +               if (unlikely(!p)) {
11392 +                       err = -ENOMEM;
11393 +                       AuTraceErr(err);
11394 +                       goto out;
11395 +               }
11396 +               f = p;
11397 +               *fdata = f;
11398 +               *allocated = v;
11399 +               drinfo = &f->drinfo;
11400 +       }
11401 +       memcpy(drinfo->oldname, qname->name, qname->len);
11402 +       AuDbg("i%llu, %.*s\n",
11403 +             be64_to_cpu((__force __be64)drinfo->ino), drinfo->oldnamelen,
11404 +             drinfo->oldname);
11405 +
11406 +out:
11407 +       AuTraceErr(err);
11408 +       return err;
11409 +}
11410 +
11411 +/* callers have to free the return value */
11412 +static struct au_drinfo *au_drinfo_read_k(struct file *file, ino_t h_ino)
11413 +{
11414 +       struct au_drinfo *ret, *drinfo;
11415 +       struct au_drinfo_fdata fdata;
11416 +       int len;
11417 +       loff_t pos;
11418 +       ssize_t ssz;
11419 +
11420 +       ret = ERR_PTR(-EIO);
11421 +       pos = 0;
11422 +       ssz = vfsub_read_k(file, &fdata, sizeof(fdata), &pos);
11423 +       if (unlikely(ssz != sizeof(fdata))) {
11424 +               AuIOErr("ssz %zd, %u, %pD2\n",
11425 +                       ssz, (unsigned int)sizeof(fdata), file);
11426 +               goto out;
11427 +       }
11428 +
11429 +       fdata.magic = ntohl((__force __be32)fdata.magic);
11430 +       switch (fdata.magic) {
11431 +       case AUFS_DRINFO_MAGIC_V1:
11432 +               break;
11433 +       default:
11434 +               AuIOErr("magic-num 0x%x, 0x%x, %pD2\n",
11435 +                       fdata.magic, AUFS_DRINFO_MAGIC_V1, file);
11436 +               goto out;
11437 +       }
11438 +
11439 +       drinfo = &fdata.drinfo;
11440 +       len = drinfo->oldnamelen;
11441 +       if (!len) {
11442 +               AuIOErr("broken drinfo %pD2\n", file);
11443 +               goto out;
11444 +       }
11445 +
11446 +       ret = NULL;
11447 +       drinfo->ino = be64_to_cpu((__force __be64)drinfo->ino);
11448 +       if (unlikely(h_ino && drinfo->ino != h_ino)) {
11449 +               AuDbg("ignored i%llu, i%llu, %pD2\n",
11450 +                     (unsigned long long)drinfo->ino,
11451 +                     (unsigned long long)h_ino, file);
11452 +               goto out; /* success */
11453 +       }
11454 +
11455 +       ret = kmalloc(sizeof(*ret) + len, GFP_NOFS);
11456 +       if (unlikely(!ret)) {
11457 +               ret = ERR_PTR(-ENOMEM);
11458 +               AuTraceErrPtr(ret);
11459 +               goto out;
11460 +       }
11461 +
11462 +       *ret = *drinfo;
11463 +       ssz = vfsub_read_k(file, (void *)ret->oldname, len, &pos);
11464 +       if (unlikely(ssz != len)) {
11465 +               au_kfree_rcu(ret);
11466 +               ret = ERR_PTR(-EIO);
11467 +               AuIOErr("ssz %zd, %u, %pD2\n", ssz, len, file);
11468 +               goto out;
11469 +       }
11470 +
11471 +       AuDbg("oldname %.*s\n", ret->oldnamelen, ret->oldname);
11472 +
11473 +out:
11474 +       return ret;
11475 +}
11476 +
11477 +/* ---------------------------------------------------------------------- */
11478 +
11479 +/* in order to be revertible */
11480 +struct au_drinfo_rev_elm {
11481 +       int                     created;
11482 +       struct dentry           *info_dentry;
11483 +       struct au_drinfo        *info_last;
11484 +};
11485 +
11486 +struct au_drinfo_rev {
11487 +       unsigned char                   already;
11488 +       aufs_bindex_t                   nelm;
11489 +       struct au_drinfo_rev_elm        elm[];
11490 +};
11491 +
11492 +/* todo: isn't it too large? */
11493 +struct au_drinfo_store {
11494 +       struct path h_ppath;
11495 +       struct dentry *h_dentry;
11496 +       struct au_drinfo_fdata *fdata;
11497 +       char *infoname;                 /* inside of whname, just after PFX */
11498 +       char whname[sizeof(AUFS_WH_DR_INFO_PFX) + AUFS_DIRREN_ENV_VAL_SZ];
11499 +       aufs_bindex_t btgt, btail;
11500 +       unsigned char no_sio,
11501 +               allocated,              /* current size of *fdata */
11502 +               infonamelen,            /* room size for p */
11503 +               whnamelen,              /* length of the generated name */
11504 +               renameback;             /* renamed back */
11505 +};
11506 +
11507 +/* on rename(2) error, the caller should revert it using @elm */
11508 +static int au_drinfo_do_store(struct au_drinfo_store *w,
11509 +                             struct au_drinfo_rev_elm *elm)
11510 +{
11511 +       int err, len;
11512 +       ssize_t ssz;
11513 +       loff_t pos;
11514 +       struct path infopath = {
11515 +               .mnt = w->h_ppath.mnt
11516 +       };
11517 +       struct inode *h_dir, *h_inode, *delegated;
11518 +       struct file *infofile;
11519 +       struct qstr *qname;
11520 +
11521 +       AuDebugOn(elm
11522 +                 && memcmp(elm, page_address(ZERO_PAGE(0)), sizeof(*elm)));
11523 +
11524 +       infopath.dentry = vfsub_lookup_one_len(w->whname, &w->h_ppath,
11525 +                                              w->whnamelen);
11526 +       AuTraceErrPtr(infopath.dentry);
11527 +       if (IS_ERR(infopath.dentry)) {
11528 +               err = PTR_ERR(infopath.dentry);
11529 +               goto out;
11530 +       }
11531 +
11532 +       err = 0;
11533 +       h_dir = d_inode(w->h_ppath.dentry);
11534 +       if (elm && d_is_negative(infopath.dentry)) {
11535 +               err = vfsub_create(h_dir, &infopath, 0600, /*want_excl*/true);
11536 +               AuTraceErr(err);
11537 +               if (unlikely(err))
11538 +                       goto out_dput;
11539 +               elm->created = 1;
11540 +               elm->info_dentry = dget(infopath.dentry);
11541 +       }
11542 +
11543 +       infofile = vfsub_dentry_open(&infopath, O_RDWR);
11544 +       AuTraceErrPtr(infofile);
11545 +       if (IS_ERR(infofile)) {
11546 +               err = PTR_ERR(infofile);
11547 +               goto out_dput;
11548 +       }
11549 +
11550 +       h_inode = d_inode(infopath.dentry);
11551 +       if (elm && i_size_read(h_inode)) {
11552 +               h_inode = d_inode(w->h_dentry);
11553 +               elm->info_last = au_drinfo_read_k(infofile, h_inode->i_ino);
11554 +               AuTraceErrPtr(elm->info_last);
11555 +               if (IS_ERR(elm->info_last)) {
11556 +                       err = PTR_ERR(elm->info_last);
11557 +                       elm->info_last = NULL;
11558 +                       AuDebugOn(elm->info_dentry);
11559 +                       goto out_fput;
11560 +               }
11561 +       }
11562 +
11563 +       if (elm && w->renameback) {
11564 +               delegated = NULL;
11565 +               err = vfsub_unlink(h_dir, &infopath, &delegated, /*force*/0);
11566 +               AuTraceErr(err);
11567 +               if (unlikely(err == -EWOULDBLOCK))
11568 +                       iput(delegated);
11569 +               goto out_fput;
11570 +       }
11571 +
11572 +       pos = 0;
11573 +       qname = &w->h_dentry->d_name;
11574 +       len = sizeof(*w->fdata) + qname->len;
11575 +       if (!elm)
11576 +               len = sizeof(*w->fdata) + w->fdata->drinfo.oldnamelen;
11577 +       ssz = vfsub_write_k(infofile, w->fdata, len, &pos);
11578 +       if (ssz == len) {
11579 +               AuDbg("hi%llu, %.*s\n", w->fdata->drinfo.ino,
11580 +                     w->fdata->drinfo.oldnamelen, w->fdata->drinfo.oldname);
11581 +               goto out_fput; /* success */
11582 +       } else {
11583 +               err = -EIO;
11584 +               if (ssz < 0)
11585 +                       err = ssz;
11586 +               /* the caller should revert it using @elm */
11587 +       }
11588 +
11589 +out_fput:
11590 +       fput(infofile);
11591 +out_dput:
11592 +       dput(infopath.dentry);
11593 +out:
11594 +       AuTraceErr(err);
11595 +       return err;
11596 +}
11597 +
11598 +struct au_call_drinfo_do_store_args {
11599 +       int *errp;
11600 +       struct au_drinfo_store *w;
11601 +       struct au_drinfo_rev_elm *elm;
11602 +};
11603 +
11604 +static void au_call_drinfo_do_store(void *args)
11605 +{
11606 +       struct au_call_drinfo_do_store_args *a = args;
11607 +
11608 +       *a->errp = au_drinfo_do_store(a->w, a->elm);
11609 +}
11610 +
11611 +static int au_drinfo_store_sio(struct au_drinfo_store *w,
11612 +                              struct au_drinfo_rev_elm *elm)
11613 +{
11614 +       int err, wkq_err;
11615 +
11616 +       if (w->no_sio)
11617 +               err = au_drinfo_do_store(w, elm);
11618 +       else {
11619 +               struct au_call_drinfo_do_store_args a = {
11620 +                       .errp   = &err,
11621 +                       .w      = w,
11622 +                       .elm    = elm
11623 +               };
11624 +               wkq_err = au_wkq_wait(au_call_drinfo_do_store, &a);
11625 +               if (unlikely(wkq_err))
11626 +                       err = wkq_err;
11627 +       }
11628 +       AuTraceErr(err);
11629 +
11630 +       return err;
11631 +}
11632 +
11633 +static int au_drinfo_store_work_init(struct au_drinfo_store *w,
11634 +                                    aufs_bindex_t btgt)
11635 +{
11636 +       int err;
11637 +
11638 +       memset(w, 0, sizeof(*w));
11639 +       w->allocated = roundup_pow_of_two(sizeof(*w->fdata) + 40);
11640 +       strcpy(w->whname, AUFS_WH_DR_INFO_PFX);
11641 +       w->infoname = w->whname + sizeof(AUFS_WH_DR_INFO_PFX) - 1;
11642 +       w->infonamelen = sizeof(w->whname) - sizeof(AUFS_WH_DR_INFO_PFX);
11643 +       w->btgt = btgt;
11644 +       w->no_sio = !!uid_eq(current_fsuid(), GLOBAL_ROOT_UID);
11645 +
11646 +       err = -ENOMEM;
11647 +       w->fdata = kcalloc(1, w->allocated, GFP_NOFS);
11648 +       if (unlikely(!w->fdata)) {
11649 +               AuTraceErr(err);
11650 +               goto out;
11651 +       }
11652 +       w->fdata->magic = (__force uint32_t)htonl(AUFS_DRINFO_MAGIC_V1);
11653 +       err = 0;
11654 +
11655 +out:
11656 +       return err;
11657 +}
11658 +
11659 +static void au_drinfo_store_work_fin(struct au_drinfo_store *w)
11660 +{
11661 +       au_kfree_rcu(w->fdata);
11662 +}
11663 +
11664 +static void au_drinfo_store_rev(struct au_drinfo_rev *rev,
11665 +                               struct au_drinfo_store *w)
11666 +{
11667 +       struct au_drinfo_rev_elm *elm;
11668 +       struct inode *h_dir, *delegated;
11669 +       int err, nelm;
11670 +       struct path infopath = {
11671 +               .mnt = w->h_ppath.mnt
11672 +       };
11673 +
11674 +       h_dir = d_inode(w->h_ppath.dentry);
11675 +       IMustLock(h_dir);
11676 +
11677 +       err = 0;
11678 +       elm = rev->elm;
11679 +       for (nelm = rev->nelm; nelm > 0; nelm--, elm++) {
11680 +               AuDebugOn(elm->created && elm->info_last);
11681 +               if (elm->created) {
11682 +                       AuDbg("here\n");
11683 +                       delegated = NULL;
11684 +                       infopath.dentry = elm->info_dentry;
11685 +                       err = vfsub_unlink(h_dir, &infopath, &delegated,
11686 +                                          !w->no_sio);
11687 +                       AuTraceErr(err);
11688 +                       if (unlikely(err == -EWOULDBLOCK))
11689 +                               iput(delegated);
11690 +                       dput(elm->info_dentry);
11691 +               } else if (elm->info_last) {
11692 +                       AuDbg("here\n");
11693 +                       w->fdata->drinfo = *elm->info_last;
11694 +                       memcpy(w->fdata->drinfo.oldname,
11695 +                              elm->info_last->oldname,
11696 +                              elm->info_last->oldnamelen);
11697 +                       err = au_drinfo_store_sio(w, /*elm*/NULL);
11698 +                       au_kfree_rcu(elm->info_last);
11699 +               }
11700 +               if (unlikely(err))
11701 +                       AuIOErr("%d, %s\n", err, w->whname);
11702 +               /* go on even if err */
11703 +       }
11704 +}
11705 +
11706 +/* caller has to call au_dr_rename_fin() later */
11707 +static int au_drinfo_store(struct dentry *dentry, aufs_bindex_t btgt,
11708 +                          struct qstr *dst_name, void *_rev)
11709 +{
11710 +       int err, sz, nelm;
11711 +       aufs_bindex_t bindex, btail;
11712 +       struct au_drinfo_store work;
11713 +       struct au_drinfo_rev *rev, **p;
11714 +       struct au_drinfo_rev_elm *elm;
11715 +       struct super_block *sb;
11716 +       struct au_branch *br;
11717 +       struct au_hinode *hdir;
11718 +
11719 +       err = au_drinfo_store_work_init(&work, btgt);
11720 +       AuTraceErr(err);
11721 +       if (unlikely(err))
11722 +               goto out;
11723 +
11724 +       err = -ENOMEM;
11725 +       btail = au_dbtaildir(dentry);
11726 +       nelm = btail - btgt;
11727 +       sz = sizeof(*rev) + sizeof(*elm) * nelm;
11728 +       rev = kcalloc(1, sz, GFP_NOFS);
11729 +       if (unlikely(!rev)) {
11730 +               AuTraceErr(err);
11731 +               goto out_args;
11732 +       }
11733 +       rev->nelm = nelm;
11734 +       elm = rev->elm;
11735 +       p = _rev;
11736 +       *p = rev;
11737 +
11738 +       err = 0;
11739 +       sb = dentry->d_sb;
11740 +       work.h_ppath.dentry = au_h_dptr(dentry, btgt);
11741 +       work.h_ppath.mnt = au_sbr_mnt(sb, btgt);
11742 +       hdir = au_hi(d_inode(dentry), btgt);
11743 +       au_hn_inode_lock_nested(hdir, AuLsc_I_CHILD);
11744 +       for (bindex = btgt + 1; bindex <= btail; bindex++, elm++) {
11745 +               work.h_dentry = au_h_dptr(dentry, bindex);
11746 +               if (!work.h_dentry)
11747 +                       continue;
11748 +
11749 +               err = au_drinfo_construct(&work.fdata, work.h_dentry,
11750 +                                         &work.allocated);
11751 +               AuTraceErr(err);
11752 +               if (unlikely(err))
11753 +                       break;
11754 +
11755 +               work.renameback = au_qstreq(&work.h_dentry->d_name, dst_name);
11756 +               br = au_sbr(sb, bindex);
11757 +               work.whnamelen = sizeof(AUFS_WH_DR_INFO_PFX) - 1;
11758 +               work.whnamelen += au_drinfo_name(br, work.infoname,
11759 +                                                work.infonamelen);
11760 +               AuDbg("whname %.*s, i%llu, %.*s\n",
11761 +                     work.whnamelen, work.whname,
11762 +                     be64_to_cpu((__force __be64)work.fdata->drinfo.ino),
11763 +                     work.fdata->drinfo.oldnamelen,
11764 +                     work.fdata->drinfo.oldname);
11765 +
11766 +               err = au_drinfo_store_sio(&work, elm);
11767 +               AuTraceErr(err);
11768 +               if (unlikely(err))
11769 +                       break;
11770 +       }
11771 +       if (unlikely(err)) {
11772 +               /* revert all drinfo */
11773 +               au_drinfo_store_rev(rev, &work);
11774 +               au_kfree_try_rcu(rev);
11775 +               *p = NULL;
11776 +       }
11777 +       au_hn_inode_unlock(hdir);
11778 +
11779 +out_args:
11780 +       au_drinfo_store_work_fin(&work);
11781 +out:
11782 +       return err;
11783 +}
11784 +
11785 +/* ---------------------------------------------------------------------- */
11786 +
11787 +int au_dr_rename(struct dentry *src, aufs_bindex_t bindex,
11788 +                struct qstr *dst_name, void *_rev)
11789 +{
11790 +       int err, already;
11791 +       ino_t ino;
11792 +       struct super_block *sb;
11793 +       struct au_branch *br;
11794 +       struct au_dr_br *dr;
11795 +       struct dentry *h_dentry;
11796 +       struct inode *h_inode;
11797 +       struct au_dr_hino *ent;
11798 +       struct au_drinfo_rev *rev, **p;
11799 +
11800 +       AuDbg("bindex %d\n", bindex);
11801 +
11802 +       err = -ENOMEM;
11803 +       ent = kmalloc(sizeof(*ent), GFP_NOFS);
11804 +       if (unlikely(!ent))
11805 +               goto out;
11806 +
11807 +       sb = src->d_sb;
11808 +       br = au_sbr(sb, bindex);
11809 +       dr = &br->br_dirren;
11810 +       h_dentry = au_h_dptr(src, bindex);
11811 +       h_inode = d_inode(h_dentry);
11812 +       ino = h_inode->i_ino;
11813 +       ent->dr_h_ino = ino;
11814 +       already = au_dr_hino_test_add(dr, ino, ent);
11815 +       AuDbg("b%d, hi%llu, already %d\n",
11816 +             bindex, (unsigned long long)ino, already);
11817 +
11818 +       err = au_drinfo_store(src, bindex, dst_name, _rev);
11819 +       AuTraceErr(err);
11820 +       if (!err) {
11821 +               p = _rev;
11822 +               rev = *p;
11823 +               rev->already = already;
11824 +               goto out; /* success */
11825 +       }
11826 +
11827 +       /* revert */
11828 +       if (!already)
11829 +               au_dr_hino_del(dr, ent);
11830 +       au_kfree_rcu(ent);
11831 +
11832 +out:
11833 +       AuTraceErr(err);
11834 +       return err;
11835 +}
11836 +
11837 +void au_dr_rename_fin(struct dentry *src, aufs_bindex_t btgt, void *_rev)
11838 +{
11839 +       struct au_drinfo_rev *rev;
11840 +       struct au_drinfo_rev_elm *elm;
11841 +       int nelm;
11842 +
11843 +       rev = _rev;
11844 +       elm = rev->elm;
11845 +       for (nelm = rev->nelm; nelm > 0; nelm--, elm++) {
11846 +               dput(elm->info_dentry);
11847 +               au_kfree_rcu(elm->info_last);
11848 +       }
11849 +       au_kfree_try_rcu(rev);
11850 +}
11851 +
11852 +void au_dr_rename_rev(struct dentry *src, aufs_bindex_t btgt, void *_rev)
11853 +{
11854 +       int err;
11855 +       struct au_drinfo_store work;
11856 +       struct au_drinfo_rev *rev = _rev;
11857 +       struct super_block *sb;
11858 +       struct au_branch *br;
11859 +       struct inode *h_inode;
11860 +       struct au_dr_br *dr;
11861 +       struct au_dr_hino *ent;
11862 +
11863 +       err = au_drinfo_store_work_init(&work, btgt);
11864 +       if (unlikely(err))
11865 +               goto out;
11866 +
11867 +       sb = src->d_sb;
11868 +       br = au_sbr(sb, btgt);
11869 +       work.h_ppath.dentry = au_h_dptr(src, btgt);
11870 +       work.h_ppath.mnt = au_br_mnt(br);
11871 +       au_drinfo_store_rev(rev, &work);
11872 +       au_drinfo_store_work_fin(&work);
11873 +       if (rev->already)
11874 +               goto out;
11875 +
11876 +       dr = &br->br_dirren;
11877 +       h_inode = d_inode(work.h_ppath.dentry);
11878 +       ent = au_dr_hino_find(dr, h_inode->i_ino);
11879 +       BUG_ON(!ent);
11880 +       au_dr_hino_del(dr, ent);
11881 +       au_kfree_rcu(ent);
11882 +
11883 +out:
11884 +       au_kfree_try_rcu(rev);
11885 +       if (unlikely(err))
11886 +               pr_err("failed to remove dirren info\n");
11887 +}
11888 +
11889 +/* ---------------------------------------------------------------------- */
11890 +
11891 +static struct au_drinfo *au_drinfo_do_load(struct path *h_ppath,
11892 +                                          char *whname, int whnamelen,
11893 +                                          struct dentry **info_dentry)
11894 +{
11895 +       struct au_drinfo *drinfo;
11896 +       struct file *f;
11897 +       struct inode *h_dir;
11898 +       struct path infopath;
11899 +       int unlocked;
11900 +
11901 +       AuDbg("%pd/%.*s\n", h_ppath->dentry, whnamelen, whname);
11902 +
11903 +       *info_dentry = NULL;
11904 +       drinfo = NULL;
11905 +       unlocked = 0;
11906 +       h_dir = d_inode(h_ppath->dentry);
11907 +       inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
11908 +       infopath.dentry = vfsub_lookup_one_len(whname, h_ppath, whnamelen);
11909 +       if (IS_ERR(infopath.dentry)) {
11910 +               drinfo = (void *)infopath.dentry;
11911 +               goto out;
11912 +       }
11913 +
11914 +       if (d_is_negative(infopath.dentry))
11915 +               goto out_dput; /* success */
11916 +
11917 +       infopath.mnt = h_ppath->mnt;
11918 +       f = vfsub_dentry_open(&infopath, O_RDONLY);
11919 +       inode_unlock_shared(h_dir);
11920 +       unlocked = 1;
11921 +       if (IS_ERR(f)) {
11922 +               drinfo = (void *)f;
11923 +               goto out_dput;
11924 +       }
11925 +
11926 +       drinfo = au_drinfo_read_k(f, /*h_ino*/0);
11927 +       if (IS_ERR_OR_NULL(drinfo))
11928 +               goto out_fput;
11929 +
11930 +       AuDbg("oldname %.*s\n", drinfo->oldnamelen, drinfo->oldname);
11931 +       *info_dentry = dget(infopath.dentry); /* keep it alive */
11932 +
11933 +out_fput:
11934 +       fput(f);
11935 +out_dput:
11936 +       dput(infopath.dentry);
11937 +out:
11938 +       if (!unlocked)
11939 +               inode_unlock_shared(h_dir);
11940 +       AuTraceErrPtr(drinfo);
11941 +       return drinfo;
11942 +}
11943 +
11944 +struct au_drinfo_do_load_args {
11945 +       struct au_drinfo **drinfop;
11946 +       struct path *h_ppath;
11947 +       char *whname;
11948 +       int whnamelen;
11949 +       struct dentry **info_dentry;
11950 +};
11951 +
11952 +static void au_call_drinfo_do_load(void *args)
11953 +{
11954 +       struct au_drinfo_do_load_args *a = args;
11955 +
11956 +       *a->drinfop = au_drinfo_do_load(a->h_ppath, a->whname, a->whnamelen,
11957 +                                       a->info_dentry);
11958 +}
11959 +
11960 +struct au_drinfo_load {
11961 +       struct path h_ppath;
11962 +       struct qstr *qname;
11963 +       unsigned char no_sio;
11964 +
11965 +       aufs_bindex_t ninfo;
11966 +       struct au_drinfo **drinfo;
11967 +};
11968 +
11969 +static int au_drinfo_load(struct au_drinfo_load *w, aufs_bindex_t bindex,
11970 +                         struct au_branch *br)
11971 +{
11972 +       int err, wkq_err, whnamelen, e;
11973 +       char whname[sizeof(AUFS_WH_DR_INFO_PFX) + AUFS_DIRREN_ENV_VAL_SZ]
11974 +               = AUFS_WH_DR_INFO_PFX;
11975 +       struct au_drinfo *drinfo;
11976 +       struct qstr oldname;
11977 +       struct inode *h_dir, *delegated;
11978 +       struct dentry *info_dentry;
11979 +       struct path infopath;
11980 +
11981 +       whnamelen = sizeof(AUFS_WH_DR_INFO_PFX) - 1;
11982 +       whnamelen += au_drinfo_name(br, whname + whnamelen,
11983 +                                   sizeof(whname) - whnamelen);
11984 +       if (w->no_sio)
11985 +               drinfo = au_drinfo_do_load(&w->h_ppath, whname, whnamelen,
11986 +                                          &info_dentry);
11987 +       else {
11988 +               struct au_drinfo_do_load_args args = {
11989 +                       .drinfop        = &drinfo,
11990 +                       .h_ppath        = &w->h_ppath,
11991 +                       .whname         = whname,
11992 +                       .whnamelen      = whnamelen,
11993 +                       .info_dentry    = &info_dentry
11994 +               };
11995 +               wkq_err = au_wkq_wait(au_call_drinfo_do_load, &args);
11996 +               if (unlikely(wkq_err))
11997 +                       drinfo = ERR_PTR(wkq_err);
11998 +       }
11999 +       err = PTR_ERR(drinfo);
12000 +       if (IS_ERR_OR_NULL(drinfo))
12001 +               goto out;
12002 +
12003 +       err = 0;
12004 +       oldname.len = drinfo->oldnamelen;
12005 +       oldname.name = drinfo->oldname;
12006 +       if (au_qstreq(w->qname, &oldname)) {
12007 +               /* the name is renamed back */
12008 +               au_kfree_rcu(drinfo);
12009 +               drinfo = NULL;
12010 +
12011 +               infopath.dentry = info_dentry;
12012 +               infopath.mnt = w->h_ppath.mnt;
12013 +               h_dir = d_inode(w->h_ppath.dentry);
12014 +               delegated = NULL;
12015 +               inode_lock_nested(h_dir, AuLsc_I_PARENT);
12016 +               e = vfsub_unlink(h_dir, &infopath, &delegated, !w->no_sio);
12017 +               inode_unlock(h_dir);
12018 +               if (unlikely(e))
12019 +                       AuIOErr("ignored %d, %pd2\n", e, &infopath.dentry);
12020 +               if (unlikely(e == -EWOULDBLOCK))
12021 +                       iput(delegated);
12022 +       }
12023 +       au_kfree_rcu(w->drinfo[bindex]);
12024 +       w->drinfo[bindex] = drinfo;
12025 +       dput(info_dentry);
12026 +
12027 +out:
12028 +       AuTraceErr(err);
12029 +       return err;
12030 +}
12031 +
12032 +/* ---------------------------------------------------------------------- */
12033 +
12034 +static void au_dr_lkup_free(struct au_drinfo **drinfo, int n)
12035 +{
12036 +       struct au_drinfo **p = drinfo;
12037 +
12038 +       while (n-- > 0)
12039 +               au_kfree_rcu(*drinfo++);
12040 +       au_kfree_try_rcu(p);
12041 +}
12042 +
12043 +int au_dr_lkup(struct au_do_lookup_args *lkup, struct dentry *dentry,
12044 +              aufs_bindex_t btgt)
12045 +{
12046 +       int err, ninfo;
12047 +       struct au_drinfo_load w;
12048 +       aufs_bindex_t bindex, bbot;
12049 +       struct au_branch *br;
12050 +       struct inode *h_dir;
12051 +       struct au_dr_hino *ent;
12052 +       struct super_block *sb;
12053 +
12054 +       AuDbg("%.*s, name %.*s, whname %.*s, b%d\n",
12055 +             AuLNPair(&dentry->d_name), AuLNPair(&lkup->dirren.dr_name),
12056 +             AuLNPair(&lkup->whname), btgt);
12057 +
12058 +       sb = dentry->d_sb;
12059 +       bbot = au_sbbot(sb);
12060 +       w.ninfo = bbot + 1;
12061 +       if (!lkup->dirren.drinfo) {
12062 +               lkup->dirren.drinfo = kcalloc(w.ninfo,
12063 +                                             sizeof(*lkup->dirren.drinfo),
12064 +                                             GFP_NOFS);
12065 +               if (unlikely(!lkup->dirren.drinfo)) {
12066 +                       err = -ENOMEM;
12067 +                       goto out;
12068 +               }
12069 +               lkup->dirren.ninfo = w.ninfo;
12070 +       }
12071 +       w.drinfo = lkup->dirren.drinfo;
12072 +       w.no_sio = !!uid_eq(current_fsuid(), GLOBAL_ROOT_UID);
12073 +       w.h_ppath.dentry = au_h_dptr(dentry, btgt);
12074 +       AuDebugOn(!w.h_ppath.dentry);
12075 +       w.h_ppath.mnt = au_sbr_mnt(sb, btgt);
12076 +       w.qname = &dentry->d_name;
12077 +
12078 +       ninfo = 0;
12079 +       for (bindex = btgt + 1; bindex <= bbot; bindex++) {
12080 +               br = au_sbr(sb, bindex);
12081 +               err = au_drinfo_load(&w, bindex, br);
12082 +               if (unlikely(err))
12083 +                       goto out_free;
12084 +               if (w.drinfo[bindex])
12085 +                       ninfo++;
12086 +       }
12087 +       if (!ninfo) {
12088 +               br = au_sbr(sb, btgt);
12089 +               h_dir = d_inode(w.h_ppath.dentry);
12090 +               ent = au_dr_hino_find(&br->br_dirren, h_dir->i_ino);
12091 +               AuDebugOn(!ent);
12092 +               au_dr_hino_del(&br->br_dirren, ent);
12093 +               au_kfree_rcu(ent);
12094 +       }
12095 +       goto out; /* success */
12096 +
12097 +out_free:
12098 +       au_dr_lkup_free(lkup->dirren.drinfo, lkup->dirren.ninfo);
12099 +       lkup->dirren.ninfo = 0;
12100 +       lkup->dirren.drinfo = NULL;
12101 +out:
12102 +       AuTraceErr(err);
12103 +       return err;
12104 +}
12105 +
12106 +void au_dr_lkup_fin(struct au_do_lookup_args *lkup)
12107 +{
12108 +       au_dr_lkup_free(lkup->dirren.drinfo, lkup->dirren.ninfo);
12109 +}
12110 +
12111 +int au_dr_lkup_name(struct au_do_lookup_args *lkup, aufs_bindex_t btgt)
12112 +{
12113 +       int err;
12114 +       struct au_drinfo *drinfo;
12115 +
12116 +       err = 0;
12117 +       if (!lkup->dirren.drinfo)
12118 +               goto out;
12119 +       AuDebugOn(lkup->dirren.ninfo <= btgt);
12120 +       drinfo = lkup->dirren.drinfo[btgt];
12121 +       if (!drinfo)
12122 +               goto out;
12123 +
12124 +       au_kfree_try_rcu(lkup->whname.name);
12125 +       lkup->whname.name = NULL;
12126 +       lkup->dirren.dr_name.len = drinfo->oldnamelen;
12127 +       lkup->dirren.dr_name.name = drinfo->oldname;
12128 +       lkup->name = &lkup->dirren.dr_name;
12129 +       err = au_wh_name_alloc(&lkup->whname, lkup->name);
12130 +       if (!err)
12131 +               AuDbg("name %.*s, whname %.*s, b%d\n",
12132 +                     AuLNPair(lkup->name), AuLNPair(&lkup->whname),
12133 +                     btgt);
12134 +
12135 +out:
12136 +       AuTraceErr(err);
12137 +       return err;
12138 +}
12139 +
12140 +int au_dr_lkup_h_ino(struct au_do_lookup_args *lkup, aufs_bindex_t bindex,
12141 +                    ino_t h_ino)
12142 +{
12143 +       int match;
12144 +       struct au_drinfo *drinfo;
12145 +
12146 +       match = 1;
12147 +       if (!lkup->dirren.drinfo)
12148 +               goto out;
12149 +       AuDebugOn(lkup->dirren.ninfo <= bindex);
12150 +       drinfo = lkup->dirren.drinfo[bindex];
12151 +       if (!drinfo)
12152 +               goto out;
12153 +
12154 +       match = (drinfo->ino == h_ino);
12155 +       AuDbg("match %d\n", match);
12156 +
12157 +out:
12158 +       return match;
12159 +}
12160 +
12161 +/* ---------------------------------------------------------------------- */
12162 +
12163 +int au_dr_opt_set(struct super_block *sb)
12164 +{
12165 +       int err;
12166 +       aufs_bindex_t bindex, bbot;
12167 +       struct au_branch *br;
12168 +
12169 +       err = 0;
12170 +       bbot = au_sbbot(sb);
12171 +       for (bindex = 0; !err && bindex <= bbot; bindex++) {
12172 +               br = au_sbr(sb, bindex);
12173 +               err = au_dr_hino(sb, bindex, /*br*/NULL, &br->br_path);
12174 +       }
12175 +
12176 +       return err;
12177 +}
12178 +
12179 +int au_dr_opt_flush(struct super_block *sb)
12180 +{
12181 +       int err;
12182 +       aufs_bindex_t bindex, bbot;
12183 +       struct au_branch *br;
12184 +
12185 +       err = 0;
12186 +       bbot = au_sbbot(sb);
12187 +       for (bindex = 0; !err && bindex <= bbot; bindex++) {
12188 +               br = au_sbr(sb, bindex);
12189 +               if (au_br_writable(br->br_perm))
12190 +                       err = au_dr_hino(sb, bindex, /*br*/NULL, /*path*/NULL);
12191 +       }
12192 +
12193 +       return err;
12194 +}
12195 +
12196 +int au_dr_opt_clr(struct super_block *sb, int no_flush)
12197 +{
12198 +       int err;
12199 +       aufs_bindex_t bindex, bbot;
12200 +       struct au_branch *br;
12201 +
12202 +       err = 0;
12203 +       if (!no_flush) {
12204 +               err = au_dr_opt_flush(sb);
12205 +               if (unlikely(err))
12206 +                       goto out;
12207 +       }
12208 +
12209 +       bbot = au_sbbot(sb);
12210 +       for (bindex = 0; bindex <= bbot; bindex++) {
12211 +               br = au_sbr(sb, bindex);
12212 +               au_dr_hino_free(&br->br_dirren);
12213 +       }
12214 +
12215 +out:
12216 +       return err;
12217 +}
12218 diff -urN /usr/share/empty/fs/aufs/dirren.h linux/fs/aufs/dirren.h
12219 --- /usr/share/empty/fs/aufs/dirren.h   1970-01-01 01:00:00.000000000 +0100
12220 +++ linux/fs/aufs/dirren.h      2021-11-01 23:48:34.206359262 +0100
12221 @@ -0,0 +1,140 @@
12222 +/* SPDX-License-Identifier: GPL-2.0 */
12223 +/*
12224 + * Copyright (C) 2017-2021 Junjiro R. Okajima
12225 + *
12226 + * This program, aufs is free software; you can redistribute it and/or modify
12227 + * it under the terms of the GNU General Public License as published by
12228 + * the Free Software Foundation; either version 2 of the License, or
12229 + * (at your option) any later version.
12230 + *
12231 + * This program is distributed in the hope that it will be useful,
12232 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12233 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12234 + * GNU General Public License for more details.
12235 + *
12236 + * You should have received a copy of the GNU General Public License
12237 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
12238 + */
12239 +
12240 +/*
12241 + * renamed dir info
12242 + */
12243 +
12244 +#ifndef __AUFS_DIRREN_H__
12245 +#define __AUFS_DIRREN_H__
12246 +
12247 +#ifdef __KERNEL__
12248 +
12249 +#include <linux/dcache.h>
12250 +#include <linux/statfs.h>
12251 +#include <linux/uuid.h>
12252 +#include "hbl.h"
12253 +
12254 +#define AuDirren_NHASH 100
12255 +
12256 +#ifdef CONFIG_AUFS_DIRREN
12257 +enum au_brid_type {
12258 +       AuBrid_Unset,
12259 +       AuBrid_UUID,
12260 +       AuBrid_FSID,
12261 +       AuBrid_DEV
12262 +};
12263 +
12264 +struct au_dr_brid {
12265 +       enum au_brid_type       type;
12266 +       union {
12267 +               uuid_t  uuid;   /* unimplemented yet */
12268 +               fsid_t  fsid;
12269 +               dev_t   dev;
12270 +       };
12271 +};
12272 +
12273 +/* 20 is the max digits length of ulong 64 */
12274 +/* brid-type "_" uuid "_" inum */
12275 +#define AUFS_DIRREN_FNAME_SZ   (1 + 1 + UUID_STRING_LEN + 20)
12276 +#define AUFS_DIRREN_ENV_VAL_SZ (AUFS_DIRREN_FNAME_SZ + 1 + 20)
12277 +
12278 +struct au_dr_hino {
12279 +       struct hlist_bl_node    dr_hnode;
12280 +       ino_t                   dr_h_ino;
12281 +};
12282 +
12283 +struct au_dr_br {
12284 +       struct hlist_bl_head    dr_h_ino[AuDirren_NHASH];
12285 +       struct au_dr_brid       dr_brid;
12286 +};
12287 +
12288 +struct au_dr_lookup {
12289 +       /* dr_name is pointed by struct au_do_lookup_args.name */
12290 +       struct qstr             dr_name; /* subset of dr_info */
12291 +       aufs_bindex_t           ninfo;
12292 +       struct au_drinfo        **drinfo;
12293 +};
12294 +#else
12295 +struct au_dr_hino;
12296 +/* empty */
12297 +struct au_dr_br { };
12298 +struct au_dr_lookup { };
12299 +#endif
12300 +
12301 +/* ---------------------------------------------------------------------- */
12302 +
12303 +struct au_branch;
12304 +struct au_do_lookup_args;
12305 +struct au_hinode;
12306 +#ifdef CONFIG_AUFS_DIRREN
12307 +int au_dr_hino_test_add(struct au_dr_br *dr, ino_t h_ino,
12308 +                       struct au_dr_hino *add_ent);
12309 +void au_dr_hino_free(struct au_dr_br *dr);
12310 +int au_dr_br_init(struct super_block *sb, struct au_branch *br,
12311 +                 const struct path *path);
12312 +int au_dr_br_fin(struct super_block *sb, struct au_branch *br);
12313 +int au_dr_rename(struct dentry *src, aufs_bindex_t bindex,
12314 +                struct qstr *dst_name, void *_rev);
12315 +void au_dr_rename_fin(struct dentry *src, aufs_bindex_t btgt, void *rev);
12316 +void au_dr_rename_rev(struct dentry *src, aufs_bindex_t bindex, void *rev);
12317 +int au_dr_lkup(struct au_do_lookup_args *lkup, struct dentry *dentry,
12318 +              aufs_bindex_t bindex);
12319 +int au_dr_lkup_name(struct au_do_lookup_args *lkup, aufs_bindex_t btgt);
12320 +int au_dr_lkup_h_ino(struct au_do_lookup_args *lkup, aufs_bindex_t bindex,
12321 +                    ino_t h_ino);
12322 +void au_dr_lkup_fin(struct au_do_lookup_args *lkup);
12323 +int au_dr_opt_set(struct super_block *sb);
12324 +int au_dr_opt_flush(struct super_block *sb);
12325 +int au_dr_opt_clr(struct super_block *sb, int no_flush);
12326 +#else
12327 +AuStubInt0(au_dr_hino_test_add, struct au_dr_br *dr, ino_t h_ino,
12328 +          struct au_dr_hino *add_ent);
12329 +AuStubVoid(au_dr_hino_free, struct au_dr_br *dr);
12330 +AuStubInt0(au_dr_br_init, struct super_block *sb, struct au_branch *br,
12331 +          const struct path *path);
12332 +AuStubInt0(au_dr_br_fin, struct super_block *sb, struct au_branch *br);
12333 +AuStubInt0(au_dr_rename, struct dentry *src, aufs_bindex_t bindex,
12334 +          struct qstr *dst_name, void *_rev);
12335 +AuStubVoid(au_dr_rename_fin, struct dentry *src, aufs_bindex_t btgt, void *rev);
12336 +AuStubVoid(au_dr_rename_rev, struct dentry *src, aufs_bindex_t bindex,
12337 +          void *rev);
12338 +AuStubInt0(au_dr_lkup, struct au_do_lookup_args *lkup, struct dentry *dentry,
12339 +          aufs_bindex_t bindex);
12340 +AuStubInt0(au_dr_lkup_name, struct au_do_lookup_args *lkup, aufs_bindex_t btgt);
12341 +AuStubInt0(au_dr_lkup_h_ino, struct au_do_lookup_args *lkup,
12342 +          aufs_bindex_t bindex, ino_t h_ino);
12343 +AuStubVoid(au_dr_lkup_fin, struct au_do_lookup_args *lkup);
12344 +AuStubInt0(au_dr_opt_set, struct super_block *sb);
12345 +AuStubInt0(au_dr_opt_flush, struct super_block *sb);
12346 +AuStubInt0(au_dr_opt_clr, struct super_block *sb, int no_flush);
12347 +#endif
12348 +
12349 +/* ---------------------------------------------------------------------- */
12350 +
12351 +#ifdef CONFIG_AUFS_DIRREN
12352 +static inline int au_dr_ihash(ino_t h_ino)
12353 +{
12354 +       return h_ino % AuDirren_NHASH;
12355 +}
12356 +#else
12357 +AuStubInt0(au_dr_ihash, ino_t h_ino);
12358 +#endif
12359 +
12360 +#endif /* __KERNEL__ */
12361 +#endif /* __AUFS_DIRREN_H__ */
12362 diff -urN /usr/share/empty/fs/aufs/dynop.c linux/fs/aufs/dynop.c
12363 --- /usr/share/empty/fs/aufs/dynop.c    1970-01-01 01:00:00.000000000 +0100
12364 +++ linux/fs/aufs/dynop.c       2021-11-01 23:48:34.206359262 +0100
12365 @@ -0,0 +1,368 @@
12366 +// SPDX-License-Identifier: GPL-2.0
12367 +/*
12368 + * Copyright (C) 2010-2021 Junjiro R. Okajima
12369 + *
12370 + * This program, aufs is free software; you can redistribute it and/or modify
12371 + * it under the terms of the GNU General Public License as published by
12372 + * the Free Software Foundation; either version 2 of the License, or
12373 + * (at your option) any later version.
12374 + *
12375 + * This program is distributed in the hope that it will be useful,
12376 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12377 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12378 + * GNU General Public License for more details.
12379 + *
12380 + * You should have received a copy of the GNU General Public License
12381 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
12382 + */
12383 +
12384 +/*
12385 + * dynamically customizable operations for regular files
12386 + */
12387 +
12388 +#include "aufs.h"
12389 +
12390 +#define DyPrSym(key)   AuDbgSym(key->dk_op.dy_hop)
12391 +
12392 +/*
12393 + * How large will these lists be?
12394 + * Usually just a few elements, 20-30 at most for each, I guess.
12395 + */
12396 +static struct hlist_bl_head dynop[AuDyLast];
12397 +
12398 +static struct au_dykey *dy_gfind_get(struct hlist_bl_head *hbl,
12399 +                                    const void *h_op)
12400 +{
12401 +       struct au_dykey *key, *tmp;
12402 +       struct hlist_bl_node *pos;
12403 +
12404 +       key = NULL;
12405 +       hlist_bl_lock(hbl);
12406 +       hlist_bl_for_each_entry(tmp, pos, hbl, dk_hnode)
12407 +               if (tmp->dk_op.dy_hop == h_op) {
12408 +                       if (kref_get_unless_zero(&tmp->dk_kref))
12409 +                               key = tmp;
12410 +                       break;
12411 +               }
12412 +       hlist_bl_unlock(hbl);
12413 +
12414 +       return key;
12415 +}
12416 +
12417 +static struct au_dykey *dy_bradd(struct au_branch *br, struct au_dykey *key)
12418 +{
12419 +       struct au_dykey **k, *found;
12420 +       const void *h_op = key->dk_op.dy_hop;
12421 +       int i;
12422 +
12423 +       found = NULL;
12424 +       k = br->br_dykey;
12425 +       for (i = 0; i < AuBrDynOp; i++)
12426 +               if (k[i]) {
12427 +                       if (k[i]->dk_op.dy_hop == h_op) {
12428 +                               found = k[i];
12429 +                               break;
12430 +                       }
12431 +               } else
12432 +                       break;
12433 +       if (!found) {
12434 +               spin_lock(&br->br_dykey_lock);
12435 +               for (; i < AuBrDynOp; i++)
12436 +                       if (k[i]) {
12437 +                               if (k[i]->dk_op.dy_hop == h_op) {
12438 +                                       found = k[i];
12439 +                                       break;
12440 +                               }
12441 +                       } else {
12442 +                               k[i] = key;
12443 +                               break;
12444 +                       }
12445 +               spin_unlock(&br->br_dykey_lock);
12446 +               BUG_ON(i == AuBrDynOp); /* expand the array */
12447 +       }
12448 +
12449 +       return found;
12450 +}
12451 +
12452 +/* kref_get() if @key is already added */
12453 +static struct au_dykey *dy_gadd(struct hlist_bl_head *hbl, struct au_dykey *key)
12454 +{
12455 +       struct au_dykey *tmp, *found;
12456 +       struct hlist_bl_node *pos;
12457 +       const void *h_op = key->dk_op.dy_hop;
12458 +
12459 +       found = NULL;
12460 +       hlist_bl_lock(hbl);
12461 +       hlist_bl_for_each_entry(tmp, pos, hbl, dk_hnode)
12462 +               if (tmp->dk_op.dy_hop == h_op) {
12463 +                       if (kref_get_unless_zero(&tmp->dk_kref))
12464 +                               found = tmp;
12465 +                       break;
12466 +               }
12467 +       if (!found)
12468 +               hlist_bl_add_head(&key->dk_hnode, hbl);
12469 +       hlist_bl_unlock(hbl);
12470 +
12471 +       if (!found)
12472 +               DyPrSym(key);
12473 +       return found;
12474 +}
12475 +
12476 +static void dy_free_rcu(struct rcu_head *rcu)
12477 +{
12478 +       struct au_dykey *key;
12479 +
12480 +       key = container_of(rcu, struct au_dykey, dk_rcu);
12481 +       DyPrSym(key);
12482 +       kfree(key);
12483 +}
12484 +
12485 +static void dy_free(struct kref *kref)
12486 +{
12487 +       struct au_dykey *key;
12488 +       struct hlist_bl_head *hbl;
12489 +
12490 +       key = container_of(kref, struct au_dykey, dk_kref);
12491 +       hbl = dynop + key->dk_op.dy_type;
12492 +       au_hbl_del(&key->dk_hnode, hbl);
12493 +       call_rcu(&key->dk_rcu, dy_free_rcu);
12494 +}
12495 +
12496 +void au_dy_put(struct au_dykey *key)
12497 +{
12498 +       kref_put(&key->dk_kref, dy_free);
12499 +}
12500 +
12501 +/* ---------------------------------------------------------------------- */
12502 +
12503 +#define DyDbgSize(cnt, op)     AuDebugOn(cnt != sizeof(op)/sizeof(void *))
12504 +
12505 +#ifdef CONFIG_AUFS_DEBUG
12506 +#define DyDbgDeclare(cnt)      unsigned int cnt = 0
12507 +#define DyDbgInc(cnt)          do { cnt++; } while (0)
12508 +#else
12509 +#define DyDbgDeclare(cnt)      do {} while (0)
12510 +#define DyDbgInc(cnt)          do {} while (0)
12511 +#endif
12512 +
12513 +#define DySet(func, dst, src, h_op, h_sb) do {                         \
12514 +       DyDbgInc(cnt);                                                  \
12515 +       if (h_op->func) {                                               \
12516 +               if (src.func)                                           \
12517 +                       dst.func = src.func;                            \
12518 +               else                                                    \
12519 +                       AuDbg("%s %s\n", au_sbtype(h_sb), #func);       \
12520 +       }                                                               \
12521 +} while (0)
12522 +
12523 +#define DySetForce(func, dst, src) do {                \
12524 +       AuDebugOn(!src.func);                   \
12525 +       DyDbgInc(cnt);                          \
12526 +       dst.func = src.func;                    \
12527 +} while (0)
12528 +
12529 +#define DySetAop(func) \
12530 +       DySet(func, dyaop->da_op, aufs_aop, h_aop, h_sb)
12531 +#define DySetAopForce(func) \
12532 +       DySetForce(func, dyaop->da_op, aufs_aop)
12533 +
12534 +static void dy_aop(struct au_dykey *key, const void *h_op,
12535 +                  struct super_block *h_sb __maybe_unused)
12536 +{
12537 +       struct au_dyaop *dyaop = (void *)key;
12538 +       const struct address_space_operations *h_aop = h_op;
12539 +       DyDbgDeclare(cnt);
12540 +
12541 +       AuDbg("%s\n", au_sbtype(h_sb));
12542 +
12543 +       DySetAop(writepage);
12544 +       DySetAopForce(readpage);        /* force */
12545 +       DySetAop(writepages);
12546 +       DySetAop(set_page_dirty);
12547 +       DySetAop(readpages);
12548 +       DySetAop(readahead);
12549 +       DySetAop(write_begin);
12550 +       DySetAop(write_end);
12551 +       DySetAop(bmap);
12552 +       DySetAop(invalidatepage);
12553 +       DySetAop(releasepage);
12554 +       DySetAop(freepage);
12555 +       /* this one will be changed according to an aufs mount option */
12556 +       DySetAop(direct_IO);
12557 +       DySetAop(migratepage);
12558 +       DySetAop(isolate_page);
12559 +       DySetAop(putback_page);
12560 +       DySetAop(launder_page);
12561 +       DySetAop(is_partially_uptodate);
12562 +       DySetAop(is_dirty_writeback);
12563 +       DySetAop(error_remove_page);
12564 +       DySetAop(swap_activate);
12565 +       DySetAop(swap_deactivate);
12566 +
12567 +       DyDbgSize(cnt, *h_aop);
12568 +}
12569 +
12570 +/* ---------------------------------------------------------------------- */
12571 +
12572 +static void dy_bug(struct kref *kref)
12573 +{
12574 +       BUG();
12575 +}
12576 +
12577 +static struct au_dykey *dy_get(struct au_dynop *op, struct au_branch *br)
12578 +{
12579 +       struct au_dykey *key, *old;
12580 +       struct hlist_bl_head *hbl;
12581 +       struct op {
12582 +               unsigned int sz;
12583 +               void (*set)(struct au_dykey *key, const void *h_op,
12584 +                           struct super_block *h_sb __maybe_unused);
12585 +       };
12586 +       static const struct op a[] = {
12587 +               [AuDy_AOP] = {
12588 +                       .sz     = sizeof(struct au_dyaop),
12589 +                       .set    = dy_aop
12590 +               }
12591 +       };
12592 +       const struct op *p;
12593 +
12594 +       hbl = dynop + op->dy_type;
12595 +       key = dy_gfind_get(hbl, op->dy_hop);
12596 +       if (key)
12597 +               goto out_add; /* success */
12598 +
12599 +       p = a + op->dy_type;
12600 +       key = kzalloc(p->sz, GFP_NOFS);
12601 +       if (unlikely(!key)) {
12602 +               key = ERR_PTR(-ENOMEM);
12603 +               goto out;
12604 +       }
12605 +
12606 +       key->dk_op.dy_hop = op->dy_hop;
12607 +       kref_init(&key->dk_kref);
12608 +       p->set(key, op->dy_hop, au_br_sb(br));
12609 +       old = dy_gadd(hbl, key);
12610 +       if (old) {
12611 +               au_kfree_rcu(key);
12612 +               key = old;
12613 +       }
12614 +
12615 +out_add:
12616 +       old = dy_bradd(br, key);
12617 +       if (old)
12618 +               /* its ref-count should never be zero here */
12619 +               kref_put(&key->dk_kref, dy_bug);
12620 +out:
12621 +       return key;
12622 +}
12623 +
12624 +/* ---------------------------------------------------------------------- */
12625 +/*
12626 + * Aufs prohibits O_DIRECT by default even if the branch supports it.
12627 + * This behaviour is necessary to return an error from open(O_DIRECT) instead
12628 + * of the succeeding I/O. The dio mount option enables O_DIRECT and makes
12629 + * open(O_DIRECT) always succeed, but the succeeding I/O may return an error.
12630 + * See the aufs manual in detail.
12631 + */
12632 +static void dy_adx(struct au_dyaop *dyaop, int do_dx)
12633 +{
12634 +       if (!do_dx)
12635 +               dyaop->da_op.direct_IO = NULL;
12636 +       else
12637 +               dyaop->da_op.direct_IO = aufs_aop.direct_IO;
12638 +}
12639 +
12640 +static struct au_dyaop *dy_aget(struct au_branch *br,
12641 +                               const struct address_space_operations *h_aop,
12642 +                               int do_dx)
12643 +{
12644 +       struct au_dyaop *dyaop;
12645 +       struct au_dynop op;
12646 +
12647 +       op.dy_type = AuDy_AOP;
12648 +       op.dy_haop = h_aop;
12649 +       dyaop = (void *)dy_get(&op, br);
12650 +       if (IS_ERR(dyaop))
12651 +               goto out;
12652 +       dy_adx(dyaop, do_dx);
12653 +
12654 +out:
12655 +       return dyaop;
12656 +}
12657 +
12658 +int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
12659 +               struct inode *h_inode)
12660 +{
12661 +       int err, do_dx;
12662 +       struct super_block *sb;
12663 +       struct au_branch *br;
12664 +       struct au_dyaop *dyaop;
12665 +
12666 +       AuDebugOn(!S_ISREG(h_inode->i_mode));
12667 +       IiMustWriteLock(inode);
12668 +
12669 +       sb = inode->i_sb;
12670 +       br = au_sbr(sb, bindex);
12671 +       do_dx = !!au_opt_test(au_mntflags(sb), DIO);
12672 +       dyaop = dy_aget(br, h_inode->i_mapping->a_ops, do_dx);
12673 +       err = PTR_ERR(dyaop);
12674 +       if (IS_ERR(dyaop))
12675 +               /* unnecessary to call dy_fput() */
12676 +               goto out;
12677 +
12678 +       err = 0;
12679 +       inode->i_mapping->a_ops = &dyaop->da_op;
12680 +
12681 +out:
12682 +       return err;
12683 +}
12684 +
12685 +/*
12686 + * Is it safe to replace a_ops during the inode/file is in operation?
12687 + * Yes, I hope so.
12688 + */
12689 +int au_dy_irefresh(struct inode *inode)
12690 +{
12691 +       int err;
12692 +       aufs_bindex_t btop;
12693 +       struct inode *h_inode;
12694 +
12695 +       err = 0;
12696 +       if (S_ISREG(inode->i_mode)) {
12697 +               btop = au_ibtop(inode);
12698 +               h_inode = au_h_iptr(inode, btop);
12699 +               err = au_dy_iaop(inode, btop, h_inode);
12700 +       }
12701 +       return err;
12702 +}
12703 +
12704 +void au_dy_arefresh(int do_dx)
12705 +{
12706 +       struct hlist_bl_head *hbl;
12707 +       struct hlist_bl_node *pos;
12708 +       struct au_dykey *key;
12709 +
12710 +       hbl = dynop + AuDy_AOP;
12711 +       hlist_bl_lock(hbl);
12712 +       hlist_bl_for_each_entry(key, pos, hbl, dk_hnode)
12713 +               dy_adx((void *)key, do_dx);
12714 +       hlist_bl_unlock(hbl);
12715 +}
12716 +
12717 +/* ---------------------------------------------------------------------- */
12718 +
12719 +void __init au_dy_init(void)
12720 +{
12721 +       int i;
12722 +
12723 +       for (i = 0; i < AuDyLast; i++)
12724 +               INIT_HLIST_BL_HEAD(dynop + i);
12725 +}
12726 +
12727 +void au_dy_fin(void)
12728 +{
12729 +       int i;
12730 +
12731 +       for (i = 0; i < AuDyLast; i++)
12732 +               WARN_ON(!hlist_bl_empty(dynop + i));
12733 +}
12734 diff -urN /usr/share/empty/fs/aufs/dynop.h linux/fs/aufs/dynop.h
12735 --- /usr/share/empty/fs/aufs/dynop.h    1970-01-01 01:00:00.000000000 +0100
12736 +++ linux/fs/aufs/dynop.h       2021-11-01 23:48:34.206359262 +0100
12737 @@ -0,0 +1,77 @@
12738 +/* SPDX-License-Identifier: GPL-2.0 */
12739 +/*
12740 + * Copyright (C) 2010-2021 Junjiro R. Okajima
12741 + *
12742 + * This program, aufs is free software; you can redistribute it and/or modify
12743 + * it under the terms of the GNU General Public License as published by
12744 + * the Free Software Foundation; either version 2 of the License, or
12745 + * (at your option) any later version.
12746 + *
12747 + * This program is distributed in the hope that it will be useful,
12748 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12749 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12750 + * GNU General Public License for more details.
12751 + *
12752 + * You should have received a copy of the GNU General Public License
12753 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
12754 + */
12755 +
12756 +/*
12757 + * dynamically customizable operations (for regular files only)
12758 + */
12759 +
12760 +#ifndef __AUFS_DYNOP_H__
12761 +#define __AUFS_DYNOP_H__
12762 +
12763 +#ifdef __KERNEL__
12764 +
12765 +#include <linux/fs.h>
12766 +#include <linux/kref.h>
12767 +
12768 +enum {AuDy_AOP, AuDyLast};
12769 +
12770 +struct au_dynop {
12771 +       int                                             dy_type;
12772 +       union {
12773 +               const void                              *dy_hop;
12774 +               const struct address_space_operations   *dy_haop;
12775 +       };
12776 +};
12777 +
12778 +struct au_dykey {
12779 +       union {
12780 +               struct hlist_bl_node    dk_hnode;
12781 +               struct rcu_head         dk_rcu;
12782 +       };
12783 +       struct au_dynop         dk_op;
12784 +
12785 +       /*
12786 +        * during I am in the branch local array, kref is gotten. when the
12787 +        * branch is removed, kref is put.
12788 +        */
12789 +       struct kref             dk_kref;
12790 +};
12791 +
12792 +/* stop unioning since their sizes are very different from each other */
12793 +struct au_dyaop {
12794 +       struct au_dykey                 da_key;
12795 +       struct address_space_operations da_op; /* not const */
12796 +};
12797 +/* make sure that 'struct au_dykey *' can be any type */
12798 +static_assert(!offsetof(struct au_dyaop, da_key));
12799 +
12800 +/* ---------------------------------------------------------------------- */
12801 +
12802 +/* dynop.c */
12803 +struct au_branch;
12804 +void au_dy_put(struct au_dykey *key);
12805 +int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
12806 +               struct inode *h_inode);
12807 +int au_dy_irefresh(struct inode *inode);
12808 +void au_dy_arefresh(int do_dio);
12809 +
12810 +void __init au_dy_init(void);
12811 +void au_dy_fin(void);
12812 +
12813 +#endif /* __KERNEL__ */
12814 +#endif /* __AUFS_DYNOP_H__ */
12815 diff -urN /usr/share/empty/fs/aufs/export.c linux/fs/aufs/export.c
12816 --- /usr/share/empty/fs/aufs/export.c   1970-01-01 01:00:00.000000000 +0100
12817 +++ linux/fs/aufs/export.c      2021-11-01 23:48:34.206359262 +0100
12818 @@ -0,0 +1,831 @@
12819 +// SPDX-License-Identifier: GPL-2.0
12820 +/*
12821 + * Copyright (C) 2005-2021 Junjiro R. Okajima
12822 + *
12823 + * This program, aufs is free software; you can redistribute it and/or modify
12824 + * it under the terms of the GNU General Public License as published by
12825 + * the Free Software Foundation; either version 2 of the License, or
12826 + * (at your option) any later version.
12827 + *
12828 + * This program is distributed in the hope that it will be useful,
12829 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12830 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12831 + * GNU General Public License for more details.
12832 + *
12833 + * You should have received a copy of the GNU General Public License
12834 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
12835 + */
12836 +
12837 +/*
12838 + * export via nfs
12839 + */
12840 +
12841 +#include <linux/exportfs.h>
12842 +#include <linux/fs_struct.h>
12843 +#include <linux/namei.h>
12844 +#include <linux/nsproxy.h>
12845 +#include <linux/random.h>
12846 +#include <linux/writeback.h>
12847 +#include "aufs.h"
12848 +
12849 +union conv {
12850 +#ifdef CONFIG_AUFS_INO_T_64
12851 +       __u32 a[2];
12852 +#else
12853 +       __u32 a[1];
12854 +#endif
12855 +       ino_t ino;
12856 +};
12857 +
12858 +static ino_t decode_ino(__u32 *a)
12859 +{
12860 +       union conv u;
12861 +
12862 +       BUILD_BUG_ON(sizeof(u.ino) != sizeof(u.a));
12863 +       u.a[0] = a[0];
12864 +#ifdef CONFIG_AUFS_INO_T_64
12865 +       u.a[1] = a[1];
12866 +#endif
12867 +       return u.ino;
12868 +}
12869 +
12870 +static void encode_ino(__u32 *a, ino_t ino)
12871 +{
12872 +       union conv u;
12873 +
12874 +       u.ino = ino;
12875 +       a[0] = u.a[0];
12876 +#ifdef CONFIG_AUFS_INO_T_64
12877 +       a[1] = u.a[1];
12878 +#endif
12879 +}
12880 +
12881 +/* NFS file handle */
12882 +enum {
12883 +       Fh_br_id,
12884 +       Fh_sigen,
12885 +#ifdef CONFIG_AUFS_INO_T_64
12886 +       /* support 64bit inode number */
12887 +       Fh_ino1,
12888 +       Fh_ino2,
12889 +       Fh_dir_ino1,
12890 +       Fh_dir_ino2,
12891 +#else
12892 +       Fh_ino1,
12893 +       Fh_dir_ino1,
12894 +#endif
12895 +       Fh_igen,
12896 +       Fh_h_type,
12897 +       Fh_tail,
12898 +
12899 +       Fh_ino = Fh_ino1,
12900 +       Fh_dir_ino = Fh_dir_ino1
12901 +};
12902 +
12903 +static int au_test_anon(struct dentry *dentry)
12904 +{
12905 +       /* note: read d_flags without d_lock */
12906 +       return !!(dentry->d_flags & DCACHE_DISCONNECTED);
12907 +}
12908 +
12909 +int au_test_nfsd(void)
12910 +{
12911 +       int ret;
12912 +       struct task_struct *tsk = current;
12913 +       char comm[sizeof(tsk->comm)];
12914 +
12915 +       ret = 0;
12916 +       if (tsk->flags & PF_KTHREAD) {
12917 +               get_task_comm(comm, tsk);
12918 +               ret = !strcmp(comm, "nfsd");
12919 +       }
12920 +
12921 +       return ret;
12922 +}
12923 +
12924 +/* ---------------------------------------------------------------------- */
12925 +/* inode generation external table */
12926 +
12927 +void au_xigen_inc(struct inode *inode)
12928 +{
12929 +       loff_t pos;
12930 +       ssize_t sz;
12931 +       __u32 igen;
12932 +       struct super_block *sb;
12933 +       struct au_sbinfo *sbinfo;
12934 +
12935 +       sb = inode->i_sb;
12936 +       AuDebugOn(!au_opt_test(au_mntflags(sb), XINO));
12937 +
12938 +       sbinfo = au_sbi(sb);
12939 +       pos = inode->i_ino;
12940 +       pos *= sizeof(igen);
12941 +       igen = inode->i_generation + 1;
12942 +       sz = xino_fwrite(sbinfo->si_xigen, &igen, sizeof(igen), &pos);
12943 +       if (sz == sizeof(igen))
12944 +               return; /* success */
12945 +
12946 +       if (unlikely(sz >= 0))
12947 +               AuIOErr("xigen error (%zd)\n", sz);
12948 +}
12949 +
12950 +int au_xigen_new(struct inode *inode)
12951 +{
12952 +       int err;
12953 +       loff_t pos;
12954 +       ssize_t sz;
12955 +       struct super_block *sb;
12956 +       struct au_sbinfo *sbinfo;
12957 +       struct file *file;
12958 +
12959 +       err = 0;
12960 +       /* todo: dirty, at mount time */
12961 +       if (inode->i_ino == AUFS_ROOT_INO)
12962 +               goto out;
12963 +       sb = inode->i_sb;
12964 +       SiMustAnyLock(sb);
12965 +       if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
12966 +               goto out;
12967 +
12968 +       err = -EFBIG;
12969 +       pos = inode->i_ino;
12970 +       if (unlikely(au_loff_max / sizeof(inode->i_generation) - 1 < pos)) {
12971 +               AuIOErr1("too large i%lld\n", pos);
12972 +               goto out;
12973 +       }
12974 +       pos *= sizeof(inode->i_generation);
12975 +
12976 +       err = 0;
12977 +       sbinfo = au_sbi(sb);
12978 +       file = sbinfo->si_xigen;
12979 +       BUG_ON(!file);
12980 +
12981 +       if (vfsub_f_size_read(file)
12982 +           < pos + sizeof(inode->i_generation)) {
12983 +               inode->i_generation = atomic_inc_return(&sbinfo->si_xigen_next);
12984 +               sz = xino_fwrite(file, &inode->i_generation,
12985 +                                sizeof(inode->i_generation), &pos);
12986 +       } else
12987 +               sz = xino_fread(file, &inode->i_generation,
12988 +                               sizeof(inode->i_generation), &pos);
12989 +       if (sz == sizeof(inode->i_generation))
12990 +               goto out; /* success */
12991 +
12992 +       err = sz;
12993 +       if (unlikely(sz >= 0)) {
12994 +               err = -EIO;
12995 +               AuIOErr("xigen error (%zd)\n", sz);
12996 +       }
12997 +
12998 +out:
12999 +       return err;
13000 +}
13001 +
13002 +int au_xigen_set(struct super_block *sb, struct path *path)
13003 +{
13004 +       int err;
13005 +       struct au_sbinfo *sbinfo;
13006 +       struct file *file;
13007 +
13008 +       SiMustWriteLock(sb);
13009 +
13010 +       sbinfo = au_sbi(sb);
13011 +       file = au_xino_create2(sb, path, sbinfo->si_xigen);
13012 +       err = PTR_ERR(file);
13013 +       if (IS_ERR(file))
13014 +               goto out;
13015 +       err = 0;
13016 +       if (sbinfo->si_xigen)
13017 +               fput(sbinfo->si_xigen);
13018 +       sbinfo->si_xigen = file;
13019 +
13020 +out:
13021 +       AuTraceErr(err);
13022 +       return err;
13023 +}
13024 +
13025 +void au_xigen_clr(struct super_block *sb)
13026 +{
13027 +       struct au_sbinfo *sbinfo;
13028 +
13029 +       SiMustWriteLock(sb);
13030 +
13031 +       sbinfo = au_sbi(sb);
13032 +       if (sbinfo->si_xigen) {
13033 +               fput(sbinfo->si_xigen);
13034 +               sbinfo->si_xigen = NULL;
13035 +       }
13036 +}
13037 +
13038 +/* ---------------------------------------------------------------------- */
13039 +
13040 +static struct dentry *decode_by_ino(struct super_block *sb, ino_t ino,
13041 +                                   ino_t dir_ino)
13042 +{
13043 +       struct dentry *dentry, *d;
13044 +       struct inode *inode;
13045 +       unsigned int sigen;
13046 +
13047 +       dentry = NULL;
13048 +       inode = ilookup(sb, ino);
13049 +       if (!inode)
13050 +               goto out;
13051 +
13052 +       dentry = ERR_PTR(-ESTALE);
13053 +       sigen = au_sigen(sb);
13054 +       if (unlikely(au_is_bad_inode(inode)
13055 +                    || IS_DEADDIR(inode)
13056 +                    || sigen != au_iigen(inode, NULL)))
13057 +               goto out_iput;
13058 +
13059 +       dentry = NULL;
13060 +       if (!dir_ino || S_ISDIR(inode->i_mode))
13061 +               dentry = d_find_alias(inode);
13062 +       else {
13063 +               spin_lock(&inode->i_lock);
13064 +               hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
13065 +                       spin_lock(&d->d_lock);
13066 +                       if (!au_test_anon(d)
13067 +                           && d_inode(d->d_parent)->i_ino == dir_ino) {
13068 +                               dentry = dget_dlock(d);
13069 +                               spin_unlock(&d->d_lock);
13070 +                               break;
13071 +                       }
13072 +                       spin_unlock(&d->d_lock);
13073 +               }
13074 +               spin_unlock(&inode->i_lock);
13075 +       }
13076 +       if (unlikely(dentry && au_digen_test(dentry, sigen))) {
13077 +               /* need to refresh */
13078 +               dput(dentry);
13079 +               dentry = NULL;
13080 +       }
13081 +
13082 +out_iput:
13083 +       iput(inode);
13084 +out:
13085 +       AuTraceErrPtr(dentry);
13086 +       return dentry;
13087 +}
13088 +
13089 +/* ---------------------------------------------------------------------- */
13090 +
13091 +/* todo: dirty? */
13092 +/* if exportfs_decode_fh() passed vfsmount*, we could be happy */
13093 +
13094 +struct au_compare_mnt_args {
13095 +       /* input */
13096 +       struct super_block *sb;
13097 +
13098 +       /* output */
13099 +       struct vfsmount *mnt;
13100 +};
13101 +
13102 +static int au_compare_mnt(struct vfsmount *mnt, void *arg)
13103 +{
13104 +       struct au_compare_mnt_args *a = arg;
13105 +
13106 +       if (mnt->mnt_sb != a->sb)
13107 +               return 0;
13108 +       a->mnt = mntget(mnt);
13109 +       return 1;
13110 +}
13111 +
13112 +static struct vfsmount *au_mnt_get(struct super_block *sb)
13113 +{
13114 +       int err;
13115 +       struct path root;
13116 +       struct au_compare_mnt_args args = {
13117 +               .sb = sb
13118 +       };
13119 +
13120 +       get_fs_root(current->fs, &root);
13121 +       rcu_read_lock();
13122 +       err = iterate_mounts(au_compare_mnt, &args, root.mnt);
13123 +       rcu_read_unlock();
13124 +       path_put(&root);
13125 +       AuDebugOn(!err);
13126 +       AuDebugOn(!args.mnt);
13127 +       return args.mnt;
13128 +}
13129 +
13130 +struct au_nfsd_si_lock {
13131 +       unsigned int sigen;
13132 +       aufs_bindex_t bindex, br_id;
13133 +       unsigned char force_lock;
13134 +};
13135 +
13136 +static int si_nfsd_read_lock(struct super_block *sb,
13137 +                            struct au_nfsd_si_lock *nsi_lock)
13138 +{
13139 +       int err;
13140 +       aufs_bindex_t bindex;
13141 +
13142 +       si_read_lock(sb, AuLock_FLUSH);
13143 +
13144 +       /* branch id may be wrapped around */
13145 +       err = 0;
13146 +       bindex = au_br_index(sb, nsi_lock->br_id);
13147 +       if (bindex >= 0 && nsi_lock->sigen + AUFS_BRANCH_MAX > au_sigen(sb))
13148 +               goto out; /* success */
13149 +
13150 +       err = -ESTALE;
13151 +       bindex = -1;
13152 +       if (!nsi_lock->force_lock)
13153 +               si_read_unlock(sb);
13154 +
13155 +out:
13156 +       nsi_lock->bindex = bindex;
13157 +       return err;
13158 +}
13159 +
13160 +struct find_name_by_ino {
13161 +       struct dir_context ctx;
13162 +       int called, found;
13163 +       ino_t ino;
13164 +       char *name;
13165 +       int namelen;
13166 +};
13167 +
13168 +static int
13169 +find_name_by_ino(struct dir_context *ctx, const char *name, int namelen,
13170 +                loff_t offset, u64 ino, unsigned int d_type)
13171 +{
13172 +       struct find_name_by_ino *a = container_of(ctx, struct find_name_by_ino,
13173 +                                                 ctx);
13174 +
13175 +       a->called++;
13176 +       if (a->ino != ino)
13177 +               return 0;
13178 +
13179 +       memcpy(a->name, name, namelen);
13180 +       a->namelen = namelen;
13181 +       a->found = 1;
13182 +       return 1;
13183 +}
13184 +
13185 +static struct dentry *au_lkup_by_ino(struct path *path, ino_t ino,
13186 +                                    struct au_nfsd_si_lock *nsi_lock)
13187 +{
13188 +       struct dentry *dentry, *parent;
13189 +       struct file *file;
13190 +       struct inode *dir;
13191 +       struct find_name_by_ino arg = {
13192 +               .ctx = {
13193 +                       .actor = find_name_by_ino
13194 +               }
13195 +       };
13196 +       int err;
13197 +
13198 +       parent = path->dentry;
13199 +       if (nsi_lock)
13200 +               si_read_unlock(parent->d_sb);
13201 +       file = vfsub_dentry_open(path, au_dir_roflags);
13202 +       dentry = (void *)file;
13203 +       if (IS_ERR(file))
13204 +               goto out;
13205 +
13206 +       dentry = ERR_PTR(-ENOMEM);
13207 +       arg.name = (void *)__get_free_page(GFP_NOFS);
13208 +       if (unlikely(!arg.name))
13209 +               goto out_file;
13210 +       arg.ino = ino;
13211 +       arg.found = 0;
13212 +       do {
13213 +               arg.called = 0;
13214 +               /* smp_mb(); */
13215 +               err = vfsub_iterate_dir(file, &arg.ctx);
13216 +       } while (!err && !arg.found && arg.called);
13217 +       dentry = ERR_PTR(err);
13218 +       if (unlikely(err))
13219 +               goto out_name;
13220 +       /* instead of ENOENT */
13221 +       dentry = ERR_PTR(-ESTALE);
13222 +       if (!arg.found)
13223 +               goto out_name;
13224 +
13225 +       /* do not call vfsub_lkup_one() */
13226 +       dir = d_inode(parent);
13227 +       dentry = vfsub_lookup_one_len_unlocked(arg.name, path, arg.namelen);
13228 +       AuTraceErrPtr(dentry);
13229 +       if (IS_ERR(dentry))
13230 +               goto out_name;
13231 +       AuDebugOn(au_test_anon(dentry));
13232 +       if (unlikely(d_really_is_negative(dentry))) {
13233 +               dput(dentry);
13234 +               dentry = ERR_PTR(-ENOENT);
13235 +       }
13236 +
13237 +out_name:
13238 +       free_page((unsigned long)arg.name);
13239 +out_file:
13240 +       fput(file);
13241 +out:
13242 +       if (unlikely(nsi_lock
13243 +                    && si_nfsd_read_lock(parent->d_sb, nsi_lock) < 0))
13244 +               if (!IS_ERR(dentry)) {
13245 +                       dput(dentry);
13246 +                       dentry = ERR_PTR(-ESTALE);
13247 +               }
13248 +       AuTraceErrPtr(dentry);
13249 +       return dentry;
13250 +}
13251 +
13252 +static struct dentry *decode_by_dir_ino(struct super_block *sb, ino_t ino,
13253 +                                       ino_t dir_ino,
13254 +                                       struct au_nfsd_si_lock *nsi_lock)
13255 +{
13256 +       struct dentry *dentry;
13257 +       struct path path;
13258 +
13259 +       if (dir_ino != AUFS_ROOT_INO) {
13260 +               path.dentry = decode_by_ino(sb, dir_ino, 0);
13261 +               dentry = path.dentry;
13262 +               if (!path.dentry || IS_ERR(path.dentry))
13263 +                       goto out;
13264 +               AuDebugOn(au_test_anon(path.dentry));
13265 +       } else
13266 +               path.dentry = dget(sb->s_root);
13267 +
13268 +       path.mnt = au_mnt_get(sb);
13269 +       dentry = au_lkup_by_ino(&path, ino, nsi_lock);
13270 +       path_put(&path);
13271 +
13272 +out:
13273 +       AuTraceErrPtr(dentry);
13274 +       return dentry;
13275 +}
13276 +
13277 +/* ---------------------------------------------------------------------- */
13278 +
13279 +static int h_acceptable(void *expv, struct dentry *dentry)
13280 +{
13281 +       return 1;
13282 +}
13283 +
13284 +static char *au_build_path(struct dentry *h_parent, struct path *h_rootpath,
13285 +                          char *buf, int len, struct super_block *sb)
13286 +{
13287 +       char *p;
13288 +       int n;
13289 +       struct path path;
13290 +
13291 +       p = d_path(h_rootpath, buf, len);
13292 +       if (IS_ERR(p))
13293 +               goto out;
13294 +       n = strlen(p);
13295 +
13296 +       path.mnt = h_rootpath->mnt;
13297 +       path.dentry = h_parent;
13298 +       p = d_path(&path, buf, len);
13299 +       if (IS_ERR(p))
13300 +               goto out;
13301 +       if (n != 1)
13302 +               p += n;
13303 +
13304 +       path.mnt = au_mnt_get(sb);
13305 +       path.dentry = sb->s_root;
13306 +       p = d_path(&path, buf, len - strlen(p));
13307 +       mntput(path.mnt);
13308 +       if (IS_ERR(p))
13309 +               goto out;
13310 +       if (n != 1)
13311 +               p[strlen(p)] = '/';
13312 +
13313 +out:
13314 +       AuTraceErrPtr(p);
13315 +       return p;
13316 +}
13317 +
13318 +static
13319 +struct dentry *decode_by_path(struct super_block *sb, ino_t ino, __u32 *fh,
13320 +                             int fh_len, struct au_nfsd_si_lock *nsi_lock)
13321 +{
13322 +       struct dentry *dentry, *h_parent, *root;
13323 +       struct super_block *h_sb;
13324 +       char *pathname, *p;
13325 +       struct vfsmount *h_mnt;
13326 +       struct au_branch *br;
13327 +       int err;
13328 +       struct path path;
13329 +
13330 +       br = au_sbr(sb, nsi_lock->bindex);
13331 +       h_mnt = au_br_mnt(br);
13332 +       h_sb = h_mnt->mnt_sb;
13333 +       /* todo: call lower fh_to_dentry()? fh_to_parent()? */
13334 +       lockdep_off();
13335 +       h_parent = exportfs_decode_fh(h_mnt, (void *)(fh + Fh_tail),
13336 +                                     fh_len - Fh_tail, fh[Fh_h_type],
13337 +                                     h_acceptable, /*context*/NULL);
13338 +       lockdep_on();
13339 +       dentry = h_parent;
13340 +       if (unlikely(!h_parent || IS_ERR(h_parent))) {
13341 +               AuWarn1("%s decode_fh failed, %ld\n",
13342 +                       au_sbtype(h_sb), PTR_ERR(h_parent));
13343 +               goto out;
13344 +       }
13345 +       dentry = NULL;
13346 +       if (unlikely(au_test_anon(h_parent))) {
13347 +               AuWarn1("%s decode_fh returned a disconnected dentry\n",
13348 +                       au_sbtype(h_sb));
13349 +               goto out_h_parent;
13350 +       }
13351 +
13352 +       dentry = ERR_PTR(-ENOMEM);
13353 +       pathname = (void *)__get_free_page(GFP_NOFS);
13354 +       if (unlikely(!pathname))
13355 +               goto out_h_parent;
13356 +
13357 +       root = sb->s_root;
13358 +       path.mnt = h_mnt;
13359 +       di_read_lock_parent(root, !AuLock_IR);
13360 +       path.dentry = au_h_dptr(root, nsi_lock->bindex);
13361 +       di_read_unlock(root, !AuLock_IR);
13362 +       p = au_build_path(h_parent, &path, pathname, PAGE_SIZE, sb);
13363 +       dentry = (void *)p;
13364 +       if (IS_ERR(p))
13365 +               goto out_pathname;
13366 +
13367 +       si_read_unlock(sb);
13368 +       err = vfsub_kern_path(p, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &path);
13369 +       dentry = ERR_PTR(err);
13370 +       if (unlikely(err))
13371 +               goto out_relock;
13372 +
13373 +       dentry = ERR_PTR(-ENOENT);
13374 +       AuDebugOn(au_test_anon(path.dentry));
13375 +       if (unlikely(d_really_is_negative(path.dentry)))
13376 +               goto out_path;
13377 +
13378 +       if (ino != d_inode(path.dentry)->i_ino)
13379 +               dentry = au_lkup_by_ino(&path, ino, /*nsi_lock*/NULL);
13380 +       else
13381 +               dentry = dget(path.dentry);
13382 +
13383 +out_path:
13384 +       path_put(&path);
13385 +out_relock:
13386 +       if (unlikely(si_nfsd_read_lock(sb, nsi_lock) < 0))
13387 +               if (!IS_ERR(dentry)) {
13388 +                       dput(dentry);
13389 +                       dentry = ERR_PTR(-ESTALE);
13390 +               }
13391 +out_pathname:
13392 +       free_page((unsigned long)pathname);
13393 +out_h_parent:
13394 +       dput(h_parent);
13395 +out:
13396 +       AuTraceErrPtr(dentry);
13397 +       return dentry;
13398 +}
13399 +
13400 +/* ---------------------------------------------------------------------- */
13401 +
13402 +static struct dentry *
13403 +aufs_fh_to_dentry(struct super_block *sb, struct fid *fid, int fh_len,
13404 +                 int fh_type)
13405 +{
13406 +       struct dentry *dentry;
13407 +       __u32 *fh = fid->raw;
13408 +       struct au_branch *br;
13409 +       ino_t ino, dir_ino;
13410 +       struct au_nfsd_si_lock nsi_lock = {
13411 +               .force_lock     = 0
13412 +       };
13413 +
13414 +       dentry = ERR_PTR(-ESTALE);
13415 +       /* it should never happen, but the file handle is unreliable */
13416 +       if (unlikely(fh_len < Fh_tail))
13417 +               goto out;
13418 +       nsi_lock.sigen = fh[Fh_sigen];
13419 +       nsi_lock.br_id = fh[Fh_br_id];
13420 +
13421 +       /* branch id may be wrapped around */
13422 +       br = NULL;
13423 +       if (unlikely(si_nfsd_read_lock(sb, &nsi_lock)))
13424 +               goto out;
13425 +       nsi_lock.force_lock = 1;
13426 +
13427 +       /* is this inode still cached? */
13428 +       ino = decode_ino(fh + Fh_ino);
13429 +       /* it should never happen */
13430 +       if (unlikely(ino == AUFS_ROOT_INO))
13431 +               goto out_unlock;
13432 +
13433 +       dir_ino = decode_ino(fh + Fh_dir_ino);
13434 +       dentry = decode_by_ino(sb, ino, dir_ino);
13435 +       if (IS_ERR(dentry))
13436 +               goto out_unlock;
13437 +       if (dentry)
13438 +               goto accept;
13439 +
13440 +       /* is the parent dir cached? */
13441 +       br = au_sbr(sb, nsi_lock.bindex);
13442 +       au_lcnt_inc(&br->br_nfiles);
13443 +       dentry = decode_by_dir_ino(sb, ino, dir_ino, &nsi_lock);
13444 +       if (IS_ERR(dentry))
13445 +               goto out_unlock;
13446 +       if (dentry)
13447 +               goto accept;
13448 +
13449 +       /* lookup path */
13450 +       dentry = decode_by_path(sb, ino, fh, fh_len, &nsi_lock);
13451 +       if (IS_ERR(dentry))
13452 +               goto out_unlock;
13453 +       if (unlikely(!dentry))
13454 +               /* todo?: make it ESTALE */
13455 +               goto out_unlock;
13456 +
13457 +accept:
13458 +       if (!au_digen_test(dentry, au_sigen(sb))
13459 +           && d_inode(dentry)->i_generation == fh[Fh_igen])
13460 +               goto out_unlock; /* success */
13461 +
13462 +       dput(dentry);
13463 +       dentry = ERR_PTR(-ESTALE);
13464 +out_unlock:
13465 +       if (br)
13466 +               au_lcnt_dec(&br->br_nfiles);
13467 +       si_read_unlock(sb);
13468 +out:
13469 +       AuTraceErrPtr(dentry);
13470 +       return dentry;
13471 +}
13472 +
13473 +#if 0 /* reserved for future use */
13474 +/* support subtreecheck option */
13475 +static struct dentry *aufs_fh_to_parent(struct super_block *sb, struct fid *fid,
13476 +                                       int fh_len, int fh_type)
13477 +{
13478 +       struct dentry *parent;
13479 +       __u32 *fh = fid->raw;
13480 +       ino_t dir_ino;
13481 +
13482 +       dir_ino = decode_ino(fh + Fh_dir_ino);
13483 +       parent = decode_by_ino(sb, dir_ino, 0);
13484 +       if (IS_ERR(parent))
13485 +               goto out;
13486 +       if (!parent)
13487 +               parent = decode_by_path(sb, au_br_index(sb, fh[Fh_br_id]),
13488 +                                       dir_ino, fh, fh_len);
13489 +
13490 +out:
13491 +       AuTraceErrPtr(parent);
13492 +       return parent;
13493 +}
13494 +#endif
13495 +
13496 +/* ---------------------------------------------------------------------- */
13497 +
13498 +static int aufs_encode_fh(struct inode *inode, __u32 *fh, int *max_len,
13499 +                         struct inode *dir)
13500 +{
13501 +       int err;
13502 +       aufs_bindex_t bindex;
13503 +       struct super_block *sb, *h_sb;
13504 +       struct dentry *dentry, *parent, *h_parent;
13505 +       struct inode *h_dir;
13506 +       struct au_branch *br;
13507 +
13508 +       err = -ENOSPC;
13509 +       if (unlikely(*max_len <= Fh_tail)) {
13510 +               AuWarn1("NFSv2 client (max_len %d)?\n", *max_len);
13511 +               goto out;
13512 +       }
13513 +
13514 +       err = FILEID_ROOT;
13515 +       if (inode->i_ino == AUFS_ROOT_INO) {
13516 +               AuDebugOn(inode->i_ino != AUFS_ROOT_INO);
13517 +               goto out;
13518 +       }
13519 +
13520 +       h_parent = NULL;
13521 +       sb = inode->i_sb;
13522 +       err = si_read_lock(sb, AuLock_FLUSH);
13523 +       if (unlikely(err))
13524 +               goto out;
13525 +
13526 +#ifdef CONFIG_AUFS_DEBUG
13527 +       if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
13528 +               AuWarn1("NFS-exporting requires xino\n");
13529 +#endif
13530 +       err = -EIO;
13531 +       parent = NULL;
13532 +       ii_read_lock_child(inode);
13533 +       bindex = au_ibtop(inode);
13534 +       if (!dir) {
13535 +               dentry = d_find_any_alias(inode);
13536 +               if (unlikely(!dentry))
13537 +                       goto out_unlock;
13538 +               AuDebugOn(au_test_anon(dentry));
13539 +               parent = dget_parent(dentry);
13540 +               dput(dentry);
13541 +               if (unlikely(!parent))
13542 +                       goto out_unlock;
13543 +               if (d_really_is_positive(parent))
13544 +                       dir = d_inode(parent);
13545 +       }
13546 +
13547 +       ii_read_lock_parent(dir);
13548 +       h_dir = au_h_iptr(dir, bindex);
13549 +       ii_read_unlock(dir);
13550 +       if (unlikely(!h_dir))
13551 +               goto out_parent;
13552 +       h_parent = d_find_any_alias(h_dir);
13553 +       if (unlikely(!h_parent))
13554 +               goto out_hparent;
13555 +
13556 +       err = -EPERM;
13557 +       br = au_sbr(sb, bindex);
13558 +       h_sb = au_br_sb(br);
13559 +       if (unlikely(!h_sb->s_export_op)) {
13560 +               AuErr1("%s branch is not exportable\n", au_sbtype(h_sb));
13561 +               goto out_hparent;
13562 +       }
13563 +
13564 +       fh[Fh_br_id] = br->br_id;
13565 +       fh[Fh_sigen] = au_sigen(sb);
13566 +       encode_ino(fh + Fh_ino, inode->i_ino);
13567 +       encode_ino(fh + Fh_dir_ino, dir->i_ino);
13568 +       fh[Fh_igen] = inode->i_generation;
13569 +
13570 +       *max_len -= Fh_tail;
13571 +       fh[Fh_h_type] = exportfs_encode_fh(h_parent, (void *)(fh + Fh_tail),
13572 +                                          max_len,
13573 +                                          /*connectable or subtreecheck*/0);
13574 +       err = fh[Fh_h_type];
13575 +       *max_len += Fh_tail;
13576 +       /* todo: macros? */
13577 +       if (err != FILEID_INVALID)
13578 +               err = 99;
13579 +       else
13580 +               AuWarn1("%s encode_fh failed\n", au_sbtype(h_sb));
13581 +
13582 +out_hparent:
13583 +       dput(h_parent);
13584 +out_parent:
13585 +       dput(parent);
13586 +out_unlock:
13587 +       ii_read_unlock(inode);
13588 +       si_read_unlock(sb);
13589 +out:
13590 +       if (unlikely(err < 0))
13591 +               err = FILEID_INVALID;
13592 +       return err;
13593 +}
13594 +
13595 +/* ---------------------------------------------------------------------- */
13596 +
13597 +static int aufs_commit_metadata(struct inode *inode)
13598 +{
13599 +       int err;
13600 +       aufs_bindex_t bindex;
13601 +       struct super_block *sb;
13602 +       struct inode *h_inode;
13603 +       int (*f)(struct inode *inode);
13604 +
13605 +       sb = inode->i_sb;
13606 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
13607 +       ii_write_lock_child(inode);
13608 +       bindex = au_ibtop(inode);
13609 +       AuDebugOn(bindex < 0);
13610 +       h_inode = au_h_iptr(inode, bindex);
13611 +
13612 +       f = h_inode->i_sb->s_export_op->commit_metadata;
13613 +       if (f)
13614 +               err = f(h_inode);
13615 +       else
13616 +               err = sync_inode_metadata(h_inode, /*wait*/1);
13617 +
13618 +       au_cpup_attr_timesizes(inode);
13619 +       ii_write_unlock(inode);
13620 +       si_read_unlock(sb);
13621 +       return err;
13622 +}
13623 +
13624 +/* ---------------------------------------------------------------------- */
13625 +
13626 +static struct export_operations aufs_export_op = {
13627 +       .fh_to_dentry           = aufs_fh_to_dentry,
13628 +       /* .fh_to_parent        = aufs_fh_to_parent, */
13629 +       .encode_fh              = aufs_encode_fh,
13630 +       .commit_metadata        = aufs_commit_metadata
13631 +};
13632 +
13633 +void au_export_init(struct super_block *sb)
13634 +{
13635 +       struct au_sbinfo *sbinfo;
13636 +       __u32 u;
13637 +
13638 +       BUILD_BUG_ON_MSG(IS_BUILTIN(CONFIG_AUFS_FS)
13639 +                        && IS_MODULE(CONFIG_EXPORTFS),
13640 +                        AUFS_NAME ": unsupported configuration "
13641 +                        "CONFIG_EXPORTFS=m and CONFIG_AUFS_FS=y");
13642 +
13643 +       sb->s_export_op = &aufs_export_op;
13644 +       sbinfo = au_sbi(sb);
13645 +       sbinfo->si_xigen = NULL;
13646 +       get_random_bytes(&u, sizeof(u));
13647 +       BUILD_BUG_ON(sizeof(u) != sizeof(int));
13648 +       atomic_set(&sbinfo->si_xigen_next, u);
13649 +}
13650 diff -urN /usr/share/empty/fs/aufs/fhsm.c linux/fs/aufs/fhsm.c
13651 --- /usr/share/empty/fs/aufs/fhsm.c     1970-01-01 01:00:00.000000000 +0100
13652 +++ linux/fs/aufs/fhsm.c        2021-11-01 23:48:34.206359262 +0100
13653 @@ -0,0 +1,427 @@
13654 +// SPDX-License-Identifier: GPL-2.0
13655 +/*
13656 + * Copyright (C) 2011-2021 Junjiro R. Okajima
13657 + *
13658 + * This program, aufs is free software; you can redistribute it and/or modify
13659 + * it under the terms of the GNU General Public License as published by
13660 + * the Free Software Foundation; either version 2 of the License, or
13661 + * (at your option) any later version.
13662 + *
13663 + * This program is distributed in the hope that it will be useful,
13664 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
13665 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13666 + * GNU General Public License for more details.
13667 + *
13668 + * You should have received a copy of the GNU General Public License
13669 + * along with this program; if not, write to the Free Software
13670 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
13671 + */
13672 +
13673 +/*
13674 + * File-based Hierarchy Storage Management
13675 + */
13676 +
13677 +#include <linux/anon_inodes.h>
13678 +#include <linux/poll.h>
13679 +#include <linux/seq_file.h>
13680 +#include <linux/statfs.h>
13681 +#include "aufs.h"
13682 +
13683 +static aufs_bindex_t au_fhsm_bottom(struct super_block *sb)
13684 +{
13685 +       struct au_sbinfo *sbinfo;
13686 +       struct au_fhsm *fhsm;
13687 +
13688 +       SiMustAnyLock(sb);
13689 +
13690 +       sbinfo = au_sbi(sb);
13691 +       fhsm = &sbinfo->si_fhsm;
13692 +       AuDebugOn(!fhsm);
13693 +       return fhsm->fhsm_bottom;
13694 +}
13695 +
13696 +void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex)
13697 +{
13698 +       struct au_sbinfo *sbinfo;
13699 +       struct au_fhsm *fhsm;
13700 +
13701 +       SiMustWriteLock(sb);
13702 +
13703 +       sbinfo = au_sbi(sb);
13704 +       fhsm = &sbinfo->si_fhsm;
13705 +       AuDebugOn(!fhsm);
13706 +       fhsm->fhsm_bottom = bindex;
13707 +}
13708 +
13709 +/* ---------------------------------------------------------------------- */
13710 +
13711 +static int au_fhsm_test_jiffy(struct au_sbinfo *sbinfo, struct au_branch *br)
13712 +{
13713 +       struct au_br_fhsm *bf;
13714 +
13715 +       bf = br->br_fhsm;
13716 +       MtxMustLock(&bf->bf_lock);
13717 +
13718 +       return !bf->bf_readable
13719 +               || time_after(jiffies,
13720 +                             bf->bf_jiffy + sbinfo->si_fhsm.fhsm_expire);
13721 +}
13722 +
13723 +/* ---------------------------------------------------------------------- */
13724 +
13725 +static void au_fhsm_notify(struct super_block *sb, int val)
13726 +{
13727 +       struct au_sbinfo *sbinfo;
13728 +       struct au_fhsm *fhsm;
13729 +
13730 +       SiMustAnyLock(sb);
13731 +
13732 +       sbinfo = au_sbi(sb);
13733 +       fhsm = &sbinfo->si_fhsm;
13734 +       if (au_fhsm_pid(fhsm)
13735 +           && atomic_read(&fhsm->fhsm_readable) != -1) {
13736 +               atomic_set(&fhsm->fhsm_readable, val);
13737 +               if (val)
13738 +                       wake_up(&fhsm->fhsm_wqh);
13739 +       }
13740 +}
13741 +
13742 +static int au_fhsm_stfs(struct super_block *sb, aufs_bindex_t bindex,
13743 +                       struct aufs_stfs *rstfs, int do_lock, int do_notify)
13744 +{
13745 +       int err;
13746 +       struct au_branch *br;
13747 +       struct au_br_fhsm *bf;
13748 +
13749 +       br = au_sbr(sb, bindex);
13750 +       AuDebugOn(au_br_rdonly(br));
13751 +       bf = br->br_fhsm;
13752 +       AuDebugOn(!bf);
13753 +
13754 +       if (do_lock)
13755 +               mutex_lock(&bf->bf_lock);
13756 +       else
13757 +               MtxMustLock(&bf->bf_lock);
13758 +
13759 +       /* sb->s_root for NFS is unreliable */
13760 +       err = au_br_stfs(br, &bf->bf_stfs);
13761 +       if (unlikely(err)) {
13762 +               AuErr1("FHSM failed (%d), b%d, ignored.\n", bindex, err);
13763 +               goto out;
13764 +       }
13765 +
13766 +       bf->bf_jiffy = jiffies;
13767 +       bf->bf_readable = 1;
13768 +       if (do_notify)
13769 +               au_fhsm_notify(sb, /*val*/1);
13770 +       if (rstfs)
13771 +               *rstfs = bf->bf_stfs;
13772 +
13773 +out:
13774 +       if (do_lock)
13775 +               mutex_unlock(&bf->bf_lock);
13776 +       au_fhsm_notify(sb, /*val*/1);
13777 +
13778 +       return err;
13779 +}
13780 +
13781 +void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force)
13782 +{
13783 +       int err;
13784 +       struct au_sbinfo *sbinfo;
13785 +       struct au_fhsm *fhsm;
13786 +       struct au_branch *br;
13787 +       struct au_br_fhsm *bf;
13788 +
13789 +       AuDbg("b%d, force %d\n", bindex, force);
13790 +       SiMustAnyLock(sb);
13791 +
13792 +       sbinfo = au_sbi(sb);
13793 +       fhsm = &sbinfo->si_fhsm;
13794 +       if (!au_ftest_si(sbinfo, FHSM)
13795 +           || fhsm->fhsm_bottom == bindex)
13796 +               return;
13797 +
13798 +       br = au_sbr(sb, bindex);
13799 +       bf = br->br_fhsm;
13800 +       AuDebugOn(!bf);
13801 +       mutex_lock(&bf->bf_lock);
13802 +       if (force
13803 +           || au_fhsm_pid(fhsm)
13804 +           || au_fhsm_test_jiffy(sbinfo, br))
13805 +               err = au_fhsm_stfs(sb, bindex, /*rstfs*/NULL, /*do_lock*/0,
13806 +                                 /*do_notify*/1);
13807 +       mutex_unlock(&bf->bf_lock);
13808 +}
13809 +
13810 +void au_fhsm_wrote_all(struct super_block *sb, int force)
13811 +{
13812 +       aufs_bindex_t bindex, bbot;
13813 +       struct au_branch *br;
13814 +
13815 +       /* exclude the bottom */
13816 +       bbot = au_fhsm_bottom(sb);
13817 +       for (bindex = 0; bindex < bbot; bindex++) {
13818 +               br = au_sbr(sb, bindex);
13819 +               if (au_br_fhsm(br->br_perm))
13820 +                       au_fhsm_wrote(sb, bindex, force);
13821 +       }
13822 +}
13823 +
13824 +/* ---------------------------------------------------------------------- */
13825 +
13826 +static __poll_t au_fhsm_poll(struct file *file, struct poll_table_struct *wait)
13827 +{
13828 +       __poll_t mask;
13829 +       struct au_sbinfo *sbinfo;
13830 +       struct au_fhsm *fhsm;
13831 +
13832 +       mask = 0;
13833 +       sbinfo = file->private_data;
13834 +       fhsm = &sbinfo->si_fhsm;
13835 +       poll_wait(file, &fhsm->fhsm_wqh, wait);
13836 +       if (atomic_read(&fhsm->fhsm_readable))
13837 +               mask = EPOLLIN /* | EPOLLRDNORM */;
13838 +
13839 +       if (!mask)
13840 +               AuDbg("mask 0x%x\n", mask);
13841 +       return mask;
13842 +}
13843 +
13844 +static int au_fhsm_do_read_one(struct aufs_stbr __user *stbr,
13845 +                             struct aufs_stfs *stfs, __s16 brid)
13846 +{
13847 +       int err;
13848 +
13849 +       err = copy_to_user(&stbr->stfs, stfs, sizeof(*stfs));
13850 +       if (!err)
13851 +               err = __put_user(brid, &stbr->brid);
13852 +       if (unlikely(err))
13853 +               err = -EFAULT;
13854 +
13855 +       return err;
13856 +}
13857 +
13858 +static ssize_t au_fhsm_do_read(struct super_block *sb,
13859 +                              struct aufs_stbr __user *stbr, size_t count)
13860 +{
13861 +       ssize_t err;
13862 +       int nstbr;
13863 +       aufs_bindex_t bindex, bbot;
13864 +       struct au_branch *br;
13865 +       struct au_br_fhsm *bf;
13866 +
13867 +       /* except the bottom branch */
13868 +       err = 0;
13869 +       nstbr = 0;
13870 +       bbot = au_fhsm_bottom(sb);
13871 +       for (bindex = 0; !err && bindex < bbot; bindex++) {
13872 +               br = au_sbr(sb, bindex);
13873 +               if (!au_br_fhsm(br->br_perm))
13874 +                       continue;
13875 +
13876 +               bf = br->br_fhsm;
13877 +               mutex_lock(&bf->bf_lock);
13878 +               if (bf->bf_readable) {
13879 +                       err = -EFAULT;
13880 +                       if (count >= sizeof(*stbr))
13881 +                               err = au_fhsm_do_read_one(stbr++, &bf->bf_stfs,
13882 +                                                         br->br_id);
13883 +                       if (!err) {
13884 +                               bf->bf_readable = 0;
13885 +                               count -= sizeof(*stbr);
13886 +                               nstbr++;
13887 +                       }
13888 +               }
13889 +               mutex_unlock(&bf->bf_lock);
13890 +       }
13891 +       if (!err)
13892 +               err = sizeof(*stbr) * nstbr;
13893 +
13894 +       return err;
13895 +}
13896 +
13897 +static ssize_t au_fhsm_read(struct file *file, char __user *buf, size_t count,
13898 +                          loff_t *pos)
13899 +{
13900 +       ssize_t err;
13901 +       int readable;
13902 +       aufs_bindex_t nfhsm, bindex, bbot;
13903 +       struct au_sbinfo *sbinfo;
13904 +       struct au_fhsm *fhsm;
13905 +       struct au_branch *br;
13906 +       struct super_block *sb;
13907 +
13908 +       err = 0;
13909 +       sbinfo = file->private_data;
13910 +       fhsm = &sbinfo->si_fhsm;
13911 +need_data:
13912 +       spin_lock_irq(&fhsm->fhsm_wqh.lock);
13913 +       if (!atomic_read(&fhsm->fhsm_readable)) {
13914 +               if (vfsub_file_flags(file) & O_NONBLOCK)
13915 +                       err = -EAGAIN;
13916 +               else
13917 +                       err = wait_event_interruptible_locked_irq
13918 +                               (fhsm->fhsm_wqh,
13919 +                                atomic_read(&fhsm->fhsm_readable));
13920 +       }
13921 +       spin_unlock_irq(&fhsm->fhsm_wqh.lock);
13922 +       if (unlikely(err))
13923 +               goto out;
13924 +
13925 +       /* sb may already be dead */
13926 +       au_rw_read_lock(&sbinfo->si_rwsem);
13927 +       readable = atomic_read(&fhsm->fhsm_readable);
13928 +       if (readable > 0) {
13929 +               sb = sbinfo->si_sb;
13930 +               AuDebugOn(!sb);
13931 +               /* exclude the bottom branch */
13932 +               nfhsm = 0;
13933 +               bbot = au_fhsm_bottom(sb);
13934 +               for (bindex = 0; bindex < bbot; bindex++) {
13935 +                       br = au_sbr(sb, bindex);
13936 +                       if (au_br_fhsm(br->br_perm))
13937 +                               nfhsm++;
13938 +               }
13939 +               err = -EMSGSIZE;
13940 +               if (nfhsm * sizeof(struct aufs_stbr) <= count) {
13941 +                       atomic_set(&fhsm->fhsm_readable, 0);
13942 +                       err = au_fhsm_do_read(sbinfo->si_sb, (void __user *)buf,
13943 +                                            count);
13944 +               }
13945 +       }
13946 +       au_rw_read_unlock(&sbinfo->si_rwsem);
13947 +       if (!readable)
13948 +               goto need_data;
13949 +
13950 +out:
13951 +       return err;
13952 +}
13953 +
13954 +static int au_fhsm_release(struct inode *inode, struct file *file)
13955 +{
13956 +       struct au_sbinfo *sbinfo;
13957 +       struct au_fhsm *fhsm;
13958 +
13959 +       /* sb may already be dead */
13960 +       sbinfo = file->private_data;
13961 +       fhsm = &sbinfo->si_fhsm;
13962 +       spin_lock(&fhsm->fhsm_spin);
13963 +       fhsm->fhsm_pid = 0;
13964 +       spin_unlock(&fhsm->fhsm_spin);
13965 +       kobject_put(&sbinfo->si_kobj);
13966 +
13967 +       return 0;
13968 +}
13969 +
13970 +static const struct file_operations au_fhsm_fops = {
13971 +       .owner          = THIS_MODULE,
13972 +       .llseek         = noop_llseek,
13973 +       .read           = au_fhsm_read,
13974 +       .poll           = au_fhsm_poll,
13975 +       .release        = au_fhsm_release
13976 +};
13977 +
13978 +int au_fhsm_fd(struct super_block *sb, int oflags)
13979 +{
13980 +       int err, fd;
13981 +       struct au_sbinfo *sbinfo;
13982 +       struct au_fhsm *fhsm;
13983 +
13984 +       err = -EPERM;
13985 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
13986 +               goto out;
13987 +
13988 +       err = -EINVAL;
13989 +       if (unlikely(oflags & ~(O_CLOEXEC | O_NONBLOCK)))
13990 +               goto out;
13991 +
13992 +       err = 0;
13993 +       sbinfo = au_sbi(sb);
13994 +       fhsm = &sbinfo->si_fhsm;
13995 +       spin_lock(&fhsm->fhsm_spin);
13996 +       if (!fhsm->fhsm_pid)
13997 +               fhsm->fhsm_pid = current->pid;
13998 +       else
13999 +               err = -EBUSY;
14000 +       spin_unlock(&fhsm->fhsm_spin);
14001 +       if (unlikely(err))
14002 +               goto out;
14003 +
14004 +       oflags |= O_RDONLY;
14005 +       /* oflags |= FMODE_NONOTIFY; */
14006 +       fd = anon_inode_getfd("[aufs_fhsm]", &au_fhsm_fops, sbinfo, oflags);
14007 +       err = fd;
14008 +       if (unlikely(fd < 0))
14009 +               goto out_pid;
14010 +
14011 +       /* succeed regardless 'fhsm' status */
14012 +       kobject_get(&sbinfo->si_kobj);
14013 +       si_noflush_read_lock(sb);
14014 +       if (au_ftest_si(sbinfo, FHSM))
14015 +               au_fhsm_wrote_all(sb, /*force*/0);
14016 +       si_read_unlock(sb);
14017 +       goto out; /* success */
14018 +
14019 +out_pid:
14020 +       spin_lock(&fhsm->fhsm_spin);
14021 +       fhsm->fhsm_pid = 0;
14022 +       spin_unlock(&fhsm->fhsm_spin);
14023 +out:
14024 +       AuTraceErr(err);
14025 +       return err;
14026 +}
14027 +
14028 +/* ---------------------------------------------------------------------- */
14029 +
14030 +int au_fhsm_br_alloc(struct au_branch *br)
14031 +{
14032 +       int err;
14033 +
14034 +       err = 0;
14035 +       br->br_fhsm = kmalloc(sizeof(*br->br_fhsm), GFP_NOFS);
14036 +       if (br->br_fhsm)
14037 +               au_br_fhsm_init(br->br_fhsm);
14038 +       else
14039 +               err = -ENOMEM;
14040 +
14041 +       return err;
14042 +}
14043 +
14044 +/* ---------------------------------------------------------------------- */
14045 +
14046 +void au_fhsm_fin(struct super_block *sb)
14047 +{
14048 +       au_fhsm_notify(sb, /*val*/-1);
14049 +}
14050 +
14051 +void au_fhsm_init(struct au_sbinfo *sbinfo)
14052 +{
14053 +       struct au_fhsm *fhsm;
14054 +
14055 +       fhsm = &sbinfo->si_fhsm;
14056 +       spin_lock_init(&fhsm->fhsm_spin);
14057 +       init_waitqueue_head(&fhsm->fhsm_wqh);
14058 +       atomic_set(&fhsm->fhsm_readable, 0);
14059 +       fhsm->fhsm_expire
14060 +               = msecs_to_jiffies(AUFS_FHSM_CACHE_DEF_SEC * MSEC_PER_SEC);
14061 +       fhsm->fhsm_bottom = -1;
14062 +}
14063 +
14064 +void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec)
14065 +{
14066 +       sbinfo->si_fhsm.fhsm_expire
14067 +               = msecs_to_jiffies(sec * MSEC_PER_SEC);
14068 +}
14069 +
14070 +void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo)
14071 +{
14072 +       unsigned int u;
14073 +
14074 +       if (!au_ftest_si(sbinfo, FHSM))
14075 +               return;
14076 +
14077 +       u = jiffies_to_msecs(sbinfo->si_fhsm.fhsm_expire) / MSEC_PER_SEC;
14078 +       if (u != AUFS_FHSM_CACHE_DEF_SEC)
14079 +               seq_printf(seq, ",fhsm_sec=%u", u);
14080 +}
14081 diff -urN /usr/share/empty/fs/aufs/file.c linux/fs/aufs/file.c
14082 --- /usr/share/empty/fs/aufs/file.c     1970-01-01 01:00:00.000000000 +0100
14083 +++ linux/fs/aufs/file.c        2021-11-01 23:48:34.206359262 +0100
14084 @@ -0,0 +1,863 @@
14085 +// SPDX-License-Identifier: GPL-2.0
14086 +/*
14087 + * Copyright (C) 2005-2021 Junjiro R. Okajima
14088 + *
14089 + * This program, aufs is free software; you can redistribute it and/or modify
14090 + * it under the terms of the GNU General Public License as published by
14091 + * the Free Software Foundation; either version 2 of the License, or
14092 + * (at your option) any later version.
14093 + *
14094 + * This program is distributed in the hope that it will be useful,
14095 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
14096 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14097 + * GNU General Public License for more details.
14098 + *
14099 + * You should have received a copy of the GNU General Public License
14100 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
14101 + */
14102 +
14103 +/*
14104 + * handling file/dir, and address_space operation
14105 + */
14106 +
14107 +#ifdef CONFIG_AUFS_DEBUG
14108 +#include <linux/migrate.h>
14109 +#endif
14110 +#include <linux/pagemap.h>
14111 +#include "aufs.h"
14112 +
14113 +/* drop flags for writing */
14114 +unsigned int au_file_roflags(unsigned int flags)
14115 +{
14116 +       flags &= ~(O_WRONLY | O_RDWR | O_APPEND | O_CREAT | O_TRUNC);
14117 +       flags |= O_RDONLY | O_NOATIME;
14118 +       return flags;
14119 +}
14120 +
14121 +/* common functions to regular file and dir */
14122 +struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
14123 +                      struct file *file, int force_wr)
14124 +{
14125 +       struct file *h_file;
14126 +       struct dentry *h_dentry;
14127 +       struct inode *h_inode;
14128 +       struct super_block *sb;
14129 +       struct au_branch *br;
14130 +       struct path h_path;
14131 +       int err;
14132 +
14133 +       /* a race condition can happen between open and unlink/rmdir */
14134 +       h_file = ERR_PTR(-ENOENT);
14135 +       h_dentry = au_h_dptr(dentry, bindex);
14136 +       if (au_test_nfsd() && (!h_dentry || d_is_negative(h_dentry)))
14137 +               goto out;
14138 +       h_inode = d_inode(h_dentry);
14139 +       spin_lock(&h_dentry->d_lock);
14140 +       err = (!d_unhashed(dentry) && d_unlinked(h_dentry))
14141 +               /* || !d_inode(dentry)->i_nlink */
14142 +               ;
14143 +       spin_unlock(&h_dentry->d_lock);
14144 +       if (unlikely(err))
14145 +               goto out;
14146 +
14147 +       sb = dentry->d_sb;
14148 +       br = au_sbr(sb, bindex);
14149 +       err = au_br_test_oflag(flags, br);
14150 +       h_file = ERR_PTR(err);
14151 +       if (unlikely(err))
14152 +               goto out;
14153 +
14154 +       /* drop flags for writing */
14155 +       if (au_test_ro(sb, bindex, d_inode(dentry))) {
14156 +               if (force_wr && !(flags & O_WRONLY))
14157 +                       force_wr = 0;
14158 +               flags = au_file_roflags(flags);
14159 +               if (force_wr) {
14160 +                       h_file = ERR_PTR(-EROFS);
14161 +                       flags = au_file_roflags(flags);
14162 +                       if (unlikely(vfsub_native_ro(h_inode)
14163 +                                    || IS_APPEND(h_inode)))
14164 +                               goto out;
14165 +                       flags &= ~O_ACCMODE;
14166 +                       flags |= O_WRONLY;
14167 +               }
14168 +       }
14169 +       flags &= ~O_CREAT;
14170 +       au_lcnt_inc(&br->br_nfiles);
14171 +       h_path.dentry = h_dentry;
14172 +       h_path.mnt = au_br_mnt(br);
14173 +       h_file = vfsub_dentry_open(&h_path, flags);
14174 +       if (IS_ERR(h_file))
14175 +               goto out_br;
14176 +
14177 +       if (flags & __FMODE_EXEC) {
14178 +               err = deny_write_access(h_file);
14179 +               if (unlikely(err)) {
14180 +                       fput(h_file);
14181 +                       h_file = ERR_PTR(err);
14182 +                       goto out_br;
14183 +               }
14184 +       }
14185 +       fsnotify_open(h_file);
14186 +       goto out; /* success */
14187 +
14188 +out_br:
14189 +       au_lcnt_dec(&br->br_nfiles);
14190 +out:
14191 +       return h_file;
14192 +}
14193 +
14194 +static int au_cmoo(struct dentry *dentry)
14195 +{
14196 +       int err, cmoo, matched;
14197 +       unsigned int udba;
14198 +       struct path h_path;
14199 +       struct au_pin pin;
14200 +       struct au_cp_generic cpg = {
14201 +               .dentry = dentry,
14202 +               .bdst   = -1,
14203 +               .bsrc   = -1,
14204 +               .len    = -1,
14205 +               .pin    = &pin,
14206 +               .flags  = AuCpup_DTIME | AuCpup_HOPEN
14207 +       };
14208 +       struct inode *delegated;
14209 +       struct super_block *sb;
14210 +       struct au_sbinfo *sbinfo;
14211 +       struct au_fhsm *fhsm;
14212 +       pid_t pid;
14213 +       struct au_branch *br;
14214 +       struct dentry *parent;
14215 +       struct au_hinode *hdir;
14216 +
14217 +       DiMustWriteLock(dentry);
14218 +       IiMustWriteLock(d_inode(dentry));
14219 +
14220 +       err = 0;
14221 +       if (IS_ROOT(dentry))
14222 +               goto out;
14223 +       cpg.bsrc = au_dbtop(dentry);
14224 +       if (!cpg.bsrc)
14225 +               goto out;
14226 +
14227 +       sb = dentry->d_sb;
14228 +       sbinfo = au_sbi(sb);
14229 +       fhsm = &sbinfo->si_fhsm;
14230 +       pid = au_fhsm_pid(fhsm);
14231 +       rcu_read_lock();
14232 +       matched = (pid
14233 +                  && (current->pid == pid
14234 +                      || rcu_dereference(current->real_parent)->pid == pid));
14235 +       rcu_read_unlock();
14236 +       if (matched)
14237 +               goto out;
14238 +
14239 +       br = au_sbr(sb, cpg.bsrc);
14240 +       cmoo = au_br_cmoo(br->br_perm);
14241 +       if (!cmoo)
14242 +               goto out;
14243 +       if (!d_is_reg(dentry))
14244 +               cmoo &= AuBrAttr_COO_ALL;
14245 +       if (!cmoo)
14246 +               goto out;
14247 +
14248 +       parent = dget_parent(dentry);
14249 +       di_write_lock_parent(parent);
14250 +       err = au_wbr_do_copyup_bu(dentry, cpg.bsrc - 1);
14251 +       cpg.bdst = err;
14252 +       if (unlikely(err < 0)) {
14253 +               err = 0;        /* there is no upper writable branch */
14254 +               goto out_dgrade;
14255 +       }
14256 +       AuDbg("bsrc %d, bdst %d\n", cpg.bsrc, cpg.bdst);
14257 +
14258 +       /* do not respect the coo attrib for the target branch */
14259 +       err = au_cpup_dirs(dentry, cpg.bdst);
14260 +       if (unlikely(err))
14261 +               goto out_dgrade;
14262 +
14263 +       di_downgrade_lock(parent, AuLock_IR);
14264 +       udba = au_opt_udba(sb);
14265 +       err = au_pin(&pin, dentry, cpg.bdst, udba,
14266 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14267 +       if (unlikely(err))
14268 +               goto out_parent;
14269 +
14270 +       err = au_sio_cpup_simple(&cpg);
14271 +       au_unpin(&pin);
14272 +       if (unlikely(err))
14273 +               goto out_parent;
14274 +       if (!(cmoo & AuBrWAttr_MOO))
14275 +               goto out_parent; /* success */
14276 +
14277 +       err = au_pin(&pin, dentry, cpg.bsrc, udba,
14278 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14279 +       if (unlikely(err))
14280 +               goto out_parent;
14281 +
14282 +       h_path.mnt = au_br_mnt(br);
14283 +       h_path.dentry = au_h_dptr(dentry, cpg.bsrc);
14284 +       hdir = au_hi(d_inode(parent), cpg.bsrc);
14285 +       delegated = NULL;
14286 +       err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated, /*force*/1);
14287 +       au_unpin(&pin);
14288 +       /* todo: keep h_dentry or not? */
14289 +       if (unlikely(err == -EWOULDBLOCK)) {
14290 +               pr_warn("cannot retry for NFSv4 delegation"
14291 +                       " for an internal unlink\n");
14292 +               iput(delegated);
14293 +       }
14294 +       if (unlikely(err)) {
14295 +               pr_err("unlink %pd after coo failed (%d), ignored\n",
14296 +                      dentry, err);
14297 +               err = 0;
14298 +       }
14299 +       goto out_parent; /* success */
14300 +
14301 +out_dgrade:
14302 +       di_downgrade_lock(parent, AuLock_IR);
14303 +out_parent:
14304 +       di_read_unlock(parent, AuLock_IR);
14305 +       dput(parent);
14306 +out:
14307 +       AuTraceErr(err);
14308 +       return err;
14309 +}
14310 +
14311 +int au_do_open(struct file *file, struct au_do_open_args *args)
14312 +{
14313 +       int err, aopen = args->aopen;
14314 +       struct dentry *dentry;
14315 +       struct au_finfo *finfo;
14316 +
14317 +       if (!aopen)
14318 +               err = au_finfo_init(file, args->fidir);
14319 +       else {
14320 +               lockdep_off();
14321 +               err = au_finfo_init(file, args->fidir);
14322 +               lockdep_on();
14323 +       }
14324 +       if (unlikely(err))
14325 +               goto out;
14326 +
14327 +       dentry = file->f_path.dentry;
14328 +       AuDebugOn(IS_ERR_OR_NULL(dentry));
14329 +       di_write_lock_child(dentry);
14330 +       err = au_cmoo(dentry);
14331 +       di_downgrade_lock(dentry, AuLock_IR);
14332 +       if (!err) {
14333 +               if (!aopen)
14334 +                       err = args->open(file, vfsub_file_flags(file), NULL);
14335 +               else {
14336 +                       lockdep_off();
14337 +                       err = args->open(file, vfsub_file_flags(file),
14338 +                                        args->h_file);
14339 +                       lockdep_on();
14340 +               }
14341 +       }
14342 +       di_read_unlock(dentry, AuLock_IR);
14343 +
14344 +       finfo = au_fi(file);
14345 +       if (!err) {
14346 +               finfo->fi_file = file;
14347 +               au_hbl_add(&finfo->fi_hlist,
14348 +                          &au_sbi(file->f_path.dentry->d_sb)->si_files);
14349 +       }
14350 +       if (!aopen)
14351 +               fi_write_unlock(file);
14352 +       else {
14353 +               lockdep_off();
14354 +               fi_write_unlock(file);
14355 +               lockdep_on();
14356 +       }
14357 +       if (unlikely(err)) {
14358 +               finfo->fi_hdir = NULL;
14359 +               au_finfo_fin(file);
14360 +       }
14361 +
14362 +out:
14363 +       AuTraceErr(err);
14364 +       return err;
14365 +}
14366 +
14367 +int au_reopen_nondir(struct file *file)
14368 +{
14369 +       int err;
14370 +       aufs_bindex_t btop;
14371 +       struct dentry *dentry;
14372 +       struct au_branch *br;
14373 +       struct file *h_file, *h_file_tmp;
14374 +
14375 +       dentry = file->f_path.dentry;
14376 +       btop = au_dbtop(dentry);
14377 +       br = au_sbr(dentry->d_sb, btop);
14378 +       h_file_tmp = NULL;
14379 +       if (au_fbtop(file) == btop) {
14380 +               h_file = au_hf_top(file);
14381 +               if (file->f_mode == h_file->f_mode)
14382 +                       return 0; /* success */
14383 +               h_file_tmp = h_file;
14384 +               get_file(h_file_tmp);
14385 +               au_lcnt_inc(&br->br_nfiles);
14386 +               au_set_h_fptr(file, btop, NULL);
14387 +       }
14388 +       AuDebugOn(au_fi(file)->fi_hdir);
14389 +       /*
14390 +        * it can happen
14391 +        * file exists on both of rw and ro
14392 +        * open --> dbtop and fbtop are both 0
14393 +        * prepend a branch as rw, "rw" become ro
14394 +        * remove rw/file
14395 +        * delete the top branch, "rw" becomes rw again
14396 +        *      --> dbtop is 1, fbtop is still 0
14397 +        * write --> fbtop is 0 but dbtop is 1
14398 +        */
14399 +       /* AuDebugOn(au_fbtop(file) < btop); */
14400 +
14401 +       h_file = au_h_open(dentry, btop, vfsub_file_flags(file) & ~O_TRUNC,
14402 +                          file, /*force_wr*/0);
14403 +       err = PTR_ERR(h_file);
14404 +       if (IS_ERR(h_file)) {
14405 +               if (h_file_tmp) {
14406 +                       /* revert */
14407 +                       au_set_h_fptr(file, btop, h_file_tmp);
14408 +                       h_file_tmp = NULL;
14409 +               }
14410 +               goto out; /* todo: close all? */
14411 +       }
14412 +
14413 +       err = 0;
14414 +       au_set_fbtop(file, btop);
14415 +       au_set_h_fptr(file, btop, h_file);
14416 +       au_update_figen(file);
14417 +       /* todo: necessary? */
14418 +       /* file->f_ra = h_file->f_ra; */
14419 +
14420 +out:
14421 +       if (h_file_tmp) {
14422 +               fput(h_file_tmp);
14423 +               au_lcnt_dec(&br->br_nfiles);
14424 +       }
14425 +       return err;
14426 +}
14427 +
14428 +/* ---------------------------------------------------------------------- */
14429 +
14430 +static int au_reopen_wh(struct file *file, aufs_bindex_t btgt,
14431 +                       struct dentry *hi_wh)
14432 +{
14433 +       int err;
14434 +       aufs_bindex_t btop;
14435 +       struct au_dinfo *dinfo;
14436 +       struct dentry *h_dentry;
14437 +       struct au_hdentry *hdp;
14438 +
14439 +       dinfo = au_di(file->f_path.dentry);
14440 +       AuRwMustWriteLock(&dinfo->di_rwsem);
14441 +
14442 +       btop = dinfo->di_btop;
14443 +       dinfo->di_btop = btgt;
14444 +       hdp = au_hdentry(dinfo, btgt);
14445 +       h_dentry = hdp->hd_dentry;
14446 +       hdp->hd_dentry = hi_wh;
14447 +       err = au_reopen_nondir(file);
14448 +       hdp->hd_dentry = h_dentry;
14449 +       dinfo->di_btop = btop;
14450 +
14451 +       return err;
14452 +}
14453 +
14454 +static int au_ready_to_write_wh(struct file *file, loff_t len,
14455 +                               aufs_bindex_t bcpup, struct au_pin *pin)
14456 +{
14457 +       int err;
14458 +       struct inode *inode, *h_inode;
14459 +       struct dentry *h_dentry, *hi_wh;
14460 +       struct au_cp_generic cpg = {
14461 +               .dentry = file->f_path.dentry,
14462 +               .bdst   = bcpup,
14463 +               .bsrc   = -1,
14464 +               .len    = len,
14465 +               .pin    = pin
14466 +       };
14467 +
14468 +       au_update_dbtop(cpg.dentry);
14469 +       inode = d_inode(cpg.dentry);
14470 +       h_inode = NULL;
14471 +       if (au_dbtop(cpg.dentry) <= bcpup
14472 +           && au_dbbot(cpg.dentry) >= bcpup) {
14473 +               h_dentry = au_h_dptr(cpg.dentry, bcpup);
14474 +               if (h_dentry && d_is_positive(h_dentry))
14475 +                       h_inode = d_inode(h_dentry);
14476 +       }
14477 +       hi_wh = au_hi_wh(inode, bcpup);
14478 +       if (!hi_wh && !h_inode)
14479 +               err = au_sio_cpup_wh(&cpg, file);
14480 +       else
14481 +               /* already copied-up after unlink */
14482 +               err = au_reopen_wh(file, bcpup, hi_wh);
14483 +
14484 +       if (!err
14485 +           && (inode->i_nlink > 1
14486 +               || (inode->i_state & I_LINKABLE))
14487 +           && au_opt_test(au_mntflags(cpg.dentry->d_sb), PLINK))
14488 +               au_plink_append(inode, bcpup, au_h_dptr(cpg.dentry, bcpup));
14489 +
14490 +       return err;
14491 +}
14492 +
14493 +/*
14494 + * prepare the @file for writing.
14495 + */
14496 +int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin)
14497 +{
14498 +       int err;
14499 +       aufs_bindex_t dbtop;
14500 +       struct dentry *parent;
14501 +       struct inode *inode;
14502 +       struct super_block *sb;
14503 +       struct file *h_file;
14504 +       struct au_cp_generic cpg = {
14505 +               .dentry = file->f_path.dentry,
14506 +               .bdst   = -1,
14507 +               .bsrc   = -1,
14508 +               .len    = len,
14509 +               .pin    = pin,
14510 +               .flags  = AuCpup_DTIME
14511 +       };
14512 +
14513 +       sb = cpg.dentry->d_sb;
14514 +       inode = d_inode(cpg.dentry);
14515 +       cpg.bsrc = au_fbtop(file);
14516 +       err = au_test_ro(sb, cpg.bsrc, inode);
14517 +       if (!err && (au_hf_top(file)->f_mode & FMODE_WRITE)) {
14518 +               err = au_pin(pin, cpg.dentry, cpg.bsrc, AuOpt_UDBA_NONE,
14519 +                            /*flags*/0);
14520 +               goto out;
14521 +       }
14522 +
14523 +       /* need to cpup or reopen */
14524 +       parent = dget_parent(cpg.dentry);
14525 +       di_write_lock_parent(parent);
14526 +       err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
14527 +       cpg.bdst = err;
14528 +       if (unlikely(err < 0))
14529 +               goto out_dgrade;
14530 +       err = 0;
14531 +
14532 +       if (!d_unhashed(cpg.dentry) && !au_h_dptr(parent, cpg.bdst)) {
14533 +               err = au_cpup_dirs(cpg.dentry, cpg.bdst);
14534 +               if (unlikely(err))
14535 +                       goto out_dgrade;
14536 +       }
14537 +
14538 +       err = au_pin(pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
14539 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14540 +       if (unlikely(err))
14541 +               goto out_dgrade;
14542 +
14543 +       dbtop = au_dbtop(cpg.dentry);
14544 +       if (dbtop <= cpg.bdst)
14545 +               cpg.bsrc = cpg.bdst;
14546 +
14547 +       if (dbtop <= cpg.bdst           /* just reopen */
14548 +           || !d_unhashed(cpg.dentry)  /* copyup and reopen */
14549 +               ) {
14550 +               h_file = au_h_open_pre(cpg.dentry, cpg.bsrc, /*force_wr*/0);
14551 +               if (IS_ERR(h_file))
14552 +                       err = PTR_ERR(h_file);
14553 +               else {
14554 +                       di_downgrade_lock(parent, AuLock_IR);
14555 +                       if (dbtop > cpg.bdst)
14556 +                               err = au_sio_cpup_simple(&cpg);
14557 +                       if (!err)
14558 +                               err = au_reopen_nondir(file);
14559 +                       au_h_open_post(cpg.dentry, cpg.bsrc, h_file);
14560 +               }
14561 +       } else {                        /* copyup as wh and reopen */
14562 +               /*
14563 +                * since writable hfsplus branch is not supported,
14564 +                * h_open_pre/post() are unnecessary.
14565 +                */
14566 +               err = au_ready_to_write_wh(file, len, cpg.bdst, pin);
14567 +               di_downgrade_lock(parent, AuLock_IR);
14568 +       }
14569 +
14570 +       if (!err) {
14571 +               au_pin_set_parent_lflag(pin, /*lflag*/0);
14572 +               goto out_dput; /* success */
14573 +       }
14574 +       au_unpin(pin);
14575 +       goto out_unlock;
14576 +
14577 +out_dgrade:
14578 +       di_downgrade_lock(parent, AuLock_IR);
14579 +out_unlock:
14580 +       di_read_unlock(parent, AuLock_IR);
14581 +out_dput:
14582 +       dput(parent);
14583 +out:
14584 +       return err;
14585 +}
14586 +
14587 +/* ---------------------------------------------------------------------- */
14588 +
14589 +int au_do_flush(struct file *file, fl_owner_t id,
14590 +               int (*flush)(struct file *file, fl_owner_t id))
14591 +{
14592 +       int err;
14593 +       struct super_block *sb;
14594 +       struct inode *inode;
14595 +
14596 +       inode = file_inode(file);
14597 +       sb = inode->i_sb;
14598 +       si_noflush_read_lock(sb);
14599 +       fi_read_lock(file);
14600 +       ii_read_lock_child(inode);
14601 +
14602 +       err = flush(file, id);
14603 +       au_cpup_attr_timesizes(inode);
14604 +
14605 +       ii_read_unlock(inode);
14606 +       fi_read_unlock(file);
14607 +       si_read_unlock(sb);
14608 +       return err;
14609 +}
14610 +
14611 +/* ---------------------------------------------------------------------- */
14612 +
14613 +static int au_file_refresh_by_inode(struct file *file, int *need_reopen)
14614 +{
14615 +       int err;
14616 +       struct au_pin pin;
14617 +       struct au_finfo *finfo;
14618 +       struct dentry *parent, *hi_wh;
14619 +       struct inode *inode;
14620 +       struct super_block *sb;
14621 +       struct au_cp_generic cpg = {
14622 +               .dentry = file->f_path.dentry,
14623 +               .bdst   = -1,
14624 +               .bsrc   = -1,
14625 +               .len    = -1,
14626 +               .pin    = &pin,
14627 +               .flags  = AuCpup_DTIME
14628 +       };
14629 +
14630 +       FiMustWriteLock(file);
14631 +
14632 +       err = 0;
14633 +       finfo = au_fi(file);
14634 +       sb = cpg.dentry->d_sb;
14635 +       inode = d_inode(cpg.dentry);
14636 +       cpg.bdst = au_ibtop(inode);
14637 +       if (cpg.bdst == finfo->fi_btop || IS_ROOT(cpg.dentry))
14638 +               goto out;
14639 +
14640 +       parent = dget_parent(cpg.dentry);
14641 +       if (au_test_ro(sb, cpg.bdst, inode)) {
14642 +               di_read_lock_parent(parent, !AuLock_IR);
14643 +               err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
14644 +               cpg.bdst = err;
14645 +               di_read_unlock(parent, !AuLock_IR);
14646 +               if (unlikely(err < 0))
14647 +                       goto out_parent;
14648 +               err = 0;
14649 +       }
14650 +
14651 +       di_read_lock_parent(parent, AuLock_IR);
14652 +       hi_wh = au_hi_wh(inode, cpg.bdst);
14653 +       if (!S_ISDIR(inode->i_mode)
14654 +           && au_opt_test(au_mntflags(sb), PLINK)
14655 +           && au_plink_test(inode)
14656 +           && !d_unhashed(cpg.dentry)
14657 +           && cpg.bdst < au_dbtop(cpg.dentry)) {
14658 +               err = au_test_and_cpup_dirs(cpg.dentry, cpg.bdst);
14659 +               if (unlikely(err))
14660 +                       goto out_unlock;
14661 +
14662 +               /* always superio. */
14663 +               err = au_pin(&pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
14664 +                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14665 +               if (!err) {
14666 +                       err = au_sio_cpup_simple(&cpg);
14667 +                       au_unpin(&pin);
14668 +               }
14669 +       } else if (hi_wh) {
14670 +               /* already copied-up after unlink */
14671 +               err = au_reopen_wh(file, cpg.bdst, hi_wh);
14672 +               *need_reopen = 0;
14673 +       }
14674 +
14675 +out_unlock:
14676 +       di_read_unlock(parent, AuLock_IR);
14677 +out_parent:
14678 +       dput(parent);
14679 +out:
14680 +       return err;
14681 +}
14682 +
14683 +static void au_do_refresh_dir(struct file *file)
14684 +{
14685 +       aufs_bindex_t bindex, bbot, new_bindex, brid;
14686 +       struct au_hfile *p, tmp, *q;
14687 +       struct au_finfo *finfo;
14688 +       struct super_block *sb;
14689 +       struct au_fidir *fidir;
14690 +
14691 +       FiMustWriteLock(file);
14692 +
14693 +       sb = file->f_path.dentry->d_sb;
14694 +       finfo = au_fi(file);
14695 +       fidir = finfo->fi_hdir;
14696 +       AuDebugOn(!fidir);
14697 +       p = fidir->fd_hfile + finfo->fi_btop;
14698 +       brid = p->hf_br->br_id;
14699 +       bbot = fidir->fd_bbot;
14700 +       for (bindex = finfo->fi_btop; bindex <= bbot; bindex++, p++) {
14701 +               if (!p->hf_file)
14702 +                       continue;
14703 +
14704 +               new_bindex = au_br_index(sb, p->hf_br->br_id);
14705 +               if (new_bindex == bindex)
14706 +                       continue;
14707 +               if (new_bindex < 0) {
14708 +                       au_set_h_fptr(file, bindex, NULL);
14709 +                       continue;
14710 +               }
14711 +
14712 +               /* swap two lower inode, and loop again */
14713 +               q = fidir->fd_hfile + new_bindex;
14714 +               tmp = *q;
14715 +               *q = *p;
14716 +               *p = tmp;
14717 +               if (tmp.hf_file) {
14718 +                       bindex--;
14719 +                       p--;
14720 +               }
14721 +       }
14722 +
14723 +       p = fidir->fd_hfile;
14724 +       if (!au_test_mmapped(file) && !d_unlinked(file->f_path.dentry)) {
14725 +               bbot = au_sbbot(sb);
14726 +               for (finfo->fi_btop = 0; finfo->fi_btop <= bbot;
14727 +                    finfo->fi_btop++, p++)
14728 +                       if (p->hf_file) {
14729 +                               if (file_inode(p->hf_file))
14730 +                                       break;
14731 +                               au_hfput(p, /*execed*/0);
14732 +                       }
14733 +       } else {
14734 +               bbot = au_br_index(sb, brid);
14735 +               for (finfo->fi_btop = 0; finfo->fi_btop < bbot;
14736 +                    finfo->fi_btop++, p++)
14737 +                       if (p->hf_file)
14738 +                               au_hfput(p, /*execed*/0);
14739 +               bbot = au_sbbot(sb);
14740 +       }
14741 +
14742 +       p = fidir->fd_hfile + bbot;
14743 +       for (fidir->fd_bbot = bbot; fidir->fd_bbot >= finfo->fi_btop;
14744 +            fidir->fd_bbot--, p--)
14745 +               if (p->hf_file) {
14746 +                       if (file_inode(p->hf_file))
14747 +                               break;
14748 +                       au_hfput(p, /*execed*/0);
14749 +               }
14750 +       AuDebugOn(fidir->fd_bbot < finfo->fi_btop);
14751 +}
14752 +
14753 +/*
14754 + * after branch manipulating, refresh the file.
14755 + */
14756 +static int refresh_file(struct file *file, int (*reopen)(struct file *file))
14757 +{
14758 +       int err, need_reopen, nbr;
14759 +       aufs_bindex_t bbot, bindex;
14760 +       struct dentry *dentry;
14761 +       struct super_block *sb;
14762 +       struct au_finfo *finfo;
14763 +       struct au_hfile *hfile;
14764 +
14765 +       dentry = file->f_path.dentry;
14766 +       sb = dentry->d_sb;
14767 +       nbr = au_sbbot(sb) + 1;
14768 +       finfo = au_fi(file);
14769 +       if (!finfo->fi_hdir) {
14770 +               hfile = &finfo->fi_htop;
14771 +               AuDebugOn(!hfile->hf_file);
14772 +               bindex = au_br_index(sb, hfile->hf_br->br_id);
14773 +               AuDebugOn(bindex < 0);
14774 +               if (bindex != finfo->fi_btop)
14775 +                       au_set_fbtop(file, bindex);
14776 +       } else {
14777 +               err = au_fidir_realloc(finfo, nbr, /*may_shrink*/0);
14778 +               if (unlikely(err))
14779 +                       goto out;
14780 +               au_do_refresh_dir(file);
14781 +       }
14782 +
14783 +       err = 0;
14784 +       need_reopen = 1;
14785 +       if (!au_test_mmapped(file))
14786 +               err = au_file_refresh_by_inode(file, &need_reopen);
14787 +       if (finfo->fi_hdir)
14788 +               /* harmless if err */
14789 +               au_fidir_realloc(finfo, nbr, /*may_shrink*/1);
14790 +       if (!err && need_reopen && !d_unlinked(dentry))
14791 +               err = reopen(file);
14792 +       if (!err) {
14793 +               au_update_figen(file);
14794 +               goto out; /* success */
14795 +       }
14796 +
14797 +       /* error, close all lower files */
14798 +       if (finfo->fi_hdir) {
14799 +               bbot = au_fbbot_dir(file);
14800 +               for (bindex = au_fbtop(file); bindex <= bbot; bindex++)
14801 +                       au_set_h_fptr(file, bindex, NULL);
14802 +       }
14803 +
14804 +out:
14805 +       return err;
14806 +}
14807 +
14808 +/* common function to regular file and dir */
14809 +int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
14810 +                         int wlock, unsigned int fi_lsc)
14811 +{
14812 +       int err;
14813 +       unsigned int sigen, figen;
14814 +       aufs_bindex_t btop;
14815 +       unsigned char pseudo_link;
14816 +       struct dentry *dentry;
14817 +       struct inode *inode;
14818 +
14819 +       err = 0;
14820 +       dentry = file->f_path.dentry;
14821 +       inode = d_inode(dentry);
14822 +       sigen = au_sigen(dentry->d_sb);
14823 +       fi_write_lock_nested(file, fi_lsc);
14824 +       figen = au_figen(file);
14825 +       if (!fi_lsc)
14826 +               di_write_lock_child(dentry);
14827 +       else
14828 +               di_write_lock_child2(dentry);
14829 +       btop = au_dbtop(dentry);
14830 +       pseudo_link = (btop != au_ibtop(inode));
14831 +       if (sigen == figen && !pseudo_link && au_fbtop(file) == btop) {
14832 +               if (!wlock) {
14833 +                       di_downgrade_lock(dentry, AuLock_IR);
14834 +                       fi_downgrade_lock(file);
14835 +               }
14836 +               goto out; /* success */
14837 +       }
14838 +
14839 +       AuDbg("sigen %d, figen %d\n", sigen, figen);
14840 +       if (au_digen_test(dentry, sigen)) {
14841 +               err = au_reval_dpath(dentry, sigen);
14842 +               AuDebugOn(!err && au_digen_test(dentry, sigen));
14843 +       }
14844 +
14845 +       if (!err)
14846 +               err = refresh_file(file, reopen);
14847 +       if (!err) {
14848 +               if (!wlock) {
14849 +                       di_downgrade_lock(dentry, AuLock_IR);
14850 +                       fi_downgrade_lock(file);
14851 +               }
14852 +       } else {
14853 +               di_write_unlock(dentry);
14854 +               fi_write_unlock(file);
14855 +       }
14856 +
14857 +out:
14858 +       return err;
14859 +}
14860 +
14861 +/* ---------------------------------------------------------------------- */
14862 +
14863 +/* cf. aufs_nopage() */
14864 +/* for madvise(2) */
14865 +static int aufs_readpage(struct file *file __maybe_unused, struct page *page)
14866 +{
14867 +       unlock_page(page);
14868 +       return 0;
14869 +}
14870 +
14871 +/* it will never be called, but necessary to support O_DIRECT */
14872 +static ssize_t aufs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
14873 +{ BUG(); return 0; }
14874 +
14875 +/* they will never be called. */
14876 +#ifdef CONFIG_AUFS_DEBUG
14877 +static int aufs_write_begin(struct file *file, struct address_space *mapping,
14878 +                           loff_t pos, unsigned len, unsigned flags,
14879 +                           struct page **pagep, void **fsdata)
14880 +{ AuUnsupport(); return 0; }
14881 +static int aufs_write_end(struct file *file, struct address_space *mapping,
14882 +                         loff_t pos, unsigned len, unsigned copied,
14883 +                         struct page *page, void *fsdata)
14884 +{ AuUnsupport(); return 0; }
14885 +static int aufs_writepage(struct page *page, struct writeback_control *wbc)
14886 +{ AuUnsupport(); return 0; }
14887 +
14888 +static int aufs_set_page_dirty(struct page *page)
14889 +{ AuUnsupport(); return 0; }
14890 +static void aufs_invalidatepage(struct page *page, unsigned int offset,
14891 +                               unsigned int length)
14892 +{ AuUnsupport(); }
14893 +static int aufs_releasepage(struct page *page, gfp_t gfp)
14894 +{ AuUnsupport(); return 0; }
14895 +#if 0 /* called by memory compaction regardless file */
14896 +static int aufs_migratepage(struct address_space *mapping, struct page *newpage,
14897 +                           struct page *page, enum migrate_mode mode)
14898 +{ AuUnsupport(); return 0; }
14899 +#endif
14900 +static bool aufs_isolate_page(struct page *page, isolate_mode_t mode)
14901 +{ AuUnsupport(); return true; }
14902 +static void aufs_putback_page(struct page *page)
14903 +{ AuUnsupport(); }
14904 +static int aufs_launder_page(struct page *page)
14905 +{ AuUnsupport(); return 0; }
14906 +static int aufs_is_partially_uptodate(struct page *page,
14907 +                                     unsigned long from,
14908 +                                     unsigned long count)
14909 +{ AuUnsupport(); return 0; }
14910 +static void aufs_is_dirty_writeback(struct page *page, bool *dirty,
14911 +                                   bool *writeback)
14912 +{ AuUnsupport(); }
14913 +static int aufs_error_remove_page(struct address_space *mapping,
14914 +                                 struct page *page)
14915 +{ AuUnsupport(); return 0; }
14916 +static int aufs_swap_activate(struct swap_info_struct *sis, struct file *file,
14917 +                             sector_t *span)
14918 +{ AuUnsupport(); return 0; }
14919 +static void aufs_swap_deactivate(struct file *file)
14920 +{ AuUnsupport(); }
14921 +#endif /* CONFIG_AUFS_DEBUG */
14922 +
14923 +const struct address_space_operations aufs_aop = {
14924 +       .readpage               = aufs_readpage,
14925 +       .direct_IO              = aufs_direct_IO,
14926 +#ifdef CONFIG_AUFS_DEBUG
14927 +       .writepage              = aufs_writepage,
14928 +       /* no writepages, because of writepage */
14929 +       .set_page_dirty         = aufs_set_page_dirty,
14930 +       /* no readpages, because of readpage */
14931 +       .write_begin            = aufs_write_begin,
14932 +       .write_end              = aufs_write_end,
14933 +       /* no bmap, no block device */
14934 +       .invalidatepage         = aufs_invalidatepage,
14935 +       .releasepage            = aufs_releasepage,
14936 +       /* is fallback_migrate_page ok? */
14937 +       /* .migratepage         = aufs_migratepage, */
14938 +       .isolate_page           = aufs_isolate_page,
14939 +       .putback_page           = aufs_putback_page,
14940 +       .launder_page           = aufs_launder_page,
14941 +       .is_partially_uptodate  = aufs_is_partially_uptodate,
14942 +       .is_dirty_writeback     = aufs_is_dirty_writeback,
14943 +       .error_remove_page      = aufs_error_remove_page,
14944 +       .swap_activate          = aufs_swap_activate,
14945 +       .swap_deactivate        = aufs_swap_deactivate
14946 +#endif /* CONFIG_AUFS_DEBUG */
14947 +};
14948 diff -urN /usr/share/empty/fs/aufs/file.h linux/fs/aufs/file.h
14949 --- /usr/share/empty/fs/aufs/file.h     1970-01-01 01:00:00.000000000 +0100
14950 +++ linux/fs/aufs/file.h        2021-11-01 23:48:34.206359262 +0100
14951 @@ -0,0 +1,342 @@
14952 +/* SPDX-License-Identifier: GPL-2.0 */
14953 +/*
14954 + * Copyright (C) 2005-2021 Junjiro R. Okajima
14955 + *
14956 + * This program, aufs is free software; you can redistribute it and/or modify
14957 + * it under the terms of the GNU General Public License as published by
14958 + * the Free Software Foundation; either version 2 of the License, or
14959 + * (at your option) any later version.
14960 + *
14961 + * This program is distributed in the hope that it will be useful,
14962 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
14963 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14964 + * GNU General Public License for more details.
14965 + *
14966 + * You should have received a copy of the GNU General Public License
14967 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
14968 + */
14969 +
14970 +/*
14971 + * file operations
14972 + */
14973 +
14974 +#ifndef __AUFS_FILE_H__
14975 +#define __AUFS_FILE_H__
14976 +
14977 +#ifdef __KERNEL__
14978 +
14979 +#include <linux/file.h>
14980 +#include <linux/fs.h>
14981 +#include <linux/mm_types.h>
14982 +#include <linux/poll.h>
14983 +#include "rwsem.h"
14984 +
14985 +struct au_branch;
14986 +struct au_hfile {
14987 +       struct file             *hf_file;
14988 +       struct au_branch        *hf_br;
14989 +};
14990 +
14991 +struct au_vdir;
14992 +struct au_fidir {
14993 +       aufs_bindex_t           fd_bbot;
14994 +       aufs_bindex_t           fd_nent;
14995 +       struct au_vdir          *fd_vdir_cache;
14996 +       struct au_hfile         fd_hfile[];
14997 +};
14998 +
14999 +static inline int au_fidir_sz(int nent)
15000 +{
15001 +       AuDebugOn(nent < 0);
15002 +       return sizeof(struct au_fidir) + sizeof(struct au_hfile) * nent;
15003 +}
15004 +
15005 +struct au_finfo {
15006 +       atomic_t                fi_generation;
15007 +
15008 +       struct au_rwsem         fi_rwsem;
15009 +       aufs_bindex_t           fi_btop;
15010 +
15011 +       /* do not union them */
15012 +       struct {                                /* for non-dir */
15013 +               struct au_hfile                 fi_htop;
15014 +               atomic_t                        fi_mmapped;
15015 +       };
15016 +       struct au_fidir         *fi_hdir;       /* for dir only */
15017 +
15018 +       struct hlist_bl_node    fi_hlist;
15019 +       struct file             *fi_file;       /* very ugly */
15020 +       struct rcu_head         rcu;
15021 +} ____cacheline_aligned_in_smp;
15022 +
15023 +/* ---------------------------------------------------------------------- */
15024 +
15025 +/* file.c */
15026 +extern const struct address_space_operations aufs_aop;
15027 +unsigned int au_file_roflags(unsigned int flags);
15028 +struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
15029 +                      struct file *file, int force_wr);
15030 +struct au_do_open_args {
15031 +       int             aopen;
15032 +       int             (*open)(struct file *file, int flags,
15033 +                               struct file *h_file);
15034 +       struct au_fidir *fidir;
15035 +       struct file     *h_file;
15036 +};
15037 +int au_do_open(struct file *file, struct au_do_open_args *args);
15038 +int au_reopen_nondir(struct file *file);
15039 +struct au_pin;
15040 +int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin);
15041 +int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
15042 +                         int wlock, unsigned int fi_lsc);
15043 +int au_do_flush(struct file *file, fl_owner_t id,
15044 +               int (*flush)(struct file *file, fl_owner_t id));
15045 +
15046 +/* poll.c */
15047 +#ifdef CONFIG_AUFS_POLL
15048 +__poll_t aufs_poll(struct file *file, struct poll_table_struct *pt);
15049 +#endif
15050 +
15051 +#ifdef CONFIG_AUFS_BR_HFSPLUS
15052 +/* hfsplus.c */
15053 +struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
15054 +                          int force_wr);
15055 +void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
15056 +                   struct file *h_file);
15057 +#else
15058 +AuStub(struct file *, au_h_open_pre, return NULL, struct dentry *dentry,
15059 +       aufs_bindex_t bindex, int force_wr)
15060 +AuStubVoid(au_h_open_post, struct dentry *dentry, aufs_bindex_t bindex,
15061 +          struct file *h_file);
15062 +#endif
15063 +
15064 +/* f_op.c */
15065 +extern const struct file_operations aufs_file_fop;
15066 +int au_do_open_nondir(struct file *file, int flags, struct file *h_file);
15067 +int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file);
15068 +struct file *au_read_pre(struct file *file, int keep_fi, unsigned int lsc);
15069 +
15070 +/* finfo.c */
15071 +void au_hfput(struct au_hfile *hf, int execed);
15072 +void au_set_h_fptr(struct file *file, aufs_bindex_t bindex,
15073 +                  struct file *h_file);
15074 +
15075 +void au_update_figen(struct file *file);
15076 +struct au_fidir *au_fidir_alloc(struct super_block *sb);
15077 +int au_fidir_realloc(struct au_finfo *finfo, int nbr, int may_shrink);
15078 +
15079 +void au_fi_init_once(void *_fi);
15080 +void au_finfo_fin(struct file *file);
15081 +int au_finfo_init(struct file *file, struct au_fidir *fidir);
15082 +
15083 +/* ioctl.c */
15084 +long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg);
15085 +#ifdef CONFIG_COMPAT
15086 +long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
15087 +                          unsigned long arg);
15088 +long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
15089 +                             unsigned long arg);
15090 +#endif
15091 +
15092 +/* ---------------------------------------------------------------------- */
15093 +
15094 +static inline struct au_finfo *au_fi(struct file *file)
15095 +{
15096 +       return file->private_data;
15097 +}
15098 +
15099 +/* ---------------------------------------------------------------------- */
15100 +
15101 +#define fi_read_lock(f)        au_rw_read_lock(&au_fi(f)->fi_rwsem)
15102 +#define fi_write_lock(f)       au_rw_write_lock(&au_fi(f)->fi_rwsem)
15103 +#define fi_read_trylock(f)     au_rw_read_trylock(&au_fi(f)->fi_rwsem)
15104 +#define fi_write_trylock(f)    au_rw_write_trylock(&au_fi(f)->fi_rwsem)
15105 +/*
15106 +#define fi_read_trylock_nested(f) \
15107 +       au_rw_read_trylock_nested(&au_fi(f)->fi_rwsem)
15108 +#define fi_write_trylock_nested(f) \
15109 +       au_rw_write_trylock_nested(&au_fi(f)->fi_rwsem)
15110 +*/
15111 +
15112 +#define fi_read_unlock(f)      au_rw_read_unlock(&au_fi(f)->fi_rwsem)
15113 +#define fi_write_unlock(f)     au_rw_write_unlock(&au_fi(f)->fi_rwsem)
15114 +#define fi_downgrade_lock(f)   au_rw_dgrade_lock(&au_fi(f)->fi_rwsem)
15115 +
15116 +/* lock subclass for finfo */
15117 +enum {
15118 +       AuLsc_FI_1,
15119 +       AuLsc_FI_2
15120 +};
15121 +
15122 +static inline void fi_read_lock_nested(struct file *f, unsigned int lsc)
15123 +{
15124 +       au_rw_read_lock_nested(&au_fi(f)->fi_rwsem, lsc);
15125 +}
15126 +
15127 +static inline void fi_write_lock_nested(struct file *f, unsigned int lsc)
15128 +{
15129 +       au_rw_write_lock_nested(&au_fi(f)->fi_rwsem, lsc);
15130 +}
15131 +
15132 +/*
15133 + * fi_read_lock_1, fi_write_lock_1,
15134 + * fi_read_lock_2, fi_write_lock_2
15135 + */
15136 +#define AuReadLockFunc(name) \
15137 +static inline void fi_read_lock_##name(struct file *f) \
15138 +{ fi_read_lock_nested(f, AuLsc_FI_##name); }
15139 +
15140 +#define AuWriteLockFunc(name) \
15141 +static inline void fi_write_lock_##name(struct file *f) \
15142 +{ fi_write_lock_nested(f, AuLsc_FI_##name); }
15143 +
15144 +#define AuRWLockFuncs(name) \
15145 +       AuReadLockFunc(name) \
15146 +       AuWriteLockFunc(name)
15147 +
15148 +AuRWLockFuncs(1);
15149 +AuRWLockFuncs(2);
15150 +
15151 +#undef AuReadLockFunc
15152 +#undef AuWriteLockFunc
15153 +#undef AuRWLockFuncs
15154 +
15155 +#define FiMustNoWaiters(f)     AuRwMustNoWaiters(&au_fi(f)->fi_rwsem)
15156 +#define FiMustAnyLock(f)       AuRwMustAnyLock(&au_fi(f)->fi_rwsem)
15157 +#define FiMustWriteLock(f)     AuRwMustWriteLock(&au_fi(f)->fi_rwsem)
15158 +
15159 +/* ---------------------------------------------------------------------- */
15160 +
15161 +/* todo: hard/soft set? */
15162 +static inline aufs_bindex_t au_fbtop(struct file *file)
15163 +{
15164 +       FiMustAnyLock(file);
15165 +       return au_fi(file)->fi_btop;
15166 +}
15167 +
15168 +static inline aufs_bindex_t au_fbbot_dir(struct file *file)
15169 +{
15170 +       FiMustAnyLock(file);
15171 +       AuDebugOn(!au_fi(file)->fi_hdir);
15172 +       return au_fi(file)->fi_hdir->fd_bbot;
15173 +}
15174 +
15175 +static inline struct au_vdir *au_fvdir_cache(struct file *file)
15176 +{
15177 +       FiMustAnyLock(file);
15178 +       AuDebugOn(!au_fi(file)->fi_hdir);
15179 +       return au_fi(file)->fi_hdir->fd_vdir_cache;
15180 +}
15181 +
15182 +static inline void au_set_fbtop(struct file *file, aufs_bindex_t bindex)
15183 +{
15184 +       FiMustWriteLock(file);
15185 +       au_fi(file)->fi_btop = bindex;
15186 +}
15187 +
15188 +static inline void au_set_fbbot_dir(struct file *file, aufs_bindex_t bindex)
15189 +{
15190 +       FiMustWriteLock(file);
15191 +       AuDebugOn(!au_fi(file)->fi_hdir);
15192 +       au_fi(file)->fi_hdir->fd_bbot = bindex;
15193 +}
15194 +
15195 +static inline void au_set_fvdir_cache(struct file *file,
15196 +                                     struct au_vdir *vdir_cache)
15197 +{
15198 +       FiMustWriteLock(file);
15199 +       AuDebugOn(!au_fi(file)->fi_hdir);
15200 +       au_fi(file)->fi_hdir->fd_vdir_cache = vdir_cache;
15201 +}
15202 +
15203 +static inline struct file *au_hf_top(struct file *file)
15204 +{
15205 +       FiMustAnyLock(file);
15206 +       AuDebugOn(au_fi(file)->fi_hdir);
15207 +       return au_fi(file)->fi_htop.hf_file;
15208 +}
15209 +
15210 +static inline struct file *au_hf_dir(struct file *file, aufs_bindex_t bindex)
15211 +{
15212 +       FiMustAnyLock(file);
15213 +       AuDebugOn(!au_fi(file)->fi_hdir);
15214 +       return au_fi(file)->fi_hdir->fd_hfile[0 + bindex].hf_file;
15215 +}
15216 +
15217 +/* todo: memory barrier? */
15218 +static inline unsigned int au_figen(struct file *f)
15219 +{
15220 +       return atomic_read(&au_fi(f)->fi_generation);
15221 +}
15222 +
15223 +static inline void au_set_mmapped(struct file *f)
15224 +{
15225 +       if (atomic_inc_return(&au_fi(f)->fi_mmapped))
15226 +               return;
15227 +       pr_warn("fi_mmapped wrapped around\n");
15228 +       while (!atomic_inc_return(&au_fi(f)->fi_mmapped))
15229 +               ;
15230 +}
15231 +
15232 +static inline void au_unset_mmapped(struct file *f)
15233 +{
15234 +       atomic_dec(&au_fi(f)->fi_mmapped);
15235 +}
15236 +
15237 +static inline int au_test_mmapped(struct file *f)
15238 +{
15239 +       return atomic_read(&au_fi(f)->fi_mmapped);
15240 +}
15241 +
15242 +/* customize vma->vm_file */
15243 +
15244 +static inline void au_do_vm_file_reset(struct vm_area_struct *vma,
15245 +                                      struct file *file)
15246 +{
15247 +       struct file *f;
15248 +
15249 +       f = vma->vm_file;
15250 +       get_file(file);
15251 +       vma->vm_file = file;
15252 +       fput(f);
15253 +}
15254 +
15255 +#ifdef CONFIG_MMU
15256 +#define AuDbgVmRegion(file, vma) do {} while (0)
15257 +
15258 +static inline void au_vm_file_reset(struct vm_area_struct *vma,
15259 +                                   struct file *file)
15260 +{
15261 +       au_do_vm_file_reset(vma, file);
15262 +}
15263 +#else
15264 +#define AuDbgVmRegion(file, vma) \
15265 +       AuDebugOn((vma)->vm_region && (vma)->vm_region->vm_file != (file))
15266 +
15267 +static inline void au_vm_file_reset(struct vm_area_struct *vma,
15268 +                                   struct file *file)
15269 +{
15270 +       struct file *f;
15271 +
15272 +       au_do_vm_file_reset(vma, file);
15273 +       f = vma->vm_region->vm_file;
15274 +       get_file(file);
15275 +       vma->vm_region->vm_file = file;
15276 +       fput(f);
15277 +}
15278 +#endif /* CONFIG_MMU */
15279 +
15280 +/* handle vma->vm_prfile */
15281 +static inline void au_vm_prfile_set(struct vm_area_struct *vma,
15282 +                                   struct file *file)
15283 +{
15284 +       get_file(file);
15285 +       vma->vm_prfile = file;
15286 +#ifndef CONFIG_MMU
15287 +       get_file(file);
15288 +       vma->vm_region->vm_prfile = file;
15289 +#endif
15290 +}
15291 +
15292 +#endif /* __KERNEL__ */
15293 +#endif /* __AUFS_FILE_H__ */
15294 diff -urN /usr/share/empty/fs/aufs/finfo.c linux/fs/aufs/finfo.c
15295 --- /usr/share/empty/fs/aufs/finfo.c    1970-01-01 01:00:00.000000000 +0100
15296 +++ linux/fs/aufs/finfo.c       2021-11-01 23:48:34.206359262 +0100
15297 @@ -0,0 +1,149 @@
15298 +// SPDX-License-Identifier: GPL-2.0
15299 +/*
15300 + * Copyright (C) 2005-2021 Junjiro R. Okajima
15301 + *
15302 + * This program, aufs is free software; you can redistribute it and/or modify
15303 + * it under the terms of the GNU General Public License as published by
15304 + * the Free Software Foundation; either version 2 of the License, or
15305 + * (at your option) any later version.
15306 + *
15307 + * This program is distributed in the hope that it will be useful,
15308 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
15309 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15310 + * GNU General Public License for more details.
15311 + *
15312 + * You should have received a copy of the GNU General Public License
15313 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15314 + */
15315 +
15316 +/*
15317 + * file private data
15318 + */
15319 +
15320 +#include "aufs.h"
15321 +
15322 +void au_hfput(struct au_hfile *hf, int execed)
15323 +{
15324 +       if (execed)
15325 +               allow_write_access(hf->hf_file);
15326 +       fput(hf->hf_file);
15327 +       hf->hf_file = NULL;
15328 +       au_lcnt_dec(&hf->hf_br->br_nfiles);
15329 +       hf->hf_br = NULL;
15330 +}
15331 +
15332 +void au_set_h_fptr(struct file *file, aufs_bindex_t bindex, struct file *val)
15333 +{
15334 +       struct au_finfo *finfo = au_fi(file);
15335 +       struct au_hfile *hf;
15336 +       struct au_fidir *fidir;
15337 +
15338 +       fidir = finfo->fi_hdir;
15339 +       if (!fidir) {
15340 +               AuDebugOn(finfo->fi_btop != bindex);
15341 +               hf = &finfo->fi_htop;
15342 +       } else
15343 +               hf = fidir->fd_hfile + bindex;
15344 +
15345 +       if (hf && hf->hf_file)
15346 +               au_hfput(hf, vfsub_file_execed(file));
15347 +       if (val) {
15348 +               FiMustWriteLock(file);
15349 +               AuDebugOn(IS_ERR_OR_NULL(file->f_path.dentry));
15350 +               hf->hf_file = val;
15351 +               hf->hf_br = au_sbr(file->f_path.dentry->d_sb, bindex);
15352 +       }
15353 +}
15354 +
15355 +void au_update_figen(struct file *file)
15356 +{
15357 +       atomic_set(&au_fi(file)->fi_generation, au_digen(file->f_path.dentry));
15358 +       /* smp_mb(); */ /* atomic_set */
15359 +}
15360 +
15361 +/* ---------------------------------------------------------------------- */
15362 +
15363 +struct au_fidir *au_fidir_alloc(struct super_block *sb)
15364 +{
15365 +       struct au_fidir *fidir;
15366 +       int nbr;
15367 +
15368 +       nbr = au_sbbot(sb) + 1;
15369 +       if (nbr < 2)
15370 +               nbr = 2; /* initial allocate for 2 branches */
15371 +       fidir = kzalloc(au_fidir_sz(nbr), GFP_NOFS);
15372 +       if (fidir) {
15373 +               fidir->fd_bbot = -1;
15374 +               fidir->fd_nent = nbr;
15375 +       }
15376 +
15377 +       return fidir;
15378 +}
15379 +
15380 +int au_fidir_realloc(struct au_finfo *finfo, int nbr, int may_shrink)
15381 +{
15382 +       int err;
15383 +       struct au_fidir *fidir, *p;
15384 +
15385 +       AuRwMustWriteLock(&finfo->fi_rwsem);
15386 +       fidir = finfo->fi_hdir;
15387 +       AuDebugOn(!fidir);
15388 +
15389 +       err = -ENOMEM;
15390 +       p = au_kzrealloc(fidir, au_fidir_sz(fidir->fd_nent), au_fidir_sz(nbr),
15391 +                        GFP_NOFS, may_shrink);
15392 +       if (p) {
15393 +               p->fd_nent = nbr;
15394 +               finfo->fi_hdir = p;
15395 +               err = 0;
15396 +       }
15397 +
15398 +       return err;
15399 +}
15400 +
15401 +/* ---------------------------------------------------------------------- */
15402 +
15403 +void au_finfo_fin(struct file *file)
15404 +{
15405 +       struct au_finfo *finfo;
15406 +
15407 +       au_lcnt_dec(&au_sbi(file->f_path.dentry->d_sb)->si_nfiles);
15408 +
15409 +       finfo = au_fi(file);
15410 +       AuDebugOn(finfo->fi_hdir);
15411 +       AuRwDestroy(&finfo->fi_rwsem);
15412 +       au_cache_free_finfo(finfo);
15413 +}
15414 +
15415 +void au_fi_init_once(void *_finfo)
15416 +{
15417 +       struct au_finfo *finfo = _finfo;
15418 +
15419 +       au_rw_init(&finfo->fi_rwsem);
15420 +}
15421 +
15422 +int au_finfo_init(struct file *file, struct au_fidir *fidir)
15423 +{
15424 +       int err;
15425 +       struct au_finfo *finfo;
15426 +       struct dentry *dentry;
15427 +
15428 +       err = -ENOMEM;
15429 +       dentry = file->f_path.dentry;
15430 +       finfo = au_cache_alloc_finfo();
15431 +       if (unlikely(!finfo))
15432 +               goto out;
15433 +
15434 +       err = 0;
15435 +       au_lcnt_inc(&au_sbi(dentry->d_sb)->si_nfiles);
15436 +       au_rw_write_lock(&finfo->fi_rwsem);
15437 +       finfo->fi_btop = -1;
15438 +       finfo->fi_hdir = fidir;
15439 +       atomic_set(&finfo->fi_generation, au_digen(dentry));
15440 +       /* smp_mb(); */ /* atomic_set */
15441 +
15442 +       file->private_data = finfo;
15443 +
15444 +out:
15445 +       return err;
15446 +}
15447 diff -urN /usr/share/empty/fs/aufs/f_op.c linux/fs/aufs/f_op.c
15448 --- /usr/share/empty/fs/aufs/f_op.c     1970-01-01 01:00:00.000000000 +0100
15449 +++ linux/fs/aufs/f_op.c        2021-11-01 23:48:34.206359262 +0100
15450 @@ -0,0 +1,771 @@
15451 +// SPDX-License-Identifier: GPL-2.0
15452 +/*
15453 + * Copyright (C) 2005-2021 Junjiro R. Okajima
15454 + *
15455 + * This program, aufs is free software; you can redistribute it and/or modify
15456 + * it under the terms of the GNU General Public License as published by
15457 + * the Free Software Foundation; either version 2 of the License, or
15458 + * (at your option) any later version.
15459 + *
15460 + * This program is distributed in the hope that it will be useful,
15461 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
15462 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15463 + * GNU General Public License for more details.
15464 + *
15465 + * You should have received a copy of the GNU General Public License
15466 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15467 + */
15468 +
15469 +/*
15470 + * file and vm operations
15471 + */
15472 +
15473 +#include <linux/aio.h>
15474 +#include <linux/fs_stack.h>
15475 +#include <linux/mman.h>
15476 +#include <linux/security.h>
15477 +#include "aufs.h"
15478 +
15479 +int au_do_open_nondir(struct file *file, int flags, struct file *h_file)
15480 +{
15481 +       int err;
15482 +       aufs_bindex_t bindex;
15483 +       struct dentry *dentry, *h_dentry;
15484 +       struct au_finfo *finfo;
15485 +       struct inode *h_inode;
15486 +
15487 +       FiMustWriteLock(file);
15488 +
15489 +       err = 0;
15490 +       dentry = file->f_path.dentry;
15491 +       AuDebugOn(IS_ERR_OR_NULL(dentry));
15492 +       finfo = au_fi(file);
15493 +       memset(&finfo->fi_htop, 0, sizeof(finfo->fi_htop));
15494 +       atomic_set(&finfo->fi_mmapped, 0);
15495 +       bindex = au_dbtop(dentry);
15496 +       if (!h_file) {
15497 +               h_dentry = au_h_dptr(dentry, bindex);
15498 +               err = vfsub_test_mntns(file->f_path.mnt, h_dentry->d_sb);
15499 +               if (unlikely(err))
15500 +                       goto out;
15501 +               h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
15502 +               if (IS_ERR(h_file)) {
15503 +                       err = PTR_ERR(h_file);
15504 +                       goto out;
15505 +               }
15506 +       } else {
15507 +               h_dentry = h_file->f_path.dentry;
15508 +               err = vfsub_test_mntns(file->f_path.mnt, h_dentry->d_sb);
15509 +               if (unlikely(err))
15510 +                       goto out;
15511 +               /* br ref is already inc-ed */
15512 +       }
15513 +
15514 +       if ((flags & __O_TMPFILE)
15515 +           && !(flags & O_EXCL)) {
15516 +               h_inode = file_inode(h_file);
15517 +               spin_lock(&h_inode->i_lock);
15518 +               h_inode->i_state |= I_LINKABLE;
15519 +               spin_unlock(&h_inode->i_lock);
15520 +       }
15521 +       au_set_fbtop(file, bindex);
15522 +       au_set_h_fptr(file, bindex, h_file);
15523 +       au_update_figen(file);
15524 +       /* todo: necessary? */
15525 +       /* file->f_ra = h_file->f_ra; */
15526 +
15527 +out:
15528 +       return err;
15529 +}
15530 +
15531 +static int aufs_open_nondir(struct inode *inode __maybe_unused,
15532 +                           struct file *file)
15533 +{
15534 +       int err;
15535 +       struct super_block *sb;
15536 +       struct au_do_open_args args = {
15537 +               .open   = au_do_open_nondir
15538 +       };
15539 +
15540 +       AuDbg("%pD, f_flags 0x%x, f_mode 0x%x\n",
15541 +             file, vfsub_file_flags(file), file->f_mode);
15542 +
15543 +       sb = file->f_path.dentry->d_sb;
15544 +       si_read_lock(sb, AuLock_FLUSH);
15545 +       err = au_do_open(file, &args);
15546 +       si_read_unlock(sb);
15547 +       return err;
15548 +}
15549 +
15550 +int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file)
15551 +{
15552 +       struct au_finfo *finfo;
15553 +       aufs_bindex_t bindex;
15554 +
15555 +       finfo = au_fi(file);
15556 +       au_hbl_del(&finfo->fi_hlist,
15557 +                  &au_sbi(file->f_path.dentry->d_sb)->si_files);
15558 +       bindex = finfo->fi_btop;
15559 +       if (bindex >= 0)
15560 +               au_set_h_fptr(file, bindex, NULL);
15561 +
15562 +       au_finfo_fin(file);
15563 +       return 0;
15564 +}
15565 +
15566 +/* ---------------------------------------------------------------------- */
15567 +
15568 +static int au_do_flush_nondir(struct file *file, fl_owner_t id)
15569 +{
15570 +       int err;
15571 +       struct file *h_file;
15572 +
15573 +       err = 0;
15574 +       h_file = au_hf_top(file);
15575 +       if (h_file)
15576 +               err = vfsub_flush(h_file, id);
15577 +       return err;
15578 +}
15579 +
15580 +static int aufs_flush_nondir(struct file *file, fl_owner_t id)
15581 +{
15582 +       return au_do_flush(file, id, au_do_flush_nondir);
15583 +}
15584 +
15585 +/* ---------------------------------------------------------------------- */
15586 +/*
15587 + * read and write functions acquire [fdi]_rwsem once, but release before
15588 + * mmap_sem. This is because to stop a race condition between mmap(2).
15589 + * Releasing these aufs-rwsem should be safe, no branch-management (by keeping
15590 + * si_rwsem), no harmful copy-up should happen. Actually copy-up may happen in
15591 + * read functions after [fdi]_rwsem are released, but it should be harmless.
15592 + */
15593 +
15594 +/* Callers should call au_read_post() or fput() in the end */
15595 +struct file *au_read_pre(struct file *file, int keep_fi, unsigned int lsc)
15596 +{
15597 +       struct file *h_file;
15598 +       int err;
15599 +
15600 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/0, lsc);
15601 +       if (!err) {
15602 +               di_read_unlock(file->f_path.dentry, AuLock_IR);
15603 +               h_file = au_hf_top(file);
15604 +               get_file(h_file);
15605 +               if (!keep_fi)
15606 +                       fi_read_unlock(file);
15607 +       } else
15608 +               h_file = ERR_PTR(err);
15609 +
15610 +       return h_file;
15611 +}
15612 +
15613 +static void au_read_post(struct inode *inode, struct file *h_file)
15614 +{
15615 +       /* update without lock, I don't think it a problem */
15616 +       fsstack_copy_attr_atime(inode, file_inode(h_file));
15617 +       fput(h_file);
15618 +}
15619 +
15620 +struct au_write_pre {
15621 +       /* input */
15622 +       unsigned int lsc;
15623 +
15624 +       /* output */
15625 +       blkcnt_t blks;
15626 +       aufs_bindex_t btop;
15627 +};
15628 +
15629 +/*
15630 + * return with iinfo is write-locked
15631 + * callers should call au_write_post() or iinfo_write_unlock() + fput() in the
15632 + * end
15633 + */
15634 +static struct file *au_write_pre(struct file *file, int do_ready,
15635 +                                struct au_write_pre *wpre)
15636 +{
15637 +       struct file *h_file;
15638 +       struct dentry *dentry;
15639 +       int err;
15640 +       unsigned int lsc;
15641 +       struct au_pin pin;
15642 +
15643 +       lsc = 0;
15644 +       if (wpre)
15645 +               lsc = wpre->lsc;
15646 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1, lsc);
15647 +       h_file = ERR_PTR(err);
15648 +       if (unlikely(err))
15649 +               goto out;
15650 +
15651 +       dentry = file->f_path.dentry;
15652 +       if (do_ready) {
15653 +               err = au_ready_to_write(file, -1, &pin);
15654 +               if (unlikely(err)) {
15655 +                       h_file = ERR_PTR(err);
15656 +                       di_write_unlock(dentry);
15657 +                       goto out_fi;
15658 +               }
15659 +       }
15660 +
15661 +       di_downgrade_lock(dentry, /*flags*/0);
15662 +       if (wpre)
15663 +               wpre->btop = au_fbtop(file);
15664 +       h_file = au_hf_top(file);
15665 +       get_file(h_file);
15666 +       if (wpre)
15667 +               wpre->blks = file_inode(h_file)->i_blocks;
15668 +       if (do_ready)
15669 +               au_unpin(&pin);
15670 +       di_read_unlock(dentry, /*flags*/0);
15671 +
15672 +out_fi:
15673 +       fi_write_unlock(file);
15674 +out:
15675 +       return h_file;
15676 +}
15677 +
15678 +static void au_write_post(struct inode *inode, struct file *h_file,
15679 +                         struct au_write_pre *wpre, ssize_t written)
15680 +{
15681 +       struct inode *h_inode;
15682 +
15683 +       au_cpup_attr_timesizes(inode);
15684 +       AuDebugOn(au_ibtop(inode) != wpre->btop);
15685 +       h_inode = file_inode(h_file);
15686 +       inode->i_mode = h_inode->i_mode;
15687 +       ii_write_unlock(inode);
15688 +       /* AuDbg("blks %llu, %llu\n", (u64)blks, (u64)h_inode->i_blocks); */
15689 +       if (written > 0)
15690 +               au_fhsm_wrote(inode->i_sb, wpre->btop,
15691 +                             /*force*/h_inode->i_blocks > wpre->blks);
15692 +       fput(h_file);
15693 +}
15694 +
15695 +/*
15696 + * todo: very ugly
15697 + * it locks both of i_mutex and si_rwsem for read in safe.
15698 + * if the plink maintenance mode continues forever (that is the problem),
15699 + * may loop forever.
15700 + */
15701 +static void au_mtx_and_read_lock(struct inode *inode)
15702 +{
15703 +       int err;
15704 +       struct super_block *sb = inode->i_sb;
15705 +
15706 +       while (1) {
15707 +               inode_lock(inode);
15708 +               err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
15709 +               if (!err)
15710 +                       break;
15711 +               inode_unlock(inode);
15712 +               si_read_lock(sb, AuLock_NOPLMW);
15713 +               si_read_unlock(sb);
15714 +       }
15715 +}
15716 +
15717 +static ssize_t au_do_iter(struct file *h_file, int rw, struct kiocb *kio,
15718 +                         struct iov_iter *iov_iter)
15719 +{
15720 +       ssize_t err;
15721 +       struct file *file;
15722 +       ssize_t (*iter)(struct kiocb *, struct iov_iter *);
15723 +
15724 +       err = security_file_permission(h_file, rw);
15725 +       if (unlikely(err))
15726 +               goto out;
15727 +
15728 +       err = -ENOSYS;  /* the branch doesn't have its ->(read|write)_iter() */
15729 +       iter = NULL;
15730 +       if (rw == MAY_READ)
15731 +               iter = h_file->f_op->read_iter;
15732 +       else if (rw == MAY_WRITE)
15733 +               iter = h_file->f_op->write_iter;
15734 +
15735 +       file = kio->ki_filp;
15736 +       kio->ki_filp = h_file;
15737 +       if (iter) {
15738 +               lockdep_off();
15739 +               err = iter(kio, iov_iter);
15740 +               lockdep_on();
15741 +       } else
15742 +               /* currently there is no such fs */
15743 +               WARN_ON_ONCE(1);
15744 +       kio->ki_filp = file;
15745 +
15746 +out:
15747 +       return err;
15748 +}
15749 +
15750 +static ssize_t aufs_read_iter(struct kiocb *kio, struct iov_iter *iov_iter)
15751 +{
15752 +       ssize_t err;
15753 +       struct file *file, *h_file;
15754 +       struct inode *inode;
15755 +       struct super_block *sb;
15756 +
15757 +       file = kio->ki_filp;
15758 +       inode = file_inode(file);
15759 +       sb = inode->i_sb;
15760 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
15761 +
15762 +       h_file = au_read_pre(file, /*keep_fi*/1, /*lsc*/0);
15763 +       err = PTR_ERR(h_file);
15764 +       if (IS_ERR(h_file))
15765 +               goto out;
15766 +
15767 +       if (au_test_loopback_kthread()) {
15768 +               au_warn_loopback(h_file->f_path.dentry->d_sb);
15769 +               if (file->f_mapping != h_file->f_mapping) {
15770 +                       file->f_mapping = h_file->f_mapping;
15771 +                       smp_mb(); /* unnecessary? */
15772 +               }
15773 +       }
15774 +       fi_read_unlock(file);
15775 +
15776 +       err = au_do_iter(h_file, MAY_READ, kio, iov_iter);
15777 +       /* todo: necessary? */
15778 +       /* file->f_ra = h_file->f_ra; */
15779 +       au_read_post(inode, h_file);
15780 +
15781 +out:
15782 +       si_read_unlock(sb);
15783 +       return err;
15784 +}
15785 +
15786 +static ssize_t aufs_write_iter(struct kiocb *kio, struct iov_iter *iov_iter)
15787 +{
15788 +       ssize_t err;
15789 +       struct au_write_pre wpre;
15790 +       struct inode *inode;
15791 +       struct file *file, *h_file;
15792 +
15793 +       file = kio->ki_filp;
15794 +       inode = file_inode(file);
15795 +       au_mtx_and_read_lock(inode);
15796 +
15797 +       wpre.lsc = 0;
15798 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15799 +       err = PTR_ERR(h_file);
15800 +       if (IS_ERR(h_file))
15801 +               goto out;
15802 +
15803 +       err = au_do_iter(h_file, MAY_WRITE, kio, iov_iter);
15804 +       au_write_post(inode, h_file, &wpre, err);
15805 +
15806 +out:
15807 +       si_read_unlock(inode->i_sb);
15808 +       inode_unlock(inode);
15809 +       return err;
15810 +}
15811 +
15812 +/*
15813 + * We may be able to remove aufs_splice_{read,write}() since almost all FSes
15814 + * don't have their own .splice_{read,write} implimentations, and they use
15815 + * generic_file_splice_read() and iter_file_splice_write() who can act like the
15816 + * simple converters to f_op->iter_read() and ->iter_write().
15817 + * But we keep our own implementations because some non-mainlined FSes may have
15818 + * their own .splice_{read,write} implimentations and aufs doesn't want to take
15819 + * away an opportunity to co-work with aufs from them.
15820 + */
15821 +static ssize_t aufs_splice_read(struct file *file, loff_t *ppos,
15822 +                               struct pipe_inode_info *pipe, size_t len,
15823 +                               unsigned int flags)
15824 +{
15825 +       ssize_t err;
15826 +       struct file *h_file;
15827 +       struct inode *inode;
15828 +       struct super_block *sb;
15829 +
15830 +       inode = file_inode(file);
15831 +       sb = inode->i_sb;
15832 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
15833 +
15834 +       h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
15835 +       err = PTR_ERR(h_file);
15836 +       if (IS_ERR(h_file))
15837 +               goto out;
15838 +
15839 +       err = vfsub_splice_to(h_file, ppos, pipe, len, flags);
15840 +       /* todo: necessary? */
15841 +       /* file->f_ra = h_file->f_ra; */
15842 +       au_read_post(inode, h_file);
15843 +
15844 +out:
15845 +       si_read_unlock(sb);
15846 +       return err;
15847 +}
15848 +
15849 +static ssize_t
15850 +aufs_splice_write(struct pipe_inode_info *pipe, struct file *file, loff_t *ppos,
15851 +                 size_t len, unsigned int flags)
15852 +{
15853 +       ssize_t err;
15854 +       struct au_write_pre wpre;
15855 +       struct inode *inode;
15856 +       struct file *h_file;
15857 +
15858 +       inode = file_inode(file);
15859 +       au_mtx_and_read_lock(inode);
15860 +
15861 +       wpre.lsc = 0;
15862 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15863 +       err = PTR_ERR(h_file);
15864 +       if (IS_ERR(h_file))
15865 +               goto out;
15866 +
15867 +       err = vfsub_splice_from(pipe, h_file, ppos, len, flags);
15868 +       au_write_post(inode, h_file, &wpre, err);
15869 +
15870 +out:
15871 +       si_read_unlock(inode->i_sb);
15872 +       inode_unlock(inode);
15873 +       return err;
15874 +}
15875 +
15876 +static long aufs_fallocate(struct file *file, int mode, loff_t offset,
15877 +                          loff_t len)
15878 +{
15879 +       long err;
15880 +       struct au_write_pre wpre;
15881 +       struct inode *inode;
15882 +       struct file *h_file;
15883 +
15884 +       inode = file_inode(file);
15885 +       au_mtx_and_read_lock(inode);
15886 +
15887 +       wpre.lsc = 0;
15888 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15889 +       err = PTR_ERR(h_file);
15890 +       if (IS_ERR(h_file))
15891 +               goto out;
15892 +
15893 +       lockdep_off();
15894 +       err = vfs_fallocate(h_file, mode, offset, len);
15895 +       lockdep_on();
15896 +       au_write_post(inode, h_file, &wpre, /*written*/1);
15897 +
15898 +out:
15899 +       si_read_unlock(inode->i_sb);
15900 +       inode_unlock(inode);
15901 +       return err;
15902 +}
15903 +
15904 +static ssize_t aufs_copy_file_range(struct file *src, loff_t src_pos,
15905 +                                   struct file *dst, loff_t dst_pos,
15906 +                                   size_t len, unsigned int flags)
15907 +{
15908 +       ssize_t err;
15909 +       struct au_write_pre wpre;
15910 +       enum { SRC, DST };
15911 +       struct {
15912 +               struct inode *inode;
15913 +               struct file *h_file;
15914 +               struct super_block *h_sb;
15915 +       } a[2];
15916 +#define a_src  a[SRC]
15917 +#define a_dst  a[DST]
15918 +
15919 +       err = -EINVAL;
15920 +       a_src.inode = file_inode(src);
15921 +       if (unlikely(!S_ISREG(a_src.inode->i_mode)))
15922 +               goto out;
15923 +       a_dst.inode = file_inode(dst);
15924 +       if (unlikely(!S_ISREG(a_dst.inode->i_mode)))
15925 +               goto out;
15926 +
15927 +       au_mtx_and_read_lock(a_dst.inode);
15928 +       /*
15929 +        * in order to match the order in di_write_lock2_{child,parent}(),
15930 +        * use f_path.dentry for this comparison.
15931 +        */
15932 +       if (src->f_path.dentry < dst->f_path.dentry) {
15933 +               a_src.h_file = au_read_pre(src, /*keep_fi*/1, AuLsc_FI_1);
15934 +               err = PTR_ERR(a_src.h_file);
15935 +               if (IS_ERR(a_src.h_file))
15936 +                       goto out_si;
15937 +
15938 +               wpre.lsc = AuLsc_FI_2;
15939 +               a_dst.h_file = au_write_pre(dst, /*do_ready*/1, &wpre);
15940 +               err = PTR_ERR(a_dst.h_file);
15941 +               if (IS_ERR(a_dst.h_file)) {
15942 +                       au_read_post(a_src.inode, a_src.h_file);
15943 +                       goto out_si;
15944 +               }
15945 +       } else {
15946 +               wpre.lsc = AuLsc_FI_1;
15947 +               a_dst.h_file = au_write_pre(dst, /*do_ready*/1, &wpre);
15948 +               err = PTR_ERR(a_dst.h_file);
15949 +               if (IS_ERR(a_dst.h_file))
15950 +                       goto out_si;
15951 +
15952 +               a_src.h_file = au_read_pre(src, /*keep_fi*/1, AuLsc_FI_2);
15953 +               err = PTR_ERR(a_src.h_file);
15954 +               if (IS_ERR(a_src.h_file)) {
15955 +                       au_write_post(a_dst.inode, a_dst.h_file, &wpre,
15956 +                                     /*written*/0);
15957 +                       goto out_si;
15958 +               }
15959 +       }
15960 +
15961 +       err = -EXDEV;
15962 +       a_src.h_sb = file_inode(a_src.h_file)->i_sb;
15963 +       a_dst.h_sb = file_inode(a_dst.h_file)->i_sb;
15964 +       if (unlikely(a_src.h_sb != a_dst.h_sb)) {
15965 +               AuDbgFile(src);
15966 +               AuDbgFile(dst);
15967 +               goto out_file;
15968 +       }
15969 +
15970 +       err = vfsub_copy_file_range(a_src.h_file, src_pos, a_dst.h_file,
15971 +                                   dst_pos, len, flags);
15972 +
15973 +out_file:
15974 +       au_write_post(a_dst.inode, a_dst.h_file, &wpre, err);
15975 +       fi_read_unlock(src);
15976 +       au_read_post(a_src.inode, a_src.h_file);
15977 +out_si:
15978 +       si_read_unlock(a_dst.inode->i_sb);
15979 +       inode_unlock(a_dst.inode);
15980 +out:
15981 +       return err;
15982 +#undef a_src
15983 +#undef a_dst
15984 +}
15985 +
15986 +/* ---------------------------------------------------------------------- */
15987 +
15988 +/*
15989 + * The locking order around current->mmap_sem.
15990 + * - in most and regular cases
15991 + *   file I/O syscall -- aufs_read() or something
15992 + *     -- si_rwsem for read -- mmap_sem
15993 + *     (Note that [fdi]i_rwsem are released before mmap_sem).
15994 + * - in mmap case
15995 + *   mmap(2) -- mmap_sem -- aufs_mmap() -- si_rwsem for read -- [fdi]i_rwsem
15996 + * This AB-BA order is definitely bad, but is not a problem since "si_rwsem for
15997 + * read" allows multiple processes to acquire it and [fdi]i_rwsem are not held
15998 + * in file I/O. Aufs needs to stop lockdep in aufs_mmap() though.
15999 + * It means that when aufs acquires si_rwsem for write, the process should never
16000 + * acquire mmap_sem.
16001 + *
16002 + * Actually aufs_iterate() holds [fdi]i_rwsem before mmap_sem, but this is not a
16003 + * problem either since any directory is not able to be mmap-ed.
16004 + * The similar scenario is applied to aufs_readlink() too.
16005 + */
16006 +
16007 +#if 0 /* stop calling security_file_mmap() */
16008 +/* cf. linux/include/linux/mman.h: calc_vm_prot_bits() */
16009 +#define AuConv_VM_PROT(f, b)   _calc_vm_trans(f, VM_##b, PROT_##b)
16010 +
16011 +static unsigned long au_arch_prot_conv(unsigned long flags)
16012 +{
16013 +       /* currently ppc64 only */
16014 +#ifdef CONFIG_PPC64
16015 +       /* cf. linux/arch/powerpc/include/asm/mman.h */
16016 +       AuDebugOn(arch_calc_vm_prot_bits(-1) != VM_SAO);
16017 +       return AuConv_VM_PROT(flags, SAO);
16018 +#else
16019 +       AuDebugOn(arch_calc_vm_prot_bits(-1));
16020 +       return 0;
16021 +#endif
16022 +}
16023 +
16024 +static unsigned long au_prot_conv(unsigned long flags)
16025 +{
16026 +       return AuConv_VM_PROT(flags, READ)
16027 +               | AuConv_VM_PROT(flags, WRITE)
16028 +               | AuConv_VM_PROT(flags, EXEC)
16029 +               | au_arch_prot_conv(flags);
16030 +}
16031 +
16032 +/* cf. linux/include/linux/mman.h: calc_vm_flag_bits() */
16033 +#define AuConv_VM_MAP(f, b)    _calc_vm_trans(f, VM_##b, MAP_##b)
16034 +
16035 +static unsigned long au_flag_conv(unsigned long flags)
16036 +{
16037 +       return AuConv_VM_MAP(flags, GROWSDOWN)
16038 +               | AuConv_VM_MAP(flags, DENYWRITE)
16039 +               | AuConv_VM_MAP(flags, LOCKED);
16040 +}
16041 +#endif
16042 +
16043 +static int aufs_mmap(struct file *file, struct vm_area_struct *vma)
16044 +{
16045 +       int err;
16046 +       const unsigned char wlock
16047 +               = (file->f_mode & FMODE_WRITE) && (vma->vm_flags & VM_SHARED);
16048 +       struct super_block *sb;
16049 +       struct file *h_file;
16050 +       struct inode *inode;
16051 +
16052 +       AuDbgVmRegion(file, vma);
16053 +
16054 +       inode = file_inode(file);
16055 +       sb = inode->i_sb;
16056 +       lockdep_off();
16057 +       si_read_lock(sb, AuLock_NOPLMW);
16058 +
16059 +       h_file = au_write_pre(file, wlock, /*wpre*/NULL);
16060 +       lockdep_on();
16061 +       err = PTR_ERR(h_file);
16062 +       if (IS_ERR(h_file))
16063 +               goto out;
16064 +
16065 +       err = 0;
16066 +       au_set_mmapped(file);
16067 +       au_vm_file_reset(vma, h_file);
16068 +       /*
16069 +        * we cannot call security_mmap_file() here since it may acquire
16070 +        * mmap_sem or i_mutex.
16071 +        *
16072 +        * err = security_mmap_file(h_file, au_prot_conv(vma->vm_flags),
16073 +        *                       au_flag_conv(vma->vm_flags));
16074 +        */
16075 +       if (!err)
16076 +               err = call_mmap(h_file, vma);
16077 +       if (!err) {
16078 +               au_vm_prfile_set(vma, file);
16079 +               fsstack_copy_attr_atime(inode, file_inode(h_file));
16080 +               goto out_fput; /* success */
16081 +       }
16082 +       au_unset_mmapped(file);
16083 +       au_vm_file_reset(vma, file);
16084 +
16085 +out_fput:
16086 +       lockdep_off();
16087 +       ii_write_unlock(inode);
16088 +       lockdep_on();
16089 +       fput(h_file);
16090 +out:
16091 +       lockdep_off();
16092 +       si_read_unlock(sb);
16093 +       lockdep_on();
16094 +       AuTraceErr(err);
16095 +       return err;
16096 +}
16097 +
16098 +/* ---------------------------------------------------------------------- */
16099 +
16100 +static int aufs_fsync_nondir(struct file *file, loff_t start, loff_t end,
16101 +                            int datasync)
16102 +{
16103 +       int err;
16104 +       struct au_write_pre wpre;
16105 +       struct inode *inode;
16106 +       struct file *h_file;
16107 +
16108 +       err = 0; /* -EBADF; */ /* posix? */
16109 +       if (unlikely(!(file->f_mode & FMODE_WRITE)))
16110 +               goto out;
16111 +
16112 +       inode = file_inode(file);
16113 +       au_mtx_and_read_lock(inode);
16114 +
16115 +       wpre.lsc = 0;
16116 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
16117 +       err = PTR_ERR(h_file);
16118 +       if (IS_ERR(h_file))
16119 +               goto out_unlock;
16120 +
16121 +       err = vfsub_fsync(h_file, &h_file->f_path, datasync);
16122 +       au_write_post(inode, h_file, &wpre, /*written*/0);
16123 +
16124 +out_unlock:
16125 +       si_read_unlock(inode->i_sb);
16126 +       inode_unlock(inode);
16127 +out:
16128 +       return err;
16129 +}
16130 +
16131 +static int aufs_fasync(int fd, struct file *file, int flag)
16132 +{
16133 +       int err;
16134 +       struct file *h_file;
16135 +       struct super_block *sb;
16136 +
16137 +       sb = file->f_path.dentry->d_sb;
16138 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
16139 +
16140 +       h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
16141 +       err = PTR_ERR(h_file);
16142 +       if (IS_ERR(h_file))
16143 +               goto out;
16144 +
16145 +       if (h_file->f_op->fasync)
16146 +               err = h_file->f_op->fasync(fd, h_file, flag);
16147 +       fput(h_file); /* instead of au_read_post() */
16148 +
16149 +out:
16150 +       si_read_unlock(sb);
16151 +       return err;
16152 +}
16153 +
16154 +static int aufs_setfl(struct file *file, unsigned long arg)
16155 +{
16156 +       int err;
16157 +       struct file *h_file;
16158 +       struct super_block *sb;
16159 +
16160 +       sb = file->f_path.dentry->d_sb;
16161 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
16162 +
16163 +       h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
16164 +       err = PTR_ERR(h_file);
16165 +       if (IS_ERR(h_file))
16166 +               goto out;
16167 +
16168 +       /* stop calling h_file->fasync */
16169 +       arg |= vfsub_file_flags(file) & FASYNC;
16170 +       err = setfl(/*unused fd*/-1, h_file, arg);
16171 +       fput(h_file); /* instead of au_read_post() */
16172 +
16173 +out:
16174 +       si_read_unlock(sb);
16175 +       return err;
16176 +}
16177 +
16178 +/* ---------------------------------------------------------------------- */
16179 +
16180 +/* no one supports this operation, currently */
16181 +#if 0 /* reserved for future use */
16182 +static ssize_t aufs_sendpage(struct file *file, struct page *page, int offset,
16183 +                            size_t len, loff_t *pos, int more)
16184 +{
16185 +}
16186 +#endif
16187 +
16188 +/* ---------------------------------------------------------------------- */
16189 +
16190 +const struct file_operations aufs_file_fop = {
16191 +       .owner          = THIS_MODULE,
16192 +
16193 +       .llseek         = default_llseek,
16194 +
16195 +       .read_iter      = aufs_read_iter,
16196 +       .write_iter     = aufs_write_iter,
16197 +
16198 +#ifdef CONFIG_AUFS_POLL
16199 +       .poll           = aufs_poll,
16200 +#endif
16201 +       .unlocked_ioctl = aufs_ioctl_nondir,
16202 +#ifdef CONFIG_COMPAT
16203 +       .compat_ioctl   = aufs_compat_ioctl_nondir,
16204 +#endif
16205 +       .mmap           = aufs_mmap,
16206 +       .open           = aufs_open_nondir,
16207 +       .flush          = aufs_flush_nondir,
16208 +       .release        = aufs_release_nondir,
16209 +       .fsync          = aufs_fsync_nondir,
16210 +       .fasync         = aufs_fasync,
16211 +       /* .sendpage    = aufs_sendpage, */
16212 +       .setfl          = aufs_setfl,
16213 +       .splice_write   = aufs_splice_write,
16214 +       .splice_read    = aufs_splice_read,
16215 +#if 0 /* reserved for future use */
16216 +       .aio_splice_write = aufs_aio_splice_write,
16217 +       .aio_splice_read  = aufs_aio_splice_read,
16218 +#endif
16219 +       .fallocate      = aufs_fallocate,
16220 +       .copy_file_range = aufs_copy_file_range
16221 +};
16222 diff -urN /usr/share/empty/fs/aufs/fstype.h linux/fs/aufs/fstype.h
16223 --- /usr/share/empty/fs/aufs/fstype.h   1970-01-01 01:00:00.000000000 +0100
16224 +++ linux/fs/aufs/fstype.h      2021-11-01 23:48:34.206359262 +0100
16225 @@ -0,0 +1,401 @@
16226 +/* SPDX-License-Identifier: GPL-2.0 */
16227 +/*
16228 + * Copyright (C) 2005-2021 Junjiro R. Okajima
16229 + *
16230 + * This program, aufs is free software; you can redistribute it and/or modify
16231 + * it under the terms of the GNU General Public License as published by
16232 + * the Free Software Foundation; either version 2 of the License, or
16233 + * (at your option) any later version.
16234 + *
16235 + * This program is distributed in the hope that it will be useful,
16236 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
16237 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16238 + * GNU General Public License for more details.
16239 + *
16240 + * You should have received a copy of the GNU General Public License
16241 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16242 + */
16243 +
16244 +/*
16245 + * judging filesystem type
16246 + */
16247 +
16248 +#ifndef __AUFS_FSTYPE_H__
16249 +#define __AUFS_FSTYPE_H__
16250 +
16251 +#ifdef __KERNEL__
16252 +
16253 +#include <linux/fs.h>
16254 +#include <linux/magic.h>
16255 +#include <linux/nfs_fs.h>
16256 +#include <linux/romfs_fs.h>
16257 +
16258 +static inline int au_test_aufs(struct super_block *sb)
16259 +{
16260 +       return sb->s_magic == AUFS_SUPER_MAGIC;
16261 +}
16262 +
16263 +static inline const char *au_sbtype(struct super_block *sb)
16264 +{
16265 +       return sb->s_type->name;
16266 +}
16267 +
16268 +static inline int au_test_iso9660(struct super_block *sb __maybe_unused)
16269 +{
16270 +#if IS_ENABLED(CONFIG_ISO9660_FS)
16271 +       return sb->s_magic == ISOFS_SUPER_MAGIC;
16272 +#else
16273 +       return 0;
16274 +#endif
16275 +}
16276 +
16277 +static inline int au_test_romfs(struct super_block *sb __maybe_unused)
16278 +{
16279 +#if IS_ENABLED(CONFIG_ROMFS_FS)
16280 +       return sb->s_magic == ROMFS_MAGIC;
16281 +#else
16282 +       return 0;
16283 +#endif
16284 +}
16285 +
16286 +static inline int au_test_cramfs(struct super_block *sb __maybe_unused)
16287 +{
16288 +#if IS_ENABLED(CONFIG_CRAMFS)
16289 +       return sb->s_magic == CRAMFS_MAGIC;
16290 +#endif
16291 +       return 0;
16292 +}
16293 +
16294 +static inline int au_test_nfs(struct super_block *sb __maybe_unused)
16295 +{
16296 +#if IS_ENABLED(CONFIG_NFS_FS)
16297 +       return sb->s_magic == NFS_SUPER_MAGIC;
16298 +#else
16299 +       return 0;
16300 +#endif
16301 +}
16302 +
16303 +static inline int au_test_fuse(struct super_block *sb __maybe_unused)
16304 +{
16305 +#if IS_ENABLED(CONFIG_FUSE_FS)
16306 +       return sb->s_magic == FUSE_SUPER_MAGIC;
16307 +#else
16308 +       return 0;
16309 +#endif
16310 +}
16311 +
16312 +static inline int au_test_xfs(struct super_block *sb __maybe_unused)
16313 +{
16314 +#if IS_ENABLED(CONFIG_XFS_FS)
16315 +       return sb->s_magic == XFS_SB_MAGIC;
16316 +#else
16317 +       return 0;
16318 +#endif
16319 +}
16320 +
16321 +static inline int au_test_tmpfs(struct super_block *sb __maybe_unused)
16322 +{
16323 +#ifdef CONFIG_TMPFS
16324 +       return sb->s_magic == TMPFS_MAGIC;
16325 +#else
16326 +       return 0;
16327 +#endif
16328 +}
16329 +
16330 +static inline int au_test_ecryptfs(struct super_block *sb __maybe_unused)
16331 +{
16332 +#if IS_ENABLED(CONFIG_ECRYPT_FS)
16333 +       return !strcmp(au_sbtype(sb), "ecryptfs");
16334 +#else
16335 +       return 0;
16336 +#endif
16337 +}
16338 +
16339 +static inline int au_test_ramfs(struct super_block *sb)
16340 +{
16341 +       return sb->s_magic == RAMFS_MAGIC;
16342 +}
16343 +
16344 +static inline int au_test_ubifs(struct super_block *sb __maybe_unused)
16345 +{
16346 +#if IS_ENABLED(CONFIG_UBIFS_FS)
16347 +       return sb->s_magic == UBIFS_SUPER_MAGIC;
16348 +#else
16349 +       return 0;
16350 +#endif
16351 +}
16352 +
16353 +static inline int au_test_procfs(struct super_block *sb __maybe_unused)
16354 +{
16355 +#ifdef CONFIG_PROC_FS
16356 +       return sb->s_magic == PROC_SUPER_MAGIC;
16357 +#else
16358 +       return 0;
16359 +#endif
16360 +}
16361 +
16362 +static inline int au_test_sysfs(struct super_block *sb __maybe_unused)
16363 +{
16364 +#ifdef CONFIG_SYSFS
16365 +       return sb->s_magic == SYSFS_MAGIC;
16366 +#else
16367 +       return 0;
16368 +#endif
16369 +}
16370 +
16371 +static inline int au_test_configfs(struct super_block *sb __maybe_unused)
16372 +{
16373 +#if IS_ENABLED(CONFIG_CONFIGFS_FS)
16374 +       return sb->s_magic == CONFIGFS_MAGIC;
16375 +#else
16376 +       return 0;
16377 +#endif
16378 +}
16379 +
16380 +static inline int au_test_minix(struct super_block *sb __maybe_unused)
16381 +{
16382 +#if IS_ENABLED(CONFIG_MINIX_FS)
16383 +       return sb->s_magic == MINIX3_SUPER_MAGIC
16384 +               || sb->s_magic == MINIX2_SUPER_MAGIC
16385 +               || sb->s_magic == MINIX2_SUPER_MAGIC2
16386 +               || sb->s_magic == MINIX_SUPER_MAGIC
16387 +               || sb->s_magic == MINIX_SUPER_MAGIC2;
16388 +#else
16389 +       return 0;
16390 +#endif
16391 +}
16392 +
16393 +static inline int au_test_fat(struct super_block *sb __maybe_unused)
16394 +{
16395 +#if IS_ENABLED(CONFIG_FAT_FS)
16396 +       return sb->s_magic == MSDOS_SUPER_MAGIC;
16397 +#else
16398 +       return 0;
16399 +#endif
16400 +}
16401 +
16402 +static inline int au_test_msdos(struct super_block *sb)
16403 +{
16404 +       return au_test_fat(sb);
16405 +}
16406 +
16407 +static inline int au_test_vfat(struct super_block *sb)
16408 +{
16409 +       return au_test_fat(sb);
16410 +}
16411 +
16412 +static inline int au_test_securityfs(struct super_block *sb __maybe_unused)
16413 +{
16414 +#ifdef CONFIG_SECURITYFS
16415 +       return sb->s_magic == SECURITYFS_MAGIC;
16416 +#else
16417 +       return 0;
16418 +#endif
16419 +}
16420 +
16421 +static inline int au_test_squashfs(struct super_block *sb __maybe_unused)
16422 +{
16423 +#if IS_ENABLED(CONFIG_SQUASHFS)
16424 +       return sb->s_magic == SQUASHFS_MAGIC;
16425 +#else
16426 +       return 0;
16427 +#endif
16428 +}
16429 +
16430 +static inline int au_test_btrfs(struct super_block *sb __maybe_unused)
16431 +{
16432 +#if IS_ENABLED(CONFIG_BTRFS_FS)
16433 +       return sb->s_magic == BTRFS_SUPER_MAGIC;
16434 +#else
16435 +       return 0;
16436 +#endif
16437 +}
16438 +
16439 +static inline int au_test_xenfs(struct super_block *sb __maybe_unused)
16440 +{
16441 +#if IS_ENABLED(CONFIG_XENFS)
16442 +       return sb->s_magic == XENFS_SUPER_MAGIC;
16443 +#else
16444 +       return 0;
16445 +#endif
16446 +}
16447 +
16448 +static inline int au_test_debugfs(struct super_block *sb __maybe_unused)
16449 +{
16450 +#ifdef CONFIG_DEBUG_FS
16451 +       return sb->s_magic == DEBUGFS_MAGIC;
16452 +#else
16453 +       return 0;
16454 +#endif
16455 +}
16456 +
16457 +static inline int au_test_nilfs(struct super_block *sb __maybe_unused)
16458 +{
16459 +#if IS_ENABLED(CONFIG_NILFS)
16460 +       return sb->s_magic == NILFS_SUPER_MAGIC;
16461 +#else
16462 +       return 0;
16463 +#endif
16464 +}
16465 +
16466 +static inline int au_test_hfsplus(struct super_block *sb __maybe_unused)
16467 +{
16468 +#if IS_ENABLED(CONFIG_HFSPLUS_FS)
16469 +       return sb->s_magic == HFSPLUS_SUPER_MAGIC;
16470 +#else
16471 +       return 0;
16472 +#endif
16473 +}
16474 +
16475 +/* ---------------------------------------------------------------------- */
16476 +/*
16477 + * they can't be an aufs branch.
16478 + */
16479 +static inline int au_test_fs_unsuppoted(struct super_block *sb)
16480 +{
16481 +       return
16482 +#ifndef CONFIG_AUFS_BR_RAMFS
16483 +               au_test_ramfs(sb) ||
16484 +#endif
16485 +               au_test_procfs(sb)
16486 +               || au_test_sysfs(sb)
16487 +               || au_test_configfs(sb)
16488 +               || au_test_debugfs(sb)
16489 +               || au_test_securityfs(sb)
16490 +               || au_test_xenfs(sb)
16491 +               || au_test_ecryptfs(sb)
16492 +               /* || !strcmp(au_sbtype(sb), "unionfs") */
16493 +               || au_test_aufs(sb); /* will be supported in next version */
16494 +}
16495 +
16496 +static inline int au_test_fs_remote(struct super_block *sb)
16497 +{
16498 +       return !au_test_tmpfs(sb)
16499 +#ifdef CONFIG_AUFS_BR_RAMFS
16500 +               && !au_test_ramfs(sb)
16501 +#endif
16502 +               && !(sb->s_type->fs_flags & FS_REQUIRES_DEV);
16503 +}
16504 +
16505 +/* ---------------------------------------------------------------------- */
16506 +
16507 +/*
16508 + * Note: these functions (below) are created after reading ->getattr() in all
16509 + * filesystems under linux/fs. it means we have to do so in every update...
16510 + */
16511 +
16512 +/*
16513 + * some filesystems require getattr to refresh the inode attributes before
16514 + * referencing.
16515 + * in most cases, we can rely on the inode attribute in NFS (or every remote fs)
16516 + * and leave the work for d_revalidate()
16517 + */
16518 +static inline int au_test_fs_refresh_iattr(struct super_block *sb)
16519 +{
16520 +       return au_test_nfs(sb)
16521 +               || au_test_fuse(sb)
16522 +               /* || au_test_btrfs(sb) */      /* untested */
16523 +               ;
16524 +}
16525 +
16526 +/*
16527 + * filesystems which don't maintain i_size or i_blocks.
16528 + */
16529 +static inline int au_test_fs_bad_iattr_size(struct super_block *sb)
16530 +{
16531 +       return au_test_xfs(sb)
16532 +               || au_test_btrfs(sb)
16533 +               || au_test_ubifs(sb)
16534 +               || au_test_hfsplus(sb)  /* maintained, but incorrect */
16535 +               /* || au_test_minix(sb) */      /* untested */
16536 +               ;
16537 +}
16538 +
16539 +/*
16540 + * filesystems which don't store the correct value in some of their inode
16541 + * attributes.
16542 + */
16543 +static inline int au_test_fs_bad_iattr(struct super_block *sb)
16544 +{
16545 +       return au_test_fs_bad_iattr_size(sb)
16546 +               || au_test_fat(sb)
16547 +               || au_test_msdos(sb)
16548 +               || au_test_vfat(sb);
16549 +}
16550 +
16551 +/* they don't check i_nlink in link(2) */
16552 +static inline int au_test_fs_no_limit_nlink(struct super_block *sb)
16553 +{
16554 +       return au_test_tmpfs(sb)
16555 +#ifdef CONFIG_AUFS_BR_RAMFS
16556 +               || au_test_ramfs(sb)
16557 +#endif
16558 +               || au_test_ubifs(sb)
16559 +               || au_test_hfsplus(sb);
16560 +}
16561 +
16562 +/*
16563 + * filesystems which sets S_NOATIME and S_NOCMTIME.
16564 + */
16565 +static inline int au_test_fs_notime(struct super_block *sb)
16566 +{
16567 +       return au_test_nfs(sb)
16568 +               || au_test_fuse(sb)
16569 +               || au_test_ubifs(sb)
16570 +               ;
16571 +}
16572 +
16573 +/* temporary support for i#1 in cramfs */
16574 +static inline int au_test_fs_unique_ino(struct inode *inode)
16575 +{
16576 +       if (au_test_cramfs(inode->i_sb))
16577 +               return inode->i_ino != 1;
16578 +       return 1;
16579 +}
16580 +
16581 +/* ---------------------------------------------------------------------- */
16582 +
16583 +/*
16584 + * the filesystem where the xino files placed must support i/o after unlink and
16585 + * maintain i_size and i_blocks.
16586 + */
16587 +static inline int au_test_fs_bad_xino(struct super_block *sb)
16588 +{
16589 +       return au_test_fs_remote(sb)
16590 +               || au_test_fs_bad_iattr_size(sb)
16591 +               /* don't want unnecessary work for xino */
16592 +               || au_test_aufs(sb)
16593 +               || au_test_ecryptfs(sb)
16594 +               || au_test_nilfs(sb);
16595 +}
16596 +
16597 +static inline int au_test_fs_trunc_xino(struct super_block *sb)
16598 +{
16599 +       return au_test_tmpfs(sb)
16600 +               || au_test_ramfs(sb);
16601 +}
16602 +
16603 +/*
16604 + * test if the @sb is real-readonly.
16605 + */
16606 +static inline int au_test_fs_rr(struct super_block *sb)
16607 +{
16608 +       return au_test_squashfs(sb)
16609 +               || au_test_iso9660(sb)
16610 +               || au_test_cramfs(sb)
16611 +               || au_test_romfs(sb);
16612 +}
16613 +
16614 +/*
16615 + * test if the @inode is nfs with 'noacl' option
16616 + * NFS always sets SB_POSIXACL regardless its mount option 'noacl.'
16617 + */
16618 +static inline int au_test_nfs_noacl(struct inode *inode)
16619 +{
16620 +       return au_test_nfs(inode->i_sb)
16621 +               /* && IS_POSIXACL(inode) */
16622 +               && !nfs_server_capable(inode, NFS_CAP_ACLS);
16623 +}
16624 +
16625 +#endif /* __KERNEL__ */
16626 +#endif /* __AUFS_FSTYPE_H__ */
16627 diff -urN /usr/share/empty/fs/aufs/hbl.h linux/fs/aufs/hbl.h
16628 --- /usr/share/empty/fs/aufs/hbl.h      1970-01-01 01:00:00.000000000 +0100
16629 +++ linux/fs/aufs/hbl.h 2021-11-01 23:48:34.206359262 +0100
16630 @@ -0,0 +1,65 @@
16631 +/* SPDX-License-Identifier: GPL-2.0 */
16632 +/*
16633 + * Copyright (C) 2017-2021 Junjiro R. Okajima
16634 + *
16635 + * This program, aufs is free software; you can redistribute it and/or modify
16636 + * it under the terms of the GNU General Public License as published by
16637 + * the Free Software Foundation; either version 2 of the License, or
16638 + * (at your option) any later version.
16639 + *
16640 + * This program is distributed in the hope that it will be useful,
16641 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
16642 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16643 + * GNU General Public License for more details.
16644 + *
16645 + * You should have received a copy of the GNU General Public License
16646 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16647 + */
16648 +
16649 +/*
16650 + * helpers for hlist_bl.h
16651 + */
16652 +
16653 +#ifndef __AUFS_HBL_H__
16654 +#define __AUFS_HBL_H__
16655 +
16656 +#ifdef __KERNEL__
16657 +
16658 +#include <linux/list_bl.h>
16659 +
16660 +static inline void au_hbl_add(struct hlist_bl_node *node,
16661 +                             struct hlist_bl_head *hbl)
16662 +{
16663 +       hlist_bl_lock(hbl);
16664 +       hlist_bl_add_head(node, hbl);
16665 +       hlist_bl_unlock(hbl);
16666 +}
16667 +
16668 +static inline void au_hbl_del(struct hlist_bl_node *node,
16669 +                             struct hlist_bl_head *hbl)
16670 +{
16671 +       hlist_bl_lock(hbl);
16672 +       hlist_bl_del(node);
16673 +       hlist_bl_unlock(hbl);
16674 +}
16675 +
16676 +#define au_hbl_for_each(pos, head)                                     \
16677 +       for (pos = hlist_bl_first(head);                                \
16678 +            pos;                                                       \
16679 +            pos = pos->next)
16680 +
16681 +static inline unsigned long au_hbl_count(struct hlist_bl_head *hbl)
16682 +{
16683 +       unsigned long cnt;
16684 +       struct hlist_bl_node *pos;
16685 +
16686 +       cnt = 0;
16687 +       hlist_bl_lock(hbl);
16688 +       au_hbl_for_each(pos, hbl)
16689 +               cnt++;
16690 +       hlist_bl_unlock(hbl);
16691 +       return cnt;
16692 +}
16693 +
16694 +#endif /* __KERNEL__ */
16695 +#endif /* __AUFS_HBL_H__ */
16696 diff -urN /usr/share/empty/fs/aufs/hfsnotify.c linux/fs/aufs/hfsnotify.c
16697 --- /usr/share/empty/fs/aufs/hfsnotify.c        1970-01-01 01:00:00.000000000 +0100
16698 +++ linux/fs/aufs/hfsnotify.c   2021-11-01 23:48:34.206359262 +0100
16699 @@ -0,0 +1,288 @@
16700 +// SPDX-License-Identifier: GPL-2.0
16701 +/*
16702 + * Copyright (C) 2005-2021 Junjiro R. Okajima
16703 + *
16704 + * This program, aufs is free software; you can redistribute it and/or modify
16705 + * it under the terms of the GNU General Public License as published by
16706 + * the Free Software Foundation; either version 2 of the License, or
16707 + * (at your option) any later version.
16708 + *
16709 + * This program is distributed in the hope that it will be useful,
16710 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
16711 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16712 + * GNU General Public License for more details.
16713 + *
16714 + * You should have received a copy of the GNU General Public License
16715 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16716 + */
16717 +
16718 +/*
16719 + * fsnotify for the lower directories
16720 + */
16721 +
16722 +#include "aufs.h"
16723 +
16724 +/* FS_IN_IGNORED is unnecessary */
16725 +static const __u32 AuHfsnMask = (FS_MOVED_TO | FS_MOVED_FROM | FS_DELETE
16726 +                                | FS_CREATE | FS_EVENT_ON_CHILD);
16727 +static DECLARE_WAIT_QUEUE_HEAD(au_hfsn_wq);
16728 +static __cacheline_aligned_in_smp atomic64_t au_hfsn_ifree = ATOMIC64_INIT(0);
16729 +
16730 +static void au_hfsn_free_mark(struct fsnotify_mark *mark)
16731 +{
16732 +       struct au_hnotify *hn = container_of(mark, struct au_hnotify,
16733 +                                            hn_mark);
16734 +       /* AuDbg("here\n"); */
16735 +       au_cache_free_hnotify(hn);
16736 +       smp_mb__before_atomic(); /* for atomic64_dec */
16737 +       if (atomic64_dec_and_test(&au_hfsn_ifree))
16738 +               wake_up(&au_hfsn_wq);
16739 +}
16740 +
16741 +static int au_hfsn_alloc(struct au_hinode *hinode)
16742 +{
16743 +       int err;
16744 +       struct au_hnotify *hn;
16745 +       struct super_block *sb;
16746 +       struct au_branch *br;
16747 +       struct fsnotify_mark *mark;
16748 +       aufs_bindex_t bindex;
16749 +
16750 +       hn = hinode->hi_notify;
16751 +       sb = hn->hn_aufs_inode->i_sb;
16752 +       bindex = au_br_index(sb, hinode->hi_id);
16753 +       br = au_sbr(sb, bindex);
16754 +       AuDebugOn(!br->br_hfsn);
16755 +
16756 +       mark = &hn->hn_mark;
16757 +       fsnotify_init_mark(mark, br->br_hfsn->hfsn_group);
16758 +       mark->mask = AuHfsnMask;
16759 +       /*
16760 +        * by udba rename or rmdir, aufs assign a new inode to the known
16761 +        * h_inode, so specify 1 to allow dups.
16762 +        */
16763 +       lockdep_off();
16764 +       err = fsnotify_add_inode_mark(mark, hinode->hi_inode, /*allow_dups*/1);
16765 +       lockdep_on();
16766 +
16767 +       return err;
16768 +}
16769 +
16770 +static int au_hfsn_free(struct au_hinode *hinode, struct au_hnotify *hn)
16771 +{
16772 +       struct fsnotify_mark *mark;
16773 +       unsigned long long ull;
16774 +       struct fsnotify_group *group;
16775 +
16776 +       ull = atomic64_inc_return(&au_hfsn_ifree);
16777 +       BUG_ON(!ull);
16778 +
16779 +       mark = &hn->hn_mark;
16780 +       spin_lock(&mark->lock);
16781 +       group = mark->group;
16782 +       fsnotify_get_group(group);
16783 +       spin_unlock(&mark->lock);
16784 +       lockdep_off();
16785 +       fsnotify_destroy_mark(mark, group);
16786 +       fsnotify_put_mark(mark);
16787 +       fsnotify_put_group(group);
16788 +       lockdep_on();
16789 +
16790 +       /* free hn by myself */
16791 +       return 0;
16792 +}
16793 +
16794 +/* ---------------------------------------------------------------------- */
16795 +
16796 +static void au_hfsn_ctl(struct au_hinode *hinode, int do_set)
16797 +{
16798 +       struct fsnotify_mark *mark;
16799 +
16800 +       mark = &hinode->hi_notify->hn_mark;
16801 +       spin_lock(&mark->lock);
16802 +       if (do_set) {
16803 +               AuDebugOn(mark->mask & AuHfsnMask);
16804 +               mark->mask |= AuHfsnMask;
16805 +       } else {
16806 +               AuDebugOn(!(mark->mask & AuHfsnMask));
16807 +               mark->mask &= ~AuHfsnMask;
16808 +       }
16809 +       spin_unlock(&mark->lock);
16810 +       /* fsnotify_recalc_inode_mask(hinode->hi_inode); */
16811 +}
16812 +
16813 +/* ---------------------------------------------------------------------- */
16814 +
16815 +/* #define AuDbgHnotify */
16816 +#ifdef AuDbgHnotify
16817 +static char *au_hfsn_name(u32 mask)
16818 +{
16819 +#ifdef CONFIG_AUFS_DEBUG
16820 +#define test_ret(flag)                         \
16821 +       do {                                    \
16822 +               if (mask & flag)                \
16823 +                       return #flag;           \
16824 +       } while (0)
16825 +       test_ret(FS_ACCESS);
16826 +       test_ret(FS_MODIFY);
16827 +       test_ret(FS_ATTRIB);
16828 +       test_ret(FS_CLOSE_WRITE);
16829 +       test_ret(FS_CLOSE_NOWRITE);
16830 +       test_ret(FS_OPEN);
16831 +       test_ret(FS_MOVED_FROM);
16832 +       test_ret(FS_MOVED_TO);
16833 +       test_ret(FS_CREATE);
16834 +       test_ret(FS_DELETE);
16835 +       test_ret(FS_DELETE_SELF);
16836 +       test_ret(FS_MOVE_SELF);
16837 +       test_ret(FS_UNMOUNT);
16838 +       test_ret(FS_Q_OVERFLOW);
16839 +       test_ret(FS_IN_IGNORED);
16840 +       test_ret(FS_ISDIR);
16841 +       test_ret(FS_IN_ONESHOT);
16842 +       test_ret(FS_EVENT_ON_CHILD);
16843 +       return "";
16844 +#undef test_ret
16845 +#else
16846 +       return "??";
16847 +#endif
16848 +}
16849 +#endif
16850 +
16851 +/* ---------------------------------------------------------------------- */
16852 +
16853 +static void au_hfsn_free_group(struct fsnotify_group *group)
16854 +{
16855 +       struct au_br_hfsnotify *hfsn = group->private;
16856 +
16857 +       /* AuDbg("here\n"); */
16858 +       au_kfree_try_rcu(hfsn);
16859 +}
16860 +
16861 +static int au_hfsn_handle_event(struct fsnotify_group *group,
16862 +                               u32 mask, const void *data, int data_type,
16863 +                               struct inode *dir,
16864 +                               const struct qstr *file_name, u32 cookie,
16865 +                               struct fsnotify_iter_info *iter_info)
16866 +{
16867 +       int err;
16868 +       struct au_hnotify *hnotify;
16869 +       struct inode *h_dir, *h_inode;
16870 +       struct fsnotify_mark *inode_mark;
16871 +
16872 +       AuDebugOn(data_type != FSNOTIFY_EVENT_INODE);
16873 +
16874 +       err = 0;
16875 +       /* if FS_UNMOUNT happens, there must be another bug */
16876 +       AuDebugOn(mask & FS_UNMOUNT);
16877 +       if (mask & (FS_IN_IGNORED | FS_UNMOUNT))
16878 +               goto out;
16879 +
16880 +       h_dir = dir;
16881 +       h_inode = NULL;
16882 +#ifdef AuDbgHnotify
16883 +       au_debug_on();
16884 +       if (1 || h_child_qstr.len != sizeof(AUFS_XINO_FNAME) - 1
16885 +           || strncmp(h_child_qstr.name, AUFS_XINO_FNAME, h_child_qstr.len)) {
16886 +               AuDbg("i%lu, mask 0x%x %s, hcname %.*s, hi%lu\n",
16887 +                     h_dir->i_ino, mask, au_hfsn_name(mask),
16888 +                     AuLNPair(&h_child_qstr), h_inode ? h_inode->i_ino : 0);
16889 +               /* WARN_ON(1); */
16890 +       }
16891 +       au_debug_off();
16892 +#endif
16893 +
16894 +       inode_mark = fsnotify_iter_inode_mark(iter_info);
16895 +       AuDebugOn(!inode_mark);
16896 +       hnotify = container_of(inode_mark, struct au_hnotify, hn_mark);
16897 +       err = au_hnotify(h_dir, hnotify, mask, file_name, h_inode);
16898 +
16899 +out:
16900 +       return err;
16901 +}
16902 +
16903 +static struct fsnotify_ops au_hfsn_ops = {
16904 +       .handle_event           = au_hfsn_handle_event,
16905 +       .free_group_priv        = au_hfsn_free_group,
16906 +       .free_mark              = au_hfsn_free_mark
16907 +};
16908 +
16909 +/* ---------------------------------------------------------------------- */
16910 +
16911 +static void au_hfsn_fin_br(struct au_branch *br)
16912 +{
16913 +       struct au_br_hfsnotify *hfsn;
16914 +
16915 +       hfsn = br->br_hfsn;
16916 +       if (hfsn) {
16917 +               lockdep_off();
16918 +               fsnotify_put_group(hfsn->hfsn_group);
16919 +               lockdep_on();
16920 +       }
16921 +}
16922 +
16923 +static int au_hfsn_init_br(struct au_branch *br, int perm)
16924 +{
16925 +       int err;
16926 +       struct fsnotify_group *group;
16927 +       struct au_br_hfsnotify *hfsn;
16928 +
16929 +       err = 0;
16930 +       br->br_hfsn = NULL;
16931 +       if (!au_br_hnotifyable(perm))
16932 +               goto out;
16933 +
16934 +       err = -ENOMEM;
16935 +       hfsn = kmalloc(sizeof(*hfsn), GFP_NOFS);
16936 +       if (unlikely(!hfsn))
16937 +               goto out;
16938 +
16939 +       err = 0;
16940 +       group = fsnotify_alloc_group(&au_hfsn_ops);
16941 +       if (IS_ERR(group)) {
16942 +               err = PTR_ERR(group);
16943 +               pr_err("fsnotify_alloc_group() failed, %d\n", err);
16944 +               goto out_hfsn;
16945 +       }
16946 +
16947 +       group->private = hfsn;
16948 +       hfsn->hfsn_group = group;
16949 +       br->br_hfsn = hfsn;
16950 +       goto out; /* success */
16951 +
16952 +out_hfsn:
16953 +       au_kfree_try_rcu(hfsn);
16954 +out:
16955 +       return err;
16956 +}
16957 +
16958 +static int au_hfsn_reset_br(unsigned int udba, struct au_branch *br, int perm)
16959 +{
16960 +       int err;
16961 +
16962 +       err = 0;
16963 +       if (!br->br_hfsn)
16964 +               err = au_hfsn_init_br(br, perm);
16965 +
16966 +       return err;
16967 +}
16968 +
16969 +/* ---------------------------------------------------------------------- */
16970 +
16971 +static void au_hfsn_fin(void)
16972 +{
16973 +       AuDbg("au_hfsn_ifree %lld\n", (long long)atomic64_read(&au_hfsn_ifree));
16974 +       wait_event(au_hfsn_wq, !atomic64_read(&au_hfsn_ifree));
16975 +}
16976 +
16977 +const struct au_hnotify_op au_hnotify_op = {
16978 +       .ctl            = au_hfsn_ctl,
16979 +       .alloc          = au_hfsn_alloc,
16980 +       .free           = au_hfsn_free,
16981 +
16982 +       .fin            = au_hfsn_fin,
16983 +
16984 +       .reset_br       = au_hfsn_reset_br,
16985 +       .fin_br         = au_hfsn_fin_br,
16986 +       .init_br        = au_hfsn_init_br
16987 +};
16988 diff -urN /usr/share/empty/fs/aufs/hfsplus.c linux/fs/aufs/hfsplus.c
16989 --- /usr/share/empty/fs/aufs/hfsplus.c  1970-01-01 01:00:00.000000000 +0100
16990 +++ linux/fs/aufs/hfsplus.c     2021-11-01 23:48:34.206359262 +0100
16991 @@ -0,0 +1,60 @@
16992 +// SPDX-License-Identifier: GPL-2.0
16993 +/*
16994 + * Copyright (C) 2010-2021 Junjiro R. Okajima
16995 + *
16996 + * This program, aufs is free software; you can redistribute it and/or modify
16997 + * it under the terms of the GNU General Public License as published by
16998 + * the Free Software Foundation; either version 2 of the License, or
16999 + * (at your option) any later version.
17000 + *
17001 + * This program is distributed in the hope that it will be useful,
17002 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
17003 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17004 + * GNU General Public License for more details.
17005 + *
17006 + * You should have received a copy of the GNU General Public License
17007 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17008 + */
17009 +
17010 +/*
17011 + * special support for filesystems which acquires an inode mutex
17012 + * at final closing a file, eg, hfsplus.
17013 + *
17014 + * This trick is very simple and stupid, just to open the file before really
17015 + * necessary open to tell hfsplus that this is not the final closing.
17016 + * The caller should call au_h_open_pre() after acquiring the inode mutex,
17017 + * and au_h_open_post() after releasing it.
17018 + */
17019 +
17020 +#include "aufs.h"
17021 +
17022 +struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
17023 +                          int force_wr)
17024 +{
17025 +       struct file *h_file;
17026 +       struct dentry *h_dentry;
17027 +
17028 +       h_dentry = au_h_dptr(dentry, bindex);
17029 +       AuDebugOn(!h_dentry);
17030 +       AuDebugOn(d_is_negative(h_dentry));
17031 +
17032 +       h_file = NULL;
17033 +       if (au_test_hfsplus(h_dentry->d_sb)
17034 +           && d_is_reg(h_dentry))
17035 +               h_file = au_h_open(dentry, bindex,
17036 +                                  O_RDONLY | O_NOATIME | O_LARGEFILE,
17037 +                                  /*file*/NULL, force_wr);
17038 +       return h_file;
17039 +}
17040 +
17041 +void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
17042 +                   struct file *h_file)
17043 +{
17044 +       struct au_branch *br;
17045 +
17046 +       if (h_file) {
17047 +               fput(h_file);
17048 +               br = au_sbr(dentry->d_sb, bindex);
17049 +               au_lcnt_dec(&br->br_nfiles);
17050 +       }
17051 +}
17052 diff -urN /usr/share/empty/fs/aufs/hnotify.c linux/fs/aufs/hnotify.c
17053 --- /usr/share/empty/fs/aufs/hnotify.c  1970-01-01 01:00:00.000000000 +0100
17054 +++ linux/fs/aufs/hnotify.c     2021-11-01 23:48:34.209692595 +0100
17055 @@ -0,0 +1,715 @@
17056 +// SPDX-License-Identifier: GPL-2.0
17057 +/*
17058 + * Copyright (C) 2005-2021 Junjiro R. Okajima
17059 + *
17060 + * This program, aufs is free software; you can redistribute it and/or modify
17061 + * it under the terms of the GNU General Public License as published by
17062 + * the Free Software Foundation; either version 2 of the License, or
17063 + * (at your option) any later version.
17064 + *
17065 + * This program is distributed in the hope that it will be useful,
17066 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
17067 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17068 + * GNU General Public License for more details.
17069 + *
17070 + * You should have received a copy of the GNU General Public License
17071 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17072 + */
17073 +
17074 +/*
17075 + * abstraction to notify the direct changes on lower directories
17076 + */
17077 +
17078 +/* #include <linux/iversion.h> */
17079 +#include "aufs.h"
17080 +
17081 +int au_hn_alloc(struct au_hinode *hinode, struct inode *inode)
17082 +{
17083 +       int err;
17084 +       struct au_hnotify *hn;
17085 +
17086 +       err = -ENOMEM;
17087 +       hn = au_cache_alloc_hnotify();
17088 +       if (hn) {
17089 +               hn->hn_aufs_inode = inode;
17090 +               hinode->hi_notify = hn;
17091 +               err = au_hnotify_op.alloc(hinode);
17092 +               AuTraceErr(err);
17093 +               if (unlikely(err)) {
17094 +                       hinode->hi_notify = NULL;
17095 +                       au_cache_free_hnotify(hn);
17096 +                       /*
17097 +                        * The upper dir was removed by udba, but the same named
17098 +                        * dir left. In this case, aufs assigns a new inode
17099 +                        * number and set the monitor again.
17100 +                        * For the lower dir, the old monitor is still left.
17101 +                        */
17102 +                       if (err == -EEXIST)
17103 +                               err = 0;
17104 +               }
17105 +       }
17106 +
17107 +       AuTraceErr(err);
17108 +       return err;
17109 +}
17110 +
17111 +void au_hn_free(struct au_hinode *hinode)
17112 +{
17113 +       struct au_hnotify *hn;
17114 +
17115 +       hn = hinode->hi_notify;
17116 +       if (hn) {
17117 +               hinode->hi_notify = NULL;
17118 +               if (au_hnotify_op.free(hinode, hn))
17119 +                       au_cache_free_hnotify(hn);
17120 +       }
17121 +}
17122 +
17123 +/* ---------------------------------------------------------------------- */
17124 +
17125 +void au_hn_ctl(struct au_hinode *hinode, int do_set)
17126 +{
17127 +       if (hinode->hi_notify)
17128 +               au_hnotify_op.ctl(hinode, do_set);
17129 +}
17130 +
17131 +void au_hn_reset(struct inode *inode, unsigned int flags)
17132 +{
17133 +       aufs_bindex_t bindex, bbot;
17134 +       struct inode *hi;
17135 +       struct dentry *iwhdentry;
17136 +
17137 +       bbot = au_ibbot(inode);
17138 +       for (bindex = au_ibtop(inode); bindex <= bbot; bindex++) {
17139 +               hi = au_h_iptr(inode, bindex);
17140 +               if (!hi)
17141 +                       continue;
17142 +
17143 +               /* inode_lock_nested(hi, AuLsc_I_CHILD); */
17144 +               iwhdentry = au_hi_wh(inode, bindex);
17145 +               if (iwhdentry)
17146 +                       dget(iwhdentry);
17147 +               au_igrab(hi);
17148 +               au_set_h_iptr(inode, bindex, NULL, 0);
17149 +               au_set_h_iptr(inode, bindex, au_igrab(hi),
17150 +                             flags & ~AuHi_XINO);
17151 +               iput(hi);
17152 +               dput(iwhdentry);
17153 +               /* inode_unlock(hi); */
17154 +       }
17155 +}
17156 +
17157 +/* ---------------------------------------------------------------------- */
17158 +
17159 +static int hn_xino(struct inode *inode, struct inode *h_inode)
17160 +{
17161 +       int err;
17162 +       aufs_bindex_t bindex, bbot, bfound, btop;
17163 +       struct inode *h_i;
17164 +
17165 +       err = 0;
17166 +       if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
17167 +               pr_warn("branch root dir was changed\n");
17168 +               goto out;
17169 +       }
17170 +
17171 +       bfound = -1;
17172 +       bbot = au_ibbot(inode);
17173 +       btop = au_ibtop(inode);
17174 +#if 0 /* reserved for future use */
17175 +       if (bindex == bbot) {
17176 +               /* keep this ino in rename case */
17177 +               goto out;
17178 +       }
17179 +#endif
17180 +       for (bindex = btop; bindex <= bbot; bindex++)
17181 +               if (au_h_iptr(inode, bindex) == h_inode) {
17182 +                       bfound = bindex;
17183 +                       break;
17184 +               }
17185 +       if (bfound < 0)
17186 +               goto out;
17187 +
17188 +       for (bindex = btop; bindex <= bbot; bindex++) {
17189 +               h_i = au_h_iptr(inode, bindex);
17190 +               if (!h_i)
17191 +                       continue;
17192 +
17193 +               err = au_xino_write(inode->i_sb, bindex, h_i->i_ino, /*ino*/0);
17194 +               /* ignore this error */
17195 +               /* bad action? */
17196 +       }
17197 +
17198 +       /* children inode number will be broken */
17199 +
17200 +out:
17201 +       AuTraceErr(err);
17202 +       return err;
17203 +}
17204 +
17205 +static int hn_gen_tree(struct dentry *dentry)
17206 +{
17207 +       int err, i, j, ndentry;
17208 +       struct au_dcsub_pages dpages;
17209 +       struct au_dpage *dpage;
17210 +       struct dentry **dentries;
17211 +
17212 +       err = au_dpages_init(&dpages, GFP_NOFS);
17213 +       if (unlikely(err))
17214 +               goto out;
17215 +       err = au_dcsub_pages(&dpages, dentry, NULL, NULL);
17216 +       if (unlikely(err))
17217 +               goto out_dpages;
17218 +
17219 +       for (i = 0; i < dpages.ndpage; i++) {
17220 +               dpage = dpages.dpages + i;
17221 +               dentries = dpage->dentries;
17222 +               ndentry = dpage->ndentry;
17223 +               for (j = 0; j < ndentry; j++) {
17224 +                       struct dentry *d;
17225 +
17226 +                       d = dentries[j];
17227 +                       if (IS_ROOT(d))
17228 +                               continue;
17229 +
17230 +                       au_digen_dec(d);
17231 +                       if (d_really_is_positive(d))
17232 +                               /* todo: reset children xino?
17233 +                                  cached children only? */
17234 +                               au_iigen_dec(d_inode(d));
17235 +               }
17236 +       }
17237 +
17238 +out_dpages:
17239 +       au_dpages_free(&dpages);
17240 +out:
17241 +       return err;
17242 +}
17243 +
17244 +/*
17245 + * return 0 if processed.
17246 + */
17247 +static int hn_gen_by_inode(char *name, unsigned int nlen, struct inode *inode,
17248 +                          const unsigned int isdir)
17249 +{
17250 +       int err;
17251 +       struct dentry *d;
17252 +       struct qstr *dname;
17253 +
17254 +       err = 1;
17255 +       if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
17256 +               pr_warn("branch root dir was changed\n");
17257 +               err = 0;
17258 +               goto out;
17259 +       }
17260 +
17261 +       if (!isdir) {
17262 +               AuDebugOn(!name);
17263 +               au_iigen_dec(inode);
17264 +               spin_lock(&inode->i_lock);
17265 +               hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
17266 +                       spin_lock(&d->d_lock);
17267 +                       dname = &d->d_name;
17268 +                       if (dname->len != nlen
17269 +                           && memcmp(dname->name, name, nlen)) {
17270 +                               spin_unlock(&d->d_lock);
17271 +                               continue;
17272 +                       }
17273 +                       err = 0;
17274 +                       au_digen_dec(d);
17275 +                       spin_unlock(&d->d_lock);
17276 +                       break;
17277 +               }
17278 +               spin_unlock(&inode->i_lock);
17279 +       } else {
17280 +               au_fset_si(au_sbi(inode->i_sb), FAILED_REFRESH_DIR);
17281 +               d = d_find_any_alias(inode);
17282 +               if (!d) {
17283 +                       au_iigen_dec(inode);
17284 +                       goto out;
17285 +               }
17286 +
17287 +               spin_lock(&d->d_lock);
17288 +               dname = &d->d_name;
17289 +               if (dname->len == nlen && !memcmp(dname->name, name, nlen)) {
17290 +                       spin_unlock(&d->d_lock);
17291 +                       err = hn_gen_tree(d);
17292 +                       spin_lock(&d->d_lock);
17293 +               }
17294 +               spin_unlock(&d->d_lock);
17295 +               dput(d);
17296 +       }
17297 +
17298 +out:
17299 +       AuTraceErr(err);
17300 +       return err;
17301 +}
17302 +
17303 +static int hn_gen_by_name(struct dentry *dentry, const unsigned int isdir)
17304 +{
17305 +       int err;
17306 +
17307 +       if (IS_ROOT(dentry)) {
17308 +               pr_warn("branch root dir was changed\n");
17309 +               return 0;
17310 +       }
17311 +
17312 +       err = 0;
17313 +       if (!isdir) {
17314 +               au_digen_dec(dentry);
17315 +               if (d_really_is_positive(dentry))
17316 +                       au_iigen_dec(d_inode(dentry));
17317 +       } else {
17318 +               au_fset_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR);
17319 +               if (d_really_is_positive(dentry))
17320 +                       err = hn_gen_tree(dentry);
17321 +       }
17322 +
17323 +       AuTraceErr(err);
17324 +       return err;
17325 +}
17326 +
17327 +/* ---------------------------------------------------------------------- */
17328 +
17329 +/* hnotify job flags */
17330 +#define AuHnJob_XINO0          1
17331 +#define AuHnJob_GEN            (1 << 1)
17332 +#define AuHnJob_DIRENT         (1 << 2)
17333 +#define AuHnJob_ISDIR          (1 << 3)
17334 +#define AuHnJob_TRYXINO0       (1 << 4)
17335 +#define AuHnJob_MNTPNT         (1 << 5)
17336 +#define au_ftest_hnjob(flags, name)    ((flags) & AuHnJob_##name)
17337 +#define au_fset_hnjob(flags, name) \
17338 +       do { (flags) |= AuHnJob_##name; } while (0)
17339 +#define au_fclr_hnjob(flags, name) \
17340 +       do { (flags) &= ~AuHnJob_##name; } while (0)
17341 +
17342 +enum {
17343 +       AuHn_CHILD,
17344 +       AuHn_PARENT,
17345 +       AuHnLast
17346 +};
17347 +
17348 +struct au_hnotify_args {
17349 +       struct inode *h_dir, *dir, *h_child_inode;
17350 +       u32 mask;
17351 +       unsigned int flags[AuHnLast];
17352 +       unsigned int h_child_nlen;
17353 +       char h_child_name[];
17354 +};
17355 +
17356 +struct hn_job_args {
17357 +       unsigned int flags;
17358 +       struct inode *inode, *h_inode, *dir, *h_dir;
17359 +       struct dentry *dentry;
17360 +       char *h_name;
17361 +       int h_nlen;
17362 +};
17363 +
17364 +static int hn_job(struct hn_job_args *a)
17365 +{
17366 +       const unsigned int isdir = au_ftest_hnjob(a->flags, ISDIR);
17367 +       int e;
17368 +
17369 +       /* reset xino */
17370 +       if (au_ftest_hnjob(a->flags, XINO0) && a->inode)
17371 +               hn_xino(a->inode, a->h_inode); /* ignore this error */
17372 +
17373 +       if (au_ftest_hnjob(a->flags, TRYXINO0)
17374 +           && a->inode
17375 +           && a->h_inode) {
17376 +               inode_lock_shared_nested(a->h_inode, AuLsc_I_CHILD);
17377 +               if (!a->h_inode->i_nlink
17378 +                   && !(a->h_inode->i_state & I_LINKABLE))
17379 +                       hn_xino(a->inode, a->h_inode); /* ignore this error */
17380 +               inode_unlock_shared(a->h_inode);
17381 +       }
17382 +
17383 +       /* make the generation obsolete */
17384 +       if (au_ftest_hnjob(a->flags, GEN)) {
17385 +               e = -1;
17386 +               if (a->inode)
17387 +                       e = hn_gen_by_inode(a->h_name, a->h_nlen, a->inode,
17388 +                                             isdir);
17389 +               if (e && a->dentry)
17390 +                       hn_gen_by_name(a->dentry, isdir);
17391 +               /* ignore this error */
17392 +       }
17393 +
17394 +       /* make dir entries obsolete */
17395 +       if (au_ftest_hnjob(a->flags, DIRENT) && a->inode) {
17396 +               struct au_vdir *vdir;
17397 +
17398 +               vdir = au_ivdir(a->inode);
17399 +               if (vdir)
17400 +                       vdir->vd_jiffy = 0;
17401 +               /* IMustLock(a->inode); */
17402 +               /* inode_inc_iversion(a->inode); */
17403 +       }
17404 +
17405 +       /* can do nothing but warn */
17406 +       if (au_ftest_hnjob(a->flags, MNTPNT)
17407 +           && a->dentry
17408 +           && d_mountpoint(a->dentry))
17409 +               pr_warn("mount-point %pd is removed or renamed\n", a->dentry);
17410 +
17411 +       return 0;
17412 +}
17413 +
17414 +/* ---------------------------------------------------------------------- */
17415 +
17416 +static struct dentry *lookup_wlock_by_name(char *name, unsigned int nlen,
17417 +                                          struct inode *dir)
17418 +{
17419 +       struct dentry *dentry, *d, *parent;
17420 +       struct qstr *dname;
17421 +
17422 +       parent = d_find_any_alias(dir);
17423 +       if (!parent)
17424 +               return NULL;
17425 +
17426 +       dentry = NULL;
17427 +       spin_lock(&parent->d_lock);
17428 +       list_for_each_entry(d, &parent->d_subdirs, d_child) {
17429 +               /* AuDbg("%pd\n", d); */
17430 +               spin_lock_nested(&d->d_lock, DENTRY_D_LOCK_NESTED);
17431 +               dname = &d->d_name;
17432 +               if (dname->len != nlen || memcmp(dname->name, name, nlen))
17433 +                       goto cont_unlock;
17434 +               if (au_di(d))
17435 +                       au_digen_dec(d);
17436 +               else
17437 +                       goto cont_unlock;
17438 +               if (au_dcount(d) > 0) {
17439 +                       dentry = dget_dlock(d);
17440 +                       spin_unlock(&d->d_lock);
17441 +                       break;
17442 +               }
17443 +
17444 +cont_unlock:
17445 +               spin_unlock(&d->d_lock);
17446 +       }
17447 +       spin_unlock(&parent->d_lock);
17448 +       dput(parent);
17449 +
17450 +       if (dentry)
17451 +               di_write_lock_child(dentry);
17452 +
17453 +       return dentry;
17454 +}
17455 +
17456 +static struct inode *lookup_wlock_by_ino(struct super_block *sb,
17457 +                                        aufs_bindex_t bindex, ino_t h_ino)
17458 +{
17459 +       struct inode *inode;
17460 +       ino_t ino;
17461 +       int err;
17462 +
17463 +       inode = NULL;
17464 +       err = au_xino_read(sb, bindex, h_ino, &ino);
17465 +       if (!err && ino)
17466 +               inode = ilookup(sb, ino);
17467 +       if (!inode)
17468 +               goto out;
17469 +
17470 +       if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
17471 +               pr_warn("wrong root branch\n");
17472 +               iput(inode);
17473 +               inode = NULL;
17474 +               goto out;
17475 +       }
17476 +
17477 +       ii_write_lock_child(inode);
17478 +
17479 +out:
17480 +       return inode;
17481 +}
17482 +
17483 +static void au_hn_bh(void *_args)
17484 +{
17485 +       struct au_hnotify_args *a = _args;
17486 +       struct super_block *sb;
17487 +       aufs_bindex_t bindex, bbot, bfound;
17488 +       unsigned char xino, try_iput;
17489 +       int err;
17490 +       struct inode *inode;
17491 +       ino_t h_ino;
17492 +       struct hn_job_args args;
17493 +       struct dentry *dentry;
17494 +       struct au_sbinfo *sbinfo;
17495 +
17496 +       AuDebugOn(!_args);
17497 +       AuDebugOn(!a->h_dir);
17498 +       AuDebugOn(!a->dir);
17499 +       AuDebugOn(!a->mask);
17500 +       AuDbg("mask 0x%x, i%lu, hi%lu, hci%lu\n",
17501 +             a->mask, a->dir->i_ino, a->h_dir->i_ino,
17502 +             a->h_child_inode ? a->h_child_inode->i_ino : 0);
17503 +
17504 +       inode = NULL;
17505 +       dentry = NULL;
17506 +       /*
17507 +        * do not lock a->dir->i_mutex here
17508 +        * because of d_revalidate() may cause a deadlock.
17509 +        */
17510 +       sb = a->dir->i_sb;
17511 +       AuDebugOn(!sb);
17512 +       sbinfo = au_sbi(sb);
17513 +       AuDebugOn(!sbinfo);
17514 +       si_write_lock(sb, AuLock_NOPLMW);
17515 +
17516 +       if (au_opt_test(sbinfo->si_mntflags, DIRREN))
17517 +               switch (a->mask & FS_EVENTS_POSS_ON_CHILD) {
17518 +               case FS_MOVED_FROM:
17519 +               case FS_MOVED_TO:
17520 +                       AuWarn1("DIRREN with UDBA may not work correctly "
17521 +                               "for the direct rename(2)\n");
17522 +               }
17523 +
17524 +       ii_read_lock_parent(a->dir);
17525 +       bfound = -1;
17526 +       bbot = au_ibbot(a->dir);
17527 +       for (bindex = au_ibtop(a->dir); bindex <= bbot; bindex++)
17528 +               if (au_h_iptr(a->dir, bindex) == a->h_dir) {
17529 +                       bfound = bindex;
17530 +                       break;
17531 +               }
17532 +       ii_read_unlock(a->dir);
17533 +       if (unlikely(bfound < 0))
17534 +               goto out;
17535 +
17536 +       xino = !!au_opt_test(au_mntflags(sb), XINO);
17537 +       h_ino = 0;
17538 +       if (a->h_child_inode)
17539 +               h_ino = a->h_child_inode->i_ino;
17540 +
17541 +       if (a->h_child_nlen
17542 +           && (au_ftest_hnjob(a->flags[AuHn_CHILD], GEN)
17543 +               || au_ftest_hnjob(a->flags[AuHn_CHILD], MNTPNT)))
17544 +               dentry = lookup_wlock_by_name(a->h_child_name, a->h_child_nlen,
17545 +                                             a->dir);
17546 +       try_iput = 0;
17547 +       if (dentry && d_really_is_positive(dentry))
17548 +               inode = d_inode(dentry);
17549 +       if (xino && !inode && h_ino
17550 +           && (au_ftest_hnjob(a->flags[AuHn_CHILD], XINO0)
17551 +               || au_ftest_hnjob(a->flags[AuHn_CHILD], TRYXINO0)
17552 +               || au_ftest_hnjob(a->flags[AuHn_CHILD], GEN))) {
17553 +               inode = lookup_wlock_by_ino(sb, bfound, h_ino);
17554 +               try_iput = 1;
17555 +       }
17556 +
17557 +       args.flags = a->flags[AuHn_CHILD];
17558 +       args.dentry = dentry;
17559 +       args.inode = inode;
17560 +       args.h_inode = a->h_child_inode;
17561 +       args.dir = a->dir;
17562 +       args.h_dir = a->h_dir;
17563 +       args.h_name = a->h_child_name;
17564 +       args.h_nlen = a->h_child_nlen;
17565 +       err = hn_job(&args);
17566 +       if (dentry) {
17567 +               if (au_di(dentry))
17568 +                       di_write_unlock(dentry);
17569 +               dput(dentry);
17570 +       }
17571 +       if (inode && try_iput) {
17572 +               ii_write_unlock(inode);
17573 +               iput(inode);
17574 +       }
17575 +
17576 +       ii_write_lock_parent(a->dir);
17577 +       args.flags = a->flags[AuHn_PARENT];
17578 +       args.dentry = NULL;
17579 +       args.inode = a->dir;
17580 +       args.h_inode = a->h_dir;
17581 +       args.dir = NULL;
17582 +       args.h_dir = NULL;
17583 +       args.h_name = NULL;
17584 +       args.h_nlen = 0;
17585 +       err = hn_job(&args);
17586 +       ii_write_unlock(a->dir);
17587 +
17588 +out:
17589 +       iput(a->h_child_inode);
17590 +       iput(a->h_dir);
17591 +       iput(a->dir);
17592 +       si_write_unlock(sb);
17593 +       au_nwt_done(&sbinfo->si_nowait);
17594 +       au_kfree_rcu(a);
17595 +}
17596 +
17597 +/* ---------------------------------------------------------------------- */
17598 +
17599 +int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
17600 +              const struct qstr *h_child_qstr, struct inode *h_child_inode)
17601 +{
17602 +       int err, len;
17603 +       unsigned int flags[AuHnLast], f;
17604 +       unsigned char isdir, isroot, wh;
17605 +       struct inode *dir;
17606 +       struct au_hnotify_args *args;
17607 +       char *p, *h_child_name;
17608 +
17609 +       err = 0;
17610 +       AuDebugOn(!hnotify || !hnotify->hn_aufs_inode);
17611 +       dir = igrab(hnotify->hn_aufs_inode);
17612 +       if (!dir)
17613 +               goto out;
17614 +
17615 +       isroot = (dir->i_ino == AUFS_ROOT_INO);
17616 +       wh = 0;
17617 +       h_child_name = (void *)h_child_qstr->name;
17618 +       len = h_child_qstr->len;
17619 +       if (h_child_name) {
17620 +               if (len > AUFS_WH_PFX_LEN
17621 +                   && !memcmp(h_child_name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
17622 +                       h_child_name += AUFS_WH_PFX_LEN;
17623 +                       len -= AUFS_WH_PFX_LEN;
17624 +                       wh = 1;
17625 +               }
17626 +       }
17627 +
17628 +       isdir = 0;
17629 +       if (h_child_inode)
17630 +               isdir = !!S_ISDIR(h_child_inode->i_mode);
17631 +       flags[AuHn_PARENT] = AuHnJob_ISDIR;
17632 +       flags[AuHn_CHILD] = 0;
17633 +       if (isdir)
17634 +               flags[AuHn_CHILD] = AuHnJob_ISDIR;
17635 +       au_fset_hnjob(flags[AuHn_PARENT], DIRENT);
17636 +       au_fset_hnjob(flags[AuHn_CHILD], GEN);
17637 +       switch (mask & ALL_FSNOTIFY_DIRENT_EVENTS) {
17638 +       case FS_MOVED_FROM:
17639 +       case FS_MOVED_TO:
17640 +               au_fset_hnjob(flags[AuHn_CHILD], XINO0);
17641 +               au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
17642 +               fallthrough;
17643 +       case FS_CREATE:
17644 +               AuDebugOn(!h_child_name);
17645 +               break;
17646 +
17647 +       case FS_DELETE:
17648 +               /*
17649 +                * aufs never be able to get this child inode.
17650 +                * revalidation should be in d_revalidate()
17651 +                * by checking i_nlink, i_generation or d_unhashed().
17652 +                */
17653 +               AuDebugOn(!h_child_name);
17654 +               au_fset_hnjob(flags[AuHn_CHILD], TRYXINO0);
17655 +               au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
17656 +               break;
17657 +
17658 +       default:
17659 +               AuDebugOn(1);
17660 +       }
17661 +
17662 +       if (wh)
17663 +               h_child_inode = NULL;
17664 +
17665 +       err = -ENOMEM;
17666 +       /* iput() and kfree() will be called in au_hnotify() */
17667 +       args = kmalloc(sizeof(*args) + len + 1, GFP_NOFS);
17668 +       if (unlikely(!args)) {
17669 +               AuErr1("no memory\n");
17670 +               iput(dir);
17671 +               goto out;
17672 +       }
17673 +       args->flags[AuHn_PARENT] = flags[AuHn_PARENT];
17674 +       args->flags[AuHn_CHILD] = flags[AuHn_CHILD];
17675 +       args->mask = mask;
17676 +       args->dir = dir;
17677 +       args->h_dir = igrab(h_dir);
17678 +       if (h_child_inode)
17679 +               h_child_inode = igrab(h_child_inode); /* can be NULL */
17680 +       args->h_child_inode = h_child_inode;
17681 +       args->h_child_nlen = len;
17682 +       if (len) {
17683 +               p = (void *)args;
17684 +               p += sizeof(*args);
17685 +               memcpy(p, h_child_name, len);
17686 +               p[len] = 0;
17687 +       }
17688 +
17689 +       /* NFS fires the event for silly-renamed one from kworker */
17690 +       f = 0;
17691 +       if (!dir->i_nlink
17692 +           || (au_test_nfs(h_dir->i_sb) && (mask & FS_DELETE)))
17693 +               f = AuWkq_NEST;
17694 +       err = au_wkq_nowait(au_hn_bh, args, dir->i_sb, f);
17695 +       if (unlikely(err)) {
17696 +               pr_err("wkq %d\n", err);
17697 +               iput(args->h_child_inode);
17698 +               iput(args->h_dir);
17699 +               iput(args->dir);
17700 +               au_kfree_rcu(args);
17701 +       }
17702 +
17703 +out:
17704 +       return err;
17705 +}
17706 +
17707 +/* ---------------------------------------------------------------------- */
17708 +
17709 +int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm)
17710 +{
17711 +       int err;
17712 +
17713 +       AuDebugOn(!(udba & AuOptMask_UDBA));
17714 +
17715 +       err = 0;
17716 +       if (au_hnotify_op.reset_br)
17717 +               err = au_hnotify_op.reset_br(udba, br, perm);
17718 +
17719 +       return err;
17720 +}
17721 +
17722 +int au_hnotify_init_br(struct au_branch *br, int perm)
17723 +{
17724 +       int err;
17725 +
17726 +       err = 0;
17727 +       if (au_hnotify_op.init_br)
17728 +               err = au_hnotify_op.init_br(br, perm);
17729 +
17730 +       return err;
17731 +}
17732 +
17733 +void au_hnotify_fin_br(struct au_branch *br)
17734 +{
17735 +       if (au_hnotify_op.fin_br)
17736 +               au_hnotify_op.fin_br(br);
17737 +}
17738 +
17739 +static void au_hn_destroy_cache(void)
17740 +{
17741 +       kmem_cache_destroy(au_cache[AuCache_HNOTIFY]);
17742 +       au_cache[AuCache_HNOTIFY] = NULL;
17743 +}
17744 +
17745 +int __init au_hnotify_init(void)
17746 +{
17747 +       int err;
17748 +
17749 +       err = -ENOMEM;
17750 +       au_cache[AuCache_HNOTIFY] = AuCache(au_hnotify);
17751 +       if (au_cache[AuCache_HNOTIFY]) {
17752 +               err = 0;
17753 +               if (au_hnotify_op.init)
17754 +                       err = au_hnotify_op.init();
17755 +               if (unlikely(err))
17756 +                       au_hn_destroy_cache();
17757 +       }
17758 +       AuTraceErr(err);
17759 +       return err;
17760 +}
17761 +
17762 +void au_hnotify_fin(void)
17763 +{
17764 +       if (au_hnotify_op.fin)
17765 +               au_hnotify_op.fin();
17766 +
17767 +       /* cf. au_cache_fin() */
17768 +       if (au_cache[AuCache_HNOTIFY])
17769 +               au_hn_destroy_cache();
17770 +}
17771 diff -urN /usr/share/empty/fs/aufs/iinfo.c linux/fs/aufs/iinfo.c
17772 --- /usr/share/empty/fs/aufs/iinfo.c    1970-01-01 01:00:00.000000000 +0100
17773 +++ linux/fs/aufs/iinfo.c       2021-11-01 23:48:34.209692595 +0100
17774 @@ -0,0 +1,286 @@
17775 +// SPDX-License-Identifier: GPL-2.0
17776 +/*
17777 + * Copyright (C) 2005-2021 Junjiro R. Okajima
17778 + *
17779 + * This program, aufs is free software; you can redistribute it and/or modify
17780 + * it under the terms of the GNU General Public License as published by
17781 + * the Free Software Foundation; either version 2 of the License, or
17782 + * (at your option) any later version.
17783 + *
17784 + * This program is distributed in the hope that it will be useful,
17785 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
17786 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17787 + * GNU General Public License for more details.
17788 + *
17789 + * You should have received a copy of the GNU General Public License
17790 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17791 + */
17792 +
17793 +/*
17794 + * inode private data
17795 + */
17796 +
17797 +#include "aufs.h"
17798 +
17799 +struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex)
17800 +{
17801 +       struct inode *h_inode;
17802 +       struct au_hinode *hinode;
17803 +
17804 +       IiMustAnyLock(inode);
17805 +
17806 +       hinode = au_hinode(au_ii(inode), bindex);
17807 +       h_inode = hinode->hi_inode;
17808 +       AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
17809 +       return h_inode;
17810 +}
17811 +
17812 +/* todo: hard/soft set? */
17813 +void au_hiput(struct au_hinode *hinode)
17814 +{
17815 +       au_hn_free(hinode);
17816 +       dput(hinode->hi_whdentry);
17817 +       iput(hinode->hi_inode);
17818 +}
17819 +
17820 +unsigned int au_hi_flags(struct inode *inode, int isdir)
17821 +{
17822 +       unsigned int flags;
17823 +       const unsigned int mnt_flags = au_mntflags(inode->i_sb);
17824 +
17825 +       flags = 0;
17826 +       if (au_opt_test(mnt_flags, XINO))
17827 +               au_fset_hi(flags, XINO);
17828 +       if (isdir && au_opt_test(mnt_flags, UDBA_HNOTIFY))
17829 +               au_fset_hi(flags, HNOTIFY);
17830 +       return flags;
17831 +}
17832 +
17833 +void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
17834 +                  struct inode *h_inode, unsigned int flags)
17835 +{
17836 +       struct au_hinode *hinode;
17837 +       struct inode *hi;
17838 +       struct au_iinfo *iinfo = au_ii(inode);
17839 +
17840 +       IiMustWriteLock(inode);
17841 +
17842 +       hinode = au_hinode(iinfo, bindex);
17843 +       hi = hinode->hi_inode;
17844 +       AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
17845 +
17846 +       if (hi)
17847 +               au_hiput(hinode);
17848 +       hinode->hi_inode = h_inode;
17849 +       if (h_inode) {
17850 +               int err;
17851 +               struct super_block *sb = inode->i_sb;
17852 +               struct au_branch *br;
17853 +
17854 +               AuDebugOn(inode->i_mode
17855 +                         && (h_inode->i_mode & S_IFMT)
17856 +                         != (inode->i_mode & S_IFMT));
17857 +               if (bindex == iinfo->ii_btop)
17858 +                       au_cpup_igen(inode, h_inode);
17859 +               br = au_sbr(sb, bindex);
17860 +               hinode->hi_id = br->br_id;
17861 +               if (au_ftest_hi(flags, XINO)) {
17862 +                       err = au_xino_write(sb, bindex, h_inode->i_ino,
17863 +                                           inode->i_ino);
17864 +                       if (unlikely(err))
17865 +                               AuIOErr1("failed au_xino_write() %d\n", err);
17866 +               }
17867 +
17868 +               if (au_ftest_hi(flags, HNOTIFY)
17869 +                   && au_br_hnotifyable(br->br_perm)) {
17870 +                       err = au_hn_alloc(hinode, inode);
17871 +                       if (unlikely(err))
17872 +                               AuIOErr1("au_hn_alloc() %d\n", err);
17873 +               }
17874 +       }
17875 +}
17876 +
17877 +void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
17878 +                 struct dentry *h_wh)
17879 +{
17880 +       struct au_hinode *hinode;
17881 +
17882 +       IiMustWriteLock(inode);
17883 +
17884 +       hinode = au_hinode(au_ii(inode), bindex);
17885 +       AuDebugOn(hinode->hi_whdentry);
17886 +       hinode->hi_whdentry = h_wh;
17887 +}
17888 +
17889 +void au_update_iigen(struct inode *inode, int half)
17890 +{
17891 +       struct au_iinfo *iinfo;
17892 +       struct au_iigen *iigen;
17893 +       unsigned int sigen;
17894 +
17895 +       sigen = au_sigen(inode->i_sb);
17896 +       iinfo = au_ii(inode);
17897 +       iigen = &iinfo->ii_generation;
17898 +       spin_lock(&iigen->ig_spin);
17899 +       iigen->ig_generation = sigen;
17900 +       if (half)
17901 +               au_ig_fset(iigen->ig_flags, HALF_REFRESHED);
17902 +       else
17903 +               au_ig_fclr(iigen->ig_flags, HALF_REFRESHED);
17904 +       spin_unlock(&iigen->ig_spin);
17905 +}
17906 +
17907 +/* it may be called at remount time, too */
17908 +void au_update_ibrange(struct inode *inode, int do_put_zero)
17909 +{
17910 +       struct au_iinfo *iinfo;
17911 +       aufs_bindex_t bindex, bbot;
17912 +
17913 +       AuDebugOn(au_is_bad_inode(inode));
17914 +       IiMustWriteLock(inode);
17915 +
17916 +       iinfo = au_ii(inode);
17917 +       if (do_put_zero && iinfo->ii_btop >= 0) {
17918 +               for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot;
17919 +                    bindex++) {
17920 +                       struct inode *h_i;
17921 +
17922 +                       h_i = au_hinode(iinfo, bindex)->hi_inode;
17923 +                       if (h_i
17924 +                           && !h_i->i_nlink
17925 +                           && !(h_i->i_state & I_LINKABLE))
17926 +                               au_set_h_iptr(inode, bindex, NULL, 0);
17927 +               }
17928 +       }
17929 +
17930 +       iinfo->ii_btop = -1;
17931 +       iinfo->ii_bbot = -1;
17932 +       bbot = au_sbbot(inode->i_sb);
17933 +       for (bindex = 0; bindex <= bbot; bindex++)
17934 +               if (au_hinode(iinfo, bindex)->hi_inode) {
17935 +                       iinfo->ii_btop = bindex;
17936 +                       break;
17937 +               }
17938 +       if (iinfo->ii_btop >= 0)
17939 +               for (bindex = bbot; bindex >= iinfo->ii_btop; bindex--)
17940 +                       if (au_hinode(iinfo, bindex)->hi_inode) {
17941 +                               iinfo->ii_bbot = bindex;
17942 +                               break;
17943 +                       }
17944 +       AuDebugOn(iinfo->ii_btop > iinfo->ii_bbot);
17945 +}
17946 +
17947 +/* ---------------------------------------------------------------------- */
17948 +
17949 +void au_icntnr_init_once(void *_c)
17950 +{
17951 +       struct au_icntnr *c = _c;
17952 +       struct au_iinfo *iinfo = &c->iinfo;
17953 +
17954 +       spin_lock_init(&iinfo->ii_generation.ig_spin);
17955 +       au_rw_init(&iinfo->ii_rwsem);
17956 +       inode_init_once(&c->vfs_inode);
17957 +}
17958 +
17959 +void au_hinode_init(struct au_hinode *hinode)
17960 +{
17961 +       hinode->hi_inode = NULL;
17962 +       hinode->hi_id = -1;
17963 +       au_hn_init(hinode);
17964 +       hinode->hi_whdentry = NULL;
17965 +}
17966 +
17967 +int au_iinfo_init(struct inode *inode)
17968 +{
17969 +       struct au_iinfo *iinfo;
17970 +       struct super_block *sb;
17971 +       struct au_hinode *hi;
17972 +       int nbr, i;
17973 +
17974 +       sb = inode->i_sb;
17975 +       iinfo = &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
17976 +       nbr = au_sbbot(sb) + 1;
17977 +       if (unlikely(nbr <= 0))
17978 +               nbr = 1;
17979 +       hi = kmalloc_array(nbr, sizeof(*iinfo->ii_hinode), GFP_NOFS);
17980 +       if (hi) {
17981 +               au_lcnt_inc(&au_sbi(sb)->si_ninodes);
17982 +
17983 +               iinfo->ii_hinode = hi;
17984 +               for (i = 0; i < nbr; i++, hi++)
17985 +                       au_hinode_init(hi);
17986 +
17987 +               iinfo->ii_generation.ig_generation = au_sigen(sb);
17988 +               iinfo->ii_btop = -1;
17989 +               iinfo->ii_bbot = -1;
17990 +               iinfo->ii_vdir = NULL;
17991 +               return 0;
17992 +       }
17993 +       return -ENOMEM;
17994 +}
17995 +
17996 +int au_hinode_realloc(struct au_iinfo *iinfo, int nbr, int may_shrink)
17997 +{
17998 +       int err, i;
17999 +       struct au_hinode *hip;
18000 +
18001 +       AuRwMustWriteLock(&iinfo->ii_rwsem);
18002 +
18003 +       err = -ENOMEM;
18004 +       hip = au_krealloc(iinfo->ii_hinode, sizeof(*hip) * nbr, GFP_NOFS,
18005 +                         may_shrink);
18006 +       if (hip) {
18007 +               iinfo->ii_hinode = hip;
18008 +               i = iinfo->ii_bbot + 1;
18009 +               hip += i;
18010 +               for (; i < nbr; i++, hip++)
18011 +                       au_hinode_init(hip);
18012 +               err = 0;
18013 +       }
18014 +
18015 +       return err;
18016 +}
18017 +
18018 +void au_iinfo_fin(struct inode *inode)
18019 +{
18020 +       struct au_iinfo *iinfo;
18021 +       struct au_hinode *hi;
18022 +       struct super_block *sb;
18023 +       aufs_bindex_t bindex, bbot;
18024 +       const unsigned char unlinked = !inode->i_nlink;
18025 +
18026 +       AuDebugOn(au_is_bad_inode(inode));
18027 +
18028 +       sb = inode->i_sb;
18029 +       au_lcnt_dec(&au_sbi(sb)->si_ninodes);
18030 +       if (si_pid_test(sb))
18031 +               au_xino_delete_inode(inode, unlinked);
18032 +       else {
18033 +               /*
18034 +                * it is safe to hide the dependency between sbinfo and
18035 +                * sb->s_umount.
18036 +                */
18037 +               lockdep_off();
18038 +               si_noflush_read_lock(sb);
18039 +               au_xino_delete_inode(inode, unlinked);
18040 +               si_read_unlock(sb);
18041 +               lockdep_on();
18042 +       }
18043 +
18044 +       iinfo = au_ii(inode);
18045 +       if (iinfo->ii_vdir)
18046 +               au_vdir_free(iinfo->ii_vdir);
18047 +
18048 +       bindex = iinfo->ii_btop;
18049 +       if (bindex >= 0) {
18050 +               hi = au_hinode(iinfo, bindex);
18051 +               bbot = iinfo->ii_bbot;
18052 +               while (bindex++ <= bbot) {
18053 +                       if (hi->hi_inode)
18054 +                               au_hiput(hi);
18055 +                       hi++;
18056 +               }
18057 +       }
18058 +       au_kfree_rcu(iinfo->ii_hinode);
18059 +       AuRwDestroy(&iinfo->ii_rwsem);
18060 +}
18061 diff -urN /usr/share/empty/fs/aufs/inode.c linux/fs/aufs/inode.c
18062 --- /usr/share/empty/fs/aufs/inode.c    1970-01-01 01:00:00.000000000 +0100
18063 +++ linux/fs/aufs/inode.c       2021-11-01 23:48:34.209692595 +0100
18064 @@ -0,0 +1,531 @@
18065 +// SPDX-License-Identifier: GPL-2.0
18066 +/*
18067 + * Copyright (C) 2005-2021 Junjiro R. Okajima
18068 + *
18069 + * This program, aufs is free software; you can redistribute it and/or modify
18070 + * it under the terms of the GNU General Public License as published by
18071 + * the Free Software Foundation; either version 2 of the License, or
18072 + * (at your option) any later version.
18073 + *
18074 + * This program is distributed in the hope that it will be useful,
18075 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
18076 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18077 + * GNU General Public License for more details.
18078 + *
18079 + * You should have received a copy of the GNU General Public License
18080 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18081 + */
18082 +
18083 +/*
18084 + * inode functions
18085 + */
18086 +
18087 +#include <linux/iversion.h>
18088 +#include "aufs.h"
18089 +
18090 +struct inode *au_igrab(struct inode *inode)
18091 +{
18092 +       if (inode) {
18093 +               AuDebugOn(!atomic_read(&inode->i_count));
18094 +               ihold(inode);
18095 +       }
18096 +       return inode;
18097 +}
18098 +
18099 +static void au_refresh_hinode_attr(struct inode *inode, int do_version)
18100 +{
18101 +       au_cpup_attr_all(inode, /*force*/0);
18102 +       au_update_iigen(inode, /*half*/1);
18103 +       if (do_version)
18104 +               inode_inc_iversion(inode);
18105 +}
18106 +
18107 +static int au_ii_refresh(struct inode *inode, int *update)
18108 +{
18109 +       int err, e, nbr;
18110 +       umode_t type;
18111 +       aufs_bindex_t bindex, new_bindex;
18112 +       struct super_block *sb;
18113 +       struct au_iinfo *iinfo;
18114 +       struct au_hinode *p, *q, tmp;
18115 +
18116 +       AuDebugOn(au_is_bad_inode(inode));
18117 +       IiMustWriteLock(inode);
18118 +
18119 +       *update = 0;
18120 +       sb = inode->i_sb;
18121 +       nbr = au_sbbot(sb) + 1;
18122 +       type = inode->i_mode & S_IFMT;
18123 +       iinfo = au_ii(inode);
18124 +       err = au_hinode_realloc(iinfo, nbr, /*may_shrink*/0);
18125 +       if (unlikely(err))
18126 +               goto out;
18127 +
18128 +       AuDebugOn(iinfo->ii_btop < 0);
18129 +       p = au_hinode(iinfo, iinfo->ii_btop);
18130 +       for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot;
18131 +            bindex++, p++) {
18132 +               if (!p->hi_inode)
18133 +                       continue;
18134 +
18135 +               AuDebugOn(type != (p->hi_inode->i_mode & S_IFMT));
18136 +               new_bindex = au_br_index(sb, p->hi_id);
18137 +               if (new_bindex == bindex)
18138 +                       continue;
18139 +
18140 +               if (new_bindex < 0) {
18141 +                       *update = 1;
18142 +                       au_hiput(p);
18143 +                       p->hi_inode = NULL;
18144 +                       continue;
18145 +               }
18146 +
18147 +               if (new_bindex < iinfo->ii_btop)
18148 +                       iinfo->ii_btop = new_bindex;
18149 +               if (iinfo->ii_bbot < new_bindex)
18150 +                       iinfo->ii_bbot = new_bindex;
18151 +               /* swap two lower inode, and loop again */
18152 +               q = au_hinode(iinfo, new_bindex);
18153 +               tmp = *q;
18154 +               *q = *p;
18155 +               *p = tmp;
18156 +               if (tmp.hi_inode) {
18157 +                       bindex--;
18158 +                       p--;
18159 +               }
18160 +       }
18161 +       au_update_ibrange(inode, /*do_put_zero*/0);
18162 +       au_hinode_realloc(iinfo, nbr, /*may_shrink*/1); /* harmless if err */
18163 +       e = au_dy_irefresh(inode);
18164 +       if (unlikely(e && !err))
18165 +               err = e;
18166 +
18167 +out:
18168 +       AuTraceErr(err);
18169 +       return err;
18170 +}
18171 +
18172 +void au_refresh_iop(struct inode *inode, int force_getattr)
18173 +{
18174 +       int type;
18175 +       struct au_sbinfo *sbi = au_sbi(inode->i_sb);
18176 +       const struct inode_operations *iop
18177 +               = force_getattr ? aufs_iop : sbi->si_iop_array;
18178 +
18179 +       if (inode->i_op == iop)
18180 +               return;
18181 +
18182 +       switch (inode->i_mode & S_IFMT) {
18183 +       case S_IFDIR:
18184 +               type = AuIop_DIR;
18185 +               break;
18186 +       case S_IFLNK:
18187 +               type = AuIop_SYMLINK;
18188 +               break;
18189 +       default:
18190 +               type = AuIop_OTHER;
18191 +               break;
18192 +       }
18193 +
18194 +       inode->i_op = iop + type;
18195 +       /* unnecessary smp_wmb() */
18196 +}
18197 +
18198 +int au_refresh_hinode_self(struct inode *inode)
18199 +{
18200 +       int err, update;
18201 +
18202 +       err = au_ii_refresh(inode, &update);
18203 +       if (!err)
18204 +               au_refresh_hinode_attr(inode, update && S_ISDIR(inode->i_mode));
18205 +
18206 +       AuTraceErr(err);
18207 +       return err;
18208 +}
18209 +
18210 +int au_refresh_hinode(struct inode *inode, struct dentry *dentry)
18211 +{
18212 +       int err, e, update;
18213 +       unsigned int flags;
18214 +       umode_t mode;
18215 +       aufs_bindex_t bindex, bbot;
18216 +       unsigned char isdir;
18217 +       struct au_hinode *p;
18218 +       struct au_iinfo *iinfo;
18219 +
18220 +       err = au_ii_refresh(inode, &update);
18221 +       if (unlikely(err))
18222 +               goto out;
18223 +
18224 +       update = 0;
18225 +       iinfo = au_ii(inode);
18226 +       p = au_hinode(iinfo, iinfo->ii_btop);
18227 +       mode = (inode->i_mode & S_IFMT);
18228 +       isdir = S_ISDIR(mode);
18229 +       flags = au_hi_flags(inode, isdir);
18230 +       bbot = au_dbbot(dentry);
18231 +       for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++) {
18232 +               struct inode *h_i, *h_inode;
18233 +               struct dentry *h_d;
18234 +
18235 +               h_d = au_h_dptr(dentry, bindex);
18236 +               if (!h_d || d_is_negative(h_d))
18237 +                       continue;
18238 +
18239 +               h_inode = d_inode(h_d);
18240 +               AuDebugOn(mode != (h_inode->i_mode & S_IFMT));
18241 +               if (iinfo->ii_btop <= bindex && bindex <= iinfo->ii_bbot) {
18242 +                       h_i = au_h_iptr(inode, bindex);
18243 +                       if (h_i) {
18244 +                               if (h_i == h_inode)
18245 +                                       continue;
18246 +                               err = -EIO;
18247 +                               break;
18248 +                       }
18249 +               }
18250 +               if (bindex < iinfo->ii_btop)
18251 +                       iinfo->ii_btop = bindex;
18252 +               if (iinfo->ii_bbot < bindex)
18253 +                       iinfo->ii_bbot = bindex;
18254 +               au_set_h_iptr(inode, bindex, au_igrab(h_inode), flags);
18255 +               update = 1;
18256 +       }
18257 +       au_update_ibrange(inode, /*do_put_zero*/0);
18258 +       e = au_dy_irefresh(inode);
18259 +       if (unlikely(e && !err))
18260 +               err = e;
18261 +       if (!err)
18262 +               au_refresh_hinode_attr(inode, update && isdir);
18263 +
18264 +out:
18265 +       AuTraceErr(err);
18266 +       return err;
18267 +}
18268 +
18269 +static int set_inode(struct inode *inode, struct dentry *dentry)
18270 +{
18271 +       int err;
18272 +       unsigned int flags;
18273 +       umode_t mode;
18274 +       aufs_bindex_t bindex, btop, btail;
18275 +       unsigned char isdir;
18276 +       struct dentry *h_dentry;
18277 +       struct inode *h_inode;
18278 +       struct au_iinfo *iinfo;
18279 +       const struct inode_operations *iop;
18280 +
18281 +       IiMustWriteLock(inode);
18282 +
18283 +       err = 0;
18284 +       isdir = 0;
18285 +       iop = au_sbi(inode->i_sb)->si_iop_array;
18286 +       btop = au_dbtop(dentry);
18287 +       h_dentry = au_h_dptr(dentry, btop);
18288 +       h_inode = d_inode(h_dentry);
18289 +       mode = h_inode->i_mode;
18290 +       switch (mode & S_IFMT) {
18291 +       case S_IFREG:
18292 +               btail = au_dbtail(dentry);
18293 +               inode->i_op = iop + AuIop_OTHER;
18294 +               inode->i_fop = &aufs_file_fop;
18295 +               err = au_dy_iaop(inode, btop, h_inode);
18296 +               if (unlikely(err))
18297 +                       goto out;
18298 +               break;
18299 +       case S_IFDIR:
18300 +               isdir = 1;
18301 +               btail = au_dbtaildir(dentry);
18302 +               inode->i_op = iop + AuIop_DIR;
18303 +               inode->i_fop = &aufs_dir_fop;
18304 +               break;
18305 +       case S_IFLNK:
18306 +               btail = au_dbtail(dentry);
18307 +               inode->i_op = iop + AuIop_SYMLINK;
18308 +               break;
18309 +       case S_IFBLK:
18310 +       case S_IFCHR:
18311 +       case S_IFIFO:
18312 +       case S_IFSOCK:
18313 +               btail = au_dbtail(dentry);
18314 +               inode->i_op = iop + AuIop_OTHER;
18315 +               init_special_inode(inode, mode, h_inode->i_rdev);
18316 +               break;
18317 +       default:
18318 +               AuIOErr("Unknown file type 0%o\n", mode);
18319 +               err = -EIO;
18320 +               goto out;
18321 +       }
18322 +
18323 +       /* do not set hnotify for whiteouted dirs (SHWH mode) */
18324 +       flags = au_hi_flags(inode, isdir);
18325 +       if (au_opt_test(au_mntflags(dentry->d_sb), SHWH)
18326 +           && au_ftest_hi(flags, HNOTIFY)
18327 +           && dentry->d_name.len > AUFS_WH_PFX_LEN
18328 +           && !memcmp(dentry->d_name.name, AUFS_WH_PFX, AUFS_WH_PFX_LEN))
18329 +               au_fclr_hi(flags, HNOTIFY);
18330 +       iinfo = au_ii(inode);
18331 +       iinfo->ii_btop = btop;
18332 +       iinfo->ii_bbot = btail;
18333 +       for (bindex = btop; bindex <= btail; bindex++) {
18334 +               h_dentry = au_h_dptr(dentry, bindex);
18335 +               if (h_dentry)
18336 +                       au_set_h_iptr(inode, bindex,
18337 +                                     au_igrab(d_inode(h_dentry)), flags);
18338 +       }
18339 +       au_cpup_attr_all(inode, /*force*/1);
18340 +       /*
18341 +        * to force calling aufs_get_acl() every time,
18342 +        * do not call cache_no_acl() for aufs inode.
18343 +        */
18344 +
18345 +out:
18346 +       return err;
18347 +}
18348 +
18349 +/*
18350 + * successful returns with iinfo write_locked
18351 + * minus: errno
18352 + * zero: success, matched
18353 + * plus: no error, but unmatched
18354 + */
18355 +static int reval_inode(struct inode *inode, struct dentry *dentry)
18356 +{
18357 +       int err;
18358 +       unsigned int gen, igflags;
18359 +       aufs_bindex_t bindex, bbot;
18360 +       struct inode *h_inode, *h_dinode;
18361 +       struct dentry *h_dentry;
18362 +
18363 +       /*
18364 +        * before this function, if aufs got any iinfo lock, it must be only
18365 +        * one, the parent dir.
18366 +        * it can happen by UDBA and the obsoleted inode number.
18367 +        */
18368 +       err = -EIO;
18369 +       if (unlikely(inode->i_ino == parent_ino(dentry)))
18370 +               goto out;
18371 +
18372 +       err = 1;
18373 +       ii_write_lock_new_child(inode);
18374 +       h_dentry = au_h_dptr(dentry, au_dbtop(dentry));
18375 +       h_dinode = d_inode(h_dentry);
18376 +       bbot = au_ibbot(inode);
18377 +       for (bindex = au_ibtop(inode); bindex <= bbot; bindex++) {
18378 +               h_inode = au_h_iptr(inode, bindex);
18379 +               if (!h_inode || h_inode != h_dinode)
18380 +                       continue;
18381 +
18382 +               err = 0;
18383 +               gen = au_iigen(inode, &igflags);
18384 +               if (gen == au_digen(dentry)
18385 +                   && !au_ig_ftest(igflags, HALF_REFRESHED))
18386 +                       break;
18387 +
18388 +               /* fully refresh inode using dentry */
18389 +               err = au_refresh_hinode(inode, dentry);
18390 +               if (!err)
18391 +                       au_update_iigen(inode, /*half*/0);
18392 +               break;
18393 +       }
18394 +
18395 +       if (unlikely(err))
18396 +               ii_write_unlock(inode);
18397 +out:
18398 +       return err;
18399 +}
18400 +
18401 +int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
18402 +          unsigned int d_type, ino_t *ino)
18403 +{
18404 +       int err, idx;
18405 +       const int isnondir = d_type != DT_DIR;
18406 +
18407 +       /* prevent hardlinked inode number from race condition */
18408 +       if (isnondir) {
18409 +               err = au_xinondir_enter(sb, bindex, h_ino, &idx);
18410 +               if (unlikely(err))
18411 +                       goto out;
18412 +       }
18413 +
18414 +       err = au_xino_read(sb, bindex, h_ino, ino);
18415 +       if (unlikely(err))
18416 +               goto out_xinondir;
18417 +
18418 +       if (!*ino) {
18419 +               err = -EIO;
18420 +               *ino = au_xino_new_ino(sb);
18421 +               if (unlikely(!*ino))
18422 +                       goto out_xinondir;
18423 +               err = au_xino_write(sb, bindex, h_ino, *ino);
18424 +               if (unlikely(err))
18425 +                       goto out_xinondir;
18426 +       }
18427 +
18428 +out_xinondir:
18429 +       if (isnondir && idx >= 0)
18430 +               au_xinondir_leave(sb, bindex, h_ino, idx);
18431 +out:
18432 +       return err;
18433 +}
18434 +
18435 +/* successful returns with iinfo write_locked */
18436 +/* todo: return with unlocked? */
18437 +struct inode *au_new_inode(struct dentry *dentry, int must_new)
18438 +{
18439 +       struct inode *inode, *h_inode;
18440 +       struct dentry *h_dentry;
18441 +       struct super_block *sb;
18442 +       ino_t h_ino, ino;
18443 +       int err, idx, hlinked;
18444 +       aufs_bindex_t btop;
18445 +
18446 +       sb = dentry->d_sb;
18447 +       btop = au_dbtop(dentry);
18448 +       h_dentry = au_h_dptr(dentry, btop);
18449 +       h_inode = d_inode(h_dentry);
18450 +       h_ino = h_inode->i_ino;
18451 +       hlinked = !d_is_dir(h_dentry) && h_inode->i_nlink > 1;
18452 +
18453 +new_ino:
18454 +       /*
18455 +        * stop 'race'-ing between hardlinks under different
18456 +        * parents.
18457 +        */
18458 +       if (hlinked) {
18459 +               err = au_xinondir_enter(sb, btop, h_ino, &idx);
18460 +               inode = ERR_PTR(err);
18461 +               if (unlikely(err))
18462 +                       goto out;
18463 +       }
18464 +
18465 +       err = au_xino_read(sb, btop, h_ino, &ino);
18466 +       inode = ERR_PTR(err);
18467 +       if (unlikely(err))
18468 +               goto out_xinondir;
18469 +
18470 +       if (!ino) {
18471 +               ino = au_xino_new_ino(sb);
18472 +               if (unlikely(!ino)) {
18473 +                       inode = ERR_PTR(-EIO);
18474 +                       goto out_xinondir;
18475 +               }
18476 +       }
18477 +
18478 +       AuDbg("i%lu\n", (unsigned long)ino);
18479 +       inode = au_iget_locked(sb, ino);
18480 +       err = PTR_ERR(inode);
18481 +       if (IS_ERR(inode))
18482 +               goto out_xinondir;
18483 +
18484 +       AuDbg("%lx, new %d\n", inode->i_state, !!(inode->i_state & I_NEW));
18485 +       if (inode->i_state & I_NEW) {
18486 +               ii_write_lock_new_child(inode);
18487 +               err = set_inode(inode, dentry);
18488 +               if (!err) {
18489 +                       unlock_new_inode(inode);
18490 +                       goto out_xinondir; /* success */
18491 +               }
18492 +
18493 +               /*
18494 +                * iget_failed() calls iput(), but we need to call
18495 +                * ii_write_unlock() after iget_failed(). so dirty hack for
18496 +                * i_count.
18497 +                */
18498 +               atomic_inc(&inode->i_count);
18499 +               iget_failed(inode);
18500 +               ii_write_unlock(inode);
18501 +               au_xino_write(sb, btop, h_ino, /*ino*/0);
18502 +               /* ignore this error */
18503 +               goto out_iput;
18504 +       } else if (!must_new && !IS_DEADDIR(inode) && inode->i_nlink) {
18505 +               /*
18506 +                * horrible race condition between lookup, readdir and copyup
18507 +                * (or something).
18508 +                */
18509 +               if (hlinked && idx >= 0)
18510 +                       au_xinondir_leave(sb, btop, h_ino, idx);
18511 +               err = reval_inode(inode, dentry);
18512 +               if (unlikely(err < 0)) {
18513 +                       hlinked = 0;
18514 +                       goto out_iput;
18515 +               }
18516 +               if (!err)
18517 +                       goto out; /* success */
18518 +               else if (hlinked && idx >= 0) {
18519 +                       err = au_xinondir_enter(sb, btop, h_ino, &idx);
18520 +                       if (unlikely(err)) {
18521 +                               iput(inode);
18522 +                               inode = ERR_PTR(err);
18523 +                               goto out;
18524 +                       }
18525 +               }
18526 +       }
18527 +
18528 +       if (unlikely(au_test_fs_unique_ino(h_inode)))
18529 +               AuWarn1("Warning: Un-notified UDBA or repeatedly renamed dir,"
18530 +                       " b%d, %s, %pd, hi%lu, i%lu.\n",
18531 +                       btop, au_sbtype(h_dentry->d_sb), dentry,
18532 +                       (unsigned long)h_ino, (unsigned long)ino);
18533 +       ino = 0;
18534 +       err = au_xino_write(sb, btop, h_ino, /*ino*/0);
18535 +       if (!err) {
18536 +               iput(inode);
18537 +               if (hlinked && idx >= 0)
18538 +                       au_xinondir_leave(sb, btop, h_ino, idx);
18539 +               goto new_ino;
18540 +       }
18541 +
18542 +out_iput:
18543 +       iput(inode);
18544 +       inode = ERR_PTR(err);
18545 +out_xinondir:
18546 +       if (hlinked && idx >= 0)
18547 +               au_xinondir_leave(sb, btop, h_ino, idx);
18548 +out:
18549 +       return inode;
18550 +}
18551 +
18552 +/* ---------------------------------------------------------------------- */
18553 +
18554 +int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
18555 +              struct inode *inode)
18556 +{
18557 +       int err;
18558 +       struct inode *hi;
18559 +
18560 +       err = au_br_rdonly(au_sbr(sb, bindex));
18561 +
18562 +       /* pseudo-link after flushed may happen out of bounds */
18563 +       if (!err
18564 +           && inode
18565 +           && au_ibtop(inode) <= bindex
18566 +           && bindex <= au_ibbot(inode)) {
18567 +               /*
18568 +                * permission check is unnecessary since vfsub routine
18569 +                * will be called later
18570 +                */
18571 +               hi = au_h_iptr(inode, bindex);
18572 +               if (hi)
18573 +                       err = IS_IMMUTABLE(hi) ? -EROFS : 0;
18574 +       }
18575 +
18576 +       return err;
18577 +}
18578 +
18579 +int au_test_h_perm(struct user_namespace *h_userns, struct inode *h_inode,
18580 +                  int mask)
18581 +{
18582 +       if (uid_eq(current_fsuid(), GLOBAL_ROOT_UID))
18583 +               return 0;
18584 +       return inode_permission(h_userns, h_inode, mask);
18585 +}
18586 +
18587 +int au_test_h_perm_sio(struct user_namespace *h_userns, struct inode *h_inode,
18588 +                      int mask)
18589 +{
18590 +       if (au_test_nfs(h_inode->i_sb)
18591 +           && (mask & MAY_WRITE)
18592 +           && S_ISDIR(h_inode->i_mode))
18593 +               mask |= MAY_READ; /* force permission check */
18594 +       return au_test_h_perm(h_userns, h_inode, mask);
18595 +}
18596 diff -urN /usr/share/empty/fs/aufs/inode.h linux/fs/aufs/inode.h
18597 --- /usr/share/empty/fs/aufs/inode.h    1970-01-01 01:00:00.000000000 +0100
18598 +++ linux/fs/aufs/inode.h       2021-11-01 23:48:34.209692595 +0100
18599 @@ -0,0 +1,705 @@
18600 +/* SPDX-License-Identifier: GPL-2.0 */
18601 +/*
18602 + * Copyright (C) 2005-2021 Junjiro R. Okajima
18603 + *
18604 + * This program, aufs is free software; you can redistribute it and/or modify
18605 + * it under the terms of the GNU General Public License as published by
18606 + * the Free Software Foundation; either version 2 of the License, or
18607 + * (at your option) any later version.
18608 + *
18609 + * This program is distributed in the hope that it will be useful,
18610 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
18611 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18612 + * GNU General Public License for more details.
18613 + *
18614 + * You should have received a copy of the GNU General Public License
18615 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18616 + */
18617 +
18618 +/*
18619 + * inode operations
18620 + */
18621 +
18622 +#ifndef __AUFS_INODE_H__
18623 +#define __AUFS_INODE_H__
18624 +
18625 +#ifdef __KERNEL__
18626 +
18627 +#include <linux/fsnotify.h>
18628 +#include "rwsem.h"
18629 +
18630 +struct vfsmount;
18631 +
18632 +struct au_hnotify {
18633 +#ifdef CONFIG_AUFS_HNOTIFY
18634 +#ifdef CONFIG_AUFS_HFSNOTIFY
18635 +       /* never use fsnotify_add_vfsmount_mark() */
18636 +       struct fsnotify_mark            hn_mark;
18637 +#endif
18638 +       struct inode            *hn_aufs_inode; /* no get/put */
18639 +       struct rcu_head         rcu;
18640 +#endif
18641 +} ____cacheline_aligned_in_smp;
18642 +
18643 +struct au_hinode {
18644 +       struct inode            *hi_inode;
18645 +       aufs_bindex_t           hi_id;
18646 +#ifdef CONFIG_AUFS_HNOTIFY
18647 +       struct au_hnotify       *hi_notify;
18648 +#endif
18649 +
18650 +       /* reference to the copied-up whiteout with get/put */
18651 +       struct dentry           *hi_whdentry;
18652 +};
18653 +
18654 +/* ig_flags */
18655 +#define AuIG_HALF_REFRESHED            1
18656 +#define au_ig_ftest(flags, name)       ((flags) & AuIG_##name)
18657 +#define au_ig_fset(flags, name) \
18658 +       do { (flags) |= AuIG_##name; } while (0)
18659 +#define au_ig_fclr(flags, name) \
18660 +       do { (flags) &= ~AuIG_##name; } while (0)
18661 +
18662 +struct au_iigen {
18663 +       spinlock_t      ig_spin;
18664 +       __u32           ig_generation, ig_flags;
18665 +};
18666 +
18667 +struct au_vdir;
18668 +struct au_iinfo {
18669 +       struct au_iigen         ii_generation;
18670 +       struct super_block      *ii_hsb1;       /* no get/put */
18671 +
18672 +       struct au_rwsem         ii_rwsem;
18673 +       aufs_bindex_t           ii_btop, ii_bbot;
18674 +       __u32                   ii_higen;
18675 +       struct au_hinode        *ii_hinode;
18676 +       struct au_vdir          *ii_vdir;
18677 +};
18678 +
18679 +struct au_icntnr {
18680 +       struct au_iinfo         iinfo;
18681 +       struct inode            vfs_inode;
18682 +       struct hlist_bl_node    plink;
18683 +       struct rcu_head         rcu;
18684 +} ____cacheline_aligned_in_smp;
18685 +
18686 +/* au_pin flags */
18687 +#define AuPin_DI_LOCKED                1
18688 +#define AuPin_MNT_WRITE                (1 << 1)
18689 +#define au_ftest_pin(flags, name)      ((flags) & AuPin_##name)
18690 +#define au_fset_pin(flags, name) \
18691 +       do { (flags) |= AuPin_##name; } while (0)
18692 +#define au_fclr_pin(flags, name) \
18693 +       do { (flags) &= ~AuPin_##name; } while (0)
18694 +
18695 +struct au_pin {
18696 +       /* input */
18697 +       struct dentry *dentry;
18698 +       unsigned int udba;
18699 +       unsigned char lsc_di, lsc_hi, flags;
18700 +       aufs_bindex_t bindex;
18701 +
18702 +       /* output */
18703 +       struct dentry *parent;
18704 +       struct au_hinode *hdir;
18705 +       struct vfsmount *h_mnt;
18706 +
18707 +       /* temporary unlock/relock for copyup */
18708 +       struct dentry *h_dentry, *h_parent;
18709 +       struct au_branch *br;
18710 +       struct task_struct *task;
18711 +};
18712 +
18713 +void au_pin_hdir_unlock(struct au_pin *p);
18714 +int au_pin_hdir_lock(struct au_pin *p);
18715 +int au_pin_hdir_relock(struct au_pin *p);
18716 +void au_pin_hdir_acquire_nest(struct au_pin *p);
18717 +void au_pin_hdir_release(struct au_pin *p);
18718 +
18719 +/* ---------------------------------------------------------------------- */
18720 +
18721 +static inline struct au_iinfo *au_ii(struct inode *inode)
18722 +{
18723 +       BUG_ON(is_bad_inode(inode));
18724 +       return &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
18725 +}
18726 +
18727 +/* ---------------------------------------------------------------------- */
18728 +
18729 +/* inode.c */
18730 +struct inode *au_igrab(struct inode *inode);
18731 +void au_refresh_iop(struct inode *inode, int force_getattr);
18732 +int au_refresh_hinode_self(struct inode *inode);
18733 +int au_refresh_hinode(struct inode *inode, struct dentry *dentry);
18734 +int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
18735 +          unsigned int d_type, ino_t *ino);
18736 +struct inode *au_new_inode(struct dentry *dentry, int must_new);
18737 +int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
18738 +              struct inode *inode);
18739 +int au_test_h_perm(struct user_namespace *h_userns, struct inode *h_inode,
18740 +                  int mask);
18741 +int au_test_h_perm_sio(struct user_namespace *h_userns, struct inode *h_inode,
18742 +                      int mask);
18743 +
18744 +static inline int au_wh_ino(struct super_block *sb, aufs_bindex_t bindex,
18745 +                           ino_t h_ino, unsigned int d_type, ino_t *ino)
18746 +{
18747 +#ifdef CONFIG_AUFS_SHWH
18748 +       return au_ino(sb, bindex, h_ino, d_type, ino);
18749 +#else
18750 +       return 0;
18751 +#endif
18752 +}
18753 +
18754 +/* i_op.c */
18755 +enum {
18756 +       AuIop_SYMLINK,
18757 +       AuIop_DIR,
18758 +       AuIop_OTHER,
18759 +       AuIop_Last
18760 +};
18761 +extern struct inode_operations aufs_iop[AuIop_Last], /* not const */
18762 +       aufs_iop_nogetattr[AuIop_Last];
18763 +
18764 +/* au_wr_dir flags */
18765 +#define AuWrDir_ADD_ENTRY      1
18766 +#define AuWrDir_ISDIR          (1 << 1)
18767 +#define AuWrDir_TMPFILE                (1 << 2)
18768 +#define au_ftest_wrdir(flags, name)    ((flags) & AuWrDir_##name)
18769 +#define au_fset_wrdir(flags, name) \
18770 +       do { (flags) |= AuWrDir_##name; } while (0)
18771 +#define au_fclr_wrdir(flags, name) \
18772 +       do { (flags) &= ~AuWrDir_##name; } while (0)
18773 +
18774 +struct au_wr_dir_args {
18775 +       aufs_bindex_t force_btgt;
18776 +       unsigned char flags;
18777 +};
18778 +int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
18779 +             struct au_wr_dir_args *args);
18780 +
18781 +struct dentry *au_pinned_h_parent(struct au_pin *pin);
18782 +void au_pin_init(struct au_pin *pin, struct dentry *dentry,
18783 +                aufs_bindex_t bindex, int lsc_di, int lsc_hi,
18784 +                unsigned int udba, unsigned char flags);
18785 +int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
18786 +          unsigned int udba, unsigned char flags) __must_check;
18787 +int au_do_pin(struct au_pin *pin) __must_check;
18788 +void au_unpin(struct au_pin *pin);
18789 +int au_reval_for_attr(struct dentry *dentry, unsigned int sigen);
18790 +
18791 +#define AuIcpup_DID_CPUP       1
18792 +#define au_ftest_icpup(flags, name)    ((flags) & AuIcpup_##name)
18793 +#define au_fset_icpup(flags, name) \
18794 +       do { (flags) |= AuIcpup_##name; } while (0)
18795 +#define au_fclr_icpup(flags, name) \
18796 +       do { (flags) &= ~AuIcpup_##name; } while (0)
18797 +
18798 +struct au_icpup_args {
18799 +       unsigned char flags;
18800 +       unsigned char pin_flags;
18801 +       aufs_bindex_t btgt;
18802 +       unsigned int udba;
18803 +       struct au_pin pin;
18804 +       struct path h_path;
18805 +       struct inode *h_inode;
18806 +};
18807 +
18808 +int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
18809 +                    struct au_icpup_args *a);
18810 +
18811 +int au_h_path_getattr(struct dentry *dentry, struct inode *inode, int force,
18812 +                     struct path *h_path, int locked);
18813 +
18814 +/* i_op_add.c */
18815 +int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
18816 +              struct dentry *h_parent, int isdir);
18817 +int aufs_mknod(struct user_namespace *userns, struct inode *dir,
18818 +              struct dentry *dentry, umode_t mode, dev_t dev);
18819 +int aufs_symlink(struct user_namespace *userns, struct inode *dir,
18820 +                struct dentry *dentry, const char *symname);
18821 +int aufs_create(struct user_namespace *userns, struct inode *dir,
18822 +               struct dentry *dentry, umode_t mode, bool want_excl);
18823 +struct vfsub_aopen_args;
18824 +int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
18825 +                      struct vfsub_aopen_args *args);
18826 +int aufs_tmpfile(struct user_namespace *userns, struct inode *dir,
18827 +                struct dentry *dentry, umode_t mode);
18828 +int aufs_link(struct dentry *src_dentry, struct inode *dir,
18829 +             struct dentry *dentry);
18830 +int aufs_mkdir(struct user_namespace *userns, struct inode *dir,
18831 +              struct dentry *dentry, umode_t mode);
18832 +
18833 +/* i_op_del.c */
18834 +int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup);
18835 +int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
18836 +              struct dentry *h_parent, int isdir);
18837 +int aufs_unlink(struct inode *dir, struct dentry *dentry);
18838 +int aufs_rmdir(struct inode *dir, struct dentry *dentry);
18839 +
18840 +/* i_op_ren.c */
18841 +int au_wbr(struct dentry *dentry, aufs_bindex_t btgt);
18842 +int aufs_rename(struct user_namespace *userns,
18843 +               struct inode *_src_dir, struct dentry *_src_dentry,
18844 +               struct inode *_dst_dir, struct dentry *_dst_dentry,
18845 +               unsigned int _flags);
18846 +
18847 +/* iinfo.c */
18848 +struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex);
18849 +void au_hiput(struct au_hinode *hinode);
18850 +void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
18851 +                 struct dentry *h_wh);
18852 +unsigned int au_hi_flags(struct inode *inode, int isdir);
18853 +
18854 +/* hinode flags */
18855 +#define AuHi_XINO      1
18856 +#define AuHi_HNOTIFY   (1 << 1)
18857 +#define au_ftest_hi(flags, name)       ((flags) & AuHi_##name)
18858 +#define au_fset_hi(flags, name) \
18859 +       do { (flags) |= AuHi_##name; } while (0)
18860 +#define au_fclr_hi(flags, name) \
18861 +       do { (flags) &= ~AuHi_##name; } while (0)
18862 +
18863 +#ifndef CONFIG_AUFS_HNOTIFY
18864 +#undef AuHi_HNOTIFY
18865 +#define AuHi_HNOTIFY   0
18866 +#endif
18867 +
18868 +void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
18869 +                  struct inode *h_inode, unsigned int flags);
18870 +
18871 +void au_update_iigen(struct inode *inode, int half);
18872 +void au_update_ibrange(struct inode *inode, int do_put_zero);
18873 +
18874 +void au_icntnr_init_once(void *_c);
18875 +void au_hinode_init(struct au_hinode *hinode);
18876 +int au_iinfo_init(struct inode *inode);
18877 +void au_iinfo_fin(struct inode *inode);
18878 +int au_hinode_realloc(struct au_iinfo *iinfo, int nbr, int may_shrink);
18879 +
18880 +#ifdef CONFIG_PROC_FS
18881 +/* plink.c */
18882 +int au_plink_maint(struct super_block *sb, int flags);
18883 +struct au_sbinfo;
18884 +void au_plink_maint_leave(struct au_sbinfo *sbinfo);
18885 +int au_plink_maint_enter(struct super_block *sb);
18886 +#ifdef CONFIG_AUFS_DEBUG
18887 +void au_plink_list(struct super_block *sb);
18888 +#else
18889 +AuStubVoid(au_plink_list, struct super_block *sb)
18890 +#endif
18891 +int au_plink_test(struct inode *inode);
18892 +struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex);
18893 +void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
18894 +                    struct dentry *h_dentry);
18895 +void au_plink_put(struct super_block *sb, int verbose);
18896 +void au_plink_clean(struct super_block *sb, int verbose);
18897 +void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id);
18898 +#else
18899 +AuStubInt0(au_plink_maint, struct super_block *sb, int flags);
18900 +AuStubVoid(au_plink_maint_leave, struct au_sbinfo *sbinfo);
18901 +AuStubInt0(au_plink_maint_enter, struct super_block *sb);
18902 +AuStubVoid(au_plink_list, struct super_block *sb);
18903 +AuStubInt0(au_plink_test, struct inode *inode);
18904 +AuStub(struct dentry *, au_plink_lkup, return NULL,
18905 +       struct inode *inode, aufs_bindex_t bindex);
18906 +AuStubVoid(au_plink_append, struct inode *inode, aufs_bindex_t bindex,
18907 +          struct dentry *h_dentry);
18908 +AuStubVoid(au_plink_put, struct super_block *sb, int verbose);
18909 +AuStubVoid(au_plink_clean, struct super_block *sb, int verbose);
18910 +AuStubVoid(au_plink_half_refresh, struct super_block *sb, aufs_bindex_t br_id);
18911 +#endif /* CONFIG_PROC_FS */
18912 +
18913 +#ifdef CONFIG_AUFS_XATTR
18914 +/* xattr.c */
18915 +int au_cpup_xattr(struct path *h_dst, struct path *h_src, int ignore_flags,
18916 +                 unsigned int verbose);
18917 +ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size);
18918 +void au_xattr_init(struct super_block *sb);
18919 +#else
18920 +AuStubInt0(au_cpup_xattr, struct path *h_dst, struct path *h_src,
18921 +          int ignore_flags, unsigned int verbose);
18922 +AuStubVoid(au_xattr_init, struct super_block *sb);
18923 +#endif
18924 +
18925 +#ifdef CONFIG_FS_POSIX_ACL
18926 +struct posix_acl *aufs_get_acl(struct inode *inode, int type, bool rcu);
18927 +int aufs_set_acl(struct user_namespace *userns, struct inode *inode,
18928 +                struct posix_acl *acl, int type);
18929 +#endif
18930 +
18931 +#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
18932 +enum {
18933 +       AU_XATTR_SET,
18934 +       AU_ACL_SET
18935 +};
18936 +
18937 +struct au_sxattr {
18938 +       int type;
18939 +       union {
18940 +               struct {
18941 +                       const char      *name;
18942 +                       const void      *value;
18943 +                       size_t          size;
18944 +                       int             flags;
18945 +               } set;
18946 +               struct {
18947 +                       struct posix_acl *acl;
18948 +                       int             type;
18949 +               } acl_set;
18950 +       } u;
18951 +};
18952 +ssize_t au_sxattr(struct dentry *dentry, struct inode *inode,
18953 +                 struct au_sxattr *arg);
18954 +#endif
18955 +
18956 +/* ---------------------------------------------------------------------- */
18957 +
18958 +/* lock subclass for iinfo */
18959 +enum {
18960 +       AuLsc_II_CHILD,         /* child first */
18961 +       AuLsc_II_CHILD2,        /* rename(2), link(2), and cpup at hnotify */
18962 +       AuLsc_II_CHILD3,        /* copyup dirs */
18963 +       AuLsc_II_PARENT,        /* see AuLsc_I_PARENT in vfsub.h */
18964 +       AuLsc_II_PARENT2,
18965 +       AuLsc_II_PARENT3,       /* copyup dirs */
18966 +       AuLsc_II_NEW_CHILD
18967 +};
18968 +
18969 +/*
18970 + * ii_read_lock_child, ii_write_lock_child,
18971 + * ii_read_lock_child2, ii_write_lock_child2,
18972 + * ii_read_lock_child3, ii_write_lock_child3,
18973 + * ii_read_lock_parent, ii_write_lock_parent,
18974 + * ii_read_lock_parent2, ii_write_lock_parent2,
18975 + * ii_read_lock_parent3, ii_write_lock_parent3,
18976 + * ii_read_lock_new_child, ii_write_lock_new_child,
18977 + */
18978 +#define AuReadLockFunc(name, lsc) \
18979 +static inline void ii_read_lock_##name(struct inode *i) \
18980 +{ \
18981 +       au_rw_read_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
18982 +}
18983 +
18984 +#define AuWriteLockFunc(name, lsc) \
18985 +static inline void ii_write_lock_##name(struct inode *i) \
18986 +{ \
18987 +       au_rw_write_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
18988 +}
18989 +
18990 +#define AuRWLockFuncs(name, lsc) \
18991 +       AuReadLockFunc(name, lsc) \
18992 +       AuWriteLockFunc(name, lsc)
18993 +
18994 +AuRWLockFuncs(child, CHILD);
18995 +AuRWLockFuncs(child2, CHILD2);
18996 +AuRWLockFuncs(child3, CHILD3);
18997 +AuRWLockFuncs(parent, PARENT);
18998 +AuRWLockFuncs(parent2, PARENT2);
18999 +AuRWLockFuncs(parent3, PARENT3);
19000 +AuRWLockFuncs(new_child, NEW_CHILD);
19001 +
19002 +#undef AuReadLockFunc
19003 +#undef AuWriteLockFunc
19004 +#undef AuRWLockFuncs
19005 +
19006 +#define ii_read_unlock(i)      au_rw_read_unlock(&au_ii(i)->ii_rwsem)
19007 +#define ii_write_unlock(i)     au_rw_write_unlock(&au_ii(i)->ii_rwsem)
19008 +#define ii_downgrade_lock(i)   au_rw_dgrade_lock(&au_ii(i)->ii_rwsem)
19009 +
19010 +#define IiMustNoWaiters(i)     AuRwMustNoWaiters(&au_ii(i)->ii_rwsem)
19011 +#define IiMustAnyLock(i)       AuRwMustAnyLock(&au_ii(i)->ii_rwsem)
19012 +#define IiMustWriteLock(i)     AuRwMustWriteLock(&au_ii(i)->ii_rwsem)
19013 +
19014 +/* ---------------------------------------------------------------------- */
19015 +
19016 +static inline void au_icntnr_init(struct au_icntnr *c)
19017 +{
19018 +#ifdef CONFIG_AUFS_DEBUG
19019 +       c->vfs_inode.i_mode = 0;
19020 +#endif
19021 +}
19022 +
19023 +static inline unsigned int au_iigen(struct inode *inode, unsigned int *igflags)
19024 +{
19025 +       unsigned int gen;
19026 +       struct au_iinfo *iinfo;
19027 +       struct au_iigen *iigen;
19028 +
19029 +       iinfo = au_ii(inode);
19030 +       iigen = &iinfo->ii_generation;
19031 +       spin_lock(&iigen->ig_spin);
19032 +       if (igflags)
19033 +               *igflags = iigen->ig_flags;
19034 +       gen = iigen->ig_generation;
19035 +       spin_unlock(&iigen->ig_spin);
19036 +
19037 +       return gen;
19038 +}
19039 +
19040 +/* tiny test for inode number */
19041 +/* tmpfs generation is too rough */
19042 +static inline int au_test_higen(struct inode *inode, struct inode *h_inode)
19043 +{
19044 +       struct au_iinfo *iinfo;
19045 +
19046 +       iinfo = au_ii(inode);
19047 +       AuRwMustAnyLock(&iinfo->ii_rwsem);
19048 +       return !(iinfo->ii_hsb1 == h_inode->i_sb
19049 +                && iinfo->ii_higen == h_inode->i_generation);
19050 +}
19051 +
19052 +static inline void au_iigen_dec(struct inode *inode)
19053 +{
19054 +       struct au_iinfo *iinfo;
19055 +       struct au_iigen *iigen;
19056 +
19057 +       iinfo = au_ii(inode);
19058 +       iigen = &iinfo->ii_generation;
19059 +       spin_lock(&iigen->ig_spin);
19060 +       iigen->ig_generation--;
19061 +       spin_unlock(&iigen->ig_spin);
19062 +}
19063 +
19064 +static inline int au_iigen_test(struct inode *inode, unsigned int sigen)
19065 +{
19066 +       int err;
19067 +
19068 +       err = 0;
19069 +       if (unlikely(inode && au_iigen(inode, NULL) != sigen))
19070 +               err = -EIO;
19071 +
19072 +       return err;
19073 +}
19074 +
19075 +/* ---------------------------------------------------------------------- */
19076 +
19077 +static inline struct au_hinode *au_hinode(struct au_iinfo *iinfo,
19078 +                                         aufs_bindex_t bindex)
19079 +{
19080 +       return iinfo->ii_hinode + bindex;
19081 +}
19082 +
19083 +static inline int au_is_bad_inode(struct inode *inode)
19084 +{
19085 +       return !!(is_bad_inode(inode) || !au_hinode(au_ii(inode), 0));
19086 +}
19087 +
19088 +static inline aufs_bindex_t au_ii_br_id(struct inode *inode,
19089 +                                       aufs_bindex_t bindex)
19090 +{
19091 +       IiMustAnyLock(inode);
19092 +       return au_hinode(au_ii(inode), bindex)->hi_id;
19093 +}
19094 +
19095 +static inline aufs_bindex_t au_ibtop(struct inode *inode)
19096 +{
19097 +       IiMustAnyLock(inode);
19098 +       return au_ii(inode)->ii_btop;
19099 +}
19100 +
19101 +static inline aufs_bindex_t au_ibbot(struct inode *inode)
19102 +{
19103 +       IiMustAnyLock(inode);
19104 +       return au_ii(inode)->ii_bbot;
19105 +}
19106 +
19107 +static inline struct au_vdir *au_ivdir(struct inode *inode)
19108 +{
19109 +       IiMustAnyLock(inode);
19110 +       return au_ii(inode)->ii_vdir;
19111 +}
19112 +
19113 +static inline struct dentry *au_hi_wh(struct inode *inode, aufs_bindex_t bindex)
19114 +{
19115 +       IiMustAnyLock(inode);
19116 +       return au_hinode(au_ii(inode), bindex)->hi_whdentry;
19117 +}
19118 +
19119 +static inline void au_set_ibtop(struct inode *inode, aufs_bindex_t bindex)
19120 +{
19121 +       IiMustWriteLock(inode);
19122 +       au_ii(inode)->ii_btop = bindex;
19123 +}
19124 +
19125 +static inline void au_set_ibbot(struct inode *inode, aufs_bindex_t bindex)
19126 +{
19127 +       IiMustWriteLock(inode);
19128 +       au_ii(inode)->ii_bbot = bindex;
19129 +}
19130 +
19131 +static inline void au_set_ivdir(struct inode *inode, struct au_vdir *vdir)
19132 +{
19133 +       IiMustWriteLock(inode);
19134 +       au_ii(inode)->ii_vdir = vdir;
19135 +}
19136 +
19137 +static inline struct au_hinode *au_hi(struct inode *inode, aufs_bindex_t bindex)
19138 +{
19139 +       IiMustAnyLock(inode);
19140 +       return au_hinode(au_ii(inode), bindex);
19141 +}
19142 +
19143 +/* ---------------------------------------------------------------------- */
19144 +
19145 +static inline struct dentry *au_pinned_parent(struct au_pin *pin)
19146 +{
19147 +       if (pin)
19148 +               return pin->parent;
19149 +       return NULL;
19150 +}
19151 +
19152 +static inline struct inode *au_pinned_h_dir(struct au_pin *pin)
19153 +{
19154 +       if (pin && pin->hdir)
19155 +               return pin->hdir->hi_inode;
19156 +       return NULL;
19157 +}
19158 +
19159 +static inline struct au_hinode *au_pinned_hdir(struct au_pin *pin)
19160 +{
19161 +       if (pin)
19162 +               return pin->hdir;
19163 +       return NULL;
19164 +}
19165 +
19166 +static inline void au_pin_set_dentry(struct au_pin *pin, struct dentry *dentry)
19167 +{
19168 +       if (pin)
19169 +               pin->dentry = dentry;
19170 +}
19171 +
19172 +static inline void au_pin_set_parent_lflag(struct au_pin *pin,
19173 +                                          unsigned char lflag)
19174 +{
19175 +       if (pin) {
19176 +               if (lflag)
19177 +                       au_fset_pin(pin->flags, DI_LOCKED);
19178 +               else
19179 +                       au_fclr_pin(pin->flags, DI_LOCKED);
19180 +       }
19181 +}
19182 +
19183 +#if 0 /* reserved */
19184 +static inline void au_pin_set_parent(struct au_pin *pin, struct dentry *parent)
19185 +{
19186 +       if (pin) {
19187 +               dput(pin->parent);
19188 +               pin->parent = dget(parent);
19189 +       }
19190 +}
19191 +#endif
19192 +
19193 +/* ---------------------------------------------------------------------- */
19194 +
19195 +struct au_branch;
19196 +#ifdef CONFIG_AUFS_HNOTIFY
19197 +struct au_hnotify_op {
19198 +       void (*ctl)(struct au_hinode *hinode, int do_set);
19199 +       int (*alloc)(struct au_hinode *hinode);
19200 +
19201 +       /*
19202 +        * if it returns true, the caller should free hinode->hi_notify,
19203 +        * otherwise ->free() frees it.
19204 +        */
19205 +       int (*free)(struct au_hinode *hinode,
19206 +                   struct au_hnotify *hn) __must_check;
19207 +
19208 +       void (*fin)(void);
19209 +       int (*init)(void);
19210 +
19211 +       int (*reset_br)(unsigned int udba, struct au_branch *br, int perm);
19212 +       void (*fin_br)(struct au_branch *br);
19213 +       int (*init_br)(struct au_branch *br, int perm);
19214 +};
19215 +
19216 +/* hnotify.c */
19217 +int au_hn_alloc(struct au_hinode *hinode, struct inode *inode);
19218 +void au_hn_free(struct au_hinode *hinode);
19219 +void au_hn_ctl(struct au_hinode *hinode, int do_set);
19220 +void au_hn_reset(struct inode *inode, unsigned int flags);
19221 +int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
19222 +              const struct qstr *h_child_qstr, struct inode *h_child_inode);
19223 +int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm);
19224 +int au_hnotify_init_br(struct au_branch *br, int perm);
19225 +void au_hnotify_fin_br(struct au_branch *br);
19226 +int __init au_hnotify_init(void);
19227 +void au_hnotify_fin(void);
19228 +
19229 +/* hfsnotify.c */
19230 +extern const struct au_hnotify_op au_hnotify_op;
19231 +
19232 +static inline
19233 +void au_hn_init(struct au_hinode *hinode)
19234 +{
19235 +       hinode->hi_notify = NULL;
19236 +}
19237 +
19238 +static inline struct au_hnotify *au_hn(struct au_hinode *hinode)
19239 +{
19240 +       return hinode->hi_notify;
19241 +}
19242 +
19243 +#else
19244 +AuStub(int, au_hn_alloc, return -EOPNOTSUPP,
19245 +       struct au_hinode *hinode __maybe_unused,
19246 +       struct inode *inode __maybe_unused)
19247 +AuStub(struct au_hnotify *, au_hn, return NULL, struct au_hinode *hinode)
19248 +AuStubVoid(au_hn_free, struct au_hinode *hinode __maybe_unused)
19249 +AuStubVoid(au_hn_ctl, struct au_hinode *hinode __maybe_unused,
19250 +          int do_set __maybe_unused)
19251 +AuStubVoid(au_hn_reset, struct inode *inode __maybe_unused,
19252 +          unsigned int flags __maybe_unused)
19253 +AuStubInt0(au_hnotify_reset_br, unsigned int udba __maybe_unused,
19254 +          struct au_branch *br __maybe_unused,
19255 +          int perm __maybe_unused)
19256 +AuStubInt0(au_hnotify_init_br, struct au_branch *br __maybe_unused,
19257 +          int perm __maybe_unused)
19258 +AuStubVoid(au_hnotify_fin_br, struct au_branch *br __maybe_unused)
19259 +AuStubInt0(__init au_hnotify_init, void)
19260 +AuStubVoid(au_hnotify_fin, void)
19261 +AuStubVoid(au_hn_init, struct au_hinode *hinode __maybe_unused)
19262 +#endif /* CONFIG_AUFS_HNOTIFY */
19263 +
19264 +static inline void au_hn_suspend(struct au_hinode *hdir)
19265 +{
19266 +       au_hn_ctl(hdir, /*do_set*/0);
19267 +}
19268 +
19269 +static inline void au_hn_resume(struct au_hinode *hdir)
19270 +{
19271 +       au_hn_ctl(hdir, /*do_set*/1);
19272 +}
19273 +
19274 +static inline void au_hn_inode_lock(struct au_hinode *hdir)
19275 +{
19276 +       inode_lock(hdir->hi_inode);
19277 +       au_hn_suspend(hdir);
19278 +}
19279 +
19280 +static inline void au_hn_inode_lock_nested(struct au_hinode *hdir,
19281 +                                         unsigned int sc __maybe_unused)
19282 +{
19283 +       inode_lock_nested(hdir->hi_inode, sc);
19284 +       au_hn_suspend(hdir);
19285 +}
19286 +
19287 +#if 0 /* unused */
19288 +#include "vfsub.h"
19289 +static inline void au_hn_inode_lock_shared_nested(struct au_hinode *hdir,
19290 +                                                 unsigned int sc)
19291 +{
19292 +       inode_lock_shared_nested(hdir->hi_inode, sc);
19293 +       au_hn_suspend(hdir);
19294 +}
19295 +#endif
19296 +
19297 +static inline void au_hn_inode_unlock(struct au_hinode *hdir)
19298 +{
19299 +       au_hn_resume(hdir);
19300 +       inode_unlock(hdir->hi_inode);
19301 +}
19302 +
19303 +#endif /* __KERNEL__ */
19304 +#endif /* __AUFS_INODE_H__ */
19305 diff -urN /usr/share/empty/fs/aufs/ioctl.c linux/fs/aufs/ioctl.c
19306 --- /usr/share/empty/fs/aufs/ioctl.c    1970-01-01 01:00:00.000000000 +0100
19307 +++ linux/fs/aufs/ioctl.c       2021-11-01 23:48:34.209692595 +0100
19308 @@ -0,0 +1,220 @@
19309 +// SPDX-License-Identifier: GPL-2.0
19310 +/*
19311 + * Copyright (C) 2005-2021 Junjiro R. Okajima
19312 + *
19313 + * This program, aufs is free software; you can redistribute it and/or modify
19314 + * it under the terms of the GNU General Public License as published by
19315 + * the Free Software Foundation; either version 2 of the License, or
19316 + * (at your option) any later version.
19317 + *
19318 + * This program is distributed in the hope that it will be useful,
19319 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
19320 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19321 + * GNU General Public License for more details.
19322 + *
19323 + * You should have received a copy of the GNU General Public License
19324 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19325 + */
19326 +
19327 +/*
19328 + * ioctl
19329 + * plink-management and readdir in userspace.
19330 + * assist the pathconf(3) wrapper library.
19331 + * move-down
19332 + * File-based Hierarchical Storage Management.
19333 + */
19334 +
19335 +#include <linux/compat.h>
19336 +#include <linux/file.h>
19337 +#include "aufs.h"
19338 +
19339 +static int au_wbr_fd(struct path *path, struct aufs_wbr_fd __user *arg)
19340 +{
19341 +       int err, fd;
19342 +       aufs_bindex_t wbi, bindex, bbot;
19343 +       struct file *h_file;
19344 +       struct super_block *sb;
19345 +       struct dentry *root;
19346 +       struct au_branch *br;
19347 +       struct aufs_wbr_fd wbrfd = {
19348 +               .oflags = au_dir_roflags,
19349 +               .brid   = -1
19350 +       };
19351 +       const int valid = O_RDONLY | O_NONBLOCK | O_LARGEFILE | O_DIRECTORY
19352 +               | O_NOATIME | O_CLOEXEC;
19353 +
19354 +       AuDebugOn(wbrfd.oflags & ~valid);
19355 +
19356 +       if (arg) {
19357 +               err = copy_from_user(&wbrfd, arg, sizeof(wbrfd));
19358 +               if (unlikely(err)) {
19359 +                       err = -EFAULT;
19360 +                       goto out;
19361 +               }
19362 +
19363 +               err = -EINVAL;
19364 +               AuDbg("wbrfd{0%o, %d}\n", wbrfd.oflags, wbrfd.brid);
19365 +               wbrfd.oflags |= au_dir_roflags;
19366 +               AuDbg("0%o\n", wbrfd.oflags);
19367 +               if (unlikely(wbrfd.oflags & ~valid))
19368 +                       goto out;
19369 +       }
19370 +
19371 +       fd = get_unused_fd_flags(0);
19372 +       err = fd;
19373 +       if (unlikely(fd < 0))
19374 +               goto out;
19375 +
19376 +       h_file = ERR_PTR(-EINVAL);
19377 +       wbi = 0;
19378 +       br = NULL;
19379 +       sb = path->dentry->d_sb;
19380 +       root = sb->s_root;
19381 +       aufs_read_lock(root, AuLock_IR);
19382 +       bbot = au_sbbot(sb);
19383 +       if (wbrfd.brid >= 0) {
19384 +               wbi = au_br_index(sb, wbrfd.brid);
19385 +               if (unlikely(wbi < 0 || wbi > bbot))
19386 +                       goto out_unlock;
19387 +       }
19388 +
19389 +       h_file = ERR_PTR(-ENOENT);
19390 +       br = au_sbr(sb, wbi);
19391 +       if (!au_br_writable(br->br_perm)) {
19392 +               if (arg)
19393 +                       goto out_unlock;
19394 +
19395 +               bindex = wbi + 1;
19396 +               wbi = -1;
19397 +               for (; bindex <= bbot; bindex++) {
19398 +                       br = au_sbr(sb, bindex);
19399 +                       if (au_br_writable(br->br_perm)) {
19400 +                               wbi = bindex;
19401 +                               br = au_sbr(sb, wbi);
19402 +                               break;
19403 +                       }
19404 +               }
19405 +       }
19406 +       AuDbg("wbi %d\n", wbi);
19407 +       if (wbi >= 0)
19408 +               h_file = au_h_open(root, wbi, wbrfd.oflags, NULL,
19409 +                                  /*force_wr*/0);
19410 +
19411 +out_unlock:
19412 +       aufs_read_unlock(root, AuLock_IR);
19413 +       err = PTR_ERR(h_file);
19414 +       if (IS_ERR(h_file))
19415 +               goto out_fd;
19416 +
19417 +       au_lcnt_dec(&br->br_nfiles); /* cf. au_h_open() */
19418 +       fd_install(fd, h_file);
19419 +       err = fd;
19420 +       goto out; /* success */
19421 +
19422 +out_fd:
19423 +       put_unused_fd(fd);
19424 +out:
19425 +       AuTraceErr(err);
19426 +       return err;
19427 +}
19428 +
19429 +/* ---------------------------------------------------------------------- */
19430 +
19431 +long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg)
19432 +{
19433 +       long err;
19434 +       struct dentry *dentry;
19435 +
19436 +       switch (cmd) {
19437 +       case AUFS_CTL_RDU:
19438 +       case AUFS_CTL_RDU_INO:
19439 +               err = au_rdu_ioctl(file, cmd, arg);
19440 +               break;
19441 +
19442 +       case AUFS_CTL_WBR_FD:
19443 +               err = au_wbr_fd(&file->f_path, (void __user *)arg);
19444 +               break;
19445 +
19446 +       case AUFS_CTL_IBUSY:
19447 +               err = au_ibusy_ioctl(file, arg);
19448 +               break;
19449 +
19450 +       case AUFS_CTL_BRINFO:
19451 +               err = au_brinfo_ioctl(file, arg);
19452 +               break;
19453 +
19454 +       case AUFS_CTL_FHSM_FD:
19455 +               dentry = file->f_path.dentry;
19456 +               if (IS_ROOT(dentry))
19457 +                       err = au_fhsm_fd(dentry->d_sb, arg);
19458 +               else
19459 +                       err = -ENOTTY;
19460 +               break;
19461 +
19462 +       default:
19463 +               /* do not call the lower */
19464 +               AuDbg("0x%x\n", cmd);
19465 +               err = -ENOTTY;
19466 +       }
19467 +
19468 +       AuTraceErr(err);
19469 +       return err;
19470 +}
19471 +
19472 +long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg)
19473 +{
19474 +       long err;
19475 +
19476 +       switch (cmd) {
19477 +       case AUFS_CTL_MVDOWN:
19478 +               err = au_mvdown(file->f_path.dentry, (void __user *)arg);
19479 +               break;
19480 +
19481 +       case AUFS_CTL_WBR_FD:
19482 +               err = au_wbr_fd(&file->f_path, (void __user *)arg);
19483 +               break;
19484 +
19485 +       default:
19486 +               /* do not call the lower */
19487 +               AuDbg("0x%x\n", cmd);
19488 +               err = -ENOTTY;
19489 +       }
19490 +
19491 +       AuTraceErr(err);
19492 +       return err;
19493 +}
19494 +
19495 +#ifdef CONFIG_COMPAT
19496 +long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
19497 +                          unsigned long arg)
19498 +{
19499 +       long err;
19500 +
19501 +       switch (cmd) {
19502 +       case AUFS_CTL_RDU:
19503 +       case AUFS_CTL_RDU_INO:
19504 +               err = au_rdu_compat_ioctl(file, cmd, arg);
19505 +               break;
19506 +
19507 +       case AUFS_CTL_IBUSY:
19508 +               err = au_ibusy_compat_ioctl(file, arg);
19509 +               break;
19510 +
19511 +       case AUFS_CTL_BRINFO:
19512 +               err = au_brinfo_compat_ioctl(file, arg);
19513 +               break;
19514 +
19515 +       default:
19516 +               err = aufs_ioctl_dir(file, cmd, arg);
19517 +       }
19518 +
19519 +       AuTraceErr(err);
19520 +       return err;
19521 +}
19522 +
19523 +long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
19524 +                             unsigned long arg)
19525 +{
19526 +       return aufs_ioctl_nondir(file, cmd, (unsigned long)compat_ptr(arg));
19527 +}
19528 +#endif
19529 diff -urN /usr/share/empty/fs/aufs/i_op_add.c linux/fs/aufs/i_op_add.c
19530 --- /usr/share/empty/fs/aufs/i_op_add.c 1970-01-01 01:00:00.000000000 +0100
19531 +++ linux/fs/aufs/i_op_add.c    2021-11-01 23:48:34.209692595 +0100
19532 @@ -0,0 +1,941 @@
19533 +// SPDX-License-Identifier: GPL-2.0
19534 +/*
19535 + * Copyright (C) 2005-2021 Junjiro R. Okajima
19536 + *
19537 + * This program, aufs is free software; you can redistribute it and/or modify
19538 + * it under the terms of the GNU General Public License as published by
19539 + * the Free Software Foundation; either version 2 of the License, or
19540 + * (at your option) any later version.
19541 + *
19542 + * This program is distributed in the hope that it will be useful,
19543 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
19544 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19545 + * GNU General Public License for more details.
19546 + *
19547 + * You should have received a copy of the GNU General Public License
19548 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19549 + */
19550 +
19551 +/*
19552 + * inode operations (add entry)
19553 + */
19554 +
19555 +#include <linux/iversion.h>
19556 +#include "aufs.h"
19557 +
19558 +/*
19559 + * final procedure of adding a new entry, except link(2).
19560 + * remove whiteout, instantiate, copyup the parent dir's times and size
19561 + * and update version.
19562 + * if it failed, re-create the removed whiteout.
19563 + */
19564 +static int epilog(struct inode *dir, aufs_bindex_t bindex,
19565 +                 struct dentry *wh_dentry, struct dentry *dentry)
19566 +{
19567 +       int err, rerr;
19568 +       aufs_bindex_t bwh;
19569 +       struct path h_path;
19570 +       struct super_block *sb;
19571 +       struct inode *inode, *h_dir;
19572 +       struct dentry *wh;
19573 +
19574 +       bwh = -1;
19575 +       sb = dir->i_sb;
19576 +       if (wh_dentry) {
19577 +               h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */
19578 +               IMustLock(h_dir);
19579 +               AuDebugOn(au_h_iptr(dir, bindex) != h_dir);
19580 +               bwh = au_dbwh(dentry);
19581 +               h_path.dentry = wh_dentry;
19582 +               h_path.mnt = au_sbr_mnt(sb, bindex);
19583 +               err = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path,
19584 +                                         dentry);
19585 +               if (unlikely(err))
19586 +                       goto out;
19587 +       }
19588 +
19589 +       inode = au_new_inode(dentry, /*must_new*/1);
19590 +       if (!IS_ERR(inode)) {
19591 +               d_instantiate(dentry, inode);
19592 +               dir = d_inode(dentry->d_parent); /* dir inode is locked */
19593 +               IMustLock(dir);
19594 +               au_dir_ts(dir, bindex);
19595 +               inode_inc_iversion(dir);
19596 +               au_fhsm_wrote(sb, bindex, /*force*/0);
19597 +               return 0; /* success */
19598 +       }
19599 +
19600 +       err = PTR_ERR(inode);
19601 +       if (!wh_dentry)
19602 +               goto out;
19603 +
19604 +       /* revert */
19605 +       /* dir inode is locked */
19606 +       wh = au_wh_create(dentry, bwh, wh_dentry->d_parent);
19607 +       rerr = PTR_ERR(wh);
19608 +       if (IS_ERR(wh)) {
19609 +               AuIOErr("%pd reverting whiteout failed(%d, %d)\n",
19610 +                       dentry, err, rerr);
19611 +               err = -EIO;
19612 +       } else
19613 +               dput(wh);
19614 +
19615 +out:
19616 +       return err;
19617 +}
19618 +
19619 +static int au_d_may_add(struct dentry *dentry)
19620 +{
19621 +       int err;
19622 +
19623 +       err = 0;
19624 +       if (unlikely(d_unhashed(dentry)))
19625 +               err = -ENOENT;
19626 +       if (unlikely(d_really_is_positive(dentry)))
19627 +               err = -EEXIST;
19628 +       return err;
19629 +}
19630 +
19631 +/*
19632 + * simple tests for the adding inode operations.
19633 + * following the checks in vfs, plus the parent-child relationship.
19634 + */
19635 +int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
19636 +              struct dentry *h_parent, int isdir)
19637 +{
19638 +       int err;
19639 +       umode_t h_mode;
19640 +       struct dentry *h_dentry;
19641 +       struct inode *h_inode;
19642 +
19643 +       err = -ENAMETOOLONG;
19644 +       if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
19645 +               goto out;
19646 +
19647 +       h_dentry = au_h_dptr(dentry, bindex);
19648 +       if (d_really_is_negative(dentry)) {
19649 +               err = -EEXIST;
19650 +               if (unlikely(d_is_positive(h_dentry)))
19651 +                       goto out;
19652 +       } else {
19653 +               /* rename(2) case */
19654 +               err = -EIO;
19655 +               if (unlikely(d_is_negative(h_dentry)))
19656 +                       goto out;
19657 +               h_inode = d_inode(h_dentry);
19658 +               if (unlikely(!h_inode->i_nlink))
19659 +                       goto out;
19660 +
19661 +               h_mode = h_inode->i_mode;
19662 +               if (!isdir) {
19663 +                       err = -EISDIR;
19664 +                       if (unlikely(S_ISDIR(h_mode)))
19665 +                               goto out;
19666 +               } else if (unlikely(!S_ISDIR(h_mode))) {
19667 +                       err = -ENOTDIR;
19668 +                       goto out;
19669 +               }
19670 +       }
19671 +
19672 +       err = 0;
19673 +       /* expected parent dir is locked */
19674 +       if (unlikely(h_parent != h_dentry->d_parent))
19675 +               err = -EIO;
19676 +
19677 +out:
19678 +       AuTraceErr(err);
19679 +       return err;
19680 +}
19681 +
19682 +/*
19683 + * initial procedure of adding a new entry.
19684 + * prepare writable branch and the parent dir, lock it,
19685 + * and lookup whiteout for the new entry.
19686 + */
19687 +static struct dentry*
19688 +lock_hdir_lkup_wh(struct dentry *dentry, struct au_dtime *dt,
19689 +                 struct dentry *src_dentry, struct au_pin *pin,
19690 +                 struct au_wr_dir_args *wr_dir_args)
19691 +{
19692 +       struct dentry *wh_dentry, *h_parent;
19693 +       struct super_block *sb;
19694 +       struct au_branch *br;
19695 +       int err;
19696 +       unsigned int udba;
19697 +       aufs_bindex_t bcpup;
19698 +
19699 +       AuDbg("%pd\n", dentry);
19700 +
19701 +       err = au_wr_dir(dentry, src_dentry, wr_dir_args);
19702 +       bcpup = err;
19703 +       wh_dentry = ERR_PTR(err);
19704 +       if (unlikely(err < 0))
19705 +               goto out;
19706 +
19707 +       sb = dentry->d_sb;
19708 +       udba = au_opt_udba(sb);
19709 +       err = au_pin(pin, dentry, bcpup, udba,
19710 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
19711 +       wh_dentry = ERR_PTR(err);
19712 +       if (unlikely(err))
19713 +               goto out;
19714 +
19715 +       h_parent = au_pinned_h_parent(pin);
19716 +       if (udba != AuOpt_UDBA_NONE
19717 +           && au_dbtop(dentry) == bcpup)
19718 +               err = au_may_add(dentry, bcpup, h_parent,
19719 +                                au_ftest_wrdir(wr_dir_args->flags, ISDIR));
19720 +       else if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
19721 +               err = -ENAMETOOLONG;
19722 +       wh_dentry = ERR_PTR(err);
19723 +       if (unlikely(err))
19724 +               goto out_unpin;
19725 +
19726 +       br = au_sbr(sb, bcpup);
19727 +       if (dt) {
19728 +               struct path tmp = {
19729 +                       .dentry = h_parent,
19730 +                       .mnt    = au_br_mnt(br)
19731 +               };
19732 +               au_dtime_store(dt, au_pinned_parent(pin), &tmp);
19733 +       }
19734 +
19735 +       wh_dentry = NULL;
19736 +       if (bcpup != au_dbwh(dentry))
19737 +               goto out; /* success */
19738 +
19739 +       /*
19740 +        * ENAMETOOLONG here means that if we allowed create such name, then it
19741 +        * would not be able to removed in the future. So we don't allow such
19742 +        * name here and we don't handle ENAMETOOLONG differently here.
19743 +        */
19744 +       wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
19745 +
19746 +out_unpin:
19747 +       if (IS_ERR(wh_dentry))
19748 +               au_unpin(pin);
19749 +out:
19750 +       return wh_dentry;
19751 +}
19752 +
19753 +/* ---------------------------------------------------------------------- */
19754 +
19755 +enum { Mknod, Symlink, Creat };
19756 +struct simple_arg {
19757 +       int type;
19758 +       union {
19759 +               struct {
19760 +                       umode_t                 mode;
19761 +                       bool                    want_excl;
19762 +                       bool                    try_aopen;
19763 +                       struct vfsub_aopen_args *aopen;
19764 +               } c;
19765 +               struct {
19766 +                       const char *symname;
19767 +               } s;
19768 +               struct {
19769 +                       umode_t mode;
19770 +                       dev_t dev;
19771 +               } m;
19772 +       } u;
19773 +};
19774 +
19775 +static int add_simple(struct inode *dir, struct dentry *dentry,
19776 +                     struct simple_arg *arg)
19777 +{
19778 +       int err, rerr;
19779 +       aufs_bindex_t btop;
19780 +       unsigned char created;
19781 +       const unsigned char try_aopen
19782 +               = (arg->type == Creat && arg->u.c.try_aopen);
19783 +       struct vfsub_aopen_args *aopen = arg->u.c.aopen;
19784 +       struct dentry *wh_dentry, *parent;
19785 +       struct inode *h_dir;
19786 +       struct super_block *sb;
19787 +       struct au_branch *br;
19788 +       /* to reduce stack size */
19789 +       struct {
19790 +               struct au_dtime dt;
19791 +               struct au_pin pin;
19792 +               struct path h_path;
19793 +               struct au_wr_dir_args wr_dir_args;
19794 +       } *a;
19795 +
19796 +       AuDbg("%pd\n", dentry);
19797 +       IMustLock(dir);
19798 +
19799 +       err = -ENOMEM;
19800 +       a = kmalloc(sizeof(*a), GFP_NOFS);
19801 +       if (unlikely(!a))
19802 +               goto out;
19803 +       a->wr_dir_args.force_btgt = -1;
19804 +       a->wr_dir_args.flags = AuWrDir_ADD_ENTRY;
19805 +
19806 +       parent = dentry->d_parent; /* dir inode is locked */
19807 +       if (!try_aopen) {
19808 +               err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
19809 +               if (unlikely(err))
19810 +                       goto out_free;
19811 +       }
19812 +       err = au_d_may_add(dentry);
19813 +       if (unlikely(err))
19814 +               goto out_unlock;
19815 +       if (!try_aopen)
19816 +               di_write_lock_parent(parent);
19817 +       wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
19818 +                                     &a->pin, &a->wr_dir_args);
19819 +       err = PTR_ERR(wh_dentry);
19820 +       if (IS_ERR(wh_dentry))
19821 +               goto out_parent;
19822 +
19823 +       btop = au_dbtop(dentry);
19824 +       sb = dentry->d_sb;
19825 +       br = au_sbr(sb, btop);
19826 +       a->h_path.dentry = au_h_dptr(dentry, btop);
19827 +       a->h_path.mnt = au_br_mnt(br);
19828 +       h_dir = au_pinned_h_dir(&a->pin);
19829 +       switch (arg->type) {
19830 +       case Creat:
19831 +               if (!try_aopen || !h_dir->i_op->atomic_open) {
19832 +                       err = vfsub_create(h_dir, &a->h_path, arg->u.c.mode,
19833 +                                          arg->u.c.want_excl);
19834 +                       created = !err;
19835 +                       if (!err && try_aopen)
19836 +                               aopen->file->f_mode |= FMODE_CREATED;
19837 +               } else {
19838 +                       aopen->br = br;
19839 +                       err = vfsub_atomic_open(h_dir, a->h_path.dentry, aopen);
19840 +                       AuDbg("err %d\n", err);
19841 +                       AuDbgFile(aopen->file);
19842 +                       created = err >= 0
19843 +                               && !!(aopen->file->f_mode & FMODE_CREATED);
19844 +               }
19845 +               break;
19846 +       case Symlink:
19847 +               err = vfsub_symlink(h_dir, &a->h_path, arg->u.s.symname);
19848 +               created = !err;
19849 +               break;
19850 +       case Mknod:
19851 +               err = vfsub_mknod(h_dir, &a->h_path, arg->u.m.mode,
19852 +                                 arg->u.m.dev);
19853 +               created = !err;
19854 +               break;
19855 +       default:
19856 +               BUG();
19857 +       }
19858 +       if (unlikely(err < 0))
19859 +               goto out_unpin;
19860 +
19861 +       err = epilog(dir, btop, wh_dentry, dentry);
19862 +       if (!err)
19863 +               goto out_unpin; /* success */
19864 +
19865 +       /* revert */
19866 +       if (created /* && d_is_positive(a->h_path.dentry) */) {
19867 +               /* no delegation since it is just created */
19868 +               rerr = vfsub_unlink(h_dir, &a->h_path, /*delegated*/NULL,
19869 +                                   /*force*/0);
19870 +               if (rerr) {
19871 +                       AuIOErr("%pd revert failure(%d, %d)\n",
19872 +                               dentry, err, rerr);
19873 +                       err = -EIO;
19874 +               }
19875 +               au_dtime_revert(&a->dt);
19876 +       }
19877 +       if (try_aopen && h_dir->i_op->atomic_open
19878 +           && (aopen->file->f_mode & FMODE_OPENED))
19879 +               /* aopen->file is still opened */
19880 +               au_lcnt_dec(&aopen->br->br_nfiles);
19881 +
19882 +out_unpin:
19883 +       au_unpin(&a->pin);
19884 +       dput(wh_dentry);
19885 +out_parent:
19886 +       if (!try_aopen)
19887 +               di_write_unlock(parent);
19888 +out_unlock:
19889 +       if (unlikely(err)) {
19890 +               au_update_dbtop(dentry);
19891 +               d_drop(dentry);
19892 +       }
19893 +       if (!try_aopen)
19894 +               aufs_read_unlock(dentry, AuLock_DW);
19895 +out_free:
19896 +       au_kfree_rcu(a);
19897 +out:
19898 +       return err;
19899 +}
19900 +
19901 +int aufs_mknod(struct user_namespace *userns, struct inode *dir,
19902 +              struct dentry *dentry, umode_t mode, dev_t dev)
19903 +{
19904 +       struct simple_arg arg = {
19905 +               .type = Mknod,
19906 +               .u.m = {
19907 +                       .mode   = mode,
19908 +                       .dev    = dev
19909 +               }
19910 +       };
19911 +       return add_simple(dir, dentry, &arg);
19912 +}
19913 +
19914 +int aufs_symlink(struct user_namespace *userns, struct inode *dir,
19915 +                struct dentry *dentry, const char *symname)
19916 +{
19917 +       struct simple_arg arg = {
19918 +               .type = Symlink,
19919 +               .u.s.symname = symname
19920 +       };
19921 +       return add_simple(dir, dentry, &arg);
19922 +}
19923 +
19924 +int aufs_create(struct user_namespace *userns, struct inode *dir,
19925 +               struct dentry *dentry, umode_t mode, bool want_excl)
19926 +{
19927 +       struct simple_arg arg = {
19928 +               .type = Creat,
19929 +               .u.c = {
19930 +                       .mode           = mode,
19931 +                       .want_excl      = want_excl
19932 +               }
19933 +       };
19934 +       return add_simple(dir, dentry, &arg);
19935 +}
19936 +
19937 +int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
19938 +                      struct vfsub_aopen_args *aopen_args)
19939 +{
19940 +       struct simple_arg arg = {
19941 +               .type = Creat,
19942 +               .u.c = {
19943 +                       .mode           = aopen_args->create_mode,
19944 +                       .want_excl      = aopen_args->open_flag & O_EXCL,
19945 +                       .try_aopen      = true,
19946 +                       .aopen          = aopen_args
19947 +               }
19948 +       };
19949 +       return add_simple(dir, dentry, &arg);
19950 +}
19951 +
19952 +int aufs_tmpfile(struct user_namespace *userns, struct inode *dir,
19953 +                struct dentry *dentry, umode_t mode)
19954 +{
19955 +       int err;
19956 +       aufs_bindex_t bindex;
19957 +       struct super_block *sb;
19958 +       struct dentry *parent, *h_parent, *h_dentry;
19959 +       struct inode *h_dir, *inode;
19960 +       struct vfsmount *h_mnt;
19961 +       struct user_namespace *h_userns;
19962 +       struct au_wr_dir_args wr_dir_args = {
19963 +               .force_btgt     = -1,
19964 +               .flags          = AuWrDir_TMPFILE
19965 +       };
19966 +
19967 +       /* copy-up may happen */
19968 +       inode_lock(dir);
19969 +
19970 +       sb = dir->i_sb;
19971 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
19972 +       if (unlikely(err))
19973 +               goto out;
19974 +
19975 +       err = au_di_init(dentry);
19976 +       if (unlikely(err))
19977 +               goto out_si;
19978 +
19979 +       err = -EBUSY;
19980 +       parent = d_find_any_alias(dir);
19981 +       AuDebugOn(!parent);
19982 +       di_write_lock_parent(parent);
19983 +       if (unlikely(d_inode(parent) != dir))
19984 +               goto out_parent;
19985 +
19986 +       err = au_digen_test(parent, au_sigen(sb));
19987 +       if (unlikely(err))
19988 +               goto out_parent;
19989 +
19990 +       bindex = au_dbtop(parent);
19991 +       au_set_dbtop(dentry, bindex);
19992 +       au_set_dbbot(dentry, bindex);
19993 +       err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
19994 +       bindex = err;
19995 +       if (unlikely(err < 0))
19996 +               goto out_parent;
19997 +
19998 +       err = -EOPNOTSUPP;
19999 +       h_dir = au_h_iptr(dir, bindex);
20000 +       if (unlikely(!h_dir->i_op->tmpfile))
20001 +               goto out_parent;
20002 +
20003 +       h_mnt = au_sbr_mnt(sb, bindex);
20004 +       err = vfsub_mnt_want_write(h_mnt);
20005 +       if (unlikely(err))
20006 +               goto out_parent;
20007 +
20008 +       h_userns = mnt_user_ns(h_mnt);
20009 +       h_parent = au_h_dptr(parent, bindex);
20010 +       h_dentry = vfs_tmpfile(h_userns, h_parent, mode, /*open_flag*/0);
20011 +       if (IS_ERR(h_dentry)) {
20012 +               err = PTR_ERR(h_dentry);
20013 +               goto out_mnt;
20014 +       }
20015 +
20016 +       au_set_dbtop(dentry, bindex);
20017 +       au_set_dbbot(dentry, bindex);
20018 +       au_set_h_dptr(dentry, bindex, dget(h_dentry));
20019 +       inode = au_new_inode(dentry, /*must_new*/1);
20020 +       if (IS_ERR(inode)) {
20021 +               err = PTR_ERR(inode);
20022 +               au_set_h_dptr(dentry, bindex, NULL);
20023 +               au_set_dbtop(dentry, -1);
20024 +               au_set_dbbot(dentry, -1);
20025 +       } else {
20026 +               if (!inode->i_nlink)
20027 +                       set_nlink(inode, 1);
20028 +               d_tmpfile(dentry, inode);
20029 +               au_di(dentry)->di_tmpfile = 1;
20030 +
20031 +               /* update without i_mutex */
20032 +               if (au_ibtop(dir) == au_dbtop(dentry))
20033 +                       au_cpup_attr_timesizes(dir);
20034 +       }
20035 +       dput(h_dentry);
20036 +
20037 +out_mnt:
20038 +       vfsub_mnt_drop_write(h_mnt);
20039 +out_parent:
20040 +       di_write_unlock(parent);
20041 +       dput(parent);
20042 +       di_write_unlock(dentry);
20043 +       if (unlikely(err)) {
20044 +               au_di_fin(dentry);
20045 +               dentry->d_fsdata = NULL;
20046 +       }
20047 +out_si:
20048 +       si_read_unlock(sb);
20049 +out:
20050 +       inode_unlock(dir);
20051 +       return err;
20052 +}
20053 +
20054 +/* ---------------------------------------------------------------------- */
20055 +
20056 +struct au_link_args {
20057 +       aufs_bindex_t bdst, bsrc;
20058 +       struct au_pin pin;
20059 +       struct path h_path;
20060 +       struct dentry *src_parent, *parent;
20061 +};
20062 +
20063 +static int au_cpup_before_link(struct dentry *src_dentry,
20064 +                              struct au_link_args *a)
20065 +{
20066 +       int err;
20067 +       struct dentry *h_src_dentry;
20068 +       struct au_cp_generic cpg = {
20069 +               .dentry = src_dentry,
20070 +               .bdst   = a->bdst,
20071 +               .bsrc   = a->bsrc,
20072 +               .len    = -1,
20073 +               .pin    = &a->pin,
20074 +               .flags  = AuCpup_DTIME | AuCpup_HOPEN /* | AuCpup_KEEPLINO */
20075 +       };
20076 +
20077 +       di_read_lock_parent(a->src_parent, AuLock_IR);
20078 +       err = au_test_and_cpup_dirs(src_dentry, a->bdst);
20079 +       if (unlikely(err))
20080 +               goto out;
20081 +
20082 +       h_src_dentry = au_h_dptr(src_dentry, a->bsrc);
20083 +       err = au_pin(&a->pin, src_dentry, a->bdst,
20084 +                    au_opt_udba(src_dentry->d_sb),
20085 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
20086 +       if (unlikely(err))
20087 +               goto out;
20088 +
20089 +       err = au_sio_cpup_simple(&cpg);
20090 +       au_unpin(&a->pin);
20091 +
20092 +out:
20093 +       di_read_unlock(a->src_parent, AuLock_IR);
20094 +       return err;
20095 +}
20096 +
20097 +static int au_cpup_or_link(struct dentry *src_dentry, struct dentry *dentry,
20098 +                          struct au_link_args *a)
20099 +{
20100 +       int err;
20101 +       unsigned char plink;
20102 +       aufs_bindex_t bbot;
20103 +       struct dentry *h_src_dentry;
20104 +       struct inode *h_inode, *inode, *delegated;
20105 +       struct super_block *sb;
20106 +       struct file *h_file;
20107 +
20108 +       plink = 0;
20109 +       h_inode = NULL;
20110 +       sb = src_dentry->d_sb;
20111 +       inode = d_inode(src_dentry);
20112 +       if (au_ibtop(inode) <= a->bdst)
20113 +               h_inode = au_h_iptr(inode, a->bdst);
20114 +       if (!h_inode || !h_inode->i_nlink) {
20115 +               /* copyup src_dentry as the name of dentry. */
20116 +               bbot = au_dbbot(dentry);
20117 +               if (bbot < a->bsrc)
20118 +                       au_set_dbbot(dentry, a->bsrc);
20119 +               au_set_h_dptr(dentry, a->bsrc,
20120 +                             dget(au_h_dptr(src_dentry, a->bsrc)));
20121 +               dget(a->h_path.dentry);
20122 +               au_set_h_dptr(dentry, a->bdst, NULL);
20123 +               AuDbg("temporary d_inode...\n");
20124 +               spin_lock(&dentry->d_lock);
20125 +               dentry->d_inode = d_inode(src_dentry); /* tmp */
20126 +               spin_unlock(&dentry->d_lock);
20127 +               h_file = au_h_open_pre(dentry, a->bsrc, /*force_wr*/0);
20128 +               if (IS_ERR(h_file))
20129 +                       err = PTR_ERR(h_file);
20130 +               else {
20131 +                       struct au_cp_generic cpg = {
20132 +                               .dentry = dentry,
20133 +                               .bdst   = a->bdst,
20134 +                               .bsrc   = -1,
20135 +                               .len    = -1,
20136 +                               .pin    = &a->pin,
20137 +                               .flags  = AuCpup_KEEPLINO
20138 +                       };
20139 +                       err = au_sio_cpup_simple(&cpg);
20140 +                       au_h_open_post(dentry, a->bsrc, h_file);
20141 +                       if (!err) {
20142 +                               dput(a->h_path.dentry);
20143 +                               a->h_path.dentry = au_h_dptr(dentry, a->bdst);
20144 +                       } else
20145 +                               au_set_h_dptr(dentry, a->bdst,
20146 +                                             a->h_path.dentry);
20147 +               }
20148 +               spin_lock(&dentry->d_lock);
20149 +               dentry->d_inode = NULL; /* restore */
20150 +               spin_unlock(&dentry->d_lock);
20151 +               AuDbg("temporary d_inode...done\n");
20152 +               au_set_h_dptr(dentry, a->bsrc, NULL);
20153 +               au_set_dbbot(dentry, bbot);
20154 +       } else {
20155 +               /* the inode of src_dentry already exists on a.bdst branch */
20156 +               h_src_dentry = d_find_alias(h_inode);
20157 +               if (!h_src_dentry && au_plink_test(inode)) {
20158 +                       plink = 1;
20159 +                       h_src_dentry = au_plink_lkup(inode, a->bdst);
20160 +                       err = PTR_ERR(h_src_dentry);
20161 +                       if (IS_ERR(h_src_dentry))
20162 +                               goto out;
20163 +
20164 +                       if (unlikely(d_is_negative(h_src_dentry))) {
20165 +                               dput(h_src_dentry);
20166 +                               h_src_dentry = NULL;
20167 +                       }
20168 +
20169 +               }
20170 +               if (h_src_dentry) {
20171 +                       delegated = NULL;
20172 +                       err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
20173 +                                        &a->h_path, &delegated);
20174 +                       if (unlikely(err == -EWOULDBLOCK)) {
20175 +                               pr_warn("cannot retry for NFSv4 delegation"
20176 +                                       " for an internal link\n");
20177 +                               iput(delegated);
20178 +                       }
20179 +                       dput(h_src_dentry);
20180 +               } else {
20181 +                       AuIOErr("no dentry found for hi%lu on b%d\n",
20182 +                               h_inode->i_ino, a->bdst);
20183 +                       err = -EIO;
20184 +               }
20185 +       }
20186 +
20187 +       if (!err && !plink)
20188 +               au_plink_append(inode, a->bdst, a->h_path.dentry);
20189 +
20190 +out:
20191 +       AuTraceErr(err);
20192 +       return err;
20193 +}
20194 +
20195 +int aufs_link(struct dentry *src_dentry, struct inode *dir,
20196 +             struct dentry *dentry)
20197 +{
20198 +       int err, rerr;
20199 +       struct au_dtime dt;
20200 +       struct au_link_args *a;
20201 +       struct dentry *wh_dentry, *h_src_dentry;
20202 +       struct inode *inode, *delegated;
20203 +       struct super_block *sb;
20204 +       struct au_wr_dir_args wr_dir_args = {
20205 +               /* .force_btgt  = -1, */
20206 +               .flags          = AuWrDir_ADD_ENTRY
20207 +       };
20208 +
20209 +       IMustLock(dir);
20210 +       inode = d_inode(src_dentry);
20211 +       IMustLock(inode);
20212 +
20213 +       err = -ENOMEM;
20214 +       a = kzalloc(sizeof(*a), GFP_NOFS);
20215 +       if (unlikely(!a))
20216 +               goto out;
20217 +
20218 +       a->parent = dentry->d_parent; /* dir inode is locked */
20219 +       err = aufs_read_and_write_lock2(dentry, src_dentry,
20220 +                                       AuLock_NOPLM | AuLock_GEN);
20221 +       if (unlikely(err))
20222 +               goto out_kfree;
20223 +       err = au_d_linkable(src_dentry);
20224 +       if (unlikely(err))
20225 +               goto out_unlock;
20226 +       err = au_d_may_add(dentry);
20227 +       if (unlikely(err))
20228 +               goto out_unlock;
20229 +
20230 +       a->src_parent = dget_parent(src_dentry);
20231 +       wr_dir_args.force_btgt = au_ibtop(inode);
20232 +
20233 +       di_write_lock_parent(a->parent);
20234 +       wr_dir_args.force_btgt = au_wbr(dentry, wr_dir_args.force_btgt);
20235 +       wh_dentry = lock_hdir_lkup_wh(dentry, &dt, src_dentry, &a->pin,
20236 +                                     &wr_dir_args);
20237 +       err = PTR_ERR(wh_dentry);
20238 +       if (IS_ERR(wh_dentry))
20239 +               goto out_parent;
20240 +
20241 +       err = 0;
20242 +       sb = dentry->d_sb;
20243 +       a->bdst = au_dbtop(dentry);
20244 +       a->h_path.dentry = au_h_dptr(dentry, a->bdst);
20245 +       a->h_path.mnt = au_sbr_mnt(sb, a->bdst);
20246 +       a->bsrc = au_ibtop(inode);
20247 +       h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
20248 +       if (!h_src_dentry && au_di(src_dentry)->di_tmpfile)
20249 +               h_src_dentry = dget(au_hi_wh(inode, a->bsrc));
20250 +       if (!h_src_dentry) {
20251 +               a->bsrc = au_dbtop(src_dentry);
20252 +               h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
20253 +               AuDebugOn(!h_src_dentry);
20254 +       } else if (IS_ERR(h_src_dentry)) {
20255 +               err = PTR_ERR(h_src_dentry);
20256 +               goto out_parent;
20257 +       }
20258 +
20259 +       /*
20260 +        * aufs doesn't touch the credential so
20261 +        * security_dentry_create_files_as() is unnecessary.
20262 +        */
20263 +       if (au_opt_test(au_mntflags(sb), PLINK)) {
20264 +               if (a->bdst < a->bsrc
20265 +                   /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */)
20266 +                       err = au_cpup_or_link(src_dentry, dentry, a);
20267 +               else {
20268 +                       delegated = NULL;
20269 +                       err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
20270 +                                        &a->h_path, &delegated);
20271 +                       if (unlikely(err == -EWOULDBLOCK)) {
20272 +                               pr_warn("cannot retry for NFSv4 delegation"
20273 +                                       " for an internal link\n");
20274 +                               iput(delegated);
20275 +                       }
20276 +               }
20277 +               dput(h_src_dentry);
20278 +       } else {
20279 +               /*
20280 +                * copyup src_dentry to the branch we process,
20281 +                * and then link(2) to it.
20282 +                */
20283 +               dput(h_src_dentry);
20284 +               if (a->bdst < a->bsrc
20285 +                   /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */) {
20286 +                       au_unpin(&a->pin);
20287 +                       di_write_unlock(a->parent);
20288 +                       err = au_cpup_before_link(src_dentry, a);
20289 +                       di_write_lock_parent(a->parent);
20290 +                       if (!err)
20291 +                               err = au_pin(&a->pin, dentry, a->bdst,
20292 +                                            au_opt_udba(sb),
20293 +                                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
20294 +                       if (unlikely(err))
20295 +                               goto out_wh;
20296 +               }
20297 +               if (!err) {
20298 +                       h_src_dentry = au_h_dptr(src_dentry, a->bdst);
20299 +                       err = -ENOENT;
20300 +                       if (h_src_dentry && d_is_positive(h_src_dentry)) {
20301 +                               delegated = NULL;
20302 +                               err = vfsub_link(h_src_dentry,
20303 +                                                au_pinned_h_dir(&a->pin),
20304 +                                                &a->h_path, &delegated);
20305 +                               if (unlikely(err == -EWOULDBLOCK)) {
20306 +                                       pr_warn("cannot retry"
20307 +                                               " for NFSv4 delegation"
20308 +                                               " for an internal link\n");
20309 +                                       iput(delegated);
20310 +                               }
20311 +                       }
20312 +               }
20313 +       }
20314 +       if (unlikely(err))
20315 +               goto out_unpin;
20316 +
20317 +       if (wh_dentry) {
20318 +               a->h_path.dentry = wh_dentry;
20319 +               err = au_wh_unlink_dentry(au_pinned_h_dir(&a->pin), &a->h_path,
20320 +                                         dentry);
20321 +               if (unlikely(err))
20322 +                       goto out_revert;
20323 +       }
20324 +
20325 +       au_dir_ts(dir, a->bdst);
20326 +       inode_inc_iversion(dir);
20327 +       inc_nlink(inode);
20328 +       inode->i_ctime = dir->i_ctime;
20329 +       d_instantiate(dentry, au_igrab(inode));
20330 +       if (d_unhashed(a->h_path.dentry))
20331 +               /* some filesystem calls d_drop() */
20332 +               d_drop(dentry);
20333 +       /* some filesystems consume an inode even hardlink */
20334 +       au_fhsm_wrote(sb, a->bdst, /*force*/0);
20335 +       goto out_unpin; /* success */
20336 +
20337 +out_revert:
20338 +       /* no delegation since it is just created */
20339 +       rerr = vfsub_unlink(au_pinned_h_dir(&a->pin), &a->h_path,
20340 +                           /*delegated*/NULL, /*force*/0);
20341 +       if (unlikely(rerr)) {
20342 +               AuIOErr("%pd reverting failed(%d, %d)\n", dentry, err, rerr);
20343 +               err = -EIO;
20344 +       }
20345 +       au_dtime_revert(&dt);
20346 +out_unpin:
20347 +       au_unpin(&a->pin);
20348 +out_wh:
20349 +       dput(wh_dentry);
20350 +out_parent:
20351 +       di_write_unlock(a->parent);
20352 +       dput(a->src_parent);
20353 +out_unlock:
20354 +       if (unlikely(err)) {
20355 +               au_update_dbtop(dentry);
20356 +               d_drop(dentry);
20357 +       }
20358 +       aufs_read_and_write_unlock2(dentry, src_dentry);
20359 +out_kfree:
20360 +       au_kfree_rcu(a);
20361 +out:
20362 +       AuTraceErr(err);
20363 +       return err;
20364 +}
20365 +
20366 +int aufs_mkdir(struct user_namespace *userns, struct inode *dir,
20367 +              struct dentry *dentry, umode_t mode)
20368 +{
20369 +       int err, rerr;
20370 +       aufs_bindex_t bindex;
20371 +       unsigned char diropq;
20372 +       struct path h_path;
20373 +       struct dentry *wh_dentry, *parent, *opq_dentry;
20374 +       struct inode *h_inode;
20375 +       struct super_block *sb;
20376 +       struct {
20377 +               struct au_pin pin;
20378 +               struct au_dtime dt;
20379 +       } *a; /* reduce the stack usage */
20380 +       struct au_wr_dir_args wr_dir_args = {
20381 +               .force_btgt     = -1,
20382 +               .flags          = AuWrDir_ADD_ENTRY | AuWrDir_ISDIR
20383 +       };
20384 +
20385 +       IMustLock(dir);
20386 +
20387 +       err = -ENOMEM;
20388 +       a = kmalloc(sizeof(*a), GFP_NOFS);
20389 +       if (unlikely(!a))
20390 +               goto out;
20391 +
20392 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
20393 +       if (unlikely(err))
20394 +               goto out_free;
20395 +       err = au_d_may_add(dentry);
20396 +       if (unlikely(err))
20397 +               goto out_unlock;
20398 +
20399 +       parent = dentry->d_parent; /* dir inode is locked */
20400 +       di_write_lock_parent(parent);
20401 +       wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
20402 +                                     &a->pin, &wr_dir_args);
20403 +       err = PTR_ERR(wh_dentry);
20404 +       if (IS_ERR(wh_dentry))
20405 +               goto out_parent;
20406 +
20407 +       sb = dentry->d_sb;
20408 +       bindex = au_dbtop(dentry);
20409 +       h_path.dentry = au_h_dptr(dentry, bindex);
20410 +       h_path.mnt = au_sbr_mnt(sb, bindex);
20411 +       err = vfsub_mkdir(au_pinned_h_dir(&a->pin), &h_path, mode);
20412 +       if (unlikely(err))
20413 +               goto out_unpin;
20414 +
20415 +       /* make the dir opaque */
20416 +       diropq = 0;
20417 +       h_inode = d_inode(h_path.dentry);
20418 +       if (wh_dentry
20419 +           || au_opt_test(au_mntflags(sb), ALWAYS_DIROPQ)) {
20420 +               inode_lock_nested(h_inode, AuLsc_I_CHILD);
20421 +               opq_dentry = au_diropq_create(dentry, bindex);
20422 +               inode_unlock(h_inode);
20423 +               err = PTR_ERR(opq_dentry);
20424 +               if (IS_ERR(opq_dentry))
20425 +                       goto out_dir;
20426 +               dput(opq_dentry);
20427 +               diropq = 1;
20428 +       }
20429 +
20430 +       err = epilog(dir, bindex, wh_dentry, dentry);
20431 +       if (!err) {
20432 +               inc_nlink(dir);
20433 +               goto out_unpin; /* success */
20434 +       }
20435 +
20436 +       /* revert */
20437 +       if (diropq) {
20438 +               AuLabel(revert opq);
20439 +               inode_lock_nested(h_inode, AuLsc_I_CHILD);
20440 +               rerr = au_diropq_remove(dentry, bindex);
20441 +               inode_unlock(h_inode);
20442 +               if (rerr) {
20443 +                       AuIOErr("%pd reverting diropq failed(%d, %d)\n",
20444 +                               dentry, err, rerr);
20445 +                       err = -EIO;
20446 +               }
20447 +       }
20448 +
20449 +out_dir:
20450 +       AuLabel(revert dir);
20451 +       rerr = vfsub_rmdir(au_pinned_h_dir(&a->pin), &h_path);
20452 +       if (rerr) {
20453 +               AuIOErr("%pd reverting dir failed(%d, %d)\n",
20454 +                       dentry, err, rerr);
20455 +               err = -EIO;
20456 +       }
20457 +       au_dtime_revert(&a->dt);
20458 +out_unpin:
20459 +       au_unpin(&a->pin);
20460 +       dput(wh_dentry);
20461 +out_parent:
20462 +       di_write_unlock(parent);
20463 +out_unlock:
20464 +       if (unlikely(err)) {
20465 +               au_update_dbtop(dentry);
20466 +               d_drop(dentry);
20467 +       }
20468 +       aufs_read_unlock(dentry, AuLock_DW);
20469 +out_free:
20470 +       au_kfree_rcu(a);
20471 +out:
20472 +       return err;
20473 +}
20474 diff -urN /usr/share/empty/fs/aufs/i_op.c linux/fs/aufs/i_op.c
20475 --- /usr/share/empty/fs/aufs/i_op.c     1970-01-01 01:00:00.000000000 +0100
20476 +++ linux/fs/aufs/i_op.c        2021-11-01 23:48:34.209692595 +0100
20477 @@ -0,0 +1,1513 @@
20478 +// SPDX-License-Identifier: GPL-2.0
20479 +/*
20480 + * Copyright (C) 2005-2021 Junjiro R. Okajima
20481 + *
20482 + * This program, aufs is free software; you can redistribute it and/or modify
20483 + * it under the terms of the GNU General Public License as published by
20484 + * the Free Software Foundation; either version 2 of the License, or
20485 + * (at your option) any later version.
20486 + *
20487 + * This program is distributed in the hope that it will be useful,
20488 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
20489 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20490 + * GNU General Public License for more details.
20491 + *
20492 + * You should have received a copy of the GNU General Public License
20493 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20494 + */
20495 +
20496 +/*
20497 + * inode operations (except add/del/rename)
20498 + */
20499 +
20500 +#include <linux/device_cgroup.h>
20501 +#include <linux/fs_stack.h>
20502 +#include <linux/iversion.h>
20503 +#include <linux/namei.h>
20504 +#include <linux/security.h>
20505 +#include "aufs.h"
20506 +
20507 +static int h_permission(struct inode *h_inode, int mask,
20508 +                       struct path *h_path, int brperm)
20509 +{
20510 +       int err;
20511 +       const unsigned char write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
20512 +       struct user_namespace *h_userns;
20513 +
20514 +       err = -EPERM;
20515 +       if (write_mask && IS_IMMUTABLE(h_inode))
20516 +               goto out;
20517 +
20518 +       err = -EACCES;
20519 +       if (((mask & MAY_EXEC)
20520 +            && S_ISREG(h_inode->i_mode)
20521 +            && (path_noexec(h_path)
20522 +                || !(h_inode->i_mode & 0111))))
20523 +               goto out;
20524 +
20525 +       /*
20526 +        * - skip the lower fs test in the case of write to ro branch.
20527 +        * - nfs dir permission write check is optimized, but a policy for
20528 +        *   link/rename requires a real check.
20529 +        * - nfs always sets SB_POSIXACL regardless its mount option 'noacl.'
20530 +        *   in this case, generic_permission() returns -EOPNOTSUPP.
20531 +        */
20532 +       h_userns = mnt_user_ns(h_path->mnt);
20533 +       if ((write_mask && !au_br_writable(brperm))
20534 +           || (au_test_nfs(h_inode->i_sb) && S_ISDIR(h_inode->i_mode)
20535 +               && write_mask && !(mask & MAY_READ))
20536 +           || !h_inode->i_op->permission) {
20537 +               /* AuLabel(generic_permission); */
20538 +               /* AuDbg("get_acl %ps\n", h_inode->i_op->get_acl); */
20539 +               err = generic_permission(h_userns, h_inode, mask);
20540 +               if (err == -EOPNOTSUPP && au_test_nfs_noacl(h_inode))
20541 +                       err = h_inode->i_op->permission(h_userns, h_inode,
20542 +                                                       mask);
20543 +               AuTraceErr(err);
20544 +       } else {
20545 +               /* AuLabel(h_inode->permission); */
20546 +               err = h_inode->i_op->permission(h_userns, h_inode, mask);
20547 +               AuTraceErr(err);
20548 +       }
20549 +
20550 +       if (!err)
20551 +               err = devcgroup_inode_permission(h_inode, mask);
20552 +       if (!err)
20553 +               err = security_inode_permission(h_inode, mask);
20554 +
20555 +out:
20556 +       return err;
20557 +}
20558 +
20559 +static int aufs_permission(struct user_namespace *userns, struct inode *inode,
20560 +                          int mask)
20561 +{
20562 +       int err;
20563 +       aufs_bindex_t bindex, bbot;
20564 +       const unsigned char isdir = !!S_ISDIR(inode->i_mode),
20565 +               write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
20566 +       struct inode *h_inode;
20567 +       struct super_block *sb;
20568 +       struct au_branch *br;
20569 +
20570 +       /* todo: support rcu-walk? */
20571 +       if (mask & MAY_NOT_BLOCK)
20572 +               return -ECHILD;
20573 +
20574 +       sb = inode->i_sb;
20575 +       si_read_lock(sb, AuLock_FLUSH);
20576 +       ii_read_lock_child(inode);
20577 +#if 0 /* reserved for future use */
20578 +       /*
20579 +        * This test may be rather 'too much' since the test is essentially done
20580 +        * in the aufs_lookup().  Theoretically it is possible that the inode
20581 +        * generation doesn't match to the superblock's here.  But it isn't a
20582 +        * big deal I suppose.
20583 +        */
20584 +       err = au_iigen_test(inode, au_sigen(sb));
20585 +       if (unlikely(err))
20586 +               goto out;
20587 +#endif
20588 +
20589 +       if (!isdir
20590 +           || write_mask
20591 +           || au_opt_test(au_mntflags(sb), DIRPERM1)) {
20592 +               err = au_busy_or_stale();
20593 +               h_inode = au_h_iptr(inode, au_ibtop(inode));
20594 +               if (unlikely(!h_inode
20595 +                            || (h_inode->i_mode & S_IFMT)
20596 +                            != (inode->i_mode & S_IFMT)))
20597 +                       goto out;
20598 +
20599 +               err = 0;
20600 +               bindex = au_ibtop(inode);
20601 +               br = au_sbr(sb, bindex);
20602 +               err = h_permission(h_inode, mask, &br->br_path, br->br_perm);
20603 +               if (write_mask
20604 +                   && !err
20605 +                   && !special_file(h_inode->i_mode)) {
20606 +                       /* test whether the upper writable branch exists */
20607 +                       err = -EROFS;
20608 +                       for (; bindex >= 0; bindex--)
20609 +                               if (!au_br_rdonly(au_sbr(sb, bindex))) {
20610 +                                       err = 0;
20611 +                                       break;
20612 +                               }
20613 +               }
20614 +               goto out;
20615 +       }
20616 +
20617 +       /* non-write to dir */
20618 +       err = 0;
20619 +       bbot = au_ibbot(inode);
20620 +       for (bindex = au_ibtop(inode); !err && bindex <= bbot; bindex++) {
20621 +               h_inode = au_h_iptr(inode, bindex);
20622 +               if (h_inode) {
20623 +                       err = au_busy_or_stale();
20624 +                       if (unlikely(!S_ISDIR(h_inode->i_mode)))
20625 +                               break;
20626 +
20627 +                       br = au_sbr(sb, bindex);
20628 +                       err = h_permission(h_inode, mask, &br->br_path,
20629 +                                          br->br_perm);
20630 +               }
20631 +       }
20632 +
20633 +out:
20634 +       ii_read_unlock(inode);
20635 +       si_read_unlock(sb);
20636 +       return err;
20637 +}
20638 +
20639 +/* ---------------------------------------------------------------------- */
20640 +
20641 +static struct dentry *aufs_lookup(struct inode *dir, struct dentry *dentry,
20642 +                                 unsigned int flags)
20643 +{
20644 +       struct dentry *ret, *parent;
20645 +       struct inode *inode;
20646 +       struct super_block *sb;
20647 +       int err, npositive;
20648 +
20649 +       IMustLock(dir);
20650 +
20651 +       /* todo: support rcu-walk? */
20652 +       ret = ERR_PTR(-ECHILD);
20653 +       if (flags & LOOKUP_RCU)
20654 +               goto out;
20655 +
20656 +       ret = ERR_PTR(-ENAMETOOLONG);
20657 +       if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
20658 +               goto out;
20659 +
20660 +       sb = dir->i_sb;
20661 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
20662 +       ret = ERR_PTR(err);
20663 +       if (unlikely(err))
20664 +               goto out;
20665 +
20666 +       err = au_di_init(dentry);
20667 +       ret = ERR_PTR(err);
20668 +       if (unlikely(err))
20669 +               goto out_si;
20670 +
20671 +       inode = NULL;
20672 +       npositive = 0; /* suppress a warning */
20673 +       parent = dentry->d_parent; /* dir inode is locked */
20674 +       di_read_lock_parent(parent, AuLock_IR);
20675 +       err = au_alive_dir(parent);
20676 +       if (!err)
20677 +               err = au_digen_test(parent, au_sigen(sb));
20678 +       if (!err) {
20679 +               /* regardless LOOKUP_CREATE, always ALLOW_NEG */
20680 +               npositive = au_lkup_dentry(dentry, au_dbtop(parent),
20681 +                                          AuLkup_ALLOW_NEG);
20682 +               err = npositive;
20683 +       }
20684 +       di_read_unlock(parent, AuLock_IR);
20685 +       ret = ERR_PTR(err);
20686 +       if (unlikely(err < 0))
20687 +               goto out_unlock;
20688 +
20689 +       if (npositive) {
20690 +               inode = au_new_inode(dentry, /*must_new*/0);
20691 +               if (IS_ERR(inode)) {
20692 +                       ret = (void *)inode;
20693 +                       inode = NULL;
20694 +                       goto out_unlock;
20695 +               }
20696 +       }
20697 +
20698 +       if (inode)
20699 +               atomic_inc(&inode->i_count);
20700 +       ret = d_splice_alias(inode, dentry);
20701 +#if 0 /* reserved for future use */
20702 +       if (unlikely(d_need_lookup(dentry))) {
20703 +               spin_lock(&dentry->d_lock);
20704 +               dentry->d_flags &= ~DCACHE_NEED_LOOKUP;
20705 +               spin_unlock(&dentry->d_lock);
20706 +       } else
20707 +#endif
20708 +       if (inode) {
20709 +               if (!IS_ERR(ret)) {
20710 +                       iput(inode);
20711 +                       if (ret && ret != dentry)
20712 +                               ii_write_unlock(inode);
20713 +               } else {
20714 +                       ii_write_unlock(inode);
20715 +                       iput(inode);
20716 +                       inode = NULL;
20717 +               }
20718 +       }
20719 +
20720 +out_unlock:
20721 +       di_write_unlock(dentry);
20722 +out_si:
20723 +       si_read_unlock(sb);
20724 +out:
20725 +       return ret;
20726 +}
20727 +
20728 +/* ---------------------------------------------------------------------- */
20729 +
20730 +/*
20731 + * very dirty and complicated aufs ->atomic_open().
20732 + * aufs_atomic_open()
20733 + * + au_aopen_or_create()
20734 + *   + add_simple()
20735 + *     + vfsub_atomic_open()
20736 + *       + branch fs ->atomic_open()
20737 + *        may call the actual 'open' for h_file
20738 + *       + inc br_nfiles only if opened
20739 + * + au_aopen_no_open() or au_aopen_do_open()
20740 + *
20741 + * au_aopen_do_open()
20742 + * + finish_open()
20743 + *   + au_do_aopen()
20744 + *     + au_do_open() the body of all 'open'
20745 + *       + au_do_open_nondir()
20746 + *        set the passed h_file
20747 + *
20748 + * au_aopen_no_open()
20749 + * + finish_no_open()
20750 + */
20751 +
20752 +struct aopen_node {
20753 +       struct hlist_bl_node hblist;
20754 +       struct file *file, *h_file;
20755 +};
20756 +
20757 +static int au_do_aopen(struct inode *inode, struct file *file)
20758 +{
20759 +       struct hlist_bl_head *aopen;
20760 +       struct hlist_bl_node *pos;
20761 +       struct aopen_node *node;
20762 +       struct au_do_open_args args = {
20763 +               .aopen  = 1,
20764 +               .open   = au_do_open_nondir
20765 +       };
20766 +
20767 +       aopen = &au_sbi(inode->i_sb)->si_aopen;
20768 +       hlist_bl_lock(aopen);
20769 +       hlist_bl_for_each_entry(node, pos, aopen, hblist)
20770 +               if (node->file == file) {
20771 +                       args.h_file = node->h_file;
20772 +                       break;
20773 +               }
20774 +       hlist_bl_unlock(aopen);
20775 +       /* AuDebugOn(!args.h_file); */
20776 +
20777 +       return au_do_open(file, &args);
20778 +}
20779 +
20780 +static int au_aopen_do_open(struct file *file, struct dentry *dentry,
20781 +                           struct aopen_node *aopen_node)
20782 +{
20783 +       int err;
20784 +       struct hlist_bl_head *aopen;
20785 +
20786 +       AuLabel(here);
20787 +       aopen = &au_sbi(dentry->d_sb)->si_aopen;
20788 +       au_hbl_add(&aopen_node->hblist, aopen);
20789 +       err = finish_open(file, dentry, au_do_aopen);
20790 +       au_hbl_del(&aopen_node->hblist, aopen);
20791 +       /* AuDbgFile(file); */
20792 +       AuDbg("%pd%s%s\n", dentry,
20793 +             (file->f_mode & FMODE_CREATED) ? " created" : "",
20794 +             (file->f_mode & FMODE_OPENED) ? " opened" : "");
20795 +
20796 +       AuTraceErr(err);
20797 +       return err;
20798 +}
20799 +
20800 +static int au_aopen_no_open(struct file *file, struct dentry *dentry)
20801 +{
20802 +       int err;
20803 +
20804 +       AuLabel(here);
20805 +       dget(dentry);
20806 +       err = finish_no_open(file, dentry);
20807 +
20808 +       AuTraceErr(err);
20809 +       return err;
20810 +}
20811 +
20812 +static int aufs_atomic_open(struct inode *dir, struct dentry *dentry,
20813 +                           struct file *file, unsigned int open_flag,
20814 +                           umode_t create_mode)
20815 +{
20816 +       int err, did_open;
20817 +       unsigned int lkup_flags;
20818 +       aufs_bindex_t bindex;
20819 +       struct super_block *sb;
20820 +       struct dentry *parent, *d;
20821 +       struct vfsub_aopen_args args = {
20822 +               .open_flag      = open_flag,
20823 +               .create_mode    = create_mode
20824 +       };
20825 +       struct aopen_node aopen_node = {
20826 +               .file   = file
20827 +       };
20828 +
20829 +       IMustLock(dir);
20830 +       AuDbg("open_flag 0%o\n", open_flag);
20831 +       AuDbgDentry(dentry);
20832 +
20833 +       err = 0;
20834 +       if (!au_di(dentry)) {
20835 +               lkup_flags = LOOKUP_OPEN;
20836 +               if (open_flag & O_CREAT)
20837 +                       lkup_flags |= LOOKUP_CREATE;
20838 +               d = aufs_lookup(dir, dentry, lkup_flags);
20839 +               if (IS_ERR(d)) {
20840 +                       err = PTR_ERR(d);
20841 +                       AuTraceErr(err);
20842 +                       goto out;
20843 +               } else if (d) {
20844 +                       /*
20845 +                        * obsoleted dentry found.
20846 +                        * another error will be returned later.
20847 +                        */
20848 +                       d_drop(d);
20849 +                       AuDbgDentry(d);
20850 +                       dput(d);
20851 +               }
20852 +               AuDbgDentry(dentry);
20853 +       }
20854 +
20855 +       if (d_is_positive(dentry)
20856 +           || d_unhashed(dentry)
20857 +           || d_unlinked(dentry)
20858 +           || !(open_flag & O_CREAT)) {
20859 +               err = au_aopen_no_open(file, dentry);
20860 +               goto out; /* success */
20861 +       }
20862 +
20863 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
20864 +       if (unlikely(err))
20865 +               goto out;
20866 +
20867 +       sb = dentry->d_sb;
20868 +       parent = dentry->d_parent;      /* dir is locked */
20869 +       di_write_lock_parent(parent);
20870 +       err = au_lkup_dentry(dentry, /*btop*/0, AuLkup_ALLOW_NEG);
20871 +       if (unlikely(err < 0))
20872 +               goto out_parent;
20873 +
20874 +       AuDbgDentry(dentry);
20875 +       if (d_is_positive(dentry)) {
20876 +               err = au_aopen_no_open(file, dentry);
20877 +               goto out_parent; /* success */
20878 +       }
20879 +
20880 +       args.file = alloc_empty_file(file->f_flags, current_cred());
20881 +       err = PTR_ERR(args.file);
20882 +       if (IS_ERR(args.file))
20883 +               goto out_parent;
20884 +
20885 +       bindex = au_dbtop(dentry);
20886 +       err = au_aopen_or_create(dir, dentry, &args);
20887 +       AuTraceErr(err);
20888 +       AuDbgFile(args.file);
20889 +       file->f_mode = args.file->f_mode & ~FMODE_OPENED;
20890 +       did_open = !!(args.file->f_mode & FMODE_OPENED);
20891 +       if (!did_open) {
20892 +               fput(args.file);
20893 +               args.file = NULL;
20894 +       }
20895 +       di_write_unlock(parent);
20896 +       di_write_unlock(dentry);
20897 +       if (unlikely(err < 0)) {
20898 +               if (args.file)
20899 +                       fput(args.file);
20900 +               goto out_sb;
20901 +       }
20902 +
20903 +       if (!did_open)
20904 +               err = au_aopen_no_open(file, dentry);
20905 +       else {
20906 +               aopen_node.h_file = args.file;
20907 +               err = au_aopen_do_open(file, dentry, &aopen_node);
20908 +       }
20909 +       if (unlikely(err < 0)) {
20910 +               if (args.file)
20911 +                       fput(args.file);
20912 +               if (did_open)
20913 +                       au_lcnt_dec(&args.br->br_nfiles);
20914 +       }
20915 +       goto out_sb; /* success */
20916 +
20917 +out_parent:
20918 +       di_write_unlock(parent);
20919 +       di_write_unlock(dentry);
20920 +out_sb:
20921 +       si_read_unlock(sb);
20922 +out:
20923 +       AuTraceErr(err);
20924 +       AuDbgFile(file);
20925 +       return err;
20926 +}
20927 +
20928 +
20929 +/* ---------------------------------------------------------------------- */
20930 +
20931 +static int au_wr_dir_cpup(struct dentry *dentry, struct dentry *parent,
20932 +                         const unsigned char add_entry, aufs_bindex_t bcpup,
20933 +                         aufs_bindex_t btop)
20934 +{
20935 +       int err;
20936 +       struct dentry *h_parent;
20937 +       struct inode *h_dir;
20938 +
20939 +       if (add_entry)
20940 +               IMustLock(d_inode(parent));
20941 +       else
20942 +               di_write_lock_parent(parent);
20943 +
20944 +       err = 0;
20945 +       if (!au_h_dptr(parent, bcpup)) {
20946 +               if (btop > bcpup)
20947 +                       err = au_cpup_dirs(dentry, bcpup);
20948 +               else if (btop < bcpup)
20949 +                       err = au_cpdown_dirs(dentry, bcpup);
20950 +               else
20951 +                       BUG();
20952 +       }
20953 +       if (!err && add_entry && !au_ftest_wrdir(add_entry, TMPFILE)) {
20954 +               h_parent = au_h_dptr(parent, bcpup);
20955 +               h_dir = d_inode(h_parent);
20956 +               inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
20957 +               err = au_lkup_neg(dentry, bcpup, /*wh*/0);
20958 +               /* todo: no unlock here */
20959 +               inode_unlock_shared(h_dir);
20960 +
20961 +               AuDbg("bcpup %d\n", bcpup);
20962 +               if (!err) {
20963 +                       if (d_really_is_negative(dentry))
20964 +                               au_set_h_dptr(dentry, btop, NULL);
20965 +                       au_update_dbrange(dentry, /*do_put_zero*/0);
20966 +               }
20967 +       }
20968 +
20969 +       if (!add_entry)
20970 +               di_write_unlock(parent);
20971 +       if (!err)
20972 +               err = bcpup; /* success */
20973 +
20974 +       AuTraceErr(err);
20975 +       return err;
20976 +}
20977 +
20978 +/*
20979 + * decide the branch and the parent dir where we will create a new entry.
20980 + * returns new bindex or an error.
20981 + * copyup the parent dir if needed.
20982 + */
20983 +int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
20984 +             struct au_wr_dir_args *args)
20985 +{
20986 +       int err;
20987 +       unsigned int flags;
20988 +       aufs_bindex_t bcpup, btop, src_btop;
20989 +       const unsigned char add_entry
20990 +               = au_ftest_wrdir(args->flags, ADD_ENTRY)
20991 +               | au_ftest_wrdir(args->flags, TMPFILE);
20992 +       struct super_block *sb;
20993 +       struct dentry *parent;
20994 +       struct au_sbinfo *sbinfo;
20995 +
20996 +       sb = dentry->d_sb;
20997 +       sbinfo = au_sbi(sb);
20998 +       parent = dget_parent(dentry);
20999 +       btop = au_dbtop(dentry);
21000 +       bcpup = btop;
21001 +       if (args->force_btgt < 0) {
21002 +               if (src_dentry) {
21003 +                       src_btop = au_dbtop(src_dentry);
21004 +                       if (src_btop < btop)
21005 +                               bcpup = src_btop;
21006 +               } else if (add_entry) {
21007 +                       flags = 0;
21008 +                       if (au_ftest_wrdir(args->flags, ISDIR))
21009 +                               au_fset_wbr(flags, DIR);
21010 +                       err = AuWbrCreate(sbinfo, dentry, flags);
21011 +                       bcpup = err;
21012 +               }
21013 +
21014 +               if (bcpup < 0 || au_test_ro(sb, bcpup, d_inode(dentry))) {
21015 +                       if (add_entry)
21016 +                               err = AuWbrCopyup(sbinfo, dentry);
21017 +                       else {
21018 +                               if (!IS_ROOT(dentry)) {
21019 +                                       di_read_lock_parent(parent, !AuLock_IR);
21020 +                                       err = AuWbrCopyup(sbinfo, dentry);
21021 +                                       di_read_unlock(parent, !AuLock_IR);
21022 +                               } else
21023 +                                       err = AuWbrCopyup(sbinfo, dentry);
21024 +                       }
21025 +                       bcpup = err;
21026 +                       if (unlikely(err < 0))
21027 +                               goto out;
21028 +               }
21029 +       } else {
21030 +               bcpup = args->force_btgt;
21031 +               AuDebugOn(au_test_ro(sb, bcpup, d_inode(dentry)));
21032 +       }
21033 +
21034 +       AuDbg("btop %d, bcpup %d\n", btop, bcpup);
21035 +       err = bcpup;
21036 +       if (bcpup == btop)
21037 +               goto out; /* success */
21038 +
21039 +       /* copyup the new parent into the branch we process */
21040 +       err = au_wr_dir_cpup(dentry, parent, add_entry, bcpup, btop);
21041 +       if (err >= 0) {
21042 +               if (d_really_is_negative(dentry)) {
21043 +                       au_set_h_dptr(dentry, btop, NULL);
21044 +                       au_set_dbtop(dentry, bcpup);
21045 +                       au_set_dbbot(dentry, bcpup);
21046 +               }
21047 +               AuDebugOn(add_entry
21048 +                         && !au_ftest_wrdir(args->flags, TMPFILE)
21049 +                         && !au_h_dptr(dentry, bcpup));
21050 +       }
21051 +
21052 +out:
21053 +       dput(parent);
21054 +       return err;
21055 +}
21056 +
21057 +/* ---------------------------------------------------------------------- */
21058 +
21059 +void au_pin_hdir_unlock(struct au_pin *p)
21060 +{
21061 +       if (p->hdir)
21062 +               au_hn_inode_unlock(p->hdir);
21063 +}
21064 +
21065 +int au_pin_hdir_lock(struct au_pin *p)
21066 +{
21067 +       int err;
21068 +
21069 +       err = 0;
21070 +       if (!p->hdir)
21071 +               goto out;
21072 +
21073 +       /* even if an error happens later, keep this lock */
21074 +       au_hn_inode_lock_nested(p->hdir, p->lsc_hi);
21075 +
21076 +       err = -EBUSY;
21077 +       if (unlikely(p->hdir->hi_inode != d_inode(p->h_parent)))
21078 +               goto out;
21079 +
21080 +       err = 0;
21081 +       if (p->h_dentry)
21082 +               err = au_h_verify(p->h_dentry, p->udba, p->hdir->hi_inode,
21083 +                                 p->h_parent, p->br);
21084 +
21085 +out:
21086 +       return err;
21087 +}
21088 +
21089 +int au_pin_hdir_relock(struct au_pin *p)
21090 +{
21091 +       int err, i;
21092 +       struct inode *h_i;
21093 +       struct dentry *h_d[] = {
21094 +               p->h_dentry,
21095 +               p->h_parent
21096 +       };
21097 +
21098 +       err = au_pin_hdir_lock(p);
21099 +       if (unlikely(err))
21100 +               goto out;
21101 +
21102 +       for (i = 0; !err && i < sizeof(h_d)/sizeof(*h_d); i++) {
21103 +               if (!h_d[i])
21104 +                       continue;
21105 +               if (d_is_positive(h_d[i])) {
21106 +                       h_i = d_inode(h_d[i]);
21107 +                       err = !h_i->i_nlink;
21108 +               }
21109 +       }
21110 +
21111 +out:
21112 +       return err;
21113 +}
21114 +
21115 +static void au_pin_hdir_set_owner(struct au_pin *p, struct task_struct *task)
21116 +{
21117 +       atomic_long_set(&p->hdir->hi_inode->i_rwsem.owner, (long)task);
21118 +}
21119 +
21120 +void au_pin_hdir_acquire_nest(struct au_pin *p)
21121 +{
21122 +       if (p->hdir) {
21123 +               rwsem_acquire_nest(&p->hdir->hi_inode->i_rwsem.dep_map,
21124 +                                  p->lsc_hi, 0, NULL, _RET_IP_);
21125 +               au_pin_hdir_set_owner(p, current);
21126 +       }
21127 +}
21128 +
21129 +void au_pin_hdir_release(struct au_pin *p)
21130 +{
21131 +       if (p->hdir) {
21132 +               au_pin_hdir_set_owner(p, p->task);
21133 +               rwsem_release(&p->hdir->hi_inode->i_rwsem.dep_map, _RET_IP_);
21134 +       }
21135 +}
21136 +
21137 +struct dentry *au_pinned_h_parent(struct au_pin *pin)
21138 +{
21139 +       if (pin && pin->parent)
21140 +               return au_h_dptr(pin->parent, pin->bindex);
21141 +       return NULL;
21142 +}
21143 +
21144 +void au_unpin(struct au_pin *p)
21145 +{
21146 +       if (p->hdir)
21147 +               au_pin_hdir_unlock(p);
21148 +       if (p->h_mnt && au_ftest_pin(p->flags, MNT_WRITE))
21149 +               vfsub_mnt_drop_write(p->h_mnt);
21150 +       if (!p->hdir)
21151 +               return;
21152 +
21153 +       if (!au_ftest_pin(p->flags, DI_LOCKED))
21154 +               di_read_unlock(p->parent, AuLock_IR);
21155 +       iput(p->hdir->hi_inode);
21156 +       dput(p->parent);
21157 +       p->parent = NULL;
21158 +       p->hdir = NULL;
21159 +       p->h_mnt = NULL;
21160 +       /* do not clear p->task */
21161 +}
21162 +
21163 +int au_do_pin(struct au_pin *p)
21164 +{
21165 +       int err;
21166 +       struct super_block *sb;
21167 +       struct inode *h_dir;
21168 +
21169 +       err = 0;
21170 +       sb = p->dentry->d_sb;
21171 +       p->br = au_sbr(sb, p->bindex);
21172 +       if (IS_ROOT(p->dentry)) {
21173 +               if (au_ftest_pin(p->flags, MNT_WRITE)) {
21174 +                       p->h_mnt = au_br_mnt(p->br);
21175 +                       err = vfsub_mnt_want_write(p->h_mnt);
21176 +                       if (unlikely(err)) {
21177 +                               au_fclr_pin(p->flags, MNT_WRITE);
21178 +                               goto out_err;
21179 +                       }
21180 +               }
21181 +               goto out;
21182 +       }
21183 +
21184 +       p->h_dentry = NULL;
21185 +       if (p->bindex <= au_dbbot(p->dentry))
21186 +               p->h_dentry = au_h_dptr(p->dentry, p->bindex);
21187 +
21188 +       p->parent = dget_parent(p->dentry);
21189 +       if (!au_ftest_pin(p->flags, DI_LOCKED))
21190 +               di_read_lock(p->parent, AuLock_IR, p->lsc_di);
21191 +
21192 +       h_dir = NULL;
21193 +       p->h_parent = au_h_dptr(p->parent, p->bindex);
21194 +       p->hdir = au_hi(d_inode(p->parent), p->bindex);
21195 +       if (p->hdir)
21196 +               h_dir = p->hdir->hi_inode;
21197 +
21198 +       /*
21199 +        * udba case, or
21200 +        * if DI_LOCKED is not set, then p->parent may be different
21201 +        * and h_parent can be NULL.
21202 +        */
21203 +       if (unlikely(!p->hdir || !h_dir || !p->h_parent)) {
21204 +               err = -EBUSY;
21205 +               if (!au_ftest_pin(p->flags, DI_LOCKED))
21206 +                       di_read_unlock(p->parent, AuLock_IR);
21207 +               dput(p->parent);
21208 +               p->parent = NULL;
21209 +               goto out_err;
21210 +       }
21211 +
21212 +       if (au_ftest_pin(p->flags, MNT_WRITE)) {
21213 +               p->h_mnt = au_br_mnt(p->br);
21214 +               err = vfsub_mnt_want_write(p->h_mnt);
21215 +               if (unlikely(err)) {
21216 +                       au_fclr_pin(p->flags, MNT_WRITE);
21217 +                       if (!au_ftest_pin(p->flags, DI_LOCKED))
21218 +                               di_read_unlock(p->parent, AuLock_IR);
21219 +                       dput(p->parent);
21220 +                       p->parent = NULL;
21221 +                       goto out_err;
21222 +               }
21223 +       }
21224 +
21225 +       au_igrab(h_dir);
21226 +       err = au_pin_hdir_lock(p);
21227 +       if (!err)
21228 +               goto out; /* success */
21229 +
21230 +       au_unpin(p);
21231 +
21232 +out_err:
21233 +       pr_err("err %d\n", err);
21234 +       err = au_busy_or_stale();
21235 +out:
21236 +       return err;
21237 +}
21238 +
21239 +void au_pin_init(struct au_pin *p, struct dentry *dentry,
21240 +                aufs_bindex_t bindex, int lsc_di, int lsc_hi,
21241 +                unsigned int udba, unsigned char flags)
21242 +{
21243 +       p->dentry = dentry;
21244 +       p->udba = udba;
21245 +       p->lsc_di = lsc_di;
21246 +       p->lsc_hi = lsc_hi;
21247 +       p->flags = flags;
21248 +       p->bindex = bindex;
21249 +
21250 +       p->parent = NULL;
21251 +       p->hdir = NULL;
21252 +       p->h_mnt = NULL;
21253 +
21254 +       p->h_dentry = NULL;
21255 +       p->h_parent = NULL;
21256 +       p->br = NULL;
21257 +       p->task = current;
21258 +}
21259 +
21260 +int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
21261 +          unsigned int udba, unsigned char flags)
21262 +{
21263 +       au_pin_init(pin, dentry, bindex, AuLsc_DI_PARENT, AuLsc_I_PARENT2,
21264 +                   udba, flags);
21265 +       return au_do_pin(pin);
21266 +}
21267 +
21268 +/* ---------------------------------------------------------------------- */
21269 +
21270 +/*
21271 + * ->setattr() and ->getattr() are called in various cases.
21272 + * chmod, stat: dentry is revalidated.
21273 + * fchmod, fstat: file and dentry are not revalidated, additionally they may be
21274 + *               unhashed.
21275 + * for ->setattr(), ia->ia_file is passed from ftruncate only.
21276 + */
21277 +/* todo: consolidate with do_refresh() and simple_reval_dpath() */
21278 +int au_reval_for_attr(struct dentry *dentry, unsigned int sigen)
21279 +{
21280 +       int err;
21281 +       struct dentry *parent;
21282 +
21283 +       err = 0;
21284 +       if (au_digen_test(dentry, sigen)) {
21285 +               parent = dget_parent(dentry);
21286 +               di_read_lock_parent(parent, AuLock_IR);
21287 +               err = au_refresh_dentry(dentry, parent);
21288 +               di_read_unlock(parent, AuLock_IR);
21289 +               dput(parent);
21290 +       }
21291 +
21292 +       AuTraceErr(err);
21293 +       return err;
21294 +}
21295 +
21296 +int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
21297 +                    struct au_icpup_args *a)
21298 +{
21299 +       int err;
21300 +       loff_t sz;
21301 +       aufs_bindex_t btop, ibtop;
21302 +       struct dentry *hi_wh, *parent;
21303 +       struct inode *inode;
21304 +       struct au_wr_dir_args wr_dir_args = {
21305 +               .force_btgt     = -1,
21306 +               .flags          = 0
21307 +       };
21308 +
21309 +       if (d_is_dir(dentry))
21310 +               au_fset_wrdir(wr_dir_args.flags, ISDIR);
21311 +       /* plink or hi_wh() case */
21312 +       btop = au_dbtop(dentry);
21313 +       inode = d_inode(dentry);
21314 +       ibtop = au_ibtop(inode);
21315 +       if (btop != ibtop && !au_test_ro(inode->i_sb, ibtop, inode))
21316 +               wr_dir_args.force_btgt = ibtop;
21317 +       err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
21318 +       if (unlikely(err < 0))
21319 +               goto out;
21320 +       a->btgt = err;
21321 +       if (err != btop)
21322 +               au_fset_icpup(a->flags, DID_CPUP);
21323 +
21324 +       err = 0;
21325 +       a->pin_flags = AuPin_MNT_WRITE;
21326 +       parent = NULL;
21327 +       if (!IS_ROOT(dentry)) {
21328 +               au_fset_pin(a->pin_flags, DI_LOCKED);
21329 +               parent = dget_parent(dentry);
21330 +               di_write_lock_parent(parent);
21331 +       }
21332 +
21333 +       err = au_pin(&a->pin, dentry, a->btgt, a->udba, a->pin_flags);
21334 +       if (unlikely(err))
21335 +               goto out_parent;
21336 +
21337 +       sz = -1;
21338 +       a->h_path.dentry = au_h_dptr(dentry, btop);
21339 +       a->h_inode = d_inode(a->h_path.dentry);
21340 +       if (ia && (ia->ia_valid & ATTR_SIZE)) {
21341 +               inode_lock_shared_nested(a->h_inode, AuLsc_I_CHILD);
21342 +               if (ia->ia_size < i_size_read(a->h_inode))
21343 +                       sz = ia->ia_size;
21344 +               inode_unlock_shared(a->h_inode);
21345 +       }
21346 +
21347 +       hi_wh = NULL;
21348 +       if (au_ftest_icpup(a->flags, DID_CPUP) && d_unlinked(dentry)) {
21349 +               hi_wh = au_hi_wh(inode, a->btgt);
21350 +               if (!hi_wh) {
21351 +                       struct au_cp_generic cpg = {
21352 +                               .dentry = dentry,
21353 +                               .bdst   = a->btgt,
21354 +                               .bsrc   = -1,
21355 +                               .len    = sz,
21356 +                               .pin    = &a->pin
21357 +                       };
21358 +                       err = au_sio_cpup_wh(&cpg, /*file*/NULL);
21359 +                       if (unlikely(err))
21360 +                               goto out_unlock;
21361 +                       hi_wh = au_hi_wh(inode, a->btgt);
21362 +                       /* todo: revalidate hi_wh? */
21363 +               }
21364 +       }
21365 +
21366 +       if (parent) {
21367 +               au_pin_set_parent_lflag(&a->pin, /*lflag*/0);
21368 +               di_downgrade_lock(parent, AuLock_IR);
21369 +               dput(parent);
21370 +               parent = NULL;
21371 +       }
21372 +       if (!au_ftest_icpup(a->flags, DID_CPUP))
21373 +               goto out; /* success */
21374 +
21375 +       if (!d_unhashed(dentry)) {
21376 +               struct au_cp_generic cpg = {
21377 +                       .dentry = dentry,
21378 +                       .bdst   = a->btgt,
21379 +                       .bsrc   = btop,
21380 +                       .len    = sz,
21381 +                       .pin    = &a->pin,
21382 +                       .flags  = AuCpup_DTIME | AuCpup_HOPEN
21383 +               };
21384 +               err = au_sio_cpup_simple(&cpg);
21385 +               if (!err)
21386 +                       a->h_path.dentry = au_h_dptr(dentry, a->btgt);
21387 +       } else if (!hi_wh)
21388 +               a->h_path.dentry = au_h_dptr(dentry, a->btgt);
21389 +       else
21390 +               a->h_path.dentry = hi_wh; /* do not dget here */
21391 +
21392 +out_unlock:
21393 +       a->h_inode = d_inode(a->h_path.dentry);
21394 +       if (!err)
21395 +               goto out; /* success */
21396 +       au_unpin(&a->pin);
21397 +out_parent:
21398 +       if (parent) {
21399 +               di_write_unlock(parent);
21400 +               dput(parent);
21401 +       }
21402 +out:
21403 +       if (!err)
21404 +               inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
21405 +       return err;
21406 +}
21407 +
21408 +static int aufs_setattr(struct user_namespace *userns, struct dentry *dentry,
21409 +                       struct iattr *ia)
21410 +{
21411 +       int err;
21412 +       struct inode *inode, *delegated;
21413 +       struct super_block *sb;
21414 +       struct file *file;
21415 +       struct au_icpup_args *a;
21416 +       struct user_namespace *h_userns;
21417 +
21418 +       inode = d_inode(dentry);
21419 +       IMustLock(inode);
21420 +
21421 +       err = setattr_prepare(userns, dentry, ia);
21422 +       if (unlikely(err))
21423 +               goto out;
21424 +
21425 +       err = -ENOMEM;
21426 +       a = kzalloc(sizeof(*a), GFP_NOFS);
21427 +       if (unlikely(!a))
21428 +               goto out;
21429 +
21430 +       if (ia->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
21431 +               ia->ia_valid &= ~ATTR_MODE;
21432 +
21433 +       file = NULL;
21434 +       sb = dentry->d_sb;
21435 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
21436 +       if (unlikely(err))
21437 +               goto out_kfree;
21438 +
21439 +       if (ia->ia_valid & ATTR_FILE) {
21440 +               /* currently ftruncate(2) only */
21441 +               AuDebugOn(!d_is_reg(dentry));
21442 +               file = ia->ia_file;
21443 +               err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1,
21444 +                                           /*fi_lsc*/0);
21445 +               if (unlikely(err))
21446 +                       goto out_si;
21447 +               ia->ia_file = au_hf_top(file);
21448 +               a->udba = AuOpt_UDBA_NONE;
21449 +       } else {
21450 +               /* fchmod() doesn't pass ia_file */
21451 +               a->udba = au_opt_udba(sb);
21452 +               di_write_lock_child(dentry);
21453 +               /* no d_unlinked(), to set UDBA_NONE for root */
21454 +               if (d_unhashed(dentry))
21455 +                       a->udba = AuOpt_UDBA_NONE;
21456 +               if (a->udba != AuOpt_UDBA_NONE) {
21457 +                       AuDebugOn(IS_ROOT(dentry));
21458 +                       err = au_reval_for_attr(dentry, au_sigen(sb));
21459 +                       if (unlikely(err))
21460 +                               goto out_dentry;
21461 +               }
21462 +       }
21463 +
21464 +       err = au_pin_and_icpup(dentry, ia, a);
21465 +       if (unlikely(err < 0))
21466 +               goto out_dentry;
21467 +       if (au_ftest_icpup(a->flags, DID_CPUP)) {
21468 +               ia->ia_file = NULL;
21469 +               ia->ia_valid &= ~ATTR_FILE;
21470 +       }
21471 +
21472 +       a->h_path.mnt = au_sbr_mnt(sb, a->btgt);
21473 +       if ((ia->ia_valid & (ATTR_MODE | ATTR_CTIME))
21474 +           == (ATTR_MODE | ATTR_CTIME)) {
21475 +               err = security_path_chmod(&a->h_path, ia->ia_mode);
21476 +               if (unlikely(err))
21477 +                       goto out_unlock;
21478 +       } else if ((ia->ia_valid & (ATTR_UID | ATTR_GID))
21479 +                  && (ia->ia_valid & ATTR_CTIME)) {
21480 +               err = security_path_chown(&a->h_path, ia->ia_uid, ia->ia_gid);
21481 +               if (unlikely(err))
21482 +                       goto out_unlock;
21483 +       }
21484 +
21485 +       if (ia->ia_valid & ATTR_SIZE) {
21486 +               struct file *f;
21487 +
21488 +               if (ia->ia_size < i_size_read(inode))
21489 +                       /* unmap only */
21490 +                       truncate_setsize(inode, ia->ia_size);
21491 +
21492 +               f = NULL;
21493 +               if (ia->ia_valid & ATTR_FILE)
21494 +                       f = ia->ia_file;
21495 +               inode_unlock(a->h_inode);
21496 +               err = vfsub_trunc(&a->h_path, ia->ia_size, ia->ia_valid, f);
21497 +               inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
21498 +       } else {
21499 +               delegated = NULL;
21500 +               while (1) {
21501 +                       err = vfsub_notify_change(&a->h_path, ia, &delegated);
21502 +                       if (delegated) {
21503 +                               err = break_deleg_wait(&delegated);
21504 +                               if (!err)
21505 +                                       continue;
21506 +                       }
21507 +                       break;
21508 +               }
21509 +       }
21510 +       /*
21511 +        * regardless aufs 'acl' option setting.
21512 +        * why don't all acl-aware fs call this func from their ->setattr()?
21513 +        */
21514 +       if (!err && (ia->ia_valid & ATTR_MODE)) {
21515 +               h_userns = mnt_user_ns(a->h_path.mnt);
21516 +               err = vfsub_acl_chmod(h_userns, a->h_inode, ia->ia_mode);
21517 +       }
21518 +       if (!err)
21519 +               au_cpup_attr_changeable(inode);
21520 +
21521 +out_unlock:
21522 +       inode_unlock(a->h_inode);
21523 +       au_unpin(&a->pin);
21524 +       if (unlikely(err))
21525 +               au_update_dbtop(dentry);
21526 +out_dentry:
21527 +       di_write_unlock(dentry);
21528 +       if (file) {
21529 +               fi_write_unlock(file);
21530 +               ia->ia_file = file;
21531 +               ia->ia_valid |= ATTR_FILE;
21532 +       }
21533 +out_si:
21534 +       si_read_unlock(sb);
21535 +out_kfree:
21536 +       au_kfree_rcu(a);
21537 +out:
21538 +       AuTraceErr(err);
21539 +       return err;
21540 +}
21541 +
21542 +#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
21543 +static int au_h_path_to_set_attr(struct dentry *dentry,
21544 +                                struct au_icpup_args *a, struct path *h_path)
21545 +{
21546 +       int err;
21547 +       struct super_block *sb;
21548 +
21549 +       sb = dentry->d_sb;
21550 +       a->udba = au_opt_udba(sb);
21551 +       /* no d_unlinked(), to set UDBA_NONE for root */
21552 +       if (d_unhashed(dentry))
21553 +               a->udba = AuOpt_UDBA_NONE;
21554 +       if (a->udba != AuOpt_UDBA_NONE) {
21555 +               AuDebugOn(IS_ROOT(dentry));
21556 +               err = au_reval_for_attr(dentry, au_sigen(sb));
21557 +               if (unlikely(err))
21558 +                       goto out;
21559 +       }
21560 +       err = au_pin_and_icpup(dentry, /*ia*/NULL, a);
21561 +       if (unlikely(err < 0))
21562 +               goto out;
21563 +
21564 +       h_path->dentry = a->h_path.dentry;
21565 +       h_path->mnt = au_sbr_mnt(sb, a->btgt);
21566 +
21567 +out:
21568 +       return err;
21569 +}
21570 +
21571 +ssize_t au_sxattr(struct dentry *dentry, struct inode *inode,
21572 +                 struct au_sxattr *arg)
21573 +{
21574 +       int err;
21575 +       struct path h_path;
21576 +       struct super_block *sb;
21577 +       struct au_icpup_args *a;
21578 +       struct inode *h_inode;
21579 +       struct user_namespace *h_userns;
21580 +
21581 +       IMustLock(inode);
21582 +
21583 +       err = -ENOMEM;
21584 +       a = kzalloc(sizeof(*a), GFP_NOFS);
21585 +       if (unlikely(!a))
21586 +               goto out;
21587 +
21588 +       sb = dentry->d_sb;
21589 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
21590 +       if (unlikely(err))
21591 +               goto out_kfree;
21592 +
21593 +       h_path.dentry = NULL;   /* silence gcc */
21594 +       di_write_lock_child(dentry);
21595 +       err = au_h_path_to_set_attr(dentry, a, &h_path);
21596 +       if (unlikely(err))
21597 +               goto out_di;
21598 +       h_userns = mnt_user_ns(h_path.mnt);
21599 +
21600 +       inode_unlock(a->h_inode);
21601 +       switch (arg->type) {
21602 +       case AU_XATTR_SET:
21603 +               AuDebugOn(d_is_negative(h_path.dentry));
21604 +               err = vfsub_setxattr(h_userns, h_path.dentry,
21605 +                                    arg->u.set.name, arg->u.set.value,
21606 +                                    arg->u.set.size, arg->u.set.flags);
21607 +               break;
21608 +       case AU_ACL_SET:
21609 +               err = -EOPNOTSUPP;
21610 +               h_inode = d_inode(h_path.dentry);
21611 +               if (h_inode->i_op->set_acl) {
21612 +                       /* this will call posix_acl_update_mode */
21613 +                       err = h_inode->i_op->set_acl(h_userns, h_inode,
21614 +                                                    arg->u.acl_set.acl,
21615 +                                                    arg->u.acl_set.type);
21616 +               }
21617 +               break;
21618 +       }
21619 +       if (!err)
21620 +               au_cpup_attr_timesizes(inode);
21621 +
21622 +       au_unpin(&a->pin);
21623 +       if (unlikely(err))
21624 +               au_update_dbtop(dentry);
21625 +
21626 +out_di:
21627 +       di_write_unlock(dentry);
21628 +       si_read_unlock(sb);
21629 +out_kfree:
21630 +       au_kfree_rcu(a);
21631 +out:
21632 +       AuTraceErr(err);
21633 +       return err;
21634 +}
21635 +#endif
21636 +
21637 +static void au_refresh_iattr(struct inode *inode, struct kstat *st,
21638 +                            unsigned int nlink)
21639 +{
21640 +       unsigned int n;
21641 +
21642 +       inode->i_mode = st->mode;
21643 +       /* don't i_[ug]id_write() here */
21644 +       inode->i_uid = st->uid;
21645 +       inode->i_gid = st->gid;
21646 +       inode->i_atime = st->atime;
21647 +       inode->i_mtime = st->mtime;
21648 +       inode->i_ctime = st->ctime;
21649 +
21650 +       au_cpup_attr_nlink(inode, /*force*/0);
21651 +       if (S_ISDIR(inode->i_mode)) {
21652 +               n = inode->i_nlink;
21653 +               n -= nlink;
21654 +               n += st->nlink;
21655 +               smp_mb(); /* for i_nlink */
21656 +               /* 0 can happen */
21657 +               set_nlink(inode, n);
21658 +       }
21659 +
21660 +       spin_lock(&inode->i_lock);
21661 +       inode->i_blocks = st->blocks;
21662 +       i_size_write(inode, st->size);
21663 +       spin_unlock(&inode->i_lock);
21664 +}
21665 +
21666 +/*
21667 + * common routine for aufs_getattr() and au_getxattr().
21668 + * returns zero or negative (an error).
21669 + * @dentry will be read-locked in success.
21670 + */
21671 +int au_h_path_getattr(struct dentry *dentry, struct inode *inode, int force,
21672 +                     struct path *h_path, int locked)
21673 +{
21674 +       int err;
21675 +       unsigned int mnt_flags, sigen;
21676 +       unsigned char udba_none;
21677 +       aufs_bindex_t bindex;
21678 +       struct super_block *sb, *h_sb;
21679 +
21680 +       h_path->mnt = NULL;
21681 +       h_path->dentry = NULL;
21682 +
21683 +       err = 0;
21684 +       sb = dentry->d_sb;
21685 +       mnt_flags = au_mntflags(sb);
21686 +       udba_none = !!au_opt_test(mnt_flags, UDBA_NONE);
21687 +
21688 +       if (unlikely(locked))
21689 +               goto body; /* skip locking dinfo */
21690 +
21691 +       /* support fstat(2) */
21692 +       if (!d_unlinked(dentry) && !udba_none) {
21693 +               sigen = au_sigen(sb);
21694 +               err = au_digen_test(dentry, sigen);
21695 +               if (!err) {
21696 +                       di_read_lock_child(dentry, AuLock_IR);
21697 +                       err = au_dbrange_test(dentry);
21698 +                       if (unlikely(err)) {
21699 +                               di_read_unlock(dentry, AuLock_IR);
21700 +                               goto out;
21701 +                       }
21702 +               } else {
21703 +                       AuDebugOn(IS_ROOT(dentry));
21704 +                       di_write_lock_child(dentry);
21705 +                       err = au_dbrange_test(dentry);
21706 +                       if (!err)
21707 +                               err = au_reval_for_attr(dentry, sigen);
21708 +                       if (!err)
21709 +                               di_downgrade_lock(dentry, AuLock_IR);
21710 +                       else {
21711 +                               di_write_unlock(dentry);
21712 +                               goto out;
21713 +                       }
21714 +               }
21715 +       } else
21716 +               di_read_lock_child(dentry, AuLock_IR);
21717 +
21718 +body:
21719 +       if (!inode) {
21720 +               inode = d_inode(dentry);
21721 +               if (unlikely(!inode))
21722 +                       goto out;
21723 +       }
21724 +       bindex = au_ibtop(inode);
21725 +       h_path->mnt = au_sbr_mnt(sb, bindex);
21726 +       h_sb = h_path->mnt->mnt_sb;
21727 +       if (!force
21728 +           && !au_test_fs_bad_iattr(h_sb)
21729 +           && udba_none)
21730 +               goto out; /* success */
21731 +
21732 +       if (au_dbtop(dentry) == bindex)
21733 +               h_path->dentry = au_h_dptr(dentry, bindex);
21734 +       else if (au_opt_test(mnt_flags, PLINK) && au_plink_test(inode)) {
21735 +               h_path->dentry = au_plink_lkup(inode, bindex);
21736 +               if (IS_ERR(h_path->dentry))
21737 +                       /* pretending success */
21738 +                       h_path->dentry = NULL;
21739 +               else
21740 +                       dput(h_path->dentry);
21741 +       }
21742 +
21743 +out:
21744 +       return err;
21745 +}
21746 +
21747 +static int aufs_getattr(struct user_namespace *userns, const struct path *path,
21748 +                       struct kstat *st, u32 request, unsigned int query)
21749 +{
21750 +       int err;
21751 +       unsigned char positive;
21752 +       struct path h_path;
21753 +       struct dentry *dentry;
21754 +       struct inode *inode;
21755 +       struct super_block *sb;
21756 +
21757 +       dentry = path->dentry;
21758 +       inode = d_inode(dentry);
21759 +       sb = dentry->d_sb;
21760 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
21761 +       if (unlikely(err))
21762 +               goto out;
21763 +       err = au_h_path_getattr(dentry, /*inode*/NULL, /*force*/0, &h_path,
21764 +                               /*locked*/0);
21765 +       if (unlikely(err))
21766 +               goto out_si;
21767 +       if (unlikely(!h_path.dentry))
21768 +               /* illegally overlapped or something */
21769 +               goto out_fill; /* pretending success */
21770 +
21771 +       positive = d_is_positive(h_path.dentry);
21772 +       if (positive)
21773 +               /* no vfsub version */
21774 +               err = vfs_getattr(&h_path, st, request, query);
21775 +       if (!err) {
21776 +               if (positive)
21777 +                       au_refresh_iattr(inode, st,
21778 +                                        d_inode(h_path.dentry)->i_nlink);
21779 +               goto out_fill; /* success */
21780 +       }
21781 +       AuTraceErr(err);
21782 +       goto out_di;
21783 +
21784 +out_fill:
21785 +       generic_fillattr(userns, inode, st);
21786 +out_di:
21787 +       di_read_unlock(dentry, AuLock_IR);
21788 +out_si:
21789 +       si_read_unlock(sb);
21790 +out:
21791 +       AuTraceErr(err);
21792 +       return err;
21793 +}
21794 +
21795 +/* ---------------------------------------------------------------------- */
21796 +
21797 +static const char *aufs_get_link(struct dentry *dentry, struct inode *inode,
21798 +                                struct delayed_call *done)
21799 +{
21800 +       const char *ret;
21801 +       struct dentry *h_dentry;
21802 +       struct inode *h_inode;
21803 +       int err;
21804 +       aufs_bindex_t bindex;
21805 +
21806 +       ret = NULL; /* suppress a warning */
21807 +       err = -ECHILD;
21808 +       if (!dentry)
21809 +               goto out;
21810 +
21811 +       err = aufs_read_lock(dentry, AuLock_IR | AuLock_GEN);
21812 +       if (unlikely(err))
21813 +               goto out;
21814 +
21815 +       err = au_d_hashed_positive(dentry);
21816 +       if (unlikely(err))
21817 +               goto out_unlock;
21818 +
21819 +       err = -EINVAL;
21820 +       inode = d_inode(dentry);
21821 +       bindex = au_ibtop(inode);
21822 +       h_inode = au_h_iptr(inode, bindex);
21823 +       if (unlikely(!h_inode->i_op->get_link))
21824 +               goto out_unlock;
21825 +
21826 +       err = -EBUSY;
21827 +       h_dentry = NULL;
21828 +       if (au_dbtop(dentry) <= bindex) {
21829 +               h_dentry = au_h_dptr(dentry, bindex);
21830 +               if (h_dentry)
21831 +                       dget(h_dentry);
21832 +       }
21833 +       if (!h_dentry) {
21834 +               h_dentry = d_find_any_alias(h_inode);
21835 +               if (IS_ERR(h_dentry)) {
21836 +                       err = PTR_ERR(h_dentry);
21837 +                       goto out_unlock;
21838 +               }
21839 +       }
21840 +       if (unlikely(!h_dentry))
21841 +               goto out_unlock;
21842 +
21843 +       err = 0;
21844 +       AuDbg("%ps\n", h_inode->i_op->get_link);
21845 +       AuDbgDentry(h_dentry);
21846 +       ret = vfs_get_link(h_dentry, done);
21847 +       dput(h_dentry);
21848 +       if (IS_ERR(ret))
21849 +               err = PTR_ERR(ret);
21850 +
21851 +out_unlock:
21852 +       aufs_read_unlock(dentry, AuLock_IR);
21853 +out:
21854 +       if (unlikely(err))
21855 +               ret = ERR_PTR(err);
21856 +       AuTraceErrPtr(ret);
21857 +       return ret;
21858 +}
21859 +
21860 +/* ---------------------------------------------------------------------- */
21861 +
21862 +static int au_is_special(struct inode *inode)
21863 +{
21864 +       return (inode->i_mode & (S_IFBLK | S_IFCHR | S_IFIFO | S_IFSOCK));
21865 +}
21866 +
21867 +static int aufs_update_time(struct inode *inode, struct timespec64 *ts,
21868 +                           int flags)
21869 +{
21870 +       int err;
21871 +       aufs_bindex_t bindex;
21872 +       struct super_block *sb;
21873 +       struct inode *h_inode;
21874 +       struct vfsmount *h_mnt;
21875 +
21876 +       sb = inode->i_sb;
21877 +       WARN_ONCE((flags & S_ATIME) && !IS_NOATIME(inode),
21878 +                 "unexpected s_flags 0x%lx", sb->s_flags);
21879 +
21880 +       /* mmap_sem might be acquired already, cf. aufs_mmap() */
21881 +       lockdep_off();
21882 +       si_read_lock(sb, AuLock_FLUSH);
21883 +       ii_write_lock_child(inode);
21884 +
21885 +       err = 0;
21886 +       bindex = au_ibtop(inode);
21887 +       h_inode = au_h_iptr(inode, bindex);
21888 +       if (!au_test_ro(sb, bindex, inode)) {
21889 +               h_mnt = au_sbr_mnt(sb, bindex);
21890 +               err = vfsub_mnt_want_write(h_mnt);
21891 +               if (!err) {
21892 +                       err = vfsub_update_time(h_inode, ts, flags);
21893 +                       vfsub_mnt_drop_write(h_mnt);
21894 +               }
21895 +       } else if (au_is_special(h_inode)) {
21896 +               /*
21897 +                * Never copy-up here.
21898 +                * These special files may already be opened and used for
21899 +                * communicating. If we copied it up, then the communication
21900 +                * would be corrupted.
21901 +                */
21902 +               AuWarn1("timestamps for i%lu are ignored "
21903 +                       "since it is on readonly branch (hi%lu).\n",
21904 +                       inode->i_ino, h_inode->i_ino);
21905 +       } else if (flags & ~S_ATIME) {
21906 +               err = -EIO;
21907 +               AuIOErr1("unexpected flags 0x%x\n", flags);
21908 +               AuDebugOn(1);
21909 +       }
21910 +
21911 +       if (!err)
21912 +               au_cpup_attr_timesizes(inode);
21913 +       ii_write_unlock(inode);
21914 +       si_read_unlock(sb);
21915 +       lockdep_on();
21916 +
21917 +       if (!err && (flags & S_VERSION))
21918 +               inode_inc_iversion(inode);
21919 +
21920 +       return err;
21921 +}
21922 +
21923 +/* ---------------------------------------------------------------------- */
21924 +
21925 +/* no getattr version will be set by module.c:aufs_init() */
21926 +struct inode_operations aufs_iop_nogetattr[AuIop_Last],
21927 +       aufs_iop[] = {
21928 +       [AuIop_SYMLINK] = {
21929 +               .permission     = aufs_permission,
21930 +#ifdef CONFIG_FS_POSIX_ACL
21931 +               .get_acl        = aufs_get_acl,
21932 +               .set_acl        = aufs_set_acl, /* unsupport for symlink? */
21933 +#endif
21934 +
21935 +               .setattr        = aufs_setattr,
21936 +               .getattr        = aufs_getattr,
21937 +
21938 +#ifdef CONFIG_AUFS_XATTR
21939 +               .listxattr      = aufs_listxattr,
21940 +#endif
21941 +
21942 +               .get_link       = aufs_get_link,
21943 +
21944 +               /* .update_time = aufs_update_time */
21945 +       },
21946 +       [AuIop_DIR] = {
21947 +               .create         = aufs_create,
21948 +               .lookup         = aufs_lookup,
21949 +               .link           = aufs_link,
21950 +               .unlink         = aufs_unlink,
21951 +               .symlink        = aufs_symlink,
21952 +               .mkdir          = aufs_mkdir,
21953 +               .rmdir          = aufs_rmdir,
21954 +               .mknod          = aufs_mknod,
21955 +               .rename         = aufs_rename,
21956 +
21957 +               .permission     = aufs_permission,
21958 +#ifdef CONFIG_FS_POSIX_ACL
21959 +               .get_acl        = aufs_get_acl,
21960 +               .set_acl        = aufs_set_acl,
21961 +#endif
21962 +
21963 +               .setattr        = aufs_setattr,
21964 +               .getattr        = aufs_getattr,
21965 +
21966 +#ifdef CONFIG_AUFS_XATTR
21967 +               .listxattr      = aufs_listxattr,
21968 +#endif
21969 +
21970 +               .update_time    = aufs_update_time,
21971 +               .atomic_open    = aufs_atomic_open,
21972 +               .tmpfile        = aufs_tmpfile
21973 +       },
21974 +       [AuIop_OTHER] = {
21975 +               .permission     = aufs_permission,
21976 +#ifdef CONFIG_FS_POSIX_ACL
21977 +               .get_acl        = aufs_get_acl,
21978 +               .set_acl        = aufs_set_acl,
21979 +#endif
21980 +
21981 +               .setattr        = aufs_setattr,
21982 +               .getattr        = aufs_getattr,
21983 +
21984 +#ifdef CONFIG_AUFS_XATTR
21985 +               .listxattr      = aufs_listxattr,
21986 +#endif
21987 +
21988 +               .update_time    = aufs_update_time
21989 +       }
21990 +};
21991 diff -urN /usr/share/empty/fs/aufs/i_op_del.c linux/fs/aufs/i_op_del.c
21992 --- /usr/share/empty/fs/aufs/i_op_del.c 1970-01-01 01:00:00.000000000 +0100
21993 +++ linux/fs/aufs/i_op_del.c    2021-11-01 23:48:34.209692595 +0100
21994 @@ -0,0 +1,522 @@
21995 +// SPDX-License-Identifier: GPL-2.0
21996 +/*
21997 + * Copyright (C) 2005-2021 Junjiro R. Okajima
21998 + *
21999 + * This program, aufs is free software; you can redistribute it and/or modify
22000 + * it under the terms of the GNU General Public License as published by
22001 + * the Free Software Foundation; either version 2 of the License, or
22002 + * (at your option) any later version.
22003 + *
22004 + * This program is distributed in the hope that it will be useful,
22005 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
22006 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22007 + * GNU General Public License for more details.
22008 + *
22009 + * You should have received a copy of the GNU General Public License
22010 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22011 + */
22012 +
22013 +/*
22014 + * inode operations (del entry)
22015 + */
22016 +
22017 +#include <linux/iversion.h>
22018 +#include "aufs.h"
22019 +
22020 +/*
22021 + * decide if a new whiteout for @dentry is necessary or not.
22022 + * when it is necessary, prepare the parent dir for the upper branch whose
22023 + * branch index is @bcpup for creation. the actual creation of the whiteout will
22024 + * be done by caller.
22025 + * return value:
22026 + * 0: wh is unnecessary
22027 + * plus: wh is necessary
22028 + * minus: error
22029 + */
22030 +int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup)
22031 +{
22032 +       int need_wh, err;
22033 +       aufs_bindex_t btop;
22034 +       struct super_block *sb;
22035 +
22036 +       sb = dentry->d_sb;
22037 +       btop = au_dbtop(dentry);
22038 +       if (*bcpup < 0) {
22039 +               *bcpup = btop;
22040 +               if (au_test_ro(sb, btop, d_inode(dentry))) {
22041 +                       err = AuWbrCopyup(au_sbi(sb), dentry);
22042 +                       *bcpup = err;
22043 +                       if (unlikely(err < 0))
22044 +                               goto out;
22045 +               }
22046 +       } else
22047 +               AuDebugOn(btop < *bcpup
22048 +                         || au_test_ro(sb, *bcpup, d_inode(dentry)));
22049 +       AuDbg("bcpup %d, btop %d\n", *bcpup, btop);
22050 +
22051 +       if (*bcpup != btop) {
22052 +               err = au_cpup_dirs(dentry, *bcpup);
22053 +               if (unlikely(err))
22054 +                       goto out;
22055 +               need_wh = 1;
22056 +       } else {
22057 +               struct au_dinfo *dinfo, *tmp;
22058 +
22059 +               need_wh = -ENOMEM;
22060 +               dinfo = au_di(dentry);
22061 +               tmp = au_di_alloc(sb, AuLsc_DI_TMP);
22062 +               if (tmp) {
22063 +                       au_di_cp(tmp, dinfo);
22064 +                       au_di_swap(tmp, dinfo);
22065 +                       /* returns the number of positive dentries */
22066 +                       need_wh = au_lkup_dentry(dentry, btop + 1,
22067 +                                                /* AuLkup_IGNORE_PERM */ 0);
22068 +                       au_di_swap(tmp, dinfo);
22069 +                       au_rw_write_unlock(&tmp->di_rwsem);
22070 +                       au_di_free(tmp);
22071 +               }
22072 +       }
22073 +       AuDbg("need_wh %d\n", need_wh);
22074 +       err = need_wh;
22075 +
22076 +out:
22077 +       return err;
22078 +}
22079 +
22080 +/*
22081 + * simple tests for the del-entry operations.
22082 + * following the checks in vfs, plus the parent-child relationship.
22083 + */
22084 +int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
22085 +              struct dentry *h_parent, int isdir)
22086 +{
22087 +       int err;
22088 +       umode_t h_mode;
22089 +       struct dentry *h_dentry, *h_latest;
22090 +       struct inode *h_inode;
22091 +       struct path h_ppath;
22092 +       struct super_block *sb;
22093 +       struct au_branch *br;
22094 +       struct user_namespace *h_userns;
22095 +
22096 +       h_dentry = au_h_dptr(dentry, bindex);
22097 +       if (d_really_is_positive(dentry)) {
22098 +               err = -ENOENT;
22099 +               if (unlikely(d_is_negative(h_dentry)))
22100 +                       goto out;
22101 +               h_inode = d_inode(h_dentry);
22102 +               if (unlikely(!h_inode->i_nlink))
22103 +                       goto out;
22104 +
22105 +               h_mode = h_inode->i_mode;
22106 +               if (!isdir) {
22107 +                       err = -EISDIR;
22108 +                       if (unlikely(S_ISDIR(h_mode)))
22109 +                               goto out;
22110 +               } else if (unlikely(!S_ISDIR(h_mode))) {
22111 +                       err = -ENOTDIR;
22112 +                       goto out;
22113 +               }
22114 +       } else {
22115 +               /* rename(2) case */
22116 +               err = -EIO;
22117 +               if (unlikely(d_is_positive(h_dentry)))
22118 +                       goto out;
22119 +       }
22120 +
22121 +       err = -ENOENT;
22122 +       /* expected parent dir is locked */
22123 +       if (unlikely(h_parent != h_dentry->d_parent))
22124 +               goto out;
22125 +       err = 0;
22126 +
22127 +       /*
22128 +        * rmdir a dir may break the consistency on some filesystem.
22129 +        * let's try heavy test.
22130 +        */
22131 +       err = -EACCES;
22132 +       sb = dentry->d_sb;
22133 +       br = au_sbr(sb, bindex);
22134 +       h_userns = au_br_userns(br);
22135 +       if (unlikely(!au_opt_test(au_mntflags(sb), DIRPERM1)
22136 +                    && au_test_h_perm(h_userns, d_inode(h_parent),
22137 +                                      MAY_EXEC | MAY_WRITE)))
22138 +               goto out;
22139 +
22140 +       h_ppath.dentry = h_parent;
22141 +       h_ppath.mnt = au_br_mnt(br);
22142 +       h_latest = au_sio_lkup_one(h_userns, &dentry->d_name, &h_ppath);
22143 +       err = -EIO;
22144 +       if (IS_ERR(h_latest))
22145 +               goto out;
22146 +       if (h_latest == h_dentry)
22147 +               err = 0;
22148 +       dput(h_latest);
22149 +
22150 +out:
22151 +       return err;
22152 +}
22153 +
22154 +/*
22155 + * decide the branch where we operate for @dentry. the branch index will be set
22156 + * @rbcpup. after deciding it, 'pin' it and store the timestamps of the parent
22157 + * dir for reverting.
22158 + * when a new whiteout is necessary, create it.
22159 + */
22160 +static struct dentry*
22161 +lock_hdir_create_wh(struct dentry *dentry, int isdir, aufs_bindex_t *rbcpup,
22162 +                   struct au_dtime *dt, struct au_pin *pin)
22163 +{
22164 +       struct dentry *wh_dentry;
22165 +       struct super_block *sb;
22166 +       struct path h_path;
22167 +       int err, need_wh;
22168 +       unsigned int udba;
22169 +       aufs_bindex_t bcpup;
22170 +
22171 +       need_wh = au_wr_dir_need_wh(dentry, isdir, rbcpup);
22172 +       wh_dentry = ERR_PTR(need_wh);
22173 +       if (unlikely(need_wh < 0))
22174 +               goto out;
22175 +
22176 +       sb = dentry->d_sb;
22177 +       udba = au_opt_udba(sb);
22178 +       bcpup = *rbcpup;
22179 +       err = au_pin(pin, dentry, bcpup, udba,
22180 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
22181 +       wh_dentry = ERR_PTR(err);
22182 +       if (unlikely(err))
22183 +               goto out;
22184 +
22185 +       h_path.dentry = au_pinned_h_parent(pin);
22186 +       if (udba != AuOpt_UDBA_NONE
22187 +           && au_dbtop(dentry) == bcpup) {
22188 +               err = au_may_del(dentry, bcpup, h_path.dentry, isdir);
22189 +               wh_dentry = ERR_PTR(err);
22190 +               if (unlikely(err))
22191 +                       goto out_unpin;
22192 +       }
22193 +
22194 +       h_path.mnt = au_sbr_mnt(sb, bcpup);
22195 +       au_dtime_store(dt, au_pinned_parent(pin), &h_path);
22196 +       wh_dentry = NULL;
22197 +       if (!need_wh)
22198 +               goto out; /* success, no need to create whiteout */
22199 +
22200 +       wh_dentry = au_wh_create(dentry, bcpup, h_path.dentry);
22201 +       if (IS_ERR(wh_dentry))
22202 +               goto out_unpin;
22203 +
22204 +       /* returns with the parent is locked and wh_dentry is dget-ed */
22205 +       goto out; /* success */
22206 +
22207 +out_unpin:
22208 +       au_unpin(pin);
22209 +out:
22210 +       return wh_dentry;
22211 +}
22212 +
22213 +/*
22214 + * when removing a dir, rename it to a unique temporary whiteout-ed name first
22215 + * in order to be revertible and save time for removing many child whiteouts
22216 + * under the dir.
22217 + * returns 1 when there are too many child whiteout and caller should remove
22218 + * them asynchronously. returns 0 when the number of children is enough small to
22219 + * remove now or the branch fs is a remote fs.
22220 + * otherwise return an error.
22221 + */
22222 +static int renwh_and_rmdir(struct dentry *dentry, aufs_bindex_t bindex,
22223 +                          struct au_nhash *whlist, struct inode *dir)
22224 +{
22225 +       int rmdir_later, err, dirwh;
22226 +       struct dentry *h_dentry;
22227 +       struct super_block *sb;
22228 +       struct inode *inode;
22229 +
22230 +       sb = dentry->d_sb;
22231 +       SiMustAnyLock(sb);
22232 +       h_dentry = au_h_dptr(dentry, bindex);
22233 +       err = au_whtmp_ren(h_dentry, au_sbr(sb, bindex));
22234 +       if (unlikely(err))
22235 +               goto out;
22236 +
22237 +       /* stop monitoring */
22238 +       inode = d_inode(dentry);
22239 +       au_hn_free(au_hi(inode, bindex));
22240 +
22241 +       if (!au_test_fs_remote(h_dentry->d_sb)) {
22242 +               dirwh = au_sbi(sb)->si_dirwh;
22243 +               rmdir_later = (dirwh <= 1);
22244 +               if (!rmdir_later)
22245 +                       rmdir_later = au_nhash_test_longer_wh(whlist, bindex,
22246 +                                                             dirwh);
22247 +               if (rmdir_later)
22248 +                       return rmdir_later;
22249 +       }
22250 +
22251 +       err = au_whtmp_rmdir(dir, bindex, h_dentry, whlist);
22252 +       if (unlikely(err)) {
22253 +               AuIOErr("rmdir %pd, b%d failed, %d. ignored\n",
22254 +                       h_dentry, bindex, err);
22255 +               err = 0;
22256 +       }
22257 +
22258 +out:
22259 +       AuTraceErr(err);
22260 +       return err;
22261 +}
22262 +
22263 +/*
22264 + * final procedure for deleting a entry.
22265 + * maintain dentry and iattr.
22266 + */
22267 +static void epilog(struct inode *dir, struct dentry *dentry,
22268 +                  aufs_bindex_t bindex)
22269 +{
22270 +       struct inode *inode;
22271 +
22272 +       inode = d_inode(dentry);
22273 +       d_drop(dentry);
22274 +       inode->i_ctime = dir->i_ctime;
22275 +
22276 +       au_dir_ts(dir, bindex);
22277 +       inode_inc_iversion(dir);
22278 +}
22279 +
22280 +/*
22281 + * when an error happened, remove the created whiteout and revert everything.
22282 + */
22283 +static int do_revert(int err, struct inode *dir, aufs_bindex_t bindex,
22284 +                    aufs_bindex_t bwh, struct dentry *wh_dentry,
22285 +                    struct dentry *dentry, struct au_dtime *dt)
22286 +{
22287 +       int rerr;
22288 +       struct path h_path = {
22289 +               .dentry = wh_dentry,
22290 +               .mnt    = au_sbr_mnt(dir->i_sb, bindex)
22291 +       };
22292 +
22293 +       rerr = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path, dentry);
22294 +       if (!rerr) {
22295 +               au_set_dbwh(dentry, bwh);
22296 +               au_dtime_revert(dt);
22297 +               return 0;
22298 +       }
22299 +
22300 +       AuIOErr("%pd reverting whiteout failed(%d, %d)\n", dentry, err, rerr);
22301 +       return -EIO;
22302 +}
22303 +
22304 +/* ---------------------------------------------------------------------- */
22305 +
22306 +int aufs_unlink(struct inode *dir, struct dentry *dentry)
22307 +{
22308 +       int err;
22309 +       aufs_bindex_t bwh, bindex, btop;
22310 +       struct inode *inode, *h_dir, *delegated;
22311 +       struct dentry *parent, *wh_dentry;
22312 +       /* to reduce stack size */
22313 +       struct {
22314 +               struct au_dtime dt;
22315 +               struct au_pin pin;
22316 +               struct path h_path;
22317 +       } *a;
22318 +
22319 +       IMustLock(dir);
22320 +
22321 +       err = -ENOMEM;
22322 +       a = kmalloc(sizeof(*a), GFP_NOFS);
22323 +       if (unlikely(!a))
22324 +               goto out;
22325 +
22326 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
22327 +       if (unlikely(err))
22328 +               goto out_free;
22329 +       err = au_d_hashed_positive(dentry);
22330 +       if (unlikely(err))
22331 +               goto out_unlock;
22332 +       inode = d_inode(dentry);
22333 +       IMustLock(inode);
22334 +       err = -EISDIR;
22335 +       if (unlikely(d_is_dir(dentry)))
22336 +               goto out_unlock; /* possible? */
22337 +
22338 +       btop = au_dbtop(dentry);
22339 +       bwh = au_dbwh(dentry);
22340 +       bindex = -1;
22341 +       parent = dentry->d_parent; /* dir inode is locked */
22342 +       di_write_lock_parent(parent);
22343 +       wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/0, &bindex, &a->dt,
22344 +                                       &a->pin);
22345 +       err = PTR_ERR(wh_dentry);
22346 +       if (IS_ERR(wh_dentry))
22347 +               goto out_parent;
22348 +
22349 +       a->h_path.mnt = au_sbr_mnt(dentry->d_sb, btop);
22350 +       a->h_path.dentry = au_h_dptr(dentry, btop);
22351 +       dget(a->h_path.dentry);
22352 +       if (bindex == btop) {
22353 +               h_dir = au_pinned_h_dir(&a->pin);
22354 +               delegated = NULL;
22355 +               err = vfsub_unlink(h_dir, &a->h_path, &delegated, /*force*/0);
22356 +               if (unlikely(err == -EWOULDBLOCK)) {
22357 +                       pr_warn("cannot retry for NFSv4 delegation"
22358 +                               " for an internal unlink\n");
22359 +                       iput(delegated);
22360 +               }
22361 +       } else {
22362 +               /* dir inode is locked */
22363 +               h_dir = d_inode(wh_dentry->d_parent);
22364 +               IMustLock(h_dir);
22365 +               err = 0;
22366 +       }
22367 +
22368 +       if (!err) {
22369 +               vfsub_drop_nlink(inode);
22370 +               epilog(dir, dentry, bindex);
22371 +
22372 +               /* update target timestamps */
22373 +               if (bindex == btop) {
22374 +                       vfsub_update_h_iattr(&a->h_path, /*did*/NULL);
22375 +                       /*ignore*/
22376 +                       inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
22377 +               } else
22378 +                       /* todo: this timestamp may be reverted later */
22379 +                       inode->i_ctime = h_dir->i_ctime;
22380 +               goto out_unpin; /* success */
22381 +       }
22382 +
22383 +       /* revert */
22384 +       if (wh_dentry) {
22385 +               int rerr;
22386 +
22387 +               rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
22388 +                                &a->dt);
22389 +               if (rerr)
22390 +                       err = rerr;
22391 +       }
22392 +
22393 +out_unpin:
22394 +       au_unpin(&a->pin);
22395 +       dput(wh_dentry);
22396 +       dput(a->h_path.dentry);
22397 +out_parent:
22398 +       di_write_unlock(parent);
22399 +out_unlock:
22400 +       aufs_read_unlock(dentry, AuLock_DW);
22401 +out_free:
22402 +       au_kfree_rcu(a);
22403 +out:
22404 +       return err;
22405 +}
22406 +
22407 +int aufs_rmdir(struct inode *dir, struct dentry *dentry)
22408 +{
22409 +       int err, rmdir_later;
22410 +       aufs_bindex_t bwh, bindex, btop;
22411 +       struct inode *inode;
22412 +       struct dentry *parent, *wh_dentry, *h_dentry;
22413 +       struct au_whtmp_rmdir *args;
22414 +       /* to reduce stack size */
22415 +       struct {
22416 +               struct au_dtime dt;
22417 +               struct au_pin pin;
22418 +       } *a;
22419 +
22420 +       IMustLock(dir);
22421 +
22422 +       err = -ENOMEM;
22423 +       a = kmalloc(sizeof(*a), GFP_NOFS);
22424 +       if (unlikely(!a))
22425 +               goto out;
22426 +
22427 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
22428 +       if (unlikely(err))
22429 +               goto out_free;
22430 +       err = au_alive_dir(dentry);
22431 +       if (unlikely(err))
22432 +               goto out_unlock;
22433 +       inode = d_inode(dentry);
22434 +       IMustLock(inode);
22435 +       err = -ENOTDIR;
22436 +       if (unlikely(!d_is_dir(dentry)))
22437 +               goto out_unlock; /* possible? */
22438 +
22439 +       err = -ENOMEM;
22440 +       args = au_whtmp_rmdir_alloc(dir->i_sb, GFP_NOFS);
22441 +       if (unlikely(!args))
22442 +               goto out_unlock;
22443 +
22444 +       parent = dentry->d_parent; /* dir inode is locked */
22445 +       di_write_lock_parent(parent);
22446 +       err = au_test_empty(dentry, &args->whlist);
22447 +       if (unlikely(err))
22448 +               goto out_parent;
22449 +
22450 +       btop = au_dbtop(dentry);
22451 +       bwh = au_dbwh(dentry);
22452 +       bindex = -1;
22453 +       wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/1, &bindex, &a->dt,
22454 +                                       &a->pin);
22455 +       err = PTR_ERR(wh_dentry);
22456 +       if (IS_ERR(wh_dentry))
22457 +               goto out_parent;
22458 +
22459 +       h_dentry = au_h_dptr(dentry, btop);
22460 +       dget(h_dentry);
22461 +       rmdir_later = 0;
22462 +       if (bindex == btop) {
22463 +               err = renwh_and_rmdir(dentry, btop, &args->whlist, dir);
22464 +               if (err > 0) {
22465 +                       rmdir_later = err;
22466 +                       err = 0;
22467 +               }
22468 +       } else {
22469 +               /* stop monitoring */
22470 +               au_hn_free(au_hi(inode, btop));
22471 +
22472 +               /* dir inode is locked */
22473 +               IMustLock(d_inode(wh_dentry->d_parent));
22474 +               err = 0;
22475 +       }
22476 +
22477 +       if (!err) {
22478 +               vfsub_dead_dir(inode);
22479 +               au_set_dbdiropq(dentry, -1);
22480 +               epilog(dir, dentry, bindex);
22481 +
22482 +               if (rmdir_later) {
22483 +                       au_whtmp_kick_rmdir(dir, btop, h_dentry, args);
22484 +                       args = NULL;
22485 +               }
22486 +
22487 +               goto out_unpin; /* success */
22488 +       }
22489 +
22490 +       /* revert */
22491 +       AuLabel(revert);
22492 +       if (wh_dentry) {
22493 +               int rerr;
22494 +
22495 +               rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
22496 +                                &a->dt);
22497 +               if (rerr)
22498 +                       err = rerr;
22499 +       }
22500 +
22501 +out_unpin:
22502 +       au_unpin(&a->pin);
22503 +       dput(wh_dentry);
22504 +       dput(h_dentry);
22505 +out_parent:
22506 +       di_write_unlock(parent);
22507 +       if (args)
22508 +               au_whtmp_rmdir_free(args);
22509 +out_unlock:
22510 +       aufs_read_unlock(dentry, AuLock_DW);
22511 +out_free:
22512 +       au_kfree_rcu(a);
22513 +out:
22514 +       AuTraceErr(err);
22515 +       return err;
22516 +}
22517 diff -urN /usr/share/empty/fs/aufs/i_op_ren.c linux/fs/aufs/i_op_ren.c
22518 --- /usr/share/empty/fs/aufs/i_op_ren.c 1970-01-01 01:00:00.000000000 +0100
22519 +++ linux/fs/aufs/i_op_ren.c    2021-11-01 23:48:34.209692595 +0100
22520 @@ -0,0 +1,1257 @@
22521 +// SPDX-License-Identifier: GPL-2.0
22522 +/*
22523 + * Copyright (C) 2005-2021 Junjiro R. Okajima
22524 + *
22525 + * This program, aufs is free software; you can redistribute it and/or modify
22526 + * it under the terms of the GNU General Public License as published by
22527 + * the Free Software Foundation; either version 2 of the License, or
22528 + * (at your option) any later version.
22529 + *
22530 + * This program is distributed in the hope that it will be useful,
22531 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
22532 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22533 + * GNU General Public License for more details.
22534 + *
22535 + * You should have received a copy of the GNU General Public License
22536 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22537 + */
22538 +
22539 +/*
22540 + * inode operation (rename entry)
22541 + * todo: this is crazy monster
22542 + */
22543 +
22544 +#include <linux/iversion.h>
22545 +#include "aufs.h"
22546 +
22547 +enum { AuSRC, AuDST, AuSrcDst };
22548 +enum { AuPARENT, AuCHILD, AuParentChild };
22549 +
22550 +#define AuRen_ISDIR_SRC                1
22551 +#define AuRen_ISDIR_DST                (1 << 1)
22552 +#define AuRen_ISSAMEDIR                (1 << 2)
22553 +#define AuRen_WHSRC            (1 << 3)
22554 +#define AuRen_WHDST            (1 << 4)
22555 +#define AuRen_MNT_WRITE                (1 << 5)
22556 +#define AuRen_DT_DSTDIR                (1 << 6)
22557 +#define AuRen_DIROPQ_SRC       (1 << 7)
22558 +#define AuRen_DIROPQ_DST       (1 << 8)
22559 +#define AuRen_DIRREN           (1 << 9)
22560 +#define AuRen_DROPPED_SRC      (1 << 10)
22561 +#define AuRen_DROPPED_DST      (1 << 11)
22562 +#define au_ftest_ren(flags, name)      ((flags) & AuRen_##name)
22563 +#define au_fset_ren(flags, name) \
22564 +       do { (flags) |= AuRen_##name; } while (0)
22565 +#define au_fclr_ren(flags, name) \
22566 +       do { (flags) &= ~AuRen_##name; } while (0)
22567 +
22568 +#ifndef CONFIG_AUFS_DIRREN
22569 +#undef AuRen_DIRREN
22570 +#define AuRen_DIRREN           0
22571 +#endif
22572 +
22573 +struct au_ren_args {
22574 +       struct {
22575 +               struct dentry *dentry, *h_dentry, *parent, *h_parent,
22576 +                       *wh_dentry;
22577 +               struct inode *dir, *inode;
22578 +               struct au_hinode *hdir, *hinode;
22579 +               struct au_dtime dt[AuParentChild];
22580 +               aufs_bindex_t btop, bdiropq;
22581 +       } sd[AuSrcDst];
22582 +
22583 +#define src_dentry     sd[AuSRC].dentry
22584 +#define src_dir                sd[AuSRC].dir
22585 +#define src_inode      sd[AuSRC].inode
22586 +#define src_h_dentry   sd[AuSRC].h_dentry
22587 +#define src_parent     sd[AuSRC].parent
22588 +#define src_h_parent   sd[AuSRC].h_parent
22589 +#define src_wh_dentry  sd[AuSRC].wh_dentry
22590 +#define src_hdir       sd[AuSRC].hdir
22591 +#define src_hinode     sd[AuSRC].hinode
22592 +#define src_h_dir      sd[AuSRC].hdir->hi_inode
22593 +#define src_dt         sd[AuSRC].dt
22594 +#define src_btop       sd[AuSRC].btop
22595 +#define src_bdiropq    sd[AuSRC].bdiropq
22596 +
22597 +#define dst_dentry     sd[AuDST].dentry
22598 +#define dst_dir                sd[AuDST].dir
22599 +#define dst_inode      sd[AuDST].inode
22600 +#define dst_h_dentry   sd[AuDST].h_dentry
22601 +#define dst_parent     sd[AuDST].parent
22602 +#define dst_h_parent   sd[AuDST].h_parent
22603 +#define dst_wh_dentry  sd[AuDST].wh_dentry
22604 +#define dst_hdir       sd[AuDST].hdir
22605 +#define dst_hinode     sd[AuDST].hinode
22606 +#define dst_h_dir      sd[AuDST].hdir->hi_inode
22607 +#define dst_dt         sd[AuDST].dt
22608 +#define dst_btop       sd[AuDST].btop
22609 +#define dst_bdiropq    sd[AuDST].bdiropq
22610 +
22611 +       struct dentry *h_trap;
22612 +       struct au_branch *br;
22613 +       struct path h_path;
22614 +       struct au_nhash whlist;
22615 +       aufs_bindex_t btgt, src_bwh;
22616 +
22617 +       struct {
22618 +               unsigned short auren_flags;
22619 +               unsigned char flags;    /* syscall parameter */
22620 +               unsigned char exchange;
22621 +       } __packed;
22622 +
22623 +       struct au_whtmp_rmdir *thargs;
22624 +       struct dentry *h_dst;
22625 +       struct au_hinode *h_root;
22626 +};
22627 +
22628 +/* ---------------------------------------------------------------------- */
22629 +
22630 +/*
22631 + * functions for reverting.
22632 + * when an error happened in a single rename systemcall, we should revert
22633 + * everything as if nothing happened.
22634 + * we don't need to revert the copied-up/down the parent dir since they are
22635 + * harmless.
22636 + */
22637 +
22638 +#define RevertFailure(fmt, ...) do { \
22639 +       AuIOErr("revert failure: " fmt " (%d, %d)\n", \
22640 +               ##__VA_ARGS__, err, rerr); \
22641 +       err = -EIO; \
22642 +} while (0)
22643 +
22644 +static void au_ren_do_rev_diropq(int err, struct au_ren_args *a, int idx)
22645 +{
22646 +       int rerr;
22647 +       struct dentry *d;
22648 +#define src_or_dst(member) a->sd[idx].member
22649 +
22650 +       d = src_or_dst(dentry); /* {src,dst}_dentry */
22651 +       au_hn_inode_lock_nested(src_or_dst(hinode), AuLsc_I_CHILD);
22652 +       rerr = au_diropq_remove(d, a->btgt);
22653 +       au_hn_inode_unlock(src_or_dst(hinode));
22654 +       au_set_dbdiropq(d, src_or_dst(bdiropq));
22655 +       if (rerr)
22656 +               RevertFailure("remove diropq %pd", d);
22657 +
22658 +#undef src_or_dst_
22659 +}
22660 +
22661 +static void au_ren_rev_diropq(int err, struct au_ren_args *a)
22662 +{
22663 +       if (au_ftest_ren(a->auren_flags, DIROPQ_SRC))
22664 +               au_ren_do_rev_diropq(err, a, AuSRC);
22665 +       if (au_ftest_ren(a->auren_flags, DIROPQ_DST))
22666 +               au_ren_do_rev_diropq(err, a, AuDST);
22667 +}
22668 +
22669 +static void au_ren_rev_rename(int err, struct au_ren_args *a)
22670 +{
22671 +       int rerr;
22672 +       struct inode *delegated;
22673 +       struct path h_ppath = {
22674 +               .dentry = a->src_h_parent,
22675 +               .mnt    = a->h_path.mnt
22676 +       };
22677 +
22678 +       a->h_path.dentry = vfsub_lkup_one(&a->src_dentry->d_name, &h_ppath);
22679 +       rerr = PTR_ERR(a->h_path.dentry);
22680 +       if (IS_ERR(a->h_path.dentry)) {
22681 +               RevertFailure("lkup one %pd", a->src_dentry);
22682 +               return;
22683 +       }
22684 +
22685 +       delegated = NULL;
22686 +       rerr = vfsub_rename(a->dst_h_dir,
22687 +                           au_h_dptr(a->src_dentry, a->btgt),
22688 +                           a->src_h_dir, &a->h_path, &delegated, a->flags);
22689 +       if (unlikely(rerr == -EWOULDBLOCK)) {
22690 +               pr_warn("cannot retry for NFSv4 delegation"
22691 +                       " for an internal rename\n");
22692 +               iput(delegated);
22693 +       }
22694 +       d_drop(a->h_path.dentry);
22695 +       dput(a->h_path.dentry);
22696 +       /* au_set_h_dptr(a->src_dentry, a->btgt, NULL); */
22697 +       if (rerr)
22698 +               RevertFailure("rename %pd", a->src_dentry);
22699 +}
22700 +
22701 +static void au_ren_rev_whtmp(int err, struct au_ren_args *a)
22702 +{
22703 +       int rerr;
22704 +       struct inode *delegated;
22705 +       struct path h_ppath = {
22706 +               .dentry = a->dst_h_parent,
22707 +               .mnt    = a->h_path.mnt
22708 +       };
22709 +
22710 +       a->h_path.dentry = vfsub_lkup_one(&a->dst_dentry->d_name, &h_ppath);
22711 +       rerr = PTR_ERR(a->h_path.dentry);
22712 +       if (IS_ERR(a->h_path.dentry)) {
22713 +               RevertFailure("lkup one %pd", a->dst_dentry);
22714 +               return;
22715 +       }
22716 +       if (d_is_positive(a->h_path.dentry)) {
22717 +               d_drop(a->h_path.dentry);
22718 +               dput(a->h_path.dentry);
22719 +               return;
22720 +       }
22721 +
22722 +       delegated = NULL;
22723 +       rerr = vfsub_rename(a->dst_h_dir, a->h_dst, a->dst_h_dir, &a->h_path,
22724 +                           &delegated, a->flags);
22725 +       if (unlikely(rerr == -EWOULDBLOCK)) {
22726 +               pr_warn("cannot retry for NFSv4 delegation"
22727 +                       " for an internal rename\n");
22728 +               iput(delegated);
22729 +       }
22730 +       d_drop(a->h_path.dentry);
22731 +       dput(a->h_path.dentry);
22732 +       if (!rerr)
22733 +               au_set_h_dptr(a->dst_dentry, a->btgt, dget(a->h_dst));
22734 +       else
22735 +               RevertFailure("rename %pd", a->h_dst);
22736 +}
22737 +
22738 +static void au_ren_rev_whsrc(int err, struct au_ren_args *a)
22739 +{
22740 +       int rerr;
22741 +
22742 +       a->h_path.dentry = a->src_wh_dentry;
22743 +       rerr = au_wh_unlink_dentry(a->src_h_dir, &a->h_path, a->src_dentry);
22744 +       au_set_dbwh(a->src_dentry, a->src_bwh);
22745 +       if (rerr)
22746 +               RevertFailure("unlink %pd", a->src_wh_dentry);
22747 +}
22748 +#undef RevertFailure
22749 +
22750 +/* ---------------------------------------------------------------------- */
22751 +
22752 +/*
22753 + * when we have to copyup the renaming entry, do it with the rename-target name
22754 + * in order to minimize the cost (the later actual rename is unnecessary).
22755 + * otherwise rename it on the target branch.
22756 + */
22757 +static int au_ren_or_cpup(struct au_ren_args *a)
22758 +{
22759 +       int err;
22760 +       struct dentry *d;
22761 +       struct inode *delegated;
22762 +
22763 +       d = a->src_dentry;
22764 +       if (au_dbtop(d) == a->btgt) {
22765 +               a->h_path.dentry = a->dst_h_dentry;
22766 +               AuDebugOn(au_dbtop(d) != a->btgt);
22767 +               delegated = NULL;
22768 +               err = vfsub_rename(a->src_h_dir, au_h_dptr(d, a->btgt),
22769 +                                  a->dst_h_dir, &a->h_path, &delegated,
22770 +                                  a->flags);
22771 +               if (unlikely(err == -EWOULDBLOCK)) {
22772 +                       pr_warn("cannot retry for NFSv4 delegation"
22773 +                               " for an internal rename\n");
22774 +                       iput(delegated);
22775 +               }
22776 +       } else
22777 +               BUG();
22778 +
22779 +       if (!err && a->h_dst)
22780 +               /* it will be set to dinfo later */
22781 +               dget(a->h_dst);
22782 +
22783 +       return err;
22784 +}
22785 +
22786 +/* cf. aufs_rmdir() */
22787 +static int au_ren_del_whtmp(struct au_ren_args *a)
22788 +{
22789 +       int err;
22790 +       struct inode *dir;
22791 +
22792 +       dir = a->dst_dir;
22793 +       SiMustAnyLock(dir->i_sb);
22794 +       if (!au_nhash_test_longer_wh(&a->whlist, a->btgt,
22795 +                                    au_sbi(dir->i_sb)->si_dirwh)
22796 +           || au_test_fs_remote(a->h_dst->d_sb)) {
22797 +               err = au_whtmp_rmdir(dir, a->btgt, a->h_dst, &a->whlist);
22798 +               if (unlikely(err))
22799 +                       pr_warn("failed removing whtmp dir %pd (%d), "
22800 +                               "ignored.\n", a->h_dst, err);
22801 +       } else {
22802 +               au_nhash_wh_free(&a->thargs->whlist);
22803 +               a->thargs->whlist = a->whlist;
22804 +               a->whlist.nh_num = 0;
22805 +               au_whtmp_kick_rmdir(dir, a->btgt, a->h_dst, a->thargs);
22806 +               dput(a->h_dst);
22807 +               a->thargs = NULL;
22808 +       }
22809 +
22810 +       return 0;
22811 +}
22812 +
22813 +/* make it 'opaque' dir. */
22814 +static int au_ren_do_diropq(struct au_ren_args *a, int idx)
22815 +{
22816 +       int err;
22817 +       struct dentry *d, *diropq;
22818 +#define src_or_dst(member) a->sd[idx].member
22819 +
22820 +       err = 0;
22821 +       d = src_or_dst(dentry); /* {src,dst}_dentry */
22822 +       src_or_dst(bdiropq) = au_dbdiropq(d);
22823 +       src_or_dst(hinode) = au_hi(src_or_dst(inode), a->btgt);
22824 +       au_hn_inode_lock_nested(src_or_dst(hinode), AuLsc_I_CHILD);
22825 +       diropq = au_diropq_create(d, a->btgt);
22826 +       au_hn_inode_unlock(src_or_dst(hinode));
22827 +       if (IS_ERR(diropq))
22828 +               err = PTR_ERR(diropq);
22829 +       else
22830 +               dput(diropq);
22831 +
22832 +#undef src_or_dst_
22833 +       return err;
22834 +}
22835 +
22836 +static int au_ren_diropq(struct au_ren_args *a)
22837 +{
22838 +       int err;
22839 +       unsigned char always;
22840 +       struct dentry *d;
22841 +
22842 +       err = 0;
22843 +       d = a->dst_dentry; /* already renamed on the branch */
22844 +       always = !!au_opt_test(au_mntflags(d->d_sb), ALWAYS_DIROPQ);
22845 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC)
22846 +           && !au_ftest_ren(a->auren_flags, DIRREN)
22847 +           && a->btgt != au_dbdiropq(a->src_dentry)
22848 +           && (a->dst_wh_dentry
22849 +               || a->btgt <= au_dbdiropq(d)
22850 +               /* hide the lower to keep xino */
22851 +               /* the lowers may not be a dir, but we hide them anyway */
22852 +               || a->btgt < au_dbbot(d)
22853 +               || always)) {
22854 +               AuDbg("here\n");
22855 +               err = au_ren_do_diropq(a, AuSRC);
22856 +               if (unlikely(err))
22857 +                       goto out;
22858 +               au_fset_ren(a->auren_flags, DIROPQ_SRC);
22859 +       }
22860 +       if (!a->exchange)
22861 +               goto out; /* success */
22862 +
22863 +       d = a->src_dentry; /* already renamed on the branch */
22864 +       if (au_ftest_ren(a->auren_flags, ISDIR_DST)
22865 +           && a->btgt != au_dbdiropq(a->dst_dentry)
22866 +           && (a->btgt < au_dbdiropq(d)
22867 +               || a->btgt < au_dbbot(d)
22868 +               || always)) {
22869 +               AuDbgDentry(a->src_dentry);
22870 +               AuDbgDentry(a->dst_dentry);
22871 +               err = au_ren_do_diropq(a, AuDST);
22872 +               if (unlikely(err))
22873 +                       goto out_rev_src;
22874 +               au_fset_ren(a->auren_flags, DIROPQ_DST);
22875 +       }
22876 +       goto out; /* success */
22877 +
22878 +out_rev_src:
22879 +       AuDbg("err %d, reverting src\n", err);
22880 +       au_ren_rev_diropq(err, a);
22881 +out:
22882 +       return err;
22883 +}
22884 +
22885 +static int do_rename(struct au_ren_args *a)
22886 +{
22887 +       int err;
22888 +       struct dentry *d, *h_d;
22889 +
22890 +       if (!a->exchange) {
22891 +               /* prepare workqueue args for asynchronous rmdir */
22892 +               h_d = a->dst_h_dentry;
22893 +               if (au_ftest_ren(a->auren_flags, ISDIR_DST)
22894 +                   /* && !au_ftest_ren(a->auren_flags, DIRREN) */
22895 +                   && d_is_positive(h_d)) {
22896 +                       err = -ENOMEM;
22897 +                       a->thargs = au_whtmp_rmdir_alloc(a->src_dentry->d_sb,
22898 +                                                        GFP_NOFS);
22899 +                       if (unlikely(!a->thargs))
22900 +                               goto out;
22901 +                       a->h_dst = dget(h_d);
22902 +               }
22903 +
22904 +               /* create whiteout for src_dentry */
22905 +               if (au_ftest_ren(a->auren_flags, WHSRC)) {
22906 +                       a->src_bwh = au_dbwh(a->src_dentry);
22907 +                       AuDebugOn(a->src_bwh >= 0);
22908 +                       a->src_wh_dentry = au_wh_create(a->src_dentry, a->btgt,
22909 +                                                       a->src_h_parent);
22910 +                       err = PTR_ERR(a->src_wh_dentry);
22911 +                       if (IS_ERR(a->src_wh_dentry))
22912 +                               goto out_thargs;
22913 +               }
22914 +
22915 +               /* lookup whiteout for dentry */
22916 +               if (au_ftest_ren(a->auren_flags, WHDST)) {
22917 +                       h_d = au_wh_lkup(a->dst_h_parent,
22918 +                                        &a->dst_dentry->d_name, a->br);
22919 +                       err = PTR_ERR(h_d);
22920 +                       if (IS_ERR(h_d))
22921 +                               goto out_whsrc;
22922 +                       if (d_is_negative(h_d))
22923 +                               dput(h_d);
22924 +                       else
22925 +                               a->dst_wh_dentry = h_d;
22926 +               }
22927 +
22928 +               /* rename dentry to tmpwh */
22929 +               if (a->thargs) {
22930 +                       err = au_whtmp_ren(a->dst_h_dentry, a->br);
22931 +                       if (unlikely(err))
22932 +                               goto out_whdst;
22933 +
22934 +                       d = a->dst_dentry;
22935 +                       au_set_h_dptr(d, a->btgt, NULL);
22936 +                       err = au_lkup_neg(d, a->btgt, /*wh*/0);
22937 +                       if (unlikely(err))
22938 +                               goto out_whtmp;
22939 +                       a->dst_h_dentry = au_h_dptr(d, a->btgt);
22940 +               }
22941 +       }
22942 +
22943 +       BUG_ON(d_is_positive(a->dst_h_dentry) && a->src_btop != a->btgt);
22944 +#if 0 /* debugging */
22945 +       BUG_ON(!au_ftest_ren(a->auren_flags, DIRREN)
22946 +              && d_is_positive(a->dst_h_dentry)
22947 +              && a->src_btop != a->btgt);
22948 +#endif
22949 +
22950 +       /* rename by vfs_rename or cpup */
22951 +       err = au_ren_or_cpup(a);
22952 +       if (unlikely(err))
22953 +               /* leave the copied-up one */
22954 +               goto out_whtmp;
22955 +
22956 +       /* make dir opaque */
22957 +       err = au_ren_diropq(a);
22958 +       if (unlikely(err))
22959 +               goto out_rename;
22960 +
22961 +       /* update target timestamps */
22962 +       if (a->exchange) {
22963 +               AuDebugOn(au_dbtop(a->dst_dentry) != a->btgt);
22964 +               a->h_path.dentry = au_h_dptr(a->dst_dentry, a->btgt);
22965 +               vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
22966 +               a->dst_inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
22967 +       }
22968 +       AuDebugOn(au_dbtop(a->src_dentry) != a->btgt);
22969 +       a->h_path.dentry = au_h_dptr(a->src_dentry, a->btgt);
22970 +       vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
22971 +       a->src_inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
22972 +
22973 +       if (!a->exchange) {
22974 +               /* remove whiteout for dentry */
22975 +               if (a->dst_wh_dentry) {
22976 +                       a->h_path.dentry = a->dst_wh_dentry;
22977 +                       err = au_wh_unlink_dentry(a->dst_h_dir, &a->h_path,
22978 +                                                 a->dst_dentry);
22979 +                       if (unlikely(err))
22980 +                               goto out_diropq;
22981 +               }
22982 +
22983 +               /* remove whtmp */
22984 +               if (a->thargs)
22985 +                       au_ren_del_whtmp(a); /* ignore this error */
22986 +
22987 +               au_fhsm_wrote(a->src_dentry->d_sb, a->btgt, /*force*/0);
22988 +       }
22989 +       err = 0;
22990 +       goto out_success;
22991 +
22992 +out_diropq:
22993 +       au_ren_rev_diropq(err, a);
22994 +out_rename:
22995 +       au_ren_rev_rename(err, a);
22996 +       dput(a->h_dst);
22997 +out_whtmp:
22998 +       if (a->thargs)
22999 +               au_ren_rev_whtmp(err, a);
23000 +out_whdst:
23001 +       dput(a->dst_wh_dentry);
23002 +       a->dst_wh_dentry = NULL;
23003 +out_whsrc:
23004 +       if (a->src_wh_dentry)
23005 +               au_ren_rev_whsrc(err, a);
23006 +out_success:
23007 +       dput(a->src_wh_dentry);
23008 +       dput(a->dst_wh_dentry);
23009 +out_thargs:
23010 +       if (a->thargs) {
23011 +               dput(a->h_dst);
23012 +               au_whtmp_rmdir_free(a->thargs);
23013 +               a->thargs = NULL;
23014 +       }
23015 +out:
23016 +       return err;
23017 +}
23018 +
23019 +/* ---------------------------------------------------------------------- */
23020 +
23021 +/*
23022 + * test if @dentry dir can be rename destination or not.
23023 + * success means, it is a logically empty dir.
23024 + */
23025 +static int may_rename_dstdir(struct dentry *dentry, struct au_nhash *whlist)
23026 +{
23027 +       return au_test_empty(dentry, whlist);
23028 +}
23029 +
23030 +/*
23031 + * test if @a->src_dentry dir can be rename source or not.
23032 + * if it can, return 0.
23033 + * success means,
23034 + * - it is a logically empty dir.
23035 + * - or, it exists on writable branch and has no children including whiteouts
23036 + *   on the lower branch unless DIRREN is on.
23037 + */
23038 +static int may_rename_srcdir(struct au_ren_args *a)
23039 +{
23040 +       int err;
23041 +       unsigned int rdhash;
23042 +       aufs_bindex_t btop, btgt;
23043 +       struct dentry *dentry;
23044 +       struct super_block *sb;
23045 +       struct au_sbinfo *sbinfo;
23046 +
23047 +       dentry = a->src_dentry;
23048 +       sb = dentry->d_sb;
23049 +       sbinfo = au_sbi(sb);
23050 +       if (au_opt_test(sbinfo->si_mntflags, DIRREN))
23051 +               au_fset_ren(a->auren_flags, DIRREN);
23052 +
23053 +       btgt = a->btgt;
23054 +       btop = au_dbtop(dentry);
23055 +       if (btop != btgt) {
23056 +               struct au_nhash whlist;
23057 +
23058 +               SiMustAnyLock(sb);
23059 +               rdhash = sbinfo->si_rdhash;
23060 +               if (!rdhash)
23061 +                       rdhash = au_rdhash_est(au_dir_size(/*file*/NULL,
23062 +                                                          dentry));
23063 +               err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
23064 +               if (unlikely(err))
23065 +                       goto out;
23066 +               err = au_test_empty(dentry, &whlist);
23067 +               au_nhash_wh_free(&whlist);
23068 +               goto out;
23069 +       }
23070 +
23071 +       if (btop == au_dbtaildir(dentry))
23072 +               return 0; /* success */
23073 +
23074 +       err = au_test_empty_lower(dentry);
23075 +
23076 +out:
23077 +       if (err == -ENOTEMPTY) {
23078 +               if (au_ftest_ren(a->auren_flags, DIRREN)) {
23079 +                       err = 0;
23080 +               } else {
23081 +                       AuWarn1("renaming dir who has child(ren) on multiple "
23082 +                               "branches, is not supported\n");
23083 +                       err = -EXDEV;
23084 +               }
23085 +       }
23086 +       return err;
23087 +}
23088 +
23089 +/* side effect: sets whlist and h_dentry */
23090 +static int au_ren_may_dir(struct au_ren_args *a)
23091 +{
23092 +       int err;
23093 +       unsigned int rdhash;
23094 +       struct dentry *d;
23095 +
23096 +       d = a->dst_dentry;
23097 +       SiMustAnyLock(d->d_sb);
23098 +
23099 +       err = 0;
23100 +       if (au_ftest_ren(a->auren_flags, ISDIR_DST) && a->dst_inode) {
23101 +               rdhash = au_sbi(d->d_sb)->si_rdhash;
23102 +               if (!rdhash)
23103 +                       rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, d));
23104 +               err = au_nhash_alloc(&a->whlist, rdhash, GFP_NOFS);
23105 +               if (unlikely(err))
23106 +                       goto out;
23107 +
23108 +               if (!a->exchange) {
23109 +                       au_set_dbtop(d, a->dst_btop);
23110 +                       err = may_rename_dstdir(d, &a->whlist);
23111 +                       au_set_dbtop(d, a->btgt);
23112 +               } else
23113 +                       err = may_rename_srcdir(a);
23114 +       }
23115 +       a->dst_h_dentry = au_h_dptr(d, au_dbtop(d));
23116 +       if (unlikely(err))
23117 +               goto out;
23118 +
23119 +       d = a->src_dentry;
23120 +       a->src_h_dentry = au_h_dptr(d, au_dbtop(d));
23121 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC)) {
23122 +               err = may_rename_srcdir(a);
23123 +               if (unlikely(err)) {
23124 +                       au_nhash_wh_free(&a->whlist);
23125 +                       a->whlist.nh_num = 0;
23126 +               }
23127 +       }
23128 +out:
23129 +       return err;
23130 +}
23131 +
23132 +/* ---------------------------------------------------------------------- */
23133 +
23134 +/*
23135 + * simple tests for rename.
23136 + * following the checks in vfs, plus the parent-child relationship.
23137 + */
23138 +static int au_may_ren(struct au_ren_args *a)
23139 +{
23140 +       int err, isdir;
23141 +       struct inode *h_inode;
23142 +
23143 +       if (a->src_btop == a->btgt) {
23144 +               err = au_may_del(a->src_dentry, a->btgt, a->src_h_parent,
23145 +                                au_ftest_ren(a->auren_flags, ISDIR_SRC));
23146 +               if (unlikely(err))
23147 +                       goto out;
23148 +               err = -EINVAL;
23149 +               if (unlikely(a->src_h_dentry == a->h_trap))
23150 +                       goto out;
23151 +       }
23152 +
23153 +       err = 0;
23154 +       if (a->dst_btop != a->btgt)
23155 +               goto out;
23156 +
23157 +       err = -ENOTEMPTY;
23158 +       if (unlikely(a->dst_h_dentry == a->h_trap))
23159 +               goto out;
23160 +
23161 +       err = -EIO;
23162 +       isdir = !!au_ftest_ren(a->auren_flags, ISDIR_DST);
23163 +       if (d_really_is_negative(a->dst_dentry)) {
23164 +               if (d_is_negative(a->dst_h_dentry))
23165 +                       err = au_may_add(a->dst_dentry, a->btgt,
23166 +                                        a->dst_h_parent, isdir);
23167 +       } else {
23168 +               if (unlikely(d_is_negative(a->dst_h_dentry)))
23169 +                       goto out;
23170 +               h_inode = d_inode(a->dst_h_dentry);
23171 +               if (h_inode->i_nlink)
23172 +                       err = au_may_del(a->dst_dentry, a->btgt,
23173 +                                        a->dst_h_parent, isdir);
23174 +       }
23175 +
23176 +out:
23177 +       if (unlikely(err == -ENOENT || err == -EEXIST))
23178 +               err = -EIO;
23179 +       AuTraceErr(err);
23180 +       return err;
23181 +}
23182 +
23183 +/* ---------------------------------------------------------------------- */
23184 +
23185 +/*
23186 + * locking order
23187 + * (VFS)
23188 + * - src_dir and dir by lock_rename()
23189 + * - inode if exists
23190 + * (aufs)
23191 + * - lock all
23192 + *   + src_dentry and dentry by aufs_read_and_write_lock2() which calls,
23193 + *     + si_read_lock
23194 + *     + di_write_lock2_child()
23195 + *       + di_write_lock_child()
23196 + *        + ii_write_lock_child()
23197 + *       + di_write_lock_child2()
23198 + *        + ii_write_lock_child2()
23199 + *     + src_parent and parent
23200 + *       + di_write_lock_parent()
23201 + *        + ii_write_lock_parent()
23202 + *       + di_write_lock_parent2()
23203 + *        + ii_write_lock_parent2()
23204 + *   + lower src_dir and dir by vfsub_lock_rename()
23205 + *   + verify the every relationships between child and parent. if any
23206 + *     of them failed, unlock all and return -EBUSY.
23207 + */
23208 +static void au_ren_unlock(struct au_ren_args *a)
23209 +{
23210 +       vfsub_unlock_rename(a->src_h_parent, a->src_hdir,
23211 +                           a->dst_h_parent, a->dst_hdir);
23212 +       if (au_ftest_ren(a->auren_flags, DIRREN)
23213 +           && a->h_root)
23214 +               au_hn_inode_unlock(a->h_root);
23215 +       if (au_ftest_ren(a->auren_flags, MNT_WRITE))
23216 +               vfsub_mnt_drop_write(au_br_mnt(a->br));
23217 +}
23218 +
23219 +static int au_ren_lock(struct au_ren_args *a)
23220 +{
23221 +       int err;
23222 +       unsigned int udba;
23223 +
23224 +       err = 0;
23225 +       a->src_h_parent = au_h_dptr(a->src_parent, a->btgt);
23226 +       a->src_hdir = au_hi(a->src_dir, a->btgt);
23227 +       a->dst_h_parent = au_h_dptr(a->dst_parent, a->btgt);
23228 +       a->dst_hdir = au_hi(a->dst_dir, a->btgt);
23229 +
23230 +       err = vfsub_mnt_want_write(au_br_mnt(a->br));
23231 +       if (unlikely(err))
23232 +               goto out;
23233 +       au_fset_ren(a->auren_flags, MNT_WRITE);
23234 +       if (au_ftest_ren(a->auren_flags, DIRREN)) {
23235 +               struct dentry *root;
23236 +               struct inode *dir;
23237 +
23238 +               /*
23239 +                * sbinfo is already locked, so this ii_read_lock is
23240 +                * unnecessary. but our debugging feature checks it.
23241 +                */
23242 +               root = a->src_inode->i_sb->s_root;
23243 +               if (root != a->src_parent && root != a->dst_parent) {
23244 +                       dir = d_inode(root);
23245 +                       ii_read_lock_parent3(dir);
23246 +                       a->h_root = au_hi(dir, a->btgt);
23247 +                       ii_read_unlock(dir);
23248 +                       au_hn_inode_lock_nested(a->h_root, AuLsc_I_PARENT3);
23249 +               }
23250 +       }
23251 +       a->h_trap = vfsub_lock_rename(a->src_h_parent, a->src_hdir,
23252 +                                     a->dst_h_parent, a->dst_hdir);
23253 +       udba = au_opt_udba(a->src_dentry->d_sb);
23254 +       if (unlikely(a->src_hdir->hi_inode != d_inode(a->src_h_parent)
23255 +                    || a->dst_hdir->hi_inode != d_inode(a->dst_h_parent)))
23256 +               err = au_busy_or_stale();
23257 +       if (!err && au_dbtop(a->src_dentry) == a->btgt)
23258 +               err = au_h_verify(a->src_h_dentry, udba,
23259 +                                 d_inode(a->src_h_parent), a->src_h_parent,
23260 +                                 a->br);
23261 +       if (!err && au_dbtop(a->dst_dentry) == a->btgt)
23262 +               err = au_h_verify(a->dst_h_dentry, udba,
23263 +                                 d_inode(a->dst_h_parent), a->dst_h_parent,
23264 +                                 a->br);
23265 +       if (!err)
23266 +               goto out; /* success */
23267 +
23268 +       err = au_busy_or_stale();
23269 +       au_ren_unlock(a);
23270 +
23271 +out:
23272 +       return err;
23273 +}
23274 +
23275 +/* ---------------------------------------------------------------------- */
23276 +
23277 +static void au_ren_refresh_dir(struct au_ren_args *a)
23278 +{
23279 +       struct inode *dir;
23280 +
23281 +       dir = a->dst_dir;
23282 +       inode_inc_iversion(dir);
23283 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC)) {
23284 +               /* is this updating defined in POSIX? */
23285 +               au_cpup_attr_timesizes(a->src_inode);
23286 +               au_cpup_attr_nlink(dir, /*force*/1);
23287 +       }
23288 +       au_dir_ts(dir, a->btgt);
23289 +
23290 +       if (a->exchange) {
23291 +               dir = a->src_dir;
23292 +               inode_inc_iversion(dir);
23293 +               if (au_ftest_ren(a->auren_flags, ISDIR_DST)) {
23294 +                       /* is this updating defined in POSIX? */
23295 +                       au_cpup_attr_timesizes(a->dst_inode);
23296 +                       au_cpup_attr_nlink(dir, /*force*/1);
23297 +               }
23298 +               au_dir_ts(dir, a->btgt);
23299 +       }
23300 +
23301 +       if (au_ftest_ren(a->auren_flags, ISSAMEDIR))
23302 +               return;
23303 +
23304 +       dir = a->src_dir;
23305 +       inode_inc_iversion(dir);
23306 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC))
23307 +               au_cpup_attr_nlink(dir, /*force*/1);
23308 +       au_dir_ts(dir, a->btgt);
23309 +}
23310 +
23311 +static void au_ren_refresh(struct au_ren_args *a)
23312 +{
23313 +       aufs_bindex_t bbot, bindex;
23314 +       struct dentry *d, *h_d;
23315 +       struct inode *i, *h_i;
23316 +       struct super_block *sb;
23317 +
23318 +       d = a->dst_dentry;
23319 +       d_drop(d);
23320 +       if (a->h_dst)
23321 +               /* already dget-ed by au_ren_or_cpup() */
23322 +               au_set_h_dptr(d, a->btgt, a->h_dst);
23323 +
23324 +       i = a->dst_inode;
23325 +       if (i) {
23326 +               if (!a->exchange) {
23327 +                       if (!au_ftest_ren(a->auren_flags, ISDIR_DST))
23328 +                               vfsub_drop_nlink(i);
23329 +                       else {
23330 +                               vfsub_dead_dir(i);
23331 +                               au_cpup_attr_timesizes(i);
23332 +                       }
23333 +                       au_update_dbrange(d, /*do_put_zero*/1);
23334 +               } else
23335 +                       au_cpup_attr_nlink(i, /*force*/1);
23336 +       } else {
23337 +               bbot = a->btgt;
23338 +               for (bindex = au_dbtop(d); bindex < bbot; bindex++)
23339 +                       au_set_h_dptr(d, bindex, NULL);
23340 +               bbot = au_dbbot(d);
23341 +               for (bindex = a->btgt + 1; bindex <= bbot; bindex++)
23342 +                       au_set_h_dptr(d, bindex, NULL);
23343 +               au_update_dbrange(d, /*do_put_zero*/0);
23344 +       }
23345 +
23346 +       if (a->exchange
23347 +           || au_ftest_ren(a->auren_flags, DIRREN)) {
23348 +               d_drop(a->src_dentry);
23349 +               if (au_ftest_ren(a->auren_flags, DIRREN))
23350 +                       au_set_dbwh(a->src_dentry, -1);
23351 +               return;
23352 +       }
23353 +
23354 +       d = a->src_dentry;
23355 +       au_set_dbwh(d, -1);
23356 +       bbot = au_dbbot(d);
23357 +       for (bindex = a->btgt + 1; bindex <= bbot; bindex++) {
23358 +               h_d = au_h_dptr(d, bindex);
23359 +               if (h_d)
23360 +                       au_set_h_dptr(d, bindex, NULL);
23361 +       }
23362 +       au_set_dbbot(d, a->btgt);
23363 +
23364 +       sb = d->d_sb;
23365 +       i = a->src_inode;
23366 +       if (au_opt_test(au_mntflags(sb), PLINK) && au_plink_test(i))
23367 +               return; /* success */
23368 +
23369 +       bbot = au_ibbot(i);
23370 +       for (bindex = a->btgt + 1; bindex <= bbot; bindex++) {
23371 +               h_i = au_h_iptr(i, bindex);
23372 +               if (h_i) {
23373 +                       au_xino_write(sb, bindex, h_i->i_ino, /*ino*/0);
23374 +                       /* ignore this error */
23375 +                       au_set_h_iptr(i, bindex, NULL, 0);
23376 +               }
23377 +       }
23378 +       au_set_ibbot(i, a->btgt);
23379 +}
23380 +
23381 +/* ---------------------------------------------------------------------- */
23382 +
23383 +/* mainly for link(2) and rename(2) */
23384 +int au_wbr(struct dentry *dentry, aufs_bindex_t btgt)
23385 +{
23386 +       aufs_bindex_t bdiropq, bwh;
23387 +       struct dentry *parent;
23388 +       struct au_branch *br;
23389 +
23390 +       parent = dentry->d_parent;
23391 +       IMustLock(d_inode(parent)); /* dir is locked */
23392 +
23393 +       bdiropq = au_dbdiropq(parent);
23394 +       bwh = au_dbwh(dentry);
23395 +       br = au_sbr(dentry->d_sb, btgt);
23396 +       if (au_br_rdonly(br)
23397 +           || (0 <= bdiropq && bdiropq < btgt)
23398 +           || (0 <= bwh && bwh < btgt))
23399 +               btgt = -1;
23400 +
23401 +       AuDbg("btgt %d\n", btgt);
23402 +       return btgt;
23403 +}
23404 +
23405 +/* sets src_btop, dst_btop and btgt */
23406 +static int au_ren_wbr(struct au_ren_args *a)
23407 +{
23408 +       int err;
23409 +       struct au_wr_dir_args wr_dir_args = {
23410 +               /* .force_btgt  = -1, */
23411 +               .flags          = AuWrDir_ADD_ENTRY
23412 +       };
23413 +
23414 +       a->src_btop = au_dbtop(a->src_dentry);
23415 +       a->dst_btop = au_dbtop(a->dst_dentry);
23416 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC)
23417 +           || au_ftest_ren(a->auren_flags, ISDIR_DST))
23418 +               au_fset_wrdir(wr_dir_args.flags, ISDIR);
23419 +       wr_dir_args.force_btgt = a->src_btop;
23420 +       if (a->dst_inode && a->dst_btop < a->src_btop)
23421 +               wr_dir_args.force_btgt = a->dst_btop;
23422 +       wr_dir_args.force_btgt = au_wbr(a->dst_dentry, wr_dir_args.force_btgt);
23423 +       err = au_wr_dir(a->dst_dentry, a->src_dentry, &wr_dir_args);
23424 +       a->btgt = err;
23425 +       if (a->exchange)
23426 +               au_update_dbtop(a->dst_dentry);
23427 +
23428 +       return err;
23429 +}
23430 +
23431 +static void au_ren_dt(struct au_ren_args *a)
23432 +{
23433 +       a->h_path.dentry = a->src_h_parent;
23434 +       au_dtime_store(a->src_dt + AuPARENT, a->src_parent, &a->h_path);
23435 +       if (!au_ftest_ren(a->auren_flags, ISSAMEDIR)) {
23436 +               a->h_path.dentry = a->dst_h_parent;
23437 +               au_dtime_store(a->dst_dt + AuPARENT, a->dst_parent, &a->h_path);
23438 +       }
23439 +
23440 +       au_fclr_ren(a->auren_flags, DT_DSTDIR);
23441 +       if (!au_ftest_ren(a->auren_flags, ISDIR_SRC)
23442 +           && !a->exchange)
23443 +               return;
23444 +
23445 +       a->h_path.dentry = a->src_h_dentry;
23446 +       au_dtime_store(a->src_dt + AuCHILD, a->src_dentry, &a->h_path);
23447 +       if (d_is_positive(a->dst_h_dentry)) {
23448 +               au_fset_ren(a->auren_flags, DT_DSTDIR);
23449 +               a->h_path.dentry = a->dst_h_dentry;
23450 +               au_dtime_store(a->dst_dt + AuCHILD, a->dst_dentry, &a->h_path);
23451 +       }
23452 +}
23453 +
23454 +static void au_ren_rev_dt(int err, struct au_ren_args *a)
23455 +{
23456 +       struct dentry *h_d;
23457 +       struct inode *h_inode;
23458 +
23459 +       au_dtime_revert(a->src_dt + AuPARENT);
23460 +       if (!au_ftest_ren(a->auren_flags, ISSAMEDIR))
23461 +               au_dtime_revert(a->dst_dt + AuPARENT);
23462 +
23463 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC) && err != -EIO) {
23464 +               h_d = a->src_dt[AuCHILD].dt_h_path.dentry;
23465 +               h_inode = d_inode(h_d);
23466 +               inode_lock_nested(h_inode, AuLsc_I_CHILD);
23467 +               au_dtime_revert(a->src_dt + AuCHILD);
23468 +               inode_unlock(h_inode);
23469 +
23470 +               if (au_ftest_ren(a->auren_flags, DT_DSTDIR)) {
23471 +                       h_d = a->dst_dt[AuCHILD].dt_h_path.dentry;
23472 +                       h_inode = d_inode(h_d);
23473 +                       inode_lock_nested(h_inode, AuLsc_I_CHILD);
23474 +                       au_dtime_revert(a->dst_dt + AuCHILD);
23475 +                       inode_unlock(h_inode);
23476 +               }
23477 +       }
23478 +}
23479 +
23480 +/* ---------------------------------------------------------------------- */
23481 +
23482 +int aufs_rename(struct user_namespace *userns,
23483 +               struct inode *_src_dir, struct dentry *_src_dentry,
23484 +               struct inode *_dst_dir, struct dentry *_dst_dentry,
23485 +               unsigned int _flags)
23486 +{
23487 +       int err, lock_flags;
23488 +       void *rev;
23489 +       /* reduce stack space */
23490 +       struct au_ren_args *a;
23491 +       struct au_pin pin;
23492 +
23493 +       AuDbg("%pd, %pd, 0x%x\n", _src_dentry, _dst_dentry, _flags);
23494 +       IMustLock(_src_dir);
23495 +       IMustLock(_dst_dir);
23496 +
23497 +       err = -EINVAL;
23498 +       if (unlikely(_flags & RENAME_WHITEOUT))
23499 +               goto out;
23500 +
23501 +       err = -ENOMEM;
23502 +       BUILD_BUG_ON(sizeof(*a) > PAGE_SIZE);
23503 +       a = kzalloc(sizeof(*a), GFP_NOFS);
23504 +       if (unlikely(!a))
23505 +               goto out;
23506 +
23507 +       a->flags = _flags;
23508 +       BUILD_BUG_ON(sizeof(a->exchange) == sizeof(u8)
23509 +                    && RENAME_EXCHANGE > U8_MAX);
23510 +       a->exchange = _flags & RENAME_EXCHANGE;
23511 +       a->src_dir = _src_dir;
23512 +       a->src_dentry = _src_dentry;
23513 +       a->src_inode = NULL;
23514 +       if (d_really_is_positive(a->src_dentry))
23515 +               a->src_inode = d_inode(a->src_dentry);
23516 +       a->src_parent = a->src_dentry->d_parent; /* dir inode is locked */
23517 +       a->dst_dir = _dst_dir;
23518 +       a->dst_dentry = _dst_dentry;
23519 +       a->dst_inode = NULL;
23520 +       if (d_really_is_positive(a->dst_dentry))
23521 +               a->dst_inode = d_inode(a->dst_dentry);
23522 +       a->dst_parent = a->dst_dentry->d_parent; /* dir inode is locked */
23523 +       if (a->dst_inode) {
23524 +               /*
23525 +                * if EXCHANGE && src is non-dir && dst is dir,
23526 +                * dst is not locked.
23527 +                */
23528 +               /* IMustLock(a->dst_inode); */
23529 +               au_igrab(a->dst_inode);
23530 +       }
23531 +
23532 +       err = -ENOTDIR;
23533 +       lock_flags = AuLock_FLUSH | AuLock_NOPLM | AuLock_GEN;
23534 +       if (d_is_dir(a->src_dentry)) {
23535 +               au_fset_ren(a->auren_flags, ISDIR_SRC);
23536 +               if (unlikely(!a->exchange
23537 +                            && d_really_is_positive(a->dst_dentry)
23538 +                            && !d_is_dir(a->dst_dentry)))
23539 +                       goto out_free;
23540 +               lock_flags |= AuLock_DIRS;
23541 +       }
23542 +       if (a->dst_inode && d_is_dir(a->dst_dentry)) {
23543 +               au_fset_ren(a->auren_flags, ISDIR_DST);
23544 +               if (unlikely(!a->exchange
23545 +                            && d_really_is_positive(a->src_dentry)
23546 +                            && !d_is_dir(a->src_dentry)))
23547 +                       goto out_free;
23548 +               lock_flags |= AuLock_DIRS;
23549 +       }
23550 +       err = aufs_read_and_write_lock2(a->dst_dentry, a->src_dentry,
23551 +                                       lock_flags);
23552 +       if (unlikely(err))
23553 +               goto out_free;
23554 +
23555 +       err = au_d_hashed_positive(a->src_dentry);
23556 +       if (unlikely(err))
23557 +               goto out_unlock;
23558 +       err = -ENOENT;
23559 +       if (a->dst_inode) {
23560 +               /*
23561 +                * If it is a dir, VFS unhash it before this
23562 +                * function. It means we cannot rely upon d_unhashed().
23563 +                */
23564 +               if (unlikely(!a->dst_inode->i_nlink))
23565 +                       goto out_unlock;
23566 +               if (!au_ftest_ren(a->auren_flags, ISDIR_DST)) {
23567 +                       err = au_d_hashed_positive(a->dst_dentry);
23568 +                       if (unlikely(err && !a->exchange))
23569 +                               goto out_unlock;
23570 +               } else if (unlikely(IS_DEADDIR(a->dst_inode)))
23571 +                       goto out_unlock;
23572 +       } else if (unlikely(d_unhashed(a->dst_dentry)))
23573 +               goto out_unlock;
23574 +
23575 +       /*
23576 +        * is it possible?
23577 +        * yes, it happened (in linux-3.3-rcN) but I don't know why.
23578 +        * there may exist a problem somewhere else.
23579 +        */
23580 +       err = -EINVAL;
23581 +       if (unlikely(d_inode(a->dst_parent) == d_inode(a->src_dentry)))
23582 +               goto out_unlock;
23583 +
23584 +       au_fset_ren(a->auren_flags, ISSAMEDIR); /* temporary */
23585 +       di_write_lock_parent(a->dst_parent);
23586 +
23587 +       /* which branch we process */
23588 +       err = au_ren_wbr(a);
23589 +       if (unlikely(err < 0))
23590 +               goto out_parent;
23591 +       a->br = au_sbr(a->dst_dentry->d_sb, a->btgt);
23592 +       a->h_path.mnt = au_br_mnt(a->br);
23593 +
23594 +       /* are they available to be renamed */
23595 +       err = au_ren_may_dir(a);
23596 +       if (unlikely(err))
23597 +               goto out_children;
23598 +
23599 +       /* prepare the writable parent dir on the same branch */
23600 +       if (a->dst_btop == a->btgt) {
23601 +               au_fset_ren(a->auren_flags, WHDST);
23602 +       } else {
23603 +               err = au_cpup_dirs(a->dst_dentry, a->btgt);
23604 +               if (unlikely(err))
23605 +                       goto out_children;
23606 +       }
23607 +
23608 +       err = 0;
23609 +       if (!a->exchange) {
23610 +               if (a->src_dir != a->dst_dir) {
23611 +                       /*
23612 +                        * this temporary unlock is safe,
23613 +                        * because both dir->i_mutex are locked.
23614 +                        */
23615 +                       di_write_unlock(a->dst_parent);
23616 +                       di_write_lock_parent(a->src_parent);
23617 +                       err = au_wr_dir_need_wh(a->src_dentry,
23618 +                                               au_ftest_ren(a->auren_flags,
23619 +                                                            ISDIR_SRC),
23620 +                                               &a->btgt);
23621 +                       di_write_unlock(a->src_parent);
23622 +                       di_write_lock2_parent(a->src_parent, a->dst_parent,
23623 +                                             /*isdir*/1);
23624 +                       au_fclr_ren(a->auren_flags, ISSAMEDIR);
23625 +               } else
23626 +                       err = au_wr_dir_need_wh(a->src_dentry,
23627 +                                               au_ftest_ren(a->auren_flags,
23628 +                                                            ISDIR_SRC),
23629 +                                               &a->btgt);
23630 +       }
23631 +       if (unlikely(err < 0))
23632 +               goto out_children;
23633 +       if (err)
23634 +               au_fset_ren(a->auren_flags, WHSRC);
23635 +
23636 +       /* cpup src */
23637 +       if (a->src_btop != a->btgt) {
23638 +               err = au_pin(&pin, a->src_dentry, a->btgt,
23639 +                            au_opt_udba(a->src_dentry->d_sb),
23640 +                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
23641 +               if (!err) {
23642 +                       struct au_cp_generic cpg = {
23643 +                               .dentry = a->src_dentry,
23644 +                               .bdst   = a->btgt,
23645 +                               .bsrc   = a->src_btop,
23646 +                               .len    = -1,
23647 +                               .pin    = &pin,
23648 +                               .flags  = AuCpup_DTIME | AuCpup_HOPEN
23649 +                       };
23650 +                       AuDebugOn(au_dbtop(a->src_dentry) != a->src_btop);
23651 +                       err = au_sio_cpup_simple(&cpg);
23652 +                       au_unpin(&pin);
23653 +               }
23654 +               if (unlikely(err))
23655 +                       goto out_children;
23656 +               a->src_btop = a->btgt;
23657 +               a->src_h_dentry = au_h_dptr(a->src_dentry, a->btgt);
23658 +               if (!a->exchange)
23659 +                       au_fset_ren(a->auren_flags, WHSRC);
23660 +       }
23661 +
23662 +       /* cpup dst */
23663 +       if (a->exchange && a->dst_inode
23664 +           && a->dst_btop != a->btgt) {
23665 +               err = au_pin(&pin, a->dst_dentry, a->btgt,
23666 +                            au_opt_udba(a->dst_dentry->d_sb),
23667 +                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
23668 +               if (!err) {
23669 +                       struct au_cp_generic cpg = {
23670 +                               .dentry = a->dst_dentry,
23671 +                               .bdst   = a->btgt,
23672 +                               .bsrc   = a->dst_btop,
23673 +                               .len    = -1,
23674 +                               .pin    = &pin,
23675 +                               .flags  = AuCpup_DTIME | AuCpup_HOPEN
23676 +                       };
23677 +                       err = au_sio_cpup_simple(&cpg);
23678 +                       au_unpin(&pin);
23679 +               }
23680 +               if (unlikely(err))
23681 +                       goto out_children;
23682 +               a->dst_btop = a->btgt;
23683 +               a->dst_h_dentry = au_h_dptr(a->dst_dentry, a->btgt);
23684 +       }
23685 +
23686 +       /* lock them all */
23687 +       err = au_ren_lock(a);
23688 +       if (unlikely(err))
23689 +               /* leave the copied-up one */
23690 +               goto out_children;
23691 +
23692 +       if (!a->exchange) {
23693 +               if (!au_opt_test(au_mntflags(a->dst_dir->i_sb), UDBA_NONE))
23694 +                       err = au_may_ren(a);
23695 +               else if (unlikely(a->dst_dentry->d_name.len > AUFS_MAX_NAMELEN))
23696 +                       err = -ENAMETOOLONG;
23697 +               if (unlikely(err))
23698 +                       goto out_hdir;
23699 +       }
23700 +
23701 +       /* store timestamps to be revertible */
23702 +       au_ren_dt(a);
23703 +
23704 +       /* store dirren info */
23705 +       if (au_ftest_ren(a->auren_flags, DIRREN)) {
23706 +               err = au_dr_rename(a->src_dentry, a->btgt,
23707 +                                  &a->dst_dentry->d_name, &rev);
23708 +               AuTraceErr(err);
23709 +               if (unlikely(err))
23710 +                       goto out_dt;
23711 +       }
23712 +
23713 +       /* here we go */
23714 +       err = do_rename(a);
23715 +       if (unlikely(err))
23716 +               goto out_dirren;
23717 +
23718 +       if (au_ftest_ren(a->auren_flags, DIRREN))
23719 +               au_dr_rename_fin(a->src_dentry, a->btgt, rev);
23720 +
23721 +       /* update dir attributes */
23722 +       au_ren_refresh_dir(a);
23723 +
23724 +       /* dput/iput all lower dentries */
23725 +       au_ren_refresh(a);
23726 +
23727 +       goto out_hdir; /* success */
23728 +
23729 +out_dirren:
23730 +       if (au_ftest_ren(a->auren_flags, DIRREN))
23731 +               au_dr_rename_rev(a->src_dentry, a->btgt, rev);
23732 +out_dt:
23733 +       au_ren_rev_dt(err, a);
23734 +out_hdir:
23735 +       au_ren_unlock(a);
23736 +out_children:
23737 +       au_nhash_wh_free(&a->whlist);
23738 +       if (err && a->dst_inode && a->dst_btop != a->btgt) {
23739 +               AuDbg("btop %d, btgt %d\n", a->dst_btop, a->btgt);
23740 +               au_set_h_dptr(a->dst_dentry, a->btgt, NULL);
23741 +               au_set_dbtop(a->dst_dentry, a->dst_btop);
23742 +       }
23743 +out_parent:
23744 +       if (!err) {
23745 +               if (d_unhashed(a->src_dentry))
23746 +                       au_fset_ren(a->auren_flags, DROPPED_SRC);
23747 +               if (d_unhashed(a->dst_dentry))
23748 +                       au_fset_ren(a->auren_flags, DROPPED_DST);
23749 +               if (!a->exchange)
23750 +                       d_move(a->src_dentry, a->dst_dentry);
23751 +               else {
23752 +                       d_exchange(a->src_dentry, a->dst_dentry);
23753 +                       if (au_ftest_ren(a->auren_flags, DROPPED_DST))
23754 +                               d_drop(a->dst_dentry);
23755 +               }
23756 +               if (au_ftest_ren(a->auren_flags, DROPPED_SRC))
23757 +                       d_drop(a->src_dentry);
23758 +       } else {
23759 +               au_update_dbtop(a->dst_dentry);
23760 +               if (!a->dst_inode)
23761 +                       d_drop(a->dst_dentry);
23762 +       }
23763 +       if (au_ftest_ren(a->auren_flags, ISSAMEDIR))
23764 +               di_write_unlock(a->dst_parent);
23765 +       else
23766 +               di_write_unlock2(a->src_parent, a->dst_parent);
23767 +out_unlock:
23768 +       aufs_read_and_write_unlock2(a->dst_dentry, a->src_dentry);
23769 +out_free:
23770 +       iput(a->dst_inode);
23771 +       if (a->thargs)
23772 +               au_whtmp_rmdir_free(a->thargs);
23773 +       au_kfree_rcu(a);
23774 +out:
23775 +       AuTraceErr(err);
23776 +       return err;
23777 +}
23778 diff -urN /usr/share/empty/fs/aufs/Kconfig linux/fs/aufs/Kconfig
23779 --- /usr/share/empty/fs/aufs/Kconfig    1970-01-01 01:00:00.000000000 +0100
23780 +++ linux/fs/aufs/Kconfig       2021-11-01 23:48:34.203025928 +0100
23781 @@ -0,0 +1,199 @@
23782 +# SPDX-License-Identifier: GPL-2.0
23783 +config AUFS_FS
23784 +       tristate "Aufs (Advanced multi layered unification filesystem) support"
23785 +       help
23786 +       Aufs is a stackable unification filesystem such as Unionfs,
23787 +       which unifies several directories and provides a merged single
23788 +       directory.
23789 +       In the early days, aufs was entirely re-designed and
23790 +       re-implemented Unionfs Version 1.x series. Introducing many
23791 +       original ideas, approaches and improvements, it becomes totally
23792 +       different from Unionfs while keeping the basic features.
23793 +
23794 +if AUFS_FS
23795 +choice
23796 +       prompt "Maximum number of branches"
23797 +       default AUFS_BRANCH_MAX_127
23798 +       help
23799 +       Specifies the maximum number of branches (or member directories)
23800 +       in a single aufs. The larger value consumes more system
23801 +       resources and has a minor impact to performance.
23802 +config AUFS_BRANCH_MAX_127
23803 +       bool "127"
23804 +       help
23805 +       Specifies the maximum number of branches (or member directories)
23806 +       in a single aufs. The larger value consumes more system
23807 +       resources and has a minor impact to performance.
23808 +config AUFS_BRANCH_MAX_511
23809 +       bool "511"
23810 +       help
23811 +       Specifies the maximum number of branches (or member directories)
23812 +       in a single aufs. The larger value consumes more system
23813 +       resources and has a minor impact to performance.
23814 +config AUFS_BRANCH_MAX_1023
23815 +       bool "1023"
23816 +       help
23817 +       Specifies the maximum number of branches (or member directories)
23818 +       in a single aufs. The larger value consumes more system
23819 +       resources and has a minor impact to performance.
23820 +config AUFS_BRANCH_MAX_32767
23821 +       bool "32767"
23822 +       help
23823 +       Specifies the maximum number of branches (or member directories)
23824 +       in a single aufs. The larger value consumes more system
23825 +       resources and has a minor impact to performance.
23826 +endchoice
23827 +
23828 +config AUFS_SBILIST
23829 +       bool
23830 +       depends on AUFS_MAGIC_SYSRQ || PROC_FS
23831 +       default y
23832 +       help
23833 +       Automatic configuration for internal use.
23834 +       When aufs supports Magic SysRq or /proc, enabled automatically.
23835 +
23836 +config AUFS_HNOTIFY
23837 +       bool "Detect direct branch access (bypassing aufs)"
23838 +       help
23839 +       If you want to modify files on branches directly, eg. bypassing aufs,
23840 +       and want aufs to detect the changes of them fully, then enable this
23841 +       option and use 'udba=notify' mount option.
23842 +       Currently there is only one available configuration, "fsnotify".
23843 +       It will have a negative impact to the performance.
23844 +       See detail in aufs.5.
23845 +
23846 +choice
23847 +       prompt "method" if AUFS_HNOTIFY
23848 +       default AUFS_HFSNOTIFY
23849 +config AUFS_HFSNOTIFY
23850 +       bool "fsnotify"
23851 +       select FSNOTIFY
23852 +endchoice
23853 +
23854 +config AUFS_EXPORT
23855 +       bool "NFS-exportable aufs"
23856 +       depends on EXPORTFS
23857 +       help
23858 +       If you want to export your mounted aufs via NFS, then enable this
23859 +       option. There are several requirements for this configuration.
23860 +       See detail in aufs.5.
23861 +
23862 +config AUFS_INO_T_64
23863 +       bool
23864 +       depends on AUFS_EXPORT
23865 +       depends on 64BIT && !(ALPHA || S390)
23866 +       default y
23867 +       help
23868 +       Automatic configuration for internal use.
23869 +       /* typedef unsigned long/int __kernel_ino_t */
23870 +       /* alpha and s390x are int */
23871 +
23872 +config AUFS_XATTR
23873 +       bool "support for XATTR/EA (including Security Labels)"
23874 +       help
23875 +       If your branch fs supports XATTR/EA and you want to make them
23876 +       available in aufs too, then enable this opsion and specify the
23877 +       branch attributes for EA.
23878 +       See detail in aufs.5.
23879 +
23880 +config AUFS_FHSM
23881 +       bool "File-based Hierarchical Storage Management"
23882 +       help
23883 +       Hierarchical Storage Management (or HSM) is a well-known feature
23884 +       in the storage world. Aufs provides this feature as file-based.
23885 +       with multiple branches.
23886 +       These multiple branches are prioritized, ie. the topmost one
23887 +       should be the fastest drive and be used heavily.
23888 +
23889 +config AUFS_RDU
23890 +       bool "Readdir in userspace"
23891 +       help
23892 +       Aufs has two methods to provide a merged view for a directory,
23893 +       by a user-space library and by kernel-space natively. The latter
23894 +       is always enabled but sometimes large and slow.
23895 +       If you enable this option, install the library in aufs2-util
23896 +       package, and set some environment variables for your readdir(3),
23897 +       then the work will be handled in user-space which generally
23898 +       shows better performance in most cases.
23899 +       See detail in aufs.5.
23900 +
23901 +config AUFS_DIRREN
23902 +       bool "Workaround for rename(2)-ing a directory"
23903 +       help
23904 +       By default, aufs returns EXDEV error in renameing a dir who has
23905 +       his child on the lower branch, since it is a bad idea to issue
23906 +       rename(2) internally for every lower branch. But user may not
23907 +       accept this behaviour. So here is a workaround to allow such
23908 +       rename(2) and store some extra infromation on the writable
23909 +       branch. Obviously this costs high (and I don't like it).
23910 +       To use this feature, you need to enable this configuration AND
23911 +       to specify the mount option `dirren.'
23912 +       See details in aufs.5 and the design documents.
23913 +
23914 +config AUFS_SHWH
23915 +       bool "Show whiteouts"
23916 +       help
23917 +       If you want to make the whiteouts in aufs visible, then enable
23918 +       this option and specify 'shwh' mount option. Although it may
23919 +       sounds like philosophy or something, but in technically it
23920 +       simply shows the name of whiteout with keeping its behaviour.
23921 +
23922 +config AUFS_BR_RAMFS
23923 +       bool "Ramfs (initramfs/rootfs) as an aufs branch"
23924 +       help
23925 +       If you want to use ramfs as an aufs branch fs, then enable this
23926 +       option. Generally tmpfs is recommended.
23927 +       Aufs prohibited them to be a branch fs by default, because
23928 +       initramfs becomes unusable after switch_root or something
23929 +       generally. If you sets initramfs as an aufs branch and boot your
23930 +       system by switch_root, you will meet a problem easily since the
23931 +       files in initramfs may be inaccessible.
23932 +       Unless you are going to use ramfs as an aufs branch fs without
23933 +       switch_root or something, leave it N.
23934 +
23935 +config AUFS_BR_FUSE
23936 +       bool "Fuse fs as an aufs branch"
23937 +       depends on FUSE_FS
23938 +       select AUFS_POLL
23939 +       help
23940 +       If you want to use fuse-based userspace filesystem as an aufs
23941 +       branch fs, then enable this option.
23942 +       It implements the internal poll(2) operation which is
23943 +       implemented by fuse only (curretnly).
23944 +
23945 +config AUFS_POLL
23946 +       bool
23947 +       help
23948 +       Automatic configuration for internal use.
23949 +
23950 +config AUFS_BR_HFSPLUS
23951 +       bool "Hfsplus as an aufs branch"
23952 +       depends on HFSPLUS_FS
23953 +       default y
23954 +       help
23955 +       If you want to use hfsplus fs as an aufs branch fs, then enable
23956 +       this option. This option introduces a small overhead at
23957 +       copying-up a file on hfsplus.
23958 +
23959 +config AUFS_BDEV_LOOP
23960 +       bool
23961 +       depends on BLK_DEV_LOOP
23962 +       default y
23963 +       help
23964 +       Automatic configuration for internal use.
23965 +       Convert =[ym] into =y.
23966 +
23967 +config AUFS_DEBUG
23968 +       bool "Debug aufs"
23969 +       help
23970 +       Enable this to compile aufs internal debug code.
23971 +       It will have a negative impact to the performance.
23972 +
23973 +config AUFS_MAGIC_SYSRQ
23974 +       bool
23975 +       depends on AUFS_DEBUG && MAGIC_SYSRQ
23976 +       default y
23977 +       help
23978 +       Automatic configuration for internal use.
23979 +       When aufs supports Magic SysRq, enabled automatically.
23980 +endif
23981 diff -urN /usr/share/empty/fs/aufs/lcnt.h linux/fs/aufs/lcnt.h
23982 --- /usr/share/empty/fs/aufs/lcnt.h     1970-01-01 01:00:00.000000000 +0100
23983 +++ linux/fs/aufs/lcnt.h        2021-11-01 23:48:34.209692595 +0100
23984 @@ -0,0 +1,186 @@
23985 +/* SPDX-License-Identifier: GPL-2.0 */
23986 +/*
23987 + * Copyright (C) 2018-2021 Junjiro R. Okajima
23988 + *
23989 + * This program, aufs is free software; you can redistribute it and/or modify
23990 + * it under the terms of the GNU General Public License as published by
23991 + * the Free Software Foundation; either version 2 of the License, or
23992 + * (at your option) any later version.
23993 + *
23994 + * This program is distributed in the hope that it will be useful,
23995 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
23996 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23997 + * GNU General Public License for more details.
23998 + *
23999 + * You should have received a copy of the GNU General Public License
24000 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24001 + */
24002 +
24003 +/*
24004 + * simple long counter wrapper
24005 + */
24006 +
24007 +#ifndef __AUFS_LCNT_H__
24008 +#define __AUFS_LCNT_H__
24009 +
24010 +#ifdef __KERNEL__
24011 +
24012 +#include "debug.h"
24013 +
24014 +#define AuLCntATOMIC   1
24015 +#define AuLCntPCPUCNT  2
24016 +/*
24017 + * why does percpu_refcount require extra synchronize_rcu()s in
24018 + * au_br_do_free()
24019 + */
24020 +#define AuLCntPCPUREF  3
24021 +
24022 +/* #define AuLCntChosen        AuLCntATOMIC */
24023 +#define AuLCntChosen   AuLCntPCPUCNT
24024 +/* #define AuLCntChosen        AuLCntPCPUREF */
24025 +
24026 +#if AuLCntChosen == AuLCntATOMIC
24027 +#include <linux/atomic.h>
24028 +
24029 +typedef atomic_long_t au_lcnt_t;
24030 +
24031 +static inline int au_lcnt_init(au_lcnt_t *cnt, void *release __maybe_unused)
24032 +{
24033 +       atomic_long_set(cnt, 0);
24034 +       return 0;
24035 +}
24036 +
24037 +static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
24038 +{
24039 +       /* empty */
24040 +}
24041 +
24042 +static inline void au_lcnt_fin(au_lcnt_t *cnt __maybe_unused,
24043 +                              int do_sync __maybe_unused)
24044 +{
24045 +       /* empty */
24046 +}
24047 +
24048 +static inline void au_lcnt_inc(au_lcnt_t *cnt)
24049 +{
24050 +       atomic_long_inc(cnt);
24051 +}
24052 +
24053 +static inline void au_lcnt_dec(au_lcnt_t *cnt)
24054 +{
24055 +       atomic_long_dec(cnt);
24056 +}
24057 +
24058 +static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev __maybe_unused)
24059 +{
24060 +       return atomic_long_read(cnt);
24061 +}
24062 +#endif
24063 +
24064 +#if AuLCntChosen == AuLCntPCPUCNT
24065 +#include <linux/percpu_counter.h>
24066 +
24067 +typedef struct percpu_counter au_lcnt_t;
24068 +
24069 +static inline int au_lcnt_init(au_lcnt_t *cnt, void *release __maybe_unused)
24070 +{
24071 +       return percpu_counter_init(cnt, 0, GFP_NOFS);
24072 +}
24073 +
24074 +static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
24075 +{
24076 +       /* empty */
24077 +}
24078 +
24079 +static inline void au_lcnt_fin(au_lcnt_t *cnt, int do_sync __maybe_unused)
24080 +{
24081 +       percpu_counter_destroy(cnt);
24082 +}
24083 +
24084 +static inline void au_lcnt_inc(au_lcnt_t *cnt)
24085 +{
24086 +       percpu_counter_inc(cnt);
24087 +}
24088 +
24089 +static inline void au_lcnt_dec(au_lcnt_t *cnt)
24090 +{
24091 +       percpu_counter_dec(cnt);
24092 +}
24093 +
24094 +static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev __maybe_unused)
24095 +{
24096 +       s64 n;
24097 +
24098 +       n = percpu_counter_sum(cnt);
24099 +       BUG_ON(n < 0);
24100 +       if (LONG_MAX != LLONG_MAX
24101 +           && n > LONG_MAX)
24102 +               AuWarn1("%s\n", "wrap-around");
24103 +
24104 +       return n;
24105 +}
24106 +#endif
24107 +
24108 +#if AuLCntChosen == AuLCntPCPUREF
24109 +#include <linux/percpu-refcount.h>
24110 +
24111 +typedef struct percpu_ref au_lcnt_t;
24112 +
24113 +static inline int au_lcnt_init(au_lcnt_t *cnt, percpu_ref_func_t *release)
24114 +{
24115 +       if (!release)
24116 +               release = percpu_ref_exit;
24117 +       return percpu_ref_init(cnt, release, /*percpu mode*/0, GFP_NOFS);
24118 +}
24119 +
24120 +static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
24121 +{
24122 +       synchronize_rcu();
24123 +}
24124 +
24125 +static inline void au_lcnt_fin(au_lcnt_t *cnt, int do_sync)
24126 +{
24127 +       percpu_ref_kill(cnt);
24128 +       if (do_sync)
24129 +               au_lcnt_wait_for_fin(cnt);
24130 +}
24131 +
24132 +static inline void au_lcnt_inc(au_lcnt_t *cnt)
24133 +{
24134 +       percpu_ref_get(cnt);
24135 +}
24136 +
24137 +static inline void au_lcnt_dec(au_lcnt_t *cnt)
24138 +{
24139 +       percpu_ref_put(cnt);
24140 +}
24141 +
24142 +/*
24143 + * avoid calling this func as possible.
24144 + */
24145 +static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev)
24146 +{
24147 +       long l;
24148 +
24149 +       percpu_ref_switch_to_atomic_sync(cnt);
24150 +       l = atomic_long_read(&cnt->count);
24151 +       if (do_rev)
24152 +               percpu_ref_switch_to_percpu(cnt);
24153 +
24154 +       /* percpu_ref is initialized by 1 instead of 0 */
24155 +       return l - 1;
24156 +}
24157 +#endif
24158 +
24159 +#ifdef CONFIG_AUFS_DEBUG
24160 +#define AuLCntZero(val) do {                   \
24161 +       long l = val;                           \
24162 +       if (l)                                  \
24163 +               AuDbg("%s = %ld\n", #val, l);   \
24164 +} while (0)
24165 +#else
24166 +#define AuLCntZero(val)                do {} while (0)
24167 +#endif
24168 +
24169 +#endif /* __KERNEL__ */
24170 +#endif /* __AUFS_LCNT_H__ */
24171 diff -urN /usr/share/empty/fs/aufs/loop.c linux/fs/aufs/loop.c
24172 --- /usr/share/empty/fs/aufs/loop.c     1970-01-01 01:00:00.000000000 +0100
24173 +++ linux/fs/aufs/loop.c        2021-11-01 23:48:34.209692595 +0100
24174 @@ -0,0 +1,148 @@
24175 +// SPDX-License-Identifier: GPL-2.0
24176 +/*
24177 + * Copyright (C) 2005-2021 Junjiro R. Okajima
24178 + *
24179 + * This program, aufs is free software; you can redistribute it and/or modify
24180 + * it under the terms of the GNU General Public License as published by
24181 + * the Free Software Foundation; either version 2 of the License, or
24182 + * (at your option) any later version.
24183 + *
24184 + * This program is distributed in the hope that it will be useful,
24185 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
24186 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24187 + * GNU General Public License for more details.
24188 + *
24189 + * You should have received a copy of the GNU General Public License
24190 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24191 + */
24192 +
24193 +/*
24194 + * support for loopback block device as a branch
24195 + */
24196 +
24197 +#include "aufs.h"
24198 +
24199 +/* added into drivers/block/loop.c */
24200 +static struct file *(*backing_file_func)(struct super_block *sb);
24201 +
24202 +/*
24203 + * test if two lower dentries have overlapping branches.
24204 + */
24205 +int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding)
24206 +{
24207 +       struct super_block *h_sb;
24208 +       struct file *backing_file;
24209 +
24210 +       if (unlikely(!backing_file_func)) {
24211 +               /* don't load "loop" module here */
24212 +               backing_file_func = symbol_get(loop_backing_file);
24213 +               if (unlikely(!backing_file_func))
24214 +                       /* "loop" module is not loaded */
24215 +                       return 0;
24216 +       }
24217 +
24218 +       h_sb = h_adding->d_sb;
24219 +       backing_file = backing_file_func(h_sb);
24220 +       if (!backing_file)
24221 +               return 0;
24222 +
24223 +       h_adding = backing_file->f_path.dentry;
24224 +       /*
24225 +        * h_adding can be local NFS.
24226 +        * in this case aufs cannot detect the loop.
24227 +        */
24228 +       if (unlikely(h_adding->d_sb == sb))
24229 +               return 1;
24230 +       return !!au_test_subdir(h_adding, sb->s_root);
24231 +}
24232 +
24233 +/* true if a kernel thread named 'loop[0-9].*' accesses a file */
24234 +int au_test_loopback_kthread(void)
24235 +{
24236 +       int ret;
24237 +       struct task_struct *tsk = current;
24238 +       char c, comm[sizeof(tsk->comm)];
24239 +
24240 +       ret = 0;
24241 +       if (tsk->flags & PF_KTHREAD) {
24242 +               get_task_comm(comm, tsk);
24243 +               c = comm[4];
24244 +               ret = ('0' <= c && c <= '9'
24245 +                      && !strncmp(comm, "loop", 4));
24246 +       }
24247 +
24248 +       return ret;
24249 +}
24250 +
24251 +/* ---------------------------------------------------------------------- */
24252 +
24253 +#define au_warn_loopback_step  16
24254 +static int au_warn_loopback_nelem = au_warn_loopback_step;
24255 +static unsigned long *au_warn_loopback_array;
24256 +
24257 +void au_warn_loopback(struct super_block *h_sb)
24258 +{
24259 +       int i, new_nelem;
24260 +       unsigned long *a, magic;
24261 +       static DEFINE_SPINLOCK(spin);
24262 +
24263 +       magic = h_sb->s_magic;
24264 +       spin_lock(&spin);
24265 +       a = au_warn_loopback_array;
24266 +       for (i = 0; i < au_warn_loopback_nelem && *a; i++)
24267 +               if (a[i] == magic) {
24268 +                       spin_unlock(&spin);
24269 +                       return;
24270 +               }
24271 +
24272 +       /* h_sb is new to us, print it */
24273 +       if (i < au_warn_loopback_nelem) {
24274 +               a[i] = magic;
24275 +               goto pr;
24276 +       }
24277 +
24278 +       /* expand the array */
24279 +       new_nelem = au_warn_loopback_nelem + au_warn_loopback_step;
24280 +       a = au_kzrealloc(au_warn_loopback_array,
24281 +                        au_warn_loopback_nelem * sizeof(unsigned long),
24282 +                        new_nelem * sizeof(unsigned long), GFP_ATOMIC,
24283 +                        /*may_shrink*/0);
24284 +       if (a) {
24285 +               au_warn_loopback_nelem = new_nelem;
24286 +               au_warn_loopback_array = a;
24287 +               a[i] = magic;
24288 +               goto pr;
24289 +       }
24290 +
24291 +       spin_unlock(&spin);
24292 +       AuWarn1("realloc failed, ignored\n");
24293 +       return;
24294 +
24295 +pr:
24296 +       spin_unlock(&spin);
24297 +       pr_warn("you may want to try another patch for loopback file "
24298 +               "on %s(0x%lx) branch\n", au_sbtype(h_sb), magic);
24299 +}
24300 +
24301 +int au_loopback_init(void)
24302 +{
24303 +       int err;
24304 +       struct super_block *sb __maybe_unused;
24305 +
24306 +       BUILD_BUG_ON(sizeof(sb->s_magic) != sizeof(*au_warn_loopback_array));
24307 +
24308 +       err = 0;
24309 +       au_warn_loopback_array = kcalloc(au_warn_loopback_step,
24310 +                                        sizeof(unsigned long), GFP_NOFS);
24311 +       if (unlikely(!au_warn_loopback_array))
24312 +               err = -ENOMEM;
24313 +
24314 +       return err;
24315 +}
24316 +
24317 +void au_loopback_fin(void)
24318 +{
24319 +       if (backing_file_func)
24320 +               symbol_put(loop_backing_file);
24321 +       au_kfree_try_rcu(au_warn_loopback_array);
24322 +}
24323 diff -urN /usr/share/empty/fs/aufs/loop.h linux/fs/aufs/loop.h
24324 --- /usr/share/empty/fs/aufs/loop.h     1970-01-01 01:00:00.000000000 +0100
24325 +++ linux/fs/aufs/loop.h        2021-11-01 23:48:34.209692595 +0100
24326 @@ -0,0 +1,55 @@
24327 +/* SPDX-License-Identifier: GPL-2.0 */
24328 +/*
24329 + * Copyright (C) 2005-2021 Junjiro R. Okajima
24330 + *
24331 + * This program, aufs is free software; you can redistribute it and/or modify
24332 + * it under the terms of the GNU General Public License as published by
24333 + * the Free Software Foundation; either version 2 of the License, or
24334 + * (at your option) any later version.
24335 + *
24336 + * This program is distributed in the hope that it will be useful,
24337 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
24338 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24339 + * GNU General Public License for more details.
24340 + *
24341 + * You should have received a copy of the GNU General Public License
24342 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24343 + */
24344 +
24345 +/*
24346 + * support for loopback mount as a branch
24347 + */
24348 +
24349 +#ifndef __AUFS_LOOP_H__
24350 +#define __AUFS_LOOP_H__
24351 +
24352 +#ifdef __KERNEL__
24353 +
24354 +struct dentry;
24355 +struct super_block;
24356 +
24357 +#ifdef CONFIG_AUFS_BDEV_LOOP
24358 +/* drivers/block/loop.c */
24359 +struct file *loop_backing_file(struct super_block *sb);
24360 +
24361 +/* loop.c */
24362 +int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding);
24363 +int au_test_loopback_kthread(void);
24364 +void au_warn_loopback(struct super_block *h_sb);
24365 +
24366 +int au_loopback_init(void);
24367 +void au_loopback_fin(void);
24368 +#else
24369 +AuStub(struct file *, loop_backing_file, return NULL, struct super_block *sb)
24370 +
24371 +AuStubInt0(au_test_loopback_overlap, struct super_block *sb,
24372 +          struct dentry *h_adding)
24373 +AuStubInt0(au_test_loopback_kthread, void)
24374 +AuStubVoid(au_warn_loopback, struct super_block *h_sb)
24375 +
24376 +AuStubInt0(au_loopback_init, void)
24377 +AuStubVoid(au_loopback_fin, void)
24378 +#endif /* BLK_DEV_LOOP */
24379 +
24380 +#endif /* __KERNEL__ */
24381 +#endif /* __AUFS_LOOP_H__ */
24382 diff -urN /usr/share/empty/fs/aufs/magic.mk linux/fs/aufs/magic.mk
24383 --- /usr/share/empty/fs/aufs/magic.mk   1970-01-01 01:00:00.000000000 +0100
24384 +++ linux/fs/aufs/magic.mk      2021-11-01 23:48:34.209692595 +0100
24385 @@ -0,0 +1,31 @@
24386 +# SPDX-License-Identifier: GPL-2.0
24387 +
24388 +# defined in ${srctree}/fs/fuse/inode.c
24389 +# tristate
24390 +ifdef CONFIG_FUSE_FS
24391 +ccflags-y += -DFUSE_SUPER_MAGIC=0x65735546
24392 +endif
24393 +
24394 +# defined in ${srctree}/fs/xfs/xfs_sb.h
24395 +# tristate
24396 +ifdef CONFIG_XFS_FS
24397 +ccflags-y += -DXFS_SB_MAGIC=0x58465342
24398 +endif
24399 +
24400 +# defined in ${srctree}/fs/configfs/mount.c
24401 +# tristate
24402 +ifdef CONFIG_CONFIGFS_FS
24403 +ccflags-y += -DCONFIGFS_MAGIC=0x62656570
24404 +endif
24405 +
24406 +# defined in ${srctree}/fs/ubifs/ubifs.h
24407 +# tristate
24408 +ifdef CONFIG_UBIFS_FS
24409 +ccflags-y += -DUBIFS_SUPER_MAGIC=0x24051905
24410 +endif
24411 +
24412 +# defined in ${srctree}/fs/hfsplus/hfsplus_raw.h
24413 +# tristate
24414 +ifdef CONFIG_HFSPLUS_FS
24415 +ccflags-y += -DHFSPLUS_SUPER_MAGIC=0x482b
24416 +endif
24417 diff -urN /usr/share/empty/fs/aufs/Makefile linux/fs/aufs/Makefile
24418 --- /usr/share/empty/fs/aufs/Makefile   1970-01-01 01:00:00.000000000 +0100
24419 +++ linux/fs/aufs/Makefile      2021-11-01 23:48:34.203025928 +0100
24420 @@ -0,0 +1,46 @@
24421 +# SPDX-License-Identifier: GPL-2.0
24422 +
24423 +include ${src}/magic.mk
24424 +ifeq (${CONFIG_AUFS_FS},m)
24425 +include ${src}/conf.mk
24426 +endif
24427 +-include ${src}/priv_def.mk
24428 +
24429 +# cf. include/linux/kernel.h
24430 +# enable pr_debug
24431 +ccflags-y += -DDEBUG
24432 +# sparse requires the full pathname
24433 +ifdef M
24434 +ccflags-y += -include ${M}/../../include/uapi/linux/aufs_type.h
24435 +else
24436 +ccflags-y += -include ${srctree}/include/uapi/linux/aufs_type.h
24437 +endif
24438 +
24439 +obj-$(CONFIG_AUFS_FS) += aufs.o
24440 +aufs-y := module.o sbinfo.o super.o branch.o xino.o sysaufs.o opts.o \
24441 +       wkq.o vfsub.o dcsub.o \
24442 +       cpup.o whout.o wbr_policy.o \
24443 +       dinfo.o dentry.o \
24444 +       dynop.o \
24445 +       finfo.o file.o f_op.o \
24446 +       dir.o vdir.o \
24447 +       iinfo.o inode.o i_op.o i_op_add.o i_op_del.o i_op_ren.o \
24448 +       mvdown.o ioctl.o
24449 +
24450 +# all are boolean
24451 +aufs-$(CONFIG_PROC_FS) += procfs.o plink.o
24452 +aufs-$(CONFIG_SYSFS) += sysfs.o
24453 +aufs-$(CONFIG_DEBUG_FS) += dbgaufs.o
24454 +aufs-$(CONFIG_AUFS_BDEV_LOOP) += loop.o
24455 +aufs-$(CONFIG_AUFS_HNOTIFY) += hnotify.o
24456 +aufs-$(CONFIG_AUFS_HFSNOTIFY) += hfsnotify.o
24457 +aufs-$(CONFIG_AUFS_EXPORT) += export.o
24458 +aufs-$(CONFIG_AUFS_XATTR) += xattr.o
24459 +aufs-$(CONFIG_FS_POSIX_ACL) += posix_acl.o
24460 +aufs-$(CONFIG_AUFS_DIRREN) += dirren.o
24461 +aufs-$(CONFIG_AUFS_FHSM) += fhsm.o
24462 +aufs-$(CONFIG_AUFS_POLL) += poll.o
24463 +aufs-$(CONFIG_AUFS_RDU) += rdu.o
24464 +aufs-$(CONFIG_AUFS_BR_HFSPLUS) += hfsplus.o
24465 +aufs-$(CONFIG_AUFS_DEBUG) += debug.o
24466 +aufs-$(CONFIG_AUFS_MAGIC_SYSRQ) += sysrq.o
24467 diff -urN /usr/share/empty/fs/aufs/module.c linux/fs/aufs/module.c
24468 --- /usr/share/empty/fs/aufs/module.c   1970-01-01 01:00:00.000000000 +0100
24469 +++ linux/fs/aufs/module.c      2021-11-01 23:48:34.209692595 +0100
24470 @@ -0,0 +1,273 @@
24471 +// SPDX-License-Identifier: GPL-2.0
24472 +/*
24473 + * Copyright (C) 2005-2021 Junjiro R. Okajima
24474 + *
24475 + * This program, aufs is free software; you can redistribute it and/or modify
24476 + * it under the terms of the GNU General Public License as published by
24477 + * the Free Software Foundation; either version 2 of the License, or
24478 + * (at your option) any later version.
24479 + *
24480 + * This program is distributed in the hope that it will be useful,
24481 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
24482 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24483 + * GNU General Public License for more details.
24484 + *
24485 + * You should have received a copy of the GNU General Public License
24486 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24487 + */
24488 +
24489 +/*
24490 + * module global variables and operations
24491 + */
24492 +
24493 +#include <linux/module.h>
24494 +#include <linux/seq_file.h>
24495 +#include "aufs.h"
24496 +
24497 +/* shrinkable realloc */
24498 +void *au_krealloc(void *p, unsigned int new_sz, gfp_t gfp, int may_shrink)
24499 +{
24500 +       size_t sz;
24501 +       int diff;
24502 +
24503 +       sz = 0;
24504 +       diff = -1;
24505 +       if (p) {
24506 +#if 0 /* unused */
24507 +               if (!new_sz) {
24508 +                       au_kfree_rcu(p);
24509 +                       p = NULL;
24510 +                       goto out;
24511 +               }
24512 +#else
24513 +               AuDebugOn(!new_sz);
24514 +#endif
24515 +               sz = ksize(p);
24516 +               diff = au_kmidx_sub(sz, new_sz);
24517 +       }
24518 +       if (sz && !diff)
24519 +               goto out;
24520 +
24521 +       if (sz < new_sz)
24522 +               /* expand or SLOB */
24523 +               p = krealloc(p, new_sz, gfp);
24524 +       else if (new_sz < sz && may_shrink) {
24525 +               /* shrink */
24526 +               void *q;
24527 +
24528 +               q = kmalloc(new_sz, gfp);
24529 +               if (q) {
24530 +                       if (p) {
24531 +                               memcpy(q, p, new_sz);
24532 +                               au_kfree_try_rcu(p);
24533 +                       }
24534 +                       p = q;
24535 +               } else
24536 +                       p = NULL;
24537 +       }
24538 +
24539 +out:
24540 +       return p;
24541 +}
24542 +
24543 +void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp,
24544 +                  int may_shrink)
24545 +{
24546 +       p = au_krealloc(p, new_sz, gfp, may_shrink);
24547 +       if (p && new_sz > nused)
24548 +               memset(p + nused, 0, new_sz - nused);
24549 +       return p;
24550 +}
24551 +
24552 +/* ---------------------------------------------------------------------- */
24553 +/*
24554 + * aufs caches
24555 + */
24556 +struct kmem_cache *au_cache[AuCache_Last];
24557 +
24558 +static void au_cache_fin(void)
24559 +{
24560 +       int i;
24561 +
24562 +       /*
24563 +        * Make sure all delayed rcu free inodes are flushed before we
24564 +        * destroy cache.
24565 +        */
24566 +       rcu_barrier();
24567 +
24568 +       /* excluding AuCache_HNOTIFY */
24569 +       BUILD_BUG_ON(AuCache_HNOTIFY + 1 != AuCache_Last);
24570 +       for (i = 0; i < AuCache_HNOTIFY; i++) {
24571 +               kmem_cache_destroy(au_cache[i]);
24572 +               au_cache[i] = NULL;
24573 +       }
24574 +}
24575 +
24576 +static int __init au_cache_init(void)
24577 +{
24578 +       au_cache[AuCache_DINFO] = AuCacheCtor(au_dinfo, au_di_init_once);
24579 +       if (au_cache[AuCache_DINFO])
24580 +               /* SLAB_DESTROY_BY_RCU */
24581 +               au_cache[AuCache_ICNTNR] = AuCacheCtor(au_icntnr,
24582 +                                                      au_icntnr_init_once);
24583 +       if (au_cache[AuCache_ICNTNR])
24584 +               au_cache[AuCache_FINFO] = AuCacheCtor(au_finfo,
24585 +                                                     au_fi_init_once);
24586 +       if (au_cache[AuCache_FINFO])
24587 +               au_cache[AuCache_VDIR] = AuCache(au_vdir);
24588 +       if (au_cache[AuCache_VDIR])
24589 +               au_cache[AuCache_DEHSTR] = AuCache(au_vdir_dehstr);
24590 +       if (au_cache[AuCache_DEHSTR])
24591 +               return 0;
24592 +
24593 +       au_cache_fin();
24594 +       return -ENOMEM;
24595 +}
24596 +
24597 +/* ---------------------------------------------------------------------- */
24598 +
24599 +int au_dir_roflags;
24600 +
24601 +#ifdef CONFIG_AUFS_SBILIST
24602 +/*
24603 + * iterate_supers_type() doesn't protect us from
24604 + * remounting (branch management)
24605 + */
24606 +struct hlist_bl_head au_sbilist;
24607 +#endif
24608 +
24609 +/*
24610 + * functions for module interface.
24611 + */
24612 +MODULE_LICENSE("GPL");
24613 +/* MODULE_LICENSE("GPL v2"); */
24614 +MODULE_AUTHOR("Junjiro R. Okajima <aufs-users@lists.sourceforge.net>");
24615 +MODULE_DESCRIPTION(AUFS_NAME
24616 +       " -- Advanced multi layered unification filesystem");
24617 +MODULE_VERSION(AUFS_VERSION);
24618 +MODULE_ALIAS_FS(AUFS_NAME);
24619 +
24620 +/* this module parameter has no meaning when SYSFS is disabled */
24621 +int sysaufs_brs = 1;
24622 +MODULE_PARM_DESC(brs, "use <sysfs>/fs/aufs/si_*/brN");
24623 +module_param_named(brs, sysaufs_brs, int, 0444);
24624 +
24625 +/* this module parameter has no meaning when USER_NS is disabled */
24626 +bool au_userns;
24627 +MODULE_PARM_DESC(allow_userns, "allow unprivileged to mount under userns");
24628 +module_param_named(allow_userns, au_userns, bool, 0444);
24629 +
24630 +/* ---------------------------------------------------------------------- */
24631 +
24632 +static char au_esc_chars[0x20 + 3]; /* 0x01-0x20, backslash, del, and NULL */
24633 +
24634 +int au_seq_path(struct seq_file *seq, struct path *path)
24635 +{
24636 +       int err;
24637 +
24638 +       err = seq_path(seq, path, au_esc_chars);
24639 +       if (err >= 0)
24640 +               err = 0;
24641 +       else
24642 +               err = -ENOMEM;
24643 +
24644 +       return err;
24645 +}
24646 +
24647 +/* ---------------------------------------------------------------------- */
24648 +
24649 +static int __init aufs_init(void)
24650 +{
24651 +       int err, i;
24652 +       char *p;
24653 +
24654 +       p = au_esc_chars;
24655 +       for (i = 1; i <= ' '; i++)
24656 +               *p++ = i;
24657 +       *p++ = '\\';
24658 +       *p++ = '\x7f';
24659 +       *p = 0;
24660 +
24661 +       au_dir_roflags = au_file_roflags(O_DIRECTORY | O_LARGEFILE);
24662 +
24663 +       memcpy(aufs_iop_nogetattr, aufs_iop, sizeof(aufs_iop));
24664 +       for (i = 0; i < AuIop_Last; i++)
24665 +               aufs_iop_nogetattr[i].getattr = NULL;
24666 +
24667 +       memset(au_cache, 0, sizeof(au_cache));  /* including hnotify */
24668 +
24669 +       au_sbilist_init();
24670 +       sysaufs_brs_init();
24671 +       au_debug_init();
24672 +       au_dy_init();
24673 +       err = sysaufs_init();
24674 +       if (unlikely(err))
24675 +               goto out;
24676 +       err = dbgaufs_init();
24677 +       if (unlikely(err))
24678 +               goto out_sysaufs;
24679 +       err = au_procfs_init();
24680 +       if (unlikely(err))
24681 +               goto out_dbgaufs;
24682 +       err = au_wkq_init();
24683 +       if (unlikely(err))
24684 +               goto out_procfs;
24685 +       err = au_loopback_init();
24686 +       if (unlikely(err))
24687 +               goto out_wkq;
24688 +       err = au_hnotify_init();
24689 +       if (unlikely(err))
24690 +               goto out_loopback;
24691 +       err = au_sysrq_init();
24692 +       if (unlikely(err))
24693 +               goto out_hin;
24694 +       err = au_cache_init();
24695 +       if (unlikely(err))
24696 +               goto out_sysrq;
24697 +
24698 +       aufs_fs_type.fs_flags |= au_userns ? FS_USERNS_MOUNT : 0;
24699 +       err = register_filesystem(&aufs_fs_type);
24700 +       if (unlikely(err))
24701 +               goto out_cache;
24702 +
24703 +       /* since we define pr_fmt, call printk directly */
24704 +       printk(KERN_INFO AUFS_NAME " " AUFS_VERSION "\n");
24705 +       goto out; /* success */
24706 +
24707 +out_cache:
24708 +       au_cache_fin();
24709 +out_sysrq:
24710 +       au_sysrq_fin();
24711 +out_hin:
24712 +       au_hnotify_fin();
24713 +out_loopback:
24714 +       au_loopback_fin();
24715 +out_wkq:
24716 +       au_wkq_fin();
24717 +out_procfs:
24718 +       au_procfs_fin();
24719 +out_dbgaufs:
24720 +       dbgaufs_fin();
24721 +out_sysaufs:
24722 +       sysaufs_fin();
24723 +       au_dy_fin();
24724 +out:
24725 +       return err;
24726 +}
24727 +
24728 +static void __exit aufs_exit(void)
24729 +{
24730 +       unregister_filesystem(&aufs_fs_type);
24731 +       au_cache_fin();
24732 +       au_sysrq_fin();
24733 +       au_hnotify_fin();
24734 +       au_loopback_fin();
24735 +       au_wkq_fin();
24736 +       au_procfs_fin();
24737 +       dbgaufs_fin();
24738 +       sysaufs_fin();
24739 +       au_dy_fin();
24740 +}
24741 +
24742 +module_init(aufs_init);
24743 +module_exit(aufs_exit);
24744 diff -urN /usr/share/empty/fs/aufs/module.h linux/fs/aufs/module.h
24745 --- /usr/share/empty/fs/aufs/module.h   1970-01-01 01:00:00.000000000 +0100
24746 +++ linux/fs/aufs/module.h      2021-11-01 23:48:34.209692595 +0100
24747 @@ -0,0 +1,166 @@
24748 +/* SPDX-License-Identifier: GPL-2.0 */
24749 +/*
24750 + * Copyright (C) 2005-2021 Junjiro R. Okajima
24751 + *
24752 + * This program, aufs is free software; you can redistribute it and/or modify
24753 + * it under the terms of the GNU General Public License as published by
24754 + * the Free Software Foundation; either version 2 of the License, or
24755 + * (at your option) any later version.
24756 + *
24757 + * This program is distributed in the hope that it will be useful,
24758 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
24759 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24760 + * GNU General Public License for more details.
24761 + *
24762 + * You should have received a copy of the GNU General Public License
24763 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24764 + */
24765 +
24766 +/*
24767 + * module initialization and module-global
24768 + */
24769 +
24770 +#ifndef __AUFS_MODULE_H__
24771 +#define __AUFS_MODULE_H__
24772 +
24773 +#ifdef __KERNEL__
24774 +
24775 +#include <linux/slab.h>
24776 +#include "debug.h"
24777 +#include "dentry.h"
24778 +#include "dir.h"
24779 +#include "file.h"
24780 +#include "inode.h"
24781 +
24782 +struct path;
24783 +struct seq_file;
24784 +
24785 +/* module parameters */
24786 +extern int sysaufs_brs;
24787 +extern bool au_userns;
24788 +
24789 +/* ---------------------------------------------------------------------- */
24790 +
24791 +extern int au_dir_roflags;
24792 +
24793 +void *au_krealloc(void *p, unsigned int new_sz, gfp_t gfp, int may_shrink);
24794 +void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp,
24795 +                  int may_shrink);
24796 +
24797 +/*
24798 + * Comparing the size of the object with sizeof(struct rcu_head)
24799 + * case 1: object is always larger
24800 + *     --> au_kfree_rcu() or au_kfree_do_rcu()
24801 + * case 2: object is always smaller
24802 + *     --> au_kfree_small()
24803 + * case 3: object can be any size
24804 + *     --> au_kfree_try_rcu()
24805 + */
24806 +
24807 +static inline void au_kfree_do_rcu(const void *p)
24808 +{
24809 +       struct {
24810 +               struct rcu_head rcu;
24811 +       } *a = (void *)p;
24812 +
24813 +       kfree_rcu(a, rcu);
24814 +}
24815 +
24816 +#define au_kfree_rcu(_p) do {                                          \
24817 +               typeof(_p) p = (_p);                                    \
24818 +               BUILD_BUG_ON(sizeof(*p) < sizeof(struct rcu_head));     \
24819 +               if (p)                                                  \
24820 +                       au_kfree_do_rcu(p);                             \
24821 +       } while (0)
24822 +
24823 +#define au_kfree_do_sz_test(sz)        (sz >= sizeof(struct rcu_head))
24824 +#define au_kfree_sz_test(p)    (p && au_kfree_do_sz_test(ksize(p)))
24825 +
24826 +static inline void au_kfree_try_rcu(const void *p)
24827 +{
24828 +       if (!p)
24829 +               return;
24830 +       if (au_kfree_sz_test(p))
24831 +               au_kfree_do_rcu(p);
24832 +       else
24833 +               kfree(p);
24834 +}
24835 +
24836 +static inline void au_kfree_small(const void *p)
24837 +{
24838 +       if (!p)
24839 +               return;
24840 +       AuDebugOn(au_kfree_sz_test(p));
24841 +       kfree(p);
24842 +}
24843 +
24844 +static inline int au_kmidx_sub(size_t sz, size_t new_sz)
24845 +{
24846 +#ifndef CONFIG_SLOB
24847 +       return __kmalloc_index(sz, false) - __kmalloc_index(new_sz, false);
24848 +#else
24849 +       return -1; /* SLOB is untested */
24850 +#endif
24851 +}
24852 +
24853 +int au_seq_path(struct seq_file *seq, struct path *path);
24854 +
24855 +#ifdef CONFIG_PROC_FS
24856 +/* procfs.c */
24857 +int __init au_procfs_init(void);
24858 +void au_procfs_fin(void);
24859 +#else
24860 +AuStubInt0(au_procfs_init, void);
24861 +AuStubVoid(au_procfs_fin, void);
24862 +#endif
24863 +
24864 +/* ---------------------------------------------------------------------- */
24865 +
24866 +/* kmem cache */
24867 +enum {
24868 +       AuCache_DINFO,
24869 +       AuCache_ICNTNR,
24870 +       AuCache_FINFO,
24871 +       AuCache_VDIR,
24872 +       AuCache_DEHSTR,
24873 +       AuCache_HNOTIFY, /* must be last */
24874 +       AuCache_Last
24875 +};
24876 +
24877 +extern struct kmem_cache *au_cache[AuCache_Last];
24878 +
24879 +#define AuCacheFlags           (SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD)
24880 +#define AuCache(type)          KMEM_CACHE(type, AuCacheFlags)
24881 +#define AuCacheCtor(type, ctor)        \
24882 +       kmem_cache_create(#type, sizeof(struct type), \
24883 +                         __alignof__(struct type), AuCacheFlags, ctor)
24884 +
24885 +#define AuCacheFuncs(name, index)                                      \
24886 +       static inline struct au_##name *au_cache_alloc_##name(void)     \
24887 +       { return kmem_cache_alloc(au_cache[AuCache_##index], GFP_NOFS); } \
24888 +       static inline void au_cache_free_##name##_norcu(struct au_##name *p) \
24889 +       { kmem_cache_free(au_cache[AuCache_##index], p); }              \
24890 +                                                                       \
24891 +       static inline void au_cache_free_##name##_rcu_cb(struct rcu_head *rcu) \
24892 +       { void *p = rcu;                                                \
24893 +               p -= offsetof(struct au_##name, rcu);                   \
24894 +               kmem_cache_free(au_cache[AuCache_##index], p); }        \
24895 +       static inline void au_cache_free_##name##_rcu(struct au_##name *p) \
24896 +       { BUILD_BUG_ON(sizeof(struct au_##name) < sizeof(struct rcu_head)); \
24897 +               call_rcu(&p->rcu, au_cache_free_##name##_rcu_cb); }     \
24898 +                                                                       \
24899 +       static inline void au_cache_free_##name(struct au_##name *p)    \
24900 +       { /* au_cache_free_##name##_norcu(p); */                        \
24901 +               au_cache_free_##name##_rcu(p); }
24902 +
24903 +AuCacheFuncs(dinfo, DINFO);
24904 +AuCacheFuncs(icntnr, ICNTNR);
24905 +AuCacheFuncs(finfo, FINFO);
24906 +AuCacheFuncs(vdir, VDIR);
24907 +AuCacheFuncs(vdir_dehstr, DEHSTR);
24908 +#ifdef CONFIG_AUFS_HNOTIFY
24909 +AuCacheFuncs(hnotify, HNOTIFY);
24910 +#endif
24911 +
24912 +#endif /* __KERNEL__ */
24913 +#endif /* __AUFS_MODULE_H__ */
24914 diff -urN /usr/share/empty/fs/aufs/mvdown.c linux/fs/aufs/mvdown.c
24915 --- /usr/share/empty/fs/aufs/mvdown.c   1970-01-01 01:00:00.000000000 +0100
24916 +++ linux/fs/aufs/mvdown.c      2021-11-01 23:48:34.209692595 +0100
24917 @@ -0,0 +1,706 @@
24918 +// SPDX-License-Identifier: GPL-2.0
24919 +/*
24920 + * Copyright (C) 2011-2021 Junjiro R. Okajima
24921 + *
24922 + * This program, aufs is free software; you can redistribute it and/or modify
24923 + * it under the terms of the GNU General Public License as published by
24924 + * the Free Software Foundation; either version 2 of the License, or
24925 + * (at your option) any later version.
24926 + *
24927 + * This program is distributed in the hope that it will be useful,
24928 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
24929 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24930 + * GNU General Public License for more details.
24931 + *
24932 + * You should have received a copy of the GNU General Public License
24933 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24934 + */
24935 +
24936 +/*
24937 + * move-down, opposite of copy-up
24938 + */
24939 +
24940 +#include "aufs.h"
24941 +
24942 +struct au_mvd_args {
24943 +       struct {
24944 +               struct super_block *h_sb;
24945 +               struct dentry *h_parent;
24946 +               struct au_hinode *hdir;
24947 +               struct inode *h_dir, *h_inode;
24948 +               struct au_pin pin;
24949 +       } info[AUFS_MVDOWN_NARRAY];
24950 +
24951 +       struct aufs_mvdown mvdown;
24952 +       struct dentry *dentry, *parent;
24953 +       struct inode *inode, *dir;
24954 +       struct super_block *sb;
24955 +       aufs_bindex_t bopq, bwh, bfound;
24956 +       unsigned char rename_lock;
24957 +};
24958 +
24959 +#define mvd_errno              mvdown.au_errno
24960 +#define mvd_bsrc               mvdown.stbr[AUFS_MVDOWN_UPPER].bindex
24961 +#define mvd_src_brid           mvdown.stbr[AUFS_MVDOWN_UPPER].brid
24962 +#define mvd_bdst               mvdown.stbr[AUFS_MVDOWN_LOWER].bindex
24963 +#define mvd_dst_brid           mvdown.stbr[AUFS_MVDOWN_LOWER].brid
24964 +
24965 +#define mvd_h_src_sb           info[AUFS_MVDOWN_UPPER].h_sb
24966 +#define mvd_h_src_parent       info[AUFS_MVDOWN_UPPER].h_parent
24967 +#define mvd_hdir_src           info[AUFS_MVDOWN_UPPER].hdir
24968 +#define mvd_h_src_dir          info[AUFS_MVDOWN_UPPER].h_dir
24969 +#define mvd_h_src_inode                info[AUFS_MVDOWN_UPPER].h_inode
24970 +#define mvd_pin_src            info[AUFS_MVDOWN_UPPER].pin
24971 +
24972 +#define mvd_h_dst_sb           info[AUFS_MVDOWN_LOWER].h_sb
24973 +#define mvd_h_dst_parent       info[AUFS_MVDOWN_LOWER].h_parent
24974 +#define mvd_hdir_dst           info[AUFS_MVDOWN_LOWER].hdir
24975 +#define mvd_h_dst_dir          info[AUFS_MVDOWN_LOWER].h_dir
24976 +#define mvd_h_dst_inode                info[AUFS_MVDOWN_LOWER].h_inode
24977 +#define mvd_pin_dst            info[AUFS_MVDOWN_LOWER].pin
24978 +
24979 +#define AU_MVD_PR(flag, ...) do {                      \
24980 +               if (flag)                               \
24981 +                       pr_err(__VA_ARGS__);            \
24982 +       } while (0)
24983 +
24984 +static int find_lower_writable(struct au_mvd_args *a)
24985 +{
24986 +       struct super_block *sb;
24987 +       aufs_bindex_t bindex, bbot;
24988 +       struct au_branch *br;
24989 +
24990 +       sb = a->sb;
24991 +       bindex = a->mvd_bsrc;
24992 +       bbot = au_sbbot(sb);
24993 +       if (a->mvdown.flags & AUFS_MVDOWN_FHSM_LOWER)
24994 +               for (bindex++; bindex <= bbot; bindex++) {
24995 +                       br = au_sbr(sb, bindex);
24996 +                       if (au_br_fhsm(br->br_perm)
24997 +                           && !sb_rdonly(au_br_sb(br)))
24998 +                               return bindex;
24999 +               }
25000 +       else if (!(a->mvdown.flags & AUFS_MVDOWN_ROLOWER))
25001 +               for (bindex++; bindex <= bbot; bindex++) {
25002 +                       br = au_sbr(sb, bindex);
25003 +                       if (!au_br_rdonly(br))
25004 +                               return bindex;
25005 +               }
25006 +       else
25007 +               for (bindex++; bindex <= bbot; bindex++) {
25008 +                       br = au_sbr(sb, bindex);
25009 +                       if (!sb_rdonly(au_br_sb(br))) {
25010 +                               if (au_br_rdonly(br))
25011 +                                       a->mvdown.flags
25012 +                                               |= AUFS_MVDOWN_ROLOWER_R;
25013 +                               return bindex;
25014 +                       }
25015 +               }
25016 +
25017 +       return -1;
25018 +}
25019 +
25020 +/* make the parent dir on bdst */
25021 +static int au_do_mkdir(const unsigned char dmsg, struct au_mvd_args *a)
25022 +{
25023 +       int err;
25024 +
25025 +       err = 0;
25026 +       a->mvd_hdir_src = au_hi(a->dir, a->mvd_bsrc);
25027 +       a->mvd_hdir_dst = au_hi(a->dir, a->mvd_bdst);
25028 +       a->mvd_h_src_parent = au_h_dptr(a->parent, a->mvd_bsrc);
25029 +       a->mvd_h_dst_parent = NULL;
25030 +       if (au_dbbot(a->parent) >= a->mvd_bdst)
25031 +               a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
25032 +       if (!a->mvd_h_dst_parent) {
25033 +               err = au_cpdown_dirs(a->dentry, a->mvd_bdst);
25034 +               if (unlikely(err)) {
25035 +                       AU_MVD_PR(dmsg, "cpdown_dirs failed\n");
25036 +                       goto out;
25037 +               }
25038 +               a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
25039 +       }
25040 +
25041 +out:
25042 +       AuTraceErr(err);
25043 +       return err;
25044 +}
25045 +
25046 +/* lock them all */
25047 +static int au_do_lock(const unsigned char dmsg, struct au_mvd_args *a)
25048 +{
25049 +       int err;
25050 +       struct dentry *h_trap;
25051 +
25052 +       a->mvd_h_src_sb = au_sbr_sb(a->sb, a->mvd_bsrc);
25053 +       a->mvd_h_dst_sb = au_sbr_sb(a->sb, a->mvd_bdst);
25054 +       err = au_pin(&a->mvd_pin_dst, a->dentry, a->mvd_bdst,
25055 +                    au_opt_udba(a->sb),
25056 +                    AuPin_MNT_WRITE | AuPin_DI_LOCKED);
25057 +       AuTraceErr(err);
25058 +       if (unlikely(err)) {
25059 +               AU_MVD_PR(dmsg, "pin_dst failed\n");
25060 +               goto out;
25061 +       }
25062 +
25063 +       if (a->mvd_h_src_sb != a->mvd_h_dst_sb) {
25064 +               a->rename_lock = 0;
25065 +               au_pin_init(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
25066 +                           AuLsc_DI_PARENT, AuLsc_I_PARENT3,
25067 +                           au_opt_udba(a->sb),
25068 +                           AuPin_MNT_WRITE | AuPin_DI_LOCKED);
25069 +               err = au_do_pin(&a->mvd_pin_src);
25070 +               AuTraceErr(err);
25071 +               a->mvd_h_src_dir = d_inode(a->mvd_h_src_parent);
25072 +               if (unlikely(err)) {
25073 +                       AU_MVD_PR(dmsg, "pin_src failed\n");
25074 +                       goto out_dst;
25075 +               }
25076 +               goto out; /* success */
25077 +       }
25078 +
25079 +       a->rename_lock = 1;
25080 +       au_pin_hdir_unlock(&a->mvd_pin_dst);
25081 +       err = au_pin(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
25082 +                    au_opt_udba(a->sb),
25083 +                    AuPin_MNT_WRITE | AuPin_DI_LOCKED);
25084 +       AuTraceErr(err);
25085 +       a->mvd_h_src_dir = d_inode(a->mvd_h_src_parent);
25086 +       if (unlikely(err)) {
25087 +               AU_MVD_PR(dmsg, "pin_src failed\n");
25088 +               au_pin_hdir_lock(&a->mvd_pin_dst);
25089 +               goto out_dst;
25090 +       }
25091 +       au_pin_hdir_unlock(&a->mvd_pin_src);
25092 +       h_trap = vfsub_lock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
25093 +                                  a->mvd_h_dst_parent, a->mvd_hdir_dst);
25094 +       if (h_trap) {
25095 +               err = (h_trap != a->mvd_h_src_parent);
25096 +               if (err)
25097 +                       err = (h_trap != a->mvd_h_dst_parent);
25098 +       }
25099 +       BUG_ON(err); /* it should never happen */
25100 +       if (unlikely(a->mvd_h_src_dir != au_pinned_h_dir(&a->mvd_pin_src))) {
25101 +               err = -EBUSY;
25102 +               AuTraceErr(err);
25103 +               vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
25104 +                                   a->mvd_h_dst_parent, a->mvd_hdir_dst);
25105 +               au_pin_hdir_lock(&a->mvd_pin_src);
25106 +               au_unpin(&a->mvd_pin_src);
25107 +               au_pin_hdir_lock(&a->mvd_pin_dst);
25108 +               goto out_dst;
25109 +       }
25110 +       goto out; /* success */
25111 +
25112 +out_dst:
25113 +       au_unpin(&a->mvd_pin_dst);
25114 +out:
25115 +       AuTraceErr(err);
25116 +       return err;
25117 +}
25118 +
25119 +static void au_do_unlock(const unsigned char dmsg, struct au_mvd_args *a)
25120 +{
25121 +       if (!a->rename_lock)
25122 +               au_unpin(&a->mvd_pin_src);
25123 +       else {
25124 +               vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
25125 +                                   a->mvd_h_dst_parent, a->mvd_hdir_dst);
25126 +               au_pin_hdir_lock(&a->mvd_pin_src);
25127 +               au_unpin(&a->mvd_pin_src);
25128 +               au_pin_hdir_lock(&a->mvd_pin_dst);
25129 +       }
25130 +       au_unpin(&a->mvd_pin_dst);
25131 +}
25132 +
25133 +/* copy-down the file */
25134 +static int au_do_cpdown(const unsigned char dmsg, struct au_mvd_args *a)
25135 +{
25136 +       int err;
25137 +       struct au_cp_generic cpg = {
25138 +               .dentry = a->dentry,
25139 +               .bdst   = a->mvd_bdst,
25140 +               .bsrc   = a->mvd_bsrc,
25141 +               .len    = -1,
25142 +               .pin    = &a->mvd_pin_dst,
25143 +               .flags  = AuCpup_DTIME | AuCpup_HOPEN
25144 +       };
25145 +
25146 +       AuDbg("b%d, b%d\n", cpg.bsrc, cpg.bdst);
25147 +       if (a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
25148 +               au_fset_cpup(cpg.flags, OVERWRITE);
25149 +       if (a->mvdown.flags & AUFS_MVDOWN_ROLOWER)
25150 +               au_fset_cpup(cpg.flags, RWDST);
25151 +       err = au_sio_cpdown_simple(&cpg);
25152 +       if (unlikely(err))
25153 +               AU_MVD_PR(dmsg, "cpdown failed\n");
25154 +
25155 +       AuTraceErr(err);
25156 +       return err;
25157 +}
25158 +
25159 +/*
25160 + * unlink the whiteout on bdst if exist which may be created by UDBA while we
25161 + * were sleeping
25162 + */
25163 +static int au_do_unlink_wh(const unsigned char dmsg, struct au_mvd_args *a)
25164 +{
25165 +       int err;
25166 +       struct path h_path;
25167 +       struct au_branch *br;
25168 +       struct inode *delegated;
25169 +
25170 +       br = au_sbr(a->sb, a->mvd_bdst);
25171 +       h_path.dentry = au_wh_lkup(a->mvd_h_dst_parent, &a->dentry->d_name, br);
25172 +       err = PTR_ERR(h_path.dentry);
25173 +       if (IS_ERR(h_path.dentry)) {
25174 +               AU_MVD_PR(dmsg, "wh_lkup failed\n");
25175 +               goto out;
25176 +       }
25177 +
25178 +       err = 0;
25179 +       if (d_is_positive(h_path.dentry)) {
25180 +               h_path.mnt = au_br_mnt(br);
25181 +               delegated = NULL;
25182 +               err = vfsub_unlink(d_inode(a->mvd_h_dst_parent), &h_path,
25183 +                                  &delegated, /*force*/0);
25184 +               if (unlikely(err == -EWOULDBLOCK)) {
25185 +                       pr_warn("cannot retry for NFSv4 delegation"
25186 +                               " for an internal unlink\n");
25187 +                       iput(delegated);
25188 +               }
25189 +               if (unlikely(err))
25190 +                       AU_MVD_PR(dmsg, "wh_unlink failed\n");
25191 +       }
25192 +       dput(h_path.dentry);
25193 +
25194 +out:
25195 +       AuTraceErr(err);
25196 +       return err;
25197 +}
25198 +
25199 +/*
25200 + * unlink the topmost h_dentry
25201 + */
25202 +static int au_do_unlink(const unsigned char dmsg, struct au_mvd_args *a)
25203 +{
25204 +       int err;
25205 +       struct path h_path;
25206 +       struct inode *delegated;
25207 +
25208 +       h_path.mnt = au_sbr_mnt(a->sb, a->mvd_bsrc);
25209 +       h_path.dentry = au_h_dptr(a->dentry, a->mvd_bsrc);
25210 +       delegated = NULL;
25211 +       err = vfsub_unlink(a->mvd_h_src_dir, &h_path, &delegated, /*force*/0);
25212 +       if (unlikely(err == -EWOULDBLOCK)) {
25213 +               pr_warn("cannot retry for NFSv4 delegation"
25214 +                       " for an internal unlink\n");
25215 +               iput(delegated);
25216 +       }
25217 +       if (unlikely(err))
25218 +               AU_MVD_PR(dmsg, "unlink failed\n");
25219 +
25220 +       AuTraceErr(err);
25221 +       return err;
25222 +}
25223 +
25224 +/* Since mvdown succeeded, we ignore an error of this function */
25225 +static void au_do_stfs(const unsigned char dmsg, struct au_mvd_args *a)
25226 +{
25227 +       int err;
25228 +       struct au_branch *br;
25229 +
25230 +       a->mvdown.flags |= AUFS_MVDOWN_STFS_FAILED;
25231 +       br = au_sbr(a->sb, a->mvd_bsrc);
25232 +       err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_UPPER].stfs);
25233 +       if (!err) {
25234 +               br = au_sbr(a->sb, a->mvd_bdst);
25235 +               a->mvdown.stbr[AUFS_MVDOWN_LOWER].brid = br->br_id;
25236 +               err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_LOWER].stfs);
25237 +       }
25238 +       if (!err)
25239 +               a->mvdown.flags &= ~AUFS_MVDOWN_STFS_FAILED;
25240 +       else
25241 +               AU_MVD_PR(dmsg, "statfs failed (%d), ignored\n", err);
25242 +}
25243 +
25244 +/*
25245 + * copy-down the file and unlink the bsrc file.
25246 + * - unlink the bdst whout if exist
25247 + * - copy-down the file (with whtmp name and rename)
25248 + * - unlink the bsrc file
25249 + */
25250 +static int au_do_mvdown(const unsigned char dmsg, struct au_mvd_args *a)
25251 +{
25252 +       int err;
25253 +
25254 +       err = au_do_mkdir(dmsg, a);
25255 +       if (!err)
25256 +               err = au_do_lock(dmsg, a);
25257 +       if (unlikely(err))
25258 +               goto out;
25259 +
25260 +       /*
25261 +        * do not revert the activities we made on bdst since they should be
25262 +        * harmless in aufs.
25263 +        */
25264 +
25265 +       err = au_do_cpdown(dmsg, a);
25266 +       if (!err)
25267 +               err = au_do_unlink_wh(dmsg, a);
25268 +       if (!err && !(a->mvdown.flags & AUFS_MVDOWN_KUPPER))
25269 +               err = au_do_unlink(dmsg, a);
25270 +       if (unlikely(err))
25271 +               goto out_unlock;
25272 +
25273 +       AuDbg("%pd2, 0x%x, %d --> %d\n",
25274 +             a->dentry, a->mvdown.flags, a->mvd_bsrc, a->mvd_bdst);
25275 +       if (find_lower_writable(a) < 0)
25276 +               a->mvdown.flags |= AUFS_MVDOWN_BOTTOM;
25277 +
25278 +       if (a->mvdown.flags & AUFS_MVDOWN_STFS)
25279 +               au_do_stfs(dmsg, a);
25280 +
25281 +       /* maintain internal array */
25282 +       if (!(a->mvdown.flags & AUFS_MVDOWN_KUPPER)) {
25283 +               au_set_h_dptr(a->dentry, a->mvd_bsrc, NULL);
25284 +               au_set_dbtop(a->dentry, a->mvd_bdst);
25285 +               au_set_h_iptr(a->inode, a->mvd_bsrc, NULL, /*flags*/0);
25286 +               au_set_ibtop(a->inode, a->mvd_bdst);
25287 +       } else {
25288 +               /* hide the lower */
25289 +               au_set_h_dptr(a->dentry, a->mvd_bdst, NULL);
25290 +               au_set_dbbot(a->dentry, a->mvd_bsrc);
25291 +               au_set_h_iptr(a->inode, a->mvd_bdst, NULL, /*flags*/0);
25292 +               au_set_ibbot(a->inode, a->mvd_bsrc);
25293 +       }
25294 +       if (au_dbbot(a->dentry) < a->mvd_bdst)
25295 +               au_set_dbbot(a->dentry, a->mvd_bdst);
25296 +       if (au_ibbot(a->inode) < a->mvd_bdst)
25297 +               au_set_ibbot(a->inode, a->mvd_bdst);
25298 +
25299 +out_unlock:
25300 +       au_do_unlock(dmsg, a);
25301 +out:
25302 +       AuTraceErr(err);
25303 +       return err;
25304 +}
25305 +
25306 +/* ---------------------------------------------------------------------- */
25307 +
25308 +/* make sure the file is idle */
25309 +static int au_mvd_args_busy(const unsigned char dmsg, struct au_mvd_args *a)
25310 +{
25311 +       int err, plinked;
25312 +
25313 +       err = 0;
25314 +       plinked = !!au_opt_test(au_mntflags(a->sb), PLINK);
25315 +       if (au_dbtop(a->dentry) == a->mvd_bsrc
25316 +           && au_dcount(a->dentry) == 1
25317 +           && atomic_read(&a->inode->i_count) == 1
25318 +           /* && a->mvd_h_src_inode->i_nlink == 1 */
25319 +           && (!plinked || !au_plink_test(a->inode))
25320 +           && a->inode->i_nlink == 1)
25321 +               goto out;
25322 +
25323 +       err = -EBUSY;
25324 +       AU_MVD_PR(dmsg,
25325 +                 "b%d, d{b%d, c%d?}, i{c%d?, l%u}, hi{l%u}, p{%d, %d}\n",
25326 +                 a->mvd_bsrc, au_dbtop(a->dentry), au_dcount(a->dentry),
25327 +                 atomic_read(&a->inode->i_count), a->inode->i_nlink,
25328 +                 a->mvd_h_src_inode->i_nlink,
25329 +                 plinked, plinked ? au_plink_test(a->inode) : 0);
25330 +
25331 +out:
25332 +       AuTraceErr(err);
25333 +       return err;
25334 +}
25335 +
25336 +/* make sure the parent dir is fine */
25337 +static int au_mvd_args_parent(const unsigned char dmsg,
25338 +                             struct au_mvd_args *a)
25339 +{
25340 +       int err;
25341 +       aufs_bindex_t bindex;
25342 +
25343 +       err = 0;
25344 +       if (unlikely(au_alive_dir(a->parent))) {
25345 +               err = -ENOENT;
25346 +               AU_MVD_PR(dmsg, "parent dir is dead\n");
25347 +               goto out;
25348 +       }
25349 +
25350 +       a->bopq = au_dbdiropq(a->parent);
25351 +       bindex = au_wbr_nonopq(a->dentry, a->mvd_bdst);
25352 +       AuDbg("b%d\n", bindex);
25353 +       if (unlikely((bindex >= 0 && bindex < a->mvd_bdst)
25354 +                    || (a->bopq != -1 && a->bopq < a->mvd_bdst))) {
25355 +               err = -EINVAL;
25356 +               a->mvd_errno = EAU_MVDOWN_OPAQUE;
25357 +               AU_MVD_PR(dmsg, "ancestor is opaque b%d, b%d\n",
25358 +                         a->bopq, a->mvd_bdst);
25359 +       }
25360 +
25361 +out:
25362 +       AuTraceErr(err);
25363 +       return err;
25364 +}
25365 +
25366 +static int au_mvd_args_intermediate(const unsigned char dmsg,
25367 +                                   struct au_mvd_args *a)
25368 +{
25369 +       int err;
25370 +       struct au_dinfo *dinfo, *tmp;
25371 +
25372 +       /* lookup the next lower positive entry */
25373 +       err = -ENOMEM;
25374 +       tmp = au_di_alloc(a->sb, AuLsc_DI_TMP);
25375 +       if (unlikely(!tmp))
25376 +               goto out;
25377 +
25378 +       a->bfound = -1;
25379 +       a->bwh = -1;
25380 +       dinfo = au_di(a->dentry);
25381 +       au_di_cp(tmp, dinfo);
25382 +       au_di_swap(tmp, dinfo);
25383 +
25384 +       /* returns the number of positive dentries */
25385 +       err = au_lkup_dentry(a->dentry, a->mvd_bsrc + 1,
25386 +                            /* AuLkup_IGNORE_PERM */ 0);
25387 +       if (!err)
25388 +               a->bwh = au_dbwh(a->dentry);
25389 +       else if (err > 0)
25390 +               a->bfound = au_dbtop(a->dentry);
25391 +
25392 +       au_di_swap(tmp, dinfo);
25393 +       au_rw_write_unlock(&tmp->di_rwsem);
25394 +       au_di_free(tmp);
25395 +       if (unlikely(err < 0))
25396 +               AU_MVD_PR(dmsg, "failed look-up lower\n");
25397 +
25398 +       /*
25399 +        * here, we have these cases.
25400 +        * bfound == -1
25401 +        *      no positive dentry under bsrc. there are more sub-cases.
25402 +        *      bwh < 0
25403 +        *              there no whiteout, we can safely move-down.
25404 +        *      bwh <= bsrc
25405 +        *              impossible
25406 +        *      bsrc < bwh && bwh < bdst
25407 +        *              there is a whiteout on RO branch. cannot proceed.
25408 +        *      bwh == bdst
25409 +        *              there is a whiteout on the RW target branch. it should
25410 +        *              be removed.
25411 +        *      bdst < bwh
25412 +        *              there is a whiteout somewhere unrelated branch.
25413 +        * -1 < bfound && bfound <= bsrc
25414 +        *      impossible.
25415 +        * bfound < bdst
25416 +        *      found, but it is on RO branch between bsrc and bdst. cannot
25417 +        *      proceed.
25418 +        * bfound == bdst
25419 +        *      found, replace it if AUFS_MVDOWN_FORCE is set. otherwise return
25420 +        *      error.
25421 +        * bdst < bfound
25422 +        *      found, after we create the file on bdst, it will be hidden.
25423 +        */
25424 +
25425 +       AuDebugOn(a->bfound == -1
25426 +                 && a->bwh != -1
25427 +                 && a->bwh <= a->mvd_bsrc);
25428 +       AuDebugOn(-1 < a->bfound
25429 +                 && a->bfound <= a->mvd_bsrc);
25430 +
25431 +       err = -EINVAL;
25432 +       if (a->bfound == -1
25433 +           && a->mvd_bsrc < a->bwh
25434 +           && a->bwh != -1
25435 +           && a->bwh < a->mvd_bdst) {
25436 +               a->mvd_errno = EAU_MVDOWN_WHITEOUT;
25437 +               AU_MVD_PR(dmsg, "bsrc %d, bdst %d, bfound %d, bwh %d\n",
25438 +                         a->mvd_bsrc, a->mvd_bdst, a->bfound, a->bwh);
25439 +               goto out;
25440 +       } else if (a->bfound != -1 && a->bfound < a->mvd_bdst) {
25441 +               a->mvd_errno = EAU_MVDOWN_UPPER;
25442 +               AU_MVD_PR(dmsg, "bdst %d, bfound %d\n",
25443 +                         a->mvd_bdst, a->bfound);
25444 +               goto out;
25445 +       }
25446 +
25447 +       err = 0; /* success */
25448 +
25449 +out:
25450 +       AuTraceErr(err);
25451 +       return err;
25452 +}
25453 +
25454 +static int au_mvd_args_exist(const unsigned char dmsg, struct au_mvd_args *a)
25455 +{
25456 +       int err;
25457 +
25458 +       err = 0;
25459 +       if (!(a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
25460 +           && a->bfound == a->mvd_bdst)
25461 +               err = -EEXIST;
25462 +       AuTraceErr(err);
25463 +       return err;
25464 +}
25465 +
25466 +static int au_mvd_args(const unsigned char dmsg, struct au_mvd_args *a)
25467 +{
25468 +       int err;
25469 +       struct au_branch *br;
25470 +
25471 +       err = -EISDIR;
25472 +       if (unlikely(S_ISDIR(a->inode->i_mode)))
25473 +               goto out;
25474 +
25475 +       err = -EINVAL;
25476 +       if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_UPPER))
25477 +               a->mvd_bsrc = au_ibtop(a->inode);
25478 +       else {
25479 +               a->mvd_bsrc = au_br_index(a->sb, a->mvd_src_brid);
25480 +               if (unlikely(a->mvd_bsrc < 0
25481 +                            || (a->mvd_bsrc < au_dbtop(a->dentry)
25482 +                                || au_dbbot(a->dentry) < a->mvd_bsrc
25483 +                                || !au_h_dptr(a->dentry, a->mvd_bsrc))
25484 +                            || (a->mvd_bsrc < au_ibtop(a->inode)
25485 +                                || au_ibbot(a->inode) < a->mvd_bsrc
25486 +                                || !au_h_iptr(a->inode, a->mvd_bsrc)))) {
25487 +                       a->mvd_errno = EAU_MVDOWN_NOUPPER;
25488 +                       AU_MVD_PR(dmsg, "no upper\n");
25489 +                       goto out;
25490 +               }
25491 +       }
25492 +       if (unlikely(a->mvd_bsrc == au_sbbot(a->sb))) {
25493 +               a->mvd_errno = EAU_MVDOWN_BOTTOM;
25494 +               AU_MVD_PR(dmsg, "on the bottom\n");
25495 +               goto out;
25496 +       }
25497 +       a->mvd_h_src_inode = au_h_iptr(a->inode, a->mvd_bsrc);
25498 +       br = au_sbr(a->sb, a->mvd_bsrc);
25499 +       err = au_br_rdonly(br);
25500 +       if (!(a->mvdown.flags & AUFS_MVDOWN_ROUPPER)) {
25501 +               if (unlikely(err))
25502 +                       goto out;
25503 +       } else if (!(vfsub_native_ro(a->mvd_h_src_inode)
25504 +                    || IS_APPEND(a->mvd_h_src_inode))) {
25505 +               if (err)
25506 +                       a->mvdown.flags |= AUFS_MVDOWN_ROUPPER_R;
25507 +               /* go on */
25508 +       } else
25509 +               goto out;
25510 +
25511 +       err = -EINVAL;
25512 +       if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_LOWER)) {
25513 +               a->mvd_bdst = find_lower_writable(a);
25514 +               if (unlikely(a->mvd_bdst < 0)) {
25515 +                       a->mvd_errno = EAU_MVDOWN_BOTTOM;
25516 +                       AU_MVD_PR(dmsg, "no writable lower branch\n");
25517 +                       goto out;
25518 +               }
25519 +       } else {
25520 +               a->mvd_bdst = au_br_index(a->sb, a->mvd_dst_brid);
25521 +               if (unlikely(a->mvd_bdst < 0
25522 +                            || au_sbbot(a->sb) < a->mvd_bdst)) {
25523 +                       a->mvd_errno = EAU_MVDOWN_NOLOWERBR;
25524 +                       AU_MVD_PR(dmsg, "no lower brid\n");
25525 +                       goto out;
25526 +               }
25527 +       }
25528 +
25529 +       err = au_mvd_args_busy(dmsg, a);
25530 +       if (!err)
25531 +               err = au_mvd_args_parent(dmsg, a);
25532 +       if (!err)
25533 +               err = au_mvd_args_intermediate(dmsg, a);
25534 +       if (!err)
25535 +               err = au_mvd_args_exist(dmsg, a);
25536 +       if (!err)
25537 +               AuDbg("b%d, b%d\n", a->mvd_bsrc, a->mvd_bdst);
25538 +
25539 +out:
25540 +       AuTraceErr(err);
25541 +       return err;
25542 +}
25543 +
25544 +int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *uarg)
25545 +{
25546 +       int err, e;
25547 +       unsigned char dmsg;
25548 +       struct au_mvd_args *args;
25549 +       struct inode *inode;
25550 +
25551 +       inode = d_inode(dentry);
25552 +       err = -EPERM;
25553 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
25554 +               goto out;
25555 +
25556 +       err = -ENOMEM;
25557 +       args = kmalloc(sizeof(*args), GFP_NOFS);
25558 +       if (unlikely(!args))
25559 +               goto out;
25560 +
25561 +       err = copy_from_user(&args->mvdown, uarg, sizeof(args->mvdown));
25562 +       if (!err)
25563 +               /* VERIFY_WRITE */
25564 +               err = !access_ok(uarg, sizeof(*uarg));
25565 +       if (unlikely(err)) {
25566 +               err = -EFAULT;
25567 +               AuTraceErr(err);
25568 +               goto out_free;
25569 +       }
25570 +       AuDbg("flags 0x%x\n", args->mvdown.flags);
25571 +       args->mvdown.flags &= ~(AUFS_MVDOWN_ROLOWER_R | AUFS_MVDOWN_ROUPPER_R);
25572 +       args->mvdown.au_errno = 0;
25573 +       args->dentry = dentry;
25574 +       args->inode = inode;
25575 +       args->sb = dentry->d_sb;
25576 +
25577 +       err = -ENOENT;
25578 +       dmsg = !!(args->mvdown.flags & AUFS_MVDOWN_DMSG);
25579 +       args->parent = dget_parent(dentry);
25580 +       args->dir = d_inode(args->parent);
25581 +       inode_lock_nested(args->dir, I_MUTEX_PARENT);
25582 +       dput(args->parent);
25583 +       if (unlikely(args->parent != dentry->d_parent)) {
25584 +               AU_MVD_PR(dmsg, "parent dir is moved\n");
25585 +               goto out_dir;
25586 +       }
25587 +
25588 +       inode_lock_nested(inode, I_MUTEX_CHILD);
25589 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_NOPLMW);
25590 +       if (unlikely(err))
25591 +               goto out_inode;
25592 +
25593 +       di_write_lock_parent(args->parent);
25594 +       err = au_mvd_args(dmsg, args);
25595 +       if (unlikely(err))
25596 +               goto out_parent;
25597 +
25598 +       err = au_do_mvdown(dmsg, args);
25599 +       if (unlikely(err))
25600 +               goto out_parent;
25601 +
25602 +       au_cpup_attr_timesizes(args->dir);
25603 +       au_cpup_attr_timesizes(inode);
25604 +       if (!(args->mvdown.flags & AUFS_MVDOWN_KUPPER))
25605 +               au_cpup_igen(inode, au_h_iptr(inode, args->mvd_bdst));
25606 +       /* au_digen_dec(dentry); */
25607 +
25608 +out_parent:
25609 +       di_write_unlock(args->parent);
25610 +       aufs_read_unlock(dentry, AuLock_DW);
25611 +out_inode:
25612 +       inode_unlock(inode);
25613 +out_dir:
25614 +       inode_unlock(args->dir);
25615 +out_free:
25616 +       e = copy_to_user(uarg, &args->mvdown, sizeof(args->mvdown));
25617 +       if (unlikely(e))
25618 +               err = -EFAULT;
25619 +       au_kfree_rcu(args);
25620 +out:
25621 +       AuTraceErr(err);
25622 +       return err;
25623 +}
25624 diff -urN /usr/share/empty/fs/aufs/opts.c linux/fs/aufs/opts.c
25625 --- /usr/share/empty/fs/aufs/opts.c     1970-01-01 01:00:00.000000000 +0100
25626 +++ linux/fs/aufs/opts.c        2021-11-01 23:48:34.209692595 +0100
25627 @@ -0,0 +1,1880 @@
25628 +// SPDX-License-Identifier: GPL-2.0
25629 +/*
25630 + * Copyright (C) 2005-2021 Junjiro R. Okajima
25631 + *
25632 + * This program, aufs is free software; you can redistribute it and/or modify
25633 + * it under the terms of the GNU General Public License as published by
25634 + * the Free Software Foundation; either version 2 of the License, or
25635 + * (at your option) any later version.
25636 + *
25637 + * This program is distributed in the hope that it will be useful,
25638 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
25639 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25640 + * GNU General Public License for more details.
25641 + *
25642 + * You should have received a copy of the GNU General Public License
25643 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25644 + */
25645 +
25646 +/*
25647 + * mount options/flags
25648 + */
25649 +
25650 +#include <linux/namei.h>
25651 +#include <linux/types.h> /* a distribution requires */
25652 +#include <linux/parser.h>
25653 +#include "aufs.h"
25654 +
25655 +/* ---------------------------------------------------------------------- */
25656 +
25657 +enum {
25658 +       Opt_br,
25659 +       Opt_add, Opt_del, Opt_mod, Opt_append, Opt_prepend,
25660 +       Opt_idel, Opt_imod,
25661 +       Opt_dirwh, Opt_rdcache, Opt_rdblk, Opt_rdhash,
25662 +       Opt_rdblk_def, Opt_rdhash_def,
25663 +       Opt_xino, Opt_noxino,
25664 +       Opt_trunc_xino, Opt_trunc_xino_v, Opt_notrunc_xino,
25665 +       Opt_trunc_xino_path, Opt_itrunc_xino,
25666 +       Opt_trunc_xib, Opt_notrunc_xib,
25667 +       Opt_shwh, Opt_noshwh,
25668 +       Opt_plink, Opt_noplink, Opt_list_plink,
25669 +       Opt_udba,
25670 +       Opt_dio, Opt_nodio,
25671 +       Opt_diropq_a, Opt_diropq_w,
25672 +       Opt_warn_perm, Opt_nowarn_perm,
25673 +       Opt_wbr_copyup, Opt_wbr_create,
25674 +       Opt_fhsm_sec,
25675 +       Opt_verbose, Opt_noverbose,
25676 +       Opt_sum, Opt_nosum, Opt_wsum,
25677 +       Opt_dirperm1, Opt_nodirperm1,
25678 +       Opt_dirren, Opt_nodirren,
25679 +       Opt_acl, Opt_noacl,
25680 +       Opt_tail, Opt_ignore, Opt_ignore_silent, Opt_err
25681 +};
25682 +
25683 +static match_table_t options = {
25684 +       {Opt_br, "br=%s"},
25685 +       {Opt_br, "br:%s"},
25686 +
25687 +       {Opt_add, "add=%d:%s"},
25688 +       {Opt_add, "add:%d:%s"},
25689 +       {Opt_add, "ins=%d:%s"},
25690 +       {Opt_add, "ins:%d:%s"},
25691 +       {Opt_append, "append=%s"},
25692 +       {Opt_append, "append:%s"},
25693 +       {Opt_prepend, "prepend=%s"},
25694 +       {Opt_prepend, "prepend:%s"},
25695 +
25696 +       {Opt_del, "del=%s"},
25697 +       {Opt_del, "del:%s"},
25698 +       /* {Opt_idel, "idel:%d"}, */
25699 +       {Opt_mod, "mod=%s"},
25700 +       {Opt_mod, "mod:%s"},
25701 +       /* {Opt_imod, "imod:%d:%s"}, */
25702 +
25703 +       {Opt_dirwh, "dirwh=%d"},
25704 +
25705 +       {Opt_xino, "xino=%s"},
25706 +       {Opt_noxino, "noxino"},
25707 +       {Opt_trunc_xino, "trunc_xino"},
25708 +       {Opt_trunc_xino_v, "trunc_xino_v=%d:%d"},
25709 +       {Opt_notrunc_xino, "notrunc_xino"},
25710 +       {Opt_trunc_xino_path, "trunc_xino=%s"},
25711 +       {Opt_itrunc_xino, "itrunc_xino=%d"},
25712 +       /* {Opt_zxino, "zxino=%s"}, */
25713 +       {Opt_trunc_xib, "trunc_xib"},
25714 +       {Opt_notrunc_xib, "notrunc_xib"},
25715 +
25716 +#ifdef CONFIG_PROC_FS
25717 +       {Opt_plink, "plink"},
25718 +#else
25719 +       {Opt_ignore_silent, "plink"},
25720 +#endif
25721 +
25722 +       {Opt_noplink, "noplink"},
25723 +
25724 +#ifdef CONFIG_AUFS_DEBUG
25725 +       {Opt_list_plink, "list_plink"},
25726 +#endif
25727 +
25728 +       {Opt_udba, "udba=%s"},
25729 +
25730 +       {Opt_dio, "dio"},
25731 +       {Opt_nodio, "nodio"},
25732 +
25733 +#ifdef CONFIG_AUFS_DIRREN
25734 +       {Opt_dirren, "dirren"},
25735 +       {Opt_nodirren, "nodirren"},
25736 +#else
25737 +       {Opt_ignore, "dirren"},
25738 +       {Opt_ignore_silent, "nodirren"},
25739 +#endif
25740 +
25741 +#ifdef CONFIG_AUFS_FHSM
25742 +       {Opt_fhsm_sec, "fhsm_sec=%d"},
25743 +#else
25744 +       {Opt_ignore, "fhsm_sec=%d"},
25745 +#endif
25746 +
25747 +       {Opt_diropq_a, "diropq=always"},
25748 +       {Opt_diropq_a, "diropq=a"},
25749 +       {Opt_diropq_w, "diropq=whiteouted"},
25750 +       {Opt_diropq_w, "diropq=w"},
25751 +
25752 +       {Opt_warn_perm, "warn_perm"},
25753 +       {Opt_nowarn_perm, "nowarn_perm"},
25754 +
25755 +       /* keep them temporary */
25756 +       {Opt_ignore_silent, "nodlgt"},
25757 +       {Opt_ignore, "clean_plink"},
25758 +
25759 +#ifdef CONFIG_AUFS_SHWH
25760 +       {Opt_shwh, "shwh"},
25761 +#endif
25762 +       {Opt_noshwh, "noshwh"},
25763 +
25764 +       {Opt_dirperm1, "dirperm1"},
25765 +       {Opt_nodirperm1, "nodirperm1"},
25766 +
25767 +       {Opt_verbose, "verbose"},
25768 +       {Opt_verbose, "v"},
25769 +       {Opt_noverbose, "noverbose"},
25770 +       {Opt_noverbose, "quiet"},
25771 +       {Opt_noverbose, "q"},
25772 +       {Opt_noverbose, "silent"},
25773 +
25774 +       {Opt_sum, "sum"},
25775 +       {Opt_nosum, "nosum"},
25776 +       {Opt_wsum, "wsum"},
25777 +
25778 +       {Opt_rdcache, "rdcache=%d"},
25779 +       {Opt_rdblk, "rdblk=%d"},
25780 +       {Opt_rdblk_def, "rdblk=def"},
25781 +       {Opt_rdhash, "rdhash=%d"},
25782 +       {Opt_rdhash_def, "rdhash=def"},
25783 +
25784 +       {Opt_wbr_create, "create=%s"},
25785 +       {Opt_wbr_create, "create_policy=%s"},
25786 +       {Opt_wbr_copyup, "cpup=%s"},
25787 +       {Opt_wbr_copyup, "copyup=%s"},
25788 +       {Opt_wbr_copyup, "copyup_policy=%s"},
25789 +
25790 +       /* generic VFS flag */
25791 +#ifdef CONFIG_FS_POSIX_ACL
25792 +       {Opt_acl, "acl"},
25793 +       {Opt_noacl, "noacl"},
25794 +#else
25795 +       {Opt_ignore, "acl"},
25796 +       {Opt_ignore_silent, "noacl"},
25797 +#endif
25798 +
25799 +       /* internal use for the scripts */
25800 +       {Opt_ignore_silent, "si=%s"},
25801 +
25802 +       {Opt_br, "dirs=%s"},
25803 +       {Opt_ignore, "debug=%d"},
25804 +       {Opt_ignore, "delete=whiteout"},
25805 +       {Opt_ignore, "delete=all"},
25806 +       {Opt_ignore, "imap=%s"},
25807 +
25808 +       /* temporary workaround, due to old mount(8)? */
25809 +       {Opt_ignore_silent, "relatime"},
25810 +
25811 +       {Opt_err, NULL}
25812 +};
25813 +
25814 +/* ---------------------------------------------------------------------- */
25815 +
25816 +static const char *au_parser_pattern(int val, match_table_t tbl)
25817 +{
25818 +       struct match_token *p;
25819 +
25820 +       p = tbl;
25821 +       while (p->pattern) {
25822 +               if (p->token == val)
25823 +                       return p->pattern;
25824 +               p++;
25825 +       }
25826 +       BUG();
25827 +       return "??";
25828 +}
25829 +
25830 +static const char *au_optstr(int *val, match_table_t tbl)
25831 +{
25832 +       struct match_token *p;
25833 +       int v;
25834 +
25835 +       v = *val;
25836 +       if (!v)
25837 +               goto out;
25838 +       p = tbl;
25839 +       while (p->pattern) {
25840 +               if (p->token
25841 +                   && (v & p->token) == p->token) {
25842 +                       *val &= ~p->token;
25843 +                       return p->pattern;
25844 +               }
25845 +               p++;
25846 +       }
25847 +
25848 +out:
25849 +       return NULL;
25850 +}
25851 +
25852 +/* ---------------------------------------------------------------------- */
25853 +
25854 +static match_table_t brperm = {
25855 +       {AuBrPerm_RO, AUFS_BRPERM_RO},
25856 +       {AuBrPerm_RR, AUFS_BRPERM_RR},
25857 +       {AuBrPerm_RW, AUFS_BRPERM_RW},
25858 +       {0, NULL}
25859 +};
25860 +
25861 +static match_table_t brattr = {
25862 +       /* general */
25863 +       {AuBrAttr_COO_REG, AUFS_BRATTR_COO_REG},
25864 +       {AuBrAttr_COO_ALL, AUFS_BRATTR_COO_ALL},
25865 +       /* 'unpin' attrib is meaningless since linux-3.18-rc1 */
25866 +       {AuBrAttr_UNPIN, AUFS_BRATTR_UNPIN},
25867 +#ifdef CONFIG_AUFS_FHSM
25868 +       {AuBrAttr_FHSM, AUFS_BRATTR_FHSM},
25869 +#endif
25870 +#ifdef CONFIG_AUFS_XATTR
25871 +       {AuBrAttr_ICEX, AUFS_BRATTR_ICEX},
25872 +       {AuBrAttr_ICEX_SEC, AUFS_BRATTR_ICEX_SEC},
25873 +       {AuBrAttr_ICEX_SYS, AUFS_BRATTR_ICEX_SYS},
25874 +       {AuBrAttr_ICEX_TR, AUFS_BRATTR_ICEX_TR},
25875 +       {AuBrAttr_ICEX_USR, AUFS_BRATTR_ICEX_USR},
25876 +       {AuBrAttr_ICEX_OTH, AUFS_BRATTR_ICEX_OTH},
25877 +#endif
25878 +
25879 +       /* ro/rr branch */
25880 +       {AuBrRAttr_WH, AUFS_BRRATTR_WH},
25881 +
25882 +       /* rw branch */
25883 +       {AuBrWAttr_MOO, AUFS_BRWATTR_MOO},
25884 +       {AuBrWAttr_NoLinkWH, AUFS_BRWATTR_NLWH},
25885 +
25886 +       {0, NULL}
25887 +};
25888 +
25889 +static int br_attr_val(char *str, match_table_t table, substring_t args[])
25890 +{
25891 +       int attr, v;
25892 +       char *p;
25893 +
25894 +       attr = 0;
25895 +       do {
25896 +               p = strchr(str, '+');
25897 +               if (p)
25898 +                       *p = 0;
25899 +               v = match_token(str, table, args);
25900 +               if (v) {
25901 +                       if (v & AuBrAttr_CMOO_Mask)
25902 +                               attr &= ~AuBrAttr_CMOO_Mask;
25903 +                       attr |= v;
25904 +               } else {
25905 +                       if (p)
25906 +                               *p = '+';
25907 +                       pr_warn("ignored branch attribute %s\n", str);
25908 +                       break;
25909 +               }
25910 +               if (p)
25911 +                       str = p + 1;
25912 +       } while (p);
25913 +
25914 +       return attr;
25915 +}
25916 +
25917 +static int au_do_optstr_br_attr(au_br_perm_str_t *str, int perm)
25918 +{
25919 +       int sz;
25920 +       const char *p;
25921 +       char *q;
25922 +
25923 +       q = str->a;
25924 +       *q = 0;
25925 +       p = au_optstr(&perm, brattr);
25926 +       if (p) {
25927 +               sz = strlen(p);
25928 +               memcpy(q, p, sz + 1);
25929 +               q += sz;
25930 +       } else
25931 +               goto out;
25932 +
25933 +       do {
25934 +               p = au_optstr(&perm, brattr);
25935 +               if (p) {
25936 +                       *q++ = '+';
25937 +                       sz = strlen(p);
25938 +                       memcpy(q, p, sz + 1);
25939 +                       q += sz;
25940 +               }
25941 +       } while (p);
25942 +
25943 +out:
25944 +       return q - str->a;
25945 +}
25946 +
25947 +static int noinline_for_stack br_perm_val(char *perm)
25948 +{
25949 +       int val, bad, sz;
25950 +       char *p;
25951 +       substring_t args[MAX_OPT_ARGS];
25952 +       au_br_perm_str_t attr;
25953 +
25954 +       p = strchr(perm, '+');
25955 +       if (p)
25956 +               *p = 0;
25957 +       val = match_token(perm, brperm, args);
25958 +       if (!val) {
25959 +               if (p)
25960 +                       *p = '+';
25961 +               pr_warn("ignored branch permission %s\n", perm);
25962 +               val = AuBrPerm_RO;
25963 +               goto out;
25964 +       }
25965 +       if (!p)
25966 +               goto out;
25967 +
25968 +       val |= br_attr_val(p + 1, brattr, args);
25969 +
25970 +       bad = 0;
25971 +       switch (val & AuBrPerm_Mask) {
25972 +       case AuBrPerm_RO:
25973 +       case AuBrPerm_RR:
25974 +               bad = val & AuBrWAttr_Mask;
25975 +               val &= ~AuBrWAttr_Mask;
25976 +               break;
25977 +       case AuBrPerm_RW:
25978 +               bad = val & AuBrRAttr_Mask;
25979 +               val &= ~AuBrRAttr_Mask;
25980 +               break;
25981 +       }
25982 +
25983 +       /*
25984 +        * 'unpin' attrib becomes meaningless since linux-3.18-rc1, but aufs
25985 +        * does not treat it as an error, just warning.
25986 +        * this is a tiny guard for the user operation.
25987 +        */
25988 +       if (val & AuBrAttr_UNPIN) {
25989 +               bad |= AuBrAttr_UNPIN;
25990 +               val &= ~AuBrAttr_UNPIN;
25991 +       }
25992 +
25993 +       if (unlikely(bad)) {
25994 +               sz = au_do_optstr_br_attr(&attr, bad);
25995 +               AuDebugOn(!sz);
25996 +               pr_warn("ignored branch attribute %s\n", attr.a);
25997 +       }
25998 +
25999 +out:
26000 +       return val;
26001 +}
26002 +
26003 +void au_optstr_br_perm(au_br_perm_str_t *str, int perm)
26004 +{
26005 +       au_br_perm_str_t attr;
26006 +       const char *p;
26007 +       char *q;
26008 +       int sz;
26009 +
26010 +       q = str->a;
26011 +       p = au_optstr(&perm, brperm);
26012 +       AuDebugOn(!p || !*p);
26013 +       sz = strlen(p);
26014 +       memcpy(q, p, sz + 1);
26015 +       q += sz;
26016 +
26017 +       sz = au_do_optstr_br_attr(&attr, perm);
26018 +       if (sz) {
26019 +               *q++ = '+';
26020 +               memcpy(q, attr.a, sz + 1);
26021 +       }
26022 +
26023 +       AuDebugOn(strlen(str->a) >= sizeof(str->a));
26024 +}
26025 +
26026 +/* ---------------------------------------------------------------------- */
26027 +
26028 +static match_table_t udbalevel = {
26029 +       {AuOpt_UDBA_REVAL, "reval"},
26030 +       {AuOpt_UDBA_NONE, "none"},
26031 +#ifdef CONFIG_AUFS_HNOTIFY
26032 +       {AuOpt_UDBA_HNOTIFY, "notify"}, /* abstraction */
26033 +#ifdef CONFIG_AUFS_HFSNOTIFY
26034 +       {AuOpt_UDBA_HNOTIFY, "fsnotify"},
26035 +#endif
26036 +#endif
26037 +       {-1, NULL}
26038 +};
26039 +
26040 +static int noinline_for_stack udba_val(char *str)
26041 +{
26042 +       substring_t args[MAX_OPT_ARGS];
26043 +
26044 +       return match_token(str, udbalevel, args);
26045 +}
26046 +
26047 +const char *au_optstr_udba(int udba)
26048 +{
26049 +       return au_parser_pattern(udba, udbalevel);
26050 +}
26051 +
26052 +/* ---------------------------------------------------------------------- */
26053 +
26054 +static match_table_t au_wbr_create_policy = {
26055 +       {AuWbrCreate_TDP, "tdp"},
26056 +       {AuWbrCreate_TDP, "top-down-parent"},
26057 +       {AuWbrCreate_RR, "rr"},
26058 +       {AuWbrCreate_RR, "round-robin"},
26059 +       {AuWbrCreate_MFS, "mfs"},
26060 +       {AuWbrCreate_MFS, "most-free-space"},
26061 +       {AuWbrCreate_MFSV, "mfs:%d"},
26062 +       {AuWbrCreate_MFSV, "most-free-space:%d"},
26063 +
26064 +       /* top-down regardless the parent, and then mfs */
26065 +       {AuWbrCreate_TDMFS, "tdmfs:%d"},
26066 +       {AuWbrCreate_TDMFSV, "tdmfs:%d:%d"},
26067 +
26068 +       {AuWbrCreate_MFSRR, "mfsrr:%d"},
26069 +       {AuWbrCreate_MFSRRV, "mfsrr:%d:%d"},
26070 +       {AuWbrCreate_PMFS, "pmfs"},
26071 +       {AuWbrCreate_PMFSV, "pmfs:%d"},
26072 +       {AuWbrCreate_PMFSRR, "pmfsrr:%d"},
26073 +       {AuWbrCreate_PMFSRRV, "pmfsrr:%d:%d"},
26074 +
26075 +       {-1, NULL}
26076 +};
26077 +
26078 +static int au_wbr_mfs_wmark(substring_t *arg, char *str,
26079 +                           struct au_opt_wbr_create *create)
26080 +{
26081 +       int err;
26082 +       unsigned long long ull;
26083 +
26084 +       err = 0;
26085 +       if (!match_u64(arg, &ull))
26086 +               create->mfsrr_watermark = ull;
26087 +       else {
26088 +               pr_err("bad integer in %s\n", str);
26089 +               err = -EINVAL;
26090 +       }
26091 +
26092 +       return err;
26093 +}
26094 +
26095 +static int au_wbr_mfs_sec(substring_t *arg, char *str,
26096 +                         struct au_opt_wbr_create *create)
26097 +{
26098 +       int n, err;
26099 +
26100 +       err = 0;
26101 +       if (!match_int(arg, &n) && 0 <= n && n <= AUFS_MFS_MAX_SEC)
26102 +               create->mfs_second = n;
26103 +       else {
26104 +               pr_err("bad integer in %s\n", str);
26105 +               err = -EINVAL;
26106 +       }
26107 +
26108 +       return err;
26109 +}
26110 +
26111 +static int noinline_for_stack
26112 +au_wbr_create_val(char *str, struct au_opt_wbr_create *create)
26113 +{
26114 +       int err, e;
26115 +       substring_t args[MAX_OPT_ARGS];
26116 +
26117 +       err = match_token(str, au_wbr_create_policy, args);
26118 +       create->wbr_create = err;
26119 +       switch (err) {
26120 +       case AuWbrCreate_MFSRRV:
26121 +       case AuWbrCreate_TDMFSV:
26122 +       case AuWbrCreate_PMFSRRV:
26123 +               e = au_wbr_mfs_wmark(&args[0], str, create);
26124 +               if (!e)
26125 +                       e = au_wbr_mfs_sec(&args[1], str, create);
26126 +               if (unlikely(e))
26127 +                       err = e;
26128 +               break;
26129 +       case AuWbrCreate_MFSRR:
26130 +       case AuWbrCreate_TDMFS:
26131 +       case AuWbrCreate_PMFSRR:
26132 +               e = au_wbr_mfs_wmark(&args[0], str, create);
26133 +               if (unlikely(e)) {
26134 +                       err = e;
26135 +                       break;
26136 +               }
26137 +               fallthrough;
26138 +       case AuWbrCreate_MFS:
26139 +       case AuWbrCreate_PMFS:
26140 +               create->mfs_second = AUFS_MFS_DEF_SEC;
26141 +               break;
26142 +       case AuWbrCreate_MFSV:
26143 +       case AuWbrCreate_PMFSV:
26144 +               e = au_wbr_mfs_sec(&args[0], str, create);
26145 +               if (unlikely(e))
26146 +                       err = e;
26147 +               break;
26148 +       }
26149 +
26150 +       return err;
26151 +}
26152 +
26153 +const char *au_optstr_wbr_create(int wbr_create)
26154 +{
26155 +       return au_parser_pattern(wbr_create, au_wbr_create_policy);
26156 +}
26157 +
26158 +static match_table_t au_wbr_copyup_policy = {
26159 +       {AuWbrCopyup_TDP, "tdp"},
26160 +       {AuWbrCopyup_TDP, "top-down-parent"},
26161 +       {AuWbrCopyup_BUP, "bup"},
26162 +       {AuWbrCopyup_BUP, "bottom-up-parent"},
26163 +       {AuWbrCopyup_BU, "bu"},
26164 +       {AuWbrCopyup_BU, "bottom-up"},
26165 +       {-1, NULL}
26166 +};
26167 +
26168 +static int noinline_for_stack au_wbr_copyup_val(char *str)
26169 +{
26170 +       substring_t args[MAX_OPT_ARGS];
26171 +
26172 +       return match_token(str, au_wbr_copyup_policy, args);
26173 +}
26174 +
26175 +const char *au_optstr_wbr_copyup(int wbr_copyup)
26176 +{
26177 +       return au_parser_pattern(wbr_copyup, au_wbr_copyup_policy);
26178 +}
26179 +
26180 +/* ---------------------------------------------------------------------- */
26181 +
26182 +static const int lkup_dirflags = LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
26183 +
26184 +static void dump_opts(struct au_opts *opts)
26185 +{
26186 +#ifdef CONFIG_AUFS_DEBUG
26187 +       /* reduce stack space */
26188 +       union {
26189 +               struct au_opt_add *add;
26190 +               struct au_opt_del *del;
26191 +               struct au_opt_mod *mod;
26192 +               struct au_opt_xino *xino;
26193 +               struct au_opt_xino_itrunc *xino_itrunc;
26194 +               struct au_opt_wbr_create *create;
26195 +       } u;
26196 +       struct au_opt *opt;
26197 +
26198 +       opt = opts->opt;
26199 +       while (opt->type != Opt_tail) {
26200 +               switch (opt->type) {
26201 +               case Opt_add:
26202 +                       u.add = &opt->add;
26203 +                       AuDbg("add {b%d, %s, 0x%x, %p}\n",
26204 +                                 u.add->bindex, u.add->pathname, u.add->perm,
26205 +                                 u.add->path.dentry);
26206 +                       break;
26207 +               case Opt_del:
26208 +               case Opt_idel:
26209 +                       u.del = &opt->del;
26210 +                       AuDbg("del {%s, %p}\n",
26211 +                             u.del->pathname, u.del->h_path.dentry);
26212 +                       break;
26213 +               case Opt_mod:
26214 +               case Opt_imod:
26215 +                       u.mod = &opt->mod;
26216 +                       AuDbg("mod {%s, 0x%x, %p}\n",
26217 +                                 u.mod->path, u.mod->perm, u.mod->h_root);
26218 +                       break;
26219 +               case Opt_append:
26220 +                       u.add = &opt->add;
26221 +                       AuDbg("append {b%d, %s, 0x%x, %p}\n",
26222 +                                 u.add->bindex, u.add->pathname, u.add->perm,
26223 +                                 u.add->path.dentry);
26224 +                       break;
26225 +               case Opt_prepend:
26226 +                       u.add = &opt->add;
26227 +                       AuDbg("prepend {b%d, %s, 0x%x, %p}\n",
26228 +                                 u.add->bindex, u.add->pathname, u.add->perm,
26229 +                                 u.add->path.dentry);
26230 +                       break;
26231 +               case Opt_dirwh:
26232 +                       AuDbg("dirwh %d\n", opt->dirwh);
26233 +                       break;
26234 +               case Opt_rdcache:
26235 +                       AuDbg("rdcache %d\n", opt->rdcache);
26236 +                       break;
26237 +               case Opt_rdblk:
26238 +                       AuDbg("rdblk %u\n", opt->rdblk);
26239 +                       break;
26240 +               case Opt_rdblk_def:
26241 +                       AuDbg("rdblk_def\n");
26242 +                       break;
26243 +               case Opt_rdhash:
26244 +                       AuDbg("rdhash %u\n", opt->rdhash);
26245 +                       break;
26246 +               case Opt_rdhash_def:
26247 +                       AuDbg("rdhash_def\n");
26248 +                       break;
26249 +               case Opt_xino:
26250 +                       u.xino = &opt->xino;
26251 +                       AuDbg("xino {%s %pD}\n", u.xino->path, u.xino->file);
26252 +                       break;
26253 +               case Opt_trunc_xino:
26254 +                       AuLabel(trunc_xino);
26255 +                       break;
26256 +               case Opt_notrunc_xino:
26257 +                       AuLabel(notrunc_xino);
26258 +                       break;
26259 +               case Opt_trunc_xino_path:
26260 +               case Opt_itrunc_xino:
26261 +                       u.xino_itrunc = &opt->xino_itrunc;
26262 +                       AuDbg("trunc_xino %d\n", u.xino_itrunc->bindex);
26263 +                       break;
26264 +               case Opt_noxino:
26265 +                       AuLabel(noxino);
26266 +                       break;
26267 +               case Opt_trunc_xib:
26268 +                       AuLabel(trunc_xib);
26269 +                       break;
26270 +               case Opt_notrunc_xib:
26271 +                       AuLabel(notrunc_xib);
26272 +                       break;
26273 +               case Opt_shwh:
26274 +                       AuLabel(shwh);
26275 +                       break;
26276 +               case Opt_noshwh:
26277 +                       AuLabel(noshwh);
26278 +                       break;
26279 +               case Opt_dirperm1:
26280 +                       AuLabel(dirperm1);
26281 +                       break;
26282 +               case Opt_nodirperm1:
26283 +                       AuLabel(nodirperm1);
26284 +                       break;
26285 +               case Opt_plink:
26286 +                       AuLabel(plink);
26287 +                       break;
26288 +               case Opt_noplink:
26289 +                       AuLabel(noplink);
26290 +                       break;
26291 +               case Opt_list_plink:
26292 +                       AuLabel(list_plink);
26293 +                       break;
26294 +               case Opt_udba:
26295 +                       AuDbg("udba %d, %s\n",
26296 +                                 opt->udba, au_optstr_udba(opt->udba));
26297 +                       break;
26298 +               case Opt_dio:
26299 +                       AuLabel(dio);
26300 +                       break;
26301 +               case Opt_nodio:
26302 +                       AuLabel(nodio);
26303 +                       break;
26304 +               case Opt_diropq_a:
26305 +                       AuLabel(diropq_a);
26306 +                       break;
26307 +               case Opt_diropq_w:
26308 +                       AuLabel(diropq_w);
26309 +                       break;
26310 +               case Opt_warn_perm:
26311 +                       AuLabel(warn_perm);
26312 +                       break;
26313 +               case Opt_nowarn_perm:
26314 +                       AuLabel(nowarn_perm);
26315 +                       break;
26316 +               case Opt_verbose:
26317 +                       AuLabel(verbose);
26318 +                       break;
26319 +               case Opt_noverbose:
26320 +                       AuLabel(noverbose);
26321 +                       break;
26322 +               case Opt_sum:
26323 +                       AuLabel(sum);
26324 +                       break;
26325 +               case Opt_nosum:
26326 +                       AuLabel(nosum);
26327 +                       break;
26328 +               case Opt_wsum:
26329 +                       AuLabel(wsum);
26330 +                       break;
26331 +               case Opt_wbr_create:
26332 +                       u.create = &opt->wbr_create;
26333 +                       AuDbg("create %d, %s\n", u.create->wbr_create,
26334 +                                 au_optstr_wbr_create(u.create->wbr_create));
26335 +                       switch (u.create->wbr_create) {
26336 +                       case AuWbrCreate_MFSV:
26337 +                       case AuWbrCreate_PMFSV:
26338 +                               AuDbg("%d sec\n", u.create->mfs_second);
26339 +                               break;
26340 +                       case AuWbrCreate_MFSRR:
26341 +                       case AuWbrCreate_TDMFS:
26342 +                               AuDbg("%llu watermark\n",
26343 +                                         u.create->mfsrr_watermark);
26344 +                               break;
26345 +                       case AuWbrCreate_MFSRRV:
26346 +                       case AuWbrCreate_TDMFSV:
26347 +                       case AuWbrCreate_PMFSRRV:
26348 +                               AuDbg("%llu watermark, %d sec\n",
26349 +                                         u.create->mfsrr_watermark,
26350 +                                         u.create->mfs_second);
26351 +                               break;
26352 +                       }
26353 +                       break;
26354 +               case Opt_wbr_copyup:
26355 +                       AuDbg("copyup %d, %s\n", opt->wbr_copyup,
26356 +                                 au_optstr_wbr_copyup(opt->wbr_copyup));
26357 +                       break;
26358 +               case Opt_fhsm_sec:
26359 +                       AuDbg("fhsm_sec %u\n", opt->fhsm_second);
26360 +                       break;
26361 +               case Opt_dirren:
26362 +                       AuLabel(dirren);
26363 +                       break;
26364 +               case Opt_nodirren:
26365 +                       AuLabel(nodirren);
26366 +                       break;
26367 +               case Opt_acl:
26368 +                       AuLabel(acl);
26369 +                       break;
26370 +               case Opt_noacl:
26371 +                       AuLabel(noacl);
26372 +                       break;
26373 +               default:
26374 +                       BUG();
26375 +               }
26376 +               opt++;
26377 +       }
26378 +#endif
26379 +}
26380 +
26381 +void au_opts_free(struct au_opts *opts)
26382 +{
26383 +       struct au_opt *opt;
26384 +
26385 +       opt = opts->opt;
26386 +       while (opt->type != Opt_tail) {
26387 +               switch (opt->type) {
26388 +               case Opt_add:
26389 +               case Opt_append:
26390 +               case Opt_prepend:
26391 +                       path_put(&opt->add.path);
26392 +                       break;
26393 +               case Opt_del:
26394 +               case Opt_idel:
26395 +                       path_put(&opt->del.h_path);
26396 +                       break;
26397 +               case Opt_mod:
26398 +               case Opt_imod:
26399 +                       dput(opt->mod.h_root);
26400 +                       break;
26401 +               case Opt_xino:
26402 +                       fput(opt->xino.file);
26403 +                       break;
26404 +               }
26405 +               opt++;
26406 +       }
26407 +}
26408 +
26409 +static int opt_add(struct au_opt *opt, char *opt_str, unsigned long sb_flags,
26410 +                  aufs_bindex_t bindex)
26411 +{
26412 +       int err;
26413 +       struct au_opt_add *add = &opt->add;
26414 +       char *p;
26415 +
26416 +       add->bindex = bindex;
26417 +       add->perm = AuBrPerm_RO;
26418 +       add->pathname = opt_str;
26419 +       p = strchr(opt_str, '=');
26420 +       if (p) {
26421 +               *p++ = 0;
26422 +               if (*p)
26423 +                       add->perm = br_perm_val(p);
26424 +       }
26425 +
26426 +       err = vfsub_kern_path(add->pathname, lkup_dirflags, &add->path);
26427 +       if (!err) {
26428 +               if (!p) {
26429 +                       add->perm = AuBrPerm_RO;
26430 +                       if (au_test_fs_rr(add->path.dentry->d_sb))
26431 +                               add->perm = AuBrPerm_RR;
26432 +                       else if (!bindex && !(sb_flags & SB_RDONLY))
26433 +                               add->perm = AuBrPerm_RW;
26434 +               }
26435 +               opt->type = Opt_add;
26436 +               goto out;
26437 +       }
26438 +       pr_err("lookup failed %s (%d)\n", add->pathname, err);
26439 +       err = -EINVAL;
26440 +
26441 +out:
26442 +       return err;
26443 +}
26444 +
26445 +static int au_opts_parse_del(struct au_opt_del *del, substring_t args[])
26446 +{
26447 +       int err;
26448 +
26449 +       del->pathname = args[0].from;
26450 +       AuDbg("del path %s\n", del->pathname);
26451 +
26452 +       err = vfsub_kern_path(del->pathname, lkup_dirflags, &del->h_path);
26453 +       if (unlikely(err))
26454 +               pr_err("lookup failed %s (%d)\n", del->pathname, err);
26455 +
26456 +       return err;
26457 +}
26458 +
26459 +#if 0 /* reserved for future use */
26460 +static int au_opts_parse_idel(struct super_block *sb, aufs_bindex_t bindex,
26461 +                             struct au_opt_del *del, substring_t args[])
26462 +{
26463 +       int err;
26464 +       struct dentry *root;
26465 +
26466 +       err = -EINVAL;
26467 +       root = sb->s_root;
26468 +       aufs_read_lock(root, AuLock_FLUSH);
26469 +       if (bindex < 0 || au_sbbot(sb) < bindex) {
26470 +               pr_err("out of bounds, %d\n", bindex);
26471 +               goto out;
26472 +       }
26473 +
26474 +       err = 0;
26475 +       del->h_path.dentry = dget(au_h_dptr(root, bindex));
26476 +       del->h_path.mnt = mntget(au_sbr_mnt(sb, bindex));
26477 +
26478 +out:
26479 +       aufs_read_unlock(root, !AuLock_IR);
26480 +       return err;
26481 +}
26482 +#endif
26483 +
26484 +static int noinline_for_stack
26485 +au_opts_parse_mod(struct au_opt_mod *mod, substring_t args[])
26486 +{
26487 +       int err;
26488 +       struct path path;
26489 +       char *p;
26490 +
26491 +       err = -EINVAL;
26492 +       mod->path = args[0].from;
26493 +       p = strchr(mod->path, '=');
26494 +       if (unlikely(!p)) {
26495 +               pr_err("no permission %s\n", args[0].from);
26496 +               goto out;
26497 +       }
26498 +
26499 +       *p++ = 0;
26500 +       err = vfsub_kern_path(mod->path, lkup_dirflags, &path);
26501 +       if (unlikely(err)) {
26502 +               pr_err("lookup failed %s (%d)\n", mod->path, err);
26503 +               goto out;
26504 +       }
26505 +
26506 +       mod->perm = br_perm_val(p);
26507 +       AuDbg("mod path %s, perm 0x%x, %s\n", mod->path, mod->perm, p);
26508 +       mod->h_root = dget(path.dentry);
26509 +       path_put(&path);
26510 +
26511 +out:
26512 +       return err;
26513 +}
26514 +
26515 +#if 0 /* reserved for future use */
26516 +static int au_opts_parse_imod(struct super_block *sb, aufs_bindex_t bindex,
26517 +                             struct au_opt_mod *mod, substring_t args[])
26518 +{
26519 +       int err;
26520 +       struct dentry *root;
26521 +
26522 +       err = -EINVAL;
26523 +       root = sb->s_root;
26524 +       aufs_read_lock(root, AuLock_FLUSH);
26525 +       if (bindex < 0 || au_sbbot(sb) < bindex) {
26526 +               pr_err("out of bounds, %d\n", bindex);
26527 +               goto out;
26528 +       }
26529 +
26530 +       err = 0;
26531 +       mod->perm = br_perm_val(args[1].from);
26532 +       AuDbg("mod path %s, perm 0x%x, %s\n",
26533 +             mod->path, mod->perm, args[1].from);
26534 +       mod->h_root = dget(au_h_dptr(root, bindex));
26535 +
26536 +out:
26537 +       aufs_read_unlock(root, !AuLock_IR);
26538 +       return err;
26539 +}
26540 +#endif
26541 +
26542 +static int au_opts_parse_xino(struct super_block *sb, struct au_opt_xino *xino,
26543 +                             substring_t args[])
26544 +{
26545 +       int err;
26546 +       struct file *file;
26547 +
26548 +       file = au_xino_create(sb, args[0].from, /*silent*/0, /*wbrtop*/0);
26549 +       err = PTR_ERR(file);
26550 +       if (IS_ERR(file))
26551 +               goto out;
26552 +
26553 +       err = -EINVAL;
26554 +       if (unlikely(file->f_path.dentry->d_sb == sb)) {
26555 +               fput(file);
26556 +               pr_err("%s must be outside\n", args[0].from);
26557 +               goto out;
26558 +       }
26559 +
26560 +       err = 0;
26561 +       xino->file = file;
26562 +       xino->path = args[0].from;
26563 +
26564 +out:
26565 +       return err;
26566 +}
26567 +
26568 +static int noinline_for_stack
26569 +au_opts_parse_xino_itrunc_path(struct super_block *sb,
26570 +                              struct au_opt_xino_itrunc *xino_itrunc,
26571 +                              substring_t args[])
26572 +{
26573 +       int err;
26574 +       aufs_bindex_t bbot, bindex;
26575 +       struct path path;
26576 +       struct dentry *root;
26577 +
26578 +       err = vfsub_kern_path(args[0].from, lkup_dirflags, &path);
26579 +       if (unlikely(err)) {
26580 +               pr_err("lookup failed %s (%d)\n", args[0].from, err);
26581 +               goto out;
26582 +       }
26583 +
26584 +       xino_itrunc->bindex = -1;
26585 +       root = sb->s_root;
26586 +       aufs_read_lock(root, AuLock_FLUSH);
26587 +       bbot = au_sbbot(sb);
26588 +       for (bindex = 0; bindex <= bbot; bindex++) {
26589 +               if (au_h_dptr(root, bindex) == path.dentry) {
26590 +                       xino_itrunc->bindex = bindex;
26591 +                       break;
26592 +               }
26593 +       }
26594 +       aufs_read_unlock(root, !AuLock_IR);
26595 +       path_put(&path);
26596 +
26597 +       if (unlikely(xino_itrunc->bindex < 0)) {
26598 +               pr_err("no such branch %s\n", args[0].from);
26599 +               err = -EINVAL;
26600 +       }
26601 +
26602 +out:
26603 +       return err;
26604 +}
26605 +
26606 +/* called without aufs lock */
26607 +int au_opts_parse(struct super_block *sb, char *str, struct au_opts *opts)
26608 +{
26609 +       int err, n, token;
26610 +       aufs_bindex_t bindex;
26611 +       unsigned char skipped;
26612 +       struct dentry *root;
26613 +       struct au_opt *opt, *opt_tail;
26614 +       char *opt_str;
26615 +       /* reduce the stack space */
26616 +       union {
26617 +               struct au_opt_xino_itrunc *xino_itrunc;
26618 +               struct au_opt_wbr_create *create;
26619 +       } u;
26620 +       struct {
26621 +               substring_t args[MAX_OPT_ARGS];
26622 +       } *a;
26623 +
26624 +       err = -ENOMEM;
26625 +       a = kmalloc(sizeof(*a), GFP_NOFS);
26626 +       if (unlikely(!a))
26627 +               goto out;
26628 +
26629 +       root = sb->s_root;
26630 +       err = 0;
26631 +       bindex = 0;
26632 +       opt = opts->opt;
26633 +       opt_tail = opt + opts->max_opt - 1;
26634 +       opt->type = Opt_tail;
26635 +       while (!err && (opt_str = strsep(&str, ",")) && *opt_str) {
26636 +               err = -EINVAL;
26637 +               skipped = 0;
26638 +               token = match_token(opt_str, options, a->args);
26639 +               switch (token) {
26640 +               case Opt_br:
26641 +                       err = 0;
26642 +                       while (!err && (opt_str = strsep(&a->args[0].from, ":"))
26643 +                              && *opt_str) {
26644 +                               err = opt_add(opt, opt_str, opts->sb_flags,
26645 +                                             bindex++);
26646 +                               if (unlikely(!err && ++opt > opt_tail)) {
26647 +                                       err = -E2BIG;
26648 +                                       break;
26649 +                               }
26650 +                               opt->type = Opt_tail;
26651 +                               skipped = 1;
26652 +                       }
26653 +                       break;
26654 +               case Opt_add:
26655 +                       if (unlikely(match_int(&a->args[0], &n))) {
26656 +                               pr_err("bad integer in %s\n", opt_str);
26657 +                               break;
26658 +                       }
26659 +                       bindex = n;
26660 +                       err = opt_add(opt, a->args[1].from, opts->sb_flags,
26661 +                                     bindex);
26662 +                       if (!err)
26663 +                               opt->type = token;
26664 +                       break;
26665 +               case Opt_append:
26666 +                       err = opt_add(opt, a->args[0].from, opts->sb_flags,
26667 +                                     /*dummy bindex*/1);
26668 +                       if (!err)
26669 +                               opt->type = token;
26670 +                       break;
26671 +               case Opt_prepend:
26672 +                       err = opt_add(opt, a->args[0].from, opts->sb_flags,
26673 +                                     /*bindex*/0);
26674 +                       if (!err)
26675 +                               opt->type = token;
26676 +                       break;
26677 +               case Opt_del:
26678 +                       err = au_opts_parse_del(&opt->del, a->args);
26679 +                       if (!err)
26680 +                               opt->type = token;
26681 +                       break;
26682 +#if 0 /* reserved for future use */
26683 +               case Opt_idel:
26684 +                       del->pathname = "(indexed)";
26685 +                       if (unlikely(match_int(&args[0], &n))) {
26686 +                               pr_err("bad integer in %s\n", opt_str);
26687 +                               break;
26688 +                       }
26689 +                       err = au_opts_parse_idel(sb, n, &opt->del, a->args);
26690 +                       if (!err)
26691 +                               opt->type = token;
26692 +                       break;
26693 +#endif
26694 +               case Opt_mod:
26695 +                       err = au_opts_parse_mod(&opt->mod, a->args);
26696 +                       if (!err)
26697 +                               opt->type = token;
26698 +                       break;
26699 +#ifdef IMOD /* reserved for future use */
26700 +               case Opt_imod:
26701 +                       u.mod->path = "(indexed)";
26702 +                       if (unlikely(match_int(&a->args[0], &n))) {
26703 +                               pr_err("bad integer in %s\n", opt_str);
26704 +                               break;
26705 +                       }
26706 +                       err = au_opts_parse_imod(sb, n, &opt->mod, a->args);
26707 +                       if (!err)
26708 +                               opt->type = token;
26709 +                       break;
26710 +#endif
26711 +               case Opt_xino:
26712 +                       err = au_opts_parse_xino(sb, &opt->xino, a->args);
26713 +                       if (!err)
26714 +                               opt->type = token;
26715 +                       break;
26716 +
26717 +               case Opt_trunc_xino_path:
26718 +                       err = au_opts_parse_xino_itrunc_path
26719 +                               (sb, &opt->xino_itrunc, a->args);
26720 +                       if (!err)
26721 +                               opt->type = token;
26722 +                       break;
26723 +
26724 +               case Opt_itrunc_xino:
26725 +                       u.xino_itrunc = &opt->xino_itrunc;
26726 +                       if (unlikely(match_int(&a->args[0], &n))) {
26727 +                               pr_err("bad integer in %s\n", opt_str);
26728 +                               break;
26729 +                       }
26730 +                       u.xino_itrunc->bindex = n;
26731 +                       aufs_read_lock(root, AuLock_FLUSH);
26732 +                       if (n < 0 || au_sbbot(sb) < n) {
26733 +                               pr_err("out of bounds, %d\n", n);
26734 +                               aufs_read_unlock(root, !AuLock_IR);
26735 +                               break;
26736 +                       }
26737 +                       aufs_read_unlock(root, !AuLock_IR);
26738 +                       err = 0;
26739 +                       opt->type = token;
26740 +                       break;
26741 +
26742 +               case Opt_dirwh:
26743 +                       if (unlikely(match_int(&a->args[0], &opt->dirwh)))
26744 +                               break;
26745 +                       err = 0;
26746 +                       opt->type = token;
26747 +                       break;
26748 +
26749 +               case Opt_rdcache:
26750 +                       if (unlikely(match_int(&a->args[0], &n))) {
26751 +                               pr_err("bad integer in %s\n", opt_str);
26752 +                               break;
26753 +                       }
26754 +                       if (unlikely(n > AUFS_RDCACHE_MAX)) {
26755 +                               pr_err("rdcache must be smaller than %d\n",
26756 +                                      AUFS_RDCACHE_MAX);
26757 +                               break;
26758 +                       }
26759 +                       opt->rdcache = n;
26760 +                       err = 0;
26761 +                       opt->type = token;
26762 +                       break;
26763 +               case Opt_rdblk:
26764 +                       if (unlikely(match_int(&a->args[0], &n)
26765 +                                    || n < 0
26766 +                                    || n > KMALLOC_MAX_SIZE)) {
26767 +                               pr_err("bad integer in %s\n", opt_str);
26768 +                               break;
26769 +                       }
26770 +                       if (unlikely(n && n < NAME_MAX)) {
26771 +                               pr_err("rdblk must be larger than %d\n",
26772 +                                      NAME_MAX);
26773 +                               break;
26774 +                       }
26775 +                       opt->rdblk = n;
26776 +                       err = 0;
26777 +                       opt->type = token;
26778 +                       break;
26779 +               case Opt_rdhash:
26780 +                       if (unlikely(match_int(&a->args[0], &n)
26781 +                                    || n < 0
26782 +                                    || n * sizeof(struct hlist_head)
26783 +                                    > KMALLOC_MAX_SIZE)) {
26784 +                               pr_err("bad integer in %s\n", opt_str);
26785 +                               break;
26786 +                       }
26787 +                       opt->rdhash = n;
26788 +                       err = 0;
26789 +                       opt->type = token;
26790 +                       break;
26791 +
26792 +               case Opt_trunc_xino:
26793 +               case Opt_notrunc_xino:
26794 +               case Opt_noxino:
26795 +               case Opt_trunc_xib:
26796 +               case Opt_notrunc_xib:
26797 +               case Opt_shwh:
26798 +               case Opt_noshwh:
26799 +               case Opt_dirperm1:
26800 +               case Opt_nodirperm1:
26801 +               case Opt_plink:
26802 +               case Opt_noplink:
26803 +               case Opt_list_plink:
26804 +               case Opt_dio:
26805 +               case Opt_nodio:
26806 +               case Opt_diropq_a:
26807 +               case Opt_diropq_w:
26808 +               case Opt_warn_perm:
26809 +               case Opt_nowarn_perm:
26810 +               case Opt_verbose:
26811 +               case Opt_noverbose:
26812 +               case Opt_sum:
26813 +               case Opt_nosum:
26814 +               case Opt_wsum:
26815 +               case Opt_rdblk_def:
26816 +               case Opt_rdhash_def:
26817 +               case Opt_dirren:
26818 +               case Opt_nodirren:
26819 +               case Opt_acl:
26820 +               case Opt_noacl:
26821 +                       err = 0;
26822 +                       opt->type = token;
26823 +                       break;
26824 +
26825 +               case Opt_udba:
26826 +                       opt->udba = udba_val(a->args[0].from);
26827 +                       if (opt->udba >= 0) {
26828 +                               err = 0;
26829 +                               opt->type = token;
26830 +                       } else
26831 +                               pr_err("wrong value, %s\n", opt_str);
26832 +                       break;
26833 +
26834 +               case Opt_wbr_create:
26835 +                       u.create = &opt->wbr_create;
26836 +                       u.create->wbr_create
26837 +                               = au_wbr_create_val(a->args[0].from, u.create);
26838 +                       if (u.create->wbr_create >= 0) {
26839 +                               err = 0;
26840 +                               opt->type = token;
26841 +                       } else
26842 +                               pr_err("wrong value, %s\n", opt_str);
26843 +                       break;
26844 +               case Opt_wbr_copyup:
26845 +                       opt->wbr_copyup = au_wbr_copyup_val(a->args[0].from);
26846 +                       if (opt->wbr_copyup >= 0) {
26847 +                               err = 0;
26848 +                               opt->type = token;
26849 +                       } else
26850 +                               pr_err("wrong value, %s\n", opt_str);
26851 +                       break;
26852 +
26853 +               case Opt_fhsm_sec:
26854 +                       if (unlikely(match_int(&a->args[0], &n)
26855 +                                    || n < 0)) {
26856 +                               pr_err("bad integer in %s\n", opt_str);
26857 +                               break;
26858 +                       }
26859 +                       if (sysaufs_brs) {
26860 +                               opt->fhsm_second = n;
26861 +                               opt->type = token;
26862 +                       } else
26863 +                               pr_warn("ignored %s\n", opt_str);
26864 +                       err = 0;
26865 +                       break;
26866 +
26867 +               case Opt_ignore:
26868 +                       pr_warn("ignored %s\n", opt_str);
26869 +                       fallthrough;
26870 +               case Opt_ignore_silent:
26871 +                       skipped = 1;
26872 +                       err = 0;
26873 +                       break;
26874 +               case Opt_err:
26875 +                       pr_err("unknown option %s\n", opt_str);
26876 +                       break;
26877 +               }
26878 +
26879 +               if (!err && !skipped) {
26880 +                       if (unlikely(++opt > opt_tail)) {
26881 +                               err = -E2BIG;
26882 +                               opt--;
26883 +                               opt->type = Opt_tail;
26884 +                               break;
26885 +                       }
26886 +                       opt->type = Opt_tail;
26887 +               }
26888 +       }
26889 +
26890 +       au_kfree_rcu(a);
26891 +       dump_opts(opts);
26892 +       if (unlikely(err))
26893 +               au_opts_free(opts);
26894 +
26895 +out:
26896 +       return err;
26897 +}
26898 +
26899 +static int au_opt_wbr_create(struct super_block *sb,
26900 +                            struct au_opt_wbr_create *create)
26901 +{
26902 +       int err;
26903 +       struct au_sbinfo *sbinfo;
26904 +
26905 +       SiMustWriteLock(sb);
26906 +
26907 +       err = 1; /* handled */
26908 +       sbinfo = au_sbi(sb);
26909 +       if (sbinfo->si_wbr_create_ops->fin) {
26910 +               err = sbinfo->si_wbr_create_ops->fin(sb);
26911 +               if (!err)
26912 +                       err = 1;
26913 +       }
26914 +
26915 +       sbinfo->si_wbr_create = create->wbr_create;
26916 +       sbinfo->si_wbr_create_ops = au_wbr_create_ops + create->wbr_create;
26917 +       switch (create->wbr_create) {
26918 +       case AuWbrCreate_MFSRRV:
26919 +       case AuWbrCreate_MFSRR:
26920 +       case AuWbrCreate_TDMFS:
26921 +       case AuWbrCreate_TDMFSV:
26922 +       case AuWbrCreate_PMFSRR:
26923 +       case AuWbrCreate_PMFSRRV:
26924 +               sbinfo->si_wbr_mfs.mfsrr_watermark = create->mfsrr_watermark;
26925 +               fallthrough;
26926 +       case AuWbrCreate_MFS:
26927 +       case AuWbrCreate_MFSV:
26928 +       case AuWbrCreate_PMFS:
26929 +       case AuWbrCreate_PMFSV:
26930 +               sbinfo->si_wbr_mfs.mfs_expire
26931 +                       = msecs_to_jiffies(create->mfs_second * MSEC_PER_SEC);
26932 +               break;
26933 +       }
26934 +
26935 +       if (sbinfo->si_wbr_create_ops->init)
26936 +               sbinfo->si_wbr_create_ops->init(sb); /* ignore */
26937 +
26938 +       return err;
26939 +}
26940 +
26941 +/*
26942 + * returns,
26943 + * plus: processed without an error
26944 + * zero: unprocessed
26945 + */
26946 +static int au_opt_simple(struct super_block *sb, struct au_opt *opt,
26947 +                        struct au_opts *opts)
26948 +{
26949 +       int err;
26950 +       struct au_sbinfo *sbinfo;
26951 +
26952 +       SiMustWriteLock(sb);
26953 +
26954 +       err = 1; /* handled */
26955 +       sbinfo = au_sbi(sb);
26956 +       switch (opt->type) {
26957 +       case Opt_udba:
26958 +               sbinfo->si_mntflags &= ~AuOptMask_UDBA;
26959 +               sbinfo->si_mntflags |= opt->udba;
26960 +               opts->given_udba |= opt->udba;
26961 +               break;
26962 +
26963 +       case Opt_plink:
26964 +               au_opt_set(sbinfo->si_mntflags, PLINK);
26965 +               break;
26966 +       case Opt_noplink:
26967 +               if (au_opt_test(sbinfo->si_mntflags, PLINK))
26968 +                       au_plink_put(sb, /*verbose*/1);
26969 +               au_opt_clr(sbinfo->si_mntflags, PLINK);
26970 +               break;
26971 +       case Opt_list_plink:
26972 +               if (au_opt_test(sbinfo->si_mntflags, PLINK))
26973 +                       au_plink_list(sb);
26974 +               break;
26975 +
26976 +       case Opt_dio:
26977 +               au_opt_set(sbinfo->si_mntflags, DIO);
26978 +               au_fset_opts(opts->flags, REFRESH_DYAOP);
26979 +               break;
26980 +       case Opt_nodio:
26981 +               au_opt_clr(sbinfo->si_mntflags, DIO);
26982 +               au_fset_opts(opts->flags, REFRESH_DYAOP);
26983 +               break;
26984 +
26985 +       case Opt_fhsm_sec:
26986 +               au_fhsm_set(sbinfo, opt->fhsm_second);
26987 +               break;
26988 +
26989 +       case Opt_diropq_a:
26990 +               au_opt_set(sbinfo->si_mntflags, ALWAYS_DIROPQ);
26991 +               break;
26992 +       case Opt_diropq_w:
26993 +               au_opt_clr(sbinfo->si_mntflags, ALWAYS_DIROPQ);
26994 +               break;
26995 +
26996 +       case Opt_warn_perm:
26997 +               au_opt_set(sbinfo->si_mntflags, WARN_PERM);
26998 +               break;
26999 +       case Opt_nowarn_perm:
27000 +               au_opt_clr(sbinfo->si_mntflags, WARN_PERM);
27001 +               break;
27002 +
27003 +       case Opt_verbose:
27004 +               au_opt_set(sbinfo->si_mntflags, VERBOSE);
27005 +               break;
27006 +       case Opt_noverbose:
27007 +               au_opt_clr(sbinfo->si_mntflags, VERBOSE);
27008 +               break;
27009 +
27010 +       case Opt_sum:
27011 +               au_opt_set(sbinfo->si_mntflags, SUM);
27012 +               break;
27013 +       case Opt_wsum:
27014 +               au_opt_clr(sbinfo->si_mntflags, SUM);
27015 +               au_opt_set(sbinfo->si_mntflags, SUM_W);
27016 +               break;
27017 +       case Opt_nosum:
27018 +               au_opt_clr(sbinfo->si_mntflags, SUM);
27019 +               au_opt_clr(sbinfo->si_mntflags, SUM_W);
27020 +               break;
27021 +
27022 +       case Opt_wbr_create:
27023 +               err = au_opt_wbr_create(sb, &opt->wbr_create);
27024 +               break;
27025 +       case Opt_wbr_copyup:
27026 +               sbinfo->si_wbr_copyup = opt->wbr_copyup;
27027 +               sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + opt->wbr_copyup;
27028 +               break;
27029 +
27030 +       case Opt_dirwh:
27031 +               sbinfo->si_dirwh = opt->dirwh;
27032 +               break;
27033 +
27034 +       case Opt_rdcache:
27035 +               sbinfo->si_rdcache
27036 +                       = msecs_to_jiffies(opt->rdcache * MSEC_PER_SEC);
27037 +               break;
27038 +       case Opt_rdblk:
27039 +               sbinfo->si_rdblk = opt->rdblk;
27040 +               break;
27041 +       case Opt_rdblk_def:
27042 +               sbinfo->si_rdblk = AUFS_RDBLK_DEF;
27043 +               break;
27044 +       case Opt_rdhash:
27045 +               sbinfo->si_rdhash = opt->rdhash;
27046 +               break;
27047 +       case Opt_rdhash_def:
27048 +               sbinfo->si_rdhash = AUFS_RDHASH_DEF;
27049 +               break;
27050 +
27051 +       case Opt_shwh:
27052 +               au_opt_set(sbinfo->si_mntflags, SHWH);
27053 +               break;
27054 +       case Opt_noshwh:
27055 +               au_opt_clr(sbinfo->si_mntflags, SHWH);
27056 +               break;
27057 +
27058 +       case Opt_dirperm1:
27059 +               au_opt_set(sbinfo->si_mntflags, DIRPERM1);
27060 +               break;
27061 +       case Opt_nodirperm1:
27062 +               au_opt_clr(sbinfo->si_mntflags, DIRPERM1);
27063 +               break;
27064 +
27065 +       case Opt_trunc_xino:
27066 +               au_opt_set(sbinfo->si_mntflags, TRUNC_XINO);
27067 +               break;
27068 +       case Opt_notrunc_xino:
27069 +               au_opt_clr(sbinfo->si_mntflags, TRUNC_XINO);
27070 +               break;
27071 +
27072 +       case Opt_trunc_xino_path:
27073 +       case Opt_itrunc_xino:
27074 +               err = au_xino_trunc(sb, opt->xino_itrunc.bindex,
27075 +                                   /*idx_begin*/0);
27076 +               if (!err)
27077 +                       err = 1;
27078 +               break;
27079 +
27080 +       case Opt_trunc_xib:
27081 +               au_fset_opts(opts->flags, TRUNC_XIB);
27082 +               break;
27083 +       case Opt_notrunc_xib:
27084 +               au_fclr_opts(opts->flags, TRUNC_XIB);
27085 +               break;
27086 +
27087 +       case Opt_dirren:
27088 +               err = 1;
27089 +               if (!au_opt_test(sbinfo->si_mntflags, DIRREN)) {
27090 +                       err = au_dr_opt_set(sb);
27091 +                       if (!err)
27092 +                               err = 1;
27093 +               }
27094 +               if (err == 1)
27095 +                       au_opt_set(sbinfo->si_mntflags, DIRREN);
27096 +               break;
27097 +       case Opt_nodirren:
27098 +               err = 1;
27099 +               if (au_opt_test(sbinfo->si_mntflags, DIRREN)) {
27100 +                       err = au_dr_opt_clr(sb, au_ftest_opts(opts->flags,
27101 +                                                             DR_FLUSHED));
27102 +                       if (!err)
27103 +                               err = 1;
27104 +               }
27105 +               if (err == 1)
27106 +                       au_opt_clr(sbinfo->si_mntflags, DIRREN);
27107 +               break;
27108 +
27109 +       case Opt_acl:
27110 +               sb->s_flags |= SB_POSIXACL;
27111 +               break;
27112 +       case Opt_noacl:
27113 +               sb->s_flags &= ~SB_POSIXACL;
27114 +               break;
27115 +
27116 +       default:
27117 +               err = 0;
27118 +               break;
27119 +       }
27120 +
27121 +       return err;
27122 +}
27123 +
27124 +/*
27125 + * returns tri-state.
27126 + * plus: processed without an error
27127 + * zero: unprocessed
27128 + * minus: error
27129 + */
27130 +static int au_opt_br(struct super_block *sb, struct au_opt *opt,
27131 +                    struct au_opts *opts)
27132 +{
27133 +       int err, do_refresh;
27134 +
27135 +       err = 0;
27136 +       switch (opt->type) {
27137 +       case Opt_append:
27138 +               opt->add.bindex = au_sbbot(sb) + 1;
27139 +               if (opt->add.bindex < 0)
27140 +                       opt->add.bindex = 0;
27141 +               goto add;
27142 +               /* Always goto add, not fallthrough */
27143 +       case Opt_prepend:
27144 +               opt->add.bindex = 0;
27145 +               fallthrough;
27146 +       add: /* indented label */
27147 +       case Opt_add:
27148 +               err = au_br_add(sb, &opt->add,
27149 +                               au_ftest_opts(opts->flags, REMOUNT));
27150 +               if (!err) {
27151 +                       err = 1;
27152 +                       au_fset_opts(opts->flags, REFRESH);
27153 +               }
27154 +               break;
27155 +
27156 +       case Opt_del:
27157 +       case Opt_idel:
27158 +               err = au_br_del(sb, &opt->del,
27159 +                               au_ftest_opts(opts->flags, REMOUNT));
27160 +               if (!err) {
27161 +                       err = 1;
27162 +                       au_fset_opts(opts->flags, TRUNC_XIB);
27163 +                       au_fset_opts(opts->flags, REFRESH);
27164 +               }
27165 +               break;
27166 +
27167 +       case Opt_mod:
27168 +       case Opt_imod:
27169 +               err = au_br_mod(sb, &opt->mod,
27170 +                               au_ftest_opts(opts->flags, REMOUNT),
27171 +                               &do_refresh);
27172 +               if (!err) {
27173 +                       err = 1;
27174 +                       if (do_refresh)
27175 +                               au_fset_opts(opts->flags, REFRESH);
27176 +               }
27177 +               break;
27178 +       }
27179 +       return err;
27180 +}
27181 +
27182 +static int au_opt_xino(struct super_block *sb, struct au_opt *opt,
27183 +                      struct au_opt_xino **opt_xino,
27184 +                      struct au_opts *opts)
27185 +{
27186 +       int err;
27187 +
27188 +       err = 0;
27189 +       switch (opt->type) {
27190 +       case Opt_xino:
27191 +               err = au_xino_set(sb, &opt->xino,
27192 +                                 !!au_ftest_opts(opts->flags, REMOUNT));
27193 +               if (unlikely(err))
27194 +                       break;
27195 +
27196 +               *opt_xino = &opt->xino;
27197 +               break;
27198 +
27199 +       case Opt_noxino:
27200 +               au_xino_clr(sb);
27201 +               *opt_xino = (void *)-1;
27202 +               break;
27203 +       }
27204 +
27205 +       return err;
27206 +}
27207 +
27208 +int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
27209 +                  unsigned int pending)
27210 +{
27211 +       int err, fhsm;
27212 +       aufs_bindex_t bindex, bbot;
27213 +       unsigned char do_plink, skip, do_free, can_no_dreval;
27214 +       struct au_branch *br;
27215 +       struct au_wbr *wbr;
27216 +       struct dentry *root, *dentry;
27217 +       struct inode *dir, *h_dir;
27218 +       struct au_sbinfo *sbinfo;
27219 +       struct au_hinode *hdir;
27220 +
27221 +       SiMustAnyLock(sb);
27222 +
27223 +       sbinfo = au_sbi(sb);
27224 +       AuDebugOn(!(sbinfo->si_mntflags & AuOptMask_UDBA));
27225 +
27226 +       if (!(sb_flags & SB_RDONLY)) {
27227 +               if (unlikely(!au_br_writable(au_sbr_perm(sb, 0))))
27228 +                       pr_warn("first branch should be rw\n");
27229 +               if (unlikely(au_opt_test(sbinfo->si_mntflags, SHWH)))
27230 +                       pr_warn_once("shwh should be used with ro\n");
27231 +       }
27232 +
27233 +       if (au_opt_test((sbinfo->si_mntflags | pending), UDBA_HNOTIFY)
27234 +           && !au_opt_test(sbinfo->si_mntflags, XINO))
27235 +               pr_warn_once("udba=*notify requires xino\n");
27236 +
27237 +       if (au_opt_test(sbinfo->si_mntflags, DIRPERM1))
27238 +               pr_warn_once("dirperm1 breaks the protection"
27239 +                            " by the permission bits on the lower branch\n");
27240 +
27241 +       err = 0;
27242 +       fhsm = 0;
27243 +       root = sb->s_root;
27244 +       dir = d_inode(root);
27245 +       do_plink = !!au_opt_test(sbinfo->si_mntflags, PLINK);
27246 +       can_no_dreval = !!au_opt_test((sbinfo->si_mntflags | pending),
27247 +                                     UDBA_NONE);
27248 +       bbot = au_sbbot(sb);
27249 +       for (bindex = 0; !err && bindex <= bbot; bindex++) {
27250 +               skip = 0;
27251 +               h_dir = au_h_iptr(dir, bindex);
27252 +               br = au_sbr(sb, bindex);
27253 +
27254 +               if ((br->br_perm & AuBrAttr_ICEX)
27255 +                   && !h_dir->i_op->listxattr)
27256 +                       br->br_perm &= ~AuBrAttr_ICEX;
27257 +#if 0 /* untested */
27258 +               if ((br->br_perm & AuBrAttr_ICEX_SEC)
27259 +                   && (au_br_sb(br)->s_flags & SB_NOSEC))
27260 +                       br->br_perm &= ~AuBrAttr_ICEX_SEC;
27261 +#endif
27262 +
27263 +               do_free = 0;
27264 +               wbr = br->br_wbr;
27265 +               if (wbr)
27266 +                       wbr_wh_read_lock(wbr);
27267 +
27268 +               if (!au_br_writable(br->br_perm)) {
27269 +                       do_free = !!wbr;
27270 +                       skip = (!wbr
27271 +                               || (!wbr->wbr_whbase
27272 +                                   && !wbr->wbr_plink
27273 +                                   && !wbr->wbr_orph));
27274 +               } else if (!au_br_wh_linkable(br->br_perm)) {
27275 +                       /* skip = (!br->br_whbase && !br->br_orph); */
27276 +                       skip = (!wbr || !wbr->wbr_whbase);
27277 +                       if (skip && wbr) {
27278 +                               if (do_plink)
27279 +                                       skip = !!wbr->wbr_plink;
27280 +                               else
27281 +                                       skip = !wbr->wbr_plink;
27282 +                       }
27283 +               } else {
27284 +                       /* skip = (br->br_whbase && br->br_ohph); */
27285 +                       skip = (wbr && wbr->wbr_whbase);
27286 +                       if (skip) {
27287 +                               if (do_plink)
27288 +                                       skip = !!wbr->wbr_plink;
27289 +                               else
27290 +                                       skip = !wbr->wbr_plink;
27291 +                       }
27292 +               }
27293 +               if (wbr)
27294 +                       wbr_wh_read_unlock(wbr);
27295 +
27296 +               if (can_no_dreval) {
27297 +                       dentry = br->br_path.dentry;
27298 +                       spin_lock(&dentry->d_lock);
27299 +                       if (dentry->d_flags &
27300 +                           (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE))
27301 +                               can_no_dreval = 0;
27302 +                       spin_unlock(&dentry->d_lock);
27303 +               }
27304 +
27305 +               if (au_br_fhsm(br->br_perm)) {
27306 +                       fhsm++;
27307 +                       AuDebugOn(!br->br_fhsm);
27308 +               }
27309 +
27310 +               if (skip)
27311 +                       continue;
27312 +
27313 +               hdir = au_hi(dir, bindex);
27314 +               au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
27315 +               if (wbr)
27316 +                       wbr_wh_write_lock(wbr);
27317 +               err = au_wh_init(br, sb);
27318 +               if (wbr)
27319 +                       wbr_wh_write_unlock(wbr);
27320 +               au_hn_inode_unlock(hdir);
27321 +
27322 +               if (!err && do_free) {
27323 +                       au_kfree_rcu(wbr);
27324 +                       br->br_wbr = NULL;
27325 +               }
27326 +       }
27327 +
27328 +       if (can_no_dreval)
27329 +               au_fset_si(sbinfo, NO_DREVAL);
27330 +       else
27331 +               au_fclr_si(sbinfo, NO_DREVAL);
27332 +
27333 +       if (fhsm >= 2) {
27334 +               au_fset_si(sbinfo, FHSM);
27335 +               for (bindex = bbot; bindex >= 0; bindex--) {
27336 +                       br = au_sbr(sb, bindex);
27337 +                       if (au_br_fhsm(br->br_perm)) {
27338 +                               au_fhsm_set_bottom(sb, bindex);
27339 +                               break;
27340 +                       }
27341 +               }
27342 +       } else {
27343 +               au_fclr_si(sbinfo, FHSM);
27344 +               au_fhsm_set_bottom(sb, -1);
27345 +       }
27346 +
27347 +       return err;
27348 +}
27349 +
27350 +int au_opts_mount(struct super_block *sb, struct au_opts *opts)
27351 +{
27352 +       int err;
27353 +       unsigned int tmp;
27354 +       aufs_bindex_t bindex, bbot;
27355 +       struct au_opt *opt;
27356 +       struct au_opt_xino *opt_xino, xino;
27357 +       struct au_sbinfo *sbinfo;
27358 +       struct au_branch *br;
27359 +       struct inode *dir;
27360 +
27361 +       SiMustWriteLock(sb);
27362 +
27363 +       err = 0;
27364 +       opt_xino = NULL;
27365 +       opt = opts->opt;
27366 +       while (err >= 0 && opt->type != Opt_tail)
27367 +               err = au_opt_simple(sb, opt++, opts);
27368 +       if (err > 0)
27369 +               err = 0;
27370 +       else if (unlikely(err < 0))
27371 +               goto out;
27372 +
27373 +       /* disable xino and udba temporary */
27374 +       sbinfo = au_sbi(sb);
27375 +       tmp = sbinfo->si_mntflags;
27376 +       au_opt_clr(sbinfo->si_mntflags, XINO);
27377 +       au_opt_set_udba(sbinfo->si_mntflags, UDBA_REVAL);
27378 +
27379 +       opt = opts->opt;
27380 +       while (err >= 0 && opt->type != Opt_tail)
27381 +               err = au_opt_br(sb, opt++, opts);
27382 +       if (err > 0)
27383 +               err = 0;
27384 +       else if (unlikely(err < 0))
27385 +               goto out;
27386 +
27387 +       bbot = au_sbbot(sb);
27388 +       if (unlikely(bbot < 0)) {
27389 +               err = -EINVAL;
27390 +               pr_err("no branches\n");
27391 +               goto out;
27392 +       }
27393 +
27394 +       if (au_opt_test(tmp, XINO))
27395 +               au_opt_set(sbinfo->si_mntflags, XINO);
27396 +       opt = opts->opt;
27397 +       while (!err && opt->type != Opt_tail)
27398 +               err = au_opt_xino(sb, opt++, &opt_xino, opts);
27399 +       if (unlikely(err))
27400 +               goto out;
27401 +
27402 +       err = au_opts_verify(sb, sb->s_flags, tmp);
27403 +       if (unlikely(err))
27404 +               goto out;
27405 +
27406 +       /* restore xino */
27407 +       if (au_opt_test(tmp, XINO) && !opt_xino) {
27408 +               xino.file = au_xino_def(sb);
27409 +               err = PTR_ERR(xino.file);
27410 +               if (IS_ERR(xino.file))
27411 +                       goto out;
27412 +
27413 +               err = au_xino_set(sb, &xino, /*remount*/0);
27414 +               fput(xino.file);
27415 +               if (unlikely(err))
27416 +                       goto out;
27417 +       }
27418 +
27419 +       /* restore udba */
27420 +       tmp &= AuOptMask_UDBA;
27421 +       sbinfo->si_mntflags &= ~AuOptMask_UDBA;
27422 +       sbinfo->si_mntflags |= tmp;
27423 +       bbot = au_sbbot(sb);
27424 +       for (bindex = 0; bindex <= bbot; bindex++) {
27425 +               br = au_sbr(sb, bindex);
27426 +               err = au_hnotify_reset_br(tmp, br, br->br_perm);
27427 +               if (unlikely(err))
27428 +                       AuIOErr("hnotify failed on br %d, %d, ignored\n",
27429 +                               bindex, err);
27430 +               /* go on even if err */
27431 +       }
27432 +       if (au_opt_test(tmp, UDBA_HNOTIFY)) {
27433 +               dir = d_inode(sb->s_root);
27434 +               au_hn_reset(dir, au_hi_flags(dir, /*isdir*/1) & ~AuHi_XINO);
27435 +       }
27436 +
27437 +out:
27438 +       return err;
27439 +}
27440 +
27441 +int au_opts_remount(struct super_block *sb, struct au_opts *opts)
27442 +{
27443 +       int err, rerr;
27444 +       unsigned char no_dreval;
27445 +       struct inode *dir;
27446 +       struct au_opt_xino *opt_xino;
27447 +       struct au_opt *opt;
27448 +       struct au_sbinfo *sbinfo;
27449 +
27450 +       SiMustWriteLock(sb);
27451 +
27452 +       err = au_dr_opt_flush(sb);
27453 +       if (unlikely(err))
27454 +               goto out;
27455 +       au_fset_opts(opts->flags, DR_FLUSHED);
27456 +
27457 +       dir = d_inode(sb->s_root);
27458 +       sbinfo = au_sbi(sb);
27459 +       opt_xino = NULL;
27460 +       opt = opts->opt;
27461 +       while (err >= 0 && opt->type != Opt_tail) {
27462 +               err = au_opt_simple(sb, opt, opts);
27463 +               if (!err)
27464 +                       err = au_opt_br(sb, opt, opts);
27465 +               if (!err)
27466 +                       err = au_opt_xino(sb, opt, &opt_xino, opts);
27467 +               opt++;
27468 +       }
27469 +       if (err > 0)
27470 +               err = 0;
27471 +       AuTraceErr(err);
27472 +       /* go on even err */
27473 +
27474 +       no_dreval = !!au_ftest_si(sbinfo, NO_DREVAL);
27475 +       rerr = au_opts_verify(sb, opts->sb_flags, /*pending*/0);
27476 +       if (unlikely(rerr && !err))
27477 +               err = rerr;
27478 +
27479 +       if (no_dreval != !!au_ftest_si(sbinfo, NO_DREVAL))
27480 +               au_fset_opts(opts->flags, REFRESH_IDOP);
27481 +
27482 +       if (au_ftest_opts(opts->flags, TRUNC_XIB)) {
27483 +               rerr = au_xib_trunc(sb);
27484 +               if (unlikely(rerr && !err))
27485 +                       err = rerr;
27486 +       }
27487 +
27488 +       /* will be handled by the caller */
27489 +       if (!au_ftest_opts(opts->flags, REFRESH)
27490 +           && (opts->given_udba
27491 +               || au_opt_test(sbinfo->si_mntflags, XINO)
27492 +               || au_ftest_opts(opts->flags, REFRESH_IDOP)
27493 +                   ))
27494 +               au_fset_opts(opts->flags, REFRESH);
27495 +
27496 +       AuDbg("status 0x%x\n", opts->flags);
27497 +
27498 +out:
27499 +       return err;
27500 +}
27501 +
27502 +/* ---------------------------------------------------------------------- */
27503 +
27504 +unsigned int au_opt_udba(struct super_block *sb)
27505 +{
27506 +       return au_mntflags(sb) & AuOptMask_UDBA;
27507 +}
27508 diff -urN /usr/share/empty/fs/aufs/opts.h linux/fs/aufs/opts.h
27509 --- /usr/share/empty/fs/aufs/opts.h     1970-01-01 01:00:00.000000000 +0100
27510 +++ linux/fs/aufs/opts.h        2021-11-01 23:48:34.209692595 +0100
27511 @@ -0,0 +1,225 @@
27512 +/* SPDX-License-Identifier: GPL-2.0 */
27513 +/*
27514 + * Copyright (C) 2005-2021 Junjiro R. Okajima
27515 + *
27516 + * This program, aufs is free software; you can redistribute it and/or modify
27517 + * it under the terms of the GNU General Public License as published by
27518 + * the Free Software Foundation; either version 2 of the License, or
27519 + * (at your option) any later version.
27520 + *
27521 + * This program is distributed in the hope that it will be useful,
27522 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
27523 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27524 + * GNU General Public License for more details.
27525 + *
27526 + * You should have received a copy of the GNU General Public License
27527 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
27528 + */
27529 +
27530 +/*
27531 + * mount options/flags
27532 + */
27533 +
27534 +#ifndef __AUFS_OPTS_H__
27535 +#define __AUFS_OPTS_H__
27536 +
27537 +#ifdef __KERNEL__
27538 +
27539 +#include <linux/path.h>
27540 +
27541 +struct file;
27542 +
27543 +/* ---------------------------------------------------------------------- */
27544 +
27545 +/* mount flags */
27546 +#define AuOpt_XINO             1               /* external inode number bitmap
27547 +                                                  and translation table */
27548 +#define AuOpt_TRUNC_XINO       (1 << 1)        /* truncate xino files */
27549 +#define AuOpt_UDBA_NONE                (1 << 2)        /* users direct branch access */
27550 +#define AuOpt_UDBA_REVAL       (1 << 3)
27551 +#define AuOpt_UDBA_HNOTIFY     (1 << 4)
27552 +#define AuOpt_SHWH             (1 << 5)        /* show whiteout */
27553 +#define AuOpt_PLINK            (1 << 6)        /* pseudo-link */
27554 +#define AuOpt_DIRPERM1         (1 << 7)        /* ignore the lower dir's perm
27555 +                                                  bits */
27556 +#define AuOpt_ALWAYS_DIROPQ    (1 << 9)        /* policy to creating diropq */
27557 +#define AuOpt_SUM              (1 << 10)       /* summation for statfs(2) */
27558 +#define AuOpt_SUM_W            (1 << 11)       /* unimplemented */
27559 +#define AuOpt_WARN_PERM                (1 << 12)       /* warn when add-branch */
27560 +#define AuOpt_VERBOSE          (1 << 13)       /* print the cause of error */
27561 +#define AuOpt_DIO              (1 << 14)       /* direct io */
27562 +#define AuOpt_DIRREN           (1 << 15)       /* directory rename */
27563 +
27564 +#ifndef CONFIG_AUFS_HNOTIFY
27565 +#undef AuOpt_UDBA_HNOTIFY
27566 +#define AuOpt_UDBA_HNOTIFY     0
27567 +#endif
27568 +#ifndef CONFIG_AUFS_DIRREN
27569 +#undef AuOpt_DIRREN
27570 +#define AuOpt_DIRREN           0
27571 +#endif
27572 +#ifndef CONFIG_AUFS_SHWH
27573 +#undef AuOpt_SHWH
27574 +#define AuOpt_SHWH             0
27575 +#endif
27576 +
27577 +#define AuOpt_Def      (AuOpt_XINO \
27578 +                        | AuOpt_UDBA_REVAL \
27579 +                        | AuOpt_PLINK \
27580 +                        /* | AuOpt_DIRPERM1 */ \
27581 +                        | AuOpt_WARN_PERM)
27582 +#define AuOptMask_UDBA (AuOpt_UDBA_NONE \
27583 +                        | AuOpt_UDBA_REVAL \
27584 +                        | AuOpt_UDBA_HNOTIFY)
27585 +
27586 +#define au_opt_test(flags, name)       (flags & AuOpt_##name)
27587 +#define au_opt_set(flags, name) do { \
27588 +       BUILD_BUG_ON(AuOpt_##name & AuOptMask_UDBA); \
27589 +       ((flags) |= AuOpt_##name); \
27590 +} while (0)
27591 +#define au_opt_set_udba(flags, name) do { \
27592 +       (flags) &= ~AuOptMask_UDBA; \
27593 +       ((flags) |= AuOpt_##name); \
27594 +} while (0)
27595 +#define au_opt_clr(flags, name) do { \
27596 +       ((flags) &= ~AuOpt_##name); \
27597 +} while (0)
27598 +
27599 +static inline unsigned int au_opts_plink(unsigned int mntflags)
27600 +{
27601 +#ifdef CONFIG_PROC_FS
27602 +       return mntflags;
27603 +#else
27604 +       return mntflags & ~AuOpt_PLINK;
27605 +#endif
27606 +}
27607 +
27608 +/* ---------------------------------------------------------------------- */
27609 +
27610 +/* policies to select one among multiple writable branches */
27611 +enum {
27612 +       AuWbrCreate_TDP,        /* top down parent */
27613 +       AuWbrCreate_RR,         /* round robin */
27614 +       AuWbrCreate_MFS,        /* most free space */
27615 +       AuWbrCreate_MFSV,       /* mfs with seconds */
27616 +       AuWbrCreate_MFSRR,      /* mfs then rr */
27617 +       AuWbrCreate_MFSRRV,     /* mfs then rr with seconds */
27618 +       AuWbrCreate_TDMFS,      /* top down regardless parent and mfs */
27619 +       AuWbrCreate_TDMFSV,     /* top down regardless parent and mfs */
27620 +       AuWbrCreate_PMFS,       /* parent and mfs */
27621 +       AuWbrCreate_PMFSV,      /* parent and mfs with seconds */
27622 +       AuWbrCreate_PMFSRR,     /* parent, mfs and round-robin */
27623 +       AuWbrCreate_PMFSRRV,    /* plus seconds */
27624 +
27625 +       AuWbrCreate_Def = AuWbrCreate_TDP
27626 +};
27627 +
27628 +enum {
27629 +       AuWbrCopyup_TDP,        /* top down parent */
27630 +       AuWbrCopyup_BUP,        /* bottom up parent */
27631 +       AuWbrCopyup_BU,         /* bottom up */
27632 +
27633 +       AuWbrCopyup_Def = AuWbrCopyup_TDP
27634 +};
27635 +
27636 +/* ---------------------------------------------------------------------- */
27637 +
27638 +struct au_opt_add {
27639 +       aufs_bindex_t   bindex;
27640 +       char            *pathname;
27641 +       int             perm;
27642 +       struct path     path;
27643 +};
27644 +
27645 +struct au_opt_del {
27646 +       char            *pathname;
27647 +       struct path     h_path;
27648 +};
27649 +
27650 +struct au_opt_mod {
27651 +       char            *path;
27652 +       int             perm;
27653 +       struct dentry   *h_root;
27654 +};
27655 +
27656 +struct au_opt_xino {
27657 +       char            *path;
27658 +       struct file     *file;
27659 +};
27660 +
27661 +struct au_opt_xino_itrunc {
27662 +       aufs_bindex_t   bindex;
27663 +};
27664 +
27665 +struct au_opt_wbr_create {
27666 +       int                     wbr_create;
27667 +       int                     mfs_second;
27668 +       unsigned long long      mfsrr_watermark;
27669 +};
27670 +
27671 +struct au_opt {
27672 +       int type;
27673 +       union {
27674 +               struct au_opt_xino      xino;
27675 +               struct au_opt_xino_itrunc xino_itrunc;
27676 +               struct au_opt_add       add;
27677 +               struct au_opt_del       del;
27678 +               struct au_opt_mod       mod;
27679 +               int                     dirwh;
27680 +               int                     rdcache;
27681 +               unsigned int            rdblk;
27682 +               unsigned int            rdhash;
27683 +               int                     udba;
27684 +               struct au_opt_wbr_create wbr_create;
27685 +               int                     wbr_copyup;
27686 +               unsigned int            fhsm_second;
27687 +       };
27688 +};
27689 +
27690 +/* opts flags */
27691 +#define AuOpts_REMOUNT         1
27692 +#define AuOpts_REFRESH         (1 << 1)
27693 +#define AuOpts_TRUNC_XIB       (1 << 2)
27694 +#define AuOpts_REFRESH_DYAOP   (1 << 3)
27695 +#define AuOpts_REFRESH_IDOP    (1 << 4)
27696 +#define AuOpts_DR_FLUSHED      (1 << 5)
27697 +#define au_ftest_opts(flags, name)     ((flags) & AuOpts_##name)
27698 +#define au_fset_opts(flags, name) \
27699 +       do { (flags) |= AuOpts_##name; } while (0)
27700 +#define au_fclr_opts(flags, name) \
27701 +       do { (flags) &= ~AuOpts_##name; } while (0)
27702 +
27703 +#ifndef CONFIG_AUFS_DIRREN
27704 +#undef AuOpts_DR_FLUSHED
27705 +#define AuOpts_DR_FLUSHED      0
27706 +#endif
27707 +
27708 +struct au_opts {
27709 +       struct au_opt   *opt;
27710 +       int             max_opt;
27711 +
27712 +       unsigned int    given_udba;
27713 +       unsigned int    flags;
27714 +       unsigned long   sb_flags;
27715 +};
27716 +
27717 +/* ---------------------------------------------------------------------- */
27718 +
27719 +/* opts.c */
27720 +void au_optstr_br_perm(au_br_perm_str_t *str, int perm);
27721 +const char *au_optstr_udba(int udba);
27722 +const char *au_optstr_wbr_copyup(int wbr_copyup);
27723 +const char *au_optstr_wbr_create(int wbr_create);
27724 +
27725 +void au_opts_free(struct au_opts *opts);
27726 +struct super_block;
27727 +int au_opts_parse(struct super_block *sb, char *str, struct au_opts *opts);
27728 +int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
27729 +                  unsigned int pending);
27730 +int au_opts_mount(struct super_block *sb, struct au_opts *opts);
27731 +int au_opts_remount(struct super_block *sb, struct au_opts *opts);
27732 +
27733 +unsigned int au_opt_udba(struct super_block *sb);
27734 +
27735 +#endif /* __KERNEL__ */
27736 +#endif /* __AUFS_OPTS_H__ */
27737 diff -urN /usr/share/empty/fs/aufs/plink.c linux/fs/aufs/plink.c
27738 --- /usr/share/empty/fs/aufs/plink.c    1970-01-01 01:00:00.000000000 +0100
27739 +++ linux/fs/aufs/plink.c       2021-11-01 23:48:34.209692595 +0100
27740 @@ -0,0 +1,516 @@
27741 +// SPDX-License-Identifier: GPL-2.0
27742 +/*
27743 + * Copyright (C) 2005-2021 Junjiro R. Okajima
27744 + *
27745 + * This program, aufs is free software; you can redistribute it and/or modify
27746 + * it under the terms of the GNU General Public License as published by
27747 + * the Free Software Foundation; either version 2 of the License, or
27748 + * (at your option) any later version.
27749 + *
27750 + * This program is distributed in the hope that it will be useful,
27751 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
27752 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27753 + * GNU General Public License for more details.
27754 + *
27755 + * You should have received a copy of the GNU General Public License
27756 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
27757 + */
27758 +
27759 +/*
27760 + * pseudo-link
27761 + */
27762 +
27763 +#include "aufs.h"
27764 +
27765 +/*
27766 + * the pseudo-link maintenance mode.
27767 + * during a user process maintains the pseudo-links,
27768 + * prohibit adding a new plink and branch manipulation.
27769 + *
27770 + * Flags
27771 + * NOPLM:
27772 + *     For entry functions which will handle plink, and i_mutex is already held
27773 + *     in VFS.
27774 + *     They cannot wait and should return an error at once.
27775 + *     Callers has to check the error.
27776 + * NOPLMW:
27777 + *     For entry functions which will handle plink, but i_mutex is not held
27778 + *     in VFS.
27779 + *     They can wait the plink maintenance mode to finish.
27780 + *
27781 + * They behave like F_SETLK and F_SETLKW.
27782 + * If the caller never handle plink, then both flags are unnecessary.
27783 + */
27784 +
27785 +int au_plink_maint(struct super_block *sb, int flags)
27786 +{
27787 +       int err;
27788 +       pid_t pid, ppid;
27789 +       struct task_struct *parent, *prev;
27790 +       struct au_sbinfo *sbi;
27791 +
27792 +       SiMustAnyLock(sb);
27793 +
27794 +       err = 0;
27795 +       if (!au_opt_test(au_mntflags(sb), PLINK))
27796 +               goto out;
27797 +
27798 +       sbi = au_sbi(sb);
27799 +       pid = sbi->si_plink_maint_pid;
27800 +       if (!pid || pid == current->pid)
27801 +               goto out;
27802 +
27803 +       /* todo: it highly depends upon /sbin/mount.aufs */
27804 +       prev = NULL;
27805 +       parent = current;
27806 +       ppid = 0;
27807 +       rcu_read_lock();
27808 +       while (1) {
27809 +               parent = rcu_dereference(parent->real_parent);
27810 +               if (parent == prev)
27811 +                       break;
27812 +               ppid = task_pid_vnr(parent);
27813 +               if (pid == ppid) {
27814 +                       rcu_read_unlock();
27815 +                       goto out;
27816 +               }
27817 +               prev = parent;
27818 +       }
27819 +       rcu_read_unlock();
27820 +
27821 +       if (au_ftest_lock(flags, NOPLMW)) {
27822 +               /* if there is no i_mutex lock in VFS, we don't need to wait */
27823 +               /* AuDebugOn(!lockdep_depth(current)); */
27824 +               while (sbi->si_plink_maint_pid) {
27825 +                       si_read_unlock(sb);
27826 +                       /* gave up wake_up_bit() */
27827 +                       wait_event(sbi->si_plink_wq, !sbi->si_plink_maint_pid);
27828 +
27829 +                       if (au_ftest_lock(flags, FLUSH))
27830 +                               au_nwt_flush(&sbi->si_nowait);
27831 +                       si_noflush_read_lock(sb);
27832 +               }
27833 +       } else if (au_ftest_lock(flags, NOPLM)) {
27834 +               AuDbg("ppid %d, pid %d\n", ppid, pid);
27835 +               err = -EAGAIN;
27836 +       }
27837 +
27838 +out:
27839 +       return err;
27840 +}
27841 +
27842 +void au_plink_maint_leave(struct au_sbinfo *sbinfo)
27843 +{
27844 +       spin_lock(&sbinfo->si_plink_maint_lock);
27845 +       sbinfo->si_plink_maint_pid = 0;
27846 +       spin_unlock(&sbinfo->si_plink_maint_lock);
27847 +       wake_up_all(&sbinfo->si_plink_wq);
27848 +}
27849 +
27850 +int au_plink_maint_enter(struct super_block *sb)
27851 +{
27852 +       int err;
27853 +       struct au_sbinfo *sbinfo;
27854 +
27855 +       err = 0;
27856 +       sbinfo = au_sbi(sb);
27857 +       /* make sure i am the only one in this fs */
27858 +       si_write_lock(sb, AuLock_FLUSH);
27859 +       if (au_opt_test(au_mntflags(sb), PLINK)) {
27860 +               spin_lock(&sbinfo->si_plink_maint_lock);
27861 +               if (!sbinfo->si_plink_maint_pid)
27862 +                       sbinfo->si_plink_maint_pid = current->pid;
27863 +               else
27864 +                       err = -EBUSY;
27865 +               spin_unlock(&sbinfo->si_plink_maint_lock);
27866 +       }
27867 +       si_write_unlock(sb);
27868 +
27869 +       return err;
27870 +}
27871 +
27872 +/* ---------------------------------------------------------------------- */
27873 +
27874 +#ifdef CONFIG_AUFS_DEBUG
27875 +void au_plink_list(struct super_block *sb)
27876 +{
27877 +       int i;
27878 +       struct au_sbinfo *sbinfo;
27879 +       struct hlist_bl_head *hbl;
27880 +       struct hlist_bl_node *pos;
27881 +       struct au_icntnr *icntnr;
27882 +
27883 +       SiMustAnyLock(sb);
27884 +
27885 +       sbinfo = au_sbi(sb);
27886 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
27887 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
27888 +
27889 +       for (i = 0; i < AuPlink_NHASH; i++) {
27890 +               hbl = sbinfo->si_plink + i;
27891 +               hlist_bl_lock(hbl);
27892 +               hlist_bl_for_each_entry(icntnr, pos, hbl, plink)
27893 +                       AuDbg("%lu\n", icntnr->vfs_inode.i_ino);
27894 +               hlist_bl_unlock(hbl);
27895 +       }
27896 +}
27897 +#endif
27898 +
27899 +/* is the inode pseudo-linked? */
27900 +int au_plink_test(struct inode *inode)
27901 +{
27902 +       int found, i;
27903 +       struct au_sbinfo *sbinfo;
27904 +       struct hlist_bl_head *hbl;
27905 +       struct hlist_bl_node *pos;
27906 +       struct au_icntnr *icntnr;
27907 +
27908 +       sbinfo = au_sbi(inode->i_sb);
27909 +       AuRwMustAnyLock(&sbinfo->si_rwsem);
27910 +       AuDebugOn(!au_opt_test(au_mntflags(inode->i_sb), PLINK));
27911 +       AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
27912 +
27913 +       found = 0;
27914 +       i = au_plink_hash(inode->i_ino);
27915 +       hbl =  sbinfo->si_plink + i;
27916 +       hlist_bl_lock(hbl);
27917 +       hlist_bl_for_each_entry(icntnr, pos, hbl, plink)
27918 +               if (&icntnr->vfs_inode == inode) {
27919 +                       found = 1;
27920 +                       break;
27921 +               }
27922 +       hlist_bl_unlock(hbl);
27923 +       return found;
27924 +}
27925 +
27926 +/* ---------------------------------------------------------------------- */
27927 +
27928 +/*
27929 + * generate a name for plink.
27930 + * the file will be stored under AUFS_WH_PLINKDIR.
27931 + */
27932 +/* 20 is max digits length of ulong 64 */
27933 +#define PLINK_NAME_LEN ((20 + 1) * 2)
27934 +
27935 +static int plink_name(char *name, int len, struct inode *inode,
27936 +                     aufs_bindex_t bindex)
27937 +{
27938 +       int rlen;
27939 +       struct inode *h_inode;
27940 +
27941 +       h_inode = au_h_iptr(inode, bindex);
27942 +       rlen = snprintf(name, len, "%lu.%lu", inode->i_ino, h_inode->i_ino);
27943 +       return rlen;
27944 +}
27945 +
27946 +struct au_do_plink_lkup_args {
27947 +       struct dentry **errp;
27948 +       struct qstr *tgtname;
27949 +       struct path *h_ppath;
27950 +};
27951 +
27952 +static struct dentry *au_do_plink_lkup(struct qstr *tgtname,
27953 +                                      struct path *h_ppath)
27954 +{
27955 +       struct dentry *h_dentry;
27956 +       struct inode *h_inode;
27957 +
27958 +       h_inode = d_inode(h_ppath->dentry);
27959 +       inode_lock_shared_nested(h_inode, AuLsc_I_CHILD2);
27960 +       h_dentry = vfsub_lkup_one(tgtname, h_ppath);
27961 +       inode_unlock_shared(h_inode);
27962 +
27963 +       return h_dentry;
27964 +}
27965 +
27966 +static void au_call_do_plink_lkup(void *args)
27967 +{
27968 +       struct au_do_plink_lkup_args *a = args;
27969 +       *a->errp = au_do_plink_lkup(a->tgtname, a->h_ppath);
27970 +}
27971 +
27972 +/* lookup the plink-ed @inode under the branch at @bindex */
27973 +struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex)
27974 +{
27975 +       struct dentry *h_dentry;
27976 +       struct au_branch *br;
27977 +       struct path h_ppath;
27978 +       int wkq_err;
27979 +       char a[PLINK_NAME_LEN];
27980 +       struct qstr tgtname = QSTR_INIT(a, 0);
27981 +
27982 +       AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
27983 +
27984 +       br = au_sbr(inode->i_sb, bindex);
27985 +       h_ppath.dentry = br->br_wbr->wbr_plink;
27986 +       h_ppath.mnt = au_br_mnt(br);
27987 +       tgtname.len = plink_name(a, sizeof(a), inode, bindex);
27988 +
27989 +       if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
27990 +               struct au_do_plink_lkup_args args = {
27991 +                       .errp           = &h_dentry,
27992 +                       .tgtname        = &tgtname,
27993 +                       .h_ppath        = &h_ppath
27994 +               };
27995 +
27996 +               wkq_err = au_wkq_wait(au_call_do_plink_lkup, &args);
27997 +               if (unlikely(wkq_err))
27998 +                       h_dentry = ERR_PTR(wkq_err);
27999 +       } else
28000 +               h_dentry = au_do_plink_lkup(&tgtname, &h_ppath);
28001 +
28002 +       return h_dentry;
28003 +}
28004 +
28005 +/* create a pseudo-link */
28006 +static int do_whplink(struct qstr *tgt, struct path *h_ppath,
28007 +                     struct dentry *h_dentry)
28008 +{
28009 +       int err;
28010 +       struct path h_path;
28011 +       struct inode *h_dir, *delegated;
28012 +
28013 +       h_dir = d_inode(h_ppath->dentry);
28014 +       inode_lock_nested(h_dir, AuLsc_I_CHILD2);
28015 +       h_path.mnt = h_ppath->mnt;
28016 +again:
28017 +       h_path.dentry = vfsub_lkup_one(tgt, h_ppath);
28018 +       err = PTR_ERR(h_path.dentry);
28019 +       if (IS_ERR(h_path.dentry))
28020 +               goto out;
28021 +
28022 +       err = 0;
28023 +       /* wh.plink dir is not monitored */
28024 +       /* todo: is it really safe? */
28025 +       if (d_is_positive(h_path.dentry)
28026 +           && d_inode(h_path.dentry) != d_inode(h_dentry)) {
28027 +               delegated = NULL;
28028 +               err = vfsub_unlink(h_dir, &h_path, &delegated, /*force*/0);
28029 +               if (unlikely(err == -EWOULDBLOCK)) {
28030 +                       pr_warn("cannot retry for NFSv4 delegation"
28031 +                               " for an internal unlink\n");
28032 +                       iput(delegated);
28033 +               }
28034 +               dput(h_path.dentry);
28035 +               h_path.dentry = NULL;
28036 +               if (!err)
28037 +                       goto again;
28038 +       }
28039 +       if (!err && d_is_negative(h_path.dentry)) {
28040 +               delegated = NULL;
28041 +               err = vfsub_link(h_dentry, h_dir, &h_path, &delegated);
28042 +               if (unlikely(err == -EWOULDBLOCK)) {
28043 +                       pr_warn("cannot retry for NFSv4 delegation"
28044 +                               " for an internal link\n");
28045 +                       iput(delegated);
28046 +               }
28047 +       }
28048 +       dput(h_path.dentry);
28049 +
28050 +out:
28051 +       inode_unlock(h_dir);
28052 +       return err;
28053 +}
28054 +
28055 +struct do_whplink_args {
28056 +       int *errp;
28057 +       struct qstr *tgt;
28058 +       struct path *h_ppath;
28059 +       struct dentry *h_dentry;
28060 +};
28061 +
28062 +static void call_do_whplink(void *args)
28063 +{
28064 +       struct do_whplink_args *a = args;
28065 +       *a->errp = do_whplink(a->tgt, a->h_ppath, a->h_dentry);
28066 +}
28067 +
28068 +static int whplink(struct dentry *h_dentry, struct inode *inode,
28069 +                  aufs_bindex_t bindex)
28070 +{
28071 +       int err, wkq_err;
28072 +       struct au_branch *br;
28073 +       struct au_wbr *wbr;
28074 +       struct path h_ppath;
28075 +       char a[PLINK_NAME_LEN];
28076 +       struct qstr tgtname = QSTR_INIT(a, 0);
28077 +
28078 +       br = au_sbr(inode->i_sb, bindex);
28079 +       wbr = br->br_wbr;
28080 +       h_ppath.dentry = wbr->wbr_plink;
28081 +       h_ppath.mnt = au_br_mnt(br);
28082 +       tgtname.len = plink_name(a, sizeof(a), inode, bindex);
28083 +
28084 +       /* always superio. */
28085 +       if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
28086 +               struct do_whplink_args args = {
28087 +                       .errp           = &err,
28088 +                       .tgt            = &tgtname,
28089 +                       .h_ppath        = &h_ppath,
28090 +                       .h_dentry       = h_dentry
28091 +               };
28092 +               wkq_err = au_wkq_wait(call_do_whplink, &args);
28093 +               if (unlikely(wkq_err))
28094 +                       err = wkq_err;
28095 +       } else
28096 +               err = do_whplink(&tgtname, &h_ppath, h_dentry);
28097 +
28098 +       return err;
28099 +}
28100 +
28101 +/*
28102 + * create a new pseudo-link for @h_dentry on @bindex.
28103 + * the linked inode is held in aufs @inode.
28104 + */
28105 +void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
28106 +                    struct dentry *h_dentry)
28107 +{
28108 +       struct super_block *sb;
28109 +       struct au_sbinfo *sbinfo;
28110 +       struct hlist_bl_head *hbl;
28111 +       struct hlist_bl_node *pos;
28112 +       struct au_icntnr *icntnr;
28113 +       int found, err, cnt, i;
28114 +
28115 +       sb = inode->i_sb;
28116 +       sbinfo = au_sbi(sb);
28117 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
28118 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
28119 +
28120 +       found = au_plink_test(inode);
28121 +       if (found)
28122 +               return;
28123 +
28124 +       i = au_plink_hash(inode->i_ino);
28125 +       hbl = sbinfo->si_plink + i;
28126 +       au_igrab(inode);
28127 +
28128 +       hlist_bl_lock(hbl);
28129 +       hlist_bl_for_each_entry(icntnr, pos, hbl, plink) {
28130 +               if (&icntnr->vfs_inode == inode) {
28131 +                       found = 1;
28132 +                       break;
28133 +               }
28134 +       }
28135 +       if (!found) {
28136 +               icntnr = container_of(inode, struct au_icntnr, vfs_inode);
28137 +               hlist_bl_add_head(&icntnr->plink, hbl);
28138 +       }
28139 +       hlist_bl_unlock(hbl);
28140 +       if (!found) {
28141 +               cnt = au_hbl_count(hbl);
28142 +#define msg "unexpectedly unbalanced or too many pseudo-links"
28143 +               if (cnt > AUFS_PLINK_WARN)
28144 +                       AuWarn1(msg ", %d\n", cnt);
28145 +#undef msg
28146 +               err = whplink(h_dentry, inode, bindex);
28147 +               if (unlikely(err)) {
28148 +                       pr_warn("err %d, damaged pseudo link.\n", err);
28149 +                       au_hbl_del(&icntnr->plink, hbl);
28150 +                       iput(&icntnr->vfs_inode);
28151 +               }
28152 +       } else
28153 +               iput(&icntnr->vfs_inode);
28154 +}
28155 +
28156 +/* free all plinks */
28157 +void au_plink_put(struct super_block *sb, int verbose)
28158 +{
28159 +       int i, warned;
28160 +       struct au_sbinfo *sbinfo;
28161 +       struct hlist_bl_head *hbl;
28162 +       struct hlist_bl_node *pos, *tmp;
28163 +       struct au_icntnr *icntnr;
28164 +
28165 +       SiMustWriteLock(sb);
28166 +
28167 +       sbinfo = au_sbi(sb);
28168 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
28169 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
28170 +
28171 +       /* no spin_lock since sbinfo is write-locked */
28172 +       warned = 0;
28173 +       for (i = 0; i < AuPlink_NHASH; i++) {
28174 +               hbl = sbinfo->si_plink + i;
28175 +               if (!warned && verbose && !hlist_bl_empty(hbl)) {
28176 +                       pr_warn("pseudo-link is not flushed");
28177 +                       warned = 1;
28178 +               }
28179 +               hlist_bl_for_each_entry_safe(icntnr, pos, tmp, hbl, plink)
28180 +                       iput(&icntnr->vfs_inode);
28181 +               INIT_HLIST_BL_HEAD(hbl);
28182 +       }
28183 +}
28184 +
28185 +void au_plink_clean(struct super_block *sb, int verbose)
28186 +{
28187 +       struct dentry *root;
28188 +
28189 +       root = sb->s_root;
28190 +       aufs_write_lock(root);
28191 +       if (au_opt_test(au_mntflags(sb), PLINK))
28192 +               au_plink_put(sb, verbose);
28193 +       aufs_write_unlock(root);
28194 +}
28195 +
28196 +static int au_plink_do_half_refresh(struct inode *inode, aufs_bindex_t br_id)
28197 +{
28198 +       int do_put;
28199 +       aufs_bindex_t btop, bbot, bindex;
28200 +
28201 +       do_put = 0;
28202 +       btop = au_ibtop(inode);
28203 +       bbot = au_ibbot(inode);
28204 +       if (btop >= 0) {
28205 +               for (bindex = btop; bindex <= bbot; bindex++) {
28206 +                       if (!au_h_iptr(inode, bindex)
28207 +                           || au_ii_br_id(inode, bindex) != br_id)
28208 +                               continue;
28209 +                       au_set_h_iptr(inode, bindex, NULL, 0);
28210 +                       do_put = 1;
28211 +                       break;
28212 +               }
28213 +               if (do_put)
28214 +                       for (bindex = btop; bindex <= bbot; bindex++)
28215 +                               if (au_h_iptr(inode, bindex)) {
28216 +                                       do_put = 0;
28217 +                                       break;
28218 +                               }
28219 +       } else
28220 +               do_put = 1;
28221 +
28222 +       return do_put;
28223 +}
28224 +
28225 +/* free the plinks on a branch specified by @br_id */
28226 +void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id)
28227 +{
28228 +       struct au_sbinfo *sbinfo;
28229 +       struct hlist_bl_head *hbl;
28230 +       struct hlist_bl_node *pos, *tmp;
28231 +       struct au_icntnr *icntnr;
28232 +       struct inode *inode;
28233 +       int i, do_put;
28234 +
28235 +       SiMustWriteLock(sb);
28236 +
28237 +       sbinfo = au_sbi(sb);
28238 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
28239 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
28240 +
28241 +       /* no bit_lock since sbinfo is write-locked */
28242 +       for (i = 0; i < AuPlink_NHASH; i++) {
28243 +               hbl = sbinfo->si_plink + i;
28244 +               hlist_bl_for_each_entry_safe(icntnr, pos, tmp, hbl, plink) {
28245 +                       inode = au_igrab(&icntnr->vfs_inode);
28246 +                       ii_write_lock_child(inode);
28247 +                       do_put = au_plink_do_half_refresh(inode, br_id);
28248 +                       if (do_put) {
28249 +                               hlist_bl_del(&icntnr->plink);
28250 +                               iput(inode);
28251 +                       }
28252 +                       ii_write_unlock(inode);
28253 +                       iput(inode);
28254 +               }
28255 +       }
28256 +}
28257 diff -urN /usr/share/empty/fs/aufs/poll.c linux/fs/aufs/poll.c
28258 --- /usr/share/empty/fs/aufs/poll.c     1970-01-01 01:00:00.000000000 +0100
28259 +++ linux/fs/aufs/poll.c        2021-11-01 23:48:34.209692595 +0100
28260 @@ -0,0 +1,51 @@
28261 +// SPDX-License-Identifier: GPL-2.0
28262 +/*
28263 + * Copyright (C) 2005-2021 Junjiro R. Okajima
28264 + *
28265 + * This program, aufs is free software; you can redistribute it and/or modify
28266 + * it under the terms of the GNU General Public License as published by
28267 + * the Free Software Foundation; either version 2 of the License, or
28268 + * (at your option) any later version.
28269 + *
28270 + * This program is distributed in the hope that it will be useful,
28271 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28272 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28273 + * GNU General Public License for more details.
28274 + *
28275 + * You should have received a copy of the GNU General Public License
28276 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28277 + */
28278 +
28279 +/*
28280 + * poll operation
28281 + * There is only one filesystem which implements ->poll operation, currently.
28282 + */
28283 +
28284 +#include "aufs.h"
28285 +
28286 +__poll_t aufs_poll(struct file *file, struct poll_table_struct *pt)
28287 +{
28288 +       __poll_t mask;
28289 +       struct file *h_file;
28290 +       struct super_block *sb;
28291 +
28292 +       /* We should pretend an error happened. */
28293 +       mask = EPOLLERR /* | EPOLLIN | EPOLLOUT */;
28294 +       sb = file->f_path.dentry->d_sb;
28295 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
28296 +
28297 +       h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
28298 +       if (IS_ERR(h_file)) {
28299 +               AuDbg("h_file %ld\n", PTR_ERR(h_file));
28300 +               goto out;
28301 +       }
28302 +
28303 +       mask = vfs_poll(h_file, pt);
28304 +       fput(h_file); /* instead of au_read_post() */
28305 +
28306 +out:
28307 +       si_read_unlock(sb);
28308 +       if (mask & EPOLLERR)
28309 +               AuDbg("mask 0x%x\n", mask);
28310 +       return mask;
28311 +}
28312 diff -urN /usr/share/empty/fs/aufs/posix_acl.c linux/fs/aufs/posix_acl.c
28313 --- /usr/share/empty/fs/aufs/posix_acl.c        1970-01-01 01:00:00.000000000 +0100
28314 +++ linux/fs/aufs/posix_acl.c   2021-11-01 23:48:34.209692595 +0100
28315 @@ -0,0 +1,111 @@
28316 +// SPDX-License-Identifier: GPL-2.0
28317 +/*
28318 + * Copyright (C) 2014-2021 Junjiro R. Okajima
28319 + *
28320 + * This program, aufs is free software; you can redistribute it and/or modify
28321 + * it under the terms of the GNU General Public License as published by
28322 + * the Free Software Foundation; either version 2 of the License, or
28323 + * (at your option) any later version.
28324 + *
28325 + * This program is distributed in the hope that it will be useful,
28326 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28327 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28328 + * GNU General Public License for more details.
28329 + *
28330 + * You should have received a copy of the GNU General Public License
28331 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28332 + */
28333 +
28334 +/*
28335 + * posix acl operations
28336 + */
28337 +
28338 +#include <linux/fs.h>
28339 +#include "aufs.h"
28340 +
28341 +struct posix_acl *aufs_get_acl(struct inode *inode, int type, bool rcu)
28342 +{
28343 +       struct posix_acl *acl;
28344 +       int err;
28345 +       aufs_bindex_t bindex;
28346 +       struct inode *h_inode;
28347 +       struct super_block *sb;
28348 +
28349 +       acl = ERR_PTR(-ECHILD);
28350 +       if (rcu)
28351 +               goto out;
28352 +
28353 +       acl = NULL;
28354 +       sb = inode->i_sb;
28355 +       si_read_lock(sb, AuLock_FLUSH);
28356 +       ii_read_lock_child(inode);
28357 +       if (!(sb->s_flags & SB_POSIXACL))
28358 +               goto unlock;
28359 +
28360 +       bindex = au_ibtop(inode);
28361 +       h_inode = au_h_iptr(inode, bindex);
28362 +       if (unlikely(!h_inode
28363 +                    || ((h_inode->i_mode & S_IFMT)
28364 +                        != (inode->i_mode & S_IFMT)))) {
28365 +               err = au_busy_or_stale();
28366 +               acl = ERR_PTR(err);
28367 +               goto unlock;
28368 +       }
28369 +
28370 +       /* always topmost only */
28371 +       acl = get_acl(h_inode, type);
28372 +       if (IS_ERR(acl))
28373 +               forget_cached_acl(inode, type);
28374 +       else
28375 +               set_cached_acl(inode, type, acl);
28376 +
28377 +unlock:
28378 +       ii_read_unlock(inode);
28379 +       si_read_unlock(sb);
28380 +
28381 +out:
28382 +       AuTraceErrPtr(acl);
28383 +       return acl;
28384 +}
28385 +
28386 +int aufs_set_acl(struct user_namespace *userns, struct inode *inode,
28387 +                struct posix_acl *acl, int type)
28388 +{
28389 +       int err;
28390 +       ssize_t ssz;
28391 +       struct dentry *dentry;
28392 +       struct au_sxattr arg = {
28393 +               .type = AU_ACL_SET,
28394 +               .u.acl_set = {
28395 +                       .acl    = acl,
28396 +                       .type   = type
28397 +               },
28398 +       };
28399 +
28400 +       IMustLock(inode);
28401 +
28402 +       if (inode->i_ino == AUFS_ROOT_INO)
28403 +               dentry = dget(inode->i_sb->s_root);
28404 +       else {
28405 +               dentry = d_find_alias(inode);
28406 +               if (!dentry)
28407 +                       dentry = d_find_any_alias(inode);
28408 +               if (!dentry) {
28409 +                       pr_warn("cannot handle this inode, "
28410 +                               "please report to aufs-users ML\n");
28411 +                       err = -ENOENT;
28412 +                       goto out;
28413 +               }
28414 +       }
28415 +
28416 +       ssz = au_sxattr(dentry, inode, &arg);
28417 +       /* forget even it if succeeds since the branch might set differently */
28418 +       forget_cached_acl(inode, type);
28419 +       dput(dentry);
28420 +       err = ssz;
28421 +       if (ssz >= 0)
28422 +               err = 0;
28423 +
28424 +out:
28425 +       return err;
28426 +}
28427 diff -urN /usr/share/empty/fs/aufs/procfs.c linux/fs/aufs/procfs.c
28428 --- /usr/share/empty/fs/aufs/procfs.c   1970-01-01 01:00:00.000000000 +0100
28429 +++ linux/fs/aufs/procfs.c      2021-11-01 23:48:34.209692595 +0100
28430 @@ -0,0 +1,170 @@
28431 +// SPDX-License-Identifier: GPL-2.0
28432 +/*
28433 + * Copyright (C) 2010-2021 Junjiro R. Okajima
28434 + *
28435 + * This program, aufs is free software; you can redistribute it and/or modify
28436 + * it under the terms of the GNU General Public License as published by
28437 + * the Free Software Foundation; either version 2 of the License, or
28438 + * (at your option) any later version.
28439 + *
28440 + * This program is distributed in the hope that it will be useful,
28441 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28442 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28443 + * GNU General Public License for more details.
28444 + *
28445 + * You should have received a copy of the GNU General Public License
28446 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28447 + */
28448 +
28449 +/*
28450 + * procfs interfaces
28451 + */
28452 +
28453 +#include <linux/proc_fs.h>
28454 +#include "aufs.h"
28455 +
28456 +static int au_procfs_plm_release(struct inode *inode, struct file *file)
28457 +{
28458 +       struct au_sbinfo *sbinfo;
28459 +
28460 +       sbinfo = file->private_data;
28461 +       if (sbinfo) {
28462 +               au_plink_maint_leave(sbinfo);
28463 +               kobject_put(&sbinfo->si_kobj);
28464 +       }
28465 +
28466 +       return 0;
28467 +}
28468 +
28469 +static void au_procfs_plm_write_clean(struct file *file)
28470 +{
28471 +       struct au_sbinfo *sbinfo;
28472 +
28473 +       sbinfo = file->private_data;
28474 +       if (sbinfo)
28475 +               au_plink_clean(sbinfo->si_sb, /*verbose*/0);
28476 +}
28477 +
28478 +static int au_procfs_plm_write_si(struct file *file, unsigned long id)
28479 +{
28480 +       int err;
28481 +       struct super_block *sb;
28482 +       struct au_sbinfo *sbinfo;
28483 +       struct hlist_bl_node *pos;
28484 +
28485 +       err = -EBUSY;
28486 +       if (unlikely(file->private_data))
28487 +               goto out;
28488 +
28489 +       sb = NULL;
28490 +       /* don't use au_sbilist_lock() here */
28491 +       hlist_bl_lock(&au_sbilist);
28492 +       hlist_bl_for_each_entry(sbinfo, pos, &au_sbilist, si_list)
28493 +               if (id == sysaufs_si_id(sbinfo)) {
28494 +                       if (kobject_get_unless_zero(&sbinfo->si_kobj))
28495 +                               sb = sbinfo->si_sb;
28496 +                       break;
28497 +               }
28498 +       hlist_bl_unlock(&au_sbilist);
28499 +
28500 +       err = -EINVAL;
28501 +       if (unlikely(!sb))
28502 +               goto out;
28503 +
28504 +       err = au_plink_maint_enter(sb);
28505 +       if (!err)
28506 +               /* keep kobject_get() */
28507 +               file->private_data = sbinfo;
28508 +       else
28509 +               kobject_put(&sbinfo->si_kobj);
28510 +out:
28511 +       return err;
28512 +}
28513 +
28514 +/*
28515 + * Accept a valid "si=xxxx" only.
28516 + * Once it is accepted successfully, accept "clean" too.
28517 + */
28518 +static ssize_t au_procfs_plm_write(struct file *file, const char __user *ubuf,
28519 +                                  size_t count, loff_t *ppos)
28520 +{
28521 +       ssize_t err;
28522 +       unsigned long id;
28523 +       /* last newline is allowed */
28524 +       char buf[3 + sizeof(unsigned long) * 2 + 1];
28525 +
28526 +       err = -EACCES;
28527 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
28528 +               goto out;
28529 +
28530 +       err = -EINVAL;
28531 +       if (unlikely(count > sizeof(buf)))
28532 +               goto out;
28533 +
28534 +       err = copy_from_user(buf, ubuf, count);
28535 +       if (unlikely(err)) {
28536 +               err = -EFAULT;
28537 +               goto out;
28538 +       }
28539 +       buf[count] = 0;
28540 +
28541 +       err = -EINVAL;
28542 +       if (!strcmp("clean", buf)) {
28543 +               au_procfs_plm_write_clean(file);
28544 +               goto out_success;
28545 +       } else if (unlikely(strncmp("si=", buf, 3)))
28546 +               goto out;
28547 +
28548 +       err = kstrtoul(buf + 3, 16, &id);
28549 +       if (unlikely(err))
28550 +               goto out;
28551 +
28552 +       err = au_procfs_plm_write_si(file, id);
28553 +       if (unlikely(err))
28554 +               goto out;
28555 +
28556 +out_success:
28557 +       err = count; /* success */
28558 +out:
28559 +       return err;
28560 +}
28561 +
28562 +static const struct proc_ops au_procfs_plm_op = {
28563 +       .proc_write     = au_procfs_plm_write,
28564 +       .proc_release   = au_procfs_plm_release
28565 +};
28566 +
28567 +/* ---------------------------------------------------------------------- */
28568 +
28569 +static struct proc_dir_entry *au_procfs_dir;
28570 +
28571 +void au_procfs_fin(void)
28572 +{
28573 +       remove_proc_entry(AUFS_PLINK_MAINT_NAME, au_procfs_dir);
28574 +       remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
28575 +}
28576 +
28577 +int __init au_procfs_init(void)
28578 +{
28579 +       int err;
28580 +       struct proc_dir_entry *entry;
28581 +
28582 +       err = -ENOMEM;
28583 +       au_procfs_dir = proc_mkdir(AUFS_PLINK_MAINT_DIR, NULL);
28584 +       if (unlikely(!au_procfs_dir))
28585 +               goto out;
28586 +
28587 +       entry = proc_create(AUFS_PLINK_MAINT_NAME, S_IFREG | 0200,
28588 +                           au_procfs_dir, &au_procfs_plm_op);
28589 +       if (unlikely(!entry))
28590 +               goto out_dir;
28591 +
28592 +       err = 0;
28593 +       goto out; /* success */
28594 +
28595 +
28596 +out_dir:
28597 +       remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
28598 +out:
28599 +       return err;
28600 +}
28601 diff -urN /usr/share/empty/fs/aufs/rdu.c linux/fs/aufs/rdu.c
28602 --- /usr/share/empty/fs/aufs/rdu.c      1970-01-01 01:00:00.000000000 +0100
28603 +++ linux/fs/aufs/rdu.c 2021-11-01 23:48:34.209692595 +0100
28604 @@ -0,0 +1,384 @@
28605 +// SPDX-License-Identifier: GPL-2.0
28606 +/*
28607 + * Copyright (C) 2005-2021 Junjiro R. Okajima
28608 + *
28609 + * This program, aufs is free software; you can redistribute it and/or modify
28610 + * it under the terms of the GNU General Public License as published by
28611 + * the Free Software Foundation; either version 2 of the License, or
28612 + * (at your option) any later version.
28613 + *
28614 + * This program is distributed in the hope that it will be useful,
28615 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28616 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28617 + * GNU General Public License for more details.
28618 + *
28619 + * You should have received a copy of the GNU General Public License
28620 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28621 + */
28622 +
28623 +/*
28624 + * readdir in userspace.
28625 + */
28626 +
28627 +#include <linux/compat.h>
28628 +#include <linux/fs_stack.h>
28629 +#include <linux/security.h>
28630 +#include "aufs.h"
28631 +
28632 +/* bits for struct aufs_rdu.flags */
28633 +#define        AuRdu_CALLED    1
28634 +#define        AuRdu_CONT      (1 << 1)
28635 +#define        AuRdu_FULL      (1 << 2)
28636 +#define au_ftest_rdu(flags, name)      ((flags) & AuRdu_##name)
28637 +#define au_fset_rdu(flags, name) \
28638 +       do { (flags) |= AuRdu_##name; } while (0)
28639 +#define au_fclr_rdu(flags, name) \
28640 +       do { (flags) &= ~AuRdu_##name; } while (0)
28641 +
28642 +struct au_rdu_arg {
28643 +       struct dir_context              ctx;
28644 +       struct aufs_rdu                 *rdu;
28645 +       union au_rdu_ent_ul             ent;
28646 +       unsigned long                   end;
28647 +
28648 +       struct super_block              *sb;
28649 +       int                             err;
28650 +};
28651 +
28652 +static int au_rdu_fill(struct dir_context *ctx, const char *name, int nlen,
28653 +                      loff_t offset, u64 h_ino, unsigned int d_type)
28654 +{
28655 +       int err, len;
28656 +       struct au_rdu_arg *arg = container_of(ctx, struct au_rdu_arg, ctx);
28657 +       struct aufs_rdu *rdu = arg->rdu;
28658 +       struct au_rdu_ent ent;
28659 +
28660 +       err = 0;
28661 +       arg->err = 0;
28662 +       au_fset_rdu(rdu->cookie.flags, CALLED);
28663 +       len = au_rdu_len(nlen);
28664 +       if (arg->ent.ul + len  < arg->end) {
28665 +               ent.ino = h_ino;
28666 +               ent.bindex = rdu->cookie.bindex;
28667 +               ent.type = d_type;
28668 +               ent.nlen = nlen;
28669 +               if (unlikely(nlen > AUFS_MAX_NAMELEN))
28670 +                       ent.type = DT_UNKNOWN;
28671 +
28672 +               /* unnecessary to support mmap_sem since this is a dir */
28673 +               err = -EFAULT;
28674 +               if (copy_to_user(arg->ent.e, &ent, sizeof(ent)))
28675 +                       goto out;
28676 +               if (copy_to_user(arg->ent.e->name, name, nlen))
28677 +                       goto out;
28678 +               /* the terminating NULL */
28679 +               if (__put_user(0, arg->ent.e->name + nlen))
28680 +                       goto out;
28681 +               err = 0;
28682 +               /* AuDbg("%p, %.*s\n", arg->ent.p, nlen, name); */
28683 +               arg->ent.ul += len;
28684 +               rdu->rent++;
28685 +       } else {
28686 +               err = -EFAULT;
28687 +               au_fset_rdu(rdu->cookie.flags, FULL);
28688 +               rdu->full = 1;
28689 +               rdu->tail = arg->ent;
28690 +       }
28691 +
28692 +out:
28693 +       /* AuTraceErr(err); */
28694 +       return err;
28695 +}
28696 +
28697 +static int au_rdu_do(struct file *h_file, struct au_rdu_arg *arg)
28698 +{
28699 +       int err;
28700 +       loff_t offset;
28701 +       struct au_rdu_cookie *cookie = &arg->rdu->cookie;
28702 +
28703 +       /* we don't have to care (FMODE_32BITHASH | FMODE_64BITHASH) for ext4 */
28704 +       offset = vfsub_llseek(h_file, cookie->h_pos, SEEK_SET);
28705 +       err = offset;
28706 +       if (unlikely(offset != cookie->h_pos))
28707 +               goto out;
28708 +
28709 +       err = 0;
28710 +       do {
28711 +               arg->err = 0;
28712 +               au_fclr_rdu(cookie->flags, CALLED);
28713 +               /* smp_mb(); */
28714 +               err = vfsub_iterate_dir(h_file, &arg->ctx);
28715 +               if (err >= 0)
28716 +                       err = arg->err;
28717 +       } while (!err
28718 +                && au_ftest_rdu(cookie->flags, CALLED)
28719 +                && !au_ftest_rdu(cookie->flags, FULL));
28720 +       cookie->h_pos = h_file->f_pos;
28721 +
28722 +out:
28723 +       AuTraceErr(err);
28724 +       return err;
28725 +}
28726 +
28727 +static int au_rdu(struct file *file, struct aufs_rdu *rdu)
28728 +{
28729 +       int err;
28730 +       aufs_bindex_t bbot;
28731 +       struct au_rdu_arg arg = {
28732 +               .ctx = {
28733 +                       .actor = au_rdu_fill
28734 +               }
28735 +       };
28736 +       struct dentry *dentry;
28737 +       struct inode *inode;
28738 +       struct file *h_file;
28739 +       struct au_rdu_cookie *cookie = &rdu->cookie;
28740 +
28741 +       /* VERIFY_WRITE */
28742 +       err = !access_ok(rdu->ent.e, rdu->sz);
28743 +       if (unlikely(err)) {
28744 +               err = -EFAULT;
28745 +               AuTraceErr(err);
28746 +               goto out;
28747 +       }
28748 +       rdu->rent = 0;
28749 +       rdu->tail = rdu->ent;
28750 +       rdu->full = 0;
28751 +       arg.rdu = rdu;
28752 +       arg.ent = rdu->ent;
28753 +       arg.end = arg.ent.ul;
28754 +       arg.end += rdu->sz;
28755 +
28756 +       err = -ENOTDIR;
28757 +       if (unlikely(!file->f_op->iterate && !file->f_op->iterate_shared))
28758 +               goto out;
28759 +
28760 +       err = security_file_permission(file, MAY_READ);
28761 +       AuTraceErr(err);
28762 +       if (unlikely(err))
28763 +               goto out;
28764 +
28765 +       dentry = file->f_path.dentry;
28766 +       inode = d_inode(dentry);
28767 +       inode_lock_shared(inode);
28768 +
28769 +       arg.sb = inode->i_sb;
28770 +       err = si_read_lock(arg.sb, AuLock_FLUSH | AuLock_NOPLM);
28771 +       if (unlikely(err))
28772 +               goto out_mtx;
28773 +       err = au_alive_dir(dentry);
28774 +       if (unlikely(err))
28775 +               goto out_si;
28776 +       /* todo: reval? */
28777 +       fi_read_lock(file);
28778 +
28779 +       err = -EAGAIN;
28780 +       if (unlikely(au_ftest_rdu(cookie->flags, CONT)
28781 +                    && cookie->generation != au_figen(file)))
28782 +               goto out_unlock;
28783 +
28784 +       err = 0;
28785 +       if (!rdu->blk) {
28786 +               rdu->blk = au_sbi(arg.sb)->si_rdblk;
28787 +               if (!rdu->blk)
28788 +                       rdu->blk = au_dir_size(file, /*dentry*/NULL);
28789 +       }
28790 +       bbot = au_fbtop(file);
28791 +       if (cookie->bindex < bbot)
28792 +               cookie->bindex = bbot;
28793 +       bbot = au_fbbot_dir(file);
28794 +       /* AuDbg("b%d, b%d\n", cookie->bindex, bbot); */
28795 +       for (; !err && cookie->bindex <= bbot;
28796 +            cookie->bindex++, cookie->h_pos = 0) {
28797 +               h_file = au_hf_dir(file, cookie->bindex);
28798 +               if (!h_file)
28799 +                       continue;
28800 +
28801 +               au_fclr_rdu(cookie->flags, FULL);
28802 +               err = au_rdu_do(h_file, &arg);
28803 +               AuTraceErr(err);
28804 +               if (unlikely(au_ftest_rdu(cookie->flags, FULL) || err))
28805 +                       break;
28806 +       }
28807 +       AuDbg("rent %llu\n", rdu->rent);
28808 +
28809 +       if (!err && !au_ftest_rdu(cookie->flags, CONT)) {
28810 +               rdu->shwh = !!au_opt_test(au_sbi(arg.sb)->si_mntflags, SHWH);
28811 +               au_fset_rdu(cookie->flags, CONT);
28812 +               cookie->generation = au_figen(file);
28813 +       }
28814 +
28815 +       ii_read_lock_child(inode);
28816 +       fsstack_copy_attr_atime(inode, au_h_iptr(inode, au_ibtop(inode)));
28817 +       ii_read_unlock(inode);
28818 +
28819 +out_unlock:
28820 +       fi_read_unlock(file);
28821 +out_si:
28822 +       si_read_unlock(arg.sb);
28823 +out_mtx:
28824 +       inode_unlock_shared(inode);
28825 +out:
28826 +       AuTraceErr(err);
28827 +       return err;
28828 +}
28829 +
28830 +static int au_rdu_ino(struct file *file, struct aufs_rdu *rdu)
28831 +{
28832 +       int err;
28833 +       ino_t ino;
28834 +       unsigned long long nent;
28835 +       union au_rdu_ent_ul *u;
28836 +       struct au_rdu_ent ent;
28837 +       struct super_block *sb;
28838 +
28839 +       err = 0;
28840 +       nent = rdu->nent;
28841 +       u = &rdu->ent;
28842 +       sb = file->f_path.dentry->d_sb;
28843 +       si_read_lock(sb, AuLock_FLUSH);
28844 +       while (nent-- > 0) {
28845 +               /* unnecessary to support mmap_sem since this is a dir */
28846 +               err = copy_from_user(&ent, u->e, sizeof(ent));
28847 +               if (!err)
28848 +                       /* VERIFY_WRITE */
28849 +                       err = !access_ok(&u->e->ino, sizeof(ino));
28850 +               if (unlikely(err)) {
28851 +                       err = -EFAULT;
28852 +                       AuTraceErr(err);
28853 +                       break;
28854 +               }
28855 +
28856 +               /* AuDbg("b%d, i%llu\n", ent.bindex, ent.ino); */
28857 +               if (!ent.wh)
28858 +                       err = au_ino(sb, ent.bindex, ent.ino, ent.type, &ino);
28859 +               else
28860 +                       err = au_wh_ino(sb, ent.bindex, ent.ino, ent.type,
28861 +                                       &ino);
28862 +               if (unlikely(err)) {
28863 +                       AuTraceErr(err);
28864 +                       break;
28865 +               }
28866 +
28867 +               err = __put_user(ino, &u->e->ino);
28868 +               if (unlikely(err)) {
28869 +                       err = -EFAULT;
28870 +                       AuTraceErr(err);
28871 +                       break;
28872 +               }
28873 +               u->ul += au_rdu_len(ent.nlen);
28874 +       }
28875 +       si_read_unlock(sb);
28876 +
28877 +       return err;
28878 +}
28879 +
28880 +/* ---------------------------------------------------------------------- */
28881 +
28882 +static int au_rdu_verify(struct aufs_rdu *rdu)
28883 +{
28884 +       AuDbg("rdu{%llu, %p, %u | %u | %llu, %u, %u | "
28885 +             "%llu, b%d, 0x%x, g%u}\n",
28886 +             rdu->sz, rdu->ent.e, rdu->verify[AufsCtlRduV_SZ],
28887 +             rdu->blk,
28888 +             rdu->rent, rdu->shwh, rdu->full,
28889 +             rdu->cookie.h_pos, rdu->cookie.bindex, rdu->cookie.flags,
28890 +             rdu->cookie.generation);
28891 +
28892 +       if (rdu->verify[AufsCtlRduV_SZ] == sizeof(*rdu))
28893 +               return 0;
28894 +
28895 +       AuDbg("%u:%u\n",
28896 +             rdu->verify[AufsCtlRduV_SZ], (unsigned int)sizeof(*rdu));
28897 +       return -EINVAL;
28898 +}
28899 +
28900 +long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
28901 +{
28902 +       long err, e;
28903 +       struct aufs_rdu rdu;
28904 +       void __user *p = (void __user *)arg;
28905 +
28906 +       err = copy_from_user(&rdu, p, sizeof(rdu));
28907 +       if (unlikely(err)) {
28908 +               err = -EFAULT;
28909 +               AuTraceErr(err);
28910 +               goto out;
28911 +       }
28912 +       err = au_rdu_verify(&rdu);
28913 +       if (unlikely(err))
28914 +               goto out;
28915 +
28916 +       switch (cmd) {
28917 +       case AUFS_CTL_RDU:
28918 +               err = au_rdu(file, &rdu);
28919 +               if (unlikely(err))
28920 +                       break;
28921 +
28922 +               e = copy_to_user(p, &rdu, sizeof(rdu));
28923 +               if (unlikely(e)) {
28924 +                       err = -EFAULT;
28925 +                       AuTraceErr(err);
28926 +               }
28927 +               break;
28928 +       case AUFS_CTL_RDU_INO:
28929 +               err = au_rdu_ino(file, &rdu);
28930 +               break;
28931 +
28932 +       default:
28933 +               /* err = -ENOTTY; */
28934 +               err = -EINVAL;
28935 +       }
28936 +
28937 +out:
28938 +       AuTraceErr(err);
28939 +       return err;
28940 +}
28941 +
28942 +#ifdef CONFIG_COMPAT
28943 +long au_rdu_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
28944 +{
28945 +       long err, e;
28946 +       struct aufs_rdu rdu;
28947 +       void __user *p = compat_ptr(arg);
28948 +
28949 +       /* todo: get_user()? */
28950 +       err = copy_from_user(&rdu, p, sizeof(rdu));
28951 +       if (unlikely(err)) {
28952 +               err = -EFAULT;
28953 +               AuTraceErr(err);
28954 +               goto out;
28955 +       }
28956 +       rdu.ent.e = compat_ptr(rdu.ent.ul);
28957 +       err = au_rdu_verify(&rdu);
28958 +       if (unlikely(err))
28959 +               goto out;
28960 +
28961 +       switch (cmd) {
28962 +       case AUFS_CTL_RDU:
28963 +               err = au_rdu(file, &rdu);
28964 +               if (unlikely(err))
28965 +                       break;
28966 +
28967 +               rdu.ent.ul = ptr_to_compat(rdu.ent.e);
28968 +               rdu.tail.ul = ptr_to_compat(rdu.tail.e);
28969 +               e = copy_to_user(p, &rdu, sizeof(rdu));
28970 +               if (unlikely(e)) {
28971 +                       err = -EFAULT;
28972 +                       AuTraceErr(err);
28973 +               }
28974 +               break;
28975 +       case AUFS_CTL_RDU_INO:
28976 +               err = au_rdu_ino(file, &rdu);
28977 +               break;
28978 +
28979 +       default:
28980 +               /* err = -ENOTTY; */
28981 +               err = -EINVAL;
28982 +       }
28983 +
28984 +out:
28985 +       AuTraceErr(err);
28986 +       return err;
28987 +}
28988 +#endif
28989 diff -urN /usr/share/empty/fs/aufs/rwsem.h linux/fs/aufs/rwsem.h
28990 --- /usr/share/empty/fs/aufs/rwsem.h    1970-01-01 01:00:00.000000000 +0100
28991 +++ linux/fs/aufs/rwsem.h       2021-11-01 23:48:34.209692595 +0100
28992 @@ -0,0 +1,85 @@
28993 +/* SPDX-License-Identifier: GPL-2.0 */
28994 +/*
28995 + * Copyright (C) 2005-2021 Junjiro R. Okajima
28996 + *
28997 + * This program, aufs is free software; you can redistribute it and/or modify
28998 + * it under the terms of the GNU General Public License as published by
28999 + * the Free Software Foundation; either version 2 of the License, or
29000 + * (at your option) any later version.
29001 + *
29002 + * This program is distributed in the hope that it will be useful,
29003 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
29004 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29005 + * GNU General Public License for more details.
29006 + *
29007 + * You should have received a copy of the GNU General Public License
29008 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
29009 + */
29010 +
29011 +/*
29012 + * simple read-write semaphore wrappers
29013 + */
29014 +
29015 +#ifndef __AUFS_RWSEM_H__
29016 +#define __AUFS_RWSEM_H__
29017 +
29018 +#ifdef __KERNEL__
29019 +
29020 +#include "debug.h"
29021 +
29022 +/* in the future, the name 'au_rwsem' will be totally gone */
29023 +#define au_rwsem       rw_semaphore
29024 +
29025 +/* to debug easier, do not make them inlined functions */
29026 +#define AuRwMustNoWaiters(rw)  AuDebugOn(rwsem_is_contended(rw))
29027 +
29028 +#ifdef CONFIG_LOCKDEP
29029 +/* rwsem_is_locked() is unusable */
29030 +#define AuRwMustReadLock(rw)   AuDebugOn(IS_ENABLED(CONFIG_LOCKDEP)    \
29031 +                                         && !lockdep_recursing(current) \
29032 +                                         && debug_locks                \
29033 +                                         && !lockdep_is_held_type(rw, 1))
29034 +#define AuRwMustWriteLock(rw)  AuDebugOn(IS_ENABLED(CONFIG_LOCKDEP)    \
29035 +                                         && !lockdep_recursing(current) \
29036 +                                         && debug_locks                \
29037 +                                         && !lockdep_is_held_type(rw, 0))
29038 +#define AuRwMustAnyLock(rw)    AuDebugOn(IS_ENABLED(CONFIG_LOCKDEP)    \
29039 +                                         && !lockdep_recursing(current) \
29040 +                                         && debug_locks                \
29041 +                                         && !lockdep_is_held(rw))
29042 +#define AuRwDestroy(rw)                AuDebugOn(IS_ENABLED(CONFIG_LOCKDEP)    \
29043 +                                         && !lockdep_recursing(current) \
29044 +                                         && debug_locks                \
29045 +                                         && lockdep_is_held(rw))
29046 +#else
29047 +#define AuRwMustReadLock(rw)   do {} while (0)
29048 +#define AuRwMustWriteLock(rw)  do {} while (0)
29049 +#define AuRwMustAnyLock(rw)    do {} while (0)
29050 +#define AuRwDestroy(rw)                do {} while (0)
29051 +#endif
29052 +
29053 +#define au_rw_init(rw) init_rwsem(rw)
29054 +
29055 +#define au_rw_init_wlock(rw) do {              \
29056 +               au_rw_init(rw);                 \
29057 +               down_write(rw);                 \
29058 +       } while (0)
29059 +
29060 +#define au_rw_init_wlock_nested(rw, lsc) do {  \
29061 +               au_rw_init(rw);                 \
29062 +               down_write_nested(rw, lsc);     \
29063 +       } while (0)
29064 +
29065 +#define au_rw_read_lock(rw)            down_read(rw)
29066 +#define au_rw_read_lock_nested(rw, lsc)        down_read_nested(rw, lsc)
29067 +#define au_rw_read_unlock(rw)          up_read(rw)
29068 +#define au_rw_dgrade_lock(rw)          downgrade_write(rw)
29069 +#define au_rw_write_lock(rw)           down_write(rw)
29070 +#define au_rw_write_lock_nested(rw, lsc) down_write_nested(rw, lsc)
29071 +#define au_rw_write_unlock(rw)         up_write(rw)
29072 +/* why is not _nested version defined? */
29073 +#define au_rw_read_trylock(rw)         down_read_trylock(rw)
29074 +#define au_rw_write_trylock(rw)                down_write_trylock(rw)
29075 +
29076 +#endif /* __KERNEL__ */
29077 +#endif /* __AUFS_RWSEM_H__ */
29078 diff -urN /usr/share/empty/fs/aufs/sbinfo.c linux/fs/aufs/sbinfo.c
29079 --- /usr/share/empty/fs/aufs/sbinfo.c   1970-01-01 01:00:00.000000000 +0100
29080 +++ linux/fs/aufs/sbinfo.c      2021-11-01 23:48:34.213025928 +0100
29081 @@ -0,0 +1,314 @@
29082 +// SPDX-License-Identifier: GPL-2.0
29083 +/*
29084 + * Copyright (C) 2005-2021 Junjiro R. Okajima
29085 + *
29086 + * This program, aufs is free software; you can redistribute it and/or modify
29087 + * it under the terms of the GNU General Public License as published by
29088 + * the Free Software Foundation; either version 2 of the License, or
29089 + * (at your option) any later version.
29090 + *
29091 + * This program is distributed in the hope that it will be useful,
29092 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
29093 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29094 + * GNU General Public License for more details.
29095 + *
29096 + * You should have received a copy of the GNU General Public License
29097 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
29098 + */
29099 +
29100 +/*
29101 + * superblock private data
29102 + */
29103 +
29104 +#include <linux/iversion.h>
29105 +#include "aufs.h"
29106 +
29107 +/*
29108 + * they are necessary regardless sysfs is disabled.
29109 + */
29110 +void au_si_free(struct kobject *kobj)
29111 +{
29112 +       int i;
29113 +       struct au_sbinfo *sbinfo;
29114 +       char *locked __maybe_unused; /* debug only */
29115 +
29116 +       sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
29117 +       for (i = 0; i < AuPlink_NHASH; i++)
29118 +               AuDebugOn(!hlist_bl_empty(sbinfo->si_plink + i));
29119 +       AuDebugOn(atomic_read(&sbinfo->si_nowait.nw_len));
29120 +
29121 +       AuLCntZero(au_lcnt_read(&sbinfo->si_ninodes, /*do_rev*/0));
29122 +       au_lcnt_fin(&sbinfo->si_ninodes, /*do_sync*/0);
29123 +       AuLCntZero(au_lcnt_read(&sbinfo->si_nfiles, /*do_rev*/0));
29124 +       au_lcnt_fin(&sbinfo->si_nfiles, /*do_sync*/0);
29125 +
29126 +       dbgaufs_si_fin(sbinfo);
29127 +       au_rw_write_lock(&sbinfo->si_rwsem);
29128 +       au_br_free(sbinfo);
29129 +       au_rw_write_unlock(&sbinfo->si_rwsem);
29130 +
29131 +       au_kfree_try_rcu(sbinfo->si_branch);
29132 +       mutex_destroy(&sbinfo->si_xib_mtx);
29133 +       AuRwDestroy(&sbinfo->si_rwsem);
29134 +
29135 +       au_lcnt_wait_for_fin(&sbinfo->si_ninodes);
29136 +       /* si_nfiles is waited too */
29137 +       au_kfree_rcu(sbinfo);
29138 +}
29139 +
29140 +int au_si_alloc(struct super_block *sb)
29141 +{
29142 +       int err, i;
29143 +       struct au_sbinfo *sbinfo;
29144 +
29145 +       err = -ENOMEM;
29146 +       sbinfo = kzalloc(sizeof(*sbinfo), GFP_NOFS);
29147 +       if (unlikely(!sbinfo))
29148 +               goto out;
29149 +
29150 +       /* will be reallocated separately */
29151 +       sbinfo->si_branch = kzalloc(sizeof(*sbinfo->si_branch), GFP_NOFS);
29152 +       if (unlikely(!sbinfo->si_branch))
29153 +               goto out_sbinfo;
29154 +
29155 +       err = sysaufs_si_init(sbinfo);
29156 +       if (!err) {
29157 +               dbgaufs_si_null(sbinfo);
29158 +               err = dbgaufs_si_init(sbinfo);
29159 +               if (unlikely(err))
29160 +                       kobject_put(&sbinfo->si_kobj);
29161 +       }
29162 +       if (unlikely(err))
29163 +               goto out_br;
29164 +
29165 +       au_nwt_init(&sbinfo->si_nowait);
29166 +       au_rw_init_wlock(&sbinfo->si_rwsem);
29167 +
29168 +       au_lcnt_init(&sbinfo->si_ninodes, /*release*/NULL);
29169 +       au_lcnt_init(&sbinfo->si_nfiles, /*release*/NULL);
29170 +
29171 +       sbinfo->si_bbot = -1;
29172 +       sbinfo->si_last_br_id = AUFS_BRANCH_MAX / 2;
29173 +
29174 +       sbinfo->si_wbr_copyup = AuWbrCopyup_Def;
29175 +       sbinfo->si_wbr_create = AuWbrCreate_Def;
29176 +       sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + sbinfo->si_wbr_copyup;
29177 +       sbinfo->si_wbr_create_ops = au_wbr_create_ops + sbinfo->si_wbr_create;
29178 +
29179 +       au_fhsm_init(sbinfo);
29180 +
29181 +       sbinfo->si_mntflags = au_opts_plink(AuOpt_Def);
29182 +
29183 +       sbinfo->si_xino_jiffy = jiffies;
29184 +       sbinfo->si_xino_expire
29185 +               = msecs_to_jiffies(AUFS_XINO_DEF_SEC * MSEC_PER_SEC);
29186 +       mutex_init(&sbinfo->si_xib_mtx);
29187 +       /* leave si_xib_last_pindex and si_xib_next_bit */
29188 +
29189 +       INIT_HLIST_BL_HEAD(&sbinfo->si_aopen);
29190 +
29191 +       sbinfo->si_rdcache = msecs_to_jiffies(AUFS_RDCACHE_DEF * MSEC_PER_SEC);
29192 +       sbinfo->si_rdblk = AUFS_RDBLK_DEF;
29193 +       sbinfo->si_rdhash = AUFS_RDHASH_DEF;
29194 +       sbinfo->si_dirwh = AUFS_DIRWH_DEF;
29195 +
29196 +       for (i = 0; i < AuPlink_NHASH; i++)
29197 +               INIT_HLIST_BL_HEAD(sbinfo->si_plink + i);
29198 +       init_waitqueue_head(&sbinfo->si_plink_wq);
29199 +       spin_lock_init(&sbinfo->si_plink_maint_lock);
29200 +
29201 +       INIT_HLIST_BL_HEAD(&sbinfo->si_files);
29202 +
29203 +       /* with getattr by default */
29204 +       sbinfo->si_iop_array = aufs_iop;
29205 +
29206 +       /* leave other members for sysaufs and si_mnt. */
29207 +       sbinfo->si_sb = sb;
29208 +       sb->s_fs_info = sbinfo;
29209 +       si_pid_set(sb);
29210 +       return 0; /* success */
29211 +
29212 +out_br:
29213 +       au_kfree_try_rcu(sbinfo->si_branch);
29214 +out_sbinfo:
29215 +       au_kfree_rcu(sbinfo);
29216 +out:
29217 +       return err;
29218 +}
29219 +
29220 +int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr, int may_shrink)
29221 +{
29222 +       int err, sz;
29223 +       struct au_branch **brp;
29224 +
29225 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
29226 +
29227 +       err = -ENOMEM;
29228 +       sz = sizeof(*brp) * (sbinfo->si_bbot + 1);
29229 +       if (unlikely(!sz))
29230 +               sz = sizeof(*brp);
29231 +       brp = au_kzrealloc(sbinfo->si_branch, sz, sizeof(*brp) * nbr, GFP_NOFS,
29232 +                          may_shrink);
29233 +       if (brp) {
29234 +               sbinfo->si_branch = brp;
29235 +               err = 0;
29236 +       }
29237 +
29238 +       return err;
29239 +}
29240 +
29241 +/* ---------------------------------------------------------------------- */
29242 +
29243 +unsigned int au_sigen_inc(struct super_block *sb)
29244 +{
29245 +       unsigned int gen;
29246 +       struct inode *inode;
29247 +
29248 +       SiMustWriteLock(sb);
29249 +
29250 +       gen = ++au_sbi(sb)->si_generation;
29251 +       au_update_digen(sb->s_root);
29252 +       inode = d_inode(sb->s_root);
29253 +       au_update_iigen(inode, /*half*/0);
29254 +       inode_inc_iversion(inode);
29255 +       return gen;
29256 +}
29257 +
29258 +aufs_bindex_t au_new_br_id(struct super_block *sb)
29259 +{
29260 +       aufs_bindex_t br_id;
29261 +       int i;
29262 +       struct au_sbinfo *sbinfo;
29263 +
29264 +       SiMustWriteLock(sb);
29265 +
29266 +       sbinfo = au_sbi(sb);
29267 +       for (i = 0; i <= AUFS_BRANCH_MAX; i++) {
29268 +               br_id = ++sbinfo->si_last_br_id;
29269 +               AuDebugOn(br_id < 0);
29270 +               if (br_id && au_br_index(sb, br_id) < 0)
29271 +                       return br_id;
29272 +       }
29273 +
29274 +       return -1;
29275 +}
29276 +
29277 +/* ---------------------------------------------------------------------- */
29278 +
29279 +/* it is ok that new 'nwt' tasks are appended while we are sleeping */
29280 +int si_read_lock(struct super_block *sb, int flags)
29281 +{
29282 +       int err;
29283 +
29284 +       err = 0;
29285 +       if (au_ftest_lock(flags, FLUSH))
29286 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
29287 +
29288 +       si_noflush_read_lock(sb);
29289 +       err = au_plink_maint(sb, flags);
29290 +       if (unlikely(err))
29291 +               si_read_unlock(sb);
29292 +
29293 +       return err;
29294 +}
29295 +
29296 +int si_write_lock(struct super_block *sb, int flags)
29297 +{
29298 +       int err;
29299 +
29300 +       if (au_ftest_lock(flags, FLUSH))
29301 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
29302 +
29303 +       si_noflush_write_lock(sb);
29304 +       err = au_plink_maint(sb, flags);
29305 +       if (unlikely(err))
29306 +               si_write_unlock(sb);
29307 +
29308 +       return err;
29309 +}
29310 +
29311 +/* dentry and super_block lock. call at entry point */
29312 +int aufs_read_lock(struct dentry *dentry, int flags)
29313 +{
29314 +       int err;
29315 +       struct super_block *sb;
29316 +
29317 +       sb = dentry->d_sb;
29318 +       err = si_read_lock(sb, flags);
29319 +       if (unlikely(err))
29320 +               goto out;
29321 +
29322 +       if (au_ftest_lock(flags, DW))
29323 +               di_write_lock_child(dentry);
29324 +       else
29325 +               di_read_lock_child(dentry, flags);
29326 +
29327 +       if (au_ftest_lock(flags, GEN)) {
29328 +               err = au_digen_test(dentry, au_sigen(sb));
29329 +               if (!au_opt_test(au_mntflags(sb), UDBA_NONE))
29330 +                       AuDebugOn(!err && au_dbrange_test(dentry));
29331 +               else if (!err)
29332 +                       err = au_dbrange_test(dentry);
29333 +               if (unlikely(err))
29334 +                       aufs_read_unlock(dentry, flags);
29335 +       }
29336 +
29337 +out:
29338 +       return err;
29339 +}
29340 +
29341 +void aufs_read_unlock(struct dentry *dentry, int flags)
29342 +{
29343 +       if (au_ftest_lock(flags, DW))
29344 +               di_write_unlock(dentry);
29345 +       else
29346 +               di_read_unlock(dentry, flags);
29347 +       si_read_unlock(dentry->d_sb);
29348 +}
29349 +
29350 +void aufs_write_lock(struct dentry *dentry)
29351 +{
29352 +       si_write_lock(dentry->d_sb, AuLock_FLUSH | AuLock_NOPLMW);
29353 +       di_write_lock_child(dentry);
29354 +}
29355 +
29356 +void aufs_write_unlock(struct dentry *dentry)
29357 +{
29358 +       di_write_unlock(dentry);
29359 +       si_write_unlock(dentry->d_sb);
29360 +}
29361 +
29362 +int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags)
29363 +{
29364 +       int err;
29365 +       unsigned int sigen;
29366 +       struct super_block *sb;
29367 +
29368 +       sb = d1->d_sb;
29369 +       err = si_read_lock(sb, flags);
29370 +       if (unlikely(err))
29371 +               goto out;
29372 +
29373 +       di_write_lock2_child(d1, d2, au_ftest_lock(flags, DIRS));
29374 +
29375 +       if (au_ftest_lock(flags, GEN)) {
29376 +               sigen = au_sigen(sb);
29377 +               err = au_digen_test(d1, sigen);
29378 +               AuDebugOn(!err && au_dbrange_test(d1));
29379 +               if (!err) {
29380 +                       err = au_digen_test(d2, sigen);
29381 +                       AuDebugOn(!err && au_dbrange_test(d2));
29382 +               }
29383 +               if (unlikely(err))
29384 +                       aufs_read_and_write_unlock2(d1, d2);
29385 +       }
29386 +
29387 +out:
29388 +       return err;
29389 +}
29390 +
29391 +void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2)
29392 +{
29393 +       di_write_unlock2(d1, d2);
29394 +       si_read_unlock(d1->d_sb);
29395 +}
29396 diff -urN /usr/share/empty/fs/aufs/super.c linux/fs/aufs/super.c
29397 --- /usr/share/empty/fs/aufs/super.c    1970-01-01 01:00:00.000000000 +0100
29398 +++ linux/fs/aufs/super.c       2021-11-01 23:48:34.213025928 +0100
29399 @@ -0,0 +1,1050 @@
29400 +// SPDX-License-Identifier: GPL-2.0
29401 +/*
29402 + * Copyright (C) 2005-2021 Junjiro R. Okajima
29403 + *
29404 + * This program, aufs is free software; you can redistribute it and/or modify
29405 + * it under the terms of the GNU General Public License as published by
29406 + * the Free Software Foundation; either version 2 of the License, or
29407 + * (at your option) any later version.
29408 + *
29409 + * This program is distributed in the hope that it will be useful,
29410 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
29411 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29412 + * GNU General Public License for more details.
29413 + *
29414 + * You should have received a copy of the GNU General Public License
29415 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
29416 + */
29417 +
29418 +/*
29419 + * mount and super_block operations
29420 + */
29421 +
29422 +#include <linux/iversion.h>
29423 +#include <linux/mm.h>
29424 +#include <linux/seq_file.h>
29425 +#include <linux/statfs.h>
29426 +#include <linux/vmalloc.h>
29427 +#include "aufs.h"
29428 +
29429 +/*
29430 + * super_operations
29431 + */
29432 +static struct inode *aufs_alloc_inode(struct super_block *sb __maybe_unused)
29433 +{
29434 +       struct au_icntnr *c;
29435 +
29436 +       c = au_cache_alloc_icntnr();
29437 +       if (c) {
29438 +               au_icntnr_init(c);
29439 +               inode_set_iversion(&c->vfs_inode, 1); /* sigen(sb); */
29440 +               c->iinfo.ii_hinode = NULL;
29441 +               return &c->vfs_inode;
29442 +       }
29443 +       return NULL;
29444 +}
29445 +
29446 +static void aufs_destroy_inode(struct inode *inode)
29447 +{
29448 +       if (!au_is_bad_inode(inode))
29449 +               au_iinfo_fin(inode);
29450 +}
29451 +
29452 +static void aufs_free_inode(struct inode *inode)
29453 +{
29454 +       au_cache_free_icntnr(container_of(inode, struct au_icntnr, vfs_inode));
29455 +}
29456 +
29457 +struct inode *au_iget_locked(struct super_block *sb, ino_t ino)
29458 +{
29459 +       struct inode *inode;
29460 +       int err;
29461 +
29462 +       inode = iget_locked(sb, ino);
29463 +       if (unlikely(!inode)) {
29464 +               inode = ERR_PTR(-ENOMEM);
29465 +               goto out;
29466 +       }
29467 +       if (!(inode->i_state & I_NEW))
29468 +               goto out;
29469 +
29470 +       err = au_xigen_new(inode);
29471 +       if (!err)
29472 +               err = au_iinfo_init(inode);
29473 +       if (!err)
29474 +               inode_inc_iversion(inode);
29475 +       else {
29476 +               iget_failed(inode);
29477 +               inode = ERR_PTR(err);
29478 +       }
29479 +
29480 +out:
29481 +       /* never return NULL */
29482 +       AuDebugOn(!inode);
29483 +       AuTraceErrPtr(inode);
29484 +       return inode;
29485 +}
29486 +
29487 +/* lock free root dinfo */
29488 +static int au_show_brs(struct seq_file *seq, struct super_block *sb)
29489 +{
29490 +       int err;
29491 +       aufs_bindex_t bindex, bbot;
29492 +       struct path path;
29493 +       struct au_hdentry *hdp;
29494 +       struct au_branch *br;
29495 +       au_br_perm_str_t perm;
29496 +
29497 +       err = 0;
29498 +       bbot = au_sbbot(sb);
29499 +       bindex = 0;
29500 +       hdp = au_hdentry(au_di(sb->s_root), bindex);
29501 +       for (; !err && bindex <= bbot; bindex++, hdp++) {
29502 +               br = au_sbr(sb, bindex);
29503 +               path.mnt = au_br_mnt(br);
29504 +               path.dentry = hdp->hd_dentry;
29505 +               err = au_seq_path(seq, &path);
29506 +               if (!err) {
29507 +                       au_optstr_br_perm(&perm, br->br_perm);
29508 +                       seq_printf(seq, "=%s", perm.a);
29509 +                       if (bindex != bbot)
29510 +                               seq_putc(seq, ':');
29511 +               }
29512 +       }
29513 +       if (unlikely(err || seq_has_overflowed(seq)))
29514 +               err = -E2BIG;
29515 +
29516 +       return err;
29517 +}
29518 +
29519 +static void au_gen_fmt(char *fmt, int len __maybe_unused, const char *pat,
29520 +                      const char *append)
29521 +{
29522 +       char *p;
29523 +
29524 +       p = fmt;
29525 +       while (*pat != ':')
29526 +               *p++ = *pat++;
29527 +       *p++ = *pat++;
29528 +       strcpy(p, append);
29529 +       AuDebugOn(strlen(fmt) >= len);
29530 +}
29531 +
29532 +static void au_show_wbr_create(struct seq_file *m, int v,
29533 +                              struct au_sbinfo *sbinfo)
29534 +{
29535 +       const char *pat;
29536 +       char fmt[32];
29537 +       struct au_wbr_mfs *mfs;
29538 +
29539 +       AuRwMustAnyLock(&sbinfo->si_rwsem);
29540 +
29541 +       seq_puts(m, ",create=");
29542 +       pat = au_optstr_wbr_create(v);
29543 +       mfs = &sbinfo->si_wbr_mfs;
29544 +       switch (v) {
29545 +       case AuWbrCreate_TDP:
29546 +       case AuWbrCreate_RR:
29547 +       case AuWbrCreate_MFS:
29548 +       case AuWbrCreate_PMFS:
29549 +               seq_puts(m, pat);
29550 +               break;
29551 +       case AuWbrCreate_MFSRR:
29552 +       case AuWbrCreate_TDMFS:
29553 +       case AuWbrCreate_PMFSRR:
29554 +               au_gen_fmt(fmt, sizeof(fmt), pat, "%llu");
29555 +               seq_printf(m, fmt, mfs->mfsrr_watermark);
29556 +               break;
29557 +       case AuWbrCreate_MFSV:
29558 +       case AuWbrCreate_PMFSV:
29559 +               au_gen_fmt(fmt, sizeof(fmt), pat, "%lu");
29560 +               seq_printf(m, fmt,
29561 +                          jiffies_to_msecs(mfs->mfs_expire)
29562 +                          / MSEC_PER_SEC);
29563 +               break;
29564 +       case AuWbrCreate_MFSRRV:
29565 +       case AuWbrCreate_TDMFSV:
29566 +       case AuWbrCreate_PMFSRRV:
29567 +               au_gen_fmt(fmt, sizeof(fmt), pat, "%llu:%lu");
29568 +               seq_printf(m, fmt, mfs->mfsrr_watermark,
29569 +                          jiffies_to_msecs(mfs->mfs_expire) / MSEC_PER_SEC);
29570 +               break;
29571 +       default:
29572 +               BUG();
29573 +       }
29574 +}
29575 +
29576 +static int au_show_xino(struct seq_file *seq, struct super_block *sb)
29577 +{
29578 +#ifdef CONFIG_SYSFS
29579 +       return 0;
29580 +#else
29581 +       int err;
29582 +       const int len = sizeof(AUFS_XINO_FNAME) - 1;
29583 +       aufs_bindex_t bindex, brid;
29584 +       struct qstr *name;
29585 +       struct file *f;
29586 +       struct dentry *d, *h_root;
29587 +       struct au_branch *br;
29588 +
29589 +       AuRwMustAnyLock(&sbinfo->si_rwsem);
29590 +
29591 +       err = 0;
29592 +       f = au_sbi(sb)->si_xib;
29593 +       if (!f)
29594 +               goto out;
29595 +
29596 +       /* stop printing the default xino path on the first writable branch */
29597 +       h_root = NULL;
29598 +       bindex = au_xi_root(sb, f->f_path.dentry);
29599 +       if (bindex >= 0) {
29600 +               br = au_sbr_sb(sb, bindex);
29601 +               h_root = au_br_dentry(br);
29602 +       }
29603 +
29604 +       d = f->f_path.dentry;
29605 +       name = &d->d_name;
29606 +       /* safe ->d_parent because the file is unlinked */
29607 +       if (d->d_parent == h_root
29608 +           && name->len == len
29609 +           && !memcmp(name->name, AUFS_XINO_FNAME, len))
29610 +               goto out;
29611 +
29612 +       seq_puts(seq, ",xino=");
29613 +       err = au_xino_path(seq, f);
29614 +
29615 +out:
29616 +       return err;
29617 +#endif
29618 +}
29619 +
29620 +/* seq_file will re-call me in case of too long string */
29621 +static int aufs_show_options(struct seq_file *m, struct dentry *dentry)
29622 +{
29623 +       int err;
29624 +       unsigned int mnt_flags, v;
29625 +       struct super_block *sb;
29626 +       struct au_sbinfo *sbinfo;
29627 +
29628 +#define AuBool(name, str) do { \
29629 +       v = au_opt_test(mnt_flags, name); \
29630 +       if (v != au_opt_test(AuOpt_Def, name)) \
29631 +               seq_printf(m, ",%s" #str, v ? "" : "no"); \
29632 +} while (0)
29633 +
29634 +#define AuStr(name, str) do { \
29635 +       v = mnt_flags & AuOptMask_##name; \
29636 +       if (v != (AuOpt_Def & AuOptMask_##name)) \
29637 +               seq_printf(m, "," #str "=%s", au_optstr_##str(v)); \
29638 +} while (0)
29639 +
29640 +#define AuUInt(name, str, val) do { \
29641 +       if (val != AUFS_##name##_DEF) \
29642 +               seq_printf(m, "," #str "=%u", val); \
29643 +} while (0)
29644 +
29645 +       sb = dentry->d_sb;
29646 +       if (sb->s_flags & SB_POSIXACL)
29647 +               seq_puts(m, ",acl");
29648 +#if 0 /* reserved for future use */
29649 +       if (sb->s_flags & SB_I_VERSION)
29650 +               seq_puts(m, ",i_version");
29651 +#endif
29652 +
29653 +       /* lock free root dinfo */
29654 +       si_noflush_read_lock(sb);
29655 +       sbinfo = au_sbi(sb);
29656 +       seq_printf(m, ",si=%lx", sysaufs_si_id(sbinfo));
29657 +
29658 +       mnt_flags = au_mntflags(sb);
29659 +       if (au_opt_test(mnt_flags, XINO)) {
29660 +               err = au_show_xino(m, sb);
29661 +               if (unlikely(err))
29662 +                       goto out;
29663 +       } else
29664 +               seq_puts(m, ",noxino");
29665 +
29666 +       AuBool(TRUNC_XINO, trunc_xino);
29667 +       AuStr(UDBA, udba);
29668 +       AuBool(SHWH, shwh);
29669 +       AuBool(PLINK, plink);
29670 +       AuBool(DIO, dio);
29671 +       AuBool(DIRPERM1, dirperm1);
29672 +
29673 +       v = sbinfo->si_wbr_create;
29674 +       if (v != AuWbrCreate_Def)
29675 +               au_show_wbr_create(m, v, sbinfo);
29676 +
29677 +       v = sbinfo->si_wbr_copyup;
29678 +       if (v != AuWbrCopyup_Def)
29679 +               seq_printf(m, ",cpup=%s", au_optstr_wbr_copyup(v));
29680 +
29681 +       v = au_opt_test(mnt_flags, ALWAYS_DIROPQ);
29682 +       if (v != au_opt_test(AuOpt_Def, ALWAYS_DIROPQ))
29683 +               seq_printf(m, ",diropq=%c", v ? 'a' : 'w');
29684 +
29685 +       AuUInt(DIRWH, dirwh, sbinfo->si_dirwh);
29686 +
29687 +       v = jiffies_to_msecs(sbinfo->si_rdcache) / MSEC_PER_SEC;
29688 +       AuUInt(RDCACHE, rdcache, v);
29689 +
29690 +       AuUInt(RDBLK, rdblk, sbinfo->si_rdblk);
29691 +       AuUInt(RDHASH, rdhash, sbinfo->si_rdhash);
29692 +
29693 +       au_fhsm_show(m, sbinfo);
29694 +
29695 +       AuBool(DIRREN, dirren);
29696 +       AuBool(SUM, sum);
29697 +       /* AuBool(SUM_W, wsum); */
29698 +       AuBool(WARN_PERM, warn_perm);
29699 +       AuBool(VERBOSE, verbose);
29700 +
29701 +out:
29702 +       /* be sure to print "br:" last */
29703 +       if (!sysaufs_brs) {
29704 +               seq_puts(m, ",br:");
29705 +               au_show_brs(m, sb);
29706 +       }
29707 +       si_read_unlock(sb);
29708 +       return 0;
29709 +
29710 +#undef AuBool
29711 +#undef AuStr
29712 +#undef AuUInt
29713 +}
29714 +
29715 +/* ---------------------------------------------------------------------- */
29716 +
29717 +/* sum mode which returns the summation for statfs(2) */
29718 +
29719 +static u64 au_add_till_max(u64 a, u64 b)
29720 +{
29721 +       u64 old;
29722 +
29723 +       old = a;
29724 +       a += b;
29725 +       if (old <= a)
29726 +               return a;
29727 +       return ULLONG_MAX;
29728 +}
29729 +
29730 +static u64 au_mul_till_max(u64 a, long mul)
29731 +{
29732 +       u64 old;
29733 +
29734 +       old = a;
29735 +       a *= mul;
29736 +       if (old <= a)
29737 +               return a;
29738 +       return ULLONG_MAX;
29739 +}
29740 +
29741 +static int au_statfs_sum(struct super_block *sb, struct kstatfs *buf)
29742 +{
29743 +       int err;
29744 +       long bsize, factor;
29745 +       u64 blocks, bfree, bavail, files, ffree;
29746 +       aufs_bindex_t bbot, bindex, i;
29747 +       unsigned char shared;
29748 +       struct path h_path;
29749 +       struct super_block *h_sb;
29750 +
29751 +       err = 0;
29752 +       bsize = LONG_MAX;
29753 +       files = 0;
29754 +       ffree = 0;
29755 +       blocks = 0;
29756 +       bfree = 0;
29757 +       bavail = 0;
29758 +       bbot = au_sbbot(sb);
29759 +       for (bindex = 0; bindex <= bbot; bindex++) {
29760 +               h_path.mnt = au_sbr_mnt(sb, bindex);
29761 +               h_sb = h_path.mnt->mnt_sb;
29762 +               shared = 0;
29763 +               for (i = 0; !shared && i < bindex; i++)
29764 +                       shared = (au_sbr_sb(sb, i) == h_sb);
29765 +               if (shared)
29766 +                       continue;
29767 +
29768 +               /* sb->s_root for NFS is unreliable */
29769 +               h_path.dentry = h_path.mnt->mnt_root;
29770 +               err = vfs_statfs(&h_path, buf);
29771 +               if (unlikely(err))
29772 +                       goto out;
29773 +
29774 +               if (bsize > buf->f_bsize) {
29775 +                       /*
29776 +                        * we will reduce bsize, so we have to expand blocks
29777 +                        * etc. to match them again
29778 +                        */
29779 +                       factor = (bsize / buf->f_bsize);
29780 +                       blocks = au_mul_till_max(blocks, factor);
29781 +                       bfree = au_mul_till_max(bfree, factor);
29782 +                       bavail = au_mul_till_max(bavail, factor);
29783 +                       bsize = buf->f_bsize;
29784 +               }
29785 +
29786 +               factor = (buf->f_bsize / bsize);
29787 +               blocks = au_add_till_max(blocks,
29788 +                               au_mul_till_max(buf->f_blocks, factor));
29789 +               bfree = au_add_till_max(bfree,
29790 +                               au_mul_till_max(buf->f_bfree, factor));
29791 +               bavail = au_add_till_max(bavail,
29792 +                               au_mul_till_max(buf->f_bavail, factor));
29793 +               files = au_add_till_max(files, buf->f_files);
29794 +               ffree = au_add_till_max(ffree, buf->f_ffree);
29795 +       }
29796 +
29797 +       buf->f_bsize = bsize;
29798 +       buf->f_blocks = blocks;
29799 +       buf->f_bfree = bfree;
29800 +       buf->f_bavail = bavail;
29801 +       buf->f_files = files;
29802 +       buf->f_ffree = ffree;
29803 +       buf->f_frsize = 0;
29804 +
29805 +out:
29806 +       return err;
29807 +}
29808 +
29809 +static int aufs_statfs(struct dentry *dentry, struct kstatfs *buf)
29810 +{
29811 +       int err;
29812 +       struct path h_path;
29813 +       struct super_block *sb;
29814 +
29815 +       /* lock free root dinfo */
29816 +       sb = dentry->d_sb;
29817 +       si_noflush_read_lock(sb);
29818 +       if (!au_opt_test(au_mntflags(sb), SUM)) {
29819 +               /* sb->s_root for NFS is unreliable */
29820 +               h_path.mnt = au_sbr_mnt(sb, 0);
29821 +               h_path.dentry = h_path.mnt->mnt_root;
29822 +               err = vfs_statfs(&h_path, buf);
29823 +       } else
29824 +               err = au_statfs_sum(sb, buf);
29825 +       si_read_unlock(sb);
29826 +
29827 +       if (!err) {
29828 +               buf->f_type = AUFS_SUPER_MAGIC;
29829 +               buf->f_namelen = AUFS_MAX_NAMELEN;
29830 +               memset(&buf->f_fsid, 0, sizeof(buf->f_fsid));
29831 +       }
29832 +       /* buf->f_bsize = buf->f_blocks = buf->f_bfree = buf->f_bavail = -1; */
29833 +
29834 +       return err;
29835 +}
29836 +
29837 +/* ---------------------------------------------------------------------- */
29838 +
29839 +static int aufs_sync_fs(struct super_block *sb, int wait)
29840 +{
29841 +       int err, e;
29842 +       aufs_bindex_t bbot, bindex;
29843 +       struct au_branch *br;
29844 +       struct super_block *h_sb;
29845 +
29846 +       err = 0;
29847 +       si_noflush_read_lock(sb);
29848 +       bbot = au_sbbot(sb);
29849 +       for (bindex = 0; bindex <= bbot; bindex++) {
29850 +               br = au_sbr(sb, bindex);
29851 +               if (!au_br_writable(br->br_perm))
29852 +                       continue;
29853 +
29854 +               h_sb = au_sbr_sb(sb, bindex);
29855 +               e = vfsub_sync_filesystem(h_sb, wait);
29856 +               if (unlikely(e && !err))
29857 +                       err = e;
29858 +               /* go on even if an error happens */
29859 +       }
29860 +       si_read_unlock(sb);
29861 +
29862 +       return err;
29863 +}
29864 +
29865 +/* ---------------------------------------------------------------------- */
29866 +
29867 +/* final actions when unmounting a file system */
29868 +static void aufs_put_super(struct super_block *sb)
29869 +{
29870 +       struct au_sbinfo *sbinfo;
29871 +
29872 +       sbinfo = au_sbi(sb);
29873 +       if (sbinfo)
29874 +               kobject_put(&sbinfo->si_kobj);
29875 +}
29876 +
29877 +/* ---------------------------------------------------------------------- */
29878 +
29879 +void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb,
29880 +                    struct super_block *sb, void *arg)
29881 +{
29882 +       void *array;
29883 +       unsigned long long n, sz;
29884 +
29885 +       array = NULL;
29886 +       n = 0;
29887 +       if (!*hint)
29888 +               goto out;
29889 +
29890 +       if (*hint > ULLONG_MAX / sizeof(array)) {
29891 +               array = ERR_PTR(-EMFILE);
29892 +               pr_err("hint %llu\n", *hint);
29893 +               goto out;
29894 +       }
29895 +
29896 +       sz = sizeof(array) * *hint;
29897 +       array = kzalloc(sz, GFP_NOFS);
29898 +       if (unlikely(!array))
29899 +               array = vzalloc(sz);
29900 +       if (unlikely(!array)) {
29901 +               array = ERR_PTR(-ENOMEM);
29902 +               goto out;
29903 +       }
29904 +
29905 +       n = cb(sb, array, *hint, arg);
29906 +       AuDebugOn(n > *hint);
29907 +
29908 +out:
29909 +       *hint = n;
29910 +       return array;
29911 +}
29912 +
29913 +static unsigned long long au_iarray_cb(struct super_block *sb, void *a,
29914 +                                      unsigned long long max __maybe_unused,
29915 +                                      void *arg)
29916 +{
29917 +       unsigned long long n;
29918 +       struct inode **p, *inode;
29919 +       struct list_head *head;
29920 +
29921 +       n = 0;
29922 +       p = a;
29923 +       head = arg;
29924 +       spin_lock(&sb->s_inode_list_lock);
29925 +       list_for_each_entry(inode, head, i_sb_list) {
29926 +               if (!au_is_bad_inode(inode)
29927 +                   && au_ii(inode)->ii_btop >= 0) {
29928 +                       spin_lock(&inode->i_lock);
29929 +                       if (atomic_read(&inode->i_count)) {
29930 +                               au_igrab(inode);
29931 +                               *p++ = inode;
29932 +                               n++;
29933 +                               AuDebugOn(n > max);
29934 +                       }
29935 +                       spin_unlock(&inode->i_lock);
29936 +               }
29937 +       }
29938 +       spin_unlock(&sb->s_inode_list_lock);
29939 +
29940 +       return n;
29941 +}
29942 +
29943 +struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max)
29944 +{
29945 +       struct au_sbinfo *sbi;
29946 +
29947 +       sbi = au_sbi(sb);
29948 +       *max = au_lcnt_read(&sbi->si_ninodes, /*do_rev*/1);
29949 +       return au_array_alloc(max, au_iarray_cb, sb, &sb->s_inodes);
29950 +}
29951 +
29952 +void au_iarray_free(struct inode **a, unsigned long long max)
29953 +{
29954 +       unsigned long long ull;
29955 +
29956 +       for (ull = 0; ull < max; ull++)
29957 +               iput(a[ull]);
29958 +       kvfree(a);
29959 +}
29960 +
29961 +/* ---------------------------------------------------------------------- */
29962 +
29963 +/*
29964 + * refresh dentry and inode at remount time.
29965 + */
29966 +/* todo: consolidate with simple_reval_dpath() and au_reval_for_attr() */
29967 +static int au_do_refresh(struct dentry *dentry, unsigned int dir_flags,
29968 +                     struct dentry *parent)
29969 +{
29970 +       int err;
29971 +
29972 +       di_write_lock_child(dentry);
29973 +       di_read_lock_parent(parent, AuLock_IR);
29974 +       err = au_refresh_dentry(dentry, parent);
29975 +       if (!err && dir_flags)
29976 +               au_hn_reset(d_inode(dentry), dir_flags);
29977 +       di_read_unlock(parent, AuLock_IR);
29978 +       di_write_unlock(dentry);
29979 +
29980 +       return err;
29981 +}
29982 +
29983 +static int au_do_refresh_d(struct dentry *dentry, unsigned int sigen,
29984 +                          struct au_sbinfo *sbinfo,
29985 +                          const unsigned int dir_flags, unsigned int do_idop)
29986 +{
29987 +       int err;
29988 +       struct dentry *parent;
29989 +
29990 +       err = 0;
29991 +       parent = dget_parent(dentry);
29992 +       if (!au_digen_test(parent, sigen) && au_digen_test(dentry, sigen)) {
29993 +               if (d_really_is_positive(dentry)) {
29994 +                       if (!d_is_dir(dentry))
29995 +                               err = au_do_refresh(dentry, /*dir_flags*/0,
29996 +                                                parent);
29997 +                       else {
29998 +                               err = au_do_refresh(dentry, dir_flags, parent);
29999 +                               if (unlikely(err))
30000 +                                       au_fset_si(sbinfo, FAILED_REFRESH_DIR);
30001 +                       }
30002 +               } else
30003 +                       err = au_do_refresh(dentry, /*dir_flags*/0, parent);
30004 +               AuDbgDentry(dentry);
30005 +       }
30006 +       dput(parent);
30007 +
30008 +       if (!err) {
30009 +               if (do_idop)
30010 +                       au_refresh_dop(dentry, /*force_reval*/0);
30011 +       } else
30012 +               au_refresh_dop(dentry, /*force_reval*/1);
30013 +
30014 +       AuTraceErr(err);
30015 +       return err;
30016 +}
30017 +
30018 +static int au_refresh_d(struct super_block *sb, unsigned int do_idop)
30019 +{
30020 +       int err, i, j, ndentry, e;
30021 +       unsigned int sigen;
30022 +       struct au_dcsub_pages dpages;
30023 +       struct au_dpage *dpage;
30024 +       struct dentry **dentries, *d;
30025 +       struct au_sbinfo *sbinfo;
30026 +       struct dentry *root = sb->s_root;
30027 +       const unsigned int dir_flags = au_hi_flags(d_inode(root), /*isdir*/1);
30028 +
30029 +       if (do_idop)
30030 +               au_refresh_dop(root, /*force_reval*/0);
30031 +
30032 +       err = au_dpages_init(&dpages, GFP_NOFS);
30033 +       if (unlikely(err))
30034 +               goto out;
30035 +       err = au_dcsub_pages(&dpages, root, NULL, NULL);
30036 +       if (unlikely(err))
30037 +               goto out_dpages;
30038 +
30039 +       sigen = au_sigen(sb);
30040 +       sbinfo = au_sbi(sb);
30041 +       for (i = 0; i < dpages.ndpage; i++) {
30042 +               dpage = dpages.dpages + i;
30043 +               dentries = dpage->dentries;
30044 +               ndentry = dpage->ndentry;
30045 +               for (j = 0; j < ndentry; j++) {
30046 +                       d = dentries[j];
30047 +                       e = au_do_refresh_d(d, sigen, sbinfo, dir_flags,
30048 +                                           do_idop);
30049 +                       if (unlikely(e && !err))
30050 +                               err = e;
30051 +                       /* go on even err */
30052 +               }
30053 +       }
30054 +
30055 +out_dpages:
30056 +       au_dpages_free(&dpages);
30057 +out:
30058 +       return err;
30059 +}
30060 +
30061 +static int au_refresh_i(struct super_block *sb, unsigned int do_idop)
30062 +{
30063 +       int err, e;
30064 +       unsigned int sigen;
30065 +       unsigned long long max, ull;
30066 +       struct inode *inode, **array;
30067 +
30068 +       array = au_iarray_alloc(sb, &max);
30069 +       err = PTR_ERR(array);
30070 +       if (IS_ERR(array))
30071 +               goto out;
30072 +
30073 +       err = 0;
30074 +       sigen = au_sigen(sb);
30075 +       for (ull = 0; ull < max; ull++) {
30076 +               inode = array[ull];
30077 +               if (unlikely(!inode))
30078 +                       break;
30079 +
30080 +               e = 0;
30081 +               ii_write_lock_child(inode);
30082 +               if (au_iigen(inode, NULL) != sigen) {
30083 +                       e = au_refresh_hinode_self(inode);
30084 +                       if (unlikely(e)) {
30085 +                               au_refresh_iop(inode, /*force_getattr*/1);
30086 +                               pr_err("error %d, i%lu\n", e, inode->i_ino);
30087 +                               if (!err)
30088 +                                       err = e;
30089 +                               /* go on even if err */
30090 +                       }
30091 +               }
30092 +               if (!e && do_idop)
30093 +                       au_refresh_iop(inode, /*force_getattr*/0);
30094 +               ii_write_unlock(inode);
30095 +       }
30096 +
30097 +       au_iarray_free(array, max);
30098 +
30099 +out:
30100 +       return err;
30101 +}
30102 +
30103 +static void au_remount_refresh(struct super_block *sb, unsigned int do_idop)
30104 +{
30105 +       int err, e;
30106 +       unsigned int udba;
30107 +       aufs_bindex_t bindex, bbot;
30108 +       struct dentry *root;
30109 +       struct inode *inode;
30110 +       struct au_branch *br;
30111 +       struct au_sbinfo *sbi;
30112 +
30113 +       au_sigen_inc(sb);
30114 +       sbi = au_sbi(sb);
30115 +       au_fclr_si(sbi, FAILED_REFRESH_DIR);
30116 +
30117 +       root = sb->s_root;
30118 +       DiMustNoWaiters(root);
30119 +       inode = d_inode(root);
30120 +       IiMustNoWaiters(inode);
30121 +
30122 +       udba = au_opt_udba(sb);
30123 +       bbot = au_sbbot(sb);
30124 +       for (bindex = 0; bindex <= bbot; bindex++) {
30125 +               br = au_sbr(sb, bindex);
30126 +               err = au_hnotify_reset_br(udba, br, br->br_perm);
30127 +               if (unlikely(err))
30128 +                       AuIOErr("hnotify failed on br %d, %d, ignored\n",
30129 +                               bindex, err);
30130 +               /* go on even if err */
30131 +       }
30132 +       au_hn_reset(inode, au_hi_flags(inode, /*isdir*/1));
30133 +
30134 +       if (do_idop) {
30135 +               if (au_ftest_si(sbi, NO_DREVAL)) {
30136 +                       AuDebugOn(sb->s_d_op == &aufs_dop_noreval);
30137 +                       sb->s_d_op = &aufs_dop_noreval;
30138 +                       AuDebugOn(sbi->si_iop_array == aufs_iop_nogetattr);
30139 +                       sbi->si_iop_array = aufs_iop_nogetattr;
30140 +               } else {
30141 +                       AuDebugOn(sb->s_d_op == &aufs_dop);
30142 +                       sb->s_d_op = &aufs_dop;
30143 +                       AuDebugOn(sbi->si_iop_array == aufs_iop);
30144 +                       sbi->si_iop_array = aufs_iop;
30145 +               }
30146 +               pr_info("reset to %ps and %ps\n",
30147 +                       sb->s_d_op, sbi->si_iop_array);
30148 +       }
30149 +
30150 +       di_write_unlock(root);
30151 +       err = au_refresh_d(sb, do_idop);
30152 +       e = au_refresh_i(sb, do_idop);
30153 +       if (unlikely(e && !err))
30154 +               err = e;
30155 +       /* aufs_write_lock() calls ..._child() */
30156 +       di_write_lock_child(root);
30157 +
30158 +       au_cpup_attr_all(inode, /*force*/1);
30159 +
30160 +       if (unlikely(err))
30161 +               AuIOErr("refresh failed, ignored, %d\n", err);
30162 +}
30163 +
30164 +/* stop extra interpretation of errno in mount(8), and strange error messages */
30165 +static int cvt_err(int err)
30166 +{
30167 +       AuTraceErr(err);
30168 +
30169 +       switch (err) {
30170 +       case -ENOENT:
30171 +       case -ENOTDIR:
30172 +       case -EEXIST:
30173 +       case -EIO:
30174 +               err = -EINVAL;
30175 +       }
30176 +       return err;
30177 +}
30178 +
30179 +static int aufs_remount_fs(struct super_block *sb, int *flags, char *data)
30180 +{
30181 +       int err, do_dx;
30182 +       unsigned int mntflags;
30183 +       struct au_opts opts = {
30184 +               .opt = NULL
30185 +       };
30186 +       struct dentry *root;
30187 +       struct inode *inode;
30188 +       struct au_sbinfo *sbinfo;
30189 +
30190 +       err = 0;
30191 +       root = sb->s_root;
30192 +       if (!data || !*data) {
30193 +               err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
30194 +               if (!err) {
30195 +                       di_write_lock_child(root);
30196 +                       err = au_opts_verify(sb, *flags, /*pending*/0);
30197 +                       aufs_write_unlock(root);
30198 +               }
30199 +               goto out;
30200 +       }
30201 +
30202 +       err = -ENOMEM;
30203 +       opts.opt = (void *)__get_free_page(GFP_NOFS);
30204 +       if (unlikely(!opts.opt))
30205 +               goto out;
30206 +       opts.max_opt = PAGE_SIZE / sizeof(*opts.opt);
30207 +       opts.flags = AuOpts_REMOUNT;
30208 +       opts.sb_flags = *flags;
30209 +
30210 +       /* parse it before aufs lock */
30211 +       err = au_opts_parse(sb, data, &opts);
30212 +       if (unlikely(err))
30213 +               goto out_opts;
30214 +
30215 +       sbinfo = au_sbi(sb);
30216 +       inode = d_inode(root);
30217 +       inode_lock(inode);
30218 +       err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
30219 +       if (unlikely(err))
30220 +               goto out_mtx;
30221 +       di_write_lock_child(root);
30222 +
30223 +       /* au_opts_remount() may return an error */
30224 +       err = au_opts_remount(sb, &opts);
30225 +       au_opts_free(&opts);
30226 +
30227 +       if (au_ftest_opts(opts.flags, REFRESH))
30228 +               au_remount_refresh(sb, au_ftest_opts(opts.flags, REFRESH_IDOP));
30229 +
30230 +       if (au_ftest_opts(opts.flags, REFRESH_DYAOP)) {
30231 +               mntflags = au_mntflags(sb);
30232 +               do_dx = !!au_opt_test(mntflags, DIO);
30233 +               au_dy_arefresh(do_dx);
30234 +       }
30235 +
30236 +       au_fhsm_wrote_all(sb, /*force*/1); /* ?? */
30237 +       aufs_write_unlock(root);
30238 +
30239 +out_mtx:
30240 +       inode_unlock(inode);
30241 +out_opts:
30242 +       free_page((unsigned long)opts.opt);
30243 +out:
30244 +       err = cvt_err(err);
30245 +       AuTraceErr(err);
30246 +       return err;
30247 +}
30248 +
30249 +static const struct super_operations aufs_sop = {
30250 +       .alloc_inode    = aufs_alloc_inode,
30251 +       .destroy_inode  = aufs_destroy_inode,
30252 +       .free_inode     = aufs_free_inode,
30253 +       /* always deleting, no clearing */
30254 +       .drop_inode     = generic_delete_inode,
30255 +       .show_options   = aufs_show_options,
30256 +       .statfs         = aufs_statfs,
30257 +       .put_super      = aufs_put_super,
30258 +       .sync_fs        = aufs_sync_fs,
30259 +       .remount_fs     = aufs_remount_fs
30260 +};
30261 +
30262 +/* ---------------------------------------------------------------------- */
30263 +
30264 +static int alloc_root(struct super_block *sb)
30265 +{
30266 +       int err;
30267 +       struct inode *inode;
30268 +       struct dentry *root;
30269 +
30270 +       err = -ENOMEM;
30271 +       inode = au_iget_locked(sb, AUFS_ROOT_INO);
30272 +       err = PTR_ERR(inode);
30273 +       if (IS_ERR(inode))
30274 +               goto out;
30275 +
30276 +       inode->i_op = aufs_iop + AuIop_DIR; /* with getattr by default */
30277 +       inode->i_fop = &aufs_dir_fop;
30278 +       inode->i_mode = S_IFDIR;
30279 +       set_nlink(inode, 2);
30280 +       unlock_new_inode(inode);
30281 +
30282 +       root = d_make_root(inode);
30283 +       if (unlikely(!root))
30284 +               goto out;
30285 +       err = PTR_ERR(root);
30286 +       if (IS_ERR(root))
30287 +               goto out;
30288 +
30289 +       err = au_di_init(root);
30290 +       if (!err) {
30291 +               sb->s_root = root;
30292 +               return 0; /* success */
30293 +       }
30294 +       dput(root);
30295 +
30296 +out:
30297 +       return err;
30298 +}
30299 +
30300 +static int aufs_fill_super(struct super_block *sb, void *raw_data,
30301 +                          int silent __maybe_unused)
30302 +{
30303 +       int err;
30304 +       struct au_opts opts = {
30305 +               .opt = NULL
30306 +       };
30307 +       struct au_sbinfo *sbinfo;
30308 +       struct dentry *root;
30309 +       struct inode *inode;
30310 +       char *arg = raw_data;
30311 +
30312 +       if (unlikely(!arg || !*arg)) {
30313 +               err = -EINVAL;
30314 +               pr_err("no arg\n");
30315 +               goto out;
30316 +       }
30317 +
30318 +       err = -ENOMEM;
30319 +       opts.opt = (void *)__get_free_page(GFP_NOFS);
30320 +       if (unlikely(!opts.opt))
30321 +               goto out;
30322 +       opts.max_opt = PAGE_SIZE / sizeof(*opts.opt);
30323 +       opts.sb_flags = sb->s_flags;
30324 +
30325 +       err = au_si_alloc(sb);
30326 +       if (unlikely(err))
30327 +               goto out_opts;
30328 +       sbinfo = au_sbi(sb);
30329 +
30330 +       /* all timestamps always follow the ones on the branch */
30331 +       sb->s_flags |= SB_NOATIME | SB_NODIRATIME;
30332 +       sb->s_flags |= SB_I_VERSION; /* do we really need this? */
30333 +       sb->s_op = &aufs_sop;
30334 +       sb->s_d_op = &aufs_dop;
30335 +       sb->s_magic = AUFS_SUPER_MAGIC;
30336 +       sb->s_maxbytes = 0;
30337 +       sb->s_stack_depth = 1;
30338 +       au_export_init(sb);
30339 +       au_xattr_init(sb);
30340 +
30341 +       err = alloc_root(sb);
30342 +       if (unlikely(err)) {
30343 +               si_write_unlock(sb);
30344 +               goto out_info;
30345 +       }
30346 +       root = sb->s_root;
30347 +       inode = d_inode(root);
30348 +
30349 +       /*
30350 +        * actually we can parse options regardless aufs lock here.
30351 +        * but at remount time, parsing must be done before aufs lock.
30352 +        * so we follow the same rule.
30353 +        */
30354 +       ii_write_lock_parent(inode);
30355 +       aufs_write_unlock(root);
30356 +       err = au_opts_parse(sb, arg, &opts);
30357 +       if (unlikely(err))
30358 +               goto out_root;
30359 +
30360 +       /* lock vfs_inode first, then aufs. */
30361 +       inode_lock(inode);
30362 +       aufs_write_lock(root);
30363 +       err = au_opts_mount(sb, &opts);
30364 +       au_opts_free(&opts);
30365 +       if (!err && au_ftest_si(sbinfo, NO_DREVAL)) {
30366 +               sb->s_d_op = &aufs_dop_noreval;
30367 +               pr_info("%ps\n", sb->s_d_op);
30368 +               au_refresh_dop(root, /*force_reval*/0);
30369 +               sbinfo->si_iop_array = aufs_iop_nogetattr;
30370 +               au_refresh_iop(inode, /*force_getattr*/0);
30371 +       }
30372 +       aufs_write_unlock(root);
30373 +       inode_unlock(inode);
30374 +       if (!err)
30375 +               goto out_opts; /* success */
30376 +
30377 +out_root:
30378 +       dput(root);
30379 +       sb->s_root = NULL;
30380 +out_info:
30381 +       kobject_put(&sbinfo->si_kobj);
30382 +       sb->s_fs_info = NULL;
30383 +out_opts:
30384 +       free_page((unsigned long)opts.opt);
30385 +out:
30386 +       AuTraceErr(err);
30387 +       err = cvt_err(err);
30388 +       AuTraceErr(err);
30389 +       return err;
30390 +}
30391 +
30392 +/* ---------------------------------------------------------------------- */
30393 +
30394 +static struct dentry *aufs_mount(struct file_system_type *fs_type, int flags,
30395 +                                const char *dev_name __maybe_unused,
30396 +                                void *raw_data)
30397 +{
30398 +       struct dentry *root;
30399 +
30400 +       /* all timestamps always follow the ones on the branch */
30401 +       /* mnt->mnt_flags |= MNT_NOATIME | MNT_NODIRATIME; */
30402 +       root = mount_nodev(fs_type, flags, raw_data, aufs_fill_super);
30403 +       if (IS_ERR(root))
30404 +               goto out;
30405 +
30406 +       au_sbilist_add(root->d_sb);
30407 +
30408 +out:
30409 +       return root;
30410 +}
30411 +
30412 +static void aufs_kill_sb(struct super_block *sb)
30413 +{
30414 +       struct au_sbinfo *sbinfo;
30415 +
30416 +       sbinfo = au_sbi(sb);
30417 +       if (sbinfo) {
30418 +               au_sbilist_del(sb);
30419 +               aufs_write_lock(sb->s_root);
30420 +               au_fhsm_fin(sb);
30421 +               if (sbinfo->si_wbr_create_ops->fin)
30422 +                       sbinfo->si_wbr_create_ops->fin(sb);
30423 +               if (au_opt_test(sbinfo->si_mntflags, UDBA_HNOTIFY)) {
30424 +                       au_opt_set_udba(sbinfo->si_mntflags, UDBA_NONE);
30425 +                       au_remount_refresh(sb, /*do_idop*/0);
30426 +               }
30427 +               if (au_opt_test(sbinfo->si_mntflags, PLINK))
30428 +                       au_plink_put(sb, /*verbose*/1);
30429 +               au_xino_clr(sb);
30430 +               au_dr_opt_flush(sb);
30431 +               sbinfo->si_sb = NULL;
30432 +               aufs_write_unlock(sb->s_root);
30433 +               au_nwt_flush(&sbinfo->si_nowait);
30434 +       }
30435 +       kill_anon_super(sb);
30436 +}
30437 +
30438 +struct file_system_type aufs_fs_type = {
30439 +       .name           = AUFS_FSTYPE,
30440 +       /* a race between rename and others */
30441 +       .fs_flags       = FS_RENAME_DOES_D_MOVE
30442 +                               /* untested */
30443 +                               /*| FS_ALLOW_IDMAP*/
30444 +                               ,
30445 +       .mount          = aufs_mount,
30446 +       .kill_sb        = aufs_kill_sb,
30447 +       /* no need to __module_get() and module_put(). */
30448 +       .owner          = THIS_MODULE,
30449 +};
30450 diff -urN /usr/share/empty/fs/aufs/super.h linux/fs/aufs/super.h
30451 --- /usr/share/empty/fs/aufs/super.h    1970-01-01 01:00:00.000000000 +0100
30452 +++ linux/fs/aufs/super.h       2021-11-01 23:48:34.213025928 +0100
30453 @@ -0,0 +1,587 @@
30454 +/* SPDX-License-Identifier: GPL-2.0 */
30455 +/*
30456 + * Copyright (C) 2005-2021 Junjiro R. Okajima
30457 + *
30458 + * This program, aufs is free software; you can redistribute it and/or modify
30459 + * it under the terms of the GNU General Public License as published by
30460 + * the Free Software Foundation; either version 2 of the License, or
30461 + * (at your option) any later version.
30462 + *
30463 + * This program is distributed in the hope that it will be useful,
30464 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
30465 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30466 + * GNU General Public License for more details.
30467 + *
30468 + * You should have received a copy of the GNU General Public License
30469 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
30470 + */
30471 +
30472 +/*
30473 + * super_block operations
30474 + */
30475 +
30476 +#ifndef __AUFS_SUPER_H__
30477 +#define __AUFS_SUPER_H__
30478 +
30479 +#ifdef __KERNEL__
30480 +
30481 +#include <linux/fs.h>
30482 +#include <linux/kobject.h>
30483 +#include "hbl.h"
30484 +#include "lcnt.h"
30485 +#include "rwsem.h"
30486 +#include "wkq.h"
30487 +
30488 +/* policies to select one among multiple writable branches */
30489 +struct au_wbr_copyup_operations {
30490 +       int (*copyup)(struct dentry *dentry);
30491 +};
30492 +
30493 +#define AuWbr_DIR      1               /* target is a dir */
30494 +#define AuWbr_PARENT   (1 << 1)        /* always require a parent */
30495 +
30496 +#define au_ftest_wbr(flags, name)      ((flags) & AuWbr_##name)
30497 +#define au_fset_wbr(flags, name)       { (flags) |= AuWbr_##name; }
30498 +#define au_fclr_wbr(flags, name)       { (flags) &= ~AuWbr_##name; }
30499 +
30500 +struct au_wbr_create_operations {
30501 +       int (*create)(struct dentry *dentry, unsigned int flags);
30502 +       int (*init)(struct super_block *sb);
30503 +       int (*fin)(struct super_block *sb);
30504 +};
30505 +
30506 +struct au_wbr_mfs {
30507 +       struct mutex    mfs_lock; /* protect this structure */
30508 +       unsigned long   mfs_jiffy;
30509 +       unsigned long   mfs_expire;
30510 +       aufs_bindex_t   mfs_bindex;
30511 +
30512 +       unsigned long long      mfsrr_bytes;
30513 +       unsigned long long      mfsrr_watermark;
30514 +};
30515 +
30516 +#define AuPlink_NHASH 100
30517 +static inline int au_plink_hash(ino_t ino)
30518 +{
30519 +       return ino % AuPlink_NHASH;
30520 +}
30521 +
30522 +/* File-based Hierarchical Storage Management */
30523 +struct au_fhsm {
30524 +#ifdef CONFIG_AUFS_FHSM
30525 +       /* allow only one process who can receive the notification */
30526 +       spinlock_t              fhsm_spin;
30527 +       pid_t                   fhsm_pid;
30528 +       wait_queue_head_t       fhsm_wqh;
30529 +       atomic_t                fhsm_readable;
30530 +
30531 +       /* these are protected by si_rwsem */
30532 +       unsigned long           fhsm_expire;
30533 +       aufs_bindex_t           fhsm_bottom;
30534 +#endif
30535 +};
30536 +
30537 +struct au_branch;
30538 +struct au_sbinfo {
30539 +       /* nowait tasks in the system-wide workqueue */
30540 +       struct au_nowait_tasks  si_nowait;
30541 +
30542 +       /*
30543 +        * tried sb->s_umount, but failed due to the dependency between i_mutex.
30544 +        * rwsem for au_sbinfo is necessary.
30545 +        */
30546 +       struct au_rwsem         si_rwsem;
30547 +
30548 +       /*
30549 +        * dirty approach to protect sb->sb_inodes and ->s_files (gone) from
30550 +        * remount.
30551 +        */
30552 +       au_lcnt_t               si_ninodes, si_nfiles;
30553 +
30554 +       /* branch management */
30555 +       unsigned int            si_generation;
30556 +
30557 +       /* see AuSi_ flags */
30558 +       unsigned char           au_si_status;
30559 +
30560 +       aufs_bindex_t           si_bbot;
30561 +
30562 +       /* dirty trick to keep br_id plus */
30563 +       unsigned int            si_last_br_id :
30564 +                               sizeof(aufs_bindex_t) * BITS_PER_BYTE - 1;
30565 +       struct au_branch        **si_branch;
30566 +
30567 +       /* policy to select a writable branch */
30568 +       unsigned char           si_wbr_copyup;
30569 +       unsigned char           si_wbr_create;
30570 +       struct au_wbr_copyup_operations *si_wbr_copyup_ops;
30571 +       struct au_wbr_create_operations *si_wbr_create_ops;
30572 +
30573 +       /* round robin */
30574 +       atomic_t                si_wbr_rr_next;
30575 +
30576 +       /* most free space */
30577 +       struct au_wbr_mfs       si_wbr_mfs;
30578 +
30579 +       /* File-based Hierarchical Storage Management */
30580 +       struct au_fhsm          si_fhsm;
30581 +
30582 +       /* mount flags */
30583 +       /* include/asm-ia64/siginfo.h defines a macro named si_flags */
30584 +       unsigned int            si_mntflags;
30585 +
30586 +       /* external inode number (bitmap and translation table) */
30587 +       loff_t                  si_ximaxent;    /* max entries in a xino */
30588 +
30589 +       struct file             *si_xib;
30590 +       struct mutex            si_xib_mtx; /* protect xib members */
30591 +       unsigned long           *si_xib_buf;
30592 +       unsigned long           si_xib_last_pindex;
30593 +       int                     si_xib_next_bit;
30594 +
30595 +       unsigned long           si_xino_jiffy;
30596 +       unsigned long           si_xino_expire;
30597 +       /* reserved for future use */
30598 +       /* unsigned long long   si_xib_limit; */        /* Max xib file size */
30599 +
30600 +#ifdef CONFIG_AUFS_EXPORT
30601 +       /* i_generation */
30602 +       /* todo: make xigen file an array to support many inode numbers */
30603 +       struct file             *si_xigen;
30604 +       atomic_t                si_xigen_next;
30605 +#endif
30606 +
30607 +       /* dirty trick to support atomic_open */
30608 +       struct hlist_bl_head    si_aopen;
30609 +
30610 +       /* vdir parameters */
30611 +       unsigned long           si_rdcache;     /* max cache time in jiffies */
30612 +       unsigned int            si_rdblk;       /* deblk size */
30613 +       unsigned int            si_rdhash;      /* hash size */
30614 +
30615 +       /*
30616 +        * If the number of whiteouts are larger than si_dirwh, leave all of
30617 +        * them after au_whtmp_ren to reduce the cost of rmdir(2).
30618 +        * future fsck.aufs or kernel thread will remove them later.
30619 +        * Otherwise, remove all whiteouts and the dir in rmdir(2).
30620 +        */
30621 +       unsigned int            si_dirwh;
30622 +
30623 +       /* pseudo_link list */
30624 +       struct hlist_bl_head    si_plink[AuPlink_NHASH];
30625 +       wait_queue_head_t       si_plink_wq;
30626 +       spinlock_t              si_plink_maint_lock;
30627 +       pid_t                   si_plink_maint_pid;
30628 +
30629 +       /* file list */
30630 +       struct hlist_bl_head    si_files;
30631 +
30632 +       /* with/without getattr, brother of sb->s_d_op */
30633 +       const struct inode_operations *si_iop_array;
30634 +
30635 +       /*
30636 +        * sysfs and lifetime management.
30637 +        * this is not a small structure and it may be a waste of memory in case
30638 +        * of sysfs is disabled, particularly when many aufs-es are mounted.
30639 +        * but using sysfs is majority.
30640 +        */
30641 +       struct kobject          si_kobj;
30642 +#ifdef CONFIG_DEBUG_FS
30643 +       struct dentry            *si_dbgaufs;
30644 +       struct dentry            *si_dbgaufs_plink;
30645 +       struct dentry            *si_dbgaufs_xib;
30646 +#ifdef CONFIG_AUFS_EXPORT
30647 +       struct dentry            *si_dbgaufs_xigen;
30648 +#endif
30649 +#endif
30650 +
30651 +#ifdef CONFIG_AUFS_SBILIST
30652 +       struct hlist_bl_node    si_list;
30653 +#endif
30654 +
30655 +       /* dirty, necessary for unmounting, sysfs and sysrq */
30656 +       struct super_block      *si_sb;
30657 +};
30658 +
30659 +/* sbinfo status flags */
30660 +/*
30661 + * set true when refresh_dirs() failed at remount time.
30662 + * then try refreshing dirs at access time again.
30663 + * if it is false, refreshing dirs at access time is unnecessary
30664 + */
30665 +#define AuSi_FAILED_REFRESH_DIR        1
30666 +#define AuSi_FHSM              (1 << 1)        /* fhsm is active now */
30667 +#define AuSi_NO_DREVAL         (1 << 2)        /* disable all d_revalidate */
30668 +
30669 +#ifndef CONFIG_AUFS_FHSM
30670 +#undef AuSi_FHSM
30671 +#define AuSi_FHSM              0
30672 +#endif
30673 +
30674 +static inline unsigned char au_do_ftest_si(struct au_sbinfo *sbi,
30675 +                                          unsigned int flag)
30676 +{
30677 +       AuRwMustAnyLock(&sbi->si_rwsem);
30678 +       return sbi->au_si_status & flag;
30679 +}
30680 +#define au_ftest_si(sbinfo, name)      au_do_ftest_si(sbinfo, AuSi_##name)
30681 +#define au_fset_si(sbinfo, name) do { \
30682 +       AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
30683 +       (sbinfo)->au_si_status |= AuSi_##name; \
30684 +} while (0)
30685 +#define au_fclr_si(sbinfo, name) do { \
30686 +       AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
30687 +       (sbinfo)->au_si_status &= ~AuSi_##name; \
30688 +} while (0)
30689 +
30690 +/* ---------------------------------------------------------------------- */
30691 +
30692 +/* policy to select one among writable branches */
30693 +#define AuWbrCopyup(sbinfo, ...) \
30694 +       ((sbinfo)->si_wbr_copyup_ops->copyup(__VA_ARGS__))
30695 +#define AuWbrCreate(sbinfo, ...) \
30696 +       ((sbinfo)->si_wbr_create_ops->create(__VA_ARGS__))
30697 +
30698 +/* flags for si_read_lock()/aufs_read_lock()/di_read_lock() */
30699 +#define AuLock_DW              1               /* write-lock dentry */
30700 +#define AuLock_IR              (1 << 1)        /* read-lock inode */
30701 +#define AuLock_IW              (1 << 2)        /* write-lock inode */
30702 +#define AuLock_FLUSH           (1 << 3)        /* wait for 'nowait' tasks */
30703 +#define AuLock_DIRS            (1 << 4)        /* target is a pair of dirs */
30704 +                                               /* except RENAME_EXCHANGE */
30705 +#define AuLock_NOPLM           (1 << 5)        /* return err in plm mode */
30706 +#define AuLock_NOPLMW          (1 << 6)        /* wait for plm mode ends */
30707 +#define AuLock_GEN             (1 << 7)        /* test digen/iigen */
30708 +#define au_ftest_lock(flags, name)     ((flags) & AuLock_##name)
30709 +#define au_fset_lock(flags, name) \
30710 +       do { (flags) |= AuLock_##name; } while (0)
30711 +#define au_fclr_lock(flags, name) \
30712 +       do { (flags) &= ~AuLock_##name; } while (0)
30713 +
30714 +/* ---------------------------------------------------------------------- */
30715 +
30716 +/* super.c */
30717 +extern struct file_system_type aufs_fs_type;
30718 +struct inode *au_iget_locked(struct super_block *sb, ino_t ino);
30719 +typedef unsigned long long (*au_arraycb_t)(struct super_block *sb, void *array,
30720 +                                          unsigned long long max, void *arg);
30721 +void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb,
30722 +                    struct super_block *sb, void *arg);
30723 +struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max);
30724 +void au_iarray_free(struct inode **a, unsigned long long max);
30725 +
30726 +/* sbinfo.c */
30727 +void au_si_free(struct kobject *kobj);
30728 +int au_si_alloc(struct super_block *sb);
30729 +int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr, int may_shrink);
30730 +
30731 +unsigned int au_sigen_inc(struct super_block *sb);
30732 +aufs_bindex_t au_new_br_id(struct super_block *sb);
30733 +
30734 +int si_read_lock(struct super_block *sb, int flags);
30735 +int si_write_lock(struct super_block *sb, int flags);
30736 +int aufs_read_lock(struct dentry *dentry, int flags);
30737 +void aufs_read_unlock(struct dentry *dentry, int flags);
30738 +void aufs_write_lock(struct dentry *dentry);
30739 +void aufs_write_unlock(struct dentry *dentry);
30740 +int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags);
30741 +void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2);
30742 +
30743 +/* wbr_policy.c */
30744 +extern struct au_wbr_copyup_operations au_wbr_copyup_ops[];
30745 +extern struct au_wbr_create_operations au_wbr_create_ops[];
30746 +int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst);
30747 +int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex);
30748 +int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t btop);
30749 +
30750 +/* mvdown.c */
30751 +int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *arg);
30752 +
30753 +#ifdef CONFIG_AUFS_FHSM
30754 +/* fhsm.c */
30755 +
30756 +static inline pid_t au_fhsm_pid(struct au_fhsm *fhsm)
30757 +{
30758 +       pid_t pid;
30759 +
30760 +       spin_lock(&fhsm->fhsm_spin);
30761 +       pid = fhsm->fhsm_pid;
30762 +       spin_unlock(&fhsm->fhsm_spin);
30763 +
30764 +       return pid;
30765 +}
30766 +
30767 +void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force);
30768 +void au_fhsm_wrote_all(struct super_block *sb, int force);
30769 +int au_fhsm_fd(struct super_block *sb, int oflags);
30770 +int au_fhsm_br_alloc(struct au_branch *br);
30771 +void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex);
30772 +void au_fhsm_fin(struct super_block *sb);
30773 +void au_fhsm_init(struct au_sbinfo *sbinfo);
30774 +void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec);
30775 +void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo);
30776 +#else
30777 +AuStubVoid(au_fhsm_wrote, struct super_block *sb, aufs_bindex_t bindex,
30778 +          int force)
30779 +AuStubVoid(au_fhsm_wrote_all, struct super_block *sb, int force)
30780 +AuStub(int, au_fhsm_fd, return -EOPNOTSUPP, struct super_block *sb, int oflags)
30781 +AuStub(pid_t, au_fhsm_pid, return 0, struct au_fhsm *fhsm)
30782 +AuStubInt0(au_fhsm_br_alloc, struct au_branch *br)
30783 +AuStubVoid(au_fhsm_set_bottom, struct super_block *sb, aufs_bindex_t bindex)
30784 +AuStubVoid(au_fhsm_fin, struct super_block *sb)
30785 +AuStubVoid(au_fhsm_init, struct au_sbinfo *sbinfo)
30786 +AuStubVoid(au_fhsm_set, struct au_sbinfo *sbinfo, unsigned int sec)
30787 +AuStubVoid(au_fhsm_show, struct seq_file *seq, struct au_sbinfo *sbinfo)
30788 +#endif
30789 +
30790 +/* ---------------------------------------------------------------------- */
30791 +
30792 +static inline struct au_sbinfo *au_sbi(struct super_block *sb)
30793 +{
30794 +       return sb->s_fs_info;
30795 +}
30796 +
30797 +/* ---------------------------------------------------------------------- */
30798 +
30799 +#ifdef CONFIG_AUFS_EXPORT
30800 +int au_test_nfsd(void);
30801 +void au_export_init(struct super_block *sb);
30802 +void au_xigen_inc(struct inode *inode);
30803 +int au_xigen_new(struct inode *inode);
30804 +int au_xigen_set(struct super_block *sb, struct path *path);
30805 +void au_xigen_clr(struct super_block *sb);
30806 +
30807 +static inline int au_busy_or_stale(void)
30808 +{
30809 +       if (!au_test_nfsd())
30810 +               return -EBUSY;
30811 +       return -ESTALE;
30812 +}
30813 +#else
30814 +AuStubInt0(au_test_nfsd, void)
30815 +AuStubVoid(au_export_init, struct super_block *sb)
30816 +AuStubVoid(au_xigen_inc, struct inode *inode)
30817 +AuStubInt0(au_xigen_new, struct inode *inode)
30818 +AuStubInt0(au_xigen_set, struct super_block *sb, struct path *path)
30819 +AuStubVoid(au_xigen_clr, struct super_block *sb)
30820 +AuStub(int, au_busy_or_stale, return -EBUSY, void)
30821 +#endif /* CONFIG_AUFS_EXPORT */
30822 +
30823 +/* ---------------------------------------------------------------------- */
30824 +
30825 +#ifdef CONFIG_AUFS_SBILIST
30826 +/* module.c */
30827 +extern struct hlist_bl_head au_sbilist;
30828 +
30829 +static inline void au_sbilist_init(void)
30830 +{
30831 +       INIT_HLIST_BL_HEAD(&au_sbilist);
30832 +}
30833 +
30834 +static inline void au_sbilist_add(struct super_block *sb)
30835 +{
30836 +       au_hbl_add(&au_sbi(sb)->si_list, &au_sbilist);
30837 +}
30838 +
30839 +static inline void au_sbilist_del(struct super_block *sb)
30840 +{
30841 +       au_hbl_del(&au_sbi(sb)->si_list, &au_sbilist);
30842 +}
30843 +
30844 +#ifdef CONFIG_AUFS_MAGIC_SYSRQ
30845 +static inline void au_sbilist_lock(void)
30846 +{
30847 +       hlist_bl_lock(&au_sbilist);
30848 +}
30849 +
30850 +static inline void au_sbilist_unlock(void)
30851 +{
30852 +       hlist_bl_unlock(&au_sbilist);
30853 +}
30854 +#define AuGFP_SBILIST  GFP_ATOMIC
30855 +#else
30856 +AuStubVoid(au_sbilist_lock, void)
30857 +AuStubVoid(au_sbilist_unlock, void)
30858 +#define AuGFP_SBILIST  GFP_NOFS
30859 +#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
30860 +#else
30861 +AuStubVoid(au_sbilist_init, void)
30862 +AuStubVoid(au_sbilist_add, struct super_block *sb)
30863 +AuStubVoid(au_sbilist_del, struct super_block *sb)
30864 +AuStubVoid(au_sbilist_lock, void)
30865 +AuStubVoid(au_sbilist_unlock, void)
30866 +#define AuGFP_SBILIST  GFP_NOFS
30867 +#endif
30868 +
30869 +/* ---------------------------------------------------------------------- */
30870 +
30871 +static inline void dbgaufs_si_null(struct au_sbinfo *sbinfo)
30872 +{
30873 +       /*
30874 +        * This function is a dynamic '__init' function actually,
30875 +        * so the tiny check for si_rwsem is unnecessary.
30876 +        */
30877 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
30878 +#ifdef CONFIG_DEBUG_FS
30879 +       sbinfo->si_dbgaufs = NULL;
30880 +       sbinfo->si_dbgaufs_plink = NULL;
30881 +       sbinfo->si_dbgaufs_xib = NULL;
30882 +#ifdef CONFIG_AUFS_EXPORT
30883 +       sbinfo->si_dbgaufs_xigen = NULL;
30884 +#endif
30885 +#endif
30886 +}
30887 +
30888 +/* ---------------------------------------------------------------------- */
30889 +
30890 +/* current->atomic_flags */
30891 +/* this value should never corrupt the ones defined in linux/sched.h */
30892 +#define PFA_AUFS       0x10
30893 +
30894 +TASK_PFA_TEST(AUFS, test_aufs) /* task_test_aufs */
30895 +TASK_PFA_SET(AUFS, aufs)       /* task_set_aufs */
30896 +TASK_PFA_CLEAR(AUFS, aufs)     /* task_clear_aufs */
30897 +
30898 +static inline int si_pid_test(struct super_block *sb)
30899 +{
30900 +       return !!task_test_aufs(current);
30901 +}
30902 +
30903 +static inline void si_pid_clr(struct super_block *sb)
30904 +{
30905 +       AuDebugOn(!task_test_aufs(current));
30906 +       task_clear_aufs(current);
30907 +}
30908 +
30909 +static inline void si_pid_set(struct super_block *sb)
30910 +{
30911 +       AuDebugOn(task_test_aufs(current));
30912 +       task_set_aufs(current);
30913 +}
30914 +
30915 +/* ---------------------------------------------------------------------- */
30916 +
30917 +/* lock superblock. mainly for entry point functions */
30918 +#define __si_read_lock(sb)     au_rw_read_lock(&au_sbi(sb)->si_rwsem)
30919 +#define __si_write_lock(sb)    au_rw_write_lock(&au_sbi(sb)->si_rwsem)
30920 +#define __si_read_trylock(sb)  au_rw_read_trylock(&au_sbi(sb)->si_rwsem)
30921 +#define __si_write_trylock(sb) au_rw_write_trylock(&au_sbi(sb)->si_rwsem)
30922 +/*
30923 +#define __si_read_trylock_nested(sb) \
30924 +       au_rw_read_trylock_nested(&au_sbi(sb)->si_rwsem)
30925 +#define __si_write_trylock_nested(sb) \
30926 +       au_rw_write_trylock_nested(&au_sbi(sb)->si_rwsem)
30927 +*/
30928 +
30929 +#define __si_read_unlock(sb)   au_rw_read_unlock(&au_sbi(sb)->si_rwsem)
30930 +#define __si_write_unlock(sb)  au_rw_write_unlock(&au_sbi(sb)->si_rwsem)
30931 +#define __si_downgrade_lock(sb)        au_rw_dgrade_lock(&au_sbi(sb)->si_rwsem)
30932 +
30933 +#define SiMustNoWaiters(sb)    AuRwMustNoWaiters(&au_sbi(sb)->si_rwsem)
30934 +#define SiMustAnyLock(sb)      AuRwMustAnyLock(&au_sbi(sb)->si_rwsem)
30935 +#define SiMustWriteLock(sb)    AuRwMustWriteLock(&au_sbi(sb)->si_rwsem)
30936 +
30937 +static inline void si_noflush_read_lock(struct super_block *sb)
30938 +{
30939 +       __si_read_lock(sb);
30940 +       si_pid_set(sb);
30941 +}
30942 +
30943 +static inline int si_noflush_read_trylock(struct super_block *sb)
30944 +{
30945 +       int locked;
30946 +
30947 +       locked = __si_read_trylock(sb);
30948 +       if (locked)
30949 +               si_pid_set(sb);
30950 +       return locked;
30951 +}
30952 +
30953 +static inline void si_noflush_write_lock(struct super_block *sb)
30954 +{
30955 +       __si_write_lock(sb);
30956 +       si_pid_set(sb);
30957 +}
30958 +
30959 +static inline int si_noflush_write_trylock(struct super_block *sb)
30960 +{
30961 +       int locked;
30962 +
30963 +       locked = __si_write_trylock(sb);
30964 +       if (locked)
30965 +               si_pid_set(sb);
30966 +       return locked;
30967 +}
30968 +
30969 +#if 0 /* reserved */
30970 +static inline int si_read_trylock(struct super_block *sb, int flags)
30971 +{
30972 +       if (au_ftest_lock(flags, FLUSH))
30973 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
30974 +       return si_noflush_read_trylock(sb);
30975 +}
30976 +#endif
30977 +
30978 +static inline void si_read_unlock(struct super_block *sb)
30979 +{
30980 +       si_pid_clr(sb);
30981 +       __si_read_unlock(sb);
30982 +}
30983 +
30984 +#if 0 /* reserved */
30985 +static inline int si_write_trylock(struct super_block *sb, int flags)
30986 +{
30987 +       if (au_ftest_lock(flags, FLUSH))
30988 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
30989 +       return si_noflush_write_trylock(sb);
30990 +}
30991 +#endif
30992 +
30993 +static inline void si_write_unlock(struct super_block *sb)
30994 +{
30995 +       si_pid_clr(sb);
30996 +       __si_write_unlock(sb);
30997 +}
30998 +
30999 +#if 0 /* reserved */
31000 +static inline void si_downgrade_lock(struct super_block *sb)
31001 +{
31002 +       __si_downgrade_lock(sb);
31003 +}
31004 +#endif
31005 +
31006 +/* ---------------------------------------------------------------------- */
31007 +
31008 +static inline aufs_bindex_t au_sbbot(struct super_block *sb)
31009 +{
31010 +       SiMustAnyLock(sb);
31011 +       return au_sbi(sb)->si_bbot;
31012 +}
31013 +
31014 +static inline unsigned int au_mntflags(struct super_block *sb)
31015 +{
31016 +       SiMustAnyLock(sb);
31017 +       return au_sbi(sb)->si_mntflags;
31018 +}
31019 +
31020 +static inline unsigned int au_sigen(struct super_block *sb)
31021 +{
31022 +       SiMustAnyLock(sb);
31023 +       return au_sbi(sb)->si_generation;
31024 +}
31025 +
31026 +static inline struct au_branch *au_sbr(struct super_block *sb,
31027 +                                      aufs_bindex_t bindex)
31028 +{
31029 +       SiMustAnyLock(sb);
31030 +       return au_sbi(sb)->si_branch[0 + bindex];
31031 +}
31032 +
31033 +static inline loff_t au_xi_maxent(struct super_block *sb)
31034 +{
31035 +       SiMustAnyLock(sb);
31036 +       return au_sbi(sb)->si_ximaxent;
31037 +}
31038 +
31039 +#endif /* __KERNEL__ */
31040 +#endif /* __AUFS_SUPER_H__ */
31041 diff -urN /usr/share/empty/fs/aufs/sysaufs.c linux/fs/aufs/sysaufs.c
31042 --- /usr/share/empty/fs/aufs/sysaufs.c  1970-01-01 01:00:00.000000000 +0100
31043 +++ linux/fs/aufs/sysaufs.c     2021-11-01 23:48:34.213025928 +0100
31044 @@ -0,0 +1,93 @@
31045 +// SPDX-License-Identifier: GPL-2.0
31046 +/*
31047 + * Copyright (C) 2005-2021 Junjiro R. Okajima
31048 + *
31049 + * This program, aufs is free software; you can redistribute it and/or modify
31050 + * it under the terms of the GNU General Public License as published by
31051 + * the Free Software Foundation; either version 2 of the License, or
31052 + * (at your option) any later version.
31053 + *
31054 + * This program is distributed in the hope that it will be useful,
31055 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31056 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31057 + * GNU General Public License for more details.
31058 + *
31059 + * You should have received a copy of the GNU General Public License
31060 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31061 + */
31062 +
31063 +/*
31064 + * sysfs interface and lifetime management
31065 + * they are necessary regardless sysfs is disabled.
31066 + */
31067 +
31068 +#include <linux/random.h>
31069 +#include "aufs.h"
31070 +
31071 +unsigned long sysaufs_si_mask;
31072 +struct kset *sysaufs_kset;
31073 +
31074 +#define AuSiAttr(_name) { \
31075 +       .attr   = { .name = __stringify(_name), .mode = 0444 }, \
31076 +       .show   = sysaufs_si_##_name,                           \
31077 +}
31078 +
31079 +static struct sysaufs_si_attr sysaufs_si_attr_xi_path = AuSiAttr(xi_path);
31080 +struct attribute *sysaufs_si_attrs[] = {
31081 +       &sysaufs_si_attr_xi_path.attr,
31082 +       NULL,
31083 +};
31084 +
31085 +static const struct sysfs_ops au_sbi_ops = {
31086 +       .show   = sysaufs_si_show
31087 +};
31088 +
31089 +static struct kobj_type au_sbi_ktype = {
31090 +       .release        = au_si_free,
31091 +       .sysfs_ops      = &au_sbi_ops,
31092 +       .default_attrs  = sysaufs_si_attrs
31093 +};
31094 +
31095 +/* ---------------------------------------------------------------------- */
31096 +
31097 +int sysaufs_si_init(struct au_sbinfo *sbinfo)
31098 +{
31099 +       int err;
31100 +
31101 +       sbinfo->si_kobj.kset = sysaufs_kset;
31102 +       /* cf. sysaufs_name() */
31103 +       err = kobject_init_and_add
31104 +               (&sbinfo->si_kobj, &au_sbi_ktype, /*&sysaufs_kset->kobj*/NULL,
31105 +                SysaufsSiNamePrefix "%lx", sysaufs_si_id(sbinfo));
31106 +
31107 +       return err;
31108 +}
31109 +
31110 +void sysaufs_fin(void)
31111 +{
31112 +       sysfs_remove_group(&sysaufs_kset->kobj, sysaufs_attr_group);
31113 +       kset_unregister(sysaufs_kset);
31114 +}
31115 +
31116 +int __init sysaufs_init(void)
31117 +{
31118 +       int err;
31119 +
31120 +       do {
31121 +               get_random_bytes(&sysaufs_si_mask, sizeof(sysaufs_si_mask));
31122 +       } while (!sysaufs_si_mask);
31123 +
31124 +       err = -EINVAL;
31125 +       sysaufs_kset = kset_create_and_add(AUFS_NAME, NULL, fs_kobj);
31126 +       if (unlikely(!sysaufs_kset))
31127 +               goto out;
31128 +       err = PTR_ERR(sysaufs_kset);
31129 +       if (IS_ERR(sysaufs_kset))
31130 +               goto out;
31131 +       err = sysfs_create_group(&sysaufs_kset->kobj, sysaufs_attr_group);
31132 +       if (unlikely(err))
31133 +               kset_unregister(sysaufs_kset);
31134 +
31135 +out:
31136 +       return err;
31137 +}
31138 diff -urN /usr/share/empty/fs/aufs/sysaufs.h linux/fs/aufs/sysaufs.h
31139 --- /usr/share/empty/fs/aufs/sysaufs.h  1970-01-01 01:00:00.000000000 +0100
31140 +++ linux/fs/aufs/sysaufs.h     2021-11-01 23:48:34.213025928 +0100
31141 @@ -0,0 +1,102 @@
31142 +/* SPDX-License-Identifier: GPL-2.0 */
31143 +/*
31144 + * Copyright (C) 2005-2021 Junjiro R. Okajima
31145 + *
31146 + * This program, aufs is free software; you can redistribute it and/or modify
31147 + * it under the terms of the GNU General Public License as published by
31148 + * the Free Software Foundation; either version 2 of the License, or
31149 + * (at your option) any later version.
31150 + *
31151 + * This program is distributed in the hope that it will be useful,
31152 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31153 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31154 + * GNU General Public License for more details.
31155 + *
31156 + * You should have received a copy of the GNU General Public License
31157 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31158 + */
31159 +
31160 +/*
31161 + * sysfs interface and mount lifetime management
31162 + */
31163 +
31164 +#ifndef __SYSAUFS_H__
31165 +#define __SYSAUFS_H__
31166 +
31167 +#ifdef __KERNEL__
31168 +
31169 +#include <linux/sysfs.h>
31170 +#include "module.h"
31171 +
31172 +struct super_block;
31173 +struct au_sbinfo;
31174 +
31175 +struct sysaufs_si_attr {
31176 +       struct attribute attr;
31177 +       int (*show)(struct seq_file *seq, struct super_block *sb);
31178 +};
31179 +
31180 +/* ---------------------------------------------------------------------- */
31181 +
31182 +/* sysaufs.c */
31183 +extern unsigned long sysaufs_si_mask;
31184 +extern struct kset *sysaufs_kset;
31185 +extern struct attribute *sysaufs_si_attrs[];
31186 +int sysaufs_si_init(struct au_sbinfo *sbinfo);
31187 +int __init sysaufs_init(void);
31188 +void sysaufs_fin(void);
31189 +
31190 +/* ---------------------------------------------------------------------- */
31191 +
31192 +/* some people doesn't like to show a pointer in kernel */
31193 +static inline unsigned long sysaufs_si_id(struct au_sbinfo *sbinfo)
31194 +{
31195 +       return sysaufs_si_mask ^ (unsigned long)sbinfo;
31196 +}
31197 +
31198 +#define SysaufsSiNamePrefix    "si_"
31199 +#define SysaufsSiNameLen       (sizeof(SysaufsSiNamePrefix) + 16)
31200 +static inline void sysaufs_name(struct au_sbinfo *sbinfo, char *name)
31201 +{
31202 +       snprintf(name, SysaufsSiNameLen, SysaufsSiNamePrefix "%lx",
31203 +                sysaufs_si_id(sbinfo));
31204 +}
31205 +
31206 +struct au_branch;
31207 +#ifdef CONFIG_SYSFS
31208 +/* sysfs.c */
31209 +extern struct attribute_group *sysaufs_attr_group;
31210 +
31211 +int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb);
31212 +ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
31213 +                        char *buf);
31214 +long au_brinfo_ioctl(struct file *file, unsigned long arg);
31215 +#ifdef CONFIG_COMPAT
31216 +long au_brinfo_compat_ioctl(struct file *file, unsigned long arg);
31217 +#endif
31218 +
31219 +void sysaufs_br_init(struct au_branch *br);
31220 +void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex);
31221 +void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
31222 +
31223 +#define sysaufs_brs_init()     do {} while (0)
31224 +
31225 +#else
31226 +#define sysaufs_attr_group     NULL
31227 +
31228 +AuStubInt0(sysaufs_si_xi_path, struct seq_file *seq, struct super_block *sb)
31229 +AuStub(ssize_t, sysaufs_si_show, return 0, struct kobject *kobj,
31230 +       struct attribute *attr, char *buf)
31231 +AuStubVoid(sysaufs_br_init, struct au_branch *br)
31232 +AuStubVoid(sysaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex)
31233 +AuStubVoid(sysaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
31234 +
31235 +static inline void sysaufs_brs_init(void)
31236 +{
31237 +       sysaufs_brs = 0;
31238 +}
31239 +
31240 +#endif /* CONFIG_SYSFS */
31241 +
31242 +#endif /* __KERNEL__ */
31243 +#endif /* __SYSAUFS_H__ */
31244 diff -urN /usr/share/empty/fs/aufs/sysfs.c linux/fs/aufs/sysfs.c
31245 --- /usr/share/empty/fs/aufs/sysfs.c    1970-01-01 01:00:00.000000000 +0100
31246 +++ linux/fs/aufs/sysfs.c       2021-11-01 23:48:34.213025928 +0100
31247 @@ -0,0 +1,374 @@
31248 +// SPDX-License-Identifier: GPL-2.0
31249 +/*
31250 + * Copyright (C) 2005-2021 Junjiro R. Okajima
31251 + *
31252 + * This program, aufs is free software; you can redistribute it and/or modify
31253 + * it under the terms of the GNU General Public License as published by
31254 + * the Free Software Foundation; either version 2 of the License, or
31255 + * (at your option) any later version.
31256 + *
31257 + * This program is distributed in the hope that it will be useful,
31258 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31259 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31260 + * GNU General Public License for more details.
31261 + *
31262 + * You should have received a copy of the GNU General Public License
31263 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31264 + */
31265 +
31266 +/*
31267 + * sysfs interface
31268 + */
31269 +
31270 +#include <linux/compat.h>
31271 +#include <linux/seq_file.h>
31272 +#include "aufs.h"
31273 +
31274 +#ifdef CONFIG_AUFS_FS_MODULE
31275 +/* this entry violates the "one line per file" policy of sysfs */
31276 +static ssize_t config_show(struct kobject *kobj, struct kobj_attribute *attr,
31277 +                          char *buf)
31278 +{
31279 +       ssize_t err;
31280 +       static char *conf =
31281 +/* this file is generated at compiling */
31282 +#include "conf.str"
31283 +               ;
31284 +
31285 +       err = snprintf(buf, PAGE_SIZE, conf);
31286 +       if (unlikely(err >= PAGE_SIZE))
31287 +               err = -EFBIG;
31288 +       return err;
31289 +}
31290 +
31291 +static struct kobj_attribute au_config_attr = __ATTR_RO(config);
31292 +#endif
31293 +
31294 +static struct attribute *au_attr[] = {
31295 +#ifdef CONFIG_AUFS_FS_MODULE
31296 +       &au_config_attr.attr,
31297 +#endif
31298 +       NULL,   /* need to NULL terminate the list of attributes */
31299 +};
31300 +
31301 +static struct attribute_group sysaufs_attr_group_body = {
31302 +       .attrs = au_attr
31303 +};
31304 +
31305 +struct attribute_group *sysaufs_attr_group = &sysaufs_attr_group_body;
31306 +
31307 +/* ---------------------------------------------------------------------- */
31308 +
31309 +int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb)
31310 +{
31311 +       int err;
31312 +
31313 +       SiMustAnyLock(sb);
31314 +
31315 +       err = 0;
31316 +       if (au_opt_test(au_mntflags(sb), XINO)) {
31317 +               err = au_xino_path(seq, au_sbi(sb)->si_xib);
31318 +               seq_putc(seq, '\n');
31319 +       }
31320 +       return err;
31321 +}
31322 +
31323 +/*
31324 + * the lifetime of branch is independent from the entry under sysfs.
31325 + * sysfs handles the lifetime of the entry, and never call ->show() after it is
31326 + * unlinked.
31327 + */
31328 +static int sysaufs_si_br(struct seq_file *seq, struct super_block *sb,
31329 +                        aufs_bindex_t bindex, int idx)
31330 +{
31331 +       int err;
31332 +       struct path path;
31333 +       struct dentry *root;
31334 +       struct au_branch *br;
31335 +       au_br_perm_str_t perm;
31336 +
31337 +       AuDbg("b%d\n", bindex);
31338 +
31339 +       err = 0;
31340 +       root = sb->s_root;
31341 +       di_read_lock_parent(root, !AuLock_IR);
31342 +       br = au_sbr(sb, bindex);
31343 +
31344 +       switch (idx) {
31345 +       case AuBrSysfs_BR:
31346 +               path.mnt = au_br_mnt(br);
31347 +               path.dentry = au_h_dptr(root, bindex);
31348 +               err = au_seq_path(seq, &path);
31349 +               if (!err) {
31350 +                       au_optstr_br_perm(&perm, br->br_perm);
31351 +                       seq_printf(seq, "=%s\n", perm.a);
31352 +               }
31353 +               break;
31354 +       case AuBrSysfs_BRID:
31355 +               seq_printf(seq, "%d\n", br->br_id);
31356 +               break;
31357 +       }
31358 +       di_read_unlock(root, !AuLock_IR);
31359 +       if (unlikely(err || seq_has_overflowed(seq)))
31360 +               err = -E2BIG;
31361 +
31362 +       return err;
31363 +}
31364 +
31365 +/* ---------------------------------------------------------------------- */
31366 +
31367 +static struct seq_file *au_seq(char *p, ssize_t len)
31368 +{
31369 +       struct seq_file *seq;
31370 +
31371 +       seq = kzalloc(sizeof(*seq), GFP_NOFS);
31372 +       if (seq) {
31373 +               /* mutex_init(&seq.lock); */
31374 +               seq->buf = p;
31375 +               seq->size = len;
31376 +               return seq; /* success */
31377 +       }
31378 +
31379 +       seq = ERR_PTR(-ENOMEM);
31380 +       return seq;
31381 +}
31382 +
31383 +#define SysaufsBr_PREFIX       "br"
31384 +#define SysaufsBrid_PREFIX     "brid"
31385 +
31386 +/* todo: file size may exceed PAGE_SIZE */
31387 +ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
31388 +                       char *buf)
31389 +{
31390 +       ssize_t err;
31391 +       int idx;
31392 +       long l;
31393 +       aufs_bindex_t bbot;
31394 +       struct au_sbinfo *sbinfo;
31395 +       struct super_block *sb;
31396 +       struct seq_file *seq;
31397 +       char *name;
31398 +       struct attribute **cattr;
31399 +
31400 +       sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
31401 +       sb = sbinfo->si_sb;
31402 +
31403 +       /*
31404 +        * prevent a race condition between sysfs and aufs.
31405 +        * for instance, sysfs_file_read() calls sysfs_get_active_two() which
31406 +        * prohibits maintaining the sysfs entries.
31407 +        * hew we acquire read lock after sysfs_get_active_two().
31408 +        * on the other hand, the remount process may maintain the sysfs/aufs
31409 +        * entries after acquiring write lock.
31410 +        * it can cause a deadlock.
31411 +        * simply we gave up processing read here.
31412 +        */
31413 +       err = -EBUSY;
31414 +       if (unlikely(!si_noflush_read_trylock(sb)))
31415 +               goto out;
31416 +
31417 +       seq = au_seq(buf, PAGE_SIZE);
31418 +       err = PTR_ERR(seq);
31419 +       if (IS_ERR(seq))
31420 +               goto out_unlock;
31421 +
31422 +       name = (void *)attr->name;
31423 +       cattr = sysaufs_si_attrs;
31424 +       while (*cattr) {
31425 +               if (!strcmp(name, (*cattr)->name)) {
31426 +                       err = container_of(*cattr, struct sysaufs_si_attr, attr)
31427 +                               ->show(seq, sb);
31428 +                       goto out_seq;
31429 +               }
31430 +               cattr++;
31431 +       }
31432 +
31433 +       if (!strncmp(name, SysaufsBrid_PREFIX,
31434 +                    sizeof(SysaufsBrid_PREFIX) - 1)) {
31435 +               idx = AuBrSysfs_BRID;
31436 +               name += sizeof(SysaufsBrid_PREFIX) - 1;
31437 +       } else if (!strncmp(name, SysaufsBr_PREFIX,
31438 +                           sizeof(SysaufsBr_PREFIX) - 1)) {
31439 +               idx = AuBrSysfs_BR;
31440 +               name += sizeof(SysaufsBr_PREFIX) - 1;
31441 +       } else
31442 +                 BUG();
31443 +
31444 +       err = kstrtol(name, 10, &l);
31445 +       if (!err) {
31446 +               bbot = au_sbbot(sb);
31447 +               if (l <= bbot)
31448 +                       err = sysaufs_si_br(seq, sb, (aufs_bindex_t)l, idx);
31449 +               else
31450 +                       err = -ENOENT;
31451 +       }
31452 +
31453 +out_seq:
31454 +       if (!err) {
31455 +               err = seq->count;
31456 +               /* sysfs limit */
31457 +               if (unlikely(err == PAGE_SIZE))
31458 +                       err = -EFBIG;
31459 +       }
31460 +       au_kfree_rcu(seq);
31461 +out_unlock:
31462 +       si_read_unlock(sb);
31463 +out:
31464 +       return err;
31465 +}
31466 +
31467 +/* ---------------------------------------------------------------------- */
31468 +
31469 +static int au_brinfo(struct super_block *sb, union aufs_brinfo __user *arg)
31470 +{
31471 +       int err;
31472 +       int16_t brid;
31473 +       aufs_bindex_t bindex, bbot;
31474 +       size_t sz;
31475 +       char *buf;
31476 +       struct seq_file *seq;
31477 +       struct au_branch *br;
31478 +
31479 +       si_read_lock(sb, AuLock_FLUSH);
31480 +       bbot = au_sbbot(sb);
31481 +       err = bbot + 1;
31482 +       if (!arg)
31483 +               goto out;
31484 +
31485 +       err = -ENOMEM;
31486 +       buf = (void *)__get_free_page(GFP_NOFS);
31487 +       if (unlikely(!buf))
31488 +               goto out;
31489 +
31490 +       seq = au_seq(buf, PAGE_SIZE);
31491 +       err = PTR_ERR(seq);
31492 +       if (IS_ERR(seq))
31493 +               goto out_buf;
31494 +
31495 +       sz = sizeof(*arg) - offsetof(union aufs_brinfo, path);
31496 +       for (bindex = 0; bindex <= bbot; bindex++, arg++) {
31497 +               /* VERIFY_WRITE */
31498 +               err = !access_ok(arg, sizeof(*arg));
31499 +               if (unlikely(err))
31500 +                       break;
31501 +
31502 +               br = au_sbr(sb, bindex);
31503 +               brid = br->br_id;
31504 +               BUILD_BUG_ON(sizeof(brid) != sizeof(arg->id));
31505 +               err = __put_user(brid, &arg->id);
31506 +               if (unlikely(err))
31507 +                       break;
31508 +
31509 +               BUILD_BUG_ON(sizeof(br->br_perm) != sizeof(arg->perm));
31510 +               err = __put_user(br->br_perm, &arg->perm);
31511 +               if (unlikely(err))
31512 +                       break;
31513 +
31514 +               err = au_seq_path(seq, &br->br_path);
31515 +               if (unlikely(err))
31516 +                       break;
31517 +               seq_putc(seq, '\0');
31518 +               if (!seq_has_overflowed(seq)) {
31519 +                       err = copy_to_user(arg->path, seq->buf, seq->count);
31520 +                       seq->count = 0;
31521 +                       if (unlikely(err))
31522 +                               break;
31523 +               } else {
31524 +                       err = -E2BIG;
31525 +                       goto out_seq;
31526 +               }
31527 +       }
31528 +       if (unlikely(err))
31529 +               err = -EFAULT;
31530 +
31531 +out_seq:
31532 +       au_kfree_rcu(seq);
31533 +out_buf:
31534 +       free_page((unsigned long)buf);
31535 +out:
31536 +       si_read_unlock(sb);
31537 +       return err;
31538 +}
31539 +
31540 +long au_brinfo_ioctl(struct file *file, unsigned long arg)
31541 +{
31542 +       return au_brinfo(file->f_path.dentry->d_sb, (void __user *)arg);
31543 +}
31544 +
31545 +#ifdef CONFIG_COMPAT
31546 +long au_brinfo_compat_ioctl(struct file *file, unsigned long arg)
31547 +{
31548 +       return au_brinfo(file->f_path.dentry->d_sb, compat_ptr(arg));
31549 +}
31550 +#endif
31551 +
31552 +/* ---------------------------------------------------------------------- */
31553 +
31554 +void sysaufs_br_init(struct au_branch *br)
31555 +{
31556 +       int i;
31557 +       struct au_brsysfs *br_sysfs;
31558 +       struct attribute *attr;
31559 +
31560 +       br_sysfs = br->br_sysfs;
31561 +       for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31562 +               attr = &br_sysfs->attr;
31563 +               sysfs_attr_init(attr);
31564 +               attr->name = br_sysfs->name;
31565 +               attr->mode = 0444;
31566 +               br_sysfs++;
31567 +       }
31568 +}
31569 +
31570 +void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
31571 +{
31572 +       struct au_branch *br;
31573 +       struct kobject *kobj;
31574 +       struct au_brsysfs *br_sysfs;
31575 +       int i;
31576 +       aufs_bindex_t bbot;
31577 +
31578 +       if (!sysaufs_brs)
31579 +               return;
31580 +
31581 +       kobj = &au_sbi(sb)->si_kobj;
31582 +       bbot = au_sbbot(sb);
31583 +       for (; bindex <= bbot; bindex++) {
31584 +               br = au_sbr(sb, bindex);
31585 +               br_sysfs = br->br_sysfs;
31586 +               for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31587 +                       sysfs_remove_file(kobj, &br_sysfs->attr);
31588 +                       br_sysfs++;
31589 +               }
31590 +       }
31591 +}
31592 +
31593 +void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex)
31594 +{
31595 +       int err, i;
31596 +       aufs_bindex_t bbot;
31597 +       struct kobject *kobj;
31598 +       struct au_branch *br;
31599 +       struct au_brsysfs *br_sysfs;
31600 +
31601 +       if (!sysaufs_brs)
31602 +               return;
31603 +
31604 +       kobj = &au_sbi(sb)->si_kobj;
31605 +       bbot = au_sbbot(sb);
31606 +       for (; bindex <= bbot; bindex++) {
31607 +               br = au_sbr(sb, bindex);
31608 +               br_sysfs = br->br_sysfs;
31609 +               snprintf(br_sysfs[AuBrSysfs_BR].name, sizeof(br_sysfs->name),
31610 +                        SysaufsBr_PREFIX "%d", bindex);
31611 +               snprintf(br_sysfs[AuBrSysfs_BRID].name, sizeof(br_sysfs->name),
31612 +                        SysaufsBrid_PREFIX "%d", bindex);
31613 +               for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31614 +                       err = sysfs_create_file(kobj, &br_sysfs->attr);
31615 +                       if (unlikely(err))
31616 +                               pr_warn("failed %s under sysfs(%d)\n",
31617 +                                       br_sysfs->name, err);
31618 +                       br_sysfs++;
31619 +               }
31620 +       }
31621 +}
31622 diff -urN /usr/share/empty/fs/aufs/sysrq.c linux/fs/aufs/sysrq.c
31623 --- /usr/share/empty/fs/aufs/sysrq.c    1970-01-01 01:00:00.000000000 +0100
31624 +++ linux/fs/aufs/sysrq.c       2021-11-01 23:48:34.213025928 +0100
31625 @@ -0,0 +1,149 @@
31626 +// SPDX-License-Identifier: GPL-2.0
31627 +/*
31628 + * Copyright (C) 2005-2021 Junjiro R. Okajima
31629 + *
31630 + * This program, aufs is free software; you can redistribute it and/or modify
31631 + * it under the terms of the GNU General Public License as published by
31632 + * the Free Software Foundation; either version 2 of the License, or
31633 + * (at your option) any later version.
31634 + *
31635 + * This program is distributed in the hope that it will be useful,
31636 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31637 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31638 + * GNU General Public License for more details.
31639 + *
31640 + * You should have received a copy of the GNU General Public License
31641 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31642 + */
31643 +
31644 +/*
31645 + * magic sysrq handler
31646 + */
31647 +
31648 +/* #include <linux/sysrq.h> */
31649 +#include <linux/writeback.h>
31650 +#include "aufs.h"
31651 +
31652 +/* ---------------------------------------------------------------------- */
31653 +
31654 +static void sysrq_sb(struct super_block *sb)
31655 +{
31656 +       char *plevel;
31657 +       struct au_sbinfo *sbinfo;
31658 +       struct file *file;
31659 +       struct hlist_bl_head *files;
31660 +       struct hlist_bl_node *pos;
31661 +       struct au_finfo *finfo;
31662 +       struct inode *i;
31663 +
31664 +       plevel = au_plevel;
31665 +       au_plevel = KERN_WARNING;
31666 +
31667 +       /* since we define pr_fmt, call printk directly */
31668 +#define pr(str) printk(KERN_WARNING AUFS_NAME ": " str)
31669 +
31670 +       sbinfo = au_sbi(sb);
31671 +       printk(KERN_WARNING "si=%lx\n", sysaufs_si_id(sbinfo));
31672 +       pr("superblock\n");
31673 +       au_dpri_sb(sb);
31674 +
31675 +#if 0 /* reserved */
31676 +       do {
31677 +               int err, i, j, ndentry;
31678 +               struct au_dcsub_pages dpages;
31679 +               struct au_dpage *dpage;
31680 +
31681 +               err = au_dpages_init(&dpages, GFP_ATOMIC);
31682 +               if (unlikely(err))
31683 +                       break;
31684 +               err = au_dcsub_pages(&dpages, sb->s_root, NULL, NULL);
31685 +               if (!err)
31686 +                       for (i = 0; i < dpages.ndpage; i++) {
31687 +                               dpage = dpages.dpages + i;
31688 +                               ndentry = dpage->ndentry;
31689 +                               for (j = 0; j < ndentry; j++)
31690 +                                       au_dpri_dentry(dpage->dentries[j]);
31691 +                       }
31692 +               au_dpages_free(&dpages);
31693 +       } while (0);
31694 +#endif
31695 +
31696 +       pr("isolated inode\n");
31697 +       spin_lock(&sb->s_inode_list_lock);
31698 +       list_for_each_entry(i, &sb->s_inodes, i_sb_list) {
31699 +               spin_lock(&i->i_lock);
31700 +               if (hlist_empty(&i->i_dentry))
31701 +                       au_dpri_inode(i);
31702 +               spin_unlock(&i->i_lock);
31703 +       }
31704 +       spin_unlock(&sb->s_inode_list_lock);
31705 +
31706 +       pr("files\n");
31707 +       files = &au_sbi(sb)->si_files;
31708 +       hlist_bl_lock(files);
31709 +       hlist_bl_for_each_entry(finfo, pos, files, fi_hlist) {
31710 +               umode_t mode;
31711 +
31712 +               file = finfo->fi_file;
31713 +               mode = file_inode(file)->i_mode;
31714 +               if (!special_file(mode))
31715 +                       au_dpri_file(file);
31716 +       }
31717 +       hlist_bl_unlock(files);
31718 +       pr("done\n");
31719 +
31720 +#undef pr
31721 +       au_plevel = plevel;
31722 +}
31723 +
31724 +/* ---------------------------------------------------------------------- */
31725 +
31726 +/* module parameter */
31727 +static char *aufs_sysrq_key = "a";
31728 +module_param_named(sysrq, aufs_sysrq_key, charp, 0444);
31729 +MODULE_PARM_DESC(sysrq, "MagicSysRq key for " AUFS_NAME);
31730 +
31731 +static void au_sysrq(int key __maybe_unused)
31732 +{
31733 +       struct au_sbinfo *sbinfo;
31734 +       struct hlist_bl_node *pos;
31735 +
31736 +       lockdep_off();
31737 +       au_sbilist_lock();
31738 +       hlist_bl_for_each_entry(sbinfo, pos, &au_sbilist, si_list)
31739 +               sysrq_sb(sbinfo->si_sb);
31740 +       au_sbilist_unlock();
31741 +       lockdep_on();
31742 +}
31743 +
31744 +static struct sysrq_key_op au_sysrq_op = {
31745 +       .handler        = au_sysrq,
31746 +       .help_msg       = "Aufs",
31747 +       .action_msg     = "Aufs",
31748 +       .enable_mask    = SYSRQ_ENABLE_DUMP
31749 +};
31750 +
31751 +/* ---------------------------------------------------------------------- */
31752 +
31753 +int __init au_sysrq_init(void)
31754 +{
31755 +       int err;
31756 +       char key;
31757 +
31758 +       err = -1;
31759 +       key = *aufs_sysrq_key;
31760 +       if ('a' <= key && key <= 'z')
31761 +               err = register_sysrq_key(key, &au_sysrq_op);
31762 +       if (unlikely(err))
31763 +               pr_err("err %d, sysrq=%c\n", err, key);
31764 +       return err;
31765 +}
31766 +
31767 +void au_sysrq_fin(void)
31768 +{
31769 +       int err;
31770 +
31771 +       err = unregister_sysrq_key(*aufs_sysrq_key, &au_sysrq_op);
31772 +       if (unlikely(err))
31773 +               pr_err("err %d (ignored)\n", err);
31774 +}
31775 diff -urN /usr/share/empty/fs/aufs/vdir.c linux/fs/aufs/vdir.c
31776 --- /usr/share/empty/fs/aufs/vdir.c     1970-01-01 01:00:00.000000000 +0100
31777 +++ linux/fs/aufs/vdir.c        2021-11-01 23:48:34.213025928 +0100
31778 @@ -0,0 +1,896 @@
31779 +// SPDX-License-Identifier: GPL-2.0
31780 +/*
31781 + * Copyright (C) 2005-2021 Junjiro R. Okajima
31782 + *
31783 + * This program, aufs is free software; you can redistribute it and/or modify
31784 + * it under the terms of the GNU General Public License as published by
31785 + * the Free Software Foundation; either version 2 of the License, or
31786 + * (at your option) any later version.
31787 + *
31788 + * This program is distributed in the hope that it will be useful,
31789 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31790 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31791 + * GNU General Public License for more details.
31792 + *
31793 + * You should have received a copy of the GNU General Public License
31794 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31795 + */
31796 +
31797 +/*
31798 + * virtual or vertical directory
31799 + */
31800 +
31801 +#include <linux/iversion.h>
31802 +#include "aufs.h"
31803 +
31804 +static unsigned int calc_size(int nlen)
31805 +{
31806 +       return ALIGN(sizeof(struct au_vdir_de) + nlen, sizeof(ino_t));
31807 +}
31808 +
31809 +static int set_deblk_end(union au_vdir_deblk_p *p,
31810 +                        union au_vdir_deblk_p *deblk_end)
31811 +{
31812 +       if (calc_size(0) <= deblk_end->deblk - p->deblk) {
31813 +               p->de->de_str.len = 0;
31814 +               /* smp_mb(); */
31815 +               return 0;
31816 +       }
31817 +       return -1; /* error */
31818 +}
31819 +
31820 +/* returns true or false */
31821 +static int is_deblk_end(union au_vdir_deblk_p *p,
31822 +                       union au_vdir_deblk_p *deblk_end)
31823 +{
31824 +       if (calc_size(0) <= deblk_end->deblk - p->deblk)
31825 +               return !p->de->de_str.len;
31826 +       return 1;
31827 +}
31828 +
31829 +static unsigned char *last_deblk(struct au_vdir *vdir)
31830 +{
31831 +       return vdir->vd_deblk[vdir->vd_nblk - 1];
31832 +}
31833 +
31834 +/* ---------------------------------------------------------------------- */
31835 +
31836 +/* estimate the appropriate size for name hash table */
31837 +unsigned int au_rdhash_est(loff_t sz)
31838 +{
31839 +       unsigned int n;
31840 +
31841 +       n = UINT_MAX;
31842 +       sz >>= 10;
31843 +       if (sz < n)
31844 +               n = sz;
31845 +       if (sz < AUFS_RDHASH_DEF)
31846 +               n = AUFS_RDHASH_DEF;
31847 +       /* pr_info("n %u\n", n); */
31848 +       return n;
31849 +}
31850 +
31851 +/*
31852 + * the allocated memory has to be freed by
31853 + * au_nhash_wh_free() or au_nhash_de_free().
31854 + */
31855 +int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp)
31856 +{
31857 +       struct hlist_head *head;
31858 +       unsigned int u;
31859 +       size_t sz;
31860 +
31861 +       sz = sizeof(*nhash->nh_head) * num_hash;
31862 +       head = kmalloc(sz, gfp);
31863 +       if (head) {
31864 +               nhash->nh_num = num_hash;
31865 +               nhash->nh_head = head;
31866 +               for (u = 0; u < num_hash; u++)
31867 +                       INIT_HLIST_HEAD(head++);
31868 +               return 0; /* success */
31869 +       }
31870 +
31871 +       return -ENOMEM;
31872 +}
31873 +
31874 +static void nhash_count(struct hlist_head *head)
31875 +{
31876 +#if 0 /* debugging */
31877 +       unsigned long n;
31878 +       struct hlist_node *pos;
31879 +
31880 +       n = 0;
31881 +       hlist_for_each(pos, head)
31882 +               n++;
31883 +       pr_info("%lu\n", n);
31884 +#endif
31885 +}
31886 +
31887 +static void au_nhash_wh_do_free(struct hlist_head *head)
31888 +{
31889 +       struct au_vdir_wh *pos;
31890 +       struct hlist_node *node;
31891 +
31892 +       hlist_for_each_entry_safe(pos, node, head, wh_hash)
31893 +               au_kfree_rcu(pos);
31894 +}
31895 +
31896 +static void au_nhash_de_do_free(struct hlist_head *head)
31897 +{
31898 +       struct au_vdir_dehstr *pos;
31899 +       struct hlist_node *node;
31900 +
31901 +       hlist_for_each_entry_safe(pos, node, head, hash)
31902 +               au_cache_free_vdir_dehstr(pos);
31903 +}
31904 +
31905 +static void au_nhash_do_free(struct au_nhash *nhash,
31906 +                            void (*free)(struct hlist_head *head))
31907 +{
31908 +       unsigned int n;
31909 +       struct hlist_head *head;
31910 +
31911 +       n = nhash->nh_num;
31912 +       if (!n)
31913 +               return;
31914 +
31915 +       head = nhash->nh_head;
31916 +       while (n-- > 0) {
31917 +               nhash_count(head);
31918 +               free(head++);
31919 +       }
31920 +       au_kfree_try_rcu(nhash->nh_head);
31921 +}
31922 +
31923 +void au_nhash_wh_free(struct au_nhash *whlist)
31924 +{
31925 +       au_nhash_do_free(whlist, au_nhash_wh_do_free);
31926 +}
31927 +
31928 +static void au_nhash_de_free(struct au_nhash *delist)
31929 +{
31930 +       au_nhash_do_free(delist, au_nhash_de_do_free);
31931 +}
31932 +
31933 +/* ---------------------------------------------------------------------- */
31934 +
31935 +int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
31936 +                           int limit)
31937 +{
31938 +       int num;
31939 +       unsigned int u, n;
31940 +       struct hlist_head *head;
31941 +       struct au_vdir_wh *pos;
31942 +
31943 +       num = 0;
31944 +       n = whlist->nh_num;
31945 +       head = whlist->nh_head;
31946 +       for (u = 0; u < n; u++, head++)
31947 +               hlist_for_each_entry(pos, head, wh_hash)
31948 +                       if (pos->wh_bindex == btgt && ++num > limit)
31949 +                               return 1;
31950 +       return 0;
31951 +}
31952 +
31953 +static struct hlist_head *au_name_hash(struct au_nhash *nhash,
31954 +                                      unsigned char *name,
31955 +                                      unsigned int len)
31956 +{
31957 +       unsigned int v;
31958 +       /* const unsigned int magic_bit = 12; */
31959 +
31960 +       AuDebugOn(!nhash->nh_num || !nhash->nh_head);
31961 +
31962 +       v = 0;
31963 +       if (len > 8)
31964 +               len = 8;
31965 +       while (len--)
31966 +               v += *name++;
31967 +       /* v = hash_long(v, magic_bit); */
31968 +       v %= nhash->nh_num;
31969 +       return nhash->nh_head + v;
31970 +}
31971 +
31972 +static int au_nhash_test_name(struct au_vdir_destr *str, const char *name,
31973 +                             int nlen)
31974 +{
31975 +       return str->len == nlen && !memcmp(str->name, name, nlen);
31976 +}
31977 +
31978 +/* returns found or not */
31979 +int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen)
31980 +{
31981 +       struct hlist_head *head;
31982 +       struct au_vdir_wh *pos;
31983 +       struct au_vdir_destr *str;
31984 +
31985 +       head = au_name_hash(whlist, name, nlen);
31986 +       hlist_for_each_entry(pos, head, wh_hash) {
31987 +               str = &pos->wh_str;
31988 +               AuDbg("%.*s\n", str->len, str->name);
31989 +               if (au_nhash_test_name(str, name, nlen))
31990 +                       return 1;
31991 +       }
31992 +       return 0;
31993 +}
31994 +
31995 +/* returns found(true) or not */
31996 +static int test_known(struct au_nhash *delist, char *name, int nlen)
31997 +{
31998 +       struct hlist_head *head;
31999 +       struct au_vdir_dehstr *pos;
32000 +       struct au_vdir_destr *str;
32001 +
32002 +       head = au_name_hash(delist, name, nlen);
32003 +       hlist_for_each_entry(pos, head, hash) {
32004 +               str = pos->str;
32005 +               AuDbg("%.*s\n", str->len, str->name);
32006 +               if (au_nhash_test_name(str, name, nlen))
32007 +                       return 1;
32008 +       }
32009 +       return 0;
32010 +}
32011 +
32012 +static void au_shwh_init_wh(struct au_vdir_wh *wh, ino_t ino,
32013 +                           unsigned char d_type)
32014 +{
32015 +#ifdef CONFIG_AUFS_SHWH
32016 +       wh->wh_ino = ino;
32017 +       wh->wh_type = d_type;
32018 +#endif
32019 +}
32020 +
32021 +/* ---------------------------------------------------------------------- */
32022 +
32023 +int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
32024 +                      unsigned int d_type, aufs_bindex_t bindex,
32025 +                      unsigned char shwh)
32026 +{
32027 +       int err;
32028 +       struct au_vdir_destr *str;
32029 +       struct au_vdir_wh *wh;
32030 +
32031 +       AuDbg("%.*s\n", nlen, name);
32032 +       AuDebugOn(!whlist->nh_num || !whlist->nh_head);
32033 +
32034 +       err = -ENOMEM;
32035 +       wh = kmalloc(sizeof(*wh) + nlen, GFP_NOFS);
32036 +       if (unlikely(!wh))
32037 +               goto out;
32038 +
32039 +       err = 0;
32040 +       wh->wh_bindex = bindex;
32041 +       if (shwh)
32042 +               au_shwh_init_wh(wh, ino, d_type);
32043 +       str = &wh->wh_str;
32044 +       str->len = nlen;
32045 +       memcpy(str->name, name, nlen);
32046 +       hlist_add_head(&wh->wh_hash, au_name_hash(whlist, name, nlen));
32047 +       /* smp_mb(); */
32048 +
32049 +out:
32050 +       return err;
32051 +}
32052 +
32053 +static int append_deblk(struct au_vdir *vdir)
32054 +{
32055 +       int err;
32056 +       unsigned long ul;
32057 +       const unsigned int deblk_sz = vdir->vd_deblk_sz;
32058 +       union au_vdir_deblk_p p, deblk_end;
32059 +       unsigned char **o;
32060 +
32061 +       err = -ENOMEM;
32062 +       o = au_krealloc(vdir->vd_deblk, sizeof(*o) * (vdir->vd_nblk + 1),
32063 +                       GFP_NOFS, /*may_shrink*/0);
32064 +       if (unlikely(!o))
32065 +               goto out;
32066 +
32067 +       vdir->vd_deblk = o;
32068 +       p.deblk = kmalloc(deblk_sz, GFP_NOFS);
32069 +       if (p.deblk) {
32070 +               ul = vdir->vd_nblk++;
32071 +               vdir->vd_deblk[ul] = p.deblk;
32072 +               vdir->vd_last.ul = ul;
32073 +               vdir->vd_last.p.deblk = p.deblk;
32074 +               deblk_end.deblk = p.deblk + deblk_sz;
32075 +               err = set_deblk_end(&p, &deblk_end);
32076 +       }
32077 +
32078 +out:
32079 +       return err;
32080 +}
32081 +
32082 +static int append_de(struct au_vdir *vdir, char *name, int nlen, ino_t ino,
32083 +                    unsigned int d_type, struct au_nhash *delist)
32084 +{
32085 +       int err;
32086 +       unsigned int sz;
32087 +       const unsigned int deblk_sz = vdir->vd_deblk_sz;
32088 +       union au_vdir_deblk_p p, *room, deblk_end;
32089 +       struct au_vdir_dehstr *dehstr;
32090 +
32091 +       p.deblk = last_deblk(vdir);
32092 +       deblk_end.deblk = p.deblk + deblk_sz;
32093 +       room = &vdir->vd_last.p;
32094 +       AuDebugOn(room->deblk < p.deblk || deblk_end.deblk <= room->deblk
32095 +                 || !is_deblk_end(room, &deblk_end));
32096 +
32097 +       sz = calc_size(nlen);
32098 +       if (unlikely(sz > deblk_end.deblk - room->deblk)) {
32099 +               err = append_deblk(vdir);
32100 +               if (unlikely(err))
32101 +                       goto out;
32102 +
32103 +               p.deblk = last_deblk(vdir);
32104 +               deblk_end.deblk = p.deblk + deblk_sz;
32105 +               /* smp_mb(); */
32106 +               AuDebugOn(room->deblk != p.deblk);
32107 +       }
32108 +
32109 +       err = -ENOMEM;
32110 +       dehstr = au_cache_alloc_vdir_dehstr();
32111 +       if (unlikely(!dehstr))
32112 +               goto out;
32113 +
32114 +       dehstr->str = &room->de->de_str;
32115 +       hlist_add_head(&dehstr->hash, au_name_hash(delist, name, nlen));
32116 +       room->de->de_ino = ino;
32117 +       room->de->de_type = d_type;
32118 +       room->de->de_str.len = nlen;
32119 +       memcpy(room->de->de_str.name, name, nlen);
32120 +
32121 +       err = 0;
32122 +       room->deblk += sz;
32123 +       if (unlikely(set_deblk_end(room, &deblk_end)))
32124 +               err = append_deblk(vdir);
32125 +       /* smp_mb(); */
32126 +
32127 +out:
32128 +       return err;
32129 +}
32130 +
32131 +/* ---------------------------------------------------------------------- */
32132 +
32133 +void au_vdir_free(struct au_vdir *vdir)
32134 +{
32135 +       unsigned char **deblk;
32136 +
32137 +       deblk = vdir->vd_deblk;
32138 +       while (vdir->vd_nblk--)
32139 +               au_kfree_try_rcu(*deblk++);
32140 +       au_kfree_try_rcu(vdir->vd_deblk);
32141 +       au_cache_free_vdir(vdir);
32142 +}
32143 +
32144 +static struct au_vdir *alloc_vdir(struct file *file)
32145 +{
32146 +       struct au_vdir *vdir;
32147 +       struct super_block *sb;
32148 +       int err;
32149 +
32150 +       sb = file->f_path.dentry->d_sb;
32151 +       SiMustAnyLock(sb);
32152 +
32153 +       err = -ENOMEM;
32154 +       vdir = au_cache_alloc_vdir();
32155 +       if (unlikely(!vdir))
32156 +               goto out;
32157 +
32158 +       vdir->vd_deblk = kzalloc(sizeof(*vdir->vd_deblk), GFP_NOFS);
32159 +       if (unlikely(!vdir->vd_deblk))
32160 +               goto out_free;
32161 +
32162 +       vdir->vd_deblk_sz = au_sbi(sb)->si_rdblk;
32163 +       if (!vdir->vd_deblk_sz) {
32164 +               /* estimate the appropriate size for deblk */
32165 +               vdir->vd_deblk_sz = au_dir_size(file, /*dentry*/NULL);
32166 +               /* pr_info("vd_deblk_sz %u\n", vdir->vd_deblk_sz); */
32167 +       }
32168 +       vdir->vd_nblk = 0;
32169 +       vdir->vd_version = 0;
32170 +       vdir->vd_jiffy = 0;
32171 +       err = append_deblk(vdir);
32172 +       if (!err)
32173 +               return vdir; /* success */
32174 +
32175 +       au_kfree_try_rcu(vdir->vd_deblk);
32176 +
32177 +out_free:
32178 +       au_cache_free_vdir(vdir);
32179 +out:
32180 +       vdir = ERR_PTR(err);
32181 +       return vdir;
32182 +}
32183 +
32184 +static int reinit_vdir(struct au_vdir *vdir)
32185 +{
32186 +       int err;
32187 +       union au_vdir_deblk_p p, deblk_end;
32188 +
32189 +       while (vdir->vd_nblk > 1) {
32190 +               au_kfree_try_rcu(vdir->vd_deblk[vdir->vd_nblk - 1]);
32191 +               /* vdir->vd_deblk[vdir->vd_nblk - 1] = NULL; */
32192 +               vdir->vd_nblk--;
32193 +       }
32194 +       p.deblk = vdir->vd_deblk[0];
32195 +       deblk_end.deblk = p.deblk + vdir->vd_deblk_sz;
32196 +       err = set_deblk_end(&p, &deblk_end);
32197 +       /* keep vd_dblk_sz */
32198 +       vdir->vd_last.ul = 0;
32199 +       vdir->vd_last.p.deblk = vdir->vd_deblk[0];
32200 +       vdir->vd_version = 0;
32201 +       vdir->vd_jiffy = 0;
32202 +       /* smp_mb(); */
32203 +       return err;
32204 +}
32205 +
32206 +/* ---------------------------------------------------------------------- */
32207 +
32208 +#define AuFillVdir_CALLED      1
32209 +#define AuFillVdir_WHABLE      (1 << 1)
32210 +#define AuFillVdir_SHWH                (1 << 2)
32211 +#define au_ftest_fillvdir(flags, name) ((flags) & AuFillVdir_##name)
32212 +#define au_fset_fillvdir(flags, name) \
32213 +       do { (flags) |= AuFillVdir_##name; } while (0)
32214 +#define au_fclr_fillvdir(flags, name) \
32215 +       do { (flags) &= ~AuFillVdir_##name; } while (0)
32216 +
32217 +#ifndef CONFIG_AUFS_SHWH
32218 +#undef AuFillVdir_SHWH
32219 +#define AuFillVdir_SHWH                0
32220 +#endif
32221 +
32222 +struct fillvdir_arg {
32223 +       struct dir_context      ctx;
32224 +       struct file             *file;
32225 +       struct au_vdir          *vdir;
32226 +       struct au_nhash         delist;
32227 +       struct au_nhash         whlist;
32228 +       aufs_bindex_t           bindex;
32229 +       unsigned int            flags;
32230 +       int                     err;
32231 +};
32232 +
32233 +static int fillvdir(struct dir_context *ctx, const char *__name, int nlen,
32234 +                   loff_t offset __maybe_unused, u64 h_ino,
32235 +                   unsigned int d_type)
32236 +{
32237 +       struct fillvdir_arg *arg = container_of(ctx, struct fillvdir_arg, ctx);
32238 +       char *name = (void *)__name;
32239 +       struct super_block *sb;
32240 +       ino_t ino;
32241 +       const unsigned char shwh = !!au_ftest_fillvdir(arg->flags, SHWH);
32242 +
32243 +       arg->err = 0;
32244 +       sb = arg->file->f_path.dentry->d_sb;
32245 +       au_fset_fillvdir(arg->flags, CALLED);
32246 +       /* smp_mb(); */
32247 +       if (nlen <= AUFS_WH_PFX_LEN
32248 +           || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
32249 +               if (test_known(&arg->delist, name, nlen)
32250 +                   || au_nhash_test_known_wh(&arg->whlist, name, nlen))
32251 +                       goto out; /* already exists or whiteouted */
32252 +
32253 +               arg->err = au_ino(sb, arg->bindex, h_ino, d_type, &ino);
32254 +               if (!arg->err) {
32255 +                       if (unlikely(nlen > AUFS_MAX_NAMELEN))
32256 +                               d_type = DT_UNKNOWN;
32257 +                       arg->err = append_de(arg->vdir, name, nlen, ino,
32258 +                                            d_type, &arg->delist);
32259 +               }
32260 +       } else if (au_ftest_fillvdir(arg->flags, WHABLE)) {
32261 +               name += AUFS_WH_PFX_LEN;
32262 +               nlen -= AUFS_WH_PFX_LEN;
32263 +               if (au_nhash_test_known_wh(&arg->whlist, name, nlen))
32264 +                       goto out; /* already whiteouted */
32265 +
32266 +               ino = 0; /* just to suppress a warning */
32267 +               if (shwh)
32268 +                       arg->err = au_wh_ino(sb, arg->bindex, h_ino, d_type,
32269 +                                            &ino);
32270 +               if (!arg->err) {
32271 +                       if (nlen <= AUFS_MAX_NAMELEN + AUFS_WH_PFX_LEN)
32272 +                               d_type = DT_UNKNOWN;
32273 +                       arg->err = au_nhash_append_wh
32274 +                               (&arg->whlist, name, nlen, ino, d_type,
32275 +                                arg->bindex, shwh);
32276 +               }
32277 +       }
32278 +
32279 +out:
32280 +       if (!arg->err)
32281 +               arg->vdir->vd_jiffy = jiffies;
32282 +       /* smp_mb(); */
32283 +       AuTraceErr(arg->err);
32284 +       return arg->err;
32285 +}
32286 +
32287 +static int au_handle_shwh(struct super_block *sb, struct au_vdir *vdir,
32288 +                         struct au_nhash *whlist, struct au_nhash *delist)
32289 +{
32290 +#ifdef CONFIG_AUFS_SHWH
32291 +       int err;
32292 +       unsigned int nh, u;
32293 +       struct hlist_head *head;
32294 +       struct au_vdir_wh *pos;
32295 +       struct hlist_node *n;
32296 +       char *p, *o;
32297 +       struct au_vdir_destr *destr;
32298 +
32299 +       AuDebugOn(!au_opt_test(au_mntflags(sb), SHWH));
32300 +
32301 +       err = -ENOMEM;
32302 +       o = p = (void *)__get_free_page(GFP_NOFS);
32303 +       if (unlikely(!p))
32304 +               goto out;
32305 +
32306 +       err = 0;
32307 +       nh = whlist->nh_num;
32308 +       memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
32309 +       p += AUFS_WH_PFX_LEN;
32310 +       for (u = 0; u < nh; u++) {
32311 +               head = whlist->nh_head + u;
32312 +               hlist_for_each_entry_safe(pos, n, head, wh_hash) {
32313 +                       destr = &pos->wh_str;
32314 +                       memcpy(p, destr->name, destr->len);
32315 +                       err = append_de(vdir, o, destr->len + AUFS_WH_PFX_LEN,
32316 +                                       pos->wh_ino, pos->wh_type, delist);
32317 +                       if (unlikely(err))
32318 +                               break;
32319 +               }
32320 +       }
32321 +
32322 +       free_page((unsigned long)o);
32323 +
32324 +out:
32325 +       AuTraceErr(err);
32326 +       return err;
32327 +#else
32328 +       return 0;
32329 +#endif
32330 +}
32331 +
32332 +static int au_do_read_vdir(struct fillvdir_arg *arg)
32333 +{
32334 +       int err;
32335 +       unsigned int rdhash;
32336 +       loff_t offset;
32337 +       aufs_bindex_t bbot, bindex, btop;
32338 +       unsigned char shwh;
32339 +       struct file *hf, *file;
32340 +       struct super_block *sb;
32341 +
32342 +       file = arg->file;
32343 +       sb = file->f_path.dentry->d_sb;
32344 +       SiMustAnyLock(sb);
32345 +
32346 +       rdhash = au_sbi(sb)->si_rdhash;
32347 +       if (!rdhash)
32348 +               rdhash = au_rdhash_est(au_dir_size(file, /*dentry*/NULL));
32349 +       err = au_nhash_alloc(&arg->delist, rdhash, GFP_NOFS);
32350 +       if (unlikely(err))
32351 +               goto out;
32352 +       err = au_nhash_alloc(&arg->whlist, rdhash, GFP_NOFS);
32353 +       if (unlikely(err))
32354 +               goto out_delist;
32355 +
32356 +       err = 0;
32357 +       arg->flags = 0;
32358 +       shwh = 0;
32359 +       if (au_opt_test(au_mntflags(sb), SHWH)) {
32360 +               shwh = 1;
32361 +               au_fset_fillvdir(arg->flags, SHWH);
32362 +       }
32363 +       btop = au_fbtop(file);
32364 +       bbot = au_fbbot_dir(file);
32365 +       for (bindex = btop; !err && bindex <= bbot; bindex++) {
32366 +               hf = au_hf_dir(file, bindex);
32367 +               if (!hf)
32368 +                       continue;
32369 +
32370 +               offset = vfsub_llseek(hf, 0, SEEK_SET);
32371 +               err = offset;
32372 +               if (unlikely(offset))
32373 +                       break;
32374 +
32375 +               arg->bindex = bindex;
32376 +               au_fclr_fillvdir(arg->flags, WHABLE);
32377 +               if (shwh
32378 +                   || (bindex != bbot
32379 +                       && au_br_whable(au_sbr_perm(sb, bindex))))
32380 +                       au_fset_fillvdir(arg->flags, WHABLE);
32381 +               do {
32382 +                       arg->err = 0;
32383 +                       au_fclr_fillvdir(arg->flags, CALLED);
32384 +                       /* smp_mb(); */
32385 +                       err = vfsub_iterate_dir(hf, &arg->ctx);
32386 +                       if (err >= 0)
32387 +                               err = arg->err;
32388 +               } while (!err && au_ftest_fillvdir(arg->flags, CALLED));
32389 +
32390 +               /*
32391 +                * dir_relax() may be good for concurrency, but aufs should not
32392 +                * use it since it will cause a lockdep problem.
32393 +                */
32394 +       }
32395 +
32396 +       if (!err && shwh)
32397 +               err = au_handle_shwh(sb, arg->vdir, &arg->whlist, &arg->delist);
32398 +
32399 +       au_nhash_wh_free(&arg->whlist);
32400 +
32401 +out_delist:
32402 +       au_nhash_de_free(&arg->delist);
32403 +out:
32404 +       return err;
32405 +}
32406 +
32407 +static int read_vdir(struct file *file, int may_read)
32408 +{
32409 +       int err;
32410 +       unsigned long expire;
32411 +       unsigned char do_read;
32412 +       struct fillvdir_arg arg = {
32413 +               .ctx = {
32414 +                       .actor = fillvdir
32415 +               }
32416 +       };
32417 +       struct inode *inode;
32418 +       struct au_vdir *vdir, *allocated;
32419 +
32420 +       err = 0;
32421 +       inode = file_inode(file);
32422 +       IMustLock(inode);
32423 +       IiMustWriteLock(inode);
32424 +       SiMustAnyLock(inode->i_sb);
32425 +
32426 +       allocated = NULL;
32427 +       do_read = 0;
32428 +       expire = au_sbi(inode->i_sb)->si_rdcache;
32429 +       vdir = au_ivdir(inode);
32430 +       if (!vdir) {
32431 +               do_read = 1;
32432 +               vdir = alloc_vdir(file);
32433 +               err = PTR_ERR(vdir);
32434 +               if (IS_ERR(vdir))
32435 +                       goto out;
32436 +               err = 0;
32437 +               allocated = vdir;
32438 +       } else if (may_read
32439 +                  && (!inode_eq_iversion(inode, vdir->vd_version)
32440 +                      || time_after(jiffies, vdir->vd_jiffy + expire))) {
32441 +               do_read = 1;
32442 +               err = reinit_vdir(vdir);
32443 +               if (unlikely(err))
32444 +                       goto out;
32445 +       }
32446 +
32447 +       if (!do_read)
32448 +               return 0; /* success */
32449 +
32450 +       arg.file = file;
32451 +       arg.vdir = vdir;
32452 +       err = au_do_read_vdir(&arg);
32453 +       if (!err) {
32454 +               /* file->f_pos = 0; */ /* todo: ctx->pos? */
32455 +               vdir->vd_version = inode_query_iversion(inode);
32456 +               vdir->vd_last.ul = 0;
32457 +               vdir->vd_last.p.deblk = vdir->vd_deblk[0];
32458 +               if (allocated)
32459 +                       au_set_ivdir(inode, allocated);
32460 +       } else if (allocated)
32461 +               au_vdir_free(allocated);
32462 +
32463 +out:
32464 +       return err;
32465 +}
32466 +
32467 +static int copy_vdir(struct au_vdir *tgt, struct au_vdir *src)
32468 +{
32469 +       int err, rerr;
32470 +       unsigned long ul, n;
32471 +       const unsigned int deblk_sz = src->vd_deblk_sz;
32472 +
32473 +       AuDebugOn(tgt->vd_nblk != 1);
32474 +
32475 +       err = -ENOMEM;
32476 +       if (tgt->vd_nblk < src->vd_nblk) {
32477 +               unsigned char **p;
32478 +
32479 +               p = au_krealloc(tgt->vd_deblk, sizeof(*p) * src->vd_nblk,
32480 +                               GFP_NOFS, /*may_shrink*/0);
32481 +               if (unlikely(!p))
32482 +                       goto out;
32483 +               tgt->vd_deblk = p;
32484 +       }
32485 +
32486 +       if (tgt->vd_deblk_sz != deblk_sz) {
32487 +               unsigned char *p;
32488 +
32489 +               tgt->vd_deblk_sz = deblk_sz;
32490 +               p = au_krealloc(tgt->vd_deblk[0], deblk_sz, GFP_NOFS,
32491 +                               /*may_shrink*/1);
32492 +               if (unlikely(!p))
32493 +                       goto out;
32494 +               tgt->vd_deblk[0] = p;
32495 +       }
32496 +       memcpy(tgt->vd_deblk[0], src->vd_deblk[0], deblk_sz);
32497 +       tgt->vd_version = src->vd_version;
32498 +       tgt->vd_jiffy = src->vd_jiffy;
32499 +
32500 +       n = src->vd_nblk;
32501 +       for (ul = 1; ul < n; ul++) {
32502 +               tgt->vd_deblk[ul] = kmemdup(src->vd_deblk[ul], deblk_sz,
32503 +                                           GFP_NOFS);
32504 +               if (unlikely(!tgt->vd_deblk[ul]))
32505 +                       goto out;
32506 +               tgt->vd_nblk++;
32507 +       }
32508 +       tgt->vd_nblk = n;
32509 +       tgt->vd_last.ul = tgt->vd_last.ul;
32510 +       tgt->vd_last.p.deblk = tgt->vd_deblk[tgt->vd_last.ul];
32511 +       tgt->vd_last.p.deblk += src->vd_last.p.deblk
32512 +               - src->vd_deblk[src->vd_last.ul];
32513 +       /* smp_mb(); */
32514 +       return 0; /* success */
32515 +
32516 +out:
32517 +       rerr = reinit_vdir(tgt);
32518 +       BUG_ON(rerr);
32519 +       return err;
32520 +}
32521 +
32522 +int au_vdir_init(struct file *file)
32523 +{
32524 +       int err;
32525 +       struct inode *inode;
32526 +       struct au_vdir *vdir_cache, *allocated;
32527 +
32528 +       /* test file->f_pos here instead of ctx->pos */
32529 +       err = read_vdir(file, !file->f_pos);
32530 +       if (unlikely(err))
32531 +               goto out;
32532 +
32533 +       allocated = NULL;
32534 +       vdir_cache = au_fvdir_cache(file);
32535 +       if (!vdir_cache) {
32536 +               vdir_cache = alloc_vdir(file);
32537 +               err = PTR_ERR(vdir_cache);
32538 +               if (IS_ERR(vdir_cache))
32539 +                       goto out;
32540 +               allocated = vdir_cache;
32541 +       } else if (!file->f_pos && vdir_cache->vd_version != file->f_version) {
32542 +               /* test file->f_pos here instead of ctx->pos */
32543 +               err = reinit_vdir(vdir_cache);
32544 +               if (unlikely(err))
32545 +                       goto out;
32546 +       } else
32547 +               return 0; /* success */
32548 +
32549 +       inode = file_inode(file);
32550 +       err = copy_vdir(vdir_cache, au_ivdir(inode));
32551 +       if (!err) {
32552 +               file->f_version = inode_query_iversion(inode);
32553 +               if (allocated)
32554 +                       au_set_fvdir_cache(file, allocated);
32555 +       } else if (allocated)
32556 +               au_vdir_free(allocated);
32557 +
32558 +out:
32559 +       return err;
32560 +}
32561 +
32562 +static loff_t calc_offset(struct au_vdir *vdir)
32563 +{
32564 +       loff_t offset;
32565 +       union au_vdir_deblk_p p;
32566 +
32567 +       p.deblk = vdir->vd_deblk[vdir->vd_last.ul];
32568 +       offset = vdir->vd_last.p.deblk - p.deblk;
32569 +       offset += vdir->vd_deblk_sz * vdir->vd_last.ul;
32570 +       return offset;
32571 +}
32572 +
32573 +/* returns true or false */
32574 +static int seek_vdir(struct file *file, struct dir_context *ctx)
32575 +{
32576 +       int valid;
32577 +       unsigned int deblk_sz;
32578 +       unsigned long ul, n;
32579 +       loff_t offset;
32580 +       union au_vdir_deblk_p p, deblk_end;
32581 +       struct au_vdir *vdir_cache;
32582 +
32583 +       valid = 1;
32584 +       vdir_cache = au_fvdir_cache(file);
32585 +       offset = calc_offset(vdir_cache);
32586 +       AuDbg("offset %lld\n", offset);
32587 +       if (ctx->pos == offset)
32588 +               goto out;
32589 +
32590 +       vdir_cache->vd_last.ul = 0;
32591 +       vdir_cache->vd_last.p.deblk = vdir_cache->vd_deblk[0];
32592 +       if (!ctx->pos)
32593 +               goto out;
32594 +
32595 +       valid = 0;
32596 +       deblk_sz = vdir_cache->vd_deblk_sz;
32597 +       ul = div64_u64(ctx->pos, deblk_sz);
32598 +       AuDbg("ul %lu\n", ul);
32599 +       if (ul >= vdir_cache->vd_nblk)
32600 +               goto out;
32601 +
32602 +       n = vdir_cache->vd_nblk;
32603 +       for (; ul < n; ul++) {
32604 +               p.deblk = vdir_cache->vd_deblk[ul];
32605 +               deblk_end.deblk = p.deblk + deblk_sz;
32606 +               offset = ul;
32607 +               offset *= deblk_sz;
32608 +               while (!is_deblk_end(&p, &deblk_end) && offset < ctx->pos) {
32609 +                       unsigned int l;
32610 +
32611 +                       l = calc_size(p.de->de_str.len);
32612 +                       offset += l;
32613 +                       p.deblk += l;
32614 +               }
32615 +               if (!is_deblk_end(&p, &deblk_end)) {
32616 +                       valid = 1;
32617 +                       vdir_cache->vd_last.ul = ul;
32618 +                       vdir_cache->vd_last.p = p;
32619 +                       break;
32620 +               }
32621 +       }
32622 +
32623 +out:
32624 +       /* smp_mb(); */
32625 +       if (!valid)
32626 +               AuDbg("valid %d\n", !valid);
32627 +       return valid;
32628 +}
32629 +
32630 +int au_vdir_fill_de(struct file *file, struct dir_context *ctx)
32631 +{
32632 +       unsigned int l, deblk_sz;
32633 +       union au_vdir_deblk_p deblk_end;
32634 +       struct au_vdir *vdir_cache;
32635 +       struct au_vdir_de *de;
32636 +
32637 +       if (!seek_vdir(file, ctx))
32638 +               return 0;
32639 +
32640 +       vdir_cache = au_fvdir_cache(file);
32641 +       deblk_sz = vdir_cache->vd_deblk_sz;
32642 +       while (1) {
32643 +               deblk_end.deblk = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
32644 +               deblk_end.deblk += deblk_sz;
32645 +               while (!is_deblk_end(&vdir_cache->vd_last.p, &deblk_end)) {
32646 +                       de = vdir_cache->vd_last.p.de;
32647 +                       AuDbg("%.*s, off%lld, i%lu, dt%d\n",
32648 +                             de->de_str.len, de->de_str.name, ctx->pos,
32649 +                             (unsigned long)de->de_ino, de->de_type);
32650 +                       if (unlikely(!dir_emit(ctx, de->de_str.name,
32651 +                                              de->de_str.len, de->de_ino,
32652 +                                              de->de_type))) {
32653 +                               /* todo: ignore the error caused by udba? */
32654 +                               /* return err; */
32655 +                               return 0;
32656 +                       }
32657 +
32658 +                       l = calc_size(de->de_str.len);
32659 +                       vdir_cache->vd_last.p.deblk += l;
32660 +                       ctx->pos += l;
32661 +               }
32662 +               if (vdir_cache->vd_last.ul < vdir_cache->vd_nblk - 1) {
32663 +                       vdir_cache->vd_last.ul++;
32664 +                       vdir_cache->vd_last.p.deblk
32665 +                               = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
32666 +                       ctx->pos = deblk_sz * vdir_cache->vd_last.ul;
32667 +                       continue;
32668 +               }
32669 +               break;
32670 +       }
32671 +
32672 +       /* smp_mb(); */
32673 +       return 0;
32674 +}
32675 diff -urN /usr/share/empty/fs/aufs/vfsub.c linux/fs/aufs/vfsub.c
32676 --- /usr/share/empty/fs/aufs/vfsub.c    1970-01-01 01:00:00.000000000 +0100
32677 +++ linux/fs/aufs/vfsub.c       2021-11-01 23:48:34.213025928 +0100
32678 @@ -0,0 +1,919 @@
32679 +// SPDX-License-Identifier: GPL-2.0
32680 +/*
32681 + * Copyright (C) 2005-2021 Junjiro R. Okajima
32682 + *
32683 + * This program, aufs is free software; you can redistribute it and/or modify
32684 + * it under the terms of the GNU General Public License as published by
32685 + * the Free Software Foundation; either version 2 of the License, or
32686 + * (at your option) any later version.
32687 + *
32688 + * This program is distributed in the hope that it will be useful,
32689 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
32690 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
32691 + * GNU General Public License for more details.
32692 + *
32693 + * You should have received a copy of the GNU General Public License
32694 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
32695 + */
32696 +
32697 +/*
32698 + * sub-routines for VFS
32699 + */
32700 +
32701 +#include <linux/mnt_namespace.h>
32702 +#include <linux/namei.h>
32703 +#include <linux/nsproxy.h>
32704 +#include <linux/security.h>
32705 +#include <linux/splice.h>
32706 +#include "aufs.h"
32707 +
32708 +#ifdef CONFIG_AUFS_BR_FUSE
32709 +int vfsub_test_mntns(struct vfsmount *mnt, struct super_block *h_sb)
32710 +{
32711 +       if (!au_test_fuse(h_sb) || !au_userns)
32712 +               return 0;
32713 +
32714 +       return is_current_mnt_ns(mnt) ? 0 : -EACCES;
32715 +}
32716 +#endif
32717 +
32718 +int vfsub_sync_filesystem(struct super_block *h_sb, int wait)
32719 +{
32720 +       int err;
32721 +
32722 +       lockdep_off();
32723 +       down_read(&h_sb->s_umount);
32724 +       err = __sync_filesystem(h_sb, wait);
32725 +       up_read(&h_sb->s_umount);
32726 +       lockdep_on();
32727 +
32728 +       return err;
32729 +}
32730 +
32731 +/* ---------------------------------------------------------------------- */
32732 +
32733 +int vfsub_update_h_iattr(struct path *h_path, int *did)
32734 +{
32735 +       int err;
32736 +       struct kstat st;
32737 +       struct super_block *h_sb;
32738 +
32739 +       /*
32740 +        * Always needs h_path->mnt for LSM or FUSE branch.
32741 +        */
32742 +       AuDebugOn(!h_path->mnt);
32743 +
32744 +       /* for remote fs, leave work for its getattr or d_revalidate */
32745 +       /* for bad i_attr fs, handle them in aufs_getattr() */
32746 +       /* still some fs may acquire i_mutex. we need to skip them */
32747 +       err = 0;
32748 +       if (!did)
32749 +               did = &err;
32750 +       h_sb = h_path->dentry->d_sb;
32751 +       *did = (!au_test_fs_remote(h_sb) && au_test_fs_refresh_iattr(h_sb));
32752 +       if (*did)
32753 +               err = vfsub_getattr(h_path, &st);
32754 +
32755 +       return err;
32756 +}
32757 +
32758 +/* ---------------------------------------------------------------------- */
32759 +
32760 +struct file *vfsub_dentry_open(struct path *path, int flags)
32761 +{
32762 +       return dentry_open(path, flags /* | __FMODE_NONOTIFY */,
32763 +                          current_cred());
32764 +}
32765 +
32766 +struct file *vfsub_filp_open(const char *path, int oflags, int mode)
32767 +{
32768 +       struct file *file;
32769 +
32770 +       lockdep_off();
32771 +       file = filp_open(path,
32772 +                        oflags /* | __FMODE_NONOTIFY */,
32773 +                        mode);
32774 +       lockdep_on();
32775 +       if (IS_ERR(file))
32776 +               goto out;
32777 +       vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
32778 +
32779 +out:
32780 +       return file;
32781 +}
32782 +
32783 +/*
32784 + * Ideally this function should call VFS:do_last() in order to keep all its
32785 + * checkings. But it is very hard for aufs to regenerate several VFS internal
32786 + * structure such as nameidata. This is a second (or third) best approach.
32787 + * cf. linux/fs/namei.c:do_last(), lookup_open() and atomic_open().
32788 + */
32789 +int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
32790 +                     struct vfsub_aopen_args *args)
32791 +{
32792 +       int err;
32793 +       struct au_branch *br = args->br;
32794 +       struct file *file = args->file;
32795 +       /* copied from linux/fs/namei.c:atomic_open() */
32796 +       struct dentry *const DENTRY_NOT_SET = (void *)-1UL;
32797 +
32798 +       IMustLock(dir);
32799 +       AuDebugOn(!dir->i_op->atomic_open);
32800 +
32801 +       err = au_br_test_oflag(args->open_flag, br);
32802 +       if (unlikely(err))
32803 +               goto out;
32804 +
32805 +       au_lcnt_inc(&br->br_nfiles);
32806 +       file->f_path.dentry = DENTRY_NOT_SET;
32807 +       file->f_path.mnt = au_br_mnt(br);
32808 +       AuDbg("%ps\n", dir->i_op->atomic_open);
32809 +       err = dir->i_op->atomic_open(dir, dentry, file, args->open_flag,
32810 +                                    args->create_mode);
32811 +       if (unlikely(err < 0)) {
32812 +               au_lcnt_dec(&br->br_nfiles);
32813 +               goto out;
32814 +       }
32815 +
32816 +       /* temporary workaround for nfsv4 branch */
32817 +       if (au_test_nfs(dir->i_sb))
32818 +               nfs_mark_for_revalidate(dir);
32819 +
32820 +       if (file->f_mode & FMODE_CREATED)
32821 +               fsnotify_create(dir, dentry);
32822 +       if (!(file->f_mode & FMODE_OPENED)) {
32823 +               au_lcnt_dec(&br->br_nfiles);
32824 +               goto out;
32825 +       }
32826 +
32827 +       /* todo: call VFS:may_open() here */
32828 +       /* todo: ima_file_check() too? */
32829 +       if (!err && (args->open_flag & __FMODE_EXEC))
32830 +               err = deny_write_access(file);
32831 +       if (!err)
32832 +               fsnotify_open(file);
32833 +       else
32834 +               au_lcnt_dec(&br->br_nfiles);
32835 +       /* note that the file is created and still opened */
32836 +
32837 +out:
32838 +       return err;
32839 +}
32840 +
32841 +int vfsub_kern_path(const char *name, unsigned int flags, struct path *path)
32842 +{
32843 +       int err;
32844 +
32845 +       err = kern_path(name, flags, path);
32846 +       if (!err && d_is_positive(path->dentry))
32847 +               vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
32848 +       return err;
32849 +}
32850 +
32851 +struct dentry *vfsub_lookup_one_len_unlocked(const char *name,
32852 +                                            struct path *ppath, int len)
32853 +{
32854 +       struct path path;
32855 +
32856 +       path.dentry = lookup_one_len_unlocked(name, ppath->dentry, len);
32857 +       if (IS_ERR(path.dentry))
32858 +               goto out;
32859 +       if (d_is_positive(path.dentry)) {
32860 +               path.mnt = ppath->mnt;
32861 +               vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
32862 +       }
32863 +
32864 +out:
32865 +       AuTraceErrPtr(path.dentry);
32866 +       return path.dentry;
32867 +}
32868 +
32869 +struct dentry *vfsub_lookup_one_len(const char *name, struct path *ppath,
32870 +                                   int len)
32871 +{
32872 +       struct path path;
32873 +
32874 +       /* VFS checks it too, but by WARN_ON_ONCE() */
32875 +       IMustLock(d_inode(ppath->dentry));
32876 +
32877 +       path.dentry = lookup_one_len(name, ppath->dentry, len);
32878 +       if (IS_ERR(path.dentry))
32879 +               goto out;
32880 +       if (d_is_positive(path.dentry)) {
32881 +               path.mnt = ppath->mnt;
32882 +               vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
32883 +       }
32884 +
32885 +out:
32886 +       AuTraceErrPtr(path.dentry);
32887 +       return path.dentry;
32888 +}
32889 +
32890 +void vfsub_call_lkup_one(void *args)
32891 +{
32892 +       struct vfsub_lkup_one_args *a = args;
32893 +       *a->errp = vfsub_lkup_one(a->name, a->ppath);
32894 +}
32895 +
32896 +/* ---------------------------------------------------------------------- */
32897 +
32898 +struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
32899 +                                struct dentry *d2, struct au_hinode *hdir2)
32900 +{
32901 +       struct dentry *d;
32902 +
32903 +       lockdep_off();
32904 +       d = lock_rename(d1, d2);
32905 +       lockdep_on();
32906 +       au_hn_suspend(hdir1);
32907 +       if (hdir1 != hdir2)
32908 +               au_hn_suspend(hdir2);
32909 +
32910 +       return d;
32911 +}
32912 +
32913 +void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
32914 +                        struct dentry *d2, struct au_hinode *hdir2)
32915 +{
32916 +       au_hn_resume(hdir1);
32917 +       if (hdir1 != hdir2)
32918 +               au_hn_resume(hdir2);
32919 +       lockdep_off();
32920 +       unlock_rename(d1, d2);
32921 +       lockdep_on();
32922 +}
32923 +
32924 +/* ---------------------------------------------------------------------- */
32925 +
32926 +int vfsub_create(struct inode *dir, struct path *path, int mode, bool want_excl)
32927 +{
32928 +       int err;
32929 +       struct dentry *d;
32930 +       struct user_namespace *userns;
32931 +
32932 +       IMustLock(dir);
32933 +
32934 +       d = path->dentry;
32935 +       path->dentry = d->d_parent;
32936 +       err = security_path_mknod(path, d, mode, 0);
32937 +       path->dentry = d;
32938 +       if (unlikely(err))
32939 +               goto out;
32940 +       userns = mnt_user_ns(path->mnt);
32941 +
32942 +       lockdep_off();
32943 +       err = vfs_create(userns, dir, path->dentry, mode, want_excl);
32944 +       lockdep_on();
32945 +       if (!err) {
32946 +               struct path tmp = *path;
32947 +               int did;
32948 +
32949 +               vfsub_update_h_iattr(&tmp, &did);
32950 +               if (did) {
32951 +                       tmp.dentry = path->dentry->d_parent;
32952 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
32953 +               }
32954 +               /*ignore*/
32955 +       }
32956 +
32957 +out:
32958 +       return err;
32959 +}
32960 +
32961 +int vfsub_symlink(struct inode *dir, struct path *path, const char *symname)
32962 +{
32963 +       int err;
32964 +       struct dentry *d;
32965 +       struct user_namespace *userns;
32966 +
32967 +       IMustLock(dir);
32968 +
32969 +       d = path->dentry;
32970 +       path->dentry = d->d_parent;
32971 +       err = security_path_symlink(path, d, symname);
32972 +       path->dentry = d;
32973 +       if (unlikely(err))
32974 +               goto out;
32975 +       userns = mnt_user_ns(path->mnt);
32976 +
32977 +       lockdep_off();
32978 +       err = vfs_symlink(userns, dir, path->dentry, symname);
32979 +       lockdep_on();
32980 +       if (!err) {
32981 +               struct path tmp = *path;
32982 +               int did;
32983 +
32984 +               vfsub_update_h_iattr(&tmp, &did);
32985 +               if (did) {
32986 +                       tmp.dentry = path->dentry->d_parent;
32987 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
32988 +               }
32989 +               /*ignore*/
32990 +       }
32991 +
32992 +out:
32993 +       return err;
32994 +}
32995 +
32996 +int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev)
32997 +{
32998 +       int err;
32999 +       struct dentry *d;
33000 +       struct user_namespace *userns;
33001 +
33002 +       IMustLock(dir);
33003 +
33004 +       d = path->dentry;
33005 +       path->dentry = d->d_parent;
33006 +       err = security_path_mknod(path, d, mode, new_encode_dev(dev));
33007 +       path->dentry = d;
33008 +       if (unlikely(err))
33009 +               goto out;
33010 +       userns = mnt_user_ns(path->mnt);
33011 +
33012 +       lockdep_off();
33013 +       err = vfs_mknod(userns, dir, path->dentry, mode, dev);
33014 +       lockdep_on();
33015 +       if (!err) {
33016 +               struct path tmp = *path;
33017 +               int did;
33018 +
33019 +               vfsub_update_h_iattr(&tmp, &did);
33020 +               if (did) {
33021 +                       tmp.dentry = path->dentry->d_parent;
33022 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33023 +               }
33024 +               /*ignore*/
33025 +       }
33026 +
33027 +out:
33028 +       return err;
33029 +}
33030 +
33031 +static int au_test_nlink(struct inode *inode)
33032 +{
33033 +       const unsigned int link_max = UINT_MAX >> 1; /* rough margin */
33034 +
33035 +       if (!au_test_fs_no_limit_nlink(inode->i_sb)
33036 +           || inode->i_nlink < link_max)
33037 +               return 0;
33038 +       return -EMLINK;
33039 +}
33040 +
33041 +int vfsub_link(struct dentry *src_dentry, struct inode *dir, struct path *path,
33042 +              struct inode **delegated_inode)
33043 +{
33044 +       int err;
33045 +       struct dentry *d;
33046 +       struct user_namespace *userns;
33047 +
33048 +       IMustLock(dir);
33049 +
33050 +       err = au_test_nlink(d_inode(src_dentry));
33051 +       if (unlikely(err))
33052 +               return err;
33053 +
33054 +       /* we don't call may_linkat() */
33055 +       d = path->dentry;
33056 +       path->dentry = d->d_parent;
33057 +       err = security_path_link(src_dentry, path, d);
33058 +       path->dentry = d;
33059 +       if (unlikely(err))
33060 +               goto out;
33061 +       userns = mnt_user_ns(path->mnt);
33062 +
33063 +       lockdep_off();
33064 +       err = vfs_link(src_dentry, userns, dir, path->dentry, delegated_inode);
33065 +       lockdep_on();
33066 +       if (!err) {
33067 +               struct path tmp = *path;
33068 +               int did;
33069 +
33070 +               /* fuse has different memory inode for the same inumber */
33071 +               vfsub_update_h_iattr(&tmp, &did);
33072 +               if (did) {
33073 +                       tmp.dentry = path->dentry->d_parent;
33074 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33075 +                       tmp.dentry = src_dentry;
33076 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33077 +               }
33078 +               /*ignore*/
33079 +       }
33080 +
33081 +out:
33082 +       return err;
33083 +}
33084 +
33085 +int vfsub_rename(struct inode *src_dir, struct dentry *src_dentry,
33086 +                struct inode *dir, struct path *path,
33087 +                struct inode **delegated_inode, unsigned int flags)
33088 +{
33089 +       int err;
33090 +       struct renamedata rd;
33091 +       struct path tmp = {
33092 +               .mnt    = path->mnt
33093 +       };
33094 +       struct dentry *d;
33095 +
33096 +       IMustLock(dir);
33097 +       IMustLock(src_dir);
33098 +
33099 +       d = path->dentry;
33100 +       path->dentry = d->d_parent;
33101 +       tmp.dentry = src_dentry->d_parent;
33102 +       err = security_path_rename(&tmp, src_dentry, path, d, /*flags*/0);
33103 +       path->dentry = d;
33104 +       if (unlikely(err))
33105 +               goto out;
33106 +
33107 +       rd.old_mnt_userns = mnt_user_ns(path->mnt);
33108 +       rd.old_dir = src_dir;
33109 +       rd.old_dentry = src_dentry;
33110 +       rd.new_mnt_userns = rd.old_mnt_userns;
33111 +       rd.new_dir = dir;
33112 +       rd.new_dentry = path->dentry;
33113 +       rd.delegated_inode = delegated_inode;
33114 +       rd.flags = flags;
33115 +       lockdep_off();
33116 +       err = vfs_rename(&rd);
33117 +       lockdep_on();
33118 +       if (!err) {
33119 +               int did;
33120 +
33121 +               tmp.dentry = d->d_parent;
33122 +               vfsub_update_h_iattr(&tmp, &did);
33123 +               if (did) {
33124 +                       tmp.dentry = src_dentry;
33125 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33126 +                       tmp.dentry = src_dentry->d_parent;
33127 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33128 +               }
33129 +               /*ignore*/
33130 +       }
33131 +
33132 +out:
33133 +       return err;
33134 +}
33135 +
33136 +int vfsub_mkdir(struct inode *dir, struct path *path, int mode)
33137 +{
33138 +       int err;
33139 +       struct dentry *d;
33140 +       struct user_namespace *userns;
33141 +
33142 +       IMustLock(dir);
33143 +
33144 +       d = path->dentry;
33145 +       path->dentry = d->d_parent;
33146 +       err = security_path_mkdir(path, d, mode);
33147 +       path->dentry = d;
33148 +       if (unlikely(err))
33149 +               goto out;
33150 +       userns = mnt_user_ns(path->mnt);
33151 +
33152 +       lockdep_off();
33153 +       err = vfs_mkdir(userns, dir, path->dentry, mode);
33154 +       lockdep_on();
33155 +       if (!err) {
33156 +               struct path tmp = *path;
33157 +               int did;
33158 +
33159 +               vfsub_update_h_iattr(&tmp, &did);
33160 +               if (did) {
33161 +                       tmp.dentry = path->dentry->d_parent;
33162 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33163 +               }
33164 +               /*ignore*/
33165 +       }
33166 +
33167 +out:
33168 +       return err;
33169 +}
33170 +
33171 +int vfsub_rmdir(struct inode *dir, struct path *path)
33172 +{
33173 +       int err;
33174 +       struct dentry *d;
33175 +       struct user_namespace *userns;
33176 +
33177 +       IMustLock(dir);
33178 +
33179 +       d = path->dentry;
33180 +       path->dentry = d->d_parent;
33181 +       err = security_path_rmdir(path, d);
33182 +       path->dentry = d;
33183 +       if (unlikely(err))
33184 +               goto out;
33185 +       userns = mnt_user_ns(path->mnt);
33186 +
33187 +       lockdep_off();
33188 +       err = vfs_rmdir(userns, dir, path->dentry);
33189 +       lockdep_on();
33190 +       if (!err) {
33191 +               struct path tmp = {
33192 +                       .dentry = path->dentry->d_parent,
33193 +                       .mnt    = path->mnt
33194 +               };
33195 +
33196 +               vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
33197 +       }
33198 +
33199 +out:
33200 +       return err;
33201 +}
33202 +
33203 +/* ---------------------------------------------------------------------- */
33204 +
33205 +/* todo: support mmap_sem? */
33206 +ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
33207 +                    loff_t *ppos)
33208 +{
33209 +       ssize_t err;
33210 +
33211 +       lockdep_off();
33212 +       err = vfs_read(file, ubuf, count, ppos);
33213 +       lockdep_on();
33214 +       if (err >= 0)
33215 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33216 +       return err;
33217 +}
33218 +
33219 +ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
33220 +                    loff_t *ppos)
33221 +{
33222 +       ssize_t err;
33223 +
33224 +       lockdep_off();
33225 +       err = kernel_read(file, kbuf, count, ppos);
33226 +       lockdep_on();
33227 +       AuTraceErr(err);
33228 +       if (err >= 0)
33229 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33230 +       return err;
33231 +}
33232 +
33233 +ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
33234 +                     loff_t *ppos)
33235 +{
33236 +       ssize_t err;
33237 +
33238 +       lockdep_off();
33239 +       err = vfs_write(file, ubuf, count, ppos);
33240 +       lockdep_on();
33241 +       if (err >= 0)
33242 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33243 +       return err;
33244 +}
33245 +
33246 +ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count, loff_t *ppos)
33247 +{
33248 +       ssize_t err;
33249 +
33250 +       lockdep_off();
33251 +       err = kernel_write(file, kbuf, count, ppos);
33252 +       lockdep_on();
33253 +       if (err >= 0)
33254 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33255 +       return err;
33256 +}
33257 +
33258 +int vfsub_flush(struct file *file, fl_owner_t id)
33259 +{
33260 +       int err;
33261 +
33262 +       err = 0;
33263 +       if (file->f_op->flush) {
33264 +               if (!au_test_nfs(file->f_path.dentry->d_sb))
33265 +                       err = file->f_op->flush(file, id);
33266 +               else {
33267 +                       lockdep_off();
33268 +                       err = file->f_op->flush(file, id);
33269 +                       lockdep_on();
33270 +               }
33271 +               if (!err)
33272 +                       vfsub_update_h_iattr(&file->f_path, /*did*/NULL);
33273 +               /*ignore*/
33274 +       }
33275 +       return err;
33276 +}
33277 +
33278 +int vfsub_iterate_dir(struct file *file, struct dir_context *ctx)
33279 +{
33280 +       int err;
33281 +
33282 +       AuDbg("%pD, ctx{%ps, %llu}\n", file, ctx->actor, ctx->pos);
33283 +
33284 +       lockdep_off();
33285 +       err = iterate_dir(file, ctx);
33286 +       lockdep_on();
33287 +       if (err >= 0)
33288 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33289 +
33290 +       return err;
33291 +}
33292 +
33293 +long vfsub_splice_to(struct file *in, loff_t *ppos,
33294 +                    struct pipe_inode_info *pipe, size_t len,
33295 +                    unsigned int flags)
33296 +{
33297 +       long err;
33298 +
33299 +       lockdep_off();
33300 +       err = do_splice_to(in, ppos, pipe, len, flags);
33301 +       lockdep_on();
33302 +       file_accessed(in);
33303 +       if (err >= 0)
33304 +               vfsub_update_h_iattr(&in->f_path, /*did*/NULL); /*ignore*/
33305 +       return err;
33306 +}
33307 +
33308 +long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
33309 +                      loff_t *ppos, size_t len, unsigned int flags)
33310 +{
33311 +       long err;
33312 +
33313 +       lockdep_off();
33314 +       err = do_splice_from(pipe, out, ppos, len, flags);
33315 +       lockdep_on();
33316 +       if (err >= 0)
33317 +               vfsub_update_h_iattr(&out->f_path, /*did*/NULL); /*ignore*/
33318 +       return err;
33319 +}
33320 +
33321 +int vfsub_fsync(struct file *file, struct path *path, int datasync)
33322 +{
33323 +       int err;
33324 +
33325 +       /* file can be NULL */
33326 +       lockdep_off();
33327 +       err = vfs_fsync(file, datasync);
33328 +       lockdep_on();
33329 +       if (!err) {
33330 +               if (!path) {
33331 +                       AuDebugOn(!file);
33332 +                       path = &file->f_path;
33333 +               }
33334 +               vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
33335 +       }
33336 +       return err;
33337 +}
33338 +
33339 +/* cf. open.c:do_sys_truncate() and do_sys_ftruncate() */
33340 +int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
33341 +               struct file *h_file)
33342 +{
33343 +       int err;
33344 +       struct inode *h_inode;
33345 +       struct super_block *h_sb;
33346 +       struct user_namespace *h_userns;
33347 +
33348 +       if (!h_file) {
33349 +               err = vfsub_truncate(h_path, length);
33350 +               goto out;
33351 +       }
33352 +
33353 +       h_inode = d_inode(h_path->dentry);
33354 +       h_sb = h_inode->i_sb;
33355 +       lockdep_off();
33356 +       sb_start_write(h_sb);
33357 +       lockdep_on();
33358 +       err = security_path_truncate(h_path);
33359 +       if (!err) {
33360 +               h_userns = mnt_user_ns(h_path->mnt);
33361 +               lockdep_off();
33362 +               err = do_truncate(h_userns, h_path->dentry, length, attr,
33363 +                                 h_file);
33364 +               lockdep_on();
33365 +       }
33366 +       lockdep_off();
33367 +       sb_end_write(h_sb);
33368 +       lockdep_on();
33369 +
33370 +out:
33371 +       return err;
33372 +}
33373 +
33374 +/* ---------------------------------------------------------------------- */
33375 +
33376 +struct au_vfsub_mkdir_args {
33377 +       int *errp;
33378 +       struct inode *dir;
33379 +       struct path *path;
33380 +       int mode;
33381 +};
33382 +
33383 +static void au_call_vfsub_mkdir(void *args)
33384 +{
33385 +       struct au_vfsub_mkdir_args *a = args;
33386 +       *a->errp = vfsub_mkdir(a->dir, a->path, a->mode);
33387 +}
33388 +
33389 +int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode)
33390 +{
33391 +       int err, do_sio, wkq_err;
33392 +       struct user_namespace *userns;
33393 +
33394 +       userns = mnt_user_ns(path->mnt);
33395 +       do_sio = au_test_h_perm_sio(userns, dir, MAY_EXEC | MAY_WRITE);
33396 +       if (!do_sio) {
33397 +               lockdep_off();
33398 +               err = vfsub_mkdir(dir, path, mode);
33399 +               lockdep_on();
33400 +       } else {
33401 +               struct au_vfsub_mkdir_args args = {
33402 +                       .errp   = &err,
33403 +                       .dir    = dir,
33404 +                       .path   = path,
33405 +                       .mode   = mode
33406 +               };
33407 +               wkq_err = au_wkq_wait(au_call_vfsub_mkdir, &args);
33408 +               if (unlikely(wkq_err))
33409 +                       err = wkq_err;
33410 +       }
33411 +
33412 +       return err;
33413 +}
33414 +
33415 +struct au_vfsub_rmdir_args {
33416 +       int *errp;
33417 +       struct inode *dir;
33418 +       struct path *path;
33419 +};
33420 +
33421 +static void au_call_vfsub_rmdir(void *args)
33422 +{
33423 +       struct au_vfsub_rmdir_args *a = args;
33424 +       *a->errp = vfsub_rmdir(a->dir, a->path);
33425 +}
33426 +
33427 +int vfsub_sio_rmdir(struct inode *dir, struct path *path)
33428 +{
33429 +       int err, do_sio, wkq_err;
33430 +       struct user_namespace *userns;
33431 +
33432 +       userns = mnt_user_ns(path->mnt);
33433 +       do_sio = au_test_h_perm_sio(userns, dir, MAY_EXEC | MAY_WRITE);
33434 +       if (!do_sio) {
33435 +               lockdep_off();
33436 +               err = vfsub_rmdir(dir, path);
33437 +               lockdep_on();
33438 +       } else {
33439 +               struct au_vfsub_rmdir_args args = {
33440 +                       .errp   = &err,
33441 +                       .dir    = dir,
33442 +                       .path   = path
33443 +               };
33444 +               wkq_err = au_wkq_wait(au_call_vfsub_rmdir, &args);
33445 +               if (unlikely(wkq_err))
33446 +                       err = wkq_err;
33447 +       }
33448 +
33449 +       return err;
33450 +}
33451 +
33452 +/* ---------------------------------------------------------------------- */
33453 +
33454 +struct notify_change_args {
33455 +       int *errp;
33456 +       struct path *path;
33457 +       struct iattr *ia;
33458 +       struct inode **delegated_inode;
33459 +};
33460 +
33461 +static void call_notify_change(void *args)
33462 +{
33463 +       struct notify_change_args *a = args;
33464 +       struct inode *h_inode;
33465 +       struct user_namespace *userns;
33466 +
33467 +       h_inode = d_inode(a->path->dentry);
33468 +       IMustLock(h_inode);
33469 +
33470 +       *a->errp = -EPERM;
33471 +       if (!IS_IMMUTABLE(h_inode) && !IS_APPEND(h_inode)) {
33472 +               userns = mnt_user_ns(a->path->mnt);
33473 +               lockdep_off();
33474 +               *a->errp = notify_change(userns, a->path->dentry, a->ia,
33475 +                                        a->delegated_inode);
33476 +               lockdep_on();
33477 +               if (!*a->errp)
33478 +                       vfsub_update_h_iattr(a->path, /*did*/NULL); /*ignore*/
33479 +       }
33480 +       AuTraceErr(*a->errp);
33481 +}
33482 +
33483 +int vfsub_notify_change(struct path *path, struct iattr *ia,
33484 +                       struct inode **delegated_inode)
33485 +{
33486 +       int err;
33487 +       struct notify_change_args args = {
33488 +               .errp                   = &err,
33489 +               .path                   = path,
33490 +               .ia                     = ia,
33491 +               .delegated_inode        = delegated_inode
33492 +       };
33493 +
33494 +       call_notify_change(&args);
33495 +
33496 +       return err;
33497 +}
33498 +
33499 +int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
33500 +                           struct inode **delegated_inode)
33501 +{
33502 +       int err, wkq_err;
33503 +       struct notify_change_args args = {
33504 +               .errp                   = &err,
33505 +               .path                   = path,
33506 +               .ia                     = ia,
33507 +               .delegated_inode        = delegated_inode
33508 +       };
33509 +
33510 +       wkq_err = au_wkq_wait(call_notify_change, &args);
33511 +       if (unlikely(wkq_err))
33512 +               err = wkq_err;
33513 +
33514 +       return err;
33515 +}
33516 +
33517 +/* ---------------------------------------------------------------------- */
33518 +
33519 +struct unlink_args {
33520 +       int *errp;
33521 +       struct inode *dir;
33522 +       struct path *path;
33523 +       struct inode **delegated_inode;
33524 +};
33525 +
33526 +static void call_unlink(void *args)
33527 +{
33528 +       struct unlink_args *a = args;
33529 +       struct dentry *d = a->path->dentry;
33530 +       struct inode *h_inode;
33531 +       struct user_namespace *userns;
33532 +       const int stop_sillyrename = (au_test_nfs(d->d_sb)
33533 +                                     && au_dcount(d) == 1);
33534 +
33535 +       IMustLock(a->dir);
33536 +
33537 +       a->path->dentry = d->d_parent;
33538 +       *a->errp = security_path_unlink(a->path, d);
33539 +       a->path->dentry = d;
33540 +       if (unlikely(*a->errp))
33541 +               return;
33542 +
33543 +       if (!stop_sillyrename)
33544 +               dget(d);
33545 +       h_inode = NULL;
33546 +       if (d_is_positive(d)) {
33547 +               h_inode = d_inode(d);
33548 +               ihold(h_inode);
33549 +       }
33550 +
33551 +       userns = mnt_user_ns(a->path->mnt);
33552 +       lockdep_off();
33553 +       *a->errp = vfs_unlink(userns, a->dir, d, a->delegated_inode);
33554 +       lockdep_on();
33555 +       if (!*a->errp) {
33556 +               struct path tmp = {
33557 +                       .dentry = d->d_parent,
33558 +                       .mnt    = a->path->mnt
33559 +               };
33560 +               vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
33561 +       }
33562 +
33563 +       if (!stop_sillyrename)
33564 +               dput(d);
33565 +       if (h_inode)
33566 +               iput(h_inode);
33567 +
33568 +       AuTraceErr(*a->errp);
33569 +}
33570 +
33571 +/*
33572 + * @dir: must be locked.
33573 + * @dentry: target dentry.
33574 + */
33575 +int vfsub_unlink(struct inode *dir, struct path *path,
33576 +                struct inode **delegated_inode, int force)
33577 +{
33578 +       int err;
33579 +       struct unlink_args args = {
33580 +               .errp                   = &err,
33581 +               .dir                    = dir,
33582 +               .path                   = path,
33583 +               .delegated_inode        = delegated_inode
33584 +       };
33585 +
33586 +       if (!force)
33587 +               call_unlink(&args);
33588 +       else {
33589 +               int wkq_err;
33590 +
33591 +               wkq_err = au_wkq_wait(call_unlink, &args);
33592 +               if (unlikely(wkq_err))
33593 +                       err = wkq_err;
33594 +       }
33595 +
33596 +       return err;
33597 +}
33598 diff -urN /usr/share/empty/fs/aufs/vfsub.h linux/fs/aufs/vfsub.h
33599 --- /usr/share/empty/fs/aufs/vfsub.h    1970-01-01 01:00:00.000000000 +0100
33600 +++ linux/fs/aufs/vfsub.h       2021-11-01 23:48:34.213025928 +0100
33601 @@ -0,0 +1,358 @@
33602 +/* SPDX-License-Identifier: GPL-2.0 */
33603 +/*
33604 + * Copyright (C) 2005-2021 Junjiro R. Okajima
33605 + *
33606 + * This program, aufs is free software; you can redistribute it and/or modify
33607 + * it under the terms of the GNU General Public License as published by
33608 + * the Free Software Foundation; either version 2 of the License, or
33609 + * (at your option) any later version.
33610 + *
33611 + * This program is distributed in the hope that it will be useful,
33612 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
33613 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33614 + * GNU General Public License for more details.
33615 + *
33616 + * You should have received a copy of the GNU General Public License
33617 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
33618 + */
33619 +
33620 +/*
33621 + * sub-routines for VFS
33622 + */
33623 +
33624 +#ifndef __AUFS_VFSUB_H__
33625 +#define __AUFS_VFSUB_H__
33626 +
33627 +#ifdef __KERNEL__
33628 +
33629 +#include <linux/fs.h>
33630 +#include <linux/mount.h>
33631 +#include <linux/posix_acl.h>
33632 +#include <linux/xattr.h>
33633 +#include "debug.h"
33634 +
33635 +/* copied from linux/fs/internal.h */
33636 +/* todo: BAD approach!! */
33637 +extern void __mnt_drop_write(struct vfsmount *);
33638 +extern struct file *alloc_empty_file(int, const struct cred *);
33639 +
33640 +/* ---------------------------------------------------------------------- */
33641 +
33642 +/* lock subclass for lower inode */
33643 +/* default MAX_LOCKDEP_SUBCLASSES(8) is not enough */
33644 +/* reduce? gave up. */
33645 +enum {
33646 +       AuLsc_I_Begin = I_MUTEX_PARENT2, /* 5 */
33647 +       AuLsc_I_PARENT,         /* lower inode, parent first */
33648 +       AuLsc_I_PARENT2,        /* copyup dirs */
33649 +       AuLsc_I_PARENT3,        /* copyup wh */
33650 +       AuLsc_I_CHILD,
33651 +       AuLsc_I_CHILD2,
33652 +       AuLsc_I_End
33653 +};
33654 +
33655 +/* to debug easier, do not make them inlined functions */
33656 +#define MtxMustLock(mtx)       AuDebugOn(!mutex_is_locked(mtx))
33657 +#define IMustLock(i)           AuDebugOn(!inode_is_locked(i))
33658 +
33659 +/* ---------------------------------------------------------------------- */
33660 +
33661 +static inline void vfsub_drop_nlink(struct inode *inode)
33662 +{
33663 +       AuDebugOn(!inode->i_nlink);
33664 +       drop_nlink(inode);
33665 +}
33666 +
33667 +static inline void vfsub_dead_dir(struct inode *inode)
33668 +{
33669 +       AuDebugOn(!S_ISDIR(inode->i_mode));
33670 +       inode->i_flags |= S_DEAD;
33671 +       clear_nlink(inode);
33672 +}
33673 +
33674 +static inline int vfsub_native_ro(struct inode *inode)
33675 +{
33676 +       return sb_rdonly(inode->i_sb)
33677 +               || IS_RDONLY(inode)
33678 +               /* || IS_APPEND(inode) */
33679 +               || IS_IMMUTABLE(inode);
33680 +}
33681 +
33682 +#ifdef CONFIG_AUFS_BR_FUSE
33683 +int vfsub_test_mntns(struct vfsmount *mnt, struct super_block *h_sb);
33684 +#else
33685 +AuStubInt0(vfsub_test_mntns, struct vfsmount *mnt, struct super_block *h_sb);
33686 +#endif
33687 +
33688 +int vfsub_sync_filesystem(struct super_block *h_sb, int wait);
33689 +
33690 +/* ---------------------------------------------------------------------- */
33691 +
33692 +int vfsub_update_h_iattr(struct path *h_path, int *did);
33693 +struct file *vfsub_dentry_open(struct path *path, int flags);
33694 +struct file *vfsub_filp_open(const char *path, int oflags, int mode);
33695 +struct au_branch;
33696 +struct vfsub_aopen_args {
33697 +       struct file             *file;
33698 +       unsigned int            open_flag;
33699 +       umode_t                 create_mode;
33700 +       struct au_branch        *br;
33701 +};
33702 +int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
33703 +                     struct vfsub_aopen_args *args);
33704 +int vfsub_kern_path(const char *name, unsigned int flags, struct path *path);
33705 +
33706 +struct dentry *vfsub_lookup_one_len_unlocked(const char *name,
33707 +                                            struct path *ppath, int len);
33708 +struct dentry *vfsub_lookup_one_len(const char *name, struct path *ppath,
33709 +                                   int len);
33710 +
33711 +struct vfsub_lkup_one_args {
33712 +       struct dentry **errp;
33713 +       struct qstr *name;
33714 +       struct path *ppath;
33715 +};
33716 +
33717 +static inline struct dentry *vfsub_lkup_one(struct qstr *name,
33718 +                                           struct path *ppath)
33719 +{
33720 +       return vfsub_lookup_one_len(name->name, ppath, name->len);
33721 +}
33722 +
33723 +void vfsub_call_lkup_one(void *args);
33724 +
33725 +/* ---------------------------------------------------------------------- */
33726 +
33727 +static inline int vfsub_mnt_want_write(struct vfsmount *mnt)
33728 +{
33729 +       int err;
33730 +
33731 +       lockdep_off();
33732 +       err = mnt_want_write(mnt);
33733 +       lockdep_on();
33734 +       return err;
33735 +}
33736 +
33737 +static inline void vfsub_mnt_drop_write(struct vfsmount *mnt)
33738 +{
33739 +       lockdep_off();
33740 +       mnt_drop_write(mnt);
33741 +       lockdep_on();
33742 +}
33743 +
33744 +#if 0 /* reserved */
33745 +static inline void vfsub_mnt_drop_write_file(struct file *file)
33746 +{
33747 +       lockdep_off();
33748 +       mnt_drop_write_file(file);
33749 +       lockdep_on();
33750 +}
33751 +#endif
33752 +
33753 +/* ---------------------------------------------------------------------- */
33754 +
33755 +struct au_hinode;
33756 +struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
33757 +                                struct dentry *d2, struct au_hinode *hdir2);
33758 +void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
33759 +                        struct dentry *d2, struct au_hinode *hdir2);
33760 +
33761 +int vfsub_create(struct inode *dir, struct path *path, int mode,
33762 +                bool want_excl);
33763 +int vfsub_symlink(struct inode *dir, struct path *path,
33764 +                 const char *symname);
33765 +int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev);
33766 +int vfsub_link(struct dentry *src_dentry, struct inode *dir,
33767 +              struct path *path, struct inode **delegated_inode);
33768 +int vfsub_rename(struct inode *src_hdir, struct dentry *src_dentry,
33769 +                struct inode *hdir, struct path *path,
33770 +                struct inode **delegated_inode, unsigned int flags);
33771 +int vfsub_mkdir(struct inode *dir, struct path *path, int mode);
33772 +int vfsub_rmdir(struct inode *dir, struct path *path);
33773 +
33774 +/* ---------------------------------------------------------------------- */
33775 +
33776 +ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
33777 +                    loff_t *ppos);
33778 +ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
33779 +                       loff_t *ppos);
33780 +ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
33781 +                     loff_t *ppos);
33782 +ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count,
33783 +                     loff_t *ppos);
33784 +int vfsub_flush(struct file *file, fl_owner_t id);
33785 +int vfsub_iterate_dir(struct file *file, struct dir_context *ctx);
33786 +
33787 +static inline loff_t vfsub_f_size_read(struct file *file)
33788 +{
33789 +       return i_size_read(file_inode(file));
33790 +}
33791 +
33792 +static inline unsigned int vfsub_file_flags(struct file *file)
33793 +{
33794 +       unsigned int flags;
33795 +
33796 +       spin_lock(&file->f_lock);
33797 +       flags = file->f_flags;
33798 +       spin_unlock(&file->f_lock);
33799 +
33800 +       return flags;
33801 +}
33802 +
33803 +static inline int vfsub_file_execed(struct file *file)
33804 +{
33805 +       /* todo: direct access f_flags */
33806 +       return !!(vfsub_file_flags(file) & __FMODE_EXEC);
33807 +}
33808 +
33809 +#if 0 /* reserved */
33810 +static inline void vfsub_file_accessed(struct file *h_file)
33811 +{
33812 +       file_accessed(h_file);
33813 +       vfsub_update_h_iattr(&h_file->f_path, /*did*/NULL); /*ignore*/
33814 +}
33815 +#endif
33816 +
33817 +#if 0 /* reserved */
33818 +static inline void vfsub_touch_atime(struct vfsmount *h_mnt,
33819 +                                    struct dentry *h_dentry)
33820 +{
33821 +       struct path h_path = {
33822 +               .dentry = h_dentry,
33823 +               .mnt    = h_mnt
33824 +       };
33825 +       touch_atime(&h_path);
33826 +       vfsub_update_h_iattr(&h_path, /*did*/NULL); /*ignore*/
33827 +}
33828 +#endif
33829 +
33830 +static inline int vfsub_update_time(struct inode *h_inode,
33831 +                                   struct timespec64 *ts, int flags)
33832 +{
33833 +       return update_time(h_inode, ts, flags);
33834 +       /* no vfsub_update_h_iattr() since we don't have struct path */
33835 +}
33836 +
33837 +#ifdef CONFIG_FS_POSIX_ACL
33838 +static inline int vfsub_acl_chmod(struct user_namespace *h_userns,
33839 +                                 struct inode *h_inode, umode_t h_mode)
33840 +{
33841 +       int err;
33842 +
33843 +       err = posix_acl_chmod(h_userns, h_inode, h_mode);
33844 +       if (err == -EOPNOTSUPP)
33845 +               err = 0;
33846 +       return err;
33847 +}
33848 +#else
33849 +AuStubInt0(vfsub_acl_chmod, struct user_namespace *h_userns,
33850 +          struct inode *h_inode, umode_t h_mode);
33851 +#endif
33852 +
33853 +long vfsub_splice_to(struct file *in, loff_t *ppos,
33854 +                    struct pipe_inode_info *pipe, size_t len,
33855 +                    unsigned int flags);
33856 +long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
33857 +                      loff_t *ppos, size_t len, unsigned int flags);
33858 +
33859 +static inline long vfsub_truncate(struct path *path, loff_t length)
33860 +{
33861 +       long err;
33862 +
33863 +       lockdep_off();
33864 +       err = vfs_truncate(path, length);
33865 +       lockdep_on();
33866 +       return err;
33867 +}
33868 +
33869 +int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
33870 +               struct file *h_file);
33871 +int vfsub_fsync(struct file *file, struct path *path, int datasync);
33872 +
33873 +/*
33874 + * re-use branch fs's ioctl(FICLONE) while aufs itself doesn't support such
33875 + * ioctl.
33876 + */
33877 +static inline loff_t vfsub_clone_file_range(struct file *src, struct file *dst,
33878 +                                           loff_t len)
33879 +{
33880 +       loff_t err;
33881 +
33882 +       lockdep_off();
33883 +       err = vfs_clone_file_range(src, 0, dst, 0, len, /*remap_flags*/0);
33884 +       lockdep_on();
33885 +
33886 +       return err;
33887 +}
33888 +
33889 +/* copy_file_range(2) is a systemcall */
33890 +static inline ssize_t vfsub_copy_file_range(struct file *src, loff_t src_pos,
33891 +                                           struct file *dst, loff_t dst_pos,
33892 +                                           size_t len, unsigned int flags)
33893 +{
33894 +       ssize_t ssz;
33895 +
33896 +       lockdep_off();
33897 +       ssz = vfs_copy_file_range(src, src_pos, dst, dst_pos, len, flags);
33898 +       lockdep_on();
33899 +
33900 +       return ssz;
33901 +}
33902 +
33903 +/* ---------------------------------------------------------------------- */
33904 +
33905 +static inline loff_t vfsub_llseek(struct file *file, loff_t offset, int origin)
33906 +{
33907 +       loff_t err;
33908 +
33909 +       lockdep_off();
33910 +       err = vfs_llseek(file, offset, origin);
33911 +       lockdep_on();
33912 +       return err;
33913 +}
33914 +
33915 +/* ---------------------------------------------------------------------- */
33916 +
33917 +int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode);
33918 +int vfsub_sio_rmdir(struct inode *dir, struct path *path);
33919 +int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
33920 +                           struct inode **delegated_inode);
33921 +int vfsub_notify_change(struct path *path, struct iattr *ia,
33922 +                       struct inode **delegated_inode);
33923 +int vfsub_unlink(struct inode *dir, struct path *path,
33924 +                struct inode **delegated_inode, int force);
33925 +
33926 +static inline int vfsub_getattr(const struct path *path, struct kstat *st)
33927 +{
33928 +       return vfs_getattr(path, st, STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
33929 +}
33930 +
33931 +/* ---------------------------------------------------------------------- */
33932 +
33933 +static inline int vfsub_setxattr(struct user_namespace *userns,
33934 +                                struct dentry *dentry, const char *name,
33935 +                                const void *value, size_t size, int flags)
33936 +{
33937 +       int err;
33938 +
33939 +       lockdep_off();
33940 +       err = vfs_setxattr(userns, dentry, name, value, size, flags);
33941 +       lockdep_on();
33942 +
33943 +       return err;
33944 +}
33945 +
33946 +static inline int vfsub_removexattr(struct user_namespace *userns,
33947 +                                   struct dentry *dentry, const char *name)
33948 +{
33949 +       int err;
33950 +
33951 +       lockdep_off();
33952 +       err = vfs_removexattr(userns, dentry, name);
33953 +       lockdep_on();
33954 +
33955 +       return err;
33956 +}
33957 +
33958 +#endif /* __KERNEL__ */
33959 +#endif /* __AUFS_VFSUB_H__ */
33960 diff -urN /usr/share/empty/fs/aufs/wbr_policy.c linux/fs/aufs/wbr_policy.c
33961 --- /usr/share/empty/fs/aufs/wbr_policy.c       1970-01-01 01:00:00.000000000 +0100
33962 +++ linux/fs/aufs/wbr_policy.c  2021-11-01 23:48:34.213025928 +0100
33963 @@ -0,0 +1,830 @@
33964 +// SPDX-License-Identifier: GPL-2.0
33965 +/*
33966 + * Copyright (C) 2005-2021 Junjiro R. Okajima
33967 + *
33968 + * This program, aufs is free software; you can redistribute it and/or modify
33969 + * it under the terms of the GNU General Public License as published by
33970 + * the Free Software Foundation; either version 2 of the License, or
33971 + * (at your option) any later version.
33972 + *
33973 + * This program is distributed in the hope that it will be useful,
33974 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
33975 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33976 + * GNU General Public License for more details.
33977 + *
33978 + * You should have received a copy of the GNU General Public License
33979 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
33980 + */
33981 +
33982 +/*
33983 + * policies for selecting one among multiple writable branches
33984 + */
33985 +
33986 +#include <linux/statfs.h>
33987 +#include "aufs.h"
33988 +
33989 +/* subset of cpup_attr() */
33990 +static noinline_for_stack
33991 +int au_cpdown_attr(struct path *h_path, struct dentry *h_src)
33992 +{
33993 +       int err, sbits;
33994 +       struct iattr ia;
33995 +       struct inode *h_isrc;
33996 +
33997 +       h_isrc = d_inode(h_src);
33998 +       ia.ia_valid = ATTR_FORCE | ATTR_MODE | ATTR_UID | ATTR_GID;
33999 +       ia.ia_mode = h_isrc->i_mode;
34000 +       ia.ia_uid = h_isrc->i_uid;
34001 +       ia.ia_gid = h_isrc->i_gid;
34002 +       sbits = !!(ia.ia_mode & (S_ISUID | S_ISGID));
34003 +       au_cpup_attr_flags(d_inode(h_path->dentry), h_isrc->i_flags);
34004 +       /* no delegation since it is just created */
34005 +       err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
34006 +
34007 +       /* is this nfs only? */
34008 +       if (!err && sbits && au_test_nfs(h_path->dentry->d_sb)) {
34009 +               ia.ia_valid = ATTR_FORCE | ATTR_MODE;
34010 +               ia.ia_mode = h_isrc->i_mode;
34011 +               err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
34012 +       }
34013 +
34014 +       return err;
34015 +}
34016 +
34017 +#define AuCpdown_PARENT_OPQ    1
34018 +#define AuCpdown_WHED          (1 << 1)
34019 +#define AuCpdown_MADE_DIR      (1 << 2)
34020 +#define AuCpdown_DIROPQ                (1 << 3)
34021 +#define au_ftest_cpdown(flags, name)   ((flags) & AuCpdown_##name)
34022 +#define au_fset_cpdown(flags, name) \
34023 +       do { (flags) |= AuCpdown_##name; } while (0)
34024 +#define au_fclr_cpdown(flags, name) \
34025 +       do { (flags) &= ~AuCpdown_##name; } while (0)
34026 +
34027 +static int au_cpdown_dir_opq(struct dentry *dentry, aufs_bindex_t bdst,
34028 +                            unsigned int *flags)
34029 +{
34030 +       int err;
34031 +       struct dentry *opq_dentry;
34032 +
34033 +       opq_dentry = au_diropq_create(dentry, bdst);
34034 +       err = PTR_ERR(opq_dentry);
34035 +       if (IS_ERR(opq_dentry))
34036 +               goto out;
34037 +       dput(opq_dentry);
34038 +       au_fset_cpdown(*flags, DIROPQ);
34039 +
34040 +out:
34041 +       return err;
34042 +}
34043 +
34044 +static int au_cpdown_dir_wh(struct dentry *dentry, struct dentry *h_parent,
34045 +                           struct inode *dir, aufs_bindex_t bdst)
34046 +{
34047 +       int err;
34048 +       struct path h_path;
34049 +       struct au_branch *br;
34050 +
34051 +       br = au_sbr(dentry->d_sb, bdst);
34052 +       h_path.dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
34053 +       err = PTR_ERR(h_path.dentry);
34054 +       if (IS_ERR(h_path.dentry))
34055 +               goto out;
34056 +
34057 +       err = 0;
34058 +       if (d_is_positive(h_path.dentry)) {
34059 +               h_path.mnt = au_br_mnt(br);
34060 +               err = au_wh_unlink_dentry(au_h_iptr(dir, bdst), &h_path,
34061 +                                         dentry);
34062 +       }
34063 +       dput(h_path.dentry);
34064 +
34065 +out:
34066 +       return err;
34067 +}
34068 +
34069 +static int au_cpdown_dir(struct dentry *dentry, aufs_bindex_t bdst,
34070 +                        struct au_pin *pin,
34071 +                        struct dentry *h_parent, void *arg)
34072 +{
34073 +       int err, rerr;
34074 +       aufs_bindex_t bopq, btop;
34075 +       struct path h_path;
34076 +       struct dentry *parent;
34077 +       struct inode *h_dir, *h_inode, *inode, *dir;
34078 +       unsigned int *flags = arg;
34079 +
34080 +       btop = au_dbtop(dentry);
34081 +       /* dentry is di-locked */
34082 +       parent = dget_parent(dentry);
34083 +       dir = d_inode(parent);
34084 +       h_dir = d_inode(h_parent);
34085 +       AuDebugOn(h_dir != au_h_iptr(dir, bdst));
34086 +       IMustLock(h_dir);
34087 +
34088 +       err = au_lkup_neg(dentry, bdst, /*wh*/0);
34089 +       if (unlikely(err < 0))
34090 +               goto out;
34091 +       h_path.dentry = au_h_dptr(dentry, bdst);
34092 +       h_path.mnt = au_sbr_mnt(dentry->d_sb, bdst);
34093 +       err = vfsub_sio_mkdir(au_h_iptr(dir, bdst), &h_path, 0755);
34094 +       if (unlikely(err))
34095 +               goto out_put;
34096 +       au_fset_cpdown(*flags, MADE_DIR);
34097 +
34098 +       bopq = au_dbdiropq(dentry);
34099 +       au_fclr_cpdown(*flags, WHED);
34100 +       au_fclr_cpdown(*flags, DIROPQ);
34101 +       if (au_dbwh(dentry) == bdst)
34102 +               au_fset_cpdown(*flags, WHED);
34103 +       if (!au_ftest_cpdown(*flags, PARENT_OPQ) && bopq <= bdst)
34104 +               au_fset_cpdown(*flags, PARENT_OPQ);
34105 +       h_inode = d_inode(h_path.dentry);
34106 +       inode_lock_nested(h_inode, AuLsc_I_CHILD);
34107 +       if (au_ftest_cpdown(*flags, WHED)) {
34108 +               err = au_cpdown_dir_opq(dentry, bdst, flags);
34109 +               if (unlikely(err)) {
34110 +                       inode_unlock(h_inode);
34111 +                       goto out_dir;
34112 +               }
34113 +       }
34114 +
34115 +       err = au_cpdown_attr(&h_path, au_h_dptr(dentry, btop));
34116 +       inode_unlock(h_inode);
34117 +       if (unlikely(err))
34118 +               goto out_opq;
34119 +
34120 +       if (au_ftest_cpdown(*flags, WHED)) {
34121 +               err = au_cpdown_dir_wh(dentry, h_parent, dir, bdst);
34122 +               if (unlikely(err))
34123 +                       goto out_opq;
34124 +       }
34125 +
34126 +       inode = d_inode(dentry);
34127 +       if (au_ibbot(inode) < bdst)
34128 +               au_set_ibbot(inode, bdst);
34129 +       au_set_h_iptr(inode, bdst, au_igrab(h_inode),
34130 +                     au_hi_flags(inode, /*isdir*/1));
34131 +       au_fhsm_wrote(dentry->d_sb, bdst, /*force*/0);
34132 +       goto out; /* success */
34133 +
34134 +       /* revert */
34135 +out_opq:
34136 +       if (au_ftest_cpdown(*flags, DIROPQ)) {
34137 +               inode_lock_nested(h_inode, AuLsc_I_CHILD);
34138 +               rerr = au_diropq_remove(dentry, bdst);
34139 +               inode_unlock(h_inode);
34140 +               if (unlikely(rerr)) {
34141 +                       AuIOErr("failed removing diropq for %pd b%d (%d)\n",
34142 +                               dentry, bdst, rerr);
34143 +                       err = -EIO;
34144 +                       goto out;
34145 +               }
34146 +       }
34147 +out_dir:
34148 +       if (au_ftest_cpdown(*flags, MADE_DIR)) {
34149 +               rerr = vfsub_sio_rmdir(au_h_iptr(dir, bdst), &h_path);
34150 +               if (unlikely(rerr)) {
34151 +                       AuIOErr("failed removing %pd b%d (%d)\n",
34152 +                               dentry, bdst, rerr);
34153 +                       err = -EIO;
34154 +               }
34155 +       }
34156 +out_put:
34157 +       au_set_h_dptr(dentry, bdst, NULL);
34158 +       if (au_dbbot(dentry) == bdst)
34159 +               au_update_dbbot(dentry);
34160 +out:
34161 +       dput(parent);
34162 +       return err;
34163 +}
34164 +
34165 +int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst)
34166 +{
34167 +       int err;
34168 +       unsigned int flags;
34169 +
34170 +       flags = 0;
34171 +       err = au_cp_dirs(dentry, bdst, au_cpdown_dir, &flags);
34172 +
34173 +       return err;
34174 +}
34175 +
34176 +/* ---------------------------------------------------------------------- */
34177 +
34178 +/* policies for create */
34179 +
34180 +int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex)
34181 +{
34182 +       int err, i, j, ndentry;
34183 +       aufs_bindex_t bopq;
34184 +       struct au_dcsub_pages dpages;
34185 +       struct au_dpage *dpage;
34186 +       struct dentry **dentries, *parent, *d;
34187 +
34188 +       err = au_dpages_init(&dpages, GFP_NOFS);
34189 +       if (unlikely(err))
34190 +               goto out;
34191 +       parent = dget_parent(dentry);
34192 +       err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/0);
34193 +       if (unlikely(err))
34194 +               goto out_free;
34195 +
34196 +       err = bindex;
34197 +       for (i = 0; i < dpages.ndpage; i++) {
34198 +               dpage = dpages.dpages + i;
34199 +               dentries = dpage->dentries;
34200 +               ndentry = dpage->ndentry;
34201 +               for (j = 0; j < ndentry; j++) {
34202 +                       d = dentries[j];
34203 +                       di_read_lock_parent2(d, !AuLock_IR);
34204 +                       bopq = au_dbdiropq(d);
34205 +                       di_read_unlock(d, !AuLock_IR);
34206 +                       if (bopq >= 0 && bopq < err)
34207 +                               err = bopq;
34208 +               }
34209 +       }
34210 +
34211 +out_free:
34212 +       dput(parent);
34213 +       au_dpages_free(&dpages);
34214 +out:
34215 +       return err;
34216 +}
34217 +
34218 +static int au_wbr_bu(struct super_block *sb, aufs_bindex_t bindex)
34219 +{
34220 +       for (; bindex >= 0; bindex--)
34221 +               if (!au_br_rdonly(au_sbr(sb, bindex)))
34222 +                       return bindex;
34223 +       return -EROFS;
34224 +}
34225 +
34226 +/* top down parent */
34227 +static int au_wbr_create_tdp(struct dentry *dentry,
34228 +                            unsigned int flags __maybe_unused)
34229 +{
34230 +       int err;
34231 +       aufs_bindex_t btop, bindex;
34232 +       struct super_block *sb;
34233 +       struct dentry *parent, *h_parent;
34234 +
34235 +       sb = dentry->d_sb;
34236 +       btop = au_dbtop(dentry);
34237 +       err = btop;
34238 +       if (!au_br_rdonly(au_sbr(sb, btop)))
34239 +               goto out;
34240 +
34241 +       err = -EROFS;
34242 +       parent = dget_parent(dentry);
34243 +       for (bindex = au_dbtop(parent); bindex < btop; bindex++) {
34244 +               h_parent = au_h_dptr(parent, bindex);
34245 +               if (!h_parent || d_is_negative(h_parent))
34246 +                       continue;
34247 +
34248 +               if (!au_br_rdonly(au_sbr(sb, bindex))) {
34249 +                       err = bindex;
34250 +                       break;
34251 +               }
34252 +       }
34253 +       dput(parent);
34254 +
34255 +       /* bottom up here */
34256 +       if (unlikely(err < 0)) {
34257 +               err = au_wbr_bu(sb, btop - 1);
34258 +               if (err >= 0)
34259 +                       err = au_wbr_nonopq(dentry, err);
34260 +       }
34261 +
34262 +out:
34263 +       AuDbg("b%d\n", err);
34264 +       return err;
34265 +}
34266 +
34267 +/* ---------------------------------------------------------------------- */
34268 +
34269 +/* an exception for the policy other than tdp */
34270 +static int au_wbr_create_exp(struct dentry *dentry)
34271 +{
34272 +       int err;
34273 +       aufs_bindex_t bwh, bdiropq;
34274 +       struct dentry *parent;
34275 +
34276 +       err = -1;
34277 +       bwh = au_dbwh(dentry);
34278 +       parent = dget_parent(dentry);
34279 +       bdiropq = au_dbdiropq(parent);
34280 +       if (bwh >= 0) {
34281 +               if (bdiropq >= 0)
34282 +                       err = min(bdiropq, bwh);
34283 +               else
34284 +                       err = bwh;
34285 +               AuDbg("%d\n", err);
34286 +       } else if (bdiropq >= 0) {
34287 +               err = bdiropq;
34288 +               AuDbg("%d\n", err);
34289 +       }
34290 +       dput(parent);
34291 +
34292 +       if (err >= 0)
34293 +               err = au_wbr_nonopq(dentry, err);
34294 +
34295 +       if (err >= 0 && au_br_rdonly(au_sbr(dentry->d_sb, err)))
34296 +               err = -1;
34297 +
34298 +       AuDbg("%d\n", err);
34299 +       return err;
34300 +}
34301 +
34302 +/* ---------------------------------------------------------------------- */
34303 +
34304 +/* round robin */
34305 +static int au_wbr_create_init_rr(struct super_block *sb)
34306 +{
34307 +       int err;
34308 +
34309 +       err = au_wbr_bu(sb, au_sbbot(sb));
34310 +       atomic_set(&au_sbi(sb)->si_wbr_rr_next, -err); /* less important */
34311 +       /* smp_mb(); */
34312 +
34313 +       AuDbg("b%d\n", err);
34314 +       return err;
34315 +}
34316 +
34317 +static int au_wbr_create_rr(struct dentry *dentry, unsigned int flags)
34318 +{
34319 +       int err, nbr;
34320 +       unsigned int u;
34321 +       aufs_bindex_t bindex, bbot;
34322 +       struct super_block *sb;
34323 +       atomic_t *next;
34324 +
34325 +       err = au_wbr_create_exp(dentry);
34326 +       if (err >= 0)
34327 +               goto out;
34328 +
34329 +       sb = dentry->d_sb;
34330 +       next = &au_sbi(sb)->si_wbr_rr_next;
34331 +       bbot = au_sbbot(sb);
34332 +       nbr = bbot + 1;
34333 +       for (bindex = 0; bindex <= bbot; bindex++) {
34334 +               if (!au_ftest_wbr(flags, DIR)) {
34335 +                       err = atomic_dec_return(next) + 1;
34336 +                       /* modulo for 0 is meaningless */
34337 +                       if (unlikely(!err))
34338 +                               err = atomic_dec_return(next) + 1;
34339 +               } else
34340 +                       err = atomic_read(next);
34341 +               AuDbg("%d\n", err);
34342 +               u = err;
34343 +               err = u % nbr;
34344 +               AuDbg("%d\n", err);
34345 +               if (!au_br_rdonly(au_sbr(sb, err)))
34346 +                       break;
34347 +               err = -EROFS;
34348 +       }
34349 +
34350 +       if (err >= 0)
34351 +               err = au_wbr_nonopq(dentry, err);
34352 +
34353 +out:
34354 +       AuDbg("%d\n", err);
34355 +       return err;
34356 +}
34357 +
34358 +/* ---------------------------------------------------------------------- */
34359 +
34360 +/* most free space */
34361 +static void au_mfs(struct dentry *dentry, struct dentry *parent)
34362 +{
34363 +       struct super_block *sb;
34364 +       struct au_branch *br;
34365 +       struct au_wbr_mfs *mfs;
34366 +       struct dentry *h_parent;
34367 +       aufs_bindex_t bindex, bbot;
34368 +       int err;
34369 +       unsigned long long b, bavail;
34370 +       struct path h_path;
34371 +       /* reduce the stack usage */
34372 +       struct kstatfs *st;
34373 +
34374 +       st = kmalloc(sizeof(*st), GFP_NOFS);
34375 +       if (unlikely(!st)) {
34376 +               AuWarn1("failed updating mfs(%d), ignored\n", -ENOMEM);
34377 +               return;
34378 +       }
34379 +
34380 +       bavail = 0;
34381 +       sb = dentry->d_sb;
34382 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34383 +       MtxMustLock(&mfs->mfs_lock);
34384 +       mfs->mfs_bindex = -EROFS;
34385 +       mfs->mfsrr_bytes = 0;
34386 +       if (!parent) {
34387 +               bindex = 0;
34388 +               bbot = au_sbbot(sb);
34389 +       } else {
34390 +               bindex = au_dbtop(parent);
34391 +               bbot = au_dbtaildir(parent);
34392 +       }
34393 +
34394 +       for (; bindex <= bbot; bindex++) {
34395 +               if (parent) {
34396 +                       h_parent = au_h_dptr(parent, bindex);
34397 +                       if (!h_parent || d_is_negative(h_parent))
34398 +                               continue;
34399 +               }
34400 +               br = au_sbr(sb, bindex);
34401 +               if (au_br_rdonly(br))
34402 +                       continue;
34403 +
34404 +               /* sb->s_root for NFS is unreliable */
34405 +               h_path.mnt = au_br_mnt(br);
34406 +               h_path.dentry = h_path.mnt->mnt_root;
34407 +               err = vfs_statfs(&h_path, st);
34408 +               if (unlikely(err)) {
34409 +                       AuWarn1("failed statfs, b%d, %d\n", bindex, err);
34410 +                       continue;
34411 +               }
34412 +
34413 +               /* when the available size is equal, select the lower one */
34414 +               BUILD_BUG_ON(sizeof(b) < sizeof(st->f_bavail)
34415 +                            || sizeof(b) < sizeof(st->f_bsize));
34416 +               b = st->f_bavail * st->f_bsize;
34417 +               br->br_wbr->wbr_bytes = b;
34418 +               if (b >= bavail) {
34419 +                       bavail = b;
34420 +                       mfs->mfs_bindex = bindex;
34421 +                       mfs->mfs_jiffy = jiffies;
34422 +               }
34423 +       }
34424 +
34425 +       mfs->mfsrr_bytes = bavail;
34426 +       AuDbg("b%d\n", mfs->mfs_bindex);
34427 +       au_kfree_rcu(st);
34428 +}
34429 +
34430 +static int au_wbr_create_mfs(struct dentry *dentry, unsigned int flags)
34431 +{
34432 +       int err;
34433 +       struct dentry *parent;
34434 +       struct super_block *sb;
34435 +       struct au_wbr_mfs *mfs;
34436 +
34437 +       err = au_wbr_create_exp(dentry);
34438 +       if (err >= 0)
34439 +               goto out;
34440 +
34441 +       sb = dentry->d_sb;
34442 +       parent = NULL;
34443 +       if (au_ftest_wbr(flags, PARENT))
34444 +               parent = dget_parent(dentry);
34445 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34446 +       mutex_lock(&mfs->mfs_lock);
34447 +       if (time_after(jiffies, mfs->mfs_jiffy + mfs->mfs_expire)
34448 +           || mfs->mfs_bindex < 0
34449 +           || au_br_rdonly(au_sbr(sb, mfs->mfs_bindex)))
34450 +               au_mfs(dentry, parent);
34451 +       mutex_unlock(&mfs->mfs_lock);
34452 +       err = mfs->mfs_bindex;
34453 +       dput(parent);
34454 +
34455 +       if (err >= 0)
34456 +               err = au_wbr_nonopq(dentry, err);
34457 +
34458 +out:
34459 +       AuDbg("b%d\n", err);
34460 +       return err;
34461 +}
34462 +
34463 +static int au_wbr_create_init_mfs(struct super_block *sb)
34464 +{
34465 +       struct au_wbr_mfs *mfs;
34466 +
34467 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34468 +       mutex_init(&mfs->mfs_lock);
34469 +       mfs->mfs_jiffy = 0;
34470 +       mfs->mfs_bindex = -EROFS;
34471 +
34472 +       return 0;
34473 +}
34474 +
34475 +static int au_wbr_create_fin_mfs(struct super_block *sb __maybe_unused)
34476 +{
34477 +       mutex_destroy(&au_sbi(sb)->si_wbr_mfs.mfs_lock);
34478 +       return 0;
34479 +}
34480 +
34481 +/* ---------------------------------------------------------------------- */
34482 +
34483 +/* top down regardless parent, and then mfs */
34484 +static int au_wbr_create_tdmfs(struct dentry *dentry,
34485 +                              unsigned int flags __maybe_unused)
34486 +{
34487 +       int err;
34488 +       aufs_bindex_t bwh, btail, bindex, bfound, bmfs;
34489 +       unsigned long long watermark;
34490 +       struct super_block *sb;
34491 +       struct au_wbr_mfs *mfs;
34492 +       struct au_branch *br;
34493 +       struct dentry *parent;
34494 +
34495 +       sb = dentry->d_sb;
34496 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34497 +       mutex_lock(&mfs->mfs_lock);
34498 +       if (time_after(jiffies, mfs->mfs_jiffy + mfs->mfs_expire)
34499 +           || mfs->mfs_bindex < 0)
34500 +               au_mfs(dentry, /*parent*/NULL);
34501 +       watermark = mfs->mfsrr_watermark;
34502 +       bmfs = mfs->mfs_bindex;
34503 +       mutex_unlock(&mfs->mfs_lock);
34504 +
34505 +       /* another style of au_wbr_create_exp() */
34506 +       bwh = au_dbwh(dentry);
34507 +       parent = dget_parent(dentry);
34508 +       btail = au_dbtaildir(parent);
34509 +       if (bwh >= 0 && bwh < btail)
34510 +               btail = bwh;
34511 +
34512 +       err = au_wbr_nonopq(dentry, btail);
34513 +       if (unlikely(err < 0))
34514 +               goto out;
34515 +       btail = err;
34516 +       bfound = -1;
34517 +       for (bindex = 0; bindex <= btail; bindex++) {
34518 +               br = au_sbr(sb, bindex);
34519 +               if (au_br_rdonly(br))
34520 +                       continue;
34521 +               if (br->br_wbr->wbr_bytes > watermark) {
34522 +                       bfound = bindex;
34523 +                       break;
34524 +               }
34525 +       }
34526 +       err = bfound;
34527 +       if (err < 0)
34528 +               err = bmfs;
34529 +
34530 +out:
34531 +       dput(parent);
34532 +       AuDbg("b%d\n", err);
34533 +       return err;
34534 +}
34535 +
34536 +/* ---------------------------------------------------------------------- */
34537 +
34538 +/* most free space and then round robin */
34539 +static int au_wbr_create_mfsrr(struct dentry *dentry, unsigned int flags)
34540 +{
34541 +       int err;
34542 +       struct au_wbr_mfs *mfs;
34543 +
34544 +       err = au_wbr_create_mfs(dentry, flags);
34545 +       if (err >= 0) {
34546 +               mfs = &au_sbi(dentry->d_sb)->si_wbr_mfs;
34547 +               mutex_lock(&mfs->mfs_lock);
34548 +               if (mfs->mfsrr_bytes < mfs->mfsrr_watermark)
34549 +                       err = au_wbr_create_rr(dentry, flags);
34550 +               mutex_unlock(&mfs->mfs_lock);
34551 +       }
34552 +
34553 +       AuDbg("b%d\n", err);
34554 +       return err;
34555 +}
34556 +
34557 +static int au_wbr_create_init_mfsrr(struct super_block *sb)
34558 +{
34559 +       int err;
34560 +
34561 +       au_wbr_create_init_mfs(sb); /* ignore */
34562 +       err = au_wbr_create_init_rr(sb);
34563 +
34564 +       return err;
34565 +}
34566 +
34567 +/* ---------------------------------------------------------------------- */
34568 +
34569 +/* top down parent and most free space */
34570 +static int au_wbr_create_pmfs(struct dentry *dentry, unsigned int flags)
34571 +{
34572 +       int err, e2;
34573 +       unsigned long long b;
34574 +       aufs_bindex_t bindex, btop, bbot;
34575 +       struct super_block *sb;
34576 +       struct dentry *parent, *h_parent;
34577 +       struct au_branch *br;
34578 +
34579 +       err = au_wbr_create_tdp(dentry, flags);
34580 +       if (unlikely(err < 0))
34581 +               goto out;
34582 +       parent = dget_parent(dentry);
34583 +       btop = au_dbtop(parent);
34584 +       bbot = au_dbtaildir(parent);
34585 +       if (btop == bbot)
34586 +               goto out_parent; /* success */
34587 +
34588 +       e2 = au_wbr_create_mfs(dentry, flags);
34589 +       if (e2 < 0)
34590 +               goto out_parent; /* success */
34591 +
34592 +       /* when the available size is equal, select upper one */
34593 +       sb = dentry->d_sb;
34594 +       br = au_sbr(sb, err);
34595 +       b = br->br_wbr->wbr_bytes;
34596 +       AuDbg("b%d, %llu\n", err, b);
34597 +
34598 +       for (bindex = btop; bindex <= bbot; bindex++) {
34599 +               h_parent = au_h_dptr(parent, bindex);
34600 +               if (!h_parent || d_is_negative(h_parent))
34601 +                       continue;
34602 +
34603 +               br = au_sbr(sb, bindex);
34604 +               if (!au_br_rdonly(br) && br->br_wbr->wbr_bytes > b) {
34605 +                       b = br->br_wbr->wbr_bytes;
34606 +                       err = bindex;
34607 +                       AuDbg("b%d, %llu\n", err, b);
34608 +               }
34609 +       }
34610 +
34611 +       if (err >= 0)
34612 +               err = au_wbr_nonopq(dentry, err);
34613 +
34614 +out_parent:
34615 +       dput(parent);
34616 +out:
34617 +       AuDbg("b%d\n", err);
34618 +       return err;
34619 +}
34620 +
34621 +/* ---------------------------------------------------------------------- */
34622 +
34623 +/*
34624 + * - top down parent
34625 + * - most free space with parent
34626 + * - most free space round-robin regardless parent
34627 + */
34628 +static int au_wbr_create_pmfsrr(struct dentry *dentry, unsigned int flags)
34629 +{
34630 +       int err;
34631 +       unsigned long long watermark;
34632 +       struct super_block *sb;
34633 +       struct au_branch *br;
34634 +       struct au_wbr_mfs *mfs;
34635 +
34636 +       err = au_wbr_create_pmfs(dentry, flags | AuWbr_PARENT);
34637 +       if (unlikely(err < 0))
34638 +               goto out;
34639 +
34640 +       sb = dentry->d_sb;
34641 +       br = au_sbr(sb, err);
34642 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34643 +       mutex_lock(&mfs->mfs_lock);
34644 +       watermark = mfs->mfsrr_watermark;
34645 +       mutex_unlock(&mfs->mfs_lock);
34646 +       if (br->br_wbr->wbr_bytes < watermark)
34647 +               /* regardless the parent dir */
34648 +               err = au_wbr_create_mfsrr(dentry, flags);
34649 +
34650 +out:
34651 +       AuDbg("b%d\n", err);
34652 +       return err;
34653 +}
34654 +
34655 +/* ---------------------------------------------------------------------- */
34656 +
34657 +/* policies for copyup */
34658 +
34659 +/* top down parent */
34660 +static int au_wbr_copyup_tdp(struct dentry *dentry)
34661 +{
34662 +       return au_wbr_create_tdp(dentry, /*flags, anything is ok*/0);
34663 +}
34664 +
34665 +/* bottom up parent */
34666 +static int au_wbr_copyup_bup(struct dentry *dentry)
34667 +{
34668 +       int err;
34669 +       aufs_bindex_t bindex, btop;
34670 +       struct dentry *parent, *h_parent;
34671 +       struct super_block *sb;
34672 +
34673 +       err = -EROFS;
34674 +       sb = dentry->d_sb;
34675 +       parent = dget_parent(dentry);
34676 +       btop = au_dbtop(parent);
34677 +       for (bindex = au_dbtop(dentry); bindex >= btop; bindex--) {
34678 +               h_parent = au_h_dptr(parent, bindex);
34679 +               if (!h_parent || d_is_negative(h_parent))
34680 +                       continue;
34681 +
34682 +               if (!au_br_rdonly(au_sbr(sb, bindex))) {
34683 +                       err = bindex;
34684 +                       break;
34685 +               }
34686 +       }
34687 +       dput(parent);
34688 +
34689 +       /* bottom up here */
34690 +       if (unlikely(err < 0))
34691 +               err = au_wbr_bu(sb, btop - 1);
34692 +
34693 +       AuDbg("b%d\n", err);
34694 +       return err;
34695 +}
34696 +
34697 +/* bottom up */
34698 +int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t btop)
34699 +{
34700 +       int err;
34701 +
34702 +       err = au_wbr_bu(dentry->d_sb, btop);
34703 +       AuDbg("b%d\n", err);
34704 +       if (err > btop)
34705 +               err = au_wbr_nonopq(dentry, err);
34706 +
34707 +       AuDbg("b%d\n", err);
34708 +       return err;
34709 +}
34710 +
34711 +static int au_wbr_copyup_bu(struct dentry *dentry)
34712 +{
34713 +       int err;
34714 +       aufs_bindex_t btop;
34715 +
34716 +       btop = au_dbtop(dentry);
34717 +       err = au_wbr_do_copyup_bu(dentry, btop);
34718 +       return err;
34719 +}
34720 +
34721 +/* ---------------------------------------------------------------------- */
34722 +
34723 +struct au_wbr_copyup_operations au_wbr_copyup_ops[] = {
34724 +       [AuWbrCopyup_TDP] = {
34725 +               .copyup = au_wbr_copyup_tdp
34726 +       },
34727 +       [AuWbrCopyup_BUP] = {
34728 +               .copyup = au_wbr_copyup_bup
34729 +       },
34730 +       [AuWbrCopyup_BU] = {
34731 +               .copyup = au_wbr_copyup_bu
34732 +       }
34733 +};
34734 +
34735 +struct au_wbr_create_operations au_wbr_create_ops[] = {
34736 +       [AuWbrCreate_TDP] = {
34737 +               .create = au_wbr_create_tdp
34738 +       },
34739 +       [AuWbrCreate_RR] = {
34740 +               .create = au_wbr_create_rr,
34741 +               .init   = au_wbr_create_init_rr
34742 +       },
34743 +       [AuWbrCreate_MFS] = {
34744 +               .create = au_wbr_create_mfs,
34745 +               .init   = au_wbr_create_init_mfs,
34746 +               .fin    = au_wbr_create_fin_mfs
34747 +       },
34748 +       [AuWbrCreate_MFSV] = {
34749 +               .create = au_wbr_create_mfs,
34750 +               .init   = au_wbr_create_init_mfs,
34751 +               .fin    = au_wbr_create_fin_mfs
34752 +       },
34753 +       [AuWbrCreate_MFSRR] = {
34754 +               .create = au_wbr_create_mfsrr,
34755 +               .init   = au_wbr_create_init_mfsrr,
34756 +               .fin    = au_wbr_create_fin_mfs
34757 +       },
34758 +       [AuWbrCreate_MFSRRV] = {
34759 +               .create = au_wbr_create_mfsrr,
34760 +               .init   = au_wbr_create_init_mfsrr,
34761 +               .fin    = au_wbr_create_fin_mfs
34762 +       },
34763 +       [AuWbrCreate_TDMFS] = {
34764 +               .create = au_wbr_create_tdmfs,
34765 +               .init   = au_wbr_create_init_mfs,
34766 +               .fin    = au_wbr_create_fin_mfs
34767 +       },
34768 +       [AuWbrCreate_TDMFSV] = {
34769 +               .create = au_wbr_create_tdmfs,
34770 +               .init   = au_wbr_create_init_mfs,
34771 +               .fin    = au_wbr_create_fin_mfs
34772 +       },
34773 +       [AuWbrCreate_PMFS] = {
34774 +               .create = au_wbr_create_pmfs,
34775 +               .init   = au_wbr_create_init_mfs,
34776 +               .fin    = au_wbr_create_fin_mfs
34777 +       },
34778 +       [AuWbrCreate_PMFSV] = {
34779 +               .create = au_wbr_create_pmfs,
34780 +               .init   = au_wbr_create_init_mfs,
34781 +               .fin    = au_wbr_create_fin_mfs
34782 +       },
34783 +       [AuWbrCreate_PMFSRR] = {
34784 +               .create = au_wbr_create_pmfsrr,
34785 +               .init   = au_wbr_create_init_mfsrr,
34786 +               .fin    = au_wbr_create_fin_mfs
34787 +       },
34788 +       [AuWbrCreate_PMFSRRV] = {
34789 +               .create = au_wbr_create_pmfsrr,
34790 +               .init   = au_wbr_create_init_mfsrr,
34791 +               .fin    = au_wbr_create_fin_mfs
34792 +       }
34793 +};
34794 diff -urN /usr/share/empty/fs/aufs/whout.c linux/fs/aufs/whout.c
34795 --- /usr/share/empty/fs/aufs/whout.c    1970-01-01 01:00:00.000000000 +0100
34796 +++ linux/fs/aufs/whout.c       2021-11-01 23:48:34.213025928 +0100
34797 @@ -0,0 +1,1072 @@
34798 +// SPDX-License-Identifier: GPL-2.0
34799 +/*
34800 + * Copyright (C) 2005-2021 Junjiro R. Okajima
34801 + *
34802 + * This program, aufs is free software; you can redistribute it and/or modify
34803 + * it under the terms of the GNU General Public License as published by
34804 + * the Free Software Foundation; either version 2 of the License, or
34805 + * (at your option) any later version.
34806 + *
34807 + * This program is distributed in the hope that it will be useful,
34808 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
34809 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
34810 + * GNU General Public License for more details.
34811 + *
34812 + * You should have received a copy of the GNU General Public License
34813 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
34814 + */
34815 +
34816 +/*
34817 + * whiteout for logical deletion and opaque directory
34818 + */
34819 +
34820 +#include "aufs.h"
34821 +
34822 +#define WH_MASK                        0444
34823 +
34824 +/*
34825 + * If a directory contains this file, then it is opaque.  We start with the
34826 + * .wh. flag so that it is blocked by lookup.
34827 + */
34828 +static struct qstr diropq_name = QSTR_INIT(AUFS_WH_DIROPQ,
34829 +                                          sizeof(AUFS_WH_DIROPQ) - 1);
34830 +
34831 +/*
34832 + * generate whiteout name, which is NOT terminated by NULL.
34833 + * @name: original d_name.name
34834 + * @len: original d_name.len
34835 + * @wh: whiteout qstr
34836 + * returns zero when succeeds, otherwise error.
34837 + * succeeded value as wh->name should be freed by kfree().
34838 + */
34839 +int au_wh_name_alloc(struct qstr *wh, const struct qstr *name)
34840 +{
34841 +       char *p;
34842 +
34843 +       if (unlikely(name->len > PATH_MAX - AUFS_WH_PFX_LEN))
34844 +               return -ENAMETOOLONG;
34845 +
34846 +       wh->len = name->len + AUFS_WH_PFX_LEN;
34847 +       p = kmalloc(wh->len, GFP_NOFS);
34848 +       wh->name = p;
34849 +       if (p) {
34850 +               memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
34851 +               memcpy(p + AUFS_WH_PFX_LEN, name->name, name->len);
34852 +               /* smp_mb(); */
34853 +               return 0;
34854 +       }
34855 +       return -ENOMEM;
34856 +}
34857 +
34858 +/* ---------------------------------------------------------------------- */
34859 +
34860 +/*
34861 + * test if the @wh_name exists under @h_ppath.
34862 + * @try_sio specifies the necessary of super-io.
34863 + */
34864 +int au_wh_test(struct user_namespace *h_userns, struct path *h_ppath,
34865 +              struct qstr *wh_name, int try_sio)
34866 +{
34867 +       int err;
34868 +       struct dentry *wh_dentry;
34869 +
34870 +       if (!try_sio)
34871 +               wh_dentry = vfsub_lkup_one(wh_name, h_ppath);
34872 +       else
34873 +               wh_dentry = au_sio_lkup_one(h_userns, wh_name, h_ppath);
34874 +       err = PTR_ERR(wh_dentry);
34875 +       if (IS_ERR(wh_dentry)) {
34876 +               if (err == -ENAMETOOLONG)
34877 +                       err = 0;
34878 +               goto out;
34879 +       }
34880 +
34881 +       err = 0;
34882 +       if (d_is_negative(wh_dentry))
34883 +               goto out_wh; /* success */
34884 +
34885 +       err = 1;
34886 +       if (d_is_reg(wh_dentry))
34887 +               goto out_wh; /* success */
34888 +
34889 +       err = -EIO;
34890 +       AuIOErr("%pd Invalid whiteout entry type 0%o.\n",
34891 +               wh_dentry, d_inode(wh_dentry)->i_mode);
34892 +
34893 +out_wh:
34894 +       dput(wh_dentry);
34895 +out:
34896 +       return err;
34897 +}
34898 +
34899 +/*
34900 + * test if the @h_path->dentry sets opaque or not.
34901 + */
34902 +int au_diropq_test(struct user_namespace *h_userns, struct path *h_path)
34903 +{
34904 +       int err;
34905 +       struct inode *h_dir;
34906 +
34907 +       h_dir = d_inode(h_path->dentry);
34908 +       err = au_wh_test(h_userns, h_path, &diropq_name,
34909 +                        au_test_h_perm_sio(h_userns, h_dir, MAY_EXEC));
34910 +       return err;
34911 +}
34912 +
34913 +/*
34914 + * returns a negative dentry whose name is unique and temporary.
34915 + */
34916 +struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
34917 +                            struct qstr *prefix)
34918 +{
34919 +       struct dentry *dentry;
34920 +       int i;
34921 +       char defname[NAME_MAX - AUFS_MAX_NAMELEN + DNAME_INLINE_LEN + 1],
34922 +               *name, *p;
34923 +       /* strict atomic_t is unnecessary here */
34924 +       static unsigned short cnt;
34925 +       struct qstr qs;
34926 +       struct path h_ppath;
34927 +       struct user_namespace *h_userns;
34928 +
34929 +       BUILD_BUG_ON(sizeof(cnt) * 2 > AUFS_WH_TMP_LEN);
34930 +
34931 +       name = defname;
34932 +       qs.len = sizeof(defname) - DNAME_INLINE_LEN + prefix->len - 1;
34933 +       if (unlikely(prefix->len > DNAME_INLINE_LEN)) {
34934 +               dentry = ERR_PTR(-ENAMETOOLONG);
34935 +               if (unlikely(qs.len > NAME_MAX))
34936 +                       goto out;
34937 +               dentry = ERR_PTR(-ENOMEM);
34938 +               name = kmalloc(qs.len + 1, GFP_NOFS);
34939 +               if (unlikely(!name))
34940 +                       goto out;
34941 +       }
34942 +
34943 +       /* doubly whiteout-ed */
34944 +       memcpy(name, AUFS_WH_PFX AUFS_WH_PFX, AUFS_WH_PFX_LEN * 2);
34945 +       p = name + AUFS_WH_PFX_LEN * 2;
34946 +       memcpy(p, prefix->name, prefix->len);
34947 +       p += prefix->len;
34948 +       *p++ = '.';
34949 +       AuDebugOn(name + qs.len + 1 - p <= AUFS_WH_TMP_LEN);
34950 +
34951 +       h_ppath.dentry = h_parent;
34952 +       h_ppath.mnt = au_br_mnt(br);
34953 +       h_userns = au_br_userns(br);
34954 +       qs.name = name;
34955 +       for (i = 0; i < 3; i++) {
34956 +               sprintf(p, "%.*x", AUFS_WH_TMP_LEN, cnt++);
34957 +               dentry = au_sio_lkup_one(h_userns, &qs, &h_ppath);
34958 +               if (IS_ERR(dentry) || d_is_negative(dentry))
34959 +                       goto out_name;
34960 +               dput(dentry);
34961 +       }
34962 +       /* pr_warn("could not get random name\n"); */
34963 +       dentry = ERR_PTR(-EEXIST);
34964 +       AuDbg("%.*s\n", AuLNPair(&qs));
34965 +       BUG();
34966 +
34967 +out_name:
34968 +       if (name != defname)
34969 +               au_kfree_try_rcu(name);
34970 +out:
34971 +       AuTraceErrPtr(dentry);
34972 +       return dentry;
34973 +}
34974 +
34975 +/*
34976 + * rename the @h_dentry on @br to the whiteouted temporary name.
34977 + */
34978 +int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br)
34979 +{
34980 +       int err;
34981 +       struct path h_path = {
34982 +               .mnt = au_br_mnt(br)
34983 +       };
34984 +       struct inode *h_dir, *delegated;
34985 +       struct dentry *h_parent;
34986 +
34987 +       h_parent = h_dentry->d_parent; /* dir inode is locked */
34988 +       h_dir = d_inode(h_parent);
34989 +       IMustLock(h_dir);
34990 +
34991 +       h_path.dentry = au_whtmp_lkup(h_parent, br, &h_dentry->d_name);
34992 +       err = PTR_ERR(h_path.dentry);
34993 +       if (IS_ERR(h_path.dentry))
34994 +               goto out;
34995 +
34996 +       /* under the same dir, no need to lock_rename() */
34997 +       delegated = NULL;
34998 +       err = vfsub_rename(h_dir, h_dentry, h_dir, &h_path, &delegated,
34999 +                          /*flags*/0);
35000 +       AuTraceErr(err);
35001 +       if (unlikely(err == -EWOULDBLOCK)) {
35002 +               pr_warn("cannot retry for NFSv4 delegation"
35003 +                       " for an internal rename\n");
35004 +               iput(delegated);
35005 +       }
35006 +       dput(h_path.dentry);
35007 +
35008 +out:
35009 +       AuTraceErr(err);
35010 +       return err;
35011 +}
35012 +
35013 +/* ---------------------------------------------------------------------- */
35014 +/*
35015 + * functions for removing a whiteout
35016 + */
35017 +
35018 +static int do_unlink_wh(struct inode *h_dir, struct path *h_path)
35019 +{
35020 +       int err, force;
35021 +       struct inode *delegated;
35022 +
35023 +       /*
35024 +        * forces superio when the dir has a sticky bit.
35025 +        * this may be a violation of unix fs semantics.
35026 +        */
35027 +       force = (h_dir->i_mode & S_ISVTX)
35028 +               && !uid_eq(current_fsuid(), d_inode(h_path->dentry)->i_uid);
35029 +       delegated = NULL;
35030 +       err = vfsub_unlink(h_dir, h_path, &delegated, force);
35031 +       if (unlikely(err == -EWOULDBLOCK)) {
35032 +               pr_warn("cannot retry for NFSv4 delegation"
35033 +                       " for an internal unlink\n");
35034 +               iput(delegated);
35035 +       }
35036 +       return err;
35037 +}
35038 +
35039 +int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
35040 +                       struct dentry *dentry)
35041 +{
35042 +       int err;
35043 +
35044 +       err = do_unlink_wh(h_dir, h_path);
35045 +       if (!err && dentry)
35046 +               au_set_dbwh(dentry, -1);
35047 +
35048 +       return err;
35049 +}
35050 +
35051 +static int unlink_wh_name(struct path *h_ppath, struct qstr *wh)
35052 +{
35053 +       int err;
35054 +       struct path h_path;
35055 +
35056 +       err = 0;
35057 +       h_path.dentry = vfsub_lkup_one(wh, h_ppath);
35058 +       if (IS_ERR(h_path.dentry))
35059 +               err = PTR_ERR(h_path.dentry);
35060 +       else {
35061 +               if (d_is_reg(h_path.dentry)) {
35062 +                       h_path.mnt = h_ppath->mnt;
35063 +                       err = do_unlink_wh(d_inode(h_ppath->dentry), &h_path);
35064 +               }
35065 +               dput(h_path.dentry);
35066 +       }
35067 +
35068 +       return err;
35069 +}
35070 +
35071 +/* ---------------------------------------------------------------------- */
35072 +/*
35073 + * initialize/clean whiteout for a branch
35074 + */
35075 +
35076 +static void au_wh_clean(struct inode *h_dir, struct path *whpath,
35077 +                       const int isdir)
35078 +{
35079 +       int err;
35080 +       struct inode *delegated;
35081 +
35082 +       if (d_is_negative(whpath->dentry))
35083 +               return;
35084 +
35085 +       if (isdir)
35086 +               err = vfsub_rmdir(h_dir, whpath);
35087 +       else {
35088 +               delegated = NULL;
35089 +               err = vfsub_unlink(h_dir, whpath, &delegated, /*force*/0);
35090 +               if (unlikely(err == -EWOULDBLOCK)) {
35091 +                       pr_warn("cannot retry for NFSv4 delegation"
35092 +                               " for an internal unlink\n");
35093 +                       iput(delegated);
35094 +               }
35095 +       }
35096 +       if (unlikely(err))
35097 +               pr_warn("failed removing %pd (%d), ignored.\n",
35098 +                       whpath->dentry, err);
35099 +}
35100 +
35101 +static int test_linkable(struct dentry *h_root)
35102 +{
35103 +       struct inode *h_dir = d_inode(h_root);
35104 +
35105 +       if (h_dir->i_op->link)
35106 +               return 0;
35107 +
35108 +       pr_err("%pd (%s) doesn't support link(2), use noplink and rw+nolwh\n",
35109 +              h_root, au_sbtype(h_root->d_sb));
35110 +       return -ENOSYS; /* the branch doesn't have its ->link() */
35111 +}
35112 +
35113 +/* todo: should this mkdir be done in /sbin/mount.aufs helper? */
35114 +static int au_whdir(struct inode *h_dir, struct path *path)
35115 +{
35116 +       int err;
35117 +
35118 +       err = -EEXIST;
35119 +       if (d_is_negative(path->dentry)) {
35120 +               int mode = 0700;
35121 +
35122 +               if (au_test_nfs(path->dentry->d_sb))
35123 +                       mode |= 0111;
35124 +               err = vfsub_mkdir(h_dir, path, mode);
35125 +       } else if (d_is_dir(path->dentry))
35126 +               err = 0;
35127 +       else
35128 +               pr_err("unknown %pd exists\n", path->dentry);
35129 +
35130 +       return err;
35131 +}
35132 +
35133 +struct au_wh_base {
35134 +       const struct qstr *name;
35135 +       struct dentry *dentry;
35136 +};
35137 +
35138 +static void au_wh_init_ro(struct inode *h_dir, struct au_wh_base base[],
35139 +                         struct path *h_path)
35140 +{
35141 +       h_path->dentry = base[AuBrWh_BASE].dentry;
35142 +       au_wh_clean(h_dir, h_path, /*isdir*/0);
35143 +       h_path->dentry = base[AuBrWh_PLINK].dentry;
35144 +       au_wh_clean(h_dir, h_path, /*isdir*/1);
35145 +       h_path->dentry = base[AuBrWh_ORPH].dentry;
35146 +       au_wh_clean(h_dir, h_path, /*isdir*/1);
35147 +}
35148 +
35149 +/*
35150 + * returns tri-state,
35151 + * minus: error, caller should print the message
35152 + * zero: success
35153 + * plus: error, caller should NOT print the message
35154 + */
35155 +static int au_wh_init_rw_nolink(struct dentry *h_root, struct au_wbr *wbr,
35156 +                               int do_plink, struct au_wh_base base[],
35157 +                               struct path *h_path)
35158 +{
35159 +       int err;
35160 +       struct inode *h_dir;
35161 +
35162 +       h_dir = d_inode(h_root);
35163 +       h_path->dentry = base[AuBrWh_BASE].dentry;
35164 +       au_wh_clean(h_dir, h_path, /*isdir*/0);
35165 +       h_path->dentry = base[AuBrWh_PLINK].dentry;
35166 +       if (do_plink) {
35167 +               err = test_linkable(h_root);
35168 +               if (unlikely(err)) {
35169 +                       err = 1;
35170 +                       goto out;
35171 +               }
35172 +
35173 +               err = au_whdir(h_dir, h_path);
35174 +               if (unlikely(err))
35175 +                       goto out;
35176 +               wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
35177 +       } else
35178 +               au_wh_clean(h_dir, h_path, /*isdir*/1);
35179 +       h_path->dentry = base[AuBrWh_ORPH].dentry;
35180 +       err = au_whdir(h_dir, h_path);
35181 +       if (unlikely(err))
35182 +               goto out;
35183 +       wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
35184 +
35185 +out:
35186 +       return err;
35187 +}
35188 +
35189 +/*
35190 + * for the moment, aufs supports the branch filesystem which does not support
35191 + * link(2). testing on FAT which does not support i_op->setattr() fully either,
35192 + * copyup failed. finally, such filesystem will not be used as the writable
35193 + * branch.
35194 + *
35195 + * returns tri-state, see above.
35196 + */
35197 +static int au_wh_init_rw(struct dentry *h_root, struct au_wbr *wbr,
35198 +                        int do_plink, struct au_wh_base base[],
35199 +                        struct path *h_path)
35200 +{
35201 +       int err;
35202 +       struct inode *h_dir;
35203 +
35204 +       WbrWhMustWriteLock(wbr);
35205 +
35206 +       err = test_linkable(h_root);
35207 +       if (unlikely(err)) {
35208 +               err = 1;
35209 +               goto out;
35210 +       }
35211 +
35212 +       /*
35213 +        * todo: should this create be done in /sbin/mount.aufs helper?
35214 +        */
35215 +       err = -EEXIST;
35216 +       h_dir = d_inode(h_root);
35217 +       if (d_is_negative(base[AuBrWh_BASE].dentry)) {
35218 +               h_path->dentry = base[AuBrWh_BASE].dentry;
35219 +               err = vfsub_create(h_dir, h_path, WH_MASK, /*want_excl*/true);
35220 +       } else if (d_is_reg(base[AuBrWh_BASE].dentry))
35221 +               err = 0;
35222 +       else
35223 +               pr_err("unknown %pd2 exists\n", base[AuBrWh_BASE].dentry);
35224 +       if (unlikely(err))
35225 +               goto out;
35226 +
35227 +       h_path->dentry = base[AuBrWh_PLINK].dentry;
35228 +       if (do_plink) {
35229 +               err = au_whdir(h_dir, h_path);
35230 +               if (unlikely(err))
35231 +                       goto out;
35232 +               wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
35233 +       } else
35234 +               au_wh_clean(h_dir, h_path, /*isdir*/1);
35235 +       wbr->wbr_whbase = dget(base[AuBrWh_BASE].dentry);
35236 +
35237 +       h_path->dentry = base[AuBrWh_ORPH].dentry;
35238 +       err = au_whdir(h_dir, h_path);
35239 +       if (unlikely(err))
35240 +               goto out;
35241 +       wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
35242 +
35243 +out:
35244 +       return err;
35245 +}
35246 +
35247 +/*
35248 + * initialize the whiteout base file/dir for @br.
35249 + */
35250 +int au_wh_init(struct au_branch *br, struct super_block *sb)
35251 +{
35252 +       int err, i;
35253 +       const unsigned char do_plink
35254 +               = !!au_opt_test(au_mntflags(sb), PLINK);
35255 +       struct inode *h_dir;
35256 +       struct path path = br->br_path;
35257 +       struct dentry *h_root = path.dentry;
35258 +       struct au_wbr *wbr = br->br_wbr;
35259 +       static const struct qstr base_name[] = {
35260 +               [AuBrWh_BASE] = QSTR_INIT(AUFS_BASE_NAME,
35261 +                                         sizeof(AUFS_BASE_NAME) - 1),
35262 +               [AuBrWh_PLINK] = QSTR_INIT(AUFS_PLINKDIR_NAME,
35263 +                                          sizeof(AUFS_PLINKDIR_NAME) - 1),
35264 +               [AuBrWh_ORPH] = QSTR_INIT(AUFS_ORPHDIR_NAME,
35265 +                                         sizeof(AUFS_ORPHDIR_NAME) - 1)
35266 +       };
35267 +       struct au_wh_base base[] = {
35268 +               [AuBrWh_BASE] = {
35269 +                       .name   = base_name + AuBrWh_BASE,
35270 +                       .dentry = NULL
35271 +               },
35272 +               [AuBrWh_PLINK] = {
35273 +                       .name   = base_name + AuBrWh_PLINK,
35274 +                       .dentry = NULL
35275 +               },
35276 +               [AuBrWh_ORPH] = {
35277 +                       .name   = base_name + AuBrWh_ORPH,
35278 +                       .dentry = NULL
35279 +               }
35280 +       };
35281 +
35282 +       if (wbr)
35283 +               WbrWhMustWriteLock(wbr);
35284 +
35285 +       for (i = 0; i < AuBrWh_Last; i++) {
35286 +               /* doubly whiteouted */
35287 +               struct dentry *d;
35288 +
35289 +               d = au_wh_lkup(h_root, (void *)base[i].name, br);
35290 +               err = PTR_ERR(d);
35291 +               if (IS_ERR(d))
35292 +                       goto out;
35293 +
35294 +               base[i].dentry = d;
35295 +               AuDebugOn(wbr
35296 +                         && wbr->wbr_wh[i]
35297 +                         && wbr->wbr_wh[i] != base[i].dentry);
35298 +       }
35299 +
35300 +       if (wbr)
35301 +               for (i = 0; i < AuBrWh_Last; i++) {
35302 +                       dput(wbr->wbr_wh[i]);
35303 +                       wbr->wbr_wh[i] = NULL;
35304 +               }
35305 +
35306 +       err = 0;
35307 +       if (!au_br_writable(br->br_perm)) {
35308 +               h_dir = d_inode(h_root);
35309 +               au_wh_init_ro(h_dir, base, &path);
35310 +       } else if (!au_br_wh_linkable(br->br_perm)) {
35311 +               err = au_wh_init_rw_nolink(h_root, wbr, do_plink, base, &path);
35312 +               if (err > 0)
35313 +                       goto out;
35314 +               else if (err)
35315 +                       goto out_err;
35316 +       } else {
35317 +               err = au_wh_init_rw(h_root, wbr, do_plink, base, &path);
35318 +               if (err > 0)
35319 +                       goto out;
35320 +               else if (err)
35321 +                       goto out_err;
35322 +       }
35323 +       goto out; /* success */
35324 +
35325 +out_err:
35326 +       pr_err("an error(%d) on the writable branch %pd(%s)\n",
35327 +              err, h_root, au_sbtype(h_root->d_sb));
35328 +out:
35329 +       for (i = 0; i < AuBrWh_Last; i++)
35330 +               dput(base[i].dentry);
35331 +       return err;
35332 +}
35333 +
35334 +/* ---------------------------------------------------------------------- */
35335 +/*
35336 + * whiteouts are all hard-linked usually.
35337 + * when its link count reaches a ceiling, we create a new whiteout base
35338 + * asynchronously.
35339 + */
35340 +
35341 +struct reinit_br_wh {
35342 +       struct super_block *sb;
35343 +       struct au_branch *br;
35344 +};
35345 +
35346 +static void reinit_br_wh(void *arg)
35347 +{
35348 +       int err;
35349 +       aufs_bindex_t bindex;
35350 +       struct path h_path;
35351 +       struct reinit_br_wh *a = arg;
35352 +       struct au_wbr *wbr;
35353 +       struct inode *dir, *delegated;
35354 +       struct dentry *h_root;
35355 +       struct au_hinode *hdir;
35356 +
35357 +       err = 0;
35358 +       wbr = a->br->br_wbr;
35359 +       /* big aufs lock */
35360 +       si_noflush_write_lock(a->sb);
35361 +       if (!au_br_writable(a->br->br_perm))
35362 +               goto out;
35363 +       bindex = au_br_index(a->sb, a->br->br_id);
35364 +       if (unlikely(bindex < 0))
35365 +               goto out;
35366 +
35367 +       di_read_lock_parent(a->sb->s_root, AuLock_IR);
35368 +       dir = d_inode(a->sb->s_root);
35369 +       hdir = au_hi(dir, bindex);
35370 +       h_root = au_h_dptr(a->sb->s_root, bindex);
35371 +       AuDebugOn(h_root != au_br_dentry(a->br));
35372 +
35373 +       au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
35374 +       wbr_wh_write_lock(wbr);
35375 +       err = au_h_verify(wbr->wbr_whbase, au_opt_udba(a->sb), hdir->hi_inode,
35376 +                         h_root, a->br);
35377 +       if (!err) {
35378 +               h_path.dentry = wbr->wbr_whbase;
35379 +               h_path.mnt = au_br_mnt(a->br);
35380 +               delegated = NULL;
35381 +               err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated,
35382 +                                  /*force*/0);
35383 +               if (unlikely(err == -EWOULDBLOCK)) {
35384 +                       pr_warn("cannot retry for NFSv4 delegation"
35385 +                               " for an internal unlink\n");
35386 +                       iput(delegated);
35387 +               }
35388 +       } else {
35389 +               pr_warn("%pd is moved, ignored\n", wbr->wbr_whbase);
35390 +               err = 0;
35391 +       }
35392 +       dput(wbr->wbr_whbase);
35393 +       wbr->wbr_whbase = NULL;
35394 +       if (!err)
35395 +               err = au_wh_init(a->br, a->sb);
35396 +       wbr_wh_write_unlock(wbr);
35397 +       au_hn_inode_unlock(hdir);
35398 +       di_read_unlock(a->sb->s_root, AuLock_IR);
35399 +       if (!err)
35400 +               au_fhsm_wrote(a->sb, bindex, /*force*/0);
35401 +
35402 +out:
35403 +       if (wbr)
35404 +               atomic_dec(&wbr->wbr_wh_running);
35405 +       au_lcnt_dec(&a->br->br_count);
35406 +       si_write_unlock(a->sb);
35407 +       au_nwt_done(&au_sbi(a->sb)->si_nowait);
35408 +       au_kfree_rcu(a);
35409 +       if (unlikely(err))
35410 +               AuIOErr("err %d\n", err);
35411 +}
35412 +
35413 +static void kick_reinit_br_wh(struct super_block *sb, struct au_branch *br)
35414 +{
35415 +       int do_dec, wkq_err;
35416 +       struct reinit_br_wh *arg;
35417 +
35418 +       do_dec = 1;
35419 +       if (atomic_inc_return(&br->br_wbr->wbr_wh_running) != 1)
35420 +               goto out;
35421 +
35422 +       /* ignore ENOMEM */
35423 +       arg = kmalloc(sizeof(*arg), GFP_NOFS);
35424 +       if (arg) {
35425 +               /*
35426 +                * dec(wh_running), kfree(arg) and dec(br_count)
35427 +                * in reinit function
35428 +                */
35429 +               arg->sb = sb;
35430 +               arg->br = br;
35431 +               au_lcnt_inc(&br->br_count);
35432 +               wkq_err = au_wkq_nowait(reinit_br_wh, arg, sb, /*flags*/0);
35433 +               if (unlikely(wkq_err)) {
35434 +                       atomic_dec(&br->br_wbr->wbr_wh_running);
35435 +                       au_lcnt_dec(&br->br_count);
35436 +                       au_kfree_rcu(arg);
35437 +               }
35438 +               do_dec = 0;
35439 +       }
35440 +
35441 +out:
35442 +       if (do_dec)
35443 +               atomic_dec(&br->br_wbr->wbr_wh_running);
35444 +}
35445 +
35446 +/* ---------------------------------------------------------------------- */
35447 +
35448 +/*
35449 + * create the whiteout @wh.
35450 + */
35451 +static int link_or_create_wh(struct super_block *sb, aufs_bindex_t bindex,
35452 +                            struct dentry *wh)
35453 +{
35454 +       int err;
35455 +       struct path h_path = {
35456 +               .dentry = wh
35457 +       };
35458 +       struct au_branch *br;
35459 +       struct au_wbr *wbr;
35460 +       struct dentry *h_parent;
35461 +       struct inode *h_dir, *delegated;
35462 +
35463 +       h_parent = wh->d_parent; /* dir inode is locked */
35464 +       h_dir = d_inode(h_parent);
35465 +       IMustLock(h_dir);
35466 +
35467 +       br = au_sbr(sb, bindex);
35468 +       h_path.mnt = au_br_mnt(br);
35469 +       wbr = br->br_wbr;
35470 +       wbr_wh_read_lock(wbr);
35471 +       if (wbr->wbr_whbase) {
35472 +               delegated = NULL;
35473 +               err = vfsub_link(wbr->wbr_whbase, h_dir, &h_path, &delegated);
35474 +               if (unlikely(err == -EWOULDBLOCK)) {
35475 +                       pr_warn("cannot retry for NFSv4 delegation"
35476 +                               " for an internal link\n");
35477 +                       iput(delegated);
35478 +               }
35479 +               if (!err || err != -EMLINK)
35480 +                       goto out;
35481 +
35482 +               /* link count full. re-initialize br_whbase. */
35483 +               kick_reinit_br_wh(sb, br);
35484 +       }
35485 +
35486 +       /* return this error in this context */
35487 +       err = vfsub_create(h_dir, &h_path, WH_MASK, /*want_excl*/true);
35488 +       if (!err)
35489 +               au_fhsm_wrote(sb, bindex, /*force*/0);
35490 +
35491 +out:
35492 +       wbr_wh_read_unlock(wbr);
35493 +       return err;
35494 +}
35495 +
35496 +/* ---------------------------------------------------------------------- */
35497 +
35498 +/*
35499 + * create or remove the diropq.
35500 + */
35501 +static struct dentry *do_diropq(struct dentry *dentry, aufs_bindex_t bindex,
35502 +                               unsigned int flags)
35503 +{
35504 +       struct dentry *opq_dentry;
35505 +       struct super_block *sb;
35506 +       struct au_branch *br;
35507 +       struct path h_path;
35508 +       int err;
35509 +
35510 +       sb = dentry->d_sb;
35511 +       br = au_sbr(sb, bindex);
35512 +       h_path.dentry = au_h_dptr(dentry, bindex);
35513 +       h_path.mnt = au_br_mnt(br);
35514 +       opq_dentry = vfsub_lkup_one(&diropq_name, &h_path);
35515 +       if (IS_ERR(opq_dentry))
35516 +               goto out;
35517 +
35518 +       if (au_ftest_diropq(flags, CREATE)) {
35519 +               err = link_or_create_wh(sb, bindex, opq_dentry);
35520 +               if (!err) {
35521 +                       au_set_dbdiropq(dentry, bindex);
35522 +                       goto out; /* success */
35523 +               }
35524 +       } else {
35525 +               h_path.dentry = opq_dentry;
35526 +               err = do_unlink_wh(au_h_iptr(d_inode(dentry), bindex), &h_path);
35527 +               if (!err)
35528 +                       au_set_dbdiropq(dentry, -1);
35529 +       }
35530 +       dput(opq_dentry);
35531 +       opq_dentry = ERR_PTR(err);
35532 +
35533 +out:
35534 +       return opq_dentry;
35535 +}
35536 +
35537 +struct do_diropq_args {
35538 +       struct dentry **errp;
35539 +       struct dentry *dentry;
35540 +       aufs_bindex_t bindex;
35541 +       unsigned int flags;
35542 +};
35543 +
35544 +static void call_do_diropq(void *args)
35545 +{
35546 +       struct do_diropq_args *a = args;
35547 +       *a->errp = do_diropq(a->dentry, a->bindex, a->flags);
35548 +}
35549 +
35550 +struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
35551 +                            unsigned int flags)
35552 +{
35553 +       struct dentry *diropq, *h_dentry;
35554 +       struct user_namespace *h_userns;
35555 +
35556 +       h_userns = au_sbr_userns(dentry->d_sb, bindex);
35557 +       h_dentry = au_h_dptr(dentry, bindex);
35558 +       if (!au_test_h_perm_sio(h_userns, d_inode(h_dentry),
35559 +                               MAY_EXEC | MAY_WRITE))
35560 +               diropq = do_diropq(dentry, bindex, flags);
35561 +       else {
35562 +               int wkq_err;
35563 +               struct do_diropq_args args = {
35564 +                       .errp           = &diropq,
35565 +                       .dentry         = dentry,
35566 +                       .bindex         = bindex,
35567 +                       .flags          = flags
35568 +               };
35569 +
35570 +               wkq_err = au_wkq_wait(call_do_diropq, &args);
35571 +               if (unlikely(wkq_err))
35572 +                       diropq = ERR_PTR(wkq_err);
35573 +       }
35574 +
35575 +       return diropq;
35576 +}
35577 +
35578 +/* ---------------------------------------------------------------------- */
35579 +
35580 +/*
35581 + * lookup whiteout dentry.
35582 + * @h_parent: lower parent dentry which must exist and be locked
35583 + * @base_name: name of dentry which will be whiteouted
35584 + * returns dentry for whiteout.
35585 + */
35586 +struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
35587 +                         struct au_branch *br)
35588 +{
35589 +       int err;
35590 +       struct qstr wh_name;
35591 +       struct dentry *wh_dentry;
35592 +       struct path h_path;
35593 +
35594 +       err = au_wh_name_alloc(&wh_name, base_name);
35595 +       wh_dentry = ERR_PTR(err);
35596 +       if (!err) {
35597 +               h_path.dentry = h_parent;
35598 +               h_path.mnt = au_br_mnt(br);
35599 +               wh_dentry = vfsub_lkup_one(&wh_name, &h_path);
35600 +               au_kfree_try_rcu(wh_name.name);
35601 +       }
35602 +       return wh_dentry;
35603 +}
35604 +
35605 +/*
35606 + * link/create a whiteout for @dentry on @bindex.
35607 + */
35608 +struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
35609 +                           struct dentry *h_parent)
35610 +{
35611 +       struct dentry *wh_dentry;
35612 +       struct super_block *sb;
35613 +       int err;
35614 +
35615 +       sb = dentry->d_sb;
35616 +       wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, au_sbr(sb, bindex));
35617 +       if (!IS_ERR(wh_dentry) && d_is_negative(wh_dentry)) {
35618 +               err = link_or_create_wh(sb, bindex, wh_dentry);
35619 +               if (!err) {
35620 +                       au_set_dbwh(dentry, bindex);
35621 +                       au_fhsm_wrote(sb, bindex, /*force*/0);
35622 +               } else {
35623 +                       dput(wh_dentry);
35624 +                       wh_dentry = ERR_PTR(err);
35625 +               }
35626 +       }
35627 +
35628 +       return wh_dentry;
35629 +}
35630 +
35631 +/* ---------------------------------------------------------------------- */
35632 +
35633 +/* Delete all whiteouts in this directory on branch bindex. */
35634 +static int del_wh_children(struct path *h_path, struct au_nhash *whlist,
35635 +                          aufs_bindex_t bindex)
35636 +{
35637 +       int err;
35638 +       unsigned long ul, n;
35639 +       struct qstr wh_name;
35640 +       char *p;
35641 +       struct hlist_head *head;
35642 +       struct au_vdir_wh *pos;
35643 +       struct au_vdir_destr *str;
35644 +
35645 +       err = -ENOMEM;
35646 +       p = (void *)__get_free_page(GFP_NOFS);
35647 +       wh_name.name = p;
35648 +       if (unlikely(!wh_name.name))
35649 +               goto out;
35650 +
35651 +       err = 0;
35652 +       memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
35653 +       p += AUFS_WH_PFX_LEN;
35654 +       n = whlist->nh_num;
35655 +       head = whlist->nh_head;
35656 +       for (ul = 0; !err && ul < n; ul++, head++) {
35657 +               hlist_for_each_entry(pos, head, wh_hash) {
35658 +                       if (pos->wh_bindex != bindex)
35659 +                               continue;
35660 +
35661 +                       str = &pos->wh_str;
35662 +                       if (str->len + AUFS_WH_PFX_LEN <= PATH_MAX) {
35663 +                               memcpy(p, str->name, str->len);
35664 +                               wh_name.len = AUFS_WH_PFX_LEN + str->len;
35665 +                               err = unlink_wh_name(h_path, &wh_name);
35666 +                               if (!err)
35667 +                                       continue;
35668 +                               break;
35669 +                       }
35670 +                       AuIOErr("whiteout name too long %.*s\n",
35671 +                               str->len, str->name);
35672 +                       err = -EIO;
35673 +                       break;
35674 +               }
35675 +       }
35676 +       free_page((unsigned long)wh_name.name);
35677 +
35678 +out:
35679 +       return err;
35680 +}
35681 +
35682 +struct del_wh_children_args {
35683 +       int *errp;
35684 +       struct path *h_path;
35685 +       struct au_nhash *whlist;
35686 +       aufs_bindex_t bindex;
35687 +};
35688 +
35689 +static void call_del_wh_children(void *args)
35690 +{
35691 +       struct del_wh_children_args *a = args;
35692 +       *a->errp = del_wh_children(a->h_path, a->whlist, a->bindex);
35693 +}
35694 +
35695 +/* ---------------------------------------------------------------------- */
35696 +
35697 +struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp)
35698 +{
35699 +       struct au_whtmp_rmdir *whtmp;
35700 +       int err;
35701 +       unsigned int rdhash;
35702 +
35703 +       SiMustAnyLock(sb);
35704 +
35705 +       whtmp = kzalloc(sizeof(*whtmp), gfp);
35706 +       if (unlikely(!whtmp)) {
35707 +               whtmp = ERR_PTR(-ENOMEM);
35708 +               goto out;
35709 +       }
35710 +
35711 +       /* no estimation for dir size */
35712 +       rdhash = au_sbi(sb)->si_rdhash;
35713 +       if (!rdhash)
35714 +               rdhash = AUFS_RDHASH_DEF;
35715 +       err = au_nhash_alloc(&whtmp->whlist, rdhash, gfp);
35716 +       if (unlikely(err)) {
35717 +               au_kfree_rcu(whtmp);
35718 +               whtmp = ERR_PTR(err);
35719 +       }
35720 +
35721 +out:
35722 +       return whtmp;
35723 +}
35724 +
35725 +void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp)
35726 +{
35727 +       if (whtmp->br)
35728 +               au_lcnt_dec(&whtmp->br->br_count);
35729 +       dput(whtmp->wh_dentry);
35730 +       iput(whtmp->dir);
35731 +       au_nhash_wh_free(&whtmp->whlist);
35732 +       au_kfree_rcu(whtmp);
35733 +}
35734 +
35735 +/*
35736 + * rmdir the whiteouted temporary named dir @h_dentry.
35737 + * @whlist: whiteouted children.
35738 + */
35739 +int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
35740 +                  struct dentry *wh_dentry, struct au_nhash *whlist)
35741 +{
35742 +       int err;
35743 +       unsigned int h_nlink;
35744 +       struct path wh_path;
35745 +       struct inode *wh_inode, *h_dir;
35746 +       struct au_branch *br;
35747 +       struct user_namespace *h_userns;
35748 +
35749 +       h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */
35750 +       IMustLock(h_dir);
35751 +
35752 +       br = au_sbr(dir->i_sb, bindex);
35753 +       wh_path.dentry = wh_dentry;
35754 +       wh_path.mnt = au_br_mnt(br);
35755 +       h_userns = au_br_userns(br);
35756 +       wh_inode = d_inode(wh_dentry);
35757 +       inode_lock_nested(wh_inode, AuLsc_I_CHILD);
35758 +
35759 +       /*
35760 +        * someone else might change some whiteouts while we were sleeping.
35761 +        * it means this whlist may have an obsoleted entry.
35762 +        */
35763 +       if (!au_test_h_perm_sio(h_userns, wh_inode, MAY_EXEC | MAY_WRITE))
35764 +               err = del_wh_children(&wh_path, whlist, bindex);
35765 +       else {
35766 +               int wkq_err;
35767 +               struct del_wh_children_args args = {
35768 +                       .errp           = &err,
35769 +                       .h_path         = &wh_path,
35770 +                       .whlist         = whlist,
35771 +                       .bindex         = bindex
35772 +               };
35773 +
35774 +               wkq_err = au_wkq_wait(call_del_wh_children, &args);
35775 +               if (unlikely(wkq_err))
35776 +                       err = wkq_err;
35777 +       }
35778 +       inode_unlock(wh_inode);
35779 +
35780 +       if (!err) {
35781 +               h_nlink = h_dir->i_nlink;
35782 +               err = vfsub_rmdir(h_dir, &wh_path);
35783 +               /* some fs doesn't change the parent nlink in some cases */
35784 +               h_nlink -= h_dir->i_nlink;
35785 +       }
35786 +
35787 +       if (!err) {
35788 +               if (au_ibtop(dir) == bindex) {
35789 +                       /* todo: dir->i_mutex is necessary */
35790 +                       au_cpup_attr_timesizes(dir);
35791 +                       if (h_nlink)
35792 +                               vfsub_drop_nlink(dir);
35793 +               }
35794 +               return 0; /* success */
35795 +       }
35796 +
35797 +       pr_warn("failed removing %pd(%d), ignored\n", wh_dentry, err);
35798 +       return err;
35799 +}
35800 +
35801 +static void call_rmdir_whtmp(void *args)
35802 +{
35803 +       int err;
35804 +       aufs_bindex_t bindex;
35805 +       struct au_whtmp_rmdir *a = args;
35806 +       struct super_block *sb;
35807 +       struct dentry *h_parent;
35808 +       struct inode *h_dir;
35809 +       struct au_hinode *hdir;
35810 +
35811 +       /* rmdir by nfsd may cause deadlock with this i_mutex */
35812 +       /* inode_lock(a->dir); */
35813 +       err = -EROFS;
35814 +       sb = a->dir->i_sb;
35815 +       si_read_lock(sb, !AuLock_FLUSH);
35816 +       if (!au_br_writable(a->br->br_perm))
35817 +               goto out;
35818 +       bindex = au_br_index(sb, a->br->br_id);
35819 +       if (unlikely(bindex < 0))
35820 +               goto out;
35821 +
35822 +       err = -EIO;
35823 +       ii_write_lock_parent(a->dir);
35824 +       h_parent = dget_parent(a->wh_dentry);
35825 +       h_dir = d_inode(h_parent);
35826 +       hdir = au_hi(a->dir, bindex);
35827 +       err = vfsub_mnt_want_write(au_br_mnt(a->br));
35828 +       if (unlikely(err))
35829 +               goto out_mnt;
35830 +       au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
35831 +       err = au_h_verify(a->wh_dentry, au_opt_udba(sb), h_dir, h_parent,
35832 +                         a->br);
35833 +       if (!err)
35834 +               err = au_whtmp_rmdir(a->dir, bindex, a->wh_dentry, &a->whlist);
35835 +       au_hn_inode_unlock(hdir);
35836 +       vfsub_mnt_drop_write(au_br_mnt(a->br));
35837 +
35838 +out_mnt:
35839 +       dput(h_parent);
35840 +       ii_write_unlock(a->dir);
35841 +out:
35842 +       /* inode_unlock(a->dir); */
35843 +       au_whtmp_rmdir_free(a);
35844 +       si_read_unlock(sb);
35845 +       au_nwt_done(&au_sbi(sb)->si_nowait);
35846 +       if (unlikely(err))
35847 +               AuIOErr("err %d\n", err);
35848 +}
35849 +
35850 +void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
35851 +                        struct dentry *wh_dentry, struct au_whtmp_rmdir *args)
35852 +{
35853 +       int wkq_err;
35854 +       struct super_block *sb;
35855 +
35856 +       IMustLock(dir);
35857 +
35858 +       /* all post-process will be done in do_rmdir_whtmp(). */
35859 +       sb = dir->i_sb;
35860 +       args->dir = au_igrab(dir);
35861 +       args->br = au_sbr(sb, bindex);
35862 +       au_lcnt_inc(&args->br->br_count);
35863 +       args->wh_dentry = dget(wh_dentry);
35864 +       wkq_err = au_wkq_nowait(call_rmdir_whtmp, args, sb, /*flags*/0);
35865 +       if (unlikely(wkq_err)) {
35866 +               pr_warn("rmdir error %pd (%d), ignored\n", wh_dentry, wkq_err);
35867 +               au_whtmp_rmdir_free(args);
35868 +       }
35869 +}
35870 diff -urN /usr/share/empty/fs/aufs/whout.h linux/fs/aufs/whout.h
35871 --- /usr/share/empty/fs/aufs/whout.h    1970-01-01 01:00:00.000000000 +0100
35872 +++ linux/fs/aufs/whout.h       2021-11-01 23:48:34.213025928 +0100
35873 @@ -0,0 +1,87 @@
35874 +/* SPDX-License-Identifier: GPL-2.0 */
35875 +/*
35876 + * Copyright (C) 2005-2021 Junjiro R. Okajima
35877 + *
35878 + * This program, aufs is free software; you can redistribute it and/or modify
35879 + * it under the terms of the GNU General Public License as published by
35880 + * the Free Software Foundation; either version 2 of the License, or
35881 + * (at your option) any later version.
35882 + *
35883 + * This program is distributed in the hope that it will be useful,
35884 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
35885 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
35886 + * GNU General Public License for more details.
35887 + *
35888 + * You should have received a copy of the GNU General Public License
35889 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
35890 + */
35891 +
35892 +/*
35893 + * whiteout for logical deletion and opaque directory
35894 + */
35895 +
35896 +#ifndef __AUFS_WHOUT_H__
35897 +#define __AUFS_WHOUT_H__
35898 +
35899 +#ifdef __KERNEL__
35900 +
35901 +#include "dir.h"
35902 +
35903 +/* whout.c */
35904 +int au_wh_name_alloc(struct qstr *wh, const struct qstr *name);
35905 +int au_wh_test(struct user_namespace *h_userns, struct path *h_ppath,
35906 +              struct qstr *wh_name, int try_sio);
35907 +int au_diropq_test(struct user_namespace *h_userns, struct path *h_path);
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 2021-11-01 23:48:34.213025928 +0100
35964 @@ -0,0 +1,372 @@
35965 +// SPDX-License-Identifier: GPL-2.0
35966 +/*
35967 + * Copyright (C) 2005-2021 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 2021-11-01 23:48:34.213025928 +0100
36340 @@ -0,0 +1,89 @@
36341 +/* SPDX-License-Identifier: GPL-2.0 */
36342 +/*
36343 + * Copyright (C) 2005-2021 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       2021-11-01 23:48:34.213025928 +0100
36433 @@ -0,0 +1,368 @@
36434 +// SPDX-License-Identifier: GPL-2.0
36435 +/*
36436 + * Copyright (C) 2014-2021 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 path *h_dst, struct path *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 +       struct dentry *h_dst_dentry, *h_src_dentry;
36508 +       struct user_namespace *h_dst_userns, *h_src_userns;
36509 +
36510 +       h_src_userns = mnt_user_ns(h_src->mnt);
36511 +       h_src_dentry = h_src->dentry;
36512 +       ssz = vfs_getxattr_alloc(h_src_userns, h_src_dentry, name, buf, 0,
36513 +                                GFP_NOFS);
36514 +       err = ssz;
36515 +       if (unlikely(err <= 0)) {
36516 +               if (err == -ENODATA
36517 +                   || (err == -EOPNOTSUPP
36518 +                       && ((ignore_flags & au_xattr_out_of_list)
36519 +                           || (au_test_nfs_noacl(d_inode(h_src_dentry))
36520 +                               && (!strcmp(name, XATTR_NAME_POSIX_ACL_ACCESS)
36521 +                                   || !strcmp(name,
36522 +                                              XATTR_NAME_POSIX_ACL_DEFAULT))))
36523 +                           ))
36524 +                       err = 0;
36525 +               if (err && (verbose || au_debug_test()))
36526 +                       pr_err("%s, err %d\n", name, err);
36527 +               goto out;
36528 +       }
36529 +
36530 +       /* unlock it temporary */
36531 +       h_dst_userns = mnt_user_ns(h_dst->mnt);
36532 +       h_dst_dentry = h_dst->dentry;
36533 +       h_idst = d_inode(h_dst_dentry);
36534 +       inode_unlock(h_idst);
36535 +       err = vfsub_setxattr(h_dst_userns, h_dst_dentry, name, *buf, ssz,
36536 +                            /*flags*/0);
36537 +       inode_lock_nested(h_idst, AuLsc_I_CHILD2);
36538 +       if (unlikely(err)) {
36539 +               if (verbose || au_debug_test())
36540 +                       pr_err("%s, err %d\n", name, err);
36541 +               err = au_xattr_ignore(err, name, ignore_flags);
36542 +       }
36543 +
36544 +out:
36545 +       return err;
36546 +}
36547 +
36548 +int au_cpup_xattr(struct path *h_dst, struct path *h_src, int ignore_flags,
36549 +                 unsigned int verbose)
36550 +{
36551 +       int err, unlocked, acl_access, acl_default;
36552 +       ssize_t ssz;
36553 +       struct dentry *h_dst_dentry, *h_src_dentry;
36554 +       struct inode *h_isrc, *h_idst;
36555 +       char *value, *p, *o, *e;
36556 +
36557 +       /* try stopping to update the source inode while we are referencing */
36558 +       /* there should not be the parent-child relationship between them */
36559 +       h_dst_dentry = h_dst->dentry;
36560 +       h_idst = d_inode(h_dst_dentry);
36561 +       h_src_dentry = h_src->dentry;
36562 +       h_isrc = d_inode(h_src_dentry);
36563 +       inode_unlock(h_idst);
36564 +       inode_lock_shared_nested(h_isrc, AuLsc_I_CHILD);
36565 +       inode_lock_nested(h_idst, AuLsc_I_CHILD2);
36566 +       unlocked = 0;
36567 +
36568 +       /* some filesystems don't list POSIX ACL, for example tmpfs */
36569 +       ssz = vfs_listxattr(h_src_dentry, NULL, 0);
36570 +       err = ssz;
36571 +       if (unlikely(err < 0)) {
36572 +               AuTraceErr(err);
36573 +               if (err == -ENODATA
36574 +                   || err == -EOPNOTSUPP)
36575 +                       err = 0;        /* ignore */
36576 +               goto out;
36577 +       }
36578 +
36579 +       err = 0;
36580 +       p = NULL;
36581 +       o = NULL;
36582 +       if (ssz) {
36583 +               err = -ENOMEM;
36584 +               p = kmalloc(ssz, GFP_NOFS);
36585 +               o = p;
36586 +               if (unlikely(!p))
36587 +                       goto out;
36588 +               err = vfs_listxattr(h_src_dentry, p, ssz);
36589 +       }
36590 +       inode_unlock_shared(h_isrc);
36591 +       unlocked = 1;
36592 +       AuDbg("err %d, ssz %zd\n", err, ssz);
36593 +       if (unlikely(err < 0))
36594 +               goto out_free;
36595 +
36596 +       err = 0;
36597 +       e = p + ssz;
36598 +       value = NULL;
36599 +       acl_access = 0;
36600 +       acl_default = 0;
36601 +       while (!err && p < e) {
36602 +               acl_access |= !strncmp(p, XATTR_NAME_POSIX_ACL_ACCESS,
36603 +                                      sizeof(XATTR_NAME_POSIX_ACL_ACCESS) - 1);
36604 +               acl_default |= !strncmp(p, XATTR_NAME_POSIX_ACL_DEFAULT,
36605 +                                       sizeof(XATTR_NAME_POSIX_ACL_DEFAULT)
36606 +                                       - 1);
36607 +               err = au_do_cpup_xattr(h_dst, h_src, p, &value, ignore_flags,
36608 +                                      verbose);
36609 +               p += strlen(p) + 1;
36610 +       }
36611 +       AuTraceErr(err);
36612 +       ignore_flags |= au_xattr_out_of_list;
36613 +       if (!err && !acl_access) {
36614 +               err = au_do_cpup_xattr(h_dst, h_src,
36615 +                                      XATTR_NAME_POSIX_ACL_ACCESS, &value,
36616 +                                      ignore_flags, verbose);
36617 +               AuTraceErr(err);
36618 +       }
36619 +       if (!err && !acl_default) {
36620 +               err = au_do_cpup_xattr(h_dst, h_src,
36621 +                                      XATTR_NAME_POSIX_ACL_DEFAULT, &value,
36622 +                                      ignore_flags, verbose);
36623 +               AuTraceErr(err);
36624 +       }
36625 +
36626 +       au_kfree_try_rcu(value);
36627 +
36628 +out_free:
36629 +       au_kfree_try_rcu(o);
36630 +out:
36631 +       if (!unlocked)
36632 +               inode_unlock_shared(h_isrc);
36633 +       AuTraceErr(err);
36634 +       return err;
36635 +}
36636 +
36637 +/* ---------------------------------------------------------------------- */
36638 +
36639 +static int au_smack_reentering(struct super_block *sb)
36640 +{
36641 +#if IS_ENABLED(CONFIG_SECURITY_SMACK) || IS_ENABLED(CONFIG_SECURITY_SELINUX)
36642 +       /*
36643 +        * as a part of lookup, smack_d_instantiate() is called, and it calls
36644 +        * i_op->getxattr(). ouch.
36645 +        */
36646 +       return si_pid_test(sb);
36647 +#else
36648 +       return 0;
36649 +#endif
36650 +}
36651 +
36652 +enum {
36653 +       AU_XATTR_LIST,
36654 +       AU_XATTR_GET
36655 +};
36656 +
36657 +struct au_lgxattr {
36658 +       int type;
36659 +       union {
36660 +               struct {
36661 +                       char    *list;
36662 +                       size_t  size;
36663 +               } list;
36664 +               struct {
36665 +                       const char      *name;
36666 +                       void            *value;
36667 +                       size_t          size;
36668 +               } get;
36669 +       } u;
36670 +};
36671 +
36672 +static ssize_t au_lgxattr(struct dentry *dentry, struct inode *inode,
36673 +                         struct au_lgxattr *arg)
36674 +{
36675 +       ssize_t err;
36676 +       int reenter;
36677 +       struct path h_path;
36678 +       struct super_block *sb;
36679 +
36680 +       sb = dentry->d_sb;
36681 +       reenter = au_smack_reentering(sb);
36682 +       if (!reenter) {
36683 +               err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
36684 +               if (unlikely(err))
36685 +                       goto out;
36686 +       }
36687 +       err = au_h_path_getattr(dentry, inode, /*force*/1, &h_path, reenter);
36688 +       if (unlikely(err))
36689 +               goto out_si;
36690 +       if (unlikely(!h_path.dentry))
36691 +               /* illegally overlapped or something */
36692 +               goto out_di; /* pretending success */
36693 +
36694 +       /* always topmost entry only */
36695 +       switch (arg->type) {
36696 +       case AU_XATTR_LIST:
36697 +               err = vfs_listxattr(h_path.dentry,
36698 +                                   arg->u.list.list, arg->u.list.size);
36699 +               break;
36700 +       case AU_XATTR_GET:
36701 +               AuDebugOn(d_is_negative(h_path.dentry));
36702 +               err = vfs_getxattr(mnt_user_ns(h_path.mnt), h_path.dentry,
36703 +                                  arg->u.get.name, arg->u.get.value,
36704 +                                  arg->u.get.size);
36705 +               break;
36706 +       }
36707 +
36708 +out_di:
36709 +       if (!reenter)
36710 +               di_read_unlock(dentry, AuLock_IR);
36711 +out_si:
36712 +       if (!reenter)
36713 +               si_read_unlock(sb);
36714 +out:
36715 +       AuTraceErr(err);
36716 +       return err;
36717 +}
36718 +
36719 +ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size)
36720 +{
36721 +       struct au_lgxattr arg = {
36722 +               .type = AU_XATTR_LIST,
36723 +               .u.list = {
36724 +                       .list   = list,
36725 +                       .size   = size
36726 +               },
36727 +       };
36728 +
36729 +       return au_lgxattr(dentry, /*inode*/NULL, &arg);
36730 +}
36731 +
36732 +static ssize_t au_getxattr(struct dentry *dentry, struct inode *inode,
36733 +                          const char *name, void *value, size_t size)
36734 +{
36735 +       struct au_lgxattr arg = {
36736 +               .type = AU_XATTR_GET,
36737 +               .u.get = {
36738 +                       .name   = name,
36739 +                       .value  = value,
36740 +                       .size   = size
36741 +               },
36742 +       };
36743 +
36744 +       return au_lgxattr(dentry, inode, &arg);
36745 +}
36746 +
36747 +static int au_setxattr(struct dentry *dentry, struct inode *inode,
36748 +                      const char *name, const void *value, size_t size,
36749 +                      int flags)
36750 +{
36751 +       struct au_sxattr arg = {
36752 +               .type = AU_XATTR_SET,
36753 +               .u.set = {
36754 +                       .name   = name,
36755 +                       .value  = value,
36756 +                       .size   = size,
36757 +                       .flags  = flags
36758 +               },
36759 +       };
36760 +
36761 +       return au_sxattr(dentry, inode, &arg);
36762 +}
36763 +
36764 +/* ---------------------------------------------------------------------- */
36765 +
36766 +static int au_xattr_get(const struct xattr_handler *handler,
36767 +                       struct dentry *dentry, struct inode *inode,
36768 +                       const char *name, void *buffer, size_t size)
36769 +{
36770 +       return au_getxattr(dentry, inode, name, buffer, size);
36771 +}
36772 +
36773 +static int au_xattr_set(const struct xattr_handler *handler,
36774 +                       struct user_namespace *userns,
36775 +                       struct dentry *dentry, struct inode *inode,
36776 +                       const char *name, const void *value, size_t size,
36777 +                       int flags)
36778 +{
36779 +       return au_setxattr(dentry, inode, name, value, size, flags);
36780 +}
36781 +
36782 +static const struct xattr_handler au_xattr_handler = {
36783 +       .name   = "",
36784 +       .prefix = "",
36785 +       .get    = au_xattr_get,
36786 +       .set    = au_xattr_set
36787 +};
36788 +
36789 +static const struct xattr_handler *au_xattr_handlers[] = {
36790 +#ifdef CONFIG_FS_POSIX_ACL
36791 +       &posix_acl_access_xattr_handler,
36792 +       &posix_acl_default_xattr_handler,
36793 +#endif
36794 +       &au_xattr_handler, /* must be last */
36795 +       NULL
36796 +};
36797 +
36798 +void au_xattr_init(struct super_block *sb)
36799 +{
36800 +       sb->s_xattr = au_xattr_handlers;
36801 +}
36802 diff -urN /usr/share/empty/fs/aufs/xino.c linux/fs/aufs/xino.c
36803 --- /usr/share/empty/fs/aufs/xino.c     1970-01-01 01:00:00.000000000 +0100
36804 +++ linux/fs/aufs/xino.c        2021-11-01 23:48:34.213025928 +0100
36805 @@ -0,0 +1,1926 @@
36806 +// SPDX-License-Identifier: GPL-2.0
36807 +/*
36808 + * Copyright (C) 2005-2021 Junjiro R. Okajima
36809 + *
36810 + * This program, aufs is free software; you can redistribute it and/or modify
36811 + * it under the terms of the GNU General Public License as published by
36812 + * the Free Software Foundation; either version 2 of the License, or
36813 + * (at your option) any later version.
36814 + *
36815 + * This program is distributed in the hope that it will be useful,
36816 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
36817 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36818 + * GNU General Public License for more details.
36819 + *
36820 + * You should have received a copy of the GNU General Public License
36821 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
36822 + */
36823 +
36824 +/*
36825 + * external inode number translation table and bitmap
36826 + *
36827 + * things to consider
36828 + * - the lifetime
36829 + *   + au_xino object
36830 + *   + XINO files (xino, xib, xigen)
36831 + *   + dynamic debugfs entries (xiN)
36832 + *   + static debugfs entries (xib, xigen)
36833 + *   + static sysfs entry (xi_path)
36834 + * - several entry points to handle them.
36835 + *   + mount(2) without xino option (default)
36836 + *   + mount(2) with xino option
36837 + *   + mount(2) with noxino option
36838 + *   + umount(2)
36839 + *   + remount with add/del branches
36840 + *   + remount with xino/noxino options
36841 + */
36842 +
36843 +#include <linux/seq_file.h>
36844 +#include <linux/statfs.h>
36845 +#include "aufs.h"
36846 +
36847 +static aufs_bindex_t sbr_find_shared(struct super_block *sb, aufs_bindex_t btop,
36848 +                                    aufs_bindex_t bbot,
36849 +                                    struct super_block *h_sb)
36850 +{
36851 +       /* todo: try binary-search if the branches are many */
36852 +       for (; btop <= bbot; btop++)
36853 +               if (h_sb == au_sbr_sb(sb, btop))
36854 +                       return btop;
36855 +       return -1;
36856 +}
36857 +
36858 +/*
36859 + * find another branch who is on the same filesystem of the specified
36860 + * branch{@btgt}. search until @bbot.
36861 + */
36862 +static aufs_bindex_t is_sb_shared(struct super_block *sb, aufs_bindex_t btgt,
36863 +                                 aufs_bindex_t bbot)
36864 +{
36865 +       aufs_bindex_t bindex;
36866 +       struct super_block *tgt_sb;
36867 +
36868 +       tgt_sb = au_sbr_sb(sb, btgt);
36869 +       bindex = sbr_find_shared(sb, /*btop*/0, btgt - 1, tgt_sb);
36870 +       if (bindex < 0)
36871 +               bindex = sbr_find_shared(sb, btgt + 1, bbot, tgt_sb);
36872 +
36873 +       return bindex;
36874 +}
36875 +
36876 +/* ---------------------------------------------------------------------- */
36877 +
36878 +/*
36879 + * stop unnecessary notify events at creating xino files
36880 + */
36881 +
36882 +aufs_bindex_t au_xi_root(struct super_block *sb, struct dentry *dentry)
36883 +{
36884 +       aufs_bindex_t bfound, bindex, bbot;
36885 +       struct dentry *parent;
36886 +       struct au_branch *br;
36887 +
36888 +       bfound = -1;
36889 +       parent = dentry->d_parent; /* safe d_parent access */
36890 +       bbot = au_sbbot(sb);
36891 +       for (bindex = 0; bindex <= bbot; bindex++) {
36892 +               br = au_sbr(sb, bindex);
36893 +               if (au_br_dentry(br) == parent) {
36894 +                       bfound = bindex;
36895 +                       break;
36896 +               }
36897 +       }
36898 +
36899 +       AuDbg("bfound b%d\n", bfound);
36900 +       return bfound;
36901 +}
36902 +
36903 +struct au_xino_lock_dir {
36904 +       struct au_hinode *hdir;
36905 +       struct dentry *parent;
36906 +       struct inode *dir;
36907 +};
36908 +
36909 +static struct dentry *au_dget_parent_lock(struct dentry *dentry,
36910 +                                         unsigned int lsc)
36911 +{
36912 +       struct dentry *parent;
36913 +       struct inode *dir;
36914 +
36915 +       parent = dget_parent(dentry);
36916 +       dir = d_inode(parent);
36917 +       inode_lock_nested(dir, lsc);
36918 +#if 0 /* it should not happen */
36919 +       spin_lock(&dentry->d_lock);
36920 +       if (unlikely(dentry->d_parent != parent)) {
36921 +               spin_unlock(&dentry->d_lock);
36922 +               inode_unlock(dir);
36923 +               dput(parent);
36924 +               parent = NULL;
36925 +               goto out;
36926 +       }
36927 +       spin_unlock(&dentry->d_lock);
36928 +
36929 +out:
36930 +#endif
36931 +       return parent;
36932 +}
36933 +
36934 +static void au_xino_lock_dir(struct super_block *sb, struct path *xipath,
36935 +                            struct au_xino_lock_dir *ldir)
36936 +{
36937 +       aufs_bindex_t bindex;
36938 +
36939 +       ldir->hdir = NULL;
36940 +       bindex = au_xi_root(sb, xipath->dentry);
36941 +       if (bindex >= 0) {
36942 +               /* rw branch root */
36943 +               ldir->hdir = au_hi(d_inode(sb->s_root), bindex);
36944 +               au_hn_inode_lock_nested(ldir->hdir, AuLsc_I_PARENT);
36945 +       } else {
36946 +               /* other */
36947 +               ldir->parent = au_dget_parent_lock(xipath->dentry,
36948 +                                                  AuLsc_I_PARENT);
36949 +               ldir->dir = d_inode(ldir->parent);
36950 +       }
36951 +}
36952 +
36953 +static void au_xino_unlock_dir(struct au_xino_lock_dir *ldir)
36954 +{
36955 +       if (ldir->hdir)
36956 +               au_hn_inode_unlock(ldir->hdir);
36957 +       else {
36958 +               inode_unlock(ldir->dir);
36959 +               dput(ldir->parent);
36960 +       }
36961 +}
36962 +
36963 +/* ---------------------------------------------------------------------- */
36964 +
36965 +/*
36966 + * create and set a new xino file
36967 + */
36968 +struct file *au_xino_create(struct super_block *sb, char *fpath, int silent,
36969 +                           int wbrtop)
36970 +{
36971 +       struct file *file;
36972 +       struct dentry *h_parent, *d;
36973 +       struct inode *h_dir, *inode;
36974 +       int err;
36975 +       static DEFINE_MUTEX(mtx);
36976 +
36977 +       /*
36978 +        * at mount-time, and the xino file is the default path,
36979 +        * hnotify is disabled so we have no notify events to ignore.
36980 +        * when a user specified the xino, we cannot get au_hdir to be ignored.
36981 +        */
36982 +       if (!wbrtop)
36983 +               mutex_lock(&mtx);
36984 +       file = vfsub_filp_open(fpath, O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
36985 +                              /* | __FMODE_NONOTIFY */,
36986 +                              0666);
36987 +       if (IS_ERR(file)) {
36988 +               if (!wbrtop)
36989 +                       mutex_unlock(&mtx);
36990 +               if (!silent)
36991 +                       pr_err("open %s(%ld)\n", fpath, PTR_ERR(file));
36992 +               return file;
36993 +       }
36994 +
36995 +       /* keep file count */
36996 +       err = 0;
36997 +       d = file->f_path.dentry;
36998 +       h_parent = au_dget_parent_lock(d, AuLsc_I_PARENT);
36999 +       if (!wbrtop)
37000 +               mutex_unlock(&mtx);
37001 +       /* mnt_want_write() is unnecessary here */
37002 +       h_dir = d_inode(h_parent);
37003 +       inode = file_inode(file);
37004 +       /* no delegation since it is just created */
37005 +       if (inode->i_nlink)
37006 +               err = vfsub_unlink(h_dir, &file->f_path, /*delegated*/NULL,
37007 +                                  /*force*/0);
37008 +       inode_unlock(h_dir);
37009 +       dput(h_parent);
37010 +       if (unlikely(err)) {
37011 +               if (!silent)
37012 +                       pr_err("unlink %s(%d)\n", fpath, err);
37013 +               goto out;
37014 +       }
37015 +
37016 +       err = -EINVAL;
37017 +       if (unlikely(sb == d->d_sb)) {
37018 +               if (!silent)
37019 +                       pr_err("%s must be outside\n", fpath);
37020 +               goto out;
37021 +       }
37022 +       if (unlikely(au_test_fs_bad_xino(d->d_sb))) {
37023 +               if (!silent)
37024 +                       pr_err("xino doesn't support %s(%s)\n",
37025 +                              fpath, au_sbtype(d->d_sb));
37026 +               goto out;
37027 +       }
37028 +       return file; /* success */
37029 +
37030 +out:
37031 +       fput(file);
37032 +       file = ERR_PTR(err);
37033 +       return file;
37034 +}
37035 +
37036 +/*
37037 + * create a new xinofile at the same place/path as @base.
37038 + */
37039 +struct file *au_xino_create2(struct super_block *sb, struct path *base,
37040 +                            struct file *copy_src)
37041 +{
37042 +       struct file *file;
37043 +       struct dentry *dentry;
37044 +       struct inode *dir, *delegated;
37045 +       struct qstr *name;
37046 +       struct path ppath, path;
37047 +       int err, do_unlock;
37048 +       struct au_xino_lock_dir ldir;
37049 +
37050 +       do_unlock = 1;
37051 +       au_xino_lock_dir(sb, base, &ldir);
37052 +       dentry = base->dentry;
37053 +       ppath.dentry = dentry->d_parent; /* dir inode is locked */
37054 +       ppath.mnt = base->mnt;
37055 +       dir = d_inode(ppath.dentry);
37056 +       IMustLock(dir);
37057 +
37058 +       name = &dentry->d_name;
37059 +       path.dentry = vfsub_lookup_one_len(name->name, &ppath, name->len);
37060 +       if (IS_ERR(path.dentry)) {
37061 +               file = (void *)path.dentry;
37062 +               pr_err("%pd lookup err %ld\n", dentry, PTR_ERR(path.dentry));
37063 +               goto out;
37064 +       }
37065 +
37066 +       /* no need to mnt_want_write() since we call dentry_open() later */
37067 +       err = vfs_create(mnt_user_ns(base->mnt), dir, path.dentry, 0666, NULL);
37068 +       if (unlikely(err)) {
37069 +               file = ERR_PTR(err);
37070 +               pr_err("%pd create err %d\n", dentry, err);
37071 +               goto out_dput;
37072 +       }
37073 +
37074 +       path.mnt = base->mnt;
37075 +       file = vfsub_dentry_open(&path,
37076 +                                O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
37077 +                                /* | __FMODE_NONOTIFY */);
37078 +       if (IS_ERR(file)) {
37079 +               pr_err("%pd open err %ld\n", dentry, PTR_ERR(file));
37080 +               goto out_dput;
37081 +       }
37082 +
37083 +       delegated = NULL;
37084 +       err = vfsub_unlink(dir, &file->f_path, &delegated, /*force*/0);
37085 +       au_xino_unlock_dir(&ldir);
37086 +       do_unlock = 0;
37087 +       if (unlikely(err == -EWOULDBLOCK)) {
37088 +               pr_warn("cannot retry for NFSv4 delegation"
37089 +                       " for an internal unlink\n");
37090 +               iput(delegated);
37091 +       }
37092 +       if (unlikely(err)) {
37093 +               pr_err("%pd unlink err %d\n", dentry, err);
37094 +               goto out_fput;
37095 +       }
37096 +
37097 +       if (copy_src) {
37098 +               /* no one can touch copy_src xino */
37099 +               err = au_copy_file(file, copy_src, vfsub_f_size_read(copy_src));
37100 +               if (unlikely(err)) {
37101 +                       pr_err("%pd copy err %d\n", dentry, err);
37102 +                       goto out_fput;
37103 +               }
37104 +       }
37105 +       goto out_dput; /* success */
37106 +
37107 +out_fput:
37108 +       fput(file);
37109 +       file = ERR_PTR(err);
37110 +out_dput:
37111 +       dput(path.dentry);
37112 +out:
37113 +       if (do_unlock)
37114 +               au_xino_unlock_dir(&ldir);
37115 +       return file;
37116 +}
37117 +
37118 +struct file *au_xino_file1(struct au_xino *xi)
37119 +{
37120 +       struct file *file;
37121 +       unsigned int u, nfile;
37122 +
37123 +       file = NULL;
37124 +       nfile = xi->xi_nfile;
37125 +       for (u = 0; u < nfile; u++) {
37126 +               file = xi->xi_file[u];
37127 +               if (file)
37128 +                       break;
37129 +       }
37130 +
37131 +       return file;
37132 +}
37133 +
37134 +static int au_xino_file_set(struct au_xino *xi, int idx, struct file *file)
37135 +{
37136 +       int err;
37137 +       struct file *f;
37138 +       void *p;
37139 +
37140 +       if (file)
37141 +               get_file(file);
37142 +
37143 +       err = 0;
37144 +       f = NULL;
37145 +       if (idx < xi->xi_nfile) {
37146 +               f = xi->xi_file[idx];
37147 +               if (f)
37148 +                       fput(f);
37149 +       } else {
37150 +               p = au_kzrealloc(xi->xi_file,
37151 +                                sizeof(*xi->xi_file) * xi->xi_nfile,
37152 +                                sizeof(*xi->xi_file) * (idx + 1),
37153 +                                GFP_NOFS, /*may_shrink*/0);
37154 +               if (p) {
37155 +                       MtxMustLock(&xi->xi_mtx);
37156 +                       xi->xi_file = p;
37157 +                       xi->xi_nfile = idx + 1;
37158 +               } else {
37159 +                       err = -ENOMEM;
37160 +                       if (file)
37161 +                               fput(file);
37162 +                       goto out;
37163 +               }
37164 +       }
37165 +       xi->xi_file[idx] = file;
37166 +
37167 +out:
37168 +       return err;
37169 +}
37170 +
37171 +/*
37172 + * if @xinew->xi is not set, then create new xigen file.
37173 + */
37174 +struct file *au_xi_new(struct super_block *sb, struct au_xi_new *xinew)
37175 +{
37176 +       struct file *file;
37177 +       int err;
37178 +
37179 +       SiMustAnyLock(sb);
37180 +
37181 +       file = au_xino_create2(sb, xinew->base, xinew->copy_src);
37182 +       if (IS_ERR(file)) {
37183 +               err = PTR_ERR(file);
37184 +               pr_err("%s[%d], err %d\n",
37185 +                      xinew->xi ? "xino" : "xigen",
37186 +                      xinew->idx, err);
37187 +               goto out;
37188 +       }
37189 +
37190 +       if (xinew->xi)
37191 +               err = au_xino_file_set(xinew->xi, xinew->idx, file);
37192 +       else {
37193 +               BUG();
37194 +               /* todo: make xigen file an array */
37195 +               /* err = au_xigen_file_set(sb, xinew->idx, file); */
37196 +       }
37197 +       fput(file);
37198 +       if (unlikely(err))
37199 +               file = ERR_PTR(err);
37200 +
37201 +out:
37202 +       return file;
37203 +}
37204 +
37205 +/* ---------------------------------------------------------------------- */
37206 +
37207 +/*
37208 + * truncate xino files
37209 + */
37210 +static int au_xino_do_trunc(struct super_block *sb, aufs_bindex_t bindex,
37211 +                           int idx, struct kstatfs *st)
37212 +{
37213 +       int err;
37214 +       blkcnt_t blocks;
37215 +       struct file *file, *new_xino;
37216 +       struct au_xi_new xinew = {
37217 +               .idx = idx
37218 +       };
37219 +
37220 +       err = 0;
37221 +       xinew.xi = au_sbr(sb, bindex)->br_xino;
37222 +       file = au_xino_file(xinew.xi, idx);
37223 +       if (!file)
37224 +               goto out;
37225 +
37226 +       xinew.base = &file->f_path;
37227 +       err = vfs_statfs(xinew.base, st);
37228 +       if (unlikely(err)) {
37229 +               AuErr1("statfs err %d, ignored\n", err);
37230 +               err = 0;
37231 +               goto out;
37232 +       }
37233 +
37234 +       blocks = file_inode(file)->i_blocks;
37235 +       pr_info("begin truncating xino(b%d-%d), ib%llu, %llu/%llu free blks\n",
37236 +               bindex, idx, (u64)blocks, st->f_bfree, st->f_blocks);
37237 +
37238 +       xinew.copy_src = file;
37239 +       new_xino = au_xi_new(sb, &xinew);
37240 +       if (IS_ERR(new_xino)) {
37241 +               err = PTR_ERR(new_xino);
37242 +               pr_err("xino(b%d-%d), err %d, ignored\n", bindex, idx, err);
37243 +               goto out;
37244 +       }
37245 +
37246 +       err = vfs_statfs(&new_xino->f_path, st);
37247 +       if (!err)
37248 +               pr_info("end truncating xino(b%d-%d), ib%llu, %llu/%llu free blks\n",
37249 +                       bindex, idx, (u64)file_inode(new_xino)->i_blocks,
37250 +                       st->f_bfree, st->f_blocks);
37251 +       else {
37252 +               AuErr1("statfs err %d, ignored\n", err);
37253 +               err = 0;
37254 +       }
37255 +
37256 +out:
37257 +       return err;
37258 +}
37259 +
37260 +int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex, int idx_begin)
37261 +{
37262 +       int err, i;
37263 +       unsigned long jiffy;
37264 +       aufs_bindex_t bbot;
37265 +       struct kstatfs *st;
37266 +       struct au_branch *br;
37267 +       struct au_xino *xi;
37268 +
37269 +       err = -ENOMEM;
37270 +       st = kmalloc(sizeof(*st), GFP_NOFS);
37271 +       if (unlikely(!st))
37272 +               goto out;
37273 +
37274 +       err = -EINVAL;
37275 +       bbot = au_sbbot(sb);
37276 +       if (unlikely(bindex < 0 || bbot < bindex))
37277 +               goto out_st;
37278 +
37279 +       err = 0;
37280 +       jiffy = jiffies;
37281 +       br = au_sbr(sb, bindex);
37282 +       xi = br->br_xino;
37283 +       for (i = idx_begin; !err && i < xi->xi_nfile; i++)
37284 +               err = au_xino_do_trunc(sb, bindex, i, st);
37285 +       if (!err)
37286 +               au_sbi(sb)->si_xino_jiffy = jiffy;
37287 +
37288 +out_st:
37289 +       au_kfree_rcu(st);
37290 +out:
37291 +       return err;
37292 +}
37293 +
37294 +struct xino_do_trunc_args {
37295 +       struct super_block *sb;
37296 +       struct au_branch *br;
37297 +       int idx;
37298 +};
37299 +
37300 +static void xino_do_trunc(void *_args)
37301 +{
37302 +       struct xino_do_trunc_args *args = _args;
37303 +       struct super_block *sb;
37304 +       struct au_branch *br;
37305 +       struct inode *dir;
37306 +       int err, idx;
37307 +       aufs_bindex_t bindex;
37308 +
37309 +       err = 0;
37310 +       sb = args->sb;
37311 +       dir = d_inode(sb->s_root);
37312 +       br = args->br;
37313 +       idx = args->idx;
37314 +
37315 +       si_noflush_write_lock(sb);
37316 +       ii_read_lock_parent(dir);
37317 +       bindex = au_br_index(sb, br->br_id);
37318 +       err = au_xino_trunc(sb, bindex, idx);
37319 +       ii_read_unlock(dir);
37320 +       if (unlikely(err))
37321 +               pr_warn("err b%d, (%d)\n", bindex, err);
37322 +       atomic_dec(&br->br_xino->xi_truncating);
37323 +       au_lcnt_dec(&br->br_count);
37324 +       si_write_unlock(sb);
37325 +       au_nwt_done(&au_sbi(sb)->si_nowait);
37326 +       au_kfree_rcu(args);
37327 +}
37328 +
37329 +/*
37330 + * returns the index in the xi_file array whose corresponding file is necessary
37331 + * to truncate, or -1 which means no need to truncate.
37332 + */
37333 +static int xino_trunc_test(struct super_block *sb, struct au_branch *br)
37334 +{
37335 +       int err;
37336 +       unsigned int u;
37337 +       struct kstatfs st;
37338 +       struct au_sbinfo *sbinfo;
37339 +       struct au_xino *xi;
37340 +       struct file *file;
37341 +
37342 +       /* todo: si_xino_expire and the ratio should be customizable */
37343 +       sbinfo = au_sbi(sb);
37344 +       if (time_before(jiffies,
37345 +                       sbinfo->si_xino_jiffy + sbinfo->si_xino_expire))
37346 +               return -1;
37347 +
37348 +       /* truncation border */
37349 +       xi = br->br_xino;
37350 +       for (u = 0; u < xi->xi_nfile; u++) {
37351 +               file = au_xino_file(xi, u);
37352 +               if (!file)
37353 +                       continue;
37354 +
37355 +               err = vfs_statfs(&file->f_path, &st);
37356 +               if (unlikely(err)) {
37357 +                       AuErr1("statfs err %d, ignored\n", err);
37358 +                       return -1;
37359 +               }
37360 +               if (div64_u64(st.f_bfree * 100, st.f_blocks)
37361 +                   >= AUFS_XINO_DEF_TRUNC)
37362 +                       return u;
37363 +       }
37364 +
37365 +       return -1;
37366 +}
37367 +
37368 +static void xino_try_trunc(struct super_block *sb, struct au_branch *br)
37369 +{
37370 +       int idx;
37371 +       struct xino_do_trunc_args *args;
37372 +       int wkq_err;
37373 +
37374 +       idx = xino_trunc_test(sb, br);
37375 +       if (idx < 0)
37376 +               return;
37377 +
37378 +       if (atomic_inc_return(&br->br_xino->xi_truncating) > 1)
37379 +               goto out;
37380 +
37381 +       /* lock and kfree() will be called in trunc_xino() */
37382 +       args = kmalloc(sizeof(*args), GFP_NOFS);
37383 +       if (unlikely(!args)) {
37384 +               AuErr1("no memory\n");
37385 +               goto out;
37386 +       }
37387 +
37388 +       au_lcnt_inc(&br->br_count);
37389 +       args->sb = sb;
37390 +       args->br = br;
37391 +       args->idx = idx;
37392 +       wkq_err = au_wkq_nowait(xino_do_trunc, args, sb, /*flags*/0);
37393 +       if (!wkq_err)
37394 +               return; /* success */
37395 +
37396 +       pr_err("wkq %d\n", wkq_err);
37397 +       au_lcnt_dec(&br->br_count);
37398 +       au_kfree_rcu(args);
37399 +
37400 +out:
37401 +       atomic_dec(&br->br_xino->xi_truncating);
37402 +}
37403 +
37404 +/* ---------------------------------------------------------------------- */
37405 +
37406 +struct au_xi_calc {
37407 +       int idx;
37408 +       loff_t pos;
37409 +};
37410 +
37411 +static void au_xi_calc(struct super_block *sb, ino_t h_ino,
37412 +                      struct au_xi_calc *calc)
37413 +{
37414 +       loff_t maxent;
37415 +
37416 +       maxent = au_xi_maxent(sb);
37417 +       calc->idx = div64_u64_rem(h_ino, maxent, &calc->pos);
37418 +       calc->pos *= sizeof(ino_t);
37419 +}
37420 +
37421 +static int au_xino_do_new_async(struct super_block *sb, struct au_branch *br,
37422 +                               struct au_xi_calc *calc)
37423 +{
37424 +       int err;
37425 +       struct file *file;
37426 +       struct au_xino *xi = br->br_xino;
37427 +       struct au_xi_new xinew = {
37428 +               .xi = xi
37429 +       };
37430 +
37431 +       SiMustAnyLock(sb);
37432 +
37433 +       err = 0;
37434 +       if (!xi)
37435 +               goto out;
37436 +
37437 +       mutex_lock(&xi->xi_mtx);
37438 +       file = au_xino_file(xi, calc->idx);
37439 +       if (file)
37440 +               goto out_mtx;
37441 +
37442 +       file = au_xino_file(xi, /*idx*/-1);
37443 +       AuDebugOn(!file);
37444 +       xinew.idx = calc->idx;
37445 +       xinew.base = &file->f_path;
37446 +       /* xinew.copy_src = NULL; */
37447 +       file = au_xi_new(sb, &xinew);
37448 +       if (IS_ERR(file))
37449 +               err = PTR_ERR(file);
37450 +
37451 +out_mtx:
37452 +       mutex_unlock(&xi->xi_mtx);
37453 +out:
37454 +       return err;
37455 +}
37456 +
37457 +struct au_xino_do_new_async_args {
37458 +       struct super_block *sb;
37459 +       struct au_branch *br;
37460 +       struct au_xi_calc calc;
37461 +       ino_t ino;
37462 +};
37463 +
37464 +struct au_xi_writing {
37465 +       struct hlist_bl_node node;
37466 +       ino_t h_ino, ino;
37467 +};
37468 +
37469 +static int au_xino_do_write(struct file *file, struct au_xi_calc *calc,
37470 +                           ino_t ino);
37471 +
37472 +static void au_xino_call_do_new_async(void *args)
37473 +{
37474 +       struct au_xino_do_new_async_args *a = args;
37475 +       struct au_branch *br;
37476 +       struct super_block *sb;
37477 +       struct au_sbinfo *sbi;
37478 +       struct inode *root;
37479 +       struct file *file;
37480 +       struct au_xi_writing *del, *p;
37481 +       struct hlist_bl_head *hbl;
37482 +       struct hlist_bl_node *pos;
37483 +       int err;
37484 +
37485 +       br = a->br;
37486 +       sb = a->sb;
37487 +       sbi = au_sbi(sb);
37488 +       si_noflush_read_lock(sb);
37489 +       root = d_inode(sb->s_root);
37490 +       ii_read_lock_child(root);
37491 +       err = au_xino_do_new_async(sb, br, &a->calc);
37492 +       if (unlikely(err)) {
37493 +               AuIOErr("err %d\n", err);
37494 +               goto out;
37495 +       }
37496 +
37497 +       file = au_xino_file(br->br_xino, a->calc.idx);
37498 +       AuDebugOn(!file);
37499 +       err = au_xino_do_write(file, &a->calc, a->ino);
37500 +       if (unlikely(err)) {
37501 +               AuIOErr("err %d\n", err);
37502 +               goto out;
37503 +       }
37504 +
37505 +       del = NULL;
37506 +       hbl = &br->br_xino->xi_writing;
37507 +       hlist_bl_lock(hbl);
37508 +       au_hbl_for_each(pos, hbl) {
37509 +               p = container_of(pos, struct au_xi_writing, node);
37510 +               if (p->ino == a->ino) {
37511 +                       del = p;
37512 +                       hlist_bl_del(&p->node);
37513 +                       break;
37514 +               }
37515 +       }
37516 +       hlist_bl_unlock(hbl);
37517 +       au_kfree_rcu(del);
37518 +
37519 +out:
37520 +       au_lcnt_dec(&br->br_count);
37521 +       ii_read_unlock(root);
37522 +       si_read_unlock(sb);
37523 +       au_nwt_done(&sbi->si_nowait);
37524 +       au_kfree_rcu(a);
37525 +}
37526 +
37527 +/*
37528 + * create a new xino file asynchronously
37529 + */
37530 +static int au_xino_new_async(struct super_block *sb, struct au_branch *br,
37531 +                            struct au_xi_calc *calc, ino_t ino)
37532 +{
37533 +       int err;
37534 +       struct au_xino_do_new_async_args *arg;
37535 +
37536 +       err = -ENOMEM;
37537 +       arg = kmalloc(sizeof(*arg), GFP_NOFS);
37538 +       if (unlikely(!arg))
37539 +               goto out;
37540 +
37541 +       arg->sb = sb;
37542 +       arg->br = br;
37543 +       arg->calc = *calc;
37544 +       arg->ino = ino;
37545 +       au_lcnt_inc(&br->br_count);
37546 +       err = au_wkq_nowait(au_xino_call_do_new_async, arg, sb, AuWkq_NEST);
37547 +       if (unlikely(err)) {
37548 +               pr_err("wkq %d\n", err);
37549 +               au_lcnt_dec(&br->br_count);
37550 +               au_kfree_rcu(arg);
37551 +       }
37552 +
37553 +out:
37554 +       return err;
37555 +}
37556 +
37557 +/*
37558 + * read @ino from xinofile for the specified branch{@sb, @bindex}
37559 + * at the position of @h_ino.
37560 + */
37561 +int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
37562 +                ino_t *ino)
37563 +{
37564 +       int err;
37565 +       ssize_t sz;
37566 +       struct au_xi_calc calc;
37567 +       struct au_sbinfo *sbinfo;
37568 +       struct file *file;
37569 +       struct au_xino *xi;
37570 +       struct hlist_bl_head *hbl;
37571 +       struct hlist_bl_node *pos;
37572 +       struct au_xi_writing *p;
37573 +
37574 +       *ino = 0;
37575 +       if (!au_opt_test(au_mntflags(sb), XINO))
37576 +               return 0; /* no xino */
37577 +
37578 +       err = 0;
37579 +       au_xi_calc(sb, h_ino, &calc);
37580 +       xi = au_sbr(sb, bindex)->br_xino;
37581 +       file = au_xino_file(xi, calc.idx);
37582 +       if (!file) {
37583 +               hbl = &xi->xi_writing;
37584 +               hlist_bl_lock(hbl);
37585 +               au_hbl_for_each(pos, hbl) {
37586 +                       p = container_of(pos, struct au_xi_writing, node);
37587 +                       if (p->h_ino == h_ino) {
37588 +                               AuDbg("hi%llu, i%llu, found\n",
37589 +                                     (u64)p->h_ino, (u64)p->ino);
37590 +                               *ino = p->ino;
37591 +                               break;
37592 +                       }
37593 +               }
37594 +               hlist_bl_unlock(hbl);
37595 +               return 0;
37596 +       } else if (vfsub_f_size_read(file) < calc.pos + sizeof(*ino))
37597 +               return 0; /* no xino */
37598 +
37599 +       sbinfo = au_sbi(sb);
37600 +       sz = xino_fread(file, ino, sizeof(*ino), &calc.pos);
37601 +       if (sz == sizeof(*ino))
37602 +               return 0; /* success */
37603 +
37604 +       err = sz;
37605 +       if (unlikely(sz >= 0)) {
37606 +               err = -EIO;
37607 +               AuIOErr("xino read error (%zd)\n", sz);
37608 +       }
37609 +       return err;
37610 +}
37611 +
37612 +static int au_xino_do_write(struct file *file, struct au_xi_calc *calc,
37613 +                           ino_t ino)
37614 +{
37615 +       ssize_t sz;
37616 +
37617 +       sz = xino_fwrite(file, &ino, sizeof(ino), &calc->pos);
37618 +       if (sz == sizeof(ino))
37619 +               return 0; /* success */
37620 +
37621 +       AuIOErr("write failed (%zd)\n", sz);
37622 +       return -EIO;
37623 +}
37624 +
37625 +/*
37626 + * write @ino to the xinofile for the specified branch{@sb, @bindex}
37627 + * at the position of @h_ino.
37628 + * even if @ino is zero, it is written to the xinofile and means no entry.
37629 + * if the size of the xino file on a specific filesystem exceeds the watermark,
37630 + * try truncating it.
37631 + */
37632 +int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
37633 +                 ino_t ino)
37634 +{
37635 +       int err;
37636 +       unsigned int mnt_flags;
37637 +       struct au_xi_calc calc;
37638 +       struct file *file;
37639 +       struct au_branch *br;
37640 +       struct au_xino *xi;
37641 +       struct au_xi_writing *p;
37642 +
37643 +       SiMustAnyLock(sb);
37644 +
37645 +       mnt_flags = au_mntflags(sb);
37646 +       if (!au_opt_test(mnt_flags, XINO))
37647 +               return 0;
37648 +
37649 +       au_xi_calc(sb, h_ino, &calc);
37650 +       br = au_sbr(sb, bindex);
37651 +       xi = br->br_xino;
37652 +       file = au_xino_file(xi, calc.idx);
37653 +       if (!file) {
37654 +               /* store the inum pair into the list */
37655 +               p = kmalloc(sizeof(*p), GFP_NOFS | __GFP_NOFAIL);
37656 +               p->h_ino = h_ino;
37657 +               p->ino = ino;
37658 +               au_hbl_add(&p->node, &xi->xi_writing);
37659 +
37660 +               /* create and write a new xino file asynchronously */
37661 +               err = au_xino_new_async(sb, br, &calc, ino);
37662 +               if (!err)
37663 +                       return 0; /* success */
37664 +               goto out;
37665 +       }
37666 +
37667 +       err = au_xino_do_write(file, &calc, ino);
37668 +       if (!err) {
37669 +               br = au_sbr(sb, bindex);
37670 +               if (au_opt_test(mnt_flags, TRUNC_XINO)
37671 +                   && au_test_fs_trunc_xino(au_br_sb(br)))
37672 +                       xino_try_trunc(sb, br);
37673 +               return 0; /* success */
37674 +       }
37675 +
37676 +out:
37677 +       AuIOErr("write failed (%d)\n", err);
37678 +       return -EIO;
37679 +}
37680 +
37681 +static ssize_t xino_fread_wkq(struct file *file, void *buf, size_t size,
37682 +                             loff_t *pos);
37683 +
37684 +/* todo: unnecessary to support mmap_sem since kernel-space? */
37685 +ssize_t xino_fread(struct file *file, void *kbuf, size_t size, loff_t *pos)
37686 +{
37687 +       ssize_t err;
37688 +       int i;
37689 +       const int prevent_endless = 10;
37690 +
37691 +       i = 0;
37692 +       do {
37693 +               err = vfsub_read_k(file, kbuf, size, pos);
37694 +               if (err == -EINTR
37695 +                   && !au_wkq_test()
37696 +                   && fatal_signal_pending(current)) {
37697 +                       err = xino_fread_wkq(file, kbuf, size, pos);
37698 +                       BUG_ON(err == -EINTR);
37699 +               }
37700 +       } while (i++ < prevent_endless
37701 +                && (err == -EAGAIN || err == -EINTR));
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 +       struct file *file;
37714 +       void *buf;
37715 +       size_t size;
37716 +       loff_t *pos;
37717 +};
37718 +
37719 +static void call_xino_fread(void *args)
37720 +{
37721 +       struct xino_fread_args *a = args;
37722 +       *a->errp = xino_fread(a->file, a->buf, a->size, a->pos);
37723 +}
37724 +
37725 +static ssize_t xino_fread_wkq(struct file *file, void *buf, size_t size,
37726 +                             loff_t *pos)
37727 +{
37728 +       ssize_t err;
37729 +       int wkq_err;
37730 +       struct xino_fread_args args = {
37731 +               .errp   = &err,
37732 +               .file   = file,
37733 +               .buf    = buf,
37734 +               .size   = size,
37735 +               .pos    = pos
37736 +       };
37737 +
37738 +       wkq_err = au_wkq_wait(call_xino_fread, &args);
37739 +       if (unlikely(wkq_err))
37740 +               err = wkq_err;
37741 +
37742 +       return err;
37743 +}
37744 +
37745 +static ssize_t xino_fwrite_wkq(struct file *file, void *buf, size_t size,
37746 +                              loff_t *pos);
37747 +
37748 +static ssize_t do_xino_fwrite(struct file *file, void *kbuf, size_t size,
37749 +                             loff_t *pos)
37750 +{
37751 +       ssize_t err;
37752 +       int i;
37753 +       const int prevent_endless = 10;
37754 +
37755 +       i = 0;
37756 +       do {
37757 +               err = vfsub_write_k(file, kbuf, size, pos);
37758 +               if (err == -EINTR
37759 +                   && !au_wkq_test()
37760 +                   && fatal_signal_pending(current)) {
37761 +                       err = xino_fwrite_wkq(file, kbuf, size, pos);
37762 +                       BUG_ON(err == -EINTR);
37763 +               }
37764 +       } while (i++ < prevent_endless
37765 +                && (err == -EAGAIN || err == -EINTR));
37766 +
37767 +#if 0 /* reserved for future use */
37768 +       if (err > 0)
37769 +               fsnotify_modify(file->f_path.dentry);
37770 +#endif
37771 +
37772 +       return err;
37773 +}
37774 +
37775 +struct do_xino_fwrite_args {
37776 +       ssize_t *errp;
37777 +       struct file *file;
37778 +       void *buf;
37779 +       size_t size;
37780 +       loff_t *pos;
37781 +};
37782 +
37783 +static void call_do_xino_fwrite(void *args)
37784 +{
37785 +       struct do_xino_fwrite_args *a = args;
37786 +       *a->errp = do_xino_fwrite(a->file, a->buf, a->size, a->pos);
37787 +}
37788 +
37789 +static ssize_t xino_fwrite_wkq(struct file *file, void *buf, size_t size,
37790 +                              loff_t *pos)
37791 +{
37792 +       ssize_t err;
37793 +       int wkq_err;
37794 +       struct do_xino_fwrite_args args = {
37795 +               .errp   = &err,
37796 +               .file   = file,
37797 +               .buf    = buf,
37798 +               .size   = size,
37799 +               .pos    = pos
37800 +       };
37801 +
37802 +       /*
37803 +        * it breaks RLIMIT_FSIZE and normal user's limit,
37804 +        * users should care about quota and real 'filesystem full.'
37805 +        */
37806 +       wkq_err = au_wkq_wait(call_do_xino_fwrite, &args);
37807 +       if (unlikely(wkq_err))
37808 +               err = wkq_err;
37809 +
37810 +       return err;
37811 +}
37812 +
37813 +ssize_t xino_fwrite(struct file *file, void *buf, size_t size, loff_t *pos)
37814 +{
37815 +       ssize_t err;
37816 +
37817 +       if (rlimit(RLIMIT_FSIZE) == RLIM_INFINITY) {
37818 +               lockdep_off();
37819 +               err = do_xino_fwrite(file, buf, size, pos);
37820 +               lockdep_on();
37821 +       } else {
37822 +               lockdep_off();
37823 +               err = xino_fwrite_wkq(file, buf, size, pos);
37824 +               lockdep_on();
37825 +       }
37826 +
37827 +       return err;
37828 +}
37829 +
37830 +/* ---------------------------------------------------------------------- */
37831 +
37832 +/*
37833 + * inode number bitmap
37834 + */
37835 +static const int page_bits = (int)PAGE_SIZE * BITS_PER_BYTE;
37836 +static ino_t xib_calc_ino(unsigned long pindex, int bit)
37837 +{
37838 +       ino_t ino;
37839 +
37840 +       AuDebugOn(bit < 0 || page_bits <= bit);
37841 +       ino = AUFS_FIRST_INO + pindex * page_bits + bit;
37842 +       return ino;
37843 +}
37844 +
37845 +static void xib_calc_bit(ino_t ino, unsigned long *pindex, int *bit)
37846 +{
37847 +       AuDebugOn(ino < AUFS_FIRST_INO);
37848 +       ino -= AUFS_FIRST_INO;
37849 +       *pindex = ino / page_bits;
37850 +       *bit = ino % page_bits;
37851 +}
37852 +
37853 +static int xib_pindex(struct super_block *sb, unsigned long pindex)
37854 +{
37855 +       int err;
37856 +       loff_t pos;
37857 +       ssize_t sz;
37858 +       struct au_sbinfo *sbinfo;
37859 +       struct file *xib;
37860 +       unsigned long *p;
37861 +
37862 +       sbinfo = au_sbi(sb);
37863 +       MtxMustLock(&sbinfo->si_xib_mtx);
37864 +       AuDebugOn(pindex > ULONG_MAX / PAGE_SIZE
37865 +                 || !au_opt_test(sbinfo->si_mntflags, XINO));
37866 +
37867 +       if (pindex == sbinfo->si_xib_last_pindex)
37868 +               return 0;
37869 +
37870 +       xib = sbinfo->si_xib;
37871 +       p = sbinfo->si_xib_buf;
37872 +       pos = sbinfo->si_xib_last_pindex;
37873 +       pos *= PAGE_SIZE;
37874 +       sz = xino_fwrite(xib, p, PAGE_SIZE, &pos);
37875 +       if (unlikely(sz != PAGE_SIZE))
37876 +               goto out;
37877 +
37878 +       pos = pindex;
37879 +       pos *= PAGE_SIZE;
37880 +       if (vfsub_f_size_read(xib) >= pos + PAGE_SIZE)
37881 +               sz = xino_fread(xib, p, PAGE_SIZE, &pos);
37882 +       else {
37883 +               memset(p, 0, PAGE_SIZE);
37884 +               sz = xino_fwrite(xib, p, PAGE_SIZE, &pos);
37885 +       }
37886 +       if (sz == PAGE_SIZE) {
37887 +               sbinfo->si_xib_last_pindex = pindex;
37888 +               return 0; /* success */
37889 +       }
37890 +
37891 +out:
37892 +       AuIOErr1("write failed (%zd)\n", sz);
37893 +       err = sz;
37894 +       if (sz >= 0)
37895 +               err = -EIO;
37896 +       return err;
37897 +}
37898 +
37899 +static void au_xib_clear_bit(struct inode *inode)
37900 +{
37901 +       int err, bit;
37902 +       unsigned long pindex;
37903 +       struct super_block *sb;
37904 +       struct au_sbinfo *sbinfo;
37905 +
37906 +       AuDebugOn(inode->i_nlink);
37907 +
37908 +       sb = inode->i_sb;
37909 +       xib_calc_bit(inode->i_ino, &pindex, &bit);
37910 +       AuDebugOn(page_bits <= bit);
37911 +       sbinfo = au_sbi(sb);
37912 +       mutex_lock(&sbinfo->si_xib_mtx);
37913 +       err = xib_pindex(sb, pindex);
37914 +       if (!err) {
37915 +               clear_bit(bit, sbinfo->si_xib_buf);
37916 +               sbinfo->si_xib_next_bit = bit;
37917 +       }
37918 +       mutex_unlock(&sbinfo->si_xib_mtx);
37919 +}
37920 +
37921 +/* ---------------------------------------------------------------------- */
37922 +
37923 +/*
37924 + * truncate a xino bitmap file
37925 + */
37926 +
37927 +/* todo: slow */
37928 +static int do_xib_restore(struct super_block *sb, struct file *file, void *page)
37929 +{
37930 +       int err, bit;
37931 +       ssize_t sz;
37932 +       unsigned long pindex;
37933 +       loff_t pos, pend;
37934 +       struct au_sbinfo *sbinfo;
37935 +       ino_t *ino;
37936 +       unsigned long *p;
37937 +
37938 +       err = 0;
37939 +       sbinfo = au_sbi(sb);
37940 +       MtxMustLock(&sbinfo->si_xib_mtx);
37941 +       p = sbinfo->si_xib_buf;
37942 +       pend = vfsub_f_size_read(file);
37943 +       pos = 0;
37944 +       while (pos < pend) {
37945 +               sz = xino_fread(file, page, PAGE_SIZE, &pos);
37946 +               err = sz;
37947 +               if (unlikely(sz <= 0))
37948 +                       goto out;
37949 +
37950 +               err = 0;
37951 +               for (ino = page; sz > 0; ino++, sz -= sizeof(ino)) {
37952 +                       if (unlikely(*ino < AUFS_FIRST_INO))
37953 +                               continue;
37954 +
37955 +                       xib_calc_bit(*ino, &pindex, &bit);
37956 +                       AuDebugOn(page_bits <= bit);
37957 +                       err = xib_pindex(sb, pindex);
37958 +                       if (!err)
37959 +                               set_bit(bit, p);
37960 +                       else
37961 +                               goto out;
37962 +               }
37963 +       }
37964 +
37965 +out:
37966 +       return err;
37967 +}
37968 +
37969 +static int xib_restore(struct super_block *sb)
37970 +{
37971 +       int err, i;
37972 +       unsigned int nfile;
37973 +       aufs_bindex_t bindex, bbot;
37974 +       void *page;
37975 +       struct au_branch *br;
37976 +       struct au_xino *xi;
37977 +       struct file *file;
37978 +
37979 +       err = -ENOMEM;
37980 +       page = (void *)__get_free_page(GFP_NOFS);
37981 +       if (unlikely(!page))
37982 +               goto out;
37983 +
37984 +       err = 0;
37985 +       bbot = au_sbbot(sb);
37986 +       for (bindex = 0; !err && bindex <= bbot; bindex++)
37987 +               if (!bindex || is_sb_shared(sb, bindex, bindex - 1) < 0) {
37988 +                       br = au_sbr(sb, bindex);
37989 +                       xi = br->br_xino;
37990 +                       nfile = xi->xi_nfile;
37991 +                       for (i = 0; i < nfile; i++) {
37992 +                               file = au_xino_file(xi, i);
37993 +                               if (file)
37994 +                                       err = do_xib_restore(sb, file, page);
37995 +                       }
37996 +               } else
37997 +                       AuDbg("skip shared b%d\n", bindex);
37998 +       free_page((unsigned long)page);
37999 +
38000 +out:
38001 +       return err;
38002 +}
38003 +
38004 +int au_xib_trunc(struct super_block *sb)
38005 +{
38006 +       int err;
38007 +       ssize_t sz;
38008 +       loff_t pos;
38009 +       struct au_sbinfo *sbinfo;
38010 +       unsigned long *p;
38011 +       struct file *file;
38012 +
38013 +       SiMustWriteLock(sb);
38014 +
38015 +       err = 0;
38016 +       sbinfo = au_sbi(sb);
38017 +       if (!au_opt_test(sbinfo->si_mntflags, XINO))
38018 +               goto out;
38019 +
38020 +       file = sbinfo->si_xib;
38021 +       if (vfsub_f_size_read(file) <= PAGE_SIZE)
38022 +               goto out;
38023 +
38024 +       file = au_xino_create2(sb, &sbinfo->si_xib->f_path, NULL);
38025 +       err = PTR_ERR(file);
38026 +       if (IS_ERR(file))
38027 +               goto out;
38028 +       fput(sbinfo->si_xib);
38029 +       sbinfo->si_xib = file;
38030 +
38031 +       p = sbinfo->si_xib_buf;
38032 +       memset(p, 0, PAGE_SIZE);
38033 +       pos = 0;
38034 +       sz = xino_fwrite(sbinfo->si_xib, p, PAGE_SIZE, &pos);
38035 +       if (unlikely(sz != PAGE_SIZE)) {
38036 +               err = sz;
38037 +               AuIOErr("err %d\n", err);
38038 +               if (sz >= 0)
38039 +                       err = -EIO;
38040 +               goto out;
38041 +       }
38042 +
38043 +       mutex_lock(&sbinfo->si_xib_mtx);
38044 +       /* mnt_want_write() is unnecessary here */
38045 +       err = xib_restore(sb);
38046 +       mutex_unlock(&sbinfo->si_xib_mtx);
38047 +
38048 +out:
38049 +       return err;
38050 +}
38051 +
38052 +/* ---------------------------------------------------------------------- */
38053 +
38054 +struct au_xino *au_xino_alloc(unsigned int nfile)
38055 +{
38056 +       struct au_xino *xi;
38057 +
38058 +       xi = kzalloc(sizeof(*xi), GFP_NOFS);
38059 +       if (unlikely(!xi))
38060 +               goto out;
38061 +       xi->xi_nfile = nfile;
38062 +       xi->xi_file = kcalloc(nfile, sizeof(*xi->xi_file), GFP_NOFS);
38063 +       if (unlikely(!xi->xi_file))
38064 +               goto out_free;
38065 +
38066 +       xi->xi_nondir.total = 8; /* initial size */
38067 +       xi->xi_nondir.array = kcalloc(xi->xi_nondir.total, sizeof(ino_t),
38068 +                                     GFP_NOFS);
38069 +       if (unlikely(!xi->xi_nondir.array))
38070 +               goto out_file;
38071 +
38072 +       spin_lock_init(&xi->xi_nondir.spin);
38073 +       init_waitqueue_head(&xi->xi_nondir.wqh);
38074 +       mutex_init(&xi->xi_mtx);
38075 +       INIT_HLIST_BL_HEAD(&xi->xi_writing);
38076 +       atomic_set(&xi->xi_truncating, 0);
38077 +       kref_init(&xi->xi_kref);
38078 +       goto out; /* success */
38079 +
38080 +out_file:
38081 +       au_kfree_try_rcu(xi->xi_file);
38082 +out_free:
38083 +       au_kfree_rcu(xi);
38084 +       xi = NULL;
38085 +out:
38086 +       return xi;
38087 +}
38088 +
38089 +static int au_xino_init(struct au_branch *br, int idx, struct file *file)
38090 +{
38091 +       int err;
38092 +       struct au_xino *xi;
38093 +
38094 +       err = 0;
38095 +       xi = au_xino_alloc(idx + 1);
38096 +       if (unlikely(!xi)) {
38097 +               err = -ENOMEM;
38098 +               goto out;
38099 +       }
38100 +
38101 +       if (file)
38102 +               get_file(file);
38103 +       xi->xi_file[idx] = file;
38104 +       AuDebugOn(br->br_xino);
38105 +       br->br_xino = xi;
38106 +
38107 +out:
38108 +       return err;
38109 +}
38110 +
38111 +static void au_xino_release(struct kref *kref)
38112 +{
38113 +       struct au_xino *xi;
38114 +       int i;
38115 +       unsigned long ul;
38116 +       struct hlist_bl_head *hbl;
38117 +       struct hlist_bl_node *pos, *n;
38118 +       struct au_xi_writing *p;
38119 +
38120 +       xi = container_of(kref, struct au_xino, xi_kref);
38121 +       for (i = 0; i < xi->xi_nfile; i++)
38122 +               if (xi->xi_file[i])
38123 +                       fput(xi->xi_file[i]);
38124 +       for (i = xi->xi_nondir.total - 1; i >= 0; i--)
38125 +               AuDebugOn(xi->xi_nondir.array[i]);
38126 +       mutex_destroy(&xi->xi_mtx);
38127 +       hbl = &xi->xi_writing;
38128 +       ul = au_hbl_count(hbl);
38129 +       if (unlikely(ul)) {
38130 +               pr_warn("xi_writing %lu\n", ul);
38131 +               hlist_bl_lock(hbl);
38132 +               hlist_bl_for_each_entry_safe(p, pos, n, hbl, node) {
38133 +                       hlist_bl_del(&p->node);
38134 +                       /* kmemleak reported au_kfree_rcu() doesn't free it */
38135 +                       kfree(p);
38136 +               }
38137 +               hlist_bl_unlock(hbl);
38138 +       }
38139 +       au_kfree_try_rcu(xi->xi_file);
38140 +       au_kfree_try_rcu(xi->xi_nondir.array);
38141 +       au_kfree_rcu(xi);
38142 +}
38143 +
38144 +int au_xino_put(struct au_branch *br)
38145 +{
38146 +       int ret;
38147 +       struct au_xino *xi;
38148 +
38149 +       ret = 0;
38150 +       xi = br->br_xino;
38151 +       if (xi) {
38152 +               br->br_xino = NULL;
38153 +               ret = kref_put(&xi->xi_kref, au_xino_release);
38154 +       }
38155 +
38156 +       return ret;
38157 +}
38158 +
38159 +/* ---------------------------------------------------------------------- */
38160 +
38161 +/*
38162 + * xino mount option handlers
38163 + */
38164 +
38165 +/* xino bitmap */
38166 +static void xino_clear_xib(struct super_block *sb)
38167 +{
38168 +       struct au_sbinfo *sbinfo;
38169 +
38170 +       SiMustWriteLock(sb);
38171 +
38172 +       sbinfo = au_sbi(sb);
38173 +       if (sbinfo->si_xib)
38174 +               fput(sbinfo->si_xib);
38175 +       sbinfo->si_xib = NULL;
38176 +       if (sbinfo->si_xib_buf)
38177 +               free_page((unsigned long)sbinfo->si_xib_buf);
38178 +       sbinfo->si_xib_buf = NULL;
38179 +}
38180 +
38181 +static int au_xino_set_xib(struct super_block *sb, struct path *path)
38182 +{
38183 +       int err;
38184 +       loff_t pos;
38185 +       struct au_sbinfo *sbinfo;
38186 +       struct file *file;
38187 +       struct super_block *xi_sb;
38188 +
38189 +       SiMustWriteLock(sb);
38190 +
38191 +       sbinfo = au_sbi(sb);
38192 +       file = au_xino_create2(sb, path, sbinfo->si_xib);
38193 +       err = PTR_ERR(file);
38194 +       if (IS_ERR(file))
38195 +               goto out;
38196 +       if (sbinfo->si_xib)
38197 +               fput(sbinfo->si_xib);
38198 +       sbinfo->si_xib = file;
38199 +       xi_sb = file_inode(file)->i_sb;
38200 +       sbinfo->si_ximaxent = xi_sb->s_maxbytes;
38201 +       if (unlikely(sbinfo->si_ximaxent < PAGE_SIZE)) {
38202 +               err = -EIO;
38203 +               pr_err("s_maxbytes(%llu) on %s is too small\n",
38204 +                      (u64)sbinfo->si_ximaxent, au_sbtype(xi_sb));
38205 +               goto out_unset;
38206 +       }
38207 +       sbinfo->si_ximaxent /= sizeof(ino_t);
38208 +
38209 +       err = -ENOMEM;
38210 +       if (!sbinfo->si_xib_buf)
38211 +               sbinfo->si_xib_buf = (void *)get_zeroed_page(GFP_NOFS);
38212 +       if (unlikely(!sbinfo->si_xib_buf))
38213 +               goto out_unset;
38214 +
38215 +       sbinfo->si_xib_last_pindex = 0;
38216 +       sbinfo->si_xib_next_bit = 0;
38217 +       if (vfsub_f_size_read(file) < PAGE_SIZE) {
38218 +               pos = 0;
38219 +               err = xino_fwrite(file, sbinfo->si_xib_buf, PAGE_SIZE, &pos);
38220 +               if (unlikely(err != PAGE_SIZE))
38221 +                       goto out_free;
38222 +       }
38223 +       err = 0;
38224 +       goto out; /* success */
38225 +
38226 +out_free:
38227 +       if (sbinfo->si_xib_buf)
38228 +               free_page((unsigned long)sbinfo->si_xib_buf);
38229 +       sbinfo->si_xib_buf = NULL;
38230 +       if (err >= 0)
38231 +               err = -EIO;
38232 +out_unset:
38233 +       fput(sbinfo->si_xib);
38234 +       sbinfo->si_xib = NULL;
38235 +out:
38236 +       AuTraceErr(err);
38237 +       return err;
38238 +}
38239 +
38240 +/* xino for each branch */
38241 +static void xino_clear_br(struct super_block *sb)
38242 +{
38243 +       aufs_bindex_t bindex, bbot;
38244 +       struct au_branch *br;
38245 +
38246 +       bbot = au_sbbot(sb);
38247 +       for (bindex = 0; bindex <= bbot; bindex++) {
38248 +               br = au_sbr(sb, bindex);
38249 +               AuDebugOn(!br);
38250 +               au_xino_put(br);
38251 +       }
38252 +}
38253 +
38254 +static void au_xino_set_br_shared(struct super_block *sb, struct au_branch *br,
38255 +                                 aufs_bindex_t bshared)
38256 +{
38257 +       struct au_branch *brshared;
38258 +
38259 +       brshared = au_sbr(sb, bshared);
38260 +       AuDebugOn(!brshared->br_xino);
38261 +       AuDebugOn(!brshared->br_xino->xi_file);
38262 +       if (br->br_xino != brshared->br_xino) {
38263 +               au_xino_get(brshared);
38264 +               au_xino_put(br);
38265 +               br->br_xino = brshared->br_xino;
38266 +       }
38267 +}
38268 +
38269 +struct au_xino_do_set_br {
38270 +       struct au_branch *br;
38271 +       ino_t h_ino;
38272 +       aufs_bindex_t bshared;
38273 +};
38274 +
38275 +static int au_xino_do_set_br(struct super_block *sb, struct path *path,
38276 +                            struct au_xino_do_set_br *args)
38277 +{
38278 +       int err;
38279 +       struct au_xi_calc calc;
38280 +       struct file *file;
38281 +       struct au_branch *br;
38282 +       struct au_xi_new xinew = {
38283 +               .base = path
38284 +       };
38285 +
38286 +       br = args->br;
38287 +       xinew.xi = br->br_xino;
38288 +       au_xi_calc(sb, args->h_ino, &calc);
38289 +       xinew.copy_src = au_xino_file(xinew.xi, calc.idx);
38290 +       if (args->bshared >= 0)
38291 +               /* shared xino */
38292 +               au_xino_set_br_shared(sb, br, args->bshared);
38293 +       else if (!xinew.xi) {
38294 +               /* new xino */
38295 +               err = au_xino_init(br, calc.idx, xinew.copy_src);
38296 +               if (unlikely(err))
38297 +                       goto out;
38298 +       }
38299 +
38300 +       /* force re-creating */
38301 +       xinew.xi = br->br_xino;
38302 +       xinew.idx = calc.idx;
38303 +       mutex_lock(&xinew.xi->xi_mtx);
38304 +       file = au_xi_new(sb, &xinew);
38305 +       mutex_unlock(&xinew.xi->xi_mtx);
38306 +       err = PTR_ERR(file);
38307 +       if (IS_ERR(file))
38308 +               goto out;
38309 +       AuDebugOn(!file);
38310 +
38311 +       err = au_xino_do_write(file, &calc, AUFS_ROOT_INO);
38312 +       if (unlikely(err))
38313 +               au_xino_put(br);
38314 +
38315 +out:
38316 +       AuTraceErr(err);
38317 +       return err;
38318 +}
38319 +
38320 +static int au_xino_set_br(struct super_block *sb, struct path *path)
38321 +{
38322 +       int err;
38323 +       aufs_bindex_t bindex, bbot;
38324 +       struct au_xino_do_set_br args;
38325 +       struct inode *inode;
38326 +
38327 +       SiMustWriteLock(sb);
38328 +
38329 +       bbot = au_sbbot(sb);
38330 +       inode = d_inode(sb->s_root);
38331 +       for (bindex = 0; bindex <= bbot; bindex++) {
38332 +               args.h_ino = au_h_iptr(inode, bindex)->i_ino;
38333 +               args.br = au_sbr(sb, bindex);
38334 +               args.bshared = is_sb_shared(sb, bindex, bindex - 1);
38335 +               err = au_xino_do_set_br(sb, path, &args);
38336 +               if (unlikely(err))
38337 +                       break;
38338 +       }
38339 +
38340 +       AuTraceErr(err);
38341 +       return err;
38342 +}
38343 +
38344 +void au_xino_clr(struct super_block *sb)
38345 +{
38346 +       struct au_sbinfo *sbinfo;
38347 +
38348 +       au_xigen_clr(sb);
38349 +       xino_clear_xib(sb);
38350 +       xino_clear_br(sb);
38351 +       dbgaufs_brs_del(sb, 0);
38352 +       sbinfo = au_sbi(sb);
38353 +       /* lvalue, do not call au_mntflags() */
38354 +       au_opt_clr(sbinfo->si_mntflags, XINO);
38355 +}
38356 +
38357 +int au_xino_set(struct super_block *sb, struct au_opt_xino *xiopt, int remount)
38358 +{
38359 +       int err, skip;
38360 +       struct dentry *dentry, *parent, *cur_dentry, *cur_parent;
38361 +       struct qstr *dname, *cur_name;
38362 +       struct file *cur_xino;
38363 +       struct au_sbinfo *sbinfo;
38364 +       struct path *path, *cur_path;
38365 +
38366 +       SiMustWriteLock(sb);
38367 +
38368 +       err = 0;
38369 +       sbinfo = au_sbi(sb);
38370 +       path = &xiopt->file->f_path;
38371 +       dentry = path->dentry;
38372 +       parent = dget_parent(dentry);
38373 +       if (remount) {
38374 +               skip = 0;
38375 +               cur_xino = sbinfo->si_xib;
38376 +               if (cur_xino) {
38377 +                       cur_path = &cur_xino->f_path;
38378 +                       cur_dentry = cur_path->dentry;
38379 +                       cur_parent = dget_parent(cur_dentry);
38380 +                       cur_name = &cur_dentry->d_name;
38381 +                       dname = &dentry->d_name;
38382 +                       skip = (cur_parent == parent
38383 +                               && au_qstreq(dname, cur_name));
38384 +                       dput(cur_parent);
38385 +               }
38386 +               if (skip)
38387 +                       goto out;
38388 +       }
38389 +
38390 +       au_opt_set(sbinfo->si_mntflags, XINO);
38391 +       err = au_xino_set_xib(sb, path);
38392 +       /* si_x{read,write} are set */
38393 +       if (!err)
38394 +               err = au_xigen_set(sb, path);
38395 +       if (!err)
38396 +               err = au_xino_set_br(sb, path);
38397 +       if (!err) {
38398 +               dbgaufs_brs_add(sb, 0, /*topdown*/1);
38399 +               goto out; /* success */
38400 +       }
38401 +
38402 +       /* reset all */
38403 +       AuIOErr("failed setting xino(%d).\n", err);
38404 +       au_xino_clr(sb);
38405 +
38406 +out:
38407 +       dput(parent);
38408 +       return err;
38409 +}
38410 +
38411 +/*
38412 + * create a xinofile at the default place/path.
38413 + */
38414 +struct file *au_xino_def(struct super_block *sb)
38415 +{
38416 +       struct file *file;
38417 +       char *page, *p;
38418 +       struct au_branch *br;
38419 +       struct super_block *h_sb;
38420 +       struct path path;
38421 +       aufs_bindex_t bbot, bindex, bwr;
38422 +
38423 +       br = NULL;
38424 +       bbot = au_sbbot(sb);
38425 +       bwr = -1;
38426 +       for (bindex = 0; bindex <= bbot; bindex++) {
38427 +               br = au_sbr(sb, bindex);
38428 +               if (au_br_writable(br->br_perm)
38429 +                   && !au_test_fs_bad_xino(au_br_sb(br))) {
38430 +                       bwr = bindex;
38431 +                       break;
38432 +               }
38433 +       }
38434 +
38435 +       if (bwr >= 0) {
38436 +               file = ERR_PTR(-ENOMEM);
38437 +               page = (void *)__get_free_page(GFP_NOFS);
38438 +               if (unlikely(!page))
38439 +                       goto out;
38440 +               path.mnt = au_br_mnt(br);
38441 +               path.dentry = au_h_dptr(sb->s_root, bwr);
38442 +               p = d_path(&path, page, PATH_MAX - sizeof(AUFS_XINO_FNAME));
38443 +               file = (void *)p;
38444 +               if (!IS_ERR(p)) {
38445 +                       strcat(p, "/" AUFS_XINO_FNAME);
38446 +                       AuDbg("%s\n", p);
38447 +                       file = au_xino_create(sb, p, /*silent*/0, /*wbrtop*/1);
38448 +               }
38449 +               free_page((unsigned long)page);
38450 +       } else {
38451 +               file = au_xino_create(sb, AUFS_XINO_DEFPATH, /*silent*/0,
38452 +                                     /*wbrtop*/0);
38453 +               if (IS_ERR(file))
38454 +                       goto out;
38455 +               h_sb = file->f_path.dentry->d_sb;
38456 +               if (unlikely(au_test_fs_bad_xino(h_sb))) {
38457 +                       pr_err("xino doesn't support %s(%s)\n",
38458 +                              AUFS_XINO_DEFPATH, au_sbtype(h_sb));
38459 +                       fput(file);
38460 +                       file = ERR_PTR(-EINVAL);
38461 +               }
38462 +       }
38463 +
38464 +out:
38465 +       return file;
38466 +}
38467 +
38468 +/* ---------------------------------------------------------------------- */
38469 +
38470 +/*
38471 + * initialize the xinofile for the specified branch @br
38472 + * at the place/path where @base_file indicates.
38473 + * test whether another branch is on the same filesystem or not,
38474 + * if found then share the xinofile with another branch.
38475 + */
38476 +int au_xino_init_br(struct super_block *sb, struct au_branch *br, ino_t h_ino,
38477 +                   struct path *base)
38478 +{
38479 +       int err;
38480 +       struct au_xino_do_set_br args = {
38481 +               .h_ino  = h_ino,
38482 +               .br     = br
38483 +       };
38484 +
38485 +       args.bshared = sbr_find_shared(sb, /*btop*/0, au_sbbot(sb),
38486 +                                      au_br_sb(br));
38487 +       err = au_xino_do_set_br(sb, base, &args);
38488 +       if (unlikely(err))
38489 +               au_xino_put(br);
38490 +
38491 +       return err;
38492 +}
38493 +
38494 +/* ---------------------------------------------------------------------- */
38495 +
38496 +/*
38497 + * get an unused inode number from bitmap
38498 + */
38499 +ino_t au_xino_new_ino(struct super_block *sb)
38500 +{
38501 +       ino_t ino;
38502 +       unsigned long *p, pindex, ul, pend;
38503 +       struct au_sbinfo *sbinfo;
38504 +       struct file *file;
38505 +       int free_bit, err;
38506 +
38507 +       if (!au_opt_test(au_mntflags(sb), XINO))
38508 +               return iunique(sb, AUFS_FIRST_INO);
38509 +
38510 +       sbinfo = au_sbi(sb);
38511 +       mutex_lock(&sbinfo->si_xib_mtx);
38512 +       p = sbinfo->si_xib_buf;
38513 +       free_bit = sbinfo->si_xib_next_bit;
38514 +       if (free_bit < page_bits && !test_bit(free_bit, p))
38515 +               goto out; /* success */
38516 +       free_bit = find_first_zero_bit(p, page_bits);
38517 +       if (free_bit < page_bits)
38518 +               goto out; /* success */
38519 +
38520 +       pindex = sbinfo->si_xib_last_pindex;
38521 +       for (ul = pindex - 1; ul < ULONG_MAX; ul--) {
38522 +               err = xib_pindex(sb, ul);
38523 +               if (unlikely(err))
38524 +                       goto out_err;
38525 +               free_bit = find_first_zero_bit(p, page_bits);
38526 +               if (free_bit < page_bits)
38527 +                       goto out; /* success */
38528 +       }
38529 +
38530 +       file = sbinfo->si_xib;
38531 +       pend = vfsub_f_size_read(file) / PAGE_SIZE;
38532 +       for (ul = pindex + 1; ul <= pend; ul++) {
38533 +               err = xib_pindex(sb, ul);
38534 +               if (unlikely(err))
38535 +                       goto out_err;
38536 +               free_bit = find_first_zero_bit(p, page_bits);
38537 +               if (free_bit < page_bits)
38538 +                       goto out; /* success */
38539 +       }
38540 +       BUG();
38541 +
38542 +out:
38543 +       set_bit(free_bit, p);
38544 +       sbinfo->si_xib_next_bit = free_bit + 1;
38545 +       pindex = sbinfo->si_xib_last_pindex;
38546 +       mutex_unlock(&sbinfo->si_xib_mtx);
38547 +       ino = xib_calc_ino(pindex, free_bit);
38548 +       AuDbg("i%lu\n", (unsigned long)ino);
38549 +       return ino;
38550 +out_err:
38551 +       mutex_unlock(&sbinfo->si_xib_mtx);
38552 +       AuDbg("i0\n");
38553 +       return 0;
38554 +}
38555 +
38556 +/* for s_op->delete_inode() */
38557 +void au_xino_delete_inode(struct inode *inode, const int unlinked)
38558 +{
38559 +       int err;
38560 +       unsigned int mnt_flags;
38561 +       aufs_bindex_t bindex, bbot, bi;
38562 +       unsigned char try_trunc;
38563 +       struct au_iinfo *iinfo;
38564 +       struct super_block *sb;
38565 +       struct au_hinode *hi;
38566 +       struct inode *h_inode;
38567 +       struct au_branch *br;
38568 +       struct au_xi_calc calc;
38569 +       struct file *file;
38570 +
38571 +       AuDebugOn(au_is_bad_inode(inode));
38572 +
38573 +       sb = inode->i_sb;
38574 +       mnt_flags = au_mntflags(sb);
38575 +       if (!au_opt_test(mnt_flags, XINO)
38576 +           || inode->i_ino == AUFS_ROOT_INO)
38577 +               return;
38578 +
38579 +       if (unlinked) {
38580 +               au_xigen_inc(inode);
38581 +               au_xib_clear_bit(inode);
38582 +       }
38583 +
38584 +       iinfo = au_ii(inode);
38585 +       bindex = iinfo->ii_btop;
38586 +       if (bindex < 0)
38587 +               return;
38588 +
38589 +       try_trunc = !!au_opt_test(mnt_flags, TRUNC_XINO);
38590 +       hi = au_hinode(iinfo, bindex);
38591 +       bbot = iinfo->ii_bbot;
38592 +       for (; bindex <= bbot; bindex++, hi++) {
38593 +               h_inode = hi->hi_inode;
38594 +               if (!h_inode
38595 +                   || (!unlinked && h_inode->i_nlink))
38596 +                       continue;
38597 +
38598 +               /* inode may not be revalidated */
38599 +               bi = au_br_index(sb, hi->hi_id);
38600 +               if (bi < 0)
38601 +                       continue;
38602 +
38603 +               br = au_sbr(sb, bi);
38604 +               au_xi_calc(sb, h_inode->i_ino, &calc);
38605 +               file = au_xino_file(br->br_xino, calc.idx);
38606 +               if (IS_ERR_OR_NULL(file))
38607 +                       continue;
38608 +
38609 +               err = au_xino_do_write(file, &calc, /*ino*/0);
38610 +               if (!err && try_trunc
38611 +                   && au_test_fs_trunc_xino(au_br_sb(br)))
38612 +                       xino_try_trunc(sb, br);
38613 +       }
38614 +}
38615 +
38616 +/* ---------------------------------------------------------------------- */
38617 +
38618 +static int au_xinondir_find(struct au_xino *xi, ino_t h_ino)
38619 +{
38620 +       int found, total, i;
38621 +
38622 +       found = -1;
38623 +       total = xi->xi_nondir.total;
38624 +       for (i = 0; i < total; i++) {
38625 +               if (xi->xi_nondir.array[i] != h_ino)
38626 +                       continue;
38627 +               found = i;
38628 +               break;
38629 +       }
38630 +
38631 +       return found;
38632 +}
38633 +
38634 +static int au_xinondir_expand(struct au_xino *xi)
38635 +{
38636 +       int err, sz;
38637 +       ino_t *p;
38638 +
38639 +       BUILD_BUG_ON(KMALLOC_MAX_SIZE > INT_MAX);
38640 +
38641 +       err = -ENOMEM;
38642 +       sz = xi->xi_nondir.total * sizeof(ino_t);
38643 +       if (unlikely(sz > KMALLOC_MAX_SIZE / 2))
38644 +               goto out;
38645 +       p = au_kzrealloc(xi->xi_nondir.array, sz, sz << 1, GFP_ATOMIC,
38646 +                        /*may_shrink*/0);
38647 +       if (p) {
38648 +               xi->xi_nondir.array = p;
38649 +               xi->xi_nondir.total <<= 1;
38650 +               AuDbg("xi_nondir.total %d\n", xi->xi_nondir.total);
38651 +               err = 0;
38652 +       }
38653 +
38654 +out:
38655 +       return err;
38656 +}
38657 +
38658 +void au_xinondir_leave(struct super_block *sb, aufs_bindex_t bindex,
38659 +                      ino_t h_ino, int idx)
38660 +{
38661 +       struct au_xino *xi;
38662 +
38663 +       AuDebugOn(!au_opt_test(au_mntflags(sb), XINO));
38664 +       xi = au_sbr(sb, bindex)->br_xino;
38665 +       AuDebugOn(idx < 0 || xi->xi_nondir.total <= idx);
38666 +
38667 +       spin_lock(&xi->xi_nondir.spin);
38668 +       AuDebugOn(xi->xi_nondir.array[idx] != h_ino);
38669 +       xi->xi_nondir.array[idx] = 0;
38670 +       spin_unlock(&xi->xi_nondir.spin);
38671 +       wake_up_all(&xi->xi_nondir.wqh);
38672 +}
38673 +
38674 +int au_xinondir_enter(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
38675 +                     int *idx)
38676 +{
38677 +       int err, found, empty;
38678 +       struct au_xino *xi;
38679 +
38680 +       err = 0;
38681 +       *idx = -1;
38682 +       if (!au_opt_test(au_mntflags(sb), XINO))
38683 +               goto out; /* no xino */
38684 +
38685 +       xi = au_sbr(sb, bindex)->br_xino;
38686 +
38687 +again:
38688 +       spin_lock(&xi->xi_nondir.spin);
38689 +       found = au_xinondir_find(xi, h_ino);
38690 +       if (found == -1) {
38691 +               empty = au_xinondir_find(xi, /*h_ino*/0);
38692 +               if (empty == -1) {
38693 +                       empty = xi->xi_nondir.total;
38694 +                       err = au_xinondir_expand(xi);
38695 +                       if (unlikely(err))
38696 +                               goto out_unlock;
38697 +               }
38698 +               xi->xi_nondir.array[empty] = h_ino;
38699 +               *idx = empty;
38700 +       } else {
38701 +               spin_unlock(&xi->xi_nondir.spin);
38702 +               wait_event(xi->xi_nondir.wqh,
38703 +                          xi->xi_nondir.array[found] != h_ino);
38704 +               goto again;
38705 +       }
38706 +
38707 +out_unlock:
38708 +       spin_unlock(&xi->xi_nondir.spin);
38709 +out:
38710 +       return err;
38711 +}
38712 +
38713 +/* ---------------------------------------------------------------------- */
38714 +
38715 +int au_xino_path(struct seq_file *seq, struct file *file)
38716 +{
38717 +       int err;
38718 +
38719 +       err = au_seq_path(seq, &file->f_path);
38720 +       if (unlikely(err))
38721 +               goto out;
38722 +
38723 +#define Deleted "\\040(deleted)"
38724 +       seq->count -= sizeof(Deleted) - 1;
38725 +       AuDebugOn(memcmp(seq->buf + seq->count, Deleted,
38726 +                        sizeof(Deleted) - 1));
38727 +#undef Deleted
38728 +
38729 +out:
38730 +       return err;
38731 +}
38732 diff -urN /usr/share/empty/include/uapi/linux/aufs_type.h linux/include/uapi/linux/aufs_type.h
38733 --- /usr/share/empty/include/uapi/linux/aufs_type.h     1970-01-01 01:00:00.000000000 +0100
38734 +++ linux/include/uapi/linux/aufs_type.h        2021-11-01 23:48:34.213025928 +0100
38735 @@ -0,0 +1,452 @@
38736 +/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
38737 +/*
38738 + * Copyright (C) 2005-2021 Junjiro R. Okajima
38739 + *
38740 + * This program, aufs is free software; you can redistribute it and/or modify
38741 + * it under the terms of the GNU General Public License as published by
38742 + * the Free Software Foundation; either version 2 of the License, or
38743 + * (at your option) any later version.
38744 + *
38745 + * This program is distributed in the hope that it will be useful,
38746 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
38747 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
38748 + * GNU General Public License for more details.
38749 + *
38750 + * You should have received a copy of the GNU General Public License
38751 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
38752 + */
38753 +
38754 +#ifndef __AUFS_TYPE_H__
38755 +#define __AUFS_TYPE_H__
38756 +
38757 +#define AUFS_NAME      "aufs"
38758 +
38759 +#ifdef __KERNEL__
38760 +/*
38761 + * define it before including all other headers.
38762 + * sched.h may use pr_* macros before defining "current", so define the
38763 + * no-current version first, and re-define later.
38764 + */
38765 +#define pr_fmt(fmt)    AUFS_NAME " %s:%d: " fmt, __func__, __LINE__
38766 +#include <linux/sched.h>
38767 +#undef pr_fmt
38768 +#define pr_fmt(fmt) \
38769 +               AUFS_NAME " %s:%d:%.*s[%d]: " fmt, __func__, __LINE__, \
38770 +               (int)sizeof(current->comm), current->comm, current->pid
38771 +#include <linux/limits.h>
38772 +#else
38773 +#include <stdint.h>
38774 +#include <sys/types.h>
38775 +#include <limits.h>
38776 +#endif /* __KERNEL__ */
38777 +
38778 +#define AUFS_VERSION   "5.x-rcN-20211018"
38779 +
38780 +/* todo? move this to linux-2.6.19/include/magic.h */
38781 +#define AUFS_SUPER_MAGIC       ('a' << 24 | 'u' << 16 | 'f' << 8 | 's')
38782 +
38783 +/* ---------------------------------------------------------------------- */
38784 +
38785 +#ifdef __KERNEL__
38786 +#ifdef CONFIG_AUFS_BRANCH_MAX_127
38787 +typedef int8_t aufs_bindex_t;
38788 +#define AUFS_BRANCH_MAX 127
38789 +#else
38790 +typedef int16_t aufs_bindex_t;
38791 +#ifdef CONFIG_AUFS_BRANCH_MAX_511
38792 +#define AUFS_BRANCH_MAX 511
38793 +#elif defined(CONFIG_AUFS_BRANCH_MAX_1023)
38794 +#define AUFS_BRANCH_MAX 1023
38795 +#elif defined(CONFIG_AUFS_BRANCH_MAX_32767)
38796 +#define AUFS_BRANCH_MAX 32767
38797 +#endif
38798 +#endif
38799 +
38800 +#ifndef AUFS_BRANCH_MAX
38801 +#error unknown CONFIG_AUFS_BRANCH_MAX value
38802 +#endif
38803 +#endif /* __KERNEL__ */
38804 +
38805 +/* ---------------------------------------------------------------------- */
38806 +
38807 +#define AUFS_FSTYPE            AUFS_NAME
38808 +
38809 +#define AUFS_ROOT_INO          2
38810 +#define AUFS_FIRST_INO         11
38811 +
38812 +#define AUFS_WH_PFX            ".wh."
38813 +#define AUFS_WH_PFX_LEN                ((int)sizeof(AUFS_WH_PFX) - 1)
38814 +#define AUFS_WH_TMP_LEN                4
38815 +/* a limit for rmdir/rename a dir and copyup */
38816 +#define AUFS_MAX_NAMELEN       (NAME_MAX \
38817 +                               - AUFS_WH_PFX_LEN * 2   /* doubly whiteouted */\
38818 +                               - 1                     /* dot */\
38819 +                               - AUFS_WH_TMP_LEN)      /* hex */
38820 +#define AUFS_XINO_FNAME                "." AUFS_NAME ".xino"
38821 +#define AUFS_XINO_DEFPATH      "/tmp/" AUFS_XINO_FNAME
38822 +#define AUFS_XINO_DEF_SEC      30 /* seconds */
38823 +#define AUFS_XINO_DEF_TRUNC    45 /* percentage */
38824 +#define AUFS_DIRWH_DEF         3
38825 +#define AUFS_RDCACHE_DEF       10 /* seconds */
38826 +#define AUFS_RDCACHE_MAX       3600 /* seconds */
38827 +#define AUFS_RDBLK_DEF         512 /* bytes */
38828 +#define AUFS_RDHASH_DEF                32
38829 +#define AUFS_WKQ_NAME          AUFS_NAME "d"
38830 +#define AUFS_MFS_DEF_SEC       30 /* seconds */
38831 +#define AUFS_MFS_MAX_SEC       3600 /* seconds */
38832 +#define AUFS_FHSM_CACHE_DEF_SEC        30 /* seconds */
38833 +#define AUFS_PLINK_WARN                50 /* number of plinks in a single bucket */
38834 +
38835 +/* pseudo-link maintenace under /proc */
38836 +#define AUFS_PLINK_MAINT_NAME  "plink_maint"
38837 +#define AUFS_PLINK_MAINT_DIR   "fs/" AUFS_NAME
38838 +#define AUFS_PLINK_MAINT_PATH  AUFS_PLINK_MAINT_DIR "/" AUFS_PLINK_MAINT_NAME
38839 +
38840 +/* dirren, renamed dir */
38841 +#define AUFS_DR_INFO_PFX       AUFS_WH_PFX ".dr."
38842 +#define AUFS_DR_BRHINO_NAME    AUFS_WH_PFX "hino"
38843 +/* whiteouted doubly */
38844 +#define AUFS_WH_DR_INFO_PFX    AUFS_WH_PFX AUFS_DR_INFO_PFX
38845 +#define AUFS_WH_DR_BRHINO      AUFS_WH_PFX AUFS_DR_BRHINO_NAME
38846 +
38847 +#define AUFS_DIROPQ_NAME       AUFS_WH_PFX ".opq" /* whiteouted doubly */
38848 +#define AUFS_WH_DIROPQ         AUFS_WH_PFX AUFS_DIROPQ_NAME
38849 +
38850 +#define AUFS_BASE_NAME         AUFS_WH_PFX AUFS_NAME
38851 +#define AUFS_PLINKDIR_NAME     AUFS_WH_PFX "plnk"
38852 +#define AUFS_ORPHDIR_NAME      AUFS_WH_PFX "orph"
38853 +
38854 +/* doubly whiteouted */
38855 +#define AUFS_WH_BASE           AUFS_WH_PFX AUFS_BASE_NAME
38856 +#define AUFS_WH_PLINKDIR       AUFS_WH_PFX AUFS_PLINKDIR_NAME
38857 +#define AUFS_WH_ORPHDIR                AUFS_WH_PFX AUFS_ORPHDIR_NAME
38858 +
38859 +/* branch permissions and attributes */
38860 +#define AUFS_BRPERM_RW         "rw"
38861 +#define AUFS_BRPERM_RO         "ro"
38862 +#define AUFS_BRPERM_RR         "rr"
38863 +#define AUFS_BRATTR_COO_REG    "coo_reg"
38864 +#define AUFS_BRATTR_COO_ALL    "coo_all"
38865 +#define AUFS_BRATTR_FHSM       "fhsm"
38866 +#define AUFS_BRATTR_UNPIN      "unpin"
38867 +#define AUFS_BRATTR_ICEX       "icex"
38868 +#define AUFS_BRATTR_ICEX_SEC   "icexsec"
38869 +#define AUFS_BRATTR_ICEX_SYS   "icexsys"
38870 +#define AUFS_BRATTR_ICEX_TR    "icextr"
38871 +#define AUFS_BRATTR_ICEX_USR   "icexusr"
38872 +#define AUFS_BRATTR_ICEX_OTH   "icexoth"
38873 +#define AUFS_BRRATTR_WH                "wh"
38874 +#define AUFS_BRWATTR_NLWH      "nolwh"
38875 +#define AUFS_BRWATTR_MOO       "moo"
38876 +
38877 +#define AuBrPerm_RW            1               /* writable, hardlinkable wh */
38878 +#define AuBrPerm_RO            (1 << 1)        /* readonly */
38879 +#define AuBrPerm_RR            (1 << 2)        /* natively readonly */
38880 +#define AuBrPerm_Mask          (AuBrPerm_RW | AuBrPerm_RO | AuBrPerm_RR)
38881 +
38882 +#define AuBrAttr_COO_REG       (1 << 3)        /* copy-up on open */
38883 +#define AuBrAttr_COO_ALL       (1 << 4)
38884 +#define AuBrAttr_COO_Mask      (AuBrAttr_COO_REG | AuBrAttr_COO_ALL)
38885 +
38886 +#define AuBrAttr_FHSM          (1 << 5)        /* file-based hsm */
38887 +#define AuBrAttr_UNPIN         (1 << 6)        /* rename-able top dir of
38888 +                                                  branch. meaningless since
38889 +                                                  linux-3.18-rc1 */
38890 +
38891 +/* ignore error in copying XATTR */
38892 +#define AuBrAttr_ICEX_SEC      (1 << 7)
38893 +#define AuBrAttr_ICEX_SYS      (1 << 8)
38894 +#define AuBrAttr_ICEX_TR       (1 << 9)
38895 +#define AuBrAttr_ICEX_USR      (1 << 10)
38896 +#define AuBrAttr_ICEX_OTH      (1 << 11)
38897 +#define AuBrAttr_ICEX          (AuBrAttr_ICEX_SEC      \
38898 +                                | AuBrAttr_ICEX_SYS    \
38899 +                                | AuBrAttr_ICEX_TR     \
38900 +                                | AuBrAttr_ICEX_USR    \
38901 +                                | AuBrAttr_ICEX_OTH)
38902 +
38903 +#define AuBrRAttr_WH           (1 << 12)       /* whiteout-able */
38904 +#define AuBrRAttr_Mask         AuBrRAttr_WH
38905 +
38906 +#define AuBrWAttr_NoLinkWH     (1 << 13)       /* un-hardlinkable whiteouts */
38907 +#define AuBrWAttr_MOO          (1 << 14)       /* move-up on open */
38908 +#define AuBrWAttr_Mask         (AuBrWAttr_NoLinkWH | AuBrWAttr_MOO)
38909 +
38910 +#define AuBrAttr_CMOO_Mask     (AuBrAttr_COO_Mask | AuBrWAttr_MOO)
38911 +
38912 +/* #warning test userspace */
38913 +#ifdef __KERNEL__
38914 +#ifndef CONFIG_AUFS_FHSM
38915 +#undef AuBrAttr_FHSM
38916 +#define AuBrAttr_FHSM          0
38917 +#endif
38918 +#ifndef CONFIG_AUFS_XATTR
38919 +#undef AuBrAttr_ICEX
38920 +#define AuBrAttr_ICEX          0
38921 +#undef AuBrAttr_ICEX_SEC
38922 +#define AuBrAttr_ICEX_SEC      0
38923 +#undef AuBrAttr_ICEX_SYS
38924 +#define AuBrAttr_ICEX_SYS      0
38925 +#undef AuBrAttr_ICEX_TR
38926 +#define AuBrAttr_ICEX_TR       0
38927 +#undef AuBrAttr_ICEX_USR
38928 +#define AuBrAttr_ICEX_USR      0
38929 +#undef AuBrAttr_ICEX_OTH
38930 +#define AuBrAttr_ICEX_OTH      0
38931 +#endif
38932 +#endif
38933 +
38934 +/* the longest combination */
38935 +/* AUFS_BRATTR_ICEX and AUFS_BRATTR_ICEX_TR don't affect here */
38936 +#define AuBrPermStrSz  sizeof(AUFS_BRPERM_RW                   \
38937 +                              "+" AUFS_BRATTR_COO_REG          \
38938 +                              "+" AUFS_BRATTR_FHSM             \
38939 +                              "+" AUFS_BRATTR_UNPIN            \
38940 +                              "+" AUFS_BRATTR_ICEX_SEC         \
38941 +                              "+" AUFS_BRATTR_ICEX_SYS         \
38942 +                              "+" AUFS_BRATTR_ICEX_USR         \
38943 +                              "+" AUFS_BRATTR_ICEX_OTH         \
38944 +                              "+" AUFS_BRWATTR_NLWH)
38945 +
38946 +typedef struct {
38947 +       char a[AuBrPermStrSz];
38948 +} au_br_perm_str_t;
38949 +
38950 +static inline int au_br_writable(int brperm)
38951 +{
38952 +       return brperm & AuBrPerm_RW;
38953 +}
38954 +
38955 +static inline int au_br_whable(int brperm)
38956 +{
38957 +       return brperm & (AuBrPerm_RW | AuBrRAttr_WH);
38958 +}
38959 +
38960 +static inline int au_br_wh_linkable(int brperm)
38961 +{
38962 +       return !(brperm & AuBrWAttr_NoLinkWH);
38963 +}
38964 +
38965 +static inline int au_br_cmoo(int brperm)
38966 +{
38967 +       return brperm & AuBrAttr_CMOO_Mask;
38968 +}
38969 +
38970 +static inline int au_br_fhsm(int brperm)
38971 +{
38972 +       return brperm & AuBrAttr_FHSM;
38973 +}
38974 +
38975 +/* ---------------------------------------------------------------------- */
38976 +
38977 +/* ioctl */
38978 +enum {
38979 +       /* readdir in userspace */
38980 +       AuCtl_RDU,
38981 +       AuCtl_RDU_INO,
38982 +
38983 +       AuCtl_WBR_FD,   /* pathconf wrapper */
38984 +       AuCtl_IBUSY,    /* busy inode */
38985 +       AuCtl_MVDOWN,   /* move-down */
38986 +       AuCtl_BR,       /* info about branches */
38987 +       AuCtl_FHSM_FD   /* connection for fhsm */
38988 +};
38989 +
38990 +/* borrowed from linux/include/linux/kernel.h */
38991 +#ifndef ALIGN
38992 +#ifdef _GNU_SOURCE
38993 +#define ALIGN(x, a)            __ALIGN_MASK(x, (typeof(x))(a)-1)
38994 +#else
38995 +#define ALIGN(x, a)            (((x) + (a) - 1) & ~((a) - 1))
38996 +#endif
38997 +#define __ALIGN_MASK(x, mask)  (((x)+(mask))&~(mask))
38998 +#endif
38999 +
39000 +/* borrowed from linux/include/linux/compiler-gcc3.h */
39001 +#ifndef __aligned
39002 +#define __aligned(x)                   __attribute__((aligned(x)))
39003 +#endif
39004 +
39005 +#ifdef __KERNEL__
39006 +#ifndef __packed
39007 +#define __packed                       __attribute__((packed))
39008 +#endif
39009 +#endif
39010 +
39011 +struct au_rdu_cookie {
39012 +       uint64_t        h_pos;
39013 +       int16_t         bindex;
39014 +       uint8_t         flags;
39015 +       uint8_t         pad;
39016 +       uint32_t        generation;
39017 +} __aligned(8);
39018 +
39019 +struct au_rdu_ent {
39020 +       uint64_t        ino;
39021 +       int16_t         bindex;
39022 +       uint8_t         type;
39023 +       uint8_t         nlen;
39024 +       uint8_t         wh;
39025 +       char            name[];
39026 +} __aligned(8);
39027 +
39028 +static inline int au_rdu_len(int nlen)
39029 +{
39030 +       /* include the terminating NULL */
39031 +       return ALIGN(sizeof(struct au_rdu_ent) + nlen + 1,
39032 +                    sizeof(uint64_t));
39033 +}
39034 +
39035 +union au_rdu_ent_ul {
39036 +       struct au_rdu_ent __user        *e;
39037 +       uint64_t                        ul;
39038 +};
39039 +
39040 +enum {
39041 +       AufsCtlRduV_SZ,
39042 +       AufsCtlRduV_End
39043 +};
39044 +
39045 +struct aufs_rdu {
39046 +       /* input */
39047 +       union {
39048 +               uint64_t        sz;     /* AuCtl_RDU */
39049 +               uint64_t        nent;   /* AuCtl_RDU_INO */
39050 +       };
39051 +       union au_rdu_ent_ul     ent;
39052 +       uint16_t                verify[AufsCtlRduV_End];
39053 +
39054 +       /* input/output */
39055 +       uint32_t                blk;
39056 +
39057 +       /* output */
39058 +       union au_rdu_ent_ul     tail;
39059 +       /* number of entries which were added in a single call */
39060 +       uint64_t                rent;
39061 +       uint8_t                 full;
39062 +       uint8_t                 shwh;
39063 +
39064 +       struct au_rdu_cookie    cookie;
39065 +} __aligned(8);
39066 +
39067 +/* ---------------------------------------------------------------------- */
39068 +
39069 +/* dirren. the branch is identified by the filename who contains this */
39070 +struct au_drinfo {
39071 +       uint64_t ino;
39072 +       union {
39073 +               uint8_t oldnamelen;
39074 +               uint64_t _padding;
39075 +       };
39076 +       uint8_t oldname[];
39077 +} __aligned(8);
39078 +
39079 +struct au_drinfo_fdata {
39080 +       uint32_t magic;
39081 +       struct au_drinfo drinfo;
39082 +} __aligned(8);
39083 +
39084 +#define AUFS_DRINFO_MAGIC_V1   ('a' << 24 | 'd' << 16 | 'r' << 8 | 0x01)
39085 +/* future */
39086 +#define AUFS_DRINFO_MAGIC_V2   ('a' << 24 | 'd' << 16 | 'r' << 8 | 0x02)
39087 +
39088 +/* ---------------------------------------------------------------------- */
39089 +
39090 +struct aufs_wbr_fd {
39091 +       uint32_t        oflags;
39092 +       int16_t         brid;
39093 +} __aligned(8);
39094 +
39095 +/* ---------------------------------------------------------------------- */
39096 +
39097 +struct aufs_ibusy {
39098 +       uint64_t        ino, h_ino;
39099 +       int16_t         bindex;
39100 +} __aligned(8);
39101 +
39102 +/* ---------------------------------------------------------------------- */
39103 +
39104 +/* error code for move-down */
39105 +/* the actual message strings are implemented in aufs-util.git */
39106 +enum {
39107 +       EAU_MVDOWN_OPAQUE = 1,
39108 +       EAU_MVDOWN_WHITEOUT,
39109 +       EAU_MVDOWN_UPPER,
39110 +       EAU_MVDOWN_BOTTOM,
39111 +       EAU_MVDOWN_NOUPPER,
39112 +       EAU_MVDOWN_NOLOWERBR,
39113 +       EAU_Last
39114 +};
39115 +
39116 +/* flags for move-down */
39117 +#define AUFS_MVDOWN_DMSG       1
39118 +#define AUFS_MVDOWN_OWLOWER    (1 << 1)        /* overwrite lower */
39119 +#define AUFS_MVDOWN_KUPPER     (1 << 2)        /* keep upper */
39120 +#define AUFS_MVDOWN_ROLOWER    (1 << 3)        /* do even if lower is RO */
39121 +#define AUFS_MVDOWN_ROLOWER_R  (1 << 4)        /* did on lower RO */
39122 +#define AUFS_MVDOWN_ROUPPER    (1 << 5)        /* do even if upper is RO */
39123 +#define AUFS_MVDOWN_ROUPPER_R  (1 << 6)        /* did on upper RO */
39124 +#define AUFS_MVDOWN_BRID_UPPER (1 << 7)        /* upper brid */
39125 +#define AUFS_MVDOWN_BRID_LOWER (1 << 8)        /* lower brid */
39126 +#define AUFS_MVDOWN_FHSM_LOWER (1 << 9)        /* find fhsm attr for lower */
39127 +#define AUFS_MVDOWN_STFS       (1 << 10)       /* req. stfs */
39128 +#define AUFS_MVDOWN_STFS_FAILED        (1 << 11)       /* output: stfs is unusable */
39129 +#define AUFS_MVDOWN_BOTTOM     (1 << 12)       /* output: no more lowers */
39130 +
39131 +/* index for move-down */
39132 +enum {
39133 +       AUFS_MVDOWN_UPPER,
39134 +       AUFS_MVDOWN_LOWER,
39135 +       AUFS_MVDOWN_NARRAY
39136 +};
39137 +
39138 +/*
39139 + * additional info of move-down
39140 + * number of free blocks and inodes.
39141 + * subset of struct kstatfs, but smaller and always 64bit.
39142 + */
39143 +struct aufs_stfs {
39144 +       uint64_t        f_blocks;
39145 +       uint64_t        f_bavail;
39146 +       uint64_t        f_files;
39147 +       uint64_t        f_ffree;
39148 +};
39149 +
39150 +struct aufs_stbr {
39151 +       int16_t                 brid;   /* optional input */
39152 +       int16_t                 bindex; /* output */
39153 +       struct aufs_stfs        stfs;   /* output when AUFS_MVDOWN_STFS set */
39154 +} __aligned(8);
39155 +
39156 +struct aufs_mvdown {
39157 +       uint32_t                flags;                  /* input/output */
39158 +       struct aufs_stbr        stbr[AUFS_MVDOWN_NARRAY]; /* input/output */
39159 +       int8_t                  au_errno;               /* output */
39160 +} __aligned(8);
39161 +
39162 +/* ---------------------------------------------------------------------- */
39163 +
39164 +union aufs_brinfo {
39165 +       /* PATH_MAX may differ between kernel-space and user-space */
39166 +       char    _spacer[4096];
39167 +       struct {
39168 +               int16_t id;
39169 +               int     perm;
39170 +               char    path[];
39171 +       };
39172 +} __aligned(8);
39173 +
39174 +/* ---------------------------------------------------------------------- */
39175 +
39176 +#define AuCtlType              'A'
39177 +#define AUFS_CTL_RDU           _IOWR(AuCtlType, AuCtl_RDU, struct aufs_rdu)
39178 +#define AUFS_CTL_RDU_INO       _IOWR(AuCtlType, AuCtl_RDU_INO, struct aufs_rdu)
39179 +#define AUFS_CTL_WBR_FD                _IOW(AuCtlType, AuCtl_WBR_FD, \
39180 +                                    struct aufs_wbr_fd)
39181 +#define AUFS_CTL_IBUSY         _IOWR(AuCtlType, AuCtl_IBUSY, struct aufs_ibusy)
39182 +#define AUFS_CTL_MVDOWN                _IOWR(AuCtlType, AuCtl_MVDOWN, \
39183 +                                     struct aufs_mvdown)
39184 +#define AUFS_CTL_BRINFO                _IOW(AuCtlType, AuCtl_BR, union aufs_brinfo)
39185 +#define AUFS_CTL_FHSM_FD       _IOW(AuCtlType, AuCtl_FHSM_FD, int)
39186 +
39187 +#endif /* __AUFS_TYPE_H__ */
39188 SPDX-License-Identifier: GPL-2.0
39189 aufs5.x-rcN loopback patch
39190
39191 diff --git a/drivers/block/loop.c b/drivers/block/loop.c
39192 index a62b132cbd551..79cf20555c2b1 100644
39193 --- a/drivers/block/loop.c
39194 +++ b/drivers/block/loop.c
39195 @@ -685,6 +685,15 @@ static inline void loop_update_dio(struct loop_device *lo)
39196                                 lo->use_dio);
39197  }
39198  
39199 +static struct file *loop_real_file(struct file *file)
39200 +{
39201 +       struct file *f = NULL;
39202 +
39203 +       if (file->f_path.dentry->d_sb->s_op->real_loop)
39204 +               f = file->f_path.dentry->d_sb->s_op->real_loop(file);
39205 +       return f;
39206 +}
39207 +
39208  static void loop_reread_partitions(struct loop_device *lo)
39209  {
39210         int rc;
39211 @@ -742,6 +751,7 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39212  {
39213         struct file *file = fget(arg);
39214         struct file *old_file;
39215 +       struct file *f, *virt_file = NULL, *old_virt_file;
39216         int error;
39217         bool partscan;
39218         bool is_loop;
39219 @@ -761,11 +771,19 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39220         if (!(lo->lo_flags & LO_FLAGS_READ_ONLY))
39221                 goto out_err;
39222  
39223 +       f = loop_real_file(file);
39224 +       if (f) {
39225 +               virt_file = file;
39226 +               file = f;
39227 +               get_file(file);
39228 +       }
39229 +
39230         error = loop_validate_file(file, bdev);
39231         if (error)
39232                 goto out_err;
39233  
39234         old_file = lo->lo_backing_file;
39235 +       old_virt_file = lo->lo_backing_virt_file;
39236  
39237         error = -EINVAL;
39238  
39239 @@ -778,6 +796,7 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39240         blk_mq_freeze_queue(lo->lo_queue);
39241         mapping_set_gfp_mask(old_file->f_mapping, lo->old_gfp_mask);
39242         lo->lo_backing_file = file;
39243 +       lo->lo_backing_virt_file = virt_file;
39244         lo->old_gfp_mask = mapping_gfp_mask(file->f_mapping);
39245         mapping_set_gfp_mask(file->f_mapping,
39246                              lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
39247 @@ -800,6 +819,8 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39248          * dependency.
39249          */
39250         fput(old_file);
39251 +       if (old_virt_file)
39252 +               fput(old_virt_file);
39253         if (partscan)
39254                 loop_reread_partitions(lo);
39255         return 0;
39256 @@ -808,6 +829,8 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39257         loop_global_unlock(lo, is_loop);
39258  out_putf:
39259         fput(file);
39260 +       if (virt_file)
39261 +               fput(virt_file);
39262         return error;
39263  }
39264  
39265 @@ -1208,6 +1231,7 @@ static int loop_configure(struct loop_device *lo, fmode_t mode,
39266                           const struct loop_config *config)
39267  {
39268         struct file *file = fget(config->fd);
39269 +       struct file *f, *virt_file = NULL;
39270         struct inode *inode;
39271         struct address_space *mapping;
39272         int error;
39273 @@ -1223,6 +1247,13 @@ static int loop_configure(struct loop_device *lo, fmode_t mode,
39274         /* This is safe, since we have a reference from open(). */
39275         __module_get(THIS_MODULE);
39276  
39277 +       f = loop_real_file(file);
39278 +       if (f) {
39279 +               virt_file = file;
39280 +               file = f;
39281 +               get_file(file);
39282 +       }
39283 +
39284         /*
39285          * If we don't hold exclusive handle for the device, upgrade to it
39286          * here to avoid changing device under exclusive owner.
39287 @@ -1288,6 +1319,7 @@ static int loop_configure(struct loop_device *lo, fmode_t mode,
39288         lo->use_dio = lo->lo_flags & LO_FLAGS_DIRECT_IO;
39289         lo->lo_device = bdev;
39290         lo->lo_backing_file = file;
39291 +       lo->lo_backing_virt_file = virt_file;
39292         lo->old_gfp_mask = mapping_gfp_mask(mapping);
39293         mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
39294  
39295 @@ -1338,6 +1370,8 @@ static int loop_configure(struct loop_device *lo, fmode_t mode,
39296                 bd_abort_claiming(bdev, loop_configure);
39297  out_putf:
39298         fput(file);
39299 +       if (virt_file)
39300 +               fput(virt_file);
39301         /* This is safe: open() is still holding a reference. */
39302         module_put(THIS_MODULE);
39303         return error;
39304 @@ -1346,6 +1380,7 @@ static int loop_configure(struct loop_device *lo, fmode_t mode,
39305  static int __loop_clr_fd(struct loop_device *lo, bool release)
39306  {
39307         struct file *filp = NULL;
39308 +       struct file *virt_filp = lo->lo_backing_virt_file;
39309         gfp_t gfp = lo->old_gfp_mask;
39310         struct block_device *bdev = lo->lo_device;
39311         int err = 0;
39312 @@ -1397,6 +1432,7 @@ static int __loop_clr_fd(struct loop_device *lo, bool release)
39313  
39314         spin_lock_irq(&lo->lo_lock);
39315         lo->lo_backing_file = NULL;
39316 +       lo->lo_backing_virt_file = NULL;
39317         spin_unlock_irq(&lo->lo_lock);
39318  
39319         loop_release_xfer(lo);
39320 @@ -1477,6 +1513,8 @@ static int __loop_clr_fd(struct loop_device *lo, bool release)
39321          */
39322         if (filp)
39323                 fput(filp);
39324 +       if (virt_filp)
39325 +               fput(virt_filp);
39326         return err;
39327  }
39328  
39329 diff --git a/drivers/block/loop.h b/drivers/block/loop.h
39330 index 04c88dd6eabd6..0ff3ba22ee170 100644
39331 --- a/drivers/block/loop.h
39332 +++ b/drivers/block/loop.h
39333 @@ -46,7 +46,7 @@ struct loop_device {
39334         int             (*ioctl)(struct loop_device *, int cmd, 
39335                                  unsigned long arg); 
39336  
39337 -       struct file *   lo_backing_file;
39338 +       struct file     *lo_backing_file, *lo_backing_virt_file;
39339         struct block_device *lo_device;
39340         void            *key_data; 
39341  
39342 diff --git a/fs/aufs/f_op.c b/fs/aufs/f_op.c
39343 index 99ceca1440449..e49dfe8550329 100644
39344 --- a/fs/aufs/f_op.c
39345 +++ b/fs/aufs/f_op.c
39346 @@ -304,7 +304,7 @@ static ssize_t aufs_read_iter(struct kiocb *kio, struct iov_iter *iov_iter)
39347         if (IS_ERR(h_file))
39348                 goto out;
39349  
39350 -       if (au_test_loopback_kthread()) {
39351 +       if (0 && au_test_loopback_kthread()) {
39352                 au_warn_loopback(h_file->f_path.dentry->d_sb);
39353                 if (file->f_mapping != h_file->f_mapping) {
39354                         file->f_mapping = h_file->f_mapping;
39355 diff --git a/fs/aufs/loop.c b/fs/aufs/loop.c
39356 index 74347bd75b380..5ef888a1d53f4 100644
39357 --- a/fs/aufs/loop.c
39358 +++ b/fs/aufs/loop.c
39359 @@ -133,3 +133,19 @@ void au_loopback_fin(void)
39360                 symbol_put(loop_backing_file);
39361         au_kfree_try_rcu(au_warn_loopback_array);
39362  }
39363 +
39364 +/* ---------------------------------------------------------------------- */
39365 +
39366 +/* support the loopback block device insude aufs */
39367 +
39368 +struct file *aufs_real_loop(struct file *file)
39369 +{
39370 +       struct file *f;
39371 +
39372 +       BUG_ON(!au_test_aufs(file->f_path.dentry->d_sb));
39373 +       fi_read_lock(file);
39374 +       f = au_hf_top(file);
39375 +       fi_read_unlock(file);
39376 +       AuDebugOn(!f);
39377 +       return f;
39378 +}
39379 diff --git a/fs/aufs/loop.h b/fs/aufs/loop.h
39380 index 7293bee427f96..3345c098d0d47 100644
39381 --- a/fs/aufs/loop.h
39382 +++ b/fs/aufs/loop.h
39383 @@ -26,6 +26,8 @@ void au_warn_loopback(struct super_block *h_sb);
39384  
39385  int au_loopback_init(void);
39386  void au_loopback_fin(void);
39387 +
39388 +struct file *aufs_real_loop(struct file *file);
39389  #else
39390  AuStub(struct file *, loop_backing_file, return NULL, struct super_block *sb)
39391  
39392 @@ -36,6 +38,8 @@ AuStubVoid(au_warn_loopback, struct super_block *h_sb)
39393  
39394  AuStubInt0(au_loopback_init, void)
39395  AuStubVoid(au_loopback_fin, void)
39396 +
39397 +AuStub(struct file *, aufs_real_loop, return NULL, struct file *file)
39398  #endif /* BLK_DEV_LOOP */
39399  
39400  #endif /* __KERNEL__ */
39401 diff --git a/fs/aufs/super.c b/fs/aufs/super.c
39402 index ae38857541533..af32f0960e595 100644
39403 --- a/fs/aufs/super.c
39404 +++ b/fs/aufs/super.c
39405 @@ -844,7 +844,10 @@ static const struct super_operations aufs_sop = {
39406         .statfs         = aufs_statfs,
39407         .put_super      = aufs_put_super,
39408         .sync_fs        = aufs_sync_fs,
39409 -       .remount_fs     = aufs_remount_fs
39410 +       .remount_fs     = aufs_remount_fs,
39411 +#ifdef CONFIG_AUFS_BDEV_LOOP
39412 +       .real_loop      = aufs_real_loop
39413 +#endif
39414  };
39415  
39416  /* ---------------------------------------------------------------------- */
39417 diff --git a/include/linux/fs.h b/include/linux/fs.h
39418 index 376ddf1965aa2..adb8c854de599 100644
39419 --- a/include/linux/fs.h
39420 +++ b/include/linux/fs.h
39421 @@ -2226,6 +2226,10 @@ struct super_operations {
39422                                   struct shrink_control *);
39423         long (*free_cached_objects)(struct super_block *,
39424                                     struct shrink_control *);
39425 +#if IS_ENABLED(CONFIG_BLK_DEV_LOOP) || IS_ENABLED(CONFIG_BLK_DEV_LOOP_MODULE)
39426 +       /* and aufs */
39427 +       struct file *(*real_loop)(struct file *);
39428 +#endif
39429  };
39430  
39431  /*
This page took 3.766483 seconds and 3 git commands to generate.