]> git.pld-linux.org Git - packages/kernel.git/blob - kernel-aufs5.patch
a037e6c98dbe8c1d4001bdedb52460b014bdbf74
[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 6c7dc1387beb0..4af0a823e5f24 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 208a74e0b00e1..57d47a0762e0a 100644
18 --- a/fs/Makefile
19 +++ b/fs/Makefile
20 @@ -140,3 +140,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 05fd080b82f3a..9554f31190578 100644
30 --- a/MAINTAINERS
31 +++ b/MAINTAINERS
32 @@ -3235,6 +3235,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 19fe19eaa50e9..9009a0efc4b28 100644
54 --- a/drivers/block/loop.c
55 +++ b/drivers/block/loop.c
56 @@ -636,6 +636,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 c84269c6e8bf6..6f4f48c828351 100644
83 --- a/fs/dcache.c
84 +++ b/fs/dcache.c
85 @@ -1345,7 +1345,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/namespace.c b/fs/namespace.c
117 index de6fae84f1a1a..e189393b34b14 100644
118 --- a/fs/namespace.c
119 +++ b/fs/namespace.c
120 @@ -839,6 +839,12 @@ static inline int check_mnt(struct mount *mnt)
121         return mnt->mnt_ns == current->nsproxy->mnt_ns;
122  }
123  
124 +/* for aufs, CONFIG_AUFS_BR_FUSE */
125 +int is_current_mnt_ns(struct vfsmount *mnt)
126 +{
127 +       return check_mnt(real_mount(mnt));
128 +}
129 +
130  /*
131   * vfsmount lock must be held for write
132   */
133 diff --git a/fs/splice.c b/fs/splice.c
134 index 5dbce4dcc1a7d..3e6ba363b7775 100644
135 --- a/fs/splice.c
136 +++ b/fs/splice.c
137 @@ -759,8 +759,8 @@ static int warn_unsupported(struct file *file, const char *op)
138  /*
139   * Attempt to initiate a splice from pipe to file.
140   */
141 -static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
142 -                          loff_t *ppos, size_t len, unsigned int flags)
143 +long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
144 +                   loff_t *ppos, size_t len, unsigned int flags)
145  {
146         if (unlikely(!out->f_op->splice_write))
147                 return warn_unsupported(out, "write");
148 @@ -770,9 +770,9 @@ static long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
149  /*
150   * Attempt to initiate a splice from a file to a pipe.
151   */
152 -static long do_splice_to(struct file *in, loff_t *ppos,
153 -                        struct pipe_inode_info *pipe, size_t len,
154 -                        unsigned int flags)
155 +long do_splice_to(struct file *in, loff_t *ppos,
156 +                 struct pipe_inode_info *pipe, size_t len,
157 +                 unsigned int flags)
158  {
159         unsigned int p_space;
160         int ret;
161 diff --git a/include/linux/fs.h b/include/linux/fs.h
162 index e2d892b201b07..5479e6dc18a23 100644
163 --- a/include/linux/fs.h
164 +++ b/include/linux/fs.h
165 @@ -1372,6 +1372,7 @@ extern void fasync_free(struct fasync_struct *);
166  /* can be called from interrupts */
167  extern void kill_fasync(struct fasync_struct **, int, int);
168  
169 +extern int setfl(int fd, struct file *filp, unsigned long arg);
170  extern void __f_setown(struct file *filp, struct pid *, enum pid_type, int force);
171  extern int f_setown(struct file *filp, unsigned long arg, int force);
172  extern void f_delown(struct file *filp);
173 @@ -2003,6 +2004,7 @@ struct file_operations {
174         ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int);
175         unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
176         int (*check_flags)(int);
177 +       int (*setfl)(struct file *, unsigned long);
178         int (*flock) (struct file *, int, struct file_lock *);
179         ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, loff_t *, size_t, unsigned int);
180         ssize_t (*splice_read)(struct file *, loff_t *, struct pipe_inode_info *, size_t, unsigned int);
181 @@ -2529,6 +2531,7 @@ extern int current_umask(void);
182  extern void ihold(struct inode * inode);
183  extern void iput(struct inode *);
184  extern int generic_update_time(struct inode *, struct timespec64 *, int);
185 +extern int update_time(struct inode *, struct timespec64 *, int);
186  
187  /* /sys/fs */
188  extern struct kobject *fs_kobj;
189 @@ -2707,6 +2710,7 @@ static inline bool sb_is_blkdev_sb(struct super_block *sb)
190  }
191  
192  void emergency_thaw_all(void);
193 +extern int __sync_filesystem(struct super_block *, int);
194  extern int sync_filesystem(struct super_block *);
195  extern const struct file_operations def_blk_fops;
196  extern const struct file_operations def_chr_fops;
197 diff --git a/include/linux/lockdep.h b/include/linux/lockdep.h
198 index 467b94257105e..dab20282c82e2 100644
199 --- a/include/linux/lockdep.h
200 +++ b/include/linux/lockdep.h
201 @@ -248,6 +248,8 @@ static inline int lockdep_match_key(struct lockdep_map *lock,
202         return lock->key == key;
203  }
204  
205 +struct lock_class *lockdep_hlock_class(struct held_lock *hlock);
206 +
207  /*
208   * Acquire a lock.
209   *
210 diff --git a/include/linux/mnt_namespace.h b/include/linux/mnt_namespace.h
211 index 8f882f5881e87..6b9808f098435 100644
212 --- a/include/linux/mnt_namespace.h
213 +++ b/include/linux/mnt_namespace.h
214 @@ -7,12 +7,15 @@ struct mnt_namespace;
215  struct fs_struct;
216  struct user_namespace;
217  struct ns_common;
218 +struct vfsmount;
219  
220  extern struct mnt_namespace *copy_mnt_ns(unsigned long, struct mnt_namespace *,
221                 struct user_namespace *, struct fs_struct *);
222  extern void put_mnt_ns(struct mnt_namespace *ns);
223  extern struct ns_common *from_mnt_ns(struct mnt_namespace *);
224  
225 +extern int is_current_mnt_ns(struct vfsmount *mnt);
226 +
227  extern const struct file_operations proc_mounts_operations;
228  extern const struct file_operations proc_mountinfo_operations;
229  extern const struct file_operations proc_mountstats_operations;
230 diff --git a/include/linux/splice.h b/include/linux/splice.h
231 index a55179fd60fc3..8e21c53cf8831 100644
232 --- a/include/linux/splice.h
233 +++ b/include/linux/splice.h
234 @@ -93,4 +93,10 @@ extern void splice_shrink_spd(struct splice_pipe_desc *);
235  
236  extern const struct pipe_buf_operations page_cache_pipe_buf_ops;
237  extern const struct pipe_buf_operations default_pipe_buf_ops;
238 +
239 +extern long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
240 +                          loff_t *ppos, size_t len, unsigned int flags);
241 +extern long do_splice_to(struct file *in, loff_t *ppos,
242 +                        struct pipe_inode_info *pipe, size_t len,
243 +                        unsigned int flags);
244  #endif
245 diff --git a/kernel/locking/lockdep.c b/kernel/locking/lockdep.c
246 index f8a0212189cad..43e3d1fdd570b 100644
247 --- a/kernel/locking/lockdep.c
248 +++ b/kernel/locking/lockdep.c
249 @@ -189,7 +189,7 @@ static
250  struct lock_class lock_classes[MAX_LOCKDEP_KEYS];
251  DECLARE_BITMAP(lock_classes_in_use, MAX_LOCKDEP_KEYS);
252  
253 -static inline struct lock_class *hlock_class(struct held_lock *hlock)
254 +inline struct lock_class *lockdep_hlock_class(struct held_lock *hlock)
255  {
256         unsigned int class_idx = hlock->class_idx;
257  
258 @@ -210,6 +210,7 @@ static inline struct lock_class *hlock_class(struct held_lock *hlock)
259          */
260         return lock_classes + class_idx;
261  }
262 +#define hlock_class(hlock) lockdep_hlock_class(hlock)
263  
264  #ifdef CONFIG_LOCK_STAT
265  static DEFINE_PER_CPU(struct lock_class_stats[MAX_LOCKDEP_KEYS], cpu_lock_stats);
266 SPDX-License-Identifier: GPL-2.0
267 aufs5.x-rcN mmap patch
268
269 diff --git a/fs/proc/base.c b/fs/proc/base.c
270 index d654ce7150fdd..422713de90f60 100644
271 --- a/fs/proc/base.c
272 +++ b/fs/proc/base.c
273 @@ -2191,7 +2191,7 @@ static int map_files_get_link(struct dentry *dentry, struct path *path)
274         rc = -ENOENT;
275         vma = find_exact_vma(mm, vm_start, vm_end);
276         if (vma && vma->vm_file) {
277 -               *path = vma->vm_file->f_path;
278 +               *path = vma_pr_or_file(vma)->f_path;
279                 path_get(path);
280                 rc = 0;
281         }
282 diff --git a/fs/proc/nommu.c b/fs/proc/nommu.c
283 index 13452b32e2bd5..38acccfef9d49 100644
284 --- a/fs/proc/nommu.c
285 +++ b/fs/proc/nommu.c
286 @@ -40,7 +40,10 @@ static int nommu_region_show(struct seq_file *m, struct vm_region *region)
287         file = region->vm_file;
288  
289         if (file) {
290 -               struct inode *inode = file_inode(region->vm_file);
291 +               struct inode *inode;
292 +
293 +               file = vmr_pr_or_file(region);
294 +               inode = file_inode(file);
295                 dev = inode->i_sb->s_dev;
296                 ino = inode->i_ino;
297         }
298 diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c
299 index f46060eb91b5c..2a7c8709acadd 100644
300 --- a/fs/proc/task_mmu.c
301 +++ b/fs/proc/task_mmu.c
302 @@ -281,7 +281,10 @@ show_map_vma(struct seq_file *m, struct vm_area_struct *vma)
303         const char *name = NULL;
304  
305         if (file) {
306 -               struct inode *inode = file_inode(vma->vm_file);
307 +               struct inode *inode;
308 +
309 +               file = vma_pr_or_file(vma);
310 +               inode = file_inode(file);
311                 dev = inode->i_sb->s_dev;
312                 ino = inode->i_ino;
313                 pgoff = ((loff_t)vma->vm_pgoff) << PAGE_SHIFT;
314 @@ -1903,7 +1906,7 @@ static int show_numa_map(struct seq_file *m, void *v)
315         struct proc_maps_private *proc_priv = &numa_priv->proc_maps;
316         struct vm_area_struct *vma = v;
317         struct numa_maps *md = &numa_priv->md;
318 -       struct file *file = vma->vm_file;
319 +       struct file *file = vma_pr_or_file(vma);
320         struct mm_struct *mm = vma->vm_mm;
321         struct mempolicy *pol;
322         char buffer[64];
323 diff --git a/fs/proc/task_nommu.c b/fs/proc/task_nommu.c
324 index a6d21fc0033c6..02c2de31196e0 100644
325 --- a/fs/proc/task_nommu.c
326 +++ b/fs/proc/task_nommu.c
327 @@ -155,7 +155,10 @@ static int nommu_vma_show(struct seq_file *m, struct vm_area_struct *vma)
328         file = vma->vm_file;
329  
330         if (file) {
331 -               struct inode *inode = file_inode(vma->vm_file);
332 +               struct inode *inode;
333 +
334 +               file = vma_pr_or_file(vma);
335 +               inode = file_inode(file);
336                 dev = inode->i_sb->s_dev;
337                 ino = inode->i_ino;
338                 pgoff = (loff_t)vma->vm_pgoff << PAGE_SHIFT;
339 diff --git a/include/linux/mm.h b/include/linux/mm.h
340 index 5744a3fc47169..f2f4c5ec69530 100644
341 --- a/include/linux/mm.h
342 +++ b/include/linux/mm.h
343 @@ -1895,6 +1895,28 @@ static inline void unmap_shared_mapping_range(struct address_space *mapping,
344         unmap_mapping_range(mapping, holebegin, holelen, 0);
345  }
346  
347 +extern void vma_do_file_update_time(struct vm_area_struct *, const char[], int);
348 +extern struct file *vma_do_pr_or_file(struct vm_area_struct *, const char[],
349 +                                     int);
350 +extern void vma_do_get_file(struct vm_area_struct *, const char[], int);
351 +extern void vma_do_fput(struct vm_area_struct *, const char[], int);
352 +
353 +#define vma_file_update_time(vma)      vma_do_file_update_time(vma, __func__, \
354 +                                                               __LINE__)
355 +#define vma_pr_or_file(vma)            vma_do_pr_or_file(vma, __func__, \
356 +                                                         __LINE__)
357 +#define vma_get_file(vma)              vma_do_get_file(vma, __func__, __LINE__)
358 +#define vma_fput(vma)                  vma_do_fput(vma, __func__, __LINE__)
359 +
360 +#ifndef CONFIG_MMU
361 +extern struct file *vmr_do_pr_or_file(struct vm_region *, const char[], int);
362 +extern void vmr_do_fput(struct vm_region *, const char[], int);
363 +
364 +#define vmr_pr_or_file(region)         vmr_do_pr_or_file(region, __func__, \
365 +                                                         __LINE__)
366 +#define vmr_fput(region)               vmr_do_fput(region, __func__, __LINE__)
367 +#endif /* !CONFIG_MMU */
368 +
369  extern int access_process_vm(struct task_struct *tsk, unsigned long addr,
370                 void *buf, int len, unsigned int gup_flags);
371  extern int access_remote_vm(struct mm_struct *mm, unsigned long addr,
372 diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h
373 index 0f549870da6a0..67d02ae46b8fa 100644
374 --- a/include/linux/mm_types.h
375 +++ b/include/linux/mm_types.h
376 @@ -344,6 +344,7 @@ struct vm_region {
377         unsigned long   vm_top;         /* region allocated to here */
378         unsigned long   vm_pgoff;       /* the offset in vm_file corresponding to vm_start */
379         struct file     *vm_file;       /* the backing file or NULL */
380 +       struct file     *vm_prfile;     /* the virtual backing file or NULL */
381  
382         int             vm_usage;       /* region usage count (access under nommu_region_sem) */
383         bool            vm_icache_flushed : 1; /* true if the icache has been flushed for
384 @@ -440,6 +441,7 @@ struct vm_area_struct {
385         unsigned long vm_pgoff;         /* Offset (within vm_file) in PAGE_SIZE
386                                            units */
387         struct file * vm_file;          /* File we map to (can be NULL). */
388 +       struct file *vm_prfile;         /* shadow of vm_file */
389         void * vm_private_data;         /* was vm_pte (shared mem) */
390  
391  #ifdef CONFIG_SWAP
392 diff --git a/kernel/fork.c b/kernel/fork.c
393 index f1e89007f2288..f085ecd96a7db 100644
394 --- a/kernel/fork.c
395 +++ b/kernel/fork.c
396 @@ -575,7 +575,7 @@ static __latent_entropy int dup_mmap(struct mm_struct *mm,
397                 if (file) {
398                         struct address_space *mapping = file->f_mapping;
399  
400 -                       get_file(file);
401 +                       vma_get_file(tmp);
402                         i_mmap_lock_write(mapping);
403                         if (tmp->vm_flags & VM_SHARED)
404                                 mapping_allow_writable(mapping);
405 diff --git a/mm/Makefile b/mm/Makefile
406 index 70d4309c9ce33..9acff75987dfa 100644
407 --- a/mm/Makefile
408 +++ b/mm/Makefile
409 @@ -54,7 +54,7 @@ obj-y                 := filemap.o mempool.o oom_kill.o fadvise.o \
410                            mm_init.o percpu.o slab_common.o \
411                            compaction.o vmacache.o \
412                            interval_tree.o list_lru.o workingset.o \
413 -                          debug.o gup.o mmap_lock.o $(mmu-y)
414 +                          prfile.o debug.o gup.o mmap_lock.o $(mmu-y)
415  
416  # Give 'page_alloc' its own module-parameter namespace
417  page-alloc-y := page_alloc.o
418 diff --git a/mm/filemap.c b/mm/filemap.c
419 index ad8c39d90bf94..f95b595a49b7b 100644
420 --- a/mm/filemap.c
421 +++ b/mm/filemap.c
422 @@ -3395,7 +3395,7 @@ vm_fault_t filemap_page_mkwrite(struct vm_fault *vmf)
423         vm_fault_t ret = VM_FAULT_LOCKED;
424  
425         sb_start_pagefault(mapping->host->i_sb);
426 -       file_update_time(vmf->vma->vm_file);
427 +       vma_file_update_time(vmf->vma);
428         folio_lock(folio);
429         if (folio->mapping != mapping) {
430                 folio_unlock(folio);
431 diff --git a/mm/mmap.c b/mm/mmap.c
432 index f61a15474dd6d..071c0cd6ef623 100644
433 --- a/mm/mmap.c
434 +++ b/mm/mmap.c
435 @@ -184,7 +184,7 @@ static struct vm_area_struct *remove_vma(struct vm_area_struct *vma)
436         if (vma->vm_ops && vma->vm_ops->close)
437                 vma->vm_ops->close(vma);
438         if (vma->vm_file)
439 -               fput(vma->vm_file);
440 +               vma_fput(vma);
441         mpol_put(vma_policy(vma));
442         vm_area_free(vma);
443         return next;
444 @@ -953,7 +953,7 @@ int __vma_adjust(struct vm_area_struct *vma, unsigned long start,
445         if (remove_next) {
446                 if (file) {
447                         uprobe_munmap(next, next->vm_start, next->vm_end);
448 -                       fput(file);
449 +                       vma_fput(vma);
450                 }
451                 if (next->anon_vma)
452                         anon_vma_merge(vma, next);
453 @@ -1879,7 +1879,7 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
454         return addr;
455  
456  unmap_and_free_vma:
457 -       fput(vma->vm_file);
458 +       vma_fput(vma);
459         vma->vm_file = NULL;
460  
461         /* Undo any partial mapping done by a device driver. */
462 @@ -2737,7 +2737,7 @@ int __split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
463                 goto out_free_mpol;
464  
465         if (new->vm_file)
466 -               get_file(new->vm_file);
467 +               vma_get_file(new);
468  
469         if (new->vm_ops && new->vm_ops->open)
470                 new->vm_ops->open(new);
471 @@ -2756,7 +2756,7 @@ int __split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
472         if (new->vm_ops && new->vm_ops->close)
473                 new->vm_ops->close(new);
474         if (new->vm_file)
475 -               fput(new->vm_file);
476 +               vma_fput(new);
477         unlink_anon_vmas(new);
478   out_free_mpol:
479         mpol_put(vma_policy(new));
480 @@ -2950,7 +2950,7 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
481         struct vm_area_struct *vma;
482         unsigned long populate = 0;
483         unsigned long ret = -EINVAL;
484 -       struct file *file;
485 +       struct file *file, *prfile;
486  
487         pr_warn_once("%s (%d) uses deprecated remap_file_pages() syscall. See Documentation/vm/remap_file_pages.rst.\n",
488                      current->comm, current->pid);
489 @@ -3006,10 +3006,27 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
490         if (vma->vm_flags & VM_LOCKED)
491                 flags |= MAP_LOCKED;
492  
493 -       file = get_file(vma->vm_file);
494 +       vma_get_file(vma);
495 +       file = vma->vm_file;
496 +       prfile = vma->vm_prfile;
497         ret = do_mmap(vma->vm_file, start, size,
498                         prot, flags, pgoff, &populate, NULL);
499 +       if (!IS_ERR_VALUE(ret) && file && prfile) {
500 +               struct vm_area_struct *new_vma;
501 +
502 +               new_vma = find_vma(mm, ret);
503 +               if (!new_vma->vm_prfile)
504 +                       new_vma->vm_prfile = prfile;
505 +               if (new_vma != vma)
506 +                       get_file(prfile);
507 +       }
508 +       /*
509 +        * two fput()s instead of vma_fput(vma),
510 +        * coz vma may not be available anymore.
511 +        */
512         fput(file);
513 +       if (prfile)
514 +               fput(prfile);
515  out:
516         mmap_write_unlock(mm);
517         if (populate)
518 @@ -3291,7 +3308,7 @@ struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,
519                 if (anon_vma_clone(new_vma, vma))
520                         goto out_free_mempol;
521                 if (new_vma->vm_file)
522 -                       get_file(new_vma->vm_file);
523 +                       vma_get_file(new_vma);
524                 if (new_vma->vm_ops && new_vma->vm_ops->open)
525                         new_vma->vm_ops->open(new_vma);
526                 vma_link(mm, new_vma, prev, rb_link, rb_parent);
527 diff --git a/mm/nommu.c b/mm/nommu.c
528 index 55a9e48a7a026..8dc77ce96335a 100644
529 --- a/mm/nommu.c
530 +++ b/mm/nommu.c
531 @@ -522,7 +522,7 @@ static void __put_nommu_region(struct vm_region *region)
532                 up_write(&nommu_region_sem);
533  
534                 if (region->vm_file)
535 -                       fput(region->vm_file);
536 +                       vmr_fput(region);
537  
538                 /* IO memory and memory shared directly out of the pagecache
539                  * from ramfs/tmpfs mustn't be released here */
540 @@ -654,7 +654,7 @@ static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma)
541         if (vma->vm_ops && vma->vm_ops->close)
542                 vma->vm_ops->close(vma);
543         if (vma->vm_file)
544 -               fput(vma->vm_file);
545 +               vma_fput(vma);
546         put_nommu_region(vma->vm_region);
547         vm_area_free(vma);
548  }
549 @@ -1174,7 +1174,7 @@ unsigned long do_mmap(struct file *file,
550                                         goto error_just_free;
551                                 }
552                         }
553 -                       fput(region->vm_file);
554 +                       vmr_fput(region);
555                         kmem_cache_free(vm_region_jar, region);
556                         region = pregion;
557                         result = start;
558 @@ -1251,10 +1251,10 @@ unsigned long do_mmap(struct file *file,
559         up_write(&nommu_region_sem);
560  error:
561         if (region->vm_file)
562 -               fput(region->vm_file);
563 +               vmr_fput(region);
564         kmem_cache_free(vm_region_jar, region);
565         if (vma->vm_file)
566 -               fput(vma->vm_file);
567 +               vma_fput(vma);
568         vm_area_free(vma);
569         return ret;
570  
571 diff --git a/mm/prfile.c b/mm/prfile.c
572 new file mode 100644
573 index 0000000000000..511543ab1b418
574 --- /dev/null
575 +++ b/mm/prfile.c
576 @@ -0,0 +1,86 @@
577 +// SPDX-License-Identifier: GPL-2.0
578 +/*
579 + * Mainly for aufs which mmap(2) different file and wants to print different
580 + * path in /proc/PID/maps.
581 + * Call these functions via macros defined in linux/mm.h.
582 + *
583 + * See Documentation/filesystems/aufs/design/06mmap.txt
584 + *
585 + * Copyright (c) 2014-2021 Junjro R. Okajima
586 + * Copyright (c) 2014 Ian Campbell
587 + */
588 +
589 +#include <linux/mm.h>
590 +#include <linux/file.h>
591 +#include <linux/fs.h>
592 +
593 +/* #define PRFILE_TRACE */
594 +static inline void prfile_trace(struct file *f, struct file *pr,
595 +                             const char func[], int line, const char func2[])
596 +{
597 +#ifdef PRFILE_TRACE
598 +       if (pr)
599 +               pr_info("%s:%d: %s, %pD2\n", func, line, func2, f);
600 +#endif
601 +}
602 +
603 +void vma_do_file_update_time(struct vm_area_struct *vma, const char func[],
604 +                            int line)
605 +{
606 +       struct file *f = vma->vm_file, *pr = vma->vm_prfile;
607 +
608 +       prfile_trace(f, pr, func, line, __func__);
609 +       file_update_time(f);
610 +       if (f && pr)
611 +               file_update_time(pr);
612 +}
613 +
614 +struct file *vma_do_pr_or_file(struct vm_area_struct *vma, const char func[],
615 +                              int line)
616 +{
617 +       struct file *f = vma->vm_file, *pr = vma->vm_prfile;
618 +
619 +       prfile_trace(f, pr, func, line, __func__);
620 +       return (f && pr) ? pr : f;
621 +}
622 +
623 +void vma_do_get_file(struct vm_area_struct *vma, const char func[], int line)
624 +{
625 +       struct file *f = vma->vm_file, *pr = vma->vm_prfile;
626 +
627 +       prfile_trace(f, pr, func, line, __func__);
628 +       get_file(f);
629 +       if (f && pr)
630 +               get_file(pr);
631 +}
632 +
633 +void vma_do_fput(struct vm_area_struct *vma, const char func[], int line)
634 +{
635 +       struct file *f = vma->vm_file, *pr = vma->vm_prfile;
636 +
637 +       prfile_trace(f, pr, func, line, __func__);
638 +       fput(f);
639 +       if (f && pr)
640 +               fput(pr);
641 +}
642 +
643 +#ifndef CONFIG_MMU
644 +struct file *vmr_do_pr_or_file(struct vm_region *region, const char func[],
645 +                              int line)
646 +{
647 +       struct file *f = region->vm_file, *pr = region->vm_prfile;
648 +
649 +       prfile_trace(f, pr, func, line, __func__);
650 +       return (f && pr) ? pr : f;
651 +}
652 +
653 +void vmr_do_fput(struct vm_region *region, const char func[], int line)
654 +{
655 +       struct file *f = region->vm_file, *pr = region->vm_prfile;
656 +
657 +       prfile_trace(f, pr, func, line, __func__);
658 +       fput(f);
659 +       if (f && pr)
660 +               fput(pr);
661 +}
662 +#endif /* !CONFIG_MMU */
663 SPDX-License-Identifier: GPL-2.0
664 aufs5.x-rcN standalone patch
665
666 diff --git a/fs/dcache.c b/fs/dcache.c
667 index 6f4f48c828351..7ce9e4401f08a 100644
668 --- a/fs/dcache.c
669 +++ b/fs/dcache.c
670 @@ -1450,6 +1450,7 @@ void d_walk(struct dentry *parent, void *data,
671         seq = 1;
672         goto again;
673  }
674 +EXPORT_SYMBOL_GPL(d_walk);
675  
676  struct check_mount {
677         struct vfsmount *mnt;
678 @@ -2995,6 +2996,7 @@ void d_exchange(struct dentry *dentry1, struct dentry *dentry2)
679  
680         write_sequnlock(&rename_lock);
681  }
682 +EXPORT_SYMBOL_GPL(d_exchange);
683  
684  /**
685   * d_ancestor - search for an ancestor
686 diff --git a/fs/exec.c b/fs/exec.c
687 index 79f2c9483302d..d9bc22409be6a 100644
688 --- a/fs/exec.c
689 +++ b/fs/exec.c
690 @@ -112,6 +112,7 @@ bool path_noexec(const struct path *path)
691         return (path->mnt->mnt_flags & MNT_NOEXEC) ||
692                (path->mnt->mnt_sb->s_iflags & SB_I_NOEXEC);
693  }
694 +EXPORT_SYMBOL_GPL(path_noexec);
695  
696  #ifdef CONFIG_USELIB
697  /*
698 diff --git a/fs/fcntl.c b/fs/fcntl.c
699 index 02382fa9bd341..3418c60b90146 100644
700 --- a/fs/fcntl.c
701 +++ b/fs/fcntl.c
702 @@ -86,6 +86,7 @@ int setfl(int fd, struct file *filp, unsigned long arg)
703   out:
704         return error;
705  }
706 +EXPORT_SYMBOL_GPL(setfl);
707  
708  static void f_modown(struct file *filp, struct pid *pid, enum pid_type type,
709                       int force)
710 diff --git a/fs/file_table.c b/fs/file_table.c
711 index 7d2e692b66a94..91942789c49aa 100644
712 --- a/fs/file_table.c
713 +++ b/fs/file_table.c
714 @@ -198,6 +198,7 @@ struct file *alloc_empty_file(int flags, const struct cred *cred)
715         }
716         return ERR_PTR(-ENFILE);
717  }
718 +EXPORT_SYMBOL_GPL(alloc_empty_file);
719  
720  /*
721   * Variant of alloc_empty_file() that doesn't check and modify nr_files.
722 diff --git a/fs/namespace.c b/fs/namespace.c
723 index e189393b34b14..5d7d122d1067a 100644
724 --- a/fs/namespace.c
725 +++ b/fs/namespace.c
726 @@ -440,6 +440,7 @@ void __mnt_drop_write(struct vfsmount *mnt)
727         mnt_dec_writers(real_mount(mnt));
728         preempt_enable();
729  }
730 +EXPORT_SYMBOL_GPL(__mnt_drop_write);
731  
732  /**
733   * mnt_drop_write - give up write access to a mount
734 @@ -844,6 +845,7 @@ int is_current_mnt_ns(struct vfsmount *mnt)
735  {
736         return check_mnt(real_mount(mnt));
737  }
738 +EXPORT_SYMBOL_GPL(is_current_mnt_ns);
739  
740  /*
741   * vfsmount lock must be held for write
742 @@ -2047,6 +2049,7 @@ int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg,
743         }
744         return 0;
745  }
746 +EXPORT_SYMBOL_GPL(iterate_mounts);
747  
748  static void lock_mnt_tree(struct mount *mnt)
749  {
750 diff --git a/fs/notify/group.c b/fs/notify/group.c
751 index b7d4d64f87c29..2d2d2c6d33e41 100644
752 --- a/fs/notify/group.c
753 +++ b/fs/notify/group.c
754 @@ -100,6 +100,7 @@ void fsnotify_get_group(struct fsnotify_group *group)
755  {
756         refcount_inc(&group->refcnt);
757  }
758 +EXPORT_SYMBOL_GPL(fsnotify_get_group);
759  
760  /*
761   * Drop a reference to a group.  Free it if it's through.
762 diff --git a/fs/open.c b/fs/open.c
763 index 9ff2f621b760b..3b8deceee506f 100644
764 --- a/fs/open.c
765 +++ b/fs/open.c
766 @@ -66,6 +66,7 @@ int do_truncate(struct user_namespace *mnt_userns, struct dentry *dentry,
767         inode_unlock(dentry->d_inode);
768         return ret;
769  }
770 +EXPORT_SYMBOL_GPL(do_truncate);
771  
772  long vfs_truncate(const struct path *path, loff_t length)
773  {
774 diff --git a/fs/read_write.c b/fs/read_write.c
775 index 0074afa7ecb3e..612cf04d9f6b9 100644
776 --- a/fs/read_write.c
777 +++ b/fs/read_write.c
778 @@ -488,6 +488,7 @@ ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos)
779         inc_syscr(current);
780         return ret;
781  }
782 +EXPORT_SYMBOL_GPL(vfs_read);
783  
784  static ssize_t new_sync_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos)
785  {
786 @@ -598,6 +599,7 @@ ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_
787         file_end_write(file);
788         return ret;
789  }
790 +EXPORT_SYMBOL_GPL(vfs_write);
791  
792  /* file_ppos returns &file->f_pos or NULL if file is stream */
793  static inline loff_t *file_ppos(struct file *file)
794 diff --git a/fs/splice.c b/fs/splice.c
795 index 3e6ba363b7775..7c1be373eb7cd 100644
796 --- a/fs/splice.c
797 +++ b/fs/splice.c
798 @@ -766,6 +766,7 @@ long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
799                 return warn_unsupported(out, "write");
800         return out->f_op->splice_write(pipe, out, ppos, len, flags);
801  }
802 +EXPORT_SYMBOL_GPL(do_splice_from);
803  
804  /*
805   * Attempt to initiate a splice from a file to a pipe.
806 @@ -795,6 +796,7 @@ long do_splice_to(struct file *in, loff_t *ppos,
807                 return warn_unsupported(in, "read");
808         return in->f_op->splice_read(in, ppos, pipe, len, flags);
809  }
810 +EXPORT_SYMBOL_GPL(do_splice_to);
811  
812  /**
813   * splice_direct_to_actor - splices data directly between two non-pipes
814 diff --git a/fs/xattr.c b/fs/xattr.c
815 index 5c8c5175b385c..ff7e9ff774b73 100644
816 --- a/fs/xattr.c
817 +++ b/fs/xattr.c
818 @@ -384,6 +384,7 @@ vfs_getxattr_alloc(struct user_namespace *mnt_userns, struct dentry *dentry,
819         *xattr_value = value;
820         return error;
821  }
822 +EXPORT_SYMBOL_GPL(vfs_getxattr_alloc);
823  
824  ssize_t
825  __vfs_getxattr(struct dentry *dentry, struct inode *inode, const char *name,
826 diff --git a/kernel/locking/lockdep.c b/kernel/locking/lockdep.c
827 index 43e3d1fdd570b..5d6f33f1f2f28 100644
828 --- a/kernel/locking/lockdep.c
829 +++ b/kernel/locking/lockdep.c
830 @@ -210,6 +210,7 @@ inline struct lock_class *lockdep_hlock_class(struct held_lock *hlock)
831          */
832         return lock_classes + class_idx;
833  }
834 +EXPORT_SYMBOL_GPL(lockdep_hlock_class);
835  #define hlock_class(hlock) lockdep_hlock_class(hlock)
836  
837  #ifdef CONFIG_LOCK_STAT
838 diff --git a/kernel/task_work.c b/kernel/task_work.c
839 index 1698fbe6f0e13..081b05acadf82 100644
840 --- a/kernel/task_work.c
841 +++ b/kernel/task_work.c
842 @@ -167,3 +167,4 @@ void task_work_run(void)
843                 } while (work);
844         }
845  }
846 +EXPORT_SYMBOL_GPL(task_work_run);
847 diff --git a/security/security.c b/security/security.c
848 index 22261d79f3333..732e4c219ea29 100644
849 --- a/security/security.c
850 +++ b/security/security.c
851 @@ -1151,6 +1151,7 @@ int security_path_rmdir(const struct path *dir, struct dentry *dentry)
852                 return 0;
853         return call_int_hook(path_rmdir, 0, dir, dentry);
854  }
855 +EXPORT_SYMBOL_GPL(security_path_rmdir);
856  
857  int security_path_unlink(const struct path *dir, struct dentry *dentry)
858  {
859 @@ -1167,6 +1168,7 @@ int security_path_symlink(const struct path *dir, struct dentry *dentry,
860                 return 0;
861         return call_int_hook(path_symlink, 0, dir, dentry, old_name);
862  }
863 +EXPORT_SYMBOL_GPL(security_path_symlink);
864  
865  int security_path_link(struct dentry *old_dentry, const struct path *new_dir,
866                        struct dentry *new_dentry)
867 @@ -1175,6 +1177,7 @@ int security_path_link(struct dentry *old_dentry, const struct path *new_dir,
868                 return 0;
869         return call_int_hook(path_link, 0, old_dentry, new_dir, new_dentry);
870  }
871 +EXPORT_SYMBOL_GPL(security_path_link);
872  
873  int security_path_rename(const struct path *old_dir, struct dentry *old_dentry,
874                          const struct path *new_dir, struct dentry *new_dentry,
875 @@ -1202,6 +1205,7 @@ int security_path_truncate(const struct path *path)
876                 return 0;
877         return call_int_hook(path_truncate, 0, path);
878  }
879 +EXPORT_SYMBOL_GPL(security_path_truncate);
880  
881  int security_path_chmod(const struct path *path, umode_t mode)
882  {
883 @@ -1209,6 +1213,7 @@ int security_path_chmod(const struct path *path, umode_t mode)
884                 return 0;
885         return call_int_hook(path_chmod, 0, path, mode);
886  }
887 +EXPORT_SYMBOL_GPL(security_path_chmod);
888  
889  int security_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
890  {
891 @@ -1216,6 +1221,7 @@ int security_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
892                 return 0;
893         return call_int_hook(path_chown, 0, path, uid, gid);
894  }
895 +EXPORT_SYMBOL_GPL(security_path_chown);
896  
897  int security_path_chroot(const struct path *path)
898  {
899 @@ -1316,6 +1322,7 @@ int security_inode_permission(struct inode *inode, int mask)
900                 return 0;
901         return call_int_hook(inode_permission, 0, inode, mask);
902  }
903 +EXPORT_SYMBOL_GPL(security_inode_permission);
904  
905  int security_inode_setattr(struct dentry *dentry, struct iattr *attr)
906  {
907 @@ -1513,6 +1520,7 @@ int security_file_permission(struct file *file, int mask)
908  
909         return fsnotify_perm(file, mask);
910  }
911 +EXPORT_SYMBOL_GPL(security_file_permission);
912  
913  int security_file_alloc(struct file *file)
914  {
915 diff -urN /usr/share/empty/Documentation/ABI/testing/debugfs-aufs linux/Documentation/ABI/testing/debugfs-aufs
916 --- /usr/share/empty/Documentation/ABI/testing/debugfs-aufs     1970-01-01 01:00:00.000000000 +0100
917 +++ linux/Documentation/ABI/testing/debugfs-aufs        2022-03-21 14:49:05.713299677 +0100
918 @@ -0,0 +1,55 @@
919 +What:          /debug/aufs/si_<id>/
920 +Date:          March 2009
921 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
922 +Description:
923 +               Under /debug/aufs, a directory named si_<id> is created
924 +               per aufs mount, where <id> is a unique id generated
925 +               internally.
926 +
927 +What:          /debug/aufs/si_<id>/plink
928 +Date:          Apr 2013
929 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
930 +Description:
931 +               It has three lines and shows the information about the
932 +               pseudo-link. The first line is a single number
933 +               representing a number of buckets. The second line is a
934 +               number of pseudo-links per buckets (separated by a
935 +               blank). The last line is a single number representing a
936 +               total number of psedo-links.
937 +               When the aufs mount option 'noplink' is specified, it
938 +               will show "1\n0\n0\n".
939 +
940 +What:          /debug/aufs/si_<id>/xib
941 +Date:          March 2009
942 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
943 +Description:
944 +               It shows the consumed blocks by xib (External Inode Number
945 +               Bitmap), its block size and file size.
946 +               When the aufs mount option 'noxino' is specified, it
947 +               will be empty. About XINO files, see the aufs manual.
948 +
949 +What:          /debug/aufs/si_<id>/xi<branch-index>
950 +Date:          March 2009
951 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
952 +Description:
953 +               It shows the consumed blocks by xino (External Inode Number
954 +               Translation Table), its link count, block size and file
955 +               size.
956 +               Due to the file size limit, there may exist multiple
957 +               xino files per branch.  In this case, "-N" is added to
958 +               the filename and it corresponds to the index of the
959 +               internal xino array.  "-0" is omitted.
960 +               When the aufs mount option 'noxino' is specified, Those
961 +               entries won't exist.  About XINO files, see the aufs
962 +               manual.
963 +
964 +What:          /debug/aufs/si_<id>/xigen
965 +Date:          March 2009
966 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
967 +Description:
968 +               It shows the consumed blocks by xigen (External Inode
969 +               Generation Table), its block size and file size.
970 +               If CONFIG_AUFS_EXPORT is disabled, this entry will not
971 +               be created.
972 +               When the aufs mount option 'noxino' is specified, it
973 +               will be empty. About XINO files, see the aufs manual.
974 diff -urN /usr/share/empty/Documentation/ABI/testing/sysfs-aufs linux/Documentation/ABI/testing/sysfs-aufs
975 --- /usr/share/empty/Documentation/ABI/testing/sysfs-aufs       1970-01-01 01:00:00.000000000 +0100
976 +++ linux/Documentation/ABI/testing/sysfs-aufs  2022-03-21 14:49:05.713299677 +0100
977 @@ -0,0 +1,31 @@
978 +What:          /sys/fs/aufs/si_<id>/
979 +Date:          March 2009
980 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
981 +Description:
982 +               Under /sys/fs/aufs, a directory named si_<id> is created
983 +               per aufs mount, where <id> is a unique id generated
984 +               internally.
985 +
986 +What:          /sys/fs/aufs/si_<id>/br<idx>
987 +Date:          March 2009
988 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
989 +Description:
990 +               It shows the abolute path of a member directory (which
991 +               is called branch) in aufs, and its permission.
992 +
993 +What:          /sys/fs/aufs/si_<id>/brid<idx>
994 +Date:          July 2013
995 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
996 +Description:
997 +               It shows the id of a member directory (which is called
998 +               branch) in aufs.
999 +
1000 +What:          /sys/fs/aufs/si_<id>/xi_path
1001 +Date:          March 2009
1002 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1003 +Description:
1004 +               It shows the abolute path of XINO (External Inode Number
1005 +               Bitmap, Translation Table and Generation Table) file
1006 +               even if it is the default path.
1007 +               When the aufs mount option 'noxino' is specified, it
1008 +               will be empty. About XINO files, see the aufs manual.
1009 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/01intro.txt linux/Documentation/filesystems/aufs/design/01intro.txt
1010 --- /usr/share/empty/Documentation/filesystems/aufs/design/01intro.txt  1970-01-01 01:00:00.000000000 +0100
1011 +++ linux/Documentation/filesystems/aufs/design/01intro.txt     2022-03-21 14:49:05.713299677 +0100
1012 @@ -0,0 +1,171 @@
1013 +
1014 +# Copyright (C) 2005-2021 Junjiro R. Okajima
1015 +# 
1016 +# This program is free software; you can redistribute it and/or modify
1017 +# it under the terms of the GNU General Public License as published by
1018 +# the Free Software Foundation; either version 2 of the License, or
1019 +# (at your option) any later version.
1020 +# 
1021 +# This program is distributed in the hope that it will be useful,
1022 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1023 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1024 +# GNU General Public License for more details.
1025 +# 
1026 +# You should have received a copy of the GNU General Public License
1027 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1028 +
1029 +Introduction
1030 +----------------------------------------
1031 +
1032 +aufs [ei ju: ef es] | /ey-yoo-ef-es/ | [a u f s]
1033 +1. abbrev. for "advanced multi-layered unification filesystem".
1034 +2. abbrev. for "another unionfs".
1035 +3. abbrev. for "auf das" in German which means "on the" in English.
1036 +   Ex. "Butter aufs Brot"(G) means "butter onto bread"(E).
1037 +   But "Filesystem aufs Filesystem" is hard to understand.
1038 +4. abbrev. for "African Urban Fashion Show".
1039 +
1040 +AUFS is a filesystem with features:
1041 +- multi layered stackable unification filesystem, the member directory
1042 +  is called as a branch.
1043 +- branch permission and attribute, 'readonly', 'real-readonly',
1044 +  'readwrite', 'whiteout-able', 'link-able whiteout', etc. and their
1045 +  combination.
1046 +- internal "file copy-on-write".
1047 +- logical deletion, whiteout.
1048 +- dynamic branch manipulation, adding, deleting and changing permission.
1049 +- allow bypassing aufs, user's direct branch access.
1050 +- external inode number translation table and bitmap which maintains the
1051 +  persistent aufs inode number.
1052 +- seekable directory, including NFS readdir.
1053 +- file mapping, mmap and sharing pages.
1054 +- pseudo-link, hardlink over branches.
1055 +- loopback mounted filesystem as a branch.
1056 +- several policies to select one among multiple writable branches.
1057 +- revert a single systemcall when an error occurs in aufs.
1058 +- and more...
1059 +
1060 +
1061 +Multi Layered Stackable Unification Filesystem
1062 +----------------------------------------------------------------------
1063 +Most people already knows what it is.
1064 +It is a filesystem which unifies several directories and provides a
1065 +merged single directory. When users access a file, the access will be
1066 +passed/re-directed/converted (sorry, I am not sure which English word is
1067 +correct) to the real file on the member filesystem. The member
1068 +filesystem is called 'lower filesystem' or 'branch' and has a mode
1069 +'readonly' and 'readwrite.' And the deletion for a file on the lower
1070 +readonly branch is handled by creating 'whiteout' on the upper writable
1071 +branch.
1072 +
1073 +On LKML, there have been discussions about UnionMount (Jan Blunck,
1074 +Bharata B Rao and Valerie Aurora) and Unionfs (Erez Zadok). They took
1075 +different approaches to implement the merged-view.
1076 +The former tries putting it into VFS, and the latter implements as a
1077 +separate filesystem.
1078 +(If I misunderstand about these implementations, please let me know and
1079 +I shall correct it. Because it is a long time ago when I read their
1080 +source files last time).
1081 +
1082 +UnionMount's approach will be able to small, but may be hard to share
1083 +branches between several UnionMount since the whiteout in it is
1084 +implemented in the inode on branch filesystem and always
1085 +shared. According to Bharata's post, readdir does not seems to be
1086 +finished yet.
1087 +There are several missing features known in this implementations such as
1088 +- for users, the inode number may change silently. eg. copy-up.
1089 +- link(2) may break by copy-up.
1090 +- read(2) may get an obsoleted filedata (fstat(2) too).
1091 +- fcntl(F_SETLK) may be broken by copy-up.
1092 +- unnecessary copy-up may happen, for example mmap(MAP_PRIVATE) after
1093 +  open(O_RDWR).
1094 +
1095 +In linux-3.18, "overlay" filesystem (formerly known as "overlayfs") was
1096 +merged into mainline. This is another implementation of UnionMount as a
1097 +separated filesystem. All the limitations and known problems which
1098 +UnionMount are equally inherited to "overlay" filesystem.
1099 +
1100 +Unionfs has a longer history. When I started implementing a stackable
1101 +filesystem (Aug 2005), it already existed. It has virtual super_block,
1102 +inode, dentry and file objects and they have an array pointing lower
1103 +same kind objects. After contributing many patches for Unionfs, I
1104 +re-started my project AUFS (Jun 2006).
1105 +
1106 +In AUFS, the structure of filesystem resembles to Unionfs, but I
1107 +implemented my own ideas, approaches and enhancements and it became
1108 +totally different one.
1109 +
1110 +Comparing DM snapshot and fs based implementation
1111 +- the number of bytes to be copied between devices is much smaller.
1112 +- the type of filesystem must be one and only.
1113 +- the fs must be writable, no readonly fs, even for the lower original
1114 +  device. so the compression fs will not be usable. but if we use
1115 +  loopback mount, we may address this issue.
1116 +  for instance,
1117 +       mount /cdrom/squashfs.img /sq
1118 +       losetup /sq/ext2.img
1119 +       losetup /somewhere/cow
1120 +       dmsetup "snapshot /dev/loop0 /dev/loop1 ..."
1121 +- it will be difficult (or needs more operations) to extract the
1122 +  difference between the original device and COW.
1123 +- DM snapshot-merge may help a lot when users try merging. in the
1124 +  fs-layer union, users will use rsync(1).
1125 +
1126 +You may want to read my old paper "Filesystems in LiveCD"
1127 +(http://aufs.sourceforge.net/aufs2/report/sq/sq.pdf).
1128 +
1129 +
1130 +Several characters/aspects/persona of aufs
1131 +----------------------------------------------------------------------
1132 +
1133 +Aufs has several characters, aspects or persona.
1134 +1. a filesystem, callee of VFS helper
1135 +2. sub-VFS, caller of VFS helper for branches
1136 +3. a virtual filesystem which maintains persistent inode number
1137 +4. reader/writer of files on branches such like an application
1138 +
1139 +1. Callee of VFS Helper
1140 +As an ordinary linux filesystem, aufs is a callee of VFS. For instance,
1141 +unlink(2) from an application reaches sys_unlink() kernel function and
1142 +then vfs_unlink() is called. vfs_unlink() is one of VFS helper and it
1143 +calls filesystem specific unlink operation. Actually aufs implements the
1144 +unlink operation but it behaves like a redirector.
1145 +
1146 +2. Caller of VFS Helper for Branches
1147 +aufs_unlink() passes the unlink request to the branch filesystem as if
1148 +it were called from VFS. So the called unlink operation of the branch
1149 +filesystem acts as usual. As a caller of VFS helper, aufs should handle
1150 +every necessary pre/post operation for the branch filesystem.
1151 +- acquire the lock for the parent dir on a branch
1152 +- lookup in a branch
1153 +- revalidate dentry on a branch
1154 +- mnt_want_write() for a branch
1155 +- vfs_unlink() for a branch
1156 +- mnt_drop_write() for a branch
1157 +- release the lock on a branch
1158 +
1159 +3. Persistent Inode Number
1160 +One of the most important issue for a filesystem is to maintain inode
1161 +numbers. This is particularly important to support exporting a
1162 +filesystem via NFS. Aufs is a virtual filesystem which doesn't have a
1163 +backend block device for its own. But some storage is necessary to
1164 +keep and maintain the inode numbers. It may be a large space and may not
1165 +suit to keep in memory. Aufs rents some space from its first writable
1166 +branch filesystem (by default) and creates file(s) on it. These files
1167 +are created by aufs internally and removed soon (currently) keeping
1168 +opened.
1169 +Note: Because these files are removed, they are totally gone after
1170 +      unmounting aufs. It means the inode numbers are not persistent
1171 +      across unmount or reboot. I have a plan to make them really
1172 +      persistent which will be important for aufs on NFS server.
1173 +
1174 +4. Read/Write Files Internally (copy-on-write)
1175 +Because a branch can be readonly, when you write a file on it, aufs will
1176 +"copy-up" it to the upper writable branch internally. And then write the
1177 +originally requested thing to the file. Generally kernel doesn't
1178 +open/read/write file actively. In aufs, even a single write may cause a
1179 +internal "file copy". This behaviour is very similar to cp(1) command.
1180 +
1181 +Some people may think it is better to pass such work to user space
1182 +helper, instead of doing in kernel space. Actually I am still thinking
1183 +about it. But currently I have implemented it in kernel space.
1184 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/02struct.txt linux/Documentation/filesystems/aufs/design/02struct.txt
1185 --- /usr/share/empty/Documentation/filesystems/aufs/design/02struct.txt 1970-01-01 01:00:00.000000000 +0100
1186 +++ linux/Documentation/filesystems/aufs/design/02struct.txt    2022-03-21 14:49:05.713299677 +0100
1187 @@ -0,0 +1,258 @@
1188 +
1189 +# Copyright (C) 2005-2021 Junjiro R. Okajima
1190 +# 
1191 +# This program is free software; you can redistribute it and/or modify
1192 +# it under the terms of the GNU General Public License as published by
1193 +# the Free Software Foundation; either version 2 of the License, or
1194 +# (at your option) any later version.
1195 +# 
1196 +# This program is distributed in the hope that it will be useful,
1197 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1198 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1199 +# GNU General Public License for more details.
1200 +# 
1201 +# You should have received a copy of the GNU General Public License
1202 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1203 +
1204 +Basic Aufs Internal Structure
1205 +
1206 +Superblock/Inode/Dentry/File Objects
1207 +----------------------------------------------------------------------
1208 +As like an ordinary filesystem, aufs has its own
1209 +superblock/inode/dentry/file objects. All these objects have a
1210 +dynamically allocated array and store the same kind of pointers to the
1211 +lower filesystem, branch.
1212 +For example, when you build a union with one readwrite branch and one
1213 +readonly, mounted /au, /rw and /ro respectively.
1214 +- /au = /rw + /ro
1215 +- /ro/fileA exists but /rw/fileA
1216 +
1217 +Aufs lookup operation finds /ro/fileA and gets dentry for that. These
1218 +pointers are stored in a aufs dentry. The array in aufs dentry will be,
1219 +- [0] = NULL (because /rw/fileA doesn't exist)
1220 +- [1] = /ro/fileA
1221 +
1222 +This style of an array is essentially same to the aufs
1223 +superblock/inode/dentry/file objects.
1224 +
1225 +Because aufs supports manipulating branches, ie. add/delete/change
1226 +branches dynamically, these objects has its own generation. When
1227 +branches are changed, the generation in aufs superblock is
1228 +incremented. And a generation in other object are compared when it is
1229 +accessed. When a generation in other objects are obsoleted, aufs
1230 +refreshes the internal array.
1231 +
1232 +
1233 +Superblock
1234 +----------------------------------------------------------------------
1235 +Additionally aufs superblock has some data for policies to select one
1236 +among multiple writable branches, XIB files, pseudo-links and kobject.
1237 +See below in detail.
1238 +About the policies which supports copy-down a directory, see
1239 +wbr_policy.txt too.
1240 +
1241 +
1242 +Branch and XINO(External Inode Number Translation Table)
1243 +----------------------------------------------------------------------
1244 +Every branch has its own xino (external inode number translation table)
1245 +file. The xino file is created and unlinked by aufs internally. When two
1246 +members of a union exist on the same filesystem, they share the single
1247 +xino file.
1248 +The struct of a xino file is simple, just a sequence of aufs inode
1249 +numbers which is indexed by the lower inode number.
1250 +In the above sample, assume the inode number of /ro/fileA is i111 and
1251 +aufs assigns the inode number i999 for fileA. Then aufs writes 999 as
1252 +4(8) bytes at 111 * 4(8) bytes offset in the xino file.
1253 +
1254 +When the inode numbers are not contiguous, the xino file will be sparse
1255 +which has a hole in it and doesn't consume as much disk space as it
1256 +might appear. If your branch filesystem consumes disk space for such
1257 +holes, then you should specify 'xino=' option at mounting aufs.
1258 +
1259 +Aufs has a mount option to free the disk blocks for such holes in XINO
1260 +files on tmpfs or ramdisk. But it is not so effective actually. If you
1261 +meet a problem of disk shortage due to XINO files, then you should try
1262 +"tmpfs-ino.patch" (and "vfs-ino.patch" too) in aufs4-standalone.git.
1263 +The patch localizes the assignment inumbers per tmpfs-mount and avoid
1264 +the holes in XINO files.
1265 +
1266 +Also a writable branch has three kinds of "whiteout bases". All these
1267 +are existed when the branch is joined to aufs, and their names are
1268 +whiteout-ed doubly, so that users will never see their names in aufs
1269 +hierarchy.
1270 +1. a regular file which will be hardlinked to all whiteouts.
1271 +2. a directory to store a pseudo-link.
1272 +3. a directory to store an "orphan"-ed file temporary.
1273 +
1274 +1. Whiteout Base
1275 +   When you remove a file on a readonly branch, aufs handles it as a
1276 +   logical deletion and creates a whiteout on the upper writable branch
1277 +   as a hardlink of this file in order not to consume inode on the
1278 +   writable branch.
1279 +2. Pseudo-link Dir
1280 +   See below, Pseudo-link.
1281 +3. Step-Parent Dir
1282 +   When "fileC" exists on the lower readonly branch only and it is
1283 +   opened and removed with its parent dir, and then user writes
1284 +   something into it, then aufs copies-up fileC to this
1285 +   directory. Because there is no other dir to store fileC. After
1286 +   creating a file under this dir, the file is unlinked.
1287 +
1288 +Because aufs supports manipulating branches, ie. add/delete/change
1289 +dynamically, a branch has its own id. When the branch order changes,
1290 +aufs finds the new index by searching the branch id.
1291 +
1292 +
1293 +Pseudo-link
1294 +----------------------------------------------------------------------
1295 +Assume "fileA" exists on the lower readonly branch only and it is
1296 +hardlinked to "fileB" on the branch. When you write something to fileA,
1297 +aufs copies-up it to the upper writable branch. Additionally aufs
1298 +creates a hardlink under the Pseudo-link Directory of the writable
1299 +branch. The inode of a pseudo-link is kept in aufs super_block as a
1300 +simple list. If fileB is read after unlinking fileA, aufs returns
1301 +filedata from the pseudo-link instead of the lower readonly
1302 +branch. Because the pseudo-link is based upon the inode, to keep the
1303 +inode number by xino (see above) is essentially necessary.
1304 +
1305 +All the hardlinks under the Pseudo-link Directory of the writable branch
1306 +should be restored in a proper location later. Aufs provides a utility
1307 +to do this. The userspace helpers executed at remounting and unmounting
1308 +aufs by default.
1309 +During this utility is running, it puts aufs into the pseudo-link
1310 +maintenance mode. In this mode, only the process which began the
1311 +maintenance mode (and its child processes) is allowed to operate in
1312 +aufs. Some other processes which are not related to the pseudo-link will
1313 +be allowed to run too, but the rest have to return an error or wait
1314 +until the maintenance mode ends. If a process already acquires an inode
1315 +mutex (in VFS), it has to return an error.
1316 +
1317 +
1318 +XIB(external inode number bitmap)
1319 +----------------------------------------------------------------------
1320 +Addition to the xino file per a branch, aufs has an external inode number
1321 +bitmap in a superblock object. It is also an internal file such like a
1322 +xino file.
1323 +It is a simple bitmap to mark whether the aufs inode number is in-use or
1324 +not.
1325 +To reduce the file I/O, aufs prepares a single memory page to cache xib.
1326 +
1327 +As well as XINO files, aufs has a feature to truncate/refresh XIB to
1328 +reduce the number of consumed disk blocks for these files.
1329 +
1330 +
1331 +Virtual or Vertical Dir, and Readdir in Userspace
1332 +----------------------------------------------------------------------
1333 +In order to support multiple layers (branches), aufs readdir operation
1334 +constructs a virtual dir block on memory. For readdir, aufs calls
1335 +vfs_readdir() internally for each dir on branches, merges their entries
1336 +with eliminating the whiteout-ed ones, and sets it to file (dir)
1337 +object. So the file object has its entry list until it is closed. The
1338 +entry list will be updated when the file position is zero and becomes
1339 +obsoleted. This decision is made in aufs automatically.
1340 +
1341 +The dynamically allocated memory block for the name of entries has a
1342 +unit of 512 bytes (by default) and stores the names contiguously (no
1343 +padding). Another block for each entry is handled by kmem_cache too.
1344 +During building dir blocks, aufs creates hash list and judging whether
1345 +the entry is whiteouted by its upper branch or already listed.
1346 +The merged result is cached in the corresponding inode object and
1347 +maintained by a customizable life-time option.
1348 +
1349 +Some people may call it can be a security hole or invite DoS attack
1350 +since the opened and once readdir-ed dir (file object) holds its entry
1351 +list and becomes a pressure for system memory. But I'd say it is similar
1352 +to files under /proc or /sys. The virtual files in them also holds a
1353 +memory page (generally) while they are opened. When an idea to reduce
1354 +memory for them is introduced, it will be applied to aufs too.
1355 +For those who really hate this situation, I've developed readdir(3)
1356 +library which operates this merging in userspace. You just need to set
1357 +LD_PRELOAD environment variable, and aufs will not consume no memory in
1358 +kernel space for readdir(3).
1359 +
1360 +
1361 +Workqueue
1362 +----------------------------------------------------------------------
1363 +Aufs sometimes requires privilege access to a branch. For instance,
1364 +in copy-up/down operation. When a user process is going to make changes
1365 +to a file which exists in the lower readonly branch only, and the mode
1366 +of one of ancestor directories may not be writable by a user
1367 +process. Here aufs copy-up the file with its ancestors and they may
1368 +require privilege to set its owner/group/mode/etc.
1369 +This is a typical case of a application character of aufs (see
1370 +Introduction).
1371 +
1372 +Aufs uses workqueue synchronously for this case. It creates its own
1373 +workqueue. The workqueue is a kernel thread and has privilege. Aufs
1374 +passes the request to call mkdir or write (for example), and wait for
1375 +its completion. This approach solves a problem of a signal handler
1376 +simply.
1377 +If aufs didn't adopt the workqueue and changed the privilege of the
1378 +process, then the process may receive the unexpected SIGXFSZ or other
1379 +signals.
1380 +
1381 +Also aufs uses the system global workqueue ("events" kernel thread) too
1382 +for asynchronous tasks, such like handling inotify/fsnotify, re-creating a
1383 +whiteout base and etc. This is unrelated to a privilege.
1384 +Most of aufs operation tries acquiring a rw_semaphore for aufs
1385 +superblock at the beginning, at the same time waits for the completion
1386 +of all queued asynchronous tasks.
1387 +
1388 +
1389 +Whiteout
1390 +----------------------------------------------------------------------
1391 +The whiteout in aufs is very similar to Unionfs's. That is represented
1392 +by its filename. UnionMount takes an approach of a file mode, but I am
1393 +afraid several utilities (find(1) or something) will have to support it.
1394 +
1395 +Basically the whiteout represents "logical deletion" which stops aufs to
1396 +lookup further, but also it represents "dir is opaque" which also stop
1397 +further lookup.
1398 +
1399 +In aufs, rmdir(2) and rename(2) for dir uses whiteout alternatively.
1400 +In order to make several functions in a single systemcall to be
1401 +revertible, aufs adopts an approach to rename a directory to a temporary
1402 +unique whiteouted name.
1403 +For example, in rename(2) dir where the target dir already existed, aufs
1404 +renames the target dir to a temporary unique whiteouted name before the
1405 +actual rename on a branch, and then handles other actions (make it opaque,
1406 +update the attributes, etc). If an error happens in these actions, aufs
1407 +simply renames the whiteouted name back and returns an error. If all are
1408 +succeeded, aufs registers a function to remove the whiteouted unique
1409 +temporary name completely and asynchronously to the system global
1410 +workqueue.
1411 +
1412 +
1413 +Copy-up
1414 +----------------------------------------------------------------------
1415 +It is a well-known feature or concept.
1416 +When user modifies a file on a readonly branch, aufs operate "copy-up"
1417 +internally and makes change to the new file on the upper writable branch.
1418 +When the trigger systemcall does not update the timestamps of the parent
1419 +dir, aufs reverts it after copy-up.
1420 +
1421 +
1422 +Move-down (aufs3.9 and later)
1423 +----------------------------------------------------------------------
1424 +"Copy-up" is one of the essential feature in aufs. It copies a file from
1425 +the lower readonly branch to the upper writable branch when a user
1426 +changes something about the file.
1427 +"Move-down" is an opposite action of copy-up. Basically this action is
1428 +ran manually instead of automatically and internally.
1429 +For desgin and implementation, aufs has to consider these issues.
1430 +- whiteout for the file may exist on the lower branch.
1431 +- ancestor directories may not exist on the lower branch.
1432 +- diropq for the ancestor directories may exist on the upper branch.
1433 +- free space on the lower branch will reduce.
1434 +- another access to the file may happen during moving-down, including
1435 +  UDBA (see "Revalidate Dentry and UDBA").
1436 +- the file should not be hard-linked nor pseudo-linked. they should be
1437 +  handled by auplink utility later.
1438 +
1439 +Sometimes users want to move-down a file from the upper writable branch
1440 +to the lower readonly or writable branch. For instance,
1441 +- the free space of the upper writable branch is going to run out.
1442 +- create a new intermediate branch between the upper and lower branch.
1443 +- etc.
1444 +
1445 +For this purpose, use "aumvdown" command in aufs-util.git.
1446 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/03atomic_open.txt linux/Documentation/filesystems/aufs/design/03atomic_open.txt
1447 --- /usr/share/empty/Documentation/filesystems/aufs/design/03atomic_open.txt    1970-01-01 01:00:00.000000000 +0100
1448 +++ linux/Documentation/filesystems/aufs/design/03atomic_open.txt       2022-03-21 14:49:05.713299677 +0100
1449 @@ -0,0 +1,85 @@
1450 +
1451 +# Copyright (C) 2015-2021 Junjiro R. Okajima
1452 +# 
1453 +# This program is free software; you can redistribute it and/or modify
1454 +# it under the terms of the GNU General Public License as published by
1455 +# the Free Software Foundation; either version 2 of the License, or
1456 +# (at your option) any later version.
1457 +# 
1458 +# This program is distributed in the hope that it will be useful,
1459 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1460 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1461 +# GNU General Public License for more details.
1462 +# 
1463 +# You should have received a copy of the GNU General Public License
1464 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1465 +
1466 +Support for a branch who has its ->atomic_open()
1467 +----------------------------------------------------------------------
1468 +The filesystems who implement its ->atomic_open() are not majority. For
1469 +example NFSv4 does, and aufs should call NFSv4 ->atomic_open,
1470 +particularly for open(O_CREAT|O_EXCL, 0400) case. Other than
1471 +->atomic_open(), NFSv4 returns an error for this open(2). While I am not
1472 +sure whether all filesystems who have ->atomic_open() behave like this,
1473 +but NFSv4 surely returns the error.
1474 +
1475 +In order to support ->atomic_open() for aufs, there are a few
1476 +approaches.
1477 +
1478 +A. Introduce aufs_atomic_open()
1479 +   - calls one of VFS:do_last(), lookup_open() or atomic_open() for
1480 +     branch fs.
1481 +B. Introduce aufs_atomic_open() calling create, open and chmod. this is
1482 +   an aufs user Pip Cet's approach
1483 +   - calls aufs_create(), VFS finish_open() and notify_change().
1484 +   - pass fake-mode to finish_open(), and then correct the mode by
1485 +     notify_change().
1486 +C. Extend aufs_open() to call branch fs's ->atomic_open()
1487 +   - no aufs_atomic_open().
1488 +   - aufs_lookup() registers the TID to an aufs internal object.
1489 +   - aufs_create() does nothing when the matching TID is registered, but
1490 +     registers the mode.
1491 +   - aufs_open() calls branch fs's ->atomic_open() when the matching
1492 +     TID is registered.
1493 +D. Extend aufs_open() to re-try branch fs's ->open() with superuser's
1494 +   credential
1495 +   - no aufs_atomic_open().
1496 +   - aufs_create() registers the TID to an internal object. this info
1497 +     represents "this process created this file just now."
1498 +   - when aufs gets EACCES from branch fs's ->open(), then confirm the
1499 +     registered TID and re-try open() with superuser's credential.
1500 +
1501 +Pros and cons for each approach.
1502 +
1503 +A.
1504 +   - straightforward but highly depends upon VFS internal.
1505 +   - the atomic behavaiour is kept.
1506 +   - some of parameters such as nameidata are hard to reproduce for
1507 +     branch fs.
1508 +   - large overhead.
1509 +B.
1510 +   - easy to implement.
1511 +   - the atomic behavaiour is lost.
1512 +C.
1513 +   - the atomic behavaiour is kept.
1514 +   - dirty and tricky.
1515 +   - VFS checks whether the file is created correctly after calling
1516 +     ->create(), which means this approach doesn't work.
1517 +D.
1518 +   - easy to implement.
1519 +   - the atomic behavaiour is lost.
1520 +   - to open a file with superuser's credential and give it to a user
1521 +     process is a bad idea, since the file object keeps the credential
1522 +     in it. It may affect LSM or something. This approach doesn't work
1523 +     either.
1524 +
1525 +The approach A is ideal, but it hard to implement. So here is a
1526 +variation of A, which is to be implemented.
1527 +
1528 +A-1. Introduce aufs_atomic_open()
1529 +     - calls branch fs ->atomic_open() if exists. otherwise calls
1530 +       vfs_create() and finish_open().
1531 +     - the demerit is that the several checks after branch fs
1532 +       ->atomic_open() are lost. in the ordinary case, the checks are
1533 +       done by VFS:do_last(), lookup_open() and atomic_open(). some can
1534 +       be implemented in aufs, but not all I am afraid.
1535 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/03lookup.txt linux/Documentation/filesystems/aufs/design/03lookup.txt
1536 --- /usr/share/empty/Documentation/filesystems/aufs/design/03lookup.txt 1970-01-01 01:00:00.000000000 +0100
1537 +++ linux/Documentation/filesystems/aufs/design/03lookup.txt    2022-03-21 14:49:05.713299677 +0100
1538 @@ -0,0 +1,113 @@
1539 +
1540 +# Copyright (C) 2005-2021 Junjiro R. Okajima
1541 +# 
1542 +# This program is free software; you can redistribute it and/or modify
1543 +# it under the terms of the GNU General Public License as published by
1544 +# the Free Software Foundation; either version 2 of the License, or
1545 +# (at your option) any later version.
1546 +# 
1547 +# This program is distributed in the hope that it will be useful,
1548 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1549 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1550 +# GNU General Public License for more details.
1551 +# 
1552 +# You should have received a copy of the GNU General Public License
1553 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1554 +
1555 +Lookup in a Branch
1556 +----------------------------------------------------------------------
1557 +Since aufs has a character of sub-VFS (see Introduction), it operates
1558 +lookup for branches as VFS does. It may be a heavy work. But almost all
1559 +lookup operation in aufs is the simplest case, ie. lookup only an entry
1560 +directly connected to its parent. Digging down the directory hierarchy
1561 +is unnecessary. VFS has a function lookup_one_len() for that use, and
1562 +aufs calls it.
1563 +
1564 +When a branch is a remote filesystem, aufs basically relies upon its
1565 +->d_revalidate(), also aufs forces the hardest revalidate tests for
1566 +them.
1567 +For d_revalidate, aufs implements three levels of revalidate tests. See
1568 +"Revalidate Dentry and UDBA" in detail.
1569 +
1570 +
1571 +Test Only the Highest One for the Directory Permission (dirperm1 option)
1572 +----------------------------------------------------------------------
1573 +Let's try case study.
1574 +- aufs has two branches, upper readwrite and lower readonly.
1575 +  /au = /rw + /ro
1576 +- "dirA" exists under /ro, but /rw. and its mode is 0700.
1577 +- user invoked "chmod a+rx /au/dirA"
1578 +- the internal copy-up is activated and "/rw/dirA" is created and its
1579 +  permission bits are set to world readable.
1580 +- then "/au/dirA" becomes world readable?
1581 +
1582 +In this case, /ro/dirA is still 0700 since it exists in readonly branch,
1583 +or it may be a natively readonly filesystem. If aufs respects the lower
1584 +branch, it should not respond readdir request from other users. But user
1585 +allowed it by chmod. Should really aufs rejects showing the entries
1586 +under /ro/dirA?
1587 +
1588 +To be honest, I don't have a good solution for this case. So aufs
1589 +implements 'dirperm1' and 'nodirperm1' mount options, and leave it to
1590 +users.
1591 +When dirperm1 is specified, aufs checks only the highest one for the
1592 +directory permission, and shows the entries. Otherwise, as usual, checks
1593 +every dir existing on all branches and rejects the request.
1594 +
1595 +As a side effect, dirperm1 option improves the performance of aufs
1596 +because the number of permission check is reduced when the number of
1597 +branch is many.
1598 +
1599 +
1600 +Revalidate Dentry and UDBA (User's Direct Branch Access)
1601 +----------------------------------------------------------------------
1602 +Generally VFS helpers re-validate a dentry as a part of lookup.
1603 +0. digging down the directory hierarchy.
1604 +1. lock the parent dir by its i_mutex.
1605 +2. lookup the final (child) entry.
1606 +3. revalidate it.
1607 +4. call the actual operation (create, unlink, etc.)
1608 +5. unlock the parent dir
1609 +
1610 +If the filesystem implements its ->d_revalidate() (step 3), then it is
1611 +called. Actually aufs implements it and checks the dentry on a branch is
1612 +still valid.
1613 +But it is not enough. Because aufs has to release the lock for the
1614 +parent dir on a branch at the end of ->lookup() (step 2) and
1615 +->d_revalidate() (step 3) while the i_mutex of the aufs dir is still
1616 +held by VFS.
1617 +If the file on a branch is changed directly, eg. bypassing aufs, after
1618 +aufs released the lock, then the subsequent operation may cause
1619 +something unpleasant result.
1620 +
1621 +This situation is a result of VFS architecture, ->lookup() and
1622 +->d_revalidate() is separated. But I never say it is wrong. It is a good
1623 +design from VFS's point of view. It is just not suitable for sub-VFS
1624 +character in aufs.
1625 +
1626 +Aufs supports such case by three level of revalidation which is
1627 +selectable by user.
1628 +1. Simple Revalidate
1629 +   Addition to the native flow in VFS's, confirm the child-parent
1630 +   relationship on the branch just after locking the parent dir on the
1631 +   branch in the "actual operation" (step 4). When this validation
1632 +   fails, aufs returns EBUSY. ->d_revalidate() (step 3) in aufs still
1633 +   checks the validation of the dentry on branches.
1634 +2. Monitor Changes Internally by Inotify/Fsnotify
1635 +   Addition to above, in the "actual operation" (step 4) aufs re-lookup
1636 +   the dentry on the branch, and returns EBUSY if it finds different
1637 +   dentry.
1638 +   Additionally, aufs sets the inotify/fsnotify watch for every dir on branches
1639 +   during it is in cache. When the event is notified, aufs registers a
1640 +   function to kernel 'events' thread by schedule_work(). And the
1641 +   function sets some special status to the cached aufs dentry and inode
1642 +   private data. If they are not cached, then aufs has nothing to
1643 +   do. When the same file is accessed through aufs (step 0-3) later,
1644 +   aufs will detect the status and refresh all necessary data.
1645 +   In this mode, aufs has to ignore the event which is fired by aufs
1646 +   itself.
1647 +3. No Extra Validation
1648 +   This is the simplest test and doesn't add any additional revalidation
1649 +   test, and skip the revalidation in step 4. It is useful and improves
1650 +   aufs performance when system surely hide the aufs branches from user,
1651 +   by over-mounting something (or another method).
1652 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/04branch.txt linux/Documentation/filesystems/aufs/design/04branch.txt
1653 --- /usr/share/empty/Documentation/filesystems/aufs/design/04branch.txt 1970-01-01 01:00:00.000000000 +0100
1654 +++ linux/Documentation/filesystems/aufs/design/04branch.txt    2022-03-21 14:49:05.713299677 +0100
1655 @@ -0,0 +1,74 @@
1656 +
1657 +# Copyright (C) 2005-2021 Junjiro R. Okajima
1658 +# 
1659 +# This program is free software; you can redistribute it and/or modify
1660 +# it under the terms of the GNU General Public License as published by
1661 +# the Free Software Foundation; either version 2 of the License, or
1662 +# (at your option) any later version.
1663 +# 
1664 +# This program is distributed in the hope that it will be useful,
1665 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1666 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1667 +# GNU General Public License for more details.
1668 +# 
1669 +# You should have received a copy of the GNU General Public License
1670 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1671 +
1672 +Branch Manipulation
1673 +
1674 +Since aufs supports dynamic branch manipulation, ie. add/remove a branch
1675 +and changing its permission/attribute, there are a lot of works to do.
1676 +
1677 +
1678 +Add a Branch
1679 +----------------------------------------------------------------------
1680 +o Confirm the adding dir exists outside of aufs, including loopback
1681 +  mount, and its various attributes.
1682 +o Initialize the xino file and whiteout bases if necessary.
1683 +  See struct.txt.
1684 +
1685 +o Check the owner/group/mode of the directory
1686 +  When the owner/group/mode of the adding directory differs from the
1687 +  existing branch, aufs issues a warning because it may impose a
1688 +  security risk.
1689 +  For example, when a upper writable branch has a world writable empty
1690 +  top directory, a malicious user can create any files on the writable
1691 +  branch directly, like copy-up and modify manually. If something like
1692 +  /etc/{passwd,shadow} exists on the lower readonly branch but the upper
1693 +  writable branch, and the writable branch is world-writable, then a
1694 +  malicious guy may create /etc/passwd on the writable branch directly
1695 +  and the infected file will be valid in aufs.
1696 +  I am afraid it can be a security issue, but aufs can do nothing except
1697 +  producing a warning.
1698 +
1699 +
1700 +Delete a Branch
1701 +----------------------------------------------------------------------
1702 +o Confirm the deleting branch is not busy
1703 +  To be general, there is one merit to adopt "remount" interface to
1704 +  manipulate branches. It is to discard caches. At deleting a branch,
1705 +  aufs checks the still cached (and connected) dentries and inodes. If
1706 +  there are any, then they are all in-use. An inode without its
1707 +  corresponding dentry can be alive alone (for example, inotify/fsnotify case).
1708 +
1709 +  For the cached one, aufs checks whether the same named entry exists on
1710 +  other branches.
1711 +  If the cached one is a directory, because aufs provides a merged view
1712 +  to users, as long as one dir is left on any branch aufs can show the
1713 +  dir to users. In this case, the branch can be removed from aufs.
1714 +  Otherwise aufs rejects deleting the branch.
1715 +
1716 +  If any file on the deleting branch is opened by aufs, then aufs
1717 +  rejects deleting.
1718 +
1719 +
1720 +Modify the Permission of a Branch
1721 +----------------------------------------------------------------------
1722 +o Re-initialize or remove the xino file and whiteout bases if necessary.
1723 +  See struct.txt.
1724 +
1725 +o rw --> ro: Confirm the modifying branch is not busy
1726 +  Aufs rejects the request if any of these conditions are true.
1727 +  - a file on the branch is mmap-ed.
1728 +  - a regular file on the branch is opened for write and there is no
1729 +    same named entry on the upper branch.
1730 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/05wbr_policy.txt linux/Documentation/filesystems/aufs/design/05wbr_policy.txt
1731 --- /usr/share/empty/Documentation/filesystems/aufs/design/05wbr_policy.txt     1970-01-01 01:00:00.000000000 +0100
1732 +++ linux/Documentation/filesystems/aufs/design/05wbr_policy.txt        2022-03-21 14:49:05.713299677 +0100
1733 @@ -0,0 +1,64 @@
1734 +
1735 +# Copyright (C) 2005-2021 Junjiro R. Okajima
1736 +# 
1737 +# This program is free software; you can redistribute it and/or modify
1738 +# it under the terms of the GNU General Public License as published by
1739 +# the Free Software Foundation; either version 2 of the License, or
1740 +# (at your option) any later version.
1741 +# 
1742 +# This program is distributed in the hope that it will be useful,
1743 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1744 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1745 +# GNU General Public License for more details.
1746 +# 
1747 +# You should have received a copy of the GNU General Public License
1748 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1749 +
1750 +Policies to Select One among Multiple Writable Branches
1751 +----------------------------------------------------------------------
1752 +When the number of writable branch is more than one, aufs has to decide
1753 +the target branch for file creation or copy-up. By default, the highest
1754 +writable branch which has the parent (or ancestor) dir of the target
1755 +file is chosen (top-down-parent policy).
1756 +By user's request, aufs implements some other policies to select the
1757 +writable branch, for file creation several policies, round-robin,
1758 +most-free-space, and other policies. For copy-up, top-down-parent,
1759 +bottom-up-parent, bottom-up and others.
1760 +
1761 +As expected, the round-robin policy selects the branch in circular. When
1762 +you have two writable branches and creates 10 new files, 5 files will be
1763 +created for each branch. mkdir(2) systemcall is an exception. When you
1764 +create 10 new directories, all will be created on the same branch.
1765 +And the most-free-space policy selects the one which has most free
1766 +space among the writable branches. The amount of free space will be
1767 +checked by aufs internally, and users can specify its time interval.
1768 +
1769 +The policies for copy-up is more simple,
1770 +top-down-parent is equivalent to the same named on in create policy,
1771 +bottom-up-parent selects the writable branch where the parent dir
1772 +exists and the nearest upper one from the copyup-source,
1773 +bottom-up selects the nearest upper writable branch from the
1774 +copyup-source, regardless the existence of the parent dir.
1775 +
1776 +There are some rules or exceptions to apply these policies.
1777 +- If there is a readonly branch above the policy-selected branch and
1778 +  the parent dir is marked as opaque (a variation of whiteout), or the
1779 +  target (creating) file is whiteout-ed on the upper readonly branch,
1780 +  then the result of the policy is ignored and the target file will be
1781 +  created on the nearest upper writable branch than the readonly branch.
1782 +- If there is a writable branch above the policy-selected branch and
1783 +  the parent dir is marked as opaque or the target file is whiteouted
1784 +  on the branch, then the result of the policy is ignored and the target
1785 +  file will be created on the highest one among the upper writable
1786 +  branches who has diropq or whiteout. In case of whiteout, aufs removes
1787 +  it as usual.
1788 +- link(2) and rename(2) systemcalls are exceptions in every policy.
1789 +  They try selecting the branch where the source exists as possible
1790 +  since copyup a large file will take long time. If it can't be,
1791 +  ie. the branch where the source exists is readonly, then they will
1792 +  follow the copyup policy.
1793 +- There is an exception for rename(2) when the target exists.
1794 +  If the rename target exists, aufs compares the index of the branches
1795 +  where the source and the target exists and selects the higher
1796 +  one. If the selected branch is readonly, then aufs follows the
1797 +  copyup policy.
1798 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.dot linux/Documentation/filesystems/aufs/design/06dirren.dot
1799 --- /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.dot 1970-01-01 01:00:00.000000000 +0100
1800 +++ linux/Documentation/filesystems/aufs/design/06dirren.dot    2022-03-21 14:49:05.713299677 +0100
1801 @@ -0,0 +1,31 @@
1802 +
1803 +// to view this graph, run dot(1) command in GRAPHVIZ.
1804 +
1805 +digraph G {
1806 +node [shape=box];
1807 +whinfo [label="detailed info file\n(lower_brid_root-hinum, h_inum, namelen, old name)"];
1808 +
1809 +node [shape=oval];
1810 +
1811 +aufs_rename -> whinfo [label="store/remove"];
1812 +
1813 +node [shape=oval];
1814 +inode_list [label="h_inum list in branch\ncache"];
1815 +
1816 +node [shape=box];
1817 +whinode [label="h_inum list file"];
1818 +
1819 +node [shape=oval];
1820 +brmgmt [label="br_add/del/mod/umount"];
1821 +
1822 +brmgmt -> inode_list [label="create/remove"];
1823 +brmgmt -> whinode [label="load/store"];
1824 +
1825 +inode_list -> whinode [style=dashed,dir=both];
1826 +
1827 +aufs_rename -> inode_list [label="add/del"];
1828 +
1829 +aufs_lookup -> inode_list [label="search"];
1830 +
1831 +aufs_lookup -> whinfo [label="load/remove"];
1832 +}
1833 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.txt linux/Documentation/filesystems/aufs/design/06dirren.txt
1834 --- /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.txt 1970-01-01 01:00:00.000000000 +0100
1835 +++ linux/Documentation/filesystems/aufs/design/06dirren.txt    2022-03-21 14:49:05.713299677 +0100
1836 @@ -0,0 +1,102 @@
1837 +
1838 +# Copyright (C) 2017-2021 Junjiro R. Okajima
1839 +#
1840 +# This program is free software; you can redistribute it and/or modify
1841 +# it under the terms of the GNU General Public License as published by
1842 +# the Free Software Foundation; either version 2 of the License, or
1843 +# (at your option) any later version.
1844 +#
1845 +# This program is distributed in the hope that it will be useful,
1846 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1847 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1848 +# GNU General Public License for more details.
1849 +#
1850 +# You should have received a copy of the GNU General Public License
1851 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1852 +
1853 +Special handling for renaming a directory (DIRREN)
1854 +----------------------------------------------------------------------
1855 +First, let's assume we have a simple usecase.
1856 +
1857 +- /u = /rw + /ro
1858 +- /rw/dirA exists
1859 +- /ro/dirA and /ro/dirA/file exist too
1860 +- there is no dirB on both branches
1861 +- a user issues rename("dirA", "dirB")
1862 +
1863 +Now, what should aufs behave against this rename(2)?
1864 +There are a few possible cases.
1865 +
1866 +A. returns EROFS.
1867 +   since dirA exists on a readonly branch which cannot be renamed.
1868 +B. returns EXDEV.
1869 +   it is possible to copy-up dirA (only the dir itself), but the child
1870 +   entries ("file" in this case) should not be. it must be a bad
1871 +   approach to copy-up recursively.
1872 +C. returns a success.
1873 +   even the branch /ro is readonly, aufs tries renaming it. Obviously it
1874 +   is a violation of aufs' policy.
1875 +D. construct an extra information which indicates that /ro/dirA should
1876 +   be handled as the name of dirB.
1877 +   overlayfs has a similar feature called REDIRECT.
1878 +
1879 +Until now, aufs implements the case B only which returns EXDEV, and
1880 +expects the userspace application behaves like mv(1) which tries
1881 +issueing rename(2) recursively.
1882 +
1883 +A new aufs feature called DIRREN is introduced which implements the case
1884 +D. There are several "extra information" added.
1885 +
1886 +1. detailed info per renamed directory
1887 +   path: /rw/dirB/$AUFS_WH_DR_INFO_PFX.<lower branch-id>
1888 +2. the inode-number list of directories on a branch
1889 +   path: /rw/dirB/$AUFS_WH_DR_BRHINO
1890 +
1891 +The filename of "detailed info per directory" represents the lower
1892 +branch, and its format is
1893 +- a type of the branch id
1894 +  one of these.
1895 +  + uuid (not implemented yet)
1896 +  + fsid
1897 +  + dev
1898 +- the inode-number of the branch root dir
1899 +
1900 +And it contains these info in a single regular file.
1901 +- magic number
1902 +- branch's inode-number of the logically renamed dir
1903 +- the name of the before-renamed dir
1904 +
1905 +The "detailed info per directory" file is created in aufs rename(2), and
1906 +loaded in any lookup.
1907 +The info is considered in lookup for the matching case only. Here
1908 +"matching" means that the root of branch (in the info filename) is same
1909 +to the current looking-up branch. After looking-up the before-renamed
1910 +name, the inode-number is compared. And the matched dentry is used.
1911 +
1912 +The "inode-number list of directories" is a regular file which contains
1913 +simply the inode-numbers on the branch. The file is created or updated
1914 +in removing the branch, and loaded in adding the branch. Its lifetime is
1915 +equal to the branch.
1916 +The list is refered in lookup, and when the current target inode is
1917 +found in the list, the aufs tries loading the "detailed info per
1918 +directory" and get the changed and valid name of the dir.
1919 +
1920 +Theoretically these "extra informaiton" may be able to be put into XATTR
1921 +in the dir inode. But aufs doesn't choose this way because
1922 +1. XATTR may not be supported by the branch (or its configuration)
1923 +2. XATTR may have its size limit.
1924 +3. XATTR may be less easy to convert than a regular file, when the
1925 +   format of the info is changed in the future.
1926 +At the same time, I agree that the regular file approach is much slower
1927 +than XATTR approach. So, in the future, aufs may take the XATTR or other
1928 +better approach.
1929 +
1930 +This DIRREN feature is enabled by aufs configuration, and is activated
1931 +by a new mount option.
1932 +
1933 +For the more complicated case, there is a work with UDBA option, which
1934 +is to dected the direct access to the branches (by-passing aufs) and to
1935 +maintain the cashes in aufs. Since a single cached aufs dentry may
1936 +contains two names, before- and after-rename, the name comparision in
1937 +UDBA handler may not work correctly. In this case, the behaviour will be
1938 +equivalen to udba=reval case.
1939 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06fhsm.txt linux/Documentation/filesystems/aufs/design/06fhsm.txt
1940 --- /usr/share/empty/Documentation/filesystems/aufs/design/06fhsm.txt   1970-01-01 01:00:00.000000000 +0100
1941 +++ linux/Documentation/filesystems/aufs/design/06fhsm.txt      2022-03-21 14:49:05.716633010 +0100
1942 @@ -0,0 +1,120 @@
1943 +
1944 +# Copyright (C) 2011-2021 Junjiro R. Okajima
1945 +# 
1946 +# This program is free software; you can redistribute it and/or modify
1947 +# it under the terms of the GNU General Public License as published by
1948 +# the Free Software Foundation; either version 2 of the License, or
1949 +# (at your option) any later version.
1950 +# 
1951 +# This program is distributed in the hope that it will be useful,
1952 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1953 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1954 +# GNU General Public License for more details.
1955 +# 
1956 +# You should have received a copy of the GNU General Public License
1957 +# along with this program; if not, write to the Free Software
1958 +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
1959 +
1960 +
1961 +File-based Hierarchical Storage Management (FHSM)
1962 +----------------------------------------------------------------------
1963 +Hierarchical Storage Management (or HSM) is a well-known feature in the
1964 +storage world. Aufs provides this feature as file-based with multiple
1965 +writable branches, based upon the principle of "Colder, the Lower".
1966 +Here the word "colder" means that the less used files, and "lower" means
1967 +that the position in the order of the stacked branches vertically.
1968 +These multiple writable branches are prioritized, ie. the topmost one
1969 +should be the fastest drive and be used heavily.
1970 +
1971 +o Characters in aufs FHSM story
1972 +- aufs itself and a new branch attribute.
1973 +- a new ioctl interface to move-down and to establish a connection with
1974 +  the daemon ("move-down" is a converse of "copy-up").
1975 +- userspace tool and daemon.
1976 +
1977 +The userspace daemon establishes a connection with aufs and waits for
1978 +the notification. The notified information is very similar to struct
1979 +statfs containing the number of consumed blocks and inodes.
1980 +When the consumed blocks/inodes of a branch exceeds the user-specified
1981 +upper watermark, the daemon activates its move-down process until the
1982 +consumed blocks/inodes reaches the user-specified lower watermark.
1983 +
1984 +The actual move-down is done by aufs based upon the request from
1985 +user-space since we need to maintain the inode number and the internal
1986 +pointer arrays in aufs.
1987 +
1988 +Currently aufs FHSM handles the regular files only. Additionally they
1989 +must not be hard-linked nor pseudo-linked.
1990 +
1991 +
1992 +o Cowork of aufs and the user-space daemon
1993 +  During the userspace daemon established the connection, aufs sends a
1994 +  small notification to it whenever aufs writes something into the
1995 +  writable branch. But it may cost high since aufs issues statfs(2)
1996 +  internally. So user can specify a new option to cache the
1997 +  info. Actually the notification is controlled by these factors.
1998 +  + the specified cache time.
1999 +  + classified as "force" by aufs internally.
2000 +  Until the specified time expires, aufs doesn't send the info
2001 +  except the forced cases. When aufs decide forcing, the info is always
2002 +  notified to userspace.
2003 +  For example, the number of free inodes is generally large enough and
2004 +  the shortage of it happens rarely. So aufs doesn't force the
2005 +  notification when creating a new file, directory and others. This is
2006 +  the typical case which aufs doesn't force.
2007 +  When aufs writes the actual filedata and the files consumes any of new
2008 +  blocks, the aufs forces notifying.
2009 +
2010 +
2011 +o Interfaces in aufs
2012 +- New branch attribute.
2013 +  + fhsm
2014 +    Specifies that the branch is managed by FHSM feature. In other word,
2015 +    participant in the FHSM.
2016 +    When nofhsm is set to the branch, it will not be the source/target
2017 +    branch of the move-down operation. This attribute is set
2018 +    independently from coo and moo attributes, and if you want full
2019 +    FHSM, you should specify them as well.
2020 +- New mount option.
2021 +  + fhsm_sec
2022 +    Specifies a second to suppress many less important info to be
2023 +    notified.
2024 +- New ioctl.
2025 +  + AUFS_CTL_FHSM_FD
2026 +    create a new file descriptor which userspace can read the notification
2027 +    (a subset of struct statfs) from aufs.
2028 +- Module parameter 'brs'
2029 +  It has to be set to 1. Otherwise the new mount option 'fhsm' will not
2030 +  be set.
2031 +- mount helpers /sbin/mount.aufs and /sbin/umount.aufs
2032 +  When there are two or more branches with fhsm attributes,
2033 +  /sbin/mount.aufs invokes the user-space daemon and /sbin/umount.aufs
2034 +  terminates it. As a result of remounting and branch-manipulation, the
2035 +  number of branches with fhsm attribute can be one. In this case,
2036 +  /sbin/mount.aufs will terminate the user-space daemon.
2037 +
2038 +
2039 +Finally the operation is done as these steps in kernel-space.
2040 +- make sure that,
2041 +  + no one else is using the file.
2042 +  + the file is not hard-linked.
2043 +  + the file is not pseudo-linked.
2044 +  + the file is a regular file.
2045 +  + the parent dir is not opaqued.
2046 +- find the target writable branch.
2047 +- make sure the file is not whiteout-ed by the upper (than the target)
2048 +  branch.
2049 +- make the parent dir on the target branch.
2050 +- mutex lock the inode on the branch.
2051 +- unlink the whiteout on the target branch (if exists).
2052 +- lookup and create the whiteout-ed temporary name on the target branch.
2053 +- copy the file as the whiteout-ed temporary name on the target branch.
2054 +- rename the whiteout-ed temporary name to the original name.
2055 +- unlink the file on the source branch.
2056 +- maintain the internal pointer array and the external inode number
2057 +  table (XINO).
2058 +- maintain the timestamps and other attributes of the parent dir and the
2059 +  file.
2060 +
2061 +And of course, in every step, an error may happen. So the operation
2062 +should restore the original file state after an error happens.
2063 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06mmap.txt linux/Documentation/filesystems/aufs/design/06mmap.txt
2064 --- /usr/share/empty/Documentation/filesystems/aufs/design/06mmap.txt   1970-01-01 01:00:00.000000000 +0100
2065 +++ linux/Documentation/filesystems/aufs/design/06mmap.txt      2022-03-21 14:49:05.716633010 +0100
2066 @@ -0,0 +1,72 @@
2067 +
2068 +# Copyright (C) 2005-2021 Junjiro R. Okajima
2069 +# 
2070 +# This program is free software; you can redistribute it and/or modify
2071 +# it under the terms of the GNU General Public License as published by
2072 +# the Free Software Foundation; either version 2 of the License, or
2073 +# (at your option) any later version.
2074 +# 
2075 +# This program is distributed in the hope that it will be useful,
2076 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2077 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2078 +# GNU General Public License for more details.
2079 +# 
2080 +# You should have received a copy of the GNU General Public License
2081 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2082 +
2083 +mmap(2) -- File Memory Mapping
2084 +----------------------------------------------------------------------
2085 +In aufs, the file-mapped pages are handled by a branch fs directly, no
2086 +interaction with aufs. It means aufs_mmap() calls the branch fs's
2087 +->mmap().
2088 +This approach is simple and good, but there is one problem.
2089 +Under /proc, several entries show the mmapped files by its path (with
2090 +device and inode number), and the printed path will be the path on the
2091 +branch fs's instead of virtual aufs's.
2092 +This is not a problem in most cases, but some utilities lsof(1) (and its
2093 +user) may expect the path on aufs.
2094 +
2095 +To address this issue, aufs adds a new member called vm_prfile in struct
2096 +vm_area_struct (and struct vm_region). The original vm_file points to
2097 +the file on the branch fs in order to handle everything correctly as
2098 +usual. The new vm_prfile points to a virtual file in aufs, and the
2099 +show-functions in procfs refers to vm_prfile if it is set.
2100 +Also we need to maintain several other places where touching vm_file
2101 +such like
2102 +- fork()/clone() copies vma and the reference count of vm_file is
2103 +  incremented.
2104 +- merging vma maintains the ref count too.
2105 +
2106 +This is not a good approach. It just fakes the printed path. But it
2107 +leaves all behaviour around f_mapping unchanged. This is surely an
2108 +advantage.
2109 +Actually aufs had adopted another complicated approach which calls
2110 +generic_file_mmap() and handles struct vm_operations_struct. In this
2111 +approach, aufs met a hard problem and I could not solve it without
2112 +switching the approach.
2113 +
2114 +There may be one more another approach which is
2115 +- bind-mount the branch-root onto the aufs-root internally
2116 +- grab the new vfsmount (ie. struct mount)
2117 +- lazy-umount the branch-root internally
2118 +- in open(2) the aufs-file, open the branch-file with the hidden
2119 +  vfsmount (instead of the original branch's vfsmount)
2120 +- ideally this "bind-mount and lazy-umount" should be done atomically,
2121 +  but it may be possible from userspace by the mount helper.
2122 +
2123 +Adding the internal hidden vfsmount and using it in opening a file, the
2124 +file path under /proc will be printed correctly. This approach looks
2125 +smarter, but is not possible I am afraid.
2126 +- aufs-root may be bind-mount later. when it happens, another hidden
2127 +  vfsmount will be required.
2128 +- it is hard to get the chance to bind-mount and lazy-umount
2129 +  + in kernel-space, FS can have vfsmount in open(2) via
2130 +    file->f_path, and aufs can know its vfsmount. But several locks are
2131 +    already acquired, and if aufs tries to bind-mount and lazy-umount
2132 +    here, then it may cause a deadlock.
2133 +  + in user-space, bind-mount doesn't invoke the mount helper.
2134 +- since /proc shows dev and ino, aufs has to give vma these info. it
2135 +  means a new member vm_prinode will be necessary. this is essentially
2136 +  equivalent to vm_prfile described above.
2137 +
2138 +I have to give up this "looks-smater" approach.
2139 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06xattr.txt linux/Documentation/filesystems/aufs/design/06xattr.txt
2140 --- /usr/share/empty/Documentation/filesystems/aufs/design/06xattr.txt  1970-01-01 01:00:00.000000000 +0100
2141 +++ linux/Documentation/filesystems/aufs/design/06xattr.txt     2022-03-21 14:49:05.716633010 +0100
2142 @@ -0,0 +1,96 @@
2143 +
2144 +# Copyright (C) 2014-2021 Junjiro R. Okajima
2145 +#
2146 +# This program is free software; you can redistribute it and/or modify
2147 +# it under the terms of the GNU General Public License as published by
2148 +# the Free Software Foundation; either version 2 of the License, or
2149 +# (at your option) any later version.
2150 +#
2151 +# This program is distributed in the hope that it will be useful,
2152 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2153 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2154 +# GNU General Public License for more details.
2155 +#
2156 +# You should have received a copy of the GNU General Public License
2157 +# along with this program; if not, write to the Free Software
2158 +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
2159 +
2160 +
2161 +Listing XATTR/EA and getting the value
2162 +----------------------------------------------------------------------
2163 +For the inode standard attributes (owner, group, timestamps, etc.), aufs
2164 +shows the values from the topmost existing file. This behaviour is good
2165 +for the non-dir entries since the bahaviour exactly matches the shown
2166 +information. But for the directories, aufs considers all the same named
2167 +entries on the lower branches. Which means, if one of the lower entry
2168 +rejects readdir call, then aufs returns an error even if the topmost
2169 +entry allows it. This behaviour is necessary to respect the branch fs's
2170 +security, but can make users confused since the user-visible standard
2171 +attributes don't match the behaviour.
2172 +To address this issue, aufs has a mount option called dirperm1 which
2173 +checks the permission for the topmost entry only, and ignores the lower
2174 +entry's permission.
2175 +
2176 +A similar issue can happen around XATTR.
2177 +getxattr(2) and listxattr(2) families behave as if dirperm1 option is
2178 +always set. Otherwise these very unpleasant situation would happen.
2179 +- listxattr(2) may return the duplicated entries.
2180 +- users may not be able to remove or reset the XATTR forever,
2181 +
2182 +
2183 +XATTR/EA support in the internal (copy,move)-(up,down)
2184 +----------------------------------------------------------------------
2185 +Generally the extended attributes of inode are categorized as these.
2186 +- "security" for LSM and capability.
2187 +- "system" for posix ACL, 'acl' mount option is required for the branch
2188 +  fs generally.
2189 +- "trusted" for userspace, CAP_SYS_ADMIN is required.
2190 +- "user" for userspace, 'user_xattr' mount option is required for the
2191 +  branch fs generally.
2192 +
2193 +Moreover there are some other categories. Aufs handles these rather
2194 +unpopular categories as the ordinary ones, ie. there is no special
2195 +condition nor exception.
2196 +
2197 +In copy-up, the support for XATTR on the dst branch may differ from the
2198 +src branch. In this case, the copy-up operation will get an error and
2199 +the original user operation which triggered the copy-up will fail. It
2200 +can happen that even all copy-up will fail.
2201 +When both of src and dst branches support XATTR and if an error occurs
2202 +during copying XATTR, then the copy-up should fail obviously. That is a
2203 +good reason and aufs should return an error to userspace. But when only
2204 +the src branch support that XATTR, aufs should not return an error.
2205 +For example, the src branch supports ACL but the dst branch doesn't
2206 +because the dst branch may natively un-support it or temporary
2207 +un-support it due to "noacl" mount option. Of course, the dst branch fs
2208 +may NOT return an error even if the XATTR is not supported. It is
2209 +totally up to the branch fs.
2210 +
2211 +Anyway when the aufs internal copy-up gets an error from the dst branch
2212 +fs, then aufs tries removing the just copied entry and returns the error
2213 +to the userspace. The worst case of this situation will be all copy-up
2214 +will fail.
2215 +
2216 +For the copy-up operation, there two basic approaches.
2217 +- copy the specified XATTR only (by category above), and return the
2218 +  error unconditionally if it happens.
2219 +- copy all XATTR, and ignore the error on the specified category only.
2220 +
2221 +In order to support XATTR and to implement the correct behaviour, aufs
2222 +chooses the latter approach and introduces some new branch attributes,
2223 +"icexsec", "icexsys", "icextr", "icexusr", and "icexoth".
2224 +They correspond to the XATTR namespaces (see above). Additionally, to be
2225 +convenient, "icex" is also provided which means all "icex*" attributes
2226 +are set (here the word "icex" stands for "ignore copy-error on XATTR").
2227 +
2228 +The meaning of these attributes is to ignore the error from setting
2229 +XATTR on that branch.
2230 +Note that aufs tries copying all XATTR unconditionally, and ignores the
2231 +error from the dst branch according to the specified attributes.
2232 +
2233 +Some XATTR may have its default value. The default value may come from
2234 +the parent dir or the environment. If the default value is set at the
2235 +file creating-time, it will be overwritten by copy-up.
2236 +Some contradiction may happen I am afraid.
2237 +Do we need another attribute to stop copying XATTR? I am unsure. For
2238 +now, aufs implements the branch attributes to ignore the error.
2239 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/07export.txt linux/Documentation/filesystems/aufs/design/07export.txt
2240 --- /usr/share/empty/Documentation/filesystems/aufs/design/07export.txt 1970-01-01 01:00:00.000000000 +0100
2241 +++ linux/Documentation/filesystems/aufs/design/07export.txt    2022-03-21 14:49:05.716633010 +0100
2242 @@ -0,0 +1,58 @@
2243 +
2244 +# Copyright (C) 2005-2021 Junjiro R. Okajima
2245 +# 
2246 +# This program is free software; you can redistribute it and/or modify
2247 +# it under the terms of the GNU General Public License as published by
2248 +# the Free Software Foundation; either version 2 of the License, or
2249 +# (at your option) any later version.
2250 +# 
2251 +# This program is distributed in the hope that it will be useful,
2252 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2253 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2254 +# GNU General Public License for more details.
2255 +# 
2256 +# You should have received a copy of the GNU General Public License
2257 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2258 +
2259 +Export Aufs via NFS
2260 +----------------------------------------------------------------------
2261 +Here is an approach.
2262 +- like xino/xib, add a new file 'xigen' which stores aufs inode
2263 +  generation.
2264 +- iget_locked(): initialize aufs inode generation for a new inode, and
2265 +  store it in xigen file.
2266 +- destroy_inode(): increment aufs inode generation and store it in xigen
2267 +  file. it is necessary even if it is not unlinked, because any data of
2268 +  inode may be changed by UDBA.
2269 +- encode_fh(): for a root dir, simply return FILEID_ROOT. otherwise
2270 +  build file handle by
2271 +  + branch id (4 bytes)
2272 +  + superblock generation (4 bytes)
2273 +  + inode number (4 or 8 bytes)
2274 +  + parent dir inode number (4 or 8 bytes)
2275 +  + inode generation (4 bytes))
2276 +  + return value of exportfs_encode_fh() for the parent on a branch (4
2277 +    bytes)
2278 +  + file handle for a branch (by exportfs_encode_fh())
2279 +- fh_to_dentry():
2280 +  + find the index of a branch from its id in handle, and check it is
2281 +    still exist in aufs.
2282 +  + 1st level: get the inode number from handle and search it in cache.
2283 +  + 2nd level: if not found in cache, get the parent inode number from
2284 +    the handle and search it in cache. and then open the found parent
2285 +    dir, find the matching inode number by vfs_readdir() and get its
2286 +    name, and call lookup_one_len() for the target dentry.
2287 +  + 3rd level: if the parent dir is not cached, call
2288 +    exportfs_decode_fh() for a branch and get the parent on a branch,
2289 +    build a pathname of it, convert it a pathname in aufs, call
2290 +    path_lookup(). now aufs gets a parent dir dentry, then handle it as
2291 +    the 2nd level.
2292 +  + to open the dir, aufs needs struct vfsmount. aufs keeps vfsmount
2293 +    for every branch, but not itself. to get this, (currently) aufs
2294 +    searches in current->nsproxy->mnt_ns list. it may not be a good
2295 +    idea, but I didn't get other approach.
2296 +  + test the generation of the gotten inode.
2297 +- every inode operation: they may get EBUSY due to UDBA. in this case,
2298 +  convert it into ESTALE for NFSD.
2299 +- readdir(): call lockdep_on/off() because filldir in NFSD calls
2300 +  lookup_one_len(), vfs_getattr(), encode_fh() and others.
2301 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/08shwh.txt linux/Documentation/filesystems/aufs/design/08shwh.txt
2302 --- /usr/share/empty/Documentation/filesystems/aufs/design/08shwh.txt   1970-01-01 01:00:00.000000000 +0100
2303 +++ linux/Documentation/filesystems/aufs/design/08shwh.txt      2022-03-21 14:49:05.716633010 +0100
2304 @@ -0,0 +1,52 @@
2305 +
2306 +# Copyright (C) 2005-2021 Junjiro R. Okajima
2307 +# 
2308 +# This program is free software; you can redistribute it and/or modify
2309 +# it under the terms of the GNU General Public License as published by
2310 +# the Free Software Foundation; either version 2 of the License, or
2311 +# (at your option) any later version.
2312 +# 
2313 +# This program is distributed in the hope that it will be useful,
2314 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2315 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2316 +# GNU General Public License for more details.
2317 +# 
2318 +# You should have received a copy of the GNU General Public License
2319 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2320 +
2321 +Show Whiteout Mode (shwh)
2322 +----------------------------------------------------------------------
2323 +Generally aufs hides the name of whiteouts. But in some cases, to show
2324 +them is very useful for users. For instance, creating a new middle layer
2325 +(branch) by merging existing layers.
2326 +
2327 +(borrowing aufs1 HOW-TO from a user, Michael Towers)
2328 +When you have three branches,
2329 +- Bottom: 'system', squashfs (underlying base system), read-only
2330 +- Middle: 'mods', squashfs, read-only
2331 +- Top: 'overlay', ram (tmpfs), read-write
2332 +
2333 +The top layer is loaded at boot time and saved at shutdown, to preserve
2334 +the changes made to the system during the session.
2335 +When larger changes have been made, or smaller changes have accumulated,
2336 +the size of the saved top layer data grows. At this point, it would be
2337 +nice to be able to merge the two overlay branches ('mods' and 'overlay')
2338 +and rewrite the 'mods' squashfs, clearing the top layer and thus
2339 +restoring save and load speed.
2340 +
2341 +This merging is simplified by the use of another aufs mount, of just the
2342 +two overlay branches using the 'shwh' option.
2343 +# mount -t aufs -o ro,shwh,br:/livesys/overlay=ro+wh:/livesys/mods=rr+wh \
2344 +       aufs /livesys/merge_union
2345 +
2346 +A merged view of these two branches is then available at
2347 +/livesys/merge_union, and the new feature is that the whiteouts are
2348 +visible!
2349 +Note that in 'shwh' mode the aufs mount must be 'ro', which will disable
2350 +writing to all branches. Also the default mode for all branches is 'ro'.
2351 +It is now possible to save the combined contents of the two overlay
2352 +branches to a new squashfs, e.g.:
2353 +# mksquashfs /livesys/merge_union /path/to/newmods.squash
2354 +
2355 +This new squashfs archive can be stored on the boot device and the
2356 +initramfs will use it to replace the old one at the next boot.
2357 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/10dynop.txt linux/Documentation/filesystems/aufs/design/10dynop.txt
2358 --- /usr/share/empty/Documentation/filesystems/aufs/design/10dynop.txt  1970-01-01 01:00:00.000000000 +0100
2359 +++ linux/Documentation/filesystems/aufs/design/10dynop.txt     2022-03-21 14:49:05.716633010 +0100
2360 @@ -0,0 +1,47 @@
2361 +
2362 +# Copyright (C) 2010-2021 Junjiro R. Okajima
2363 +#
2364 +# This program is free software; you can redistribute it and/or modify
2365 +# it under the terms of the GNU General Public License as published by
2366 +# the Free Software Foundation; either version 2 of the License, or
2367 +# (at your option) any later version.
2368 +#
2369 +# This program is distributed in the hope that it will be useful,
2370 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2371 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2372 +# GNU General Public License for more details.
2373 +#
2374 +# You should have received a copy of the GNU General Public License
2375 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2376 +
2377 +Dynamically customizable FS operations
2378 +----------------------------------------------------------------------
2379 +Generally FS operations (struct inode_operations, struct
2380 +address_space_operations, struct file_operations, etc.) are defined as
2381 +"static const", but it never means that FS have only one set of
2382 +operation. Some FS have multiple sets of them. For instance, ext2 has
2383 +three sets, one for XIP, for NOBH, and for normal.
2384 +Since aufs overrides and redirects these operations, sometimes aufs has
2385 +to change its behaviour according to the branch FS type. More importantly
2386 +VFS acts differently if a function (member in the struct) is set or
2387 +not. It means aufs should have several sets of operations and select one
2388 +among them according to the branch FS definition.
2389 +
2390 +In order to solve this problem and not to affect the behaviour of VFS,
2391 +aufs defines these operations dynamically. For instance, aufs defines
2392 +dummy direct_IO function for struct address_space_operations, but it may
2393 +not be set to the address_space_operations actually. When the branch FS
2394 +doesn't have it, aufs doesn't set it to its address_space_operations
2395 +while the function definition itself is still alive. So the behaviour
2396 +itself will not change, and it will return an error when direct_IO is
2397 +not set.
2398 +
2399 +The lifetime of these dynamically generated operation object is
2400 +maintained by aufs branch object. When the branch is removed from aufs,
2401 +the reference counter of the object is decremented. When it reaches
2402 +zero, the dynamically generated operation object will be freed.
2403 +
2404 +This approach is designed to support AIO (io_submit), Direct I/O and
2405 +XIP (DAX) mainly.
2406 +Currently this approach is applied to address_space_operations for
2407 +regular files only.
2408 diff -urN /usr/share/empty/Documentation/filesystems/aufs/README linux/Documentation/filesystems/aufs/README
2409 --- /usr/share/empty/Documentation/filesystems/aufs/README      1970-01-01 01:00:00.000000000 +0100
2410 +++ linux/Documentation/filesystems/aufs/README 2022-03-21 14:49:05.713299677 +0100
2411 @@ -0,0 +1,396 @@
2412 +
2413 +Aufs5 -- advanced multi layered unification filesystem version 5.x
2414 +http://aufs.sf.net
2415 +Junjiro R. Okajima
2416 +
2417 +
2418 +0. Introduction
2419 +----------------------------------------
2420 +In the early days, aufs was entirely re-designed and re-implemented
2421 +Unionfs Version 1.x series. Adding many original ideas, approaches,
2422 +improvements and implementations, it became totally different from
2423 +Unionfs while keeping the basic features.
2424 +Later, Unionfs Version 2.x series began taking some of the same
2425 +approaches to aufs1's.
2426 +Unionfs was being developed by Professor Erez Zadok at Stony Brook
2427 +University and his team.
2428 +
2429 +Aufs5 supports linux-v5.0 and later, If you want older kernel version
2430 +support,
2431 +- for linux-v4.x series, try aufs4-linux.git or aufs4-standalone.git
2432 +- for linux-v3.x series, try aufs3-linux.git or aufs3-standalone.git
2433 +- for linux-v2.6.16 and later, try aufs2-2.6.git, aufs2-standalone.git
2434 +  or aufs1 from CVS on SourceForge.
2435 +
2436 +Note: it becomes clear that "Aufs was rejected. Let's give it up."
2437 +      According to Christoph Hellwig, linux rejects all union-type
2438 +      filesystems but UnionMount.
2439 +<http://marc.info/?l=linux-kernel&m=123938533724484&w=2>
2440 +
2441 +PS. Al Viro seems have a plan to merge aufs as well as overlayfs and
2442 +    UnionMount, and he pointed out an issue around a directory mutex
2443 +    lock and aufs addressed it. But it is still unsure whether aufs will
2444 +    be merged (or any other union solution).
2445 +<http://marc.info/?l=linux-kernel&m=136312705029295&w=1>
2446 +
2447 +
2448 +1. Features
2449 +----------------------------------------
2450 +- unite several directories into a single virtual filesystem. The member
2451 +  directory is called as a branch.
2452 +- you can specify the permission flags to the branch, which are 'readonly',
2453 +  'readwrite' and 'whiteout-able.'
2454 +- by upper writable branch, internal copyup and whiteout, files/dirs on
2455 +  readonly branch are modifiable logically.
2456 +- dynamic branch manipulation, add, del.
2457 +- etc...
2458 +
2459 +Also there are many enhancements in aufs, such as:
2460 +- test only the highest one for the directory permission (dirperm1)
2461 +- copyup on open (coo=)
2462 +- 'move' policy for copy-up between two writable branches, after
2463 +  checking free space.
2464 +- xattr, acl
2465 +- readdir(3) in userspace.
2466 +- keep inode number by external inode number table
2467 +- keep the timestamps of file/dir in internal copyup operation
2468 +- seekable directory, supporting NFS readdir.
2469 +- whiteout is hardlinked in order to reduce the consumption of inodes
2470 +  on branch
2471 +- do not copyup, nor create a whiteout when it is unnecessary
2472 +- revert a single systemcall when an error occurs in aufs
2473 +- remount interface instead of ioctl
2474 +- maintain /etc/mtab by an external command, /sbin/mount.aufs.
2475 +- loopback mounted filesystem as a branch
2476 +- kernel thread for removing the dir who has a plenty of whiteouts
2477 +- support copyup sparse file (a file which has a 'hole' in it)
2478 +- default permission flags for branches
2479 +- selectable permission flags for ro branch, whether whiteout can
2480 +  exist or not
2481 +- export via NFS.
2482 +- support <sysfs>/fs/aufs and <debugfs>/aufs.
2483 +- support multiple writable branches, some policies to select one
2484 +  among multiple writable branches.
2485 +- a new semantics for link(2) and rename(2) to support multiple
2486 +  writable branches.
2487 +- no glibc changes are required.
2488 +- pseudo hardlink (hardlink over branches)
2489 +- allow a direct access manually to a file on branch, e.g. bypassing aufs.
2490 +  including NFS or remote filesystem branch.
2491 +- userspace wrapper for pathconf(3)/fpathconf(3) with _PC_LINK_MAX.
2492 +- and more...
2493 +
2494 +Currently these features are dropped temporary from aufs5.
2495 +See design/08plan.txt in detail.
2496 +- nested mount, i.e. aufs as readonly no-whiteout branch of another aufs
2497 +  (robr)
2498 +- statistics of aufs thread (/sys/fs/aufs/stat)
2499 +
2500 +Features or just an idea in the future (see also design/*.txt),
2501 +- reorder the branch index without del/re-add.
2502 +- permanent xino files for NFSD
2503 +- an option for refreshing the opened files after add/del branches
2504 +- light version, without branch manipulation. (unnecessary?)
2505 +- copyup in userspace
2506 +- inotify in userspace
2507 +- readv/writev
2508 +
2509 +
2510 +2. Download
2511 +----------------------------------------
2512 +There are three GIT trees for aufs5, aufs5-linux.git,
2513 +aufs5-standalone.git, and aufs-util.git. Note that there is no "5" in
2514 +"aufs-util.git."
2515 +While the aufs-util is always necessary, you need either of aufs5-linux
2516 +or aufs5-standalone.
2517 +
2518 +The aufs5-linux tree includes the whole linux mainline GIT tree,
2519 +git://git.kernel.org/.../torvalds/linux.git.
2520 +And you cannot select CONFIG_AUFS_FS=m for this version, eg. you cannot
2521 +build aufs5 as an external kernel module.
2522 +Several extra patches are not included in this tree. Only
2523 +aufs5-standalone tree contains them. They are described in the later
2524 +section "Configuration and Compilation."
2525 +
2526 +On the other hand, the aufs5-standalone tree has only aufs source files
2527 +and necessary patches, and you can select CONFIG_AUFS_FS=m.
2528 +But you need to apply all aufs patches manually.
2529 +
2530 +You will find GIT branches whose name is in form of "aufs5.x" where "x"
2531 +represents the linux kernel version, "linux-5.x". For instance,
2532 +"aufs5.0" is for linux-5.0. For latest "linux-5.x-rcN", use
2533 +"aufs5.x-rcN" branch.
2534 +
2535 +o aufs5-linux tree
2536 +$ git clone --reference /your/linux/git/tree \
2537 +       git://github.com/sfjro/aufs5-linux.git aufs5-linux.git
2538 +- if you don't have linux GIT tree, then remove "--reference ..."
2539 +$ cd aufs5-linux.git
2540 +$ git checkout origin/aufs5.0
2541 +
2542 +Or You may want to directly git-pull aufs into your linux GIT tree, and
2543 +leave the patch-work to GIT.
2544 +$ cd /your/linux/git/tree
2545 +$ git remote add aufs5 git://github.com/sfjro/aufs5-linux.git
2546 +$ git fetch aufs5
2547 +$ git checkout -b my5.0 v5.0
2548 +$ (add your local change...)
2549 +$ git pull aufs5 aufs5.0
2550 +- now you have v5.0 + your_changes + aufs5.0 in you my5.0 branch.
2551 +- you may need to solve some conflicts between your_changes and
2552 +  aufs5.0. in this case, git-rerere is recommended so that you can
2553 +  solve the similar conflicts automatically when you upgrade to 5.1 or
2554 +  later in the future.
2555 +
2556 +o aufs5-standalone tree
2557 +$ git clone git://github.com/sfjro/aufs5-standalone.git aufs5-standalone.git
2558 +$ cd aufs5-standalone.git
2559 +$ git checkout origin/aufs5.0
2560 +
2561 +o aufs-util tree
2562 +$ git clone git://git.code.sf.net/p/aufs/aufs-util aufs-util.git
2563 +- note that the public aufs-util.git is on SourceForge instead of
2564 +  GitHUB.
2565 +$ cd aufs-util.git
2566 +$ git checkout origin/aufs5.0
2567 +
2568 +Note: The 5.x-rcN branch is to be used with `rc' kernel versions ONLY.
2569 +The minor version number, 'x' in '5.x', of aufs may not always
2570 +follow the minor version number of the kernel.
2571 +Because changes in the kernel that cause the use of a new
2572 +minor version number do not always require changes to aufs-util.
2573 +
2574 +Since aufs-util has its own minor version number, you may not be
2575 +able to find a GIT branch in aufs-util for your kernel's
2576 +exact minor version number.
2577 +In this case, you should git-checkout the branch for the
2578 +nearest lower number.
2579 +
2580 +For (an unreleased) example:
2581 +If you are using "linux-5.10" and the "aufs5.10" branch
2582 +does not exist in aufs-util repository, then "aufs5.9", "aufs5.8"
2583 +or something numerically smaller is the branch for your kernel.
2584 +
2585 +Also you can view all branches by
2586 +       $ git branch -a
2587 +
2588 +
2589 +3. Configuration and Compilation
2590 +----------------------------------------
2591 +Make sure you have git-checkout'ed the correct branch.
2592 +
2593 +For aufs5-linux tree,
2594 +- enable CONFIG_AUFS_FS.
2595 +- set other aufs configurations if necessary.
2596 +- for aufs5.13 and later
2597 +  Because aufs is not only an ordinary filesystem (callee of VFS), but
2598 +  also a caller of VFS functions for branch filesystems, subclassing of
2599 +  the internal locks for LOCKDEP is necessary. LOCKDEP is a debugging
2600 +  feature of linux kernel. If you enable CONFIG_LOCKDEP, then you will
2601 +  need to customize some LOCKDEP numbers. Here are what I use on my
2602 +  test environment.
2603 +       CONFIG_LOCKDEP_BITS=21
2604 +       CONFIG_LOCKDEP_CHAINS_BITS=21
2605 +       CONFIG_LOCKDEP_STACK_TRACE_BITS=24
2606 +
2607 +For aufs5-standalone tree,
2608 +There are several ways to build.
2609 +
2610 +1.
2611 +- apply ./aufs5-kbuild.patch to your kernel source files.
2612 +- apply ./aufs5-base.patch too.
2613 +- apply ./aufs5-mmap.patch too.
2614 +- apply ./aufs5-standalone.patch too, if you have a plan to set
2615 +  CONFIG_AUFS_FS=m. otherwise you don't need ./aufs5-standalone.patch.
2616 +- copy ./{Documentation,fs,include/uapi/linux/aufs_type.h} files to your
2617 +  kernel source tree. Never copy $PWD/include/uapi/linux/Kbuild.
2618 +- enable CONFIG_AUFS_FS, you can select either
2619 +  =m or =y.
2620 +- and build your kernel as usual.
2621 +- install the built kernel.
2622 +- install the header files too by "make headers_install" to the
2623 +  directory where you specify. By default, it is $PWD/usr.
2624 +  "make help" shows a brief note for headers_install.
2625 +- and reboot your system.
2626 +
2627 +2.
2628 +- module only (CONFIG_AUFS_FS=m).
2629 +- apply ./aufs5-base.patch to your kernel source files.
2630 +- apply ./aufs5-mmap.patch too.
2631 +- apply ./aufs5-standalone.patch too.
2632 +- build your kernel, don't forget "make headers_install", and reboot.
2633 +- edit ./config.mk and set other aufs configurations if necessary.
2634 +  Note: You should read $PWD/fs/aufs/Kconfig carefully which describes
2635 +  every aufs configurations.
2636 +- build the module by simple "make".
2637 +- you can specify ${KDIR} make variable which points to your kernel
2638 +  source tree.
2639 +- install the files
2640 +  + run "make install" to install the aufs module, or copy the built
2641 +    $PWD/aufs.ko to /lib/modules/... and run depmod -a (or reboot simply).
2642 +  + run "make install_headers" (instead of headers_install) to install
2643 +    the modified aufs header file (you can specify DESTDIR which is
2644 +    available in aufs standalone version's Makefile only), or copy
2645 +    $PWD/usr/include/linux/aufs_type.h to /usr/include/linux or wherever
2646 +    you like manually. By default, the target directory is $PWD/usr.
2647 +- no need to apply aufs5-kbuild.patch, nor copying source files to your
2648 +  kernel source tree.
2649 +
2650 +Note: The header file aufs_type.h is necessary to build aufs-util
2651 +      as well as "make headers_install" in the kernel source tree.
2652 +      headers_install is subject to be forgotten, but it is essentially
2653 +      necessary, not only for building aufs-util.
2654 +      You may not meet problems without headers_install in some older
2655 +      version though.
2656 +
2657 +And then,
2658 +- read README in aufs-util, build and install it
2659 +- note that your distribution may contain an obsoleted version of
2660 +  aufs_type.h in /usr/include/linux or something. When you build aufs
2661 +  utilities, make sure that your compiler refers the correct aufs header
2662 +  file which is built by "make headers_install."
2663 +- if you want to use readdir(3) in userspace or pathconf(3) wrapper,
2664 +  then run "make install_ulib" too. And refer to the aufs manual in
2665 +  detail.
2666 +
2667 +There several other patches in aufs5-standalone.git. They are all
2668 +optional. When you meet some problems, they will help you.
2669 +- aufs5-loopback.patch
2670 +  Supports a nested loopback mount in a branch-fs. This patch is
2671 +  unnecessary until aufs produces a message like "you may want to try
2672 +  another patch for loopback file".
2673 +- vfs-ino.patch
2674 +  Modifies a system global kernel internal function get_next_ino() in
2675 +  order to stop assigning 0 for an inode-number. Not directly related to
2676 +  aufs, but recommended generally.
2677 +- tmpfs-idr.patch
2678 +  Keeps the tmpfs inode number as the lowest value. Effective to reduce
2679 +  the size of aufs XINO files for tmpfs branch. Also it prevents the
2680 +  duplication of inode number, which is important for backup tools and
2681 +  other utilities. When you find aufs XINO files for tmpfs branch
2682 +  growing too much, try this patch.
2683 +
2684 +
2685 +4. Usage
2686 +----------------------------------------
2687 +At first, make sure aufs-util are installed, and please read the aufs
2688 +manual, aufs.5 in aufs-util.git tree.
2689 +$ man -l aufs.5
2690 +
2691 +And then,
2692 +$ mkdir /tmp/rw /tmp/aufs
2693 +# mount -t aufs -o br=/tmp/rw:${HOME} none /tmp/aufs
2694 +
2695 +Here is another example. The result is equivalent.
2696 +# mount -t aufs -o br=/tmp/rw=rw:${HOME}=ro none /tmp/aufs
2697 +  Or
2698 +# mount -t aufs -o br:/tmp/rw none /tmp/aufs
2699 +# mount -o remount,append:${HOME} /tmp/aufs
2700 +
2701 +Then, you can see whole tree of your home dir through /tmp/aufs. If
2702 +you modify a file under /tmp/aufs, the one on your home directory is
2703 +not affected, instead the same named file will be newly created under
2704 +/tmp/rw. And all of your modification to a file will be applied to
2705 +the one under /tmp/rw. This is called the file based Copy on Write
2706 +(COW) method.
2707 +Aufs mount options are described in aufs.5.
2708 +If you run chroot or something and make your aufs as a root directory,
2709 +then you need to customize the shutdown script. See the aufs manual in
2710 +detail.
2711 +
2712 +Additionally, there are some sample usages of aufs which are a
2713 +diskless system with network booting, and LiveCD over NFS.
2714 +See sample dir in CVS tree on SourceForge.
2715 +
2716 +
2717 +5. Contact
2718 +----------------------------------------
2719 +When you have any problems or strange behaviour in aufs, please let me
2720 +know with:
2721 +- /proc/mounts (instead of the output of mount(8))
2722 +- /sys/module/aufs/*
2723 +- /sys/fs/aufs/* (if you have them)
2724 +- /debug/aufs/* (if you have them)
2725 +- linux kernel version
2726 +  if your kernel is not plain, for example modified by distributor,
2727 +  the url where i can download its source is necessary too.
2728 +- aufs version which was printed at loading the module or booting the
2729 +  system, instead of the date you downloaded.
2730 +- configuration (define/undefine CONFIG_AUFS_xxx)
2731 +- kernel configuration or /proc/config.gz (if you have it)
2732 +- LSM (linux security module, if you are using)
2733 +- behaviour which you think to be incorrect
2734 +- actual operation, reproducible one is better
2735 +- mailto: aufs-users at lists.sourceforge.net
2736 +
2737 +Usually, I don't watch the Public Areas(Bugs, Support Requests, Patches,
2738 +and Feature Requests) on SourceForge. Please join and write to
2739 +aufs-users ML.
2740 +
2741 +
2742 +6. Acknowledgements
2743 +----------------------------------------
2744 +Thanks to everyone who have tried and are using aufs, whoever
2745 +have reported a bug or any feedback.
2746 +
2747 +Especially donators:
2748 +Tomas Matejicek(slax.org) made a donation (much more than once).
2749 +       Since Apr 2010, Tomas M (the author of Slax and Linux Live
2750 +       scripts) is making "doubling" donations.
2751 +       Unfortunately I cannot list all of the donators, but I really
2752 +       appreciate.
2753 +       It ends Aug 2010, but the ordinary donation URL is still available.
2754 +       <http://sourceforge.net/donate/index.php?group_id=167503>
2755 +Dai Itasaka made a donation (2007/8).
2756 +Chuck Smith made a donation (2008/4, 10 and 12).
2757 +Henk Schoneveld made a donation (2008/9).
2758 +Chih-Wei Huang, ASUS, CTC donated Eee PC 4G (2008/10).
2759 +Francois Dupoux made a donation (2008/11).
2760 +Bruno Cesar Ribas and Luis Carlos Erpen de Bona, C3SL serves public
2761 +       aufs2 GIT tree (2009/2).
2762 +William Grant made a donation (2009/3).
2763 +Patrick Lane made a donation (2009/4).
2764 +The Mail Archive (mail-archive.com) made donations (2009/5).
2765 +Nippy Networks (Ed Wildgoose) made a donation (2009/7).
2766 +New Dream Network, LLC (www.dreamhost.com) made a donation (2009/11).
2767 +Pavel Pronskiy made a donation (2011/2).
2768 +Iridium and Inmarsat satellite phone retailer (www.mailasail.com), Nippy
2769 +       Networks (Ed Wildgoose) made a donation for hardware (2011/3).
2770 +Max Lekomcev (DOM-TV project) made a donation (2011/7, 12, 2012/3, 6 and
2771 +11).
2772 +Sam Liddicott made a donation (2011/9).
2773 +Era Scarecrow made a donation (2013/4).
2774 +Bor Ratajc made a donation (2013/4).
2775 +Alessandro Gorreta made a donation (2013/4).
2776 +POIRETTE Marc made a donation (2013/4).
2777 +Alessandro Gorreta made a donation (2013/4).
2778 +lauri kasvandik made a donation (2013/5).
2779 +"pemasu from Finland" made a donation (2013/7).
2780 +The Parted Magic Project made a donation (2013/9 and 11).
2781 +Pavel Barta made a donation (2013/10).
2782 +Nikolay Pertsev made a donation (2014/5).
2783 +James B made a donation (2014/7, 2015/7, and 2021/12).
2784 +Stefano Di Biase made a donation (2014/8).
2785 +Daniel Epellei made a donation (2015/1).
2786 +OmegaPhil made a donation (2016/1, 2018/4).
2787 +Tomasz Szewczyk made a donation (2016/4).
2788 +James Burry made a donation (2016/12).
2789 +Carsten Rose made a donation (2018/9).
2790 +Porteus Kiosk made a donation (2018/10).
2791 +
2792 +Thank you very much.
2793 +Donations are always, including future donations, very important and
2794 +helpful for me to keep on developing aufs.
2795 +
2796 +
2797 +7.
2798 +----------------------------------------
2799 +If you are an experienced user, no explanation is needed. Aufs is
2800 +just a linux filesystem.
2801 +
2802 +
2803 +Enjoy!
2804 +
2805 +# Local variables: ;
2806 +# mode: text;
2807 +# End: ;
2808 diff -urN /usr/share/empty/fs/aufs/aufs.h linux/fs/aufs/aufs.h
2809 --- /usr/share/empty/fs/aufs/aufs.h     1970-01-01 01:00:00.000000000 +0100
2810 +++ linux/fs/aufs/aufs.h        2022-03-21 14:49:05.716633010 +0100
2811 @@ -0,0 +1,62 @@
2812 +/* SPDX-License-Identifier: GPL-2.0 */
2813 +/*
2814 + * Copyright (C) 2005-2021 Junjiro R. Okajima
2815 + *
2816 + * This program, aufs is free software; you can redistribute it and/or modify
2817 + * it under the terms of the GNU General Public License as published by
2818 + * the Free Software Foundation; either version 2 of the License, or
2819 + * (at your option) any later version.
2820 + *
2821 + * This program is distributed in the hope that it will be useful,
2822 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2823 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2824 + * GNU General Public License for more details.
2825 + *
2826 + * You should have received a copy of the GNU General Public License
2827 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
2828 + */
2829 +
2830 +/*
2831 + * all header files
2832 + */
2833 +
2834 +#ifndef __AUFS_H__
2835 +#define __AUFS_H__
2836 +
2837 +#ifdef __KERNEL__
2838 +
2839 +#define AuStub(type, name, body, ...) \
2840 +       static inline type name(__VA_ARGS__) { body; }
2841 +
2842 +#define AuStubVoid(name, ...) \
2843 +       AuStub(void, name, , __VA_ARGS__)
2844 +#define AuStubInt0(name, ...) \
2845 +       AuStub(int, name, return 0, __VA_ARGS__)
2846 +
2847 +#include "debug.h"
2848 +
2849 +#include "branch.h"
2850 +#include "cpup.h"
2851 +#include "dcsub.h"
2852 +#include "dbgaufs.h"
2853 +#include "dentry.h"
2854 +#include "dir.h"
2855 +#include "dirren.h"
2856 +#include "dynop.h"
2857 +#include "file.h"
2858 +#include "fstype.h"
2859 +#include "hbl.h"
2860 +#include "inode.h"
2861 +#include "lcnt.h"
2862 +#include "loop.h"
2863 +#include "module.h"
2864 +#include "opts.h"
2865 +#include "rwsem.h"
2866 +#include "super.h"
2867 +#include "sysaufs.h"
2868 +#include "vfsub.h"
2869 +#include "whout.h"
2870 +#include "wkq.h"
2871 +
2872 +#endif /* __KERNEL__ */
2873 +#endif /* __AUFS_H__ */
2874 diff -urN /usr/share/empty/fs/aufs/branch.c linux/fs/aufs/branch.c
2875 --- /usr/share/empty/fs/aufs/branch.c   1970-01-01 01:00:00.000000000 +0100
2876 +++ linux/fs/aufs/branch.c      2022-03-21 14:49:05.719966344 +0100
2877 @@ -0,0 +1,1427 @@
2878 +// SPDX-License-Identifier: GPL-2.0
2879 +/*
2880 + * Copyright (C) 2005-2021 Junjiro R. Okajima
2881 + *
2882 + * This program, aufs is free software; you can redistribute it and/or modify
2883 + * it under the terms of the GNU General Public License as published by
2884 + * the Free Software Foundation; either version 2 of the License, or
2885 + * (at your option) any later version.
2886 + *
2887 + * This program is distributed in the hope that it will be useful,
2888 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2889 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2890 + * GNU General Public License for more details.
2891 + *
2892 + * You should have received a copy of the GNU General Public License
2893 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
2894 + */
2895 +
2896 +/*
2897 + * branch management
2898 + */
2899 +
2900 +#include <linux/compat.h>
2901 +#include <linux/statfs.h>
2902 +#include "aufs.h"
2903 +
2904 +/*
2905 + * free a single branch
2906 + */
2907 +static void au_br_do_free(struct au_branch *br)
2908 +{
2909 +       int i;
2910 +       struct au_wbr *wbr;
2911 +       struct au_dykey **key;
2912 +
2913 +       au_hnotify_fin_br(br);
2914 +       /* always, regardless the mount option */
2915 +       au_dr_hino_free(&br->br_dirren);
2916 +       au_xino_put(br);
2917 +
2918 +       AuLCntZero(au_lcnt_read(&br->br_nfiles, /*do_rev*/0));
2919 +       au_lcnt_fin(&br->br_nfiles, /*do_sync*/0);
2920 +       AuLCntZero(au_lcnt_read(&br->br_count, /*do_rev*/0));
2921 +       au_lcnt_fin(&br->br_count, /*do_sync*/0);
2922 +
2923 +       wbr = br->br_wbr;
2924 +       if (wbr) {
2925 +               for (i = 0; i < AuBrWh_Last; i++)
2926 +                       dput(wbr->wbr_wh[i]);
2927 +               AuDebugOn(atomic_read(&wbr->wbr_wh_running));
2928 +               AuRwDestroy(&wbr->wbr_wh_rwsem);
2929 +       }
2930 +
2931 +       if (br->br_fhsm) {
2932 +               au_br_fhsm_fin(br->br_fhsm);
2933 +               au_kfree_try_rcu(br->br_fhsm);
2934 +       }
2935 +
2936 +       key = br->br_dykey;
2937 +       for (i = 0; i < AuBrDynOp; i++, key++)
2938 +               if (*key)
2939 +                       au_dy_put(*key);
2940 +               else
2941 +                       break;
2942 +
2943 +       /* recursive lock, s_umount of branch's */
2944 +       /* synchronize_rcu(); */ /* why? */
2945 +       lockdep_off();
2946 +       path_put(&br->br_path);
2947 +       lockdep_on();
2948 +       au_kfree_rcu(wbr);
2949 +       au_lcnt_wait_for_fin(&br->br_nfiles);
2950 +       au_lcnt_wait_for_fin(&br->br_count);
2951 +       /* I don't know why, but percpu_refcount requires this */
2952 +       /* synchronize_rcu(); */
2953 +       au_kfree_rcu(br);
2954 +}
2955 +
2956 +/*
2957 + * frees all branches
2958 + */
2959 +void au_br_free(struct au_sbinfo *sbinfo)
2960 +{
2961 +       aufs_bindex_t bmax;
2962 +       struct au_branch **br;
2963 +
2964 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
2965 +
2966 +       bmax = sbinfo->si_bbot + 1;
2967 +       br = sbinfo->si_branch;
2968 +       while (bmax--)
2969 +               au_br_do_free(*br++);
2970 +}
2971 +
2972 +/*
2973 + * find the index of a branch which is specified by @br_id.
2974 + */
2975 +int au_br_index(struct super_block *sb, aufs_bindex_t br_id)
2976 +{
2977 +       aufs_bindex_t bindex, bbot;
2978 +
2979 +       bbot = au_sbbot(sb);
2980 +       for (bindex = 0; bindex <= bbot; bindex++)
2981 +               if (au_sbr_id(sb, bindex) == br_id)
2982 +                       return bindex;
2983 +       return -1;
2984 +}
2985 +
2986 +/* ---------------------------------------------------------------------- */
2987 +
2988 +/*
2989 + * add a branch
2990 + */
2991 +
2992 +static int test_overlap(struct super_block *sb, struct dentry *h_adding,
2993 +                       struct dentry *h_root)
2994 +{
2995 +       if (unlikely(h_adding == h_root
2996 +                    || au_test_loopback_overlap(sb, h_adding)))
2997 +               return 1;
2998 +       if (h_adding->d_sb != h_root->d_sb)
2999 +               return 0;
3000 +       return au_test_subdir(h_adding, h_root)
3001 +               || au_test_subdir(h_root, h_adding);
3002 +}
3003 +
3004 +/*
3005 + * returns a newly allocated branch. @new_nbranch is a number of branches
3006 + * after adding a branch.
3007 + */
3008 +static struct au_branch *au_br_alloc(struct super_block *sb, int new_nbranch,
3009 +                                    int perm)
3010 +{
3011 +       struct au_branch *add_branch;
3012 +       struct dentry *root;
3013 +       struct inode *inode;
3014 +       int err;
3015 +
3016 +       err = -ENOMEM;
3017 +       add_branch = kzalloc(sizeof(*add_branch), GFP_NOFS);
3018 +       if (unlikely(!add_branch))
3019 +               goto out;
3020 +       add_branch->br_xino = au_xino_alloc(/*nfile*/1);
3021 +       if (unlikely(!add_branch->br_xino))
3022 +               goto out_br;
3023 +       err = au_hnotify_init_br(add_branch, perm);
3024 +       if (unlikely(err))
3025 +               goto out_xino;
3026 +
3027 +       if (au_br_writable(perm)) {
3028 +               /* may be freed separately at changing the branch permission */
3029 +               add_branch->br_wbr = kzalloc(sizeof(*add_branch->br_wbr),
3030 +                                            GFP_NOFS);
3031 +               if (unlikely(!add_branch->br_wbr))
3032 +                       goto out_hnotify;
3033 +       }
3034 +
3035 +       if (au_br_fhsm(perm)) {
3036 +               err = au_fhsm_br_alloc(add_branch);
3037 +               if (unlikely(err))
3038 +                       goto out_wbr;
3039 +       }
3040 +
3041 +       root = sb->s_root;
3042 +       err = au_sbr_realloc(au_sbi(sb), new_nbranch, /*may_shrink*/0);
3043 +       if (!err)
3044 +               err = au_di_realloc(au_di(root), new_nbranch, /*may_shrink*/0);
3045 +       if (!err) {
3046 +               inode = d_inode(root);
3047 +               err = au_hinode_realloc(au_ii(inode), new_nbranch,
3048 +                                       /*may_shrink*/0);
3049 +       }
3050 +       if (!err)
3051 +               return add_branch; /* success */
3052 +
3053 +out_wbr:
3054 +       au_kfree_rcu(add_branch->br_wbr);
3055 +out_hnotify:
3056 +       au_hnotify_fin_br(add_branch);
3057 +out_xino:
3058 +       au_xino_put(add_branch);
3059 +out_br:
3060 +       au_kfree_rcu(add_branch);
3061 +out:
3062 +       return ERR_PTR(err);
3063 +}
3064 +
3065 +/*
3066 + * test if the branch permission is legal or not.
3067 + */
3068 +static int test_br(struct inode *inode, int brperm, char *path)
3069 +{
3070 +       int err;
3071 +
3072 +       err = (au_br_writable(brperm) && IS_RDONLY(inode));
3073 +       if (!err)
3074 +               goto out;
3075 +
3076 +       err = -EINVAL;
3077 +       pr_err("write permission for readonly mount or inode, %s\n", path);
3078 +
3079 +out:
3080 +       return err;
3081 +}
3082 +
3083 +/*
3084 + * returns:
3085 + * 0: success, the caller will add it
3086 + * plus: success, it is already unified, the caller should ignore it
3087 + * minus: error
3088 + */
3089 +static int test_add(struct super_block *sb, struct au_opt_add *add, int remount)
3090 +{
3091 +       int err;
3092 +       aufs_bindex_t bbot, bindex;
3093 +       struct dentry *root, *h_dentry;
3094 +       struct inode *inode, *h_inode;
3095 +
3096 +       root = sb->s_root;
3097 +       bbot = au_sbbot(sb);
3098 +       if (unlikely(bbot >= 0
3099 +                    && au_find_dbindex(root, add->path.dentry) >= 0)) {
3100 +               err = 1;
3101 +               if (!remount) {
3102 +                       err = -EINVAL;
3103 +                       pr_err("%s duplicated\n", add->pathname);
3104 +               }
3105 +               goto out;
3106 +       }
3107 +
3108 +       err = -ENOSPC; /* -E2BIG; */
3109 +       if (unlikely(AUFS_BRANCH_MAX <= add->bindex
3110 +                    || AUFS_BRANCH_MAX - 1 <= bbot)) {
3111 +               pr_err("number of branches exceeded %s\n", add->pathname);
3112 +               goto out;
3113 +       }
3114 +
3115 +       err = -EDOM;
3116 +       if (unlikely(add->bindex < 0 || bbot + 1 < add->bindex)) {
3117 +               pr_err("bad index %d\n", add->bindex);
3118 +               goto out;
3119 +       }
3120 +
3121 +       inode = d_inode(add->path.dentry);
3122 +       err = -ENOENT;
3123 +       if (unlikely(!inode->i_nlink)) {
3124 +               pr_err("no existence %s\n", add->pathname);
3125 +               goto out;
3126 +       }
3127 +
3128 +       err = -EINVAL;
3129 +       if (unlikely(inode->i_sb == sb)) {
3130 +               pr_err("%s must be outside\n", add->pathname);
3131 +               goto out;
3132 +       }
3133 +
3134 +       if (unlikely(au_test_fs_unsuppoted(inode->i_sb))) {
3135 +               pr_err("unsupported filesystem, %s (%s)\n",
3136 +                      add->pathname, au_sbtype(inode->i_sb));
3137 +               goto out;
3138 +       }
3139 +
3140 +       if (unlikely(inode->i_sb->s_stack_depth)) {
3141 +               pr_err("already stacked, %s (%s)\n",
3142 +                      add->pathname, au_sbtype(inode->i_sb));
3143 +               goto out;
3144 +       }
3145 +
3146 +       err = test_br(d_inode(add->path.dentry), add->perm, add->pathname);
3147 +       if (unlikely(err))
3148 +               goto out;
3149 +
3150 +       if (bbot < 0)
3151 +               return 0; /* success */
3152 +
3153 +       err = -EINVAL;
3154 +       for (bindex = 0; bindex <= bbot; bindex++)
3155 +               if (unlikely(test_overlap(sb, add->path.dentry,
3156 +                                         au_h_dptr(root, bindex)))) {
3157 +                       pr_err("%s is overlapped\n", add->pathname);
3158 +                       goto out;
3159 +               }
3160 +
3161 +       err = 0;
3162 +       if (au_opt_test(au_mntflags(sb), WARN_PERM)) {
3163 +               h_dentry = au_h_dptr(root, 0);
3164 +               h_inode = d_inode(h_dentry);
3165 +               if ((h_inode->i_mode & S_IALLUGO) != (inode->i_mode & S_IALLUGO)
3166 +                   || !uid_eq(h_inode->i_uid, inode->i_uid)
3167 +                   || !gid_eq(h_inode->i_gid, inode->i_gid))
3168 +                       pr_warn("uid/gid/perm %s %u/%u/0%o, %u/%u/0%o\n",
3169 +                               add->pathname,
3170 +                               i_uid_read(inode), i_gid_read(inode),
3171 +                               (inode->i_mode & S_IALLUGO),
3172 +                               i_uid_read(h_inode), i_gid_read(h_inode),
3173 +                               (h_inode->i_mode & S_IALLUGO));
3174 +       }
3175 +
3176 +out:
3177 +       return err;
3178 +}
3179 +
3180 +/*
3181 + * initialize or clean the whiteouts for an adding branch
3182 + */
3183 +static int au_br_init_wh(struct super_block *sb, struct au_branch *br,
3184 +                        int new_perm)
3185 +{
3186 +       int err, old_perm;
3187 +       aufs_bindex_t bindex;
3188 +       struct inode *h_inode;
3189 +       struct au_wbr *wbr;
3190 +       struct au_hinode *hdir;
3191 +       struct dentry *h_dentry;
3192 +
3193 +       err = vfsub_mnt_want_write(au_br_mnt(br));
3194 +       if (unlikely(err))
3195 +               goto out;
3196 +
3197 +       wbr = br->br_wbr;
3198 +       old_perm = br->br_perm;
3199 +       br->br_perm = new_perm;
3200 +       hdir = NULL;
3201 +       h_inode = NULL;
3202 +       bindex = au_br_index(sb, br->br_id);
3203 +       if (0 <= bindex) {
3204 +               hdir = au_hi(d_inode(sb->s_root), bindex);
3205 +               au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
3206 +       } else {
3207 +               h_dentry = au_br_dentry(br);
3208 +               h_inode = d_inode(h_dentry);
3209 +               inode_lock_nested(h_inode, AuLsc_I_PARENT);
3210 +       }
3211 +       if (!wbr)
3212 +               err = au_wh_init(br, sb);
3213 +       else {
3214 +               wbr_wh_write_lock(wbr);
3215 +               err = au_wh_init(br, sb);
3216 +               wbr_wh_write_unlock(wbr);
3217 +       }
3218 +       if (hdir)
3219 +               au_hn_inode_unlock(hdir);
3220 +       else
3221 +               inode_unlock(h_inode);
3222 +       vfsub_mnt_drop_write(au_br_mnt(br));
3223 +       br->br_perm = old_perm;
3224 +
3225 +       if (!err && wbr && !au_br_writable(new_perm)) {
3226 +               au_kfree_rcu(wbr);
3227 +               br->br_wbr = NULL;
3228 +       }
3229 +
3230 +out:
3231 +       return err;
3232 +}
3233 +
3234 +static int au_wbr_init(struct au_branch *br, struct super_block *sb,
3235 +                      int perm)
3236 +{
3237 +       int err;
3238 +       struct kstatfs kst;
3239 +       struct au_wbr *wbr;
3240 +
3241 +       wbr = br->br_wbr;
3242 +       au_rw_init(&wbr->wbr_wh_rwsem);
3243 +       atomic_set(&wbr->wbr_wh_running, 0);
3244 +
3245 +       /*
3246 +        * a limit for rmdir/rename a dir
3247 +        * cf. AUFS_MAX_NAMELEN in include/uapi/linux/aufs_type.h
3248 +        */
3249 +       err = vfs_statfs(&br->br_path, &kst);
3250 +       if (unlikely(err))
3251 +               goto out;
3252 +       err = -EINVAL;
3253 +       if (kst.f_namelen >= NAME_MAX)
3254 +               err = au_br_init_wh(sb, br, perm);
3255 +       else
3256 +               pr_err("%pd(%s), unsupported namelen %ld\n",
3257 +                      au_br_dentry(br),
3258 +                      au_sbtype(au_br_dentry(br)->d_sb), kst.f_namelen);
3259 +
3260 +out:
3261 +       return err;
3262 +}
3263 +
3264 +/* initialize a new branch */
3265 +static int au_br_init(struct au_branch *br, struct super_block *sb,
3266 +                     struct au_opt_add *add)
3267 +{
3268 +       int err;
3269 +       struct au_branch *brbase;
3270 +       struct file *xf;
3271 +       struct inode *h_inode;
3272 +
3273 +       err = 0;
3274 +       br->br_perm = add->perm;
3275 +       br->br_path = add->path; /* set first, path_get() later */
3276 +       spin_lock_init(&br->br_dykey_lock);
3277 +       au_lcnt_init(&br->br_nfiles, /*release*/NULL);
3278 +       au_lcnt_init(&br->br_count, /*release*/NULL);
3279 +       br->br_id = au_new_br_id(sb);
3280 +       AuDebugOn(br->br_id < 0);
3281 +
3282 +       /* always, regardless the given option */
3283 +       err = au_dr_br_init(sb, br, &add->path);
3284 +       if (unlikely(err))
3285 +               goto out_err;
3286 +
3287 +       if (au_br_writable(add->perm)) {
3288 +               err = au_wbr_init(br, sb, add->perm);
3289 +               if (unlikely(err))
3290 +                       goto out_err;
3291 +       }
3292 +
3293 +       if (au_opt_test(au_mntflags(sb), XINO)) {
3294 +               brbase = au_sbr(sb, 0);
3295 +               xf = au_xino_file(brbase->br_xino, /*idx*/-1);
3296 +               AuDebugOn(!xf);
3297 +               h_inode = d_inode(add->path.dentry);
3298 +               err = au_xino_init_br(sb, br, h_inode->i_ino, &xf->f_path);
3299 +               if (unlikely(err)) {
3300 +                       AuDebugOn(au_xino_file(br->br_xino, /*idx*/-1));
3301 +                       goto out_err;
3302 +               }
3303 +       }
3304 +
3305 +       sysaufs_br_init(br);
3306 +       path_get(&br->br_path);
3307 +       goto out; /* success */
3308 +
3309 +out_err:
3310 +       memset(&br->br_path, 0, sizeof(br->br_path));
3311 +out:
3312 +       return err;
3313 +}
3314 +
3315 +static void au_br_do_add_brp(struct au_sbinfo *sbinfo, aufs_bindex_t bindex,
3316 +                            struct au_branch *br, aufs_bindex_t bbot,
3317 +                            aufs_bindex_t amount)
3318 +{
3319 +       struct au_branch **brp;
3320 +
3321 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
3322 +
3323 +       brp = sbinfo->si_branch + bindex;
3324 +       memmove(brp + 1, brp, sizeof(*brp) * amount);
3325 +       *brp = br;
3326 +       sbinfo->si_bbot++;
3327 +       if (unlikely(bbot < 0))
3328 +               sbinfo->si_bbot = 0;
3329 +}
3330 +
3331 +static void au_br_do_add_hdp(struct au_dinfo *dinfo, aufs_bindex_t bindex,
3332 +                            aufs_bindex_t bbot, aufs_bindex_t amount)
3333 +{
3334 +       struct au_hdentry *hdp;
3335 +
3336 +       AuRwMustWriteLock(&dinfo->di_rwsem);
3337 +
3338 +       hdp = au_hdentry(dinfo, bindex);
3339 +       memmove(hdp + 1, hdp, sizeof(*hdp) * amount);
3340 +       au_h_dentry_init(hdp);
3341 +       dinfo->di_bbot++;
3342 +       if (unlikely(bbot < 0))
3343 +               dinfo->di_btop = 0;
3344 +}
3345 +
3346 +static void au_br_do_add_hip(struct au_iinfo *iinfo, aufs_bindex_t bindex,
3347 +                            aufs_bindex_t bbot, aufs_bindex_t amount)
3348 +{
3349 +       struct au_hinode *hip;
3350 +
3351 +       AuRwMustWriteLock(&iinfo->ii_rwsem);
3352 +
3353 +       hip = au_hinode(iinfo, bindex);
3354 +       memmove(hip + 1, hip, sizeof(*hip) * amount);
3355 +       au_hinode_init(hip);
3356 +       iinfo->ii_bbot++;
3357 +       if (unlikely(bbot < 0))
3358 +               iinfo->ii_btop = 0;
3359 +}
3360 +
3361 +static void au_br_do_add(struct super_block *sb, struct au_branch *br,
3362 +                        aufs_bindex_t bindex)
3363 +{
3364 +       struct dentry *root, *h_dentry;
3365 +       struct inode *root_inode, *h_inode;
3366 +       aufs_bindex_t bbot, amount;
3367 +
3368 +       root = sb->s_root;
3369 +       root_inode = d_inode(root);
3370 +       bbot = au_sbbot(sb);
3371 +       amount = bbot + 1 - bindex;
3372 +       h_dentry = au_br_dentry(br);
3373 +       au_sbilist_lock();
3374 +       au_br_do_add_brp(au_sbi(sb), bindex, br, bbot, amount);
3375 +       au_br_do_add_hdp(au_di(root), bindex, bbot, amount);
3376 +       au_br_do_add_hip(au_ii(root_inode), bindex, bbot, amount);
3377 +       au_set_h_dptr(root, bindex, dget(h_dentry));
3378 +       h_inode = d_inode(h_dentry);
3379 +       au_set_h_iptr(root_inode, bindex, au_igrab(h_inode), /*flags*/0);
3380 +       au_sbilist_unlock();
3381 +}
3382 +
3383 +int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount)
3384 +{
3385 +       int err;
3386 +       aufs_bindex_t bbot, add_bindex;
3387 +       struct dentry *root, *h_dentry;
3388 +       struct inode *root_inode;
3389 +       struct au_branch *add_branch;
3390 +
3391 +       root = sb->s_root;
3392 +       root_inode = d_inode(root);
3393 +       IMustLock(root_inode);
3394 +       IiMustWriteLock(root_inode);
3395 +       err = test_add(sb, add, remount);
3396 +       if (unlikely(err < 0))
3397 +               goto out;
3398 +       if (err) {
3399 +               err = 0;
3400 +               goto out; /* success */
3401 +       }
3402 +
3403 +       bbot = au_sbbot(sb);
3404 +       add_branch = au_br_alloc(sb, bbot + 2, add->perm);
3405 +       err = PTR_ERR(add_branch);
3406 +       if (IS_ERR(add_branch))
3407 +               goto out;
3408 +
3409 +       err = au_br_init(add_branch, sb, add);
3410 +       if (unlikely(err)) {
3411 +               au_br_do_free(add_branch);
3412 +               goto out;
3413 +       }
3414 +
3415 +       add_bindex = add->bindex;
3416 +       sysaufs_brs_del(sb, add_bindex);        /* remove successors */
3417 +       au_br_do_add(sb, add_branch, add_bindex);
3418 +       sysaufs_brs_add(sb, add_bindex);        /* append successors */
3419 +       dbgaufs_brs_add(sb, add_bindex, /*topdown*/0);  /* rename successors */
3420 +
3421 +       h_dentry = add->path.dentry;
3422 +       if (!add_bindex) {
3423 +               au_cpup_attr_all(root_inode, /*force*/1);
3424 +               sb->s_maxbytes = h_dentry->d_sb->s_maxbytes;
3425 +       } else
3426 +               au_add_nlink(root_inode, d_inode(h_dentry));
3427 +
3428 +out:
3429 +       return err;
3430 +}
3431 +
3432 +/* ---------------------------------------------------------------------- */
3433 +
3434 +static unsigned long long au_farray_cb(struct super_block *sb, void *a,
3435 +                                      unsigned long long max __maybe_unused,
3436 +                                      void *arg)
3437 +{
3438 +       unsigned long long n;
3439 +       struct file **p, *f;
3440 +       struct hlist_bl_head *files;
3441 +       struct hlist_bl_node *pos;
3442 +       struct au_finfo *finfo;
3443 +
3444 +       n = 0;
3445 +       p = a;
3446 +       files = &au_sbi(sb)->si_files;
3447 +       hlist_bl_lock(files);
3448 +       hlist_bl_for_each_entry(finfo, pos, files, fi_hlist) {
3449 +               f = finfo->fi_file;
3450 +               if (file_count(f)
3451 +                   && !special_file(file_inode(f)->i_mode)) {
3452 +                       get_file(f);
3453 +                       *p++ = f;
3454 +                       n++;
3455 +                       AuDebugOn(n > max);
3456 +               }
3457 +       }
3458 +       hlist_bl_unlock(files);
3459 +
3460 +       return n;
3461 +}
3462 +
3463 +static struct file **au_farray_alloc(struct super_block *sb,
3464 +                                    unsigned long long *max)
3465 +{
3466 +       struct au_sbinfo *sbi;
3467 +
3468 +       sbi = au_sbi(sb);
3469 +       *max = au_lcnt_read(&sbi->si_nfiles, /*do_rev*/1);
3470 +       return au_array_alloc(max, au_farray_cb, sb, /*arg*/NULL);
3471 +}
3472 +
3473 +static void au_farray_free(struct file **a, unsigned long long max)
3474 +{
3475 +       unsigned long long ull;
3476 +
3477 +       for (ull = 0; ull < max; ull++)
3478 +               if (a[ull])
3479 +                       fput(a[ull]);
3480 +       kvfree(a);
3481 +}
3482 +
3483 +/* ---------------------------------------------------------------------- */
3484 +
3485 +/*
3486 + * delete a branch
3487 + */
3488 +
3489 +/* to show the line number, do not make it inlined function */
3490 +#define AuVerbose(do_info, fmt, ...) do { \
3491 +       if (do_info) \
3492 +               pr_info(fmt, ##__VA_ARGS__); \
3493 +} while (0)
3494 +
3495 +static int au_test_ibusy(struct inode *inode, aufs_bindex_t btop,
3496 +                        aufs_bindex_t bbot)
3497 +{
3498 +       return (inode && !S_ISDIR(inode->i_mode)) || btop == bbot;
3499 +}
3500 +
3501 +static int au_test_dbusy(struct dentry *dentry, aufs_bindex_t btop,
3502 +                        aufs_bindex_t bbot)
3503 +{
3504 +       return au_test_ibusy(d_inode(dentry), btop, bbot);
3505 +}
3506 +
3507 +/*
3508 + * test if the branch is deletable or not.
3509 + */
3510 +static int test_dentry_busy(struct dentry *root, aufs_bindex_t bindex,
3511 +                           unsigned int sigen, const unsigned int verbose)
3512 +{
3513 +       int err, i, j, ndentry;
3514 +       aufs_bindex_t btop, bbot;
3515 +       struct au_dcsub_pages dpages;
3516 +       struct au_dpage *dpage;
3517 +       struct dentry *d;
3518 +
3519 +       err = au_dpages_init(&dpages, GFP_NOFS);
3520 +       if (unlikely(err))
3521 +               goto out;
3522 +       err = au_dcsub_pages(&dpages, root, NULL, NULL);
3523 +       if (unlikely(err))
3524 +               goto out_dpages;
3525 +
3526 +       for (i = 0; !err && i < dpages.ndpage; i++) {
3527 +               dpage = dpages.dpages + i;
3528 +               ndentry = dpage->ndentry;
3529 +               for (j = 0; !err && j < ndentry; j++) {
3530 +                       d = dpage->dentries[j];
3531 +                       AuDebugOn(au_dcount(d) <= 0);
3532 +                       if (!au_digen_test(d, sigen)) {
3533 +                               di_read_lock_child(d, AuLock_IR);
3534 +                               if (unlikely(au_dbrange_test(d))) {
3535 +                                       di_read_unlock(d, AuLock_IR);
3536 +                                       continue;
3537 +                               }
3538 +                       } else {
3539 +                               di_write_lock_child(d);
3540 +                               if (unlikely(au_dbrange_test(d))) {
3541 +                                       di_write_unlock(d);
3542 +                                       continue;
3543 +                               }
3544 +                               err = au_reval_dpath(d, sigen);
3545 +                               if (!err)
3546 +                                       di_downgrade_lock(d, AuLock_IR);
3547 +                               else {
3548 +                                       di_write_unlock(d);
3549 +                                       break;
3550 +                               }
3551 +                       }
3552 +
3553 +                       /* AuDbgDentry(d); */
3554 +                       btop = au_dbtop(d);
3555 +                       bbot = au_dbbot(d);
3556 +                       if (btop <= bindex
3557 +                           && bindex <= bbot
3558 +                           && au_h_dptr(d, bindex)
3559 +                           && au_test_dbusy(d, btop, bbot)) {
3560 +                               err = -EBUSY;
3561 +                               AuVerbose(verbose, "busy %pd\n", d);
3562 +                               AuDbgDentry(d);
3563 +                       }
3564 +                       di_read_unlock(d, AuLock_IR);
3565 +               }
3566 +       }
3567 +
3568 +out_dpages:
3569 +       au_dpages_free(&dpages);
3570 +out:
3571 +       return err;
3572 +}
3573 +
3574 +static int test_inode_busy(struct super_block *sb, aufs_bindex_t bindex,
3575 +                          unsigned int sigen, const unsigned int verbose)
3576 +{
3577 +       int err;
3578 +       unsigned long long max, ull;
3579 +       struct inode *i, **array;
3580 +       aufs_bindex_t btop, bbot;
3581 +
3582 +       array = au_iarray_alloc(sb, &max);
3583 +       err = PTR_ERR(array);
3584 +       if (IS_ERR(array))
3585 +               goto out;
3586 +
3587 +       err = 0;
3588 +       AuDbg("b%d\n", bindex);
3589 +       for (ull = 0; !err && ull < max; ull++) {
3590 +               i = array[ull];
3591 +               if (unlikely(!i))
3592 +                       break;
3593 +               if (i->i_ino == AUFS_ROOT_INO)
3594 +                       continue;
3595 +
3596 +               /* AuDbgInode(i); */
3597 +               if (au_iigen(i, NULL) == sigen)
3598 +                       ii_read_lock_child(i);
3599 +               else {
3600 +                       ii_write_lock_child(i);
3601 +                       err = au_refresh_hinode_self(i);
3602 +                       au_iigen_dec(i);
3603 +                       if (!err)
3604 +                               ii_downgrade_lock(i);
3605 +                       else {
3606 +                               ii_write_unlock(i);
3607 +                               break;
3608 +                       }
3609 +               }
3610 +
3611 +               btop = au_ibtop(i);
3612 +               bbot = au_ibbot(i);
3613 +               if (btop <= bindex
3614 +                   && bindex <= bbot
3615 +                   && au_h_iptr(i, bindex)
3616 +                   && au_test_ibusy(i, btop, bbot)) {
3617 +                       err = -EBUSY;
3618 +                       AuVerbose(verbose, "busy i%lu\n", i->i_ino);
3619 +                       AuDbgInode(i);
3620 +               }
3621 +               ii_read_unlock(i);
3622 +       }
3623 +       au_iarray_free(array, max);
3624 +
3625 +out:
3626 +       return err;
3627 +}
3628 +
3629 +static int test_children_busy(struct dentry *root, aufs_bindex_t bindex,
3630 +                             const unsigned int verbose)
3631 +{
3632 +       int err;
3633 +       unsigned int sigen;
3634 +
3635 +       sigen = au_sigen(root->d_sb);
3636 +       DiMustNoWaiters(root);
3637 +       IiMustNoWaiters(d_inode(root));
3638 +       di_write_unlock(root);
3639 +       err = test_dentry_busy(root, bindex, sigen, verbose);
3640 +       if (!err)
3641 +               err = test_inode_busy(root->d_sb, bindex, sigen, verbose);
3642 +       di_write_lock_child(root); /* aufs_write_lock() calls ..._child() */
3643 +
3644 +       return err;
3645 +}
3646 +
3647 +static int test_dir_busy(struct file *file, aufs_bindex_t br_id,
3648 +                        struct file **to_free, int *idx)
3649 +{
3650 +       int err;
3651 +       unsigned char matched, root;
3652 +       aufs_bindex_t bindex, bbot;
3653 +       struct au_fidir *fidir;
3654 +       struct au_hfile *hfile;
3655 +
3656 +       err = 0;
3657 +       root = IS_ROOT(file->f_path.dentry);
3658 +       if (root) {
3659 +               get_file(file);
3660 +               to_free[*idx] = file;
3661 +               (*idx)++;
3662 +               goto out;
3663 +       }
3664 +
3665 +       matched = 0;
3666 +       fidir = au_fi(file)->fi_hdir;
3667 +       AuDebugOn(!fidir);
3668 +       bbot = au_fbbot_dir(file);
3669 +       for (bindex = au_fbtop(file); bindex <= bbot; bindex++) {
3670 +               hfile = fidir->fd_hfile + bindex;
3671 +               if (!hfile->hf_file)
3672 +                       continue;
3673 +
3674 +               if (hfile->hf_br->br_id == br_id) {
3675 +                       matched = 1;
3676 +                       break;
3677 +               }
3678 +       }
3679 +       if (matched)
3680 +               err = -EBUSY;
3681 +
3682 +out:
3683 +       return err;
3684 +}
3685 +
3686 +static int test_file_busy(struct super_block *sb, aufs_bindex_t br_id,
3687 +                         struct file **to_free, int opened)
3688 +{
3689 +       int err, idx;
3690 +       unsigned long long ull, max;
3691 +       aufs_bindex_t btop;
3692 +       struct file *file, **array;
3693 +       struct dentry *root;
3694 +       struct au_hfile *hfile;
3695 +
3696 +       array = au_farray_alloc(sb, &max);
3697 +       err = PTR_ERR(array);
3698 +       if (IS_ERR(array))
3699 +               goto out;
3700 +
3701 +       err = 0;
3702 +       idx = 0;
3703 +       root = sb->s_root;
3704 +       di_write_unlock(root);
3705 +       for (ull = 0; ull < max; ull++) {
3706 +               file = array[ull];
3707 +               if (unlikely(!file))
3708 +                       break;
3709 +
3710 +               /* AuDbg("%pD\n", file); */
3711 +               fi_read_lock(file);
3712 +               btop = au_fbtop(file);
3713 +               if (!d_is_dir(file->f_path.dentry)) {
3714 +                       hfile = &au_fi(file)->fi_htop;
3715 +                       if (hfile->hf_br->br_id == br_id)
3716 +                               err = -EBUSY;
3717 +               } else
3718 +                       err = test_dir_busy(file, br_id, to_free, &idx);
3719 +               fi_read_unlock(file);
3720 +               if (unlikely(err))
3721 +                       break;
3722 +       }
3723 +       di_write_lock_child(root);
3724 +       au_farray_free(array, max);
3725 +       AuDebugOn(idx > opened);
3726 +
3727 +out:
3728 +       return err;
3729 +}
3730 +
3731 +static void br_del_file(struct file **to_free, unsigned long long opened,
3732 +                       aufs_bindex_t br_id)
3733 +{
3734 +       unsigned long long ull;
3735 +       aufs_bindex_t bindex, btop, bbot, bfound;
3736 +       struct file *file;
3737 +       struct au_fidir *fidir;
3738 +       struct au_hfile *hfile;
3739 +
3740 +       for (ull = 0; ull < opened; ull++) {
3741 +               file = to_free[ull];
3742 +               if (unlikely(!file))
3743 +                       break;
3744 +
3745 +               /* AuDbg("%pD\n", file); */
3746 +               AuDebugOn(!d_is_dir(file->f_path.dentry));
3747 +               bfound = -1;
3748 +               fidir = au_fi(file)->fi_hdir;
3749 +               AuDebugOn(!fidir);
3750 +               fi_write_lock(file);
3751 +               btop = au_fbtop(file);
3752 +               bbot = au_fbbot_dir(file);
3753 +               for (bindex = btop; bindex <= bbot; bindex++) {
3754 +                       hfile = fidir->fd_hfile + bindex;
3755 +                       if (!hfile->hf_file)
3756 +                               continue;
3757 +
3758 +                       if (hfile->hf_br->br_id == br_id) {
3759 +                               bfound = bindex;
3760 +                               break;
3761 +                       }
3762 +               }
3763 +               AuDebugOn(bfound < 0);
3764 +               au_set_h_fptr(file, bfound, NULL);
3765 +               if (bfound == btop) {
3766 +                       for (btop++; btop <= bbot; btop++)
3767 +                               if (au_hf_dir(file, btop)) {
3768 +                                       au_set_fbtop(file, btop);
3769 +                                       break;
3770 +                               }
3771 +               }
3772 +               fi_write_unlock(file);
3773 +       }
3774 +}
3775 +
3776 +static void au_br_do_del_brp(struct au_sbinfo *sbinfo,
3777 +                            const aufs_bindex_t bindex,
3778 +                            const aufs_bindex_t bbot)
3779 +{
3780 +       struct au_branch **brp, **p;
3781 +
3782 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
3783 +
3784 +       brp = sbinfo->si_branch + bindex;
3785 +       if (bindex < bbot)
3786 +               memmove(brp, brp + 1, sizeof(*brp) * (bbot - bindex));
3787 +       sbinfo->si_branch[0 + bbot] = NULL;
3788 +       sbinfo->si_bbot--;
3789 +
3790 +       p = au_krealloc(sbinfo->si_branch, sizeof(*p) * bbot, AuGFP_SBILIST,
3791 +                       /*may_shrink*/1);
3792 +       if (p)
3793 +               sbinfo->si_branch = p;
3794 +       /* harmless error */
3795 +}
3796 +
3797 +static void au_br_do_del_hdp(struct au_dinfo *dinfo, const aufs_bindex_t bindex,
3798 +                            const aufs_bindex_t bbot)
3799 +{
3800 +       struct au_hdentry *hdp, *p;
3801 +
3802 +       AuRwMustWriteLock(&dinfo->di_rwsem);
3803 +
3804 +       hdp = au_hdentry(dinfo, bindex);
3805 +       if (bindex < bbot)
3806 +               memmove(hdp, hdp + 1, sizeof(*hdp) * (bbot - bindex));
3807 +       /* au_h_dentry_init(au_hdentry(dinfo, bbot); */
3808 +       dinfo->di_bbot--;
3809 +
3810 +       p = au_krealloc(dinfo->di_hdentry, sizeof(*p) * bbot, AuGFP_SBILIST,
3811 +                       /*may_shrink*/1);
3812 +       if (p)
3813 +               dinfo->di_hdentry = p;
3814 +       /* harmless error */
3815 +}
3816 +
3817 +static void au_br_do_del_hip(struct au_iinfo *iinfo, const aufs_bindex_t bindex,
3818 +                            const aufs_bindex_t bbot)
3819 +{
3820 +       struct au_hinode *hip, *p;
3821 +
3822 +       AuRwMustWriteLock(&iinfo->ii_rwsem);
3823 +
3824 +       hip = au_hinode(iinfo, bindex);
3825 +       if (bindex < bbot)
3826 +               memmove(hip, hip + 1, sizeof(*hip) * (bbot - bindex));
3827 +       /* au_hinode_init(au_hinode(iinfo, bbot)); */
3828 +       iinfo->ii_bbot--;
3829 +
3830 +       p = au_krealloc(iinfo->ii_hinode, sizeof(*p) * bbot, AuGFP_SBILIST,
3831 +                       /*may_shrink*/1);
3832 +       if (p)
3833 +               iinfo->ii_hinode = p;
3834 +       /* harmless error */
3835 +}
3836 +
3837 +static void au_br_do_del(struct super_block *sb, aufs_bindex_t bindex,
3838 +                        struct au_branch *br)
3839 +{
3840 +       aufs_bindex_t bbot;
3841 +       struct au_sbinfo *sbinfo;
3842 +       struct dentry *root, *h_root;
3843 +       struct inode *inode, *h_inode;
3844 +       struct au_hinode *hinode;
3845 +
3846 +       SiMustWriteLock(sb);
3847 +
3848 +       root = sb->s_root;
3849 +       inode = d_inode(root);
3850 +       sbinfo = au_sbi(sb);
3851 +       bbot = sbinfo->si_bbot;
3852 +
3853 +       h_root = au_h_dptr(root, bindex);
3854 +       hinode = au_hi(inode, bindex);
3855 +       h_inode = au_igrab(hinode->hi_inode);
3856 +       au_hiput(hinode);
3857 +
3858 +       au_sbilist_lock();
3859 +       au_br_do_del_brp(sbinfo, bindex, bbot);
3860 +       au_br_do_del_hdp(au_di(root), bindex, bbot);
3861 +       au_br_do_del_hip(au_ii(inode), bindex, bbot);
3862 +       au_sbilist_unlock();
3863 +
3864 +       /* ignore an error */
3865 +       au_dr_br_fin(sb, br); /* always, regardless the mount option */
3866 +
3867 +       dput(h_root);
3868 +       iput(h_inode);
3869 +       au_br_do_free(br);
3870 +}
3871 +
3872 +static unsigned long long empty_cb(struct super_block *sb, void *array,
3873 +                                  unsigned long long max, void *arg)
3874 +{
3875 +       return max;
3876 +}
3877 +
3878 +int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount)
3879 +{
3880 +       int err, rerr, i;
3881 +       unsigned long long opened;
3882 +       unsigned int mnt_flags;
3883 +       aufs_bindex_t bindex, bbot, br_id;
3884 +       unsigned char do_wh, verbose;
3885 +       struct au_branch *br;
3886 +       struct au_wbr *wbr;
3887 +       struct dentry *root;
3888 +       struct file **to_free;
3889 +
3890 +       err = 0;
3891 +       opened = 0;
3892 +       to_free = NULL;
3893 +       root = sb->s_root;
3894 +       bindex = au_find_dbindex(root, del->h_path.dentry);
3895 +       if (bindex < 0) {
3896 +               if (remount)
3897 +                       goto out; /* success */
3898 +               err = -ENOENT;
3899 +               pr_err("%s no such branch\n", del->pathname);
3900 +               goto out;
3901 +       }
3902 +       AuDbg("bindex b%d\n", bindex);
3903 +
3904 +       err = -EBUSY;
3905 +       mnt_flags = au_mntflags(sb);
3906 +       verbose = !!au_opt_test(mnt_flags, VERBOSE);
3907 +       bbot = au_sbbot(sb);
3908 +       if (unlikely(!bbot)) {
3909 +               AuVerbose(verbose, "no more branches left\n");
3910 +               goto out;
3911 +       }
3912 +
3913 +       br = au_sbr(sb, bindex);
3914 +       AuDebugOn(!path_equal(&br->br_path, &del->h_path));
3915 +       if (unlikely(au_lcnt_read(&br->br_count, /*do_rev*/1))) {
3916 +               AuVerbose(verbose, "br %pd2 is busy now\n", del->h_path.dentry);
3917 +               goto out;
3918 +       }
3919 +
3920 +       br_id = br->br_id;
3921 +       opened = au_lcnt_read(&br->br_nfiles, /*do_rev*/1);
3922 +       if (unlikely(opened)) {
3923 +               to_free = au_array_alloc(&opened, empty_cb, sb, NULL);
3924 +               err = PTR_ERR(to_free);
3925 +               if (IS_ERR(to_free))
3926 +                       goto out;
3927 +
3928 +               err = test_file_busy(sb, br_id, to_free, opened);
3929 +               if (unlikely(err)) {
3930 +                       AuVerbose(verbose, "%llu file(s) opened\n", opened);
3931 +                       goto out;
3932 +               }
3933 +       }
3934 +
3935 +       wbr = br->br_wbr;
3936 +       do_wh = wbr && (wbr->wbr_whbase || wbr->wbr_plink || wbr->wbr_orph);
3937 +       if (do_wh) {
3938 +               /* instead of WbrWhMustWriteLock(wbr) */
3939 +               SiMustWriteLock(sb);
3940 +               for (i = 0; i < AuBrWh_Last; i++) {
3941 +                       dput(wbr->wbr_wh[i]);
3942 +                       wbr->wbr_wh[i] = NULL;
3943 +               }
3944 +       }
3945 +
3946 +       err = test_children_busy(root, bindex, verbose);
3947 +       if (unlikely(err)) {
3948 +               if (do_wh)
3949 +                       goto out_wh;
3950 +               goto out;
3951 +       }
3952 +
3953 +       err = 0;
3954 +       if (to_free) {
3955 +               /*
3956 +                * now we confirmed the branch is deletable.
3957 +                * let's free the remaining opened dirs on the branch.
3958 +                */
3959 +               di_write_unlock(root);
3960 +               br_del_file(to_free, opened, br_id);
3961 +               di_write_lock_child(root);
3962 +       }
3963 +
3964 +       sysaufs_brs_del(sb, bindex);    /* remove successors */
3965 +       dbgaufs_xino_del(br);           /* remove one */
3966 +       au_br_do_del(sb, bindex, br);
3967 +       sysaufs_brs_add(sb, bindex);    /* append successors */
3968 +       dbgaufs_brs_add(sb, bindex, /*topdown*/1);      /* rename successors */
3969 +
3970 +       if (!bindex) {
3971 +               au_cpup_attr_all(d_inode(root), /*force*/1);
3972 +               sb->s_maxbytes = au_sbr_sb(sb, 0)->s_maxbytes;
3973 +       } else
3974 +               au_sub_nlink(d_inode(root), d_inode(del->h_path.dentry));
3975 +       if (au_opt_test(mnt_flags, PLINK))
3976 +               au_plink_half_refresh(sb, br_id);
3977 +
3978 +       goto out; /* success */
3979 +
3980 +out_wh:
3981 +       /* revert */
3982 +       rerr = au_br_init_wh(sb, br, br->br_perm);
3983 +       if (rerr)
3984 +               pr_warn("failed re-creating base whiteout, %s. (%d)\n",
3985 +                       del->pathname, rerr);
3986 +out:
3987 +       if (to_free)
3988 +               au_farray_free(to_free, opened);
3989 +       return err;
3990 +}
3991 +
3992 +/* ---------------------------------------------------------------------- */
3993 +
3994 +static int au_ibusy(struct super_block *sb, struct aufs_ibusy __user *arg)
3995 +{
3996 +       int err;
3997 +       aufs_bindex_t btop, bbot;
3998 +       struct aufs_ibusy ibusy;
3999 +       struct inode *inode, *h_inode;
4000 +
4001 +       err = -EPERM;
4002 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
4003 +               goto out;
4004 +
4005 +       err = copy_from_user(&ibusy, arg, sizeof(ibusy));
4006 +       if (!err)
4007 +               /* VERIFY_WRITE */
4008 +               err = !access_ok(&arg->h_ino, sizeof(arg->h_ino));
4009 +       if (unlikely(err)) {
4010 +               err = -EFAULT;
4011 +               AuTraceErr(err);
4012 +               goto out;
4013 +       }
4014 +
4015 +       err = -EINVAL;
4016 +       si_read_lock(sb, AuLock_FLUSH);
4017 +       if (unlikely(ibusy.bindex < 0 || ibusy.bindex > au_sbbot(sb)))
4018 +               goto out_unlock;
4019 +
4020 +       err = 0;
4021 +       ibusy.h_ino = 0; /* invalid */
4022 +       inode = ilookup(sb, ibusy.ino);
4023 +       if (!inode
4024 +           || inode->i_ino == AUFS_ROOT_INO
4025 +           || au_is_bad_inode(inode))
4026 +               goto out_unlock;
4027 +
4028 +       ii_read_lock_child(inode);
4029 +       btop = au_ibtop(inode);
4030 +       bbot = au_ibbot(inode);
4031 +       if (btop <= ibusy.bindex && ibusy.bindex <= bbot) {
4032 +               h_inode = au_h_iptr(inode, ibusy.bindex);
4033 +               if (h_inode && au_test_ibusy(inode, btop, bbot))
4034 +                       ibusy.h_ino = h_inode->i_ino;
4035 +       }
4036 +       ii_read_unlock(inode);
4037 +       iput(inode);
4038 +
4039 +out_unlock:
4040 +       si_read_unlock(sb);
4041 +       if (!err) {
4042 +               err = __put_user(ibusy.h_ino, &arg->h_ino);
4043 +               if (unlikely(err)) {
4044 +                       err = -EFAULT;
4045 +                       AuTraceErr(err);
4046 +               }
4047 +       }
4048 +out:
4049 +       return err;
4050 +}
4051 +
4052 +long au_ibusy_ioctl(struct file *file, unsigned long arg)
4053 +{
4054 +       return au_ibusy(file->f_path.dentry->d_sb, (void __user *)arg);
4055 +}
4056 +
4057 +#ifdef CONFIG_COMPAT
4058 +long au_ibusy_compat_ioctl(struct file *file, unsigned long arg)
4059 +{
4060 +       return au_ibusy(file->f_path.dentry->d_sb, compat_ptr(arg));
4061 +}
4062 +#endif
4063 +
4064 +/* ---------------------------------------------------------------------- */
4065 +
4066 +/*
4067 + * change a branch permission
4068 + */
4069 +
4070 +static void au_warn_ima(void)
4071 +{
4072 +#ifdef CONFIG_IMA
4073 +       /* since it doesn't support mark_files_ro() */
4074 +       AuWarn1("RW -> RO makes IMA to produce wrong message\n");
4075 +#endif
4076 +}
4077 +
4078 +static int do_need_sigen_inc(int a, int b)
4079 +{
4080 +       return au_br_whable(a) && !au_br_whable(b);
4081 +}
4082 +
4083 +static int need_sigen_inc(int old, int new)
4084 +{
4085 +       return do_need_sigen_inc(old, new)
4086 +               || do_need_sigen_inc(new, old);
4087 +}
4088 +
4089 +static int au_br_mod_files_ro(struct super_block *sb, aufs_bindex_t bindex)
4090 +{
4091 +       int err, do_warn;
4092 +       unsigned int mnt_flags;
4093 +       unsigned long long ull, max;
4094 +       aufs_bindex_t br_id;
4095 +       unsigned char verbose, writer;
4096 +       struct file *file, *hf, **array;
4097 +       struct au_hfile *hfile;
4098 +       struct inode *h_inode;
4099 +
4100 +       mnt_flags = au_mntflags(sb);
4101 +       verbose = !!au_opt_test(mnt_flags, VERBOSE);
4102 +
4103 +       array = au_farray_alloc(sb, &max);
4104 +       err = PTR_ERR(array);
4105 +       if (IS_ERR(array))
4106 +               goto out;
4107 +
4108 +       do_warn = 0;
4109 +       br_id = au_sbr_id(sb, bindex);
4110 +       for (ull = 0; ull < max; ull++) {
4111 +               file = array[ull];
4112 +               if (unlikely(!file))
4113 +                       break;
4114 +
4115 +               /* AuDbg("%pD\n", file); */
4116 +               fi_read_lock(file);
4117 +               if (unlikely(au_test_mmapped(file))) {
4118 +                       err = -EBUSY;
4119 +                       AuVerbose(verbose, "mmapped %pD\n", file);
4120 +                       AuDbgFile(file);
4121 +                       FiMustNoWaiters(file);
4122 +                       fi_read_unlock(file);
4123 +                       goto out_array;
4124 +               }
4125 +
4126 +               hfile = &au_fi(file)->fi_htop;
4127 +               hf = hfile->hf_file;
4128 +               if (!d_is_reg(file->f_path.dentry)
4129 +                   || !(file->f_mode & FMODE_WRITE)
4130 +                   || hfile->hf_br->br_id != br_id
4131 +                   || !(hf->f_mode & FMODE_WRITE))
4132 +                       array[ull] = NULL;
4133 +               else {
4134 +                       do_warn = 1;
4135 +                       get_file(file);
4136 +               }
4137 +
4138 +               FiMustNoWaiters(file);
4139 +               fi_read_unlock(file);
4140 +               fput(file);
4141 +       }
4142 +
4143 +       err = 0;
4144 +       if (do_warn)
4145 +               au_warn_ima();
4146 +
4147 +       for (ull = 0; ull < max; ull++) {
4148 +               file = array[ull];
4149 +               if (!file)
4150 +                       continue;
4151 +
4152 +               /* todo: already flushed? */
4153 +               /*
4154 +                * fs/super.c:mark_files_ro() is gone, but aufs keeps its
4155 +                * approach which resets f_mode and calls mnt_drop_write() and
4156 +                * file_release_write() for each file, because the branch
4157 +                * attribute in aufs world is totally different from the native
4158 +                * fs rw/ro mode.
4159 +               */
4160 +               /* fi_read_lock(file); */
4161 +               hfile = &au_fi(file)->fi_htop;
4162 +               hf = hfile->hf_file;
4163 +               /* fi_read_unlock(file); */
4164 +               spin_lock(&hf->f_lock);
4165 +               writer = !!(hf->f_mode & FMODE_WRITER);
4166 +               hf->f_mode &= ~(FMODE_WRITE | FMODE_WRITER);
4167 +               spin_unlock(&hf->f_lock);
4168 +               if (writer) {
4169 +                       h_inode = file_inode(hf);
4170 +                       if (hf->f_mode & FMODE_READ)
4171 +                               i_readcount_inc(h_inode);
4172 +                       put_write_access(h_inode);
4173 +                       __mnt_drop_write(hf->f_path.mnt);
4174 +               }
4175 +       }
4176 +
4177 +out_array:
4178 +       au_farray_free(array, max);
4179 +out:
4180 +       AuTraceErr(err);
4181 +       return err;
4182 +}
4183 +
4184 +int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
4185 +             int *do_refresh)
4186 +{
4187 +       int err, rerr;
4188 +       aufs_bindex_t bindex;
4189 +       struct dentry *root;
4190 +       struct au_branch *br;
4191 +       struct au_br_fhsm *bf;
4192 +
4193 +       root = sb->s_root;
4194 +       bindex = au_find_dbindex(root, mod->h_root);
4195 +       if (bindex < 0) {
4196 +               if (remount)
4197 +                       return 0; /* success */
4198 +               err = -ENOENT;
4199 +               pr_err("%s no such branch\n", mod->path);
4200 +               goto out;
4201 +       }
4202 +       AuDbg("bindex b%d\n", bindex);
4203 +
4204 +       err = test_br(d_inode(mod->h_root), mod->perm, mod->path);
4205 +       if (unlikely(err))
4206 +               goto out;
4207 +
4208 +       br = au_sbr(sb, bindex);
4209 +       AuDebugOn(mod->h_root != au_br_dentry(br));
4210 +       if (br->br_perm == mod->perm)
4211 +               return 0; /* success */
4212 +
4213 +       /* pre-allocate for non-fhsm --> fhsm */
4214 +       bf = NULL;
4215 +       if (!au_br_fhsm(br->br_perm) && au_br_fhsm(mod->perm)) {
4216 +               err = au_fhsm_br_alloc(br);
4217 +               if (unlikely(err))
4218 +                       goto out;
4219 +               bf = br->br_fhsm;
4220 +               br->br_fhsm = NULL;
4221 +       }
4222 +
4223 +       if (au_br_writable(br->br_perm)) {
4224 +               /* remove whiteout base */
4225 +               err = au_br_init_wh(sb, br, mod->perm);
4226 +               if (unlikely(err))
4227 +                       goto out_bf;
4228 +
4229 +               if (!au_br_writable(mod->perm)) {
4230 +                       /* rw --> ro, file might be mmapped */
4231 +                       DiMustNoWaiters(root);
4232 +                       IiMustNoWaiters(d_inode(root));
4233 +                       di_write_unlock(root);
4234 +                       err = au_br_mod_files_ro(sb, bindex);
4235 +                       /* aufs_write_lock() calls ..._child() */
4236 +                       di_write_lock_child(root);
4237 +
4238 +                       if (unlikely(err)) {
4239 +                               rerr = -ENOMEM;
4240 +                               br->br_wbr = kzalloc(sizeof(*br->br_wbr),
4241 +                                                    GFP_NOFS);
4242 +                               if (br->br_wbr)
4243 +                                       rerr = au_wbr_init(br, sb, br->br_perm);
4244 +                               if (unlikely(rerr)) {
4245 +                                       AuIOErr("nested error %d (%d)\n",
4246 +                                               rerr, err);
4247 +                                       br->br_perm = mod->perm;
4248 +                               }
4249 +                       }
4250 +               }
4251 +       } else if (au_br_writable(mod->perm)) {
4252 +               /* ro --> rw */
4253 +               err = -ENOMEM;
4254 +               br->br_wbr = kzalloc(sizeof(*br->br_wbr), GFP_NOFS);
4255 +               if (br->br_wbr) {
4256 +                       err = au_wbr_init(br, sb, mod->perm);
4257 +                       if (unlikely(err)) {
4258 +                               au_kfree_rcu(br->br_wbr);
4259 +                               br->br_wbr = NULL;
4260 +                       }
4261 +               }
4262 +       }
4263 +       if (unlikely(err))
4264 +               goto out_bf;
4265 +
4266 +       if (au_br_fhsm(br->br_perm)) {
4267 +               if (!au_br_fhsm(mod->perm)) {
4268 +                       /* fhsm --> non-fhsm */
4269 +                       au_br_fhsm_fin(br->br_fhsm);
4270 +                       au_kfree_rcu(br->br_fhsm);
4271 +                       br->br_fhsm = NULL;
4272 +               }
4273 +       } else if (au_br_fhsm(mod->perm))
4274 +               /* non-fhsm --> fhsm */
4275 +               br->br_fhsm = bf;
4276 +
4277 +       *do_refresh |= need_sigen_inc(br->br_perm, mod->perm);
4278 +       br->br_perm = mod->perm;
4279 +       goto out; /* success */
4280 +
4281 +out_bf:
4282 +       au_kfree_try_rcu(bf);
4283 +out:
4284 +       AuTraceErr(err);
4285 +       return err;
4286 +}
4287 +
4288 +/* ---------------------------------------------------------------------- */
4289 +
4290 +int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs)
4291 +{
4292 +       int err;
4293 +       struct kstatfs kstfs;
4294 +
4295 +       err = vfs_statfs(&br->br_path, &kstfs);
4296 +       if (!err) {
4297 +               stfs->f_blocks = kstfs.f_blocks;
4298 +               stfs->f_bavail = kstfs.f_bavail;
4299 +               stfs->f_files = kstfs.f_files;
4300 +               stfs->f_ffree = kstfs.f_ffree;
4301 +       }
4302 +
4303 +       return err;
4304 +}
4305 diff -urN /usr/share/empty/fs/aufs/branch.h linux/fs/aufs/branch.h
4306 --- /usr/share/empty/fs/aufs/branch.h   1970-01-01 01:00:00.000000000 +0100
4307 +++ linux/fs/aufs/branch.h      2022-03-21 14:49:05.719966344 +0100
4308 @@ -0,0 +1,375 @@
4309 +/* SPDX-License-Identifier: GPL-2.0 */
4310 +/*
4311 + * Copyright (C) 2005-2021 Junjiro R. Okajima
4312 + *
4313 + * This program, aufs is free software; you can redistribute it and/or modify
4314 + * it under the terms of the GNU General Public License as published by
4315 + * the Free Software Foundation; either version 2 of the License, or
4316 + * (at your option) any later version.
4317 + *
4318 + * This program is distributed in the hope that it will be useful,
4319 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
4320 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
4321 + * GNU General Public License for more details.
4322 + *
4323 + * You should have received a copy of the GNU General Public License
4324 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
4325 + */
4326 +
4327 +/*
4328 + * branch filesystems and xino for them
4329 + */
4330 +
4331 +#ifndef __AUFS_BRANCH_H__
4332 +#define __AUFS_BRANCH_H__
4333 +
4334 +#ifdef __KERNEL__
4335 +
4336 +#include <linux/mount.h>
4337 +#include "dirren.h"
4338 +#include "dynop.h"
4339 +#include "lcnt.h"
4340 +#include "rwsem.h"
4341 +#include "super.h"
4342 +
4343 +/* ---------------------------------------------------------------------- */
4344 +
4345 +/* a xino file */
4346 +struct au_xino {
4347 +       struct file             **xi_file;
4348 +       unsigned int            xi_nfile;
4349 +
4350 +       struct {
4351 +               spinlock_t              spin;
4352 +               ino_t                   *array;
4353 +               int                     total;
4354 +               /* reserved for future use */
4355 +               /* unsigned long        *bitmap; */
4356 +               wait_queue_head_t       wqh;
4357 +       } xi_nondir;
4358 +
4359 +       struct mutex            xi_mtx; /* protects xi_file array */
4360 +       struct hlist_bl_head    xi_writing;
4361 +
4362 +       atomic_t                xi_truncating;
4363 +
4364 +       struct kref             xi_kref;
4365 +};
4366 +
4367 +/* File-based Hierarchical Storage Management */
4368 +struct au_br_fhsm {
4369 +#ifdef CONFIG_AUFS_FHSM
4370 +       struct mutex            bf_lock;
4371 +       unsigned long           bf_jiffy;
4372 +       struct aufs_stfs        bf_stfs;
4373 +       int                     bf_readable;
4374 +#endif
4375 +};
4376 +
4377 +/* members for writable branch only */
4378 +enum {AuBrWh_BASE, AuBrWh_PLINK, AuBrWh_ORPH, AuBrWh_Last};
4379 +struct au_wbr {
4380 +       struct au_rwsem         wbr_wh_rwsem;
4381 +       struct dentry           *wbr_wh[AuBrWh_Last];
4382 +       atomic_t                wbr_wh_running;
4383 +#define wbr_whbase             wbr_wh[AuBrWh_BASE]     /* whiteout base */
4384 +#define wbr_plink              wbr_wh[AuBrWh_PLINK]    /* pseudo-link dir */
4385 +#define wbr_orph               wbr_wh[AuBrWh_ORPH]     /* dir for orphans */
4386 +
4387 +       /* mfs mode */
4388 +       unsigned long long      wbr_bytes;
4389 +};
4390 +
4391 +/* ext2 has 3 types of operations at least, ext3 has 4 */
4392 +#define AuBrDynOp (AuDyLast * 4)
4393 +
4394 +#ifdef CONFIG_AUFS_HFSNOTIFY
4395 +/* support for asynchronous destruction */
4396 +struct au_br_hfsnotify {
4397 +       struct fsnotify_group   *hfsn_group;
4398 +};
4399 +#endif
4400 +
4401 +/* sysfs entries */
4402 +struct au_brsysfs {
4403 +       char                    name[16];
4404 +       struct attribute        attr;
4405 +};
4406 +
4407 +enum {
4408 +       AuBrSysfs_BR,
4409 +       AuBrSysfs_BRID,
4410 +       AuBrSysfs_Last
4411 +};
4412 +
4413 +/* protected by superblock rwsem */
4414 +struct au_branch {
4415 +       struct au_xino          *br_xino;
4416 +
4417 +       aufs_bindex_t           br_id;
4418 +
4419 +       int                     br_perm;
4420 +       struct path             br_path;
4421 +       spinlock_t              br_dykey_lock;
4422 +       struct au_dykey         *br_dykey[AuBrDynOp];
4423 +       au_lcnt_t               br_nfiles;      /* opened files */
4424 +       au_lcnt_t               br_count;       /* in-use for other */
4425 +
4426 +       struct au_wbr           *br_wbr;
4427 +       struct au_br_fhsm       *br_fhsm;
4428 +
4429 +#ifdef CONFIG_AUFS_HFSNOTIFY
4430 +       struct au_br_hfsnotify  *br_hfsn;
4431 +#endif
4432 +
4433 +#ifdef CONFIG_SYSFS
4434 +       /* entries under sysfs per mount-point */
4435 +       struct au_brsysfs       br_sysfs[AuBrSysfs_Last];
4436 +#endif
4437 +
4438 +#ifdef CONFIG_DEBUG_FS
4439 +       struct dentry            *br_dbgaufs; /* xino */
4440 +#endif
4441 +
4442 +       struct au_dr_br         br_dirren;
4443 +};
4444 +
4445 +/* ---------------------------------------------------------------------- */
4446 +
4447 +static inline struct vfsmount *au_br_mnt(struct au_branch *br)
4448 +{
4449 +       return br->br_path.mnt;
4450 +}
4451 +
4452 +static inline struct dentry *au_br_dentry(struct au_branch *br)
4453 +{
4454 +       return br->br_path.dentry;
4455 +}
4456 +
4457 +static inline struct user_namespace *au_br_userns(struct au_branch *br)
4458 +{
4459 +       return mnt_user_ns(br->br_path.mnt);
4460 +}
4461 +
4462 +static inline struct super_block *au_br_sb(struct au_branch *br)
4463 +{
4464 +       return au_br_mnt(br)->mnt_sb;
4465 +}
4466 +
4467 +static inline int au_br_rdonly(struct au_branch *br)
4468 +{
4469 +       return (sb_rdonly(au_br_sb(br))
4470 +               || !au_br_writable(br->br_perm))
4471 +               ? -EROFS : 0;
4472 +}
4473 +
4474 +static inline int au_br_hnotifyable(int brperm __maybe_unused)
4475 +{
4476 +#ifdef CONFIG_AUFS_HNOTIFY
4477 +       return !(brperm & AuBrPerm_RR);
4478 +#else
4479 +       return 0;
4480 +#endif
4481 +}
4482 +
4483 +static inline int au_br_test_oflag(int oflag, struct au_branch *br)
4484 +{
4485 +       int err, exec_flag;
4486 +
4487 +       err = 0;
4488 +       exec_flag = oflag & __FMODE_EXEC;
4489 +       if (unlikely(exec_flag && path_noexec(&br->br_path)))
4490 +               err = -EACCES;
4491 +
4492 +       return err;
4493 +}
4494 +
4495 +static inline void au_xino_get(struct au_branch *br)
4496 +{
4497 +       struct au_xino *xi;
4498 +
4499 +       xi = br->br_xino;
4500 +       if (xi)
4501 +               kref_get(&xi->xi_kref);
4502 +}
4503 +
4504 +static inline int au_xino_count(struct au_branch *br)
4505 +{
4506 +       int v;
4507 +       struct au_xino *xi;
4508 +
4509 +       v = 0;
4510 +       xi = br->br_xino;
4511 +       if (xi)
4512 +               v = kref_read(&xi->xi_kref);
4513 +
4514 +       return v;
4515 +}
4516 +
4517 +/* ---------------------------------------------------------------------- */
4518 +
4519 +/* branch.c */
4520 +struct au_sbinfo;
4521 +void au_br_free(struct au_sbinfo *sinfo);
4522 +int au_br_index(struct super_block *sb, aufs_bindex_t br_id);
4523 +struct au_opt_add;
4524 +int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount);
4525 +struct au_opt_del;
4526 +int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount);
4527 +long au_ibusy_ioctl(struct file *file, unsigned long arg);
4528 +#ifdef CONFIG_COMPAT
4529 +long au_ibusy_compat_ioctl(struct file *file, unsigned long arg);
4530 +#endif
4531 +struct au_opt_mod;
4532 +int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
4533 +             int *do_refresh);
4534 +struct aufs_stfs;
4535 +int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs);
4536 +
4537 +/* xino.c */
4538 +static const loff_t au_loff_max = LLONG_MAX;
4539 +
4540 +aufs_bindex_t au_xi_root(struct super_block *sb, struct dentry *dentry);
4541 +struct file *au_xino_create(struct super_block *sb, char *fpath, int silent,
4542 +                           int wbrtop);
4543 +struct file *au_xino_create2(struct super_block *sb, struct path *base,
4544 +                            struct file *copy_src);
4545 +struct au_xi_new {
4546 +       struct au_xino *xi;     /* switch between xino and xigen */
4547 +       int idx;
4548 +       struct path *base;
4549 +       struct file *copy_src;
4550 +};
4551 +struct file *au_xi_new(struct super_block *sb, struct au_xi_new *xinew);
4552 +
4553 +int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4554 +                ino_t *ino);
4555 +int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4556 +                 ino_t ino);
4557 +ssize_t xino_fread(struct file *file, void *buf, size_t size, loff_t *pos);
4558 +ssize_t xino_fwrite(struct file *file, void *buf, size_t size, loff_t *pos);
4559 +
4560 +int au_xib_trunc(struct super_block *sb);
4561 +int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex, int idx_begin);
4562 +
4563 +struct au_xino *au_xino_alloc(unsigned int nfile);
4564 +int au_xino_put(struct au_branch *br);
4565 +struct file *au_xino_file1(struct au_xino *xi);
4566 +
4567 +struct au_opt_xino;
4568 +void au_xino_clr(struct super_block *sb);
4569 +int au_xino_set(struct super_block *sb, struct au_opt_xino *xiopt, int remount);
4570 +struct file *au_xino_def(struct super_block *sb);
4571 +int au_xino_init_br(struct super_block *sb, struct au_branch *br, ino_t hino,
4572 +                   struct path *base);
4573 +
4574 +ino_t au_xino_new_ino(struct super_block *sb);
4575 +void au_xino_delete_inode(struct inode *inode, const int unlinked);
4576 +
4577 +void au_xinondir_leave(struct super_block *sb, aufs_bindex_t bindex,
4578 +                      ino_t h_ino, int idx);
4579 +int au_xinondir_enter(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4580 +                     int *idx);
4581 +
4582 +int au_xino_path(struct seq_file *seq, struct file *file);
4583 +
4584 +/* ---------------------------------------------------------------------- */
4585 +
4586 +/* @idx is signed to accept -1 meaning the first file */
4587 +static inline struct file *au_xino_file(struct au_xino *xi, int idx)
4588 +{
4589 +       struct file *file;
4590 +
4591 +       file = NULL;
4592 +       if (!xi)
4593 +               goto out;
4594 +
4595 +       if (idx >= 0) {
4596 +               if (idx < xi->xi_nfile)
4597 +                       file = xi->xi_file[idx];
4598 +       } else
4599 +               file = au_xino_file1(xi);
4600 +
4601 +out:
4602 +       return file;
4603 +}
4604 +
4605 +/* ---------------------------------------------------------------------- */
4606 +
4607 +/* Superblock to branch */
4608 +static inline
4609 +aufs_bindex_t au_sbr_id(struct super_block *sb, aufs_bindex_t bindex)
4610 +{
4611 +       return au_sbr(sb, bindex)->br_id;
4612 +}
4613 +
4614 +static inline
4615 +struct vfsmount *au_sbr_mnt(struct super_block *sb, aufs_bindex_t bindex)
4616 +{
4617 +       return au_br_mnt(au_sbr(sb, bindex));
4618 +}
4619 +
4620 +static inline
4621 +struct user_namespace *au_sbr_userns(struct super_block *sb, aufs_bindex_t bindex)
4622 +{
4623 +       return au_br_userns(au_sbr(sb, bindex));
4624 +}
4625 +
4626 +static inline
4627 +struct super_block *au_sbr_sb(struct super_block *sb, aufs_bindex_t bindex)
4628 +{
4629 +       return au_br_sb(au_sbr(sb, bindex));
4630 +}
4631 +
4632 +static inline int au_sbr_perm(struct super_block *sb, aufs_bindex_t bindex)
4633 +{
4634 +       return au_sbr(sb, bindex)->br_perm;
4635 +}
4636 +
4637 +static inline int au_sbr_whable(struct super_block *sb, aufs_bindex_t bindex)
4638 +{
4639 +       return au_br_whable(au_sbr_perm(sb, bindex));
4640 +}
4641 +
4642 +/* ---------------------------------------------------------------------- */
4643 +
4644 +#define wbr_wh_read_lock(wbr)  au_rw_read_lock(&(wbr)->wbr_wh_rwsem)
4645 +#define wbr_wh_write_lock(wbr) au_rw_write_lock(&(wbr)->wbr_wh_rwsem)
4646 +#define wbr_wh_read_trylock(wbr)       au_rw_read_trylock(&(wbr)->wbr_wh_rwsem)
4647 +#define wbr_wh_write_trylock(wbr) au_rw_write_trylock(&(wbr)->wbr_wh_rwsem)
4648 +/*
4649 +#define wbr_wh_read_trylock_nested(wbr) \
4650 +       au_rw_read_trylock_nested(&(wbr)->wbr_wh_rwsem)
4651 +#define wbr_wh_write_trylock_nested(wbr) \
4652 +       au_rw_write_trylock_nested(&(wbr)->wbr_wh_rwsem)
4653 +*/
4654 +
4655 +#define wbr_wh_read_unlock(wbr)        au_rw_read_unlock(&(wbr)->wbr_wh_rwsem)
4656 +#define wbr_wh_write_unlock(wbr)       au_rw_write_unlock(&(wbr)->wbr_wh_rwsem)
4657 +#define wbr_wh_downgrade_lock(wbr)     au_rw_dgrade_lock(&(wbr)->wbr_wh_rwsem)
4658 +
4659 +#define WbrWhMustNoWaiters(wbr)        AuRwMustNoWaiters(&(wbr)->wbr_wh_rwsem)
4660 +#define WbrWhMustAnyLock(wbr)  AuRwMustAnyLock(&(wbr)->wbr_wh_rwsem)
4661 +#define WbrWhMustWriteLock(wbr)        AuRwMustWriteLock(&(wbr)->wbr_wh_rwsem)
4662 +
4663 +/* ---------------------------------------------------------------------- */
4664 +
4665 +#ifdef CONFIG_AUFS_FHSM
4666 +static inline void au_br_fhsm_init(struct au_br_fhsm *brfhsm)
4667 +{
4668 +       mutex_init(&brfhsm->bf_lock);
4669 +       brfhsm->bf_jiffy = 0;
4670 +       brfhsm->bf_readable = 0;
4671 +}
4672 +
4673 +static inline void au_br_fhsm_fin(struct au_br_fhsm *brfhsm)
4674 +{
4675 +       mutex_destroy(&brfhsm->bf_lock);
4676 +}
4677 +#else
4678 +AuStubVoid(au_br_fhsm_init, struct au_br_fhsm *brfhsm)
4679 +AuStubVoid(au_br_fhsm_fin, struct au_br_fhsm *brfhsm)
4680 +#endif
4681 +
4682 +#endif /* __KERNEL__ */
4683 +#endif /* __AUFS_BRANCH_H__ */
4684 diff -urN /usr/share/empty/fs/aufs/conf.mk linux/fs/aufs/conf.mk
4685 --- /usr/share/empty/fs/aufs/conf.mk    1970-01-01 01:00:00.000000000 +0100
4686 +++ linux/fs/aufs/conf.mk       2022-03-21 14:49:05.719966344 +0100
4687 @@ -0,0 +1,40 @@
4688 +# SPDX-License-Identifier: GPL-2.0
4689 +
4690 +AuConfStr = CONFIG_AUFS_FS=${CONFIG_AUFS_FS}
4691 +
4692 +define AuConf
4693 +ifdef ${1}
4694 +AuConfStr += ${1}=${${1}}
4695 +endif
4696 +endef
4697 +
4698 +AuConfAll = BRANCH_MAX_127 BRANCH_MAX_511 BRANCH_MAX_1023 BRANCH_MAX_32767 \
4699 +       SBILIST \
4700 +       HNOTIFY HFSNOTIFY \
4701 +       EXPORT INO_T_64 \
4702 +       XATTR \
4703 +       FHSM \
4704 +       RDU \
4705 +       DIRREN \
4706 +       SHWH \
4707 +       BR_RAMFS \
4708 +       BR_FUSE POLL \
4709 +       BR_HFSPLUS \
4710 +       BDEV_LOOP \
4711 +       DEBUG MAGIC_SYSRQ
4712 +$(foreach i, ${AuConfAll}, \
4713 +       $(eval $(call AuConf,CONFIG_AUFS_${i})))
4714 +
4715 +AuConfName = ${obj}/conf.str
4716 +${AuConfName}.tmp: FORCE
4717 +       @echo ${AuConfStr} | tr ' ' '\n' | sed -e 's/^/"/' -e 's/$$/\\n"/' > $@
4718 +${AuConfName}: ${AuConfName}.tmp
4719 +       @diff -q $< $@ > /dev/null 2>&1 || { \
4720 +       echo '  GEN    ' $@; \
4721 +       cp -p $< $@; \
4722 +       }
4723 +FORCE:
4724 +clean-files += ${AuConfName} ${AuConfName}.tmp
4725 +${obj}/sysfs.o: ${AuConfName}
4726 +
4727 +-include ${srctree}/${src}/conf_priv.mk
4728 diff -urN /usr/share/empty/fs/aufs/cpup.c linux/fs/aufs/cpup.c
4729 --- /usr/share/empty/fs/aufs/cpup.c     1970-01-01 01:00:00.000000000 +0100
4730 +++ linux/fs/aufs/cpup.c        2022-03-21 14:49:05.719966344 +0100
4731 @@ -0,0 +1,1459 @@
4732 +// SPDX-License-Identifier: GPL-2.0
4733 +/*
4734 + * Copyright (C) 2005-2021 Junjiro R. Okajima
4735 + *
4736 + * This program, aufs is free software; you can redistribute it and/or modify
4737 + * it under the terms of the GNU General Public License as published by
4738 + * the Free Software Foundation; either version 2 of the License, or
4739 + * (at your option) any later version.
4740 + *
4741 + * This program is distributed in the hope that it will be useful,
4742 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
4743 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
4744 + * GNU General Public License for more details.
4745 + *
4746 + * You should have received a copy of the GNU General Public License
4747 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
4748 + */
4749 +
4750 +/*
4751 + * copy-up functions, see wbr_policy.c for copy-down
4752 + */
4753 +
4754 +#include <linux/fs_stack.h>
4755 +#include <linux/mm.h>
4756 +#include <linux/task_work.h>
4757 +#include "aufs.h"
4758 +
4759 +void au_cpup_attr_flags(struct inode *dst, unsigned int iflags)
4760 +{
4761 +       const unsigned int mask = S_DEAD | S_SWAPFILE | S_PRIVATE
4762 +               | S_NOATIME | S_NOCMTIME | S_AUTOMOUNT;
4763 +
4764 +       BUILD_BUG_ON(sizeof(iflags) != sizeof(dst->i_flags));
4765 +
4766 +       dst->i_flags |= iflags & ~mask;
4767 +       if (au_test_fs_notime(dst->i_sb))
4768 +               dst->i_flags |= S_NOATIME | S_NOCMTIME;
4769 +}
4770 +
4771 +void au_cpup_attr_timesizes(struct inode *inode)
4772 +{
4773 +       struct inode *h_inode;
4774 +
4775 +       h_inode = au_h_iptr(inode, au_ibtop(inode));
4776 +       fsstack_copy_attr_times(inode, h_inode);
4777 +       fsstack_copy_inode_size(inode, h_inode);
4778 +}
4779 +
4780 +void au_cpup_attr_nlink(struct inode *inode, int force)
4781 +{
4782 +       struct inode *h_inode;
4783 +       struct super_block *sb;
4784 +       aufs_bindex_t bindex, bbot;
4785 +
4786 +       sb = inode->i_sb;
4787 +       bindex = au_ibtop(inode);
4788 +       h_inode = au_h_iptr(inode, bindex);
4789 +       if (!force
4790 +           && !S_ISDIR(h_inode->i_mode)
4791 +           && au_opt_test(au_mntflags(sb), PLINK)
4792 +           && au_plink_test(inode))
4793 +               return;
4794 +
4795 +       /*
4796 +        * 0 can happen in revalidating.
4797 +        * h_inode->i_mutex may not be held here, but it is harmless since once
4798 +        * i_nlink reaches 0, it will never become positive except O_TMPFILE
4799 +        * case.
4800 +        * todo: O_TMPFILE+linkat(AT_SYMLINK_FOLLOW) bypassing aufs may cause
4801 +        *       the incorrect link count.
4802 +        */
4803 +       set_nlink(inode, h_inode->i_nlink);
4804 +
4805 +       /*
4806 +        * fewer nlink makes find(1) noisy, but larger nlink doesn't.
4807 +        * it may includes whplink directory.
4808 +        */
4809 +       if (S_ISDIR(h_inode->i_mode)) {
4810 +               bbot = au_ibbot(inode);
4811 +               for (bindex++; bindex <= bbot; bindex++) {
4812 +                       h_inode = au_h_iptr(inode, bindex);
4813 +                       if (h_inode)
4814 +                               au_add_nlink(inode, h_inode);
4815 +               }
4816 +       }
4817 +}
4818 +
4819 +void au_cpup_attr_changeable(struct inode *inode)
4820 +{
4821 +       struct inode *h_inode;
4822 +
4823 +       h_inode = au_h_iptr(inode, au_ibtop(inode));
4824 +       inode->i_mode = h_inode->i_mode;
4825 +       inode->i_uid = h_inode->i_uid;
4826 +       inode->i_gid = h_inode->i_gid;
4827 +       au_cpup_attr_timesizes(inode);
4828 +       au_cpup_attr_flags(inode, h_inode->i_flags);
4829 +}
4830 +
4831 +void au_cpup_igen(struct inode *inode, struct inode *h_inode)
4832 +{
4833 +       struct au_iinfo *iinfo = au_ii(inode);
4834 +
4835 +       IiMustWriteLock(inode);
4836 +
4837 +       iinfo->ii_higen = h_inode->i_generation;
4838 +       iinfo->ii_hsb1 = h_inode->i_sb;
4839 +}
4840 +
4841 +void au_cpup_attr_all(struct inode *inode, int force)
4842 +{
4843 +       struct inode *h_inode;
4844 +
4845 +       h_inode = au_h_iptr(inode, au_ibtop(inode));
4846 +       au_cpup_attr_changeable(inode);
4847 +       if (inode->i_nlink > 0)
4848 +               au_cpup_attr_nlink(inode, force);
4849 +       inode->i_rdev = h_inode->i_rdev;
4850 +       inode->i_blkbits = h_inode->i_blkbits;
4851 +       au_cpup_igen(inode, h_inode);
4852 +}
4853 +
4854 +/* ---------------------------------------------------------------------- */
4855 +
4856 +/* Note: dt_dentry and dt_h_dentry are not dget/dput-ed */
4857 +
4858 +/* keep the timestamps of the parent dir when cpup */
4859 +void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
4860 +                   struct path *h_path)
4861 +{
4862 +       struct inode *h_inode;
4863 +
4864 +       dt->dt_dentry = dentry;
4865 +       dt->dt_h_path = *h_path;
4866 +       h_inode = d_inode(h_path->dentry);
4867 +       dt->dt_atime = h_inode->i_atime;
4868 +       dt->dt_mtime = h_inode->i_mtime;
4869 +       /* smp_mb(); */
4870 +}
4871 +
4872 +void au_dtime_revert(struct au_dtime *dt)
4873 +{
4874 +       struct iattr attr;
4875 +       int err;
4876 +
4877 +       attr.ia_atime = dt->dt_atime;
4878 +       attr.ia_mtime = dt->dt_mtime;
4879 +       attr.ia_valid = ATTR_FORCE | ATTR_MTIME | ATTR_MTIME_SET
4880 +               | ATTR_ATIME | ATTR_ATIME_SET;
4881 +
4882 +       /* no delegation since this is a directory */
4883 +       err = vfsub_notify_change(&dt->dt_h_path, &attr, /*delegated*/NULL);
4884 +       if (unlikely(err))
4885 +               pr_warn("restoring timestamps failed(%d). ignored\n", err);
4886 +}
4887 +
4888 +/* ---------------------------------------------------------------------- */
4889 +
4890 +/* internal use only */
4891 +struct au_cpup_reg_attr {
4892 +       int             valid;
4893 +       struct kstat    st;
4894 +       unsigned int    iflags; /* inode->i_flags */
4895 +};
4896 +
4897 +static noinline_for_stack
4898 +int cpup_iattr(struct dentry *dst, aufs_bindex_t bindex, struct path *h_src,
4899 +              struct au_cpup_reg_attr *h_src_attr)
4900 +{
4901 +       int err, sbits, icex;
4902 +       unsigned int mnt_flags;
4903 +       unsigned char verbose;
4904 +       struct iattr ia;
4905 +       struct path h_path;
4906 +       struct inode *h_isrc, *h_idst;
4907 +       struct kstat *h_st;
4908 +       struct au_branch *br;
4909 +
4910 +       br = au_sbr(dst->d_sb, bindex);
4911 +       h_path.mnt = au_br_mnt(br);
4912 +       h_path.dentry = au_h_dptr(dst, bindex);
4913 +       h_idst = d_inode(h_path.dentry);
4914 +       h_isrc = d_inode(h_src->dentry);
4915 +       ia.ia_valid = ATTR_FORCE | ATTR_UID | ATTR_GID
4916 +               | ATTR_ATIME | ATTR_MTIME
4917 +               | ATTR_ATIME_SET | ATTR_MTIME_SET;
4918 +       if (h_src_attr && h_src_attr->valid) {
4919 +               h_st = &h_src_attr->st;
4920 +               ia.ia_uid = h_st->uid;
4921 +               ia.ia_gid = h_st->gid;
4922 +               ia.ia_atime = h_st->atime;
4923 +               ia.ia_mtime = h_st->mtime;
4924 +               if (h_idst->i_mode != h_st->mode
4925 +                   && !S_ISLNK(h_idst->i_mode)) {
4926 +                       ia.ia_valid |= ATTR_MODE;
4927 +                       ia.ia_mode = h_st->mode;
4928 +               }
4929 +               sbits = !!(h_st->mode & (S_ISUID | S_ISGID));
4930 +               au_cpup_attr_flags(h_idst, h_src_attr->iflags);
4931 +       } else {
4932 +               ia.ia_uid = h_isrc->i_uid;
4933 +               ia.ia_gid = h_isrc->i_gid;
4934 +               ia.ia_atime = h_isrc->i_atime;
4935 +               ia.ia_mtime = h_isrc->i_mtime;
4936 +               if (h_idst->i_mode != h_isrc->i_mode
4937 +                   && !S_ISLNK(h_idst->i_mode)) {
4938 +                       ia.ia_valid |= ATTR_MODE;
4939 +                       ia.ia_mode = h_isrc->i_mode;
4940 +               }
4941 +               sbits = !!(h_isrc->i_mode & (S_ISUID | S_ISGID));
4942 +               au_cpup_attr_flags(h_idst, h_isrc->i_flags);
4943 +       }
4944 +       /* no delegation since it is just created */
4945 +       err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
4946 +
4947 +       /* is this nfs only? */
4948 +       if (!err && sbits && au_test_nfs(h_path.dentry->d_sb)) {
4949 +               ia.ia_valid = ATTR_FORCE | ATTR_MODE;
4950 +               ia.ia_mode = h_isrc->i_mode;
4951 +               err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
4952 +       }
4953 +
4954 +       icex = br->br_perm & AuBrAttr_ICEX;
4955 +       if (!err) {
4956 +               mnt_flags = au_mntflags(dst->d_sb);
4957 +               verbose = !!au_opt_test(mnt_flags, VERBOSE);
4958 +               err = au_cpup_xattr(&h_path, h_src, icex, verbose);
4959 +       }
4960 +
4961 +       return err;
4962 +}
4963 +
4964 +/* ---------------------------------------------------------------------- */
4965 +
4966 +static int au_do_copy_file(struct file *dst, struct file *src, loff_t len,
4967 +                          char *buf, unsigned long blksize)
4968 +{
4969 +       int err;
4970 +       size_t sz, rbytes, wbytes;
4971 +       unsigned char all_zero;
4972 +       char *p, *zp;
4973 +       struct inode *h_inode;
4974 +       /* reduce stack usage */
4975 +       struct iattr *ia;
4976 +
4977 +       zp = page_address(ZERO_PAGE(0));
4978 +       if (unlikely(!zp))
4979 +               return -ENOMEM; /* possible? */
4980 +
4981 +       err = 0;
4982 +       all_zero = 0;
4983 +       while (len) {
4984 +               AuDbg("len %lld\n", len);
4985 +               sz = blksize;
4986 +               if (len < blksize)
4987 +                       sz = len;
4988 +
4989 +               rbytes = 0;
4990 +               /* todo: signal_pending? */
4991 +               while (!rbytes || err == -EAGAIN || err == -EINTR) {
4992 +                       rbytes = vfsub_read_k(src, buf, sz, &src->f_pos);
4993 +                       err = rbytes;
4994 +               }
4995 +               if (unlikely(err < 0))
4996 +                       break;
4997 +
4998 +               all_zero = 0;
4999 +               if (len >= rbytes && rbytes == blksize)
5000 +                       all_zero = !memcmp(buf, zp, rbytes);
5001 +               if (!all_zero) {
5002 +                       wbytes = rbytes;
5003 +                       p = buf;
5004 +                       while (wbytes) {
5005 +                               size_t b;
5006 +
5007 +                               b = vfsub_write_k(dst, p, wbytes, &dst->f_pos);
5008 +                               err = b;
5009 +                               /* todo: signal_pending? */
5010 +                               if (unlikely(err == -EAGAIN || err == -EINTR))
5011 +                                       continue;
5012 +                               if (unlikely(err < 0))
5013 +                                       break;
5014 +                               wbytes -= b;
5015 +                               p += b;
5016 +                       }
5017 +                       if (unlikely(err < 0))
5018 +                               break;
5019 +               } else {
5020 +                       loff_t res;
5021 +
5022 +                       AuLabel(hole);
5023 +                       res = vfsub_llseek(dst, rbytes, SEEK_CUR);
5024 +                       err = res;
5025 +                       if (unlikely(res < 0))
5026 +                               break;
5027 +               }
5028 +               len -= rbytes;
5029 +               err = 0;
5030 +       }
5031 +
5032 +       /* the last block may be a hole */
5033 +       if (!err && all_zero) {
5034 +               AuLabel(last hole);
5035 +
5036 +               err = 1;
5037 +               if (au_test_nfs(dst->f_path.dentry->d_sb)) {
5038 +                       /* nfs requires this step to make last hole */
5039 +                       /* is this only nfs? */
5040 +                       do {
5041 +                               /* todo: signal_pending? */
5042 +                               err = vfsub_write_k(dst, "\0", 1, &dst->f_pos);
5043 +                       } while (err == -EAGAIN || err == -EINTR);
5044 +                       if (err == 1)
5045 +                               dst->f_pos--;
5046 +               }
5047 +
5048 +               if (err == 1) {
5049 +                       ia = (void *)buf;
5050 +                       ia->ia_size = dst->f_pos;
5051 +                       ia->ia_valid = ATTR_SIZE | ATTR_FILE;
5052 +                       ia->ia_file = dst;
5053 +                       h_inode = file_inode(dst);
5054 +                       inode_lock_nested(h_inode, AuLsc_I_CHILD2);
5055 +                       /* no delegation since it is just created */
5056 +                       err = vfsub_notify_change(&dst->f_path, ia,
5057 +                                                 /*delegated*/NULL);
5058 +                       inode_unlock(h_inode);
5059 +               }
5060 +       }
5061 +
5062 +       return err;
5063 +}
5064 +
5065 +int au_copy_file(struct file *dst, struct file *src, loff_t len)
5066 +{
5067 +       int err;
5068 +       unsigned long blksize;
5069 +       unsigned char do_kfree;
5070 +       char *buf;
5071 +       struct super_block *h_sb;
5072 +
5073 +       err = -ENOMEM;
5074 +       h_sb = file_inode(dst)->i_sb;
5075 +       blksize = h_sb->s_blocksize;
5076 +       if (!blksize || PAGE_SIZE < blksize)
5077 +               blksize = PAGE_SIZE;
5078 +       AuDbg("blksize %lu\n", blksize);
5079 +       do_kfree = (blksize != PAGE_SIZE && blksize >= sizeof(struct iattr *));
5080 +       if (do_kfree)
5081 +               buf = kmalloc(blksize, GFP_NOFS);
5082 +       else
5083 +               buf = (void *)__get_free_page(GFP_NOFS);
5084 +       if (unlikely(!buf))
5085 +               goto out;
5086 +
5087 +       if (len > (1 << 22))
5088 +               AuDbg("copying a large file %lld\n", (long long)len);
5089 +
5090 +       src->f_pos = 0;
5091 +       dst->f_pos = 0;
5092 +       err = au_do_copy_file(dst, src, len, buf, blksize);
5093 +       if (do_kfree) {
5094 +               AuDebugOn(!au_kfree_do_sz_test(blksize));
5095 +               au_kfree_do_rcu(buf);
5096 +       } else
5097 +               free_page((unsigned long)buf);
5098 +
5099 +out:
5100 +       return err;
5101 +}
5102 +
5103 +static int au_do_copy(struct file *dst, struct file *src, loff_t len)
5104 +{
5105 +       int err;
5106 +       struct super_block *h_src_sb;
5107 +       struct inode *h_src_inode;
5108 +
5109 +       h_src_inode = file_inode(src);
5110 +       h_src_sb = h_src_inode->i_sb;
5111 +
5112 +       /* XFS acquires inode_lock */
5113 +       if (!au_test_xfs(h_src_sb))
5114 +               err = au_copy_file(dst, src, len);
5115 +       else {
5116 +               inode_unlock_shared(h_src_inode);
5117 +               err = au_copy_file(dst, src, len);
5118 +               inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5119 +       }
5120 +
5121 +       return err;
5122 +}
5123 +
5124 +static int au_clone_or_copy(struct file *dst, struct file *src, loff_t len)
5125 +{
5126 +       int err;
5127 +       loff_t lo;
5128 +       struct super_block *h_src_sb;
5129 +       struct inode *h_src_inode;
5130 +
5131 +       h_src_inode = file_inode(src);
5132 +       h_src_sb = h_src_inode->i_sb;
5133 +       if (h_src_sb != file_inode(dst)->i_sb
5134 +           || !dst->f_op->remap_file_range) {
5135 +               err = au_do_copy(dst, src, len);
5136 +               goto out;
5137 +       }
5138 +
5139 +       if (!au_test_nfs(h_src_sb)) {
5140 +               inode_unlock_shared(h_src_inode);
5141 +               lo = vfsub_clone_file_range(src, dst, len);
5142 +               inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5143 +       } else
5144 +               lo = vfsub_clone_file_range(src, dst, len);
5145 +       if (lo == len) {
5146 +               err = 0;
5147 +               goto out; /* success */
5148 +       } else if (lo >= 0)
5149 +               /* todo: possible? */
5150 +               /* paritially succeeded */
5151 +               AuDbg("lo %lld, len %lld. Retrying.\n", lo, len);
5152 +       else if (lo != -EOPNOTSUPP) {
5153 +               /* older XFS has a condition in cloning */
5154 +               err = lo;
5155 +               goto out;
5156 +       }
5157 +
5158 +       /* the backend fs on NFS may not support cloning */
5159 +       err = au_do_copy(dst, src, len);
5160 +
5161 +out:
5162 +       AuTraceErr(err);
5163 +       return err;
5164 +}
5165 +
5166 +/*
5167 + * to support a sparse file which is opened with O_APPEND,
5168 + * we need to close the file.
5169 + */
5170 +static int au_cp_regular(struct au_cp_generic *cpg)
5171 +{
5172 +       int err, i;
5173 +       enum { SRC, DST };
5174 +       struct {
5175 +               aufs_bindex_t bindex;
5176 +               unsigned int flags;
5177 +               struct dentry *dentry;
5178 +               int force_wr;
5179 +               struct file *file;
5180 +       } *f, file[] = {
5181 +               {
5182 +                       .bindex = cpg->bsrc,
5183 +                       .flags = O_RDONLY | O_NOATIME | O_LARGEFILE,
5184 +               },
5185 +               {
5186 +                       .bindex = cpg->bdst,
5187 +                       .flags = O_WRONLY | O_NOATIME | O_LARGEFILE,
5188 +                       .force_wr = !!au_ftest_cpup(cpg->flags, RWDST),
5189 +               }
5190 +       };
5191 +       struct au_branch *br;
5192 +       struct super_block *sb, *h_src_sb;
5193 +       struct inode *h_src_inode;
5194 +       struct task_struct *tsk = current;
5195 +
5196 +       /* bsrc branch can be ro/rw. */
5197 +       sb = cpg->dentry->d_sb;
5198 +       f = file;
5199 +       for (i = 0; i < 2; i++, f++) {
5200 +               f->dentry = au_h_dptr(cpg->dentry, f->bindex);
5201 +               f->file = au_h_open(cpg->dentry, f->bindex, f->flags,
5202 +                                   /*file*/NULL, f->force_wr);
5203 +               if (IS_ERR(f->file)) {
5204 +                       err = PTR_ERR(f->file);
5205 +                       if (i == SRC)
5206 +                               goto out;
5207 +                       else
5208 +                               goto out_src;
5209 +               }
5210 +       }
5211 +
5212 +       /* try stopping to update while we copyup */
5213 +       h_src_inode = d_inode(file[SRC].dentry);
5214 +       h_src_sb = h_src_inode->i_sb;
5215 +       if (!au_test_nfs(h_src_sb))
5216 +               IMustLock(h_src_inode);
5217 +       err = au_clone_or_copy(file[DST].file, file[SRC].file, cpg->len);
5218 +
5219 +       /* i wonder if we had O_NO_DELAY_FPUT flag */
5220 +       if (tsk->flags & PF_KTHREAD)
5221 +               __fput_sync(file[DST].file);
5222 +       else {
5223 +               /* it happened actually */
5224 +               fput(file[DST].file);
5225 +               /*
5226 +                * too bad.
5227 +                * we have to call both since we don't know which place the file
5228 +                * was added to.
5229 +                */
5230 +               task_work_run();
5231 +               flush_delayed_fput();
5232 +       }
5233 +       br = au_sbr(sb, file[DST].bindex);
5234 +       au_lcnt_dec(&br->br_nfiles);
5235 +
5236 +out_src:
5237 +       fput(file[SRC].file);
5238 +       br = au_sbr(sb, file[SRC].bindex);
5239 +       au_lcnt_dec(&br->br_nfiles);
5240 +out:
5241 +       return err;
5242 +}
5243 +
5244 +static int au_do_cpup_regular(struct au_cp_generic *cpg,
5245 +                             struct au_cpup_reg_attr *h_src_attr)
5246 +{
5247 +       int err, rerr;
5248 +       loff_t l;
5249 +       struct path h_path;
5250 +       struct inode *h_src_inode, *h_dst_inode;
5251 +
5252 +       err = 0;
5253 +       h_src_inode = au_h_iptr(d_inode(cpg->dentry), cpg->bsrc);
5254 +       l = i_size_read(h_src_inode);
5255 +       if (cpg->len == -1 || l < cpg->len)
5256 +               cpg->len = l;
5257 +       if (cpg->len) {
5258 +               /* try stopping to update while we are referencing */
5259 +               inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5260 +               au_pin_hdir_unlock(cpg->pin);
5261 +
5262 +               h_path.dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
5263 +               h_path.mnt = au_sbr_mnt(cpg->dentry->d_sb, cpg->bsrc);
5264 +               h_src_attr->iflags = h_src_inode->i_flags;
5265 +               if (!au_test_nfs(h_src_inode->i_sb))
5266 +                       err = vfsub_getattr(&h_path, &h_src_attr->st);
5267 +               else {
5268 +                       inode_unlock_shared(h_src_inode);
5269 +                       err = vfsub_getattr(&h_path, &h_src_attr->st);
5270 +                       inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5271 +               }
5272 +               if (unlikely(err)) {
5273 +                       inode_unlock_shared(h_src_inode);
5274 +                       goto out;
5275 +               }
5276 +               h_src_attr->valid = 1;
5277 +               if (!au_test_nfs(h_src_inode->i_sb)) {
5278 +                       err = au_cp_regular(cpg);
5279 +                       inode_unlock_shared(h_src_inode);
5280 +               } else {
5281 +                       inode_unlock_shared(h_src_inode);
5282 +                       err = au_cp_regular(cpg);
5283 +               }
5284 +               rerr = au_pin_hdir_relock(cpg->pin);
5285 +               if (!err && rerr)
5286 +                       err = rerr;
5287 +       }
5288 +       if (!err && (h_src_inode->i_state & I_LINKABLE)) {
5289 +               h_path.dentry = au_h_dptr(cpg->dentry, cpg->bdst);
5290 +               h_dst_inode = d_inode(h_path.dentry);
5291 +               spin_lock(&h_dst_inode->i_lock);
5292 +               h_dst_inode->i_state |= I_LINKABLE;
5293 +               spin_unlock(&h_dst_inode->i_lock);
5294 +       }
5295 +
5296 +out:
5297 +       return err;
5298 +}
5299 +
5300 +static int au_do_cpup_symlink(struct path *h_path, struct dentry *h_src,
5301 +                             struct inode *h_dir)
5302 +{
5303 +       int err;
5304 +       DEFINE_DELAYED_CALL(done);
5305 +       const char *sym;
5306 +
5307 +       sym = vfs_get_link(h_src, &done);
5308 +       err = PTR_ERR(sym);
5309 +       if (IS_ERR(sym))
5310 +               goto out;
5311 +
5312 +       err = vfsub_symlink(h_dir, h_path, sym);
5313 +
5314 +out:
5315 +       do_delayed_call(&done);
5316 +       return err;
5317 +}
5318 +
5319 +/*
5320 + * regardless 'acl' option, reset all ACL.
5321 + * All ACL will be copied up later from the original entry on the lower branch.
5322 + */
5323 +static int au_reset_acl(struct inode *h_dir, struct path *h_path, umode_t mode)
5324 +{
5325 +       int err;
5326 +       struct dentry *h_dentry;
5327 +       struct inode *h_inode;
5328 +       struct user_namespace *h_userns;
5329 +
5330 +       h_userns = mnt_user_ns(h_path->mnt);
5331 +       h_dentry = h_path->dentry;
5332 +       h_inode = d_inode(h_dentry);
5333 +       /* forget_all_cached_acls(h_inode)); */
5334 +       err = vfsub_removexattr(h_userns, h_dentry, XATTR_NAME_POSIX_ACL_ACCESS);
5335 +       AuTraceErr(err);
5336 +       if (err == -EOPNOTSUPP)
5337 +               err = 0;
5338 +       if (!err)
5339 +               err = vfsub_acl_chmod(h_userns, h_inode, mode);
5340 +
5341 +       AuTraceErr(err);
5342 +       return err;
5343 +}
5344 +
5345 +static int au_do_cpup_dir(struct au_cp_generic *cpg, struct dentry *dst_parent,
5346 +                         struct inode *h_dir, struct path *h_path)
5347 +{
5348 +       int err;
5349 +       struct inode *dir, *inode;
5350 +       struct user_namespace *h_userns;
5351 +
5352 +       h_userns = mnt_user_ns(h_path->mnt);
5353 +       err = vfsub_removexattr(h_userns, h_path->dentry,
5354 +                               XATTR_NAME_POSIX_ACL_DEFAULT);
5355 +       AuTraceErr(err);
5356 +       if (err == -EOPNOTSUPP)
5357 +               err = 0;
5358 +       if (unlikely(err))
5359 +               goto out;
5360 +
5361 +       /*
5362 +        * strange behaviour from the users view,
5363 +        * particularly setattr case
5364 +        */
5365 +       dir = d_inode(dst_parent);
5366 +       if (au_ibtop(dir) == cpg->bdst)
5367 +               au_cpup_attr_nlink(dir, /*force*/1);
5368 +       inode = d_inode(cpg->dentry);
5369 +       au_cpup_attr_nlink(inode, /*force*/1);
5370 +
5371 +out:
5372 +       return err;
5373 +}
5374 +
5375 +static noinline_for_stack
5376 +int cpup_entry(struct au_cp_generic *cpg, struct dentry *dst_parent,
5377 +              struct au_cpup_reg_attr *h_src_attr)
5378 +{
5379 +       int err;
5380 +       umode_t mode;
5381 +       unsigned int mnt_flags;
5382 +       unsigned char isdir, isreg, force;
5383 +       const unsigned char do_dt = !!au_ftest_cpup(cpg->flags, DTIME);
5384 +       struct au_dtime dt;
5385 +       struct path h_path;
5386 +       struct dentry *h_src, *h_dst, *h_parent;
5387 +       struct inode *h_inode, *h_dir;
5388 +       struct super_block *sb;
5389 +
5390 +       /* bsrc branch can be ro/rw. */
5391 +       h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
5392 +       h_inode = d_inode(h_src);
5393 +       AuDebugOn(h_inode != au_h_iptr(d_inode(cpg->dentry), cpg->bsrc));
5394 +
5395 +       /* try stopping to be referenced while we are creating */
5396 +       h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
5397 +       if (au_ftest_cpup(cpg->flags, RENAME))
5398 +               AuDebugOn(strncmp(h_dst->d_name.name, AUFS_WH_PFX,
5399 +                                 AUFS_WH_PFX_LEN));
5400 +       h_parent = h_dst->d_parent; /* dir inode is locked */
5401 +       h_dir = d_inode(h_parent);
5402 +       IMustLock(h_dir);
5403 +       AuDebugOn(h_parent != h_dst->d_parent);
5404 +
5405 +       sb = cpg->dentry->d_sb;
5406 +       h_path.mnt = au_sbr_mnt(sb, cpg->bdst);
5407 +       if (do_dt) {
5408 +               h_path.dentry = h_parent;
5409 +               au_dtime_store(&dt, dst_parent, &h_path);
5410 +       }
5411 +       h_path.dentry = h_dst;
5412 +
5413 +       isreg = 0;
5414 +       isdir = 0;
5415 +       mode = h_inode->i_mode;
5416 +       switch (mode & S_IFMT) {
5417 +       case S_IFREG:
5418 +               isreg = 1;
5419 +               err = vfsub_create(h_dir, &h_path, 0600, /*want_excl*/true);
5420 +               if (!err)
5421 +                       err = au_do_cpup_regular(cpg, h_src_attr);
5422 +               break;
5423 +       case S_IFDIR:
5424 +               isdir = 1;
5425 +               err = vfsub_mkdir(h_dir, &h_path, mode);
5426 +               if (!err)
5427 +                       err = au_do_cpup_dir(cpg, dst_parent, h_dir, &h_path);
5428 +               break;
5429 +       case S_IFLNK:
5430 +               err = au_do_cpup_symlink(&h_path, h_src, h_dir);
5431 +               break;
5432 +       case S_IFCHR:
5433 +       case S_IFBLK:
5434 +               AuDebugOn(!capable(CAP_MKNOD));
5435 +               fallthrough;
5436 +       case S_IFIFO:
5437 +       case S_IFSOCK:
5438 +               err = vfsub_mknod(h_dir, &h_path, mode, h_inode->i_rdev);
5439 +               break;
5440 +       default:
5441 +               AuIOErr("Unknown inode type 0%o\n", mode);
5442 +               err = -EIO;
5443 +       }
5444 +       if (!err)
5445 +               err = au_reset_acl(h_dir, &h_path, mode);
5446 +
5447 +       mnt_flags = au_mntflags(sb);
5448 +       if (!au_opt_test(mnt_flags, UDBA_NONE)
5449 +           && !isdir
5450 +           && au_opt_test(mnt_flags, XINO)
5451 +           && (h_inode->i_nlink == 1
5452 +               || (h_inode->i_state & I_LINKABLE))
5453 +           /* todo: unnecessary? */
5454 +           /* && d_inode(cpg->dentry)->i_nlink == 1 */
5455 +           && cpg->bdst < cpg->bsrc
5456 +           && !au_ftest_cpup(cpg->flags, KEEPLINO))
5457 +               au_xino_write(sb, cpg->bsrc, h_inode->i_ino, /*ino*/0);
5458 +               /* ignore this error */
5459 +
5460 +       if (!err) {
5461 +               force = 0;
5462 +               if (isreg) {
5463 +                       force = !!cpg->len;
5464 +                       if (cpg->len == -1)
5465 +                               force = !!i_size_read(h_inode);
5466 +               }
5467 +               au_fhsm_wrote(sb, cpg->bdst, force);
5468 +       }
5469 +
5470 +       if (do_dt)
5471 +               au_dtime_revert(&dt);
5472 +       return err;
5473 +}
5474 +
5475 +static int au_do_ren_after_cpup(struct au_cp_generic *cpg, struct path *h_path)
5476 +{
5477 +       int err;
5478 +       struct dentry *dentry, *h_dentry, *h_parent, *parent;
5479 +       struct path h_ppath;
5480 +       struct inode *h_dir;
5481 +       aufs_bindex_t bdst;
5482 +
5483 +       dentry = cpg->dentry;
5484 +       bdst = cpg->bdst;
5485 +       h_ppath.mnt = au_sbr_mnt(dentry->d_sb, bdst);
5486 +       h_dentry = au_h_dptr(dentry, bdst);
5487 +       if (!au_ftest_cpup(cpg->flags, OVERWRITE)) {
5488 +               dget(h_dentry);
5489 +               au_set_h_dptr(dentry, bdst, NULL);
5490 +               err = au_lkup_neg(dentry, bdst, /*wh*/0);
5491 +               if (!err)
5492 +                       h_path->dentry = dget(au_h_dptr(dentry, bdst));
5493 +               au_set_h_dptr(dentry, bdst, h_dentry);
5494 +       } else {
5495 +               err = 0;
5496 +               parent = dget_parent(dentry);
5497 +               h_ppath.dentry = au_h_dptr(parent, bdst);
5498 +               dput(parent);
5499 +               h_path->dentry = vfsub_lkup_one(&dentry->d_name, &h_ppath);
5500 +               if (IS_ERR(h_path->dentry))
5501 +                       err = PTR_ERR(h_path->dentry);
5502 +       }
5503 +       if (unlikely(err))
5504 +               goto out;
5505 +
5506 +       h_parent = h_dentry->d_parent; /* dir inode is locked */
5507 +       h_dir = d_inode(h_parent);
5508 +       IMustLock(h_dir);
5509 +       AuDbg("%pd %pd\n", h_dentry, h_path->dentry);
5510 +       /* no delegation since it is just created */
5511 +       err = vfsub_rename(h_dir, h_dentry, h_dir, h_path, /*delegated*/NULL,
5512 +                          /*flags*/0);
5513 +       dput(h_path->dentry);
5514 +
5515 +out:
5516 +       return err;
5517 +}
5518 +
5519 +/*
5520 + * copyup the @dentry from @bsrc to @bdst.
5521 + * the caller must set the both of lower dentries.
5522 + * @len is for truncating when it is -1 copyup the entire file.
5523 + * in link/rename cases, @dst_parent may be different from the real one.
5524 + * basic->bsrc can be larger than basic->bdst.
5525 + * aufs doesn't touch the credential so
5526 + * security_inode_copy_up{,_xattr}() are unnecessary.
5527 + */
5528 +static int au_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
5529 +{
5530 +       int err, rerr;
5531 +       aufs_bindex_t old_ibtop;
5532 +       unsigned char isdir, plink;
5533 +       struct dentry *h_src, *h_dst, *h_parent;
5534 +       struct inode *dst_inode, *h_dir, *inode, *delegated, *src_inode;
5535 +       struct super_block *sb;
5536 +       struct au_branch *br;
5537 +       struct path h_src_path;
5538 +       /* to reduce stack size */
5539 +       struct {
5540 +               struct au_dtime dt;
5541 +               struct path h_path;
5542 +               struct au_cpup_reg_attr h_src_attr;
5543 +       } *a;
5544 +
5545 +       err = -ENOMEM;
5546 +       a = kmalloc(sizeof(*a), GFP_NOFS);
5547 +       if (unlikely(!a))
5548 +               goto out;
5549 +       a->h_src_attr.valid = 0;
5550 +
5551 +       sb = cpg->dentry->d_sb;
5552 +       br = au_sbr(sb, cpg->bdst);
5553 +       a->h_path.mnt = au_br_mnt(br);
5554 +       h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
5555 +       h_parent = h_dst->d_parent; /* dir inode is locked */
5556 +       h_dir = d_inode(h_parent);
5557 +       IMustLock(h_dir);
5558 +
5559 +       h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
5560 +       inode = d_inode(cpg->dentry);
5561 +
5562 +       if (!dst_parent)
5563 +               dst_parent = dget_parent(cpg->dentry);
5564 +       else
5565 +               dget(dst_parent);
5566 +
5567 +       plink = !!au_opt_test(au_mntflags(sb), PLINK);
5568 +       dst_inode = au_h_iptr(inode, cpg->bdst);
5569 +       if (dst_inode) {
5570 +               if (unlikely(!plink)) {
5571 +                       err = -EIO;
5572 +                       AuIOErr("hi%lu(i%lu) exists on b%d "
5573 +                               "but plink is disabled\n",
5574 +                               dst_inode->i_ino, inode->i_ino, cpg->bdst);
5575 +                       goto out_parent;
5576 +               }
5577 +
5578 +               if (dst_inode->i_nlink) {
5579 +                       const int do_dt = au_ftest_cpup(cpg->flags, DTIME);
5580 +
5581 +                       h_src = au_plink_lkup(inode, cpg->bdst);
5582 +                       err = PTR_ERR(h_src);
5583 +                       if (IS_ERR(h_src))
5584 +                               goto out_parent;
5585 +                       if (unlikely(d_is_negative(h_src))) {
5586 +                               err = -EIO;
5587 +                               AuIOErr("i%lu exists on b%d "
5588 +                                       "but not pseudo-linked\n",
5589 +                                       inode->i_ino, cpg->bdst);
5590 +                               dput(h_src);
5591 +                               goto out_parent;
5592 +                       }
5593 +
5594 +                       if (do_dt) {
5595 +                               a->h_path.dentry = h_parent;
5596 +                               au_dtime_store(&a->dt, dst_parent, &a->h_path);
5597 +                       }
5598 +
5599 +                       a->h_path.dentry = h_dst;
5600 +                       delegated = NULL;
5601 +                       err = vfsub_link(h_src, h_dir, &a->h_path, &delegated);
5602 +                       if (!err && au_ftest_cpup(cpg->flags, RENAME))
5603 +                               err = au_do_ren_after_cpup(cpg, &a->h_path);
5604 +                       if (do_dt)
5605 +                               au_dtime_revert(&a->dt);
5606 +                       if (unlikely(err == -EWOULDBLOCK)) {
5607 +                               pr_warn("cannot retry for NFSv4 delegation"
5608 +                                       " for an internal link\n");
5609 +                               iput(delegated);
5610 +                       }
5611 +                       dput(h_src);
5612 +                       goto out_parent;
5613 +               } else
5614 +                       /* todo: cpup_wh_file? */
5615 +                       /* udba work */
5616 +                       au_update_ibrange(inode, /*do_put_zero*/1);
5617 +       }
5618 +
5619 +       isdir = S_ISDIR(inode->i_mode);
5620 +       old_ibtop = au_ibtop(inode);
5621 +       err = cpup_entry(cpg, dst_parent, &a->h_src_attr);
5622 +       if (unlikely(err))
5623 +               goto out_rev;
5624 +       dst_inode = d_inode(h_dst);
5625 +       inode_lock_nested(dst_inode, AuLsc_I_CHILD2);
5626 +       /* todo: necessary? */
5627 +       /* au_pin_hdir_unlock(cpg->pin); */
5628 +
5629 +       h_src_path.dentry = h_src;
5630 +       h_src_path.mnt = au_sbr_mnt(sb, cpg->bsrc);
5631 +       err = cpup_iattr(cpg->dentry, cpg->bdst, &h_src_path, &a->h_src_attr);
5632 +       if (unlikely(err)) {
5633 +               /* todo: necessary? */
5634 +               /* au_pin_hdir_relock(cpg->pin); */ /* ignore an error */
5635 +               inode_unlock(dst_inode);
5636 +               goto out_rev;
5637 +       }
5638 +
5639 +       if (cpg->bdst < old_ibtop) {
5640 +               if (S_ISREG(inode->i_mode)) {
5641 +                       err = au_dy_iaop(inode, cpg->bdst, dst_inode);
5642 +                       if (unlikely(err)) {
5643 +                               /* ignore an error */
5644 +                               /* au_pin_hdir_relock(cpg->pin); */
5645 +                               inode_unlock(dst_inode);
5646 +                               goto out_rev;
5647 +                       }
5648 +               }
5649 +               au_set_ibtop(inode, cpg->bdst);
5650 +       } else
5651 +               au_set_ibbot(inode, cpg->bdst);
5652 +       au_set_h_iptr(inode, cpg->bdst, au_igrab(dst_inode),
5653 +                     au_hi_flags(inode, isdir));
5654 +
5655 +       /* todo: necessary? */
5656 +       /* err = au_pin_hdir_relock(cpg->pin); */
5657 +       inode_unlock(dst_inode);
5658 +       if (unlikely(err))
5659 +               goto out_rev;
5660 +
5661 +       src_inode = d_inode(h_src);
5662 +       if (!isdir
5663 +           && (src_inode->i_nlink > 1
5664 +               || src_inode->i_state & I_LINKABLE)
5665 +           && plink)
5666 +               au_plink_append(inode, cpg->bdst, h_dst);
5667 +
5668 +       if (au_ftest_cpup(cpg->flags, RENAME)) {
5669 +               a->h_path.dentry = h_dst;
5670 +               err = au_do_ren_after_cpup(cpg, &a->h_path);
5671 +       }
5672 +       if (!err)
5673 +               goto out_parent; /* success */
5674 +
5675 +       /* revert */
5676 +out_rev:
5677 +       a->h_path.dentry = h_parent;
5678 +       au_dtime_store(&a->dt, dst_parent, &a->h_path);
5679 +       a->h_path.dentry = h_dst;
5680 +       rerr = 0;
5681 +       if (d_is_positive(h_dst)) {
5682 +               if (!isdir) {
5683 +                       /* no delegation since it is just created */
5684 +                       rerr = vfsub_unlink(h_dir, &a->h_path,
5685 +                                           /*delegated*/NULL, /*force*/0);
5686 +               } else
5687 +                       rerr = vfsub_rmdir(h_dir, &a->h_path);
5688 +       }
5689 +       au_dtime_revert(&a->dt);
5690 +       if (rerr) {
5691 +               AuIOErr("failed removing broken entry(%d, %d)\n", err, rerr);
5692 +               err = -EIO;
5693 +       }
5694 +out_parent:
5695 +       dput(dst_parent);
5696 +       au_kfree_rcu(a);
5697 +out:
5698 +       return err;
5699 +}
5700 +
5701 +#if 0 /* reserved */
5702 +struct au_cpup_single_args {
5703 +       int *errp;
5704 +       struct au_cp_generic *cpg;
5705 +       struct dentry *dst_parent;
5706 +};
5707 +
5708 +static void au_call_cpup_single(void *args)
5709 +{
5710 +       struct au_cpup_single_args *a = args;
5711 +
5712 +       au_pin_hdir_acquire_nest(a->cpg->pin);
5713 +       *a->errp = au_cpup_single(a->cpg, a->dst_parent);
5714 +       au_pin_hdir_release(a->cpg->pin);
5715 +}
5716 +#endif
5717 +
5718 +/*
5719 + * prevent SIGXFSZ in copy-up.
5720 + * testing CAP_MKNOD is for generic fs,
5721 + * but CAP_FSETID is for xfs only, currently.
5722 + */
5723 +static int au_cpup_sio_test(struct au_pin *pin, umode_t mode)
5724 +{
5725 +       int do_sio;
5726 +       struct super_block *sb;
5727 +       struct inode *h_dir;
5728 +
5729 +       do_sio = 0;
5730 +       sb = au_pinned_parent(pin)->d_sb;
5731 +       if (!au_wkq_test()
5732 +           && (!au_sbi(sb)->si_plink_maint_pid
5733 +               || au_plink_maint(sb, AuLock_NOPLM))) {
5734 +               switch (mode & S_IFMT) {
5735 +               case S_IFREG:
5736 +                       /* no condition about RLIMIT_FSIZE and the file size */
5737 +                       do_sio = 1;
5738 +                       break;
5739 +               case S_IFCHR:
5740 +               case S_IFBLK:
5741 +                       do_sio = !capable(CAP_MKNOD);
5742 +                       break;
5743 +               }
5744 +               if (!do_sio)
5745 +                       do_sio = ((mode & (S_ISUID | S_ISGID))
5746 +                                 && !capable(CAP_FSETID));
5747 +               /* this workaround may be removed in the future */
5748 +               if (!do_sio) {
5749 +                       h_dir = au_pinned_h_dir(pin);
5750 +                       do_sio = h_dir->i_mode & S_ISVTX;
5751 +               }
5752 +       }
5753 +
5754 +       return do_sio;
5755 +}
5756 +
5757 +#if 0 /* reserved */
5758 +int au_sio_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
5759 +{
5760 +       int err, wkq_err;
5761 +       struct dentry *h_dentry;
5762 +
5763 +       h_dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
5764 +       if (!au_cpup_sio_test(pin, d_inode(h_dentry)->i_mode))
5765 +               err = au_cpup_single(cpg, dst_parent);
5766 +       else {
5767 +               struct au_cpup_single_args args = {
5768 +                       .errp           = &err,
5769 +                       .cpg            = cpg,
5770 +                       .dst_parent     = dst_parent
5771 +               };
5772 +               wkq_err = au_wkq_wait(au_call_cpup_single, &args);
5773 +               if (unlikely(wkq_err))
5774 +                       err = wkq_err;
5775 +       }
5776 +
5777 +       return err;
5778 +}
5779 +#endif
5780 +
5781 +/*
5782 + * copyup the @dentry from the first active lower branch to @bdst,
5783 + * using au_cpup_single().
5784 + */
5785 +static int au_cpup_simple(struct au_cp_generic *cpg)
5786 +{
5787 +       int err;
5788 +       unsigned int flags_orig;
5789 +       struct dentry *dentry;
5790 +
5791 +       AuDebugOn(cpg->bsrc < 0);
5792 +
5793 +       dentry = cpg->dentry;
5794 +       DiMustWriteLock(dentry);
5795 +
5796 +       err = au_lkup_neg(dentry, cpg->bdst, /*wh*/1);
5797 +       if (!err) {
5798 +               flags_orig = cpg->flags;
5799 +               au_fset_cpup(cpg->flags, RENAME);
5800 +               err = au_cpup_single(cpg, NULL);
5801 +               cpg->flags = flags_orig;
5802 +               if (!err)
5803 +                       return 0; /* success */
5804 +
5805 +               /* revert */
5806 +               au_set_h_dptr(dentry, cpg->bdst, NULL);
5807 +               au_set_dbtop(dentry, cpg->bsrc);
5808 +       }
5809 +
5810 +       return err;
5811 +}
5812 +
5813 +struct au_cpup_simple_args {
5814 +       int *errp;
5815 +       struct au_cp_generic *cpg;
5816 +};
5817 +
5818 +static void au_call_cpup_simple(void *args)
5819 +{
5820 +       struct au_cpup_simple_args *a = args;
5821 +
5822 +       au_pin_hdir_acquire_nest(a->cpg->pin);
5823 +       *a->errp = au_cpup_simple(a->cpg);
5824 +       au_pin_hdir_release(a->cpg->pin);
5825 +}
5826 +
5827 +static int au_do_sio_cpup_simple(struct au_cp_generic *cpg)
5828 +{
5829 +       int err, wkq_err;
5830 +       struct dentry *dentry, *parent;
5831 +       struct file *h_file;
5832 +       struct inode *h_dir;
5833 +       struct user_namespace *h_userns;
5834 +
5835 +       dentry = cpg->dentry;
5836 +       h_file = NULL;
5837 +       if (au_ftest_cpup(cpg->flags, HOPEN)) {
5838 +               AuDebugOn(cpg->bsrc < 0);
5839 +               h_file = au_h_open_pre(dentry, cpg->bsrc, /*force_wr*/0);
5840 +               err = PTR_ERR(h_file);
5841 +               if (IS_ERR(h_file))
5842 +                       goto out;
5843 +       }
5844 +
5845 +       parent = dget_parent(dentry);
5846 +       h_dir = au_h_iptr(d_inode(parent), cpg->bdst);
5847 +       h_userns = au_sbr_userns(dentry->d_sb, cpg->bdst);
5848 +       if (!au_test_h_perm_sio(h_userns, h_dir, MAY_EXEC | MAY_WRITE)
5849 +           && !au_cpup_sio_test(cpg->pin, d_inode(dentry)->i_mode))
5850 +               err = au_cpup_simple(cpg);
5851 +       else {
5852 +               struct au_cpup_simple_args args = {
5853 +                       .errp           = &err,
5854 +                       .cpg            = cpg
5855 +               };
5856 +               wkq_err = au_wkq_wait(au_call_cpup_simple, &args);
5857 +               if (unlikely(wkq_err))
5858 +                       err = wkq_err;
5859 +       }
5860 +
5861 +       dput(parent);
5862 +       if (h_file)
5863 +               au_h_open_post(dentry, cpg->bsrc, h_file);
5864 +
5865 +out:
5866 +       return err;
5867 +}
5868 +
5869 +int au_sio_cpup_simple(struct au_cp_generic *cpg)
5870 +{
5871 +       aufs_bindex_t bsrc, bbot;
5872 +       struct dentry *dentry, *h_dentry;
5873 +
5874 +       if (cpg->bsrc < 0) {
5875 +               dentry = cpg->dentry;
5876 +               bbot = au_dbbot(dentry);
5877 +               for (bsrc = cpg->bdst + 1; bsrc <= bbot; bsrc++) {
5878 +                       h_dentry = au_h_dptr(dentry, bsrc);
5879 +                       if (h_dentry) {
5880 +                               AuDebugOn(d_is_negative(h_dentry));
5881 +                               break;
5882 +                       }
5883 +               }
5884 +               AuDebugOn(bsrc > bbot);
5885 +               cpg->bsrc = bsrc;
5886 +       }
5887 +       AuDebugOn(cpg->bsrc <= cpg->bdst);
5888 +       return au_do_sio_cpup_simple(cpg);
5889 +}
5890 +
5891 +int au_sio_cpdown_simple(struct au_cp_generic *cpg)
5892 +{
5893 +       AuDebugOn(cpg->bdst <= cpg->bsrc);
5894 +       return au_do_sio_cpup_simple(cpg);
5895 +}
5896 +
5897 +/* ---------------------------------------------------------------------- */
5898 +
5899 +/*
5900 + * copyup the deleted file for writing.
5901 + */
5902 +static int au_do_cpup_wh(struct au_cp_generic *cpg, struct dentry *wh_dentry,
5903 +                        struct file *file)
5904 +{
5905 +       int err;
5906 +       unsigned int flags_orig;
5907 +       aufs_bindex_t bsrc_orig;
5908 +       struct au_dinfo *dinfo;
5909 +       struct {
5910 +               struct au_hdentry *hd;
5911 +               struct dentry *h_dentry;
5912 +       } hdst, hsrc;
5913 +
5914 +       dinfo = au_di(cpg->dentry);
5915 +       AuRwMustWriteLock(&dinfo->di_rwsem);
5916 +
5917 +       bsrc_orig = cpg->bsrc;
5918 +       cpg->bsrc = dinfo->di_btop;
5919 +       hdst.hd = au_hdentry(dinfo, cpg->bdst);
5920 +       hdst.h_dentry = hdst.hd->hd_dentry;
5921 +       hdst.hd->hd_dentry = wh_dentry;
5922 +       dinfo->di_btop = cpg->bdst;
5923 +
5924 +       hsrc.h_dentry = NULL;
5925 +       if (file) {
5926 +               hsrc.hd = au_hdentry(dinfo, cpg->bsrc);
5927 +               hsrc.h_dentry = hsrc.hd->hd_dentry;
5928 +               hsrc.hd->hd_dentry = au_hf_top(file)->f_path.dentry;
5929 +       }
5930 +       flags_orig = cpg->flags;
5931 +       cpg->flags = !AuCpup_DTIME;
5932 +       err = au_cpup_single(cpg, /*h_parent*/NULL);
5933 +       cpg->flags = flags_orig;
5934 +       if (file) {
5935 +               if (!err)
5936 +                       err = au_reopen_nondir(file);
5937 +               hsrc.hd->hd_dentry = hsrc.h_dentry;
5938 +       }
5939 +       hdst.hd->hd_dentry = hdst.h_dentry;
5940 +       dinfo->di_btop = cpg->bsrc;
5941 +       cpg->bsrc = bsrc_orig;
5942 +
5943 +       return err;
5944 +}
5945 +
5946 +static int au_cpup_wh(struct au_cp_generic *cpg, struct file *file)
5947 +{
5948 +       int err;
5949 +       aufs_bindex_t bdst;
5950 +       struct au_dtime dt;
5951 +       struct dentry *dentry, *parent, *h_parent, *wh_dentry;
5952 +       struct au_branch *br;
5953 +       struct path h_path;
5954 +
5955 +       dentry = cpg->dentry;
5956 +       bdst = cpg->bdst;
5957 +       br = au_sbr(dentry->d_sb, bdst);
5958 +       parent = dget_parent(dentry);
5959 +       h_parent = au_h_dptr(parent, bdst);
5960 +       wh_dentry = au_whtmp_lkup(h_parent, br, &dentry->d_name);
5961 +       err = PTR_ERR(wh_dentry);
5962 +       if (IS_ERR(wh_dentry))
5963 +               goto out;
5964 +
5965 +       h_path.dentry = h_parent;
5966 +       h_path.mnt = au_br_mnt(br);
5967 +       au_dtime_store(&dt, parent, &h_path);
5968 +       err = au_do_cpup_wh(cpg, wh_dentry, file);
5969 +       if (unlikely(err))
5970 +               goto out_wh;
5971 +
5972 +       dget(wh_dentry);
5973 +       h_path.dentry = wh_dentry;
5974 +       if (!d_is_dir(wh_dentry)) {
5975 +               /* no delegation since it is just created */
5976 +               err = vfsub_unlink(d_inode(h_parent), &h_path,
5977 +                                  /*delegated*/NULL, /*force*/0);
5978 +       } else
5979 +               err = vfsub_rmdir(d_inode(h_parent), &h_path);
5980 +       if (unlikely(err)) {
5981 +               AuIOErr("failed remove copied-up tmp file %pd(%d)\n",
5982 +                       wh_dentry, err);
5983 +               err = -EIO;
5984 +       }
5985 +       au_dtime_revert(&dt);
5986 +       au_set_hi_wh(d_inode(dentry), bdst, wh_dentry);
5987 +
5988 +out_wh:
5989 +       dput(wh_dentry);
5990 +out:
5991 +       dput(parent);
5992 +       return err;
5993 +}
5994 +
5995 +struct au_cpup_wh_args {
5996 +       int *errp;
5997 +       struct au_cp_generic *cpg;
5998 +       struct file *file;
5999 +};
6000 +
6001 +static void au_call_cpup_wh(void *args)
6002 +{
6003 +       struct au_cpup_wh_args *a = args;
6004 +
6005 +       au_pin_hdir_acquire_nest(a->cpg->pin);
6006 +       *a->errp = au_cpup_wh(a->cpg, a->file);
6007 +       au_pin_hdir_release(a->cpg->pin);
6008 +}
6009 +
6010 +int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file)
6011 +{
6012 +       int err, wkq_err;
6013 +       aufs_bindex_t bdst;
6014 +       struct dentry *dentry, *parent, *h_orph, *h_parent;
6015 +       struct inode *dir, *h_dir, *h_tmpdir;
6016 +       struct au_wbr *wbr;
6017 +       struct au_pin wh_pin, *pin_orig;
6018 +       struct user_namespace *h_userns;
6019 +
6020 +       dentry = cpg->dentry;
6021 +       bdst = cpg->bdst;
6022 +       parent = dget_parent(dentry);
6023 +       dir = d_inode(parent);
6024 +       h_orph = NULL;
6025 +       h_parent = NULL;
6026 +       h_dir = au_igrab(au_h_iptr(dir, bdst));
6027 +       h_tmpdir = h_dir;
6028 +       pin_orig = NULL;
6029 +       if (!h_dir->i_nlink) {
6030 +               wbr = au_sbr(dentry->d_sb, bdst)->br_wbr;
6031 +               h_orph = wbr->wbr_orph;
6032 +
6033 +               h_parent = dget(au_h_dptr(parent, bdst));
6034 +               au_set_h_dptr(parent, bdst, dget(h_orph));
6035 +               h_tmpdir = d_inode(h_orph);
6036 +               au_set_h_iptr(dir, bdst, au_igrab(h_tmpdir), /*flags*/0);
6037 +
6038 +               inode_lock_nested(h_tmpdir, AuLsc_I_PARENT3);
6039 +               /* todo: au_h_open_pre()? */
6040 +
6041 +               pin_orig = cpg->pin;
6042 +               au_pin_init(&wh_pin, dentry, bdst, AuLsc_DI_PARENT,
6043 +                           AuLsc_I_PARENT3, cpg->pin->udba, AuPin_DI_LOCKED);
6044 +               cpg->pin = &wh_pin;
6045 +       }
6046 +
6047 +       h_userns = au_sbr_userns(dentry->d_sb, bdst);
6048 +       if (!au_test_h_perm_sio(h_userns, h_tmpdir, MAY_EXEC | MAY_WRITE)
6049 +           && !au_cpup_sio_test(cpg->pin, d_inode(dentry)->i_mode))
6050 +               err = au_cpup_wh(cpg, file);
6051 +       else {
6052 +               struct au_cpup_wh_args args = {
6053 +                       .errp   = &err,
6054 +                       .cpg    = cpg,
6055 +                       .file   = file
6056 +               };
6057 +               wkq_err = au_wkq_wait(au_call_cpup_wh, &args);
6058 +               if (unlikely(wkq_err))
6059 +                       err = wkq_err;
6060 +       }
6061 +
6062 +       if (h_orph) {
6063 +               inode_unlock(h_tmpdir);
6064 +               /* todo: au_h_open_post()? */
6065 +               au_set_h_iptr(dir, bdst, au_igrab(h_dir), /*flags*/0);
6066 +               au_set_h_dptr(parent, bdst, h_parent);
6067 +               AuDebugOn(!pin_orig);
6068 +               cpg->pin = pin_orig;
6069 +       }
6070 +       iput(h_dir);
6071 +       dput(parent);
6072 +
6073 +       return err;
6074 +}
6075 +
6076 +/* ---------------------------------------------------------------------- */
6077 +
6078 +/*
6079 + * generic routine for both of copy-up and copy-down.
6080 + */
6081 +/* cf. revalidate function in file.c */
6082 +int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
6083 +              int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
6084 +                        struct au_pin *pin,
6085 +                        struct dentry *h_parent, void *arg),
6086 +              void *arg)
6087 +{
6088 +       int err;
6089 +       struct au_pin pin;
6090 +       struct dentry *d, *parent, *h_parent, *real_parent, *h_dentry;
6091 +
6092 +       err = 0;
6093 +       parent = dget_parent(dentry);
6094 +       if (IS_ROOT(parent))
6095 +               goto out;
6096 +
6097 +       au_pin_init(&pin, dentry, bdst, AuLsc_DI_PARENT2, AuLsc_I_PARENT2,
6098 +                   au_opt_udba(dentry->d_sb), AuPin_MNT_WRITE);
6099 +
6100 +       /* do not use au_dpage */
6101 +       real_parent = parent;
6102 +       while (1) {
6103 +               dput(parent);
6104 +               parent = dget_parent(dentry);
6105 +               h_parent = au_h_dptr(parent, bdst);
6106 +               if (h_parent)
6107 +                       goto out; /* success */
6108 +
6109 +               /* find top dir which is necessary to cpup */
6110 +               do {
6111 +                       d = parent;
6112 +                       dput(parent);
6113 +                       parent = dget_parent(d);
6114 +                       di_read_lock_parent3(parent, !AuLock_IR);
6115 +                       h_parent = au_h_dptr(parent, bdst);
6116 +                       di_read_unlock(parent, !AuLock_IR);
6117 +               } while (!h_parent);
6118 +
6119 +               if (d != real_parent)
6120 +                       di_write_lock_child3(d);
6121 +
6122 +               /* somebody else might create while we were sleeping */
6123 +               h_dentry = au_h_dptr(d, bdst);
6124 +               if (!h_dentry || d_is_negative(h_dentry)) {
6125 +                       if (h_dentry)
6126 +                               au_update_dbtop(d);
6127 +
6128 +                       au_pin_set_dentry(&pin, d);
6129 +                       err = au_do_pin(&pin);
6130 +                       if (!err) {
6131 +                               err = cp(d, bdst, &pin, h_parent, arg);
6132 +                               au_unpin(&pin);
6133 +                       }
6134 +               }
6135 +
6136 +               if (d != real_parent)
6137 +                       di_write_unlock(d);
6138 +               if (unlikely(err))
6139 +                       break;
6140 +       }
6141 +
6142 +out:
6143 +       dput(parent);
6144 +       return err;
6145 +}
6146 +
6147 +static int au_cpup_dir(struct dentry *dentry, aufs_bindex_t bdst,
6148 +                      struct au_pin *pin,
6149 +                      struct dentry *h_parent __maybe_unused,
6150 +                      void *arg __maybe_unused)
6151 +{
6152 +       struct au_cp_generic cpg = {
6153 +               .dentry = dentry,
6154 +               .bdst   = bdst,
6155 +               .bsrc   = -1,
6156 +               .len    = 0,
6157 +               .pin    = pin,
6158 +               .flags  = AuCpup_DTIME
6159 +       };
6160 +       return au_sio_cpup_simple(&cpg);
6161 +}
6162 +
6163 +int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
6164 +{
6165 +       return au_cp_dirs(dentry, bdst, au_cpup_dir, NULL);
6166 +}
6167 +
6168 +int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
6169 +{
6170 +       int err;
6171 +       struct dentry *parent;
6172 +       struct inode *dir;
6173 +
6174 +       parent = dget_parent(dentry);
6175 +       dir = d_inode(parent);
6176 +       err = 0;
6177 +       if (au_h_iptr(dir, bdst))
6178 +               goto out;
6179 +
6180 +       di_read_unlock(parent, AuLock_IR);
6181 +       di_write_lock_parent(parent);
6182 +       /* someone else might change our inode while we were sleeping */
6183 +       if (!au_h_iptr(dir, bdst))
6184 +               err = au_cpup_dirs(dentry, bdst);
6185 +       di_downgrade_lock(parent, AuLock_IR);
6186 +
6187 +out:
6188 +       dput(parent);
6189 +       return err;
6190 +}
6191 diff -urN /usr/share/empty/fs/aufs/cpup.h linux/fs/aufs/cpup.h
6192 --- /usr/share/empty/fs/aufs/cpup.h     1970-01-01 01:00:00.000000000 +0100
6193 +++ linux/fs/aufs/cpup.h        2022-03-21 14:49:05.719966344 +0100
6194 @@ -0,0 +1,100 @@
6195 +/* SPDX-License-Identifier: GPL-2.0 */
6196 +/*
6197 + * Copyright (C) 2005-2021 Junjiro R. Okajima
6198 + *
6199 + * This program, aufs is free software; you can redistribute it and/or modify
6200 + * it under the terms of the GNU General Public License as published by
6201 + * the Free Software Foundation; either version 2 of the License, or
6202 + * (at your option) any later version.
6203 + *
6204 + * This program is distributed in the hope that it will be useful,
6205 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6206 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6207 + * GNU General Public License for more details.
6208 + *
6209 + * You should have received a copy of the GNU General Public License
6210 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6211 + */
6212 +
6213 +/*
6214 + * copy-up/down functions
6215 + */
6216 +
6217 +#ifndef __AUFS_CPUP_H__
6218 +#define __AUFS_CPUP_H__
6219 +
6220 +#ifdef __KERNEL__
6221 +
6222 +#include <linux/path.h>
6223 +
6224 +struct inode;
6225 +struct file;
6226 +struct au_pin;
6227 +
6228 +void au_cpup_attr_flags(struct inode *dst, unsigned int iflags);
6229 +void au_cpup_attr_timesizes(struct inode *inode);
6230 +void au_cpup_attr_nlink(struct inode *inode, int force);
6231 +void au_cpup_attr_changeable(struct inode *inode);
6232 +void au_cpup_igen(struct inode *inode, struct inode *h_inode);
6233 +void au_cpup_attr_all(struct inode *inode, int force);
6234 +
6235 +/* ---------------------------------------------------------------------- */
6236 +
6237 +struct au_cp_generic {
6238 +       struct dentry   *dentry;
6239 +       aufs_bindex_t   bdst, bsrc;
6240 +       loff_t          len;
6241 +       struct au_pin   *pin;
6242 +       unsigned int    flags;
6243 +};
6244 +
6245 +/* cpup flags */
6246 +#define AuCpup_DTIME           1               /* do dtime_store/revert */
6247 +#define AuCpup_KEEPLINO                (1 << 1)        /* do not clear the lower xino,
6248 +                                                  for link(2) */
6249 +#define AuCpup_RENAME          (1 << 2)        /* rename after cpup */
6250 +#define AuCpup_HOPEN           (1 << 3)        /* call h_open_pre/post() in
6251 +                                                  cpup */
6252 +#define AuCpup_OVERWRITE       (1 << 4)        /* allow overwriting the
6253 +                                                  existing entry */
6254 +#define AuCpup_RWDST           (1 << 5)        /* force write target even if
6255 +                                                  the branch is marked as RO */
6256 +
6257 +#ifndef CONFIG_AUFS_BR_HFSPLUS
6258 +#undef AuCpup_HOPEN
6259 +#define AuCpup_HOPEN           0
6260 +#endif
6261 +
6262 +#define au_ftest_cpup(flags, name)     ((flags) & AuCpup_##name)
6263 +#define au_fset_cpup(flags, name) \
6264 +       do { (flags) |= AuCpup_##name; } while (0)
6265 +#define au_fclr_cpup(flags, name) \
6266 +       do { (flags) &= ~AuCpup_##name; } while (0)
6267 +
6268 +int au_copy_file(struct file *dst, struct file *src, loff_t len);
6269 +int au_sio_cpup_simple(struct au_cp_generic *cpg);
6270 +int au_sio_cpdown_simple(struct au_cp_generic *cpg);
6271 +int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file);
6272 +
6273 +int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
6274 +              int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
6275 +                        struct au_pin *pin,
6276 +                        struct dentry *h_parent, void *arg),
6277 +              void *arg);
6278 +int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
6279 +int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
6280 +
6281 +/* ---------------------------------------------------------------------- */
6282 +
6283 +/* keep timestamps when copyup */
6284 +struct au_dtime {
6285 +       struct dentry *dt_dentry;
6286 +       struct path dt_h_path;
6287 +       struct timespec64 dt_atime, dt_mtime;
6288 +};
6289 +void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
6290 +                   struct path *h_path);
6291 +void au_dtime_revert(struct au_dtime *dt);
6292 +
6293 +#endif /* __KERNEL__ */
6294 +#endif /* __AUFS_CPUP_H__ */
6295 diff -urN /usr/share/empty/fs/aufs/dbgaufs.c linux/fs/aufs/dbgaufs.c
6296 --- /usr/share/empty/fs/aufs/dbgaufs.c  1970-01-01 01:00:00.000000000 +0100
6297 +++ linux/fs/aufs/dbgaufs.c     2022-03-21 14:49:05.719966344 +0100
6298 @@ -0,0 +1,526 @@
6299 +// SPDX-License-Identifier: GPL-2.0
6300 +/*
6301 + * Copyright (C) 2005-2021 Junjiro R. Okajima
6302 + *
6303 + * This program, aufs is free software; you can redistribute it and/or modify
6304 + * it under the terms of the GNU General Public License as published by
6305 + * the Free Software Foundation; either version 2 of the License, or
6306 + * (at your option) any later version.
6307 + *
6308 + * This program is distributed in the hope that it will be useful,
6309 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6310 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6311 + * GNU General Public License for more details.
6312 + *
6313 + * You should have received a copy of the GNU General Public License
6314 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6315 + */
6316 +
6317 +/*
6318 + * debugfs interface
6319 + */
6320 +
6321 +#include <linux/debugfs.h>
6322 +#include "aufs.h"
6323 +
6324 +#ifndef CONFIG_SYSFS
6325 +#error DEBUG_FS depends upon SYSFS
6326 +#endif
6327 +
6328 +static struct dentry *dbgaufs;
6329 +static const mode_t dbgaufs_mode = 0444;
6330 +
6331 +/* 20 is max digits length of ulong 64 */
6332 +struct dbgaufs_arg {
6333 +       int n;
6334 +       char a[20 * 4];
6335 +};
6336 +
6337 +/*
6338 + * common function for all XINO files
6339 + */
6340 +static int dbgaufs_xi_release(struct inode *inode __maybe_unused,
6341 +                             struct file *file)
6342 +{
6343 +       void *p;
6344 +
6345 +       p = file->private_data;
6346 +       if (p) {
6347 +               /* this is struct dbgaufs_arg */
6348 +               AuDebugOn(!au_kfree_sz_test(p));
6349 +               au_kfree_do_rcu(p);
6350 +       }
6351 +       return 0;
6352 +}
6353 +
6354 +static int dbgaufs_xi_open(struct file *xf, struct file *file, int do_fcnt,
6355 +                          int cnt)
6356 +{
6357 +       int err;
6358 +       struct kstat st;
6359 +       struct dbgaufs_arg *p;
6360 +
6361 +       err = -ENOMEM;
6362 +       p = kmalloc(sizeof(*p), GFP_NOFS);
6363 +       if (unlikely(!p))
6364 +               goto out;
6365 +
6366 +       err = 0;
6367 +       p->n = 0;
6368 +       file->private_data = p;
6369 +       if (!xf)
6370 +               goto out;
6371 +
6372 +       err = vfsub_getattr(&xf->f_path, &st);
6373 +       if (!err) {
6374 +               if (do_fcnt)
6375 +                       p->n = snprintf
6376 +                               (p->a, sizeof(p->a), "%d, %llux%u %lld\n",
6377 +                                cnt, st.blocks, st.blksize,
6378 +                                (long long)st.size);
6379 +               else
6380 +                       p->n = snprintf(p->a, sizeof(p->a), "%llux%u %lld\n",
6381 +                                       st.blocks, st.blksize,
6382 +                                       (long long)st.size);
6383 +               AuDebugOn(p->n >= sizeof(p->a));
6384 +       } else {
6385 +               p->n = snprintf(p->a, sizeof(p->a), "err %d\n", err);
6386 +               err = 0;
6387 +       }
6388 +
6389 +out:
6390 +       return err;
6391 +}
6392 +
6393 +static ssize_t dbgaufs_xi_read(struct file *file, char __user *buf,
6394 +                              size_t count, loff_t *ppos)
6395 +{
6396 +       struct dbgaufs_arg *p;
6397 +
6398 +       p = file->private_data;
6399 +       return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
6400 +}
6401 +
6402 +/* ---------------------------------------------------------------------- */
6403 +
6404 +struct dbgaufs_plink_arg {
6405 +       int n;
6406 +       char a[];
6407 +};
6408 +
6409 +static int dbgaufs_plink_release(struct inode *inode __maybe_unused,
6410 +                                struct file *file)
6411 +{
6412 +       free_page((unsigned long)file->private_data);
6413 +       return 0;
6414 +}
6415 +
6416 +static int dbgaufs_plink_open(struct inode *inode, struct file *file)
6417 +{
6418 +       int err, i, limit;
6419 +       unsigned long n, sum;
6420 +       struct dbgaufs_plink_arg *p;
6421 +       struct au_sbinfo *sbinfo;
6422 +       struct super_block *sb;
6423 +       struct hlist_bl_head *hbl;
6424 +
6425 +       err = -ENOMEM;
6426 +       p = (void *)get_zeroed_page(GFP_NOFS);
6427 +       if (unlikely(!p))
6428 +               goto out;
6429 +
6430 +       err = -EFBIG;
6431 +       sbinfo = inode->i_private;
6432 +       sb = sbinfo->si_sb;
6433 +       si_noflush_read_lock(sb);
6434 +       if (au_opt_test(au_mntflags(sb), PLINK)) {
6435 +               limit = PAGE_SIZE - sizeof(p->n);
6436 +
6437 +               /* the number of buckets */
6438 +               n = snprintf(p->a + p->n, limit, "%d\n", AuPlink_NHASH);
6439 +               p->n += n;
6440 +               limit -= n;
6441 +
6442 +               sum = 0;
6443 +               for (i = 0, hbl = sbinfo->si_plink; i < AuPlink_NHASH;
6444 +                    i++, hbl++) {
6445 +                       n = au_hbl_count(hbl);
6446 +                       sum += n;
6447 +
6448 +                       n = snprintf(p->a + p->n, limit, "%lu ", n);
6449 +                       p->n += n;
6450 +                       limit -= n;
6451 +                       if (unlikely(limit <= 0))
6452 +                               goto out_free;
6453 +               }
6454 +               p->a[p->n - 1] = '\n';
6455 +
6456 +               /* the sum of plinks */
6457 +               n = snprintf(p->a + p->n, limit, "%lu\n", sum);
6458 +               p->n += n;
6459 +               limit -= n;
6460 +               if (unlikely(limit <= 0))
6461 +                       goto out_free;
6462 +       } else {
6463 +#define str "1\n0\n0\n"
6464 +               p->n = sizeof(str) - 1;
6465 +               strcpy(p->a, str);
6466 +#undef str
6467 +       }
6468 +       si_read_unlock(sb);
6469 +
6470 +       err = 0;
6471 +       file->private_data = p;
6472 +       goto out; /* success */
6473 +
6474 +out_free:
6475 +       free_page((unsigned long)p);
6476 +out:
6477 +       return err;
6478 +}
6479 +
6480 +static ssize_t dbgaufs_plink_read(struct file *file, char __user *buf,
6481 +                                 size_t count, loff_t *ppos)
6482 +{
6483 +       struct dbgaufs_plink_arg *p;
6484 +
6485 +       p = file->private_data;
6486 +       return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
6487 +}
6488 +
6489 +static const struct file_operations dbgaufs_plink_fop = {
6490 +       .owner          = THIS_MODULE,
6491 +       .open           = dbgaufs_plink_open,
6492 +       .release        = dbgaufs_plink_release,
6493 +       .read           = dbgaufs_plink_read
6494 +};
6495 +
6496 +/* ---------------------------------------------------------------------- */
6497 +
6498 +static int dbgaufs_xib_open(struct inode *inode, struct file *file)
6499 +{
6500 +       int err;
6501 +       struct au_sbinfo *sbinfo;
6502 +       struct super_block *sb;
6503 +
6504 +       sbinfo = inode->i_private;
6505 +       sb = sbinfo->si_sb;
6506 +       si_noflush_read_lock(sb);
6507 +       err = dbgaufs_xi_open(sbinfo->si_xib, file, /*do_fcnt*/0, /*cnt*/0);
6508 +       si_read_unlock(sb);
6509 +       return err;
6510 +}
6511 +
6512 +static const struct file_operations dbgaufs_xib_fop = {
6513 +       .owner          = THIS_MODULE,
6514 +       .open           = dbgaufs_xib_open,
6515 +       .release        = dbgaufs_xi_release,
6516 +       .read           = dbgaufs_xi_read
6517 +};
6518 +
6519 +/* ---------------------------------------------------------------------- */
6520 +
6521 +#define DbgaufsXi_PREFIX "xi"
6522 +
6523 +static int dbgaufs_xino_open(struct inode *inode, struct file *file)
6524 +{
6525 +       int err, idx;
6526 +       long l;
6527 +       aufs_bindex_t bindex;
6528 +       char *p, a[sizeof(DbgaufsXi_PREFIX) + 8];
6529 +       struct au_sbinfo *sbinfo;
6530 +       struct super_block *sb;
6531 +       struct au_xino *xi;
6532 +       struct file *xf;
6533 +       struct qstr *name;
6534 +       struct au_branch *br;
6535 +
6536 +       err = -ENOENT;
6537 +       name = &file->f_path.dentry->d_name;
6538 +       if (unlikely(name->len < sizeof(DbgaufsXi_PREFIX)
6539 +                    || memcmp(name->name, DbgaufsXi_PREFIX,
6540 +                              sizeof(DbgaufsXi_PREFIX) - 1)))
6541 +               goto out;
6542 +
6543 +       AuDebugOn(name->len >= sizeof(a));
6544 +       memcpy(a, name->name, name->len);
6545 +       a[name->len] = '\0';
6546 +       p = strchr(a, '-');
6547 +       if (p)
6548 +               *p = '\0';
6549 +       err = kstrtol(a + sizeof(DbgaufsXi_PREFIX) - 1, 10, &l);
6550 +       if (unlikely(err))
6551 +               goto out;
6552 +       bindex = l;
6553 +       idx = 0;
6554 +       if (p) {
6555 +               err = kstrtol(p + 1, 10, &l);
6556 +               if (unlikely(err))
6557 +                       goto out;
6558 +               idx = l;
6559 +       }
6560 +
6561 +       err = -ENOENT;
6562 +       sbinfo = inode->i_private;
6563 +       sb = sbinfo->si_sb;
6564 +       si_noflush_read_lock(sb);
6565 +       if (unlikely(bindex < 0 || bindex > au_sbbot(sb)))
6566 +               goto out_si;
6567 +       br = au_sbr(sb, bindex);
6568 +       xi = br->br_xino;
6569 +       if (unlikely(idx >= xi->xi_nfile))
6570 +               goto out_si;
6571 +       xf = au_xino_file(xi, idx);
6572 +       if (xf)
6573 +               err = dbgaufs_xi_open(xf, file, /*do_fcnt*/1,
6574 +                                     au_xino_count(br));
6575 +
6576 +out_si:
6577 +       si_read_unlock(sb);
6578 +out:
6579 +       AuTraceErr(err);
6580 +       return err;
6581 +}
6582 +
6583 +static const struct file_operations dbgaufs_xino_fop = {
6584 +       .owner          = THIS_MODULE,
6585 +       .open           = dbgaufs_xino_open,
6586 +       .release        = dbgaufs_xi_release,
6587 +       .read           = dbgaufs_xi_read
6588 +};
6589 +
6590 +void dbgaufs_xino_del(struct au_branch *br)
6591 +{
6592 +       struct dentry *dbgaufs;
6593 +
6594 +       dbgaufs = br->br_dbgaufs;
6595 +       if (!dbgaufs)
6596 +               return;
6597 +
6598 +       br->br_dbgaufs = NULL;
6599 +       /* debugfs acquires the parent i_mutex */
6600 +       lockdep_off();
6601 +       debugfs_remove(dbgaufs);
6602 +       lockdep_on();
6603 +}
6604 +
6605 +void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
6606 +{
6607 +       aufs_bindex_t bbot;
6608 +       struct au_branch *br;
6609 +
6610 +       if (!au_sbi(sb)->si_dbgaufs)
6611 +               return;
6612 +
6613 +       bbot = au_sbbot(sb);
6614 +       for (; bindex <= bbot; bindex++) {
6615 +               br = au_sbr(sb, bindex);
6616 +               dbgaufs_xino_del(br);
6617 +       }
6618 +}
6619 +
6620 +static void dbgaufs_br_do_add(struct super_block *sb, aufs_bindex_t bindex,
6621 +                             unsigned int idx, struct dentry *parent,
6622 +                             struct au_sbinfo *sbinfo)
6623 +{
6624 +       struct au_branch *br;
6625 +       struct dentry *d;
6626 +       /* "xi" bindex(5) "-" idx(2) NULL */
6627 +       char name[sizeof(DbgaufsXi_PREFIX) + 8];
6628 +
6629 +       if (!idx)
6630 +               snprintf(name, sizeof(name), DbgaufsXi_PREFIX "%d", bindex);
6631 +       else
6632 +               snprintf(name, sizeof(name), DbgaufsXi_PREFIX "%d-%u",
6633 +                        bindex, idx);
6634 +       br = au_sbr(sb, bindex);
6635 +       if (br->br_dbgaufs) {
6636 +               struct qstr qstr = QSTR_INIT(name, strlen(name));
6637 +
6638 +               if (!au_qstreq(&br->br_dbgaufs->d_name, &qstr)) {
6639 +                       /* debugfs acquires the parent i_mutex */
6640 +                       lockdep_off();
6641 +                       d = debugfs_rename(parent, br->br_dbgaufs, parent,
6642 +                                          name);
6643 +                       lockdep_on();
6644 +                       if (unlikely(!d))
6645 +                               pr_warn("failed renaming %pd/%s, ignored.\n",
6646 +                                       parent, name);
6647 +               }
6648 +       } else {
6649 +               lockdep_off();
6650 +               br->br_dbgaufs = debugfs_create_file(name, dbgaufs_mode, parent,
6651 +                                                    sbinfo, &dbgaufs_xino_fop);
6652 +               lockdep_on();
6653 +               if (unlikely(!br->br_dbgaufs))
6654 +                       pr_warn("failed creating %pd/%s, ignored.\n",
6655 +                               parent, name);
6656 +       }
6657 +}
6658 +
6659 +static void dbgaufs_br_add(struct super_block *sb, aufs_bindex_t bindex,
6660 +                          struct dentry *parent, struct au_sbinfo *sbinfo)
6661 +{
6662 +       struct au_branch *br;
6663 +       struct au_xino *xi;
6664 +       unsigned int u;
6665 +
6666 +       br = au_sbr(sb, bindex);
6667 +       xi = br->br_xino;
6668 +       for (u = 0; u < xi->xi_nfile; u++)
6669 +               dbgaufs_br_do_add(sb, bindex, u, parent, sbinfo);
6670 +}
6671 +
6672 +void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex, int topdown)
6673 +{
6674 +       struct au_sbinfo *sbinfo;
6675 +       struct dentry *parent;
6676 +       aufs_bindex_t bbot;
6677 +
6678 +       if (!au_opt_test(au_mntflags(sb), XINO))
6679 +               return;
6680 +
6681 +       sbinfo = au_sbi(sb);
6682 +       parent = sbinfo->si_dbgaufs;
6683 +       if (!parent)
6684 +               return;
6685 +
6686 +       bbot = au_sbbot(sb);
6687 +       if (topdown)
6688 +               for (; bindex <= bbot; bindex++)
6689 +                       dbgaufs_br_add(sb, bindex, parent, sbinfo);
6690 +       else
6691 +               for (; bbot >= bindex; bbot--)
6692 +                       dbgaufs_br_add(sb, bbot, parent, sbinfo);
6693 +}
6694 +
6695 +/* ---------------------------------------------------------------------- */
6696 +
6697 +#ifdef CONFIG_AUFS_EXPORT
6698 +static int dbgaufs_xigen_open(struct inode *inode, struct file *file)
6699 +{
6700 +       int err;
6701 +       struct au_sbinfo *sbinfo;
6702 +       struct super_block *sb;
6703 +
6704 +       sbinfo = inode->i_private;
6705 +       sb = sbinfo->si_sb;
6706 +       si_noflush_read_lock(sb);
6707 +       err = dbgaufs_xi_open(sbinfo->si_xigen, file, /*do_fcnt*/0, /*cnt*/0);
6708 +       si_read_unlock(sb);
6709 +       return err;
6710 +}
6711 +
6712 +static const struct file_operations dbgaufs_xigen_fop = {
6713 +       .owner          = THIS_MODULE,
6714 +       .open           = dbgaufs_xigen_open,
6715 +       .release        = dbgaufs_xi_release,
6716 +       .read           = dbgaufs_xi_read
6717 +};
6718 +
6719 +static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
6720 +{
6721 +       int err;
6722 +
6723 +       /*
6724 +        * This function is a dynamic '__init' function actually,
6725 +        * so the tiny check for si_rwsem is unnecessary.
6726 +        */
6727 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6728 +
6729 +       err = -EIO;
6730 +       sbinfo->si_dbgaufs_xigen = debugfs_create_file
6731 +               ("xigen", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6732 +                &dbgaufs_xigen_fop);
6733 +       if (sbinfo->si_dbgaufs_xigen)
6734 +               err = 0;
6735 +
6736 +       return err;
6737 +}
6738 +#else
6739 +static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
6740 +{
6741 +       return 0;
6742 +}
6743 +#endif /* CONFIG_AUFS_EXPORT */
6744 +
6745 +/* ---------------------------------------------------------------------- */
6746 +
6747 +void dbgaufs_si_fin(struct au_sbinfo *sbinfo)
6748 +{
6749 +       /*
6750 +        * This function is a dynamic '__fin' function actually,
6751 +        * so the tiny check for si_rwsem is unnecessary.
6752 +        */
6753 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6754 +
6755 +       debugfs_remove_recursive(sbinfo->si_dbgaufs);
6756 +       sbinfo->si_dbgaufs = NULL;
6757 +}
6758 +
6759 +int dbgaufs_si_init(struct au_sbinfo *sbinfo)
6760 +{
6761 +       int err;
6762 +       char name[SysaufsSiNameLen];
6763 +
6764 +       /*
6765 +        * This function is a dynamic '__init' function actually,
6766 +        * so the tiny check for si_rwsem is unnecessary.
6767 +        */
6768 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6769 +
6770 +       err = -ENOENT;
6771 +       if (!dbgaufs) {
6772 +               AuErr1("/debug/aufs is uninitialized\n");
6773 +               goto out;
6774 +       }
6775 +
6776 +       err = -EIO;
6777 +       sysaufs_name(sbinfo, name);
6778 +       sbinfo->si_dbgaufs = debugfs_create_dir(name, dbgaufs);
6779 +       if (unlikely(!sbinfo->si_dbgaufs))
6780 +               goto out;
6781 +
6782 +       /* regardless plink/noplink option */
6783 +       sbinfo->si_dbgaufs_plink = debugfs_create_file
6784 +               ("plink", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6785 +                &dbgaufs_plink_fop);
6786 +       if (unlikely(!sbinfo->si_dbgaufs_plink))
6787 +               goto out_dir;
6788 +
6789 +       /* regardless xino/noxino option */
6790 +       sbinfo->si_dbgaufs_xib = debugfs_create_file
6791 +               ("xib", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6792 +                &dbgaufs_xib_fop);
6793 +       if (unlikely(!sbinfo->si_dbgaufs_xib))
6794 +               goto out_dir;
6795 +
6796 +       err = dbgaufs_xigen_init(sbinfo);
6797 +       if (!err)
6798 +               goto out; /* success */
6799 +
6800 +out_dir:
6801 +       dbgaufs_si_fin(sbinfo);
6802 +out:
6803 +       if (unlikely(err))
6804 +               pr_err("debugfs/aufs failed\n");
6805 +       return err;
6806 +}
6807 +
6808 +/* ---------------------------------------------------------------------- */
6809 +
6810 +void dbgaufs_fin(void)
6811 +{
6812 +       debugfs_remove(dbgaufs);
6813 +}
6814 +
6815 +int __init dbgaufs_init(void)
6816 +{
6817 +       int err;
6818 +
6819 +       err = -EIO;
6820 +       dbgaufs = debugfs_create_dir(AUFS_NAME, NULL);
6821 +       if (dbgaufs)
6822 +               err = 0;
6823 +       return err;
6824 +}
6825 diff -urN /usr/share/empty/fs/aufs/dbgaufs.h linux/fs/aufs/dbgaufs.h
6826 --- /usr/share/empty/fs/aufs/dbgaufs.h  1970-01-01 01:00:00.000000000 +0100
6827 +++ linux/fs/aufs/dbgaufs.h     2022-03-21 14:49:05.719966344 +0100
6828 @@ -0,0 +1,53 @@
6829 +/* SPDX-License-Identifier: GPL-2.0 */
6830 +/*
6831 + * Copyright (C) 2005-2021 Junjiro R. Okajima
6832 + *
6833 + * This program, aufs is free software; you can redistribute it and/or modify
6834 + * it under the terms of the GNU General Public License as published by
6835 + * the Free Software Foundation; either version 2 of the License, or
6836 + * (at your option) any later version.
6837 + *
6838 + * This program is distributed in the hope that it will be useful,
6839 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6840 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6841 + * GNU General Public License for more details.
6842 + *
6843 + * You should have received a copy of the GNU General Public License
6844 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6845 + */
6846 +
6847 +/*
6848 + * debugfs interface
6849 + */
6850 +
6851 +#ifndef __DBGAUFS_H__
6852 +#define __DBGAUFS_H__
6853 +
6854 +#ifdef __KERNEL__
6855 +
6856 +struct super_block;
6857 +struct au_sbinfo;
6858 +struct au_branch;
6859 +
6860 +#ifdef CONFIG_DEBUG_FS
6861 +/* dbgaufs.c */
6862 +void dbgaufs_xino_del(struct au_branch *br);
6863 +void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
6864 +void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex, int topdown);
6865 +void dbgaufs_si_fin(struct au_sbinfo *sbinfo);
6866 +int dbgaufs_si_init(struct au_sbinfo *sbinfo);
6867 +void dbgaufs_fin(void);
6868 +int __init dbgaufs_init(void);
6869 +#else
6870 +AuStubVoid(dbgaufs_xino_del, struct au_branch *br)
6871 +AuStubVoid(dbgaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
6872 +AuStubVoid(dbgaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex,
6873 +          int topdown)
6874 +AuStubVoid(dbgaufs_si_fin, struct au_sbinfo *sbinfo)
6875 +AuStubInt0(dbgaufs_si_init, struct au_sbinfo *sbinfo)
6876 +AuStubVoid(dbgaufs_fin, void)
6877 +AuStubInt0(__init dbgaufs_init, void)
6878 +#endif /* CONFIG_DEBUG_FS */
6879 +
6880 +#endif /* __KERNEL__ */
6881 +#endif /* __DBGAUFS_H__ */
6882 diff -urN /usr/share/empty/fs/aufs/dcsub.c linux/fs/aufs/dcsub.c
6883 --- /usr/share/empty/fs/aufs/dcsub.c    1970-01-01 01:00:00.000000000 +0100
6884 +++ linux/fs/aufs/dcsub.c       2022-03-21 14:49:05.719966344 +0100
6885 @@ -0,0 +1,225 @@
6886 +// SPDX-License-Identifier: GPL-2.0
6887 +/*
6888 + * Copyright (C) 2005-2021 Junjiro R. Okajima
6889 + *
6890 + * This program, aufs is free software; you can redistribute it and/or modify
6891 + * it under the terms of the GNU General Public License as published by
6892 + * the Free Software Foundation; either version 2 of the License, or
6893 + * (at your option) any later version.
6894 + *
6895 + * This program is distributed in the hope that it will be useful,
6896 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6897 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6898 + * GNU General Public License for more details.
6899 + *
6900 + * You should have received a copy of the GNU General Public License
6901 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6902 + */
6903 +
6904 +/*
6905 + * sub-routines for dentry cache
6906 + */
6907 +
6908 +#include "aufs.h"
6909 +
6910 +static void au_dpage_free(struct au_dpage *dpage)
6911 +{
6912 +       int i;
6913 +       struct dentry **p;
6914 +
6915 +       p = dpage->dentries;
6916 +       for (i = 0; i < dpage->ndentry; i++)
6917 +               dput(*p++);
6918 +       free_page((unsigned long)dpage->dentries);
6919 +}
6920 +
6921 +int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp)
6922 +{
6923 +       int err;
6924 +       void *p;
6925 +
6926 +       err = -ENOMEM;
6927 +       dpages->dpages = kmalloc(sizeof(*dpages->dpages), gfp);
6928 +       if (unlikely(!dpages->dpages))
6929 +               goto out;
6930 +
6931 +       p = (void *)__get_free_page(gfp);
6932 +       if (unlikely(!p))
6933 +               goto out_dpages;
6934 +
6935 +       dpages->dpages[0].ndentry = 0;
6936 +       dpages->dpages[0].dentries = p;
6937 +       dpages->ndpage = 1;
6938 +       return 0; /* success */
6939 +
6940 +out_dpages:
6941 +       au_kfree_try_rcu(dpages->dpages);
6942 +out:
6943 +       return err;
6944 +}
6945 +
6946 +void au_dpages_free(struct au_dcsub_pages *dpages)
6947 +{
6948 +       int i;
6949 +       struct au_dpage *p;
6950 +
6951 +       p = dpages->dpages;
6952 +       for (i = 0; i < dpages->ndpage; i++)
6953 +               au_dpage_free(p++);
6954 +       au_kfree_try_rcu(dpages->dpages);
6955 +}
6956 +
6957 +static int au_dpages_append(struct au_dcsub_pages *dpages,
6958 +                           struct dentry *dentry, gfp_t gfp)
6959 +{
6960 +       int err, sz;
6961 +       struct au_dpage *dpage;
6962 +       void *p;
6963 +
6964 +       dpage = dpages->dpages + dpages->ndpage - 1;
6965 +       sz = PAGE_SIZE / sizeof(dentry);
6966 +       if (unlikely(dpage->ndentry >= sz)) {
6967 +               AuLabel(new dpage);
6968 +               err = -ENOMEM;
6969 +               sz = dpages->ndpage * sizeof(*dpages->dpages);
6970 +               p = au_kzrealloc(dpages->dpages, sz,
6971 +                                sz + sizeof(*dpages->dpages), gfp,
6972 +                                /*may_shrink*/0);
6973 +               if (unlikely(!p))
6974 +                       goto out;
6975 +
6976 +               dpages->dpages = p;
6977 +               dpage = dpages->dpages + dpages->ndpage;
6978 +               p = (void *)__get_free_page(gfp);
6979 +               if (unlikely(!p))
6980 +                       goto out;
6981 +
6982 +               dpage->ndentry = 0;
6983 +               dpage->dentries = p;
6984 +               dpages->ndpage++;
6985 +       }
6986 +
6987 +       AuDebugOn(au_dcount(dentry) <= 0);
6988 +       dpage->dentries[dpage->ndentry++] = dget_dlock(dentry);
6989 +       return 0; /* success */
6990 +
6991 +out:
6992 +       return err;
6993 +}
6994 +
6995 +/* todo: BAD approach */
6996 +/* copied from linux/fs/dcache.c */
6997 +enum d_walk_ret {
6998 +       D_WALK_CONTINUE,
6999 +       D_WALK_QUIT,
7000 +       D_WALK_NORETRY,
7001 +       D_WALK_SKIP,
7002 +};
7003 +
7004 +extern void d_walk(struct dentry *parent, void *data,
7005 +                  enum d_walk_ret (*enter)(void *, struct dentry *));
7006 +
7007 +struct ac_dpages_arg {
7008 +       int err;
7009 +       struct au_dcsub_pages *dpages;
7010 +       struct super_block *sb;
7011 +       au_dpages_test test;
7012 +       void *arg;
7013 +};
7014 +
7015 +static enum d_walk_ret au_call_dpages_append(void *_arg, struct dentry *dentry)
7016 +{
7017 +       enum d_walk_ret ret;
7018 +       struct ac_dpages_arg *arg = _arg;
7019 +
7020 +       ret = D_WALK_CONTINUE;
7021 +       if (dentry->d_sb == arg->sb
7022 +           && !IS_ROOT(dentry)
7023 +           && au_dcount(dentry) > 0
7024 +           && au_di(dentry)
7025 +           && (!arg->test || arg->test(dentry, arg->arg))) {
7026 +               arg->err = au_dpages_append(arg->dpages, dentry, GFP_ATOMIC);
7027 +               if (unlikely(arg->err))
7028 +                       ret = D_WALK_QUIT;
7029 +       }
7030 +
7031 +       return ret;
7032 +}
7033 +
7034 +int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
7035 +                  au_dpages_test test, void *arg)
7036 +{
7037 +       struct ac_dpages_arg args = {
7038 +               .err    = 0,
7039 +               .dpages = dpages,
7040 +               .sb     = root->d_sb,
7041 +               .test   = test,
7042 +               .arg    = arg
7043 +       };
7044 +
7045 +       d_walk(root, &args, au_call_dpages_append);
7046 +
7047 +       return args.err;
7048 +}
7049 +
7050 +int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
7051 +                      int do_include, au_dpages_test test, void *arg)
7052 +{
7053 +       int err;
7054 +
7055 +       err = 0;
7056 +       write_seqlock(&rename_lock);
7057 +       spin_lock(&dentry->d_lock);
7058 +       if (do_include
7059 +           && au_dcount(dentry) > 0
7060 +           && (!test || test(dentry, arg)))
7061 +               err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
7062 +       spin_unlock(&dentry->d_lock);
7063 +       if (unlikely(err))
7064 +               goto out;
7065 +
7066 +       /*
7067 +        * RCU for vfsmount is unnecessary since this is a traverse in a single
7068 +        * mount
7069 +        */
7070 +       while (!IS_ROOT(dentry)) {
7071 +               dentry = dentry->d_parent; /* rename_lock is locked */
7072 +               spin_lock(&dentry->d_lock);
7073 +               if (au_dcount(dentry) > 0
7074 +                   && (!test || test(dentry, arg)))
7075 +                       err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
7076 +               spin_unlock(&dentry->d_lock);
7077 +               if (unlikely(err))
7078 +                       break;
7079 +       }
7080 +
7081 +out:
7082 +       write_sequnlock(&rename_lock);
7083 +       return err;
7084 +}
7085 +
7086 +static inline int au_dcsub_dpages_aufs(struct dentry *dentry, void *arg)
7087 +{
7088 +       return au_di(dentry) && dentry->d_sb == arg;
7089 +}
7090 +
7091 +int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
7092 +                           struct dentry *dentry, int do_include)
7093 +{
7094 +       return au_dcsub_pages_rev(dpages, dentry, do_include,
7095 +                                 au_dcsub_dpages_aufs, dentry->d_sb);
7096 +}
7097 +
7098 +int au_test_subdir(struct dentry *d1, struct dentry *d2)
7099 +{
7100 +       struct path path[2] = {
7101 +               {
7102 +                       .dentry = d1
7103 +               },
7104 +               {
7105 +                       .dentry = d2
7106 +               }
7107 +       };
7108 +
7109 +       return path_is_under(path + 0, path + 1);
7110 +}
7111 diff -urN /usr/share/empty/fs/aufs/dcsub.h linux/fs/aufs/dcsub.h
7112 --- /usr/share/empty/fs/aufs/dcsub.h    1970-01-01 01:00:00.000000000 +0100
7113 +++ linux/fs/aufs/dcsub.h       2022-03-21 14:49:05.719966344 +0100
7114 @@ -0,0 +1,137 @@
7115 +/* SPDX-License-Identifier: GPL-2.0 */
7116 +/*
7117 + * Copyright (C) 2005-2021 Junjiro R. Okajima
7118 + *
7119 + * This program, aufs is free software; you can redistribute it and/or modify
7120 + * it under the terms of the GNU General Public License as published by
7121 + * the Free Software Foundation; either version 2 of the License, or
7122 + * (at your option) any later version.
7123 + *
7124 + * This program is distributed in the hope that it will be useful,
7125 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7126 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7127 + * GNU General Public License for more details.
7128 + *
7129 + * You should have received a copy of the GNU General Public License
7130 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
7131 + */
7132 +
7133 +/*
7134 + * sub-routines for dentry cache
7135 + */
7136 +
7137 +#ifndef __AUFS_DCSUB_H__
7138 +#define __AUFS_DCSUB_H__
7139 +
7140 +#ifdef __KERNEL__
7141 +
7142 +#include <linux/dcache.h>
7143 +#include <linux/fs.h>
7144 +
7145 +struct au_dpage {
7146 +       int ndentry;
7147 +       struct dentry **dentries;
7148 +};
7149 +
7150 +struct au_dcsub_pages {
7151 +       int ndpage;
7152 +       struct au_dpage *dpages;
7153 +};
7154 +
7155 +/* ---------------------------------------------------------------------- */
7156 +
7157 +/* dcsub.c */
7158 +int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp);
7159 +void au_dpages_free(struct au_dcsub_pages *dpages);
7160 +typedef int (*au_dpages_test)(struct dentry *dentry, void *arg);
7161 +int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
7162 +                  au_dpages_test test, void *arg);
7163 +int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
7164 +                      int do_include, au_dpages_test test, void *arg);
7165 +int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
7166 +                           struct dentry *dentry, int do_include);
7167 +int au_test_subdir(struct dentry *d1, struct dentry *d2);
7168 +
7169 +/* ---------------------------------------------------------------------- */
7170 +
7171 +/*
7172 + * todo: in linux-3.13, several similar (but faster) helpers are added to
7173 + * include/linux/dcache.h. Try them (in the future).
7174 + */
7175 +
7176 +static inline int au_d_hashed_positive(struct dentry *d)
7177 +{
7178 +       int err;
7179 +       struct inode *inode = d_inode(d);
7180 +
7181 +       err = 0;
7182 +       if (unlikely(d_unhashed(d)
7183 +                    || d_is_negative(d)
7184 +                    || !inode->i_nlink))
7185 +               err = -ENOENT;
7186 +       return err;
7187 +}
7188 +
7189 +static inline int au_d_linkable(struct dentry *d)
7190 +{
7191 +       int err;
7192 +       struct inode *inode = d_inode(d);
7193 +
7194 +       err = au_d_hashed_positive(d);
7195 +       if (err
7196 +           && d_is_positive(d)
7197 +           && (inode->i_state & I_LINKABLE))
7198 +               err = 0;
7199 +       return err;
7200 +}
7201 +
7202 +static inline int au_d_alive(struct dentry *d)
7203 +{
7204 +       int err;
7205 +       struct inode *inode;
7206 +
7207 +       err = 0;
7208 +       if (!IS_ROOT(d))
7209 +               err = au_d_hashed_positive(d);
7210 +       else {
7211 +               inode = d_inode(d);
7212 +               if (unlikely(d_unlinked(d)
7213 +                            || d_is_negative(d)
7214 +                            || !inode->i_nlink))
7215 +                       err = -ENOENT;
7216 +       }
7217 +       return err;
7218 +}
7219 +
7220 +static inline int au_alive_dir(struct dentry *d)
7221 +{
7222 +       int err;
7223 +
7224 +       err = au_d_alive(d);
7225 +       if (unlikely(err || IS_DEADDIR(d_inode(d))))
7226 +               err = -ENOENT;
7227 +       return err;
7228 +}
7229 +
7230 +static inline int au_qstreq(struct qstr *a, struct qstr *b)
7231 +{
7232 +       return a->len == b->len
7233 +               && !memcmp(a->name, b->name, a->len);
7234 +}
7235 +
7236 +/*
7237 + * by the commit
7238 + * 360f547 2015-01-25 dcache: let the dentry count go down to zero without
7239 + *                     taking d_lock
7240 + * the type of d_lockref.count became int, but the inlined function d_count()
7241 + * still returns unsigned int.
7242 + * I don't know why. Maybe it is for every d_count() users?
7243 + * Anyway au_dcount() lives on.
7244 + */
7245 +static inline int au_dcount(struct dentry *d)
7246 +{
7247 +       return (int)d_count(d);
7248 +}
7249 +
7250 +#endif /* __KERNEL__ */
7251 +#endif /* __AUFS_DCSUB_H__ */
7252 diff -urN /usr/share/empty/fs/aufs/debug.c linux/fs/aufs/debug.c
7253 --- /usr/share/empty/fs/aufs/debug.c    1970-01-01 01:00:00.000000000 +0100
7254 +++ linux/fs/aufs/debug.c       2022-03-21 14:49:05.719966344 +0100
7255 @@ -0,0 +1,444 @@
7256 +// SPDX-License-Identifier: GPL-2.0
7257 +/*
7258 + * Copyright (C) 2005-2021 Junjiro R. Okajima
7259 + *
7260 + * This program, aufs is free software; you can redistribute it and/or modify
7261 + * it under the terms of the GNU General Public License as published by
7262 + * the Free Software Foundation; either version 2 of the License, or
7263 + * (at your option) any later version.
7264 + *
7265 + * This program is distributed in the hope that it will be useful,
7266 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7267 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7268 + * GNU General Public License for more details.
7269 + *
7270 + * You should have received a copy of the GNU General Public License
7271 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
7272 + */
7273 +
7274 +/*
7275 + * debug print functions
7276 + */
7277 +
7278 +#include <linux/iversion.h>
7279 +#include "aufs.h"
7280 +
7281 +/* Returns 0, or -errno.  arg is in kp->arg. */
7282 +static int param_atomic_t_set(const char *val, const struct kernel_param *kp)
7283 +{
7284 +       int err, n;
7285 +
7286 +       err = kstrtoint(val, 0, &n);
7287 +       if (!err) {
7288 +               if (n > 0)
7289 +                       au_debug_on();
7290 +               else
7291 +                       au_debug_off();
7292 +       }
7293 +       return err;
7294 +}
7295 +
7296 +/* Returns length written or -errno.  Buffer is 4k (ie. be short!) */
7297 +static int param_atomic_t_get(char *buffer, const struct kernel_param *kp)
7298 +{
7299 +       atomic_t *a;
7300 +
7301 +       a = kp->arg;
7302 +       return sprintf(buffer, "%d", atomic_read(a));
7303 +}
7304 +
7305 +static struct kernel_param_ops param_ops_atomic_t = {
7306 +       .set = param_atomic_t_set,
7307 +       .get = param_atomic_t_get
7308 +       /* void (*free)(void *arg) */
7309 +};
7310 +
7311 +atomic_t aufs_debug = ATOMIC_INIT(0);
7312 +MODULE_PARM_DESC(debug, "debug print");
7313 +module_param_named(debug, aufs_debug, atomic_t, 0664);
7314 +
7315 +DEFINE_MUTEX(au_dbg_mtx);      /* just to serialize the dbg msgs */
7316 +char *au_plevel = KERN_DEBUG;
7317 +#define dpri(fmt, ...) do {                                    \
7318 +       if ((au_plevel                                          \
7319 +            && strcmp(au_plevel, KERN_DEBUG))                  \
7320 +           || au_debug_test())                                 \
7321 +               printk("%s" fmt, au_plevel, ##__VA_ARGS__);     \
7322 +} while (0)
7323 +
7324 +/* ---------------------------------------------------------------------- */
7325 +
7326 +void au_dpri_whlist(struct au_nhash *whlist)
7327 +{
7328 +       unsigned long ul, n;
7329 +       struct hlist_head *head;
7330 +       struct au_vdir_wh *pos;
7331 +
7332 +       n = whlist->nh_num;
7333 +       head = whlist->nh_head;
7334 +       for (ul = 0; ul < n; ul++) {
7335 +               hlist_for_each_entry(pos, head, wh_hash)
7336 +                       dpri("b%d, %.*s, %d\n",
7337 +                            pos->wh_bindex,
7338 +                            pos->wh_str.len, pos->wh_str.name,
7339 +                            pos->wh_str.len);
7340 +               head++;
7341 +       }
7342 +}
7343 +
7344 +void au_dpri_vdir(struct au_vdir *vdir)
7345 +{
7346 +       unsigned long ul;
7347 +       union au_vdir_deblk_p p;
7348 +       unsigned char *o;
7349 +
7350 +       if (!vdir || IS_ERR(vdir)) {
7351 +               dpri("err %ld\n", PTR_ERR(vdir));
7352 +               return;
7353 +       }
7354 +
7355 +       dpri("deblk %u, nblk %lu, deblk %p, last{%lu, %p}, ver %llu\n",
7356 +            vdir->vd_deblk_sz, vdir->vd_nblk, vdir->vd_deblk,
7357 +            vdir->vd_last.ul, vdir->vd_last.p.deblk, vdir->vd_version);
7358 +       for (ul = 0; ul < vdir->vd_nblk; ul++) {
7359 +               p.deblk = vdir->vd_deblk[ul];
7360 +               o = p.deblk;
7361 +               dpri("[%lu]: %p\n", ul, o);
7362 +       }
7363 +}
7364 +
7365 +static int do_pri_inode(aufs_bindex_t bindex, struct inode *inode, int hn,
7366 +                       struct dentry *wh)
7367 +{
7368 +       char *n = NULL;
7369 +       int l = 0;
7370 +
7371 +       if (!inode || IS_ERR(inode)) {
7372 +               dpri("i%d: err %ld\n", bindex, PTR_ERR(inode));
7373 +               return -1;
7374 +       }
7375 +
7376 +       /* the type of i_blocks depends upon CONFIG_LBDAF */
7377 +       BUILD_BUG_ON(sizeof(inode->i_blocks) != sizeof(unsigned long)
7378 +                    && sizeof(inode->i_blocks) != sizeof(u64));
7379 +       if (wh) {
7380 +               n = (void *)wh->d_name.name;
7381 +               l = wh->d_name.len;
7382 +       }
7383 +
7384 +       dpri("i%d: %p, i%lu, %s, cnt %d, nl %u, 0%o, sz %llu, blk %llu,"
7385 +            " hn %d, ct %lld, np %lu, st 0x%lx, f 0x%x, v %llu, g %x%s%.*s\n",
7386 +            bindex, inode,
7387 +            inode->i_ino, inode->i_sb ? au_sbtype(inode->i_sb) : "??",
7388 +            atomic_read(&inode->i_count), inode->i_nlink, inode->i_mode,
7389 +            i_size_read(inode), (unsigned long long)inode->i_blocks,
7390 +            hn, (long long)timespec64_to_ns(&inode->i_ctime) & 0x0ffff,
7391 +            inode->i_mapping ? inode->i_mapping->nrpages : 0,
7392 +            inode->i_state, inode->i_flags, inode_peek_iversion(inode),
7393 +            inode->i_generation,
7394 +            l ? ", wh " : "", l, n);
7395 +       return 0;
7396 +}
7397 +
7398 +void au_dpri_inode(struct inode *inode)
7399 +{
7400 +       struct au_iinfo *iinfo;
7401 +       struct au_hinode *hi;
7402 +       aufs_bindex_t bindex;
7403 +       int err, hn;
7404 +
7405 +       err = do_pri_inode(-1, inode, -1, NULL);
7406 +       if (err || !au_test_aufs(inode->i_sb) || au_is_bad_inode(inode))
7407 +               return;
7408 +
7409 +       iinfo = au_ii(inode);
7410 +       dpri("i-1: btop %d, bbot %d, gen %d\n",
7411 +            iinfo->ii_btop, iinfo->ii_bbot, au_iigen(inode, NULL));
7412 +       if (iinfo->ii_btop < 0)
7413 +               return;
7414 +       hn = 0;
7415 +       for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot; bindex++) {
7416 +               hi = au_hinode(iinfo, bindex);
7417 +               hn = !!au_hn(hi);
7418 +               do_pri_inode(bindex, hi->hi_inode, hn, hi->hi_whdentry);
7419 +       }
7420 +}
7421 +
7422 +void au_dpri_dalias(struct inode *inode)
7423 +{
7424 +       struct dentry *d;
7425 +
7426 +       spin_lock(&inode->i_lock);
7427 +       hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias)
7428 +               au_dpri_dentry(d);
7429 +       spin_unlock(&inode->i_lock);
7430 +}
7431 +
7432 +static int do_pri_dentry(aufs_bindex_t bindex, struct dentry *dentry)
7433 +{
7434 +       struct dentry *wh = NULL;
7435 +       int hn;
7436 +       struct inode *inode;
7437 +       struct au_iinfo *iinfo;
7438 +       struct au_hinode *hi;
7439 +
7440 +       if (!dentry || IS_ERR(dentry)) {
7441 +               dpri("d%d: err %ld\n", bindex, PTR_ERR(dentry));
7442 +               return -1;
7443 +       }
7444 +       /* do not call dget_parent() here */
7445 +       /* note: access d_xxx without d_lock */
7446 +       dpri("d%d: %p, %pd2?, %s, cnt %d, flags 0x%x, %shashed\n",
7447 +            bindex, dentry, dentry,
7448 +            dentry->d_sb ? au_sbtype(dentry->d_sb) : "??",
7449 +            au_dcount(dentry), dentry->d_flags,
7450 +            d_unhashed(dentry) ? "un" : "");
7451 +       hn = -1;
7452 +       inode = NULL;
7453 +       if (d_is_positive(dentry))
7454 +               inode = d_inode(dentry);
7455 +       if (inode
7456 +           && au_test_aufs(dentry->d_sb)
7457 +           && bindex >= 0
7458 +           && !au_is_bad_inode(inode)) {
7459 +               iinfo = au_ii(inode);
7460 +               hi = au_hinode(iinfo, bindex);
7461 +               hn = !!au_hn(hi);
7462 +               wh = hi->hi_whdentry;
7463 +       }
7464 +       do_pri_inode(bindex, inode, hn, wh);
7465 +       return 0;
7466 +}
7467 +
7468 +void au_dpri_dentry(struct dentry *dentry)
7469 +{
7470 +       struct au_dinfo *dinfo;
7471 +       aufs_bindex_t bindex;
7472 +       int err;
7473 +
7474 +       err = do_pri_dentry(-1, dentry);
7475 +       if (err || !au_test_aufs(dentry->d_sb))
7476 +               return;
7477 +
7478 +       dinfo = au_di(dentry);
7479 +       if (!dinfo)
7480 +               return;
7481 +       dpri("d-1: btop %d, bbot %d, bwh %d, bdiropq %d, gen %d, tmp %d\n",
7482 +            dinfo->di_btop, dinfo->di_bbot,
7483 +            dinfo->di_bwh, dinfo->di_bdiropq, au_digen(dentry),
7484 +            dinfo->di_tmpfile);
7485 +       if (dinfo->di_btop < 0)
7486 +               return;
7487 +       for (bindex = dinfo->di_btop; bindex <= dinfo->di_bbot; bindex++)
7488 +               do_pri_dentry(bindex, au_hdentry(dinfo, bindex)->hd_dentry);
7489 +}
7490 +
7491 +static int do_pri_file(aufs_bindex_t bindex, struct file *file)
7492 +{
7493 +       char a[32];
7494 +
7495 +       if (!file || IS_ERR(file)) {
7496 +               dpri("f%d: err %ld\n", bindex, PTR_ERR(file));
7497 +               return -1;
7498 +       }
7499 +       a[0] = 0;
7500 +       if (bindex < 0
7501 +           && !IS_ERR_OR_NULL(file->f_path.dentry)
7502 +           && au_test_aufs(file->f_path.dentry->d_sb)
7503 +           && au_fi(file))
7504 +               snprintf(a, sizeof(a), ", gen %d, mmapped %d",
7505 +                        au_figen(file), atomic_read(&au_fi(file)->fi_mmapped));
7506 +       dpri("f%d: mode 0x%x, flags 0%o, cnt %ld, v %llu, pos %llu%s\n",
7507 +            bindex, file->f_mode, file->f_flags, (long)file_count(file),
7508 +            file->f_version, file->f_pos, a);
7509 +       if (!IS_ERR_OR_NULL(file->f_path.dentry))
7510 +               do_pri_dentry(bindex, file->f_path.dentry);
7511 +       return 0;
7512 +}
7513 +
7514 +void au_dpri_file(struct file *file)
7515 +{
7516 +       struct au_finfo *finfo;
7517 +       struct au_fidir *fidir;
7518 +       struct au_hfile *hfile;
7519 +       aufs_bindex_t bindex;
7520 +       int err;
7521 +
7522 +       err = do_pri_file(-1, file);
7523 +       if (err
7524 +           || IS_ERR_OR_NULL(file->f_path.dentry)
7525 +           || !au_test_aufs(file->f_path.dentry->d_sb))
7526 +               return;
7527 +
7528 +       finfo = au_fi(file);
7529 +       if (!finfo)
7530 +               return;
7531 +       if (finfo->fi_btop < 0)
7532 +               return;
7533 +       fidir = finfo->fi_hdir;
7534 +       if (!fidir)
7535 +               do_pri_file(finfo->fi_btop, finfo->fi_htop.hf_file);
7536 +       else
7537 +               for (bindex = finfo->fi_btop;
7538 +                    bindex >= 0 && bindex <= fidir->fd_bbot;
7539 +                    bindex++) {
7540 +                       hfile = fidir->fd_hfile + bindex;
7541 +                       do_pri_file(bindex, hfile ? hfile->hf_file : NULL);
7542 +               }
7543 +}
7544 +
7545 +static int do_pri_br(aufs_bindex_t bindex, struct au_branch *br)
7546 +{
7547 +       struct vfsmount *mnt;
7548 +       struct super_block *sb;
7549 +
7550 +       if (!br || IS_ERR(br))
7551 +               goto out;
7552 +       mnt = au_br_mnt(br);
7553 +       if (!mnt || IS_ERR(mnt))
7554 +               goto out;
7555 +       sb = mnt->mnt_sb;
7556 +       if (!sb || IS_ERR(sb))
7557 +               goto out;
7558 +
7559 +       dpri("s%d: {perm 0x%x, id %d, wbr %p}, "
7560 +            "%s, dev 0x%02x%02x, flags 0x%lx, cnt %d, active %d, "
7561 +            "xino %d\n",
7562 +            bindex, br->br_perm, br->br_id, br->br_wbr,
7563 +            au_sbtype(sb), MAJOR(sb->s_dev), MINOR(sb->s_dev),
7564 +            sb->s_flags, sb->s_count,
7565 +            atomic_read(&sb->s_active),
7566 +            !!au_xino_file(br->br_xino, /*idx*/-1));
7567 +       return 0;
7568 +
7569 +out:
7570 +       dpri("s%d: err %ld\n", bindex, PTR_ERR(br));
7571 +       return -1;
7572 +}
7573 +
7574 +void au_dpri_sb(struct super_block *sb)
7575 +{
7576 +       struct au_sbinfo *sbinfo;
7577 +       aufs_bindex_t bindex;
7578 +       int err;
7579 +       /* to reduce stack size */
7580 +       struct {
7581 +               struct vfsmount mnt;
7582 +               struct au_branch fake;
7583 +       } *a;
7584 +
7585 +       /* this function can be called from magic sysrq */
7586 +       a = kzalloc(sizeof(*a), GFP_ATOMIC);
7587 +       if (unlikely(!a)) {
7588 +               dpri("no memory\n");
7589 +               return;
7590 +       }
7591 +
7592 +       a->mnt.mnt_sb = sb;
7593 +       a->fake.br_path.mnt = &a->mnt;
7594 +       err = do_pri_br(-1, &a->fake);
7595 +       au_kfree_rcu(a);
7596 +       dpri("dev 0x%x\n", sb->s_dev);
7597 +       if (err || !au_test_aufs(sb))
7598 +               return;
7599 +
7600 +       sbinfo = au_sbi(sb);
7601 +       if (!sbinfo)
7602 +               return;
7603 +       dpri("nw %d, gen %u, kobj %d\n",
7604 +            atomic_read(&sbinfo->si_nowait.nw_len), sbinfo->si_generation,
7605 +            kref_read(&sbinfo->si_kobj.kref));
7606 +       for (bindex = 0; bindex <= sbinfo->si_bbot; bindex++)
7607 +               do_pri_br(bindex, sbinfo->si_branch[0 + bindex]);
7608 +}
7609 +
7610 +/* ---------------------------------------------------------------------- */
7611 +
7612 +void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line)
7613 +{
7614 +       struct inode *h_inode, *inode = d_inode(dentry);
7615 +       struct dentry *h_dentry;
7616 +       aufs_bindex_t bindex, bbot, bi;
7617 +
7618 +       if (!inode /* || au_di(dentry)->di_lsc == AuLsc_DI_TMP */)
7619 +               return;
7620 +
7621 +       bbot = au_dbbot(dentry);
7622 +       bi = au_ibbot(inode);
7623 +       if (bi < bbot)
7624 +               bbot = bi;
7625 +       bindex = au_dbtop(dentry);
7626 +       bi = au_ibtop(inode);
7627 +       if (bi > bindex)
7628 +               bindex = bi;
7629 +
7630 +       for (; bindex <= bbot; bindex++) {
7631 +               h_dentry = au_h_dptr(dentry, bindex);
7632 +               if (!h_dentry)
7633 +                       continue;
7634 +               h_inode = au_h_iptr(inode, bindex);
7635 +               if (unlikely(h_inode != d_inode(h_dentry))) {
7636 +                       au_debug_on();
7637 +                       AuDbg("b%d, %s:%d\n", bindex, func, line);
7638 +                       AuDbgDentry(dentry);
7639 +                       AuDbgInode(inode);
7640 +                       au_debug_off();
7641 +                       if (au_test_fuse(h_inode->i_sb))
7642 +                               WARN_ON_ONCE(1);
7643 +                       else
7644 +                               BUG();
7645 +               }
7646 +       }
7647 +}
7648 +
7649 +void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen)
7650 +{
7651 +       int err, i, j;
7652 +       struct au_dcsub_pages dpages;
7653 +       struct au_dpage *dpage;
7654 +       struct dentry **dentries;
7655 +
7656 +       err = au_dpages_init(&dpages, GFP_NOFS);
7657 +       AuDebugOn(err);
7658 +       err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/1);
7659 +       AuDebugOn(err);
7660 +       for (i = dpages.ndpage - 1; !err && i >= 0; i--) {
7661 +               dpage = dpages.dpages + i;
7662 +               dentries = dpage->dentries;
7663 +               for (j = dpage->ndentry - 1; !err && j >= 0; j--)
7664 +                       AuDebugOn(au_digen_test(dentries[j], sigen));
7665 +       }
7666 +       au_dpages_free(&dpages);
7667 +}
7668 +
7669 +void au_dbg_verify_kthread(void)
7670 +{
7671 +       if (au_wkq_test()) {
7672 +               au_dbg_blocked();
7673 +               /*
7674 +                * It may be recursive, but udba=notify between two aufs mounts,
7675 +                * where a single ro branch is shared, is not a problem.
7676 +                */
7677 +               /* WARN_ON(1); */
7678 +       }
7679 +}
7680 +
7681 +/* ---------------------------------------------------------------------- */
7682 +
7683 +int __init au_debug_init(void)
7684 +{
7685 +       aufs_bindex_t bindex;
7686 +       struct au_vdir_destr destr;
7687 +
7688 +       bindex = -1;
7689 +       AuDebugOn(bindex >= 0);
7690 +
7691 +       destr.len = -1;
7692 +       AuDebugOn(destr.len < NAME_MAX);
7693 +
7694 +#ifdef CONFIG_4KSTACKS
7695 +       pr_warn("CONFIG_4KSTACKS is defined.\n");
7696 +#endif
7697 +
7698 +       return 0;
7699 +}
7700 diff -urN /usr/share/empty/fs/aufs/debug.h linux/fs/aufs/debug.h
7701 --- /usr/share/empty/fs/aufs/debug.h    1970-01-01 01:00:00.000000000 +0100
7702 +++ linux/fs/aufs/debug.h       2022-03-21 14:49:05.719966344 +0100
7703 @@ -0,0 +1,226 @@
7704 +/* SPDX-License-Identifier: GPL-2.0 */
7705 +/*
7706 + * Copyright (C) 2005-2021 Junjiro R. Okajima
7707 + *
7708 + * This program, aufs is free software; you can redistribute it and/or modify
7709 + * it under the terms of the GNU General Public License as published by
7710 + * the Free Software Foundation; either version 2 of the License, or
7711 + * (at your option) any later version.
7712 + *
7713 + * This program is distributed in the hope that it will be useful,
7714 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7715 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7716 + * GNU General Public License for more details.
7717 + *
7718 + * You should have received a copy of the GNU General Public License
7719 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
7720 + */
7721 +
7722 +/*
7723 + * debug print functions
7724 + */
7725 +
7726 +#ifndef __AUFS_DEBUG_H__
7727 +#define __AUFS_DEBUG_H__
7728 +
7729 +#ifdef __KERNEL__
7730 +
7731 +#include <linux/atomic.h>
7732 +#include <linux/module.h>
7733 +#include <linux/kallsyms.h>
7734 +#include <linux/sysrq.h>
7735 +
7736 +#ifdef CONFIG_AUFS_DEBUG
7737 +#define AuDebugOn(a)           BUG_ON(a)
7738 +
7739 +/* module parameter */
7740 +extern atomic_t aufs_debug;
7741 +static inline void au_debug_on(void)
7742 +{
7743 +       atomic_inc(&aufs_debug);
7744 +}
7745 +static inline void au_debug_off(void)
7746 +{
7747 +       atomic_dec_if_positive(&aufs_debug);
7748 +}
7749 +
7750 +static inline int au_debug_test(void)
7751 +{
7752 +       return atomic_read(&aufs_debug) > 0;
7753 +}
7754 +#else
7755 +#define AuDebugOn(a)           do {} while (0)
7756 +AuStubVoid(au_debug_on, void)
7757 +AuStubVoid(au_debug_off, void)
7758 +AuStubInt0(au_debug_test, void)
7759 +#endif /* CONFIG_AUFS_DEBUG */
7760 +
7761 +#define param_check_atomic_t(name, p) __param_check(name, p, atomic_t)
7762 +
7763 +/* ---------------------------------------------------------------------- */
7764 +
7765 +/* debug print */
7766 +
7767 +#define AuDbg(fmt, ...) do { \
7768 +       if (au_debug_test()) \
7769 +               pr_debug("DEBUG: " fmt, ##__VA_ARGS__); \
7770 +} while (0)
7771 +#define AuLabel(l)             AuDbg(#l "\n")
7772 +#define AuIOErr(fmt, ...)      pr_err("I/O Error, " fmt, ##__VA_ARGS__)
7773 +#define AuWarn1(fmt, ...) do { \
7774 +       static unsigned char _c; \
7775 +       if (!_c++) \
7776 +               pr_warn(fmt, ##__VA_ARGS__); \
7777 +} while (0)
7778 +
7779 +#define AuErr1(fmt, ...) do { \
7780 +       static unsigned char _c; \
7781 +       if (!_c++) \
7782 +               pr_err(fmt, ##__VA_ARGS__); \
7783 +} while (0)
7784 +
7785 +#define AuIOErr1(fmt, ...) do { \
7786 +       static unsigned char _c; \
7787 +       if (!_c++) \
7788 +               AuIOErr(fmt, ##__VA_ARGS__); \
7789 +} while (0)
7790 +
7791 +#define AuUnsupportMsg "This operation is not supported." \
7792 +                       " Please report this application to aufs-users ML."
7793 +#define AuUnsupport(fmt, ...) do { \
7794 +       pr_err(AuUnsupportMsg "\n" fmt, ##__VA_ARGS__); \
7795 +       dump_stack(); \
7796 +} while (0)
7797 +
7798 +#define AuTraceErr(e) do { \
7799 +       if (unlikely((e) < 0)) \
7800 +               AuDbg("err %d\n", (int)(e)); \
7801 +} while (0)
7802 +
7803 +#define AuTraceErrPtr(p) do { \
7804 +       if (IS_ERR(p)) \
7805 +               AuDbg("err %ld\n", PTR_ERR(p)); \
7806 +} while (0)
7807 +
7808 +/* dirty macros for debug print, use with "%.*s" and caution */
7809 +#define AuLNPair(qstr)         (qstr)->len, (qstr)->name
7810 +
7811 +/* ---------------------------------------------------------------------- */
7812 +
7813 +struct dentry;
7814 +#ifdef CONFIG_AUFS_DEBUG
7815 +extern struct mutex au_dbg_mtx;
7816 +extern char *au_plevel;
7817 +struct au_nhash;
7818 +void au_dpri_whlist(struct au_nhash *whlist);
7819 +struct au_vdir;
7820 +void au_dpri_vdir(struct au_vdir *vdir);
7821 +struct inode;
7822 +void au_dpri_inode(struct inode *inode);
7823 +void au_dpri_dalias(struct inode *inode);
7824 +void au_dpri_dentry(struct dentry *dentry);
7825 +struct file;
7826 +void au_dpri_file(struct file *filp);
7827 +struct super_block;
7828 +void au_dpri_sb(struct super_block *sb);
7829 +
7830 +#define au_dbg_verify_dinode(d) __au_dbg_verify_dinode(d, __func__, __LINE__)
7831 +void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line);
7832 +void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen);
7833 +void au_dbg_verify_kthread(void);
7834 +
7835 +int __init au_debug_init(void);
7836 +
7837 +#define AuDbgWhlist(w) do { \
7838 +       mutex_lock(&au_dbg_mtx); \
7839 +       AuDbg(#w "\n"); \
7840 +       au_dpri_whlist(w); \
7841 +       mutex_unlock(&au_dbg_mtx); \
7842 +} while (0)
7843 +
7844 +#define AuDbgVdir(v) do { \
7845 +       mutex_lock(&au_dbg_mtx); \
7846 +       AuDbg(#v "\n"); \
7847 +       au_dpri_vdir(v); \
7848 +       mutex_unlock(&au_dbg_mtx); \
7849 +} while (0)
7850 +
7851 +#define AuDbgInode(i) do { \
7852 +       mutex_lock(&au_dbg_mtx); \
7853 +       AuDbg(#i "\n"); \
7854 +       au_dpri_inode(i); \
7855 +       mutex_unlock(&au_dbg_mtx); \
7856 +} while (0)
7857 +
7858 +#define AuDbgDAlias(i) do { \
7859 +       mutex_lock(&au_dbg_mtx); \
7860 +       AuDbg(#i "\n"); \
7861 +       au_dpri_dalias(i); \
7862 +       mutex_unlock(&au_dbg_mtx); \
7863 +} while (0)
7864 +
7865 +#define AuDbgDentry(d) do { \
7866 +       mutex_lock(&au_dbg_mtx); \
7867 +       AuDbg(#d "\n"); \
7868 +       au_dpri_dentry(d); \
7869 +       mutex_unlock(&au_dbg_mtx); \
7870 +} while (0)
7871 +
7872 +#define AuDbgFile(f) do { \
7873 +       mutex_lock(&au_dbg_mtx); \
7874 +       AuDbg(#f "\n"); \
7875 +       au_dpri_file(f); \
7876 +       mutex_unlock(&au_dbg_mtx); \
7877 +} while (0)
7878 +
7879 +#define AuDbgSb(sb) do { \
7880 +       mutex_lock(&au_dbg_mtx); \
7881 +       AuDbg(#sb "\n"); \
7882 +       au_dpri_sb(sb); \
7883 +       mutex_unlock(&au_dbg_mtx); \
7884 +} while (0)
7885 +
7886 +#define AuDbgSym(addr) do {                            \
7887 +       char sym[KSYM_SYMBOL_LEN];                      \
7888 +       sprint_symbol(sym, (unsigned long)addr);        \
7889 +       AuDbg("%s\n", sym);                             \
7890 +} while (0)
7891 +#else
7892 +AuStubVoid(au_dbg_verify_dinode, struct dentry *dentry)
7893 +AuStubVoid(au_dbg_verify_gen, struct dentry *parent, unsigned int sigen)
7894 +AuStubVoid(au_dbg_verify_kthread, void)
7895 +AuStubInt0(__init au_debug_init, void)
7896 +
7897 +#define AuDbgWhlist(w)         do {} while (0)
7898 +#define AuDbgVdir(v)           do {} while (0)
7899 +#define AuDbgInode(i)          do {} while (0)
7900 +#define AuDbgDAlias(i)         do {} while (0)
7901 +#define AuDbgDentry(d)         do {} while (0)
7902 +#define AuDbgFile(f)           do {} while (0)
7903 +#define AuDbgSb(sb)            do {} while (0)
7904 +#define AuDbgSym(addr)         do {} while (0)
7905 +#endif /* CONFIG_AUFS_DEBUG */
7906 +
7907 +/* ---------------------------------------------------------------------- */
7908 +
7909 +#ifdef CONFIG_AUFS_MAGIC_SYSRQ
7910 +int __init au_sysrq_init(void);
7911 +void au_sysrq_fin(void);
7912 +
7913 +#ifdef CONFIG_HW_CONSOLE
7914 +#define au_dbg_blocked() do { \
7915 +       WARN_ON(1); \
7916 +       handle_sysrq('w'); \
7917 +} while (0)
7918 +#else
7919 +AuStubVoid(au_dbg_blocked, void)
7920 +#endif
7921 +
7922 +#else
7923 +AuStubInt0(__init au_sysrq_init, void)
7924 +AuStubVoid(au_sysrq_fin, void)
7925 +AuStubVoid(au_dbg_blocked, void)
7926 +#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
7927 +
7928 +#endif /* __KERNEL__ */
7929 +#endif /* __AUFS_DEBUG_H__ */
7930 diff -urN /usr/share/empty/fs/aufs/dentry.c linux/fs/aufs/dentry.c
7931 --- /usr/share/empty/fs/aufs/dentry.c   1970-01-01 01:00:00.000000000 +0100
7932 +++ linux/fs/aufs/dentry.c      2022-03-21 14:49:05.719966344 +0100
7933 @@ -0,0 +1,1168 @@
7934 +// SPDX-License-Identifier: GPL-2.0
7935 +/*
7936 + * Copyright (C) 2005-2021 Junjiro R. Okajima
7937 + *
7938 + * This program, aufs is free software; you can redistribute it and/or modify
7939 + * it under the terms of the GNU General Public License as published by
7940 + * the Free Software Foundation; either version 2 of the License, or
7941 + * (at your option) any later version.
7942 + *
7943 + * This program is distributed in the hope that it will be useful,
7944 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7945 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7946 + * GNU General Public License for more details.
7947 + *
7948 + * You should have received a copy of the GNU General Public License
7949 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
7950 + */
7951 +
7952 +/*
7953 + * lookup and dentry operations
7954 + */
7955 +
7956 +#include <linux/iversion.h>
7957 +#include "aufs.h"
7958 +
7959 +/*
7960 + * returns positive/negative dentry, NULL or an error.
7961 + * NULL means whiteout-ed or not-found.
7962 + */
7963 +static struct dentry*
7964 +au_do_lookup(struct dentry *h_parent, struct dentry *dentry,
7965 +            aufs_bindex_t bindex, struct au_do_lookup_args *args)
7966 +{
7967 +       struct dentry *h_dentry;
7968 +       struct inode *h_inode;
7969 +       struct au_branch *br;
7970 +       struct user_namespace *h_userns;
7971 +       struct path h_path;
7972 +       int wh_found, opq;
7973 +       unsigned char wh_able;
7974 +       const unsigned char allow_neg = !!au_ftest_lkup(args->flags, ALLOW_NEG);
7975 +       const unsigned char ignore_perm = !!au_ftest_lkup(args->flags,
7976 +                                                         IGNORE_PERM);
7977 +
7978 +       wh_found = 0;
7979 +       br = au_sbr(dentry->d_sb, bindex);
7980 +       h_path.dentry = h_parent;
7981 +       h_path.mnt = au_br_mnt(br);
7982 +       h_userns = au_br_userns(br);
7983 +       wh_able = !!au_br_whable(br->br_perm);
7984 +       if (wh_able)
7985 +               wh_found = au_wh_test(h_userns, &h_path, &args->whname,
7986 +                                     ignore_perm);
7987 +       h_dentry = ERR_PTR(wh_found);
7988 +       if (!wh_found)
7989 +               goto real_lookup;
7990 +       if (unlikely(wh_found < 0))
7991 +               goto out;
7992 +
7993 +       /* We found a whiteout */
7994 +       /* au_set_dbbot(dentry, bindex); */
7995 +       au_set_dbwh(dentry, bindex);
7996 +       if (!allow_neg)
7997 +               return NULL; /* success */
7998 +
7999 +real_lookup:
8000 +       if (!ignore_perm)
8001 +               h_dentry = vfsub_lkup_one(args->name, &h_path);
8002 +       else
8003 +               h_dentry = au_sio_lkup_one(h_userns, args->name, &h_path);
8004 +       if (IS_ERR(h_dentry)) {
8005 +               if (PTR_ERR(h_dentry) == -ENAMETOOLONG
8006 +                   && !allow_neg)
8007 +                       h_dentry = NULL;
8008 +               goto out;
8009 +       }
8010 +
8011 +       h_inode = d_inode(h_dentry);
8012 +       if (d_is_negative(h_dentry)) {
8013 +               if (!allow_neg)
8014 +                       goto out_neg;
8015 +       } else if (wh_found
8016 +                  || (args->type && args->type != (h_inode->i_mode & S_IFMT)))
8017 +               goto out_neg;
8018 +       else if (au_ftest_lkup(args->flags, DIRREN)
8019 +                /* && h_inode */
8020 +                && !au_dr_lkup_h_ino(args, bindex, h_inode->i_ino)) {
8021 +               AuDbg("b%d %pd ignored hi%llu\n", bindex, h_dentry,
8022 +                     (unsigned long long)h_inode->i_ino);
8023 +               goto out_neg;
8024 +       }
8025 +
8026 +       if (au_dbbot(dentry) <= bindex)
8027 +               au_set_dbbot(dentry, bindex);
8028 +       if (au_dbtop(dentry) < 0 || bindex < au_dbtop(dentry))
8029 +               au_set_dbtop(dentry, bindex);
8030 +       au_set_h_dptr(dentry, bindex, h_dentry);
8031 +
8032 +       if (!d_is_dir(h_dentry)
8033 +           || !wh_able
8034 +           || (d_really_is_positive(dentry) && !d_is_dir(dentry)))
8035 +               goto out; /* success */
8036 +
8037 +       h_path.dentry = h_dentry;
8038 +       inode_lock_shared_nested(h_inode, AuLsc_I_CHILD);
8039 +       opq = au_diropq_test(h_userns, &h_path);
8040 +       inode_unlock_shared(h_inode);
8041 +       if (opq > 0)
8042 +               au_set_dbdiropq(dentry, bindex);
8043 +       else if (unlikely(opq < 0)) {
8044 +               au_set_h_dptr(dentry, bindex, NULL);
8045 +               h_dentry = ERR_PTR(opq);
8046 +       }
8047 +       goto out;
8048 +
8049 +out_neg:
8050 +       dput(h_dentry);
8051 +       h_dentry = NULL;
8052 +out:
8053 +       return h_dentry;
8054 +}
8055 +
8056 +static int au_test_shwh(struct super_block *sb, const struct qstr *name)
8057 +{
8058 +       if (unlikely(!au_opt_test(au_mntflags(sb), SHWH)
8059 +                    && !strncmp(name->name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)))
8060 +               return -EPERM;
8061 +       return 0;
8062 +}
8063 +
8064 +/*
8065 + * returns the number of lower positive dentries,
8066 + * otherwise an error.
8067 + * can be called at unlinking with @type is zero.
8068 + */
8069 +int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t btop,
8070 +                  unsigned int flags)
8071 +{
8072 +       int npositive, err;
8073 +       aufs_bindex_t bindex, btail, bdiropq;
8074 +       unsigned char isdir, dirperm1, dirren;
8075 +       struct au_do_lookup_args args = {
8076 +               .flags          = flags,
8077 +               .name           = &dentry->d_name
8078 +       };
8079 +       struct dentry *parent;
8080 +       struct super_block *sb;
8081 +
8082 +       sb = dentry->d_sb;
8083 +       err = au_test_shwh(sb, args.name);
8084 +       if (unlikely(err))
8085 +               goto out;
8086 +
8087 +       err = au_wh_name_alloc(&args.whname, args.name);
8088 +       if (unlikely(err))
8089 +               goto out;
8090 +
8091 +       isdir = !!d_is_dir(dentry);
8092 +       dirperm1 = !!au_opt_test(au_mntflags(sb), DIRPERM1);
8093 +       dirren = !!au_opt_test(au_mntflags(sb), DIRREN);
8094 +       if (dirren)
8095 +               au_fset_lkup(args.flags, DIRREN);
8096 +
8097 +       npositive = 0;
8098 +       parent = dget_parent(dentry);
8099 +       btail = au_dbtaildir(parent);
8100 +       for (bindex = btop; bindex <= btail; bindex++) {
8101 +               struct dentry *h_parent, *h_dentry;
8102 +               struct inode *h_inode, *h_dir;
8103 +               struct au_branch *br;
8104 +
8105 +               h_dentry = au_h_dptr(dentry, bindex);
8106 +               if (h_dentry) {
8107 +                       if (d_is_positive(h_dentry))
8108 +                               npositive++;
8109 +                       break;
8110 +               }
8111 +               h_parent = au_h_dptr(parent, bindex);
8112 +               if (!h_parent || !d_is_dir(h_parent))
8113 +                       continue;
8114 +
8115 +               if (dirren) {
8116 +                       /* if the inum matches, then use the prepared name */
8117 +                       err = au_dr_lkup_name(&args, bindex);
8118 +                       if (unlikely(err))
8119 +                               goto out_parent;
8120 +               }
8121 +
8122 +               h_dir = d_inode(h_parent);
8123 +               inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
8124 +               h_dentry = au_do_lookup(h_parent, dentry, bindex, &args);
8125 +               inode_unlock_shared(h_dir);
8126 +               err = PTR_ERR(h_dentry);
8127 +               if (IS_ERR(h_dentry))
8128 +                       goto out_parent;
8129 +               if (h_dentry)
8130 +                       au_fclr_lkup(args.flags, ALLOW_NEG);
8131 +               if (dirperm1)
8132 +                       au_fset_lkup(args.flags, IGNORE_PERM);
8133 +
8134 +               if (au_dbwh(dentry) == bindex)
8135 +                       break;
8136 +               if (!h_dentry)
8137 +                       continue;
8138 +               if (d_is_negative(h_dentry))
8139 +                       continue;
8140 +               h_inode = d_inode(h_dentry);
8141 +               npositive++;
8142 +               if (!args.type)
8143 +                       args.type = h_inode->i_mode & S_IFMT;
8144 +               if (args.type != S_IFDIR)
8145 +                       break;
8146 +               else if (isdir) {
8147 +                       /* the type of lower may be different */
8148 +                       bdiropq = au_dbdiropq(dentry);
8149 +                       if (bdiropq >= 0 && bdiropq <= bindex)
8150 +                               break;
8151 +               }
8152 +               br = au_sbr(sb, bindex);
8153 +               if (dirren
8154 +                   && au_dr_hino_test_add(&br->br_dirren, h_inode->i_ino,
8155 +                                          /*add_ent*/NULL)) {
8156 +                       /* prepare next name to lookup */
8157 +                       err = au_dr_lkup(&args, dentry, bindex);
8158 +                       if (unlikely(err))
8159 +                               goto out_parent;
8160 +               }
8161 +       }
8162 +
8163 +       if (npositive) {
8164 +               AuLabel(positive);
8165 +               au_update_dbtop(dentry);
8166 +       }
8167 +       err = npositive;
8168 +       if (unlikely(!au_opt_test(au_mntflags(sb), UDBA_NONE)
8169 +                    && au_dbtop(dentry) < 0)) {
8170 +               err = -EIO;
8171 +               AuIOErr("both of real entry and whiteout found, %pd, err %d\n",
8172 +                       dentry, err);
8173 +       }
8174 +
8175 +out_parent:
8176 +       dput(parent);
8177 +       au_kfree_try_rcu(args.whname.name);
8178 +       if (dirren)
8179 +               au_dr_lkup_fin(&args);
8180 +out:
8181 +       return err;
8182 +}
8183 +
8184 +struct dentry *au_sio_lkup_one(struct user_namespace *userns, struct qstr *name,
8185 +                              struct path *ppath)
8186 +{
8187 +       struct dentry *dentry;
8188 +       int wkq_err;
8189 +
8190 +       if (!au_test_h_perm_sio(userns, d_inode(ppath->dentry), MAY_EXEC))
8191 +               dentry = vfsub_lkup_one(name, ppath);
8192 +       else {
8193 +               struct vfsub_lkup_one_args args = {
8194 +                       .errp   = &dentry,
8195 +                       .name   = name,
8196 +                       .ppath  = ppath
8197 +               };
8198 +
8199 +               wkq_err = au_wkq_wait(vfsub_call_lkup_one, &args);
8200 +               if (unlikely(wkq_err))
8201 +                       dentry = ERR_PTR(wkq_err);
8202 +       }
8203 +
8204 +       return dentry;
8205 +}
8206 +
8207 +/*
8208 + * lookup @dentry on @bindex which should be negative.
8209 + */
8210 +int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh)
8211 +{
8212 +       int err;
8213 +       struct dentry *parent, *h_dentry;
8214 +       struct au_branch *br;
8215 +       struct user_namespace *h_userns;
8216 +       struct path h_ppath;
8217 +
8218 +       parent = dget_parent(dentry);
8219 +       br = au_sbr(dentry->d_sb, bindex);
8220 +       h_ppath.dentry = au_h_dptr(parent, bindex);
8221 +       h_ppath.mnt = au_br_mnt(br);
8222 +       h_userns = au_br_userns(br);
8223 +       if (wh)
8224 +               h_dentry = au_whtmp_lkup(h_ppath.dentry, br, &dentry->d_name);
8225 +       else
8226 +               h_dentry = au_sio_lkup_one(h_userns, &dentry->d_name, &h_ppath);
8227 +       err = PTR_ERR(h_dentry);
8228 +       if (IS_ERR(h_dentry))
8229 +               goto out;
8230 +       if (unlikely(d_is_positive(h_dentry))) {
8231 +               err = -EIO;
8232 +               AuIOErr("%pd should be negative on b%d.\n", h_dentry, bindex);
8233 +               dput(h_dentry);
8234 +               goto out;
8235 +       }
8236 +
8237 +       err = 0;
8238 +       if (bindex < au_dbtop(dentry))
8239 +               au_set_dbtop(dentry, bindex);
8240 +       if (au_dbbot(dentry) < bindex)
8241 +               au_set_dbbot(dentry, bindex);
8242 +       au_set_h_dptr(dentry, bindex, h_dentry);
8243 +
8244 +out:
8245 +       dput(parent);
8246 +       return err;
8247 +}
8248 +
8249 +/* ---------------------------------------------------------------------- */
8250 +
8251 +/* subset of struct inode */
8252 +struct au_iattr {
8253 +       unsigned long           i_ino;
8254 +       /* unsigned int         i_nlink; */
8255 +       kuid_t                  i_uid;
8256 +       kgid_t                  i_gid;
8257 +       u64                     i_version;
8258 +/*
8259 +       loff_t                  i_size;
8260 +       blkcnt_t                i_blocks;
8261 +*/
8262 +       umode_t                 i_mode;
8263 +};
8264 +
8265 +static void au_iattr_save(struct au_iattr *ia, struct inode *h_inode)
8266 +{
8267 +       ia->i_ino = h_inode->i_ino;
8268 +       /* ia->i_nlink = h_inode->i_nlink; */
8269 +       ia->i_uid = h_inode->i_uid;
8270 +       ia->i_gid = h_inode->i_gid;
8271 +       ia->i_version = inode_query_iversion(h_inode);
8272 +/*
8273 +       ia->i_size = h_inode->i_size;
8274 +       ia->i_blocks = h_inode->i_blocks;
8275 +*/
8276 +       ia->i_mode = (h_inode->i_mode & S_IFMT);
8277 +}
8278 +
8279 +static int au_iattr_test(struct au_iattr *ia, struct inode *h_inode)
8280 +{
8281 +       return ia->i_ino != h_inode->i_ino
8282 +               /* || ia->i_nlink != h_inode->i_nlink */
8283 +               || !uid_eq(ia->i_uid, h_inode->i_uid)
8284 +               || !gid_eq(ia->i_gid, h_inode->i_gid)
8285 +               || !inode_eq_iversion(h_inode, ia->i_version)
8286 +/*
8287 +               || ia->i_size != h_inode->i_size
8288 +               || ia->i_blocks != h_inode->i_blocks
8289 +*/
8290 +               || ia->i_mode != (h_inode->i_mode & S_IFMT);
8291 +}
8292 +
8293 +static int au_h_verify_dentry(struct dentry *h_dentry, struct dentry *h_parent,
8294 +                             struct au_branch *br)
8295 +{
8296 +       int err;
8297 +       struct au_iattr ia;
8298 +       struct inode *h_inode;
8299 +       struct dentry *h_d;
8300 +       struct super_block *h_sb;
8301 +       struct path h_ppath;
8302 +
8303 +       err = 0;
8304 +       memset(&ia, -1, sizeof(ia));
8305 +       h_sb = h_dentry->d_sb;
8306 +       h_inode = NULL;
8307 +       if (d_is_positive(h_dentry)) {
8308 +               h_inode = d_inode(h_dentry);
8309 +               au_iattr_save(&ia, h_inode);
8310 +       } else if (au_test_nfs(h_sb) || au_test_fuse(h_sb))
8311 +               /* nfs d_revalidate may return 0 for negative dentry */
8312 +               /* fuse d_revalidate always return 0 for negative dentry */
8313 +               goto out;
8314 +
8315 +       /* main purpose is namei.c:cached_lookup() and d_revalidate */
8316 +       h_ppath.dentry = h_parent;
8317 +       h_ppath.mnt = au_br_mnt(br);
8318 +       h_d = vfsub_lkup_one(&h_dentry->d_name, &h_ppath);
8319 +       err = PTR_ERR(h_d);
8320 +       if (IS_ERR(h_d))
8321 +               goto out;
8322 +
8323 +       err = 0;
8324 +       if (unlikely(h_d != h_dentry
8325 +                    || d_inode(h_d) != h_inode
8326 +                    || (h_inode && au_iattr_test(&ia, h_inode))))
8327 +               err = au_busy_or_stale();
8328 +       dput(h_d);
8329 +
8330 +out:
8331 +       AuTraceErr(err);
8332 +       return err;
8333 +}
8334 +
8335 +int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
8336 +               struct dentry *h_parent, struct au_branch *br)
8337 +{
8338 +       int err;
8339 +
8340 +       err = 0;
8341 +       if (udba == AuOpt_UDBA_REVAL
8342 +           && !au_test_fs_remote(h_dentry->d_sb)) {
8343 +               IMustLock(h_dir);
8344 +               err = (d_inode(h_dentry->d_parent) != h_dir);
8345 +       } else if (udba != AuOpt_UDBA_NONE)
8346 +               err = au_h_verify_dentry(h_dentry, h_parent, br);
8347 +
8348 +       return err;
8349 +}
8350 +
8351 +/* ---------------------------------------------------------------------- */
8352 +
8353 +static int au_do_refresh_hdentry(struct dentry *dentry, struct dentry *parent)
8354 +{
8355 +       int err;
8356 +       aufs_bindex_t new_bindex, bindex, bbot, bwh, bdiropq;
8357 +       struct au_hdentry tmp, *p, *q;
8358 +       struct au_dinfo *dinfo;
8359 +       struct super_block *sb;
8360 +
8361 +       DiMustWriteLock(dentry);
8362 +
8363 +       sb = dentry->d_sb;
8364 +       dinfo = au_di(dentry);
8365 +       bbot = dinfo->di_bbot;
8366 +       bwh = dinfo->di_bwh;
8367 +       bdiropq = dinfo->di_bdiropq;
8368 +       bindex = dinfo->di_btop;
8369 +       p = au_hdentry(dinfo, bindex);
8370 +       for (; bindex <= bbot; bindex++, p++) {
8371 +               if (!p->hd_dentry)
8372 +                       continue;
8373 +
8374 +               new_bindex = au_br_index(sb, p->hd_id);
8375 +               if (new_bindex == bindex)
8376 +                       continue;
8377 +
8378 +               if (dinfo->di_bwh == bindex)
8379 +                       bwh = new_bindex;
8380 +               if (dinfo->di_bdiropq == bindex)
8381 +                       bdiropq = new_bindex;
8382 +               if (new_bindex < 0) {
8383 +                       au_hdput(p);
8384 +                       p->hd_dentry = NULL;
8385 +                       continue;
8386 +               }
8387 +
8388 +               /* swap two lower dentries, and loop again */
8389 +               q = au_hdentry(dinfo, new_bindex);
8390 +               tmp = *q;
8391 +               *q = *p;
8392 +               *p = tmp;
8393 +               if (tmp.hd_dentry) {
8394 +                       bindex--;
8395 +                       p--;
8396 +               }
8397 +       }
8398 +
8399 +       dinfo->di_bwh = -1;
8400 +       if (bwh >= 0 && bwh <= au_sbbot(sb) && au_sbr_whable(sb, bwh))
8401 +               dinfo->di_bwh = bwh;
8402 +
8403 +       dinfo->di_bdiropq = -1;
8404 +       if (bdiropq >= 0
8405 +           && bdiropq <= au_sbbot(sb)
8406 +           && au_sbr_whable(sb, bdiropq))
8407 +               dinfo->di_bdiropq = bdiropq;
8408 +
8409 +       err = -EIO;
8410 +       dinfo->di_btop = -1;
8411 +       dinfo->di_bbot = -1;
8412 +       bbot = au_dbbot(parent);
8413 +       bindex = 0;
8414 +       p = au_hdentry(dinfo, bindex);
8415 +       for (; bindex <= bbot; bindex++, p++)
8416 +               if (p->hd_dentry) {
8417 +                       dinfo->di_btop = bindex;
8418 +                       break;
8419 +               }
8420 +
8421 +       if (dinfo->di_btop >= 0) {
8422 +               bindex = bbot;
8423 +               p = au_hdentry(dinfo, bindex);
8424 +               for (; bindex >= 0; bindex--, p--)
8425 +                       if (p->hd_dentry) {
8426 +                               dinfo->di_bbot = bindex;
8427 +                               err = 0;
8428 +                               break;
8429 +                       }
8430 +       }
8431 +
8432 +       return err;
8433 +}
8434 +
8435 +static void au_do_hide(struct dentry *dentry)
8436 +{
8437 +       struct inode *inode;
8438 +
8439 +       if (d_really_is_positive(dentry)) {
8440 +               inode = d_inode(dentry);
8441 +               if (!d_is_dir(dentry)) {
8442 +                       if (inode->i_nlink && !d_unhashed(dentry))
8443 +                               drop_nlink(inode);
8444 +               } else {
8445 +                       clear_nlink(inode);
8446 +                       /* stop next lookup */
8447 +                       inode->i_flags |= S_DEAD;
8448 +               }
8449 +               smp_mb(); /* necessary? */
8450 +       }
8451 +       d_drop(dentry);
8452 +}
8453 +
8454 +static int au_hide_children(struct dentry *parent)
8455 +{
8456 +       int err, i, j, ndentry;
8457 +       struct au_dcsub_pages dpages;
8458 +       struct au_dpage *dpage;
8459 +       struct dentry *dentry;
8460 +
8461 +       err = au_dpages_init(&dpages, GFP_NOFS);
8462 +       if (unlikely(err))
8463 +               goto out;
8464 +       err = au_dcsub_pages(&dpages, parent, NULL, NULL);
8465 +       if (unlikely(err))
8466 +               goto out_dpages;
8467 +
8468 +       /* in reverse order */
8469 +       for (i = dpages.ndpage - 1; i >= 0; i--) {
8470 +               dpage = dpages.dpages + i;
8471 +               ndentry = dpage->ndentry;
8472 +               for (j = ndentry - 1; j >= 0; j--) {
8473 +                       dentry = dpage->dentries[j];
8474 +                       if (dentry != parent)
8475 +                               au_do_hide(dentry);
8476 +               }
8477 +       }
8478 +
8479 +out_dpages:
8480 +       au_dpages_free(&dpages);
8481 +out:
8482 +       return err;
8483 +}
8484 +
8485 +static void au_hide(struct dentry *dentry)
8486 +{
8487 +       int err;
8488 +
8489 +       AuDbgDentry(dentry);
8490 +       if (d_is_dir(dentry)) {
8491 +               /* shrink_dcache_parent(dentry); */
8492 +               err = au_hide_children(dentry);
8493 +               if (unlikely(err))
8494 +                       AuIOErr("%pd, failed hiding children, ignored %d\n",
8495 +                               dentry, err);
8496 +       }
8497 +       au_do_hide(dentry);
8498 +}
8499 +
8500 +/*
8501 + * By adding a dirty branch, a cached dentry may be affected in various ways.
8502 + *
8503 + * a dirty branch is added
8504 + * - on the top of layers
8505 + * - in the middle of layers
8506 + * - to the bottom of layers
8507 + *
8508 + * on the added branch there exists
8509 + * - a whiteout
8510 + * - a diropq
8511 + * - a same named entry
8512 + *   + exist
8513 + *     * negative --> positive
8514 + *     * positive --> positive
8515 + *      - type is unchanged
8516 + *      - type is changed
8517 + *   + doesn't exist
8518 + *     * negative --> negative
8519 + *     * positive --> negative (rejected by au_br_del() for non-dir case)
8520 + * - none
8521 + */
8522 +static int au_refresh_by_dinfo(struct dentry *dentry, struct au_dinfo *dinfo,
8523 +                              struct au_dinfo *tmp)
8524 +{
8525 +       int err;
8526 +       aufs_bindex_t bindex, bbot;
8527 +       struct {
8528 +               struct dentry *dentry;
8529 +               struct inode *inode;
8530 +               mode_t mode;
8531 +       } orig_h, tmp_h = {
8532 +               .dentry = NULL
8533 +       };
8534 +       struct au_hdentry *hd;
8535 +       struct inode *inode, *h_inode;
8536 +       struct dentry *h_dentry;
8537 +
8538 +       err = 0;
8539 +       AuDebugOn(dinfo->di_btop < 0);
8540 +       orig_h.mode = 0;
8541 +       orig_h.dentry = au_hdentry(dinfo, dinfo->di_btop)->hd_dentry;
8542 +       orig_h.inode = NULL;
8543 +       if (d_is_positive(orig_h.dentry)) {
8544 +               orig_h.inode = d_inode(orig_h.dentry);
8545 +               orig_h.mode = orig_h.inode->i_mode & S_IFMT;
8546 +       }
8547 +       if (tmp->di_btop >= 0) {
8548 +               tmp_h.dentry = au_hdentry(tmp, tmp->di_btop)->hd_dentry;
8549 +               if (d_is_positive(tmp_h.dentry)) {
8550 +                       tmp_h.inode = d_inode(tmp_h.dentry);
8551 +                       tmp_h.mode = tmp_h.inode->i_mode & S_IFMT;
8552 +               }
8553 +       }
8554 +
8555 +       inode = NULL;
8556 +       if (d_really_is_positive(dentry))
8557 +               inode = d_inode(dentry);
8558 +       if (!orig_h.inode) {
8559 +               AuDbg("negative originally\n");
8560 +               if (inode) {
8561 +                       au_hide(dentry);
8562 +                       goto out;
8563 +               }
8564 +               AuDebugOn(inode);
8565 +               AuDebugOn(dinfo->di_btop != dinfo->di_bbot);
8566 +               AuDebugOn(dinfo->di_bdiropq != -1);
8567 +
8568 +               if (!tmp_h.inode) {
8569 +                       AuDbg("negative --> negative\n");
8570 +                       /* should have only one negative lower */
8571 +                       if (tmp->di_btop >= 0
8572 +                           && tmp->di_btop < dinfo->di_btop) {
8573 +                               AuDebugOn(tmp->di_btop != tmp->di_bbot);
8574 +                               AuDebugOn(dinfo->di_btop != dinfo->di_bbot);
8575 +                               au_set_h_dptr(dentry, dinfo->di_btop, NULL);
8576 +                               au_di_cp(dinfo, tmp);
8577 +                               hd = au_hdentry(tmp, tmp->di_btop);
8578 +                               au_set_h_dptr(dentry, tmp->di_btop,
8579 +                                             dget(hd->hd_dentry));
8580 +                       }
8581 +                       au_dbg_verify_dinode(dentry);
8582 +               } else {
8583 +                       AuDbg("negative --> positive\n");
8584 +                       /*
8585 +                        * similar to the behaviour of creating with bypassing
8586 +                        * aufs.
8587 +                        * unhash it in order to force an error in the
8588 +                        * succeeding create operation.
8589 +                        * we should not set S_DEAD here.
8590 +                        */
8591 +                       d_drop(dentry);
8592 +                       /* au_di_swap(tmp, dinfo); */
8593 +                       au_dbg_verify_dinode(dentry);
8594 +               }
8595 +       } else {
8596 +               AuDbg("positive originally\n");
8597 +               /* inode may be NULL */
8598 +               AuDebugOn(inode && (inode->i_mode & S_IFMT) != orig_h.mode);
8599 +               if (!tmp_h.inode) {
8600 +                       AuDbg("positive --> negative\n");
8601 +                       /* or bypassing aufs */
8602 +                       au_hide(dentry);
8603 +                       if (tmp->di_bwh >= 0 && tmp->di_bwh <= dinfo->di_btop)
8604 +                               dinfo->di_bwh = tmp->di_bwh;
8605 +                       if (inode)
8606 +                               err = au_refresh_hinode_self(inode);
8607 +                       au_dbg_verify_dinode(dentry);
8608 +               } else if (orig_h.mode == tmp_h.mode) {
8609 +                       AuDbg("positive --> positive, same type\n");
8610 +                       if (!S_ISDIR(orig_h.mode)
8611 +                           && dinfo->di_btop > tmp->di_btop) {
8612 +                               /*
8613 +                                * similar to the behaviour of removing and
8614 +                                * creating.
8615 +                                */
8616 +                               au_hide(dentry);
8617 +                               if (inode)
8618 +                                       err = au_refresh_hinode_self(inode);
8619 +                               au_dbg_verify_dinode(dentry);
8620 +                       } else {
8621 +                               /* fill empty slots */
8622 +                               if (dinfo->di_btop > tmp->di_btop)
8623 +                                       dinfo->di_btop = tmp->di_btop;
8624 +                               if (dinfo->di_bbot < tmp->di_bbot)
8625 +                                       dinfo->di_bbot = tmp->di_bbot;
8626 +                               dinfo->di_bwh = tmp->di_bwh;
8627 +                               dinfo->di_bdiropq = tmp->di_bdiropq;
8628 +                               bbot = dinfo->di_bbot;
8629 +                               bindex = tmp->di_btop;
8630 +                               hd = au_hdentry(tmp, bindex);
8631 +                               for (; bindex <= bbot; bindex++, hd++) {
8632 +                                       if (au_h_dptr(dentry, bindex))
8633 +                                               continue;
8634 +                                       h_dentry = hd->hd_dentry;
8635 +                                       if (!h_dentry)
8636 +                                               continue;
8637 +                                       AuDebugOn(d_is_negative(h_dentry));
8638 +                                       h_inode = d_inode(h_dentry);
8639 +                                       AuDebugOn(orig_h.mode
8640 +                                                 != (h_inode->i_mode
8641 +                                                     & S_IFMT));
8642 +                                       au_set_h_dptr(dentry, bindex,
8643 +                                                     dget(h_dentry));
8644 +                               }
8645 +                               if (inode)
8646 +                                       err = au_refresh_hinode(inode, dentry);
8647 +                               au_dbg_verify_dinode(dentry);
8648 +                       }
8649 +               } else {
8650 +                       AuDbg("positive --> positive, different type\n");
8651 +                       /* similar to the behaviour of removing and creating */
8652 +                       au_hide(dentry);
8653 +                       if (inode)
8654 +                               err = au_refresh_hinode_self(inode);
8655 +                       au_dbg_verify_dinode(dentry);
8656 +               }
8657 +       }
8658 +
8659 +out:
8660 +       return err;
8661 +}
8662 +
8663 +void au_refresh_dop(struct dentry *dentry, int force_reval)
8664 +{
8665 +       const struct dentry_operations *dop
8666 +               = force_reval ? &aufs_dop : dentry->d_sb->s_d_op;
8667 +       static const unsigned int mask
8668 +               = DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE;
8669 +
8670 +       BUILD_BUG_ON(sizeof(mask) != sizeof(dentry->d_flags));
8671 +
8672 +       if (dentry->d_op == dop)
8673 +               return;
8674 +
8675 +       AuDbg("%pd\n", dentry);
8676 +       spin_lock(&dentry->d_lock);
8677 +       if (dop == &aufs_dop)
8678 +               dentry->d_flags |= mask;
8679 +       else
8680 +               dentry->d_flags &= ~mask;
8681 +       dentry->d_op = dop;
8682 +       spin_unlock(&dentry->d_lock);
8683 +}
8684 +
8685 +int au_refresh_dentry(struct dentry *dentry, struct dentry *parent)
8686 +{
8687 +       int err, ebrange, nbr;
8688 +       unsigned int sigen;
8689 +       struct au_dinfo *dinfo, *tmp;
8690 +       struct super_block *sb;
8691 +       struct inode *inode;
8692 +
8693 +       DiMustWriteLock(dentry);
8694 +       AuDebugOn(IS_ROOT(dentry));
8695 +       AuDebugOn(d_really_is_negative(parent));
8696 +
8697 +       sb = dentry->d_sb;
8698 +       sigen = au_sigen(sb);
8699 +       err = au_digen_test(parent, sigen);
8700 +       if (unlikely(err))
8701 +               goto out;
8702 +
8703 +       nbr = au_sbbot(sb) + 1;
8704 +       dinfo = au_di(dentry);
8705 +       err = au_di_realloc(dinfo, nbr, /*may_shrink*/0);
8706 +       if (unlikely(err))
8707 +               goto out;
8708 +       ebrange = au_dbrange_test(dentry);
8709 +       if (!ebrange)
8710 +               ebrange = au_do_refresh_hdentry(dentry, parent);
8711 +
8712 +       if (d_unhashed(dentry) || ebrange /* || dinfo->di_tmpfile */) {
8713 +               AuDebugOn(au_dbtop(dentry) < 0 && au_dbbot(dentry) >= 0);
8714 +               if (d_really_is_positive(dentry)) {
8715 +                       inode = d_inode(dentry);
8716 +                       err = au_refresh_hinode_self(inode);
8717 +               }
8718 +               au_dbg_verify_dinode(dentry);
8719 +               if (!err)
8720 +                       goto out_dgen; /* success */
8721 +               goto out;
8722 +       }
8723 +
8724 +       /* temporary dinfo */
8725 +       AuDbgDentry(dentry);
8726 +       err = -ENOMEM;
8727 +       tmp = au_di_alloc(sb, AuLsc_DI_TMP);
8728 +       if (unlikely(!tmp))
8729 +               goto out;
8730 +       au_di_swap(tmp, dinfo);
8731 +       /* returns the number of positive dentries */
8732 +       /*
8733 +        * if current working dir is removed, it returns an error.
8734 +        * but the dentry is legal.
8735 +        */
8736 +       err = au_lkup_dentry(dentry, /*btop*/0, AuLkup_ALLOW_NEG);
8737 +       AuDbgDentry(dentry);
8738 +       au_di_swap(tmp, dinfo);
8739 +       if (err == -ENOENT)
8740 +               err = 0;
8741 +       if (err >= 0) {
8742 +               /* compare/refresh by dinfo */
8743 +               AuDbgDentry(dentry);
8744 +               err = au_refresh_by_dinfo(dentry, dinfo, tmp);
8745 +               au_dbg_verify_dinode(dentry);
8746 +               AuTraceErr(err);
8747 +       }
8748 +       au_di_realloc(dinfo, nbr, /*may_shrink*/1); /* harmless if err */
8749 +       au_rw_write_unlock(&tmp->di_rwsem);
8750 +       au_di_free(tmp);
8751 +       if (unlikely(err))
8752 +               goto out;
8753 +
8754 +out_dgen:
8755 +       au_update_digen(dentry);
8756 +out:
8757 +       if (unlikely(err && !(dentry->d_flags & DCACHE_NFSFS_RENAMED))) {
8758 +               AuIOErr("failed refreshing %pd, %d\n", dentry, err);
8759 +               AuDbgDentry(dentry);
8760 +       }
8761 +       AuTraceErr(err);
8762 +       return err;
8763 +}
8764 +
8765 +static int au_do_h_d_reval(struct dentry *h_dentry, unsigned int flags,
8766 +                          struct dentry *dentry, aufs_bindex_t bindex)
8767 +{
8768 +       int err, valid;
8769 +
8770 +       err = 0;
8771 +       if (!(h_dentry->d_flags & DCACHE_OP_REVALIDATE))
8772 +               goto out;
8773 +
8774 +       AuDbg("b%d\n", bindex);
8775 +       /*
8776 +        * gave up supporting LOOKUP_CREATE/OPEN for lower fs,
8777 +        * due to whiteout and branch permission.
8778 +        */
8779 +       flags &= ~(/*LOOKUP_PARENT |*/ LOOKUP_OPEN | LOOKUP_CREATE
8780 +                  | LOOKUP_FOLLOW | LOOKUP_EXCL);
8781 +       /* it may return tri-state */
8782 +       valid = h_dentry->d_op->d_revalidate(h_dentry, flags);
8783 +
8784 +       if (unlikely(valid < 0))
8785 +               err = valid;
8786 +       else if (!valid)
8787 +               err = -EINVAL;
8788 +
8789 +out:
8790 +       AuTraceErr(err);
8791 +       return err;
8792 +}
8793 +
8794 +/* todo: remove this */
8795 +static int h_d_revalidate(struct dentry *dentry, struct inode *inode,
8796 +                         unsigned int flags, int do_udba, int dirren)
8797 +{
8798 +       int err;
8799 +       umode_t mode, h_mode;
8800 +       aufs_bindex_t bindex, btail, btop, ibs, ibe;
8801 +       unsigned char plus, unhashed, is_root, h_plus, h_nfs, tmpfile;
8802 +       struct inode *h_inode, *h_cached_inode;
8803 +       struct dentry *h_dentry;
8804 +       struct qstr *name, *h_name;
8805 +
8806 +       err = 0;
8807 +       plus = 0;
8808 +       mode = 0;
8809 +       ibs = -1;
8810 +       ibe = -1;
8811 +       unhashed = !!d_unhashed(dentry);
8812 +       is_root = !!IS_ROOT(dentry);
8813 +       name = &dentry->d_name;
8814 +       tmpfile = au_di(dentry)->di_tmpfile;
8815 +
8816 +       /*
8817 +        * Theoretically, REVAL test should be unnecessary in case of
8818 +        * {FS,I}NOTIFY.
8819 +        * But {fs,i}notify doesn't fire some necessary events,
8820 +        *      IN_ATTRIB for atime/nlink/pageio
8821 +        * Let's do REVAL test too.
8822 +        */
8823 +       if (do_udba && inode) {
8824 +               mode = (inode->i_mode & S_IFMT);
8825 +               plus = (inode->i_nlink > 0);
8826 +               ibs = au_ibtop(inode);
8827 +               ibe = au_ibbot(inode);
8828 +       }
8829 +
8830 +       btop = au_dbtop(dentry);
8831 +       btail = btop;
8832 +       if (inode && S_ISDIR(inode->i_mode))
8833 +               btail = au_dbtaildir(dentry);
8834 +       for (bindex = btop; bindex <= btail; bindex++) {
8835 +               h_dentry = au_h_dptr(dentry, bindex);
8836 +               if (!h_dentry)
8837 +                       continue;
8838 +
8839 +               AuDbg("b%d, %pd\n", bindex, h_dentry);
8840 +               h_nfs = !!au_test_nfs(h_dentry->d_sb);
8841 +               spin_lock(&h_dentry->d_lock);
8842 +               h_name = &h_dentry->d_name;
8843 +               if (unlikely(do_udba
8844 +                            && !is_root
8845 +                            && ((!h_nfs
8846 +                                 && (unhashed != !!d_unhashed(h_dentry)
8847 +                                     || (!tmpfile && !dirren
8848 +                                         && !au_qstreq(name, h_name))
8849 +                                         ))
8850 +                                || (h_nfs
8851 +                                    && !(flags & LOOKUP_OPEN)
8852 +                                    && (h_dentry->d_flags
8853 +                                        & DCACHE_NFSFS_RENAMED)))
8854 +                           )) {
8855 +                       int h_unhashed;
8856 +
8857 +                       h_unhashed = d_unhashed(h_dentry);
8858 +                       spin_unlock(&h_dentry->d_lock);
8859 +                       AuDbg("unhash 0x%x 0x%x, %pd %pd\n",
8860 +                             unhashed, h_unhashed, dentry, h_dentry);
8861 +                       goto err;
8862 +               }
8863 +               spin_unlock(&h_dentry->d_lock);
8864 +
8865 +               err = au_do_h_d_reval(h_dentry, flags, dentry, bindex);
8866 +               if (unlikely(err))
8867 +                       /* do not goto err, to keep the errno */
8868 +                       break;
8869 +
8870 +               /* todo: plink too? */
8871 +               if (!do_udba)
8872 +                       continue;
8873 +
8874 +               /* UDBA tests */
8875 +               if (unlikely(!!inode != d_is_positive(h_dentry)))
8876 +                       goto err;
8877 +
8878 +               h_inode = NULL;
8879 +               if (d_is_positive(h_dentry))
8880 +                       h_inode = d_inode(h_dentry);
8881 +               h_plus = plus;
8882 +               h_mode = mode;
8883 +               h_cached_inode = h_inode;
8884 +               if (h_inode) {
8885 +                       h_mode = (h_inode->i_mode & S_IFMT);
8886 +                       h_plus = (h_inode->i_nlink > 0);
8887 +               }
8888 +               if (inode && ibs <= bindex && bindex <= ibe)
8889 +                       h_cached_inode = au_h_iptr(inode, bindex);
8890 +
8891 +               if (!h_nfs) {
8892 +                       if (unlikely(plus != h_plus && !tmpfile))
8893 +                               goto err;
8894 +               } else {
8895 +                       if (unlikely(!(h_dentry->d_flags & DCACHE_NFSFS_RENAMED)
8896 +                                    && !is_root
8897 +                                    && !IS_ROOT(h_dentry)
8898 +                                    && unhashed != d_unhashed(h_dentry)))
8899 +                               goto err;
8900 +               }
8901 +               if (unlikely(mode != h_mode
8902 +                            || h_cached_inode != h_inode))
8903 +                       goto err;
8904 +               continue;
8905 +
8906 +err:
8907 +               err = -EINVAL;
8908 +               break;
8909 +       }
8910 +
8911 +       AuTraceErr(err);
8912 +       return err;
8913 +}
8914 +
8915 +/* todo: consolidate with do_refresh() and au_reval_for_attr() */
8916 +static int simple_reval_dpath(struct dentry *dentry, unsigned int sigen)
8917 +{
8918 +       int err;
8919 +       struct dentry *parent;
8920 +
8921 +       if (!au_digen_test(dentry, sigen))
8922 +               return 0;
8923 +
8924 +       parent = dget_parent(dentry);
8925 +       di_read_lock_parent(parent, AuLock_IR);
8926 +       AuDebugOn(au_digen_test(parent, sigen));
8927 +       au_dbg_verify_gen(parent, sigen);
8928 +       err = au_refresh_dentry(dentry, parent);
8929 +       di_read_unlock(parent, AuLock_IR);
8930 +       dput(parent);
8931 +       AuTraceErr(err);
8932 +       return err;
8933 +}
8934 +
8935 +int au_reval_dpath(struct dentry *dentry, unsigned int sigen)
8936 +{
8937 +       int err;
8938 +       struct dentry *d, *parent;
8939 +
8940 +       if (!au_ftest_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR))
8941 +               return simple_reval_dpath(dentry, sigen);
8942 +
8943 +       /* slow loop, keep it simple and stupid */
8944 +       /* cf: au_cpup_dirs() */
8945 +       err = 0;
8946 +       parent = NULL;
8947 +       while (au_digen_test(dentry, sigen)) {
8948 +               d = dentry;
8949 +               while (1) {
8950 +                       dput(parent);
8951 +                       parent = dget_parent(d);
8952 +                       if (!au_digen_test(parent, sigen))
8953 +                               break;
8954 +                       d = parent;
8955 +               }
8956 +
8957 +               if (d != dentry)
8958 +                       di_write_lock_child2(d);
8959 +
8960 +               /* someone might update our dentry while we were sleeping */
8961 +               if (au_digen_test(d, sigen)) {
8962 +                       /*
8963 +                        * todo: consolidate with simple_reval_dpath(),
8964 +                        * do_refresh() and au_reval_for_attr().
8965 +                        */
8966 +                       di_read_lock_parent(parent, AuLock_IR);
8967 +                       err = au_refresh_dentry(d, parent);
8968 +                       di_read_unlock(parent, AuLock_IR);
8969 +               }
8970 +
8971 +               if (d != dentry)
8972 +                       di_write_unlock(d);
8973 +               dput(parent);
8974 +               if (unlikely(err))
8975 +                       break;
8976 +       }
8977 +
8978 +       return err;
8979 +}
8980 +
8981 +/*
8982 + * if valid returns 1, otherwise 0.
8983 + */
8984 +static int aufs_d_revalidate(struct dentry *dentry, unsigned int flags)
8985 +{
8986 +       int valid, err;
8987 +       unsigned int sigen;
8988 +       unsigned char do_udba, dirren;
8989 +       struct super_block *sb;
8990 +       struct inode *inode;
8991 +
8992 +       /* todo: support rcu-walk? */
8993 +       if (flags & LOOKUP_RCU)
8994 +               return -ECHILD;
8995 +
8996 +       valid = 0;
8997 +       if (unlikely(!au_di(dentry)))
8998 +               goto out;
8999 +
9000 +       valid = 1;
9001 +       sb = dentry->d_sb;
9002 +       /*
9003 +        * todo: very ugly
9004 +        * i_mutex of parent dir may be held,
9005 +        * but we should not return 'invalid' due to busy.
9006 +        */
9007 +       err = aufs_read_lock(dentry, AuLock_FLUSH | AuLock_DW | AuLock_NOPLM);
9008 +       if (unlikely(err)) {
9009 +               valid = err;
9010 +               AuTraceErr(err);
9011 +               goto out;
9012 +       }
9013 +       inode = NULL;
9014 +       if (d_really_is_positive(dentry))
9015 +               inode = d_inode(dentry);
9016 +       if (unlikely(inode && au_is_bad_inode(inode))) {
9017 +               err = -EINVAL;
9018 +               AuTraceErr(err);
9019 +               goto out_dgrade;
9020 +       }
9021 +       if (unlikely(au_dbrange_test(dentry))) {
9022 +               err = -EINVAL;
9023 +               AuTraceErr(err);
9024 +               goto out_dgrade;
9025 +       }
9026 +
9027 +       sigen = au_sigen(sb);
9028 +       if (au_digen_test(dentry, sigen)) {
9029 +               AuDebugOn(IS_ROOT(dentry));
9030 +               err = au_reval_dpath(dentry, sigen);
9031 +               if (unlikely(err)) {
9032 +                       AuTraceErr(err);
9033 +                       goto out_dgrade;
9034 +               }
9035 +       }
9036 +       di_downgrade_lock(dentry, AuLock_IR);
9037 +
9038 +       err = -EINVAL;
9039 +       if (!(flags & (LOOKUP_OPEN | LOOKUP_EMPTY))
9040 +           && inode
9041 +           && !(inode->i_state && I_LINKABLE)
9042 +           && (IS_DEADDIR(inode) || !inode->i_nlink)) {
9043 +               AuTraceErr(err);
9044 +               goto out_inval;
9045 +       }
9046 +
9047 +       do_udba = !au_opt_test(au_mntflags(sb), UDBA_NONE);
9048 +       if (do_udba && inode) {
9049 +               aufs_bindex_t btop = au_ibtop(inode);
9050 +               struct inode *h_inode;
9051 +
9052 +               if (btop >= 0) {
9053 +                       h_inode = au_h_iptr(inode, btop);
9054 +                       if (h_inode && au_test_higen(inode, h_inode)) {
9055 +                               AuTraceErr(err);
9056 +                               goto out_inval;
9057 +                       }
9058 +               }
9059 +       }
9060 +
9061 +       dirren = !!au_opt_test(au_mntflags(sb), DIRREN);
9062 +       err = h_d_revalidate(dentry, inode, flags, do_udba, dirren);
9063 +       if (unlikely(!err && do_udba && au_dbtop(dentry) < 0)) {
9064 +               err = -EIO;
9065 +               AuDbg("both of real entry and whiteout found, %p, err %d\n",
9066 +                     dentry, err);
9067 +       }
9068 +       goto out_inval;
9069 +
9070 +out_dgrade:
9071 +       di_downgrade_lock(dentry, AuLock_IR);
9072 +out_inval:
9073 +       aufs_read_unlock(dentry, AuLock_IR);
9074 +       AuTraceErr(err);
9075 +       valid = !err;
9076 +out:
9077 +       if (!valid) {
9078 +               AuDbg("%pd invalid, %d\n", dentry, valid);
9079 +               d_drop(dentry);
9080 +       }
9081 +       return valid;
9082 +}
9083 +
9084 +static void aufs_d_release(struct dentry *dentry)
9085 +{
9086 +       if (au_di(dentry)) {
9087 +               au_di_fin(dentry);
9088 +               au_hn_di_reinit(dentry);
9089 +       }
9090 +}
9091 +
9092 +const struct dentry_operations aufs_dop = {
9093 +       .d_revalidate           = aufs_d_revalidate,
9094 +       .d_weak_revalidate      = aufs_d_revalidate,
9095 +       .d_release              = aufs_d_release
9096 +};
9097 +
9098 +/* aufs_dop without d_revalidate */
9099 +const struct dentry_operations aufs_dop_noreval = {
9100 +       .d_release              = aufs_d_release
9101 +};
9102 diff -urN /usr/share/empty/fs/aufs/dentry.h linux/fs/aufs/dentry.h
9103 --- /usr/share/empty/fs/aufs/dentry.h   1970-01-01 01:00:00.000000000 +0100
9104 +++ linux/fs/aufs/dentry.h      2022-03-21 14:49:05.719966344 +0100
9105 @@ -0,0 +1,269 @@
9106 +/* SPDX-License-Identifier: GPL-2.0 */
9107 +/*
9108 + * Copyright (C) 2005-2021 Junjiro R. Okajima
9109 + *
9110 + * This program, aufs is free software; you can redistribute it and/or modify
9111 + * it under the terms of the GNU General Public License as published by
9112 + * the Free Software Foundation; either version 2 of the License, or
9113 + * (at your option) any later version.
9114 + *
9115 + * This program is distributed in the hope that it will be useful,
9116 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
9117 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9118 + * GNU General Public License for more details.
9119 + *
9120 + * You should have received a copy of the GNU General Public License
9121 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
9122 + */
9123 +
9124 +/*
9125 + * lookup and dentry operations
9126 + */
9127 +
9128 +#ifndef __AUFS_DENTRY_H__
9129 +#define __AUFS_DENTRY_H__
9130 +
9131 +#ifdef __KERNEL__
9132 +
9133 +#include <linux/dcache.h>
9134 +#include "dirren.h"
9135 +#include "rwsem.h"
9136 +
9137 +struct au_hdentry {
9138 +       struct dentry           *hd_dentry;
9139 +       aufs_bindex_t           hd_id;
9140 +};
9141 +
9142 +struct au_dinfo {
9143 +       atomic_t                di_generation;
9144 +
9145 +       struct au_rwsem         di_rwsem;
9146 +       aufs_bindex_t           di_btop, di_bbot, di_bwh, di_bdiropq;
9147 +       unsigned char           di_tmpfile; /* to allow the different name */
9148 +       struct au_hdentry       *di_hdentry;
9149 +       struct rcu_head         rcu;
9150 +} ____cacheline_aligned_in_smp;
9151 +
9152 +/* ---------------------------------------------------------------------- */
9153 +
9154 +/* flags for au_lkup_dentry() */
9155 +#define AuLkup_ALLOW_NEG       1
9156 +#define AuLkup_IGNORE_PERM     (1 << 1)
9157 +#define AuLkup_DIRREN          (1 << 2)
9158 +#define au_ftest_lkup(flags, name)     ((flags) & AuLkup_##name)
9159 +#define au_fset_lkup(flags, name) \
9160 +       do { (flags) |= AuLkup_##name; } while (0)
9161 +#define au_fclr_lkup(flags, name) \
9162 +       do { (flags) &= ~AuLkup_##name; } while (0)
9163 +
9164 +#ifndef CONFIG_AUFS_DIRREN
9165 +#undef AuLkup_DIRREN
9166 +#define AuLkup_DIRREN 0
9167 +#endif
9168 +
9169 +struct au_do_lookup_args {
9170 +       unsigned int            flags;
9171 +       mode_t                  type;
9172 +       struct qstr             whname, *name;
9173 +       struct au_dr_lookup     dirren;
9174 +};
9175 +
9176 +/* ---------------------------------------------------------------------- */
9177 +
9178 +/* dentry.c */
9179 +extern const struct dentry_operations aufs_dop, aufs_dop_noreval;
9180 +struct au_branch;
9181 +struct dentry *au_sio_lkup_one(struct user_namespace *userns, struct qstr *name,
9182 +                              struct path *ppath);
9183 +int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
9184 +               struct dentry *h_parent, struct au_branch *br);
9185 +
9186 +int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t btop,
9187 +                  unsigned int flags);
9188 +int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh);
9189 +int au_refresh_dentry(struct dentry *dentry, struct dentry *parent);
9190 +int au_reval_dpath(struct dentry *dentry, unsigned int sigen);
9191 +void au_refresh_dop(struct dentry *dentry, int force_reval);
9192 +
9193 +/* dinfo.c */
9194 +void au_di_init_once(void *_di);
9195 +struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc);
9196 +void au_di_free(struct au_dinfo *dinfo);
9197 +void au_di_swap(struct au_dinfo *a, struct au_dinfo *b);
9198 +void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src);
9199 +int au_di_init(struct dentry *dentry);
9200 +void au_di_fin(struct dentry *dentry);
9201 +int au_di_realloc(struct au_dinfo *dinfo, int nbr, int may_shrink);
9202 +
9203 +void di_read_lock(struct dentry *d, int flags, unsigned int lsc);
9204 +void di_read_unlock(struct dentry *d, int flags);
9205 +void di_downgrade_lock(struct dentry *d, int flags);
9206 +void di_write_lock(struct dentry *d, unsigned int lsc);
9207 +void di_write_unlock(struct dentry *d);
9208 +void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir);
9209 +void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir);
9210 +void di_write_unlock2(struct dentry *d1, struct dentry *d2);
9211 +
9212 +struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex);
9213 +struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex);
9214 +aufs_bindex_t au_dbtail(struct dentry *dentry);
9215 +aufs_bindex_t au_dbtaildir(struct dentry *dentry);
9216 +
9217 +void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
9218 +                  struct dentry *h_dentry);
9219 +int au_digen_test(struct dentry *dentry, unsigned int sigen);
9220 +int au_dbrange_test(struct dentry *dentry);
9221 +void au_update_digen(struct dentry *dentry);
9222 +void au_update_dbrange(struct dentry *dentry, int do_put_zero);
9223 +void au_update_dbtop(struct dentry *dentry);
9224 +void au_update_dbbot(struct dentry *dentry);
9225 +int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry);
9226 +
9227 +/* ---------------------------------------------------------------------- */
9228 +
9229 +static inline struct au_dinfo *au_di(struct dentry *dentry)
9230 +{
9231 +       return dentry->d_fsdata;
9232 +}
9233 +
9234 +/* ---------------------------------------------------------------------- */
9235 +
9236 +/* lock subclass for dinfo */
9237 +enum {
9238 +       AuLsc_DI_CHILD,         /* child first */
9239 +       AuLsc_DI_CHILD2,        /* rename(2), link(2), and cpup at hnotify */
9240 +       AuLsc_DI_CHILD3,        /* copyup dirs */
9241 +       AuLsc_DI_PARENT,
9242 +       AuLsc_DI_PARENT2,
9243 +       AuLsc_DI_PARENT3,
9244 +       AuLsc_DI_TMP            /* temp for replacing dinfo */
9245 +};
9246 +
9247 +/*
9248 + * di_read_lock_child, di_write_lock_child,
9249 + * di_read_lock_child2, di_write_lock_child2,
9250 + * di_read_lock_child3, di_write_lock_child3,
9251 + * di_read_lock_parent, di_write_lock_parent,
9252 + * di_read_lock_parent2, di_write_lock_parent2,
9253 + * di_read_lock_parent3, di_write_lock_parent3,
9254 + */
9255 +#define AuReadLockFunc(name, lsc) \
9256 +static inline void di_read_lock_##name(struct dentry *d, int flags) \
9257 +{ di_read_lock(d, flags, AuLsc_DI_##lsc); }
9258 +
9259 +#define AuWriteLockFunc(name, lsc) \
9260 +static inline void di_write_lock_##name(struct dentry *d) \
9261 +{ di_write_lock(d, AuLsc_DI_##lsc); }
9262 +
9263 +#define AuRWLockFuncs(name, lsc) \
9264 +       AuReadLockFunc(name, lsc) \
9265 +       AuWriteLockFunc(name, lsc)
9266 +
9267 +AuRWLockFuncs(child, CHILD);
9268 +AuRWLockFuncs(child2, CHILD2);
9269 +AuRWLockFuncs(child3, CHILD3);
9270 +AuRWLockFuncs(parent, PARENT);
9271 +AuRWLockFuncs(parent2, PARENT2);
9272 +AuRWLockFuncs(parent3, PARENT3);
9273 +
9274 +#undef AuReadLockFunc
9275 +#undef AuWriteLockFunc
9276 +#undef AuRWLockFuncs
9277 +
9278 +#define DiMustNoWaiters(d)     AuRwMustNoWaiters(&au_di(d)->di_rwsem)
9279 +#define DiMustAnyLock(d)       AuRwMustAnyLock(&au_di(d)->di_rwsem)
9280 +#define DiMustWriteLock(d)     AuRwMustWriteLock(&au_di(d)->di_rwsem)
9281 +
9282 +/* ---------------------------------------------------------------------- */
9283 +
9284 +/* todo: memory barrier? */
9285 +static inline unsigned int au_digen(struct dentry *d)
9286 +{
9287 +       return atomic_read(&au_di(d)->di_generation);
9288 +}
9289 +
9290 +static inline void au_h_dentry_init(struct au_hdentry *hdentry)
9291 +{
9292 +       hdentry->hd_dentry = NULL;
9293 +}
9294 +
9295 +static inline struct au_hdentry *au_hdentry(struct au_dinfo *di,
9296 +                                           aufs_bindex_t bindex)
9297 +{
9298 +       return di->di_hdentry + bindex;
9299 +}
9300 +
9301 +static inline void au_hdput(struct au_hdentry *hd)
9302 +{
9303 +       if (hd)
9304 +               dput(hd->hd_dentry);
9305 +}
9306 +
9307 +static inline aufs_bindex_t au_dbtop(struct dentry *dentry)
9308 +{
9309 +       DiMustAnyLock(dentry);
9310 +       return au_di(dentry)->di_btop;
9311 +}
9312 +
9313 +static inline aufs_bindex_t au_dbbot(struct dentry *dentry)
9314 +{
9315 +       DiMustAnyLock(dentry);
9316 +       return au_di(dentry)->di_bbot;
9317 +}
9318 +
9319 +static inline aufs_bindex_t au_dbwh(struct dentry *dentry)
9320 +{
9321 +       DiMustAnyLock(dentry);
9322 +       return au_di(dentry)->di_bwh;
9323 +}
9324 +
9325 +static inline aufs_bindex_t au_dbdiropq(struct dentry *dentry)
9326 +{
9327 +       DiMustAnyLock(dentry);
9328 +       return au_di(dentry)->di_bdiropq;
9329 +}
9330 +
9331 +/* todo: hard/soft set? */
9332 +static inline void au_set_dbtop(struct dentry *dentry, aufs_bindex_t bindex)
9333 +{
9334 +       DiMustWriteLock(dentry);
9335 +       au_di(dentry)->di_btop = bindex;
9336 +}
9337 +
9338 +static inline void au_set_dbbot(struct dentry *dentry, aufs_bindex_t bindex)
9339 +{
9340 +       DiMustWriteLock(dentry);
9341 +       au_di(dentry)->di_bbot = bindex;
9342 +}
9343 +
9344 +static inline void au_set_dbwh(struct dentry *dentry, aufs_bindex_t bindex)
9345 +{
9346 +       DiMustWriteLock(dentry);
9347 +       /* dbwh can be outside of btop - bbot range */
9348 +       au_di(dentry)->di_bwh = bindex;
9349 +}
9350 +
9351 +static inline void au_set_dbdiropq(struct dentry *dentry, aufs_bindex_t bindex)
9352 +{
9353 +       DiMustWriteLock(dentry);
9354 +       au_di(dentry)->di_bdiropq = bindex;
9355 +}
9356 +
9357 +/* ---------------------------------------------------------------------- */
9358 +
9359 +#ifdef CONFIG_AUFS_HNOTIFY
9360 +static inline void au_digen_dec(struct dentry *d)
9361 +{
9362 +       atomic_dec(&au_di(d)->di_generation);
9363 +}
9364 +
9365 +static inline void au_hn_di_reinit(struct dentry *dentry)
9366 +{
9367 +       dentry->d_fsdata = NULL;
9368 +}
9369 +#else
9370 +AuStubVoid(au_hn_di_reinit, struct dentry *dentry __maybe_unused)
9371 +#endif /* CONFIG_AUFS_HNOTIFY */
9372 +
9373 +#endif /* __KERNEL__ */
9374 +#endif /* __AUFS_DENTRY_H__ */
9375 diff -urN /usr/share/empty/fs/aufs/dinfo.c linux/fs/aufs/dinfo.c
9376 --- /usr/share/empty/fs/aufs/dinfo.c    1970-01-01 01:00:00.000000000 +0100
9377 +++ linux/fs/aufs/dinfo.c       2022-03-21 14:49:05.719966344 +0100
9378 @@ -0,0 +1,554 @@
9379 +// SPDX-License-Identifier: GPL-2.0
9380 +/*
9381 + * Copyright (C) 2005-2021 Junjiro R. Okajima
9382 + *
9383 + * This program, aufs is free software; you can redistribute it and/or modify
9384 + * it under the terms of the GNU General Public License as published by
9385 + * the Free Software Foundation; either version 2 of the License, or
9386 + * (at your option) any later version.
9387 + *
9388 + * This program is distributed in the hope that it will be useful,
9389 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
9390 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9391 + * GNU General Public License for more details.
9392 + *
9393 + * You should have received a copy of the GNU General Public License
9394 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
9395 + */
9396 +
9397 +/*
9398 + * dentry private data
9399 + */
9400 +
9401 +#include "aufs.h"
9402 +
9403 +void au_di_init_once(void *_dinfo)
9404 +{
9405 +       struct au_dinfo *dinfo = _dinfo;
9406 +
9407 +       au_rw_init(&dinfo->di_rwsem);
9408 +}
9409 +
9410 +struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc)
9411 +{
9412 +       struct au_dinfo *dinfo;
9413 +       int nbr, i;
9414 +
9415 +       dinfo = au_cache_alloc_dinfo();
9416 +       if (unlikely(!dinfo))
9417 +               goto out;
9418 +
9419 +       nbr = au_sbbot(sb) + 1;
9420 +       if (nbr <= 0)
9421 +               nbr = 1;
9422 +       dinfo->di_hdentry = kcalloc(nbr, sizeof(*dinfo->di_hdentry), GFP_NOFS);
9423 +       if (dinfo->di_hdentry) {
9424 +               au_rw_write_lock_nested(&dinfo->di_rwsem, lsc);
9425 +               dinfo->di_btop = -1;
9426 +               dinfo->di_bbot = -1;
9427 +               dinfo->di_bwh = -1;
9428 +               dinfo->di_bdiropq = -1;
9429 +               dinfo->di_tmpfile = 0;
9430 +               for (i = 0; i < nbr; i++)
9431 +                       dinfo->di_hdentry[i].hd_id = -1;
9432 +               goto out;
9433 +       }
9434 +
9435 +       au_cache_free_dinfo(dinfo);
9436 +       dinfo = NULL;
9437 +
9438 +out:
9439 +       return dinfo;
9440 +}
9441 +
9442 +void au_di_free(struct au_dinfo *dinfo)
9443 +{
9444 +       struct au_hdentry *p;
9445 +       aufs_bindex_t bbot, bindex;
9446 +
9447 +       /* dentry may not be revalidated */
9448 +       bindex = dinfo->di_btop;
9449 +       if (bindex >= 0) {
9450 +               bbot = dinfo->di_bbot;
9451 +               p = au_hdentry(dinfo, bindex);
9452 +               while (bindex++ <= bbot)
9453 +                       au_hdput(p++);
9454 +       }
9455 +       au_kfree_try_rcu(dinfo->di_hdentry);
9456 +       au_cache_free_dinfo(dinfo);
9457 +}
9458 +
9459 +void au_di_swap(struct au_dinfo *a, struct au_dinfo *b)
9460 +{
9461 +       struct au_hdentry *p;
9462 +       aufs_bindex_t bi;
9463 +
9464 +       AuRwMustWriteLock(&a->di_rwsem);
9465 +       AuRwMustWriteLock(&b->di_rwsem);
9466 +
9467 +#define DiSwap(v, name)                                \
9468 +       do {                                    \
9469 +               v = a->di_##name;               \
9470 +               a->di_##name = b->di_##name;    \
9471 +               b->di_##name = v;               \
9472 +       } while (0)
9473 +
9474 +       DiSwap(p, hdentry);
9475 +       DiSwap(bi, btop);
9476 +       DiSwap(bi, bbot);
9477 +       DiSwap(bi, bwh);
9478 +       DiSwap(bi, bdiropq);
9479 +       /* smp_mb(); */
9480 +
9481 +#undef DiSwap
9482 +}
9483 +
9484 +void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src)
9485 +{
9486 +       AuRwMustWriteLock(&dst->di_rwsem);
9487 +       AuRwMustWriteLock(&src->di_rwsem);
9488 +
9489 +       dst->di_btop = src->di_btop;
9490 +       dst->di_bbot = src->di_bbot;
9491 +       dst->di_bwh = src->di_bwh;
9492 +       dst->di_bdiropq = src->di_bdiropq;
9493 +       /* smp_mb(); */
9494 +}
9495 +
9496 +int au_di_init(struct dentry *dentry)
9497 +{
9498 +       int err;
9499 +       struct super_block *sb;
9500 +       struct au_dinfo *dinfo;
9501 +
9502 +       err = 0;
9503 +       sb = dentry->d_sb;
9504 +       dinfo = au_di_alloc(sb, AuLsc_DI_CHILD);
9505 +       if (dinfo) {
9506 +               atomic_set(&dinfo->di_generation, au_sigen(sb));
9507 +               /* smp_mb(); */ /* atomic_set */
9508 +               dentry->d_fsdata = dinfo;
9509 +       } else
9510 +               err = -ENOMEM;
9511 +
9512 +       return err;
9513 +}
9514 +
9515 +void au_di_fin(struct dentry *dentry)
9516 +{
9517 +       struct au_dinfo *dinfo;
9518 +
9519 +       dinfo = au_di(dentry);
9520 +       AuRwDestroy(&dinfo->di_rwsem);
9521 +       au_di_free(dinfo);
9522 +}
9523 +
9524 +int au_di_realloc(struct au_dinfo *dinfo, int nbr, int may_shrink)
9525 +{
9526 +       int err, sz;
9527 +       struct au_hdentry *hdp;
9528 +
9529 +       AuRwMustWriteLock(&dinfo->di_rwsem);
9530 +
9531 +       err = -ENOMEM;
9532 +       sz = sizeof(*hdp) * (dinfo->di_bbot + 1);
9533 +       if (!sz)
9534 +               sz = sizeof(*hdp);
9535 +       hdp = au_kzrealloc(dinfo->di_hdentry, sz, sizeof(*hdp) * nbr, GFP_NOFS,
9536 +                          may_shrink);
9537 +       if (hdp) {
9538 +               dinfo->di_hdentry = hdp;
9539 +               err = 0;
9540 +       }
9541 +
9542 +       return err;
9543 +}
9544 +
9545 +/* ---------------------------------------------------------------------- */
9546 +
9547 +static void do_ii_write_lock(struct inode *inode, unsigned int lsc)
9548 +{
9549 +       switch (lsc) {
9550 +       case AuLsc_DI_CHILD:
9551 +               ii_write_lock_child(inode);
9552 +               break;
9553 +       case AuLsc_DI_CHILD2:
9554 +               ii_write_lock_child2(inode);
9555 +               break;
9556 +       case AuLsc_DI_CHILD3:
9557 +               ii_write_lock_child3(inode);
9558 +               break;
9559 +       case AuLsc_DI_PARENT:
9560 +               ii_write_lock_parent(inode);
9561 +               break;
9562 +       case AuLsc_DI_PARENT2:
9563 +               ii_write_lock_parent2(inode);
9564 +               break;
9565 +       case AuLsc_DI_PARENT3:
9566 +               ii_write_lock_parent3(inode);
9567 +               break;
9568 +       default:
9569 +               BUG();
9570 +       }
9571 +}
9572 +
9573 +static void do_ii_read_lock(struct inode *inode, unsigned int lsc)
9574 +{
9575 +       switch (lsc) {
9576 +       case AuLsc_DI_CHILD:
9577 +               ii_read_lock_child(inode);
9578 +               break;
9579 +       case AuLsc_DI_CHILD2:
9580 +               ii_read_lock_child2(inode);
9581 +               break;
9582 +       case AuLsc_DI_CHILD3:
9583 +               ii_read_lock_child3(inode);
9584 +               break;
9585 +       case AuLsc_DI_PARENT:
9586 +               ii_read_lock_parent(inode);
9587 +               break;
9588 +       case AuLsc_DI_PARENT2:
9589 +               ii_read_lock_parent2(inode);
9590 +               break;
9591 +       case AuLsc_DI_PARENT3:
9592 +               ii_read_lock_parent3(inode);
9593 +               break;
9594 +       default:
9595 +               BUG();
9596 +       }
9597 +}
9598 +
9599 +void di_read_lock(struct dentry *d, int flags, unsigned int lsc)
9600 +{
9601 +       struct inode *inode;
9602 +
9603 +       au_rw_read_lock_nested(&au_di(d)->di_rwsem, lsc);
9604 +       if (d_really_is_positive(d)) {
9605 +               inode = d_inode(d);
9606 +               if (au_ftest_lock(flags, IW))
9607 +                       do_ii_write_lock(inode, lsc);
9608 +               else if (au_ftest_lock(flags, IR))
9609 +                       do_ii_read_lock(inode, lsc);
9610 +       }
9611 +}
9612 +
9613 +void di_read_unlock(struct dentry *d, int flags)
9614 +{
9615 +       struct inode *inode;
9616 +
9617 +       if (d_really_is_positive(d)) {
9618 +               inode = d_inode(d);
9619 +               if (au_ftest_lock(flags, IW)) {
9620 +                       au_dbg_verify_dinode(d);
9621 +                       ii_write_unlock(inode);
9622 +               } else if (au_ftest_lock(flags, IR)) {
9623 +                       au_dbg_verify_dinode(d);
9624 +                       ii_read_unlock(inode);
9625 +               }
9626 +       }
9627 +       au_rw_read_unlock(&au_di(d)->di_rwsem);
9628 +}
9629 +
9630 +void di_downgrade_lock(struct dentry *d, int flags)
9631 +{
9632 +       if (d_really_is_positive(d) && au_ftest_lock(flags, IR))
9633 +               ii_downgrade_lock(d_inode(d));
9634 +       au_rw_dgrade_lock(&au_di(d)->di_rwsem);
9635 +}
9636 +
9637 +void di_write_lock(struct dentry *d, unsigned int lsc)
9638 +{
9639 +       au_rw_write_lock_nested(&au_di(d)->di_rwsem, lsc);
9640 +       if (d_really_is_positive(d))
9641 +               do_ii_write_lock(d_inode(d), lsc);
9642 +}
9643 +
9644 +void di_write_unlock(struct dentry *d)
9645 +{
9646 +       au_dbg_verify_dinode(d);
9647 +       if (d_really_is_positive(d))
9648 +               ii_write_unlock(d_inode(d));
9649 +       au_rw_write_unlock(&au_di(d)->di_rwsem);
9650 +}
9651 +
9652 +void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir)
9653 +{
9654 +       AuDebugOn(d1 == d2
9655 +                 || d_inode(d1) == d_inode(d2)
9656 +                 || d1->d_sb != d2->d_sb);
9657 +
9658 +       if ((isdir && au_test_subdir(d1, d2))
9659 +           || d1 < d2) {
9660 +               di_write_lock_child(d1);
9661 +               di_write_lock_child2(d2);
9662 +       } else {
9663 +               di_write_lock_child(d2);
9664 +               di_write_lock_child2(d1);
9665 +       }
9666 +}
9667 +
9668 +void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir)
9669 +{
9670 +       AuDebugOn(d1 == d2
9671 +                 || d_inode(d1) == d_inode(d2)
9672 +                 || d1->d_sb != d2->d_sb);
9673 +
9674 +       if ((isdir && au_test_subdir(d1, d2))
9675 +           || d1 < d2) {
9676 +               di_write_lock_parent(d1);
9677 +               di_write_lock_parent2(d2);
9678 +       } else {
9679 +               di_write_lock_parent(d2);
9680 +               di_write_lock_parent2(d1);
9681 +       }
9682 +}
9683 +
9684 +void di_write_unlock2(struct dentry *d1, struct dentry *d2)
9685 +{
9686 +       di_write_unlock(d1);
9687 +       if (d_inode(d1) == d_inode(d2))
9688 +               au_rw_write_unlock(&au_di(d2)->di_rwsem);
9689 +       else
9690 +               di_write_unlock(d2);
9691 +}
9692 +
9693 +/* ---------------------------------------------------------------------- */
9694 +
9695 +struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex)
9696 +{
9697 +       struct dentry *d;
9698 +
9699 +       DiMustAnyLock(dentry);
9700 +
9701 +       if (au_dbtop(dentry) < 0 || bindex < au_dbtop(dentry))
9702 +               return NULL;
9703 +       AuDebugOn(bindex < 0);
9704 +       d = au_hdentry(au_di(dentry), bindex)->hd_dentry;
9705 +       AuDebugOn(d && au_dcount(d) <= 0);
9706 +       return d;
9707 +}
9708 +
9709 +/*
9710 + * extended version of au_h_dptr().
9711 + * returns a hashed and positive (or linkable) h_dentry in bindex, NULL, or
9712 + * error.
9713 + */
9714 +struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex)
9715 +{
9716 +       struct dentry *h_dentry;
9717 +       struct inode *inode, *h_inode;
9718 +
9719 +       AuDebugOn(d_really_is_negative(dentry));
9720 +
9721 +       h_dentry = NULL;
9722 +       if (au_dbtop(dentry) <= bindex
9723 +           && bindex <= au_dbbot(dentry))
9724 +               h_dentry = au_h_dptr(dentry, bindex);
9725 +       if (h_dentry && !au_d_linkable(h_dentry)) {
9726 +               dget(h_dentry);
9727 +               goto out; /* success */
9728 +       }
9729 +
9730 +       inode = d_inode(dentry);
9731 +       AuDebugOn(bindex < au_ibtop(inode));
9732 +       AuDebugOn(au_ibbot(inode) < bindex);
9733 +       h_inode = au_h_iptr(inode, bindex);
9734 +       h_dentry = d_find_alias(h_inode);
9735 +       if (h_dentry) {
9736 +               if (!IS_ERR(h_dentry)) {
9737 +                       if (!au_d_linkable(h_dentry))
9738 +                               goto out; /* success */
9739 +                       dput(h_dentry);
9740 +               } else
9741 +                       goto out;
9742 +       }
9743 +
9744 +       if (au_opt_test(au_mntflags(dentry->d_sb), PLINK)) {
9745 +               h_dentry = au_plink_lkup(inode, bindex);
9746 +               AuDebugOn(!h_dentry);
9747 +               if (!IS_ERR(h_dentry)) {
9748 +                       if (!au_d_hashed_positive(h_dentry))
9749 +                               goto out; /* success */
9750 +                       dput(h_dentry);
9751 +                       h_dentry = NULL;
9752 +               }
9753 +       }
9754 +
9755 +out:
9756 +       AuDbgDentry(h_dentry);
9757 +       return h_dentry;
9758 +}
9759 +
9760 +aufs_bindex_t au_dbtail(struct dentry *dentry)
9761 +{
9762 +       aufs_bindex_t bbot, bwh;
9763 +
9764 +       bbot = au_dbbot(dentry);
9765 +       if (0 <= bbot) {
9766 +               bwh = au_dbwh(dentry);
9767 +               if (!bwh)
9768 +                       return bwh;
9769 +               if (0 < bwh && bwh < bbot)
9770 +                       return bwh - 1;
9771 +       }
9772 +       return bbot;
9773 +}
9774 +
9775 +aufs_bindex_t au_dbtaildir(struct dentry *dentry)
9776 +{
9777 +       aufs_bindex_t bbot, bopq;
9778 +
9779 +       bbot = au_dbtail(dentry);
9780 +       if (0 <= bbot) {
9781 +               bopq = au_dbdiropq(dentry);
9782 +               if (0 <= bopq && bopq < bbot)
9783 +                       bbot = bopq;
9784 +       }
9785 +       return bbot;
9786 +}
9787 +
9788 +/* ---------------------------------------------------------------------- */
9789 +
9790 +void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
9791 +                  struct dentry *h_dentry)
9792 +{
9793 +       struct au_dinfo *dinfo;
9794 +       struct au_hdentry *hd;
9795 +       struct au_branch *br;
9796 +
9797 +       DiMustWriteLock(dentry);
9798 +
9799 +       dinfo = au_di(dentry);
9800 +       hd = au_hdentry(dinfo, bindex);
9801 +       au_hdput(hd);
9802 +       hd->hd_dentry = h_dentry;
9803 +       if (h_dentry) {
9804 +               br = au_sbr(dentry->d_sb, bindex);
9805 +               hd->hd_id = br->br_id;
9806 +       }
9807 +}
9808 +
9809 +int au_dbrange_test(struct dentry *dentry)
9810 +{
9811 +       int err;
9812 +       aufs_bindex_t btop, bbot;
9813 +
9814 +       err = 0;
9815 +       btop = au_dbtop(dentry);
9816 +       bbot = au_dbbot(dentry);
9817 +       if (btop >= 0)
9818 +               AuDebugOn(bbot < 0 && btop > bbot);
9819 +       else {
9820 +               err = -EIO;
9821 +               AuDebugOn(bbot >= 0);
9822 +       }
9823 +
9824 +       return err;
9825 +}
9826 +
9827 +int au_digen_test(struct dentry *dentry, unsigned int sigen)
9828 +{
9829 +       int err;
9830 +
9831 +       err = 0;
9832 +       if (unlikely(au_digen(dentry) != sigen
9833 +                    || au_iigen_test(d_inode(dentry), sigen)))
9834 +               err = -EIO;
9835 +
9836 +       return err;
9837 +}
9838 +
9839 +void au_update_digen(struct dentry *dentry)
9840 +{
9841 +       atomic_set(&au_di(dentry)->di_generation, au_sigen(dentry->d_sb));
9842 +       /* smp_mb(); */ /* atomic_set */
9843 +}
9844 +
9845 +void au_update_dbrange(struct dentry *dentry, int do_put_zero)
9846 +{
9847 +       struct au_dinfo *dinfo;
9848 +       struct dentry *h_d;
9849 +       struct au_hdentry *hdp;
9850 +       aufs_bindex_t bindex, bbot;
9851 +
9852 +       DiMustWriteLock(dentry);
9853 +
9854 +       dinfo = au_di(dentry);
9855 +       if (!dinfo || dinfo->di_btop < 0)
9856 +               return;
9857 +
9858 +       if (do_put_zero) {
9859 +               bbot = dinfo->di_bbot;
9860 +               bindex = dinfo->di_btop;
9861 +               hdp = au_hdentry(dinfo, bindex);
9862 +               for (; bindex <= bbot; bindex++, hdp++) {
9863 +                       h_d = hdp->hd_dentry;
9864 +                       if (h_d && d_is_negative(h_d))
9865 +                               au_set_h_dptr(dentry, bindex, NULL);
9866 +               }
9867 +       }
9868 +
9869 +       dinfo->di_btop = 0;
9870 +       hdp = au_hdentry(dinfo, dinfo->di_btop);
9871 +       for (; dinfo->di_btop <= dinfo->di_bbot; dinfo->di_btop++, hdp++)
9872 +               if (hdp->hd_dentry)
9873 +                       break;
9874 +       if (dinfo->di_btop > dinfo->di_bbot) {
9875 +               dinfo->di_btop = -1;
9876 +               dinfo->di_bbot = -1;
9877 +               return;
9878 +       }
9879 +
9880 +       hdp = au_hdentry(dinfo, dinfo->di_bbot);
9881 +       for (; dinfo->di_bbot >= 0; dinfo->di_bbot--, hdp--)
9882 +               if (hdp->hd_dentry)
9883 +                       break;
9884 +       AuDebugOn(dinfo->di_btop > dinfo->di_bbot || dinfo->di_bbot < 0);
9885 +}
9886 +
9887 +void au_update_dbtop(struct dentry *dentry)
9888 +{
9889 +       aufs_bindex_t bindex, bbot;
9890 +       struct dentry *h_dentry;
9891 +
9892 +       bbot = au_dbbot(dentry);
9893 +       for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++) {
9894 +               h_dentry = au_h_dptr(dentry, bindex);
9895 +               if (!h_dentry)
9896 +                       continue;
9897 +               if (d_is_positive(h_dentry)) {
9898 +                       au_set_dbtop(dentry, bindex);
9899 +                       return;
9900 +               }
9901 +               au_set_h_dptr(dentry, bindex, NULL);
9902 +       }
9903 +}
9904 +
9905 +void au_update_dbbot(struct dentry *dentry)
9906 +{
9907 +       aufs_bindex_t bindex, btop;
9908 +       struct dentry *h_dentry;
9909 +
9910 +       btop = au_dbtop(dentry);
9911 +       for (bindex = au_dbbot(dentry); bindex >= btop; bindex--) {
9912 +               h_dentry = au_h_dptr(dentry, bindex);
9913 +               if (!h_dentry)
9914 +                       continue;
9915 +               if (d_is_positive(h_dentry)) {
9916 +                       au_set_dbbot(dentry, bindex);
9917 +                       return;
9918 +               }
9919 +               au_set_h_dptr(dentry, bindex, NULL);
9920 +       }
9921 +}
9922 +
9923 +int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry)
9924 +{
9925 +       aufs_bindex_t bindex, bbot;
9926 +
9927 +       bbot = au_dbbot(dentry);
9928 +       for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++)
9929 +               if (au_h_dptr(dentry, bindex) == h_dentry)
9930 +                       return bindex;
9931 +       return -1;
9932 +}
9933 diff -urN /usr/share/empty/fs/aufs/dir.c linux/fs/aufs/dir.c
9934 --- /usr/share/empty/fs/aufs/dir.c      1970-01-01 01:00:00.000000000 +0100
9935 +++ linux/fs/aufs/dir.c 2022-03-21 14:49:05.719966344 +0100
9936 @@ -0,0 +1,765 @@
9937 +// SPDX-License-Identifier: GPL-2.0
9938 +/*
9939 + * Copyright (C) 2005-2021 Junjiro R. Okajima
9940 + *
9941 + * This program, aufs is free software; you can redistribute it and/or modify
9942 + * it under the terms of the GNU General Public License as published by
9943 + * the Free Software Foundation; either version 2 of the License, or
9944 + * (at your option) any later version.
9945 + *
9946 + * This program is distributed in the hope that it will be useful,
9947 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
9948 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9949 + * GNU General Public License for more details.
9950 + *
9951 + * You should have received a copy of the GNU General Public License
9952 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
9953 + */
9954 +
9955 +/*
9956 + * directory operations
9957 + */
9958 +
9959 +#include <linux/fs_stack.h>
9960 +#include <linux/iversion.h>
9961 +#include "aufs.h"
9962 +
9963 +void au_add_nlink(struct inode *dir, struct inode *h_dir)
9964 +{
9965 +       unsigned int nlink;
9966 +
9967 +       AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
9968 +
9969 +       nlink = dir->i_nlink;
9970 +       nlink += h_dir->i_nlink - 2;
9971 +       if (h_dir->i_nlink < 2)
9972 +               nlink += 2;
9973 +       smp_mb(); /* for i_nlink */
9974 +       /* 0 can happen in revaliding */
9975 +       set_nlink(dir, nlink);
9976 +}
9977 +
9978 +void au_sub_nlink(struct inode *dir, struct inode *h_dir)
9979 +{
9980 +       unsigned int nlink;
9981 +
9982 +       AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
9983 +
9984 +       nlink = dir->i_nlink;
9985 +       nlink -= h_dir->i_nlink - 2;
9986 +       if (h_dir->i_nlink < 2)
9987 +               nlink -= 2;
9988 +       smp_mb(); /* for i_nlink */
9989 +       /* nlink == 0 means the branch-fs is broken */
9990 +       set_nlink(dir, nlink);
9991 +}
9992 +
9993 +loff_t au_dir_size(struct file *file, struct dentry *dentry)
9994 +{
9995 +       loff_t sz;
9996 +       aufs_bindex_t bindex, bbot;
9997 +       struct file *h_file;
9998 +       struct dentry *h_dentry;
9999 +
10000 +       sz = 0;
10001 +       if (file) {
10002 +               AuDebugOn(!d_is_dir(file->f_path.dentry));
10003 +
10004 +               bbot = au_fbbot_dir(file);
10005 +               for (bindex = au_fbtop(file);
10006 +                    bindex <= bbot && sz < KMALLOC_MAX_SIZE;
10007 +                    bindex++) {
10008 +                       h_file = au_hf_dir(file, bindex);
10009 +                       if (h_file && file_inode(h_file))
10010 +                               sz += vfsub_f_size_read(h_file);
10011 +               }
10012 +       } else {
10013 +               AuDebugOn(!dentry);
10014 +               AuDebugOn(!d_is_dir(dentry));
10015 +
10016 +               bbot = au_dbtaildir(dentry);
10017 +               for (bindex = au_dbtop(dentry);
10018 +                    bindex <= bbot && sz < KMALLOC_MAX_SIZE;
10019 +                    bindex++) {
10020 +                       h_dentry = au_h_dptr(dentry, bindex);
10021 +                       if (h_dentry && d_is_positive(h_dentry))
10022 +                               sz += i_size_read(d_inode(h_dentry));
10023 +               }
10024 +       }
10025 +       if (sz < KMALLOC_MAX_SIZE)
10026 +               sz = roundup_pow_of_two(sz);
10027 +       if (sz > KMALLOC_MAX_SIZE)
10028 +               sz = KMALLOC_MAX_SIZE;
10029 +       else if (sz < NAME_MAX) {
10030 +               BUILD_BUG_ON(AUFS_RDBLK_DEF < NAME_MAX);
10031 +               sz = AUFS_RDBLK_DEF;
10032 +       }
10033 +       return sz;
10034 +}
10035 +
10036 +struct au_dir_ts_arg {
10037 +       struct dentry *dentry;
10038 +       aufs_bindex_t brid;
10039 +};
10040 +
10041 +static void au_do_dir_ts(void *arg)
10042 +{
10043 +       struct au_dir_ts_arg *a = arg;
10044 +       struct au_dtime dt;
10045 +       struct path h_path;
10046 +       struct inode *dir, *h_dir;
10047 +       struct super_block *sb;
10048 +       struct au_branch *br;
10049 +       struct au_hinode *hdir;
10050 +       int err;
10051 +       aufs_bindex_t btop, bindex;
10052 +
10053 +       sb = a->dentry->d_sb;
10054 +       if (d_really_is_negative(a->dentry))
10055 +               goto out;
10056 +       /* no dir->i_mutex lock */
10057 +       aufs_read_lock(a->dentry, AuLock_DW); /* noflush */
10058 +
10059 +       dir = d_inode(a->dentry);
10060 +       btop = au_ibtop(dir);
10061 +       bindex = au_br_index(sb, a->brid);
10062 +       if (bindex < btop)
10063 +               goto out_unlock;
10064 +
10065 +       br = au_sbr(sb, bindex);
10066 +       h_path.dentry = au_h_dptr(a->dentry, bindex);
10067 +       if (!h_path.dentry)
10068 +               goto out_unlock;
10069 +       h_path.mnt = au_br_mnt(br);
10070 +       au_dtime_store(&dt, a->dentry, &h_path);
10071 +
10072 +       br = au_sbr(sb, btop);
10073 +       if (!au_br_writable(br->br_perm))
10074 +               goto out_unlock;
10075 +       h_path.dentry = au_h_dptr(a->dentry, btop);
10076 +       h_path.mnt = au_br_mnt(br);
10077 +       err = vfsub_mnt_want_write(h_path.mnt);
10078 +       if (err)
10079 +               goto out_unlock;
10080 +       hdir = au_hi(dir, btop);
10081 +       au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
10082 +       h_dir = au_h_iptr(dir, btop);
10083 +       if (h_dir->i_nlink
10084 +           && timespec64_compare(&h_dir->i_mtime, &dt.dt_mtime) < 0) {
10085 +               dt.dt_h_path = h_path;
10086 +               au_dtime_revert(&dt);
10087 +       }
10088 +       au_hn_inode_unlock(hdir);
10089 +       vfsub_mnt_drop_write(h_path.mnt);
10090 +       au_cpup_attr_timesizes(dir);
10091 +
10092 +out_unlock:
10093 +       aufs_read_unlock(a->dentry, AuLock_DW);
10094 +out:
10095 +       dput(a->dentry);
10096 +       au_nwt_done(&au_sbi(sb)->si_nowait);
10097 +       au_kfree_try_rcu(arg);
10098 +}
10099 +
10100 +void au_dir_ts(struct inode *dir, aufs_bindex_t bindex)
10101 +{
10102 +       int perm, wkq_err;
10103 +       aufs_bindex_t btop;
10104 +       struct au_dir_ts_arg *arg;
10105 +       struct dentry *dentry;
10106 +       struct super_block *sb;
10107 +
10108 +       IMustLock(dir);
10109 +
10110 +       dentry = d_find_any_alias(dir);
10111 +       AuDebugOn(!dentry);
10112 +       sb = dentry->d_sb;
10113 +       btop = au_ibtop(dir);
10114 +       if (btop == bindex) {
10115 +               au_cpup_attr_timesizes(dir);
10116 +               goto out;
10117 +       }
10118 +
10119 +       perm = au_sbr_perm(sb, btop);
10120 +       if (!au_br_writable(perm))
10121 +               goto out;
10122 +
10123 +       arg = kmalloc(sizeof(*arg), GFP_NOFS);
10124 +       if (!arg)
10125 +               goto out;
10126 +
10127 +       arg->dentry = dget(dentry); /* will be dput-ted by au_do_dir_ts() */
10128 +       arg->brid = au_sbr_id(sb, bindex);
10129 +       wkq_err = au_wkq_nowait(au_do_dir_ts, arg, sb, /*flags*/0);
10130 +       if (unlikely(wkq_err)) {
10131 +               pr_err("wkq %d\n", wkq_err);
10132 +               dput(dentry);
10133 +               au_kfree_try_rcu(arg);
10134 +       }
10135 +
10136 +out:
10137 +       dput(dentry);
10138 +}
10139 +
10140 +/* ---------------------------------------------------------------------- */
10141 +
10142 +static int reopen_dir(struct file *file)
10143 +{
10144 +       int err;
10145 +       unsigned int flags;
10146 +       aufs_bindex_t bindex, btail, btop;
10147 +       struct dentry *dentry, *h_dentry;
10148 +       struct file *h_file;
10149 +
10150 +       /* open all lower dirs */
10151 +       dentry = file->f_path.dentry;
10152 +       btop = au_dbtop(dentry);
10153 +       for (bindex = au_fbtop(file); bindex < btop; bindex++)
10154 +               au_set_h_fptr(file, bindex, NULL);
10155 +       au_set_fbtop(file, btop);
10156 +
10157 +       btail = au_dbtaildir(dentry);
10158 +       for (bindex = au_fbbot_dir(file); btail < bindex; bindex--)
10159 +               au_set_h_fptr(file, bindex, NULL);
10160 +       au_set_fbbot_dir(file, btail);
10161 +
10162 +       flags = vfsub_file_flags(file);
10163 +       for (bindex = btop; bindex <= btail; bindex++) {
10164 +               h_dentry = au_h_dptr(dentry, bindex);
10165 +               if (!h_dentry)
10166 +                       continue;
10167 +               h_file = au_hf_dir(file, bindex);
10168 +               if (h_file)
10169 +                       continue;
10170 +
10171 +               h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
10172 +               err = PTR_ERR(h_file);
10173 +               if (IS_ERR(h_file))
10174 +                       goto out; /* close all? */
10175 +               au_set_h_fptr(file, bindex, h_file);
10176 +       }
10177 +       au_update_figen(file);
10178 +       /* todo: necessary? */
10179 +       /* file->f_ra = h_file->f_ra; */
10180 +       err = 0;
10181 +
10182 +out:
10183 +       return err;
10184 +}
10185 +
10186 +static int do_open_dir(struct file *file, int flags, struct file *h_file)
10187 +{
10188 +       int err;
10189 +       aufs_bindex_t bindex, btail;
10190 +       struct dentry *dentry, *h_dentry;
10191 +       struct vfsmount *mnt;
10192 +
10193 +       FiMustWriteLock(file);
10194 +       AuDebugOn(h_file);
10195 +
10196 +       err = 0;
10197 +       mnt = file->f_path.mnt;
10198 +       dentry = file->f_path.dentry;
10199 +       file->f_version = inode_query_iversion(d_inode(dentry));
10200 +       bindex = au_dbtop(dentry);
10201 +       au_set_fbtop(file, bindex);
10202 +       btail = au_dbtaildir(dentry);
10203 +       au_set_fbbot_dir(file, btail);
10204 +       for (; !err && bindex <= btail; bindex++) {
10205 +               h_dentry = au_h_dptr(dentry, bindex);
10206 +               if (!h_dentry)
10207 +                       continue;
10208 +
10209 +               err = vfsub_test_mntns(mnt, h_dentry->d_sb);
10210 +               if (unlikely(err))
10211 +                       break;
10212 +               h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
10213 +               if (IS_ERR(h_file)) {
10214 +                       err = PTR_ERR(h_file);
10215 +                       break;
10216 +               }
10217 +               au_set_h_fptr(file, bindex, h_file);
10218 +       }
10219 +       au_update_figen(file);
10220 +       /* todo: necessary? */
10221 +       /* file->f_ra = h_file->f_ra; */
10222 +       if (!err)
10223 +               return 0; /* success */
10224 +
10225 +       /* close all */
10226 +       for (bindex = au_fbtop(file); bindex <= btail; bindex++)
10227 +               au_set_h_fptr(file, bindex, NULL);
10228 +       au_set_fbtop(file, -1);
10229 +       au_set_fbbot_dir(file, -1);
10230 +
10231 +       return err;
10232 +}
10233 +
10234 +static int aufs_open_dir(struct inode *inode __maybe_unused,
10235 +                        struct file *file)
10236 +{
10237 +       int err;
10238 +       struct super_block *sb;
10239 +       struct au_fidir *fidir;
10240 +
10241 +       err = -ENOMEM;
10242 +       sb = file->f_path.dentry->d_sb;
10243 +       si_read_lock(sb, AuLock_FLUSH);
10244 +       fidir = au_fidir_alloc(sb);
10245 +       if (fidir) {
10246 +               struct au_do_open_args args = {
10247 +                       .open   = do_open_dir,
10248 +                       .fidir  = fidir
10249 +               };
10250 +               err = au_do_open(file, &args);
10251 +               if (unlikely(err))
10252 +                       au_kfree_rcu(fidir);
10253 +       }
10254 +       si_read_unlock(sb);
10255 +       return err;
10256 +}
10257 +
10258 +static int aufs_release_dir(struct inode *inode __maybe_unused,
10259 +                           struct file *file)
10260 +{
10261 +       struct au_vdir *vdir_cache;
10262 +       struct au_finfo *finfo;
10263 +       struct au_fidir *fidir;
10264 +       struct au_hfile *hf;
10265 +       aufs_bindex_t bindex, bbot;
10266 +
10267 +       finfo = au_fi(file);
10268 +       fidir = finfo->fi_hdir;
10269 +       if (fidir) {
10270 +               au_hbl_del(&finfo->fi_hlist,
10271 +                          &au_sbi(file->f_path.dentry->d_sb)->si_files);
10272 +               vdir_cache = fidir->fd_vdir_cache; /* lock-free */
10273 +               if (vdir_cache)
10274 +                       au_vdir_free(vdir_cache);
10275 +
10276 +               bindex = finfo->fi_btop;
10277 +               if (bindex >= 0) {
10278 +                       hf = fidir->fd_hfile + bindex;
10279 +                       /*
10280 +                        * calls fput() instead of filp_close(),
10281 +                        * since no dnotify or lock for the lower file.
10282 +                        */
10283 +                       bbot = fidir->fd_bbot;
10284 +                       for (; bindex <= bbot; bindex++, hf++)
10285 +                               if (hf->hf_file)
10286 +                                       au_hfput(hf, /*execed*/0);
10287 +               }
10288 +               au_kfree_rcu(fidir);
10289 +               finfo->fi_hdir = NULL;
10290 +       }
10291 +       au_finfo_fin(file);
10292 +       return 0;
10293 +}
10294 +
10295 +/* ---------------------------------------------------------------------- */
10296 +
10297 +static int au_do_flush_dir(struct file *file, fl_owner_t id)
10298 +{
10299 +       int err;
10300 +       aufs_bindex_t bindex, bbot;
10301 +       struct file *h_file;
10302 +
10303 +       err = 0;
10304 +       bbot = au_fbbot_dir(file);
10305 +       for (bindex = au_fbtop(file); !err && bindex <= bbot; bindex++) {
10306 +               h_file = au_hf_dir(file, bindex);
10307 +               if (h_file)
10308 +                       err = vfsub_flush(h_file, id);
10309 +       }
10310 +       return err;
10311 +}
10312 +
10313 +static int aufs_flush_dir(struct file *file, fl_owner_t id)
10314 +{
10315 +       return au_do_flush(file, id, au_do_flush_dir);
10316 +}
10317 +
10318 +/* ---------------------------------------------------------------------- */
10319 +
10320 +static int au_do_fsync_dir_no_file(struct dentry *dentry, int datasync)
10321 +{
10322 +       int err;
10323 +       aufs_bindex_t bbot, bindex;
10324 +       struct inode *inode;
10325 +       struct super_block *sb;
10326 +
10327 +       err = 0;
10328 +       sb = dentry->d_sb;
10329 +       inode = d_inode(dentry);
10330 +       IMustLock(inode);
10331 +       bbot = au_dbbot(dentry);
10332 +       for (bindex = au_dbtop(dentry); !err && bindex <= bbot; bindex++) {
10333 +               struct path h_path;
10334 +
10335 +               if (au_test_ro(sb, bindex, inode))
10336 +                       continue;
10337 +               h_path.dentry = au_h_dptr(dentry, bindex);
10338 +               if (!h_path.dentry)
10339 +                       continue;
10340 +
10341 +               h_path.mnt = au_sbr_mnt(sb, bindex);
10342 +               err = vfsub_fsync(NULL, &h_path, datasync);
10343 +       }
10344 +
10345 +       return err;
10346 +}
10347 +
10348 +static int au_do_fsync_dir(struct file *file, int datasync)
10349 +{
10350 +       int err;
10351 +       aufs_bindex_t bbot, bindex;
10352 +       struct file *h_file;
10353 +       struct super_block *sb;
10354 +       struct inode *inode;
10355 +
10356 +       err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1, /*fi_lsc*/0);
10357 +       if (unlikely(err))
10358 +               goto out;
10359 +
10360 +       inode = file_inode(file);
10361 +       sb = inode->i_sb;
10362 +       bbot = au_fbbot_dir(file);
10363 +       for (bindex = au_fbtop(file); !err && bindex <= bbot; bindex++) {
10364 +               h_file = au_hf_dir(file, bindex);
10365 +               if (!h_file || au_test_ro(sb, bindex, inode))
10366 +                       continue;
10367 +
10368 +               err = vfsub_fsync(h_file, &h_file->f_path, datasync);
10369 +       }
10370 +
10371 +out:
10372 +       return err;
10373 +}
10374 +
10375 +/*
10376 + * @file may be NULL
10377 + */
10378 +static int aufs_fsync_dir(struct file *file, loff_t start, loff_t end,
10379 +                         int datasync)
10380 +{
10381 +       int err;
10382 +       struct dentry *dentry;
10383 +       struct inode *inode;
10384 +       struct super_block *sb;
10385 +
10386 +       err = 0;
10387 +       dentry = file->f_path.dentry;
10388 +       inode = d_inode(dentry);
10389 +       inode_lock(inode);
10390 +       sb = dentry->d_sb;
10391 +       si_noflush_read_lock(sb);
10392 +       if (file)
10393 +               err = au_do_fsync_dir(file, datasync);
10394 +       else {
10395 +               di_write_lock_child(dentry);
10396 +               err = au_do_fsync_dir_no_file(dentry, datasync);
10397 +       }
10398 +       au_cpup_attr_timesizes(inode);
10399 +       di_write_unlock(dentry);
10400 +       if (file)
10401 +               fi_write_unlock(file);
10402 +
10403 +       si_read_unlock(sb);
10404 +       inode_unlock(inode);
10405 +       return err;
10406 +}
10407 +
10408 +/* ---------------------------------------------------------------------- */
10409 +
10410 +static int aufs_iterate_shared(struct file *file, struct dir_context *ctx)
10411 +{
10412 +       int err;
10413 +       struct dentry *dentry;
10414 +       struct inode *inode, *h_inode;
10415 +       struct super_block *sb;
10416 +
10417 +       AuDbg("%pD, ctx{%ps, %llu}\n", file, ctx->actor, ctx->pos);
10418 +
10419 +       dentry = file->f_path.dentry;
10420 +       inode = d_inode(dentry);
10421 +       IMustLock(inode);
10422 +
10423 +       sb = dentry->d_sb;
10424 +       si_read_lock(sb, AuLock_FLUSH);
10425 +       err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1, /*fi_lsc*/0);
10426 +       if (unlikely(err))
10427 +               goto out;
10428 +       err = au_alive_dir(dentry);
10429 +       if (!err)
10430 +               err = au_vdir_init(file);
10431 +       di_downgrade_lock(dentry, AuLock_IR);
10432 +       if (unlikely(err))
10433 +               goto out_unlock;
10434 +
10435 +       h_inode = au_h_iptr(inode, au_ibtop(inode));
10436 +       if (!au_test_nfsd()) {
10437 +               err = au_vdir_fill_de(file, ctx);
10438 +               fsstack_copy_attr_atime(inode, h_inode);
10439 +       } else {
10440 +               /*
10441 +                * nfsd filldir may call lookup_one_len(), vfs_getattr(),
10442 +                * encode_fh() and others.
10443 +                */
10444 +               atomic_inc(&h_inode->i_count);
10445 +               di_read_unlock(dentry, AuLock_IR);
10446 +               si_read_unlock(sb);
10447 +               err = au_vdir_fill_de(file, ctx);
10448 +               fsstack_copy_attr_atime(inode, h_inode);
10449 +               fi_write_unlock(file);
10450 +               iput(h_inode);
10451 +
10452 +               AuTraceErr(err);
10453 +               return err;
10454 +       }
10455 +
10456 +out_unlock:
10457 +       di_read_unlock(dentry, AuLock_IR);
10458 +       fi_write_unlock(file);
10459 +out:
10460 +       si_read_unlock(sb);
10461 +       return err;
10462 +}
10463 +
10464 +/* ---------------------------------------------------------------------- */
10465 +
10466 +#define AuTestEmpty_WHONLY     1
10467 +#define AuTestEmpty_CALLED     (1 << 1)
10468 +#define AuTestEmpty_SHWH       (1 << 2)
10469 +#define au_ftest_testempty(flags, name)        ((flags) & AuTestEmpty_##name)
10470 +#define au_fset_testempty(flags, name) \
10471 +       do { (flags) |= AuTestEmpty_##name; } while (0)
10472 +#define au_fclr_testempty(flags, name) \
10473 +       do { (flags) &= ~AuTestEmpty_##name; } while (0)
10474 +
10475 +#ifndef CONFIG_AUFS_SHWH
10476 +#undef AuTestEmpty_SHWH
10477 +#define AuTestEmpty_SHWH       0
10478 +#endif
10479 +
10480 +struct test_empty_arg {
10481 +       struct dir_context ctx;
10482 +       struct au_nhash *whlist;
10483 +       unsigned int flags;
10484 +       int err;
10485 +       aufs_bindex_t bindex;
10486 +};
10487 +
10488 +static int test_empty_cb(struct dir_context *ctx, const char *__name,
10489 +                        int namelen, loff_t offset __maybe_unused, u64 ino,
10490 +                        unsigned int d_type)
10491 +{
10492 +       struct test_empty_arg *arg = container_of(ctx, struct test_empty_arg,
10493 +                                                 ctx);
10494 +       char *name = (void *)__name;
10495 +
10496 +       arg->err = 0;
10497 +       au_fset_testempty(arg->flags, CALLED);
10498 +       /* smp_mb(); */
10499 +       if (name[0] == '.'
10500 +           && (namelen == 1 || (name[1] == '.' && namelen == 2)))
10501 +               goto out; /* success */
10502 +
10503 +       if (namelen <= AUFS_WH_PFX_LEN
10504 +           || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
10505 +               if (au_ftest_testempty(arg->flags, WHONLY)
10506 +                   && !au_nhash_test_known_wh(arg->whlist, name, namelen))
10507 +                       arg->err = -ENOTEMPTY;
10508 +               goto out;
10509 +       }
10510 +
10511 +       name += AUFS_WH_PFX_LEN;
10512 +       namelen -= AUFS_WH_PFX_LEN;
10513 +       if (!au_nhash_test_known_wh(arg->whlist, name, namelen))
10514 +               arg->err = au_nhash_append_wh
10515 +                       (arg->whlist, name, namelen, ino, d_type, arg->bindex,
10516 +                        au_ftest_testempty(arg->flags, SHWH));
10517 +
10518 +out:
10519 +       /* smp_mb(); */
10520 +       AuTraceErr(arg->err);
10521 +       return arg->err;
10522 +}
10523 +
10524 +static int do_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
10525 +{
10526 +       int err;
10527 +       struct file *h_file;
10528 +       struct au_branch *br;
10529 +
10530 +       h_file = au_h_open(dentry, arg->bindex,
10531 +                          O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_LARGEFILE,
10532 +                          /*file*/NULL, /*force_wr*/0);
10533 +       err = PTR_ERR(h_file);
10534 +       if (IS_ERR(h_file))
10535 +               goto out;
10536 +
10537 +       err = 0;
10538 +       if (!au_opt_test(au_mntflags(dentry->d_sb), UDBA_NONE)
10539 +           && !file_inode(h_file)->i_nlink)
10540 +               goto out_put;
10541 +
10542 +       do {
10543 +               arg->err = 0;
10544 +               au_fclr_testempty(arg->flags, CALLED);
10545 +               /* smp_mb(); */
10546 +               err = vfsub_iterate_dir(h_file, &arg->ctx);
10547 +               if (err >= 0)
10548 +                       err = arg->err;
10549 +       } while (!err && au_ftest_testempty(arg->flags, CALLED));
10550 +
10551 +out_put:
10552 +       fput(h_file);
10553 +       br = au_sbr(dentry->d_sb, arg->bindex);
10554 +       au_lcnt_dec(&br->br_nfiles);
10555 +out:
10556 +       return err;
10557 +}
10558 +
10559 +struct do_test_empty_args {
10560 +       int *errp;
10561 +       struct dentry *dentry;
10562 +       struct test_empty_arg *arg;
10563 +};
10564 +
10565 +static void call_do_test_empty(void *args)
10566 +{
10567 +       struct do_test_empty_args *a = args;
10568 +       *a->errp = do_test_empty(a->dentry, a->arg);
10569 +}
10570 +
10571 +static int sio_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
10572 +{
10573 +       int err, wkq_err;
10574 +       struct dentry *h_dentry;
10575 +       struct inode *h_inode;
10576 +       struct user_namespace *h_userns;
10577 +
10578 +       h_userns = au_sbr_userns(dentry->d_sb, arg->bindex);
10579 +       h_dentry = au_h_dptr(dentry, arg->bindex);
10580 +       h_inode = d_inode(h_dentry);
10581 +       /* todo: i_mode changes anytime? */
10582 +       inode_lock_shared_nested(h_inode, AuLsc_I_CHILD);
10583 +       err = au_test_h_perm_sio(h_userns, h_inode, MAY_EXEC | MAY_READ);
10584 +       inode_unlock_shared(h_inode);
10585 +       if (!err)
10586 +               err = do_test_empty(dentry, arg);
10587 +       else {
10588 +               struct do_test_empty_args args = {
10589 +                       .errp   = &err,
10590 +                       .dentry = dentry,
10591 +                       .arg    = arg
10592 +               };
10593 +               unsigned int flags = arg->flags;
10594 +
10595 +               wkq_err = au_wkq_wait(call_do_test_empty, &args);
10596 +               if (unlikely(wkq_err))
10597 +                       err = wkq_err;
10598 +               arg->flags = flags;
10599 +       }
10600 +
10601 +       return err;
10602 +}
10603 +
10604 +int au_test_empty_lower(struct dentry *dentry)
10605 +{
10606 +       int err;
10607 +       unsigned int rdhash;
10608 +       aufs_bindex_t bindex, btop, btail;
10609 +       struct au_nhash whlist;
10610 +       struct test_empty_arg arg = {
10611 +               .ctx = {
10612 +                       .actor = test_empty_cb
10613 +               }
10614 +       };
10615 +       int (*test_empty)(struct dentry *dentry, struct test_empty_arg *arg);
10616 +
10617 +       SiMustAnyLock(dentry->d_sb);
10618 +
10619 +       rdhash = au_sbi(dentry->d_sb)->si_rdhash;
10620 +       if (!rdhash)
10621 +               rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, dentry));
10622 +       err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
10623 +       if (unlikely(err))
10624 +               goto out;
10625 +
10626 +       arg.flags = 0;
10627 +       arg.whlist = &whlist;
10628 +       btop = au_dbtop(dentry);
10629 +       if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
10630 +               au_fset_testempty(arg.flags, SHWH);
10631 +       test_empty = do_test_empty;
10632 +       if (au_opt_test(au_mntflags(dentry->d_sb), DIRPERM1))
10633 +               test_empty = sio_test_empty;
10634 +       arg.bindex = btop;
10635 +       err = test_empty(dentry, &arg);
10636 +       if (unlikely(err))
10637 +               goto out_whlist;
10638 +
10639 +       au_fset_testempty(arg.flags, WHONLY);
10640 +       btail = au_dbtaildir(dentry);
10641 +       for (bindex = btop + 1; !err && bindex <= btail; bindex++) {
10642 +               struct dentry *h_dentry;
10643 +
10644 +               h_dentry = au_h_dptr(dentry, bindex);
10645 +               if (h_dentry && d_is_positive(h_dentry)) {
10646 +                       arg.bindex = bindex;
10647 +                       err = test_empty(dentry, &arg);
10648 +               }
10649 +       }
10650 +
10651 +out_whlist:
10652 +       au_nhash_wh_free(&whlist);
10653 +out:
10654 +       return err;
10655 +}
10656 +
10657 +int au_test_empty(struct dentry *dentry, struct au_nhash *whlist)
10658 +{
10659 +       int err;
10660 +       struct test_empty_arg arg = {
10661 +               .ctx = {
10662 +                       .actor = test_empty_cb
10663 +               }
10664 +       };
10665 +       aufs_bindex_t bindex, btail;
10666 +
10667 +       err = 0;
10668 +       arg.whlist = whlist;
10669 +       arg.flags = AuTestEmpty_WHONLY;
10670 +       if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
10671 +               au_fset_testempty(arg.flags, SHWH);
10672 +       btail = au_dbtaildir(dentry);
10673 +       for (bindex = au_dbtop(dentry); !err && bindex <= btail; bindex++) {
10674 +               struct dentry *h_dentry;
10675 +
10676 +               h_dentry = au_h_dptr(dentry, bindex);
10677 +               if (h_dentry && d_is_positive(h_dentry)) {
10678 +                       arg.bindex = bindex;
10679 +                       err = sio_test_empty(dentry, &arg);
10680 +               }
10681 +       }
10682 +
10683 +       return err;
10684 +}
10685 +
10686 +/* ---------------------------------------------------------------------- */
10687 +
10688 +const struct file_operations aufs_dir_fop = {
10689 +       .owner          = THIS_MODULE,
10690 +       .llseek         = default_llseek,
10691 +       .read           = generic_read_dir,
10692 +       .iterate_shared = aufs_iterate_shared,
10693 +       .unlocked_ioctl = aufs_ioctl_dir,
10694 +#ifdef CONFIG_COMPAT
10695 +       .compat_ioctl   = aufs_compat_ioctl_dir,
10696 +#endif
10697 +       .open           = aufs_open_dir,
10698 +       .release        = aufs_release_dir,
10699 +       .flush          = aufs_flush_dir,
10700 +       .fsync          = aufs_fsync_dir
10701 +};
10702 diff -urN /usr/share/empty/fs/aufs/dir.h linux/fs/aufs/dir.h
10703 --- /usr/share/empty/fs/aufs/dir.h      1970-01-01 01:00:00.000000000 +0100
10704 +++ linux/fs/aufs/dir.h 2022-03-21 14:49:05.719966344 +0100
10705 @@ -0,0 +1,134 @@
10706 +/* SPDX-License-Identifier: GPL-2.0 */
10707 +/*
10708 + * Copyright (C) 2005-2021 Junjiro R. Okajima
10709 + *
10710 + * This program, aufs is free software; you can redistribute it and/or modify
10711 + * it under the terms of the GNU General Public License as published by
10712 + * the Free Software Foundation; either version 2 of the License, or
10713 + * (at your option) any later version.
10714 + *
10715 + * This program is distributed in the hope that it will be useful,
10716 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
10717 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10718 + * GNU General Public License for more details.
10719 + *
10720 + * You should have received a copy of the GNU General Public License
10721 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
10722 + */
10723 +
10724 +/*
10725 + * directory operations
10726 + */
10727 +
10728 +#ifndef __AUFS_DIR_H__
10729 +#define __AUFS_DIR_H__
10730 +
10731 +#ifdef __KERNEL__
10732 +
10733 +#include <linux/fs.h>
10734 +
10735 +/* ---------------------------------------------------------------------- */
10736 +
10737 +/* need to be faster and smaller */
10738 +
10739 +struct au_nhash {
10740 +       unsigned int            nh_num;
10741 +       struct hlist_head       *nh_head;
10742 +};
10743 +
10744 +struct au_vdir_destr {
10745 +       unsigned char   len;
10746 +       unsigned char   name[];
10747 +} __packed;
10748 +
10749 +struct au_vdir_dehstr {
10750 +       struct hlist_node       hash;
10751 +       struct au_vdir_destr    *str;
10752 +       struct rcu_head         rcu;
10753 +} ____cacheline_aligned_in_smp;
10754 +
10755 +struct au_vdir_de {
10756 +       ino_t                   de_ino;
10757 +       unsigned char           de_type;
10758 +       /* caution: packed */
10759 +       struct au_vdir_destr    de_str;
10760 +} __packed;
10761 +
10762 +struct au_vdir_wh {
10763 +       struct hlist_node       wh_hash;
10764 +#ifdef CONFIG_AUFS_SHWH
10765 +       ino_t                   wh_ino;
10766 +       aufs_bindex_t           wh_bindex;
10767 +       unsigned char           wh_type;
10768 +#else
10769 +       aufs_bindex_t           wh_bindex;
10770 +#endif
10771 +       /* caution: packed */
10772 +       struct au_vdir_destr    wh_str;
10773 +} __packed;
10774 +
10775 +union au_vdir_deblk_p {
10776 +       unsigned char           *deblk;
10777 +       struct au_vdir_de       *de;
10778 +};
10779 +
10780 +struct au_vdir {
10781 +       unsigned char   **vd_deblk;
10782 +       unsigned long   vd_nblk;
10783 +       struct {
10784 +               unsigned long           ul;
10785 +               union au_vdir_deblk_p   p;
10786 +       } vd_last;
10787 +
10788 +       u64             vd_version;
10789 +       unsigned int    vd_deblk_sz;
10790 +       unsigned long   vd_jiffy;
10791 +       struct rcu_head rcu;
10792 +} ____cacheline_aligned_in_smp;
10793 +
10794 +/* ---------------------------------------------------------------------- */
10795 +
10796 +/* dir.c */
10797 +extern const struct file_operations aufs_dir_fop;
10798 +void au_add_nlink(struct inode *dir, struct inode *h_dir);
10799 +void au_sub_nlink(struct inode *dir, struct inode *h_dir);
10800 +loff_t au_dir_size(struct file *file, struct dentry *dentry);
10801 +void au_dir_ts(struct inode *dir, aufs_bindex_t bsrc);
10802 +int au_test_empty_lower(struct dentry *dentry);
10803 +int au_test_empty(struct dentry *dentry, struct au_nhash *whlist);
10804 +
10805 +/* vdir.c */
10806 +unsigned int au_rdhash_est(loff_t sz);
10807 +int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp);
10808 +void au_nhash_wh_free(struct au_nhash *whlist);
10809 +int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
10810 +                           int limit);
10811 +int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen);
10812 +int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
10813 +                      unsigned int d_type, aufs_bindex_t bindex,
10814 +                      unsigned char shwh);
10815 +void au_vdir_free(struct au_vdir *vdir);
10816 +int au_vdir_init(struct file *file);
10817 +int au_vdir_fill_de(struct file *file, struct dir_context *ctx);
10818 +
10819 +/* ioctl.c */
10820 +long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg);
10821 +
10822 +#ifdef CONFIG_AUFS_RDU
10823 +/* rdu.c */
10824 +long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
10825 +#ifdef CONFIG_COMPAT
10826 +long au_rdu_compat_ioctl(struct file *file, unsigned int cmd,
10827 +                        unsigned long arg);
10828 +#endif
10829 +#else
10830 +AuStub(long, au_rdu_ioctl, return -EINVAL, struct file *file,
10831 +       unsigned int cmd, unsigned long arg)
10832 +#ifdef CONFIG_COMPAT
10833 +AuStub(long, au_rdu_compat_ioctl, return -EINVAL, struct file *file,
10834 +       unsigned int cmd, unsigned long arg)
10835 +#endif
10836 +#endif
10837 +
10838 +#endif /* __KERNEL__ */
10839 +#endif /* __AUFS_DIR_H__ */
10840 diff -urN /usr/share/empty/fs/aufs/dirren.c linux/fs/aufs/dirren.c
10841 --- /usr/share/empty/fs/aufs/dirren.c   1970-01-01 01:00:00.000000000 +0100
10842 +++ linux/fs/aufs/dirren.c      2022-03-21 14:49:05.723299677 +0100
10843 @@ -0,0 +1,1315 @@
10844 +// SPDX-License-Identifier: GPL-2.0
10845 +/*
10846 + * Copyright (C) 2017-2021 Junjiro R. Okajima
10847 + *
10848 + * This program, aufs is free software; you can redistribute it and/or modify
10849 + * it under the terms of the GNU General Public License as published by
10850 + * the Free Software Foundation; either version 2 of the License, or
10851 + * (at your option) any later version.
10852 + *
10853 + * This program is distributed in the hope that it will be useful,
10854 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
10855 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10856 + * GNU General Public License for more details.
10857 + *
10858 + * You should have received a copy of the GNU General Public License
10859 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
10860 + */
10861 +
10862 +/*
10863 + * special handling in renaming a directory
10864 + * in order to support looking-up the before-renamed name on the lower readonly
10865 + * branches
10866 + */
10867 +
10868 +#include <linux/byteorder/generic.h>
10869 +#include "aufs.h"
10870 +
10871 +static void au_dr_hino_del(struct au_dr_br *dr, struct au_dr_hino *ent)
10872 +{
10873 +       int idx;
10874 +
10875 +       idx = au_dr_ihash(ent->dr_h_ino);
10876 +       au_hbl_del(&ent->dr_hnode, dr->dr_h_ino + idx);
10877 +}
10878 +
10879 +static int au_dr_hino_test_empty(struct au_dr_br *dr)
10880 +{
10881 +       int ret, i;
10882 +       struct hlist_bl_head *hbl;
10883 +
10884 +       ret = 1;
10885 +       for (i = 0; ret && i < AuDirren_NHASH; i++) {
10886 +               hbl = dr->dr_h_ino + i;
10887 +               hlist_bl_lock(hbl);
10888 +               ret &= hlist_bl_empty(hbl);
10889 +               hlist_bl_unlock(hbl);
10890 +       }
10891 +
10892 +       return ret;
10893 +}
10894 +
10895 +static struct au_dr_hino *au_dr_hino_find(struct au_dr_br *dr, ino_t ino)
10896 +{
10897 +       struct au_dr_hino *found, *ent;
10898 +       struct hlist_bl_head *hbl;
10899 +       struct hlist_bl_node *pos;
10900 +       int idx;
10901 +
10902 +       found = NULL;
10903 +       idx = au_dr_ihash(ino);
10904 +       hbl = dr->dr_h_ino + idx;
10905 +       hlist_bl_lock(hbl);
10906 +       hlist_bl_for_each_entry(ent, pos, hbl, dr_hnode)
10907 +               if (ent->dr_h_ino == ino) {
10908 +                       found = ent;
10909 +                       break;
10910 +               }
10911 +       hlist_bl_unlock(hbl);
10912 +
10913 +       return found;
10914 +}
10915 +
10916 +int au_dr_hino_test_add(struct au_dr_br *dr, ino_t ino,
10917 +                       struct au_dr_hino *add_ent)
10918 +{
10919 +       int found, idx;
10920 +       struct hlist_bl_head *hbl;
10921 +       struct hlist_bl_node *pos;
10922 +       struct au_dr_hino *ent;
10923 +
10924 +       found = 0;
10925 +       idx = au_dr_ihash(ino);
10926 +       hbl = dr->dr_h_ino + idx;
10927 +#if 0 /* debug print */
10928 +       {
10929 +               struct hlist_bl_node *tmp;
10930 +
10931 +               hlist_bl_for_each_entry_safe(ent, pos, tmp, hbl, dr_hnode)
10932 +                       AuDbg("hi%llu\n", (unsigned long long)ent->dr_h_ino);
10933 +       }
10934 +#endif
10935 +       hlist_bl_lock(hbl);
10936 +       hlist_bl_for_each_entry(ent, pos, hbl, dr_hnode)
10937 +               if (ent->dr_h_ino == ino) {
10938 +                       found = 1;
10939 +                       break;
10940 +               }
10941 +       if (!found && add_ent)
10942 +               hlist_bl_add_head(&add_ent->dr_hnode, hbl);
10943 +       hlist_bl_unlock(hbl);
10944 +
10945 +       if (!found && add_ent)
10946 +               AuDbg("i%llu added\n", (unsigned long long)add_ent->dr_h_ino);
10947 +
10948 +       return found;
10949 +}
10950 +
10951 +void au_dr_hino_free(struct au_dr_br *dr)
10952 +{
10953 +       int i;
10954 +       struct hlist_bl_head *hbl;
10955 +       struct hlist_bl_node *pos, *tmp;
10956 +       struct au_dr_hino *ent;
10957 +
10958 +       /* SiMustWriteLock(sb); */
10959 +
10960 +       for (i = 0; i < AuDirren_NHASH; i++) {
10961 +               hbl = dr->dr_h_ino + i;
10962 +               /* no spinlock since sbinfo must be write-locked */
10963 +               hlist_bl_for_each_entry_safe(ent, pos, tmp, hbl, dr_hnode)
10964 +                       au_kfree_rcu(ent);
10965 +               INIT_HLIST_BL_HEAD(hbl);
10966 +       }
10967 +}
10968 +
10969 +/* returns the number of inodes or an error */
10970 +static int au_dr_hino_store(struct super_block *sb, struct au_branch *br,
10971 +                           struct file *hinofile)
10972 +{
10973 +       int err, i;
10974 +       ssize_t ssz;
10975 +       loff_t pos, oldsize;
10976 +       __be64 u64;
10977 +       struct inode *hinoinode;
10978 +       struct hlist_bl_head *hbl;
10979 +       struct hlist_bl_node *n1, *n2;
10980 +       struct au_dr_hino *ent;
10981 +
10982 +       SiMustWriteLock(sb);
10983 +       AuDebugOn(!au_br_writable(br->br_perm));
10984 +
10985 +       hinoinode = file_inode(hinofile);
10986 +       oldsize = i_size_read(hinoinode);
10987 +
10988 +       err = 0;
10989 +       pos = 0;
10990 +       hbl = br->br_dirren.dr_h_ino;
10991 +       for (i = 0; !err && i < AuDirren_NHASH; i++, hbl++) {
10992 +               /* no bit-lock since sbinfo must be write-locked */
10993 +               hlist_bl_for_each_entry_safe(ent, n1, n2, hbl, dr_hnode) {
10994 +                       AuDbg("hi%llu, %pD2\n",
10995 +                             (unsigned long long)ent->dr_h_ino, hinofile);
10996 +                       u64 = cpu_to_be64(ent->dr_h_ino);
10997 +                       ssz = vfsub_write_k(hinofile, &u64, sizeof(u64), &pos);
10998 +                       if (ssz == sizeof(u64))
10999 +                               continue;
11000 +
11001 +                       /* write error */
11002 +                       pr_err("ssz %zd, %pD2\n", ssz, hinofile);
11003 +                       err = -ENOSPC;
11004 +                       if (ssz < 0)
11005 +                               err = ssz;
11006 +                       break;
11007 +               }
11008 +       }
11009 +       /* regardless the error */
11010 +       if (pos < oldsize) {
11011 +               err = vfsub_trunc(&hinofile->f_path, pos, /*attr*/0, hinofile);
11012 +               AuTraceErr(err);
11013 +       }
11014 +
11015 +       AuTraceErr(err);
11016 +       return err;
11017 +}
11018 +
11019 +static int au_dr_hino_load(struct au_dr_br *dr, struct file *hinofile)
11020 +{
11021 +       int err, hidx;
11022 +       ssize_t ssz;
11023 +       size_t sz, n;
11024 +       loff_t pos;
11025 +       uint64_t u64;
11026 +       struct au_dr_hino *ent;
11027 +       struct inode *hinoinode;
11028 +       struct hlist_bl_head *hbl;
11029 +
11030 +       err = 0;
11031 +       pos = 0;
11032 +       hbl = dr->dr_h_ino;
11033 +       hinoinode = file_inode(hinofile);
11034 +       sz = i_size_read(hinoinode);
11035 +       AuDebugOn(sz % sizeof(u64));
11036 +       n = sz / sizeof(u64);
11037 +       while (n--) {
11038 +               ssz = vfsub_read_k(hinofile, &u64, sizeof(u64), &pos);
11039 +               if (unlikely(ssz != sizeof(u64))) {
11040 +                       pr_err("ssz %zd, %pD2\n", ssz, hinofile);
11041 +                       err = -EINVAL;
11042 +                       if (ssz < 0)
11043 +                               err = ssz;
11044 +                       goto out_free;
11045 +               }
11046 +
11047 +               ent = kmalloc(sizeof(*ent), GFP_NOFS);
11048 +               if (!ent) {
11049 +                       err = -ENOMEM;
11050 +                       AuTraceErr(err);
11051 +                       goto out_free;
11052 +               }
11053 +               ent->dr_h_ino = be64_to_cpu((__force __be64)u64);
11054 +               AuDbg("hi%llu, %pD2\n",
11055 +                     (unsigned long long)ent->dr_h_ino, hinofile);
11056 +               hidx = au_dr_ihash(ent->dr_h_ino);
11057 +               au_hbl_add(&ent->dr_hnode, hbl + hidx);
11058 +       }
11059 +       goto out; /* success */
11060 +
11061 +out_free:
11062 +       au_dr_hino_free(dr);
11063 +out:
11064 +       AuTraceErr(err);
11065 +       return err;
11066 +}
11067 +
11068 +/*
11069 + * @bindex/@br is a switch to distinguish whether suspending hnotify or not.
11070 + * @path is a switch to distinguish load and store.
11071 + */
11072 +static int au_dr_hino(struct super_block *sb, aufs_bindex_t bindex,
11073 +                     struct au_branch *br, const struct path *path)
11074 +{
11075 +       int err, flags;
11076 +       unsigned char load, suspend;
11077 +       struct file *hinofile;
11078 +       struct au_hinode *hdir;
11079 +       struct inode *dir, *delegated;
11080 +       struct path hinopath;
11081 +       struct qstr hinoname = QSTR_INIT(AUFS_WH_DR_BRHINO,
11082 +                                        sizeof(AUFS_WH_DR_BRHINO) - 1);
11083 +
11084 +       AuDebugOn(bindex < 0 && !br);
11085 +       AuDebugOn(bindex >= 0 && br);
11086 +
11087 +       err = -EINVAL;
11088 +       suspend = !br;
11089 +       if (suspend)
11090 +               br = au_sbr(sb, bindex);
11091 +       load = !!path;
11092 +       if (!load) {
11093 +               path = &br->br_path;
11094 +               AuDebugOn(!au_br_writable(br->br_perm));
11095 +               if (unlikely(!au_br_writable(br->br_perm)))
11096 +                       goto out;
11097 +       }
11098 +
11099 +       hdir = NULL;
11100 +       if (suspend) {
11101 +               dir = d_inode(sb->s_root);
11102 +               hdir = au_hinode(au_ii(dir), bindex);
11103 +               dir = hdir->hi_inode;
11104 +               au_hn_inode_lock_nested(hdir, AuLsc_I_CHILD);
11105 +       } else {
11106 +               dir = d_inode(path->dentry);
11107 +               inode_lock_nested(dir, AuLsc_I_CHILD);
11108 +       }
11109 +       hinopath.mnt = path->mnt;
11110 +       hinopath.dentry = vfsub_lkup_one(&hinoname, (struct path *)path);
11111 +       err = PTR_ERR(hinopath.dentry);
11112 +       if (IS_ERR(hinopath.dentry))
11113 +               goto out_unlock;
11114 +
11115 +       err = 0;
11116 +       flags = O_RDONLY;
11117 +       if (load) {
11118 +               if (d_is_negative(hinopath.dentry))
11119 +                       goto out_dput; /* success */
11120 +       } else {
11121 +               if (au_dr_hino_test_empty(&br->br_dirren)) {
11122 +                       if (d_is_positive(hinopath.dentry)) {
11123 +                               delegated = NULL;
11124 +                               err = vfsub_unlink(dir, &hinopath, &delegated,
11125 +                                                  /*force*/0);
11126 +                               AuTraceErr(err);
11127 +                               if (unlikely(err))
11128 +                                       pr_err("ignored err %d, %pd2\n",
11129 +                                              err, hinopath.dentry);
11130 +                               if (unlikely(err == -EWOULDBLOCK))
11131 +                                       iput(delegated);
11132 +                               err = 0;
11133 +                       }
11134 +                       goto out_dput;
11135 +               } else if (!d_is_positive(hinopath.dentry)) {
11136 +                       err = vfsub_create(dir, &hinopath, 0600,
11137 +                                          /*want_excl*/false);
11138 +                       AuTraceErr(err);
11139 +                       if (unlikely(err))
11140 +                               goto out_dput;
11141 +               }
11142 +               flags = O_WRONLY;
11143 +       }
11144 +       hinofile = vfsub_dentry_open(&hinopath, flags);
11145 +       if (suspend)
11146 +               au_hn_inode_unlock(hdir);
11147 +       else
11148 +               inode_unlock(dir);
11149 +       dput(hinopath.dentry);
11150 +       AuTraceErrPtr(hinofile);
11151 +       if (IS_ERR(hinofile)) {
11152 +               err = PTR_ERR(hinofile);
11153 +               goto out;
11154 +       }
11155 +
11156 +       if (load)
11157 +               err = au_dr_hino_load(&br->br_dirren, hinofile);
11158 +       else
11159 +               err = au_dr_hino_store(sb, br, hinofile);
11160 +       fput(hinofile);
11161 +       goto out;
11162 +
11163 +out_dput:
11164 +       dput(hinopath.dentry);
11165 +out_unlock:
11166 +       if (suspend)
11167 +               au_hn_inode_unlock(hdir);
11168 +       else
11169 +               inode_unlock(dir);
11170 +out:
11171 +       AuTraceErr(err);
11172 +       return err;
11173 +}
11174 +
11175 +/* ---------------------------------------------------------------------- */
11176 +
11177 +static int au_dr_brid_init(struct au_dr_brid *brid, const struct path *path)
11178 +{
11179 +       int err;
11180 +       struct kstatfs kstfs;
11181 +       dev_t dev;
11182 +       struct dentry *dentry;
11183 +       struct super_block *sb;
11184 +
11185 +       err = vfs_statfs((void *)path, &kstfs);
11186 +       AuTraceErr(err);
11187 +       if (unlikely(err))
11188 +               goto out;
11189 +
11190 +       /* todo: support for UUID */
11191 +
11192 +       if (kstfs.f_fsid.val[0] || kstfs.f_fsid.val[1]) {
11193 +               brid->type = AuBrid_FSID;
11194 +               brid->fsid = kstfs.f_fsid;
11195 +       } else {
11196 +               dentry = path->dentry;
11197 +               sb = dentry->d_sb;
11198 +               dev = sb->s_dev;
11199 +               if (dev) {
11200 +                       brid->type = AuBrid_DEV;
11201 +                       brid->dev = dev;
11202 +               }
11203 +       }
11204 +
11205 +out:
11206 +       return err;
11207 +}
11208 +
11209 +int au_dr_br_init(struct super_block *sb, struct au_branch *br,
11210 +                 const struct path *path)
11211 +{
11212 +       int err, i;
11213 +       struct au_dr_br *dr;
11214 +       struct hlist_bl_head *hbl;
11215 +
11216 +       dr = &br->br_dirren;
11217 +       hbl = dr->dr_h_ino;
11218 +       for (i = 0; i < AuDirren_NHASH; i++, hbl++)
11219 +               INIT_HLIST_BL_HEAD(hbl);
11220 +
11221 +       err = au_dr_brid_init(&dr->dr_brid, path);
11222 +       if (unlikely(err))
11223 +               goto out;
11224 +
11225 +       if (au_opt_test(au_mntflags(sb), DIRREN))
11226 +               err = au_dr_hino(sb, /*bindex*/-1, br, path);
11227 +
11228 +out:
11229 +       AuTraceErr(err);
11230 +       return err;
11231 +}
11232 +
11233 +int au_dr_br_fin(struct super_block *sb, struct au_branch *br)
11234 +{
11235 +       int err;
11236 +
11237 +       err = 0;
11238 +       if (au_br_writable(br->br_perm))
11239 +               err = au_dr_hino(sb, /*bindex*/-1, br, /*path*/NULL);
11240 +       if (!err)
11241 +               au_dr_hino_free(&br->br_dirren);
11242 +
11243 +       return err;
11244 +}
11245 +
11246 +/* ---------------------------------------------------------------------- */
11247 +
11248 +static int au_brid_str(struct au_dr_brid *brid, struct inode *h_inode,
11249 +                      char *buf, size_t sz)
11250 +{
11251 +       int err;
11252 +       unsigned int major, minor;
11253 +       char *p;
11254 +
11255 +       p = buf;
11256 +       err = snprintf(p, sz, "%d_", brid->type);
11257 +       AuDebugOn(err > sz);
11258 +       p += err;
11259 +       sz -= err;
11260 +       switch (brid->type) {
11261 +       case AuBrid_Unset:
11262 +               return -EINVAL;
11263 +       case AuBrid_UUID:
11264 +               err = snprintf(p, sz, "%pU", brid->uuid.b);
11265 +               break;
11266 +       case AuBrid_FSID:
11267 +               err = snprintf(p, sz, "%08x-%08x",
11268 +                              brid->fsid.val[0], brid->fsid.val[1]);
11269 +               break;
11270 +       case AuBrid_DEV:
11271 +               major = MAJOR(brid->dev);
11272 +               minor = MINOR(brid->dev);
11273 +               if (major <= 0xff && minor <= 0xff)
11274 +                       err = snprintf(p, sz, "%02x%02x", major, minor);
11275 +               else
11276 +                       err = snprintf(p, sz, "%03x:%05x", major, minor);
11277 +               break;
11278 +       }
11279 +       AuDebugOn(err > sz);
11280 +       p += err;
11281 +       sz -= err;
11282 +       err = snprintf(p, sz, "_%llu", (unsigned long long)h_inode->i_ino);
11283 +       AuDebugOn(err > sz);
11284 +       p += err;
11285 +       sz -= err;
11286 +
11287 +       return p - buf;
11288 +}
11289 +
11290 +static int au_drinfo_name(struct au_branch *br, char *name, int len)
11291 +{
11292 +       int rlen;
11293 +       struct dentry *br_dentry;
11294 +       struct inode *br_inode;
11295 +
11296 +       br_dentry = au_br_dentry(br);
11297 +       br_inode = d_inode(br_dentry);
11298 +       rlen = au_brid_str(&br->br_dirren.dr_brid, br_inode, name, len);
11299 +       AuDebugOn(rlen >= AUFS_DIRREN_ENV_VAL_SZ);
11300 +       AuDebugOn(rlen > len);
11301 +
11302 +       return rlen;
11303 +}
11304 +
11305 +/* ---------------------------------------------------------------------- */
11306 +
11307 +/*
11308 + * from the given @h_dentry, construct drinfo at @*fdata.
11309 + * when the size of @*fdata is not enough, reallocate and return new @fdata and
11310 + * @allocated.
11311 + */
11312 +static int au_drinfo_construct(struct au_drinfo_fdata **fdata,
11313 +                              struct dentry *h_dentry,
11314 +                              unsigned char *allocated)
11315 +{
11316 +       int err, v;
11317 +       struct au_drinfo_fdata *f, *p;
11318 +       struct au_drinfo *drinfo;
11319 +       struct inode *h_inode;
11320 +       struct qstr *qname;
11321 +
11322 +       err = 0;
11323 +       f = *fdata;
11324 +       h_inode = d_inode(h_dentry);
11325 +       qname = &h_dentry->d_name;
11326 +       drinfo = &f->drinfo;
11327 +       drinfo->ino = (__force uint64_t)cpu_to_be64(h_inode->i_ino);
11328 +       drinfo->oldnamelen = qname->len;
11329 +       if (*allocated < sizeof(*f) + qname->len) {
11330 +               v = roundup_pow_of_two(*allocated + qname->len);
11331 +               p = au_krealloc(f, v, GFP_NOFS, /*may_shrink*/0);
11332 +               if (unlikely(!p)) {
11333 +                       err = -ENOMEM;
11334 +                       AuTraceErr(err);
11335 +                       goto out;
11336 +               }
11337 +               f = p;
11338 +               *fdata = f;
11339 +               *allocated = v;
11340 +               drinfo = &f->drinfo;
11341 +       }
11342 +       memcpy(drinfo->oldname, qname->name, qname->len);
11343 +       AuDbg("i%llu, %.*s\n",
11344 +             be64_to_cpu((__force __be64)drinfo->ino), drinfo->oldnamelen,
11345 +             drinfo->oldname);
11346 +
11347 +out:
11348 +       AuTraceErr(err);
11349 +       return err;
11350 +}
11351 +
11352 +/* callers have to free the return value */
11353 +static struct au_drinfo *au_drinfo_read_k(struct file *file, ino_t h_ino)
11354 +{
11355 +       struct au_drinfo *ret, *drinfo;
11356 +       struct au_drinfo_fdata fdata;
11357 +       int len;
11358 +       loff_t pos;
11359 +       ssize_t ssz;
11360 +
11361 +       ret = ERR_PTR(-EIO);
11362 +       pos = 0;
11363 +       ssz = vfsub_read_k(file, &fdata, sizeof(fdata), &pos);
11364 +       if (unlikely(ssz != sizeof(fdata))) {
11365 +               AuIOErr("ssz %zd, %u, %pD2\n",
11366 +                       ssz, (unsigned int)sizeof(fdata), file);
11367 +               goto out;
11368 +       }
11369 +
11370 +       fdata.magic = ntohl((__force __be32)fdata.magic);
11371 +       switch (fdata.magic) {
11372 +       case AUFS_DRINFO_MAGIC_V1:
11373 +               break;
11374 +       default:
11375 +               AuIOErr("magic-num 0x%x, 0x%x, %pD2\n",
11376 +                       fdata.magic, AUFS_DRINFO_MAGIC_V1, file);
11377 +               goto out;
11378 +       }
11379 +
11380 +       drinfo = &fdata.drinfo;
11381 +       len = drinfo->oldnamelen;
11382 +       if (!len) {
11383 +               AuIOErr("broken drinfo %pD2\n", file);
11384 +               goto out;
11385 +       }
11386 +
11387 +       ret = NULL;
11388 +       drinfo->ino = be64_to_cpu((__force __be64)drinfo->ino);
11389 +       if (unlikely(h_ino && drinfo->ino != h_ino)) {
11390 +               AuDbg("ignored i%llu, i%llu, %pD2\n",
11391 +                     (unsigned long long)drinfo->ino,
11392 +                     (unsigned long long)h_ino, file);
11393 +               goto out; /* success */
11394 +       }
11395 +
11396 +       ret = kmalloc(sizeof(*ret) + len, GFP_NOFS);
11397 +       if (unlikely(!ret)) {
11398 +               ret = ERR_PTR(-ENOMEM);
11399 +               AuTraceErrPtr(ret);
11400 +               goto out;
11401 +       }
11402 +
11403 +       *ret = *drinfo;
11404 +       ssz = vfsub_read_k(file, (void *)ret->oldname, len, &pos);
11405 +       if (unlikely(ssz != len)) {
11406 +               au_kfree_rcu(ret);
11407 +               ret = ERR_PTR(-EIO);
11408 +               AuIOErr("ssz %zd, %u, %pD2\n", ssz, len, file);
11409 +               goto out;
11410 +       }
11411 +
11412 +       AuDbg("oldname %.*s\n", ret->oldnamelen, ret->oldname);
11413 +
11414 +out:
11415 +       return ret;
11416 +}
11417 +
11418 +/* ---------------------------------------------------------------------- */
11419 +
11420 +/* in order to be revertible */
11421 +struct au_drinfo_rev_elm {
11422 +       int                     created;
11423 +       struct dentry           *info_dentry;
11424 +       struct au_drinfo        *info_last;
11425 +};
11426 +
11427 +struct au_drinfo_rev {
11428 +       unsigned char                   already;
11429 +       aufs_bindex_t                   nelm;
11430 +       struct au_drinfo_rev_elm        elm[];
11431 +};
11432 +
11433 +/* todo: isn't it too large? */
11434 +struct au_drinfo_store {
11435 +       struct path h_ppath;
11436 +       struct dentry *h_dentry;
11437 +       struct au_drinfo_fdata *fdata;
11438 +       char *infoname;                 /* inside of whname, just after PFX */
11439 +       char whname[sizeof(AUFS_WH_DR_INFO_PFX) + AUFS_DIRREN_ENV_VAL_SZ];
11440 +       aufs_bindex_t btgt, btail;
11441 +       unsigned char no_sio,
11442 +               allocated,              /* current size of *fdata */
11443 +               infonamelen,            /* room size for p */
11444 +               whnamelen,              /* length of the generated name */
11445 +               renameback;             /* renamed back */
11446 +};
11447 +
11448 +/* on rename(2) error, the caller should revert it using @elm */
11449 +static int au_drinfo_do_store(struct au_drinfo_store *w,
11450 +                             struct au_drinfo_rev_elm *elm)
11451 +{
11452 +       int err, len;
11453 +       ssize_t ssz;
11454 +       loff_t pos;
11455 +       struct path infopath = {
11456 +               .mnt = w->h_ppath.mnt
11457 +       };
11458 +       struct inode *h_dir, *h_inode, *delegated;
11459 +       struct file *infofile;
11460 +       struct qstr *qname;
11461 +
11462 +       AuDebugOn(elm
11463 +                 && memcmp(elm, page_address(ZERO_PAGE(0)), sizeof(*elm)));
11464 +
11465 +       infopath.dentry = vfsub_lookup_one_len(w->whname, &w->h_ppath,
11466 +                                              w->whnamelen);
11467 +       AuTraceErrPtr(infopath.dentry);
11468 +       if (IS_ERR(infopath.dentry)) {
11469 +               err = PTR_ERR(infopath.dentry);
11470 +               goto out;
11471 +       }
11472 +
11473 +       err = 0;
11474 +       h_dir = d_inode(w->h_ppath.dentry);
11475 +       if (elm && d_is_negative(infopath.dentry)) {
11476 +               err = vfsub_create(h_dir, &infopath, 0600, /*want_excl*/true);
11477 +               AuTraceErr(err);
11478 +               if (unlikely(err))
11479 +                       goto out_dput;
11480 +               elm->created = 1;
11481 +               elm->info_dentry = dget(infopath.dentry);
11482 +       }
11483 +
11484 +       infofile = vfsub_dentry_open(&infopath, O_RDWR);
11485 +       AuTraceErrPtr(infofile);
11486 +       if (IS_ERR(infofile)) {
11487 +               err = PTR_ERR(infofile);
11488 +               goto out_dput;
11489 +       }
11490 +
11491 +       h_inode = d_inode(infopath.dentry);
11492 +       if (elm && i_size_read(h_inode)) {
11493 +               h_inode = d_inode(w->h_dentry);
11494 +               elm->info_last = au_drinfo_read_k(infofile, h_inode->i_ino);
11495 +               AuTraceErrPtr(elm->info_last);
11496 +               if (IS_ERR(elm->info_last)) {
11497 +                       err = PTR_ERR(elm->info_last);
11498 +                       elm->info_last = NULL;
11499 +                       AuDebugOn(elm->info_dentry);
11500 +                       goto out_fput;
11501 +               }
11502 +       }
11503 +
11504 +       if (elm && w->renameback) {
11505 +               delegated = NULL;
11506 +               err = vfsub_unlink(h_dir, &infopath, &delegated, /*force*/0);
11507 +               AuTraceErr(err);
11508 +               if (unlikely(err == -EWOULDBLOCK))
11509 +                       iput(delegated);
11510 +               goto out_fput;
11511 +       }
11512 +
11513 +       pos = 0;
11514 +       qname = &w->h_dentry->d_name;
11515 +       len = sizeof(*w->fdata) + qname->len;
11516 +       if (!elm)
11517 +               len = sizeof(*w->fdata) + w->fdata->drinfo.oldnamelen;
11518 +       ssz = vfsub_write_k(infofile, w->fdata, len, &pos);
11519 +       if (ssz == len) {
11520 +               AuDbg("hi%llu, %.*s\n", w->fdata->drinfo.ino,
11521 +                     w->fdata->drinfo.oldnamelen, w->fdata->drinfo.oldname);
11522 +               goto out_fput; /* success */
11523 +       } else {
11524 +               err = -EIO;
11525 +               if (ssz < 0)
11526 +                       err = ssz;
11527 +               /* the caller should revert it using @elm */
11528 +       }
11529 +
11530 +out_fput:
11531 +       fput(infofile);
11532 +out_dput:
11533 +       dput(infopath.dentry);
11534 +out:
11535 +       AuTraceErr(err);
11536 +       return err;
11537 +}
11538 +
11539 +struct au_call_drinfo_do_store_args {
11540 +       int *errp;
11541 +       struct au_drinfo_store *w;
11542 +       struct au_drinfo_rev_elm *elm;
11543 +};
11544 +
11545 +static void au_call_drinfo_do_store(void *args)
11546 +{
11547 +       struct au_call_drinfo_do_store_args *a = args;
11548 +
11549 +       *a->errp = au_drinfo_do_store(a->w, a->elm);
11550 +}
11551 +
11552 +static int au_drinfo_store_sio(struct au_drinfo_store *w,
11553 +                              struct au_drinfo_rev_elm *elm)
11554 +{
11555 +       int err, wkq_err;
11556 +
11557 +       if (w->no_sio)
11558 +               err = au_drinfo_do_store(w, elm);
11559 +       else {
11560 +               struct au_call_drinfo_do_store_args a = {
11561 +                       .errp   = &err,
11562 +                       .w      = w,
11563 +                       .elm    = elm
11564 +               };
11565 +               wkq_err = au_wkq_wait(au_call_drinfo_do_store, &a);
11566 +               if (unlikely(wkq_err))
11567 +                       err = wkq_err;
11568 +       }
11569 +       AuTraceErr(err);
11570 +
11571 +       return err;
11572 +}
11573 +
11574 +static int au_drinfo_store_work_init(struct au_drinfo_store *w,
11575 +                                    aufs_bindex_t btgt)
11576 +{
11577 +       int err;
11578 +
11579 +       memset(w, 0, sizeof(*w));
11580 +       w->allocated = roundup_pow_of_two(sizeof(*w->fdata) + 40);
11581 +       strcpy(w->whname, AUFS_WH_DR_INFO_PFX);
11582 +       w->infoname = w->whname + sizeof(AUFS_WH_DR_INFO_PFX) - 1;
11583 +       w->infonamelen = sizeof(w->whname) - sizeof(AUFS_WH_DR_INFO_PFX);
11584 +       w->btgt = btgt;
11585 +       w->no_sio = !!uid_eq(current_fsuid(), GLOBAL_ROOT_UID);
11586 +
11587 +       err = -ENOMEM;
11588 +       w->fdata = kcalloc(1, w->allocated, GFP_NOFS);
11589 +       if (unlikely(!w->fdata)) {
11590 +               AuTraceErr(err);
11591 +               goto out;
11592 +       }
11593 +       w->fdata->magic = (__force uint32_t)htonl(AUFS_DRINFO_MAGIC_V1);
11594 +       err = 0;
11595 +
11596 +out:
11597 +       return err;
11598 +}
11599 +
11600 +static void au_drinfo_store_work_fin(struct au_drinfo_store *w)
11601 +{
11602 +       au_kfree_rcu(w->fdata);
11603 +}
11604 +
11605 +static void au_drinfo_store_rev(struct au_drinfo_rev *rev,
11606 +                               struct au_drinfo_store *w)
11607 +{
11608 +       struct au_drinfo_rev_elm *elm;
11609 +       struct inode *h_dir, *delegated;
11610 +       int err, nelm;
11611 +       struct path infopath = {
11612 +               .mnt = w->h_ppath.mnt
11613 +       };
11614 +
11615 +       h_dir = d_inode(w->h_ppath.dentry);
11616 +       IMustLock(h_dir);
11617 +
11618 +       err = 0;
11619 +       elm = rev->elm;
11620 +       for (nelm = rev->nelm; nelm > 0; nelm--, elm++) {
11621 +               AuDebugOn(elm->created && elm->info_last);
11622 +               if (elm->created) {
11623 +                       AuDbg("here\n");
11624 +                       delegated = NULL;
11625 +                       infopath.dentry = elm->info_dentry;
11626 +                       err = vfsub_unlink(h_dir, &infopath, &delegated,
11627 +                                          !w->no_sio);
11628 +                       AuTraceErr(err);
11629 +                       if (unlikely(err == -EWOULDBLOCK))
11630 +                               iput(delegated);
11631 +                       dput(elm->info_dentry);
11632 +               } else if (elm->info_last) {
11633 +                       AuDbg("here\n");
11634 +                       w->fdata->drinfo = *elm->info_last;
11635 +                       memcpy(w->fdata->drinfo.oldname,
11636 +                              elm->info_last->oldname,
11637 +                              elm->info_last->oldnamelen);
11638 +                       err = au_drinfo_store_sio(w, /*elm*/NULL);
11639 +                       au_kfree_rcu(elm->info_last);
11640 +               }
11641 +               if (unlikely(err))
11642 +                       AuIOErr("%d, %s\n", err, w->whname);
11643 +               /* go on even if err */
11644 +       }
11645 +}
11646 +
11647 +/* caller has to call au_dr_rename_fin() later */
11648 +static int au_drinfo_store(struct dentry *dentry, aufs_bindex_t btgt,
11649 +                          struct qstr *dst_name, void *_rev)
11650 +{
11651 +       int err, sz, nelm;
11652 +       aufs_bindex_t bindex, btail;
11653 +       struct au_drinfo_store work;
11654 +       struct au_drinfo_rev *rev, **p;
11655 +       struct au_drinfo_rev_elm *elm;
11656 +       struct super_block *sb;
11657 +       struct au_branch *br;
11658 +       struct au_hinode *hdir;
11659 +
11660 +       err = au_drinfo_store_work_init(&work, btgt);
11661 +       AuTraceErr(err);
11662 +       if (unlikely(err))
11663 +               goto out;
11664 +
11665 +       err = -ENOMEM;
11666 +       btail = au_dbtaildir(dentry);
11667 +       nelm = btail - btgt;
11668 +       sz = sizeof(*rev) + sizeof(*elm) * nelm;
11669 +       rev = kcalloc(1, sz, GFP_NOFS);
11670 +       if (unlikely(!rev)) {
11671 +               AuTraceErr(err);
11672 +               goto out_args;
11673 +       }
11674 +       rev->nelm = nelm;
11675 +       elm = rev->elm;
11676 +       p = _rev;
11677 +       *p = rev;
11678 +
11679 +       err = 0;
11680 +       sb = dentry->d_sb;
11681 +       work.h_ppath.dentry = au_h_dptr(dentry, btgt);
11682 +       work.h_ppath.mnt = au_sbr_mnt(sb, btgt);
11683 +       hdir = au_hi(d_inode(dentry), btgt);
11684 +       au_hn_inode_lock_nested(hdir, AuLsc_I_CHILD);
11685 +       for (bindex = btgt + 1; bindex <= btail; bindex++, elm++) {
11686 +               work.h_dentry = au_h_dptr(dentry, bindex);
11687 +               if (!work.h_dentry)
11688 +                       continue;
11689 +
11690 +               err = au_drinfo_construct(&work.fdata, work.h_dentry,
11691 +                                         &work.allocated);
11692 +               AuTraceErr(err);
11693 +               if (unlikely(err))
11694 +                       break;
11695 +
11696 +               work.renameback = au_qstreq(&work.h_dentry->d_name, dst_name);
11697 +               br = au_sbr(sb, bindex);
11698 +               work.whnamelen = sizeof(AUFS_WH_DR_INFO_PFX) - 1;
11699 +               work.whnamelen += au_drinfo_name(br, work.infoname,
11700 +                                                work.infonamelen);
11701 +               AuDbg("whname %.*s, i%llu, %.*s\n",
11702 +                     work.whnamelen, work.whname,
11703 +                     be64_to_cpu((__force __be64)work.fdata->drinfo.ino),
11704 +                     work.fdata->drinfo.oldnamelen,
11705 +                     work.fdata->drinfo.oldname);
11706 +
11707 +               err = au_drinfo_store_sio(&work, elm);
11708 +               AuTraceErr(err);
11709 +               if (unlikely(err))
11710 +                       break;
11711 +       }
11712 +       if (unlikely(err)) {
11713 +               /* revert all drinfo */
11714 +               au_drinfo_store_rev(rev, &work);
11715 +               au_kfree_try_rcu(rev);
11716 +               *p = NULL;
11717 +       }
11718 +       au_hn_inode_unlock(hdir);
11719 +
11720 +out_args:
11721 +       au_drinfo_store_work_fin(&work);
11722 +out:
11723 +       return err;
11724 +}
11725 +
11726 +/* ---------------------------------------------------------------------- */
11727 +
11728 +int au_dr_rename(struct dentry *src, aufs_bindex_t bindex,
11729 +                struct qstr *dst_name, void *_rev)
11730 +{
11731 +       int err, already;
11732 +       ino_t ino;
11733 +       struct super_block *sb;
11734 +       struct au_branch *br;
11735 +       struct au_dr_br *dr;
11736 +       struct dentry *h_dentry;
11737 +       struct inode *h_inode;
11738 +       struct au_dr_hino *ent;
11739 +       struct au_drinfo_rev *rev, **p;
11740 +
11741 +       AuDbg("bindex %d\n", bindex);
11742 +
11743 +       err = -ENOMEM;
11744 +       ent = kmalloc(sizeof(*ent), GFP_NOFS);
11745 +       if (unlikely(!ent))
11746 +               goto out;
11747 +
11748 +       sb = src->d_sb;
11749 +       br = au_sbr(sb, bindex);
11750 +       dr = &br->br_dirren;
11751 +       h_dentry = au_h_dptr(src, bindex);
11752 +       h_inode = d_inode(h_dentry);
11753 +       ino = h_inode->i_ino;
11754 +       ent->dr_h_ino = ino;
11755 +       already = au_dr_hino_test_add(dr, ino, ent);
11756 +       AuDbg("b%d, hi%llu, already %d\n",
11757 +             bindex, (unsigned long long)ino, already);
11758 +
11759 +       err = au_drinfo_store(src, bindex, dst_name, _rev);
11760 +       AuTraceErr(err);
11761 +       if (!err) {
11762 +               p = _rev;
11763 +               rev = *p;
11764 +               rev->already = already;
11765 +               goto out; /* success */
11766 +       }
11767 +
11768 +       /* revert */
11769 +       if (!already)
11770 +               au_dr_hino_del(dr, ent);
11771 +       au_kfree_rcu(ent);
11772 +
11773 +out:
11774 +       AuTraceErr(err);
11775 +       return err;
11776 +}
11777 +
11778 +void au_dr_rename_fin(struct dentry *src, aufs_bindex_t btgt, void *_rev)
11779 +{
11780 +       struct au_drinfo_rev *rev;
11781 +       struct au_drinfo_rev_elm *elm;
11782 +       int nelm;
11783 +
11784 +       rev = _rev;
11785 +       elm = rev->elm;
11786 +       for (nelm = rev->nelm; nelm > 0; nelm--, elm++) {
11787 +               dput(elm->info_dentry);
11788 +               au_kfree_rcu(elm->info_last);
11789 +       }
11790 +       au_kfree_try_rcu(rev);
11791 +}
11792 +
11793 +void au_dr_rename_rev(struct dentry *src, aufs_bindex_t btgt, void *_rev)
11794 +{
11795 +       int err;
11796 +       struct au_drinfo_store work;
11797 +       struct au_drinfo_rev *rev = _rev;
11798 +       struct super_block *sb;
11799 +       struct au_branch *br;
11800 +       struct inode *h_inode;
11801 +       struct au_dr_br *dr;
11802 +       struct au_dr_hino *ent;
11803 +
11804 +       err = au_drinfo_store_work_init(&work, btgt);
11805 +       if (unlikely(err))
11806 +               goto out;
11807 +
11808 +       sb = src->d_sb;
11809 +       br = au_sbr(sb, btgt);
11810 +       work.h_ppath.dentry = au_h_dptr(src, btgt);
11811 +       work.h_ppath.mnt = au_br_mnt(br);
11812 +       au_drinfo_store_rev(rev, &work);
11813 +       au_drinfo_store_work_fin(&work);
11814 +       if (rev->already)
11815 +               goto out;
11816 +
11817 +       dr = &br->br_dirren;
11818 +       h_inode = d_inode(work.h_ppath.dentry);
11819 +       ent = au_dr_hino_find(dr, h_inode->i_ino);
11820 +       BUG_ON(!ent);
11821 +       au_dr_hino_del(dr, ent);
11822 +       au_kfree_rcu(ent);
11823 +
11824 +out:
11825 +       au_kfree_try_rcu(rev);
11826 +       if (unlikely(err))
11827 +               pr_err("failed to remove dirren info\n");
11828 +}
11829 +
11830 +/* ---------------------------------------------------------------------- */
11831 +
11832 +static struct au_drinfo *au_drinfo_do_load(struct path *h_ppath,
11833 +                                          char *whname, int whnamelen,
11834 +                                          struct dentry **info_dentry)
11835 +{
11836 +       struct au_drinfo *drinfo;
11837 +       struct file *f;
11838 +       struct inode *h_dir;
11839 +       struct path infopath;
11840 +       int unlocked;
11841 +
11842 +       AuDbg("%pd/%.*s\n", h_ppath->dentry, whnamelen, whname);
11843 +
11844 +       *info_dentry = NULL;
11845 +       drinfo = NULL;
11846 +       unlocked = 0;
11847 +       h_dir = d_inode(h_ppath->dentry);
11848 +       inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
11849 +       infopath.dentry = vfsub_lookup_one_len(whname, h_ppath, whnamelen);
11850 +       if (IS_ERR(infopath.dentry)) {
11851 +               drinfo = (void *)infopath.dentry;
11852 +               goto out;
11853 +       }
11854 +
11855 +       if (d_is_negative(infopath.dentry))
11856 +               goto out_dput; /* success */
11857 +
11858 +       infopath.mnt = h_ppath->mnt;
11859 +       f = vfsub_dentry_open(&infopath, O_RDONLY);
11860 +       inode_unlock_shared(h_dir);
11861 +       unlocked = 1;
11862 +       if (IS_ERR(f)) {
11863 +               drinfo = (void *)f;
11864 +               goto out_dput;
11865 +       }
11866 +
11867 +       drinfo = au_drinfo_read_k(f, /*h_ino*/0);
11868 +       if (IS_ERR_OR_NULL(drinfo))
11869 +               goto out_fput;
11870 +
11871 +       AuDbg("oldname %.*s\n", drinfo->oldnamelen, drinfo->oldname);
11872 +       *info_dentry = dget(infopath.dentry); /* keep it alive */
11873 +
11874 +out_fput:
11875 +       fput(f);
11876 +out_dput:
11877 +       dput(infopath.dentry);
11878 +out:
11879 +       if (!unlocked)
11880 +               inode_unlock_shared(h_dir);
11881 +       AuTraceErrPtr(drinfo);
11882 +       return drinfo;
11883 +}
11884 +
11885 +struct au_drinfo_do_load_args {
11886 +       struct au_drinfo **drinfop;
11887 +       struct path *h_ppath;
11888 +       char *whname;
11889 +       int whnamelen;
11890 +       struct dentry **info_dentry;
11891 +};
11892 +
11893 +static void au_call_drinfo_do_load(void *args)
11894 +{
11895 +       struct au_drinfo_do_load_args *a = args;
11896 +
11897 +       *a->drinfop = au_drinfo_do_load(a->h_ppath, a->whname, a->whnamelen,
11898 +                                       a->info_dentry);
11899 +}
11900 +
11901 +struct au_drinfo_load {
11902 +       struct path h_ppath;
11903 +       struct qstr *qname;
11904 +       unsigned char no_sio;
11905 +
11906 +       aufs_bindex_t ninfo;
11907 +       struct au_drinfo **drinfo;
11908 +};
11909 +
11910 +static int au_drinfo_load(struct au_drinfo_load *w, aufs_bindex_t bindex,
11911 +                         struct au_branch *br)
11912 +{
11913 +       int err, wkq_err, whnamelen, e;
11914 +       char whname[sizeof(AUFS_WH_DR_INFO_PFX) + AUFS_DIRREN_ENV_VAL_SZ]
11915 +               = AUFS_WH_DR_INFO_PFX;
11916 +       struct au_drinfo *drinfo;
11917 +       struct qstr oldname;
11918 +       struct inode *h_dir, *delegated;
11919 +       struct dentry *info_dentry;
11920 +       struct path infopath;
11921 +
11922 +       whnamelen = sizeof(AUFS_WH_DR_INFO_PFX) - 1;
11923 +       whnamelen += au_drinfo_name(br, whname + whnamelen,
11924 +                                   sizeof(whname) - whnamelen);
11925 +       if (w->no_sio)
11926 +               drinfo = au_drinfo_do_load(&w->h_ppath, whname, whnamelen,
11927 +                                          &info_dentry);
11928 +       else {
11929 +               struct au_drinfo_do_load_args args = {
11930 +                       .drinfop        = &drinfo,
11931 +                       .h_ppath        = &w->h_ppath,
11932 +                       .whname         = whname,
11933 +                       .whnamelen      = whnamelen,
11934 +                       .info_dentry    = &info_dentry
11935 +               };
11936 +               wkq_err = au_wkq_wait(au_call_drinfo_do_load, &args);
11937 +               if (unlikely(wkq_err))
11938 +                       drinfo = ERR_PTR(wkq_err);
11939 +       }
11940 +       err = PTR_ERR(drinfo);
11941 +       if (IS_ERR_OR_NULL(drinfo))
11942 +               goto out;
11943 +
11944 +       err = 0;
11945 +       oldname.len = drinfo->oldnamelen;
11946 +       oldname.name = drinfo->oldname;
11947 +       if (au_qstreq(w->qname, &oldname)) {
11948 +               /* the name is renamed back */
11949 +               au_kfree_rcu(drinfo);
11950 +               drinfo = NULL;
11951 +
11952 +               infopath.dentry = info_dentry;
11953 +               infopath.mnt = w->h_ppath.mnt;
11954 +               h_dir = d_inode(w->h_ppath.dentry);
11955 +               delegated = NULL;
11956 +               inode_lock_nested(h_dir, AuLsc_I_PARENT);
11957 +               e = vfsub_unlink(h_dir, &infopath, &delegated, !w->no_sio);
11958 +               inode_unlock(h_dir);
11959 +               if (unlikely(e))
11960 +                       AuIOErr("ignored %d, %pd2\n", e, &infopath.dentry);
11961 +               if (unlikely(e == -EWOULDBLOCK))
11962 +                       iput(delegated);
11963 +       }
11964 +       au_kfree_rcu(w->drinfo[bindex]);
11965 +       w->drinfo[bindex] = drinfo;
11966 +       dput(info_dentry);
11967 +
11968 +out:
11969 +       AuTraceErr(err);
11970 +       return err;
11971 +}
11972 +
11973 +/* ---------------------------------------------------------------------- */
11974 +
11975 +static void au_dr_lkup_free(struct au_drinfo **drinfo, int n)
11976 +{
11977 +       struct au_drinfo **p = drinfo;
11978 +
11979 +       while (n-- > 0)
11980 +               au_kfree_rcu(*drinfo++);
11981 +       au_kfree_try_rcu(p);
11982 +}
11983 +
11984 +int au_dr_lkup(struct au_do_lookup_args *lkup, struct dentry *dentry,
11985 +              aufs_bindex_t btgt)
11986 +{
11987 +       int err, ninfo;
11988 +       struct au_drinfo_load w;
11989 +       aufs_bindex_t bindex, bbot;
11990 +       struct au_branch *br;
11991 +       struct inode *h_dir;
11992 +       struct au_dr_hino *ent;
11993 +       struct super_block *sb;
11994 +
11995 +       AuDbg("%.*s, name %.*s, whname %.*s, b%d\n",
11996 +             AuLNPair(&dentry->d_name), AuLNPair(&lkup->dirren.dr_name),
11997 +             AuLNPair(&lkup->whname), btgt);
11998 +
11999 +       sb = dentry->d_sb;
12000 +       bbot = au_sbbot(sb);
12001 +       w.ninfo = bbot + 1;
12002 +       if (!lkup->dirren.drinfo) {
12003 +               lkup->dirren.drinfo = kcalloc(w.ninfo,
12004 +                                             sizeof(*lkup->dirren.drinfo),
12005 +                                             GFP_NOFS);
12006 +               if (unlikely(!lkup->dirren.drinfo)) {
12007 +                       err = -ENOMEM;
12008 +                       goto out;
12009 +               }
12010 +               lkup->dirren.ninfo = w.ninfo;
12011 +       }
12012 +       w.drinfo = lkup->dirren.drinfo;
12013 +       w.no_sio = !!uid_eq(current_fsuid(), GLOBAL_ROOT_UID);
12014 +       w.h_ppath.dentry = au_h_dptr(dentry, btgt);
12015 +       AuDebugOn(!w.h_ppath.dentry);
12016 +       w.h_ppath.mnt = au_sbr_mnt(sb, btgt);
12017 +       w.qname = &dentry->d_name;
12018 +
12019 +       ninfo = 0;
12020 +       for (bindex = btgt + 1; bindex <= bbot; bindex++) {
12021 +               br = au_sbr(sb, bindex);
12022 +               err = au_drinfo_load(&w, bindex, br);
12023 +               if (unlikely(err))
12024 +                       goto out_free;
12025 +               if (w.drinfo[bindex])
12026 +                       ninfo++;
12027 +       }
12028 +       if (!ninfo) {
12029 +               br = au_sbr(sb, btgt);
12030 +               h_dir = d_inode(w.h_ppath.dentry);
12031 +               ent = au_dr_hino_find(&br->br_dirren, h_dir->i_ino);
12032 +               AuDebugOn(!ent);
12033 +               au_dr_hino_del(&br->br_dirren, ent);
12034 +               au_kfree_rcu(ent);
12035 +       }
12036 +       goto out; /* success */
12037 +
12038 +out_free:
12039 +       au_dr_lkup_free(lkup->dirren.drinfo, lkup->dirren.ninfo);
12040 +       lkup->dirren.ninfo = 0;
12041 +       lkup->dirren.drinfo = NULL;
12042 +out:
12043 +       AuTraceErr(err);
12044 +       return err;
12045 +}
12046 +
12047 +void au_dr_lkup_fin(struct au_do_lookup_args *lkup)
12048 +{
12049 +       au_dr_lkup_free(lkup->dirren.drinfo, lkup->dirren.ninfo);
12050 +}
12051 +
12052 +int au_dr_lkup_name(struct au_do_lookup_args *lkup, aufs_bindex_t btgt)
12053 +{
12054 +       int err;
12055 +       struct au_drinfo *drinfo;
12056 +
12057 +       err = 0;
12058 +       if (!lkup->dirren.drinfo)
12059 +               goto out;
12060 +       AuDebugOn(lkup->dirren.ninfo <= btgt);
12061 +       drinfo = lkup->dirren.drinfo[btgt];
12062 +       if (!drinfo)
12063 +               goto out;
12064 +
12065 +       au_kfree_try_rcu(lkup->whname.name);
12066 +       lkup->whname.name = NULL;
12067 +       lkup->dirren.dr_name.len = drinfo->oldnamelen;
12068 +       lkup->dirren.dr_name.name = drinfo->oldname;
12069 +       lkup->name = &lkup->dirren.dr_name;
12070 +       err = au_wh_name_alloc(&lkup->whname, lkup->name);
12071 +       if (!err)
12072 +               AuDbg("name %.*s, whname %.*s, b%d\n",
12073 +                     AuLNPair(lkup->name), AuLNPair(&lkup->whname),
12074 +                     btgt);
12075 +
12076 +out:
12077 +       AuTraceErr(err);
12078 +       return err;
12079 +}
12080 +
12081 +int au_dr_lkup_h_ino(struct au_do_lookup_args *lkup, aufs_bindex_t bindex,
12082 +                    ino_t h_ino)
12083 +{
12084 +       int match;
12085 +       struct au_drinfo *drinfo;
12086 +
12087 +       match = 1;
12088 +       if (!lkup->dirren.drinfo)
12089 +               goto out;
12090 +       AuDebugOn(lkup->dirren.ninfo <= bindex);
12091 +       drinfo = lkup->dirren.drinfo[bindex];
12092 +       if (!drinfo)
12093 +               goto out;
12094 +
12095 +       match = (drinfo->ino == h_ino);
12096 +       AuDbg("match %d\n", match);
12097 +
12098 +out:
12099 +       return match;
12100 +}
12101 +
12102 +/* ---------------------------------------------------------------------- */
12103 +
12104 +int au_dr_opt_set(struct super_block *sb)
12105 +{
12106 +       int err;
12107 +       aufs_bindex_t bindex, bbot;
12108 +       struct au_branch *br;
12109 +
12110 +       err = 0;
12111 +       bbot = au_sbbot(sb);
12112 +       for (bindex = 0; !err && bindex <= bbot; bindex++) {
12113 +               br = au_sbr(sb, bindex);
12114 +               err = au_dr_hino(sb, bindex, /*br*/NULL, &br->br_path);
12115 +       }
12116 +
12117 +       return err;
12118 +}
12119 +
12120 +int au_dr_opt_flush(struct super_block *sb)
12121 +{
12122 +       int err;
12123 +       aufs_bindex_t bindex, bbot;
12124 +       struct au_branch *br;
12125 +
12126 +       err = 0;
12127 +       bbot = au_sbbot(sb);
12128 +       for (bindex = 0; !err && bindex <= bbot; bindex++) {
12129 +               br = au_sbr(sb, bindex);
12130 +               if (au_br_writable(br->br_perm))
12131 +                       err = au_dr_hino(sb, bindex, /*br*/NULL, /*path*/NULL);
12132 +       }
12133 +
12134 +       return err;
12135 +}
12136 +
12137 +int au_dr_opt_clr(struct super_block *sb, int no_flush)
12138 +{
12139 +       int err;
12140 +       aufs_bindex_t bindex, bbot;
12141 +       struct au_branch *br;
12142 +
12143 +       err = 0;
12144 +       if (!no_flush) {
12145 +               err = au_dr_opt_flush(sb);
12146 +               if (unlikely(err))
12147 +                       goto out;
12148 +       }
12149 +
12150 +       bbot = au_sbbot(sb);
12151 +       for (bindex = 0; bindex <= bbot; bindex++) {
12152 +               br = au_sbr(sb, bindex);
12153 +               au_dr_hino_free(&br->br_dirren);
12154 +       }
12155 +
12156 +out:
12157 +       return err;
12158 +}
12159 diff -urN /usr/share/empty/fs/aufs/dirren.h linux/fs/aufs/dirren.h
12160 --- /usr/share/empty/fs/aufs/dirren.h   1970-01-01 01:00:00.000000000 +0100
12161 +++ linux/fs/aufs/dirren.h      2022-03-21 14:49:05.723299677 +0100
12162 @@ -0,0 +1,140 @@
12163 +/* SPDX-License-Identifier: GPL-2.0 */
12164 +/*
12165 + * Copyright (C) 2017-2021 Junjiro R. Okajima
12166 + *
12167 + * This program, aufs is free software; you can redistribute it and/or modify
12168 + * it under the terms of the GNU General Public License as published by
12169 + * the Free Software Foundation; either version 2 of the License, or
12170 + * (at your option) any later version.
12171 + *
12172 + * This program is distributed in the hope that it will be useful,
12173 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12174 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12175 + * GNU General Public License for more details.
12176 + *
12177 + * You should have received a copy of the GNU General Public License
12178 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
12179 + */
12180 +
12181 +/*
12182 + * renamed dir info
12183 + */
12184 +
12185 +#ifndef __AUFS_DIRREN_H__
12186 +#define __AUFS_DIRREN_H__
12187 +
12188 +#ifdef __KERNEL__
12189 +
12190 +#include <linux/dcache.h>
12191 +#include <linux/statfs.h>
12192 +#include <linux/uuid.h>
12193 +#include "hbl.h"
12194 +
12195 +#define AuDirren_NHASH 100
12196 +
12197 +#ifdef CONFIG_AUFS_DIRREN
12198 +enum au_brid_type {
12199 +       AuBrid_Unset,
12200 +       AuBrid_UUID,
12201 +       AuBrid_FSID,
12202 +       AuBrid_DEV
12203 +};
12204 +
12205 +struct au_dr_brid {
12206 +       enum au_brid_type       type;
12207 +       union {
12208 +               uuid_t  uuid;   /* unimplemented yet */
12209 +               fsid_t  fsid;
12210 +               dev_t   dev;
12211 +       };
12212 +};
12213 +
12214 +/* 20 is the max digits length of ulong 64 */
12215 +/* brid-type "_" uuid "_" inum */
12216 +#define AUFS_DIRREN_FNAME_SZ   (1 + 1 + UUID_STRING_LEN + 20)
12217 +#define AUFS_DIRREN_ENV_VAL_SZ (AUFS_DIRREN_FNAME_SZ + 1 + 20)
12218 +
12219 +struct au_dr_hino {
12220 +       struct hlist_bl_node    dr_hnode;
12221 +       ino_t                   dr_h_ino;
12222 +};
12223 +
12224 +struct au_dr_br {
12225 +       struct hlist_bl_head    dr_h_ino[AuDirren_NHASH];
12226 +       struct au_dr_brid       dr_brid;
12227 +};
12228 +
12229 +struct au_dr_lookup {
12230 +       /* dr_name is pointed by struct au_do_lookup_args.name */
12231 +       struct qstr             dr_name; /* subset of dr_info */
12232 +       aufs_bindex_t           ninfo;
12233 +       struct au_drinfo        **drinfo;
12234 +};
12235 +#else
12236 +struct au_dr_hino;
12237 +/* empty */
12238 +struct au_dr_br { };
12239 +struct au_dr_lookup { };
12240 +#endif
12241 +
12242 +/* ---------------------------------------------------------------------- */
12243 +
12244 +struct au_branch;
12245 +struct au_do_lookup_args;
12246 +struct au_hinode;
12247 +#ifdef CONFIG_AUFS_DIRREN
12248 +int au_dr_hino_test_add(struct au_dr_br *dr, ino_t h_ino,
12249 +                       struct au_dr_hino *add_ent);
12250 +void au_dr_hino_free(struct au_dr_br *dr);
12251 +int au_dr_br_init(struct super_block *sb, struct au_branch *br,
12252 +                 const struct path *path);
12253 +int au_dr_br_fin(struct super_block *sb, struct au_branch *br);
12254 +int au_dr_rename(struct dentry *src, aufs_bindex_t bindex,
12255 +                struct qstr *dst_name, void *_rev);
12256 +void au_dr_rename_fin(struct dentry *src, aufs_bindex_t btgt, void *rev);
12257 +void au_dr_rename_rev(struct dentry *src, aufs_bindex_t bindex, void *rev);
12258 +int au_dr_lkup(struct au_do_lookup_args *lkup, struct dentry *dentry,
12259 +              aufs_bindex_t bindex);
12260 +int au_dr_lkup_name(struct au_do_lookup_args *lkup, aufs_bindex_t btgt);
12261 +int au_dr_lkup_h_ino(struct au_do_lookup_args *lkup, aufs_bindex_t bindex,
12262 +                    ino_t h_ino);
12263 +void au_dr_lkup_fin(struct au_do_lookup_args *lkup);
12264 +int au_dr_opt_set(struct super_block *sb);
12265 +int au_dr_opt_flush(struct super_block *sb);
12266 +int au_dr_opt_clr(struct super_block *sb, int no_flush);
12267 +#else
12268 +AuStubInt0(au_dr_hino_test_add, struct au_dr_br *dr, ino_t h_ino,
12269 +          struct au_dr_hino *add_ent);
12270 +AuStubVoid(au_dr_hino_free, struct au_dr_br *dr);
12271 +AuStubInt0(au_dr_br_init, struct super_block *sb, struct au_branch *br,
12272 +          const struct path *path);
12273 +AuStubInt0(au_dr_br_fin, struct super_block *sb, struct au_branch *br);
12274 +AuStubInt0(au_dr_rename, struct dentry *src, aufs_bindex_t bindex,
12275 +          struct qstr *dst_name, void *_rev);
12276 +AuStubVoid(au_dr_rename_fin, struct dentry *src, aufs_bindex_t btgt, void *rev);
12277 +AuStubVoid(au_dr_rename_rev, struct dentry *src, aufs_bindex_t bindex,
12278 +          void *rev);
12279 +AuStubInt0(au_dr_lkup, struct au_do_lookup_args *lkup, struct dentry *dentry,
12280 +          aufs_bindex_t bindex);
12281 +AuStubInt0(au_dr_lkup_name, struct au_do_lookup_args *lkup, aufs_bindex_t btgt);
12282 +AuStubInt0(au_dr_lkup_h_ino, struct au_do_lookup_args *lkup,
12283 +          aufs_bindex_t bindex, ino_t h_ino);
12284 +AuStubVoid(au_dr_lkup_fin, struct au_do_lookup_args *lkup);
12285 +AuStubInt0(au_dr_opt_set, struct super_block *sb);
12286 +AuStubInt0(au_dr_opt_flush, struct super_block *sb);
12287 +AuStubInt0(au_dr_opt_clr, struct super_block *sb, int no_flush);
12288 +#endif
12289 +
12290 +/* ---------------------------------------------------------------------- */
12291 +
12292 +#ifdef CONFIG_AUFS_DIRREN
12293 +static inline int au_dr_ihash(ino_t h_ino)
12294 +{
12295 +       return h_ino % AuDirren_NHASH;
12296 +}
12297 +#else
12298 +AuStubInt0(au_dr_ihash, ino_t h_ino);
12299 +#endif
12300 +
12301 +#endif /* __KERNEL__ */
12302 +#endif /* __AUFS_DIRREN_H__ */
12303 diff -urN /usr/share/empty/fs/aufs/dynop.c linux/fs/aufs/dynop.c
12304 --- /usr/share/empty/fs/aufs/dynop.c    1970-01-01 01:00:00.000000000 +0100
12305 +++ linux/fs/aufs/dynop.c       2022-03-21 14:49:05.723299677 +0100
12306 @@ -0,0 +1,368 @@
12307 +// SPDX-License-Identifier: GPL-2.0
12308 +/*
12309 + * Copyright (C) 2010-2021 Junjiro R. Okajima
12310 + *
12311 + * This program, aufs is free software; you can redistribute it and/or modify
12312 + * it under the terms of the GNU General Public License as published by
12313 + * the Free Software Foundation; either version 2 of the License, or
12314 + * (at your option) any later version.
12315 + *
12316 + * This program is distributed in the hope that it will be useful,
12317 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12318 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12319 + * GNU General Public License for more details.
12320 + *
12321 + * You should have received a copy of the GNU General Public License
12322 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
12323 + */
12324 +
12325 +/*
12326 + * dynamically customizable operations for regular files
12327 + */
12328 +
12329 +#include "aufs.h"
12330 +
12331 +#define DyPrSym(key)   AuDbgSym(key->dk_op.dy_hop)
12332 +
12333 +/*
12334 + * How large will these lists be?
12335 + * Usually just a few elements, 20-30 at most for each, I guess.
12336 + */
12337 +static struct hlist_bl_head dynop[AuDyLast];
12338 +
12339 +static struct au_dykey *dy_gfind_get(struct hlist_bl_head *hbl,
12340 +                                    const void *h_op)
12341 +{
12342 +       struct au_dykey *key, *tmp;
12343 +       struct hlist_bl_node *pos;
12344 +
12345 +       key = NULL;
12346 +       hlist_bl_lock(hbl);
12347 +       hlist_bl_for_each_entry(tmp, pos, hbl, dk_hnode)
12348 +               if (tmp->dk_op.dy_hop == h_op) {
12349 +                       if (kref_get_unless_zero(&tmp->dk_kref))
12350 +                               key = tmp;
12351 +                       break;
12352 +               }
12353 +       hlist_bl_unlock(hbl);
12354 +
12355 +       return key;
12356 +}
12357 +
12358 +static struct au_dykey *dy_bradd(struct au_branch *br, struct au_dykey *key)
12359 +{
12360 +       struct au_dykey **k, *found;
12361 +       const void *h_op = key->dk_op.dy_hop;
12362 +       int i;
12363 +
12364 +       found = NULL;
12365 +       k = br->br_dykey;
12366 +       for (i = 0; i < AuBrDynOp; i++)
12367 +               if (k[i]) {
12368 +                       if (k[i]->dk_op.dy_hop == h_op) {
12369 +                               found = k[i];
12370 +                               break;
12371 +                       }
12372 +               } else
12373 +                       break;
12374 +       if (!found) {
12375 +               spin_lock(&br->br_dykey_lock);
12376 +               for (; i < AuBrDynOp; i++)
12377 +                       if (k[i]) {
12378 +                               if (k[i]->dk_op.dy_hop == h_op) {
12379 +                                       found = k[i];
12380 +                                       break;
12381 +                               }
12382 +                       } else {
12383 +                               k[i] = key;
12384 +                               break;
12385 +                       }
12386 +               spin_unlock(&br->br_dykey_lock);
12387 +               BUG_ON(i == AuBrDynOp); /* expand the array */
12388 +       }
12389 +
12390 +       return found;
12391 +}
12392 +
12393 +/* kref_get() if @key is already added */
12394 +static struct au_dykey *dy_gadd(struct hlist_bl_head *hbl, struct au_dykey *key)
12395 +{
12396 +       struct au_dykey *tmp, *found;
12397 +       struct hlist_bl_node *pos;
12398 +       const void *h_op = key->dk_op.dy_hop;
12399 +
12400 +       found = NULL;
12401 +       hlist_bl_lock(hbl);
12402 +       hlist_bl_for_each_entry(tmp, pos, hbl, dk_hnode)
12403 +               if (tmp->dk_op.dy_hop == h_op) {
12404 +                       if (kref_get_unless_zero(&tmp->dk_kref))
12405 +                               found = tmp;
12406 +                       break;
12407 +               }
12408 +       if (!found)
12409 +               hlist_bl_add_head(&key->dk_hnode, hbl);
12410 +       hlist_bl_unlock(hbl);
12411 +
12412 +       if (!found)
12413 +               DyPrSym(key);
12414 +       return found;
12415 +}
12416 +
12417 +static void dy_free_rcu(struct rcu_head *rcu)
12418 +{
12419 +       struct au_dykey *key;
12420 +
12421 +       key = container_of(rcu, struct au_dykey, dk_rcu);
12422 +       DyPrSym(key);
12423 +       kfree(key);
12424 +}
12425 +
12426 +static void dy_free(struct kref *kref)
12427 +{
12428 +       struct au_dykey *key;
12429 +       struct hlist_bl_head *hbl;
12430 +
12431 +       key = container_of(kref, struct au_dykey, dk_kref);
12432 +       hbl = dynop + key->dk_op.dy_type;
12433 +       au_hbl_del(&key->dk_hnode, hbl);
12434 +       call_rcu(&key->dk_rcu, dy_free_rcu);
12435 +}
12436 +
12437 +void au_dy_put(struct au_dykey *key)
12438 +{
12439 +       kref_put(&key->dk_kref, dy_free);
12440 +}
12441 +
12442 +/* ---------------------------------------------------------------------- */
12443 +
12444 +#define DyDbgSize(cnt, op)     AuDebugOn(cnt != sizeof(op)/sizeof(void *))
12445 +
12446 +#ifdef CONFIG_AUFS_DEBUG
12447 +#define DyDbgDeclare(cnt)      unsigned int cnt = 0
12448 +#define DyDbgInc(cnt)          do { cnt++; } while (0)
12449 +#else
12450 +#define DyDbgDeclare(cnt)      do {} while (0)
12451 +#define DyDbgInc(cnt)          do {} while (0)
12452 +#endif
12453 +
12454 +#define DySet(func, dst, src, h_op, h_sb) do {                         \
12455 +       DyDbgInc(cnt);                                                  \
12456 +       if (h_op->func) {                                               \
12457 +               if (src.func)                                           \
12458 +                       dst.func = src.func;                            \
12459 +               else                                                    \
12460 +                       AuDbg("%s %s\n", au_sbtype(h_sb), #func);       \
12461 +       }                                                               \
12462 +} while (0)
12463 +
12464 +#define DySetForce(func, dst, src) do {                \
12465 +       AuDebugOn(!src.func);                   \
12466 +       DyDbgInc(cnt);                          \
12467 +       dst.func = src.func;                    \
12468 +} while (0)
12469 +
12470 +#define DySetAop(func) \
12471 +       DySet(func, dyaop->da_op, aufs_aop, h_aop, h_sb)
12472 +#define DySetAopForce(func) \
12473 +       DySetForce(func, dyaop->da_op, aufs_aop)
12474 +
12475 +static void dy_aop(struct au_dykey *key, const void *h_op,
12476 +                  struct super_block *h_sb __maybe_unused)
12477 +{
12478 +       struct au_dyaop *dyaop = (void *)key;
12479 +       const struct address_space_operations *h_aop = h_op;
12480 +       DyDbgDeclare(cnt);
12481 +
12482 +       AuDbg("%s\n", au_sbtype(h_sb));
12483 +
12484 +       DySetAop(writepage);
12485 +       DySetAopForce(readpage);        /* force */
12486 +       DySetAop(writepages);
12487 +       DySetAop(set_page_dirty);
12488 +       DySetAop(readpages);
12489 +       DySetAop(readahead);
12490 +       DySetAop(write_begin);
12491 +       DySetAop(write_end);
12492 +       DySetAop(bmap);
12493 +       DySetAop(invalidatepage);
12494 +       DySetAop(releasepage);
12495 +       DySetAop(freepage);
12496 +       /* this one will be changed according to an aufs mount option */
12497 +       DySetAop(direct_IO);
12498 +       DySetAop(migratepage);
12499 +       DySetAop(isolate_page);
12500 +       DySetAop(putback_page);
12501 +       DySetAop(launder_page);
12502 +       DySetAop(is_partially_uptodate);
12503 +       DySetAop(is_dirty_writeback);
12504 +       DySetAop(error_remove_page);
12505 +       DySetAop(swap_activate);
12506 +       DySetAop(swap_deactivate);
12507 +
12508 +       DyDbgSize(cnt, *h_aop);
12509 +}
12510 +
12511 +/* ---------------------------------------------------------------------- */
12512 +
12513 +static void dy_bug(struct kref *kref)
12514 +{
12515 +       BUG();
12516 +}
12517 +
12518 +static struct au_dykey *dy_get(struct au_dynop *op, struct au_branch *br)
12519 +{
12520 +       struct au_dykey *key, *old;
12521 +       struct hlist_bl_head *hbl;
12522 +       struct op {
12523 +               unsigned int sz;
12524 +               void (*set)(struct au_dykey *key, const void *h_op,
12525 +                           struct super_block *h_sb __maybe_unused);
12526 +       };
12527 +       static const struct op a[] = {
12528 +               [AuDy_AOP] = {
12529 +                       .sz     = sizeof(struct au_dyaop),
12530 +                       .set    = dy_aop
12531 +               }
12532 +       };
12533 +       const struct op *p;
12534 +
12535 +       hbl = dynop + op->dy_type;
12536 +       key = dy_gfind_get(hbl, op->dy_hop);
12537 +       if (key)
12538 +               goto out_add; /* success */
12539 +
12540 +       p = a + op->dy_type;
12541 +       key = kzalloc(p->sz, GFP_NOFS);
12542 +       if (unlikely(!key)) {
12543 +               key = ERR_PTR(-ENOMEM);
12544 +               goto out;
12545 +       }
12546 +
12547 +       key->dk_op.dy_hop = op->dy_hop;
12548 +       kref_init(&key->dk_kref);
12549 +       p->set(key, op->dy_hop, au_br_sb(br));
12550 +       old = dy_gadd(hbl, key);
12551 +       if (old) {
12552 +               au_kfree_rcu(key);
12553 +               key = old;
12554 +       }
12555 +
12556 +out_add:
12557 +       old = dy_bradd(br, key);
12558 +       if (old)
12559 +               /* its ref-count should never be zero here */
12560 +               kref_put(&key->dk_kref, dy_bug);
12561 +out:
12562 +       return key;
12563 +}
12564 +
12565 +/* ---------------------------------------------------------------------- */
12566 +/*
12567 + * Aufs prohibits O_DIRECT by default even if the branch supports it.
12568 + * This behaviour is necessary to return an error from open(O_DIRECT) instead
12569 + * of the succeeding I/O. The dio mount option enables O_DIRECT and makes
12570 + * open(O_DIRECT) always succeed, but the succeeding I/O may return an error.
12571 + * See the aufs manual in detail.
12572 + */
12573 +static void dy_adx(struct au_dyaop *dyaop, int do_dx)
12574 +{
12575 +       if (!do_dx)
12576 +               dyaop->da_op.direct_IO = NULL;
12577 +       else
12578 +               dyaop->da_op.direct_IO = aufs_aop.direct_IO;
12579 +}
12580 +
12581 +static struct au_dyaop *dy_aget(struct au_branch *br,
12582 +                               const struct address_space_operations *h_aop,
12583 +                               int do_dx)
12584 +{
12585 +       struct au_dyaop *dyaop;
12586 +       struct au_dynop op;
12587 +
12588 +       op.dy_type = AuDy_AOP;
12589 +       op.dy_haop = h_aop;
12590 +       dyaop = (void *)dy_get(&op, br);
12591 +       if (IS_ERR(dyaop))
12592 +               goto out;
12593 +       dy_adx(dyaop, do_dx);
12594 +
12595 +out:
12596 +       return dyaop;
12597 +}
12598 +
12599 +int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
12600 +               struct inode *h_inode)
12601 +{
12602 +       int err, do_dx;
12603 +       struct super_block *sb;
12604 +       struct au_branch *br;
12605 +       struct au_dyaop *dyaop;
12606 +
12607 +       AuDebugOn(!S_ISREG(h_inode->i_mode));
12608 +       IiMustWriteLock(inode);
12609 +
12610 +       sb = inode->i_sb;
12611 +       br = au_sbr(sb, bindex);
12612 +       do_dx = !!au_opt_test(au_mntflags(sb), DIO);
12613 +       dyaop = dy_aget(br, h_inode->i_mapping->a_ops, do_dx);
12614 +       err = PTR_ERR(dyaop);
12615 +       if (IS_ERR(dyaop))
12616 +               /* unnecessary to call dy_fput() */
12617 +               goto out;
12618 +
12619 +       err = 0;
12620 +       inode->i_mapping->a_ops = &dyaop->da_op;
12621 +
12622 +out:
12623 +       return err;
12624 +}
12625 +
12626 +/*
12627 + * Is it safe to replace a_ops during the inode/file is in operation?
12628 + * Yes, I hope so.
12629 + */
12630 +int au_dy_irefresh(struct inode *inode)
12631 +{
12632 +       int err;
12633 +       aufs_bindex_t btop;
12634 +       struct inode *h_inode;
12635 +
12636 +       err = 0;
12637 +       if (S_ISREG(inode->i_mode)) {
12638 +               btop = au_ibtop(inode);
12639 +               h_inode = au_h_iptr(inode, btop);
12640 +               err = au_dy_iaop(inode, btop, h_inode);
12641 +       }
12642 +       return err;
12643 +}
12644 +
12645 +void au_dy_arefresh(int do_dx)
12646 +{
12647 +       struct hlist_bl_head *hbl;
12648 +       struct hlist_bl_node *pos;
12649 +       struct au_dykey *key;
12650 +
12651 +       hbl = dynop + AuDy_AOP;
12652 +       hlist_bl_lock(hbl);
12653 +       hlist_bl_for_each_entry(key, pos, hbl, dk_hnode)
12654 +               dy_adx((void *)key, do_dx);
12655 +       hlist_bl_unlock(hbl);
12656 +}
12657 +
12658 +/* ---------------------------------------------------------------------- */
12659 +
12660 +void __init au_dy_init(void)
12661 +{
12662 +       int i;
12663 +
12664 +       for (i = 0; i < AuDyLast; i++)
12665 +               INIT_HLIST_BL_HEAD(dynop + i);
12666 +}
12667 +
12668 +void au_dy_fin(void)
12669 +{
12670 +       int i;
12671 +
12672 +       for (i = 0; i < AuDyLast; i++)
12673 +               WARN_ON(!hlist_bl_empty(dynop + i));
12674 +}
12675 diff -urN /usr/share/empty/fs/aufs/dynop.h linux/fs/aufs/dynop.h
12676 --- /usr/share/empty/fs/aufs/dynop.h    1970-01-01 01:00:00.000000000 +0100
12677 +++ linux/fs/aufs/dynop.h       2022-03-21 14:49:05.723299677 +0100
12678 @@ -0,0 +1,77 @@
12679 +/* SPDX-License-Identifier: GPL-2.0 */
12680 +/*
12681 + * Copyright (C) 2010-2021 Junjiro R. Okajima
12682 + *
12683 + * This program, aufs is free software; you can redistribute it and/or modify
12684 + * it under the terms of the GNU General Public License as published by
12685 + * the Free Software Foundation; either version 2 of the License, or
12686 + * (at your option) any later version.
12687 + *
12688 + * This program is distributed in the hope that it will be useful,
12689 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12690 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12691 + * GNU General Public License for more details.
12692 + *
12693 + * You should have received a copy of the GNU General Public License
12694 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
12695 + */
12696 +
12697 +/*
12698 + * dynamically customizable operations (for regular files only)
12699 + */
12700 +
12701 +#ifndef __AUFS_DYNOP_H__
12702 +#define __AUFS_DYNOP_H__
12703 +
12704 +#ifdef __KERNEL__
12705 +
12706 +#include <linux/fs.h>
12707 +#include <linux/kref.h>
12708 +
12709 +enum {AuDy_AOP, AuDyLast};
12710 +
12711 +struct au_dynop {
12712 +       int                                             dy_type;
12713 +       union {
12714 +               const void                              *dy_hop;
12715 +               const struct address_space_operations   *dy_haop;
12716 +       };
12717 +};
12718 +
12719 +struct au_dykey {
12720 +       union {
12721 +               struct hlist_bl_node    dk_hnode;
12722 +               struct rcu_head         dk_rcu;
12723 +       };
12724 +       struct au_dynop         dk_op;
12725 +
12726 +       /*
12727 +        * during I am in the branch local array, kref is gotten. when the
12728 +        * branch is removed, kref is put.
12729 +        */
12730 +       struct kref             dk_kref;
12731 +};
12732 +
12733 +/* stop unioning since their sizes are very different from each other */
12734 +struct au_dyaop {
12735 +       struct au_dykey                 da_key;
12736 +       struct address_space_operations da_op; /* not const */
12737 +};
12738 +/* make sure that 'struct au_dykey *' can be any type */
12739 +static_assert(!offsetof(struct au_dyaop, da_key));
12740 +
12741 +/* ---------------------------------------------------------------------- */
12742 +
12743 +/* dynop.c */
12744 +struct au_branch;
12745 +void au_dy_put(struct au_dykey *key);
12746 +int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
12747 +               struct inode *h_inode);
12748 +int au_dy_irefresh(struct inode *inode);
12749 +void au_dy_arefresh(int do_dio);
12750 +
12751 +void __init au_dy_init(void);
12752 +void au_dy_fin(void);
12753 +
12754 +#endif /* __KERNEL__ */
12755 +#endif /* __AUFS_DYNOP_H__ */
12756 diff -urN /usr/share/empty/fs/aufs/export.c linux/fs/aufs/export.c
12757 --- /usr/share/empty/fs/aufs/export.c   1970-01-01 01:00:00.000000000 +0100
12758 +++ linux/fs/aufs/export.c      2022-03-21 14:49:05.723299677 +0100
12759 @@ -0,0 +1,830 @@
12760 +// SPDX-License-Identifier: GPL-2.0
12761 +/*
12762 + * Copyright (C) 2005-2021 Junjiro R. Okajima
12763 + *
12764 + * This program, aufs is free software; you can redistribute it and/or modify
12765 + * it under the terms of the GNU General Public License as published by
12766 + * the Free Software Foundation; either version 2 of the License, or
12767 + * (at your option) any later version.
12768 + *
12769 + * This program is distributed in the hope that it will be useful,
12770 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12771 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12772 + * GNU General Public License for more details.
12773 + *
12774 + * You should have received a copy of the GNU General Public License
12775 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
12776 + */
12777 +
12778 +/*
12779 + * export via nfs
12780 + */
12781 +
12782 +#include <linux/exportfs.h>
12783 +#include <linux/fs_struct.h>
12784 +#include <linux/nsproxy.h>
12785 +#include <linux/random.h>
12786 +#include <linux/writeback.h>
12787 +#include "aufs.h"
12788 +
12789 +union conv {
12790 +#ifdef CONFIG_AUFS_INO_T_64
12791 +       __u32 a[2];
12792 +#else
12793 +       __u32 a[1];
12794 +#endif
12795 +       ino_t ino;
12796 +};
12797 +
12798 +static ino_t decode_ino(__u32 *a)
12799 +{
12800 +       union conv u;
12801 +
12802 +       BUILD_BUG_ON(sizeof(u.ino) != sizeof(u.a));
12803 +       u.a[0] = a[0];
12804 +#ifdef CONFIG_AUFS_INO_T_64
12805 +       u.a[1] = a[1];
12806 +#endif
12807 +       return u.ino;
12808 +}
12809 +
12810 +static void encode_ino(__u32 *a, ino_t ino)
12811 +{
12812 +       union conv u;
12813 +
12814 +       u.ino = ino;
12815 +       a[0] = u.a[0];
12816 +#ifdef CONFIG_AUFS_INO_T_64
12817 +       a[1] = u.a[1];
12818 +#endif
12819 +}
12820 +
12821 +/* NFS file handle */
12822 +enum {
12823 +       Fh_br_id,
12824 +       Fh_sigen,
12825 +#ifdef CONFIG_AUFS_INO_T_64
12826 +       /* support 64bit inode number */
12827 +       Fh_ino1,
12828 +       Fh_ino2,
12829 +       Fh_dir_ino1,
12830 +       Fh_dir_ino2,
12831 +#else
12832 +       Fh_ino1,
12833 +       Fh_dir_ino1,
12834 +#endif
12835 +       Fh_igen,
12836 +       Fh_h_type,
12837 +       Fh_tail,
12838 +
12839 +       Fh_ino = Fh_ino1,
12840 +       Fh_dir_ino = Fh_dir_ino1
12841 +};
12842 +
12843 +static int au_test_anon(struct dentry *dentry)
12844 +{
12845 +       /* note: read d_flags without d_lock */
12846 +       return !!(dentry->d_flags & DCACHE_DISCONNECTED);
12847 +}
12848 +
12849 +int au_test_nfsd(void)
12850 +{
12851 +       int ret;
12852 +       struct task_struct *tsk = current;
12853 +       char comm[sizeof(tsk->comm)];
12854 +
12855 +       ret = 0;
12856 +       if (tsk->flags & PF_KTHREAD) {
12857 +               get_task_comm(comm, tsk);
12858 +               ret = !strcmp(comm, "nfsd");
12859 +       }
12860 +
12861 +       return ret;
12862 +}
12863 +
12864 +/* ---------------------------------------------------------------------- */
12865 +/* inode generation external table */
12866 +
12867 +void au_xigen_inc(struct inode *inode)
12868 +{
12869 +       loff_t pos;
12870 +       ssize_t sz;
12871 +       __u32 igen;
12872 +       struct super_block *sb;
12873 +       struct au_sbinfo *sbinfo;
12874 +
12875 +       sb = inode->i_sb;
12876 +       AuDebugOn(!au_opt_test(au_mntflags(sb), XINO));
12877 +
12878 +       sbinfo = au_sbi(sb);
12879 +       pos = inode->i_ino;
12880 +       pos *= sizeof(igen);
12881 +       igen = inode->i_generation + 1;
12882 +       sz = xino_fwrite(sbinfo->si_xigen, &igen, sizeof(igen), &pos);
12883 +       if (sz == sizeof(igen))
12884 +               return; /* success */
12885 +
12886 +       if (unlikely(sz >= 0))
12887 +               AuIOErr("xigen error (%zd)\n", sz);
12888 +}
12889 +
12890 +int au_xigen_new(struct inode *inode)
12891 +{
12892 +       int err;
12893 +       loff_t pos;
12894 +       ssize_t sz;
12895 +       struct super_block *sb;
12896 +       struct au_sbinfo *sbinfo;
12897 +       struct file *file;
12898 +
12899 +       err = 0;
12900 +       /* todo: dirty, at mount time */
12901 +       if (inode->i_ino == AUFS_ROOT_INO)
12902 +               goto out;
12903 +       sb = inode->i_sb;
12904 +       SiMustAnyLock(sb);
12905 +       if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
12906 +               goto out;
12907 +
12908 +       err = -EFBIG;
12909 +       pos = inode->i_ino;
12910 +       if (unlikely(au_loff_max / sizeof(inode->i_generation) - 1 < pos)) {
12911 +               AuIOErr1("too large i%lld\n", pos);
12912 +               goto out;
12913 +       }
12914 +       pos *= sizeof(inode->i_generation);
12915 +
12916 +       err = 0;
12917 +       sbinfo = au_sbi(sb);
12918 +       file = sbinfo->si_xigen;
12919 +       BUG_ON(!file);
12920 +
12921 +       if (vfsub_f_size_read(file)
12922 +           < pos + sizeof(inode->i_generation)) {
12923 +               inode->i_generation = atomic_inc_return(&sbinfo->si_xigen_next);
12924 +               sz = xino_fwrite(file, &inode->i_generation,
12925 +                                sizeof(inode->i_generation), &pos);
12926 +       } else
12927 +               sz = xino_fread(file, &inode->i_generation,
12928 +                               sizeof(inode->i_generation), &pos);
12929 +       if (sz == sizeof(inode->i_generation))
12930 +               goto out; /* success */
12931 +
12932 +       err = sz;
12933 +       if (unlikely(sz >= 0)) {
12934 +               err = -EIO;
12935 +               AuIOErr("xigen error (%zd)\n", sz);
12936 +       }
12937 +
12938 +out:
12939 +       return err;
12940 +}
12941 +
12942 +int au_xigen_set(struct super_block *sb, struct path *path)
12943 +{
12944 +       int err;
12945 +       struct au_sbinfo *sbinfo;
12946 +       struct file *file;
12947 +
12948 +       SiMustWriteLock(sb);
12949 +
12950 +       sbinfo = au_sbi(sb);
12951 +       file = au_xino_create2(sb, path, sbinfo->si_xigen);
12952 +       err = PTR_ERR(file);
12953 +       if (IS_ERR(file))
12954 +               goto out;
12955 +       err = 0;
12956 +       if (sbinfo->si_xigen)
12957 +               fput(sbinfo->si_xigen);
12958 +       sbinfo->si_xigen = file;
12959 +
12960 +out:
12961 +       AuTraceErr(err);
12962 +       return err;
12963 +}
12964 +
12965 +void au_xigen_clr(struct super_block *sb)
12966 +{
12967 +       struct au_sbinfo *sbinfo;
12968 +
12969 +       SiMustWriteLock(sb);
12970 +
12971 +       sbinfo = au_sbi(sb);
12972 +       if (sbinfo->si_xigen) {
12973 +               fput(sbinfo->si_xigen);
12974 +               sbinfo->si_xigen = NULL;
12975 +       }
12976 +}
12977 +
12978 +/* ---------------------------------------------------------------------- */
12979 +
12980 +static struct dentry *decode_by_ino(struct super_block *sb, ino_t ino,
12981 +                                   ino_t dir_ino)
12982 +{
12983 +       struct dentry *dentry, *d;
12984 +       struct inode *inode;
12985 +       unsigned int sigen;
12986 +
12987 +       dentry = NULL;
12988 +       inode = ilookup(sb, ino);
12989 +       if (!inode)
12990 +               goto out;
12991 +
12992 +       dentry = ERR_PTR(-ESTALE);
12993 +       sigen = au_sigen(sb);
12994 +       if (unlikely(au_is_bad_inode(inode)
12995 +                    || IS_DEADDIR(inode)
12996 +                    || sigen != au_iigen(inode, NULL)))
12997 +               goto out_iput;
12998 +
12999 +       dentry = NULL;
13000 +       if (!dir_ino || S_ISDIR(inode->i_mode))
13001 +               dentry = d_find_alias(inode);
13002 +       else {
13003 +               spin_lock(&inode->i_lock);
13004 +               hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
13005 +                       spin_lock(&d->d_lock);
13006 +                       if (!au_test_anon(d)
13007 +                           && d_inode(d->d_parent)->i_ino == dir_ino) {
13008 +                               dentry = dget_dlock(d);
13009 +                               spin_unlock(&d->d_lock);
13010 +                               break;
13011 +                       }
13012 +                       spin_unlock(&d->d_lock);
13013 +               }
13014 +               spin_unlock(&inode->i_lock);
13015 +       }
13016 +       if (unlikely(dentry && au_digen_test(dentry, sigen))) {
13017 +               /* need to refresh */
13018 +               dput(dentry);
13019 +               dentry = NULL;
13020 +       }
13021 +
13022 +out_iput:
13023 +       iput(inode);
13024 +out:
13025 +       AuTraceErrPtr(dentry);
13026 +       return dentry;
13027 +}
13028 +
13029 +/* ---------------------------------------------------------------------- */
13030 +
13031 +/* todo: dirty? */
13032 +/* if exportfs_decode_fh() passed vfsmount*, we could be happy */
13033 +
13034 +struct au_compare_mnt_args {
13035 +       /* input */
13036 +       struct super_block *sb;
13037 +
13038 +       /* output */
13039 +       struct vfsmount *mnt;
13040 +};
13041 +
13042 +static int au_compare_mnt(struct vfsmount *mnt, void *arg)
13043 +{
13044 +       struct au_compare_mnt_args *a = arg;
13045 +
13046 +       if (mnt->mnt_sb != a->sb)
13047 +               return 0;
13048 +       a->mnt = mntget(mnt);
13049 +       return 1;
13050 +}
13051 +
13052 +static struct vfsmount *au_mnt_get(struct super_block *sb)
13053 +{
13054 +       int err;
13055 +       struct path root;
13056 +       struct au_compare_mnt_args args = {
13057 +               .sb = sb
13058 +       };
13059 +
13060 +       get_fs_root(current->fs, &root);
13061 +       rcu_read_lock();
13062 +       err = iterate_mounts(au_compare_mnt, &args, root.mnt);
13063 +       rcu_read_unlock();
13064 +       path_put(&root);
13065 +       AuDebugOn(!err);
13066 +       AuDebugOn(!args.mnt);
13067 +       return args.mnt;
13068 +}
13069 +
13070 +struct au_nfsd_si_lock {
13071 +       unsigned int sigen;
13072 +       aufs_bindex_t bindex, br_id;
13073 +       unsigned char force_lock;
13074 +};
13075 +
13076 +static int si_nfsd_read_lock(struct super_block *sb,
13077 +                            struct au_nfsd_si_lock *nsi_lock)
13078 +{
13079 +       int err;
13080 +       aufs_bindex_t bindex;
13081 +
13082 +       si_read_lock(sb, AuLock_FLUSH);
13083 +
13084 +       /* branch id may be wrapped around */
13085 +       err = 0;
13086 +       bindex = au_br_index(sb, nsi_lock->br_id);
13087 +       if (bindex >= 0 && nsi_lock->sigen + AUFS_BRANCH_MAX > au_sigen(sb))
13088 +               goto out; /* success */
13089 +
13090 +       err = -ESTALE;
13091 +       bindex = -1;
13092 +       if (!nsi_lock->force_lock)
13093 +               si_read_unlock(sb);
13094 +
13095 +out:
13096 +       nsi_lock->bindex = bindex;
13097 +       return err;
13098 +}
13099 +
13100 +struct find_name_by_ino {
13101 +       struct dir_context ctx;
13102 +       int called, found;
13103 +       ino_t ino;
13104 +       char *name;
13105 +       int namelen;
13106 +};
13107 +
13108 +static int
13109 +find_name_by_ino(struct dir_context *ctx, const char *name, int namelen,
13110 +                loff_t offset, u64 ino, unsigned int d_type)
13111 +{
13112 +       struct find_name_by_ino *a = container_of(ctx, struct find_name_by_ino,
13113 +                                                 ctx);
13114 +
13115 +       a->called++;
13116 +       if (a->ino != ino)
13117 +               return 0;
13118 +
13119 +       memcpy(a->name, name, namelen);
13120 +       a->namelen = namelen;
13121 +       a->found = 1;
13122 +       return 1;
13123 +}
13124 +
13125 +static struct dentry *au_lkup_by_ino(struct path *path, ino_t ino,
13126 +                                    struct au_nfsd_si_lock *nsi_lock)
13127 +{
13128 +       struct dentry *dentry, *parent;
13129 +       struct file *file;
13130 +       struct inode *dir;
13131 +       struct find_name_by_ino arg = {
13132 +               .ctx = {
13133 +                       .actor = find_name_by_ino
13134 +               }
13135 +       };
13136 +       int err;
13137 +
13138 +       parent = path->dentry;
13139 +       if (nsi_lock)
13140 +               si_read_unlock(parent->d_sb);
13141 +       file = vfsub_dentry_open(path, au_dir_roflags);
13142 +       dentry = (void *)file;
13143 +       if (IS_ERR(file))
13144 +               goto out;
13145 +
13146 +       dentry = ERR_PTR(-ENOMEM);
13147 +       arg.name = (void *)__get_free_page(GFP_NOFS);
13148 +       if (unlikely(!arg.name))
13149 +               goto out_file;
13150 +       arg.ino = ino;
13151 +       arg.found = 0;
13152 +       do {
13153 +               arg.called = 0;
13154 +               /* smp_mb(); */
13155 +               err = vfsub_iterate_dir(file, &arg.ctx);
13156 +       } while (!err && !arg.found && arg.called);
13157 +       dentry = ERR_PTR(err);
13158 +       if (unlikely(err))
13159 +               goto out_name;
13160 +       /* instead of ENOENT */
13161 +       dentry = ERR_PTR(-ESTALE);
13162 +       if (!arg.found)
13163 +               goto out_name;
13164 +
13165 +       /* do not call vfsub_lkup_one() */
13166 +       dir = d_inode(parent);
13167 +       dentry = vfsub_lookup_one_len_unlocked(arg.name, path, arg.namelen);
13168 +       AuTraceErrPtr(dentry);
13169 +       if (IS_ERR(dentry))
13170 +               goto out_name;
13171 +       AuDebugOn(au_test_anon(dentry));
13172 +       if (unlikely(d_really_is_negative(dentry))) {
13173 +               dput(dentry);
13174 +               dentry = ERR_PTR(-ENOENT);
13175 +       }
13176 +
13177 +out_name:
13178 +       free_page((unsigned long)arg.name);
13179 +out_file:
13180 +       fput(file);
13181 +out:
13182 +       if (unlikely(nsi_lock
13183 +                    && si_nfsd_read_lock(parent->d_sb, nsi_lock) < 0))
13184 +               if (!IS_ERR(dentry)) {
13185 +                       dput(dentry);
13186 +                       dentry = ERR_PTR(-ESTALE);
13187 +               }
13188 +       AuTraceErrPtr(dentry);
13189 +       return dentry;
13190 +}
13191 +
13192 +static struct dentry *decode_by_dir_ino(struct super_block *sb, ino_t ino,
13193 +                                       ino_t dir_ino,
13194 +                                       struct au_nfsd_si_lock *nsi_lock)
13195 +{
13196 +       struct dentry *dentry;
13197 +       struct path path;
13198 +
13199 +       if (dir_ino != AUFS_ROOT_INO) {
13200 +               path.dentry = decode_by_ino(sb, dir_ino, 0);
13201 +               dentry = path.dentry;
13202 +               if (!path.dentry || IS_ERR(path.dentry))
13203 +                       goto out;
13204 +               AuDebugOn(au_test_anon(path.dentry));
13205 +       } else
13206 +               path.dentry = dget(sb->s_root);
13207 +
13208 +       path.mnt = au_mnt_get(sb);
13209 +       dentry = au_lkup_by_ino(&path, ino, nsi_lock);
13210 +       path_put(&path);
13211 +
13212 +out:
13213 +       AuTraceErrPtr(dentry);
13214 +       return dentry;
13215 +}
13216 +
13217 +/* ---------------------------------------------------------------------- */
13218 +
13219 +static int h_acceptable(void *expv, struct dentry *dentry)
13220 +{
13221 +       return 1;
13222 +}
13223 +
13224 +static char *au_build_path(struct dentry *h_parent, struct path *h_rootpath,
13225 +                          char *buf, int len, struct super_block *sb)
13226 +{
13227 +       char *p;
13228 +       int n;
13229 +       struct path path;
13230 +
13231 +       p = d_path(h_rootpath, buf, len);
13232 +       if (IS_ERR(p))
13233 +               goto out;
13234 +       n = strlen(p);
13235 +
13236 +       path.mnt = h_rootpath->mnt;
13237 +       path.dentry = h_parent;
13238 +       p = d_path(&path, buf, len);
13239 +       if (IS_ERR(p))
13240 +               goto out;
13241 +       if (n != 1)
13242 +               p += n;
13243 +
13244 +       path.mnt = au_mnt_get(sb);
13245 +       path.dentry = sb->s_root;
13246 +       p = d_path(&path, buf, len - strlen(p));
13247 +       mntput(path.mnt);
13248 +       if (IS_ERR(p))
13249 +               goto out;
13250 +       if (n != 1)
13251 +               p[strlen(p)] = '/';
13252 +
13253 +out:
13254 +       AuTraceErrPtr(p);
13255 +       return p;
13256 +}
13257 +
13258 +static
13259 +struct dentry *decode_by_path(struct super_block *sb, ino_t ino, __u32 *fh,
13260 +                             int fh_len, struct au_nfsd_si_lock *nsi_lock)
13261 +{
13262 +       struct dentry *dentry, *h_parent, *root;
13263 +       struct super_block *h_sb;
13264 +       char *pathname, *p;
13265 +       struct vfsmount *h_mnt;
13266 +       struct au_branch *br;
13267 +       int err;
13268 +       struct path path;
13269 +
13270 +       br = au_sbr(sb, nsi_lock->bindex);
13271 +       h_mnt = au_br_mnt(br);
13272 +       h_sb = h_mnt->mnt_sb;
13273 +       /* todo: call lower fh_to_dentry()? fh_to_parent()? */
13274 +       lockdep_off();
13275 +       h_parent = exportfs_decode_fh(h_mnt, (void *)(fh + Fh_tail),
13276 +                                     fh_len - Fh_tail, fh[Fh_h_type],
13277 +                                     h_acceptable, /*context*/NULL);
13278 +       lockdep_on();
13279 +       dentry = h_parent;
13280 +       if (unlikely(!h_parent || IS_ERR(h_parent))) {
13281 +               AuWarn1("%s decode_fh failed, %ld\n",
13282 +                       au_sbtype(h_sb), PTR_ERR(h_parent));
13283 +               goto out;
13284 +       }
13285 +       dentry = NULL;
13286 +       if (unlikely(au_test_anon(h_parent))) {
13287 +               AuWarn1("%s decode_fh returned a disconnected dentry\n",
13288 +                       au_sbtype(h_sb));
13289 +               goto out_h_parent;
13290 +       }
13291 +
13292 +       dentry = ERR_PTR(-ENOMEM);
13293 +       pathname = (void *)__get_free_page(GFP_NOFS);
13294 +       if (unlikely(!pathname))
13295 +               goto out_h_parent;
13296 +
13297 +       root = sb->s_root;
13298 +       path.mnt = h_mnt;
13299 +       di_read_lock_parent(root, !AuLock_IR);
13300 +       path.dentry = au_h_dptr(root, nsi_lock->bindex);
13301 +       di_read_unlock(root, !AuLock_IR);
13302 +       p = au_build_path(h_parent, &path, pathname, PAGE_SIZE, sb);
13303 +       dentry = (void *)p;
13304 +       if (IS_ERR(p))
13305 +               goto out_pathname;
13306 +
13307 +       si_read_unlock(sb);
13308 +       err = vfsub_kern_path(p, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &path);
13309 +       dentry = ERR_PTR(err);
13310 +       if (unlikely(err))
13311 +               goto out_relock;
13312 +
13313 +       dentry = ERR_PTR(-ENOENT);
13314 +       AuDebugOn(au_test_anon(path.dentry));
13315 +       if (unlikely(d_really_is_negative(path.dentry)))
13316 +               goto out_path;
13317 +
13318 +       if (ino != d_inode(path.dentry)->i_ino)
13319 +               dentry = au_lkup_by_ino(&path, ino, /*nsi_lock*/NULL);
13320 +       else
13321 +               dentry = dget(path.dentry);
13322 +
13323 +out_path:
13324 +       path_put(&path);
13325 +out_relock:
13326 +       if (unlikely(si_nfsd_read_lock(sb, nsi_lock) < 0))
13327 +               if (!IS_ERR(dentry)) {
13328 +                       dput(dentry);
13329 +                       dentry = ERR_PTR(-ESTALE);
13330 +               }
13331 +out_pathname:
13332 +       free_page((unsigned long)pathname);
13333 +out_h_parent:
13334 +       dput(h_parent);
13335 +out:
13336 +       AuTraceErrPtr(dentry);
13337 +       return dentry;
13338 +}
13339 +
13340 +/* ---------------------------------------------------------------------- */
13341 +
13342 +static struct dentry *
13343 +aufs_fh_to_dentry(struct super_block *sb, struct fid *fid, int fh_len,
13344 +                 int fh_type)
13345 +{
13346 +       struct dentry *dentry;
13347 +       __u32 *fh = fid->raw;
13348 +       struct au_branch *br;
13349 +       ino_t ino, dir_ino;
13350 +       struct au_nfsd_si_lock nsi_lock = {
13351 +               .force_lock     = 0
13352 +       };
13353 +
13354 +       dentry = ERR_PTR(-ESTALE);
13355 +       /* it should never happen, but the file handle is unreliable */
13356 +       if (unlikely(fh_len < Fh_tail))
13357 +               goto out;
13358 +       nsi_lock.sigen = fh[Fh_sigen];
13359 +       nsi_lock.br_id = fh[Fh_br_id];
13360 +
13361 +       /* branch id may be wrapped around */
13362 +       br = NULL;
13363 +       if (unlikely(si_nfsd_read_lock(sb, &nsi_lock)))
13364 +               goto out;
13365 +       nsi_lock.force_lock = 1;
13366 +
13367 +       /* is this inode still cached? */
13368 +       ino = decode_ino(fh + Fh_ino);
13369 +       /* it should never happen */
13370 +       if (unlikely(ino == AUFS_ROOT_INO))
13371 +               goto out_unlock;
13372 +
13373 +       dir_ino = decode_ino(fh + Fh_dir_ino);
13374 +       dentry = decode_by_ino(sb, ino, dir_ino);
13375 +       if (IS_ERR(dentry))
13376 +               goto out_unlock;
13377 +       if (dentry)
13378 +               goto accept;
13379 +
13380 +       /* is the parent dir cached? */
13381 +       br = au_sbr(sb, nsi_lock.bindex);
13382 +       au_lcnt_inc(&br->br_nfiles);
13383 +       dentry = decode_by_dir_ino(sb, ino, dir_ino, &nsi_lock);
13384 +       if (IS_ERR(dentry))
13385 +               goto out_unlock;
13386 +       if (dentry)
13387 +               goto accept;
13388 +
13389 +       /* lookup path */
13390 +       dentry = decode_by_path(sb, ino, fh, fh_len, &nsi_lock);
13391 +       if (IS_ERR(dentry))
13392 +               goto out_unlock;
13393 +       if (unlikely(!dentry))
13394 +               /* todo?: make it ESTALE */
13395 +               goto out_unlock;
13396 +
13397 +accept:
13398 +       if (!au_digen_test(dentry, au_sigen(sb))
13399 +           && d_inode(dentry)->i_generation == fh[Fh_igen])
13400 +               goto out_unlock; /* success */
13401 +
13402 +       dput(dentry);
13403 +       dentry = ERR_PTR(-ESTALE);
13404 +out_unlock:
13405 +       if (br)
13406 +               au_lcnt_dec(&br->br_nfiles);
13407 +       si_read_unlock(sb);
13408 +out:
13409 +       AuTraceErrPtr(dentry);
13410 +       return dentry;
13411 +}
13412 +
13413 +#if 0 /* reserved for future use */
13414 +/* support subtreecheck option */
13415 +static struct dentry *aufs_fh_to_parent(struct super_block *sb, struct fid *fid,
13416 +                                       int fh_len, int fh_type)
13417 +{
13418 +       struct dentry *parent;
13419 +       __u32 *fh = fid->raw;
13420 +       ino_t dir_ino;
13421 +
13422 +       dir_ino = decode_ino(fh + Fh_dir_ino);
13423 +       parent = decode_by_ino(sb, dir_ino, 0);
13424 +       if (IS_ERR(parent))
13425 +               goto out;
13426 +       if (!parent)
13427 +               parent = decode_by_path(sb, au_br_index(sb, fh[Fh_br_id]),
13428 +                                       dir_ino, fh, fh_len);
13429 +
13430 +out:
13431 +       AuTraceErrPtr(parent);
13432 +       return parent;
13433 +}
13434 +#endif
13435 +
13436 +/* ---------------------------------------------------------------------- */
13437 +
13438 +static int aufs_encode_fh(struct inode *inode, __u32 *fh, int *max_len,
13439 +                         struct inode *dir)
13440 +{
13441 +       int err;
13442 +       aufs_bindex_t bindex;
13443 +       struct super_block *sb, *h_sb;
13444 +       struct dentry *dentry, *parent, *h_parent;
13445 +       struct inode *h_dir;
13446 +       struct au_branch *br;
13447 +
13448 +       err = -ENOSPC;
13449 +       if (unlikely(*max_len <= Fh_tail)) {
13450 +               AuWarn1("NFSv2 client (max_len %d)?\n", *max_len);
13451 +               goto out;
13452 +       }
13453 +
13454 +       err = FILEID_ROOT;
13455 +       if (inode->i_ino == AUFS_ROOT_INO) {
13456 +               AuDebugOn(inode->i_ino != AUFS_ROOT_INO);
13457 +               goto out;
13458 +       }
13459 +
13460 +       h_parent = NULL;
13461 +       sb = inode->i_sb;
13462 +       err = si_read_lock(sb, AuLock_FLUSH);
13463 +       if (unlikely(err))
13464 +               goto out;
13465 +
13466 +#ifdef CONFIG_AUFS_DEBUG
13467 +       if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
13468 +               AuWarn1("NFS-exporting requires xino\n");
13469 +#endif
13470 +       err = -EIO;
13471 +       parent = NULL;
13472 +       ii_read_lock_child(inode);
13473 +       bindex = au_ibtop(inode);
13474 +       if (!dir) {
13475 +               dentry = d_find_any_alias(inode);
13476 +               if (unlikely(!dentry))
13477 +                       goto out_unlock;
13478 +               AuDebugOn(au_test_anon(dentry));
13479 +               parent = dget_parent(dentry);
13480 +               dput(dentry);
13481 +               if (unlikely(!parent))
13482 +                       goto out_unlock;
13483 +               if (d_really_is_positive(parent))
13484 +                       dir = d_inode(parent);
13485 +       }
13486 +
13487 +       ii_read_lock_parent(dir);
13488 +       h_dir = au_h_iptr(dir, bindex);
13489 +       ii_read_unlock(dir);
13490 +       if (unlikely(!h_dir))
13491 +               goto out_parent;
13492 +       h_parent = d_find_any_alias(h_dir);
13493 +       if (unlikely(!h_parent))
13494 +               goto out_hparent;
13495 +
13496 +       err = -EPERM;
13497 +       br = au_sbr(sb, bindex);
13498 +       h_sb = au_br_sb(br);
13499 +       if (unlikely(!h_sb->s_export_op)) {
13500 +               AuErr1("%s branch is not exportable\n", au_sbtype(h_sb));
13501 +               goto out_hparent;
13502 +       }
13503 +
13504 +       fh[Fh_br_id] = br->br_id;
13505 +       fh[Fh_sigen] = au_sigen(sb);
13506 +       encode_ino(fh + Fh_ino, inode->i_ino);
13507 +       encode_ino(fh + Fh_dir_ino, dir->i_ino);
13508 +       fh[Fh_igen] = inode->i_generation;
13509 +
13510 +       *max_len -= Fh_tail;
13511 +       fh[Fh_h_type] = exportfs_encode_fh(h_parent, (void *)(fh + Fh_tail),
13512 +                                          max_len,
13513 +                                          /*connectable or subtreecheck*/0);
13514 +       err = fh[Fh_h_type];
13515 +       *max_len += Fh_tail;
13516 +       /* todo: macros? */
13517 +       if (err != FILEID_INVALID)
13518 +               err = 99;
13519 +       else
13520 +               AuWarn1("%s encode_fh failed\n", au_sbtype(h_sb));
13521 +
13522 +out_hparent:
13523 +       dput(h_parent);
13524 +out_parent:
13525 +       dput(parent);
13526 +out_unlock:
13527 +       ii_read_unlock(inode);
13528 +       si_read_unlock(sb);
13529 +out:
13530 +       if (unlikely(err < 0))
13531 +               err = FILEID_INVALID;
13532 +       return err;
13533 +}
13534 +
13535 +/* ---------------------------------------------------------------------- */
13536 +
13537 +static int aufs_commit_metadata(struct inode *inode)
13538 +{
13539 +       int err;
13540 +       aufs_bindex_t bindex;
13541 +       struct super_block *sb;
13542 +       struct inode *h_inode;
13543 +       int (*f)(struct inode *inode);
13544 +
13545 +       sb = inode->i_sb;
13546 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
13547 +       ii_write_lock_child(inode);
13548 +       bindex = au_ibtop(inode);
13549 +       AuDebugOn(bindex < 0);
13550 +       h_inode = au_h_iptr(inode, bindex);
13551 +
13552 +       f = h_inode->i_sb->s_export_op->commit_metadata;
13553 +       if (f)
13554 +               err = f(h_inode);
13555 +       else
13556 +               err = sync_inode_metadata(h_inode, /*wait*/1);
13557 +
13558 +       au_cpup_attr_timesizes(inode);
13559 +       ii_write_unlock(inode);
13560 +       si_read_unlock(sb);
13561 +       return err;
13562 +}
13563 +
13564 +/* ---------------------------------------------------------------------- */
13565 +
13566 +static struct export_operations aufs_export_op = {
13567 +       .fh_to_dentry           = aufs_fh_to_dentry,
13568 +       /* .fh_to_parent        = aufs_fh_to_parent, */
13569 +       .encode_fh              = aufs_encode_fh,
13570 +       .commit_metadata        = aufs_commit_metadata
13571 +};
13572 +
13573 +void au_export_init(struct super_block *sb)
13574 +{
13575 +       struct au_sbinfo *sbinfo;
13576 +       __u32 u;
13577 +
13578 +       BUILD_BUG_ON_MSG(IS_BUILTIN(CONFIG_AUFS_FS)
13579 +                        && IS_MODULE(CONFIG_EXPORTFS),
13580 +                        AUFS_NAME ": unsupported configuration "
13581 +                        "CONFIG_EXPORTFS=m and CONFIG_AUFS_FS=y");
13582 +
13583 +       sb->s_export_op = &aufs_export_op;
13584 +       sbinfo = au_sbi(sb);
13585 +       sbinfo->si_xigen = NULL;
13586 +       get_random_bytes(&u, sizeof(u));
13587 +       BUILD_BUG_ON(sizeof(u) != sizeof(int));
13588 +       atomic_set(&sbinfo->si_xigen_next, u);
13589 +}
13590 diff -urN /usr/share/empty/fs/aufs/fhsm.c linux/fs/aufs/fhsm.c
13591 --- /usr/share/empty/fs/aufs/fhsm.c     1970-01-01 01:00:00.000000000 +0100
13592 +++ linux/fs/aufs/fhsm.c        2022-03-21 14:49:05.723299677 +0100
13593 @@ -0,0 +1,427 @@
13594 +// SPDX-License-Identifier: GPL-2.0
13595 +/*
13596 + * Copyright (C) 2011-2021 Junjiro R. Okajima
13597 + *
13598 + * This program, aufs is free software; you can redistribute it and/or modify
13599 + * it under the terms of the GNU General Public License as published by
13600 + * the Free Software Foundation; either version 2 of the License, or
13601 + * (at your option) any later version.
13602 + *
13603 + * This program is distributed in the hope that it will be useful,
13604 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
13605 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13606 + * GNU General Public License for more details.
13607 + *
13608 + * You should have received a copy of the GNU General Public License
13609 + * along with this program; if not, write to the Free Software
13610 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
13611 + */
13612 +
13613 +/*
13614 + * File-based Hierarchy Storage Management
13615 + */
13616 +
13617 +#include <linux/anon_inodes.h>
13618 +#include <linux/poll.h>
13619 +#include <linux/seq_file.h>
13620 +#include <linux/statfs.h>
13621 +#include "aufs.h"
13622 +
13623 +static aufs_bindex_t au_fhsm_bottom(struct super_block *sb)
13624 +{
13625 +       struct au_sbinfo *sbinfo;
13626 +       struct au_fhsm *fhsm;
13627 +
13628 +       SiMustAnyLock(sb);
13629 +
13630 +       sbinfo = au_sbi(sb);
13631 +       fhsm = &sbinfo->si_fhsm;
13632 +       AuDebugOn(!fhsm);
13633 +       return fhsm->fhsm_bottom;
13634 +}
13635 +
13636 +void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex)
13637 +{
13638 +       struct au_sbinfo *sbinfo;
13639 +       struct au_fhsm *fhsm;
13640 +
13641 +       SiMustWriteLock(sb);
13642 +
13643 +       sbinfo = au_sbi(sb);
13644 +       fhsm = &sbinfo->si_fhsm;
13645 +       AuDebugOn(!fhsm);
13646 +       fhsm->fhsm_bottom = bindex;
13647 +}
13648 +
13649 +/* ---------------------------------------------------------------------- */
13650 +
13651 +static int au_fhsm_test_jiffy(struct au_sbinfo *sbinfo, struct au_branch *br)
13652 +{
13653 +       struct au_br_fhsm *bf;
13654 +
13655 +       bf = br->br_fhsm;
13656 +       MtxMustLock(&bf->bf_lock);
13657 +
13658 +       return !bf->bf_readable
13659 +               || time_after(jiffies,
13660 +                             bf->bf_jiffy + sbinfo->si_fhsm.fhsm_expire);
13661 +}
13662 +
13663 +/* ---------------------------------------------------------------------- */
13664 +
13665 +static void au_fhsm_notify(struct super_block *sb, int val)
13666 +{
13667 +       struct au_sbinfo *sbinfo;
13668 +       struct au_fhsm *fhsm;
13669 +
13670 +       SiMustAnyLock(sb);
13671 +
13672 +       sbinfo = au_sbi(sb);
13673 +       fhsm = &sbinfo->si_fhsm;
13674 +       if (au_fhsm_pid(fhsm)
13675 +           && atomic_read(&fhsm->fhsm_readable) != -1) {
13676 +               atomic_set(&fhsm->fhsm_readable, val);
13677 +               if (val)
13678 +                       wake_up(&fhsm->fhsm_wqh);
13679 +       }
13680 +}
13681 +
13682 +static int au_fhsm_stfs(struct super_block *sb, aufs_bindex_t bindex,
13683 +                       struct aufs_stfs *rstfs, int do_lock, int do_notify)
13684 +{
13685 +       int err;
13686 +       struct au_branch *br;
13687 +       struct au_br_fhsm *bf;
13688 +
13689 +       br = au_sbr(sb, bindex);
13690 +       AuDebugOn(au_br_rdonly(br));
13691 +       bf = br->br_fhsm;
13692 +       AuDebugOn(!bf);
13693 +
13694 +       if (do_lock)
13695 +               mutex_lock(&bf->bf_lock);
13696 +       else
13697 +               MtxMustLock(&bf->bf_lock);
13698 +
13699 +       /* sb->s_root for NFS is unreliable */
13700 +       err = au_br_stfs(br, &bf->bf_stfs);
13701 +       if (unlikely(err)) {
13702 +               AuErr1("FHSM failed (%d), b%d, ignored.\n", bindex, err);
13703 +               goto out;
13704 +       }
13705 +
13706 +       bf->bf_jiffy = jiffies;
13707 +       bf->bf_readable = 1;
13708 +       if (do_notify)
13709 +               au_fhsm_notify(sb, /*val*/1);
13710 +       if (rstfs)
13711 +               *rstfs = bf->bf_stfs;
13712 +
13713 +out:
13714 +       if (do_lock)
13715 +               mutex_unlock(&bf->bf_lock);
13716 +       au_fhsm_notify(sb, /*val*/1);
13717 +
13718 +       return err;
13719 +}
13720 +
13721 +void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force)
13722 +{
13723 +       int err;
13724 +       struct au_sbinfo *sbinfo;
13725 +       struct au_fhsm *fhsm;
13726 +       struct au_branch *br;
13727 +       struct au_br_fhsm *bf;
13728 +
13729 +       AuDbg("b%d, force %d\n", bindex, force);
13730 +       SiMustAnyLock(sb);
13731 +
13732 +       sbinfo = au_sbi(sb);
13733 +       fhsm = &sbinfo->si_fhsm;
13734 +       if (!au_ftest_si(sbinfo, FHSM)
13735 +           || fhsm->fhsm_bottom == bindex)
13736 +               return;
13737 +
13738 +       br = au_sbr(sb, bindex);
13739 +       bf = br->br_fhsm;
13740 +       AuDebugOn(!bf);
13741 +       mutex_lock(&bf->bf_lock);
13742 +       if (force
13743 +           || au_fhsm_pid(fhsm)
13744 +           || au_fhsm_test_jiffy(sbinfo, br))
13745 +               err = au_fhsm_stfs(sb, bindex, /*rstfs*/NULL, /*do_lock*/0,
13746 +                                 /*do_notify*/1);
13747 +       mutex_unlock(&bf->bf_lock);
13748 +}
13749 +
13750 +void au_fhsm_wrote_all(struct super_block *sb, int force)
13751 +{
13752 +       aufs_bindex_t bindex, bbot;
13753 +       struct au_branch *br;
13754 +
13755 +       /* exclude the bottom */
13756 +       bbot = au_fhsm_bottom(sb);
13757 +       for (bindex = 0; bindex < bbot; bindex++) {
13758 +               br = au_sbr(sb, bindex);
13759 +               if (au_br_fhsm(br->br_perm))
13760 +                       au_fhsm_wrote(sb, bindex, force);
13761 +       }
13762 +}
13763 +
13764 +/* ---------------------------------------------------------------------- */
13765 +
13766 +static __poll_t au_fhsm_poll(struct file *file, struct poll_table_struct *wait)
13767 +{
13768 +       __poll_t mask;
13769 +       struct au_sbinfo *sbinfo;
13770 +       struct au_fhsm *fhsm;
13771 +
13772 +       mask = 0;
13773 +       sbinfo = file->private_data;
13774 +       fhsm = &sbinfo->si_fhsm;
13775 +       poll_wait(file, &fhsm->fhsm_wqh, wait);
13776 +       if (atomic_read(&fhsm->fhsm_readable))
13777 +               mask = EPOLLIN /* | EPOLLRDNORM */;
13778 +
13779 +       if (!mask)
13780 +               AuDbg("mask 0x%x\n", mask);
13781 +       return mask;
13782 +}
13783 +
13784 +static int au_fhsm_do_read_one(struct aufs_stbr __user *stbr,
13785 +                             struct aufs_stfs *stfs, __s16 brid)
13786 +{
13787 +       int err;
13788 +
13789 +       err = copy_to_user(&stbr->stfs, stfs, sizeof(*stfs));
13790 +       if (!err)
13791 +               err = __put_user(brid, &stbr->brid);
13792 +       if (unlikely(err))
13793 +               err = -EFAULT;
13794 +
13795 +       return err;
13796 +}
13797 +
13798 +static ssize_t au_fhsm_do_read(struct super_block *sb,
13799 +                              struct aufs_stbr __user *stbr, size_t count)
13800 +{
13801 +       ssize_t err;
13802 +       int nstbr;
13803 +       aufs_bindex_t bindex, bbot;
13804 +       struct au_branch *br;
13805 +       struct au_br_fhsm *bf;
13806 +
13807 +       /* except the bottom branch */
13808 +       err = 0;
13809 +       nstbr = 0;
13810 +       bbot = au_fhsm_bottom(sb);
13811 +       for (bindex = 0; !err && bindex < bbot; bindex++) {
13812 +               br = au_sbr(sb, bindex);
13813 +               if (!au_br_fhsm(br->br_perm))
13814 +                       continue;
13815 +
13816 +               bf = br->br_fhsm;
13817 +               mutex_lock(&bf->bf_lock);
13818 +               if (bf->bf_readable) {
13819 +                       err = -EFAULT;
13820 +                       if (count >= sizeof(*stbr))
13821 +                               err = au_fhsm_do_read_one(stbr++, &bf->bf_stfs,
13822 +                                                         br->br_id);
13823 +                       if (!err) {
13824 +                               bf->bf_readable = 0;
13825 +                               count -= sizeof(*stbr);
13826 +                               nstbr++;
13827 +                       }
13828 +               }
13829 +               mutex_unlock(&bf->bf_lock);
13830 +       }
13831 +       if (!err)
13832 +               err = sizeof(*stbr) * nstbr;
13833 +
13834 +       return err;
13835 +}
13836 +
13837 +static ssize_t au_fhsm_read(struct file *file, char __user *buf, size_t count,
13838 +                          loff_t *pos)
13839 +{
13840 +       ssize_t err;
13841 +       int readable;
13842 +       aufs_bindex_t nfhsm, bindex, bbot;
13843 +       struct au_sbinfo *sbinfo;
13844 +       struct au_fhsm *fhsm;
13845 +       struct au_branch *br;
13846 +       struct super_block *sb;
13847 +
13848 +       err = 0;
13849 +       sbinfo = file->private_data;
13850 +       fhsm = &sbinfo->si_fhsm;
13851 +need_data:
13852 +       spin_lock_irq(&fhsm->fhsm_wqh.lock);
13853 +       if (!atomic_read(&fhsm->fhsm_readable)) {
13854 +               if (vfsub_file_flags(file) & O_NONBLOCK)
13855 +                       err = -EAGAIN;
13856 +               else
13857 +                       err = wait_event_interruptible_locked_irq
13858 +                               (fhsm->fhsm_wqh,
13859 +                                atomic_read(&fhsm->fhsm_readable));
13860 +       }
13861 +       spin_unlock_irq(&fhsm->fhsm_wqh.lock);
13862 +       if (unlikely(err))
13863 +               goto out;
13864 +
13865 +       /* sb may already be dead */
13866 +       au_rw_read_lock(&sbinfo->si_rwsem);
13867 +       readable = atomic_read(&fhsm->fhsm_readable);
13868 +       if (readable > 0) {
13869 +               sb = sbinfo->si_sb;
13870 +               AuDebugOn(!sb);
13871 +               /* exclude the bottom branch */
13872 +               nfhsm = 0;
13873 +               bbot = au_fhsm_bottom(sb);
13874 +               for (bindex = 0; bindex < bbot; bindex++) {
13875 +                       br = au_sbr(sb, bindex);
13876 +                       if (au_br_fhsm(br->br_perm))
13877 +                               nfhsm++;
13878 +               }
13879 +               err = -EMSGSIZE;
13880 +               if (nfhsm * sizeof(struct aufs_stbr) <= count) {
13881 +                       atomic_set(&fhsm->fhsm_readable, 0);
13882 +                       err = au_fhsm_do_read(sbinfo->si_sb, (void __user *)buf,
13883 +                                            count);
13884 +               }
13885 +       }
13886 +       au_rw_read_unlock(&sbinfo->si_rwsem);
13887 +       if (!readable)
13888 +               goto need_data;
13889 +
13890 +out:
13891 +       return err;
13892 +}
13893 +
13894 +static int au_fhsm_release(struct inode *inode, struct file *file)
13895 +{
13896 +       struct au_sbinfo *sbinfo;
13897 +       struct au_fhsm *fhsm;
13898 +
13899 +       /* sb may already be dead */
13900 +       sbinfo = file->private_data;
13901 +       fhsm = &sbinfo->si_fhsm;
13902 +       spin_lock(&fhsm->fhsm_spin);
13903 +       fhsm->fhsm_pid = 0;
13904 +       spin_unlock(&fhsm->fhsm_spin);
13905 +       kobject_put(&sbinfo->si_kobj);
13906 +
13907 +       return 0;
13908 +}
13909 +
13910 +static const struct file_operations au_fhsm_fops = {
13911 +       .owner          = THIS_MODULE,
13912 +       .llseek         = noop_llseek,
13913 +       .read           = au_fhsm_read,
13914 +       .poll           = au_fhsm_poll,
13915 +       .release        = au_fhsm_release
13916 +};
13917 +
13918 +int au_fhsm_fd(struct super_block *sb, int oflags)
13919 +{
13920 +       int err, fd;
13921 +       struct au_sbinfo *sbinfo;
13922 +       struct au_fhsm *fhsm;
13923 +
13924 +       err = -EPERM;
13925 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
13926 +               goto out;
13927 +
13928 +       err = -EINVAL;
13929 +       if (unlikely(oflags & ~(O_CLOEXEC | O_NONBLOCK)))
13930 +               goto out;
13931 +
13932 +       err = 0;
13933 +       sbinfo = au_sbi(sb);
13934 +       fhsm = &sbinfo->si_fhsm;
13935 +       spin_lock(&fhsm->fhsm_spin);
13936 +       if (!fhsm->fhsm_pid)
13937 +               fhsm->fhsm_pid = current->pid;
13938 +       else
13939 +               err = -EBUSY;
13940 +       spin_unlock(&fhsm->fhsm_spin);
13941 +       if (unlikely(err))
13942 +               goto out;
13943 +
13944 +       oflags |= O_RDONLY;
13945 +       /* oflags |= FMODE_NONOTIFY; */
13946 +       fd = anon_inode_getfd("[aufs_fhsm]", &au_fhsm_fops, sbinfo, oflags);
13947 +       err = fd;
13948 +       if (unlikely(fd < 0))
13949 +               goto out_pid;
13950 +
13951 +       /* succeed regardless 'fhsm' status */
13952 +       kobject_get(&sbinfo->si_kobj);
13953 +       si_noflush_read_lock(sb);
13954 +       if (au_ftest_si(sbinfo, FHSM))
13955 +               au_fhsm_wrote_all(sb, /*force*/0);
13956 +       si_read_unlock(sb);
13957 +       goto out; /* success */
13958 +
13959 +out_pid:
13960 +       spin_lock(&fhsm->fhsm_spin);
13961 +       fhsm->fhsm_pid = 0;
13962 +       spin_unlock(&fhsm->fhsm_spin);
13963 +out:
13964 +       AuTraceErr(err);
13965 +       return err;
13966 +}
13967 +
13968 +/* ---------------------------------------------------------------------- */
13969 +
13970 +int au_fhsm_br_alloc(struct au_branch *br)
13971 +{
13972 +       int err;
13973 +
13974 +       err = 0;
13975 +       br->br_fhsm = kmalloc(sizeof(*br->br_fhsm), GFP_NOFS);
13976 +       if (br->br_fhsm)
13977 +               au_br_fhsm_init(br->br_fhsm);
13978 +       else
13979 +               err = -ENOMEM;
13980 +
13981 +       return err;
13982 +}
13983 +
13984 +/* ---------------------------------------------------------------------- */
13985 +
13986 +void au_fhsm_fin(struct super_block *sb)
13987 +{
13988 +       au_fhsm_notify(sb, /*val*/-1);
13989 +}
13990 +
13991 +void au_fhsm_init(struct au_sbinfo *sbinfo)
13992 +{
13993 +       struct au_fhsm *fhsm;
13994 +
13995 +       fhsm = &sbinfo->si_fhsm;
13996 +       spin_lock_init(&fhsm->fhsm_spin);
13997 +       init_waitqueue_head(&fhsm->fhsm_wqh);
13998 +       atomic_set(&fhsm->fhsm_readable, 0);
13999 +       fhsm->fhsm_expire
14000 +               = msecs_to_jiffies(AUFS_FHSM_CACHE_DEF_SEC * MSEC_PER_SEC);
14001 +       fhsm->fhsm_bottom = -1;
14002 +}
14003 +
14004 +void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec)
14005 +{
14006 +       sbinfo->si_fhsm.fhsm_expire
14007 +               = msecs_to_jiffies(sec * MSEC_PER_SEC);
14008 +}
14009 +
14010 +void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo)
14011 +{
14012 +       unsigned int u;
14013 +
14014 +       if (!au_ftest_si(sbinfo, FHSM))
14015 +               return;
14016 +
14017 +       u = jiffies_to_msecs(sbinfo->si_fhsm.fhsm_expire) / MSEC_PER_SEC;
14018 +       if (u != AUFS_FHSM_CACHE_DEF_SEC)
14019 +               seq_printf(seq, ",fhsm_sec=%u", u);
14020 +}
14021 diff -urN /usr/share/empty/fs/aufs/file.c linux/fs/aufs/file.c
14022 --- /usr/share/empty/fs/aufs/file.c     1970-01-01 01:00:00.000000000 +0100
14023 +++ linux/fs/aufs/file.c        2022-03-21 14:49:05.723299677 +0100
14024 @@ -0,0 +1,863 @@
14025 +// SPDX-License-Identifier: GPL-2.0
14026 +/*
14027 + * Copyright (C) 2005-2021 Junjiro R. Okajima
14028 + *
14029 + * This program, aufs is free software; you can redistribute it and/or modify
14030 + * it under the terms of the GNU General Public License as published by
14031 + * the Free Software Foundation; either version 2 of the License, or
14032 + * (at your option) any later version.
14033 + *
14034 + * This program is distributed in the hope that it will be useful,
14035 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
14036 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14037 + * GNU General Public License for more details.
14038 + *
14039 + * You should have received a copy of the GNU General Public License
14040 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
14041 + */
14042 +
14043 +/*
14044 + * handling file/dir, and address_space operation
14045 + */
14046 +
14047 +#ifdef CONFIG_AUFS_DEBUG
14048 +#include <linux/migrate.h>
14049 +#endif
14050 +#include <linux/pagemap.h>
14051 +#include "aufs.h"
14052 +
14053 +/* drop flags for writing */
14054 +unsigned int au_file_roflags(unsigned int flags)
14055 +{
14056 +       flags &= ~(O_WRONLY | O_RDWR | O_APPEND | O_CREAT | O_TRUNC);
14057 +       flags |= O_RDONLY | O_NOATIME;
14058 +       return flags;
14059 +}
14060 +
14061 +/* common functions to regular file and dir */
14062 +struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
14063 +                      struct file *file, int force_wr)
14064 +{
14065 +       struct file *h_file;
14066 +       struct dentry *h_dentry;
14067 +       struct inode *h_inode;
14068 +       struct super_block *sb;
14069 +       struct au_branch *br;
14070 +       struct path h_path;
14071 +       int err;
14072 +
14073 +       /* a race condition can happen between open and unlink/rmdir */
14074 +       h_file = ERR_PTR(-ENOENT);
14075 +       h_dentry = au_h_dptr(dentry, bindex);
14076 +       if (au_test_nfsd() && (!h_dentry || d_is_negative(h_dentry)))
14077 +               goto out;
14078 +       h_inode = d_inode(h_dentry);
14079 +       spin_lock(&h_dentry->d_lock);
14080 +       err = (!d_unhashed(dentry) && d_unlinked(h_dentry))
14081 +               /* || !d_inode(dentry)->i_nlink */
14082 +               ;
14083 +       spin_unlock(&h_dentry->d_lock);
14084 +       if (unlikely(err))
14085 +               goto out;
14086 +
14087 +       sb = dentry->d_sb;
14088 +       br = au_sbr(sb, bindex);
14089 +       err = au_br_test_oflag(flags, br);
14090 +       h_file = ERR_PTR(err);
14091 +       if (unlikely(err))
14092 +               goto out;
14093 +
14094 +       /* drop flags for writing */
14095 +       if (au_test_ro(sb, bindex, d_inode(dentry))) {
14096 +               if (force_wr && !(flags & O_WRONLY))
14097 +                       force_wr = 0;
14098 +               flags = au_file_roflags(flags);
14099 +               if (force_wr) {
14100 +                       h_file = ERR_PTR(-EROFS);
14101 +                       flags = au_file_roflags(flags);
14102 +                       if (unlikely(vfsub_native_ro(h_inode)
14103 +                                    || IS_APPEND(h_inode)))
14104 +                               goto out;
14105 +                       flags &= ~O_ACCMODE;
14106 +                       flags |= O_WRONLY;
14107 +               }
14108 +       }
14109 +       flags &= ~O_CREAT;
14110 +       au_lcnt_inc(&br->br_nfiles);
14111 +       h_path.dentry = h_dentry;
14112 +       h_path.mnt = au_br_mnt(br);
14113 +       h_file = vfsub_dentry_open(&h_path, flags);
14114 +       if (IS_ERR(h_file))
14115 +               goto out_br;
14116 +
14117 +       if (flags & __FMODE_EXEC) {
14118 +               err = deny_write_access(h_file);
14119 +               if (unlikely(err)) {
14120 +                       fput(h_file);
14121 +                       h_file = ERR_PTR(err);
14122 +                       goto out_br;
14123 +               }
14124 +       }
14125 +       fsnotify_open(h_file);
14126 +       goto out; /* success */
14127 +
14128 +out_br:
14129 +       au_lcnt_dec(&br->br_nfiles);
14130 +out:
14131 +       return h_file;
14132 +}
14133 +
14134 +static int au_cmoo(struct dentry *dentry)
14135 +{
14136 +       int err, cmoo, matched;
14137 +       unsigned int udba;
14138 +       struct path h_path;
14139 +       struct au_pin pin;
14140 +       struct au_cp_generic cpg = {
14141 +               .dentry = dentry,
14142 +               .bdst   = -1,
14143 +               .bsrc   = -1,
14144 +               .len    = -1,
14145 +               .pin    = &pin,
14146 +               .flags  = AuCpup_DTIME | AuCpup_HOPEN
14147 +       };
14148 +       struct inode *delegated;
14149 +       struct super_block *sb;
14150 +       struct au_sbinfo *sbinfo;
14151 +       struct au_fhsm *fhsm;
14152 +       pid_t pid;
14153 +       struct au_branch *br;
14154 +       struct dentry *parent;
14155 +       struct au_hinode *hdir;
14156 +
14157 +       DiMustWriteLock(dentry);
14158 +       IiMustWriteLock(d_inode(dentry));
14159 +
14160 +       err = 0;
14161 +       if (IS_ROOT(dentry))
14162 +               goto out;
14163 +       cpg.bsrc = au_dbtop(dentry);
14164 +       if (!cpg.bsrc)
14165 +               goto out;
14166 +
14167 +       sb = dentry->d_sb;
14168 +       sbinfo = au_sbi(sb);
14169 +       fhsm = &sbinfo->si_fhsm;
14170 +       pid = au_fhsm_pid(fhsm);
14171 +       rcu_read_lock();
14172 +       matched = (pid
14173 +                  && (current->pid == pid
14174 +                      || rcu_dereference(current->real_parent)->pid == pid));
14175 +       rcu_read_unlock();
14176 +       if (matched)
14177 +               goto out;
14178 +
14179 +       br = au_sbr(sb, cpg.bsrc);
14180 +       cmoo = au_br_cmoo(br->br_perm);
14181 +       if (!cmoo)
14182 +               goto out;
14183 +       if (!d_is_reg(dentry))
14184 +               cmoo &= AuBrAttr_COO_ALL;
14185 +       if (!cmoo)
14186 +               goto out;
14187 +
14188 +       parent = dget_parent(dentry);
14189 +       di_write_lock_parent(parent);
14190 +       err = au_wbr_do_copyup_bu(dentry, cpg.bsrc - 1);
14191 +       cpg.bdst = err;
14192 +       if (unlikely(err < 0)) {
14193 +               err = 0;        /* there is no upper writable branch */
14194 +               goto out_dgrade;
14195 +       }
14196 +       AuDbg("bsrc %d, bdst %d\n", cpg.bsrc, cpg.bdst);
14197 +
14198 +       /* do not respect the coo attrib for the target branch */
14199 +       err = au_cpup_dirs(dentry, cpg.bdst);
14200 +       if (unlikely(err))
14201 +               goto out_dgrade;
14202 +
14203 +       di_downgrade_lock(parent, AuLock_IR);
14204 +       udba = au_opt_udba(sb);
14205 +       err = au_pin(&pin, dentry, cpg.bdst, udba,
14206 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14207 +       if (unlikely(err))
14208 +               goto out_parent;
14209 +
14210 +       err = au_sio_cpup_simple(&cpg);
14211 +       au_unpin(&pin);
14212 +       if (unlikely(err))
14213 +               goto out_parent;
14214 +       if (!(cmoo & AuBrWAttr_MOO))
14215 +               goto out_parent; /* success */
14216 +
14217 +       err = au_pin(&pin, dentry, cpg.bsrc, udba,
14218 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14219 +       if (unlikely(err))
14220 +               goto out_parent;
14221 +
14222 +       h_path.mnt = au_br_mnt(br);
14223 +       h_path.dentry = au_h_dptr(dentry, cpg.bsrc);
14224 +       hdir = au_hi(d_inode(parent), cpg.bsrc);
14225 +       delegated = NULL;
14226 +       err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated, /*force*/1);
14227 +       au_unpin(&pin);
14228 +       /* todo: keep h_dentry or not? */
14229 +       if (unlikely(err == -EWOULDBLOCK)) {
14230 +               pr_warn("cannot retry for NFSv4 delegation"
14231 +                       " for an internal unlink\n");
14232 +               iput(delegated);
14233 +       }
14234 +       if (unlikely(err)) {
14235 +               pr_err("unlink %pd after coo failed (%d), ignored\n",
14236 +                      dentry, err);
14237 +               err = 0;
14238 +       }
14239 +       goto out_parent; /* success */
14240 +
14241 +out_dgrade:
14242 +       di_downgrade_lock(parent, AuLock_IR);
14243 +out_parent:
14244 +       di_read_unlock(parent, AuLock_IR);
14245 +       dput(parent);
14246 +out:
14247 +       AuTraceErr(err);
14248 +       return err;
14249 +}
14250 +
14251 +int au_do_open(struct file *file, struct au_do_open_args *args)
14252 +{
14253 +       int err, aopen = args->aopen;
14254 +       struct dentry *dentry;
14255 +       struct au_finfo *finfo;
14256 +
14257 +       if (!aopen)
14258 +               err = au_finfo_init(file, args->fidir);
14259 +       else {
14260 +               lockdep_off();
14261 +               err = au_finfo_init(file, args->fidir);
14262 +               lockdep_on();
14263 +       }
14264 +       if (unlikely(err))
14265 +               goto out;
14266 +
14267 +       dentry = file->f_path.dentry;
14268 +       AuDebugOn(IS_ERR_OR_NULL(dentry));
14269 +       di_write_lock_child(dentry);
14270 +       err = au_cmoo(dentry);
14271 +       di_downgrade_lock(dentry, AuLock_IR);
14272 +       if (!err) {
14273 +               if (!aopen)
14274 +                       err = args->open(file, vfsub_file_flags(file), NULL);
14275 +               else {
14276 +                       lockdep_off();
14277 +                       err = args->open(file, vfsub_file_flags(file),
14278 +                                        args->h_file);
14279 +                       lockdep_on();
14280 +               }
14281 +       }
14282 +       di_read_unlock(dentry, AuLock_IR);
14283 +
14284 +       finfo = au_fi(file);
14285 +       if (!err) {
14286 +               finfo->fi_file = file;
14287 +               au_hbl_add(&finfo->fi_hlist,
14288 +                          &au_sbi(file->f_path.dentry->d_sb)->si_files);
14289 +       }
14290 +       if (!aopen)
14291 +               fi_write_unlock(file);
14292 +       else {
14293 +               lockdep_off();
14294 +               fi_write_unlock(file);
14295 +               lockdep_on();
14296 +       }
14297 +       if (unlikely(err)) {
14298 +               finfo->fi_hdir = NULL;
14299 +               au_finfo_fin(file);
14300 +       }
14301 +
14302 +out:
14303 +       AuTraceErr(err);
14304 +       return err;
14305 +}
14306 +
14307 +int au_reopen_nondir(struct file *file)
14308 +{
14309 +       int err;
14310 +       aufs_bindex_t btop;
14311 +       struct dentry *dentry;
14312 +       struct au_branch *br;
14313 +       struct file *h_file, *h_file_tmp;
14314 +
14315 +       dentry = file->f_path.dentry;
14316 +       btop = au_dbtop(dentry);
14317 +       br = au_sbr(dentry->d_sb, btop);
14318 +       h_file_tmp = NULL;
14319 +       if (au_fbtop(file) == btop) {
14320 +               h_file = au_hf_top(file);
14321 +               if (file->f_mode == h_file->f_mode)
14322 +                       return 0; /* success */
14323 +               h_file_tmp = h_file;
14324 +               get_file(h_file_tmp);
14325 +               au_lcnt_inc(&br->br_nfiles);
14326 +               au_set_h_fptr(file, btop, NULL);
14327 +       }
14328 +       AuDebugOn(au_fi(file)->fi_hdir);
14329 +       /*
14330 +        * it can happen
14331 +        * file exists on both of rw and ro
14332 +        * open --> dbtop and fbtop are both 0
14333 +        * prepend a branch as rw, "rw" become ro
14334 +        * remove rw/file
14335 +        * delete the top branch, "rw" becomes rw again
14336 +        *      --> dbtop is 1, fbtop is still 0
14337 +        * write --> fbtop is 0 but dbtop is 1
14338 +        */
14339 +       /* AuDebugOn(au_fbtop(file) < btop); */
14340 +
14341 +       h_file = au_h_open(dentry, btop, vfsub_file_flags(file) & ~O_TRUNC,
14342 +                          file, /*force_wr*/0);
14343 +       err = PTR_ERR(h_file);
14344 +       if (IS_ERR(h_file)) {
14345 +               if (h_file_tmp) {
14346 +                       /* revert */
14347 +                       au_set_h_fptr(file, btop, h_file_tmp);
14348 +                       h_file_tmp = NULL;
14349 +               }
14350 +               goto out; /* todo: close all? */
14351 +       }
14352 +
14353 +       err = 0;
14354 +       au_set_fbtop(file, btop);
14355 +       au_set_h_fptr(file, btop, h_file);
14356 +       au_update_figen(file);
14357 +       /* todo: necessary? */
14358 +       /* file->f_ra = h_file->f_ra; */
14359 +
14360 +out:
14361 +       if (h_file_tmp) {
14362 +               fput(h_file_tmp);
14363 +               au_lcnt_dec(&br->br_nfiles);
14364 +       }
14365 +       return err;
14366 +}
14367 +
14368 +/* ---------------------------------------------------------------------- */
14369 +
14370 +static int au_reopen_wh(struct file *file, aufs_bindex_t btgt,
14371 +                       struct dentry *hi_wh)
14372 +{
14373 +       int err;
14374 +       aufs_bindex_t btop;
14375 +       struct au_dinfo *dinfo;
14376 +       struct dentry *h_dentry;
14377 +       struct au_hdentry *hdp;
14378 +
14379 +       dinfo = au_di(file->f_path.dentry);
14380 +       AuRwMustWriteLock(&dinfo->di_rwsem);
14381 +
14382 +       btop = dinfo->di_btop;
14383 +       dinfo->di_btop = btgt;
14384 +       hdp = au_hdentry(dinfo, btgt);
14385 +       h_dentry = hdp->hd_dentry;
14386 +       hdp->hd_dentry = hi_wh;
14387 +       err = au_reopen_nondir(file);
14388 +       hdp->hd_dentry = h_dentry;
14389 +       dinfo->di_btop = btop;
14390 +
14391 +       return err;
14392 +}
14393 +
14394 +static int au_ready_to_write_wh(struct file *file, loff_t len,
14395 +                               aufs_bindex_t bcpup, struct au_pin *pin)
14396 +{
14397 +       int err;
14398 +       struct inode *inode, *h_inode;
14399 +       struct dentry *h_dentry, *hi_wh;
14400 +       struct au_cp_generic cpg = {
14401 +               .dentry = file->f_path.dentry,
14402 +               .bdst   = bcpup,
14403 +               .bsrc   = -1,
14404 +               .len    = len,
14405 +               .pin    = pin
14406 +       };
14407 +
14408 +       au_update_dbtop(cpg.dentry);
14409 +       inode = d_inode(cpg.dentry);
14410 +       h_inode = NULL;
14411 +       if (au_dbtop(cpg.dentry) <= bcpup
14412 +           && au_dbbot(cpg.dentry) >= bcpup) {
14413 +               h_dentry = au_h_dptr(cpg.dentry, bcpup);
14414 +               if (h_dentry && d_is_positive(h_dentry))
14415 +                       h_inode = d_inode(h_dentry);
14416 +       }
14417 +       hi_wh = au_hi_wh(inode, bcpup);
14418 +       if (!hi_wh && !h_inode)
14419 +               err = au_sio_cpup_wh(&cpg, file);
14420 +       else
14421 +               /* already copied-up after unlink */
14422 +               err = au_reopen_wh(file, bcpup, hi_wh);
14423 +
14424 +       if (!err
14425 +           && (inode->i_nlink > 1
14426 +               || (inode->i_state & I_LINKABLE))
14427 +           && au_opt_test(au_mntflags(cpg.dentry->d_sb), PLINK))
14428 +               au_plink_append(inode, bcpup, au_h_dptr(cpg.dentry, bcpup));
14429 +
14430 +       return err;
14431 +}
14432 +
14433 +/*
14434 + * prepare the @file for writing.
14435 + */
14436 +int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin)
14437 +{
14438 +       int err;
14439 +       aufs_bindex_t dbtop;
14440 +       struct dentry *parent;
14441 +       struct inode *inode;
14442 +       struct super_block *sb;
14443 +       struct file *h_file;
14444 +       struct au_cp_generic cpg = {
14445 +               .dentry = file->f_path.dentry,
14446 +               .bdst   = -1,
14447 +               .bsrc   = -1,
14448 +               .len    = len,
14449 +               .pin    = pin,
14450 +               .flags  = AuCpup_DTIME
14451 +       };
14452 +
14453 +       sb = cpg.dentry->d_sb;
14454 +       inode = d_inode(cpg.dentry);
14455 +       cpg.bsrc = au_fbtop(file);
14456 +       err = au_test_ro(sb, cpg.bsrc, inode);
14457 +       if (!err && (au_hf_top(file)->f_mode & FMODE_WRITE)) {
14458 +               err = au_pin(pin, cpg.dentry, cpg.bsrc, AuOpt_UDBA_NONE,
14459 +                            /*flags*/0);
14460 +               goto out;
14461 +       }
14462 +
14463 +       /* need to cpup or reopen */
14464 +       parent = dget_parent(cpg.dentry);
14465 +       di_write_lock_parent(parent);
14466 +       err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
14467 +       cpg.bdst = err;
14468 +       if (unlikely(err < 0))
14469 +               goto out_dgrade;
14470 +       err = 0;
14471 +
14472 +       if (!d_unhashed(cpg.dentry) && !au_h_dptr(parent, cpg.bdst)) {
14473 +               err = au_cpup_dirs(cpg.dentry, cpg.bdst);
14474 +               if (unlikely(err))
14475 +                       goto out_dgrade;
14476 +       }
14477 +
14478 +       err = au_pin(pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
14479 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14480 +       if (unlikely(err))
14481 +               goto out_dgrade;
14482 +
14483 +       dbtop = au_dbtop(cpg.dentry);
14484 +       if (dbtop <= cpg.bdst)
14485 +               cpg.bsrc = cpg.bdst;
14486 +
14487 +       if (dbtop <= cpg.bdst           /* just reopen */
14488 +           || !d_unhashed(cpg.dentry)  /* copyup and reopen */
14489 +               ) {
14490 +               h_file = au_h_open_pre(cpg.dentry, cpg.bsrc, /*force_wr*/0);
14491 +               if (IS_ERR(h_file))
14492 +                       err = PTR_ERR(h_file);
14493 +               else {
14494 +                       di_downgrade_lock(parent, AuLock_IR);
14495 +                       if (dbtop > cpg.bdst)
14496 +                               err = au_sio_cpup_simple(&cpg);
14497 +                       if (!err)
14498 +                               err = au_reopen_nondir(file);
14499 +                       au_h_open_post(cpg.dentry, cpg.bsrc, h_file);
14500 +               }
14501 +       } else {                        /* copyup as wh and reopen */
14502 +               /*
14503 +                * since writable hfsplus branch is not supported,
14504 +                * h_open_pre/post() are unnecessary.
14505 +                */
14506 +               err = au_ready_to_write_wh(file, len, cpg.bdst, pin);
14507 +               di_downgrade_lock(parent, AuLock_IR);
14508 +       }
14509 +
14510 +       if (!err) {
14511 +               au_pin_set_parent_lflag(pin, /*lflag*/0);
14512 +               goto out_dput; /* success */
14513 +       }
14514 +       au_unpin(pin);
14515 +       goto out_unlock;
14516 +
14517 +out_dgrade:
14518 +       di_downgrade_lock(parent, AuLock_IR);
14519 +out_unlock:
14520 +       di_read_unlock(parent, AuLock_IR);
14521 +out_dput:
14522 +       dput(parent);
14523 +out:
14524 +       return err;
14525 +}
14526 +
14527 +/* ---------------------------------------------------------------------- */
14528 +
14529 +int au_do_flush(struct file *file, fl_owner_t id,
14530 +               int (*flush)(struct file *file, fl_owner_t id))
14531 +{
14532 +       int err;
14533 +       struct super_block *sb;
14534 +       struct inode *inode;
14535 +
14536 +       inode = file_inode(file);
14537 +       sb = inode->i_sb;
14538 +       si_noflush_read_lock(sb);
14539 +       fi_read_lock(file);
14540 +       ii_read_lock_child(inode);
14541 +
14542 +       err = flush(file, id);
14543 +       au_cpup_attr_timesizes(inode);
14544 +
14545 +       ii_read_unlock(inode);
14546 +       fi_read_unlock(file);
14547 +       si_read_unlock(sb);
14548 +       return err;
14549 +}
14550 +
14551 +/* ---------------------------------------------------------------------- */
14552 +
14553 +static int au_file_refresh_by_inode(struct file *file, int *need_reopen)
14554 +{
14555 +       int err;
14556 +       struct au_pin pin;
14557 +       struct au_finfo *finfo;
14558 +       struct dentry *parent, *hi_wh;
14559 +       struct inode *inode;
14560 +       struct super_block *sb;
14561 +       struct au_cp_generic cpg = {
14562 +               .dentry = file->f_path.dentry,
14563 +               .bdst   = -1,
14564 +               .bsrc   = -1,
14565 +               .len    = -1,
14566 +               .pin    = &pin,
14567 +               .flags  = AuCpup_DTIME
14568 +       };
14569 +
14570 +       FiMustWriteLock(file);
14571 +
14572 +       err = 0;
14573 +       finfo = au_fi(file);
14574 +       sb = cpg.dentry->d_sb;
14575 +       inode = d_inode(cpg.dentry);
14576 +       cpg.bdst = au_ibtop(inode);
14577 +       if (cpg.bdst == finfo->fi_btop || IS_ROOT(cpg.dentry))
14578 +               goto out;
14579 +
14580 +       parent = dget_parent(cpg.dentry);
14581 +       if (au_test_ro(sb, cpg.bdst, inode)) {
14582 +               di_read_lock_parent(parent, !AuLock_IR);
14583 +               err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
14584 +               cpg.bdst = err;
14585 +               di_read_unlock(parent, !AuLock_IR);
14586 +               if (unlikely(err < 0))
14587 +                       goto out_parent;
14588 +               err = 0;
14589 +       }
14590 +
14591 +       di_read_lock_parent(parent, AuLock_IR);
14592 +       hi_wh = au_hi_wh(inode, cpg.bdst);
14593 +       if (!S_ISDIR(inode->i_mode)
14594 +           && au_opt_test(au_mntflags(sb), PLINK)
14595 +           && au_plink_test(inode)
14596 +           && !d_unhashed(cpg.dentry)
14597 +           && cpg.bdst < au_dbtop(cpg.dentry)) {
14598 +               err = au_test_and_cpup_dirs(cpg.dentry, cpg.bdst);
14599 +               if (unlikely(err))
14600 +                       goto out_unlock;
14601 +
14602 +               /* always superio. */
14603 +               err = au_pin(&pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
14604 +                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14605 +               if (!err) {
14606 +                       err = au_sio_cpup_simple(&cpg);
14607 +                       au_unpin(&pin);
14608 +               }
14609 +       } else if (hi_wh) {
14610 +               /* already copied-up after unlink */
14611 +               err = au_reopen_wh(file, cpg.bdst, hi_wh);
14612 +               *need_reopen = 0;
14613 +       }
14614 +
14615 +out_unlock:
14616 +       di_read_unlock(parent, AuLock_IR);
14617 +out_parent:
14618 +       dput(parent);
14619 +out:
14620 +       return err;
14621 +}
14622 +
14623 +static void au_do_refresh_dir(struct file *file)
14624 +{
14625 +       aufs_bindex_t bindex, bbot, new_bindex, brid;
14626 +       struct au_hfile *p, tmp, *q;
14627 +       struct au_finfo *finfo;
14628 +       struct super_block *sb;
14629 +       struct au_fidir *fidir;
14630 +
14631 +       FiMustWriteLock(file);
14632 +
14633 +       sb = file->f_path.dentry->d_sb;
14634 +       finfo = au_fi(file);
14635 +       fidir = finfo->fi_hdir;
14636 +       AuDebugOn(!fidir);
14637 +       p = fidir->fd_hfile + finfo->fi_btop;
14638 +       brid = p->hf_br->br_id;
14639 +       bbot = fidir->fd_bbot;
14640 +       for (bindex = finfo->fi_btop; bindex <= bbot; bindex++, p++) {
14641 +               if (!p->hf_file)
14642 +                       continue;
14643 +
14644 +               new_bindex = au_br_index(sb, p->hf_br->br_id);
14645 +               if (new_bindex == bindex)
14646 +                       continue;
14647 +               if (new_bindex < 0) {
14648 +                       au_set_h_fptr(file, bindex, NULL);
14649 +                       continue;
14650 +               }
14651 +
14652 +               /* swap two lower inode, and loop again */
14653 +               q = fidir->fd_hfile + new_bindex;
14654 +               tmp = *q;
14655 +               *q = *p;
14656 +               *p = tmp;
14657 +               if (tmp.hf_file) {
14658 +                       bindex--;
14659 +                       p--;
14660 +               }
14661 +       }
14662 +
14663 +       p = fidir->fd_hfile;
14664 +       if (!au_test_mmapped(file) && !d_unlinked(file->f_path.dentry)) {
14665 +               bbot = au_sbbot(sb);
14666 +               for (finfo->fi_btop = 0; finfo->fi_btop <= bbot;
14667 +                    finfo->fi_btop++, p++)
14668 +                       if (p->hf_file) {
14669 +                               if (file_inode(p->hf_file))
14670 +                                       break;
14671 +                               au_hfput(p, /*execed*/0);
14672 +                       }
14673 +       } else {
14674 +               bbot = au_br_index(sb, brid);
14675 +               for (finfo->fi_btop = 0; finfo->fi_btop < bbot;
14676 +                    finfo->fi_btop++, p++)
14677 +                       if (p->hf_file)
14678 +                               au_hfput(p, /*execed*/0);
14679 +               bbot = au_sbbot(sb);
14680 +       }
14681 +
14682 +       p = fidir->fd_hfile + bbot;
14683 +       for (fidir->fd_bbot = bbot; fidir->fd_bbot >= finfo->fi_btop;
14684 +            fidir->fd_bbot--, p--)
14685 +               if (p->hf_file) {
14686 +                       if (file_inode(p->hf_file))
14687 +                               break;
14688 +                       au_hfput(p, /*execed*/0);
14689 +               }
14690 +       AuDebugOn(fidir->fd_bbot < finfo->fi_btop);
14691 +}
14692 +
14693 +/*
14694 + * after branch manipulating, refresh the file.
14695 + */
14696 +static int refresh_file(struct file *file, int (*reopen)(struct file *file))
14697 +{
14698 +       int err, need_reopen, nbr;
14699 +       aufs_bindex_t bbot, bindex;
14700 +       struct dentry *dentry;
14701 +       struct super_block *sb;
14702 +       struct au_finfo *finfo;
14703 +       struct au_hfile *hfile;
14704 +
14705 +       dentry = file->f_path.dentry;
14706 +       sb = dentry->d_sb;
14707 +       nbr = au_sbbot(sb) + 1;
14708 +       finfo = au_fi(file);
14709 +       if (!finfo->fi_hdir) {
14710 +               hfile = &finfo->fi_htop;
14711 +               AuDebugOn(!hfile->hf_file);
14712 +               bindex = au_br_index(sb, hfile->hf_br->br_id);
14713 +               AuDebugOn(bindex < 0);
14714 +               if (bindex != finfo->fi_btop)
14715 +                       au_set_fbtop(file, bindex);
14716 +       } else {
14717 +               err = au_fidir_realloc(finfo, nbr, /*may_shrink*/0);
14718 +               if (unlikely(err))
14719 +                       goto out;
14720 +               au_do_refresh_dir(file);
14721 +       }
14722 +
14723 +       err = 0;
14724 +       need_reopen = 1;
14725 +       if (!au_test_mmapped(file))
14726 +               err = au_file_refresh_by_inode(file, &need_reopen);
14727 +       if (finfo->fi_hdir)
14728 +               /* harmless if err */
14729 +               au_fidir_realloc(finfo, nbr, /*may_shrink*/1);
14730 +       if (!err && need_reopen && !d_unlinked(dentry))
14731 +               err = reopen(file);
14732 +       if (!err) {
14733 +               au_update_figen(file);
14734 +               goto out; /* success */
14735 +       }
14736 +
14737 +       /* error, close all lower files */
14738 +       if (finfo->fi_hdir) {
14739 +               bbot = au_fbbot_dir(file);
14740 +               for (bindex = au_fbtop(file); bindex <= bbot; bindex++)
14741 +                       au_set_h_fptr(file, bindex, NULL);
14742 +       }
14743 +
14744 +out:
14745 +       return err;
14746 +}
14747 +
14748 +/* common function to regular file and dir */
14749 +int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
14750 +                         int wlock, unsigned int fi_lsc)
14751 +{
14752 +       int err;
14753 +       unsigned int sigen, figen;
14754 +       aufs_bindex_t btop;
14755 +       unsigned char pseudo_link;
14756 +       struct dentry *dentry;
14757 +       struct inode *inode;
14758 +
14759 +       err = 0;
14760 +       dentry = file->f_path.dentry;
14761 +       inode = d_inode(dentry);
14762 +       sigen = au_sigen(dentry->d_sb);
14763 +       fi_write_lock_nested(file, fi_lsc);
14764 +       figen = au_figen(file);
14765 +       if (!fi_lsc)
14766 +               di_write_lock_child(dentry);
14767 +       else
14768 +               di_write_lock_child2(dentry);
14769 +       btop = au_dbtop(dentry);
14770 +       pseudo_link = (btop != au_ibtop(inode));
14771 +       if (sigen == figen && !pseudo_link && au_fbtop(file) == btop) {
14772 +               if (!wlock) {
14773 +                       di_downgrade_lock(dentry, AuLock_IR);
14774 +                       fi_downgrade_lock(file);
14775 +               }
14776 +               goto out; /* success */
14777 +       }
14778 +
14779 +       AuDbg("sigen %d, figen %d\n", sigen, figen);
14780 +       if (au_digen_test(dentry, sigen)) {
14781 +               err = au_reval_dpath(dentry, sigen);
14782 +               AuDebugOn(!err && au_digen_test(dentry, sigen));
14783 +       }
14784 +
14785 +       if (!err)
14786 +               err = refresh_file(file, reopen);
14787 +       if (!err) {
14788 +               if (!wlock) {
14789 +                       di_downgrade_lock(dentry, AuLock_IR);
14790 +                       fi_downgrade_lock(file);
14791 +               }
14792 +       } else {
14793 +               di_write_unlock(dentry);
14794 +               fi_write_unlock(file);
14795 +       }
14796 +
14797 +out:
14798 +       return err;
14799 +}
14800 +
14801 +/* ---------------------------------------------------------------------- */
14802 +
14803 +/* cf. aufs_nopage() */
14804 +/* for madvise(2) */
14805 +static int aufs_readpage(struct file *file __maybe_unused, struct page *page)
14806 +{
14807 +       unlock_page(page);
14808 +       return 0;
14809 +}
14810 +
14811 +/* it will never be called, but necessary to support O_DIRECT */
14812 +static ssize_t aufs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
14813 +{ BUG(); return 0; }
14814 +
14815 +/* they will never be called. */
14816 +#ifdef CONFIG_AUFS_DEBUG
14817 +static int aufs_write_begin(struct file *file, struct address_space *mapping,
14818 +                           loff_t pos, unsigned len, unsigned flags,
14819 +                           struct page **pagep, void **fsdata)
14820 +{ AuUnsupport(); return 0; }
14821 +static int aufs_write_end(struct file *file, struct address_space *mapping,
14822 +                         loff_t pos, unsigned len, unsigned copied,
14823 +                         struct page *page, void *fsdata)
14824 +{ AuUnsupport(); return 0; }
14825 +static int aufs_writepage(struct page *page, struct writeback_control *wbc)
14826 +{ AuUnsupport(); return 0; }
14827 +
14828 +static int aufs_set_page_dirty(struct page *page)
14829 +{ AuUnsupport(); return 0; }
14830 +static void aufs_invalidatepage(struct page *page, unsigned int offset,
14831 +                               unsigned int length)
14832 +{ AuUnsupport(); }
14833 +static int aufs_releasepage(struct page *page, gfp_t gfp)
14834 +{ AuUnsupport(); return 0; }
14835 +#if 0 /* called by memory compaction regardless file */
14836 +static int aufs_migratepage(struct address_space *mapping, struct page *newpage,
14837 +                           struct page *page, enum migrate_mode mode)
14838 +{ AuUnsupport(); return 0; }
14839 +#endif
14840 +static bool aufs_isolate_page(struct page *page, isolate_mode_t mode)
14841 +{ AuUnsupport(); return true; }
14842 +static void aufs_putback_page(struct page *page)
14843 +{ AuUnsupport(); }
14844 +static int aufs_launder_page(struct page *page)
14845 +{ AuUnsupport(); return 0; }
14846 +static int aufs_is_partially_uptodate(struct page *page,
14847 +                                     unsigned long from,
14848 +                                     unsigned long count)
14849 +{ AuUnsupport(); return 0; }
14850 +static void aufs_is_dirty_writeback(struct page *page, bool *dirty,
14851 +                                   bool *writeback)
14852 +{ AuUnsupport(); }
14853 +static int aufs_error_remove_page(struct address_space *mapping,
14854 +                                 struct page *page)
14855 +{ AuUnsupport(); return 0; }
14856 +static int aufs_swap_activate(struct swap_info_struct *sis, struct file *file,
14857 +                             sector_t *span)
14858 +{ AuUnsupport(); return 0; }
14859 +static void aufs_swap_deactivate(struct file *file)
14860 +{ AuUnsupport(); }
14861 +#endif /* CONFIG_AUFS_DEBUG */
14862 +
14863 +const struct address_space_operations aufs_aop = {
14864 +       .readpage               = aufs_readpage,
14865 +       .direct_IO              = aufs_direct_IO,
14866 +#ifdef CONFIG_AUFS_DEBUG
14867 +       .writepage              = aufs_writepage,
14868 +       /* no writepages, because of writepage */
14869 +       .set_page_dirty         = aufs_set_page_dirty,
14870 +       /* no readpages, because of readpage */
14871 +       .write_begin            = aufs_write_begin,
14872 +       .write_end              = aufs_write_end,
14873 +       /* no bmap, no block device */
14874 +       .invalidatepage         = aufs_invalidatepage,
14875 +       .releasepage            = aufs_releasepage,
14876 +       /* is fallback_migrate_page ok? */
14877 +       /* .migratepage         = aufs_migratepage, */
14878 +       .isolate_page           = aufs_isolate_page,
14879 +       .putback_page           = aufs_putback_page,
14880 +       .launder_page           = aufs_launder_page,
14881 +       .is_partially_uptodate  = aufs_is_partially_uptodate,
14882 +       .is_dirty_writeback     = aufs_is_dirty_writeback,
14883 +       .error_remove_page      = aufs_error_remove_page,
14884 +       .swap_activate          = aufs_swap_activate,
14885 +       .swap_deactivate        = aufs_swap_deactivate
14886 +#endif /* CONFIG_AUFS_DEBUG */
14887 +};
14888 diff -urN /usr/share/empty/fs/aufs/file.h linux/fs/aufs/file.h
14889 --- /usr/share/empty/fs/aufs/file.h     1970-01-01 01:00:00.000000000 +0100
14890 +++ linux/fs/aufs/file.h        2022-03-21 14:49:05.723299677 +0100
14891 @@ -0,0 +1,342 @@
14892 +/* SPDX-License-Identifier: GPL-2.0 */
14893 +/*
14894 + * Copyright (C) 2005-2021 Junjiro R. Okajima
14895 + *
14896 + * This program, aufs is free software; you can redistribute it and/or modify
14897 + * it under the terms of the GNU General Public License as published by
14898 + * the Free Software Foundation; either version 2 of the License, or
14899 + * (at your option) any later version.
14900 + *
14901 + * This program is distributed in the hope that it will be useful,
14902 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
14903 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14904 + * GNU General Public License for more details.
14905 + *
14906 + * You should have received a copy of the GNU General Public License
14907 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
14908 + */
14909 +
14910 +/*
14911 + * file operations
14912 + */
14913 +
14914 +#ifndef __AUFS_FILE_H__
14915 +#define __AUFS_FILE_H__
14916 +
14917 +#ifdef __KERNEL__
14918 +
14919 +#include <linux/file.h>
14920 +#include <linux/fs.h>
14921 +#include <linux/mm_types.h>
14922 +#include <linux/poll.h>
14923 +#include "rwsem.h"
14924 +
14925 +struct au_branch;
14926 +struct au_hfile {
14927 +       struct file             *hf_file;
14928 +       struct au_branch        *hf_br;
14929 +};
14930 +
14931 +struct au_vdir;
14932 +struct au_fidir {
14933 +       aufs_bindex_t           fd_bbot;
14934 +       aufs_bindex_t           fd_nent;
14935 +       struct au_vdir          *fd_vdir_cache;
14936 +       struct au_hfile         fd_hfile[];
14937 +};
14938 +
14939 +static inline int au_fidir_sz(int nent)
14940 +{
14941 +       AuDebugOn(nent < 0);
14942 +       return sizeof(struct au_fidir) + sizeof(struct au_hfile) * nent;
14943 +}
14944 +
14945 +struct au_finfo {
14946 +       atomic_t                fi_generation;
14947 +
14948 +       struct au_rwsem         fi_rwsem;
14949 +       aufs_bindex_t           fi_btop;
14950 +
14951 +       /* do not union them */
14952 +       struct {                                /* for non-dir */
14953 +               struct au_hfile                 fi_htop;
14954 +               atomic_t                        fi_mmapped;
14955 +       };
14956 +       struct au_fidir         *fi_hdir;       /* for dir only */
14957 +
14958 +       struct hlist_bl_node    fi_hlist;
14959 +       struct file             *fi_file;       /* very ugly */
14960 +       struct rcu_head         rcu;
14961 +} ____cacheline_aligned_in_smp;
14962 +
14963 +/* ---------------------------------------------------------------------- */
14964 +
14965 +/* file.c */
14966 +extern const struct address_space_operations aufs_aop;
14967 +unsigned int au_file_roflags(unsigned int flags);
14968 +struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
14969 +                      struct file *file, int force_wr);
14970 +struct au_do_open_args {
14971 +       int             aopen;
14972 +       int             (*open)(struct file *file, int flags,
14973 +                               struct file *h_file);
14974 +       struct au_fidir *fidir;
14975 +       struct file     *h_file;
14976 +};
14977 +int au_do_open(struct file *file, struct au_do_open_args *args);
14978 +int au_reopen_nondir(struct file *file);
14979 +struct au_pin;
14980 +int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin);
14981 +int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
14982 +                         int wlock, unsigned int fi_lsc);
14983 +int au_do_flush(struct file *file, fl_owner_t id,
14984 +               int (*flush)(struct file *file, fl_owner_t id));
14985 +
14986 +/* poll.c */
14987 +#ifdef CONFIG_AUFS_POLL
14988 +__poll_t aufs_poll(struct file *file, struct poll_table_struct *pt);
14989 +#endif
14990 +
14991 +#ifdef CONFIG_AUFS_BR_HFSPLUS
14992 +/* hfsplus.c */
14993 +struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
14994 +                          int force_wr);
14995 +void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
14996 +                   struct file *h_file);
14997 +#else
14998 +AuStub(struct file *, au_h_open_pre, return NULL, struct dentry *dentry,
14999 +       aufs_bindex_t bindex, int force_wr)
15000 +AuStubVoid(au_h_open_post, struct dentry *dentry, aufs_bindex_t bindex,
15001 +          struct file *h_file);
15002 +#endif
15003 +
15004 +/* f_op.c */
15005 +extern const struct file_operations aufs_file_fop;
15006 +int au_do_open_nondir(struct file *file, int flags, struct file *h_file);
15007 +int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file);
15008 +struct file *au_read_pre(struct file *file, int keep_fi, unsigned int lsc);
15009 +
15010 +/* finfo.c */
15011 +void au_hfput(struct au_hfile *hf, int execed);
15012 +void au_set_h_fptr(struct file *file, aufs_bindex_t bindex,
15013 +                  struct file *h_file);
15014 +
15015 +void au_update_figen(struct file *file);
15016 +struct au_fidir *au_fidir_alloc(struct super_block *sb);
15017 +int au_fidir_realloc(struct au_finfo *finfo, int nbr, int may_shrink);
15018 +
15019 +void au_fi_init_once(void *_fi);
15020 +void au_finfo_fin(struct file *file);
15021 +int au_finfo_init(struct file *file, struct au_fidir *fidir);
15022 +
15023 +/* ioctl.c */
15024 +long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg);
15025 +#ifdef CONFIG_COMPAT
15026 +long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
15027 +                          unsigned long arg);
15028 +long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
15029 +                             unsigned long arg);
15030 +#endif
15031 +
15032 +/* ---------------------------------------------------------------------- */
15033 +
15034 +static inline struct au_finfo *au_fi(struct file *file)
15035 +{
15036 +       return file->private_data;
15037 +}
15038 +
15039 +/* ---------------------------------------------------------------------- */
15040 +
15041 +#define fi_read_lock(f)        au_rw_read_lock(&au_fi(f)->fi_rwsem)
15042 +#define fi_write_lock(f)       au_rw_write_lock(&au_fi(f)->fi_rwsem)
15043 +#define fi_read_trylock(f)     au_rw_read_trylock(&au_fi(f)->fi_rwsem)
15044 +#define fi_write_trylock(f)    au_rw_write_trylock(&au_fi(f)->fi_rwsem)
15045 +/*
15046 +#define fi_read_trylock_nested(f) \
15047 +       au_rw_read_trylock_nested(&au_fi(f)->fi_rwsem)
15048 +#define fi_write_trylock_nested(f) \
15049 +       au_rw_write_trylock_nested(&au_fi(f)->fi_rwsem)
15050 +*/
15051 +
15052 +#define fi_read_unlock(f)      au_rw_read_unlock(&au_fi(f)->fi_rwsem)
15053 +#define fi_write_unlock(f)     au_rw_write_unlock(&au_fi(f)->fi_rwsem)
15054 +#define fi_downgrade_lock(f)   au_rw_dgrade_lock(&au_fi(f)->fi_rwsem)
15055 +
15056 +/* lock subclass for finfo */
15057 +enum {
15058 +       AuLsc_FI_1,
15059 +       AuLsc_FI_2
15060 +};
15061 +
15062 +static inline void fi_read_lock_nested(struct file *f, unsigned int lsc)
15063 +{
15064 +       au_rw_read_lock_nested(&au_fi(f)->fi_rwsem, lsc);
15065 +}
15066 +
15067 +static inline void fi_write_lock_nested(struct file *f, unsigned int lsc)
15068 +{
15069 +       au_rw_write_lock_nested(&au_fi(f)->fi_rwsem, lsc);
15070 +}
15071 +
15072 +/*
15073 + * fi_read_lock_1, fi_write_lock_1,
15074 + * fi_read_lock_2, fi_write_lock_2
15075 + */
15076 +#define AuReadLockFunc(name) \
15077 +static inline void fi_read_lock_##name(struct file *f) \
15078 +{ fi_read_lock_nested(f, AuLsc_FI_##name); }
15079 +
15080 +#define AuWriteLockFunc(name) \
15081 +static inline void fi_write_lock_##name(struct file *f) \
15082 +{ fi_write_lock_nested(f, AuLsc_FI_##name); }
15083 +
15084 +#define AuRWLockFuncs(name) \
15085 +       AuReadLockFunc(name) \
15086 +       AuWriteLockFunc(name)
15087 +
15088 +AuRWLockFuncs(1);
15089 +AuRWLockFuncs(2);
15090 +
15091 +#undef AuReadLockFunc
15092 +#undef AuWriteLockFunc
15093 +#undef AuRWLockFuncs
15094 +
15095 +#define FiMustNoWaiters(f)     AuRwMustNoWaiters(&au_fi(f)->fi_rwsem)
15096 +#define FiMustAnyLock(f)       AuRwMustAnyLock(&au_fi(f)->fi_rwsem)
15097 +#define FiMustWriteLock(f)     AuRwMustWriteLock(&au_fi(f)->fi_rwsem)
15098 +
15099 +/* ---------------------------------------------------------------------- */
15100 +
15101 +/* todo: hard/soft set? */
15102 +static inline aufs_bindex_t au_fbtop(struct file *file)
15103 +{
15104 +       FiMustAnyLock(file);
15105 +       return au_fi(file)->fi_btop;
15106 +}
15107 +
15108 +static inline aufs_bindex_t au_fbbot_dir(struct file *file)
15109 +{
15110 +       FiMustAnyLock(file);
15111 +       AuDebugOn(!au_fi(file)->fi_hdir);
15112 +       return au_fi(file)->fi_hdir->fd_bbot;
15113 +}
15114 +
15115 +static inline struct au_vdir *au_fvdir_cache(struct file *file)
15116 +{
15117 +       FiMustAnyLock(file);
15118 +       AuDebugOn(!au_fi(file)->fi_hdir);
15119 +       return au_fi(file)->fi_hdir->fd_vdir_cache;
15120 +}
15121 +
15122 +static inline void au_set_fbtop(struct file *file, aufs_bindex_t bindex)
15123 +{
15124 +       FiMustWriteLock(file);
15125 +       au_fi(file)->fi_btop = bindex;
15126 +}
15127 +
15128 +static inline void au_set_fbbot_dir(struct file *file, aufs_bindex_t bindex)
15129 +{
15130 +       FiMustWriteLock(file);
15131 +       AuDebugOn(!au_fi(file)->fi_hdir);
15132 +       au_fi(file)->fi_hdir->fd_bbot = bindex;
15133 +}
15134 +
15135 +static inline void au_set_fvdir_cache(struct file *file,
15136 +                                     struct au_vdir *vdir_cache)
15137 +{
15138 +       FiMustWriteLock(file);
15139 +       AuDebugOn(!au_fi(file)->fi_hdir);
15140 +       au_fi(file)->fi_hdir->fd_vdir_cache = vdir_cache;
15141 +}
15142 +
15143 +static inline struct file *au_hf_top(struct file *file)
15144 +{
15145 +       FiMustAnyLock(file);
15146 +       AuDebugOn(au_fi(file)->fi_hdir);
15147 +       return au_fi(file)->fi_htop.hf_file;
15148 +}
15149 +
15150 +static inline struct file *au_hf_dir(struct file *file, aufs_bindex_t bindex)
15151 +{
15152 +       FiMustAnyLock(file);
15153 +       AuDebugOn(!au_fi(file)->fi_hdir);
15154 +       return au_fi(file)->fi_hdir->fd_hfile[0 + bindex].hf_file;
15155 +}
15156 +
15157 +/* todo: memory barrier? */
15158 +static inline unsigned int au_figen(struct file *f)
15159 +{
15160 +       return atomic_read(&au_fi(f)->fi_generation);
15161 +}
15162 +
15163 +static inline void au_set_mmapped(struct file *f)
15164 +{
15165 +       if (atomic_inc_return(&au_fi(f)->fi_mmapped))
15166 +               return;
15167 +       pr_warn("fi_mmapped wrapped around\n");
15168 +       while (!atomic_inc_return(&au_fi(f)->fi_mmapped))
15169 +               ;
15170 +}
15171 +
15172 +static inline void au_unset_mmapped(struct file *f)
15173 +{
15174 +       atomic_dec(&au_fi(f)->fi_mmapped);
15175 +}
15176 +
15177 +static inline int au_test_mmapped(struct file *f)
15178 +{
15179 +       return atomic_read(&au_fi(f)->fi_mmapped);
15180 +}
15181 +
15182 +/* customize vma->vm_file */
15183 +
15184 +static inline void au_do_vm_file_reset(struct vm_area_struct *vma,
15185 +                                      struct file *file)
15186 +{
15187 +       struct file *f;
15188 +
15189 +       f = vma->vm_file;
15190 +       get_file(file);
15191 +       vma->vm_file = file;
15192 +       fput(f);
15193 +}
15194 +
15195 +#ifdef CONFIG_MMU
15196 +#define AuDbgVmRegion(file, vma) do {} while (0)
15197 +
15198 +static inline void au_vm_file_reset(struct vm_area_struct *vma,
15199 +                                   struct file *file)
15200 +{
15201 +       au_do_vm_file_reset(vma, file);
15202 +}
15203 +#else
15204 +#define AuDbgVmRegion(file, vma) \
15205 +       AuDebugOn((vma)->vm_region && (vma)->vm_region->vm_file != (file))
15206 +
15207 +static inline void au_vm_file_reset(struct vm_area_struct *vma,
15208 +                                   struct file *file)
15209 +{
15210 +       struct file *f;
15211 +
15212 +       au_do_vm_file_reset(vma, file);
15213 +       f = vma->vm_region->vm_file;
15214 +       get_file(file);
15215 +       vma->vm_region->vm_file = file;
15216 +       fput(f);
15217 +}
15218 +#endif /* CONFIG_MMU */
15219 +
15220 +/* handle vma->vm_prfile */
15221 +static inline void au_vm_prfile_set(struct vm_area_struct *vma,
15222 +                                   struct file *file)
15223 +{
15224 +       get_file(file);
15225 +       vma->vm_prfile = file;
15226 +#ifndef CONFIG_MMU
15227 +       get_file(file);
15228 +       vma->vm_region->vm_prfile = file;
15229 +#endif
15230 +}
15231 +
15232 +#endif /* __KERNEL__ */
15233 +#endif /* __AUFS_FILE_H__ */
15234 diff -urN /usr/share/empty/fs/aufs/finfo.c linux/fs/aufs/finfo.c
15235 --- /usr/share/empty/fs/aufs/finfo.c    1970-01-01 01:00:00.000000000 +0100
15236 +++ linux/fs/aufs/finfo.c       2022-03-21 14:49:05.723299677 +0100
15237 @@ -0,0 +1,149 @@
15238 +// SPDX-License-Identifier: GPL-2.0
15239 +/*
15240 + * Copyright (C) 2005-2021 Junjiro R. Okajima
15241 + *
15242 + * This program, aufs is free software; you can redistribute it and/or modify
15243 + * it under the terms of the GNU General Public License as published by
15244 + * the Free Software Foundation; either version 2 of the License, or
15245 + * (at your option) any later version.
15246 + *
15247 + * This program is distributed in the hope that it will be useful,
15248 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
15249 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15250 + * GNU General Public License for more details.
15251 + *
15252 + * You should have received a copy of the GNU General Public License
15253 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15254 + */
15255 +
15256 +/*
15257 + * file private data
15258 + */
15259 +
15260 +#include "aufs.h"
15261 +
15262 +void au_hfput(struct au_hfile *hf, int execed)
15263 +{
15264 +       if (execed)
15265 +               allow_write_access(hf->hf_file);
15266 +       fput(hf->hf_file);
15267 +       hf->hf_file = NULL;
15268 +       au_lcnt_dec(&hf->hf_br->br_nfiles);
15269 +       hf->hf_br = NULL;
15270 +}
15271 +
15272 +void au_set_h_fptr(struct file *file, aufs_bindex_t bindex, struct file *val)
15273 +{
15274 +       struct au_finfo *finfo = au_fi(file);
15275 +       struct au_hfile *hf;
15276 +       struct au_fidir *fidir;
15277 +
15278 +       fidir = finfo->fi_hdir;
15279 +       if (!fidir) {
15280 +               AuDebugOn(finfo->fi_btop != bindex);
15281 +               hf = &finfo->fi_htop;
15282 +       } else
15283 +               hf = fidir->fd_hfile + bindex;
15284 +
15285 +       if (hf && hf->hf_file)
15286 +               au_hfput(hf, vfsub_file_execed(file));
15287 +       if (val) {
15288 +               FiMustWriteLock(file);
15289 +               AuDebugOn(IS_ERR_OR_NULL(file->f_path.dentry));
15290 +               hf->hf_file = val;
15291 +               hf->hf_br = au_sbr(file->f_path.dentry->d_sb, bindex);
15292 +       }
15293 +}
15294 +
15295 +void au_update_figen(struct file *file)
15296 +{
15297 +       atomic_set(&au_fi(file)->fi_generation, au_digen(file->f_path.dentry));
15298 +       /* smp_mb(); */ /* atomic_set */
15299 +}
15300 +
15301 +/* ---------------------------------------------------------------------- */
15302 +
15303 +struct au_fidir *au_fidir_alloc(struct super_block *sb)
15304 +{
15305 +       struct au_fidir *fidir;
15306 +       int nbr;
15307 +
15308 +       nbr = au_sbbot(sb) + 1;
15309 +       if (nbr < 2)
15310 +               nbr = 2; /* initial allocate for 2 branches */
15311 +       fidir = kzalloc(au_fidir_sz(nbr), GFP_NOFS);
15312 +       if (fidir) {
15313 +               fidir->fd_bbot = -1;
15314 +               fidir->fd_nent = nbr;
15315 +       }
15316 +
15317 +       return fidir;
15318 +}
15319 +
15320 +int au_fidir_realloc(struct au_finfo *finfo, int nbr, int may_shrink)
15321 +{
15322 +       int err;
15323 +       struct au_fidir *fidir, *p;
15324 +
15325 +       AuRwMustWriteLock(&finfo->fi_rwsem);
15326 +       fidir = finfo->fi_hdir;
15327 +       AuDebugOn(!fidir);
15328 +
15329 +       err = -ENOMEM;
15330 +       p = au_kzrealloc(fidir, au_fidir_sz(fidir->fd_nent), au_fidir_sz(nbr),
15331 +                        GFP_NOFS, may_shrink);
15332 +       if (p) {
15333 +               p->fd_nent = nbr;
15334 +               finfo->fi_hdir = p;
15335 +               err = 0;
15336 +       }
15337 +
15338 +       return err;
15339 +}
15340 +
15341 +/* ---------------------------------------------------------------------- */
15342 +
15343 +void au_finfo_fin(struct file *file)
15344 +{
15345 +       struct au_finfo *finfo;
15346 +
15347 +       au_lcnt_dec(&au_sbi(file->f_path.dentry->d_sb)->si_nfiles);
15348 +
15349 +       finfo = au_fi(file);
15350 +       AuDebugOn(finfo->fi_hdir);
15351 +       AuRwDestroy(&finfo->fi_rwsem);
15352 +       au_cache_free_finfo(finfo);
15353 +}
15354 +
15355 +void au_fi_init_once(void *_finfo)
15356 +{
15357 +       struct au_finfo *finfo = _finfo;
15358 +
15359 +       au_rw_init(&finfo->fi_rwsem);
15360 +}
15361 +
15362 +int au_finfo_init(struct file *file, struct au_fidir *fidir)
15363 +{
15364 +       int err;
15365 +       struct au_finfo *finfo;
15366 +       struct dentry *dentry;
15367 +
15368 +       err = -ENOMEM;
15369 +       dentry = file->f_path.dentry;
15370 +       finfo = au_cache_alloc_finfo();
15371 +       if (unlikely(!finfo))
15372 +               goto out;
15373 +
15374 +       err = 0;
15375 +       au_lcnt_inc(&au_sbi(dentry->d_sb)->si_nfiles);
15376 +       au_rw_write_lock(&finfo->fi_rwsem);
15377 +       finfo->fi_btop = -1;
15378 +       finfo->fi_hdir = fidir;
15379 +       atomic_set(&finfo->fi_generation, au_digen(dentry));
15380 +       /* smp_mb(); */ /* atomic_set */
15381 +
15382 +       file->private_data = finfo;
15383 +
15384 +out:
15385 +       return err;
15386 +}
15387 diff -urN /usr/share/empty/fs/aufs/f_op.c linux/fs/aufs/f_op.c
15388 --- /usr/share/empty/fs/aufs/f_op.c     1970-01-01 01:00:00.000000000 +0100
15389 +++ linux/fs/aufs/f_op.c        2022-03-21 14:49:05.723299677 +0100
15390 @@ -0,0 +1,771 @@
15391 +// SPDX-License-Identifier: GPL-2.0
15392 +/*
15393 + * Copyright (C) 2005-2021 Junjiro R. Okajima
15394 + *
15395 + * This program, aufs is free software; you can redistribute it and/or modify
15396 + * it under the terms of the GNU General Public License as published by
15397 + * the Free Software Foundation; either version 2 of the License, or
15398 + * (at your option) any later version.
15399 + *
15400 + * This program is distributed in the hope that it will be useful,
15401 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
15402 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15403 + * GNU General Public License for more details.
15404 + *
15405 + * You should have received a copy of the GNU General Public License
15406 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15407 + */
15408 +
15409 +/*
15410 + * file and vm operations
15411 + */
15412 +
15413 +#include <linux/aio.h>
15414 +#include <linux/fs_stack.h>
15415 +#include <linux/mman.h>
15416 +#include <linux/security.h>
15417 +#include "aufs.h"
15418 +
15419 +int au_do_open_nondir(struct file *file, int flags, struct file *h_file)
15420 +{
15421 +       int err;
15422 +       aufs_bindex_t bindex;
15423 +       struct dentry *dentry, *h_dentry;
15424 +       struct au_finfo *finfo;
15425 +       struct inode *h_inode;
15426 +
15427 +       FiMustWriteLock(file);
15428 +
15429 +       err = 0;
15430 +       dentry = file->f_path.dentry;
15431 +       AuDebugOn(IS_ERR_OR_NULL(dentry));
15432 +       finfo = au_fi(file);
15433 +       memset(&finfo->fi_htop, 0, sizeof(finfo->fi_htop));
15434 +       atomic_set(&finfo->fi_mmapped, 0);
15435 +       bindex = au_dbtop(dentry);
15436 +       if (!h_file) {
15437 +               h_dentry = au_h_dptr(dentry, bindex);
15438 +               err = vfsub_test_mntns(file->f_path.mnt, h_dentry->d_sb);
15439 +               if (unlikely(err))
15440 +                       goto out;
15441 +               h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
15442 +               if (IS_ERR(h_file)) {
15443 +                       err = PTR_ERR(h_file);
15444 +                       goto out;
15445 +               }
15446 +       } else {
15447 +               h_dentry = h_file->f_path.dentry;
15448 +               err = vfsub_test_mntns(file->f_path.mnt, h_dentry->d_sb);
15449 +               if (unlikely(err))
15450 +                       goto out;
15451 +               /* br ref is already inc-ed */
15452 +       }
15453 +
15454 +       if ((flags & __O_TMPFILE)
15455 +           && !(flags & O_EXCL)) {
15456 +               h_inode = file_inode(h_file);
15457 +               spin_lock(&h_inode->i_lock);
15458 +               h_inode->i_state |= I_LINKABLE;
15459 +               spin_unlock(&h_inode->i_lock);
15460 +       }
15461 +       au_set_fbtop(file, bindex);
15462 +       au_set_h_fptr(file, bindex, h_file);
15463 +       au_update_figen(file);
15464 +       /* todo: necessary? */
15465 +       /* file->f_ra = h_file->f_ra; */
15466 +
15467 +out:
15468 +       return err;
15469 +}
15470 +
15471 +static int aufs_open_nondir(struct inode *inode __maybe_unused,
15472 +                           struct file *file)
15473 +{
15474 +       int err;
15475 +       struct super_block *sb;
15476 +       struct au_do_open_args args = {
15477 +               .open   = au_do_open_nondir
15478 +       };
15479 +
15480 +       AuDbg("%pD, f_flags 0x%x, f_mode 0x%x\n",
15481 +             file, vfsub_file_flags(file), file->f_mode);
15482 +
15483 +       sb = file->f_path.dentry->d_sb;
15484 +       si_read_lock(sb, AuLock_FLUSH);
15485 +       err = au_do_open(file, &args);
15486 +       si_read_unlock(sb);
15487 +       return err;
15488 +}
15489 +
15490 +int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file)
15491 +{
15492 +       struct au_finfo *finfo;
15493 +       aufs_bindex_t bindex;
15494 +
15495 +       finfo = au_fi(file);
15496 +       au_hbl_del(&finfo->fi_hlist,
15497 +                  &au_sbi(file->f_path.dentry->d_sb)->si_files);
15498 +       bindex = finfo->fi_btop;
15499 +       if (bindex >= 0)
15500 +               au_set_h_fptr(file, bindex, NULL);
15501 +
15502 +       au_finfo_fin(file);
15503 +       return 0;
15504 +}
15505 +
15506 +/* ---------------------------------------------------------------------- */
15507 +
15508 +static int au_do_flush_nondir(struct file *file, fl_owner_t id)
15509 +{
15510 +       int err;
15511 +       struct file *h_file;
15512 +
15513 +       err = 0;
15514 +       h_file = au_hf_top(file);
15515 +       if (h_file)
15516 +               err = vfsub_flush(h_file, id);
15517 +       return err;
15518 +}
15519 +
15520 +static int aufs_flush_nondir(struct file *file, fl_owner_t id)
15521 +{
15522 +       return au_do_flush(file, id, au_do_flush_nondir);
15523 +}
15524 +
15525 +/* ---------------------------------------------------------------------- */
15526 +/*
15527 + * read and write functions acquire [fdi]_rwsem once, but release before
15528 + * mmap_sem. This is because to stop a race condition between mmap(2).
15529 + * Releasing these aufs-rwsem should be safe, no branch-management (by keeping
15530 + * si_rwsem), no harmful copy-up should happen. Actually copy-up may happen in
15531 + * read functions after [fdi]_rwsem are released, but it should be harmless.
15532 + */
15533 +
15534 +/* Callers should call au_read_post() or fput() in the end */
15535 +struct file *au_read_pre(struct file *file, int keep_fi, unsigned int lsc)
15536 +{
15537 +       struct file *h_file;
15538 +       int err;
15539 +
15540 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/0, lsc);
15541 +       if (!err) {
15542 +               di_read_unlock(file->f_path.dentry, AuLock_IR);
15543 +               h_file = au_hf_top(file);
15544 +               get_file(h_file);
15545 +               if (!keep_fi)
15546 +                       fi_read_unlock(file);
15547 +       } else
15548 +               h_file = ERR_PTR(err);
15549 +
15550 +       return h_file;
15551 +}
15552 +
15553 +static void au_read_post(struct inode *inode, struct file *h_file)
15554 +{
15555 +       /* update without lock, I don't think it a problem */
15556 +       fsstack_copy_attr_atime(inode, file_inode(h_file));
15557 +       fput(h_file);
15558 +}
15559 +
15560 +struct au_write_pre {
15561 +       /* input */
15562 +       unsigned int lsc;
15563 +
15564 +       /* output */
15565 +       blkcnt_t blks;
15566 +       aufs_bindex_t btop;
15567 +};
15568 +
15569 +/*
15570 + * return with iinfo is write-locked
15571 + * callers should call au_write_post() or iinfo_write_unlock() + fput() in the
15572 + * end
15573 + */
15574 +static struct file *au_write_pre(struct file *file, int do_ready,
15575 +                                struct au_write_pre *wpre)
15576 +{
15577 +       struct file *h_file;
15578 +       struct dentry *dentry;
15579 +       int err;
15580 +       unsigned int lsc;
15581 +       struct au_pin pin;
15582 +
15583 +       lsc = 0;
15584 +       if (wpre)
15585 +               lsc = wpre->lsc;
15586 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1, lsc);
15587 +       h_file = ERR_PTR(err);
15588 +       if (unlikely(err))
15589 +               goto out;
15590 +
15591 +       dentry = file->f_path.dentry;
15592 +       if (do_ready) {
15593 +               err = au_ready_to_write(file, -1, &pin);
15594 +               if (unlikely(err)) {
15595 +                       h_file = ERR_PTR(err);
15596 +                       di_write_unlock(dentry);
15597 +                       goto out_fi;
15598 +               }
15599 +       }
15600 +
15601 +       di_downgrade_lock(dentry, /*flags*/0);
15602 +       if (wpre)
15603 +               wpre->btop = au_fbtop(file);
15604 +       h_file = au_hf_top(file);
15605 +       get_file(h_file);
15606 +       if (wpre)
15607 +               wpre->blks = file_inode(h_file)->i_blocks;
15608 +       if (do_ready)
15609 +               au_unpin(&pin);
15610 +       di_read_unlock(dentry, /*flags*/0);
15611 +
15612 +out_fi:
15613 +       fi_write_unlock(file);
15614 +out:
15615 +       return h_file;
15616 +}
15617 +
15618 +static void au_write_post(struct inode *inode, struct file *h_file,
15619 +                         struct au_write_pre *wpre, ssize_t written)
15620 +{
15621 +       struct inode *h_inode;
15622 +
15623 +       au_cpup_attr_timesizes(inode);
15624 +       AuDebugOn(au_ibtop(inode) != wpre->btop);
15625 +       h_inode = file_inode(h_file);
15626 +       inode->i_mode = h_inode->i_mode;
15627 +       ii_write_unlock(inode);
15628 +       /* AuDbg("blks %llu, %llu\n", (u64)blks, (u64)h_inode->i_blocks); */
15629 +       if (written > 0)
15630 +               au_fhsm_wrote(inode->i_sb, wpre->btop,
15631 +                             /*force*/h_inode->i_blocks > wpre->blks);
15632 +       fput(h_file);
15633 +}
15634 +
15635 +/*
15636 + * todo: very ugly
15637 + * it locks both of i_mutex and si_rwsem for read in safe.
15638 + * if the plink maintenance mode continues forever (that is the problem),
15639 + * may loop forever.
15640 + */
15641 +static void au_mtx_and_read_lock(struct inode *inode)
15642 +{
15643 +       int err;
15644 +       struct super_block *sb = inode->i_sb;
15645 +
15646 +       while (1) {
15647 +               inode_lock(inode);
15648 +               err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
15649 +               if (!err)
15650 +                       break;
15651 +               inode_unlock(inode);
15652 +               si_read_lock(sb, AuLock_NOPLMW);
15653 +               si_read_unlock(sb);
15654 +       }
15655 +}
15656 +
15657 +static ssize_t au_do_iter(struct file *h_file, int rw, struct kiocb *kio,
15658 +                         struct iov_iter *iov_iter)
15659 +{
15660 +       ssize_t err;
15661 +       struct file *file;
15662 +       ssize_t (*iter)(struct kiocb *, struct iov_iter *);
15663 +
15664 +       err = security_file_permission(h_file, rw);
15665 +       if (unlikely(err))
15666 +               goto out;
15667 +
15668 +       err = -ENOSYS;  /* the branch doesn't have its ->(read|write)_iter() */
15669 +       iter = NULL;
15670 +       if (rw == MAY_READ)
15671 +               iter = h_file->f_op->read_iter;
15672 +       else if (rw == MAY_WRITE)
15673 +               iter = h_file->f_op->write_iter;
15674 +
15675 +       file = kio->ki_filp;
15676 +       kio->ki_filp = h_file;
15677 +       if (iter) {
15678 +               lockdep_off();
15679 +               err = iter(kio, iov_iter);
15680 +               lockdep_on();
15681 +       } else
15682 +               /* currently there is no such fs */
15683 +               WARN_ON_ONCE(1);
15684 +       kio->ki_filp = file;
15685 +
15686 +out:
15687 +       return err;
15688 +}
15689 +
15690 +static ssize_t aufs_read_iter(struct kiocb *kio, struct iov_iter *iov_iter)
15691 +{
15692 +       ssize_t err;
15693 +       struct file *file, *h_file;
15694 +       struct inode *inode;
15695 +       struct super_block *sb;
15696 +
15697 +       file = kio->ki_filp;
15698 +       inode = file_inode(file);
15699 +       sb = inode->i_sb;
15700 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
15701 +
15702 +       h_file = au_read_pre(file, /*keep_fi*/1, /*lsc*/0);
15703 +       err = PTR_ERR(h_file);
15704 +       if (IS_ERR(h_file))
15705 +               goto out;
15706 +
15707 +       if (au_test_loopback_kthread()) {
15708 +               au_warn_loopback(h_file->f_path.dentry->d_sb);
15709 +               if (file->f_mapping != h_file->f_mapping) {
15710 +                       file->f_mapping = h_file->f_mapping;
15711 +                       smp_mb(); /* unnecessary? */
15712 +               }
15713 +       }
15714 +       fi_read_unlock(file);
15715 +
15716 +       err = au_do_iter(h_file, MAY_READ, kio, iov_iter);
15717 +       /* todo: necessary? */
15718 +       /* file->f_ra = h_file->f_ra; */
15719 +       au_read_post(inode, h_file);
15720 +
15721 +out:
15722 +       si_read_unlock(sb);
15723 +       return err;
15724 +}
15725 +
15726 +static ssize_t aufs_write_iter(struct kiocb *kio, struct iov_iter *iov_iter)
15727 +{
15728 +       ssize_t err;
15729 +       struct au_write_pre wpre;
15730 +       struct inode *inode;
15731 +       struct file *file, *h_file;
15732 +
15733 +       file = kio->ki_filp;
15734 +       inode = file_inode(file);
15735 +       au_mtx_and_read_lock(inode);
15736 +
15737 +       wpre.lsc = 0;
15738 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15739 +       err = PTR_ERR(h_file);
15740 +       if (IS_ERR(h_file))
15741 +               goto out;
15742 +
15743 +       err = au_do_iter(h_file, MAY_WRITE, kio, iov_iter);
15744 +       au_write_post(inode, h_file, &wpre, err);
15745 +
15746 +out:
15747 +       si_read_unlock(inode->i_sb);
15748 +       inode_unlock(inode);
15749 +       return err;
15750 +}
15751 +
15752 +/*
15753 + * We may be able to remove aufs_splice_{read,write}() since almost all FSes
15754 + * don't have their own .splice_{read,write} implimentations, and they use
15755 + * generic_file_splice_read() and iter_file_splice_write() who can act like the
15756 + * simple converters to f_op->iter_read() and ->iter_write().
15757 + * But we keep our own implementations because some non-mainlined FSes may have
15758 + * their own .splice_{read,write} implimentations and aufs doesn't want to take
15759 + * away an opportunity to co-work with aufs from them.
15760 + */
15761 +static ssize_t aufs_splice_read(struct file *file, loff_t *ppos,
15762 +                               struct pipe_inode_info *pipe, size_t len,
15763 +                               unsigned int flags)
15764 +{
15765 +       ssize_t err;
15766 +       struct file *h_file;
15767 +       struct inode *inode;
15768 +       struct super_block *sb;
15769 +
15770 +       inode = file_inode(file);
15771 +       sb = inode->i_sb;
15772 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
15773 +
15774 +       h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
15775 +       err = PTR_ERR(h_file);
15776 +       if (IS_ERR(h_file))
15777 +               goto out;
15778 +
15779 +       err = vfsub_splice_to(h_file, ppos, pipe, len, flags);
15780 +       /* todo: necessary? */
15781 +       /* file->f_ra = h_file->f_ra; */
15782 +       au_read_post(inode, h_file);
15783 +
15784 +out:
15785 +       si_read_unlock(sb);
15786 +       return err;
15787 +}
15788 +
15789 +static ssize_t
15790 +aufs_splice_write(struct pipe_inode_info *pipe, struct file *file, loff_t *ppos,
15791 +                 size_t len, unsigned int flags)
15792 +{
15793 +       ssize_t err;
15794 +       struct au_write_pre wpre;
15795 +       struct inode *inode;
15796 +       struct file *h_file;
15797 +
15798 +       inode = file_inode(file);
15799 +       au_mtx_and_read_lock(inode);
15800 +
15801 +       wpre.lsc = 0;
15802 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15803 +       err = PTR_ERR(h_file);
15804 +       if (IS_ERR(h_file))
15805 +               goto out;
15806 +
15807 +       err = vfsub_splice_from(pipe, h_file, ppos, len, flags);
15808 +       au_write_post(inode, h_file, &wpre, err);
15809 +
15810 +out:
15811 +       si_read_unlock(inode->i_sb);
15812 +       inode_unlock(inode);
15813 +       return err;
15814 +}
15815 +
15816 +static long aufs_fallocate(struct file *file, int mode, loff_t offset,
15817 +                          loff_t len)
15818 +{
15819 +       long err;
15820 +       struct au_write_pre wpre;
15821 +       struct inode *inode;
15822 +       struct file *h_file;
15823 +
15824 +       inode = file_inode(file);
15825 +       au_mtx_and_read_lock(inode);
15826 +
15827 +       wpre.lsc = 0;
15828 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15829 +       err = PTR_ERR(h_file);
15830 +       if (IS_ERR(h_file))
15831 +               goto out;
15832 +
15833 +       lockdep_off();
15834 +       err = vfs_fallocate(h_file, mode, offset, len);
15835 +       lockdep_on();
15836 +       au_write_post(inode, h_file, &wpre, /*written*/1);
15837 +
15838 +out:
15839 +       si_read_unlock(inode->i_sb);
15840 +       inode_unlock(inode);
15841 +       return err;
15842 +}
15843 +
15844 +static ssize_t aufs_copy_file_range(struct file *src, loff_t src_pos,
15845 +                                   struct file *dst, loff_t dst_pos,
15846 +                                   size_t len, unsigned int flags)
15847 +{
15848 +       ssize_t err;
15849 +       struct au_write_pre wpre;
15850 +       enum { SRC, DST };
15851 +       struct {
15852 +               struct inode *inode;
15853 +               struct file *h_file;
15854 +               struct super_block *h_sb;
15855 +       } a[2];
15856 +#define a_src  a[SRC]
15857 +#define a_dst  a[DST]
15858 +
15859 +       err = -EINVAL;
15860 +       a_src.inode = file_inode(src);
15861 +       if (unlikely(!S_ISREG(a_src.inode->i_mode)))
15862 +               goto out;
15863 +       a_dst.inode = file_inode(dst);
15864 +       if (unlikely(!S_ISREG(a_dst.inode->i_mode)))
15865 +               goto out;
15866 +
15867 +       au_mtx_and_read_lock(a_dst.inode);
15868 +       /*
15869 +        * in order to match the order in di_write_lock2_{child,parent}(),
15870 +        * use f_path.dentry for this comparison.
15871 +        */
15872 +       if (src->f_path.dentry < dst->f_path.dentry) {
15873 +               a_src.h_file = au_read_pre(src, /*keep_fi*/1, AuLsc_FI_1);
15874 +               err = PTR_ERR(a_src.h_file);
15875 +               if (IS_ERR(a_src.h_file))
15876 +                       goto out_si;
15877 +
15878 +               wpre.lsc = AuLsc_FI_2;
15879 +               a_dst.h_file = au_write_pre(dst, /*do_ready*/1, &wpre);
15880 +               err = PTR_ERR(a_dst.h_file);
15881 +               if (IS_ERR(a_dst.h_file)) {
15882 +                       au_read_post(a_src.inode, a_src.h_file);
15883 +                       goto out_si;
15884 +               }
15885 +       } else {
15886 +               wpre.lsc = AuLsc_FI_1;
15887 +               a_dst.h_file = au_write_pre(dst, /*do_ready*/1, &wpre);
15888 +               err = PTR_ERR(a_dst.h_file);
15889 +               if (IS_ERR(a_dst.h_file))
15890 +                       goto out_si;
15891 +
15892 +               a_src.h_file = au_read_pre(src, /*keep_fi*/1, AuLsc_FI_2);
15893 +               err = PTR_ERR(a_src.h_file);
15894 +               if (IS_ERR(a_src.h_file)) {
15895 +                       au_write_post(a_dst.inode, a_dst.h_file, &wpre,
15896 +                                     /*written*/0);
15897 +                       goto out_si;
15898 +               }
15899 +       }
15900 +
15901 +       err = -EXDEV;
15902 +       a_src.h_sb = file_inode(a_src.h_file)->i_sb;
15903 +       a_dst.h_sb = file_inode(a_dst.h_file)->i_sb;
15904 +       if (unlikely(a_src.h_sb != a_dst.h_sb)) {
15905 +               AuDbgFile(src);
15906 +               AuDbgFile(dst);
15907 +               goto out_file;
15908 +       }
15909 +
15910 +       err = vfsub_copy_file_range(a_src.h_file, src_pos, a_dst.h_file,
15911 +                                   dst_pos, len, flags);
15912 +
15913 +out_file:
15914 +       au_write_post(a_dst.inode, a_dst.h_file, &wpre, err);
15915 +       fi_read_unlock(src);
15916 +       au_read_post(a_src.inode, a_src.h_file);
15917 +out_si:
15918 +       si_read_unlock(a_dst.inode->i_sb);
15919 +       inode_unlock(a_dst.inode);
15920 +out:
15921 +       return err;
15922 +#undef a_src
15923 +#undef a_dst
15924 +}
15925 +
15926 +/* ---------------------------------------------------------------------- */
15927 +
15928 +/*
15929 + * The locking order around current->mmap_sem.
15930 + * - in most and regular cases
15931 + *   file I/O syscall -- aufs_read() or something
15932 + *     -- si_rwsem for read -- mmap_sem
15933 + *     (Note that [fdi]i_rwsem are released before mmap_sem).
15934 + * - in mmap case
15935 + *   mmap(2) -- mmap_sem -- aufs_mmap() -- si_rwsem for read -- [fdi]i_rwsem
15936 + * This AB-BA order is definitely bad, but is not a problem since "si_rwsem for
15937 + * read" allows multiple processes to acquire it and [fdi]i_rwsem are not held
15938 + * in file I/O. Aufs needs to stop lockdep in aufs_mmap() though.
15939 + * It means that when aufs acquires si_rwsem for write, the process should never
15940 + * acquire mmap_sem.
15941 + *
15942 + * Actually aufs_iterate() holds [fdi]i_rwsem before mmap_sem, but this is not a
15943 + * problem either since any directory is not able to be mmap-ed.
15944 + * The similar scenario is applied to aufs_readlink() too.
15945 + */
15946 +
15947 +#if 0 /* stop calling security_file_mmap() */
15948 +/* cf. linux/include/linux/mman.h: calc_vm_prot_bits() */
15949 +#define AuConv_VM_PROT(f, b)   _calc_vm_trans(f, VM_##b, PROT_##b)
15950 +
15951 +static unsigned long au_arch_prot_conv(unsigned long flags)
15952 +{
15953 +       /* currently ppc64 only */
15954 +#ifdef CONFIG_PPC64
15955 +       /* cf. linux/arch/powerpc/include/asm/mman.h */
15956 +       AuDebugOn(arch_calc_vm_prot_bits(-1) != VM_SAO);
15957 +       return AuConv_VM_PROT(flags, SAO);
15958 +#else
15959 +       AuDebugOn(arch_calc_vm_prot_bits(-1));
15960 +       return 0;
15961 +#endif
15962 +}
15963 +
15964 +static unsigned long au_prot_conv(unsigned long flags)
15965 +{
15966 +       return AuConv_VM_PROT(flags, READ)
15967 +               | AuConv_VM_PROT(flags, WRITE)
15968 +               | AuConv_VM_PROT(flags, EXEC)
15969 +               | au_arch_prot_conv(flags);
15970 +}
15971 +
15972 +/* cf. linux/include/linux/mman.h: calc_vm_flag_bits() */
15973 +#define AuConv_VM_MAP(f, b)    _calc_vm_trans(f, VM_##b, MAP_##b)
15974 +
15975 +static unsigned long au_flag_conv(unsigned long flags)
15976 +{
15977 +       return AuConv_VM_MAP(flags, GROWSDOWN)
15978 +               | AuConv_VM_MAP(flags, DENYWRITE)
15979 +               | AuConv_VM_MAP(flags, LOCKED);
15980 +}
15981 +#endif
15982 +
15983 +static int aufs_mmap(struct file *file, struct vm_area_struct *vma)
15984 +{
15985 +       int err;
15986 +       const unsigned char wlock
15987 +               = (file->f_mode & FMODE_WRITE) && (vma->vm_flags & VM_SHARED);
15988 +       struct super_block *sb;
15989 +       struct file *h_file;
15990 +       struct inode *inode;
15991 +
15992 +       AuDbgVmRegion(file, vma);
15993 +
15994 +       inode = file_inode(file);
15995 +       sb = inode->i_sb;
15996 +       lockdep_off();
15997 +       si_read_lock(sb, AuLock_NOPLMW);
15998 +
15999 +       h_file = au_write_pre(file, wlock, /*wpre*/NULL);
16000 +       lockdep_on();
16001 +       err = PTR_ERR(h_file);
16002 +       if (IS_ERR(h_file))
16003 +               goto out;
16004 +
16005 +       err = 0;
16006 +       au_set_mmapped(file);
16007 +       au_vm_file_reset(vma, h_file);
16008 +       /*
16009 +        * we cannot call security_mmap_file() here since it may acquire
16010 +        * mmap_sem or i_mutex.
16011 +        *
16012 +        * err = security_mmap_file(h_file, au_prot_conv(vma->vm_flags),
16013 +        *                       au_flag_conv(vma->vm_flags));
16014 +        */
16015 +       if (!err)
16016 +               err = call_mmap(h_file, vma);
16017 +       if (!err) {
16018 +               au_vm_prfile_set(vma, file);
16019 +               fsstack_copy_attr_atime(inode, file_inode(h_file));
16020 +               goto out_fput; /* success */
16021 +       }
16022 +       au_unset_mmapped(file);
16023 +       au_vm_file_reset(vma, file);
16024 +
16025 +out_fput:
16026 +       lockdep_off();
16027 +       ii_write_unlock(inode);
16028 +       lockdep_on();
16029 +       fput(h_file);
16030 +out:
16031 +       lockdep_off();
16032 +       si_read_unlock(sb);
16033 +       lockdep_on();
16034 +       AuTraceErr(err);
16035 +       return err;
16036 +}
16037 +
16038 +/* ---------------------------------------------------------------------- */
16039 +
16040 +static int aufs_fsync_nondir(struct file *file, loff_t start, loff_t end,
16041 +                            int datasync)
16042 +{
16043 +       int err;
16044 +       struct au_write_pre wpre;
16045 +       struct inode *inode;
16046 +       struct file *h_file;
16047 +
16048 +       err = 0; /* -EBADF; */ /* posix? */
16049 +       if (unlikely(!(file->f_mode & FMODE_WRITE)))
16050 +               goto out;
16051 +
16052 +       inode = file_inode(file);
16053 +       au_mtx_and_read_lock(inode);
16054 +
16055 +       wpre.lsc = 0;
16056 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
16057 +       err = PTR_ERR(h_file);
16058 +       if (IS_ERR(h_file))
16059 +               goto out_unlock;
16060 +
16061 +       err = vfsub_fsync(h_file, &h_file->f_path, datasync);
16062 +       au_write_post(inode, h_file, &wpre, /*written*/0);
16063 +
16064 +out_unlock:
16065 +       si_read_unlock(inode->i_sb);
16066 +       inode_unlock(inode);
16067 +out:
16068 +       return err;
16069 +}
16070 +
16071 +static int aufs_fasync(int fd, struct file *file, int flag)
16072 +{
16073 +       int err;
16074 +       struct file *h_file;
16075 +       struct super_block *sb;
16076 +
16077 +       sb = file->f_path.dentry->d_sb;
16078 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
16079 +
16080 +       h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
16081 +       err = PTR_ERR(h_file);
16082 +       if (IS_ERR(h_file))
16083 +               goto out;
16084 +
16085 +       if (h_file->f_op->fasync)
16086 +               err = h_file->f_op->fasync(fd, h_file, flag);
16087 +       fput(h_file); /* instead of au_read_post() */
16088 +
16089 +out:
16090 +       si_read_unlock(sb);
16091 +       return err;
16092 +}
16093 +
16094 +static int aufs_setfl(struct file *file, unsigned long arg)
16095 +{
16096 +       int err;
16097 +       struct file *h_file;
16098 +       struct super_block *sb;
16099 +
16100 +       sb = file->f_path.dentry->d_sb;
16101 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
16102 +
16103 +       h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
16104 +       err = PTR_ERR(h_file);
16105 +       if (IS_ERR(h_file))
16106 +               goto out;
16107 +
16108 +       /* stop calling h_file->fasync */
16109 +       arg |= vfsub_file_flags(file) & FASYNC;
16110 +       err = setfl(/*unused fd*/-1, h_file, arg);
16111 +       fput(h_file); /* instead of au_read_post() */
16112 +
16113 +out:
16114 +       si_read_unlock(sb);
16115 +       return err;
16116 +}
16117 +
16118 +/* ---------------------------------------------------------------------- */
16119 +
16120 +/* no one supports this operation, currently */
16121 +#if 0 /* reserved for future use */
16122 +static ssize_t aufs_sendpage(struct file *file, struct page *page, int offset,
16123 +                            size_t len, loff_t *pos, int more)
16124 +{
16125 +}
16126 +#endif
16127 +
16128 +/* ---------------------------------------------------------------------- */
16129 +
16130 +const struct file_operations aufs_file_fop = {
16131 +       .owner          = THIS_MODULE,
16132 +
16133 +       .llseek         = default_llseek,
16134 +
16135 +       .read_iter      = aufs_read_iter,
16136 +       .write_iter     = aufs_write_iter,
16137 +
16138 +#ifdef CONFIG_AUFS_POLL
16139 +       .poll           = aufs_poll,
16140 +#endif
16141 +       .unlocked_ioctl = aufs_ioctl_nondir,
16142 +#ifdef CONFIG_COMPAT
16143 +       .compat_ioctl   = aufs_compat_ioctl_nondir,
16144 +#endif
16145 +       .mmap           = aufs_mmap,
16146 +       .open           = aufs_open_nondir,
16147 +       .flush          = aufs_flush_nondir,
16148 +       .release        = aufs_release_nondir,
16149 +       .fsync          = aufs_fsync_nondir,
16150 +       .fasync         = aufs_fasync,
16151 +       /* .sendpage    = aufs_sendpage, */
16152 +       .setfl          = aufs_setfl,
16153 +       .splice_write   = aufs_splice_write,
16154 +       .splice_read    = aufs_splice_read,
16155 +#if 0 /* reserved for future use */
16156 +       .aio_splice_write = aufs_aio_splice_write,
16157 +       .aio_splice_read  = aufs_aio_splice_read,
16158 +#endif
16159 +       .fallocate      = aufs_fallocate,
16160 +       .copy_file_range = aufs_copy_file_range
16161 +};
16162 diff -urN /usr/share/empty/fs/aufs/fsctx.c linux/fs/aufs/fsctx.c
16163 --- /usr/share/empty/fs/aufs/fsctx.c    1970-01-01 01:00:00.000000000 +0100
16164 +++ linux/fs/aufs/fsctx.c       2022-03-21 14:49:05.723299677 +0100
16165 @@ -0,0 +1,1242 @@
16166 +// SPDX-License-Identifier: GPL-2.0
16167 +/*
16168 + * Copyright (C) 2022 Junjiro R. Okajima
16169 + *
16170 + * This program, aufs is free software; you can redistribute it and/or modify
16171 + * it under the terms of the GNU General Public License as published by
16172 + * the Free Software Foundation; either version 2 of the License, or
16173 + * (at your option) any later version.
16174 + *
16175 + * This program is distributed in the hope that it will be useful,
16176 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
16177 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16178 + * GNU General Public License for more details.
16179 + *
16180 + * You should have received a copy of the GNU General Public License
16181 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16182 + */
16183 +
16184 +/*
16185 + * fs context, aka new mount api
16186 + */
16187 +
16188 +#include <linux/fs_context.h>
16189 +#include "aufs.h"
16190 +
16191 +struct au_fsctx_opts {
16192 +       aufs_bindex_t bindex;
16193 +       unsigned char skipped;
16194 +       struct au_opt *opt, *opt_tail;
16195 +       struct super_block *sb;
16196 +       struct au_sbinfo *sbinfo;
16197 +       struct au_opts opts;
16198 +};
16199 +
16200 +/* stop extra interpretation of errno in mount(8), and strange error messages */
16201 +static int cvt_err(int err)
16202 +{
16203 +       AuTraceErr(err);
16204 +
16205 +       switch (err) {
16206 +       case -ENOENT:
16207 +       case -ENOTDIR:
16208 +       case -EEXIST:
16209 +       case -EIO:
16210 +               err = -EINVAL;
16211 +       }
16212 +       return err;
16213 +}
16214 +
16215 +static int au_fsctx_reconfigure(struct fs_context *fc)
16216 +{
16217 +       int err, do_dx;
16218 +       unsigned int mntflags;
16219 +       struct dentry *root;
16220 +       struct super_block *sb;
16221 +       struct inode *inode;
16222 +       struct au_fsctx_opts *a = fc->fs_private;
16223 +
16224 +       AuDbg("fc %p\n", fc);
16225 +
16226 +       root = fc->root;
16227 +       sb = root->d_sb;
16228 +       err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
16229 +       if (!err) {
16230 +               di_write_lock_child(root);
16231 +               err = au_opts_verify(sb, fc->sb_flags, /*pending*/0);
16232 +               aufs_write_unlock(root);
16233 +       }
16234 +
16235 +       inode = d_inode(root);
16236 +       inode_lock(inode);
16237 +       err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
16238 +       if (unlikely(err))
16239 +               goto out;
16240 +       di_write_lock_child(root);
16241 +
16242 +       /* au_opts_remount() may return an error */
16243 +       err = au_opts_remount(sb, &a->opts);
16244 +
16245 +       if (au_ftest_opts(a->opts.flags, REFRESH))
16246 +               au_remount_refresh(sb, au_ftest_opts(a->opts.flags,
16247 +                                                    REFRESH_IDOP));
16248 +
16249 +       if (au_ftest_opts(a->opts.flags, REFRESH_DYAOP)) {
16250 +               mntflags = au_mntflags(sb);
16251 +               do_dx = !!au_opt_test(mntflags, DIO);
16252 +               au_dy_arefresh(do_dx);
16253 +       }
16254 +
16255 +       au_fhsm_wrote_all(sb, /*force*/1); /* ?? */
16256 +       aufs_write_unlock(root);
16257 +
16258 +out:
16259 +       inode_unlock(inode);
16260 +       err = cvt_err(err);
16261 +       AuTraceErr(err);
16262 +
16263 +       return err;
16264 +}
16265 +
16266 +/* ---------------------------------------------------------------------- */
16267 +
16268 +static int au_fsctx_fill_super(struct super_block *sb, struct fs_context *fc)
16269 +{
16270 +       int err;
16271 +       struct au_fsctx_opts *a = fc->fs_private;
16272 +       struct au_sbinfo *sbinfo = a->sbinfo;
16273 +       struct dentry *root;
16274 +       struct inode *inode;
16275 +
16276 +       sbinfo->si_sb = sb;
16277 +       sb->s_fs_info = sbinfo;
16278 +       kobject_get(&sbinfo->si_kobj);
16279 +
16280 +       __si_write_lock(sb);
16281 +       si_pid_set(sb);
16282 +       au_sbilist_add(sb);
16283 +
16284 +       /* all timestamps always follow the ones on the branch */
16285 +       sb->s_flags |= SB_NOATIME | SB_NODIRATIME;
16286 +       sb->s_flags |= SB_I_VERSION; /* do we really need this? */
16287 +       sb->s_op = &aufs_sop;
16288 +       sb->s_d_op = &aufs_dop;
16289 +       sb->s_magic = AUFS_SUPER_MAGIC;
16290 +       sb->s_maxbytes = 0;
16291 +       sb->s_stack_depth = 1;
16292 +       au_export_init(sb);
16293 +       au_xattr_init(sb);
16294 +
16295 +       err = au_alloc_root(sb);
16296 +       if (unlikely(err)) {
16297 +               si_write_unlock(sb);
16298 +               goto out;
16299 +       }
16300 +       root = sb->s_root;
16301 +       inode = d_inode(root);
16302 +       ii_write_lock_parent(inode);
16303 +       aufs_write_unlock(root);
16304 +
16305 +       /* lock vfs_inode first, then aufs. */
16306 +       inode_lock(inode);
16307 +       aufs_write_lock(root);
16308 +       err = au_opts_mount(sb, &a->opts);
16309 +       AuTraceErr(err);
16310 +       if (!err && au_ftest_si(sbinfo, NO_DREVAL)) {
16311 +               sb->s_d_op = &aufs_dop_noreval;
16312 +               /* infofc(fc, "%ps", sb->s_d_op); */
16313 +               pr_info("%ps\n", sb->s_d_op);
16314 +               au_refresh_dop(root, /*force_reval*/0);
16315 +               sbinfo->si_iop_array = aufs_iop_nogetattr;
16316 +               au_refresh_iop(inode, /*force_getattr*/0);
16317 +       }
16318 +       aufs_write_unlock(root);
16319 +       inode_unlock(inode);
16320 +       if (!err)
16321 +               goto out; /* success */
16322 +
16323 +       dput(root);
16324 +       sb->s_root = NULL;
16325 +
16326 +out:
16327 +       if (unlikely(err))
16328 +               kobject_put(&sbinfo->si_kobj);
16329 +       AuTraceErr(err);
16330 +       err = cvt_err(err);
16331 +       AuTraceErr(err);
16332 +       return err;
16333 +}
16334 +
16335 +static int au_fsctx_get_tree(struct fs_context *fc)
16336 +{
16337 +       int err;
16338 +
16339 +       AuDbg("fc %p\n", fc);
16340 +       err = get_tree_nodev(fc, au_fsctx_fill_super);
16341 +
16342 +       AuTraceErr(err);
16343 +       return err;
16344 +}
16345 +
16346 +/* ---------------------------------------------------------------------- */
16347 +
16348 +static void au_fsctx_dump(struct au_opts *opts)
16349 +{
16350 +#ifdef CONFIG_AUFS_DEBUG
16351 +       /* reduce stack space */
16352 +       union {
16353 +               struct au_opt_add *add;
16354 +               struct au_opt_del *del;
16355 +               struct au_opt_mod *mod;
16356 +               struct au_opt_xino *xino;
16357 +               struct au_opt_xino_itrunc *xino_itrunc;
16358 +               struct au_opt_wbr_create *create;
16359 +       } u;
16360 +       struct au_opt *opt;
16361 +
16362 +       opt = opts->opt;
16363 +       while (opt->type != Opt_tail) {
16364 +               switch (opt->type) {
16365 +               case Opt_add:
16366 +                       u.add = &opt->add;
16367 +                       AuDbg("add {b%d, %s, 0x%x, %p}\n",
16368 +                                 u.add->bindex, u.add->pathname, u.add->perm,
16369 +                                 u.add->path.dentry);
16370 +                       break;
16371 +               case Opt_del:
16372 +                       fallthrough;
16373 +               case Opt_idel:
16374 +                       u.del = &opt->del;
16375 +                       AuDbg("del {%s, %p}\n",
16376 +                             u.del->pathname, u.del->h_path.dentry);
16377 +                       break;
16378 +               case Opt_mod:
16379 +                       fallthrough;
16380 +               case Opt_imod:
16381 +                       u.mod = &opt->mod;
16382 +                       AuDbg("mod {%s, 0x%x, %p}\n",
16383 +                                 u.mod->path, u.mod->perm, u.mod->h_root);
16384 +                       break;
16385 +               case Opt_append:
16386 +                       u.add = &opt->add;
16387 +                       AuDbg("append {b%d, %s, 0x%x, %p}\n",
16388 +                                 u.add->bindex, u.add->pathname, u.add->perm,
16389 +                                 u.add->path.dentry);
16390 +                       break;
16391 +               case Opt_prepend:
16392 +                       u.add = &opt->add;
16393 +                       AuDbg("prepend {b%d, %s, 0x%x, %p}\n",
16394 +                                 u.add->bindex, u.add->pathname, u.add->perm,
16395 +                                 u.add->path.dentry);
16396 +                       break;
16397 +
16398 +               case Opt_dirwh:
16399 +                       AuDbg("dirwh %d\n", opt->dirwh);
16400 +                       break;
16401 +               case Opt_rdcache:
16402 +                       AuDbg("rdcache %d\n", opt->rdcache);
16403 +                       break;
16404 +               case Opt_rdblk:
16405 +                       AuDbg("rdblk %d\n", opt->rdblk);
16406 +                       break;
16407 +               case Opt_rdhash:
16408 +                       AuDbg("rdhash %u\n", opt->rdhash);
16409 +                       break;
16410 +
16411 +               case Opt_xino:
16412 +                       u.xino = &opt->xino;
16413 +                       AuDbg("xino {%s %pD}\n", u.xino->path, u.xino->file);
16414 +                       break;
16415 +
16416 +#define au_fsctx_TF(name)                                        \
16417 +                       case Opt_##name:                          \
16418 +                               if (opt->tf)                      \
16419 +                                       AuLabel(name);            \
16420 +                               else                              \
16421 +                                       AuLabel(no##name);        \
16422 +                               break;
16423 +
16424 +               /* simple true/false flag */
16425 +               au_fsctx_TF(trunc_xino);
16426 +               au_fsctx_TF(trunc_xib);
16427 +               au_fsctx_TF(dirperm1);
16428 +               au_fsctx_TF(plink);
16429 +               au_fsctx_TF(shwh);
16430 +               au_fsctx_TF(dio);
16431 +               au_fsctx_TF(warn_perm);
16432 +               au_fsctx_TF(verbose);
16433 +               au_fsctx_TF(sum);
16434 +               au_fsctx_TF(dirren);
16435 +               au_fsctx_TF(acl);
16436 +#undef au_fsctx_TF
16437 +
16438 +               case Opt_trunc_xino_path:
16439 +                       fallthrough;
16440 +               case Opt_itrunc_xino:
16441 +                       u.xino_itrunc = &opt->xino_itrunc;
16442 +                       AuDbg("trunc_xino %d\n", u.xino_itrunc->bindex);
16443 +                       break;
16444 +               case Opt_noxino:
16445 +                       AuLabel(noxino);
16446 +                       break;
16447 +
16448 +               case Opt_list_plink:
16449 +                       AuLabel(list_plink);
16450 +                       break;
16451 +               case Opt_udba:
16452 +                       AuDbg("udba %d, %s\n",
16453 +                                 opt->udba, au_optstr_udba(opt->udba));
16454 +                       break;
16455 +               case Opt_diropq_a:
16456 +                       AuLabel(diropq_a);
16457 +                       break;
16458 +               case Opt_diropq_w:
16459 +                       AuLabel(diropq_w);
16460 +                       break;
16461 +               case Opt_wsum:
16462 +                       AuLabel(wsum);
16463 +                       break;
16464 +               case Opt_wbr_create:
16465 +                       u.create = &opt->wbr_create;
16466 +                       AuDbg("create %d, %s\n", u.create->wbr_create,
16467 +                                 au_optstr_wbr_create(u.create->wbr_create));
16468 +                       switch (u.create->wbr_create) {
16469 +                       case AuWbrCreate_MFSV:
16470 +                               fallthrough;
16471 +                       case AuWbrCreate_PMFSV:
16472 +                               AuDbg("%d sec\n", u.create->mfs_second);
16473 +                               break;
16474 +                       case AuWbrCreate_MFSRR:
16475 +                               fallthrough;
16476 +                       case AuWbrCreate_TDMFS:
16477 +                               AuDbg("%llu watermark\n",
16478 +                                         u.create->mfsrr_watermark);
16479 +                               break;
16480 +                       case AuWbrCreate_MFSRRV:
16481 +                               fallthrough;
16482 +                       case AuWbrCreate_TDMFSV:
16483 +                               fallthrough;
16484 +                       case AuWbrCreate_PMFSRRV:
16485 +                               AuDbg("%llu watermark, %d sec\n",
16486 +                                         u.create->mfsrr_watermark,
16487 +                                         u.create->mfs_second);
16488 +                               break;
16489 +                       }
16490 +                       break;
16491 +               case Opt_wbr_copyup:
16492 +                       AuDbg("copyup %d, %s\n", opt->wbr_copyup,
16493 +                                 au_optstr_wbr_copyup(opt->wbr_copyup));
16494 +                       break;
16495 +               case Opt_fhsm_sec:
16496 +                       AuDbg("fhsm_sec %u\n", opt->fhsm_second);
16497 +                       break;
16498 +
16499 +               default:
16500 +                       AuDbg("type %d\n", opt->type);
16501 +                       BUG();
16502 +               }
16503 +               opt++;
16504 +       }
16505 +#endif
16506 +}
16507 +
16508 +/* ---------------------------------------------------------------------- */
16509 +
16510 +/*
16511 + * For conditionally compiled mount options.
16512 + * Instead of fsparam_flag_no(), use this macro to distinguish ignore_silent.
16513 + */
16514 +#define au_ignore_flag(name, action)           \
16515 +       fsparam_flag(name, action),             \
16516 +       fsparam_flag("no" name, Opt_ignore_silent)
16517 +
16518 +const struct fs_parameter_spec aufs_fsctx_paramspec[] = {
16519 +       fsparam_string("br", Opt_br),
16520 +
16521 +       /* "add=%d:%s" or "ins=%d:%s" */
16522 +       fsparam_string("add", Opt_add),
16523 +       fsparam_string("ins", Opt_add),
16524 +       fsparam_path("append", Opt_append),
16525 +       fsparam_path("prepend", Opt_prepend),
16526 +
16527 +       fsparam_path("del", Opt_del),
16528 +       /* fsparam_s32("idel", Opt_idel), */
16529 +       fsparam_path("mod", Opt_mod),
16530 +       /* fsparam_string("imod", Opt_imod), */
16531 +
16532 +       fsparam_s32("dirwh", Opt_dirwh),
16533 +
16534 +       fsparam_path("xino", Opt_xino),
16535 +       fsparam_flag("noxino", Opt_noxino),
16536 +       fsparam_flag_no("trunc_xino", Opt_trunc_xino),
16537 +       /* "trunc_xino_v=%d:%d" */
16538 +       /* fsparam_string("trunc_xino_v", Opt_trunc_xino_v), */
16539 +       fsparam_path("trunc_xino", Opt_trunc_xino_path),
16540 +       fsparam_s32("itrunc_xino", Opt_itrunc_xino),
16541 +       /* fsparam_path("zxino", Opt_zxino), */
16542 +       fsparam_flag_no("trunc_xib", Opt_trunc_xib),
16543 +
16544 +#ifdef CONFIG_PROC_FS
16545 +       fsparam_flag_no("plink", Opt_plink),
16546 +#else
16547 +       au_ignore_flag("plink", Opt_ignore),
16548 +#endif
16549 +
16550 +#ifdef CONFIG_AUFS_DEBUG
16551 +       fsparam_flag("list_plink", Opt_list_plink),
16552 +#endif
16553 +
16554 +       fsparam_string("udba", Opt_udba),
16555 +
16556 +       fsparam_flag_no("dio", Opt_dio),
16557 +
16558 +#ifdef CONFIG_AUFS_DIRREN
16559 +       fsparam_flag_no("dirren", Opt_dirren),
16560 +#else
16561 +       au_ignore_flag("dirren", Opt_ignore),
16562 +#endif
16563 +
16564 +#ifdef CONFIG_AUFS_FHSM
16565 +       fsparam_s32("fhsm_sec", Opt_fhsm_sec),
16566 +#else
16567 +       fsparam_s32("fhsm_sec", Opt_ignore),
16568 +#endif
16569 +
16570 +       /* always | a | whiteouted | w */
16571 +       fsparam_string("diropq", Opt_diropq),
16572 +
16573 +       fsparam_flag_no("warn_perm", Opt_warn_perm),
16574 +
16575 +#ifdef CONFIG_AUFS_SHWH
16576 +       fsparam_flag_no("shwh", Opt_shwh),
16577 +#else
16578 +       au_ignore_flag("shwh", Opt_err),
16579 +#endif
16580 +
16581 +       fsparam_flag_no("dirperm1", Opt_dirperm1),
16582 +
16583 +       fsparam_flag_no("verbose", Opt_verbose),
16584 +       fsparam_flag("v", Opt_verbose),
16585 +       fsparam_flag("quiet", Opt_noverbose),
16586 +       fsparam_flag("q", Opt_noverbose),
16587 +       /* user-space may handle this */
16588 +       fsparam_flag("silent", Opt_noverbose),
16589 +
16590 +       fsparam_flag_no("sum", Opt_sum),
16591 +       fsparam_flag("wsum", Opt_wsum),
16592 +
16593 +       fsparam_s32("rdcache", Opt_rdcache),
16594 +       /* "def" or s32 */
16595 +       fsparam_string("rdblk", Opt_rdblk),
16596 +       /* "def" or s32 */
16597 +       fsparam_string("rdhash", Opt_rdhash),
16598 +
16599 +       fsparam_string("create", Opt_wbr_create),
16600 +       fsparam_string("create_policy", Opt_wbr_create),
16601 +       fsparam_string("cpup", Opt_wbr_copyup),
16602 +       fsparam_string("copyup", Opt_wbr_copyup),
16603 +       fsparam_string("copyup_policy", Opt_wbr_copyup),
16604 +
16605 +       /* generic VFS flag */
16606 +#ifdef CONFIG_FS_POSIX_ACL
16607 +       fsparam_flag_no("acl", Opt_acl),
16608 +#else
16609 +       au_ignore_flag("acl"),
16610 +#endif
16611 +
16612 +       /* internal use for the scripts */
16613 +       fsparam_string("si", Opt_ignore_silent),
16614 +
16615 +       /* obsoleted, keep them temporary */
16616 +       fsparam_flag("nodlgt", Opt_ignore_silent),
16617 +       fsparam_flag("clean_plink", Opt_ignore),
16618 +       fsparam_string("dirs", Opt_br),
16619 +       fsparam_u32("debug", Opt_ignore),
16620 +       /* "whiteout" or "all" */
16621 +       fsparam_string("delete", Opt_ignore),
16622 +       fsparam_string("imap", Opt_ignore),
16623 +
16624 +       /* temporary workaround, due to old mount(8)? */
16625 +       fsparam_flag("relatime", Opt_ignore_silent),
16626 +
16627 +       {}
16628 +};
16629 +
16630 +static int au_fsctx_parse_do_add(struct fs_context *fc, struct au_opt *opt,
16631 +                                char *brspec, size_t speclen,
16632 +                                aufs_bindex_t bindex)
16633 +{
16634 +       int err;
16635 +       char *p;
16636 +
16637 +       AuDbg("brspec %s\n", brspec);
16638 +
16639 +       err = -ENOMEM;
16640 +       if (!speclen)
16641 +               speclen = strlen(brspec);
16642 +       /* will be freed by au_fsctx_free() */
16643 +       p = kmemdup_nul(brspec, speclen, GFP_NOFS);
16644 +       if (unlikely(!p)) {
16645 +               errorfc(fc, "failed in %s", brspec);
16646 +               goto out;
16647 +       }
16648 +       err = au_opt_add(opt, p, fc->sb_flags, bindex);
16649 +
16650 +out:
16651 +       AuTraceErr(err);
16652 +       return err;
16653 +}
16654 +
16655 +static int au_fsctx_parse_br(struct fs_context *fc, char *brspec)
16656 +{
16657 +       int err;
16658 +       char *p;
16659 +       struct au_fsctx_opts *a = fc->fs_private;
16660 +       struct au_opt *opt = a->opt;
16661 +       aufs_bindex_t bindex = a->bindex;
16662 +
16663 +       AuDbg("brspec %s\n", brspec);
16664 +
16665 +       err = -EINVAL;
16666 +       while ((p = strsep(&brspec, ":")) && *p) {
16667 +               err = au_fsctx_parse_do_add(fc, opt, p, /*len*/0, bindex);
16668 +               AuTraceErr(err);
16669 +               if (unlikely(err))
16670 +                       break;
16671 +               bindex++;
16672 +               opt++;
16673 +               if (unlikely(opt > a->opt_tail)) {
16674 +                       err = -E2BIG;
16675 +                       bindex--;
16676 +                       opt--;
16677 +                       break;
16678 +               }
16679 +               opt->type = Opt_tail;
16680 +               a->skipped = 1;
16681 +       }
16682 +       a->bindex = bindex;
16683 +       a->opt = opt;
16684 +
16685 +       AuTraceErr(err);
16686 +       return err;
16687 +}
16688 +
16689 +static int au_fsctx_parse_add(struct fs_context *fc, char *addspec)
16690 +{
16691 +       int err, n;
16692 +       char *p;
16693 +       struct au_fsctx_opts *a = fc->fs_private;
16694 +       struct au_opt *opt = a->opt;
16695 +
16696 +       err = -EINVAL;
16697 +       p = strchr(addspec, ':');
16698 +       if (unlikely(!p)) {
16699 +               errorfc(fc, "bad arg in %s", addspec);
16700 +               goto out;
16701 +       }
16702 +       *p++ = '\0';
16703 +       err = kstrtoint(addspec, 0, &n);
16704 +       if (unlikely(err)) {
16705 +               errorfc(fc, "bad integer in %s", addspec);
16706 +               goto out;
16707 +       }
16708 +       AuDbg("n %d\n", n);
16709 +       err = au_fsctx_parse_do_add(fc, opt, p, /*len*/0, n);
16710 +
16711 +out:
16712 +       AuTraceErr(err);
16713 +       return err;
16714 +}
16715 +
16716 +static int au_fsctx_parse_del(struct fs_context *fc, struct au_opt_del *del,
16717 +                             struct fs_parameter *param)
16718 +{
16719 +       int err;
16720 +
16721 +       err = -ENOMEM;
16722 +       /* will be freed by au_fsctx_free() */
16723 +       del->pathname = kmemdup_nul(param->string, param->size, GFP_NOFS);
16724 +       if (unlikely(!del->pathname))
16725 +               goto out;
16726 +       AuDbg("del %s\n", del->pathname);
16727 +       err = vfsub_kern_path(del->pathname, AuOpt_LkupDirFlags, &del->h_path);
16728 +       if (unlikely(err))
16729 +               errorfc(fc, "lookup failed %s (%d)", del->pathname, err);
16730 +
16731 +out:
16732 +       AuTraceErr(err);
16733 +       return err;
16734 +}
16735 +
16736 +#if 0 /* reserved for future use */
16737 +static int au_fsctx_parse_idel(struct fs_context *fc, struct au_opt_del *del,
16738 +                              aufs_bindex_t bindex)
16739 +{
16740 +       int err;
16741 +       struct super_block *sb;
16742 +       struct dentry *root;
16743 +       struct au_fsctx_opts *a = fc->fs_private;
16744 +
16745 +       sb = a->sb;
16746 +       AuDebugOn(!sb);
16747 +
16748 +       err = -EINVAL;
16749 +       root = sb->s_root;
16750 +       aufs_read_lock(root, AuLock_FLUSH);
16751 +       if (bindex < 0 || au_sbbot(sb) < bindex) {
16752 +               errorfc(fc, "out of bounds, %d", bindex);
16753 +               goto out;
16754 +       }
16755 +
16756 +       err = 0;
16757 +       del->h_path.dentry = dget(au_h_dptr(root, bindex));
16758 +       del->h_path.mnt = mntget(au_sbr_mnt(sb, bindex));
16759 +
16760 +out:
16761 +       aufs_read_unlock(root, !AuLock_IR);
16762 +       AuTraceErr(err);
16763 +       return err;
16764 +}
16765 +#endif
16766 +
16767 +static int au_fsctx_parse_mod(struct fs_context *fc, struct au_opt_mod *mod,
16768 +                             struct fs_parameter *param)
16769 +{
16770 +       int err;
16771 +       struct path path;
16772 +       char *p;
16773 +
16774 +       err = -ENOMEM;
16775 +       /* will be freed by au_fsctx_free() */
16776 +       mod->path = kmemdup_nul(param->string, param->size, GFP_NOFS);
16777 +       if (unlikely(!mod->path))
16778 +               goto out;
16779 +
16780 +       err = -EINVAL;
16781 +       p = strchr(mod->path, '=');
16782 +       if (unlikely(!p)) {
16783 +               errorfc(fc, "no permission %s", mod->path);
16784 +               goto out;
16785 +       }
16786 +
16787 +       *p++ = 0;
16788 +       err = vfsub_kern_path(mod->path, AuOpt_LkupDirFlags, &path);
16789 +       if (unlikely(err)) {
16790 +               errorfc(fc, "lookup failed %s (%d)", mod->path, err);
16791 +               goto out;
16792 +       }
16793 +
16794 +       mod->perm = au_br_perm_val(p);
16795 +       AuDbg("mod path %s, perm 0x%x, %s", mod->path, mod->perm, p);
16796 +       mod->h_root = dget(path.dentry);
16797 +       path_put(&path);
16798 +
16799 +out:
16800 +       AuTraceErr(err);
16801 +       return err;
16802 +}
16803 +
16804 +#if 0 /* reserved for future use */
16805 +static int au_fsctx_parse_imod(struct fs_context *fc, struct au_opt_mod *mod,
16806 +                              char *ibrspec)
16807 +{
16808 +       int err, n;
16809 +       char *p;
16810 +       struct super_block *sb;
16811 +       struct dentry *root;
16812 +       struct au_fsctx_opts *a = fc->fs_private;
16813 +
16814 +       sb = a->sb;
16815 +       AuDebugOn(!sb);
16816 +
16817 +       err = -EINVAL;
16818 +       p = strchr(ibrspec, ':');
16819 +       if (unlikely(!p)) {
16820 +               errorfc(fc, "no index, %s", ibrspec);
16821 +               goto out;
16822 +       }
16823 +       *p++ = '\0';
16824 +       err = kstrtoint(ibrspec, 0, &n);
16825 +       if (unlikely(err)) {
16826 +               errorfc(fc, "bad integer in %s", ibrspec);
16827 +               goto out;
16828 +       }
16829 +       AuDbg("n %d\n", n);
16830 +
16831 +       root = sb->s_root;
16832 +       aufs_read_lock(root, AuLock_FLUSH);
16833 +       if (n < 0 || au_sbbot(sb) < n) {
16834 +               errorfc(fc, "out of bounds, %d", bindex);
16835 +               goto out_root;
16836 +       }
16837 +
16838 +       err = 0;
16839 +       mod->perm = au_br_perm_val(p);
16840 +       AuDbg("mod path %s, perm 0x%x, %s\n",
16841 +             mod->path, mod->perm, p);
16842 +       mod->h_root = dget(au_h_dptr(root, bindex));
16843 +
16844 +out_root:
16845 +       aufs_read_unlock(root, !AuLock_IR);
16846 +out:
16847 +       AuTraceErr(err);
16848 +       return err;
16849 +}
16850 +#endif
16851 +
16852 +static int au_fsctx_parse_xino(struct fs_context *fc,
16853 +                              struct au_opt_xino *xino,
16854 +                              struct fs_parameter *param)
16855 +{
16856 +       int err;
16857 +       struct au_fsctx_opts *a = fc->fs_private;
16858 +
16859 +       err = -ENOMEM;
16860 +       /* will be freed by au_opts_free() */
16861 +       xino->path = kmemdup_nul(param->string, param->size, GFP_NOFS);
16862 +       if (unlikely(!xino->path))
16863 +               goto out;
16864 +       AuDbg("path %s\n", xino->path);
16865 +
16866 +       xino->file = au_xino_create(a->sb, xino->path, /*silent*/0,
16867 +                                   /*wbrtop*/0);
16868 +       err = PTR_ERR(xino->file);
16869 +       if (IS_ERR(xino->file)) {
16870 +               xino->file = NULL;
16871 +               goto out;
16872 +       }
16873 +
16874 +       err = 0;
16875 +       if (unlikely(a->sb && xino->file->f_path.dentry->d_sb == a->sb)) {
16876 +               err = -EINVAL;
16877 +               errorfc(fc, "%s must be outside", xino->path);
16878 +       }
16879 +
16880 +out:
16881 +       AuTraceErr(err);
16882 +       return err;
16883 +}
16884 +
16885 +static
16886 +int au_fsctx_parse_xino_itrunc_path(struct fs_context *fc,
16887 +                                   struct au_opt_xino_itrunc *xino_itrunc,
16888 +                                   char *pathname)
16889 +{
16890 +       int err;
16891 +       aufs_bindex_t bbot, bindex;
16892 +       struct path path;
16893 +       struct dentry *root;
16894 +       struct au_fsctx_opts *a = fc->fs_private;
16895 +
16896 +       AuDebugOn(!a->sb);
16897 +
16898 +       err = vfsub_kern_path(pathname, AuOpt_LkupDirFlags, &path);
16899 +       if (unlikely(err)) {
16900 +               errorfc(fc, "lookup failed %s (%d)", pathname, err);
16901 +               goto out;
16902 +       }
16903 +
16904 +       xino_itrunc->bindex = -1;
16905 +       root = a->sb->s_root;
16906 +       aufs_read_lock(root, AuLock_FLUSH);
16907 +       bbot = au_sbbot(a->sb);
16908 +       for (bindex = 0; bindex <= bbot; bindex++) {
16909 +               if (au_h_dptr(root, bindex) == path.dentry) {
16910 +                       xino_itrunc->bindex = bindex;
16911 +                       break;
16912 +               }
16913 +       }
16914 +       aufs_read_unlock(root, !AuLock_IR);
16915 +       path_put(&path);
16916 +
16917 +       if (unlikely(xino_itrunc->bindex < 0)) {
16918 +               err = -EINVAL;
16919 +               errorfc(fc, "no such branch %s", pathname);
16920 +       }
16921 +
16922 +out:
16923 +       AuTraceErr(err);
16924 +       return err;
16925 +}
16926 +
16927 +static int au_fsctx_parse_xino_itrunc(struct fs_context *fc,
16928 +                                     struct au_opt_xino_itrunc *xino_itrunc,
16929 +                                     unsigned int bindex)
16930 +{
16931 +       int err;
16932 +       aufs_bindex_t bbot;
16933 +       struct super_block *sb;
16934 +       struct au_fsctx_opts *a = fc->fs_private;
16935 +
16936 +       sb = a->sb;
16937 +       AuDebugOn(!sb);
16938 +
16939 +       err = 0;
16940 +       si_noflush_read_lock(sb);
16941 +       bbot = au_sbbot(sb);
16942 +       si_read_unlock(sb);
16943 +       if (bindex <= bbot)
16944 +               xino_itrunc->bindex = bindex;
16945 +       else {
16946 +               err = -EINVAL;
16947 +               errorfc(fc, "out of bounds, %u", bindex);
16948 +       }
16949 +
16950 +       AuTraceErr(err);
16951 +       return err;
16952 +}
16953 +
16954 +static int au_fsctx_parse_param(struct fs_context *fc, struct fs_parameter *param)
16955 +{
16956 +       int err, token;
16957 +       struct fs_parse_result result;
16958 +       struct au_fsctx_opts *a = fc->fs_private;
16959 +       struct au_opt *opt = a->opt;
16960 +
16961 +       AuDbg("fc %p, param {key %s, string %s}\n",
16962 +             fc, param->key, param->string);
16963 +       err = fs_parse(fc, aufs_fsctx_paramspec, param, &result);
16964 +       if (unlikely(err < 0))
16965 +               goto out;
16966 +       token = err;
16967 +       AuDbg("token %d, res{negated %d, uint64 %llu}\n",
16968 +             token, result.negated, result.uint_64);
16969 +
16970 +       err = -EINVAL;
16971 +       a->skipped = 0;
16972 +       switch (token) {
16973 +       case Opt_br:
16974 +               err = au_fsctx_parse_br(fc, param->string);
16975 +               break;
16976 +       case Opt_add:
16977 +               err = au_fsctx_parse_add(fc, param->string);
16978 +               break;
16979 +       case Opt_append:
16980 +               err = au_fsctx_parse_do_add(fc, opt, param->string, param->size,
16981 +                                           /*dummy bindex*/1);
16982 +               break;
16983 +       case Opt_prepend:
16984 +               err = au_fsctx_parse_do_add(fc, opt, param->string, param->size,
16985 +                                           /*bindex*/0);
16986 +               break;
16987 +
16988 +       case Opt_del:
16989 +               err = au_fsctx_parse_del(fc, &opt->del, param);
16990 +               break;
16991 +#if 0 /* reserved for future use */
16992 +       case Opt_idel:
16993 +               if (!a->sb) {
16994 +                       err = 0;
16995 +                       a->skipped = 1;
16996 +                       break;
16997 +               }
16998 +               del->pathname = "(indexed)";
16999 +               err = au_opts_parse_idel(fc, &opt->del, result.uint_32);
17000 +               break;
17001 +#endif
17002 +
17003 +       case Opt_mod:
17004 +               err = au_fsctx_parse_mod(fc, &opt->mod, param);
17005 +               break;
17006 +#ifdef IMOD /* reserved for future use */
17007 +       case Opt_imod:
17008 +               if (!a->sb) {
17009 +                       err = 0;
17010 +                       a->skipped = 1;
17011 +                       break;
17012 +               }
17013 +               u.mod->path = "(indexed)";
17014 +               err = au_opts_parse_imod(fc, &opt->mod, param->string);
17015 +               break;
17016 +#endif
17017 +
17018 +       case Opt_xino:
17019 +               err = au_fsctx_parse_xino(fc, &opt->xino, param);
17020 +               break;
17021 +       case Opt_trunc_xino_path:
17022 +               if (!a->sb) {
17023 +                       errorfc(fc, "no such branch %s", param->string);
17024 +                       break;
17025 +               }
17026 +               err = au_fsctx_parse_xino_itrunc_path(fc, &opt->xino_itrunc,
17027 +                                                     param->string);
17028 +               break;
17029 +#if 0
17030 +       case Opt_trunc_xino_v:
17031 +               if (!a->sb) {
17032 +                       err = 0;
17033 +                       a->skipped = 1;
17034 +                       break;
17035 +               }
17036 +               err = au_fsctx_parse_xino_itrunc_path(fc, &opt->xino_itrunc,
17037 +                                                     param->string);
17038 +               break;
17039 +#endif
17040 +       case Opt_itrunc_xino:
17041 +               if (!a->sb) {
17042 +                       errorfc(fc, "out of bounds %s", param->string);
17043 +                       break;
17044 +               }
17045 +               err = au_fsctx_parse_xino_itrunc(fc, &opt->xino_itrunc,
17046 +                                                result.int_32);
17047 +               break;
17048 +
17049 +       case Opt_dirwh:
17050 +               err = 0;
17051 +               opt->dirwh = result.int_32;
17052 +               break;
17053 +
17054 +       case Opt_rdcache:
17055 +               if (unlikely(result.int_32 > AUFS_RDCACHE_MAX)) {
17056 +                       errorfc(fc, "rdcache must be smaller than %d",
17057 +                               AUFS_RDCACHE_MAX);
17058 +                       break;
17059 +               }
17060 +               err = 0;
17061 +               opt->rdcache = result.int_32;
17062 +               break;
17063 +
17064 +       case Opt_rdblk:
17065 +               err = 0;
17066 +               opt->rdblk = AUFS_RDBLK_DEF;
17067 +               if (!strcmp(param->string, "def"))
17068 +                       break;
17069 +
17070 +               err = kstrtoint(param->string, 0, &result.int_32);
17071 +               if (unlikely(err)) {
17072 +                       errorfc(fc, "bad value in %s", param->key);
17073 +                       break;
17074 +               }
17075 +               err = -EINVAL;
17076 +               if (unlikely(result.int_32 < 0
17077 +                            || result.int_32 > KMALLOC_MAX_SIZE)) {
17078 +                       errorfc(fc, "bad value in %s", param->key);
17079 +                       break;
17080 +               }
17081 +               if (unlikely(result.int_32 && result.int_32 < NAME_MAX)) {
17082 +                       errorfc(fc, "rdblk must be larger than %d", NAME_MAX);
17083 +                       break;
17084 +               }
17085 +               err = 0;
17086 +               opt->rdblk = result.int_32;
17087 +               break;
17088 +
17089 +       case Opt_rdhash:
17090 +               err = 0;
17091 +               opt->rdhash = AUFS_RDHASH_DEF;
17092 +               if (!strcmp(param->string, "def"))
17093 +                       break;
17094 +
17095 +               err = kstrtoint(param->string, 0, &result.int_32);
17096 +               if (unlikely(err)) {
17097 +                       errorfc(fc, "bad value in %s", param->key);
17098 +                       break;
17099 +               }
17100 +               /* how about zero? */
17101 +               if (result.int_32 < 0
17102 +                   || result.int_32 * sizeof(struct hlist_head)
17103 +                   > KMALLOC_MAX_SIZE) {
17104 +                       err = -EINVAL;
17105 +                       errorfc(fc, "bad integer in %s", param->key);
17106 +                       break;
17107 +               }
17108 +               opt->rdhash = result.int_32;
17109 +               break;
17110 +
17111 +       case Opt_diropq:
17112 +               /*
17113 +                * As other options, fs/aufs/opts.c can handle these strings by
17114 +                * match_token().  But "diropq=" is deprecated now and will
17115 +                * never have other value.  So simple strcmp() is enough here.
17116 +                */
17117 +               if (!strcmp(param->string, "a") ||
17118 +                   !strcmp(param->string, "always")) {
17119 +                       err = 0;
17120 +                       opt->type = Opt_diropq_a;
17121 +               } else if (!strcmp(param->string, "w") ||
17122 +                          !strcmp(param->string, "whiteouted")) {
17123 +                       err = 0;
17124 +                       opt->type = Opt_diropq_w;
17125 +               } else
17126 +                       errorfc(fc, "unknown value %s", param->string);
17127 +               break;
17128 +
17129 +       case Opt_udba:
17130 +               opt->udba = au_udba_val(param->string);
17131 +               if (opt->udba >= 0)
17132 +                       err = 0;
17133 +               else
17134 +                       errorf(fc, "wrong value, %s", param->string);
17135 +               break;
17136 +
17137 +       case Opt_wbr_create:
17138 +               opt->wbr_create.wbr_create
17139 +                       = au_wbr_create_val(param->string, &opt->wbr_create);
17140 +               if (opt->wbr_create.wbr_create >= 0)
17141 +                       err = 0;
17142 +               else
17143 +                       errorf(fc, "wrong value, %s", param->key);
17144 +               break;
17145 +
17146 +       case Opt_wbr_copyup:
17147 +               opt->wbr_copyup = au_wbr_copyup_val(param->string);
17148 +               if (opt->wbr_copyup >= 0)
17149 +                       err = 0;
17150 +               else
17151 +                       errorfc(fc, "wrong value, %s", param->key);
17152 +               break;
17153 +
17154 +       case Opt_fhsm_sec:
17155 +               if (unlikely(result.int_32 < 0)) {
17156 +                       errorfc(fc, "bad integer in %s\n", param->key);
17157 +                       break;
17158 +               }
17159 +               err = 0;
17160 +               if (sysaufs_brs)
17161 +                       opt->fhsm_second = result.int_32;
17162 +               else
17163 +                       warnfc(fc, "ignored %s %s", param->key, param->string);
17164 +               break;
17165 +
17166 +       /* simple true/false flag */
17167 +#define au_fsctx_TF(name)                              \
17168 +               case Opt_##name:                        \
17169 +                       err = 0;                        \
17170 +                       opt->tf = !result.negated;      \
17171 +                       break;
17172 +       au_fsctx_TF(trunc_xino);
17173 +       au_fsctx_TF(trunc_xib);
17174 +       au_fsctx_TF(dirperm1);
17175 +       au_fsctx_TF(plink);
17176 +       au_fsctx_TF(shwh);
17177 +       au_fsctx_TF(dio);
17178 +       au_fsctx_TF(warn_perm);
17179 +       au_fsctx_TF(verbose);
17180 +       au_fsctx_TF(sum);
17181 +       au_fsctx_TF(dirren);
17182 +       au_fsctx_TF(acl);
17183 +#undef au_fsctx_TF
17184 +
17185 +       case Opt_noverbose:
17186 +               err = 0;
17187 +               opt->type = Opt_verbose;
17188 +               opt->tf = false;
17189 +               break;
17190 +
17191 +       case Opt_noxino:
17192 +               fallthrough;
17193 +       case Opt_list_plink:
17194 +               fallthrough;
17195 +       case Opt_wsum:
17196 +               err = 0;
17197 +               break;
17198 +
17199 +       case Opt_ignore:
17200 +               warnfc(fc, "ignored %s", param->key);
17201 +               fallthrough;
17202 +       case Opt_ignore_silent:
17203 +               a->skipped = 1;
17204 +               err = 0;
17205 +               break;
17206 +       default:
17207 +               a->skipped = 1;
17208 +               err = -ENOPARAM;
17209 +               break;
17210 +       }
17211 +       if (unlikely(err))
17212 +               goto out;
17213 +       if (a->skipped)
17214 +               goto out;
17215 +
17216 +       switch (token) {
17217 +       case Opt_br:
17218 +               fallthrough;
17219 +       case Opt_noverbose:
17220 +               fallthrough;
17221 +       case Opt_diropq:
17222 +               break;
17223 +       default:
17224 +               opt->type = token;
17225 +               break;
17226 +       }
17227 +       opt++;
17228 +       if (unlikely(opt > a->opt_tail)) {
17229 +               err = -E2BIG;
17230 +               opt--;
17231 +       }
17232 +       opt->type = Opt_tail;
17233 +       a->opt = opt;
17234 +
17235 +out:
17236 +       return err;
17237 +}
17238 +
17239 +/*
17240 + * these options accept both 'name=val' and 'name:val' form.
17241 + * some accept optional '=' in its value.
17242 + * eg. br:/br1=rw:/br2=ro and br=/br1=rw:/br2=ro
17243 + */
17244 +static inline unsigned int is_colonopt(char *str)
17245 +{
17246 +#define do_test(name)                                  \
17247 +       if (!strncmp(str, name ":", sizeof(name)))      \
17248 +               return sizeof(name) - 1;
17249 +       do_test("br");
17250 +       do_test("add");
17251 +       do_test("ins");
17252 +       do_test("append");
17253 +       do_test("prepend");
17254 +       do_test("del");
17255 +       /* do_test("idel"); */
17256 +       do_test("mod");
17257 +       /* do_test("imod"); */
17258 +#undef do_test
17259 +
17260 +       return 0;
17261 +}
17262 +
17263 +static int au_fsctx_parse_monolithic(struct fs_context *fc, void *data)
17264 +{
17265 +       int err;
17266 +       unsigned int u;
17267 +       char *str;
17268 +       struct au_fsctx_opts *a = fc->fs_private;
17269 +
17270 +       str = data;
17271 +       AuDbg("str %s\n", str);
17272 +       while (str) {
17273 +               u = is_colonopt(str);
17274 +               if (u)
17275 +                       str[u] = '=';
17276 +               str = strchr(str, ',');
17277 +               if (!str)
17278 +                       break;
17279 +               str++;
17280 +       }
17281 +       str = data;
17282 +       AuDbg("str %s\n", str);
17283 +
17284 +       err = generic_parse_monolithic(fc, str);
17285 +       AuTraceErr(err);
17286 +       au_fsctx_dump(&a->opts);
17287 +
17288 +       return err;
17289 +}
17290 +
17291 +/* ---------------------------------------------------------------------- */
17292 +
17293 +static void au_fsctx_opts_free(struct au_opts *opts)
17294 +{
17295 +       struct au_opt *opt;
17296 +
17297 +       opt = opts->opt;
17298 +       while (opt->type != Opt_tail) {
17299 +               switch (opt->type) {
17300 +               case Opt_add:
17301 +                       fallthrough;
17302 +               case Opt_append:
17303 +                       fallthrough;
17304 +               case Opt_prepend:
17305 +                       kfree(opt->add.pathname);
17306 +                       path_put(&opt->add.path);
17307 +                       break;
17308 +               case Opt_del:
17309 +                       kfree(opt->del.pathname);
17310 +                       fallthrough;
17311 +               case Opt_idel:
17312 +                       path_put(&opt->del.h_path);
17313 +                       break;
17314 +               case Opt_mod:
17315 +                       kfree(opt->mod.path);
17316 +                       fallthrough;
17317 +               case Opt_imod:
17318 +                       dput(opt->mod.h_root);
17319 +                       break;
17320 +               case Opt_xino:
17321 +                       kfree(opt->xino.path);
17322 +                       fput(opt->xino.file);
17323 +                       break;
17324 +               }
17325 +               opt++;
17326 +       }
17327 +}
17328 +
17329 +static void au_fsctx_free(struct fs_context *fc)
17330 +{
17331 +       struct au_fsctx_opts *a = fc->fs_private;
17332 +
17333 +       /* fs_type=%p, root=%pD */
17334 +       AuDbg("fc %p{sb_flags 0x%x, sb_flags_mask 0x%x, purpose %u\n",
17335 +             fc, fc->sb_flags, fc->sb_flags_mask, fc->purpose);
17336 +
17337 +       kobject_put(&a->sbinfo->si_kobj);
17338 +       au_fsctx_opts_free(&a->opts);
17339 +       free_page((unsigned long)a->opts.opt);
17340 +       au_kfree_rcu(a);
17341 +}
17342 +
17343 +static const struct fs_context_operations au_fsctx_ops = {
17344 +       .free                   = au_fsctx_free,
17345 +       .parse_param            = au_fsctx_parse_param,
17346 +       .parse_monolithic       = au_fsctx_parse_monolithic,
17347 +       .get_tree               = au_fsctx_get_tree,
17348 +       .reconfigure            = au_fsctx_reconfigure
17349 +       /*
17350 +        * nfs4 requires ->dup()? No.
17351 +        * I don't know what is this ->dup() for.
17352 +        */
17353 +};
17354 +
17355 +int aufs_fsctx_init(struct fs_context *fc)
17356 +{
17357 +       int err;
17358 +       struct au_fsctx_opts *a;
17359 +
17360 +       /* fs_type=%p, root=%pD */
17361 +       AuDbg("fc %p{sb_flags 0x%x, sb_flags_mask 0x%x, purpose %u\n",
17362 +             fc, fc->sb_flags, fc->sb_flags_mask, fc->purpose);
17363 +
17364 +       /* they will be freed by au_fsctx_free() */
17365 +       err = -ENOMEM;
17366 +       a = kzalloc(sizeof(*a), GFP_NOFS);
17367 +       if (unlikely(!a))
17368 +               goto out;
17369 +       a->bindex = 0;
17370 +       a->opts.opt = (void *)__get_free_page(GFP_NOFS);
17371 +       if (unlikely(!a->opts.opt))
17372 +               goto out_a;
17373 +       a->opt = a->opts.opt;
17374 +       a->opt->type = Opt_tail;
17375 +       a->opts.max_opt = PAGE_SIZE / sizeof(*a->opts.opt);
17376 +       a->opt_tail = a->opt + a->opts.max_opt - 1;
17377 +       a->opts.sb_flags = fc->sb_flags;
17378 +
17379 +       a->sb = NULL;
17380 +       if (fc->root) {
17381 +               AuDebugOn(fc->purpose != FS_CONTEXT_FOR_RECONFIGURE);
17382 +               a->opts.flags = AuOpts_REMOUNT;
17383 +               a->sb = fc->root->d_sb;
17384 +               a->sbinfo = au_sbi(a->sb);
17385 +               kobject_get(&a->sbinfo->si_kobj);
17386 +       } else {
17387 +               a->sbinfo = au_si_alloc(a->sb);
17388 +               AuDebugOn(!a->sbinfo);
17389 +               err = PTR_ERR(a->sbinfo);
17390 +               if (IS_ERR(a->sbinfo))
17391 +                       goto out_opt;
17392 +               au_rw_write_unlock(&a->sbinfo->si_rwsem);
17393 +       }
17394 +
17395 +       err = 0;
17396 +       fc->fs_private = a;
17397 +       fc->ops = &au_fsctx_ops;
17398 +       goto out; /* success */
17399 +
17400 +out_opt:
17401 +       free_page((unsigned long)a->opts.opt);
17402 +out_a:
17403 +       au_kfree_rcu(a);
17404 +out:
17405 +       AuTraceErr(err);
17406 +       return err;
17407 +}
17408 diff -urN /usr/share/empty/fs/aufs/fstype.h linux/fs/aufs/fstype.h
17409 --- /usr/share/empty/fs/aufs/fstype.h   1970-01-01 01:00:00.000000000 +0100
17410 +++ linux/fs/aufs/fstype.h      2022-03-21 14:49:05.723299677 +0100
17411 @@ -0,0 +1,401 @@
17412 +/* SPDX-License-Identifier: GPL-2.0 */
17413 +/*
17414 + * Copyright (C) 2005-2021 Junjiro R. Okajima
17415 + *
17416 + * This program, aufs is free software; you can redistribute it and/or modify
17417 + * it under the terms of the GNU General Public License as published by
17418 + * the Free Software Foundation; either version 2 of the License, or
17419 + * (at your option) any later version.
17420 + *
17421 + * This program is distributed in the hope that it will be useful,
17422 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
17423 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17424 + * GNU General Public License for more details.
17425 + *
17426 + * You should have received a copy of the GNU General Public License
17427 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17428 + */
17429 +
17430 +/*
17431 + * judging filesystem type
17432 + */
17433 +
17434 +#ifndef __AUFS_FSTYPE_H__
17435 +#define __AUFS_FSTYPE_H__
17436 +
17437 +#ifdef __KERNEL__
17438 +
17439 +#include <linux/fs.h>
17440 +#include <linux/magic.h>
17441 +#include <linux/nfs_fs.h>
17442 +#include <linux/romfs_fs.h>
17443 +
17444 +static inline int au_test_aufs(struct super_block *sb)
17445 +{
17446 +       return sb->s_magic == AUFS_SUPER_MAGIC;
17447 +}
17448 +
17449 +static inline const char *au_sbtype(struct super_block *sb)
17450 +{
17451 +       return sb->s_type->name;
17452 +}
17453 +
17454 +static inline int au_test_iso9660(struct super_block *sb __maybe_unused)
17455 +{
17456 +#if IS_ENABLED(CONFIG_ISO9660_FS)
17457 +       return sb->s_magic == ISOFS_SUPER_MAGIC;
17458 +#else
17459 +       return 0;
17460 +#endif
17461 +}
17462 +
17463 +static inline int au_test_romfs(struct super_block *sb __maybe_unused)
17464 +{
17465 +#if IS_ENABLED(CONFIG_ROMFS_FS)
17466 +       return sb->s_magic == ROMFS_MAGIC;
17467 +#else
17468 +       return 0;
17469 +#endif
17470 +}
17471 +
17472 +static inline int au_test_cramfs(struct super_block *sb __maybe_unused)
17473 +{
17474 +#if IS_ENABLED(CONFIG_CRAMFS)
17475 +       return sb->s_magic == CRAMFS_MAGIC;
17476 +#endif
17477 +       return 0;
17478 +}
17479 +
17480 +static inline int au_test_nfs(struct super_block *sb __maybe_unused)
17481 +{
17482 +#if IS_ENABLED(CONFIG_NFS_FS)
17483 +       return sb->s_magic == NFS_SUPER_MAGIC;
17484 +#else
17485 +       return 0;
17486 +#endif
17487 +}
17488 +
17489 +static inline int au_test_fuse(struct super_block *sb __maybe_unused)
17490 +{
17491 +#if IS_ENABLED(CONFIG_FUSE_FS)
17492 +       return sb->s_magic == FUSE_SUPER_MAGIC;
17493 +#else
17494 +       return 0;
17495 +#endif
17496 +}
17497 +
17498 +static inline int au_test_xfs(struct super_block *sb __maybe_unused)
17499 +{
17500 +#if IS_ENABLED(CONFIG_XFS_FS)
17501 +       return sb->s_magic == XFS_SB_MAGIC;
17502 +#else
17503 +       return 0;
17504 +#endif
17505 +}
17506 +
17507 +static inline int au_test_tmpfs(struct super_block *sb __maybe_unused)
17508 +{
17509 +#ifdef CONFIG_TMPFS
17510 +       return sb->s_magic == TMPFS_MAGIC;
17511 +#else
17512 +       return 0;
17513 +#endif
17514 +}
17515 +
17516 +static inline int au_test_ecryptfs(struct super_block *sb __maybe_unused)
17517 +{
17518 +#if IS_ENABLED(CONFIG_ECRYPT_FS)
17519 +       return !strcmp(au_sbtype(sb), "ecryptfs");
17520 +#else
17521 +       return 0;
17522 +#endif
17523 +}
17524 +
17525 +static inline int au_test_ramfs(struct super_block *sb)
17526 +{
17527 +       return sb->s_magic == RAMFS_MAGIC;
17528 +}
17529 +
17530 +static inline int au_test_ubifs(struct super_block *sb __maybe_unused)
17531 +{
17532 +#if IS_ENABLED(CONFIG_UBIFS_FS)
17533 +       return sb->s_magic == UBIFS_SUPER_MAGIC;
17534 +#else
17535 +       return 0;
17536 +#endif
17537 +}
17538 +
17539 +static inline int au_test_procfs(struct super_block *sb __maybe_unused)
17540 +{
17541 +#ifdef CONFIG_PROC_FS
17542 +       return sb->s_magic == PROC_SUPER_MAGIC;
17543 +#else
17544 +       return 0;
17545 +#endif
17546 +}
17547 +
17548 +static inline int au_test_sysfs(struct super_block *sb __maybe_unused)
17549 +{
17550 +#ifdef CONFIG_SYSFS
17551 +       return sb->s_magic == SYSFS_MAGIC;
17552 +#else
17553 +       return 0;
17554 +#endif
17555 +}
17556 +
17557 +static inline int au_test_configfs(struct super_block *sb __maybe_unused)
17558 +{
17559 +#if IS_ENABLED(CONFIG_CONFIGFS_FS)
17560 +       return sb->s_magic == CONFIGFS_MAGIC;
17561 +#else
17562 +       return 0;
17563 +#endif
17564 +}
17565 +
17566 +static inline int au_test_minix(struct super_block *sb __maybe_unused)
17567 +{
17568 +#if IS_ENABLED(CONFIG_MINIX_FS)
17569 +       return sb->s_magic == MINIX3_SUPER_MAGIC
17570 +               || sb->s_magic == MINIX2_SUPER_MAGIC
17571 +               || sb->s_magic == MINIX2_SUPER_MAGIC2
17572 +               || sb->s_magic == MINIX_SUPER_MAGIC
17573 +               || sb->s_magic == MINIX_SUPER_MAGIC2;
17574 +#else
17575 +       return 0;
17576 +#endif
17577 +}
17578 +
17579 +static inline int au_test_fat(struct super_block *sb __maybe_unused)
17580 +{
17581 +#if IS_ENABLED(CONFIG_FAT_FS)
17582 +       return sb->s_magic == MSDOS_SUPER_MAGIC;
17583 +#else
17584 +       return 0;
17585 +#endif
17586 +}
17587 +
17588 +static inline int au_test_msdos(struct super_block *sb)
17589 +{
17590 +       return au_test_fat(sb);
17591 +}
17592 +
17593 +static inline int au_test_vfat(struct super_block *sb)
17594 +{
17595 +       return au_test_fat(sb);
17596 +}
17597 +
17598 +static inline int au_test_securityfs(struct super_block *sb __maybe_unused)
17599 +{
17600 +#ifdef CONFIG_SECURITYFS
17601 +       return sb->s_magic == SECURITYFS_MAGIC;
17602 +#else
17603 +       return 0;
17604 +#endif
17605 +}
17606 +
17607 +static inline int au_test_squashfs(struct super_block *sb __maybe_unused)
17608 +{
17609 +#if IS_ENABLED(CONFIG_SQUASHFS)
17610 +       return sb->s_magic == SQUASHFS_MAGIC;
17611 +#else
17612 +       return 0;
17613 +#endif
17614 +}
17615 +
17616 +static inline int au_test_btrfs(struct super_block *sb __maybe_unused)
17617 +{
17618 +#if IS_ENABLED(CONFIG_BTRFS_FS)
17619 +       return sb->s_magic == BTRFS_SUPER_MAGIC;
17620 +#else
17621 +       return 0;
17622 +#endif
17623 +}
17624 +
17625 +static inline int au_test_xenfs(struct super_block *sb __maybe_unused)
17626 +{
17627 +#if IS_ENABLED(CONFIG_XENFS)
17628 +       return sb->s_magic == XENFS_SUPER_MAGIC;
17629 +#else
17630 +       return 0;
17631 +#endif
17632 +}
17633 +
17634 +static inline int au_test_debugfs(struct super_block *sb __maybe_unused)
17635 +{
17636 +#ifdef CONFIG_DEBUG_FS
17637 +       return sb->s_magic == DEBUGFS_MAGIC;
17638 +#else
17639 +       return 0;
17640 +#endif
17641 +}
17642 +
17643 +static inline int au_test_nilfs(struct super_block *sb __maybe_unused)
17644 +{
17645 +#if IS_ENABLED(CONFIG_NILFS)
17646 +       return sb->s_magic == NILFS_SUPER_MAGIC;
17647 +#else
17648 +       return 0;
17649 +#endif
17650 +}
17651 +
17652 +static inline int au_test_hfsplus(struct super_block *sb __maybe_unused)
17653 +{
17654 +#if IS_ENABLED(CONFIG_HFSPLUS_FS)
17655 +       return sb->s_magic == HFSPLUS_SUPER_MAGIC;
17656 +#else
17657 +       return 0;
17658 +#endif
17659 +}
17660 +
17661 +/* ---------------------------------------------------------------------- */
17662 +/*
17663 + * they can't be an aufs branch.
17664 + */
17665 +static inline int au_test_fs_unsuppoted(struct super_block *sb)
17666 +{
17667 +       return
17668 +#ifndef CONFIG_AUFS_BR_RAMFS
17669 +               au_test_ramfs(sb) ||
17670 +#endif
17671 +               au_test_procfs(sb)
17672 +               || au_test_sysfs(sb)
17673 +               || au_test_configfs(sb)
17674 +               || au_test_debugfs(sb)
17675 +               || au_test_securityfs(sb)
17676 +               || au_test_xenfs(sb)
17677 +               || au_test_ecryptfs(sb)
17678 +               /* || !strcmp(au_sbtype(sb), "unionfs") */
17679 +               || au_test_aufs(sb); /* will be supported in next version */
17680 +}
17681 +
17682 +static inline int au_test_fs_remote(struct super_block *sb)
17683 +{
17684 +       return !au_test_tmpfs(sb)
17685 +#ifdef CONFIG_AUFS_BR_RAMFS
17686 +               && !au_test_ramfs(sb)
17687 +#endif
17688 +               && !(sb->s_type->fs_flags & FS_REQUIRES_DEV);
17689 +}
17690 +
17691 +/* ---------------------------------------------------------------------- */
17692 +
17693 +/*
17694 + * Note: these functions (below) are created after reading ->getattr() in all
17695 + * filesystems under linux/fs. it means we have to do so in every update...
17696 + */
17697 +
17698 +/*
17699 + * some filesystems require getattr to refresh the inode attributes before
17700 + * referencing.
17701 + * in most cases, we can rely on the inode attribute in NFS (or every remote fs)
17702 + * and leave the work for d_revalidate()
17703 + */
17704 +static inline int au_test_fs_refresh_iattr(struct super_block *sb)
17705 +{
17706 +       return au_test_nfs(sb)
17707 +               || au_test_fuse(sb)
17708 +               /* || au_test_btrfs(sb) */      /* untested */
17709 +               ;
17710 +}
17711 +
17712 +/*
17713 + * filesystems which don't maintain i_size or i_blocks.
17714 + */
17715 +static inline int au_test_fs_bad_iattr_size(struct super_block *sb)
17716 +{
17717 +       return au_test_xfs(sb)
17718 +               || au_test_btrfs(sb)
17719 +               || au_test_ubifs(sb)
17720 +               || au_test_hfsplus(sb)  /* maintained, but incorrect */
17721 +               /* || au_test_minix(sb) */      /* untested */
17722 +               ;
17723 +}
17724 +
17725 +/*
17726 + * filesystems which don't store the correct value in some of their inode
17727 + * attributes.
17728 + */
17729 +static inline int au_test_fs_bad_iattr(struct super_block *sb)
17730 +{
17731 +       return au_test_fs_bad_iattr_size(sb)
17732 +               || au_test_fat(sb)
17733 +               || au_test_msdos(sb)
17734 +               || au_test_vfat(sb);
17735 +}
17736 +
17737 +/* they don't check i_nlink in link(2) */
17738 +static inline int au_test_fs_no_limit_nlink(struct super_block *sb)
17739 +{
17740 +       return au_test_tmpfs(sb)
17741 +#ifdef CONFIG_AUFS_BR_RAMFS
17742 +               || au_test_ramfs(sb)
17743 +#endif
17744 +               || au_test_ubifs(sb)
17745 +               || au_test_hfsplus(sb);
17746 +}
17747 +
17748 +/*
17749 + * filesystems which sets S_NOATIME and S_NOCMTIME.
17750 + */
17751 +static inline int au_test_fs_notime(struct super_block *sb)
17752 +{
17753 +       return au_test_nfs(sb)
17754 +               || au_test_fuse(sb)
17755 +               || au_test_ubifs(sb)
17756 +               ;
17757 +}
17758 +
17759 +/* temporary support for i#1 in cramfs */
17760 +static inline int au_test_fs_unique_ino(struct inode *inode)
17761 +{
17762 +       if (au_test_cramfs(inode->i_sb))
17763 +               return inode->i_ino != 1;
17764 +       return 1;
17765 +}
17766 +
17767 +/* ---------------------------------------------------------------------- */
17768 +
17769 +/*
17770 + * the filesystem where the xino files placed must support i/o after unlink and
17771 + * maintain i_size and i_blocks.
17772 + */
17773 +static inline int au_test_fs_bad_xino(struct super_block *sb)
17774 +{
17775 +       return au_test_fs_remote(sb)
17776 +               || au_test_fs_bad_iattr_size(sb)
17777 +               /* don't want unnecessary work for xino */
17778 +               || au_test_aufs(sb)
17779 +               || au_test_ecryptfs(sb)
17780 +               || au_test_nilfs(sb);
17781 +}
17782 +
17783 +static inline int au_test_fs_trunc_xino(struct super_block *sb)
17784 +{
17785 +       return au_test_tmpfs(sb)
17786 +               || au_test_ramfs(sb);
17787 +}
17788 +
17789 +/*
17790 + * test if the @sb is real-readonly.
17791 + */
17792 +static inline int au_test_fs_rr(struct super_block *sb)
17793 +{
17794 +       return au_test_squashfs(sb)
17795 +               || au_test_iso9660(sb)
17796 +               || au_test_cramfs(sb)
17797 +               || au_test_romfs(sb);
17798 +}
17799 +
17800 +/*
17801 + * test if the @inode is nfs with 'noacl' option
17802 + * NFS always sets SB_POSIXACL regardless its mount option 'noacl.'
17803 + */
17804 +static inline int au_test_nfs_noacl(struct inode *inode)
17805 +{
17806 +       return au_test_nfs(inode->i_sb)
17807 +               /* && IS_POSIXACL(inode) */
17808 +               && !nfs_server_capable(inode, NFS_CAP_ACLS);
17809 +}
17810 +
17811 +#endif /* __KERNEL__ */
17812 +#endif /* __AUFS_FSTYPE_H__ */
17813 diff -urN /usr/share/empty/fs/aufs/hbl.h linux/fs/aufs/hbl.h
17814 --- /usr/share/empty/fs/aufs/hbl.h      1970-01-01 01:00:00.000000000 +0100
17815 +++ linux/fs/aufs/hbl.h 2022-03-21 14:49:05.723299677 +0100
17816 @@ -0,0 +1,65 @@
17817 +/* SPDX-License-Identifier: GPL-2.0 */
17818 +/*
17819 + * Copyright (C) 2017-2021 Junjiro R. Okajima
17820 + *
17821 + * This program, aufs is free software; you can redistribute it and/or modify
17822 + * it under the terms of the GNU General Public License as published by
17823 + * the Free Software Foundation; either version 2 of the License, or
17824 + * (at your option) any later version.
17825 + *
17826 + * This program is distributed in the hope that it will be useful,
17827 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
17828 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17829 + * GNU General Public License for more details.
17830 + *
17831 + * You should have received a copy of the GNU General Public License
17832 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17833 + */
17834 +
17835 +/*
17836 + * helpers for hlist_bl.h
17837 + */
17838 +
17839 +#ifndef __AUFS_HBL_H__
17840 +#define __AUFS_HBL_H__
17841 +
17842 +#ifdef __KERNEL__
17843 +
17844 +#include <linux/list_bl.h>
17845 +
17846 +static inline void au_hbl_add(struct hlist_bl_node *node,
17847 +                             struct hlist_bl_head *hbl)
17848 +{
17849 +       hlist_bl_lock(hbl);
17850 +       hlist_bl_add_head(node, hbl);
17851 +       hlist_bl_unlock(hbl);
17852 +}
17853 +
17854 +static inline void au_hbl_del(struct hlist_bl_node *node,
17855 +                             struct hlist_bl_head *hbl)
17856 +{
17857 +       hlist_bl_lock(hbl);
17858 +       hlist_bl_del(node);
17859 +       hlist_bl_unlock(hbl);
17860 +}
17861 +
17862 +#define au_hbl_for_each(pos, head)                                     \
17863 +       for (pos = hlist_bl_first(head);                                \
17864 +            pos;                                                       \
17865 +            pos = pos->next)
17866 +
17867 +static inline unsigned long au_hbl_count(struct hlist_bl_head *hbl)
17868 +{
17869 +       unsigned long cnt;
17870 +       struct hlist_bl_node *pos;
17871 +
17872 +       cnt = 0;
17873 +       hlist_bl_lock(hbl);
17874 +       au_hbl_for_each(pos, hbl)
17875 +               cnt++;
17876 +       hlist_bl_unlock(hbl);
17877 +       return cnt;
17878 +}
17879 +
17880 +#endif /* __KERNEL__ */
17881 +#endif /* __AUFS_HBL_H__ */
17882 diff -urN /usr/share/empty/fs/aufs/hfsnotify.c linux/fs/aufs/hfsnotify.c
17883 --- /usr/share/empty/fs/aufs/hfsnotify.c        1970-01-01 01:00:00.000000000 +0100
17884 +++ linux/fs/aufs/hfsnotify.c   2022-03-21 14:49:05.723299677 +0100
17885 @@ -0,0 +1,289 @@
17886 +// SPDX-License-Identifier: GPL-2.0
17887 +/*
17888 + * Copyright (C) 2005-2021 Junjiro R. Okajima
17889 + *
17890 + * This program, aufs is free software; you can redistribute it and/or modify
17891 + * it under the terms of the GNU General Public License as published by
17892 + * the Free Software Foundation; either version 2 of the License, or
17893 + * (at your option) any later version.
17894 + *
17895 + * This program is distributed in the hope that it will be useful,
17896 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
17897 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17898 + * GNU General Public License for more details.
17899 + *
17900 + * You should have received a copy of the GNU General Public License
17901 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17902 + */
17903 +
17904 +/*
17905 + * fsnotify for the lower directories
17906 + */
17907 +
17908 +#include "aufs.h"
17909 +
17910 +/* FS_IN_IGNORED is unnecessary */
17911 +static const __u32 AuHfsnMask = (FS_MOVED_TO | FS_MOVED_FROM | FS_DELETE
17912 +                                | FS_CREATE | FS_EVENT_ON_CHILD);
17913 +static DECLARE_WAIT_QUEUE_HEAD(au_hfsn_wq);
17914 +static __cacheline_aligned_in_smp atomic64_t au_hfsn_ifree = ATOMIC64_INIT(0);
17915 +
17916 +static void au_hfsn_free_mark(struct fsnotify_mark *mark)
17917 +{
17918 +       struct au_hnotify *hn = container_of(mark, struct au_hnotify,
17919 +                                            hn_mark);
17920 +       /* AuDbg("here\n"); */
17921 +       au_cache_free_hnotify(hn);
17922 +       smp_mb__before_atomic(); /* for atomic64_dec */
17923 +       if (atomic64_dec_and_test(&au_hfsn_ifree))
17924 +               wake_up(&au_hfsn_wq);
17925 +}
17926 +
17927 +static int au_hfsn_alloc(struct au_hinode *hinode)
17928 +{
17929 +       int err;
17930 +       struct au_hnotify *hn;
17931 +       struct super_block *sb;
17932 +       struct au_branch *br;
17933 +       struct fsnotify_mark *mark;
17934 +       aufs_bindex_t bindex;
17935 +
17936 +       hn = hinode->hi_notify;
17937 +       sb = hn->hn_aufs_inode->i_sb;
17938 +       bindex = au_br_index(sb, hinode->hi_id);
17939 +       br = au_sbr(sb, bindex);
17940 +       AuDebugOn(!br->br_hfsn);
17941 +
17942 +       mark = &hn->hn_mark;
17943 +       fsnotify_init_mark(mark, br->br_hfsn->hfsn_group);
17944 +       mark->mask = AuHfsnMask;
17945 +       /*
17946 +        * by udba rename or rmdir, aufs assign a new inode to the known
17947 +        * h_inode, so specify 1 to allow dups.
17948 +        */
17949 +       lockdep_off();
17950 +       err = fsnotify_add_inode_mark(mark, hinode->hi_inode, /*allow_dups*/1);
17951 +       lockdep_on();
17952 +
17953 +       return err;
17954 +}
17955 +
17956 +static int au_hfsn_free(struct au_hinode *hinode, struct au_hnotify *hn)
17957 +{
17958 +       struct fsnotify_mark *mark;
17959 +       unsigned long long ull;
17960 +       struct fsnotify_group *group;
17961 +
17962 +       ull = atomic64_inc_return(&au_hfsn_ifree);
17963 +       BUG_ON(!ull);
17964 +
17965 +       mark = &hn->hn_mark;
17966 +       spin_lock(&mark->lock);
17967 +       group = mark->group;
17968 +       fsnotify_get_group(group);
17969 +       spin_unlock(&mark->lock);
17970 +       lockdep_off();
17971 +       fsnotify_destroy_mark(mark, group);
17972 +       fsnotify_put_mark(mark);
17973 +       fsnotify_put_group(group);
17974 +       lockdep_on();
17975 +
17976 +       /* free hn by myself */
17977 +       return 0;
17978 +}
17979 +
17980 +/* ---------------------------------------------------------------------- */
17981 +
17982 +static void au_hfsn_ctl(struct au_hinode *hinode, int do_set)
17983 +{
17984 +       struct fsnotify_mark *mark;
17985 +
17986 +       mark = &hinode->hi_notify->hn_mark;
17987 +       spin_lock(&mark->lock);
17988 +       if (do_set) {
17989 +               AuDebugOn(mark->mask & AuHfsnMask);
17990 +               mark->mask |= AuHfsnMask;
17991 +       } else {
17992 +               AuDebugOn(!(mark->mask & AuHfsnMask));
17993 +               mark->mask &= ~AuHfsnMask;
17994 +       }
17995 +       spin_unlock(&mark->lock);
17996 +       /* fsnotify_recalc_inode_mask(hinode->hi_inode); */
17997 +}
17998 +
17999 +/* ---------------------------------------------------------------------- */
18000 +
18001 +/* #define AuDbgHnotify */
18002 +#ifdef AuDbgHnotify
18003 +static char *au_hfsn_name(u32 mask)
18004 +{
18005 +#ifdef CONFIG_AUFS_DEBUG
18006 +#define test_ret(flag)                         \
18007 +       do {                                    \
18008 +               if (mask & flag)                \
18009 +                       return #flag;           \
18010 +       } while (0)
18011 +       test_ret(FS_ACCESS);
18012 +       test_ret(FS_MODIFY);
18013 +       test_ret(FS_ATTRIB);
18014 +       test_ret(FS_CLOSE_WRITE);
18015 +       test_ret(FS_CLOSE_NOWRITE);
18016 +       test_ret(FS_OPEN);
18017 +       test_ret(FS_MOVED_FROM);
18018 +       test_ret(FS_MOVED_TO);
18019 +       test_ret(FS_CREATE);
18020 +       test_ret(FS_DELETE);
18021 +       test_ret(FS_DELETE_SELF);
18022 +       test_ret(FS_MOVE_SELF);
18023 +       test_ret(FS_UNMOUNT);
18024 +       test_ret(FS_Q_OVERFLOW);
18025 +       test_ret(FS_IN_IGNORED);
18026 +       test_ret(FS_ISDIR);
18027 +       test_ret(FS_IN_ONESHOT);
18028 +       test_ret(FS_EVENT_ON_CHILD);
18029 +       return "";
18030 +#undef test_ret
18031 +#else
18032 +       return "??";
18033 +#endif
18034 +}
18035 +#endif
18036 +
18037 +/* ---------------------------------------------------------------------- */
18038 +
18039 +static void au_hfsn_free_group(struct fsnotify_group *group)
18040 +{
18041 +       struct au_br_hfsnotify *hfsn = group->private;
18042 +
18043 +       /* AuDbg("here\n"); */
18044 +       au_kfree_try_rcu(hfsn);
18045 +}
18046 +
18047 +static int au_hfsn_handle_event(struct fsnotify_group *group,
18048 +                               u32 mask, const void *data, int data_type,
18049 +                               struct inode *dir,
18050 +                               const struct qstr *file_name, u32 cookie,
18051 +                               struct fsnotify_iter_info *iter_info)
18052 +{
18053 +       int err;
18054 +       struct au_hnotify *hnotify;
18055 +       struct inode *h_dir, *h_inode;
18056 +       struct fsnotify_mark *inode_mark;
18057 +
18058 +       AuDebugOn(!(data_type == FSNOTIFY_EVENT_INODE
18059 +                   || data_type == FSNOTIFY_EVENT_DENTRY));
18060 +
18061 +       err = 0;
18062 +       /* if FS_UNMOUNT happens, there must be another bug */
18063 +       AuDebugOn(mask & FS_UNMOUNT);
18064 +       if (mask & (FS_IN_IGNORED | FS_UNMOUNT))
18065 +               goto out;
18066 +
18067 +       h_dir = dir;
18068 +       h_inode = NULL;
18069 +#ifdef AuDbgHnotify
18070 +       au_debug_on();
18071 +       if (1 || h_child_qstr.len != sizeof(AUFS_XINO_FNAME) - 1
18072 +           || strncmp(h_child_qstr.name, AUFS_XINO_FNAME, h_child_qstr.len)) {
18073 +               AuDbg("i%lu, mask 0x%x %s, hcname %.*s, hi%lu\n",
18074 +                     h_dir->i_ino, mask, au_hfsn_name(mask),
18075 +                     AuLNPair(&h_child_qstr), h_inode ? h_inode->i_ino : 0);
18076 +               /* WARN_ON(1); */
18077 +       }
18078 +       au_debug_off();
18079 +#endif
18080 +
18081 +       inode_mark = fsnotify_iter_inode_mark(iter_info);
18082 +       AuDebugOn(!inode_mark);
18083 +       hnotify = container_of(inode_mark, struct au_hnotify, hn_mark);
18084 +       err = au_hnotify(h_dir, hnotify, mask, file_name, h_inode);
18085 +
18086 +out:
18087 +       return err;
18088 +}
18089 +
18090 +static struct fsnotify_ops au_hfsn_ops = {
18091 +       .handle_event           = au_hfsn_handle_event,
18092 +       .free_group_priv        = au_hfsn_free_group,
18093 +       .free_mark              = au_hfsn_free_mark
18094 +};
18095 +
18096 +/* ---------------------------------------------------------------------- */
18097 +
18098 +static void au_hfsn_fin_br(struct au_branch *br)
18099 +{
18100 +       struct au_br_hfsnotify *hfsn;
18101 +
18102 +       hfsn = br->br_hfsn;
18103 +       if (hfsn) {
18104 +               lockdep_off();
18105 +               fsnotify_put_group(hfsn->hfsn_group);
18106 +               lockdep_on();
18107 +       }
18108 +}
18109 +
18110 +static int au_hfsn_init_br(struct au_branch *br, int perm)
18111 +{
18112 +       int err;
18113 +       struct fsnotify_group *group;
18114 +       struct au_br_hfsnotify *hfsn;
18115 +
18116 +       err = 0;
18117 +       br->br_hfsn = NULL;
18118 +       if (!au_br_hnotifyable(perm))
18119 +               goto out;
18120 +
18121 +       err = -ENOMEM;
18122 +       hfsn = kmalloc(sizeof(*hfsn), GFP_NOFS);
18123 +       if (unlikely(!hfsn))
18124 +               goto out;
18125 +
18126 +       err = 0;
18127 +       group = fsnotify_alloc_group(&au_hfsn_ops);
18128 +       if (IS_ERR(group)) {
18129 +               err = PTR_ERR(group);
18130 +               pr_err("fsnotify_alloc_group() failed, %d\n", err);
18131 +               goto out_hfsn;
18132 +       }
18133 +
18134 +       group->private = hfsn;
18135 +       hfsn->hfsn_group = group;
18136 +       br->br_hfsn = hfsn;
18137 +       goto out; /* success */
18138 +
18139 +out_hfsn:
18140 +       au_kfree_try_rcu(hfsn);
18141 +out:
18142 +       return err;
18143 +}
18144 +
18145 +static int au_hfsn_reset_br(unsigned int udba, struct au_branch *br, int perm)
18146 +{
18147 +       int err;
18148 +
18149 +       err = 0;
18150 +       if (!br->br_hfsn)
18151 +               err = au_hfsn_init_br(br, perm);
18152 +
18153 +       return err;
18154 +}
18155 +
18156 +/* ---------------------------------------------------------------------- */
18157 +
18158 +static void au_hfsn_fin(void)
18159 +{
18160 +       AuDbg("au_hfsn_ifree %lld\n", (long long)atomic64_read(&au_hfsn_ifree));
18161 +       wait_event(au_hfsn_wq, !atomic64_read(&au_hfsn_ifree));
18162 +}
18163 +
18164 +const struct au_hnotify_op au_hnotify_op = {
18165 +       .ctl            = au_hfsn_ctl,
18166 +       .alloc          = au_hfsn_alloc,
18167 +       .free           = au_hfsn_free,
18168 +
18169 +       .fin            = au_hfsn_fin,
18170 +
18171 +       .reset_br       = au_hfsn_reset_br,
18172 +       .fin_br         = au_hfsn_fin_br,
18173 +       .init_br        = au_hfsn_init_br
18174 +};
18175 diff -urN /usr/share/empty/fs/aufs/hfsplus.c linux/fs/aufs/hfsplus.c
18176 --- /usr/share/empty/fs/aufs/hfsplus.c  1970-01-01 01:00:00.000000000 +0100
18177 +++ linux/fs/aufs/hfsplus.c     2022-03-21 14:49:05.723299677 +0100
18178 @@ -0,0 +1,60 @@
18179 +// SPDX-License-Identifier: GPL-2.0
18180 +/*
18181 + * Copyright (C) 2010-2021 Junjiro R. Okajima
18182 + *
18183 + * This program, aufs is free software; you can redistribute it and/or modify
18184 + * it under the terms of the GNU General Public License as published by
18185 + * the Free Software Foundation; either version 2 of the License, or
18186 + * (at your option) any later version.
18187 + *
18188 + * This program is distributed in the hope that it will be useful,
18189 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
18190 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18191 + * GNU General Public License for more details.
18192 + *
18193 + * You should have received a copy of the GNU General Public License
18194 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18195 + */
18196 +
18197 +/*
18198 + * special support for filesystems which acquires an inode mutex
18199 + * at final closing a file, eg, hfsplus.
18200 + *
18201 + * This trick is very simple and stupid, just to open the file before really
18202 + * necessary open to tell hfsplus that this is not the final closing.
18203 + * The caller should call au_h_open_pre() after acquiring the inode mutex,
18204 + * and au_h_open_post() after releasing it.
18205 + */
18206 +
18207 +#include "aufs.h"
18208 +
18209 +struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
18210 +                          int force_wr)
18211 +{
18212 +       struct file *h_file;
18213 +       struct dentry *h_dentry;
18214 +
18215 +       h_dentry = au_h_dptr(dentry, bindex);
18216 +       AuDebugOn(!h_dentry);
18217 +       AuDebugOn(d_is_negative(h_dentry));
18218 +
18219 +       h_file = NULL;
18220 +       if (au_test_hfsplus(h_dentry->d_sb)
18221 +           && d_is_reg(h_dentry))
18222 +               h_file = au_h_open(dentry, bindex,
18223 +                                  O_RDONLY | O_NOATIME | O_LARGEFILE,
18224 +                                  /*file*/NULL, force_wr);
18225 +       return h_file;
18226 +}
18227 +
18228 +void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
18229 +                   struct file *h_file)
18230 +{
18231 +       struct au_branch *br;
18232 +
18233 +       if (h_file) {
18234 +               fput(h_file);
18235 +               br = au_sbr(dentry->d_sb, bindex);
18236 +               au_lcnt_dec(&br->br_nfiles);
18237 +       }
18238 +}
18239 diff -urN /usr/share/empty/fs/aufs/hnotify.c linux/fs/aufs/hnotify.c
18240 --- /usr/share/empty/fs/aufs/hnotify.c  1970-01-01 01:00:00.000000000 +0100
18241 +++ linux/fs/aufs/hnotify.c     2022-03-21 14:49:05.723299677 +0100
18242 @@ -0,0 +1,715 @@
18243 +// SPDX-License-Identifier: GPL-2.0
18244 +/*
18245 + * Copyright (C) 2005-2021 Junjiro R. Okajima
18246 + *
18247 + * This program, aufs is free software; you can redistribute it and/or modify
18248 + * it under the terms of the GNU General Public License as published by
18249 + * the Free Software Foundation; either version 2 of the License, or
18250 + * (at your option) any later version.
18251 + *
18252 + * This program is distributed in the hope that it will be useful,
18253 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
18254 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18255 + * GNU General Public License for more details.
18256 + *
18257 + * You should have received a copy of the GNU General Public License
18258 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18259 + */
18260 +
18261 +/*
18262 + * abstraction to notify the direct changes on lower directories
18263 + */
18264 +
18265 +/* #include <linux/iversion.h> */
18266 +#include "aufs.h"
18267 +
18268 +int au_hn_alloc(struct au_hinode *hinode, struct inode *inode)
18269 +{
18270 +       int err;
18271 +       struct au_hnotify *hn;
18272 +
18273 +       err = -ENOMEM;
18274 +       hn = au_cache_alloc_hnotify();
18275 +       if (hn) {
18276 +               hn->hn_aufs_inode = inode;
18277 +               hinode->hi_notify = hn;
18278 +               err = au_hnotify_op.alloc(hinode);
18279 +               AuTraceErr(err);
18280 +               if (unlikely(err)) {
18281 +                       hinode->hi_notify = NULL;
18282 +                       au_cache_free_hnotify(hn);
18283 +                       /*
18284 +                        * The upper dir was removed by udba, but the same named
18285 +                        * dir left. In this case, aufs assigns a new inode
18286 +                        * number and set the monitor again.
18287 +                        * For the lower dir, the old monitor is still left.
18288 +                        */
18289 +                       if (err == -EEXIST)
18290 +                               err = 0;
18291 +               }
18292 +       }
18293 +
18294 +       AuTraceErr(err);
18295 +       return err;
18296 +}
18297 +
18298 +void au_hn_free(struct au_hinode *hinode)
18299 +{
18300 +       struct au_hnotify *hn;
18301 +
18302 +       hn = hinode->hi_notify;
18303 +       if (hn) {
18304 +               hinode->hi_notify = NULL;
18305 +               if (au_hnotify_op.free(hinode, hn))
18306 +                       au_cache_free_hnotify(hn);
18307 +       }
18308 +}
18309 +
18310 +/* ---------------------------------------------------------------------- */
18311 +
18312 +void au_hn_ctl(struct au_hinode *hinode, int do_set)
18313 +{
18314 +       if (hinode->hi_notify)
18315 +               au_hnotify_op.ctl(hinode, do_set);
18316 +}
18317 +
18318 +void au_hn_reset(struct inode *inode, unsigned int flags)
18319 +{
18320 +       aufs_bindex_t bindex, bbot;
18321 +       struct inode *hi;
18322 +       struct dentry *iwhdentry;
18323 +
18324 +       bbot = au_ibbot(inode);
18325 +       for (bindex = au_ibtop(inode); bindex <= bbot; bindex++) {
18326 +               hi = au_h_iptr(inode, bindex);
18327 +               if (!hi)
18328 +                       continue;
18329 +
18330 +               /* inode_lock_nested(hi, AuLsc_I_CHILD); */
18331 +               iwhdentry = au_hi_wh(inode, bindex);
18332 +               if (iwhdentry)
18333 +                       dget(iwhdentry);
18334 +               au_igrab(hi);
18335 +               au_set_h_iptr(inode, bindex, NULL, 0);
18336 +               au_set_h_iptr(inode, bindex, au_igrab(hi),
18337 +                             flags & ~AuHi_XINO);
18338 +               iput(hi);
18339 +               dput(iwhdentry);
18340 +               /* inode_unlock(hi); */
18341 +       }
18342 +}
18343 +
18344 +/* ---------------------------------------------------------------------- */
18345 +
18346 +static int hn_xino(struct inode *inode, struct inode *h_inode)
18347 +{
18348 +       int err;
18349 +       aufs_bindex_t bindex, bbot, bfound, btop;
18350 +       struct inode *h_i;
18351 +
18352 +       err = 0;
18353 +       if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
18354 +               pr_warn("branch root dir was changed\n");
18355 +               goto out;
18356 +       }
18357 +
18358 +       bfound = -1;
18359 +       bbot = au_ibbot(inode);
18360 +       btop = au_ibtop(inode);
18361 +#if 0 /* reserved for future use */
18362 +       if (bindex == bbot) {
18363 +               /* keep this ino in rename case */
18364 +               goto out;
18365 +       }
18366 +#endif
18367 +       for (bindex = btop; bindex <= bbot; bindex++)
18368 +               if (au_h_iptr(inode, bindex) == h_inode) {
18369 +                       bfound = bindex;
18370 +                       break;
18371 +               }
18372 +       if (bfound < 0)
18373 +               goto out;
18374 +
18375 +       for (bindex = btop; bindex <= bbot; bindex++) {
18376 +               h_i = au_h_iptr(inode, bindex);
18377 +               if (!h_i)
18378 +                       continue;
18379 +
18380 +               err = au_xino_write(inode->i_sb, bindex, h_i->i_ino, /*ino*/0);
18381 +               /* ignore this error */
18382 +               /* bad action? */
18383 +       }
18384 +
18385 +       /* children inode number will be broken */
18386 +
18387 +out:
18388 +       AuTraceErr(err);
18389 +       return err;
18390 +}
18391 +
18392 +static int hn_gen_tree(struct dentry *dentry)
18393 +{
18394 +       int err, i, j, ndentry;
18395 +       struct au_dcsub_pages dpages;
18396 +       struct au_dpage *dpage;
18397 +       struct dentry **dentries;
18398 +
18399 +       err = au_dpages_init(&dpages, GFP_NOFS);
18400 +       if (unlikely(err))
18401 +               goto out;
18402 +       err = au_dcsub_pages(&dpages, dentry, NULL, NULL);
18403 +       if (unlikely(err))
18404 +               goto out_dpages;
18405 +
18406 +       for (i = 0; i < dpages.ndpage; i++) {
18407 +               dpage = dpages.dpages + i;
18408 +               dentries = dpage->dentries;
18409 +               ndentry = dpage->ndentry;
18410 +               for (j = 0; j < ndentry; j++) {
18411 +                       struct dentry *d;
18412 +
18413 +                       d = dentries[j];
18414 +                       if (IS_ROOT(d))
18415 +                               continue;
18416 +
18417 +                       au_digen_dec(d);
18418 +                       if (d_really_is_positive(d))
18419 +                               /* todo: reset children xino?
18420 +                                  cached children only? */
18421 +                               au_iigen_dec(d_inode(d));
18422 +               }
18423 +       }
18424 +
18425 +out_dpages:
18426 +       au_dpages_free(&dpages);
18427 +out:
18428 +       return err;
18429 +}
18430 +
18431 +/*
18432 + * return 0 if processed.
18433 + */
18434 +static int hn_gen_by_inode(char *name, unsigned int nlen, struct inode *inode,
18435 +                          const unsigned int isdir)
18436 +{
18437 +       int err;
18438 +       struct dentry *d;
18439 +       struct qstr *dname;
18440 +
18441 +       err = 1;
18442 +       if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
18443 +               pr_warn("branch root dir was changed\n");
18444 +               err = 0;
18445 +               goto out;
18446 +       }
18447 +
18448 +       if (!isdir) {
18449 +               AuDebugOn(!name);
18450 +               au_iigen_dec(inode);
18451 +               spin_lock(&inode->i_lock);
18452 +               hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
18453 +                       spin_lock(&d->d_lock);
18454 +                       dname = &d->d_name;
18455 +                       if (dname->len != nlen
18456 +                           && memcmp(dname->name, name, nlen)) {
18457 +                               spin_unlock(&d->d_lock);
18458 +                               continue;
18459 +                       }
18460 +                       err = 0;
18461 +                       au_digen_dec(d);
18462 +                       spin_unlock(&d->d_lock);
18463 +                       break;
18464 +               }
18465 +               spin_unlock(&inode->i_lock);
18466 +       } else {
18467 +               au_fset_si(au_sbi(inode->i_sb), FAILED_REFRESH_DIR);
18468 +               d = d_find_any_alias(inode);
18469 +               if (!d) {
18470 +                       au_iigen_dec(inode);
18471 +                       goto out;
18472 +               }
18473 +
18474 +               spin_lock(&d->d_lock);
18475 +               dname = &d->d_name;
18476 +               if (dname->len == nlen && !memcmp(dname->name, name, nlen)) {
18477 +                       spin_unlock(&d->d_lock);
18478 +                       err = hn_gen_tree(d);
18479 +                       spin_lock(&d->d_lock);
18480 +               }
18481 +               spin_unlock(&d->d_lock);
18482 +               dput(d);
18483 +       }
18484 +
18485 +out:
18486 +       AuTraceErr(err);
18487 +       return err;
18488 +}
18489 +
18490 +static int hn_gen_by_name(struct dentry *dentry, const unsigned int isdir)
18491 +{
18492 +       int err;
18493 +
18494 +       if (IS_ROOT(dentry)) {
18495 +               pr_warn("branch root dir was changed\n");
18496 +               return 0;
18497 +       }
18498 +
18499 +       err = 0;
18500 +       if (!isdir) {
18501 +               au_digen_dec(dentry);
18502 +               if (d_really_is_positive(dentry))
18503 +                       au_iigen_dec(d_inode(dentry));
18504 +       } else {
18505 +               au_fset_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR);
18506 +               if (d_really_is_positive(dentry))
18507 +                       err = hn_gen_tree(dentry);
18508 +       }
18509 +
18510 +       AuTraceErr(err);
18511 +       return err;
18512 +}
18513 +
18514 +/* ---------------------------------------------------------------------- */
18515 +
18516 +/* hnotify job flags */
18517 +#define AuHnJob_XINO0          1
18518 +#define AuHnJob_GEN            (1 << 1)
18519 +#define AuHnJob_DIRENT         (1 << 2)
18520 +#define AuHnJob_ISDIR          (1 << 3)
18521 +#define AuHnJob_TRYXINO0       (1 << 4)
18522 +#define AuHnJob_MNTPNT         (1 << 5)
18523 +#define au_ftest_hnjob(flags, name)    ((flags) & AuHnJob_##name)
18524 +#define au_fset_hnjob(flags, name) \
18525 +       do { (flags) |= AuHnJob_##name; } while (0)
18526 +#define au_fclr_hnjob(flags, name) \
18527 +       do { (flags) &= ~AuHnJob_##name; } while (0)
18528 +
18529 +enum {
18530 +       AuHn_CHILD,
18531 +       AuHn_PARENT,
18532 +       AuHnLast
18533 +};
18534 +
18535 +struct au_hnotify_args {
18536 +       struct inode *h_dir, *dir, *h_child_inode;
18537 +       u32 mask;
18538 +       unsigned int flags[AuHnLast];
18539 +       unsigned int h_child_nlen;
18540 +       char h_child_name[];
18541 +};
18542 +
18543 +struct hn_job_args {
18544 +       unsigned int flags;
18545 +       struct inode *inode, *h_inode, *dir, *h_dir;
18546 +       struct dentry *dentry;
18547 +       char *h_name;
18548 +       int h_nlen;
18549 +};
18550 +
18551 +static int hn_job(struct hn_job_args *a)
18552 +{
18553 +       const unsigned int isdir = au_ftest_hnjob(a->flags, ISDIR);
18554 +       int e;
18555 +
18556 +       /* reset xino */
18557 +       if (au_ftest_hnjob(a->flags, XINO0) && a->inode)
18558 +               hn_xino(a->inode, a->h_inode); /* ignore this error */
18559 +
18560 +       if (au_ftest_hnjob(a->flags, TRYXINO0)
18561 +           && a->inode
18562 +           && a->h_inode) {
18563 +               inode_lock_shared_nested(a->h_inode, AuLsc_I_CHILD);
18564 +               if (!a->h_inode->i_nlink
18565 +                   && !(a->h_inode->i_state & I_LINKABLE))
18566 +                       hn_xino(a->inode, a->h_inode); /* ignore this error */
18567 +               inode_unlock_shared(a->h_inode);
18568 +       }
18569 +
18570 +       /* make the generation obsolete */
18571 +       if (au_ftest_hnjob(a->flags, GEN)) {
18572 +               e = -1;
18573 +               if (a->inode)
18574 +                       e = hn_gen_by_inode(a->h_name, a->h_nlen, a->inode,
18575 +                                             isdir);
18576 +               if (e && a->dentry)
18577 +                       hn_gen_by_name(a->dentry, isdir);
18578 +               /* ignore this error */
18579 +       }
18580 +
18581 +       /* make dir entries obsolete */
18582 +       if (au_ftest_hnjob(a->flags, DIRENT) && a->inode) {
18583 +               struct au_vdir *vdir;
18584 +
18585 +               vdir = au_ivdir(a->inode);
18586 +               if (vdir)
18587 +                       vdir->vd_jiffy = 0;
18588 +               /* IMustLock(a->inode); */
18589 +               /* inode_inc_iversion(a->inode); */
18590 +       }
18591 +
18592 +       /* can do nothing but warn */
18593 +       if (au_ftest_hnjob(a->flags, MNTPNT)
18594 +           && a->dentry
18595 +           && d_mountpoint(a->dentry))
18596 +               pr_warn("mount-point %pd is removed or renamed\n", a->dentry);
18597 +
18598 +       return 0;
18599 +}
18600 +
18601 +/* ---------------------------------------------------------------------- */
18602 +
18603 +static struct dentry *lookup_wlock_by_name(char *name, unsigned int nlen,
18604 +                                          struct inode *dir)
18605 +{
18606 +       struct dentry *dentry, *d, *parent;
18607 +       struct qstr *dname;
18608 +
18609 +       parent = d_find_any_alias(dir);
18610 +       if (!parent)
18611 +               return NULL;
18612 +
18613 +       dentry = NULL;
18614 +       spin_lock(&parent->d_lock);
18615 +       list_for_each_entry(d, &parent->d_subdirs, d_child) {
18616 +               /* AuDbg("%pd\n", d); */
18617 +               spin_lock_nested(&d->d_lock, DENTRY_D_LOCK_NESTED);
18618 +               dname = &d->d_name;
18619 +               if (dname->len != nlen || memcmp(dname->name, name, nlen))
18620 +                       goto cont_unlock;
18621 +               if (au_di(d))
18622 +                       au_digen_dec(d);
18623 +               else
18624 +                       goto cont_unlock;
18625 +               if (au_dcount(d) > 0) {
18626 +                       dentry = dget_dlock(d);
18627 +                       spin_unlock(&d->d_lock);
18628 +                       break;
18629 +               }
18630 +
18631 +cont_unlock:
18632 +               spin_unlock(&d->d_lock);
18633 +       }
18634 +       spin_unlock(&parent->d_lock);
18635 +       dput(parent);
18636 +
18637 +       if (dentry)
18638 +               di_write_lock_child(dentry);
18639 +
18640 +       return dentry;
18641 +}
18642 +
18643 +static struct inode *lookup_wlock_by_ino(struct super_block *sb,
18644 +                                        aufs_bindex_t bindex, ino_t h_ino)
18645 +{
18646 +       struct inode *inode;
18647 +       ino_t ino;
18648 +       int err;
18649 +
18650 +       inode = NULL;
18651 +       err = au_xino_read(sb, bindex, h_ino, &ino);
18652 +       if (!err && ino)
18653 +               inode = ilookup(sb, ino);
18654 +       if (!inode)
18655 +               goto out;
18656 +
18657 +       if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
18658 +               pr_warn("wrong root branch\n");
18659 +               iput(inode);
18660 +               inode = NULL;
18661 +               goto out;
18662 +       }
18663 +
18664 +       ii_write_lock_child(inode);
18665 +
18666 +out:
18667 +       return inode;
18668 +}
18669 +
18670 +static void au_hn_bh(void *_args)
18671 +{
18672 +       struct au_hnotify_args *a = _args;
18673 +       struct super_block *sb;
18674 +       aufs_bindex_t bindex, bbot, bfound;
18675 +       unsigned char xino, try_iput;
18676 +       int err;
18677 +       struct inode *inode;
18678 +       ino_t h_ino;
18679 +       struct hn_job_args args;
18680 +       struct dentry *dentry;
18681 +       struct au_sbinfo *sbinfo;
18682 +
18683 +       AuDebugOn(!_args);
18684 +       AuDebugOn(!a->h_dir);
18685 +       AuDebugOn(!a->dir);
18686 +       AuDebugOn(!a->mask);
18687 +       AuDbg("mask 0x%x, i%lu, hi%lu, hci%lu\n",
18688 +             a->mask, a->dir->i_ino, a->h_dir->i_ino,
18689 +             a->h_child_inode ? a->h_child_inode->i_ino : 0);
18690 +
18691 +       inode = NULL;
18692 +       dentry = NULL;
18693 +       /*
18694 +        * do not lock a->dir->i_mutex here
18695 +        * because of d_revalidate() may cause a deadlock.
18696 +        */
18697 +       sb = a->dir->i_sb;
18698 +       AuDebugOn(!sb);
18699 +       sbinfo = au_sbi(sb);
18700 +       AuDebugOn(!sbinfo);
18701 +       si_write_lock(sb, AuLock_NOPLMW);
18702 +
18703 +       if (au_opt_test(sbinfo->si_mntflags, DIRREN))
18704 +               switch (a->mask & FS_EVENTS_POSS_ON_CHILD) {
18705 +               case FS_MOVED_FROM:
18706 +               case FS_MOVED_TO:
18707 +                       AuWarn1("DIRREN with UDBA may not work correctly "
18708 +                               "for the direct rename(2)\n");
18709 +               }
18710 +
18711 +       ii_read_lock_parent(a->dir);
18712 +       bfound = -1;
18713 +       bbot = au_ibbot(a->dir);
18714 +       for (bindex = au_ibtop(a->dir); bindex <= bbot; bindex++)
18715 +               if (au_h_iptr(a->dir, bindex) == a->h_dir) {
18716 +                       bfound = bindex;
18717 +                       break;
18718 +               }
18719 +       ii_read_unlock(a->dir);
18720 +       if (unlikely(bfound < 0))
18721 +               goto out;
18722 +
18723 +       xino = !!au_opt_test(au_mntflags(sb), XINO);
18724 +       h_ino = 0;
18725 +       if (a->h_child_inode)
18726 +               h_ino = a->h_child_inode->i_ino;
18727 +
18728 +       if (a->h_child_nlen
18729 +           && (au_ftest_hnjob(a->flags[AuHn_CHILD], GEN)
18730 +               || au_ftest_hnjob(a->flags[AuHn_CHILD], MNTPNT)))
18731 +               dentry = lookup_wlock_by_name(a->h_child_name, a->h_child_nlen,
18732 +                                             a->dir);
18733 +       try_iput = 0;
18734 +       if (dentry && d_really_is_positive(dentry))
18735 +               inode = d_inode(dentry);
18736 +       if (xino && !inode && h_ino
18737 +           && (au_ftest_hnjob(a->flags[AuHn_CHILD], XINO0)
18738 +               || au_ftest_hnjob(a->flags[AuHn_CHILD], TRYXINO0)
18739 +               || au_ftest_hnjob(a->flags[AuHn_CHILD], GEN))) {
18740 +               inode = lookup_wlock_by_ino(sb, bfound, h_ino);
18741 +               try_iput = 1;
18742 +       }
18743 +
18744 +       args.flags = a->flags[AuHn_CHILD];
18745 +       args.dentry = dentry;
18746 +       args.inode = inode;
18747 +       args.h_inode = a->h_child_inode;
18748 +       args.dir = a->dir;
18749 +       args.h_dir = a->h_dir;
18750 +       args.h_name = a->h_child_name;
18751 +       args.h_nlen = a->h_child_nlen;
18752 +       err = hn_job(&args);
18753 +       if (dentry) {
18754 +               if (au_di(dentry))
18755 +                       di_write_unlock(dentry);
18756 +               dput(dentry);
18757 +       }
18758 +       if (inode && try_iput) {
18759 +               ii_write_unlock(inode);
18760 +               iput(inode);
18761 +       }
18762 +
18763 +       ii_write_lock_parent(a->dir);
18764 +       args.flags = a->flags[AuHn_PARENT];
18765 +       args.dentry = NULL;
18766 +       args.inode = a->dir;
18767 +       args.h_inode = a->h_dir;
18768 +       args.dir = NULL;
18769 +       args.h_dir = NULL;
18770 +       args.h_name = NULL;
18771 +       args.h_nlen = 0;
18772 +       err = hn_job(&args);
18773 +       ii_write_unlock(a->dir);
18774 +
18775 +out:
18776 +       iput(a->h_child_inode);
18777 +       iput(a->h_dir);
18778 +       iput(a->dir);
18779 +       si_write_unlock(sb);
18780 +       au_nwt_done(&sbinfo->si_nowait);
18781 +       au_kfree_rcu(a);
18782 +}
18783 +
18784 +/* ---------------------------------------------------------------------- */
18785 +
18786 +int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
18787 +              const struct qstr *h_child_qstr, struct inode *h_child_inode)
18788 +{
18789 +       int err, len;
18790 +       unsigned int flags[AuHnLast], f;
18791 +       unsigned char isdir, isroot, wh;
18792 +       struct inode *dir;
18793 +       struct au_hnotify_args *args;
18794 +       char *p, *h_child_name;
18795 +
18796 +       err = 0;
18797 +       AuDebugOn(!hnotify || !hnotify->hn_aufs_inode);
18798 +       dir = igrab(hnotify->hn_aufs_inode);
18799 +       if (!dir)
18800 +               goto out;
18801 +
18802 +       isroot = (dir->i_ino == AUFS_ROOT_INO);
18803 +       wh = 0;
18804 +       h_child_name = (void *)h_child_qstr->name;
18805 +       len = h_child_qstr->len;
18806 +       if (h_child_name) {
18807 +               if (len > AUFS_WH_PFX_LEN
18808 +                   && !memcmp(h_child_name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
18809 +                       h_child_name += AUFS_WH_PFX_LEN;
18810 +                       len -= AUFS_WH_PFX_LEN;
18811 +                       wh = 1;
18812 +               }
18813 +       }
18814 +
18815 +       isdir = 0;
18816 +       if (h_child_inode)
18817 +               isdir = !!S_ISDIR(h_child_inode->i_mode);
18818 +       flags[AuHn_PARENT] = AuHnJob_ISDIR;
18819 +       flags[AuHn_CHILD] = 0;
18820 +       if (isdir)
18821 +               flags[AuHn_CHILD] = AuHnJob_ISDIR;
18822 +       au_fset_hnjob(flags[AuHn_PARENT], DIRENT);
18823 +       au_fset_hnjob(flags[AuHn_CHILD], GEN);
18824 +       switch (mask & ALL_FSNOTIFY_DIRENT_EVENTS) {
18825 +       case FS_MOVED_FROM:
18826 +       case FS_MOVED_TO:
18827 +               au_fset_hnjob(flags[AuHn_CHILD], XINO0);
18828 +               au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
18829 +               fallthrough;
18830 +       case FS_CREATE:
18831 +               AuDebugOn(!h_child_name);
18832 +               break;
18833 +
18834 +       case FS_DELETE:
18835 +               /*
18836 +                * aufs never be able to get this child inode.
18837 +                * revalidation should be in d_revalidate()
18838 +                * by checking i_nlink, i_generation or d_unhashed().
18839 +                */
18840 +               AuDebugOn(!h_child_name);
18841 +               au_fset_hnjob(flags[AuHn_CHILD], TRYXINO0);
18842 +               au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
18843 +               break;
18844 +
18845 +       default:
18846 +               AuDebugOn(1);
18847 +       }
18848 +
18849 +       if (wh)
18850 +               h_child_inode = NULL;
18851 +
18852 +       err = -ENOMEM;
18853 +       /* iput() and kfree() will be called in au_hnotify() */
18854 +       args = kmalloc(sizeof(*args) + len + 1, GFP_NOFS);
18855 +       if (unlikely(!args)) {
18856 +               AuErr1("no memory\n");
18857 +               iput(dir);
18858 +               goto out;
18859 +       }
18860 +       args->flags[AuHn_PARENT] = flags[AuHn_PARENT];
18861 +       args->flags[AuHn_CHILD] = flags[AuHn_CHILD];
18862 +       args->mask = mask;
18863 +       args->dir = dir;
18864 +       args->h_dir = igrab(h_dir);
18865 +       if (h_child_inode)
18866 +               h_child_inode = igrab(h_child_inode); /* can be NULL */
18867 +       args->h_child_inode = h_child_inode;
18868 +       args->h_child_nlen = len;
18869 +       if (len) {
18870 +               p = (void *)args;
18871 +               p += sizeof(*args);
18872 +               memcpy(p, h_child_name, len);
18873 +               p[len] = 0;
18874 +       }
18875 +
18876 +       /* NFS fires the event for silly-renamed one from kworker */
18877 +       f = 0;
18878 +       if (!dir->i_nlink
18879 +           || (au_test_nfs(h_dir->i_sb) && (mask & FS_DELETE)))
18880 +               f = AuWkq_NEST;
18881 +       err = au_wkq_nowait(au_hn_bh, args, dir->i_sb, f);
18882 +       if (unlikely(err)) {
18883 +               pr_err("wkq %d\n", err);
18884 +               iput(args->h_child_inode);
18885 +               iput(args->h_dir);
18886 +               iput(args->dir);
18887 +               au_kfree_rcu(args);
18888 +       }
18889 +
18890 +out:
18891 +       return err;
18892 +}
18893 +
18894 +/* ---------------------------------------------------------------------- */
18895 +
18896 +int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm)
18897 +{
18898 +       int err;
18899 +
18900 +       AuDebugOn(!(udba & AuOptMask_UDBA));
18901 +
18902 +       err = 0;
18903 +       if (au_hnotify_op.reset_br)
18904 +               err = au_hnotify_op.reset_br(udba, br, perm);
18905 +
18906 +       return err;
18907 +}
18908 +
18909 +int au_hnotify_init_br(struct au_branch *br, int perm)
18910 +{
18911 +       int err;
18912 +
18913 +       err = 0;
18914 +       if (au_hnotify_op.init_br)
18915 +               err = au_hnotify_op.init_br(br, perm);
18916 +
18917 +       return err;
18918 +}
18919 +
18920 +void au_hnotify_fin_br(struct au_branch *br)
18921 +{
18922 +       if (au_hnotify_op.fin_br)
18923 +               au_hnotify_op.fin_br(br);
18924 +}
18925 +
18926 +static void au_hn_destroy_cache(void)
18927 +{
18928 +       kmem_cache_destroy(au_cache[AuCache_HNOTIFY]);
18929 +       au_cache[AuCache_HNOTIFY] = NULL;
18930 +}
18931 +
18932 +int __init au_hnotify_init(void)
18933 +{
18934 +       int err;
18935 +
18936 +       err = -ENOMEM;
18937 +       au_cache[AuCache_HNOTIFY] = AuCache(au_hnotify);
18938 +       if (au_cache[AuCache_HNOTIFY]) {
18939 +               err = 0;
18940 +               if (au_hnotify_op.init)
18941 +                       err = au_hnotify_op.init();
18942 +               if (unlikely(err))
18943 +                       au_hn_destroy_cache();
18944 +       }
18945 +       AuTraceErr(err);
18946 +       return err;
18947 +}
18948 +
18949 +void au_hnotify_fin(void)
18950 +{
18951 +       if (au_hnotify_op.fin)
18952 +               au_hnotify_op.fin();
18953 +
18954 +       /* cf. au_cache_fin() */
18955 +       if (au_cache[AuCache_HNOTIFY])
18956 +               au_hn_destroy_cache();
18957 +}
18958 diff -urN /usr/share/empty/fs/aufs/iinfo.c linux/fs/aufs/iinfo.c
18959 --- /usr/share/empty/fs/aufs/iinfo.c    1970-01-01 01:00:00.000000000 +0100
18960 +++ linux/fs/aufs/iinfo.c       2022-03-21 14:49:05.726633010 +0100
18961 @@ -0,0 +1,286 @@
18962 +// SPDX-License-Identifier: GPL-2.0
18963 +/*
18964 + * Copyright (C) 2005-2021 Junjiro R. Okajima
18965 + *
18966 + * This program, aufs is free software; you can redistribute it and/or modify
18967 + * it under the terms of the GNU General Public License as published by
18968 + * the Free Software Foundation; either version 2 of the License, or
18969 + * (at your option) any later version.
18970 + *
18971 + * This program is distributed in the hope that it will be useful,
18972 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
18973 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18974 + * GNU General Public License for more details.
18975 + *
18976 + * You should have received a copy of the GNU General Public License
18977 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18978 + */
18979 +
18980 +/*
18981 + * inode private data
18982 + */
18983 +
18984 +#include "aufs.h"
18985 +
18986 +struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex)
18987 +{
18988 +       struct inode *h_inode;
18989 +       struct au_hinode *hinode;
18990 +
18991 +       IiMustAnyLock(inode);
18992 +
18993 +       hinode = au_hinode(au_ii(inode), bindex);
18994 +       h_inode = hinode->hi_inode;
18995 +       AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
18996 +       return h_inode;
18997 +}
18998 +
18999 +/* todo: hard/soft set? */
19000 +void au_hiput(struct au_hinode *hinode)
19001 +{
19002 +       au_hn_free(hinode);
19003 +       dput(hinode->hi_whdentry);
19004 +       iput(hinode->hi_inode);
19005 +}
19006 +
19007 +unsigned int au_hi_flags(struct inode *inode, int isdir)
19008 +{
19009 +       unsigned int flags;
19010 +       const unsigned int mnt_flags = au_mntflags(inode->i_sb);
19011 +
19012 +       flags = 0;
19013 +       if (au_opt_test(mnt_flags, XINO))
19014 +               au_fset_hi(flags, XINO);
19015 +       if (isdir && au_opt_test(mnt_flags, UDBA_HNOTIFY))
19016 +               au_fset_hi(flags, HNOTIFY);
19017 +       return flags;
19018 +}
19019 +
19020 +void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
19021 +                  struct inode *h_inode, unsigned int flags)
19022 +{
19023 +       struct au_hinode *hinode;
19024 +       struct inode *hi;
19025 +       struct au_iinfo *iinfo = au_ii(inode);
19026 +
19027 +       IiMustWriteLock(inode);
19028 +
19029 +       hinode = au_hinode(iinfo, bindex);
19030 +       hi = hinode->hi_inode;
19031 +       AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
19032 +
19033 +       if (hi)
19034 +               au_hiput(hinode);
19035 +       hinode->hi_inode = h_inode;
19036 +       if (h_inode) {
19037 +               int err;
19038 +               struct super_block *sb = inode->i_sb;
19039 +               struct au_branch *br;
19040 +
19041 +               AuDebugOn(inode->i_mode
19042 +                         && (h_inode->i_mode & S_IFMT)
19043 +                         != (inode->i_mode & S_IFMT));
19044 +               if (bindex == iinfo->ii_btop)
19045 +                       au_cpup_igen(inode, h_inode);
19046 +               br = au_sbr(sb, bindex);
19047 +               hinode->hi_id = br->br_id;
19048 +               if (au_ftest_hi(flags, XINO)) {
19049 +                       err = au_xino_write(sb, bindex, h_inode->i_ino,
19050 +                                           inode->i_ino);
19051 +                       if (unlikely(err))
19052 +                               AuIOErr1("failed au_xino_write() %d\n", err);
19053 +               }
19054 +
19055 +               if (au_ftest_hi(flags, HNOTIFY)
19056 +                   && au_br_hnotifyable(br->br_perm)) {
19057 +                       err = au_hn_alloc(hinode, inode);
19058 +                       if (unlikely(err))
19059 +                               AuIOErr1("au_hn_alloc() %d\n", err);
19060 +               }
19061 +       }
19062 +}
19063 +
19064 +void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
19065 +                 struct dentry *h_wh)
19066 +{
19067 +       struct au_hinode *hinode;
19068 +
19069 +       IiMustWriteLock(inode);
19070 +
19071 +       hinode = au_hinode(au_ii(inode), bindex);
19072 +       AuDebugOn(hinode->hi_whdentry);
19073 +       hinode->hi_whdentry = h_wh;
19074 +}
19075 +
19076 +void au_update_iigen(struct inode *inode, int half)
19077 +{
19078 +       struct au_iinfo *iinfo;
19079 +       struct au_iigen *iigen;
19080 +       unsigned int sigen;
19081 +
19082 +       sigen = au_sigen(inode->i_sb);
19083 +       iinfo = au_ii(inode);
19084 +       iigen = &iinfo->ii_generation;
19085 +       spin_lock(&iigen->ig_spin);
19086 +       iigen->ig_generation = sigen;
19087 +       if (half)
19088 +               au_ig_fset(iigen->ig_flags, HALF_REFRESHED);
19089 +       else
19090 +               au_ig_fclr(iigen->ig_flags, HALF_REFRESHED);
19091 +       spin_unlock(&iigen->ig_spin);
19092 +}
19093 +
19094 +/* it may be called at remount time, too */
19095 +void au_update_ibrange(struct inode *inode, int do_put_zero)
19096 +{
19097 +       struct au_iinfo *iinfo;
19098 +       aufs_bindex_t bindex, bbot;
19099 +
19100 +       AuDebugOn(au_is_bad_inode(inode));
19101 +       IiMustWriteLock(inode);
19102 +
19103 +       iinfo = au_ii(inode);
19104 +       if (do_put_zero && iinfo->ii_btop >= 0) {
19105 +               for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot;
19106 +                    bindex++) {
19107 +                       struct inode *h_i;
19108 +
19109 +                       h_i = au_hinode(iinfo, bindex)->hi_inode;
19110 +                       if (h_i
19111 +                           && !h_i->i_nlink
19112 +                           && !(h_i->i_state & I_LINKABLE))
19113 +                               au_set_h_iptr(inode, bindex, NULL, 0);
19114 +               }
19115 +       }
19116 +
19117 +       iinfo->ii_btop = -1;
19118 +       iinfo->ii_bbot = -1;
19119 +       bbot = au_sbbot(inode->i_sb);
19120 +       for (bindex = 0; bindex <= bbot; bindex++)
19121 +               if (au_hinode(iinfo, bindex)->hi_inode) {
19122 +                       iinfo->ii_btop = bindex;
19123 +                       break;
19124 +               }
19125 +       if (iinfo->ii_btop >= 0)
19126 +               for (bindex = bbot; bindex >= iinfo->ii_btop; bindex--)
19127 +                       if (au_hinode(iinfo, bindex)->hi_inode) {
19128 +                               iinfo->ii_bbot = bindex;
19129 +                               break;
19130 +                       }
19131 +       AuDebugOn(iinfo->ii_btop > iinfo->ii_bbot);
19132 +}
19133 +
19134 +/* ---------------------------------------------------------------------- */
19135 +
19136 +void au_icntnr_init_once(void *_c)
19137 +{
19138 +       struct au_icntnr *c = _c;
19139 +       struct au_iinfo *iinfo = &c->iinfo;
19140 +
19141 +       spin_lock_init(&iinfo->ii_generation.ig_spin);
19142 +       au_rw_init(&iinfo->ii_rwsem);
19143 +       inode_init_once(&c->vfs_inode);
19144 +}
19145 +
19146 +void au_hinode_init(struct au_hinode *hinode)
19147 +{
19148 +       hinode->hi_inode = NULL;
19149 +       hinode->hi_id = -1;
19150 +       au_hn_init(hinode);
19151 +       hinode->hi_whdentry = NULL;
19152 +}
19153 +
19154 +int au_iinfo_init(struct inode *inode)
19155 +{
19156 +       struct au_iinfo *iinfo;
19157 +       struct super_block *sb;
19158 +       struct au_hinode *hi;
19159 +       int nbr, i;
19160 +
19161 +       sb = inode->i_sb;
19162 +       iinfo = &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
19163 +       nbr = au_sbbot(sb) + 1;
19164 +       if (unlikely(nbr <= 0))
19165 +               nbr = 1;
19166 +       hi = kmalloc_array(nbr, sizeof(*iinfo->ii_hinode), GFP_NOFS);
19167 +       if (hi) {
19168 +               au_lcnt_inc(&au_sbi(sb)->si_ninodes);
19169 +
19170 +               iinfo->ii_hinode = hi;
19171 +               for (i = 0; i < nbr; i++, hi++)
19172 +                       au_hinode_init(hi);
19173 +
19174 +               iinfo->ii_generation.ig_generation = au_sigen(sb);
19175 +               iinfo->ii_btop = -1;
19176 +               iinfo->ii_bbot = -1;
19177 +               iinfo->ii_vdir = NULL;
19178 +               return 0;
19179 +       }
19180 +       return -ENOMEM;
19181 +}
19182 +
19183 +int au_hinode_realloc(struct au_iinfo *iinfo, int nbr, int may_shrink)
19184 +{
19185 +       int err, i;
19186 +       struct au_hinode *hip;
19187 +
19188 +       AuRwMustWriteLock(&iinfo->ii_rwsem);
19189 +
19190 +       err = -ENOMEM;
19191 +       hip = au_krealloc(iinfo->ii_hinode, sizeof(*hip) * nbr, GFP_NOFS,
19192 +                         may_shrink);
19193 +       if (hip) {
19194 +               iinfo->ii_hinode = hip;
19195 +               i = iinfo->ii_bbot + 1;
19196 +               hip += i;
19197 +               for (; i < nbr; i++, hip++)
19198 +                       au_hinode_init(hip);
19199 +               err = 0;
19200 +       }
19201 +
19202 +       return err;
19203 +}
19204 +
19205 +void au_iinfo_fin(struct inode *inode)
19206 +{
19207 +       struct au_iinfo *iinfo;
19208 +       struct au_hinode *hi;
19209 +       struct super_block *sb;
19210 +       aufs_bindex_t bindex, bbot;
19211 +       const unsigned char unlinked = !inode->i_nlink;
19212 +
19213 +       AuDebugOn(au_is_bad_inode(inode));
19214 +
19215 +       sb = inode->i_sb;
19216 +       au_lcnt_dec(&au_sbi(sb)->si_ninodes);
19217 +       if (si_pid_test(sb))
19218 +               au_xino_delete_inode(inode, unlinked);
19219 +       else {
19220 +               /*
19221 +                * it is safe to hide the dependency between sbinfo and
19222 +                * sb->s_umount.
19223 +                */
19224 +               lockdep_off();
19225 +               si_noflush_read_lock(sb);
19226 +               au_xino_delete_inode(inode, unlinked);
19227 +               si_read_unlock(sb);
19228 +               lockdep_on();
19229 +       }
19230 +
19231 +       iinfo = au_ii(inode);
19232 +       if (iinfo->ii_vdir)
19233 +               au_vdir_free(iinfo->ii_vdir);
19234 +
19235 +       bindex = iinfo->ii_btop;
19236 +       if (bindex >= 0) {
19237 +               hi = au_hinode(iinfo, bindex);
19238 +               bbot = iinfo->ii_bbot;
19239 +               while (bindex++ <= bbot) {
19240 +                       if (hi->hi_inode)
19241 +                               au_hiput(hi);
19242 +                       hi++;
19243 +               }
19244 +       }
19245 +       au_kfree_rcu(iinfo->ii_hinode);
19246 +       AuRwDestroy(&iinfo->ii_rwsem);
19247 +}
19248 diff -urN /usr/share/empty/fs/aufs/inode.c linux/fs/aufs/inode.c
19249 --- /usr/share/empty/fs/aufs/inode.c    1970-01-01 01:00:00.000000000 +0100
19250 +++ linux/fs/aufs/inode.c       2022-03-21 14:49:05.726633010 +0100
19251 @@ -0,0 +1,531 @@
19252 +// SPDX-License-Identifier: GPL-2.0
19253 +/*
19254 + * Copyright (C) 2005-2021 Junjiro R. Okajima
19255 + *
19256 + * This program, aufs is free software; you can redistribute it and/or modify
19257 + * it under the terms of the GNU General Public License as published by
19258 + * the Free Software Foundation; either version 2 of the License, or
19259 + * (at your option) any later version.
19260 + *
19261 + * This program is distributed in the hope that it will be useful,
19262 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
19263 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19264 + * GNU General Public License for more details.
19265 + *
19266 + * You should have received a copy of the GNU General Public License
19267 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19268 + */
19269 +
19270 +/*
19271 + * inode functions
19272 + */
19273 +
19274 +#include <linux/iversion.h>
19275 +#include "aufs.h"
19276 +
19277 +struct inode *au_igrab(struct inode *inode)
19278 +{
19279 +       if (inode) {
19280 +               AuDebugOn(!atomic_read(&inode->i_count));
19281 +               ihold(inode);
19282 +       }
19283 +       return inode;
19284 +}
19285 +
19286 +static void au_refresh_hinode_attr(struct inode *inode, int do_version)
19287 +{
19288 +       au_cpup_attr_all(inode, /*force*/0);
19289 +       au_update_iigen(inode, /*half*/1);
19290 +       if (do_version)
19291 +               inode_inc_iversion(inode);
19292 +}
19293 +
19294 +static int au_ii_refresh(struct inode *inode, int *update)
19295 +{
19296 +       int err, e, nbr;
19297 +       umode_t type;
19298 +       aufs_bindex_t bindex, new_bindex;
19299 +       struct super_block *sb;
19300 +       struct au_iinfo *iinfo;
19301 +       struct au_hinode *p, *q, tmp;
19302 +
19303 +       AuDebugOn(au_is_bad_inode(inode));
19304 +       IiMustWriteLock(inode);
19305 +
19306 +       *update = 0;
19307 +       sb = inode->i_sb;
19308 +       nbr = au_sbbot(sb) + 1;
19309 +       type = inode->i_mode & S_IFMT;
19310 +       iinfo = au_ii(inode);
19311 +       err = au_hinode_realloc(iinfo, nbr, /*may_shrink*/0);
19312 +       if (unlikely(err))
19313 +               goto out;
19314 +
19315 +       AuDebugOn(iinfo->ii_btop < 0);
19316 +       p = au_hinode(iinfo, iinfo->ii_btop);
19317 +       for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot;
19318 +            bindex++, p++) {
19319 +               if (!p->hi_inode)
19320 +                       continue;
19321 +
19322 +               AuDebugOn(type != (p->hi_inode->i_mode & S_IFMT));
19323 +               new_bindex = au_br_index(sb, p->hi_id);
19324 +               if (new_bindex == bindex)
19325 +                       continue;
19326 +
19327 +               if (new_bindex < 0) {
19328 +                       *update = 1;
19329 +                       au_hiput(p);
19330 +                       p->hi_inode = NULL;
19331 +                       continue;
19332 +               }
19333 +
19334 +               if (new_bindex < iinfo->ii_btop)
19335 +                       iinfo->ii_btop = new_bindex;
19336 +               if (iinfo->ii_bbot < new_bindex)
19337 +                       iinfo->ii_bbot = new_bindex;
19338 +               /* swap two lower inode, and loop again */
19339 +               q = au_hinode(iinfo, new_bindex);
19340 +               tmp = *q;
19341 +               *q = *p;
19342 +               *p = tmp;
19343 +               if (tmp.hi_inode) {
19344 +                       bindex--;
19345 +                       p--;
19346 +               }
19347 +       }
19348 +       au_update_ibrange(inode, /*do_put_zero*/0);
19349 +       au_hinode_realloc(iinfo, nbr, /*may_shrink*/1); /* harmless if err */
19350 +       e = au_dy_irefresh(inode);
19351 +       if (unlikely(e && !err))
19352 +               err = e;
19353 +
19354 +out:
19355 +       AuTraceErr(err);
19356 +       return err;
19357 +}
19358 +
19359 +void au_refresh_iop(struct inode *inode, int force_getattr)
19360 +{
19361 +       int type;
19362 +       struct au_sbinfo *sbi = au_sbi(inode->i_sb);
19363 +       const struct inode_operations *iop
19364 +               = force_getattr ? aufs_iop : sbi->si_iop_array;
19365 +
19366 +       if (inode->i_op == iop)
19367 +               return;
19368 +
19369 +       switch (inode->i_mode & S_IFMT) {
19370 +       case S_IFDIR:
19371 +               type = AuIop_DIR;
19372 +               break;
19373 +       case S_IFLNK:
19374 +               type = AuIop_SYMLINK;
19375 +               break;
19376 +       default:
19377 +               type = AuIop_OTHER;
19378 +               break;
19379 +       }
19380 +
19381 +       inode->i_op = iop + type;
19382 +       /* unnecessary smp_wmb() */
19383 +}
19384 +
19385 +int au_refresh_hinode_self(struct inode *inode)
19386 +{
19387 +       int err, update;
19388 +
19389 +       err = au_ii_refresh(inode, &update);
19390 +       if (!err)
19391 +               au_refresh_hinode_attr(inode, update && S_ISDIR(inode->i_mode));
19392 +
19393 +       AuTraceErr(err);
19394 +       return err;
19395 +}
19396 +
19397 +int au_refresh_hinode(struct inode *inode, struct dentry *dentry)
19398 +{
19399 +       int err, e, update;
19400 +       unsigned int flags;
19401 +       umode_t mode;
19402 +       aufs_bindex_t bindex, bbot;
19403 +       unsigned char isdir;
19404 +       struct au_hinode *p;
19405 +       struct au_iinfo *iinfo;
19406 +
19407 +       err = au_ii_refresh(inode, &update);
19408 +       if (unlikely(err))
19409 +               goto out;
19410 +
19411 +       update = 0;
19412 +       iinfo = au_ii(inode);
19413 +       p = au_hinode(iinfo, iinfo->ii_btop);
19414 +       mode = (inode->i_mode & S_IFMT);
19415 +       isdir = S_ISDIR(mode);
19416 +       flags = au_hi_flags(inode, isdir);
19417 +       bbot = au_dbbot(dentry);
19418 +       for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++) {
19419 +               struct inode *h_i, *h_inode;
19420 +               struct dentry *h_d;
19421 +
19422 +               h_d = au_h_dptr(dentry, bindex);
19423 +               if (!h_d || d_is_negative(h_d))
19424 +                       continue;
19425 +
19426 +               h_inode = d_inode(h_d);
19427 +               AuDebugOn(mode != (h_inode->i_mode & S_IFMT));
19428 +               if (iinfo->ii_btop <= bindex && bindex <= iinfo->ii_bbot) {
19429 +                       h_i = au_h_iptr(inode, bindex);
19430 +                       if (h_i) {
19431 +                               if (h_i == h_inode)
19432 +                                       continue;
19433 +                               err = -EIO;
19434 +                               break;
19435 +                       }
19436 +               }
19437 +               if (bindex < iinfo->ii_btop)
19438 +                       iinfo->ii_btop = bindex;
19439 +               if (iinfo->ii_bbot < bindex)
19440 +                       iinfo->ii_bbot = bindex;
19441 +               au_set_h_iptr(inode, bindex, au_igrab(h_inode), flags);
19442 +               update = 1;
19443 +       }
19444 +       au_update_ibrange(inode, /*do_put_zero*/0);
19445 +       e = au_dy_irefresh(inode);
19446 +       if (unlikely(e && !err))
19447 +               err = e;
19448 +       if (!err)
19449 +               au_refresh_hinode_attr(inode, update && isdir);
19450 +
19451 +out:
19452 +       AuTraceErr(err);
19453 +       return err;
19454 +}
19455 +
19456 +static int set_inode(struct inode *inode, struct dentry *dentry)
19457 +{
19458 +       int err;
19459 +       unsigned int flags;
19460 +       umode_t mode;
19461 +       aufs_bindex_t bindex, btop, btail;
19462 +       unsigned char isdir;
19463 +       struct dentry *h_dentry;
19464 +       struct inode *h_inode;
19465 +       struct au_iinfo *iinfo;
19466 +       const struct inode_operations *iop;
19467 +
19468 +       IiMustWriteLock(inode);
19469 +
19470 +       err = 0;
19471 +       isdir = 0;
19472 +       iop = au_sbi(inode->i_sb)->si_iop_array;
19473 +       btop = au_dbtop(dentry);
19474 +       h_dentry = au_h_dptr(dentry, btop);
19475 +       h_inode = d_inode(h_dentry);
19476 +       mode = h_inode->i_mode;
19477 +       switch (mode & S_IFMT) {
19478 +       case S_IFREG:
19479 +               btail = au_dbtail(dentry);
19480 +               inode->i_op = iop + AuIop_OTHER;
19481 +               inode->i_fop = &aufs_file_fop;
19482 +               err = au_dy_iaop(inode, btop, h_inode);
19483 +               if (unlikely(err))
19484 +                       goto out;
19485 +               break;
19486 +       case S_IFDIR:
19487 +               isdir = 1;
19488 +               btail = au_dbtaildir(dentry);
19489 +               inode->i_op = iop + AuIop_DIR;
19490 +               inode->i_fop = &aufs_dir_fop;
19491 +               break;
19492 +       case S_IFLNK:
19493 +               btail = au_dbtail(dentry);
19494 +               inode->i_op = iop + AuIop_SYMLINK;
19495 +               break;
19496 +       case S_IFBLK:
19497 +       case S_IFCHR:
19498 +       case S_IFIFO:
19499 +       case S_IFSOCK:
19500 +               btail = au_dbtail(dentry);
19501 +               inode->i_op = iop + AuIop_OTHER;
19502 +               init_special_inode(inode, mode, h_inode->i_rdev);
19503 +               break;
19504 +       default:
19505 +               AuIOErr("Unknown file type 0%o\n", mode);
19506 +               err = -EIO;
19507 +               goto out;
19508 +       }
19509 +
19510 +       /* do not set hnotify for whiteouted dirs (SHWH mode) */
19511 +       flags = au_hi_flags(inode, isdir);
19512 +       if (au_opt_test(au_mntflags(dentry->d_sb), SHWH)
19513 +           && au_ftest_hi(flags, HNOTIFY)
19514 +           && dentry->d_name.len > AUFS_WH_PFX_LEN
19515 +           && !memcmp(dentry->d_name.name, AUFS_WH_PFX, AUFS_WH_PFX_LEN))
19516 +               au_fclr_hi(flags, HNOTIFY);
19517 +       iinfo = au_ii(inode);
19518 +       iinfo->ii_btop = btop;
19519 +       iinfo->ii_bbot = btail;
19520 +       for (bindex = btop; bindex <= btail; bindex++) {
19521 +               h_dentry = au_h_dptr(dentry, bindex);
19522 +               if (h_dentry)
19523 +                       au_set_h_iptr(inode, bindex,
19524 +                                     au_igrab(d_inode(h_dentry)), flags);
19525 +       }
19526 +       au_cpup_attr_all(inode, /*force*/1);
19527 +       /*
19528 +        * to force calling aufs_get_acl() every time,
19529 +        * do not call cache_no_acl() for aufs inode.
19530 +        */
19531 +
19532 +out:
19533 +       return err;
19534 +}
19535 +
19536 +/*
19537 + * successful returns with iinfo write_locked
19538 + * minus: errno
19539 + * zero: success, matched
19540 + * plus: no error, but unmatched
19541 + */
19542 +static int reval_inode(struct inode *inode, struct dentry *dentry)
19543 +{
19544 +       int err;
19545 +       unsigned int gen, igflags;
19546 +       aufs_bindex_t bindex, bbot;
19547 +       struct inode *h_inode, *h_dinode;
19548 +       struct dentry *h_dentry;
19549 +
19550 +       /*
19551 +        * before this function, if aufs got any iinfo lock, it must be only
19552 +        * one, the parent dir.
19553 +        * it can happen by UDBA and the obsoleted inode number.
19554 +        */
19555 +       err = -EIO;
19556 +       if (unlikely(inode->i_ino == parent_ino(dentry)))
19557 +               goto out;
19558 +
19559 +       err = 1;
19560 +       ii_write_lock_new_child(inode);
19561 +       h_dentry = au_h_dptr(dentry, au_dbtop(dentry));
19562 +       h_dinode = d_inode(h_dentry);
19563 +       bbot = au_ibbot(inode);
19564 +       for (bindex = au_ibtop(inode); bindex <= bbot; bindex++) {
19565 +               h_inode = au_h_iptr(inode, bindex);
19566 +               if (!h_inode || h_inode != h_dinode)
19567 +                       continue;
19568 +
19569 +               err = 0;
19570 +               gen = au_iigen(inode, &igflags);
19571 +               if (gen == au_digen(dentry)
19572 +                   && !au_ig_ftest(igflags, HALF_REFRESHED))
19573 +                       break;
19574 +
19575 +               /* fully refresh inode using dentry */
19576 +               err = au_refresh_hinode(inode, dentry);
19577 +               if (!err)
19578 +                       au_update_iigen(inode, /*half*/0);
19579 +               break;
19580 +       }
19581 +
19582 +       if (unlikely(err))
19583 +               ii_write_unlock(inode);
19584 +out:
19585 +       return err;
19586 +}
19587 +
19588 +int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
19589 +          unsigned int d_type, ino_t *ino)
19590 +{
19591 +       int err, idx;
19592 +       const int isnondir = d_type != DT_DIR;
19593 +
19594 +       /* prevent hardlinked inode number from race condition */
19595 +       if (isnondir) {
19596 +               err = au_xinondir_enter(sb, bindex, h_ino, &idx);
19597 +               if (unlikely(err))
19598 +                       goto out;
19599 +       }
19600 +
19601 +       err = au_xino_read(sb, bindex, h_ino, ino);
19602 +       if (unlikely(err))
19603 +               goto out_xinondir;
19604 +
19605 +       if (!*ino) {
19606 +               err = -EIO;
19607 +               *ino = au_xino_new_ino(sb);
19608 +               if (unlikely(!*ino))
19609 +                       goto out_xinondir;
19610 +               err = au_xino_write(sb, bindex, h_ino, *ino);
19611 +               if (unlikely(err))
19612 +                       goto out_xinondir;
19613 +       }
19614 +
19615 +out_xinondir:
19616 +       if (isnondir && idx >= 0)
19617 +               au_xinondir_leave(sb, bindex, h_ino, idx);
19618 +out:
19619 +       return err;
19620 +}
19621 +
19622 +/* successful returns with iinfo write_locked */
19623 +/* todo: return with unlocked? */
19624 +struct inode *au_new_inode(struct dentry *dentry, int must_new)
19625 +{
19626 +       struct inode *inode, *h_inode;
19627 +       struct dentry *h_dentry;
19628 +       struct super_block *sb;
19629 +       ino_t h_ino, ino;
19630 +       int err, idx, hlinked;
19631 +       aufs_bindex_t btop;
19632 +
19633 +       sb = dentry->d_sb;
19634 +       btop = au_dbtop(dentry);
19635 +       h_dentry = au_h_dptr(dentry, btop);
19636 +       h_inode = d_inode(h_dentry);
19637 +       h_ino = h_inode->i_ino;
19638 +       hlinked = !d_is_dir(h_dentry) && h_inode->i_nlink > 1;
19639 +
19640 +new_ino:
19641 +       /*
19642 +        * stop 'race'-ing between hardlinks under different
19643 +        * parents.
19644 +        */
19645 +       if (hlinked) {
19646 +               err = au_xinondir_enter(sb, btop, h_ino, &idx);
19647 +               inode = ERR_PTR(err);
19648 +               if (unlikely(err))
19649 +                       goto out;
19650 +       }
19651 +
19652 +       err = au_xino_read(sb, btop, h_ino, &ino);
19653 +       inode = ERR_PTR(err);
19654 +       if (unlikely(err))
19655 +               goto out_xinondir;
19656 +
19657 +       if (!ino) {
19658 +               ino = au_xino_new_ino(sb);
19659 +               if (unlikely(!ino)) {
19660 +                       inode = ERR_PTR(-EIO);
19661 +                       goto out_xinondir;
19662 +               }
19663 +       }
19664 +
19665 +       AuDbg("i%lu\n", (unsigned long)ino);
19666 +       inode = au_iget_locked(sb, ino);
19667 +       err = PTR_ERR(inode);
19668 +       if (IS_ERR(inode))
19669 +               goto out_xinondir;
19670 +
19671 +       AuDbg("%lx, new %d\n", inode->i_state, !!(inode->i_state & I_NEW));
19672 +       if (inode->i_state & I_NEW) {
19673 +               ii_write_lock_new_child(inode);
19674 +               err = set_inode(inode, dentry);
19675 +               if (!err) {
19676 +                       unlock_new_inode(inode);
19677 +                       goto out_xinondir; /* success */
19678 +               }
19679 +
19680 +               /*
19681 +                * iget_failed() calls iput(), but we need to call
19682 +                * ii_write_unlock() after iget_failed(). so dirty hack for
19683 +                * i_count.
19684 +                */
19685 +               atomic_inc(&inode->i_count);
19686 +               iget_failed(inode);
19687 +               ii_write_unlock(inode);
19688 +               au_xino_write(sb, btop, h_ino, /*ino*/0);
19689 +               /* ignore this error */
19690 +               goto out_iput;
19691 +       } else if (!must_new && !IS_DEADDIR(inode) && inode->i_nlink) {
19692 +               /*
19693 +                * horrible race condition between lookup, readdir and copyup
19694 +                * (or something).
19695 +                */
19696 +               if (hlinked && idx >= 0)
19697 +                       au_xinondir_leave(sb, btop, h_ino, idx);
19698 +               err = reval_inode(inode, dentry);
19699 +               if (unlikely(err < 0)) {
19700 +                       hlinked = 0;
19701 +                       goto out_iput;
19702 +               }
19703 +               if (!err)
19704 +                       goto out; /* success */
19705 +               else if (hlinked && idx >= 0) {
19706 +                       err = au_xinondir_enter(sb, btop, h_ino, &idx);
19707 +                       if (unlikely(err)) {
19708 +                               iput(inode);
19709 +                               inode = ERR_PTR(err);
19710 +                               goto out;
19711 +                       }
19712 +               }
19713 +       }
19714 +
19715 +       if (unlikely(au_test_fs_unique_ino(h_inode)))
19716 +               AuWarn1("Warning: Un-notified UDBA or repeatedly renamed dir,"
19717 +                       " b%d, %s, %pd, hi%lu, i%lu.\n",
19718 +                       btop, au_sbtype(h_dentry->d_sb), dentry,
19719 +                       (unsigned long)h_ino, (unsigned long)ino);
19720 +       ino = 0;
19721 +       err = au_xino_write(sb, btop, h_ino, /*ino*/0);
19722 +       if (!err) {
19723 +               iput(inode);
19724 +               if (hlinked && idx >= 0)
19725 +                       au_xinondir_leave(sb, btop, h_ino, idx);
19726 +               goto new_ino;
19727 +       }
19728 +
19729 +out_iput:
19730 +       iput(inode);
19731 +       inode = ERR_PTR(err);
19732 +out_xinondir:
19733 +       if (hlinked && idx >= 0)
19734 +               au_xinondir_leave(sb, btop, h_ino, idx);
19735 +out:
19736 +       return inode;
19737 +}
19738 +
19739 +/* ---------------------------------------------------------------------- */
19740 +
19741 +int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
19742 +              struct inode *inode)
19743 +{
19744 +       int err;
19745 +       struct inode *hi;
19746 +
19747 +       err = au_br_rdonly(au_sbr(sb, bindex));
19748 +
19749 +       /* pseudo-link after flushed may happen out of bounds */
19750 +       if (!err
19751 +           && inode
19752 +           && au_ibtop(inode) <= bindex
19753 +           && bindex <= au_ibbot(inode)) {
19754 +               /*
19755 +                * permission check is unnecessary since vfsub routine
19756 +                * will be called later
19757 +                */
19758 +               hi = au_h_iptr(inode, bindex);
19759 +               if (hi)
19760 +                       err = IS_IMMUTABLE(hi) ? -EROFS : 0;
19761 +       }
19762 +
19763 +       return err;
19764 +}
19765 +
19766 +int au_test_h_perm(struct user_namespace *h_userns, struct inode *h_inode,
19767 +                  int mask)
19768 +{
19769 +       if (uid_eq(current_fsuid(), GLOBAL_ROOT_UID))
19770 +               return 0;
19771 +       return inode_permission(h_userns, h_inode, mask);
19772 +}
19773 +
19774 +int au_test_h_perm_sio(struct user_namespace *h_userns, struct inode *h_inode,
19775 +                      int mask)
19776 +{
19777 +       if (au_test_nfs(h_inode->i_sb)
19778 +           && (mask & MAY_WRITE)
19779 +           && S_ISDIR(h_inode->i_mode))
19780 +               mask |= MAY_READ; /* force permission check */
19781 +       return au_test_h_perm(h_userns, h_inode, mask);
19782 +}
19783 diff -urN /usr/share/empty/fs/aufs/inode.h linux/fs/aufs/inode.h
19784 --- /usr/share/empty/fs/aufs/inode.h    1970-01-01 01:00:00.000000000 +0100
19785 +++ linux/fs/aufs/inode.h       2022-03-21 14:49:05.726633010 +0100
19786 @@ -0,0 +1,705 @@
19787 +/* SPDX-License-Identifier: GPL-2.0 */
19788 +/*
19789 + * Copyright (C) 2005-2021 Junjiro R. Okajima
19790 + *
19791 + * This program, aufs is free software; you can redistribute it and/or modify
19792 + * it under the terms of the GNU General Public License as published by
19793 + * the Free Software Foundation; either version 2 of the License, or
19794 + * (at your option) any later version.
19795 + *
19796 + * This program is distributed in the hope that it will be useful,
19797 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
19798 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19799 + * GNU General Public License for more details.
19800 + *
19801 + * You should have received a copy of the GNU General Public License
19802 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19803 + */
19804 +
19805 +/*
19806 + * inode operations
19807 + */
19808 +
19809 +#ifndef __AUFS_INODE_H__
19810 +#define __AUFS_INODE_H__
19811 +
19812 +#ifdef __KERNEL__
19813 +
19814 +#include <linux/fsnotify.h>
19815 +#include "rwsem.h"
19816 +
19817 +struct vfsmount;
19818 +
19819 +struct au_hnotify {
19820 +#ifdef CONFIG_AUFS_HNOTIFY
19821 +#ifdef CONFIG_AUFS_HFSNOTIFY
19822 +       /* never use fsnotify_add_vfsmount_mark() */
19823 +       struct fsnotify_mark            hn_mark;
19824 +#endif
19825 +       struct inode            *hn_aufs_inode; /* no get/put */
19826 +       struct rcu_head         rcu;
19827 +#endif
19828 +} ____cacheline_aligned_in_smp;
19829 +
19830 +struct au_hinode {
19831 +       struct inode            *hi_inode;
19832 +       aufs_bindex_t           hi_id;
19833 +#ifdef CONFIG_AUFS_HNOTIFY
19834 +       struct au_hnotify       *hi_notify;
19835 +#endif
19836 +
19837 +       /* reference to the copied-up whiteout with get/put */
19838 +       struct dentry           *hi_whdentry;
19839 +};
19840 +
19841 +/* ig_flags */
19842 +#define AuIG_HALF_REFRESHED            1
19843 +#define au_ig_ftest(flags, name)       ((flags) & AuIG_##name)
19844 +#define au_ig_fset(flags, name) \
19845 +       do { (flags) |= AuIG_##name; } while (0)
19846 +#define au_ig_fclr(flags, name) \
19847 +       do { (flags) &= ~AuIG_##name; } while (0)
19848 +
19849 +struct au_iigen {
19850 +       spinlock_t      ig_spin;
19851 +       __u32           ig_generation, ig_flags;
19852 +};
19853 +
19854 +struct au_vdir;
19855 +struct au_iinfo {
19856 +       struct au_iigen         ii_generation;
19857 +       struct super_block      *ii_hsb1;       /* no get/put */
19858 +
19859 +       struct au_rwsem         ii_rwsem;
19860 +       aufs_bindex_t           ii_btop, ii_bbot;
19861 +       __u32                   ii_higen;
19862 +       struct au_hinode        *ii_hinode;
19863 +       struct au_vdir          *ii_vdir;
19864 +};
19865 +
19866 +struct au_icntnr {
19867 +       struct au_iinfo         iinfo;
19868 +       struct inode            vfs_inode;
19869 +       struct hlist_bl_node    plink;
19870 +       struct rcu_head         rcu;
19871 +} ____cacheline_aligned_in_smp;
19872 +
19873 +/* au_pin flags */
19874 +#define AuPin_DI_LOCKED                1
19875 +#define AuPin_MNT_WRITE                (1 << 1)
19876 +#define au_ftest_pin(flags, name)      ((flags) & AuPin_##name)
19877 +#define au_fset_pin(flags, name) \
19878 +       do { (flags) |= AuPin_##name; } while (0)
19879 +#define au_fclr_pin(flags, name) \
19880 +       do { (flags) &= ~AuPin_##name; } while (0)
19881 +
19882 +struct au_pin {
19883 +       /* input */
19884 +       struct dentry *dentry;
19885 +       unsigned int udba;
19886 +       unsigned char lsc_di, lsc_hi, flags;
19887 +       aufs_bindex_t bindex;
19888 +
19889 +       /* output */
19890 +       struct dentry *parent;
19891 +       struct au_hinode *hdir;
19892 +       struct vfsmount *h_mnt;
19893 +
19894 +       /* temporary unlock/relock for copyup */
19895 +       struct dentry *h_dentry, *h_parent;
19896 +       struct au_branch *br;
19897 +       struct task_struct *task;
19898 +};
19899 +
19900 +void au_pin_hdir_unlock(struct au_pin *p);
19901 +int au_pin_hdir_lock(struct au_pin *p);
19902 +int au_pin_hdir_relock(struct au_pin *p);
19903 +void au_pin_hdir_acquire_nest(struct au_pin *p);
19904 +void au_pin_hdir_release(struct au_pin *p);
19905 +
19906 +/* ---------------------------------------------------------------------- */
19907 +
19908 +static inline struct au_iinfo *au_ii(struct inode *inode)
19909 +{
19910 +       BUG_ON(is_bad_inode(inode));
19911 +       return &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
19912 +}
19913 +
19914 +/* ---------------------------------------------------------------------- */
19915 +
19916 +/* inode.c */
19917 +struct inode *au_igrab(struct inode *inode);
19918 +void au_refresh_iop(struct inode *inode, int force_getattr);
19919 +int au_refresh_hinode_self(struct inode *inode);
19920 +int au_refresh_hinode(struct inode *inode, struct dentry *dentry);
19921 +int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
19922 +          unsigned int d_type, ino_t *ino);
19923 +struct inode *au_new_inode(struct dentry *dentry, int must_new);
19924 +int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
19925 +              struct inode *inode);
19926 +int au_test_h_perm(struct user_namespace *h_userns, struct inode *h_inode,
19927 +                  int mask);
19928 +int au_test_h_perm_sio(struct user_namespace *h_userns, struct inode *h_inode,
19929 +                      int mask);
19930 +
19931 +static inline int au_wh_ino(struct super_block *sb, aufs_bindex_t bindex,
19932 +                           ino_t h_ino, unsigned int d_type, ino_t *ino)
19933 +{
19934 +#ifdef CONFIG_AUFS_SHWH
19935 +       return au_ino(sb, bindex, h_ino, d_type, ino);
19936 +#else
19937 +       return 0;
19938 +#endif
19939 +}
19940 +
19941 +/* i_op.c */
19942 +enum {
19943 +       AuIop_SYMLINK,
19944 +       AuIop_DIR,
19945 +       AuIop_OTHER,
19946 +       AuIop_Last
19947 +};
19948 +extern struct inode_operations aufs_iop[AuIop_Last], /* not const */
19949 +       aufs_iop_nogetattr[AuIop_Last];
19950 +
19951 +/* au_wr_dir flags */
19952 +#define AuWrDir_ADD_ENTRY      1
19953 +#define AuWrDir_ISDIR          (1 << 1)
19954 +#define AuWrDir_TMPFILE                (1 << 2)
19955 +#define au_ftest_wrdir(flags, name)    ((flags) & AuWrDir_##name)
19956 +#define au_fset_wrdir(flags, name) \
19957 +       do { (flags) |= AuWrDir_##name; } while (0)
19958 +#define au_fclr_wrdir(flags, name) \
19959 +       do { (flags) &= ~AuWrDir_##name; } while (0)
19960 +
19961 +struct au_wr_dir_args {
19962 +       aufs_bindex_t force_btgt;
19963 +       unsigned char flags;
19964 +};
19965 +int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
19966 +             struct au_wr_dir_args *args);
19967 +
19968 +struct dentry *au_pinned_h_parent(struct au_pin *pin);
19969 +void au_pin_init(struct au_pin *pin, struct dentry *dentry,
19970 +                aufs_bindex_t bindex, int lsc_di, int lsc_hi,
19971 +                unsigned int udba, unsigned char flags);
19972 +int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
19973 +          unsigned int udba, unsigned char flags) __must_check;
19974 +int au_do_pin(struct au_pin *pin) __must_check;
19975 +void au_unpin(struct au_pin *pin);
19976 +int au_reval_for_attr(struct dentry *dentry, unsigned int sigen);
19977 +
19978 +#define AuIcpup_DID_CPUP       1
19979 +#define au_ftest_icpup(flags, name)    ((flags) & AuIcpup_##name)
19980 +#define au_fset_icpup(flags, name) \
19981 +       do { (flags) |= AuIcpup_##name; } while (0)
19982 +#define au_fclr_icpup(flags, name) \
19983 +       do { (flags) &= ~AuIcpup_##name; } while (0)
19984 +
19985 +struct au_icpup_args {
19986 +       unsigned char flags;
19987 +       unsigned char pin_flags;
19988 +       aufs_bindex_t btgt;
19989 +       unsigned int udba;
19990 +       struct au_pin pin;
19991 +       struct path h_path;
19992 +       struct inode *h_inode;
19993 +};
19994 +
19995 +int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
19996 +                    struct au_icpup_args *a);
19997 +
19998 +int au_h_path_getattr(struct dentry *dentry, struct inode *inode, int force,
19999 +                     struct path *h_path, int locked);
20000 +
20001 +/* i_op_add.c */
20002 +int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
20003 +              struct dentry *h_parent, int isdir);
20004 +int aufs_mknod(struct user_namespace *userns, struct inode *dir,
20005 +              struct dentry *dentry, umode_t mode, dev_t dev);
20006 +int aufs_symlink(struct user_namespace *userns, struct inode *dir,
20007 +                struct dentry *dentry, const char *symname);
20008 +int aufs_create(struct user_namespace *userns, struct inode *dir,
20009 +               struct dentry *dentry, umode_t mode, bool want_excl);
20010 +struct vfsub_aopen_args;
20011 +int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
20012 +                      struct vfsub_aopen_args *args);
20013 +int aufs_tmpfile(struct user_namespace *userns, struct inode *dir,
20014 +                struct dentry *dentry, umode_t mode);
20015 +int aufs_link(struct dentry *src_dentry, struct inode *dir,
20016 +             struct dentry *dentry);
20017 +int aufs_mkdir(struct user_namespace *userns, struct inode *dir,
20018 +              struct dentry *dentry, umode_t mode);
20019 +
20020 +/* i_op_del.c */
20021 +int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup);
20022 +int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
20023 +              struct dentry *h_parent, int isdir);
20024 +int aufs_unlink(struct inode *dir, struct dentry *dentry);
20025 +int aufs_rmdir(struct inode *dir, struct dentry *dentry);
20026 +
20027 +/* i_op_ren.c */
20028 +int au_wbr(struct dentry *dentry, aufs_bindex_t btgt);
20029 +int aufs_rename(struct user_namespace *userns,
20030 +               struct inode *_src_dir, struct dentry *_src_dentry,
20031 +               struct inode *_dst_dir, struct dentry *_dst_dentry,
20032 +               unsigned int _flags);
20033 +
20034 +/* iinfo.c */
20035 +struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex);
20036 +void au_hiput(struct au_hinode *hinode);
20037 +void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
20038 +                 struct dentry *h_wh);
20039 +unsigned int au_hi_flags(struct inode *inode, int isdir);
20040 +
20041 +/* hinode flags */
20042 +#define AuHi_XINO      1
20043 +#define AuHi_HNOTIFY   (1 << 1)
20044 +#define au_ftest_hi(flags, name)       ((flags) & AuHi_##name)
20045 +#define au_fset_hi(flags, name) \
20046 +       do { (flags) |= AuHi_##name; } while (0)
20047 +#define au_fclr_hi(flags, name) \
20048 +       do { (flags) &= ~AuHi_##name; } while (0)
20049 +
20050 +#ifndef CONFIG_AUFS_HNOTIFY
20051 +#undef AuHi_HNOTIFY
20052 +#define AuHi_HNOTIFY   0
20053 +#endif
20054 +
20055 +void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
20056 +                  struct inode *h_inode, unsigned int flags);
20057 +
20058 +void au_update_iigen(struct inode *inode, int half);
20059 +void au_update_ibrange(struct inode *inode, int do_put_zero);
20060 +
20061 +void au_icntnr_init_once(void *_c);
20062 +void au_hinode_init(struct au_hinode *hinode);
20063 +int au_iinfo_init(struct inode *inode);
20064 +void au_iinfo_fin(struct inode *inode);
20065 +int au_hinode_realloc(struct au_iinfo *iinfo, int nbr, int may_shrink);
20066 +
20067 +#ifdef CONFIG_PROC_FS
20068 +/* plink.c */
20069 +int au_plink_maint(struct super_block *sb, int flags);
20070 +struct au_sbinfo;
20071 +void au_plink_maint_leave(struct au_sbinfo *sbinfo);
20072 +int au_plink_maint_enter(struct super_block *sb);
20073 +#ifdef CONFIG_AUFS_DEBUG
20074 +void au_plink_list(struct super_block *sb);
20075 +#else
20076 +AuStubVoid(au_plink_list, struct super_block *sb)
20077 +#endif
20078 +int au_plink_test(struct inode *inode);
20079 +struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex);
20080 +void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
20081 +                    struct dentry *h_dentry);
20082 +void au_plink_put(struct super_block *sb, int verbose);
20083 +void au_plink_clean(struct super_block *sb, int verbose);
20084 +void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id);
20085 +#else
20086 +AuStubInt0(au_plink_maint, struct super_block *sb, int flags);
20087 +AuStubVoid(au_plink_maint_leave, struct au_sbinfo *sbinfo);
20088 +AuStubInt0(au_plink_maint_enter, struct super_block *sb);
20089 +AuStubVoid(au_plink_list, struct super_block *sb);
20090 +AuStubInt0(au_plink_test, struct inode *inode);
20091 +AuStub(struct dentry *, au_plink_lkup, return NULL,
20092 +       struct inode *inode, aufs_bindex_t bindex);
20093 +AuStubVoid(au_plink_append, struct inode *inode, aufs_bindex_t bindex,
20094 +          struct dentry *h_dentry);
20095 +AuStubVoid(au_plink_put, struct super_block *sb, int verbose);
20096 +AuStubVoid(au_plink_clean, struct super_block *sb, int verbose);
20097 +AuStubVoid(au_plink_half_refresh, struct super_block *sb, aufs_bindex_t br_id);
20098 +#endif /* CONFIG_PROC_FS */
20099 +
20100 +#ifdef CONFIG_AUFS_XATTR
20101 +/* xattr.c */
20102 +int au_cpup_xattr(struct path *h_dst, struct path *h_src, int ignore_flags,
20103 +                 unsigned int verbose);
20104 +ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size);
20105 +void au_xattr_init(struct super_block *sb);
20106 +#else
20107 +AuStubInt0(au_cpup_xattr, struct path *h_dst, struct path *h_src,
20108 +          int ignore_flags, unsigned int verbose);
20109 +AuStubVoid(au_xattr_init, struct super_block *sb);
20110 +#endif
20111 +
20112 +#ifdef CONFIG_FS_POSIX_ACL
20113 +struct posix_acl *aufs_get_acl(struct inode *inode, int type, bool rcu);
20114 +int aufs_set_acl(struct user_namespace *userns, struct inode *inode,
20115 +                struct posix_acl *acl, int type);
20116 +#endif
20117 +
20118 +#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
20119 +enum {
20120 +       AU_XATTR_SET,
20121 +       AU_ACL_SET
20122 +};
20123 +
20124 +struct au_sxattr {
20125 +       int type;
20126 +       union {
20127 +               struct {
20128 +                       const char      *name;
20129 +                       const void      *value;
20130 +                       size_t          size;
20131 +                       int             flags;
20132 +               } set;
20133 +               struct {
20134 +                       struct posix_acl *acl;
20135 +                       int             type;
20136 +               } acl_set;
20137 +       } u;
20138 +};
20139 +ssize_t au_sxattr(struct dentry *dentry, struct inode *inode,
20140 +                 struct au_sxattr *arg);
20141 +#endif
20142 +
20143 +/* ---------------------------------------------------------------------- */
20144 +
20145 +/* lock subclass for iinfo */
20146 +enum {
20147 +       AuLsc_II_CHILD,         /* child first */
20148 +       AuLsc_II_CHILD2,        /* rename(2), link(2), and cpup at hnotify */
20149 +       AuLsc_II_CHILD3,        /* copyup dirs */
20150 +       AuLsc_II_PARENT,        /* see AuLsc_I_PARENT in vfsub.h */
20151 +       AuLsc_II_PARENT2,
20152 +       AuLsc_II_PARENT3,       /* copyup dirs */
20153 +       AuLsc_II_NEW_CHILD
20154 +};
20155 +
20156 +/*
20157 + * ii_read_lock_child, ii_write_lock_child,
20158 + * ii_read_lock_child2, ii_write_lock_child2,
20159 + * ii_read_lock_child3, ii_write_lock_child3,
20160 + * ii_read_lock_parent, ii_write_lock_parent,
20161 + * ii_read_lock_parent2, ii_write_lock_parent2,
20162 + * ii_read_lock_parent3, ii_write_lock_parent3,
20163 + * ii_read_lock_new_child, ii_write_lock_new_child,
20164 + */
20165 +#define AuReadLockFunc(name, lsc) \
20166 +static inline void ii_read_lock_##name(struct inode *i) \
20167 +{ \
20168 +       au_rw_read_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
20169 +}
20170 +
20171 +#define AuWriteLockFunc(name, lsc) \
20172 +static inline void ii_write_lock_##name(struct inode *i) \
20173 +{ \
20174 +       au_rw_write_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
20175 +}
20176 +
20177 +#define AuRWLockFuncs(name, lsc) \
20178 +       AuReadLockFunc(name, lsc) \
20179 +       AuWriteLockFunc(name, lsc)
20180 +
20181 +AuRWLockFuncs(child, CHILD);
20182 +AuRWLockFuncs(child2, CHILD2);
20183 +AuRWLockFuncs(child3, CHILD3);
20184 +AuRWLockFuncs(parent, PARENT);
20185 +AuRWLockFuncs(parent2, PARENT2);
20186 +AuRWLockFuncs(parent3, PARENT3);
20187 +AuRWLockFuncs(new_child, NEW_CHILD);
20188 +
20189 +#undef AuReadLockFunc
20190 +#undef AuWriteLockFunc
20191 +#undef AuRWLockFuncs
20192 +
20193 +#define ii_read_unlock(i)      au_rw_read_unlock(&au_ii(i)->ii_rwsem)
20194 +#define ii_write_unlock(i)     au_rw_write_unlock(&au_ii(i)->ii_rwsem)
20195 +#define ii_downgrade_lock(i)   au_rw_dgrade_lock(&au_ii(i)->ii_rwsem)
20196 +
20197 +#define IiMustNoWaiters(i)     AuRwMustNoWaiters(&au_ii(i)->ii_rwsem)
20198 +#define IiMustAnyLock(i)       AuRwMustAnyLock(&au_ii(i)->ii_rwsem)
20199 +#define IiMustWriteLock(i)     AuRwMustWriteLock(&au_ii(i)->ii_rwsem)
20200 +
20201 +/* ---------------------------------------------------------------------- */
20202 +
20203 +static inline void au_icntnr_init(struct au_icntnr *c)
20204 +{
20205 +#ifdef CONFIG_AUFS_DEBUG
20206 +       c->vfs_inode.i_mode = 0;
20207 +#endif
20208 +}
20209 +
20210 +static inline unsigned int au_iigen(struct inode *inode, unsigned int *igflags)
20211 +{
20212 +       unsigned int gen;
20213 +       struct au_iinfo *iinfo;
20214 +       struct au_iigen *iigen;
20215 +
20216 +       iinfo = au_ii(inode);
20217 +       iigen = &iinfo->ii_generation;
20218 +       spin_lock(&iigen->ig_spin);
20219 +       if (igflags)
20220 +               *igflags = iigen->ig_flags;
20221 +       gen = iigen->ig_generation;
20222 +       spin_unlock(&iigen->ig_spin);
20223 +
20224 +       return gen;
20225 +}
20226 +
20227 +/* tiny test for inode number */
20228 +/* tmpfs generation is too rough */
20229 +static inline int au_test_higen(struct inode *inode, struct inode *h_inode)
20230 +{
20231 +       struct au_iinfo *iinfo;
20232 +
20233 +       iinfo = au_ii(inode);
20234 +       AuRwMustAnyLock(&iinfo->ii_rwsem);
20235 +       return !(iinfo->ii_hsb1 == h_inode->i_sb
20236 +                && iinfo->ii_higen == h_inode->i_generation);
20237 +}
20238 +
20239 +static inline void au_iigen_dec(struct inode *inode)
20240 +{
20241 +       struct au_iinfo *iinfo;
20242 +       struct au_iigen *iigen;
20243 +
20244 +       iinfo = au_ii(inode);
20245 +       iigen = &iinfo->ii_generation;
20246 +       spin_lock(&iigen->ig_spin);
20247 +       iigen->ig_generation--;
20248 +       spin_unlock(&iigen->ig_spin);
20249 +}
20250 +
20251 +static inline int au_iigen_test(struct inode *inode, unsigned int sigen)
20252 +{
20253 +       int err;
20254 +
20255 +       err = 0;
20256 +       if (unlikely(inode && au_iigen(inode, NULL) != sigen))
20257 +               err = -EIO;
20258 +
20259 +       return err;
20260 +}
20261 +
20262 +/* ---------------------------------------------------------------------- */
20263 +
20264 +static inline struct au_hinode *au_hinode(struct au_iinfo *iinfo,
20265 +                                         aufs_bindex_t bindex)
20266 +{
20267 +       return iinfo->ii_hinode + bindex;
20268 +}
20269 +
20270 +static inline int au_is_bad_inode(struct inode *inode)
20271 +{
20272 +       return !!(is_bad_inode(inode) || !au_hinode(au_ii(inode), 0));
20273 +}
20274 +
20275 +static inline aufs_bindex_t au_ii_br_id(struct inode *inode,
20276 +                                       aufs_bindex_t bindex)
20277 +{
20278 +       IiMustAnyLock(inode);
20279 +       return au_hinode(au_ii(inode), bindex)->hi_id;
20280 +}
20281 +
20282 +static inline aufs_bindex_t au_ibtop(struct inode *inode)
20283 +{
20284 +       IiMustAnyLock(inode);
20285 +       return au_ii(inode)->ii_btop;
20286 +}
20287 +
20288 +static inline aufs_bindex_t au_ibbot(struct inode *inode)
20289 +{
20290 +       IiMustAnyLock(inode);
20291 +       return au_ii(inode)->ii_bbot;
20292 +}
20293 +
20294 +static inline struct au_vdir *au_ivdir(struct inode *inode)
20295 +{
20296 +       IiMustAnyLock(inode);
20297 +       return au_ii(inode)->ii_vdir;
20298 +}
20299 +
20300 +static inline struct dentry *au_hi_wh(struct inode *inode, aufs_bindex_t bindex)
20301 +{
20302 +       IiMustAnyLock(inode);
20303 +       return au_hinode(au_ii(inode), bindex)->hi_whdentry;
20304 +}
20305 +
20306 +static inline void au_set_ibtop(struct inode *inode, aufs_bindex_t bindex)
20307 +{
20308 +       IiMustWriteLock(inode);
20309 +       au_ii(inode)->ii_btop = bindex;
20310 +}
20311 +
20312 +static inline void au_set_ibbot(struct inode *inode, aufs_bindex_t bindex)
20313 +{
20314 +       IiMustWriteLock(inode);
20315 +       au_ii(inode)->ii_bbot = bindex;
20316 +}
20317 +
20318 +static inline void au_set_ivdir(struct inode *inode, struct au_vdir *vdir)
20319 +{
20320 +       IiMustWriteLock(inode);
20321 +       au_ii(inode)->ii_vdir = vdir;
20322 +}
20323 +
20324 +static inline struct au_hinode *au_hi(struct inode *inode, aufs_bindex_t bindex)
20325 +{
20326 +       IiMustAnyLock(inode);
20327 +       return au_hinode(au_ii(inode), bindex);
20328 +}
20329 +
20330 +/* ---------------------------------------------------------------------- */
20331 +
20332 +static inline struct dentry *au_pinned_parent(struct au_pin *pin)
20333 +{
20334 +       if (pin)
20335 +               return pin->parent;
20336 +       return NULL;
20337 +}
20338 +
20339 +static inline struct inode *au_pinned_h_dir(struct au_pin *pin)
20340 +{
20341 +       if (pin && pin->hdir)
20342 +               return pin->hdir->hi_inode;
20343 +       return NULL;
20344 +}
20345 +
20346 +static inline struct au_hinode *au_pinned_hdir(struct au_pin *pin)
20347 +{
20348 +       if (pin)
20349 +               return pin->hdir;
20350 +       return NULL;
20351 +}
20352 +
20353 +static inline void au_pin_set_dentry(struct au_pin *pin, struct dentry *dentry)
20354 +{
20355 +       if (pin)
20356 +               pin->dentry = dentry;
20357 +}
20358 +
20359 +static inline void au_pin_set_parent_lflag(struct au_pin *pin,
20360 +                                          unsigned char lflag)
20361 +{
20362 +       if (pin) {
20363 +               if (lflag)
20364 +                       au_fset_pin(pin->flags, DI_LOCKED);
20365 +               else
20366 +                       au_fclr_pin(pin->flags, DI_LOCKED);
20367 +       }
20368 +}
20369 +
20370 +#if 0 /* reserved */
20371 +static inline void au_pin_set_parent(struct au_pin *pin, struct dentry *parent)
20372 +{
20373 +       if (pin) {
20374 +               dput(pin->parent);
20375 +               pin->parent = dget(parent);
20376 +       }
20377 +}
20378 +#endif
20379 +
20380 +/* ---------------------------------------------------------------------- */
20381 +
20382 +struct au_branch;
20383 +#ifdef CONFIG_AUFS_HNOTIFY
20384 +struct au_hnotify_op {
20385 +       void (*ctl)(struct au_hinode *hinode, int do_set);
20386 +       int (*alloc)(struct au_hinode *hinode);
20387 +
20388 +       /*
20389 +        * if it returns true, the caller should free hinode->hi_notify,
20390 +        * otherwise ->free() frees it.
20391 +        */
20392 +       int (*free)(struct au_hinode *hinode,
20393 +                   struct au_hnotify *hn) __must_check;
20394 +
20395 +       void (*fin)(void);
20396 +       int (*init)(void);
20397 +
20398 +       int (*reset_br)(unsigned int udba, struct au_branch *br, int perm);
20399 +       void (*fin_br)(struct au_branch *br);
20400 +       int (*init_br)(struct au_branch *br, int perm);
20401 +};
20402 +
20403 +/* hnotify.c */
20404 +int au_hn_alloc(struct au_hinode *hinode, struct inode *inode);
20405 +void au_hn_free(struct au_hinode *hinode);
20406 +void au_hn_ctl(struct au_hinode *hinode, int do_set);
20407 +void au_hn_reset(struct inode *inode, unsigned int flags);
20408 +int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
20409 +              const struct qstr *h_child_qstr, struct inode *h_child_inode);
20410 +int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm);
20411 +int au_hnotify_init_br(struct au_branch *br, int perm);
20412 +void au_hnotify_fin_br(struct au_branch *br);
20413 +int __init au_hnotify_init(void);
20414 +void au_hnotify_fin(void);
20415 +
20416 +/* hfsnotify.c */
20417 +extern const struct au_hnotify_op au_hnotify_op;
20418 +
20419 +static inline
20420 +void au_hn_init(struct au_hinode *hinode)
20421 +{
20422 +       hinode->hi_notify = NULL;
20423 +}
20424 +
20425 +static inline struct au_hnotify *au_hn(struct au_hinode *hinode)
20426 +{
20427 +       return hinode->hi_notify;
20428 +}
20429 +
20430 +#else
20431 +AuStub(int, au_hn_alloc, return -EOPNOTSUPP,
20432 +       struct au_hinode *hinode __maybe_unused,
20433 +       struct inode *inode __maybe_unused)
20434 +AuStub(struct au_hnotify *, au_hn, return NULL, struct au_hinode *hinode)
20435 +AuStubVoid(au_hn_free, struct au_hinode *hinode __maybe_unused)
20436 +AuStubVoid(au_hn_ctl, struct au_hinode *hinode __maybe_unused,
20437 +          int do_set __maybe_unused)
20438 +AuStubVoid(au_hn_reset, struct inode *inode __maybe_unused,
20439 +          unsigned int flags __maybe_unused)
20440 +AuStubInt0(au_hnotify_reset_br, unsigned int udba __maybe_unused,
20441 +          struct au_branch *br __maybe_unused,
20442 +          int perm __maybe_unused)
20443 +AuStubInt0(au_hnotify_init_br, struct au_branch *br __maybe_unused,
20444 +          int perm __maybe_unused)
20445 +AuStubVoid(au_hnotify_fin_br, struct au_branch *br __maybe_unused)
20446 +AuStubInt0(__init au_hnotify_init, void)
20447 +AuStubVoid(au_hnotify_fin, void)
20448 +AuStubVoid(au_hn_init, struct au_hinode *hinode __maybe_unused)
20449 +#endif /* CONFIG_AUFS_HNOTIFY */
20450 +
20451 +static inline void au_hn_suspend(struct au_hinode *hdir)
20452 +{
20453 +       au_hn_ctl(hdir, /*do_set*/0);
20454 +}
20455 +
20456 +static inline void au_hn_resume(struct au_hinode *hdir)
20457 +{
20458 +       au_hn_ctl(hdir, /*do_set*/1);
20459 +}
20460 +
20461 +static inline void au_hn_inode_lock(struct au_hinode *hdir)
20462 +{
20463 +       inode_lock(hdir->hi_inode);
20464 +       au_hn_suspend(hdir);
20465 +}
20466 +
20467 +static inline void au_hn_inode_lock_nested(struct au_hinode *hdir,
20468 +                                         unsigned int sc __maybe_unused)
20469 +{
20470 +       inode_lock_nested(hdir->hi_inode, sc);
20471 +       au_hn_suspend(hdir);
20472 +}
20473 +
20474 +#if 0 /* unused */
20475 +#include "vfsub.h"
20476 +static inline void au_hn_inode_lock_shared_nested(struct au_hinode *hdir,
20477 +                                                 unsigned int sc)
20478 +{
20479 +       inode_lock_shared_nested(hdir->hi_inode, sc);
20480 +       au_hn_suspend(hdir);
20481 +}
20482 +#endif
20483 +
20484 +static inline void au_hn_inode_unlock(struct au_hinode *hdir)
20485 +{
20486 +       au_hn_resume(hdir);
20487 +       inode_unlock(hdir->hi_inode);
20488 +}
20489 +
20490 +#endif /* __KERNEL__ */
20491 +#endif /* __AUFS_INODE_H__ */
20492 diff -urN /usr/share/empty/fs/aufs/ioctl.c linux/fs/aufs/ioctl.c
20493 --- /usr/share/empty/fs/aufs/ioctl.c    1970-01-01 01:00:00.000000000 +0100
20494 +++ linux/fs/aufs/ioctl.c       2022-03-21 14:49:05.726633010 +0100
20495 @@ -0,0 +1,220 @@
20496 +// SPDX-License-Identifier: GPL-2.0
20497 +/*
20498 + * Copyright (C) 2005-2021 Junjiro R. Okajima
20499 + *
20500 + * This program, aufs is free software; you can redistribute it and/or modify
20501 + * it under the terms of the GNU General Public License as published by
20502 + * the Free Software Foundation; either version 2 of the License, or
20503 + * (at your option) any later version.
20504 + *
20505 + * This program is distributed in the hope that it will be useful,
20506 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
20507 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20508 + * GNU General Public License for more details.
20509 + *
20510 + * You should have received a copy of the GNU General Public License
20511 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20512 + */
20513 +
20514 +/*
20515 + * ioctl
20516 + * plink-management and readdir in userspace.
20517 + * assist the pathconf(3) wrapper library.
20518 + * move-down
20519 + * File-based Hierarchical Storage Management.
20520 + */
20521 +
20522 +#include <linux/compat.h>
20523 +#include <linux/file.h>
20524 +#include "aufs.h"
20525 +
20526 +static int au_wbr_fd(struct path *path, struct aufs_wbr_fd __user *arg)
20527 +{
20528 +       int err, fd;
20529 +       aufs_bindex_t wbi, bindex, bbot;
20530 +       struct file *h_file;
20531 +       struct super_block *sb;
20532 +       struct dentry *root;
20533 +       struct au_branch *br;
20534 +       struct aufs_wbr_fd wbrfd = {
20535 +               .oflags = au_dir_roflags,
20536 +               .brid   = -1
20537 +       };
20538 +       const int valid = O_RDONLY | O_NONBLOCK | O_LARGEFILE | O_DIRECTORY
20539 +               | O_NOATIME | O_CLOEXEC;
20540 +
20541 +       AuDebugOn(wbrfd.oflags & ~valid);
20542 +
20543 +       if (arg) {
20544 +               err = copy_from_user(&wbrfd, arg, sizeof(wbrfd));
20545 +               if (unlikely(err)) {
20546 +                       err = -EFAULT;
20547 +                       goto out;
20548 +               }
20549 +
20550 +               err = -EINVAL;
20551 +               AuDbg("wbrfd{0%o, %d}\n", wbrfd.oflags, wbrfd.brid);
20552 +               wbrfd.oflags |= au_dir_roflags;
20553 +               AuDbg("0%o\n", wbrfd.oflags);
20554 +               if (unlikely(wbrfd.oflags & ~valid))
20555 +                       goto out;
20556 +       }
20557 +
20558 +       fd = get_unused_fd_flags(0);
20559 +       err = fd;
20560 +       if (unlikely(fd < 0))
20561 +               goto out;
20562 +
20563 +       h_file = ERR_PTR(-EINVAL);
20564 +       wbi = 0;
20565 +       br = NULL;
20566 +       sb = path->dentry->d_sb;
20567 +       root = sb->s_root;
20568 +       aufs_read_lock(root, AuLock_IR);
20569 +       bbot = au_sbbot(sb);
20570 +       if (wbrfd.brid >= 0) {
20571 +               wbi = au_br_index(sb, wbrfd.brid);
20572 +               if (unlikely(wbi < 0 || wbi > bbot))
20573 +                       goto out_unlock;
20574 +       }
20575 +
20576 +       h_file = ERR_PTR(-ENOENT);
20577 +       br = au_sbr(sb, wbi);
20578 +       if (!au_br_writable(br->br_perm)) {
20579 +               if (arg)
20580 +                       goto out_unlock;
20581 +
20582 +               bindex = wbi + 1;
20583 +               wbi = -1;
20584 +               for (; bindex <= bbot; bindex++) {
20585 +                       br = au_sbr(sb, bindex);
20586 +                       if (au_br_writable(br->br_perm)) {
20587 +                               wbi = bindex;
20588 +                               br = au_sbr(sb, wbi);
20589 +                               break;
20590 +                       }
20591 +               }
20592 +       }
20593 +       AuDbg("wbi %d\n", wbi);
20594 +       if (wbi >= 0)
20595 +               h_file = au_h_open(root, wbi, wbrfd.oflags, NULL,
20596 +                                  /*force_wr*/0);
20597 +
20598 +out_unlock:
20599 +       aufs_read_unlock(root, AuLock_IR);
20600 +       err = PTR_ERR(h_file);
20601 +       if (IS_ERR(h_file))
20602 +               goto out_fd;
20603 +
20604 +       au_lcnt_dec(&br->br_nfiles); /* cf. au_h_open() */
20605 +       fd_install(fd, h_file);
20606 +       err = fd;
20607 +       goto out; /* success */
20608 +
20609 +out_fd:
20610 +       put_unused_fd(fd);
20611 +out:
20612 +       AuTraceErr(err);
20613 +       return err;
20614 +}
20615 +
20616 +/* ---------------------------------------------------------------------- */
20617 +
20618 +long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg)
20619 +{
20620 +       long err;
20621 +       struct dentry *dentry;
20622 +
20623 +       switch (cmd) {
20624 +       case AUFS_CTL_RDU:
20625 +       case AUFS_CTL_RDU_INO:
20626 +               err = au_rdu_ioctl(file, cmd, arg);
20627 +               break;
20628 +
20629 +       case AUFS_CTL_WBR_FD:
20630 +               err = au_wbr_fd(&file->f_path, (void __user *)arg);
20631 +               break;
20632 +
20633 +       case AUFS_CTL_IBUSY:
20634 +               err = au_ibusy_ioctl(file, arg);
20635 +               break;
20636 +
20637 +       case AUFS_CTL_BRINFO:
20638 +               err = au_brinfo_ioctl(file, arg);
20639 +               break;
20640 +
20641 +       case AUFS_CTL_FHSM_FD:
20642 +               dentry = file->f_path.dentry;
20643 +               if (IS_ROOT(dentry))
20644 +                       err = au_fhsm_fd(dentry->d_sb, arg);
20645 +               else
20646 +                       err = -ENOTTY;
20647 +               break;
20648 +
20649 +       default:
20650 +               /* do not call the lower */
20651 +               AuDbg("0x%x\n", cmd);
20652 +               err = -ENOTTY;
20653 +       }
20654 +
20655 +       AuTraceErr(err);
20656 +       return err;
20657 +}
20658 +
20659 +long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg)
20660 +{
20661 +       long err;
20662 +
20663 +       switch (cmd) {
20664 +       case AUFS_CTL_MVDOWN:
20665 +               err = au_mvdown(file->f_path.dentry, (void __user *)arg);
20666 +               break;
20667 +
20668 +       case AUFS_CTL_WBR_FD:
20669 +               err = au_wbr_fd(&file->f_path, (void __user *)arg);
20670 +               break;
20671 +
20672 +       default:
20673 +               /* do not call the lower */
20674 +               AuDbg("0x%x\n", cmd);
20675 +               err = -ENOTTY;
20676 +       }
20677 +
20678 +       AuTraceErr(err);
20679 +       return err;
20680 +}
20681 +
20682 +#ifdef CONFIG_COMPAT
20683 +long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
20684 +                          unsigned long arg)
20685 +{
20686 +       long err;
20687 +
20688 +       switch (cmd) {
20689 +       case AUFS_CTL_RDU:
20690 +       case AUFS_CTL_RDU_INO:
20691 +               err = au_rdu_compat_ioctl(file, cmd, arg);
20692 +               break;
20693 +
20694 +       case AUFS_CTL_IBUSY:
20695 +               err = au_ibusy_compat_ioctl(file, arg);
20696 +               break;
20697 +
20698 +       case AUFS_CTL_BRINFO:
20699 +               err = au_brinfo_compat_ioctl(file, arg);
20700 +               break;
20701 +
20702 +       default:
20703 +               err = aufs_ioctl_dir(file, cmd, arg);
20704 +       }
20705 +
20706 +       AuTraceErr(err);
20707 +       return err;
20708 +}
20709 +
20710 +long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
20711 +                             unsigned long arg)
20712 +{
20713 +       return aufs_ioctl_nondir(file, cmd, (unsigned long)compat_ptr(arg));
20714 +}
20715 +#endif
20716 diff -urN /usr/share/empty/fs/aufs/i_op_add.c linux/fs/aufs/i_op_add.c
20717 --- /usr/share/empty/fs/aufs/i_op_add.c 1970-01-01 01:00:00.000000000 +0100
20718 +++ linux/fs/aufs/i_op_add.c    2022-03-21 14:49:05.726633010 +0100
20719 @@ -0,0 +1,941 @@
20720 +// SPDX-License-Identifier: GPL-2.0
20721 +/*
20722 + * Copyright (C) 2005-2021 Junjiro R. Okajima
20723 + *
20724 + * This program, aufs is free software; you can redistribute it and/or modify
20725 + * it under the terms of the GNU General Public License as published by
20726 + * the Free Software Foundation; either version 2 of the License, or
20727 + * (at your option) any later version.
20728 + *
20729 + * This program is distributed in the hope that it will be useful,
20730 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
20731 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20732 + * GNU General Public License for more details.
20733 + *
20734 + * You should have received a copy of the GNU General Public License
20735 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20736 + */
20737 +
20738 +/*
20739 + * inode operations (add entry)
20740 + */
20741 +
20742 +#include <linux/iversion.h>
20743 +#include "aufs.h"
20744 +
20745 +/*
20746 + * final procedure of adding a new entry, except link(2).
20747 + * remove whiteout, instantiate, copyup the parent dir's times and size
20748 + * and update version.
20749 + * if it failed, re-create the removed whiteout.
20750 + */
20751 +static int epilog(struct inode *dir, aufs_bindex_t bindex,
20752 +                 struct dentry *wh_dentry, struct dentry *dentry)
20753 +{
20754 +       int err, rerr;
20755 +       aufs_bindex_t bwh;
20756 +       struct path h_path;
20757 +       struct super_block *sb;
20758 +       struct inode *inode, *h_dir;
20759 +       struct dentry *wh;
20760 +
20761 +       bwh = -1;
20762 +       sb = dir->i_sb;
20763 +       if (wh_dentry) {
20764 +               h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */
20765 +               IMustLock(h_dir);
20766 +               AuDebugOn(au_h_iptr(dir, bindex) != h_dir);
20767 +               bwh = au_dbwh(dentry);
20768 +               h_path.dentry = wh_dentry;
20769 +               h_path.mnt = au_sbr_mnt(sb, bindex);
20770 +               err = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path,
20771 +                                         dentry);
20772 +               if (unlikely(err))
20773 +                       goto out;
20774 +       }
20775 +
20776 +       inode = au_new_inode(dentry, /*must_new*/1);
20777 +       if (!IS_ERR(inode)) {
20778 +               d_instantiate(dentry, inode);
20779 +               dir = d_inode(dentry->d_parent); /* dir inode is locked */
20780 +               IMustLock(dir);
20781 +               au_dir_ts(dir, bindex);
20782 +               inode_inc_iversion(dir);
20783 +               au_fhsm_wrote(sb, bindex, /*force*/0);
20784 +               return 0; /* success */
20785 +       }
20786 +
20787 +       err = PTR_ERR(inode);
20788 +       if (!wh_dentry)
20789 +               goto out;
20790 +
20791 +       /* revert */
20792 +       /* dir inode is locked */
20793 +       wh = au_wh_create(dentry, bwh, wh_dentry->d_parent);
20794 +       rerr = PTR_ERR(wh);
20795 +       if (IS_ERR(wh)) {
20796 +               AuIOErr("%pd reverting whiteout failed(%d, %d)\n",
20797 +                       dentry, err, rerr);
20798 +               err = -EIO;
20799 +       } else
20800 +               dput(wh);
20801 +
20802 +out:
20803 +       return err;
20804 +}
20805 +
20806 +static int au_d_may_add(struct dentry *dentry)
20807 +{
20808 +       int err;
20809 +
20810 +       err = 0;
20811 +       if (unlikely(d_unhashed(dentry)))
20812 +               err = -ENOENT;
20813 +       if (unlikely(d_really_is_positive(dentry)))
20814 +               err = -EEXIST;
20815 +       return err;
20816 +}
20817 +
20818 +/*
20819 + * simple tests for the adding inode operations.
20820 + * following the checks in vfs, plus the parent-child relationship.
20821 + */
20822 +int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
20823 +              struct dentry *h_parent, int isdir)
20824 +{
20825 +       int err;
20826 +       umode_t h_mode;
20827 +       struct dentry *h_dentry;
20828 +       struct inode *h_inode;
20829 +
20830 +       err = -ENAMETOOLONG;
20831 +       if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
20832 +               goto out;
20833 +
20834 +       h_dentry = au_h_dptr(dentry, bindex);
20835 +       if (d_really_is_negative(dentry)) {
20836 +               err = -EEXIST;
20837 +               if (unlikely(d_is_positive(h_dentry)))
20838 +                       goto out;
20839 +       } else {
20840 +               /* rename(2) case */
20841 +               err = -EIO;
20842 +               if (unlikely(d_is_negative(h_dentry)))
20843 +                       goto out;
20844 +               h_inode = d_inode(h_dentry);
20845 +               if (unlikely(!h_inode->i_nlink))
20846 +                       goto out;
20847 +
20848 +               h_mode = h_inode->i_mode;
20849 +               if (!isdir) {
20850 +                       err = -EISDIR;
20851 +                       if (unlikely(S_ISDIR(h_mode)))
20852 +                               goto out;
20853 +               } else if (unlikely(!S_ISDIR(h_mode))) {
20854 +                       err = -ENOTDIR;
20855 +                       goto out;
20856 +               }
20857 +       }
20858 +
20859 +       err = 0;
20860 +       /* expected parent dir is locked */
20861 +       if (unlikely(h_parent != h_dentry->d_parent))
20862 +               err = -EIO;
20863 +
20864 +out:
20865 +       AuTraceErr(err);
20866 +       return err;
20867 +}
20868 +
20869 +/*
20870 + * initial procedure of adding a new entry.
20871 + * prepare writable branch and the parent dir, lock it,
20872 + * and lookup whiteout for the new entry.
20873 + */
20874 +static struct dentry*
20875 +lock_hdir_lkup_wh(struct dentry *dentry, struct au_dtime *dt,
20876 +                 struct dentry *src_dentry, struct au_pin *pin,
20877 +                 struct au_wr_dir_args *wr_dir_args)
20878 +{
20879 +       struct dentry *wh_dentry, *h_parent;
20880 +       struct super_block *sb;
20881 +       struct au_branch *br;
20882 +       int err;
20883 +       unsigned int udba;
20884 +       aufs_bindex_t bcpup;
20885 +
20886 +       AuDbg("%pd\n", dentry);
20887 +
20888 +       err = au_wr_dir(dentry, src_dentry, wr_dir_args);
20889 +       bcpup = err;
20890 +       wh_dentry = ERR_PTR(err);
20891 +       if (unlikely(err < 0))
20892 +               goto out;
20893 +
20894 +       sb = dentry->d_sb;
20895 +       udba = au_opt_udba(sb);
20896 +       err = au_pin(pin, dentry, bcpup, udba,
20897 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
20898 +       wh_dentry = ERR_PTR(err);
20899 +       if (unlikely(err))
20900 +               goto out;
20901 +
20902 +       h_parent = au_pinned_h_parent(pin);
20903 +       if (udba != AuOpt_UDBA_NONE
20904 +           && au_dbtop(dentry) == bcpup)
20905 +               err = au_may_add(dentry, bcpup, h_parent,
20906 +                                au_ftest_wrdir(wr_dir_args->flags, ISDIR));
20907 +       else if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
20908 +               err = -ENAMETOOLONG;
20909 +       wh_dentry = ERR_PTR(err);
20910 +       if (unlikely(err))
20911 +               goto out_unpin;
20912 +
20913 +       br = au_sbr(sb, bcpup);
20914 +       if (dt) {
20915 +               struct path tmp = {
20916 +                       .dentry = h_parent,
20917 +                       .mnt    = au_br_mnt(br)
20918 +               };
20919 +               au_dtime_store(dt, au_pinned_parent(pin), &tmp);
20920 +       }
20921 +
20922 +       wh_dentry = NULL;
20923 +       if (bcpup != au_dbwh(dentry))
20924 +               goto out; /* success */
20925 +
20926 +       /*
20927 +        * ENAMETOOLONG here means that if we allowed create such name, then it
20928 +        * would not be able to removed in the future. So we don't allow such
20929 +        * name here and we don't handle ENAMETOOLONG differently here.
20930 +        */
20931 +       wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
20932 +
20933 +out_unpin:
20934 +       if (IS_ERR(wh_dentry))
20935 +               au_unpin(pin);
20936 +out:
20937 +       return wh_dentry;
20938 +}
20939 +
20940 +/* ---------------------------------------------------------------------- */
20941 +
20942 +enum { Mknod, Symlink, Creat };
20943 +struct simple_arg {
20944 +       int type;
20945 +       union {
20946 +               struct {
20947 +                       umode_t                 mode;
20948 +                       bool                    want_excl;
20949 +                       bool                    try_aopen;
20950 +                       struct vfsub_aopen_args *aopen;
20951 +               } c;
20952 +               struct {
20953 +                       const char *symname;
20954 +               } s;
20955 +               struct {
20956 +                       umode_t mode;
20957 +                       dev_t dev;
20958 +               } m;
20959 +       } u;
20960 +};
20961 +
20962 +static int add_simple(struct inode *dir, struct dentry *dentry,
20963 +                     struct simple_arg *arg)
20964 +{
20965 +       int err, rerr;
20966 +       aufs_bindex_t btop;
20967 +       unsigned char created;
20968 +       const unsigned char try_aopen
20969 +               = (arg->type == Creat && arg->u.c.try_aopen);
20970 +       struct vfsub_aopen_args *aopen = arg->u.c.aopen;
20971 +       struct dentry *wh_dentry, *parent;
20972 +       struct inode *h_dir;
20973 +       struct super_block *sb;
20974 +       struct au_branch *br;
20975 +       /* to reduce stack size */
20976 +       struct {
20977 +               struct au_dtime dt;
20978 +               struct au_pin pin;
20979 +               struct path h_path;
20980 +               struct au_wr_dir_args wr_dir_args;
20981 +       } *a;
20982 +
20983 +       AuDbg("%pd\n", dentry);
20984 +       IMustLock(dir);
20985 +
20986 +       err = -ENOMEM;
20987 +       a = kmalloc(sizeof(*a), GFP_NOFS);
20988 +       if (unlikely(!a))
20989 +               goto out;
20990 +       a->wr_dir_args.force_btgt = -1;
20991 +       a->wr_dir_args.flags = AuWrDir_ADD_ENTRY;
20992 +
20993 +       parent = dentry->d_parent; /* dir inode is locked */
20994 +       if (!try_aopen) {
20995 +               err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
20996 +               if (unlikely(err))
20997 +                       goto out_free;
20998 +       }
20999 +       err = au_d_may_add(dentry);
21000 +       if (unlikely(err))
21001 +               goto out_unlock;
21002 +       if (!try_aopen)
21003 +               di_write_lock_parent(parent);
21004 +       wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
21005 +                                     &a->pin, &a->wr_dir_args);
21006 +       err = PTR_ERR(wh_dentry);
21007 +       if (IS_ERR(wh_dentry))
21008 +               goto out_parent;
21009 +
21010 +       btop = au_dbtop(dentry);
21011 +       sb = dentry->d_sb;
21012 +       br = au_sbr(sb, btop);
21013 +       a->h_path.dentry = au_h_dptr(dentry, btop);
21014 +       a->h_path.mnt = au_br_mnt(br);
21015 +       h_dir = au_pinned_h_dir(&a->pin);
21016 +       switch (arg->type) {
21017 +       case Creat:
21018 +               if (!try_aopen || !h_dir->i_op->atomic_open) {
21019 +                       err = vfsub_create(h_dir, &a->h_path, arg->u.c.mode,
21020 +                                          arg->u.c.want_excl);
21021 +                       created = !err;
21022 +                       if (!err && try_aopen)
21023 +                               aopen->file->f_mode |= FMODE_CREATED;
21024 +               } else {
21025 +                       aopen->br = br;
21026 +                       err = vfsub_atomic_open(h_dir, a->h_path.dentry, aopen);
21027 +                       AuDbg("err %d\n", err);
21028 +                       AuDbgFile(aopen->file);
21029 +                       created = err >= 0
21030 +                               && !!(aopen->file->f_mode & FMODE_CREATED);
21031 +               }
21032 +               break;
21033 +       case Symlink:
21034 +               err = vfsub_symlink(h_dir, &a->h_path, arg->u.s.symname);
21035 +               created = !err;
21036 +               break;
21037 +       case Mknod:
21038 +               err = vfsub_mknod(h_dir, &a->h_path, arg->u.m.mode,
21039 +                                 arg->u.m.dev);
21040 +               created = !err;
21041 +               break;
21042 +       default:
21043 +               BUG();
21044 +       }
21045 +       if (unlikely(err < 0))
21046 +               goto out_unpin;
21047 +
21048 +       err = epilog(dir, btop, wh_dentry, dentry);
21049 +       if (!err)
21050 +               goto out_unpin; /* success */
21051 +
21052 +       /* revert */
21053 +       if (created /* && d_is_positive(a->h_path.dentry) */) {
21054 +               /* no delegation since it is just created */
21055 +               rerr = vfsub_unlink(h_dir, &a->h_path, /*delegated*/NULL,
21056 +                                   /*force*/0);
21057 +               if (rerr) {
21058 +                       AuIOErr("%pd revert failure(%d, %d)\n",
21059 +                               dentry, err, rerr);
21060 +                       err = -EIO;
21061 +               }
21062 +               au_dtime_revert(&a->dt);
21063 +       }
21064 +       if (try_aopen && h_dir->i_op->atomic_open
21065 +           && (aopen->file->f_mode & FMODE_OPENED))
21066 +               /* aopen->file is still opened */
21067 +               au_lcnt_dec(&aopen->br->br_nfiles);
21068 +
21069 +out_unpin:
21070 +       au_unpin(&a->pin);
21071 +       dput(wh_dentry);
21072 +out_parent:
21073 +       if (!try_aopen)
21074 +               di_write_unlock(parent);
21075 +out_unlock:
21076 +       if (unlikely(err)) {
21077 +               au_update_dbtop(dentry);
21078 +               d_drop(dentry);
21079 +       }
21080 +       if (!try_aopen)
21081 +               aufs_read_unlock(dentry, AuLock_DW);
21082 +out_free:
21083 +       au_kfree_rcu(a);
21084 +out:
21085 +       return err;
21086 +}
21087 +
21088 +int aufs_mknod(struct user_namespace *userns, struct inode *dir,
21089 +              struct dentry *dentry, umode_t mode, dev_t dev)
21090 +{
21091 +       struct simple_arg arg = {
21092 +               .type = Mknod,
21093 +               .u.m = {
21094 +                       .mode   = mode,
21095 +                       .dev    = dev
21096 +               }
21097 +       };
21098 +       return add_simple(dir, dentry, &arg);
21099 +}
21100 +
21101 +int aufs_symlink(struct user_namespace *userns, struct inode *dir,
21102 +                struct dentry *dentry, const char *symname)
21103 +{
21104 +       struct simple_arg arg = {
21105 +               .type = Symlink,
21106 +               .u.s.symname = symname
21107 +       };
21108 +       return add_simple(dir, dentry, &arg);
21109 +}
21110 +
21111 +int aufs_create(struct user_namespace *userns, struct inode *dir,
21112 +               struct dentry *dentry, umode_t mode, bool want_excl)
21113 +{
21114 +       struct simple_arg arg = {
21115 +               .type = Creat,
21116 +               .u.c = {
21117 +                       .mode           = mode,
21118 +                       .want_excl      = want_excl
21119 +               }
21120 +       };
21121 +       return add_simple(dir, dentry, &arg);
21122 +}
21123 +
21124 +int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
21125 +                      struct vfsub_aopen_args *aopen_args)
21126 +{
21127 +       struct simple_arg arg = {
21128 +               .type = Creat,
21129 +               .u.c = {
21130 +                       .mode           = aopen_args->create_mode,
21131 +                       .want_excl      = aopen_args->open_flag & O_EXCL,
21132 +                       .try_aopen      = true,
21133 +                       .aopen          = aopen_args
21134 +               }
21135 +       };
21136 +       return add_simple(dir, dentry, &arg);
21137 +}
21138 +
21139 +int aufs_tmpfile(struct user_namespace *userns, struct inode *dir,
21140 +                struct dentry *dentry, umode_t mode)
21141 +{
21142 +       int err;
21143 +       aufs_bindex_t bindex;
21144 +       struct super_block *sb;
21145 +       struct dentry *parent, *h_parent, *h_dentry;
21146 +       struct inode *h_dir, *inode;
21147 +       struct vfsmount *h_mnt;
21148 +       struct user_namespace *h_userns;
21149 +       struct au_wr_dir_args wr_dir_args = {
21150 +               .force_btgt     = -1,
21151 +               .flags          = AuWrDir_TMPFILE
21152 +       };
21153 +
21154 +       /* copy-up may happen */
21155 +       inode_lock(dir);
21156 +
21157 +       sb = dir->i_sb;
21158 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
21159 +       if (unlikely(err))
21160 +               goto out;
21161 +
21162 +       err = au_di_init(dentry);
21163 +       if (unlikely(err))
21164 +               goto out_si;
21165 +
21166 +       err = -EBUSY;
21167 +       parent = d_find_any_alias(dir);
21168 +       AuDebugOn(!parent);
21169 +       di_write_lock_parent(parent);
21170 +       if (unlikely(d_inode(parent) != dir))
21171 +               goto out_parent;
21172 +
21173 +       err = au_digen_test(parent, au_sigen(sb));
21174 +       if (unlikely(err))
21175 +               goto out_parent;
21176 +
21177 +       bindex = au_dbtop(parent);
21178 +       au_set_dbtop(dentry, bindex);
21179 +       au_set_dbbot(dentry, bindex);
21180 +       err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
21181 +       bindex = err;
21182 +       if (unlikely(err < 0))
21183 +               goto out_parent;
21184 +
21185 +       err = -EOPNOTSUPP;
21186 +       h_dir = au_h_iptr(dir, bindex);
21187 +       if (unlikely(!h_dir->i_op->tmpfile))
21188 +               goto out_parent;
21189 +
21190 +       h_mnt = au_sbr_mnt(sb, bindex);
21191 +       err = vfsub_mnt_want_write(h_mnt);
21192 +       if (unlikely(err))
21193 +               goto out_parent;
21194 +
21195 +       h_userns = mnt_user_ns(h_mnt);
21196 +       h_parent = au_h_dptr(parent, bindex);
21197 +       h_dentry = vfs_tmpfile(h_userns, h_parent, mode, /*open_flag*/0);
21198 +       if (IS_ERR(h_dentry)) {
21199 +               err = PTR_ERR(h_dentry);
21200 +               goto out_mnt;
21201 +       }
21202 +
21203 +       au_set_dbtop(dentry, bindex);
21204 +       au_set_dbbot(dentry, bindex);
21205 +       au_set_h_dptr(dentry, bindex, dget(h_dentry));
21206 +       inode = au_new_inode(dentry, /*must_new*/1);
21207 +       if (IS_ERR(inode)) {
21208 +               err = PTR_ERR(inode);
21209 +               au_set_h_dptr(dentry, bindex, NULL);
21210 +               au_set_dbtop(dentry, -1);
21211 +               au_set_dbbot(dentry, -1);
21212 +       } else {
21213 +               if (!inode->i_nlink)
21214 +                       set_nlink(inode, 1);
21215 +               d_tmpfile(dentry, inode);
21216 +               au_di(dentry)->di_tmpfile = 1;
21217 +
21218 +               /* update without i_mutex */
21219 +               if (au_ibtop(dir) == au_dbtop(dentry))
21220 +                       au_cpup_attr_timesizes(dir);
21221 +       }
21222 +       dput(h_dentry);
21223 +
21224 +out_mnt:
21225 +       vfsub_mnt_drop_write(h_mnt);
21226 +out_parent:
21227 +       di_write_unlock(parent);
21228 +       dput(parent);
21229 +       di_write_unlock(dentry);
21230 +       if (unlikely(err)) {
21231 +               au_di_fin(dentry);
21232 +               dentry->d_fsdata = NULL;
21233 +       }
21234 +out_si:
21235 +       si_read_unlock(sb);
21236 +out:
21237 +       inode_unlock(dir);
21238 +       return err;
21239 +}
21240 +
21241 +/* ---------------------------------------------------------------------- */
21242 +
21243 +struct au_link_args {
21244 +       aufs_bindex_t bdst, bsrc;
21245 +       struct au_pin pin;
21246 +       struct path h_path;
21247 +       struct dentry *src_parent, *parent;
21248 +};
21249 +
21250 +static int au_cpup_before_link(struct dentry *src_dentry,
21251 +                              struct au_link_args *a)
21252 +{
21253 +       int err;
21254 +       struct dentry *h_src_dentry;
21255 +       struct au_cp_generic cpg = {
21256 +               .dentry = src_dentry,
21257 +               .bdst   = a->bdst,
21258 +               .bsrc   = a->bsrc,
21259 +               .len    = -1,
21260 +               .pin    = &a->pin,
21261 +               .flags  = AuCpup_DTIME | AuCpup_HOPEN /* | AuCpup_KEEPLINO */
21262 +       };
21263 +
21264 +       di_read_lock_parent(a->src_parent, AuLock_IR);
21265 +       err = au_test_and_cpup_dirs(src_dentry, a->bdst);
21266 +       if (unlikely(err))
21267 +               goto out;
21268 +
21269 +       h_src_dentry = au_h_dptr(src_dentry, a->bsrc);
21270 +       err = au_pin(&a->pin, src_dentry, a->bdst,
21271 +                    au_opt_udba(src_dentry->d_sb),
21272 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
21273 +       if (unlikely(err))
21274 +               goto out;
21275 +
21276 +       err = au_sio_cpup_simple(&cpg);
21277 +       au_unpin(&a->pin);
21278 +
21279 +out:
21280 +       di_read_unlock(a->src_parent, AuLock_IR);
21281 +       return err;
21282 +}
21283 +
21284 +static int au_cpup_or_link(struct dentry *src_dentry, struct dentry *dentry,
21285 +                          struct au_link_args *a)
21286 +{
21287 +       int err;
21288 +       unsigned char plink;
21289 +       aufs_bindex_t bbot;
21290 +       struct dentry *h_src_dentry;
21291 +       struct inode *h_inode, *inode, *delegated;
21292 +       struct super_block *sb;
21293 +       struct file *h_file;
21294 +
21295 +       plink = 0;
21296 +       h_inode = NULL;
21297 +       sb = src_dentry->d_sb;
21298 +       inode = d_inode(src_dentry);
21299 +       if (au_ibtop(inode) <= a->bdst)
21300 +               h_inode = au_h_iptr(inode, a->bdst);
21301 +       if (!h_inode || !h_inode->i_nlink) {
21302 +               /* copyup src_dentry as the name of dentry. */
21303 +               bbot = au_dbbot(dentry);
21304 +               if (bbot < a->bsrc)
21305 +                       au_set_dbbot(dentry, a->bsrc);
21306 +               au_set_h_dptr(dentry, a->bsrc,
21307 +                             dget(au_h_dptr(src_dentry, a->bsrc)));
21308 +               dget(a->h_path.dentry);
21309 +               au_set_h_dptr(dentry, a->bdst, NULL);
21310 +               AuDbg("temporary d_inode...\n");
21311 +               spin_lock(&dentry->d_lock);
21312 +               dentry->d_inode = d_inode(src_dentry); /* tmp */
21313 +               spin_unlock(&dentry->d_lock);
21314 +               h_file = au_h_open_pre(dentry, a->bsrc, /*force_wr*/0);
21315 +               if (IS_ERR(h_file))
21316 +                       err = PTR_ERR(h_file);
21317 +               else {
21318 +                       struct au_cp_generic cpg = {
21319 +                               .dentry = dentry,
21320 +                               .bdst   = a->bdst,
21321 +                               .bsrc   = -1,
21322 +                               .len    = -1,
21323 +                               .pin    = &a->pin,
21324 +                               .flags  = AuCpup_KEEPLINO
21325 +                       };
21326 +                       err = au_sio_cpup_simple(&cpg);
21327 +                       au_h_open_post(dentry, a->bsrc, h_file);
21328 +                       if (!err) {
21329 +                               dput(a->h_path.dentry);
21330 +                               a->h_path.dentry = au_h_dptr(dentry, a->bdst);
21331 +                       } else
21332 +                               au_set_h_dptr(dentry, a->bdst,
21333 +                                             a->h_path.dentry);
21334 +               }
21335 +               spin_lock(&dentry->d_lock);
21336 +               dentry->d_inode = NULL; /* restore */
21337 +               spin_unlock(&dentry->d_lock);
21338 +               AuDbg("temporary d_inode...done\n");
21339 +               au_set_h_dptr(dentry, a->bsrc, NULL);
21340 +               au_set_dbbot(dentry, bbot);
21341 +       } else {
21342 +               /* the inode of src_dentry already exists on a.bdst branch */
21343 +               h_src_dentry = d_find_alias(h_inode);
21344 +               if (!h_src_dentry && au_plink_test(inode)) {
21345 +                       plink = 1;
21346 +                       h_src_dentry = au_plink_lkup(inode, a->bdst);
21347 +                       err = PTR_ERR(h_src_dentry);
21348 +                       if (IS_ERR(h_src_dentry))
21349 +                               goto out;
21350 +
21351 +                       if (unlikely(d_is_negative(h_src_dentry))) {
21352 +                               dput(h_src_dentry);
21353 +                               h_src_dentry = NULL;
21354 +                       }
21355 +
21356 +               }
21357 +               if (h_src_dentry) {
21358 +                       delegated = NULL;
21359 +                       err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
21360 +                                        &a->h_path, &delegated);
21361 +                       if (unlikely(err == -EWOULDBLOCK)) {
21362 +                               pr_warn("cannot retry for NFSv4 delegation"
21363 +                                       " for an internal link\n");
21364 +                               iput(delegated);
21365 +                       }
21366 +                       dput(h_src_dentry);
21367 +               } else {
21368 +                       AuIOErr("no dentry found for hi%lu on b%d\n",
21369 +                               h_inode->i_ino, a->bdst);
21370 +                       err = -EIO;
21371 +               }
21372 +       }
21373 +
21374 +       if (!err && !plink)
21375 +               au_plink_append(inode, a->bdst, a->h_path.dentry);
21376 +
21377 +out:
21378 +       AuTraceErr(err);
21379 +       return err;
21380 +}
21381 +
21382 +int aufs_link(struct dentry *src_dentry, struct inode *dir,
21383 +             struct dentry *dentry)
21384 +{
21385 +       int err, rerr;
21386 +       struct au_dtime dt;
21387 +       struct au_link_args *a;
21388 +       struct dentry *wh_dentry, *h_src_dentry;
21389 +       struct inode *inode, *delegated;
21390 +       struct super_block *sb;
21391 +       struct au_wr_dir_args wr_dir_args = {
21392 +               /* .force_btgt  = -1, */
21393 +               .flags          = AuWrDir_ADD_ENTRY
21394 +       };
21395 +
21396 +       IMustLock(dir);
21397 +       inode = d_inode(src_dentry);
21398 +       IMustLock(inode);
21399 +
21400 +       err = -ENOMEM;
21401 +       a = kzalloc(sizeof(*a), GFP_NOFS);
21402 +       if (unlikely(!a))
21403 +               goto out;
21404 +
21405 +       a->parent = dentry->d_parent; /* dir inode is locked */
21406 +       err = aufs_read_and_write_lock2(dentry, src_dentry,
21407 +                                       AuLock_NOPLM | AuLock_GEN);
21408 +       if (unlikely(err))
21409 +               goto out_kfree;
21410 +       err = au_d_linkable(src_dentry);
21411 +       if (unlikely(err))
21412 +               goto out_unlock;
21413 +       err = au_d_may_add(dentry);
21414 +       if (unlikely(err))
21415 +               goto out_unlock;
21416 +
21417 +       a->src_parent = dget_parent(src_dentry);
21418 +       wr_dir_args.force_btgt = au_ibtop(inode);
21419 +
21420 +       di_write_lock_parent(a->parent);
21421 +       wr_dir_args.force_btgt = au_wbr(dentry, wr_dir_args.force_btgt);
21422 +       wh_dentry = lock_hdir_lkup_wh(dentry, &dt, src_dentry, &a->pin,
21423 +                                     &wr_dir_args);
21424 +       err = PTR_ERR(wh_dentry);
21425 +       if (IS_ERR(wh_dentry))
21426 +               goto out_parent;
21427 +
21428 +       err = 0;
21429 +       sb = dentry->d_sb;
21430 +       a->bdst = au_dbtop(dentry);
21431 +       a->h_path.dentry = au_h_dptr(dentry, a->bdst);
21432 +       a->h_path.mnt = au_sbr_mnt(sb, a->bdst);
21433 +       a->bsrc = au_ibtop(inode);
21434 +       h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
21435 +       if (!h_src_dentry && au_di(src_dentry)->di_tmpfile)
21436 +               h_src_dentry = dget(au_hi_wh(inode, a->bsrc));
21437 +       if (!h_src_dentry) {
21438 +               a->bsrc = au_dbtop(src_dentry);
21439 +               h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
21440 +               AuDebugOn(!h_src_dentry);
21441 +       } else if (IS_ERR(h_src_dentry)) {
21442 +               err = PTR_ERR(h_src_dentry);
21443 +               goto out_parent;
21444 +       }
21445 +
21446 +       /*
21447 +        * aufs doesn't touch the credential so
21448 +        * security_dentry_create_files_as() is unnecessary.
21449 +        */
21450 +       if (au_opt_test(au_mntflags(sb), PLINK)) {
21451 +               if (a->bdst < a->bsrc
21452 +                   /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */)
21453 +                       err = au_cpup_or_link(src_dentry, dentry, a);
21454 +               else {
21455 +                       delegated = NULL;
21456 +                       err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
21457 +                                        &a->h_path, &delegated);
21458 +                       if (unlikely(err == -EWOULDBLOCK)) {
21459 +                               pr_warn("cannot retry for NFSv4 delegation"
21460 +                                       " for an internal link\n");
21461 +                               iput(delegated);
21462 +                       }
21463 +               }
21464 +               dput(h_src_dentry);
21465 +       } else {
21466 +               /*
21467 +                * copyup src_dentry to the branch we process,
21468 +                * and then link(2) to it.
21469 +                */
21470 +               dput(h_src_dentry);
21471 +               if (a->bdst < a->bsrc
21472 +                   /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */) {
21473 +                       au_unpin(&a->pin);
21474 +                       di_write_unlock(a->parent);
21475 +                       err = au_cpup_before_link(src_dentry, a);
21476 +                       di_write_lock_parent(a->parent);
21477 +                       if (!err)
21478 +                               err = au_pin(&a->pin, dentry, a->bdst,
21479 +                                            au_opt_udba(sb),
21480 +                                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
21481 +                       if (unlikely(err))
21482 +                               goto out_wh;
21483 +               }
21484 +               if (!err) {
21485 +                       h_src_dentry = au_h_dptr(src_dentry, a->bdst);
21486 +                       err = -ENOENT;
21487 +                       if (h_src_dentry && d_is_positive(h_src_dentry)) {
21488 +                               delegated = NULL;
21489 +                               err = vfsub_link(h_src_dentry,
21490 +                                                au_pinned_h_dir(&a->pin),
21491 +                                                &a->h_path, &delegated);
21492 +                               if (unlikely(err == -EWOULDBLOCK)) {
21493 +                                       pr_warn("cannot retry"
21494 +                                               " for NFSv4 delegation"
21495 +                                               " for an internal link\n");
21496 +                                       iput(delegated);
21497 +                               }
21498 +                       }
21499 +               }
21500 +       }
21501 +       if (unlikely(err))
21502 +               goto out_unpin;
21503 +
21504 +       if (wh_dentry) {
21505 +               a->h_path.dentry = wh_dentry;
21506 +               err = au_wh_unlink_dentry(au_pinned_h_dir(&a->pin), &a->h_path,
21507 +                                         dentry);
21508 +               if (unlikely(err))
21509 +                       goto out_revert;
21510 +       }
21511 +
21512 +       au_dir_ts(dir, a->bdst);
21513 +       inode_inc_iversion(dir);
21514 +       inc_nlink(inode);
21515 +       inode->i_ctime = dir->i_ctime;
21516 +       d_instantiate(dentry, au_igrab(inode));
21517 +       if (d_unhashed(a->h_path.dentry))
21518 +               /* some filesystem calls d_drop() */
21519 +               d_drop(dentry);
21520 +       /* some filesystems consume an inode even hardlink */
21521 +       au_fhsm_wrote(sb, a->bdst, /*force*/0);
21522 +       goto out_unpin; /* success */
21523 +
21524 +out_revert:
21525 +       /* no delegation since it is just created */
21526 +       rerr = vfsub_unlink(au_pinned_h_dir(&a->pin), &a->h_path,
21527 +                           /*delegated*/NULL, /*force*/0);
21528 +       if (unlikely(rerr)) {
21529 +               AuIOErr("%pd reverting failed(%d, %d)\n", dentry, err, rerr);
21530 +               err = -EIO;
21531 +       }
21532 +       au_dtime_revert(&dt);
21533 +out_unpin:
21534 +       au_unpin(&a->pin);
21535 +out_wh:
21536 +       dput(wh_dentry);
21537 +out_parent:
21538 +       di_write_unlock(a->parent);
21539 +       dput(a->src_parent);
21540 +out_unlock:
21541 +       if (unlikely(err)) {
21542 +               au_update_dbtop(dentry);
21543 +               d_drop(dentry);
21544 +       }
21545 +       aufs_read_and_write_unlock2(dentry, src_dentry);
21546 +out_kfree:
21547 +       au_kfree_rcu(a);
21548 +out:
21549 +       AuTraceErr(err);
21550 +       return err;
21551 +}
21552 +
21553 +int aufs_mkdir(struct user_namespace *userns, struct inode *dir,
21554 +              struct dentry *dentry, umode_t mode)
21555 +{
21556 +       int err, rerr;
21557 +       aufs_bindex_t bindex;
21558 +       unsigned char diropq;
21559 +       struct path h_path;
21560 +       struct dentry *wh_dentry, *parent, *opq_dentry;
21561 +       struct inode *h_inode;
21562 +       struct super_block *sb;
21563 +       struct {
21564 +               struct au_pin pin;
21565 +               struct au_dtime dt;
21566 +       } *a; /* reduce the stack usage */
21567 +       struct au_wr_dir_args wr_dir_args = {
21568 +               .force_btgt     = -1,
21569 +               .flags          = AuWrDir_ADD_ENTRY | AuWrDir_ISDIR
21570 +       };
21571 +
21572 +       IMustLock(dir);
21573 +
21574 +       err = -ENOMEM;
21575 +       a = kmalloc(sizeof(*a), GFP_NOFS);
21576 +       if (unlikely(!a))
21577 +               goto out;
21578 +
21579 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
21580 +       if (unlikely(err))
21581 +               goto out_free;
21582 +       err = au_d_may_add(dentry);
21583 +       if (unlikely(err))
21584 +               goto out_unlock;
21585 +
21586 +       parent = dentry->d_parent; /* dir inode is locked */
21587 +       di_write_lock_parent(parent);
21588 +       wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
21589 +                                     &a->pin, &wr_dir_args);
21590 +       err = PTR_ERR(wh_dentry);
21591 +       if (IS_ERR(wh_dentry))
21592 +               goto out_parent;
21593 +
21594 +       sb = dentry->d_sb;
21595 +       bindex = au_dbtop(dentry);
21596 +       h_path.dentry = au_h_dptr(dentry, bindex);
21597 +       h_path.mnt = au_sbr_mnt(sb, bindex);
21598 +       err = vfsub_mkdir(au_pinned_h_dir(&a->pin), &h_path, mode);
21599 +       if (unlikely(err))
21600 +               goto out_unpin;
21601 +
21602 +       /* make the dir opaque */
21603 +       diropq = 0;
21604 +       h_inode = d_inode(h_path.dentry);
21605 +       if (wh_dentry
21606 +           || au_opt_test(au_mntflags(sb), ALWAYS_DIROPQ)) {
21607 +               inode_lock_nested(h_inode, AuLsc_I_CHILD);
21608 +               opq_dentry = au_diropq_create(dentry, bindex);
21609 +               inode_unlock(h_inode);
21610 +               err = PTR_ERR(opq_dentry);
21611 +               if (IS_ERR(opq_dentry))
21612 +                       goto out_dir;
21613 +               dput(opq_dentry);
21614 +               diropq = 1;
21615 +       }
21616 +
21617 +       err = epilog(dir, bindex, wh_dentry, dentry);
21618 +       if (!err) {
21619 +               inc_nlink(dir);
21620 +               goto out_unpin; /* success */
21621 +       }
21622 +
21623 +       /* revert */
21624 +       if (diropq) {
21625 +               AuLabel(revert opq);
21626 +               inode_lock_nested(h_inode, AuLsc_I_CHILD);
21627 +               rerr = au_diropq_remove(dentry, bindex);
21628 +               inode_unlock(h_inode);
21629 +               if (rerr) {
21630 +                       AuIOErr("%pd reverting diropq failed(%d, %d)\n",
21631 +                               dentry, err, rerr);
21632 +                       err = -EIO;
21633 +               }
21634 +       }
21635 +
21636 +out_dir:
21637 +       AuLabel(revert dir);
21638 +       rerr = vfsub_rmdir(au_pinned_h_dir(&a->pin), &h_path);
21639 +       if (rerr) {
21640 +               AuIOErr("%pd reverting dir failed(%d, %d)\n",
21641 +                       dentry, err, rerr);
21642 +               err = -EIO;
21643 +       }
21644 +       au_dtime_revert(&a->dt);
21645 +out_unpin:
21646 +       au_unpin(&a->pin);
21647 +       dput(wh_dentry);
21648 +out_parent:
21649 +       di_write_unlock(parent);
21650 +out_unlock:
21651 +       if (unlikely(err)) {
21652 +               au_update_dbtop(dentry);
21653 +               d_drop(dentry);
21654 +       }
21655 +       aufs_read_unlock(dentry, AuLock_DW);
21656 +out_free:
21657 +       au_kfree_rcu(a);
21658 +out:
21659 +       return err;
21660 +}
21661 diff -urN /usr/share/empty/fs/aufs/i_op.c linux/fs/aufs/i_op.c
21662 --- /usr/share/empty/fs/aufs/i_op.c     1970-01-01 01:00:00.000000000 +0100
21663 +++ linux/fs/aufs/i_op.c        2022-03-21 14:49:05.726633010 +0100
21664 @@ -0,0 +1,1512 @@
21665 +// SPDX-License-Identifier: GPL-2.0
21666 +/*
21667 + * Copyright (C) 2005-2021 Junjiro R. Okajima
21668 + *
21669 + * This program, aufs is free software; you can redistribute it and/or modify
21670 + * it under the terms of the GNU General Public License as published by
21671 + * the Free Software Foundation; either version 2 of the License, or
21672 + * (at your option) any later version.
21673 + *
21674 + * This program is distributed in the hope that it will be useful,
21675 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
21676 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21677 + * GNU General Public License for more details.
21678 + *
21679 + * You should have received a copy of the GNU General Public License
21680 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21681 + */
21682 +
21683 +/*
21684 + * inode operations (except add/del/rename)
21685 + */
21686 +
21687 +#include <linux/device_cgroup.h>
21688 +#include <linux/fs_stack.h>
21689 +#include <linux/iversion.h>
21690 +#include <linux/security.h>
21691 +#include "aufs.h"
21692 +
21693 +static int h_permission(struct inode *h_inode, int mask,
21694 +                       struct path *h_path, int brperm)
21695 +{
21696 +       int err;
21697 +       const unsigned char write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
21698 +       struct user_namespace *h_userns;
21699 +
21700 +       err = -EPERM;
21701 +       if (write_mask && IS_IMMUTABLE(h_inode))
21702 +               goto out;
21703 +
21704 +       err = -EACCES;
21705 +       if (((mask & MAY_EXEC)
21706 +            && S_ISREG(h_inode->i_mode)
21707 +            && (path_noexec(h_path)
21708 +                || !(h_inode->i_mode & 0111))))
21709 +               goto out;
21710 +
21711 +       /*
21712 +        * - skip the lower fs test in the case of write to ro branch.
21713 +        * - nfs dir permission write check is optimized, but a policy for
21714 +        *   link/rename requires a real check.
21715 +        * - nfs always sets SB_POSIXACL regardless its mount option 'noacl.'
21716 +        *   in this case, generic_permission() returns -EOPNOTSUPP.
21717 +        */
21718 +       h_userns = mnt_user_ns(h_path->mnt);
21719 +       if ((write_mask && !au_br_writable(brperm))
21720 +           || (au_test_nfs(h_inode->i_sb) && S_ISDIR(h_inode->i_mode)
21721 +               && write_mask && !(mask & MAY_READ))
21722 +           || !h_inode->i_op->permission) {
21723 +               /* AuLabel(generic_permission); */
21724 +               /* AuDbg("get_acl %ps\n", h_inode->i_op->get_acl); */
21725 +               err = generic_permission(h_userns, h_inode, mask);
21726 +               if (err == -EOPNOTSUPP && au_test_nfs_noacl(h_inode))
21727 +                       err = h_inode->i_op->permission(h_userns, h_inode,
21728 +                                                       mask);
21729 +               AuTraceErr(err);
21730 +       } else {
21731 +               /* AuLabel(h_inode->permission); */
21732 +               err = h_inode->i_op->permission(h_userns, h_inode, mask);
21733 +               AuTraceErr(err);
21734 +       }
21735 +
21736 +       if (!err)
21737 +               err = devcgroup_inode_permission(h_inode, mask);
21738 +       if (!err)
21739 +               err = security_inode_permission(h_inode, mask);
21740 +
21741 +out:
21742 +       return err;
21743 +}
21744 +
21745 +static int aufs_permission(struct user_namespace *userns, struct inode *inode,
21746 +                          int mask)
21747 +{
21748 +       int err;
21749 +       aufs_bindex_t bindex, bbot;
21750 +       const unsigned char isdir = !!S_ISDIR(inode->i_mode),
21751 +               write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
21752 +       struct inode *h_inode;
21753 +       struct super_block *sb;
21754 +       struct au_branch *br;
21755 +
21756 +       /* todo: support rcu-walk? */
21757 +       if (mask & MAY_NOT_BLOCK)
21758 +               return -ECHILD;
21759 +
21760 +       sb = inode->i_sb;
21761 +       si_read_lock(sb, AuLock_FLUSH);
21762 +       ii_read_lock_child(inode);
21763 +#if 0 /* reserved for future use */
21764 +       /*
21765 +        * This test may be rather 'too much' since the test is essentially done
21766 +        * in the aufs_lookup().  Theoretically it is possible that the inode
21767 +        * generation doesn't match to the superblock's here.  But it isn't a
21768 +        * big deal I suppose.
21769 +        */
21770 +       err = au_iigen_test(inode, au_sigen(sb));
21771 +       if (unlikely(err))
21772 +               goto out;
21773 +#endif
21774 +
21775 +       if (!isdir
21776 +           || write_mask
21777 +           || au_opt_test(au_mntflags(sb), DIRPERM1)) {
21778 +               err = au_busy_or_stale();
21779 +               h_inode = au_h_iptr(inode, au_ibtop(inode));
21780 +               if (unlikely(!h_inode
21781 +                            || (h_inode->i_mode & S_IFMT)
21782 +                            != (inode->i_mode & S_IFMT)))
21783 +                       goto out;
21784 +
21785 +               err = 0;
21786 +               bindex = au_ibtop(inode);
21787 +               br = au_sbr(sb, bindex);
21788 +               err = h_permission(h_inode, mask, &br->br_path, br->br_perm);
21789 +               if (write_mask
21790 +                   && !err
21791 +                   && !special_file(h_inode->i_mode)) {
21792 +                       /* test whether the upper writable branch exists */
21793 +                       err = -EROFS;
21794 +                       for (; bindex >= 0; bindex--)
21795 +                               if (!au_br_rdonly(au_sbr(sb, bindex))) {
21796 +                                       err = 0;
21797 +                                       break;
21798 +                               }
21799 +               }
21800 +               goto out;
21801 +       }
21802 +
21803 +       /* non-write to dir */
21804 +       err = 0;
21805 +       bbot = au_ibbot(inode);
21806 +       for (bindex = au_ibtop(inode); !err && bindex <= bbot; bindex++) {
21807 +               h_inode = au_h_iptr(inode, bindex);
21808 +               if (h_inode) {
21809 +                       err = au_busy_or_stale();
21810 +                       if (unlikely(!S_ISDIR(h_inode->i_mode)))
21811 +                               break;
21812 +
21813 +                       br = au_sbr(sb, bindex);
21814 +                       err = h_permission(h_inode, mask, &br->br_path,
21815 +                                          br->br_perm);
21816 +               }
21817 +       }
21818 +
21819 +out:
21820 +       ii_read_unlock(inode);
21821 +       si_read_unlock(sb);
21822 +       return err;
21823 +}
21824 +
21825 +/* ---------------------------------------------------------------------- */
21826 +
21827 +static struct dentry *aufs_lookup(struct inode *dir, struct dentry *dentry,
21828 +                                 unsigned int flags)
21829 +{
21830 +       struct dentry *ret, *parent;
21831 +       struct inode *inode;
21832 +       struct super_block *sb;
21833 +       int err, npositive;
21834 +
21835 +       IMustLock(dir);
21836 +
21837 +       /* todo: support rcu-walk? */
21838 +       ret = ERR_PTR(-ECHILD);
21839 +       if (flags & LOOKUP_RCU)
21840 +               goto out;
21841 +
21842 +       ret = ERR_PTR(-ENAMETOOLONG);
21843 +       if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
21844 +               goto out;
21845 +
21846 +       sb = dir->i_sb;
21847 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
21848 +       ret = ERR_PTR(err);
21849 +       if (unlikely(err))
21850 +               goto out;
21851 +
21852 +       err = au_di_init(dentry);
21853 +       ret = ERR_PTR(err);
21854 +       if (unlikely(err))
21855 +               goto out_si;
21856 +
21857 +       inode = NULL;
21858 +       npositive = 0; /* suppress a warning */
21859 +       parent = dentry->d_parent; /* dir inode is locked */
21860 +       di_read_lock_parent(parent, AuLock_IR);
21861 +       err = au_alive_dir(parent);
21862 +       if (!err)
21863 +               err = au_digen_test(parent, au_sigen(sb));
21864 +       if (!err) {
21865 +               /* regardless LOOKUP_CREATE, always ALLOW_NEG */
21866 +               npositive = au_lkup_dentry(dentry, au_dbtop(parent),
21867 +                                          AuLkup_ALLOW_NEG);
21868 +               err = npositive;
21869 +       }
21870 +       di_read_unlock(parent, AuLock_IR);
21871 +       ret = ERR_PTR(err);
21872 +       if (unlikely(err < 0))
21873 +               goto out_unlock;
21874 +
21875 +       if (npositive) {
21876 +               inode = au_new_inode(dentry, /*must_new*/0);
21877 +               if (IS_ERR(inode)) {
21878 +                       ret = (void *)inode;
21879 +                       inode = NULL;
21880 +                       goto out_unlock;
21881 +               }
21882 +       }
21883 +
21884 +       if (inode)
21885 +               atomic_inc(&inode->i_count);
21886 +       ret = d_splice_alias(inode, dentry);
21887 +#if 0 /* reserved for future use */
21888 +       if (unlikely(d_need_lookup(dentry))) {
21889 +               spin_lock(&dentry->d_lock);
21890 +               dentry->d_flags &= ~DCACHE_NEED_LOOKUP;
21891 +               spin_unlock(&dentry->d_lock);
21892 +       } else
21893 +#endif
21894 +       if (inode) {
21895 +               if (!IS_ERR(ret)) {
21896 +                       iput(inode);
21897 +                       if (ret && ret != dentry)
21898 +                               ii_write_unlock(inode);
21899 +               } else {
21900 +                       ii_write_unlock(inode);
21901 +                       iput(inode);
21902 +                       inode = NULL;
21903 +               }
21904 +       }
21905 +
21906 +out_unlock:
21907 +       di_write_unlock(dentry);
21908 +out_si:
21909 +       si_read_unlock(sb);
21910 +out:
21911 +       return ret;
21912 +}
21913 +
21914 +/* ---------------------------------------------------------------------- */
21915 +
21916 +/*
21917 + * very dirty and complicated aufs ->atomic_open().
21918 + * aufs_atomic_open()
21919 + * + au_aopen_or_create()
21920 + *   + add_simple()
21921 + *     + vfsub_atomic_open()
21922 + *       + branch fs ->atomic_open()
21923 + *        may call the actual 'open' for h_file
21924 + *       + inc br_nfiles only if opened
21925 + * + au_aopen_no_open() or au_aopen_do_open()
21926 + *
21927 + * au_aopen_do_open()
21928 + * + finish_open()
21929 + *   + au_do_aopen()
21930 + *     + au_do_open() the body of all 'open'
21931 + *       + au_do_open_nondir()
21932 + *        set the passed h_file
21933 + *
21934 + * au_aopen_no_open()
21935 + * + finish_no_open()
21936 + */
21937 +
21938 +struct aopen_node {
21939 +       struct hlist_bl_node hblist;
21940 +       struct file *file, *h_file;
21941 +};
21942 +
21943 +static int au_do_aopen(struct inode *inode, struct file *file)
21944 +{
21945 +       struct hlist_bl_head *aopen;
21946 +       struct hlist_bl_node *pos;
21947 +       struct aopen_node *node;
21948 +       struct au_do_open_args args = {
21949 +               .aopen  = 1,
21950 +               .open   = au_do_open_nondir
21951 +       };
21952 +
21953 +       aopen = &au_sbi(inode->i_sb)->si_aopen;
21954 +       hlist_bl_lock(aopen);
21955 +       hlist_bl_for_each_entry(node, pos, aopen, hblist)
21956 +               if (node->file == file) {
21957 +                       args.h_file = node->h_file;
21958 +                       break;
21959 +               }
21960 +       hlist_bl_unlock(aopen);
21961 +       /* AuDebugOn(!args.h_file); */
21962 +
21963 +       return au_do_open(file, &args);
21964 +}
21965 +
21966 +static int au_aopen_do_open(struct file *file, struct dentry *dentry,
21967 +                           struct aopen_node *aopen_node)
21968 +{
21969 +       int err;
21970 +       struct hlist_bl_head *aopen;
21971 +
21972 +       AuLabel(here);
21973 +       aopen = &au_sbi(dentry->d_sb)->si_aopen;
21974 +       au_hbl_add(&aopen_node->hblist, aopen);
21975 +       err = finish_open(file, dentry, au_do_aopen);
21976 +       au_hbl_del(&aopen_node->hblist, aopen);
21977 +       /* AuDbgFile(file); */
21978 +       AuDbg("%pd%s%s\n", dentry,
21979 +             (file->f_mode & FMODE_CREATED) ? " created" : "",
21980 +             (file->f_mode & FMODE_OPENED) ? " opened" : "");
21981 +
21982 +       AuTraceErr(err);
21983 +       return err;
21984 +}
21985 +
21986 +static int au_aopen_no_open(struct file *file, struct dentry *dentry)
21987 +{
21988 +       int err;
21989 +
21990 +       AuLabel(here);
21991 +       dget(dentry);
21992 +       err = finish_no_open(file, dentry);
21993 +
21994 +       AuTraceErr(err);
21995 +       return err;
21996 +}
21997 +
21998 +static int aufs_atomic_open(struct inode *dir, struct dentry *dentry,
21999 +                           struct file *file, unsigned int open_flag,
22000 +                           umode_t create_mode)
22001 +{
22002 +       int err, did_open;
22003 +       unsigned int lkup_flags;
22004 +       aufs_bindex_t bindex;
22005 +       struct super_block *sb;
22006 +       struct dentry *parent, *d;
22007 +       struct vfsub_aopen_args args = {
22008 +               .open_flag      = open_flag,
22009 +               .create_mode    = create_mode
22010 +       };
22011 +       struct aopen_node aopen_node = {
22012 +               .file   = file
22013 +       };
22014 +
22015 +       IMustLock(dir);
22016 +       AuDbg("open_flag 0%o\n", open_flag);
22017 +       AuDbgDentry(dentry);
22018 +
22019 +       err = 0;
22020 +       if (!au_di(dentry)) {
22021 +               lkup_flags = LOOKUP_OPEN;
22022 +               if (open_flag & O_CREAT)
22023 +                       lkup_flags |= LOOKUP_CREATE;
22024 +               d = aufs_lookup(dir, dentry, lkup_flags);
22025 +               if (IS_ERR(d)) {
22026 +                       err = PTR_ERR(d);
22027 +                       AuTraceErr(err);
22028 +                       goto out;
22029 +               } else if (d) {
22030 +                       /*
22031 +                        * obsoleted dentry found.
22032 +                        * another error will be returned later.
22033 +                        */
22034 +                       d_drop(d);
22035 +                       AuDbgDentry(d);
22036 +                       dput(d);
22037 +               }
22038 +               AuDbgDentry(dentry);
22039 +       }
22040 +
22041 +       if (d_is_positive(dentry)
22042 +           || d_unhashed(dentry)
22043 +           || d_unlinked(dentry)
22044 +           || !(open_flag & O_CREAT)) {
22045 +               err = au_aopen_no_open(file, dentry);
22046 +               goto out; /* success */
22047 +       }
22048 +
22049 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
22050 +       if (unlikely(err))
22051 +               goto out;
22052 +
22053 +       sb = dentry->d_sb;
22054 +       parent = dentry->d_parent;      /* dir is locked */
22055 +       di_write_lock_parent(parent);
22056 +       err = au_lkup_dentry(dentry, /*btop*/0, AuLkup_ALLOW_NEG);
22057 +       if (unlikely(err < 0))
22058 +               goto out_parent;
22059 +
22060 +       AuDbgDentry(dentry);
22061 +       if (d_is_positive(dentry)) {
22062 +               err = au_aopen_no_open(file, dentry);
22063 +               goto out_parent; /* success */
22064 +       }
22065 +
22066 +       args.file = alloc_empty_file(file->f_flags, current_cred());
22067 +       err = PTR_ERR(args.file);
22068 +       if (IS_ERR(args.file))
22069 +               goto out_parent;
22070 +
22071 +       bindex = au_dbtop(dentry);
22072 +       err = au_aopen_or_create(dir, dentry, &args);
22073 +       AuTraceErr(err);
22074 +       AuDbgFile(args.file);
22075 +       file->f_mode = args.file->f_mode & ~FMODE_OPENED;
22076 +       did_open = !!(args.file->f_mode & FMODE_OPENED);
22077 +       if (!did_open) {
22078 +               fput(args.file);
22079 +               args.file = NULL;
22080 +       }
22081 +       di_write_unlock(parent);
22082 +       di_write_unlock(dentry);
22083 +       if (unlikely(err < 0)) {
22084 +               if (args.file)
22085 +                       fput(args.file);
22086 +               goto out_sb;
22087 +       }
22088 +
22089 +       if (!did_open)
22090 +               err = au_aopen_no_open(file, dentry);
22091 +       else {
22092 +               aopen_node.h_file = args.file;
22093 +               err = au_aopen_do_open(file, dentry, &aopen_node);
22094 +       }
22095 +       if (unlikely(err < 0)) {
22096 +               if (args.file)
22097 +                       fput(args.file);
22098 +               if (did_open)
22099 +                       au_lcnt_dec(&args.br->br_nfiles);
22100 +       }
22101 +       goto out_sb; /* success */
22102 +
22103 +out_parent:
22104 +       di_write_unlock(parent);
22105 +       di_write_unlock(dentry);
22106 +out_sb:
22107 +       si_read_unlock(sb);
22108 +out:
22109 +       AuTraceErr(err);
22110 +       AuDbgFile(file);
22111 +       return err;
22112 +}
22113 +
22114 +
22115 +/* ---------------------------------------------------------------------- */
22116 +
22117 +static int au_wr_dir_cpup(struct dentry *dentry, struct dentry *parent,
22118 +                         const unsigned char add_entry, aufs_bindex_t bcpup,
22119 +                         aufs_bindex_t btop)
22120 +{
22121 +       int err;
22122 +       struct dentry *h_parent;
22123 +       struct inode *h_dir;
22124 +
22125 +       if (add_entry)
22126 +               IMustLock(d_inode(parent));
22127 +       else
22128 +               di_write_lock_parent(parent);
22129 +
22130 +       err = 0;
22131 +       if (!au_h_dptr(parent, bcpup)) {
22132 +               if (btop > bcpup)
22133 +                       err = au_cpup_dirs(dentry, bcpup);
22134 +               else if (btop < bcpup)
22135 +                       err = au_cpdown_dirs(dentry, bcpup);
22136 +               else
22137 +                       BUG();
22138 +       }
22139 +       if (!err && add_entry && !au_ftest_wrdir(add_entry, TMPFILE)) {
22140 +               h_parent = au_h_dptr(parent, bcpup);
22141 +               h_dir = d_inode(h_parent);
22142 +               inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
22143 +               err = au_lkup_neg(dentry, bcpup, /*wh*/0);
22144 +               /* todo: no unlock here */
22145 +               inode_unlock_shared(h_dir);
22146 +
22147 +               AuDbg("bcpup %d\n", bcpup);
22148 +               if (!err) {
22149 +                       if (d_really_is_negative(dentry))
22150 +                               au_set_h_dptr(dentry, btop, NULL);
22151 +                       au_update_dbrange(dentry, /*do_put_zero*/0);
22152 +               }
22153 +       }
22154 +
22155 +       if (!add_entry)
22156 +               di_write_unlock(parent);
22157 +       if (!err)
22158 +               err = bcpup; /* success */
22159 +
22160 +       AuTraceErr(err);
22161 +       return err;
22162 +}
22163 +
22164 +/*
22165 + * decide the branch and the parent dir where we will create a new entry.
22166 + * returns new bindex or an error.
22167 + * copyup the parent dir if needed.
22168 + */
22169 +int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
22170 +             struct au_wr_dir_args *args)
22171 +{
22172 +       int err;
22173 +       unsigned int flags;
22174 +       aufs_bindex_t bcpup, btop, src_btop;
22175 +       const unsigned char add_entry
22176 +               = au_ftest_wrdir(args->flags, ADD_ENTRY)
22177 +               | au_ftest_wrdir(args->flags, TMPFILE);
22178 +       struct super_block *sb;
22179 +       struct dentry *parent;
22180 +       struct au_sbinfo *sbinfo;
22181 +
22182 +       sb = dentry->d_sb;
22183 +       sbinfo = au_sbi(sb);
22184 +       parent = dget_parent(dentry);
22185 +       btop = au_dbtop(dentry);
22186 +       bcpup = btop;
22187 +       if (args->force_btgt < 0) {
22188 +               if (src_dentry) {
22189 +                       src_btop = au_dbtop(src_dentry);
22190 +                       if (src_btop < btop)
22191 +                               bcpup = src_btop;
22192 +               } else if (add_entry) {
22193 +                       flags = 0;
22194 +                       if (au_ftest_wrdir(args->flags, ISDIR))
22195 +                               au_fset_wbr(flags, DIR);
22196 +                       err = AuWbrCreate(sbinfo, dentry, flags);
22197 +                       bcpup = err;
22198 +               }
22199 +
22200 +               if (bcpup < 0 || au_test_ro(sb, bcpup, d_inode(dentry))) {
22201 +                       if (add_entry)
22202 +                               err = AuWbrCopyup(sbinfo, dentry);
22203 +                       else {
22204 +                               if (!IS_ROOT(dentry)) {
22205 +                                       di_read_lock_parent(parent, !AuLock_IR);
22206 +                                       err = AuWbrCopyup(sbinfo, dentry);
22207 +                                       di_read_unlock(parent, !AuLock_IR);
22208 +                               } else
22209 +                                       err = AuWbrCopyup(sbinfo, dentry);
22210 +                       }
22211 +                       bcpup = err;
22212 +                       if (unlikely(err < 0))
22213 +                               goto out;
22214 +               }
22215 +       } else {
22216 +               bcpup = args->force_btgt;
22217 +               AuDebugOn(au_test_ro(sb, bcpup, d_inode(dentry)));
22218 +       }
22219 +
22220 +       AuDbg("btop %d, bcpup %d\n", btop, bcpup);
22221 +       err = bcpup;
22222 +       if (bcpup == btop)
22223 +               goto out; /* success */
22224 +
22225 +       /* copyup the new parent into the branch we process */
22226 +       err = au_wr_dir_cpup(dentry, parent, add_entry, bcpup, btop);
22227 +       if (err >= 0) {
22228 +               if (d_really_is_negative(dentry)) {
22229 +                       au_set_h_dptr(dentry, btop, NULL);
22230 +                       au_set_dbtop(dentry, bcpup);
22231 +                       au_set_dbbot(dentry, bcpup);
22232 +               }
22233 +               AuDebugOn(add_entry
22234 +                         && !au_ftest_wrdir(args->flags, TMPFILE)
22235 +                         && !au_h_dptr(dentry, bcpup));
22236 +       }
22237 +
22238 +out:
22239 +       dput(parent);
22240 +       return err;
22241 +}
22242 +
22243 +/* ---------------------------------------------------------------------- */
22244 +
22245 +void au_pin_hdir_unlock(struct au_pin *p)
22246 +{
22247 +       if (p->hdir)
22248 +               au_hn_inode_unlock(p->hdir);
22249 +}
22250 +
22251 +int au_pin_hdir_lock(struct au_pin *p)
22252 +{
22253 +       int err;
22254 +
22255 +       err = 0;
22256 +       if (!p->hdir)
22257 +               goto out;
22258 +
22259 +       /* even if an error happens later, keep this lock */
22260 +       au_hn_inode_lock_nested(p->hdir, p->lsc_hi);
22261 +
22262 +       err = -EBUSY;
22263 +       if (unlikely(p->hdir->hi_inode != d_inode(p->h_parent)))
22264 +               goto out;
22265 +
22266 +       err = 0;
22267 +       if (p->h_dentry)
22268 +               err = au_h_verify(p->h_dentry, p->udba, p->hdir->hi_inode,
22269 +                                 p->h_parent, p->br);
22270 +
22271 +out:
22272 +       return err;
22273 +}
22274 +
22275 +int au_pin_hdir_relock(struct au_pin *p)
22276 +{
22277 +       int err, i;
22278 +       struct inode *h_i;
22279 +       struct dentry *h_d[] = {
22280 +               p->h_dentry,
22281 +               p->h_parent
22282 +       };
22283 +
22284 +       err = au_pin_hdir_lock(p);
22285 +       if (unlikely(err))
22286 +               goto out;
22287 +
22288 +       for (i = 0; !err && i < sizeof(h_d)/sizeof(*h_d); i++) {
22289 +               if (!h_d[i])
22290 +                       continue;
22291 +               if (d_is_positive(h_d[i])) {
22292 +                       h_i = d_inode(h_d[i]);
22293 +                       err = !h_i->i_nlink;
22294 +               }
22295 +       }
22296 +
22297 +out:
22298 +       return err;
22299 +}
22300 +
22301 +static void au_pin_hdir_set_owner(struct au_pin *p, struct task_struct *task)
22302 +{
22303 +       atomic_long_set(&p->hdir->hi_inode->i_rwsem.owner, (long)task);
22304 +}
22305 +
22306 +void au_pin_hdir_acquire_nest(struct au_pin *p)
22307 +{
22308 +       if (p->hdir) {
22309 +               rwsem_acquire_nest(&p->hdir->hi_inode->i_rwsem.dep_map,
22310 +                                  p->lsc_hi, 0, NULL, _RET_IP_);
22311 +               au_pin_hdir_set_owner(p, current);
22312 +       }
22313 +}
22314 +
22315 +void au_pin_hdir_release(struct au_pin *p)
22316 +{
22317 +       if (p->hdir) {
22318 +               au_pin_hdir_set_owner(p, p->task);
22319 +               rwsem_release(&p->hdir->hi_inode->i_rwsem.dep_map, _RET_IP_);
22320 +       }
22321 +}
22322 +
22323 +struct dentry *au_pinned_h_parent(struct au_pin *pin)
22324 +{
22325 +       if (pin && pin->parent)
22326 +               return au_h_dptr(pin->parent, pin->bindex);
22327 +       return NULL;
22328 +}
22329 +
22330 +void au_unpin(struct au_pin *p)
22331 +{
22332 +       if (p->hdir)
22333 +               au_pin_hdir_unlock(p);
22334 +       if (p->h_mnt && au_ftest_pin(p->flags, MNT_WRITE))
22335 +               vfsub_mnt_drop_write(p->h_mnt);
22336 +       if (!p->hdir)
22337 +               return;
22338 +
22339 +       if (!au_ftest_pin(p->flags, DI_LOCKED))
22340 +               di_read_unlock(p->parent, AuLock_IR);
22341 +       iput(p->hdir->hi_inode);
22342 +       dput(p->parent);
22343 +       p->parent = NULL;
22344 +       p->hdir = NULL;
22345 +       p->h_mnt = NULL;
22346 +       /* do not clear p->task */
22347 +}
22348 +
22349 +int au_do_pin(struct au_pin *p)
22350 +{
22351 +       int err;
22352 +       struct super_block *sb;
22353 +       struct inode *h_dir;
22354 +
22355 +       err = 0;
22356 +       sb = p->dentry->d_sb;
22357 +       p->br = au_sbr(sb, p->bindex);
22358 +       if (IS_ROOT(p->dentry)) {
22359 +               if (au_ftest_pin(p->flags, MNT_WRITE)) {
22360 +                       p->h_mnt = au_br_mnt(p->br);
22361 +                       err = vfsub_mnt_want_write(p->h_mnt);
22362 +                       if (unlikely(err)) {
22363 +                               au_fclr_pin(p->flags, MNT_WRITE);
22364 +                               goto out_err;
22365 +                       }
22366 +               }
22367 +               goto out;
22368 +       }
22369 +
22370 +       p->h_dentry = NULL;
22371 +       if (p->bindex <= au_dbbot(p->dentry))
22372 +               p->h_dentry = au_h_dptr(p->dentry, p->bindex);
22373 +
22374 +       p->parent = dget_parent(p->dentry);
22375 +       if (!au_ftest_pin(p->flags, DI_LOCKED))
22376 +               di_read_lock(p->parent, AuLock_IR, p->lsc_di);
22377 +
22378 +       h_dir = NULL;
22379 +       p->h_parent = au_h_dptr(p->parent, p->bindex);
22380 +       p->hdir = au_hi(d_inode(p->parent), p->bindex);
22381 +       if (p->hdir)
22382 +               h_dir = p->hdir->hi_inode;
22383 +
22384 +       /*
22385 +        * udba case, or
22386 +        * if DI_LOCKED is not set, then p->parent may be different
22387 +        * and h_parent can be NULL.
22388 +        */
22389 +       if (unlikely(!p->hdir || !h_dir || !p->h_parent)) {
22390 +               err = -EBUSY;
22391 +               if (!au_ftest_pin(p->flags, DI_LOCKED))
22392 +                       di_read_unlock(p->parent, AuLock_IR);
22393 +               dput(p->parent);
22394 +               p->parent = NULL;
22395 +               goto out_err;
22396 +       }
22397 +
22398 +       if (au_ftest_pin(p->flags, MNT_WRITE)) {
22399 +               p->h_mnt = au_br_mnt(p->br);
22400 +               err = vfsub_mnt_want_write(p->h_mnt);
22401 +               if (unlikely(err)) {
22402 +                       au_fclr_pin(p->flags, MNT_WRITE);
22403 +                       if (!au_ftest_pin(p->flags, DI_LOCKED))
22404 +                               di_read_unlock(p->parent, AuLock_IR);
22405 +                       dput(p->parent);
22406 +                       p->parent = NULL;
22407 +                       goto out_err;
22408 +               }
22409 +       }
22410 +
22411 +       au_igrab(h_dir);
22412 +       err = au_pin_hdir_lock(p);
22413 +       if (!err)
22414 +               goto out; /* success */
22415 +
22416 +       au_unpin(p);
22417 +
22418 +out_err:
22419 +       pr_err("err %d\n", err);
22420 +       err = au_busy_or_stale();
22421 +out:
22422 +       return err;
22423 +}
22424 +
22425 +void au_pin_init(struct au_pin *p, struct dentry *dentry,
22426 +                aufs_bindex_t bindex, int lsc_di, int lsc_hi,
22427 +                unsigned int udba, unsigned char flags)
22428 +{
22429 +       p->dentry = dentry;
22430 +       p->udba = udba;
22431 +       p->lsc_di = lsc_di;
22432 +       p->lsc_hi = lsc_hi;
22433 +       p->flags = flags;
22434 +       p->bindex = bindex;
22435 +
22436 +       p->parent = NULL;
22437 +       p->hdir = NULL;
22438 +       p->h_mnt = NULL;
22439 +
22440 +       p->h_dentry = NULL;
22441 +       p->h_parent = NULL;
22442 +       p->br = NULL;
22443 +       p->task = current;
22444 +}
22445 +
22446 +int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
22447 +          unsigned int udba, unsigned char flags)
22448 +{
22449 +       au_pin_init(pin, dentry, bindex, AuLsc_DI_PARENT, AuLsc_I_PARENT2,
22450 +                   udba, flags);
22451 +       return au_do_pin(pin);
22452 +}
22453 +
22454 +/* ---------------------------------------------------------------------- */
22455 +
22456 +/*
22457 + * ->setattr() and ->getattr() are called in various cases.
22458 + * chmod, stat: dentry is revalidated.
22459 + * fchmod, fstat: file and dentry are not revalidated, additionally they may be
22460 + *               unhashed.
22461 + * for ->setattr(), ia->ia_file is passed from ftruncate only.
22462 + */
22463 +/* todo: consolidate with do_refresh() and simple_reval_dpath() */
22464 +int au_reval_for_attr(struct dentry *dentry, unsigned int sigen)
22465 +{
22466 +       int err;
22467 +       struct dentry *parent;
22468 +
22469 +       err = 0;
22470 +       if (au_digen_test(dentry, sigen)) {
22471 +               parent = dget_parent(dentry);
22472 +               di_read_lock_parent(parent, AuLock_IR);
22473 +               err = au_refresh_dentry(dentry, parent);
22474 +               di_read_unlock(parent, AuLock_IR);
22475 +               dput(parent);
22476 +       }
22477 +
22478 +       AuTraceErr(err);
22479 +       return err;
22480 +}
22481 +
22482 +int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
22483 +                    struct au_icpup_args *a)
22484 +{
22485 +       int err;
22486 +       loff_t sz;
22487 +       aufs_bindex_t btop, ibtop;
22488 +       struct dentry *hi_wh, *parent;
22489 +       struct inode *inode;
22490 +       struct au_wr_dir_args wr_dir_args = {
22491 +               .force_btgt     = -1,
22492 +               .flags          = 0
22493 +       };
22494 +
22495 +       if (d_is_dir(dentry))
22496 +               au_fset_wrdir(wr_dir_args.flags, ISDIR);
22497 +       /* plink or hi_wh() case */
22498 +       btop = au_dbtop(dentry);
22499 +       inode = d_inode(dentry);
22500 +       ibtop = au_ibtop(inode);
22501 +       if (btop != ibtop && !au_test_ro(inode->i_sb, ibtop, inode))
22502 +               wr_dir_args.force_btgt = ibtop;
22503 +       err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
22504 +       if (unlikely(err < 0))
22505 +               goto out;
22506 +       a->btgt = err;
22507 +       if (err != btop)
22508 +               au_fset_icpup(a->flags, DID_CPUP);
22509 +
22510 +       err = 0;
22511 +       a->pin_flags = AuPin_MNT_WRITE;
22512 +       parent = NULL;
22513 +       if (!IS_ROOT(dentry)) {
22514 +               au_fset_pin(a->pin_flags, DI_LOCKED);
22515 +               parent = dget_parent(dentry);
22516 +               di_write_lock_parent(parent);
22517 +       }
22518 +
22519 +       err = au_pin(&a->pin, dentry, a->btgt, a->udba, a->pin_flags);
22520 +       if (unlikely(err))
22521 +               goto out_parent;
22522 +
22523 +       sz = -1;
22524 +       a->h_path.dentry = au_h_dptr(dentry, btop);
22525 +       a->h_inode = d_inode(a->h_path.dentry);
22526 +       if (ia && (ia->ia_valid & ATTR_SIZE)) {
22527 +               inode_lock_shared_nested(a->h_inode, AuLsc_I_CHILD);
22528 +               if (ia->ia_size < i_size_read(a->h_inode))
22529 +                       sz = ia->ia_size;
22530 +               inode_unlock_shared(a->h_inode);
22531 +       }
22532 +
22533 +       hi_wh = NULL;
22534 +       if (au_ftest_icpup(a->flags, DID_CPUP) && d_unlinked(dentry)) {
22535 +               hi_wh = au_hi_wh(inode, a->btgt);
22536 +               if (!hi_wh) {
22537 +                       struct au_cp_generic cpg = {
22538 +                               .dentry = dentry,
22539 +                               .bdst   = a->btgt,
22540 +                               .bsrc   = -1,
22541 +                               .len    = sz,
22542 +                               .pin    = &a->pin
22543 +                       };
22544 +                       err = au_sio_cpup_wh(&cpg, /*file*/NULL);
22545 +                       if (unlikely(err))
22546 +                               goto out_unlock;
22547 +                       hi_wh = au_hi_wh(inode, a->btgt);
22548 +                       /* todo: revalidate hi_wh? */
22549 +               }
22550 +       }
22551 +
22552 +       if (parent) {
22553 +               au_pin_set_parent_lflag(&a->pin, /*lflag*/0);
22554 +               di_downgrade_lock(parent, AuLock_IR);
22555 +               dput(parent);
22556 +               parent = NULL;
22557 +       }
22558 +       if (!au_ftest_icpup(a->flags, DID_CPUP))
22559 +               goto out; /* success */
22560 +
22561 +       if (!d_unhashed(dentry)) {
22562 +               struct au_cp_generic cpg = {
22563 +                       .dentry = dentry,
22564 +                       .bdst   = a->btgt,
22565 +                       .bsrc   = btop,
22566 +                       .len    = sz,
22567 +                       .pin    = &a->pin,
22568 +                       .flags  = AuCpup_DTIME | AuCpup_HOPEN
22569 +               };
22570 +               err = au_sio_cpup_simple(&cpg);
22571 +               if (!err)
22572 +                       a->h_path.dentry = au_h_dptr(dentry, a->btgt);
22573 +       } else if (!hi_wh)
22574 +               a->h_path.dentry = au_h_dptr(dentry, a->btgt);
22575 +       else
22576 +               a->h_path.dentry = hi_wh; /* do not dget here */
22577 +
22578 +out_unlock:
22579 +       a->h_inode = d_inode(a->h_path.dentry);
22580 +       if (!err)
22581 +               goto out; /* success */
22582 +       au_unpin(&a->pin);
22583 +out_parent:
22584 +       if (parent) {
22585 +               di_write_unlock(parent);
22586 +               dput(parent);
22587 +       }
22588 +out:
22589 +       if (!err)
22590 +               inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
22591 +       return err;
22592 +}
22593 +
22594 +static int aufs_setattr(struct user_namespace *userns, struct dentry *dentry,
22595 +                       struct iattr *ia)
22596 +{
22597 +       int err;
22598 +       struct inode *inode, *delegated;
22599 +       struct super_block *sb;
22600 +       struct file *file;
22601 +       struct au_icpup_args *a;
22602 +       struct user_namespace *h_userns;
22603 +
22604 +       inode = d_inode(dentry);
22605 +       IMustLock(inode);
22606 +
22607 +       err = setattr_prepare(userns, dentry, ia);
22608 +       if (unlikely(err))
22609 +               goto out;
22610 +
22611 +       err = -ENOMEM;
22612 +       a = kzalloc(sizeof(*a), GFP_NOFS);
22613 +       if (unlikely(!a))
22614 +               goto out;
22615 +
22616 +       if (ia->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
22617 +               ia->ia_valid &= ~ATTR_MODE;
22618 +
22619 +       file = NULL;
22620 +       sb = dentry->d_sb;
22621 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
22622 +       if (unlikely(err))
22623 +               goto out_kfree;
22624 +
22625 +       if (ia->ia_valid & ATTR_FILE) {
22626 +               /* currently ftruncate(2) only */
22627 +               AuDebugOn(!d_is_reg(dentry));
22628 +               file = ia->ia_file;
22629 +               err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1,
22630 +                                           /*fi_lsc*/0);
22631 +               if (unlikely(err))
22632 +                       goto out_si;
22633 +               ia->ia_file = au_hf_top(file);
22634 +               a->udba = AuOpt_UDBA_NONE;
22635 +       } else {
22636 +               /* fchmod() doesn't pass ia_file */
22637 +               a->udba = au_opt_udba(sb);
22638 +               di_write_lock_child(dentry);
22639 +               /* no d_unlinked(), to set UDBA_NONE for root */
22640 +               if (d_unhashed(dentry))
22641 +                       a->udba = AuOpt_UDBA_NONE;
22642 +               if (a->udba != AuOpt_UDBA_NONE) {
22643 +                       AuDebugOn(IS_ROOT(dentry));
22644 +                       err = au_reval_for_attr(dentry, au_sigen(sb));
22645 +                       if (unlikely(err))
22646 +                               goto out_dentry;
22647 +               }
22648 +       }
22649 +
22650 +       err = au_pin_and_icpup(dentry, ia, a);
22651 +       if (unlikely(err < 0))
22652 +               goto out_dentry;
22653 +       if (au_ftest_icpup(a->flags, DID_CPUP)) {
22654 +               ia->ia_file = NULL;
22655 +               ia->ia_valid &= ~ATTR_FILE;
22656 +       }
22657 +
22658 +       a->h_path.mnt = au_sbr_mnt(sb, a->btgt);
22659 +       if ((ia->ia_valid & (ATTR_MODE | ATTR_CTIME))
22660 +           == (ATTR_MODE | ATTR_CTIME)) {
22661 +               err = security_path_chmod(&a->h_path, ia->ia_mode);
22662 +               if (unlikely(err))
22663 +                       goto out_unlock;
22664 +       } else if ((ia->ia_valid & (ATTR_UID | ATTR_GID))
22665 +                  && (ia->ia_valid & ATTR_CTIME)) {
22666 +               err = security_path_chown(&a->h_path, ia->ia_uid, ia->ia_gid);
22667 +               if (unlikely(err))
22668 +                       goto out_unlock;
22669 +       }
22670 +
22671 +       if (ia->ia_valid & ATTR_SIZE) {
22672 +               struct file *f;
22673 +
22674 +               if (ia->ia_size < i_size_read(inode))
22675 +                       /* unmap only */
22676 +                       truncate_setsize(inode, ia->ia_size);
22677 +
22678 +               f = NULL;
22679 +               if (ia->ia_valid & ATTR_FILE)
22680 +                       f = ia->ia_file;
22681 +               inode_unlock(a->h_inode);
22682 +               err = vfsub_trunc(&a->h_path, ia->ia_size, ia->ia_valid, f);
22683 +               inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
22684 +       } else {
22685 +               delegated = NULL;
22686 +               while (1) {
22687 +                       err = vfsub_notify_change(&a->h_path, ia, &delegated);
22688 +                       if (delegated) {
22689 +                               err = break_deleg_wait(&delegated);
22690 +                               if (!err)
22691 +                                       continue;
22692 +                       }
22693 +                       break;
22694 +               }
22695 +       }
22696 +       /*
22697 +        * regardless aufs 'acl' option setting.
22698 +        * why don't all acl-aware fs call this func from their ->setattr()?
22699 +        */
22700 +       if (!err && (ia->ia_valid & ATTR_MODE)) {
22701 +               h_userns = mnt_user_ns(a->h_path.mnt);
22702 +               err = vfsub_acl_chmod(h_userns, a->h_inode, ia->ia_mode);
22703 +       }
22704 +       if (!err)
22705 +               au_cpup_attr_changeable(inode);
22706 +
22707 +out_unlock:
22708 +       inode_unlock(a->h_inode);
22709 +       au_unpin(&a->pin);
22710 +       if (unlikely(err))
22711 +               au_update_dbtop(dentry);
22712 +out_dentry:
22713 +       di_write_unlock(dentry);
22714 +       if (file) {
22715 +               fi_write_unlock(file);
22716 +               ia->ia_file = file;
22717 +               ia->ia_valid |= ATTR_FILE;
22718 +       }
22719 +out_si:
22720 +       si_read_unlock(sb);
22721 +out_kfree:
22722 +       au_kfree_rcu(a);
22723 +out:
22724 +       AuTraceErr(err);
22725 +       return err;
22726 +}
22727 +
22728 +#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
22729 +static int au_h_path_to_set_attr(struct dentry *dentry,
22730 +                                struct au_icpup_args *a, struct path *h_path)
22731 +{
22732 +       int err;
22733 +       struct super_block *sb;
22734 +
22735 +       sb = dentry->d_sb;
22736 +       a->udba = au_opt_udba(sb);
22737 +       /* no d_unlinked(), to set UDBA_NONE for root */
22738 +       if (d_unhashed(dentry))
22739 +               a->udba = AuOpt_UDBA_NONE;
22740 +       if (a->udba != AuOpt_UDBA_NONE) {
22741 +               AuDebugOn(IS_ROOT(dentry));
22742 +               err = au_reval_for_attr(dentry, au_sigen(sb));
22743 +               if (unlikely(err))
22744 +                       goto out;
22745 +       }
22746 +       err = au_pin_and_icpup(dentry, /*ia*/NULL, a);
22747 +       if (unlikely(err < 0))
22748 +               goto out;
22749 +
22750 +       h_path->dentry = a->h_path.dentry;
22751 +       h_path->mnt = au_sbr_mnt(sb, a->btgt);
22752 +
22753 +out:
22754 +       return err;
22755 +}
22756 +
22757 +ssize_t au_sxattr(struct dentry *dentry, struct inode *inode,
22758 +                 struct au_sxattr *arg)
22759 +{
22760 +       int err;
22761 +       struct path h_path;
22762 +       struct super_block *sb;
22763 +       struct au_icpup_args *a;
22764 +       struct inode *h_inode;
22765 +       struct user_namespace *h_userns;
22766 +
22767 +       IMustLock(inode);
22768 +
22769 +       err = -ENOMEM;
22770 +       a = kzalloc(sizeof(*a), GFP_NOFS);
22771 +       if (unlikely(!a))
22772 +               goto out;
22773 +
22774 +       sb = dentry->d_sb;
22775 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
22776 +       if (unlikely(err))
22777 +               goto out_kfree;
22778 +
22779 +       h_path.dentry = NULL;   /* silence gcc */
22780 +       di_write_lock_child(dentry);
22781 +       err = au_h_path_to_set_attr(dentry, a, &h_path);
22782 +       if (unlikely(err))
22783 +               goto out_di;
22784 +       h_userns = mnt_user_ns(h_path.mnt);
22785 +
22786 +       inode_unlock(a->h_inode);
22787 +       switch (arg->type) {
22788 +       case AU_XATTR_SET:
22789 +               AuDebugOn(d_is_negative(h_path.dentry));
22790 +               err = vfsub_setxattr(h_userns, h_path.dentry,
22791 +                                    arg->u.set.name, arg->u.set.value,
22792 +                                    arg->u.set.size, arg->u.set.flags);
22793 +               break;
22794 +       case AU_ACL_SET:
22795 +               err = -EOPNOTSUPP;
22796 +               h_inode = d_inode(h_path.dentry);
22797 +               if (h_inode->i_op->set_acl) {
22798 +                       /* this will call posix_acl_update_mode */
22799 +                       err = h_inode->i_op->set_acl(h_userns, h_inode,
22800 +                                                    arg->u.acl_set.acl,
22801 +                                                    arg->u.acl_set.type);
22802 +               }
22803 +               break;
22804 +       }
22805 +       if (!err)
22806 +               au_cpup_attr_timesizes(inode);
22807 +
22808 +       au_unpin(&a->pin);
22809 +       if (unlikely(err))
22810 +               au_update_dbtop(dentry);
22811 +
22812 +out_di:
22813 +       di_write_unlock(dentry);
22814 +       si_read_unlock(sb);
22815 +out_kfree:
22816 +       au_kfree_rcu(a);
22817 +out:
22818 +       AuTraceErr(err);
22819 +       return err;
22820 +}
22821 +#endif
22822 +
22823 +static void au_refresh_iattr(struct inode *inode, struct kstat *st,
22824 +                            unsigned int nlink)
22825 +{
22826 +       unsigned int n;
22827 +
22828 +       inode->i_mode = st->mode;
22829 +       /* don't i_[ug]id_write() here */
22830 +       inode->i_uid = st->uid;
22831 +       inode->i_gid = st->gid;
22832 +       inode->i_atime = st->atime;
22833 +       inode->i_mtime = st->mtime;
22834 +       inode->i_ctime = st->ctime;
22835 +
22836 +       au_cpup_attr_nlink(inode, /*force*/0);
22837 +       if (S_ISDIR(inode->i_mode)) {
22838 +               n = inode->i_nlink;
22839 +               n -= nlink;
22840 +               n += st->nlink;
22841 +               smp_mb(); /* for i_nlink */
22842 +               /* 0 can happen */
22843 +               set_nlink(inode, n);
22844 +       }
22845 +
22846 +       spin_lock(&inode->i_lock);
22847 +       inode->i_blocks = st->blocks;
22848 +       i_size_write(inode, st->size);
22849 +       spin_unlock(&inode->i_lock);
22850 +}
22851 +
22852 +/*
22853 + * common routine for aufs_getattr() and au_getxattr().
22854 + * returns zero or negative (an error).
22855 + * @dentry will be read-locked in success.
22856 + */
22857 +int au_h_path_getattr(struct dentry *dentry, struct inode *inode, int force,
22858 +                     struct path *h_path, int locked)
22859 +{
22860 +       int err;
22861 +       unsigned int mnt_flags, sigen;
22862 +       unsigned char udba_none;
22863 +       aufs_bindex_t bindex;
22864 +       struct super_block *sb, *h_sb;
22865 +
22866 +       h_path->mnt = NULL;
22867 +       h_path->dentry = NULL;
22868 +
22869 +       err = 0;
22870 +       sb = dentry->d_sb;
22871 +       mnt_flags = au_mntflags(sb);
22872 +       udba_none = !!au_opt_test(mnt_flags, UDBA_NONE);
22873 +
22874 +       if (unlikely(locked))
22875 +               goto body; /* skip locking dinfo */
22876 +
22877 +       /* support fstat(2) */
22878 +       if (!d_unlinked(dentry) && !udba_none) {
22879 +               sigen = au_sigen(sb);
22880 +               err = au_digen_test(dentry, sigen);
22881 +               if (!err) {
22882 +                       di_read_lock_child(dentry, AuLock_IR);
22883 +                       err = au_dbrange_test(dentry);
22884 +                       if (unlikely(err)) {
22885 +                               di_read_unlock(dentry, AuLock_IR);
22886 +                               goto out;
22887 +                       }
22888 +               } else {
22889 +                       AuDebugOn(IS_ROOT(dentry));
22890 +                       di_write_lock_child(dentry);
22891 +                       err = au_dbrange_test(dentry);
22892 +                       if (!err)
22893 +                               err = au_reval_for_attr(dentry, sigen);
22894 +                       if (!err)
22895 +                               di_downgrade_lock(dentry, AuLock_IR);
22896 +                       else {
22897 +                               di_write_unlock(dentry);
22898 +                               goto out;
22899 +                       }
22900 +               }
22901 +       } else
22902 +               di_read_lock_child(dentry, AuLock_IR);
22903 +
22904 +body:
22905 +       if (!inode) {
22906 +               inode = d_inode(dentry);
22907 +               if (unlikely(!inode))
22908 +                       goto out;
22909 +       }
22910 +       bindex = au_ibtop(inode);
22911 +       h_path->mnt = au_sbr_mnt(sb, bindex);
22912 +       h_sb = h_path->mnt->mnt_sb;
22913 +       if (!force
22914 +           && !au_test_fs_bad_iattr(h_sb)
22915 +           && udba_none)
22916 +               goto out; /* success */
22917 +
22918 +       if (au_dbtop(dentry) == bindex)
22919 +               h_path->dentry = au_h_dptr(dentry, bindex);
22920 +       else if (au_opt_test(mnt_flags, PLINK) && au_plink_test(inode)) {
22921 +               h_path->dentry = au_plink_lkup(inode, bindex);
22922 +               if (IS_ERR(h_path->dentry))
22923 +                       /* pretending success */
22924 +                       h_path->dentry = NULL;
22925 +               else
22926 +                       dput(h_path->dentry);
22927 +       }
22928 +
22929 +out:
22930 +       return err;
22931 +}
22932 +
22933 +static int aufs_getattr(struct user_namespace *userns, const struct path *path,
22934 +                       struct kstat *st, u32 request, unsigned int query)
22935 +{
22936 +       int err;
22937 +       unsigned char positive;
22938 +       struct path h_path;
22939 +       struct dentry *dentry;
22940 +       struct inode *inode;
22941 +       struct super_block *sb;
22942 +
22943 +       dentry = path->dentry;
22944 +       inode = d_inode(dentry);
22945 +       sb = dentry->d_sb;
22946 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
22947 +       if (unlikely(err))
22948 +               goto out;
22949 +       err = au_h_path_getattr(dentry, /*inode*/NULL, /*force*/0, &h_path,
22950 +                               /*locked*/0);
22951 +       if (unlikely(err))
22952 +               goto out_si;
22953 +       if (unlikely(!h_path.dentry))
22954 +               /* illegally overlapped or something */
22955 +               goto out_fill; /* pretending success */
22956 +
22957 +       positive = d_is_positive(h_path.dentry);
22958 +       if (positive)
22959 +               /* no vfsub version */
22960 +               err = vfs_getattr(&h_path, st, request, query);
22961 +       if (!err) {
22962 +               if (positive)
22963 +                       au_refresh_iattr(inode, st,
22964 +                                        d_inode(h_path.dentry)->i_nlink);
22965 +               goto out_fill; /* success */
22966 +       }
22967 +       AuTraceErr(err);
22968 +       goto out_di;
22969 +
22970 +out_fill:
22971 +       generic_fillattr(userns, inode, st);
22972 +out_di:
22973 +       di_read_unlock(dentry, AuLock_IR);
22974 +out_si:
22975 +       si_read_unlock(sb);
22976 +out:
22977 +       AuTraceErr(err);
22978 +       return err;
22979 +}
22980 +
22981 +/* ---------------------------------------------------------------------- */
22982 +
22983 +static const char *aufs_get_link(struct dentry *dentry, struct inode *inode,
22984 +                                struct delayed_call *done)
22985 +{
22986 +       const char *ret;
22987 +       struct dentry *h_dentry;
22988 +       struct inode *h_inode;
22989 +       int err;
22990 +       aufs_bindex_t bindex;
22991 +
22992 +       ret = NULL; /* suppress a warning */
22993 +       err = -ECHILD;
22994 +       if (!dentry)
22995 +               goto out;
22996 +
22997 +       err = aufs_read_lock(dentry, AuLock_IR | AuLock_GEN);
22998 +       if (unlikely(err))
22999 +               goto out;
23000 +
23001 +       err = au_d_hashed_positive(dentry);
23002 +       if (unlikely(err))
23003 +               goto out_unlock;
23004 +
23005 +       err = -EINVAL;
23006 +       inode = d_inode(dentry);
23007 +       bindex = au_ibtop(inode);
23008 +       h_inode = au_h_iptr(inode, bindex);
23009 +       if (unlikely(!h_inode->i_op->get_link))
23010 +               goto out_unlock;
23011 +
23012 +       err = -EBUSY;
23013 +       h_dentry = NULL;
23014 +       if (au_dbtop(dentry) <= bindex) {
23015 +               h_dentry = au_h_dptr(dentry, bindex);
23016 +               if (h_dentry)
23017 +                       dget(h_dentry);
23018 +       }
23019 +       if (!h_dentry) {
23020 +               h_dentry = d_find_any_alias(h_inode);
23021 +               if (IS_ERR(h_dentry)) {
23022 +                       err = PTR_ERR(h_dentry);
23023 +                       goto out_unlock;
23024 +               }
23025 +       }
23026 +       if (unlikely(!h_dentry))
23027 +               goto out_unlock;
23028 +
23029 +       err = 0;
23030 +       AuDbg("%ps\n", h_inode->i_op->get_link);
23031 +       AuDbgDentry(h_dentry);
23032 +       ret = vfs_get_link(h_dentry, done);
23033 +       dput(h_dentry);
23034 +       if (IS_ERR(ret))
23035 +               err = PTR_ERR(ret);
23036 +
23037 +out_unlock:
23038 +       aufs_read_unlock(dentry, AuLock_IR);
23039 +out:
23040 +       if (unlikely(err))
23041 +               ret = ERR_PTR(err);
23042 +       AuTraceErrPtr(ret);
23043 +       return ret;
23044 +}
23045 +
23046 +/* ---------------------------------------------------------------------- */
23047 +
23048 +static int au_is_special(struct inode *inode)
23049 +{
23050 +       return (inode->i_mode & (S_IFBLK | S_IFCHR | S_IFIFO | S_IFSOCK));
23051 +}
23052 +
23053 +static int aufs_update_time(struct inode *inode, struct timespec64 *ts,
23054 +                           int flags)
23055 +{
23056 +       int err;
23057 +       aufs_bindex_t bindex;
23058 +       struct super_block *sb;
23059 +       struct inode *h_inode;
23060 +       struct vfsmount *h_mnt;
23061 +
23062 +       sb = inode->i_sb;
23063 +       WARN_ONCE((flags & S_ATIME) && !IS_NOATIME(inode),
23064 +                 "unexpected s_flags 0x%lx", sb->s_flags);
23065 +
23066 +       /* mmap_sem might be acquired already, cf. aufs_mmap() */
23067 +       lockdep_off();
23068 +       si_read_lock(sb, AuLock_FLUSH);
23069 +       ii_write_lock_child(inode);
23070 +
23071 +       err = 0;
23072 +       bindex = au_ibtop(inode);
23073 +       h_inode = au_h_iptr(inode, bindex);
23074 +       if (!au_test_ro(sb, bindex, inode)) {
23075 +               h_mnt = au_sbr_mnt(sb, bindex);
23076 +               err = vfsub_mnt_want_write(h_mnt);
23077 +               if (!err) {
23078 +                       err = vfsub_update_time(h_inode, ts, flags);
23079 +                       vfsub_mnt_drop_write(h_mnt);
23080 +               }
23081 +       } else if (au_is_special(h_inode)) {
23082 +               /*
23083 +                * Never copy-up here.
23084 +                * These special files may already be opened and used for
23085 +                * communicating. If we copied it up, then the communication
23086 +                * would be corrupted.
23087 +                */
23088 +               AuWarn1("timestamps for i%lu are ignored "
23089 +                       "since it is on readonly branch (hi%lu).\n",
23090 +                       inode->i_ino, h_inode->i_ino);
23091 +       } else if (flags & ~S_ATIME) {
23092 +               err = -EIO;
23093 +               AuIOErr1("unexpected flags 0x%x\n", flags);
23094 +               AuDebugOn(1);
23095 +       }
23096 +
23097 +       if (!err)
23098 +               au_cpup_attr_timesizes(inode);
23099 +       ii_write_unlock(inode);
23100 +       si_read_unlock(sb);
23101 +       lockdep_on();
23102 +
23103 +       if (!err && (flags & S_VERSION))
23104 +               inode_inc_iversion(inode);
23105 +
23106 +       return err;
23107 +}
23108 +
23109 +/* ---------------------------------------------------------------------- */
23110 +
23111 +/* no getattr version will be set by module.c:aufs_init() */
23112 +struct inode_operations aufs_iop_nogetattr[AuIop_Last],
23113 +       aufs_iop[] = {
23114 +       [AuIop_SYMLINK] = {
23115 +               .permission     = aufs_permission,
23116 +#ifdef CONFIG_FS_POSIX_ACL
23117 +               .get_acl        = aufs_get_acl,
23118 +               .set_acl        = aufs_set_acl, /* unsupport for symlink? */
23119 +#endif
23120 +
23121 +               .setattr        = aufs_setattr,
23122 +               .getattr        = aufs_getattr,
23123 +
23124 +#ifdef CONFIG_AUFS_XATTR
23125 +               .listxattr      = aufs_listxattr,
23126 +#endif
23127 +
23128 +               .get_link       = aufs_get_link,
23129 +
23130 +               /* .update_time = aufs_update_time */
23131 +       },
23132 +       [AuIop_DIR] = {
23133 +               .create         = aufs_create,
23134 +               .lookup         = aufs_lookup,
23135 +               .link           = aufs_link,
23136 +               .unlink         = aufs_unlink,
23137 +               .symlink        = aufs_symlink,
23138 +               .mkdir          = aufs_mkdir,
23139 +               .rmdir          = aufs_rmdir,
23140 +               .mknod          = aufs_mknod,
23141 +               .rename         = aufs_rename,
23142 +
23143 +               .permission     = aufs_permission,
23144 +#ifdef CONFIG_FS_POSIX_ACL
23145 +               .get_acl        = aufs_get_acl,
23146 +               .set_acl        = aufs_set_acl,
23147 +#endif
23148 +
23149 +               .setattr        = aufs_setattr,
23150 +               .getattr        = aufs_getattr,
23151 +
23152 +#ifdef CONFIG_AUFS_XATTR
23153 +               .listxattr      = aufs_listxattr,
23154 +#endif
23155 +
23156 +               .update_time    = aufs_update_time,
23157 +               .atomic_open    = aufs_atomic_open,
23158 +               .tmpfile        = aufs_tmpfile
23159 +       },
23160 +       [AuIop_OTHER] = {
23161 +               .permission     = aufs_permission,
23162 +#ifdef CONFIG_FS_POSIX_ACL
23163 +               .get_acl        = aufs_get_acl,
23164 +               .set_acl        = aufs_set_acl,
23165 +#endif
23166 +
23167 +               .setattr        = aufs_setattr,
23168 +               .getattr        = aufs_getattr,
23169 +
23170 +#ifdef CONFIG_AUFS_XATTR
23171 +               .listxattr      = aufs_listxattr,
23172 +#endif
23173 +
23174 +               .update_time    = aufs_update_time
23175 +       }
23176 +};
23177 diff -urN /usr/share/empty/fs/aufs/i_op_del.c linux/fs/aufs/i_op_del.c
23178 --- /usr/share/empty/fs/aufs/i_op_del.c 1970-01-01 01:00:00.000000000 +0100
23179 +++ linux/fs/aufs/i_op_del.c    2022-03-21 14:49:05.726633010 +0100
23180 @@ -0,0 +1,522 @@
23181 +// SPDX-License-Identifier: GPL-2.0
23182 +/*
23183 + * Copyright (C) 2005-2021 Junjiro R. Okajima
23184 + *
23185 + * This program, aufs is free software; you can redistribute it and/or modify
23186 + * it under the terms of the GNU General Public License as published by
23187 + * the Free Software Foundation; either version 2 of the License, or
23188 + * (at your option) any later version.
23189 + *
23190 + * This program is distributed in the hope that it will be useful,
23191 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
23192 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23193 + * GNU General Public License for more details.
23194 + *
23195 + * You should have received a copy of the GNU General Public License
23196 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23197 + */
23198 +
23199 +/*
23200 + * inode operations (del entry)
23201 + */
23202 +
23203 +#include <linux/iversion.h>
23204 +#include "aufs.h"
23205 +
23206 +/*
23207 + * decide if a new whiteout for @dentry is necessary or not.
23208 + * when it is necessary, prepare the parent dir for the upper branch whose
23209 + * branch index is @bcpup for creation. the actual creation of the whiteout will
23210 + * be done by caller.
23211 + * return value:
23212 + * 0: wh is unnecessary
23213 + * plus: wh is necessary
23214 + * minus: error
23215 + */
23216 +int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup)
23217 +{
23218 +       int need_wh, err;
23219 +       aufs_bindex_t btop;
23220 +       struct super_block *sb;
23221 +
23222 +       sb = dentry->d_sb;
23223 +       btop = au_dbtop(dentry);
23224 +       if (*bcpup < 0) {
23225 +               *bcpup = btop;
23226 +               if (au_test_ro(sb, btop, d_inode(dentry))) {
23227 +                       err = AuWbrCopyup(au_sbi(sb), dentry);
23228 +                       *bcpup = err;
23229 +                       if (unlikely(err < 0))
23230 +                               goto out;
23231 +               }
23232 +       } else
23233 +               AuDebugOn(btop < *bcpup
23234 +                         || au_test_ro(sb, *bcpup, d_inode(dentry)));
23235 +       AuDbg("bcpup %d, btop %d\n", *bcpup, btop);
23236 +
23237 +       if (*bcpup != btop) {
23238 +               err = au_cpup_dirs(dentry, *bcpup);
23239 +               if (unlikely(err))
23240 +                       goto out;
23241 +               need_wh = 1;
23242 +       } else {
23243 +               struct au_dinfo *dinfo, *tmp;
23244 +
23245 +               need_wh = -ENOMEM;
23246 +               dinfo = au_di(dentry);
23247 +               tmp = au_di_alloc(sb, AuLsc_DI_TMP);
23248 +               if (tmp) {
23249 +                       au_di_cp(tmp, dinfo);
23250 +                       au_di_swap(tmp, dinfo);
23251 +                       /* returns the number of positive dentries */
23252 +                       need_wh = au_lkup_dentry(dentry, btop + 1,
23253 +                                                /* AuLkup_IGNORE_PERM */ 0);
23254 +                       au_di_swap(tmp, dinfo);
23255 +                       au_rw_write_unlock(&tmp->di_rwsem);
23256 +                       au_di_free(tmp);
23257 +               }
23258 +       }
23259 +       AuDbg("need_wh %d\n", need_wh);
23260 +       err = need_wh;
23261 +
23262 +out:
23263 +       return err;
23264 +}
23265 +
23266 +/*
23267 + * simple tests for the del-entry operations.
23268 + * following the checks in vfs, plus the parent-child relationship.
23269 + */
23270 +int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
23271 +              struct dentry *h_parent, int isdir)
23272 +{
23273 +       int err;
23274 +       umode_t h_mode;
23275 +       struct dentry *h_dentry, *h_latest;
23276 +       struct inode *h_inode;
23277 +       struct path h_ppath;
23278 +       struct super_block *sb;
23279 +       struct au_branch *br;
23280 +       struct user_namespace *h_userns;
23281 +
23282 +       h_dentry = au_h_dptr(dentry, bindex);
23283 +       if (d_really_is_positive(dentry)) {
23284 +               err = -ENOENT;
23285 +               if (unlikely(d_is_negative(h_dentry)))
23286 +                       goto out;
23287 +               h_inode = d_inode(h_dentry);
23288 +               if (unlikely(!h_inode->i_nlink))
23289 +                       goto out;
23290 +
23291 +               h_mode = h_inode->i_mode;
23292 +               if (!isdir) {
23293 +                       err = -EISDIR;
23294 +                       if (unlikely(S_ISDIR(h_mode)))
23295 +                               goto out;
23296 +               } else if (unlikely(!S_ISDIR(h_mode))) {
23297 +                       err = -ENOTDIR;
23298 +                       goto out;
23299 +               }
23300 +       } else {
23301 +               /* rename(2) case */
23302 +               err = -EIO;
23303 +               if (unlikely(d_is_positive(h_dentry)))
23304 +                       goto out;
23305 +       }
23306 +
23307 +       err = -ENOENT;
23308 +       /* expected parent dir is locked */
23309 +       if (unlikely(h_parent != h_dentry->d_parent))
23310 +               goto out;
23311 +       err = 0;
23312 +
23313 +       /*
23314 +        * rmdir a dir may break the consistency on some filesystem.
23315 +        * let's try heavy test.
23316 +        */
23317 +       err = -EACCES;
23318 +       sb = dentry->d_sb;
23319 +       br = au_sbr(sb, bindex);
23320 +       h_userns = au_br_userns(br);
23321 +       if (unlikely(!au_opt_test(au_mntflags(sb), DIRPERM1)
23322 +                    && au_test_h_perm(h_userns, d_inode(h_parent),
23323 +                                      MAY_EXEC | MAY_WRITE)))
23324 +               goto out;
23325 +
23326 +       h_ppath.dentry = h_parent;
23327 +       h_ppath.mnt = au_br_mnt(br);
23328 +       h_latest = au_sio_lkup_one(h_userns, &dentry->d_name, &h_ppath);
23329 +       err = -EIO;
23330 +       if (IS_ERR(h_latest))
23331 +               goto out;
23332 +       if (h_latest == h_dentry)
23333 +               err = 0;
23334 +       dput(h_latest);
23335 +
23336 +out:
23337 +       return err;
23338 +}
23339 +
23340 +/*
23341 + * decide the branch where we operate for @dentry. the branch index will be set
23342 + * @rbcpup. after deciding it, 'pin' it and store the timestamps of the parent
23343 + * dir for reverting.
23344 + * when a new whiteout is necessary, create it.
23345 + */
23346 +static struct dentry*
23347 +lock_hdir_create_wh(struct dentry *dentry, int isdir, aufs_bindex_t *rbcpup,
23348 +                   struct au_dtime *dt, struct au_pin *pin)
23349 +{
23350 +       struct dentry *wh_dentry;
23351 +       struct super_block *sb;
23352 +       struct path h_path;
23353 +       int err, need_wh;
23354 +       unsigned int udba;
23355 +       aufs_bindex_t bcpup;
23356 +
23357 +       need_wh = au_wr_dir_need_wh(dentry, isdir, rbcpup);
23358 +       wh_dentry = ERR_PTR(need_wh);
23359 +       if (unlikely(need_wh < 0))
23360 +               goto out;
23361 +
23362 +       sb = dentry->d_sb;
23363 +       udba = au_opt_udba(sb);
23364 +       bcpup = *rbcpup;
23365 +       err = au_pin(pin, dentry, bcpup, udba,
23366 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
23367 +       wh_dentry = ERR_PTR(err);
23368 +       if (unlikely(err))
23369 +               goto out;
23370 +
23371 +       h_path.dentry = au_pinned_h_parent(pin);
23372 +       if (udba != AuOpt_UDBA_NONE
23373 +           && au_dbtop(dentry) == bcpup) {
23374 +               err = au_may_del(dentry, bcpup, h_path.dentry, isdir);
23375 +               wh_dentry = ERR_PTR(err);
23376 +               if (unlikely(err))
23377 +                       goto out_unpin;
23378 +       }
23379 +
23380 +       h_path.mnt = au_sbr_mnt(sb, bcpup);
23381 +       au_dtime_store(dt, au_pinned_parent(pin), &h_path);
23382 +       wh_dentry = NULL;
23383 +       if (!need_wh)
23384 +               goto out; /* success, no need to create whiteout */
23385 +
23386 +       wh_dentry = au_wh_create(dentry, bcpup, h_path.dentry);
23387 +       if (IS_ERR(wh_dentry))
23388 +               goto out_unpin;
23389 +
23390 +       /* returns with the parent is locked and wh_dentry is dget-ed */
23391 +       goto out; /* success */
23392 +
23393 +out_unpin:
23394 +       au_unpin(pin);
23395 +out:
23396 +       return wh_dentry;
23397 +}
23398 +
23399 +/*
23400 + * when removing a dir, rename it to a unique temporary whiteout-ed name first
23401 + * in order to be revertible and save time for removing many child whiteouts
23402 + * under the dir.
23403 + * returns 1 when there are too many child whiteout and caller should remove
23404 + * them asynchronously. returns 0 when the number of children is enough small to
23405 + * remove now or the branch fs is a remote fs.
23406 + * otherwise return an error.
23407 + */
23408 +static int renwh_and_rmdir(struct dentry *dentry, aufs_bindex_t bindex,
23409 +                          struct au_nhash *whlist, struct inode *dir)
23410 +{
23411 +       int rmdir_later, err, dirwh;
23412 +       struct dentry *h_dentry;
23413 +       struct super_block *sb;
23414 +       struct inode *inode;
23415 +
23416 +       sb = dentry->d_sb;
23417 +       SiMustAnyLock(sb);
23418 +       h_dentry = au_h_dptr(dentry, bindex);
23419 +       err = au_whtmp_ren(h_dentry, au_sbr(sb, bindex));
23420 +       if (unlikely(err))
23421 +               goto out;
23422 +
23423 +       /* stop monitoring */
23424 +       inode = d_inode(dentry);
23425 +       au_hn_free(au_hi(inode, bindex));
23426 +
23427 +       if (!au_test_fs_remote(h_dentry->d_sb)) {
23428 +               dirwh = au_sbi(sb)->si_dirwh;
23429 +               rmdir_later = (dirwh <= 1);
23430 +               if (!rmdir_later)
23431 +                       rmdir_later = au_nhash_test_longer_wh(whlist, bindex,
23432 +                                                             dirwh);
23433 +               if (rmdir_later)
23434 +                       return rmdir_later;
23435 +       }
23436 +
23437 +       err = au_whtmp_rmdir(dir, bindex, h_dentry, whlist);
23438 +       if (unlikely(err)) {
23439 +               AuIOErr("rmdir %pd, b%d failed, %d. ignored\n",
23440 +                       h_dentry, bindex, err);
23441 +               err = 0;
23442 +       }
23443 +
23444 +out:
23445 +       AuTraceErr(err);
23446 +       return err;
23447 +}
23448 +
23449 +/*
23450 + * final procedure for deleting a entry.
23451 + * maintain dentry and iattr.
23452 + */
23453 +static void epilog(struct inode *dir, struct dentry *dentry,
23454 +                  aufs_bindex_t bindex)
23455 +{
23456 +       struct inode *inode;
23457 +
23458 +       inode = d_inode(dentry);
23459 +       d_drop(dentry);
23460 +       inode->i_ctime = dir->i_ctime;
23461 +
23462 +       au_dir_ts(dir, bindex);
23463 +       inode_inc_iversion(dir);
23464 +}
23465 +
23466 +/*
23467 + * when an error happened, remove the created whiteout and revert everything.
23468 + */
23469 +static int do_revert(int err, struct inode *dir, aufs_bindex_t bindex,
23470 +                    aufs_bindex_t bwh, struct dentry *wh_dentry,
23471 +                    struct dentry *dentry, struct au_dtime *dt)
23472 +{
23473 +       int rerr;
23474 +       struct path h_path = {
23475 +               .dentry = wh_dentry,
23476 +               .mnt    = au_sbr_mnt(dir->i_sb, bindex)
23477 +       };
23478 +
23479 +       rerr = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path, dentry);
23480 +       if (!rerr) {
23481 +               au_set_dbwh(dentry, bwh);
23482 +               au_dtime_revert(dt);
23483 +               return 0;
23484 +       }
23485 +
23486 +       AuIOErr("%pd reverting whiteout failed(%d, %d)\n", dentry, err, rerr);
23487 +       return -EIO;
23488 +}
23489 +
23490 +/* ---------------------------------------------------------------------- */
23491 +
23492 +int aufs_unlink(struct inode *dir, struct dentry *dentry)
23493 +{
23494 +       int err;
23495 +       aufs_bindex_t bwh, bindex, btop;
23496 +       struct inode *inode, *h_dir, *delegated;
23497 +       struct dentry *parent, *wh_dentry;
23498 +       /* to reduce stack size */
23499 +       struct {
23500 +               struct au_dtime dt;
23501 +               struct au_pin pin;
23502 +               struct path h_path;
23503 +       } *a;
23504 +
23505 +       IMustLock(dir);
23506 +
23507 +       err = -ENOMEM;
23508 +       a = kmalloc(sizeof(*a), GFP_NOFS);
23509 +       if (unlikely(!a))
23510 +               goto out;
23511 +
23512 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
23513 +       if (unlikely(err))
23514 +               goto out_free;
23515 +       err = au_d_hashed_positive(dentry);
23516 +       if (unlikely(err))
23517 +               goto out_unlock;
23518 +       inode = d_inode(dentry);
23519 +       IMustLock(inode);
23520 +       err = -EISDIR;
23521 +       if (unlikely(d_is_dir(dentry)))
23522 +               goto out_unlock; /* possible? */
23523 +
23524 +       btop = au_dbtop(dentry);
23525 +       bwh = au_dbwh(dentry);
23526 +       bindex = -1;
23527 +       parent = dentry->d_parent; /* dir inode is locked */
23528 +       di_write_lock_parent(parent);
23529 +       wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/0, &bindex, &a->dt,
23530 +                                       &a->pin);
23531 +       err = PTR_ERR(wh_dentry);
23532 +       if (IS_ERR(wh_dentry))
23533 +               goto out_parent;
23534 +
23535 +       a->h_path.mnt = au_sbr_mnt(dentry->d_sb, btop);
23536 +       a->h_path.dentry = au_h_dptr(dentry, btop);
23537 +       dget(a->h_path.dentry);
23538 +       if (bindex == btop) {
23539 +               h_dir = au_pinned_h_dir(&a->pin);
23540 +               delegated = NULL;
23541 +               err = vfsub_unlink(h_dir, &a->h_path, &delegated, /*force*/0);
23542 +               if (unlikely(err == -EWOULDBLOCK)) {
23543 +                       pr_warn("cannot retry for NFSv4 delegation"
23544 +                               " for an internal unlink\n");
23545 +                       iput(delegated);
23546 +               }
23547 +       } else {
23548 +               /* dir inode is locked */
23549 +               h_dir = d_inode(wh_dentry->d_parent);
23550 +               IMustLock(h_dir);
23551 +               err = 0;
23552 +       }
23553 +
23554 +       if (!err) {
23555 +               vfsub_drop_nlink(inode);
23556 +               epilog(dir, dentry, bindex);
23557 +
23558 +               /* update target timestamps */
23559 +               if (bindex == btop) {
23560 +                       vfsub_update_h_iattr(&a->h_path, /*did*/NULL);
23561 +                       /*ignore*/
23562 +                       inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
23563 +               } else
23564 +                       /* todo: this timestamp may be reverted later */
23565 +                       inode->i_ctime = h_dir->i_ctime;
23566 +               goto out_unpin; /* success */
23567 +       }
23568 +
23569 +       /* revert */
23570 +       if (wh_dentry) {
23571 +               int rerr;
23572 +
23573 +               rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
23574 +                                &a->dt);
23575 +               if (rerr)
23576 +                       err = rerr;
23577 +       }
23578 +
23579 +out_unpin:
23580 +       au_unpin(&a->pin);
23581 +       dput(wh_dentry);
23582 +       dput(a->h_path.dentry);
23583 +out_parent:
23584 +       di_write_unlock(parent);
23585 +out_unlock:
23586 +       aufs_read_unlock(dentry, AuLock_DW);
23587 +out_free:
23588 +       au_kfree_rcu(a);
23589 +out:
23590 +       return err;
23591 +}
23592 +
23593 +int aufs_rmdir(struct inode *dir, struct dentry *dentry)
23594 +{
23595 +       int err, rmdir_later;
23596 +       aufs_bindex_t bwh, bindex, btop;
23597 +       struct inode *inode;
23598 +       struct dentry *parent, *wh_dentry, *h_dentry;
23599 +       struct au_whtmp_rmdir *args;
23600 +       /* to reduce stack size */
23601 +       struct {
23602 +               struct au_dtime dt;
23603 +               struct au_pin pin;
23604 +       } *a;
23605 +
23606 +       IMustLock(dir);
23607 +
23608 +       err = -ENOMEM;
23609 +       a = kmalloc(sizeof(*a), GFP_NOFS);
23610 +       if (unlikely(!a))
23611 +               goto out;
23612 +
23613 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
23614 +       if (unlikely(err))
23615 +               goto out_free;
23616 +       err = au_alive_dir(dentry);
23617 +       if (unlikely(err))
23618 +               goto out_unlock;
23619 +       inode = d_inode(dentry);
23620 +       IMustLock(inode);
23621 +       err = -ENOTDIR;
23622 +       if (unlikely(!d_is_dir(dentry)))
23623 +               goto out_unlock; /* possible? */
23624 +
23625 +       err = -ENOMEM;
23626 +       args = au_whtmp_rmdir_alloc(dir->i_sb, GFP_NOFS);
23627 +       if (unlikely(!args))
23628 +               goto out_unlock;
23629 +
23630 +       parent = dentry->d_parent; /* dir inode is locked */
23631 +       di_write_lock_parent(parent);
23632 +       err = au_test_empty(dentry, &args->whlist);
23633 +       if (unlikely(err))
23634 +               goto out_parent;
23635 +
23636 +       btop = au_dbtop(dentry);
23637 +       bwh = au_dbwh(dentry);
23638 +       bindex = -1;
23639 +       wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/1, &bindex, &a->dt,
23640 +                                       &a->pin);
23641 +       err = PTR_ERR(wh_dentry);
23642 +       if (IS_ERR(wh_dentry))
23643 +               goto out_parent;
23644 +
23645 +       h_dentry = au_h_dptr(dentry, btop);
23646 +       dget(h_dentry);
23647 +       rmdir_later = 0;
23648 +       if (bindex == btop) {
23649 +               err = renwh_and_rmdir(dentry, btop, &args->whlist, dir);
23650 +               if (err > 0) {
23651 +                       rmdir_later = err;
23652 +                       err = 0;
23653 +               }
23654 +       } else {
23655 +               /* stop monitoring */
23656 +               au_hn_free(au_hi(inode, btop));
23657 +
23658 +               /* dir inode is locked */
23659 +               IMustLock(d_inode(wh_dentry->d_parent));
23660 +               err = 0;
23661 +       }
23662 +
23663 +       if (!err) {
23664 +               vfsub_dead_dir(inode);
23665 +               au_set_dbdiropq(dentry, -1);
23666 +               epilog(dir, dentry, bindex);
23667 +
23668 +               if (rmdir_later) {
23669 +                       au_whtmp_kick_rmdir(dir, btop, h_dentry, args);
23670 +                       args = NULL;
23671 +               }
23672 +
23673 +               goto out_unpin; /* success */
23674 +       }
23675 +
23676 +       /* revert */
23677 +       AuLabel(revert);
23678 +       if (wh_dentry) {
23679 +               int rerr;
23680 +
23681 +               rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
23682 +                                &a->dt);
23683 +               if (rerr)
23684 +                       err = rerr;
23685 +       }
23686 +
23687 +out_unpin:
23688 +       au_unpin(&a->pin);
23689 +       dput(wh_dentry);
23690 +       dput(h_dentry);
23691 +out_parent:
23692 +       di_write_unlock(parent);
23693 +       if (args)
23694 +               au_whtmp_rmdir_free(args);
23695 +out_unlock:
23696 +       aufs_read_unlock(dentry, AuLock_DW);
23697 +out_free:
23698 +       au_kfree_rcu(a);
23699 +out:
23700 +       AuTraceErr(err);
23701 +       return err;
23702 +}
23703 diff -urN /usr/share/empty/fs/aufs/i_op_ren.c linux/fs/aufs/i_op_ren.c
23704 --- /usr/share/empty/fs/aufs/i_op_ren.c 1970-01-01 01:00:00.000000000 +0100
23705 +++ linux/fs/aufs/i_op_ren.c    2022-03-21 14:49:05.726633010 +0100
23706 @@ -0,0 +1,1257 @@
23707 +// SPDX-License-Identifier: GPL-2.0
23708 +/*
23709 + * Copyright (C) 2005-2021 Junjiro R. Okajima
23710 + *
23711 + * This program, aufs is free software; you can redistribute it and/or modify
23712 + * it under the terms of the GNU General Public License as published by
23713 + * the Free Software Foundation; either version 2 of the License, or
23714 + * (at your option) any later version.
23715 + *
23716 + * This program is distributed in the hope that it will be useful,
23717 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
23718 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23719 + * GNU General Public License for more details.
23720 + *
23721 + * You should have received a copy of the GNU General Public License
23722 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23723 + */
23724 +
23725 +/*
23726 + * inode operation (rename entry)
23727 + * todo: this is crazy monster
23728 + */
23729 +
23730 +#include <linux/iversion.h>
23731 +#include "aufs.h"
23732 +
23733 +enum { AuSRC, AuDST, AuSrcDst };
23734 +enum { AuPARENT, AuCHILD, AuParentChild };
23735 +
23736 +#define AuRen_ISDIR_SRC                1
23737 +#define AuRen_ISDIR_DST                (1 << 1)
23738 +#define AuRen_ISSAMEDIR                (1 << 2)
23739 +#define AuRen_WHSRC            (1 << 3)
23740 +#define AuRen_WHDST            (1 << 4)
23741 +#define AuRen_MNT_WRITE                (1 << 5)
23742 +#define AuRen_DT_DSTDIR                (1 << 6)
23743 +#define AuRen_DIROPQ_SRC       (1 << 7)
23744 +#define AuRen_DIROPQ_DST       (1 << 8)
23745 +#define AuRen_DIRREN           (1 << 9)
23746 +#define AuRen_DROPPED_SRC      (1 << 10)
23747 +#define AuRen_DROPPED_DST      (1 << 11)
23748 +#define au_ftest_ren(flags, name)      ((flags) & AuRen_##name)
23749 +#define au_fset_ren(flags, name) \
23750 +       do { (flags) |= AuRen_##name; } while (0)
23751 +#define au_fclr_ren(flags, name) \
23752 +       do { (flags) &= ~AuRen_##name; } while (0)
23753 +
23754 +#ifndef CONFIG_AUFS_DIRREN
23755 +#undef AuRen_DIRREN
23756 +#define AuRen_DIRREN           0
23757 +#endif
23758 +
23759 +struct au_ren_args {
23760 +       struct {
23761 +               struct dentry *dentry, *h_dentry, *parent, *h_parent,
23762 +                       *wh_dentry;
23763 +               struct inode *dir, *inode;
23764 +               struct au_hinode *hdir, *hinode;
23765 +               struct au_dtime dt[AuParentChild];
23766 +               aufs_bindex_t btop, bdiropq;
23767 +       } sd[AuSrcDst];
23768 +
23769 +#define src_dentry     sd[AuSRC].dentry
23770 +#define src_dir                sd[AuSRC].dir
23771 +#define src_inode      sd[AuSRC].inode
23772 +#define src_h_dentry   sd[AuSRC].h_dentry
23773 +#define src_parent     sd[AuSRC].parent
23774 +#define src_h_parent   sd[AuSRC].h_parent
23775 +#define src_wh_dentry  sd[AuSRC].wh_dentry
23776 +#define src_hdir       sd[AuSRC].hdir
23777 +#define src_hinode     sd[AuSRC].hinode
23778 +#define src_h_dir      sd[AuSRC].hdir->hi_inode
23779 +#define src_dt         sd[AuSRC].dt
23780 +#define src_btop       sd[AuSRC].btop
23781 +#define src_bdiropq    sd[AuSRC].bdiropq
23782 +
23783 +#define dst_dentry     sd[AuDST].dentry
23784 +#define dst_dir                sd[AuDST].dir
23785 +#define dst_inode      sd[AuDST].inode
23786 +#define dst_h_dentry   sd[AuDST].h_dentry
23787 +#define dst_parent     sd[AuDST].parent
23788 +#define dst_h_parent   sd[AuDST].h_parent
23789 +#define dst_wh_dentry  sd[AuDST].wh_dentry
23790 +#define dst_hdir       sd[AuDST].hdir
23791 +#define dst_hinode     sd[AuDST].hinode
23792 +#define dst_h_dir      sd[AuDST].hdir->hi_inode
23793 +#define dst_dt         sd[AuDST].dt
23794 +#define dst_btop       sd[AuDST].btop
23795 +#define dst_bdiropq    sd[AuDST].bdiropq
23796 +
23797 +       struct dentry *h_trap;
23798 +       struct au_branch *br;
23799 +       struct path h_path;
23800 +       struct au_nhash whlist;
23801 +       aufs_bindex_t btgt, src_bwh;
23802 +
23803 +       struct {
23804 +               unsigned short auren_flags;
23805 +               unsigned char flags;    /* syscall parameter */
23806 +               unsigned char exchange;
23807 +       } __packed;
23808 +
23809 +       struct au_whtmp_rmdir *thargs;
23810 +       struct dentry *h_dst;
23811 +       struct au_hinode *h_root;
23812 +};
23813 +
23814 +/* ---------------------------------------------------------------------- */
23815 +
23816 +/*
23817 + * functions for reverting.
23818 + * when an error happened in a single rename systemcall, we should revert
23819 + * everything as if nothing happened.
23820 + * we don't need to revert the copied-up/down the parent dir since they are
23821 + * harmless.
23822 + */
23823 +
23824 +#define RevertFailure(fmt, ...) do { \
23825 +       AuIOErr("revert failure: " fmt " (%d, %d)\n", \
23826 +               ##__VA_ARGS__, err, rerr); \
23827 +       err = -EIO; \
23828 +} while (0)
23829 +
23830 +static void au_ren_do_rev_diropq(int err, struct au_ren_args *a, int idx)
23831 +{
23832 +       int rerr;
23833 +       struct dentry *d;
23834 +#define src_or_dst(member) a->sd[idx].member
23835 +
23836 +       d = src_or_dst(dentry); /* {src,dst}_dentry */
23837 +       au_hn_inode_lock_nested(src_or_dst(hinode), AuLsc_I_CHILD);
23838 +       rerr = au_diropq_remove(d, a->btgt);
23839 +       au_hn_inode_unlock(src_or_dst(hinode));
23840 +       au_set_dbdiropq(d, src_or_dst(bdiropq));
23841 +       if (rerr)
23842 +               RevertFailure("remove diropq %pd", d);
23843 +
23844 +#undef src_or_dst_
23845 +}
23846 +
23847 +static void au_ren_rev_diropq(int err, struct au_ren_args *a)
23848 +{
23849 +       if (au_ftest_ren(a->auren_flags, DIROPQ_SRC))
23850 +               au_ren_do_rev_diropq(err, a, AuSRC);
23851 +       if (au_ftest_ren(a->auren_flags, DIROPQ_DST))
23852 +               au_ren_do_rev_diropq(err, a, AuDST);
23853 +}
23854 +
23855 +static void au_ren_rev_rename(int err, struct au_ren_args *a)
23856 +{
23857 +       int rerr;
23858 +       struct inode *delegated;
23859 +       struct path h_ppath = {
23860 +               .dentry = a->src_h_parent,
23861 +               .mnt    = a->h_path.mnt
23862 +       };
23863 +
23864 +       a->h_path.dentry = vfsub_lkup_one(&a->src_dentry->d_name, &h_ppath);
23865 +       rerr = PTR_ERR(a->h_path.dentry);
23866 +       if (IS_ERR(a->h_path.dentry)) {
23867 +               RevertFailure("lkup one %pd", a->src_dentry);
23868 +               return;
23869 +       }
23870 +
23871 +       delegated = NULL;
23872 +       rerr = vfsub_rename(a->dst_h_dir,
23873 +                           au_h_dptr(a->src_dentry, a->btgt),
23874 +                           a->src_h_dir, &a->h_path, &delegated, a->flags);
23875 +       if (unlikely(rerr == -EWOULDBLOCK)) {
23876 +               pr_warn("cannot retry for NFSv4 delegation"
23877 +                       " for an internal rename\n");
23878 +               iput(delegated);
23879 +       }
23880 +       d_drop(a->h_path.dentry);
23881 +       dput(a->h_path.dentry);
23882 +       /* au_set_h_dptr(a->src_dentry, a->btgt, NULL); */
23883 +       if (rerr)
23884 +               RevertFailure("rename %pd", a->src_dentry);
23885 +}
23886 +
23887 +static void au_ren_rev_whtmp(int err, struct au_ren_args *a)
23888 +{
23889 +       int rerr;
23890 +       struct inode *delegated;
23891 +       struct path h_ppath = {
23892 +               .dentry = a->dst_h_parent,
23893 +               .mnt    = a->h_path.mnt
23894 +       };
23895 +
23896 +       a->h_path.dentry = vfsub_lkup_one(&a->dst_dentry->d_name, &h_ppath);
23897 +       rerr = PTR_ERR(a->h_path.dentry);
23898 +       if (IS_ERR(a->h_path.dentry)) {
23899 +               RevertFailure("lkup one %pd", a->dst_dentry);
23900 +               return;
23901 +       }
23902 +       if (d_is_positive(a->h_path.dentry)) {
23903 +               d_drop(a->h_path.dentry);
23904 +               dput(a->h_path.dentry);
23905 +               return;
23906 +       }
23907 +
23908 +       delegated = NULL;
23909 +       rerr = vfsub_rename(a->dst_h_dir, a->h_dst, a->dst_h_dir, &a->h_path,
23910 +                           &delegated, a->flags);
23911 +       if (unlikely(rerr == -EWOULDBLOCK)) {
23912 +               pr_warn("cannot retry for NFSv4 delegation"
23913 +                       " for an internal rename\n");
23914 +               iput(delegated);
23915 +       }
23916 +       d_drop(a->h_path.dentry);
23917 +       dput(a->h_path.dentry);
23918 +       if (!rerr)
23919 +               au_set_h_dptr(a->dst_dentry, a->btgt, dget(a->h_dst));
23920 +       else
23921 +               RevertFailure("rename %pd", a->h_dst);
23922 +}
23923 +
23924 +static void au_ren_rev_whsrc(int err, struct au_ren_args *a)
23925 +{
23926 +       int rerr;
23927 +
23928 +       a->h_path.dentry = a->src_wh_dentry;
23929 +       rerr = au_wh_unlink_dentry(a->src_h_dir, &a->h_path, a->src_dentry);
23930 +       au_set_dbwh(a->src_dentry, a->src_bwh);
23931 +       if (rerr)
23932 +               RevertFailure("unlink %pd", a->src_wh_dentry);
23933 +}
23934 +#undef RevertFailure
23935 +
23936 +/* ---------------------------------------------------------------------- */
23937 +
23938 +/*
23939 + * when we have to copyup the renaming entry, do it with the rename-target name
23940 + * in order to minimize the cost (the later actual rename is unnecessary).
23941 + * otherwise rename it on the target branch.
23942 + */
23943 +static int au_ren_or_cpup(struct au_ren_args *a)
23944 +{
23945 +       int err;
23946 +       struct dentry *d;
23947 +       struct inode *delegated;
23948 +
23949 +       d = a->src_dentry;
23950 +       if (au_dbtop(d) == a->btgt) {
23951 +               a->h_path.dentry = a->dst_h_dentry;
23952 +               AuDebugOn(au_dbtop(d) != a->btgt);
23953 +               delegated = NULL;
23954 +               err = vfsub_rename(a->src_h_dir, au_h_dptr(d, a->btgt),
23955 +                                  a->dst_h_dir, &a->h_path, &delegated,
23956 +                                  a->flags);
23957 +               if (unlikely(err == -EWOULDBLOCK)) {
23958 +                       pr_warn("cannot retry for NFSv4 delegation"
23959 +                               " for an internal rename\n");
23960 +                       iput(delegated);
23961 +               }
23962 +       } else
23963 +               BUG();
23964 +
23965 +       if (!err && a->h_dst)
23966 +               /* it will be set to dinfo later */
23967 +               dget(a->h_dst);
23968 +
23969 +       return err;
23970 +}
23971 +
23972 +/* cf. aufs_rmdir() */
23973 +static int au_ren_del_whtmp(struct au_ren_args *a)
23974 +{
23975 +       int err;
23976 +       struct inode *dir;
23977 +
23978 +       dir = a->dst_dir;
23979 +       SiMustAnyLock(dir->i_sb);
23980 +       if (!au_nhash_test_longer_wh(&a->whlist, a->btgt,
23981 +                                    au_sbi(dir->i_sb)->si_dirwh)
23982 +           || au_test_fs_remote(a->h_dst->d_sb)) {
23983 +               err = au_whtmp_rmdir(dir, a->btgt, a->h_dst, &a->whlist);
23984 +               if (unlikely(err))
23985 +                       pr_warn("failed removing whtmp dir %pd (%d), "
23986 +                               "ignored.\n", a->h_dst, err);
23987 +       } else {
23988 +               au_nhash_wh_free(&a->thargs->whlist);
23989 +               a->thargs->whlist = a->whlist;
23990 +               a->whlist.nh_num = 0;
23991 +               au_whtmp_kick_rmdir(dir, a->btgt, a->h_dst, a->thargs);
23992 +               dput(a->h_dst);
23993 +               a->thargs = NULL;
23994 +       }
23995 +
23996 +       return 0;
23997 +}
23998 +
23999 +/* make it 'opaque' dir. */
24000 +static int au_ren_do_diropq(struct au_ren_args *a, int idx)
24001 +{
24002 +       int err;
24003 +       struct dentry *d, *diropq;
24004 +#define src_or_dst(member) a->sd[idx].member
24005 +
24006 +       err = 0;
24007 +       d = src_or_dst(dentry); /* {src,dst}_dentry */
24008 +       src_or_dst(bdiropq) = au_dbdiropq(d);
24009 +       src_or_dst(hinode) = au_hi(src_or_dst(inode), a->btgt);
24010 +       au_hn_inode_lock_nested(src_or_dst(hinode), AuLsc_I_CHILD);
24011 +       diropq = au_diropq_create(d, a->btgt);
24012 +       au_hn_inode_unlock(src_or_dst(hinode));
24013 +       if (IS_ERR(diropq))
24014 +               err = PTR_ERR(diropq);
24015 +       else
24016 +               dput(diropq);
24017 +
24018 +#undef src_or_dst_
24019 +       return err;
24020 +}
24021 +
24022 +static int au_ren_diropq(struct au_ren_args *a)
24023 +{
24024 +       int err;
24025 +       unsigned char always;
24026 +       struct dentry *d;
24027 +
24028 +       err = 0;
24029 +       d = a->dst_dentry; /* already renamed on the branch */
24030 +       always = !!au_opt_test(au_mntflags(d->d_sb), ALWAYS_DIROPQ);
24031 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC)
24032 +           && !au_ftest_ren(a->auren_flags, DIRREN)
24033 +           && a->btgt != au_dbdiropq(a->src_dentry)
24034 +           && (a->dst_wh_dentry
24035 +               || a->btgt <= au_dbdiropq(d)
24036 +               /* hide the lower to keep xino */
24037 +               /* the lowers may not be a dir, but we hide them anyway */
24038 +               || a->btgt < au_dbbot(d)
24039 +               || always)) {
24040 +               AuDbg("here\n");
24041 +               err = au_ren_do_diropq(a, AuSRC);
24042 +               if (unlikely(err))
24043 +                       goto out;
24044 +               au_fset_ren(a->auren_flags, DIROPQ_SRC);
24045 +       }
24046 +       if (!a->exchange)
24047 +               goto out; /* success */
24048 +
24049 +       d = a->src_dentry; /* already renamed on the branch */
24050 +       if (au_ftest_ren(a->auren_flags, ISDIR_DST)
24051 +           && a->btgt != au_dbdiropq(a->dst_dentry)
24052 +           && (a->btgt < au_dbdiropq(d)
24053 +               || a->btgt < au_dbbot(d)
24054 +               || always)) {
24055 +               AuDbgDentry(a->src_dentry);
24056 +               AuDbgDentry(a->dst_dentry);
24057 +               err = au_ren_do_diropq(a, AuDST);
24058 +               if (unlikely(err))
24059 +                       goto out_rev_src;
24060 +               au_fset_ren(a->auren_flags, DIROPQ_DST);
24061 +       }
24062 +       goto out; /* success */
24063 +
24064 +out_rev_src:
24065 +       AuDbg("err %d, reverting src\n", err);
24066 +       au_ren_rev_diropq(err, a);
24067 +out:
24068 +       return err;
24069 +}
24070 +
24071 +static int do_rename(struct au_ren_args *a)
24072 +{
24073 +       int err;
24074 +       struct dentry *d, *h_d;
24075 +
24076 +       if (!a->exchange) {
24077 +               /* prepare workqueue args for asynchronous rmdir */
24078 +               h_d = a->dst_h_dentry;
24079 +               if (au_ftest_ren(a->auren_flags, ISDIR_DST)
24080 +                   /* && !au_ftest_ren(a->auren_flags, DIRREN) */
24081 +                   && d_is_positive(h_d)) {
24082 +                       err = -ENOMEM;
24083 +                       a->thargs = au_whtmp_rmdir_alloc(a->src_dentry->d_sb,
24084 +                                                        GFP_NOFS);
24085 +                       if (unlikely(!a->thargs))
24086 +                               goto out;
24087 +                       a->h_dst = dget(h_d);
24088 +               }
24089 +
24090 +               /* create whiteout for src_dentry */
24091 +               if (au_ftest_ren(a->auren_flags, WHSRC)) {
24092 +                       a->src_bwh = au_dbwh(a->src_dentry);
24093 +                       AuDebugOn(a->src_bwh >= 0);
24094 +                       a->src_wh_dentry = au_wh_create(a->src_dentry, a->btgt,
24095 +                                                       a->src_h_parent);
24096 +                       err = PTR_ERR(a->src_wh_dentry);
24097 +                       if (IS_ERR(a->src_wh_dentry))
24098 +                               goto out_thargs;
24099 +               }
24100 +
24101 +               /* lookup whiteout for dentry */
24102 +               if (au_ftest_ren(a->auren_flags, WHDST)) {
24103 +                       h_d = au_wh_lkup(a->dst_h_parent,
24104 +                                        &a->dst_dentry->d_name, a->br);
24105 +                       err = PTR_ERR(h_d);
24106 +                       if (IS_ERR(h_d))
24107 +                               goto out_whsrc;
24108 +                       if (d_is_negative(h_d))
24109 +                               dput(h_d);
24110 +                       else
24111 +                               a->dst_wh_dentry = h_d;
24112 +               }
24113 +
24114 +               /* rename dentry to tmpwh */
24115 +               if (a->thargs) {
24116 +                       err = au_whtmp_ren(a->dst_h_dentry, a->br);
24117 +                       if (unlikely(err))
24118 +                               goto out_whdst;
24119 +
24120 +                       d = a->dst_dentry;
24121 +                       au_set_h_dptr(d, a->btgt, NULL);
24122 +                       err = au_lkup_neg(d, a->btgt, /*wh*/0);
24123 +                       if (unlikely(err))
24124 +                               goto out_whtmp;
24125 +                       a->dst_h_dentry = au_h_dptr(d, a->btgt);
24126 +               }
24127 +       }
24128 +
24129 +       BUG_ON(d_is_positive(a->dst_h_dentry) && a->src_btop != a->btgt);
24130 +#if 0 /* debugging */
24131 +       BUG_ON(!au_ftest_ren(a->auren_flags, DIRREN)
24132 +              && d_is_positive(a->dst_h_dentry)
24133 +              && a->src_btop != a->btgt);
24134 +#endif
24135 +
24136 +       /* rename by vfs_rename or cpup */
24137 +       err = au_ren_or_cpup(a);
24138 +       if (unlikely(err))
24139 +               /* leave the copied-up one */
24140 +               goto out_whtmp;
24141 +
24142 +       /* make dir opaque */
24143 +       err = au_ren_diropq(a);
24144 +       if (unlikely(err))
24145 +               goto out_rename;
24146 +
24147 +       /* update target timestamps */
24148 +       if (a->exchange) {
24149 +               AuDebugOn(au_dbtop(a->dst_dentry) != a->btgt);
24150 +               a->h_path.dentry = au_h_dptr(a->dst_dentry, a->btgt);
24151 +               vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
24152 +               a->dst_inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
24153 +       }
24154 +       AuDebugOn(au_dbtop(a->src_dentry) != a->btgt);
24155 +       a->h_path.dentry = au_h_dptr(a->src_dentry, a->btgt);
24156 +       vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
24157 +       a->src_inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
24158 +
24159 +       if (!a->exchange) {
24160 +               /* remove whiteout for dentry */
24161 +               if (a->dst_wh_dentry) {
24162 +                       a->h_path.dentry = a->dst_wh_dentry;
24163 +                       err = au_wh_unlink_dentry(a->dst_h_dir, &a->h_path,
24164 +                                                 a->dst_dentry);
24165 +                       if (unlikely(err))
24166 +                               goto out_diropq;
24167 +               }
24168 +
24169 +               /* remove whtmp */
24170 +               if (a->thargs)
24171 +                       au_ren_del_whtmp(a); /* ignore this error */
24172 +
24173 +               au_fhsm_wrote(a->src_dentry->d_sb, a->btgt, /*force*/0);
24174 +       }
24175 +       err = 0;
24176 +       goto out_success;
24177 +
24178 +out_diropq:
24179 +       au_ren_rev_diropq(err, a);
24180 +out_rename:
24181 +       au_ren_rev_rename(err, a);
24182 +       dput(a->h_dst);
24183 +out_whtmp:
24184 +       if (a->thargs)
24185 +               au_ren_rev_whtmp(err, a);
24186 +out_whdst:
24187 +       dput(a->dst_wh_dentry);
24188 +       a->dst_wh_dentry = NULL;
24189 +out_whsrc:
24190 +       if (a->src_wh_dentry)
24191 +               au_ren_rev_whsrc(err, a);
24192 +out_success:
24193 +       dput(a->src_wh_dentry);
24194 +       dput(a->dst_wh_dentry);
24195 +out_thargs:
24196 +       if (a->thargs) {
24197 +               dput(a->h_dst);
24198 +               au_whtmp_rmdir_free(a->thargs);
24199 +               a->thargs = NULL;
24200 +       }
24201 +out:
24202 +       return err;
24203 +}
24204 +
24205 +/* ---------------------------------------------------------------------- */
24206 +
24207 +/*
24208 + * test if @dentry dir can be rename destination or not.
24209 + * success means, it is a logically empty dir.
24210 + */
24211 +static int may_rename_dstdir(struct dentry *dentry, struct au_nhash *whlist)
24212 +{
24213 +       return au_test_empty(dentry, whlist);
24214 +}
24215 +
24216 +/*
24217 + * test if @a->src_dentry dir can be rename source or not.
24218 + * if it can, return 0.
24219 + * success means,
24220 + * - it is a logically empty dir.
24221 + * - or, it exists on writable branch and has no children including whiteouts
24222 + *   on the lower branch unless DIRREN is on.
24223 + */
24224 +static int may_rename_srcdir(struct au_ren_args *a)
24225 +{
24226 +       int err;
24227 +       unsigned int rdhash;
24228 +       aufs_bindex_t btop, btgt;
24229 +       struct dentry *dentry;
24230 +       struct super_block *sb;
24231 +       struct au_sbinfo *sbinfo;
24232 +
24233 +       dentry = a->src_dentry;
24234 +       sb = dentry->d_sb;
24235 +       sbinfo = au_sbi(sb);
24236 +       if (au_opt_test(sbinfo->si_mntflags, DIRREN))
24237 +               au_fset_ren(a->auren_flags, DIRREN);
24238 +
24239 +       btgt = a->btgt;
24240 +       btop = au_dbtop(dentry);
24241 +       if (btop != btgt) {
24242 +               struct au_nhash whlist;
24243 +
24244 +               SiMustAnyLock(sb);
24245 +               rdhash = sbinfo->si_rdhash;
24246 +               if (!rdhash)
24247 +                       rdhash = au_rdhash_est(au_dir_size(/*file*/NULL,
24248 +                                                          dentry));
24249 +               err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
24250 +               if (unlikely(err))
24251 +                       goto out;
24252 +               err = au_test_empty(dentry, &whlist);
24253 +               au_nhash_wh_free(&whlist);
24254 +               goto out;
24255 +       }
24256 +
24257 +       if (btop == au_dbtaildir(dentry))
24258 +               return 0; /* success */
24259 +
24260 +       err = au_test_empty_lower(dentry);
24261 +
24262 +out:
24263 +       if (err == -ENOTEMPTY) {
24264 +               if (au_ftest_ren(a->auren_flags, DIRREN)) {
24265 +                       err = 0;
24266 +               } else {
24267 +                       AuWarn1("renaming dir who has child(ren) on multiple "
24268 +                               "branches, is not supported\n");
24269 +                       err = -EXDEV;
24270 +               }
24271 +       }
24272 +       return err;
24273 +}
24274 +
24275 +/* side effect: sets whlist and h_dentry */
24276 +static int au_ren_may_dir(struct au_ren_args *a)
24277 +{
24278 +       int err;
24279 +       unsigned int rdhash;
24280 +       struct dentry *d;
24281 +
24282 +       d = a->dst_dentry;
24283 +       SiMustAnyLock(d->d_sb);
24284 +
24285 +       err = 0;
24286 +       if (au_ftest_ren(a->auren_flags, ISDIR_DST) && a->dst_inode) {
24287 +               rdhash = au_sbi(d->d_sb)->si_rdhash;
24288 +               if (!rdhash)
24289 +                       rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, d));
24290 +               err = au_nhash_alloc(&a->whlist, rdhash, GFP_NOFS);
24291 +               if (unlikely(err))
24292 +                       goto out;
24293 +
24294 +               if (!a->exchange) {
24295 +                       au_set_dbtop(d, a->dst_btop);
24296 +                       err = may_rename_dstdir(d, &a->whlist);
24297 +                       au_set_dbtop(d, a->btgt);
24298 +               } else
24299 +                       err = may_rename_srcdir(a);
24300 +       }
24301 +       a->dst_h_dentry = au_h_dptr(d, au_dbtop(d));
24302 +       if (unlikely(err))
24303 +               goto out;
24304 +
24305 +       d = a->src_dentry;
24306 +       a->src_h_dentry = au_h_dptr(d, au_dbtop(d));
24307 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC)) {
24308 +               err = may_rename_srcdir(a);
24309 +               if (unlikely(err)) {
24310 +                       au_nhash_wh_free(&a->whlist);
24311 +                       a->whlist.nh_num = 0;
24312 +               }
24313 +       }
24314 +out:
24315 +       return err;
24316 +}
24317 +
24318 +/* ---------------------------------------------------------------------- */
24319 +
24320 +/*
24321 + * simple tests for rename.
24322 + * following the checks in vfs, plus the parent-child relationship.
24323 + */
24324 +static int au_may_ren(struct au_ren_args *a)
24325 +{
24326 +       int err, isdir;
24327 +       struct inode *h_inode;
24328 +
24329 +       if (a->src_btop == a->btgt) {
24330 +               err = au_may_del(a->src_dentry, a->btgt, a->src_h_parent,
24331 +                                au_ftest_ren(a->auren_flags, ISDIR_SRC));
24332 +               if (unlikely(err))
24333 +                       goto out;
24334 +               err = -EINVAL;
24335 +               if (unlikely(a->src_h_dentry == a->h_trap))
24336 +                       goto out;
24337 +       }
24338 +
24339 +       err = 0;
24340 +       if (a->dst_btop != a->btgt)
24341 +               goto out;
24342 +
24343 +       err = -ENOTEMPTY;
24344 +       if (unlikely(a->dst_h_dentry == a->h_trap))
24345 +               goto out;
24346 +
24347 +       err = -EIO;
24348 +       isdir = !!au_ftest_ren(a->auren_flags, ISDIR_DST);
24349 +       if (d_really_is_negative(a->dst_dentry)) {
24350 +               if (d_is_negative(a->dst_h_dentry))
24351 +                       err = au_may_add(a->dst_dentry, a->btgt,
24352 +                                        a->dst_h_parent, isdir);
24353 +       } else {
24354 +               if (unlikely(d_is_negative(a->dst_h_dentry)))
24355 +                       goto out;
24356 +               h_inode = d_inode(a->dst_h_dentry);
24357 +               if (h_inode->i_nlink)
24358 +                       err = au_may_del(a->dst_dentry, a->btgt,
24359 +                                        a->dst_h_parent, isdir);
24360 +       }
24361 +
24362 +out:
24363 +       if (unlikely(err == -ENOENT || err == -EEXIST))
24364 +               err = -EIO;
24365 +       AuTraceErr(err);
24366 +       return err;
24367 +}
24368 +
24369 +/* ---------------------------------------------------------------------- */
24370 +
24371 +/*
24372 + * locking order
24373 + * (VFS)
24374 + * - src_dir and dir by lock_rename()
24375 + * - inode if exists
24376 + * (aufs)
24377 + * - lock all
24378 + *   + src_dentry and dentry by aufs_read_and_write_lock2() which calls,
24379 + *     + si_read_lock
24380 + *     + di_write_lock2_child()
24381 + *       + di_write_lock_child()
24382 + *        + ii_write_lock_child()
24383 + *       + di_write_lock_child2()
24384 + *        + ii_write_lock_child2()
24385 + *     + src_parent and parent
24386 + *       + di_write_lock_parent()
24387 + *        + ii_write_lock_parent()
24388 + *       + di_write_lock_parent2()
24389 + *        + ii_write_lock_parent2()
24390 + *   + lower src_dir and dir by vfsub_lock_rename()
24391 + *   + verify the every relationships between child and parent. if any
24392 + *     of them failed, unlock all and return -EBUSY.
24393 + */
24394 +static void au_ren_unlock(struct au_ren_args *a)
24395 +{
24396 +       vfsub_unlock_rename(a->src_h_parent, a->src_hdir,
24397 +                           a->dst_h_parent, a->dst_hdir);
24398 +       if (au_ftest_ren(a->auren_flags, DIRREN)
24399 +           && a->h_root)
24400 +               au_hn_inode_unlock(a->h_root);
24401 +       if (au_ftest_ren(a->auren_flags, MNT_WRITE))
24402 +               vfsub_mnt_drop_write(au_br_mnt(a->br));
24403 +}
24404 +
24405 +static int au_ren_lock(struct au_ren_args *a)
24406 +{
24407 +       int err;
24408 +       unsigned int udba;
24409 +
24410 +       err = 0;
24411 +       a->src_h_parent = au_h_dptr(a->src_parent, a->btgt);
24412 +       a->src_hdir = au_hi(a->src_dir, a->btgt);
24413 +       a->dst_h_parent = au_h_dptr(a->dst_parent, a->btgt);
24414 +       a->dst_hdir = au_hi(a->dst_dir, a->btgt);
24415 +
24416 +       err = vfsub_mnt_want_write(au_br_mnt(a->br));
24417 +       if (unlikely(err))
24418 +               goto out;
24419 +       au_fset_ren(a->auren_flags, MNT_WRITE);
24420 +       if (au_ftest_ren(a->auren_flags, DIRREN)) {
24421 +               struct dentry *root;
24422 +               struct inode *dir;
24423 +
24424 +               /*
24425 +                * sbinfo is already locked, so this ii_read_lock is
24426 +                * unnecessary. but our debugging feature checks it.
24427 +                */
24428 +               root = a->src_inode->i_sb->s_root;
24429 +               if (root != a->src_parent && root != a->dst_parent) {
24430 +                       dir = d_inode(root);
24431 +                       ii_read_lock_parent3(dir);
24432 +                       a->h_root = au_hi(dir, a->btgt);
24433 +                       ii_read_unlock(dir);
24434 +                       au_hn_inode_lock_nested(a->h_root, AuLsc_I_PARENT3);
24435 +               }
24436 +       }
24437 +       a->h_trap = vfsub_lock_rename(a->src_h_parent, a->src_hdir,
24438 +                                     a->dst_h_parent, a->dst_hdir);
24439 +       udba = au_opt_udba(a->src_dentry->d_sb);
24440 +       if (unlikely(a->src_hdir->hi_inode != d_inode(a->src_h_parent)
24441 +                    || a->dst_hdir->hi_inode != d_inode(a->dst_h_parent)))
24442 +               err = au_busy_or_stale();
24443 +       if (!err && au_dbtop(a->src_dentry) == a->btgt)
24444 +               err = au_h_verify(a->src_h_dentry, udba,
24445 +                                 d_inode(a->src_h_parent), a->src_h_parent,
24446 +                                 a->br);
24447 +       if (!err && au_dbtop(a->dst_dentry) == a->btgt)
24448 +               err = au_h_verify(a->dst_h_dentry, udba,
24449 +                                 d_inode(a->dst_h_parent), a->dst_h_parent,
24450 +                                 a->br);
24451 +       if (!err)
24452 +               goto out; /* success */
24453 +
24454 +       err = au_busy_or_stale();
24455 +       au_ren_unlock(a);
24456 +
24457 +out:
24458 +       return err;
24459 +}
24460 +
24461 +/* ---------------------------------------------------------------------- */
24462 +
24463 +static void au_ren_refresh_dir(struct au_ren_args *a)
24464 +{
24465 +       struct inode *dir;
24466 +
24467 +       dir = a->dst_dir;
24468 +       inode_inc_iversion(dir);
24469 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC)) {
24470 +               /* is this updating defined in POSIX? */
24471 +               au_cpup_attr_timesizes(a->src_inode);
24472 +               au_cpup_attr_nlink(dir, /*force*/1);
24473 +       }
24474 +       au_dir_ts(dir, a->btgt);
24475 +
24476 +       if (a->exchange) {
24477 +               dir = a->src_dir;
24478 +               inode_inc_iversion(dir);
24479 +               if (au_ftest_ren(a->auren_flags, ISDIR_DST)) {
24480 +                       /* is this updating defined in POSIX? */
24481 +                       au_cpup_attr_timesizes(a->dst_inode);
24482 +                       au_cpup_attr_nlink(dir, /*force*/1);
24483 +               }
24484 +               au_dir_ts(dir, a->btgt);
24485 +       }
24486 +
24487 +       if (au_ftest_ren(a->auren_flags, ISSAMEDIR))
24488 +               return;
24489 +
24490 +       dir = a->src_dir;
24491 +       inode_inc_iversion(dir);
24492 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC))
24493 +               au_cpup_attr_nlink(dir, /*force*/1);
24494 +       au_dir_ts(dir, a->btgt);
24495 +}
24496 +
24497 +static void au_ren_refresh(struct au_ren_args *a)
24498 +{
24499 +       aufs_bindex_t bbot, bindex;
24500 +       struct dentry *d, *h_d;
24501 +       struct inode *i, *h_i;
24502 +       struct super_block *sb;
24503 +
24504 +       d = a->dst_dentry;
24505 +       d_drop(d);
24506 +       if (a->h_dst)
24507 +               /* already dget-ed by au_ren_or_cpup() */
24508 +               au_set_h_dptr(d, a->btgt, a->h_dst);
24509 +
24510 +       i = a->dst_inode;
24511 +       if (i) {
24512 +               if (!a->exchange) {
24513 +                       if (!au_ftest_ren(a->auren_flags, ISDIR_DST))
24514 +                               vfsub_drop_nlink(i);
24515 +                       else {
24516 +                               vfsub_dead_dir(i);
24517 +                               au_cpup_attr_timesizes(i);
24518 +                       }
24519 +                       au_update_dbrange(d, /*do_put_zero*/1);
24520 +               } else
24521 +                       au_cpup_attr_nlink(i, /*force*/1);
24522 +       } else {
24523 +               bbot = a->btgt;
24524 +               for (bindex = au_dbtop(d); bindex < bbot; bindex++)
24525 +                       au_set_h_dptr(d, bindex, NULL);
24526 +               bbot = au_dbbot(d);
24527 +               for (bindex = a->btgt + 1; bindex <= bbot; bindex++)
24528 +                       au_set_h_dptr(d, bindex, NULL);
24529 +               au_update_dbrange(d, /*do_put_zero*/0);
24530 +       }
24531 +
24532 +       if (a->exchange
24533 +           || au_ftest_ren(a->auren_flags, DIRREN)) {
24534 +               d_drop(a->src_dentry);
24535 +               if (au_ftest_ren(a->auren_flags, DIRREN))
24536 +                       au_set_dbwh(a->src_dentry, -1);
24537 +               return;
24538 +       }
24539 +
24540 +       d = a->src_dentry;
24541 +       au_set_dbwh(d, -1);
24542 +       bbot = au_dbbot(d);
24543 +       for (bindex = a->btgt + 1; bindex <= bbot; bindex++) {
24544 +               h_d = au_h_dptr(d, bindex);
24545 +               if (h_d)
24546 +                       au_set_h_dptr(d, bindex, NULL);
24547 +       }
24548 +       au_set_dbbot(d, a->btgt);
24549 +
24550 +       sb = d->d_sb;
24551 +       i = a->src_inode;
24552 +       if (au_opt_test(au_mntflags(sb), PLINK) && au_plink_test(i))
24553 +               return; /* success */
24554 +
24555 +       bbot = au_ibbot(i);
24556 +       for (bindex = a->btgt + 1; bindex <= bbot; bindex++) {
24557 +               h_i = au_h_iptr(i, bindex);
24558 +               if (h_i) {
24559 +                       au_xino_write(sb, bindex, h_i->i_ino, /*ino*/0);
24560 +                       /* ignore this error */
24561 +                       au_set_h_iptr(i, bindex, NULL, 0);
24562 +               }
24563 +       }
24564 +       au_set_ibbot(i, a->btgt);
24565 +}
24566 +
24567 +/* ---------------------------------------------------------------------- */
24568 +
24569 +/* mainly for link(2) and rename(2) */
24570 +int au_wbr(struct dentry *dentry, aufs_bindex_t btgt)
24571 +{
24572 +       aufs_bindex_t bdiropq, bwh;
24573 +       struct dentry *parent;
24574 +       struct au_branch *br;
24575 +
24576 +       parent = dentry->d_parent;
24577 +       IMustLock(d_inode(parent)); /* dir is locked */
24578 +
24579 +       bdiropq = au_dbdiropq(parent);
24580 +       bwh = au_dbwh(dentry);
24581 +       br = au_sbr(dentry->d_sb, btgt);
24582 +       if (au_br_rdonly(br)
24583 +           || (0 <= bdiropq && bdiropq < btgt)
24584 +           || (0 <= bwh && bwh < btgt))
24585 +               btgt = -1;
24586 +
24587 +       AuDbg("btgt %d\n", btgt);
24588 +       return btgt;
24589 +}
24590 +
24591 +/* sets src_btop, dst_btop and btgt */
24592 +static int au_ren_wbr(struct au_ren_args *a)
24593 +{
24594 +       int err;
24595 +       struct au_wr_dir_args wr_dir_args = {
24596 +               /* .force_btgt  = -1, */
24597 +               .flags          = AuWrDir_ADD_ENTRY
24598 +       };
24599 +
24600 +       a->src_btop = au_dbtop(a->src_dentry);
24601 +       a->dst_btop = au_dbtop(a->dst_dentry);
24602 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC)
24603 +           || au_ftest_ren(a->auren_flags, ISDIR_DST))
24604 +               au_fset_wrdir(wr_dir_args.flags, ISDIR);
24605 +       wr_dir_args.force_btgt = a->src_btop;
24606 +       if (a->dst_inode && a->dst_btop < a->src_btop)
24607 +               wr_dir_args.force_btgt = a->dst_btop;
24608 +       wr_dir_args.force_btgt = au_wbr(a->dst_dentry, wr_dir_args.force_btgt);
24609 +       err = au_wr_dir(a->dst_dentry, a->src_dentry, &wr_dir_args);
24610 +       a->btgt = err;
24611 +       if (a->exchange)
24612 +               au_update_dbtop(a->dst_dentry);
24613 +
24614 +       return err;
24615 +}
24616 +
24617 +static void au_ren_dt(struct au_ren_args *a)
24618 +{
24619 +       a->h_path.dentry = a->src_h_parent;
24620 +       au_dtime_store(a->src_dt + AuPARENT, a->src_parent, &a->h_path);
24621 +       if (!au_ftest_ren(a->auren_flags, ISSAMEDIR)) {
24622 +               a->h_path.dentry = a->dst_h_parent;
24623 +               au_dtime_store(a->dst_dt + AuPARENT, a->dst_parent, &a->h_path);
24624 +       }
24625 +
24626 +       au_fclr_ren(a->auren_flags, DT_DSTDIR);
24627 +       if (!au_ftest_ren(a->auren_flags, ISDIR_SRC)
24628 +           && !a->exchange)
24629 +               return;
24630 +
24631 +       a->h_path.dentry = a->src_h_dentry;
24632 +       au_dtime_store(a->src_dt + AuCHILD, a->src_dentry, &a->h_path);
24633 +       if (d_is_positive(a->dst_h_dentry)) {
24634 +               au_fset_ren(a->auren_flags, DT_DSTDIR);
24635 +               a->h_path.dentry = a->dst_h_dentry;
24636 +               au_dtime_store(a->dst_dt + AuCHILD, a->dst_dentry, &a->h_path);
24637 +       }
24638 +}
24639 +
24640 +static void au_ren_rev_dt(int err, struct au_ren_args *a)
24641 +{
24642 +       struct dentry *h_d;
24643 +       struct inode *h_inode;
24644 +
24645 +       au_dtime_revert(a->src_dt + AuPARENT);
24646 +       if (!au_ftest_ren(a->auren_flags, ISSAMEDIR))
24647 +               au_dtime_revert(a->dst_dt + AuPARENT);
24648 +
24649 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC) && err != -EIO) {
24650 +               h_d = a->src_dt[AuCHILD].dt_h_path.dentry;
24651 +               h_inode = d_inode(h_d);
24652 +               inode_lock_nested(h_inode, AuLsc_I_CHILD);
24653 +               au_dtime_revert(a->src_dt + AuCHILD);
24654 +               inode_unlock(h_inode);
24655 +
24656 +               if (au_ftest_ren(a->auren_flags, DT_DSTDIR)) {
24657 +                       h_d = a->dst_dt[AuCHILD].dt_h_path.dentry;
24658 +                       h_inode = d_inode(h_d);
24659 +                       inode_lock_nested(h_inode, AuLsc_I_CHILD);
24660 +                       au_dtime_revert(a->dst_dt + AuCHILD);
24661 +                       inode_unlock(h_inode);
24662 +               }
24663 +       }
24664 +}
24665 +
24666 +/* ---------------------------------------------------------------------- */
24667 +
24668 +int aufs_rename(struct user_namespace *userns,
24669 +               struct inode *_src_dir, struct dentry *_src_dentry,
24670 +               struct inode *_dst_dir, struct dentry *_dst_dentry,
24671 +               unsigned int _flags)
24672 +{
24673 +       int err, lock_flags;
24674 +       void *rev;
24675 +       /* reduce stack space */
24676 +       struct au_ren_args *a;
24677 +       struct au_pin pin;
24678 +
24679 +       AuDbg("%pd, %pd, 0x%x\n", _src_dentry, _dst_dentry, _flags);
24680 +       IMustLock(_src_dir);
24681 +       IMustLock(_dst_dir);
24682 +
24683 +       err = -EINVAL;
24684 +       if (unlikely(_flags & RENAME_WHITEOUT))
24685 +               goto out;
24686 +
24687 +       err = -ENOMEM;
24688 +       BUILD_BUG_ON(sizeof(*a) > PAGE_SIZE);
24689 +       a = kzalloc(sizeof(*a), GFP_NOFS);
24690 +       if (unlikely(!a))
24691 +               goto out;
24692 +
24693 +       a->flags = _flags;
24694 +       BUILD_BUG_ON(sizeof(a->exchange) == sizeof(u8)
24695 +                    && RENAME_EXCHANGE > U8_MAX);
24696 +       a->exchange = _flags & RENAME_EXCHANGE;
24697 +       a->src_dir = _src_dir;
24698 +       a->src_dentry = _src_dentry;
24699 +       a->src_inode = NULL;
24700 +       if (d_really_is_positive(a->src_dentry))
24701 +               a->src_inode = d_inode(a->src_dentry);
24702 +       a->src_parent = a->src_dentry->d_parent; /* dir inode is locked */
24703 +       a->dst_dir = _dst_dir;
24704 +       a->dst_dentry = _dst_dentry;
24705 +       a->dst_inode = NULL;
24706 +       if (d_really_is_positive(a->dst_dentry))
24707 +               a->dst_inode = d_inode(a->dst_dentry);
24708 +       a->dst_parent = a->dst_dentry->d_parent; /* dir inode is locked */
24709 +       if (a->dst_inode) {
24710 +               /*
24711 +                * if EXCHANGE && src is non-dir && dst is dir,
24712 +                * dst is not locked.
24713 +                */
24714 +               /* IMustLock(a->dst_inode); */
24715 +               au_igrab(a->dst_inode);
24716 +       }
24717 +
24718 +       err = -ENOTDIR;
24719 +       lock_flags = AuLock_FLUSH | AuLock_NOPLM | AuLock_GEN;
24720 +       if (d_is_dir(a->src_dentry)) {
24721 +               au_fset_ren(a->auren_flags, ISDIR_SRC);
24722 +               if (unlikely(!a->exchange
24723 +                            && d_really_is_positive(a->dst_dentry)
24724 +                            && !d_is_dir(a->dst_dentry)))
24725 +                       goto out_free;
24726 +               lock_flags |= AuLock_DIRS;
24727 +       }
24728 +       if (a->dst_inode && d_is_dir(a->dst_dentry)) {
24729 +               au_fset_ren(a->auren_flags, ISDIR_DST);
24730 +               if (unlikely(!a->exchange
24731 +                            && d_really_is_positive(a->src_dentry)
24732 +                            && !d_is_dir(a->src_dentry)))
24733 +                       goto out_free;
24734 +               lock_flags |= AuLock_DIRS;
24735 +       }
24736 +       err = aufs_read_and_write_lock2(a->dst_dentry, a->src_dentry,
24737 +                                       lock_flags);
24738 +       if (unlikely(err))
24739 +               goto out_free;
24740 +
24741 +       err = au_d_hashed_positive(a->src_dentry);
24742 +       if (unlikely(err))
24743 +               goto out_unlock;
24744 +       err = -ENOENT;
24745 +       if (a->dst_inode) {
24746 +               /*
24747 +                * If it is a dir, VFS unhash it before this
24748 +                * function. It means we cannot rely upon d_unhashed().
24749 +                */
24750 +               if (unlikely(!a->dst_inode->i_nlink))
24751 +                       goto out_unlock;
24752 +               if (!au_ftest_ren(a->auren_flags, ISDIR_DST)) {
24753 +                       err = au_d_hashed_positive(a->dst_dentry);
24754 +                       if (unlikely(err && !a->exchange))
24755 +                               goto out_unlock;
24756 +               } else if (unlikely(IS_DEADDIR(a->dst_inode)))
24757 +                       goto out_unlock;
24758 +       } else if (unlikely(d_unhashed(a->dst_dentry)))
24759 +               goto out_unlock;
24760 +
24761 +       /*
24762 +        * is it possible?
24763 +        * yes, it happened (in linux-3.3-rcN) but I don't know why.
24764 +        * there may exist a problem somewhere else.
24765 +        */
24766 +       err = -EINVAL;
24767 +       if (unlikely(d_inode(a->dst_parent) == d_inode(a->src_dentry)))
24768 +               goto out_unlock;
24769 +
24770 +       au_fset_ren(a->auren_flags, ISSAMEDIR); /* temporary */
24771 +       di_write_lock_parent(a->dst_parent);
24772 +
24773 +       /* which branch we process */
24774 +       err = au_ren_wbr(a);
24775 +       if (unlikely(err < 0))
24776 +               goto out_parent;
24777 +       a->br = au_sbr(a->dst_dentry->d_sb, a->btgt);
24778 +       a->h_path.mnt = au_br_mnt(a->br);
24779 +
24780 +       /* are they available to be renamed */
24781 +       err = au_ren_may_dir(a);
24782 +       if (unlikely(err))
24783 +               goto out_children;
24784 +
24785 +       /* prepare the writable parent dir on the same branch */
24786 +       if (a->dst_btop == a->btgt) {
24787 +               au_fset_ren(a->auren_flags, WHDST);
24788 +       } else {
24789 +               err = au_cpup_dirs(a->dst_dentry, a->btgt);
24790 +               if (unlikely(err))
24791 +                       goto out_children;
24792 +       }
24793 +
24794 +       err = 0;
24795 +       if (!a->exchange) {
24796 +               if (a->src_dir != a->dst_dir) {
24797 +                       /*
24798 +                        * this temporary unlock is safe,
24799 +                        * because both dir->i_mutex are locked.
24800 +                        */
24801 +                       di_write_unlock(a->dst_parent);
24802 +                       di_write_lock_parent(a->src_parent);
24803 +                       err = au_wr_dir_need_wh(a->src_dentry,
24804 +                                               au_ftest_ren(a->auren_flags,
24805 +                                                            ISDIR_SRC),
24806 +                                               &a->btgt);
24807 +                       di_write_unlock(a->src_parent);
24808 +                       di_write_lock2_parent(a->src_parent, a->dst_parent,
24809 +                                             /*isdir*/1);
24810 +                       au_fclr_ren(a->auren_flags, ISSAMEDIR);
24811 +               } else
24812 +                       err = au_wr_dir_need_wh(a->src_dentry,
24813 +                                               au_ftest_ren(a->auren_flags,
24814 +                                                            ISDIR_SRC),
24815 +                                               &a->btgt);
24816 +       }
24817 +       if (unlikely(err < 0))
24818 +               goto out_children;
24819 +       if (err)
24820 +               au_fset_ren(a->auren_flags, WHSRC);
24821 +
24822 +       /* cpup src */
24823 +       if (a->src_btop != a->btgt) {
24824 +               err = au_pin(&pin, a->src_dentry, a->btgt,
24825 +                            au_opt_udba(a->src_dentry->d_sb),
24826 +                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
24827 +               if (!err) {
24828 +                       struct au_cp_generic cpg = {
24829 +                               .dentry = a->src_dentry,
24830 +                               .bdst   = a->btgt,
24831 +                               .bsrc   = a->src_btop,
24832 +                               .len    = -1,
24833 +                               .pin    = &pin,
24834 +                               .flags  = AuCpup_DTIME | AuCpup_HOPEN
24835 +                       };
24836 +                       AuDebugOn(au_dbtop(a->src_dentry) != a->src_btop);
24837 +                       err = au_sio_cpup_simple(&cpg);
24838 +                       au_unpin(&pin);
24839 +               }
24840 +               if (unlikely(err))
24841 +                       goto out_children;
24842 +               a->src_btop = a->btgt;
24843 +               a->src_h_dentry = au_h_dptr(a->src_dentry, a->btgt);
24844 +               if (!a->exchange)
24845 +                       au_fset_ren(a->auren_flags, WHSRC);
24846 +       }
24847 +
24848 +       /* cpup dst */
24849 +       if (a->exchange && a->dst_inode
24850 +           && a->dst_btop != a->btgt) {
24851 +               err = au_pin(&pin, a->dst_dentry, a->btgt,
24852 +                            au_opt_udba(a->dst_dentry->d_sb),
24853 +                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
24854 +               if (!err) {
24855 +                       struct au_cp_generic cpg = {
24856 +                               .dentry = a->dst_dentry,
24857 +                               .bdst   = a->btgt,
24858 +                               .bsrc   = a->dst_btop,
24859 +                               .len    = -1,
24860 +                               .pin    = &pin,
24861 +                               .flags  = AuCpup_DTIME | AuCpup_HOPEN
24862 +                       };
24863 +                       err = au_sio_cpup_simple(&cpg);
24864 +                       au_unpin(&pin);
24865 +               }
24866 +               if (unlikely(err))
24867 +                       goto out_children;
24868 +               a->dst_btop = a->btgt;
24869 +               a->dst_h_dentry = au_h_dptr(a->dst_dentry, a->btgt);
24870 +       }
24871 +
24872 +       /* lock them all */
24873 +       err = au_ren_lock(a);
24874 +       if (unlikely(err))
24875 +               /* leave the copied-up one */
24876 +               goto out_children;
24877 +
24878 +       if (!a->exchange) {
24879 +               if (!au_opt_test(au_mntflags(a->dst_dir->i_sb), UDBA_NONE))
24880 +                       err = au_may_ren(a);
24881 +               else if (unlikely(a->dst_dentry->d_name.len > AUFS_MAX_NAMELEN))
24882 +                       err = -ENAMETOOLONG;
24883 +               if (unlikely(err))
24884 +                       goto out_hdir;
24885 +       }
24886 +
24887 +       /* store timestamps to be revertible */
24888 +       au_ren_dt(a);
24889 +
24890 +       /* store dirren info */
24891 +       if (au_ftest_ren(a->auren_flags, DIRREN)) {
24892 +               err = au_dr_rename(a->src_dentry, a->btgt,
24893 +                                  &a->dst_dentry->d_name, &rev);
24894 +               AuTraceErr(err);
24895 +               if (unlikely(err))
24896 +                       goto out_dt;
24897 +       }
24898 +
24899 +       /* here we go */
24900 +       err = do_rename(a);
24901 +       if (unlikely(err))
24902 +               goto out_dirren;
24903 +
24904 +       if (au_ftest_ren(a->auren_flags, DIRREN))
24905 +               au_dr_rename_fin(a->src_dentry, a->btgt, rev);
24906 +
24907 +       /* update dir attributes */
24908 +       au_ren_refresh_dir(a);
24909 +
24910 +       /* dput/iput all lower dentries */
24911 +       au_ren_refresh(a);
24912 +
24913 +       goto out_hdir; /* success */
24914 +
24915 +out_dirren:
24916 +       if (au_ftest_ren(a->auren_flags, DIRREN))
24917 +               au_dr_rename_rev(a->src_dentry, a->btgt, rev);
24918 +out_dt:
24919 +       au_ren_rev_dt(err, a);
24920 +out_hdir:
24921 +       au_ren_unlock(a);
24922 +out_children:
24923 +       au_nhash_wh_free(&a->whlist);
24924 +       if (err && a->dst_inode && a->dst_btop != a->btgt) {
24925 +               AuDbg("btop %d, btgt %d\n", a->dst_btop, a->btgt);
24926 +               au_set_h_dptr(a->dst_dentry, a->btgt, NULL);
24927 +               au_set_dbtop(a->dst_dentry, a->dst_btop);
24928 +       }
24929 +out_parent:
24930 +       if (!err) {
24931 +               if (d_unhashed(a->src_dentry))
24932 +                       au_fset_ren(a->auren_flags, DROPPED_SRC);
24933 +               if (d_unhashed(a->dst_dentry))
24934 +                       au_fset_ren(a->auren_flags, DROPPED_DST);
24935 +               if (!a->exchange)
24936 +                       d_move(a->src_dentry, a->dst_dentry);
24937 +               else {
24938 +                       d_exchange(a->src_dentry, a->dst_dentry);
24939 +                       if (au_ftest_ren(a->auren_flags, DROPPED_DST))
24940 +                               d_drop(a->dst_dentry);
24941 +               }
24942 +               if (au_ftest_ren(a->auren_flags, DROPPED_SRC))
24943 +                       d_drop(a->src_dentry);
24944 +       } else {
24945 +               au_update_dbtop(a->dst_dentry);
24946 +               if (!a->dst_inode)
24947 +                       d_drop(a->dst_dentry);
24948 +       }
24949 +       if (au_ftest_ren(a->auren_flags, ISSAMEDIR))
24950 +               di_write_unlock(a->dst_parent);
24951 +       else
24952 +               di_write_unlock2(a->src_parent, a->dst_parent);
24953 +out_unlock:
24954 +       aufs_read_and_write_unlock2(a->dst_dentry, a->src_dentry);
24955 +out_free:
24956 +       iput(a->dst_inode);
24957 +       if (a->thargs)
24958 +               au_whtmp_rmdir_free(a->thargs);
24959 +       au_kfree_rcu(a);
24960 +out:
24961 +       AuTraceErr(err);
24962 +       return err;
24963 +}
24964 diff -urN /usr/share/empty/fs/aufs/Kconfig linux/fs/aufs/Kconfig
24965 --- /usr/share/empty/fs/aufs/Kconfig    1970-01-01 01:00:00.000000000 +0100
24966 +++ linux/fs/aufs/Kconfig       2022-03-21 14:49:05.716633010 +0100
24967 @@ -0,0 +1,199 @@
24968 +# SPDX-License-Identifier: GPL-2.0
24969 +config AUFS_FS
24970 +       tristate "Aufs (Advanced multi layered unification filesystem) support"
24971 +       help
24972 +       Aufs is a stackable unification filesystem such as Unionfs,
24973 +       which unifies several directories and provides a merged single
24974 +       directory.
24975 +       In the early days, aufs was entirely re-designed and
24976 +       re-implemented Unionfs Version 1.x series. Introducing many
24977 +       original ideas, approaches and improvements, it becomes totally
24978 +       different from Unionfs while keeping the basic features.
24979 +
24980 +if AUFS_FS
24981 +choice
24982 +       prompt "Maximum number of branches"
24983 +       default AUFS_BRANCH_MAX_127
24984 +       help
24985 +       Specifies the maximum number of branches (or member directories)
24986 +       in a single aufs. The larger value consumes more system
24987 +       resources and has a minor impact to performance.
24988 +config AUFS_BRANCH_MAX_127
24989 +       bool "127"
24990 +       help
24991 +       Specifies the maximum number of branches (or member directories)
24992 +       in a single aufs. The larger value consumes more system
24993 +       resources and has a minor impact to performance.
24994 +config AUFS_BRANCH_MAX_511
24995 +       bool "511"
24996 +       help
24997 +       Specifies the maximum number of branches (or member directories)
24998 +       in a single aufs. The larger value consumes more system
24999 +       resources and has a minor impact to performance.
25000 +config AUFS_BRANCH_MAX_1023
25001 +       bool "1023"
25002 +       help
25003 +       Specifies the maximum number of branches (or member directories)
25004 +       in a single aufs. The larger value consumes more system
25005 +       resources and has a minor impact to performance.
25006 +config AUFS_BRANCH_MAX_32767
25007 +       bool "32767"
25008 +       help
25009 +       Specifies the maximum number of branches (or member directories)
25010 +       in a single aufs. The larger value consumes more system
25011 +       resources and has a minor impact to performance.
25012 +endchoice
25013 +
25014 +config AUFS_SBILIST
25015 +       bool
25016 +       depends on AUFS_MAGIC_SYSRQ || PROC_FS
25017 +       default y
25018 +       help
25019 +       Automatic configuration for internal use.
25020 +       When aufs supports Magic SysRq or /proc, enabled automatically.
25021 +
25022 +config AUFS_HNOTIFY
25023 +       bool "Detect direct branch access (bypassing aufs)"
25024 +       help
25025 +       If you want to modify files on branches directly, eg. bypassing aufs,
25026 +       and want aufs to detect the changes of them fully, then enable this
25027 +       option and use 'udba=notify' mount option.
25028 +       Currently there is only one available configuration, "fsnotify".
25029 +       It will have a negative impact to the performance.
25030 +       See detail in aufs.5.
25031 +
25032 +choice
25033 +       prompt "method" if AUFS_HNOTIFY
25034 +       default AUFS_HFSNOTIFY
25035 +config AUFS_HFSNOTIFY
25036 +       bool "fsnotify"
25037 +       select FSNOTIFY
25038 +endchoice
25039 +
25040 +config AUFS_EXPORT
25041 +       bool "NFS-exportable aufs"
25042 +       depends on EXPORTFS
25043 +       help
25044 +       If you want to export your mounted aufs via NFS, then enable this
25045 +       option. There are several requirements for this configuration.
25046 +       See detail in aufs.5.
25047 +
25048 +config AUFS_INO_T_64
25049 +       bool
25050 +       depends on AUFS_EXPORT
25051 +       depends on 64BIT && !(ALPHA || S390)
25052 +       default y
25053 +       help
25054 +       Automatic configuration for internal use.
25055 +       /* typedef unsigned long/int __kernel_ino_t */
25056 +       /* alpha and s390x are int */
25057 +
25058 +config AUFS_XATTR
25059 +       bool "support for XATTR/EA (including Security Labels)"
25060 +       help
25061 +       If your branch fs supports XATTR/EA and you want to make them
25062 +       available in aufs too, then enable this opsion and specify the
25063 +       branch attributes for EA.
25064 +       See detail in aufs.5.
25065 +
25066 +config AUFS_FHSM
25067 +       bool "File-based Hierarchical Storage Management"
25068 +       help
25069 +       Hierarchical Storage Management (or HSM) is a well-known feature
25070 +       in the storage world. Aufs provides this feature as file-based.
25071 +       with multiple branches.
25072 +       These multiple branches are prioritized, ie. the topmost one
25073 +       should be the fastest drive and be used heavily.
25074 +
25075 +config AUFS_RDU
25076 +       bool "Readdir in userspace"
25077 +       help
25078 +       Aufs has two methods to provide a merged view for a directory,
25079 +       by a user-space library and by kernel-space natively. The latter
25080 +       is always enabled but sometimes large and slow.
25081 +       If you enable this option, install the library in aufs2-util
25082 +       package, and set some environment variables for your readdir(3),
25083 +       then the work will be handled in user-space which generally
25084 +       shows better performance in most cases.
25085 +       See detail in aufs.5.
25086 +
25087 +config AUFS_DIRREN
25088 +       bool "Workaround for rename(2)-ing a directory"
25089 +       help
25090 +       By default, aufs returns EXDEV error in renameing a dir who has
25091 +       his child on the lower branch, since it is a bad idea to issue
25092 +       rename(2) internally for every lower branch. But user may not
25093 +       accept this behaviour. So here is a workaround to allow such
25094 +       rename(2) and store some extra infromation on the writable
25095 +       branch. Obviously this costs high (and I don't like it).
25096 +       To use this feature, you need to enable this configuration AND
25097 +       to specify the mount option `dirren.'
25098 +       See details in aufs.5 and the design documents.
25099 +
25100 +config AUFS_SHWH
25101 +       bool "Show whiteouts"
25102 +       help
25103 +       If you want to make the whiteouts in aufs visible, then enable
25104 +       this option and specify 'shwh' mount option. Although it may
25105 +       sounds like philosophy or something, but in technically it
25106 +       simply shows the name of whiteout with keeping its behaviour.
25107 +
25108 +config AUFS_BR_RAMFS
25109 +       bool "Ramfs (initramfs/rootfs) as an aufs branch"
25110 +       help
25111 +       If you want to use ramfs as an aufs branch fs, then enable this
25112 +       option. Generally tmpfs is recommended.
25113 +       Aufs prohibited them to be a branch fs by default, because
25114 +       initramfs becomes unusable after switch_root or something
25115 +       generally. If you sets initramfs as an aufs branch and boot your
25116 +       system by switch_root, you will meet a problem easily since the
25117 +       files in initramfs may be inaccessible.
25118 +       Unless you are going to use ramfs as an aufs branch fs without
25119 +       switch_root or something, leave it N.
25120 +
25121 +config AUFS_BR_FUSE
25122 +       bool "Fuse fs as an aufs branch"
25123 +       depends on FUSE_FS
25124 +       select AUFS_POLL
25125 +       help
25126 +       If you want to use fuse-based userspace filesystem as an aufs
25127 +       branch fs, then enable this option.
25128 +       It implements the internal poll(2) operation which is
25129 +       implemented by fuse only (curretnly).
25130 +
25131 +config AUFS_POLL
25132 +       bool
25133 +       help
25134 +       Automatic configuration for internal use.
25135 +
25136 +config AUFS_BR_HFSPLUS
25137 +       bool "Hfsplus as an aufs branch"
25138 +       depends on HFSPLUS_FS
25139 +       default y
25140 +       help
25141 +       If you want to use hfsplus fs as an aufs branch fs, then enable
25142 +       this option. This option introduces a small overhead at
25143 +       copying-up a file on hfsplus.
25144 +
25145 +config AUFS_BDEV_LOOP
25146 +       bool
25147 +       depends on BLK_DEV_LOOP
25148 +       default y
25149 +       help
25150 +       Automatic configuration for internal use.
25151 +       Convert =[ym] into =y.
25152 +
25153 +config AUFS_DEBUG
25154 +       bool "Debug aufs"
25155 +       help
25156 +       Enable this to compile aufs internal debug code.
25157 +       It will have a negative impact to the performance.
25158 +
25159 +config AUFS_MAGIC_SYSRQ
25160 +       bool
25161 +       depends on AUFS_DEBUG && MAGIC_SYSRQ
25162 +       default y
25163 +       help
25164 +       Automatic configuration for internal use.
25165 +       When aufs supports Magic SysRq, enabled automatically.
25166 +endif
25167 diff -urN /usr/share/empty/fs/aufs/lcnt.h linux/fs/aufs/lcnt.h
25168 --- /usr/share/empty/fs/aufs/lcnt.h     1970-01-01 01:00:00.000000000 +0100
25169 +++ linux/fs/aufs/lcnt.h        2022-03-21 14:49:05.726633010 +0100
25170 @@ -0,0 +1,186 @@
25171 +/* SPDX-License-Identifier: GPL-2.0 */
25172 +/*
25173 + * Copyright (C) 2018-2021 Junjiro R. Okajima
25174 + *
25175 + * This program, aufs is free software; you can redistribute it and/or modify
25176 + * it under the terms of the GNU General Public License as published by
25177 + * the Free Software Foundation; either version 2 of the License, or
25178 + * (at your option) any later version.
25179 + *
25180 + * This program is distributed in the hope that it will be useful,
25181 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
25182 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25183 + * GNU General Public License for more details.
25184 + *
25185 + * You should have received a copy of the GNU General Public License
25186 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25187 + */
25188 +
25189 +/*
25190 + * simple long counter wrapper
25191 + */
25192 +
25193 +#ifndef __AUFS_LCNT_H__
25194 +#define __AUFS_LCNT_H__
25195 +
25196 +#ifdef __KERNEL__
25197 +
25198 +#include "debug.h"
25199 +
25200 +#define AuLCntATOMIC   1
25201 +#define AuLCntPCPUCNT  2
25202 +/*
25203 + * why does percpu_refcount require extra synchronize_rcu()s in
25204 + * au_br_do_free()
25205 + */
25206 +#define AuLCntPCPUREF  3
25207 +
25208 +/* #define AuLCntChosen        AuLCntATOMIC */
25209 +#define AuLCntChosen   AuLCntPCPUCNT
25210 +/* #define AuLCntChosen        AuLCntPCPUREF */
25211 +
25212 +#if AuLCntChosen == AuLCntATOMIC
25213 +#include <linux/atomic.h>
25214 +
25215 +typedef atomic_long_t au_lcnt_t;
25216 +
25217 +static inline int au_lcnt_init(au_lcnt_t *cnt, void *release __maybe_unused)
25218 +{
25219 +       atomic_long_set(cnt, 0);
25220 +       return 0;
25221 +}
25222 +
25223 +static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
25224 +{
25225 +       /* empty */
25226 +}
25227 +
25228 +static inline void au_lcnt_fin(au_lcnt_t *cnt __maybe_unused,
25229 +                              int do_sync __maybe_unused)
25230 +{
25231 +       /* empty */
25232 +}
25233 +
25234 +static inline void au_lcnt_inc(au_lcnt_t *cnt)
25235 +{
25236 +       atomic_long_inc(cnt);
25237 +}
25238 +
25239 +static inline void au_lcnt_dec(au_lcnt_t *cnt)
25240 +{
25241 +       atomic_long_dec(cnt);
25242 +}
25243 +
25244 +static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev __maybe_unused)
25245 +{
25246 +       return atomic_long_read(cnt);
25247 +}
25248 +#endif
25249 +
25250 +#if AuLCntChosen == AuLCntPCPUCNT
25251 +#include <linux/percpu_counter.h>
25252 +
25253 +typedef struct percpu_counter au_lcnt_t;
25254 +
25255 +static inline int au_lcnt_init(au_lcnt_t *cnt, void *release __maybe_unused)
25256 +{
25257 +       return percpu_counter_init(cnt, 0, GFP_NOFS);
25258 +}
25259 +
25260 +static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
25261 +{
25262 +       /* empty */
25263 +}
25264 +
25265 +static inline void au_lcnt_fin(au_lcnt_t *cnt, int do_sync __maybe_unused)
25266 +{
25267 +       percpu_counter_destroy(cnt);
25268 +}
25269 +
25270 +static inline void au_lcnt_inc(au_lcnt_t *cnt)
25271 +{
25272 +       percpu_counter_inc(cnt);
25273 +}
25274 +
25275 +static inline void au_lcnt_dec(au_lcnt_t *cnt)
25276 +{
25277 +       percpu_counter_dec(cnt);
25278 +}
25279 +
25280 +static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev __maybe_unused)
25281 +{
25282 +       s64 n;
25283 +
25284 +       n = percpu_counter_sum(cnt);
25285 +       BUG_ON(n < 0);
25286 +       if (LONG_MAX != LLONG_MAX
25287 +           && n > LONG_MAX)
25288 +               AuWarn1("%s\n", "wrap-around");
25289 +
25290 +       return n;
25291 +}
25292 +#endif
25293 +
25294 +#if AuLCntChosen == AuLCntPCPUREF
25295 +#include <linux/percpu-refcount.h>
25296 +
25297 +typedef struct percpu_ref au_lcnt_t;
25298 +
25299 +static inline int au_lcnt_init(au_lcnt_t *cnt, percpu_ref_func_t *release)
25300 +{
25301 +       if (!release)
25302 +               release = percpu_ref_exit;
25303 +       return percpu_ref_init(cnt, release, /*percpu mode*/0, GFP_NOFS);
25304 +}
25305 +
25306 +static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
25307 +{
25308 +       synchronize_rcu();
25309 +}
25310 +
25311 +static inline void au_lcnt_fin(au_lcnt_t *cnt, int do_sync)
25312 +{
25313 +       percpu_ref_kill(cnt);
25314 +       if (do_sync)
25315 +               au_lcnt_wait_for_fin(cnt);
25316 +}
25317 +
25318 +static inline void au_lcnt_inc(au_lcnt_t *cnt)
25319 +{
25320 +       percpu_ref_get(cnt);
25321 +}
25322 +
25323 +static inline void au_lcnt_dec(au_lcnt_t *cnt)
25324 +{
25325 +       percpu_ref_put(cnt);
25326 +}
25327 +
25328 +/*
25329 + * avoid calling this func as possible.
25330 + */
25331 +static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev)
25332 +{
25333 +       long l;
25334 +
25335 +       percpu_ref_switch_to_atomic_sync(cnt);
25336 +       l = atomic_long_read(&cnt->count);
25337 +       if (do_rev)
25338 +               percpu_ref_switch_to_percpu(cnt);
25339 +
25340 +       /* percpu_ref is initialized by 1 instead of 0 */
25341 +       return l - 1;
25342 +}
25343 +#endif
25344 +
25345 +#ifdef CONFIG_AUFS_DEBUG
25346 +#define AuLCntZero(val) do {                   \
25347 +       long l = val;                           \
25348 +       if (l)                                  \
25349 +               AuDbg("%s = %ld\n", #val, l);   \
25350 +} while (0)
25351 +#else
25352 +#define AuLCntZero(val)                do {} while (0)
25353 +#endif
25354 +
25355 +#endif /* __KERNEL__ */
25356 +#endif /* __AUFS_LCNT_H__ */
25357 diff -urN /usr/share/empty/fs/aufs/loop.c linux/fs/aufs/loop.c
25358 --- /usr/share/empty/fs/aufs/loop.c     1970-01-01 01:00:00.000000000 +0100
25359 +++ linux/fs/aufs/loop.c        2022-03-21 14:49:05.726633010 +0100
25360 @@ -0,0 +1,148 @@
25361 +// SPDX-License-Identifier: GPL-2.0
25362 +/*
25363 + * Copyright (C) 2005-2021 Junjiro R. Okajima
25364 + *
25365 + * This program, aufs is free software; you can redistribute it and/or modify
25366 + * it under the terms of the GNU General Public License as published by
25367 + * the Free Software Foundation; either version 2 of the License, or
25368 + * (at your option) any later version.
25369 + *
25370 + * This program is distributed in the hope that it will be useful,
25371 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
25372 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25373 + * GNU General Public License for more details.
25374 + *
25375 + * You should have received a copy of the GNU General Public License
25376 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25377 + */
25378 +
25379 +/*
25380 + * support for loopback block device as a branch
25381 + */
25382 +
25383 +#include "aufs.h"
25384 +
25385 +/* added into drivers/block/loop.c */
25386 +static struct file *(*backing_file_func)(struct super_block *sb);
25387 +
25388 +/*
25389 + * test if two lower dentries have overlapping branches.
25390 + */
25391 +int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding)
25392 +{
25393 +       struct super_block *h_sb;
25394 +       struct file *backing_file;
25395 +
25396 +       if (unlikely(!backing_file_func)) {
25397 +               /* don't load "loop" module here */
25398 +               backing_file_func = symbol_get(loop_backing_file);
25399 +               if (unlikely(!backing_file_func))
25400 +                       /* "loop" module is not loaded */
25401 +                       return 0;
25402 +       }
25403 +
25404 +       h_sb = h_adding->d_sb;
25405 +       backing_file = backing_file_func(h_sb);
25406 +       if (!backing_file)
25407 +               return 0;
25408 +
25409 +       h_adding = backing_file->f_path.dentry;
25410 +       /*
25411 +        * h_adding can be local NFS.
25412 +        * in this case aufs cannot detect the loop.
25413 +        */
25414 +       if (unlikely(h_adding->d_sb == sb))
25415 +               return 1;
25416 +       return !!au_test_subdir(h_adding, sb->s_root);
25417 +}
25418 +
25419 +/* true if a kernel thread named 'loop[0-9].*' accesses a file */
25420 +int au_test_loopback_kthread(void)
25421 +{
25422 +       int ret;
25423 +       struct task_struct *tsk = current;
25424 +       char c, comm[sizeof(tsk->comm)];
25425 +
25426 +       ret = 0;
25427 +       if (tsk->flags & PF_KTHREAD) {
25428 +               get_task_comm(comm, tsk);
25429 +               c = comm[4];
25430 +               ret = ('0' <= c && c <= '9'
25431 +                      && !strncmp(comm, "loop", 4));
25432 +       }
25433 +
25434 +       return ret;
25435 +}
25436 +
25437 +/* ---------------------------------------------------------------------- */
25438 +
25439 +#define au_warn_loopback_step  16
25440 +static int au_warn_loopback_nelem = au_warn_loopback_step;
25441 +static unsigned long *au_warn_loopback_array;
25442 +
25443 +void au_warn_loopback(struct super_block *h_sb)
25444 +{
25445 +       int i, new_nelem;
25446 +       unsigned long *a, magic;
25447 +       static DEFINE_SPINLOCK(spin);
25448 +
25449 +       magic = h_sb->s_magic;
25450 +       spin_lock(&spin);
25451 +       a = au_warn_loopback_array;
25452 +       for (i = 0; i < au_warn_loopback_nelem && *a; i++)
25453 +               if (a[i] == magic) {
25454 +                       spin_unlock(&spin);
25455 +                       return;
25456 +               }
25457 +
25458 +       /* h_sb is new to us, print it */
25459 +       if (i < au_warn_loopback_nelem) {
25460 +               a[i] = magic;
25461 +               goto pr;
25462 +       }
25463 +
25464 +       /* expand the array */
25465 +       new_nelem = au_warn_loopback_nelem + au_warn_loopback_step;
25466 +       a = au_kzrealloc(au_warn_loopback_array,
25467 +                        au_warn_loopback_nelem * sizeof(unsigned long),
25468 +                        new_nelem * sizeof(unsigned long), GFP_ATOMIC,
25469 +                        /*may_shrink*/0);
25470 +       if (a) {
25471 +               au_warn_loopback_nelem = new_nelem;
25472 +               au_warn_loopback_array = a;
25473 +               a[i] = magic;
25474 +               goto pr;
25475 +       }
25476 +
25477 +       spin_unlock(&spin);
25478 +       AuWarn1("realloc failed, ignored\n");
25479 +       return;
25480 +
25481 +pr:
25482 +       spin_unlock(&spin);
25483 +       pr_warn("you may want to try another patch for loopback file "
25484 +               "on %s(0x%lx) branch\n", au_sbtype(h_sb), magic);
25485 +}
25486 +
25487 +int au_loopback_init(void)
25488 +{
25489 +       int err;
25490 +       struct super_block *sb __maybe_unused;
25491 +
25492 +       BUILD_BUG_ON(sizeof(sb->s_magic) != sizeof(*au_warn_loopback_array));
25493 +
25494 +       err = 0;
25495 +       au_warn_loopback_array = kcalloc(au_warn_loopback_step,
25496 +                                        sizeof(unsigned long), GFP_NOFS);
25497 +       if (unlikely(!au_warn_loopback_array))
25498 +               err = -ENOMEM;
25499 +
25500 +       return err;
25501 +}
25502 +
25503 +void au_loopback_fin(void)
25504 +{
25505 +       if (backing_file_func)
25506 +               symbol_put(loop_backing_file);
25507 +       au_kfree_try_rcu(au_warn_loopback_array);
25508 +}
25509 diff -urN /usr/share/empty/fs/aufs/loop.h linux/fs/aufs/loop.h
25510 --- /usr/share/empty/fs/aufs/loop.h     1970-01-01 01:00:00.000000000 +0100
25511 +++ linux/fs/aufs/loop.h        2022-03-21 14:49:05.726633010 +0100
25512 @@ -0,0 +1,55 @@
25513 +/* SPDX-License-Identifier: GPL-2.0 */
25514 +/*
25515 + * Copyright (C) 2005-2021 Junjiro R. Okajima
25516 + *
25517 + * This program, aufs is free software; you can redistribute it and/or modify
25518 + * it under the terms of the GNU General Public License as published by
25519 + * the Free Software Foundation; either version 2 of the License, or
25520 + * (at your option) any later version.
25521 + *
25522 + * This program is distributed in the hope that it will be useful,
25523 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
25524 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25525 + * GNU General Public License for more details.
25526 + *
25527 + * You should have received a copy of the GNU General Public License
25528 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25529 + */
25530 +
25531 +/*
25532 + * support for loopback mount as a branch
25533 + */
25534 +
25535 +#ifndef __AUFS_LOOP_H__
25536 +#define __AUFS_LOOP_H__
25537 +
25538 +#ifdef __KERNEL__
25539 +
25540 +struct dentry;
25541 +struct super_block;
25542 +
25543 +#ifdef CONFIG_AUFS_BDEV_LOOP
25544 +/* drivers/block/loop.c */
25545 +struct file *loop_backing_file(struct super_block *sb);
25546 +
25547 +/* loop.c */
25548 +int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding);
25549 +int au_test_loopback_kthread(void);
25550 +void au_warn_loopback(struct super_block *h_sb);
25551 +
25552 +int au_loopback_init(void);
25553 +void au_loopback_fin(void);
25554 +#else
25555 +AuStub(struct file *, loop_backing_file, return NULL, struct super_block *sb)
25556 +
25557 +AuStubInt0(au_test_loopback_overlap, struct super_block *sb,
25558 +          struct dentry *h_adding)
25559 +AuStubInt0(au_test_loopback_kthread, void)
25560 +AuStubVoid(au_warn_loopback, struct super_block *h_sb)
25561 +
25562 +AuStubInt0(au_loopback_init, void)
25563 +AuStubVoid(au_loopback_fin, void)
25564 +#endif /* BLK_DEV_LOOP */
25565 +
25566 +#endif /* __KERNEL__ */
25567 +#endif /* __AUFS_LOOP_H__ */
25568 diff -urN /usr/share/empty/fs/aufs/magic.mk linux/fs/aufs/magic.mk
25569 --- /usr/share/empty/fs/aufs/magic.mk   1970-01-01 01:00:00.000000000 +0100
25570 +++ linux/fs/aufs/magic.mk      2022-03-21 14:49:05.726633010 +0100
25571 @@ -0,0 +1,31 @@
25572 +# SPDX-License-Identifier: GPL-2.0
25573 +
25574 +# defined in ${srctree}/fs/fuse/inode.c
25575 +# tristate
25576 +ifdef CONFIG_FUSE_FS
25577 +ccflags-y += -DFUSE_SUPER_MAGIC=0x65735546
25578 +endif
25579 +
25580 +# defined in ${srctree}/fs/xfs/xfs_sb.h
25581 +# tristate
25582 +ifdef CONFIG_XFS_FS
25583 +ccflags-y += -DXFS_SB_MAGIC=0x58465342
25584 +endif
25585 +
25586 +# defined in ${srctree}/fs/configfs/mount.c
25587 +# tristate
25588 +ifdef CONFIG_CONFIGFS_FS
25589 +ccflags-y += -DCONFIGFS_MAGIC=0x62656570
25590 +endif
25591 +
25592 +# defined in ${srctree}/fs/ubifs/ubifs.h
25593 +# tristate
25594 +ifdef CONFIG_UBIFS_FS
25595 +ccflags-y += -DUBIFS_SUPER_MAGIC=0x24051905
25596 +endif
25597 +
25598 +# defined in ${srctree}/fs/hfsplus/hfsplus_raw.h
25599 +# tristate
25600 +ifdef CONFIG_HFSPLUS_FS
25601 +ccflags-y += -DHFSPLUS_SUPER_MAGIC=0x482b
25602 +endif
25603 diff -urN /usr/share/empty/fs/aufs/Makefile linux/fs/aufs/Makefile
25604 --- /usr/share/empty/fs/aufs/Makefile   1970-01-01 01:00:00.000000000 +0100
25605 +++ linux/fs/aufs/Makefile      2022-03-21 14:49:05.716633010 +0100
25606 @@ -0,0 +1,46 @@
25607 +# SPDX-License-Identifier: GPL-2.0
25608 +
25609 +include ${src}/magic.mk
25610 +ifeq (${CONFIG_AUFS_FS},m)
25611 +include ${src}/conf.mk
25612 +endif
25613 +-include ${src}/priv_def.mk
25614 +
25615 +# cf. include/linux/kernel.h
25616 +# enable pr_debug
25617 +ccflags-y += -DDEBUG
25618 +# sparse requires the full pathname
25619 +ifdef M
25620 +ccflags-y += -include ${M}/../../include/uapi/linux/aufs_type.h
25621 +else
25622 +ccflags-y += -include ${srctree}/include/uapi/linux/aufs_type.h
25623 +endif
25624 +
25625 +obj-$(CONFIG_AUFS_FS) += aufs.o
25626 +aufs-y := module.o sbinfo.o super.o branch.o xino.o sysaufs.o opts.o fsctx.o \
25627 +       wkq.o vfsub.o dcsub.o \
25628 +       cpup.o whout.o wbr_policy.o \
25629 +       dinfo.o dentry.o \
25630 +       dynop.o \
25631 +       finfo.o file.o f_op.o \
25632 +       dir.o vdir.o \
25633 +       iinfo.o inode.o i_op.o i_op_add.o i_op_del.o i_op_ren.o \
25634 +       mvdown.o ioctl.o
25635 +
25636 +# all are boolean
25637 +aufs-$(CONFIG_PROC_FS) += procfs.o plink.o
25638 +aufs-$(CONFIG_SYSFS) += sysfs.o
25639 +aufs-$(CONFIG_DEBUG_FS) += dbgaufs.o
25640 +aufs-$(CONFIG_AUFS_BDEV_LOOP) += loop.o
25641 +aufs-$(CONFIG_AUFS_HNOTIFY) += hnotify.o
25642 +aufs-$(CONFIG_AUFS_HFSNOTIFY) += hfsnotify.o
25643 +aufs-$(CONFIG_AUFS_EXPORT) += export.o
25644 +aufs-$(CONFIG_AUFS_XATTR) += xattr.o
25645 +aufs-$(CONFIG_FS_POSIX_ACL) += posix_acl.o
25646 +aufs-$(CONFIG_AUFS_DIRREN) += dirren.o
25647 +aufs-$(CONFIG_AUFS_FHSM) += fhsm.o
25648 +aufs-$(CONFIG_AUFS_POLL) += poll.o
25649 +aufs-$(CONFIG_AUFS_RDU) += rdu.o
25650 +aufs-$(CONFIG_AUFS_BR_HFSPLUS) += hfsplus.o
25651 +aufs-$(CONFIG_AUFS_DEBUG) += debug.o
25652 +aufs-$(CONFIG_AUFS_MAGIC_SYSRQ) += sysrq.o
25653 diff -urN /usr/share/empty/fs/aufs/module.c linux/fs/aufs/module.c
25654 --- /usr/share/empty/fs/aufs/module.c   1970-01-01 01:00:00.000000000 +0100
25655 +++ linux/fs/aufs/module.c      2022-03-21 14:49:05.726633010 +0100
25656 @@ -0,0 +1,273 @@
25657 +// SPDX-License-Identifier: GPL-2.0
25658 +/*
25659 + * Copyright (C) 2005-2021 Junjiro R. Okajima
25660 + *
25661 + * This program, aufs is free software; you can redistribute it and/or modify
25662 + * it under the terms of the GNU General Public License as published by
25663 + * the Free Software Foundation; either version 2 of the License, or
25664 + * (at your option) any later version.
25665 + *
25666 + * This program is distributed in the hope that it will be useful,
25667 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
25668 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25669 + * GNU General Public License for more details.
25670 + *
25671 + * You should have received a copy of the GNU General Public License
25672 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25673 + */
25674 +
25675 +/*
25676 + * module global variables and operations
25677 + */
25678 +
25679 +#include <linux/module.h>
25680 +#include <linux/seq_file.h>
25681 +#include "aufs.h"
25682 +
25683 +/* shrinkable realloc */
25684 +void *au_krealloc(void *p, unsigned int new_sz, gfp_t gfp, int may_shrink)
25685 +{
25686 +       size_t sz;
25687 +       int diff;
25688 +
25689 +       sz = 0;
25690 +       diff = -1;
25691 +       if (p) {
25692 +#if 0 /* unused */
25693 +               if (!new_sz) {
25694 +                       au_kfree_rcu(p);
25695 +                       p = NULL;
25696 +                       goto out;
25697 +               }
25698 +#else
25699 +               AuDebugOn(!new_sz);
25700 +#endif
25701 +               sz = ksize(p);
25702 +               diff = au_kmidx_sub(sz, new_sz);
25703 +       }
25704 +       if (sz && !diff)
25705 +               goto out;
25706 +
25707 +       if (sz < new_sz)
25708 +               /* expand or SLOB */
25709 +               p = krealloc(p, new_sz, gfp);
25710 +       else if (new_sz < sz && may_shrink) {
25711 +               /* shrink */
25712 +               void *q;
25713 +
25714 +               q = kmalloc(new_sz, gfp);
25715 +               if (q) {
25716 +                       if (p) {
25717 +                               memcpy(q, p, new_sz);
25718 +                               au_kfree_try_rcu(p);
25719 +                       }
25720 +                       p = q;
25721 +               } else
25722 +                       p = NULL;
25723 +       }
25724 +
25725 +out:
25726 +       return p;
25727 +}
25728 +
25729 +void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp,
25730 +                  int may_shrink)
25731 +{
25732 +       p = au_krealloc(p, new_sz, gfp, may_shrink);
25733 +       if (p && new_sz > nused)
25734 +               memset(p + nused, 0, new_sz - nused);
25735 +       return p;
25736 +}
25737 +
25738 +/* ---------------------------------------------------------------------- */
25739 +/*
25740 + * aufs caches
25741 + */
25742 +struct kmem_cache *au_cache[AuCache_Last];
25743 +
25744 +static void au_cache_fin(void)
25745 +{
25746 +       int i;
25747 +
25748 +       /*
25749 +        * Make sure all delayed rcu free inodes are flushed before we
25750 +        * destroy cache.
25751 +        */
25752 +       rcu_barrier();
25753 +
25754 +       /* excluding AuCache_HNOTIFY */
25755 +       BUILD_BUG_ON(AuCache_HNOTIFY + 1 != AuCache_Last);
25756 +       for (i = 0; i < AuCache_HNOTIFY; i++) {
25757 +               kmem_cache_destroy(au_cache[i]);
25758 +               au_cache[i] = NULL;
25759 +       }
25760 +}
25761 +
25762 +static int __init au_cache_init(void)
25763 +{
25764 +       au_cache[AuCache_DINFO] = AuCacheCtor(au_dinfo, au_di_init_once);
25765 +       if (au_cache[AuCache_DINFO])
25766 +               /* SLAB_DESTROY_BY_RCU */
25767 +               au_cache[AuCache_ICNTNR] = AuCacheCtor(au_icntnr,
25768 +                                                      au_icntnr_init_once);
25769 +       if (au_cache[AuCache_ICNTNR])
25770 +               au_cache[AuCache_FINFO] = AuCacheCtor(au_finfo,
25771 +                                                     au_fi_init_once);
25772 +       if (au_cache[AuCache_FINFO])
25773 +               au_cache[AuCache_VDIR] = AuCache(au_vdir);
25774 +       if (au_cache[AuCache_VDIR])
25775 +               au_cache[AuCache_DEHSTR] = AuCache(au_vdir_dehstr);
25776 +       if (au_cache[AuCache_DEHSTR])
25777 +               return 0;
25778 +
25779 +       au_cache_fin();
25780 +       return -ENOMEM;
25781 +}
25782 +
25783 +/* ---------------------------------------------------------------------- */
25784 +
25785 +int au_dir_roflags;
25786 +
25787 +#ifdef CONFIG_AUFS_SBILIST
25788 +/*
25789 + * iterate_supers_type() doesn't protect us from
25790 + * remounting (branch management)
25791 + */
25792 +struct hlist_bl_head au_sbilist;
25793 +#endif
25794 +
25795 +/*
25796 + * functions for module interface.
25797 + */
25798 +MODULE_LICENSE("GPL");
25799 +/* MODULE_LICENSE("GPL v2"); */
25800 +MODULE_AUTHOR("Junjiro R. Okajima <aufs-users@lists.sourceforge.net>");
25801 +MODULE_DESCRIPTION(AUFS_NAME
25802 +       " -- Advanced multi layered unification filesystem");
25803 +MODULE_VERSION(AUFS_VERSION);
25804 +MODULE_ALIAS_FS(AUFS_NAME);
25805 +
25806 +/* this module parameter has no meaning when SYSFS is disabled */
25807 +int sysaufs_brs = 1;
25808 +MODULE_PARM_DESC(brs, "use <sysfs>/fs/aufs/si_*/brN");
25809 +module_param_named(brs, sysaufs_brs, int, 0444);
25810 +
25811 +/* this module parameter has no meaning when USER_NS is disabled */
25812 +bool au_userns;
25813 +MODULE_PARM_DESC(allow_userns, "allow unprivileged to mount under userns");
25814 +module_param_named(allow_userns, au_userns, bool, 0444);
25815 +
25816 +/* ---------------------------------------------------------------------- */
25817 +
25818 +static char au_esc_chars[0x20 + 3]; /* 0x01-0x20, backslash, del, and NULL */
25819 +
25820 +int au_seq_path(struct seq_file *seq, struct path *path)
25821 +{
25822 +       int err;
25823 +
25824 +       err = seq_path(seq, path, au_esc_chars);
25825 +       if (err >= 0)
25826 +               err = 0;
25827 +       else
25828 +               err = -ENOMEM;
25829 +
25830 +       return err;
25831 +}
25832 +
25833 +/* ---------------------------------------------------------------------- */
25834 +
25835 +static int __init aufs_init(void)
25836 +{
25837 +       int err, i;
25838 +       char *p;
25839 +
25840 +       p = au_esc_chars;
25841 +       for (i = 1; i <= ' '; i++)
25842 +               *p++ = i;
25843 +       *p++ = '\\';
25844 +       *p++ = '\x7f';
25845 +       *p = 0;
25846 +
25847 +       au_dir_roflags = au_file_roflags(O_DIRECTORY | O_LARGEFILE);
25848 +
25849 +       memcpy(aufs_iop_nogetattr, aufs_iop, sizeof(aufs_iop));
25850 +       for (i = 0; i < AuIop_Last; i++)
25851 +               aufs_iop_nogetattr[i].getattr = NULL;
25852 +
25853 +       memset(au_cache, 0, sizeof(au_cache));  /* including hnotify */
25854 +
25855 +       au_sbilist_init();
25856 +       sysaufs_brs_init();
25857 +       au_debug_init();
25858 +       au_dy_init();
25859 +       err = sysaufs_init();
25860 +       if (unlikely(err))
25861 +               goto out;
25862 +       err = dbgaufs_init();
25863 +       if (unlikely(err))
25864 +               goto out_sysaufs;
25865 +       err = au_procfs_init();
25866 +       if (unlikely(err))
25867 +               goto out_dbgaufs;
25868 +       err = au_wkq_init();
25869 +       if (unlikely(err))
25870 +               goto out_procfs;
25871 +       err = au_loopback_init();
25872 +       if (unlikely(err))
25873 +               goto out_wkq;
25874 +       err = au_hnotify_init();
25875 +       if (unlikely(err))
25876 +               goto out_loopback;
25877 +       err = au_sysrq_init();
25878 +       if (unlikely(err))
25879 +               goto out_hin;
25880 +       err = au_cache_init();
25881 +       if (unlikely(err))
25882 +               goto out_sysrq;
25883 +
25884 +       aufs_fs_type.fs_flags |= au_userns ? FS_USERNS_MOUNT : 0;
25885 +       err = register_filesystem(&aufs_fs_type);
25886 +       if (unlikely(err))
25887 +               goto out_cache;
25888 +
25889 +       /* since we define pr_fmt, call printk directly */
25890 +       printk(KERN_INFO AUFS_NAME " " AUFS_VERSION "\n");
25891 +       goto out; /* success */
25892 +
25893 +out_cache:
25894 +       au_cache_fin();
25895 +out_sysrq:
25896 +       au_sysrq_fin();
25897 +out_hin:
25898 +       au_hnotify_fin();
25899 +out_loopback:
25900 +       au_loopback_fin();
25901 +out_wkq:
25902 +       au_wkq_fin();
25903 +out_procfs:
25904 +       au_procfs_fin();
25905 +out_dbgaufs:
25906 +       dbgaufs_fin();
25907 +out_sysaufs:
25908 +       sysaufs_fin();
25909 +       au_dy_fin();
25910 +out:
25911 +       return err;
25912 +}
25913 +
25914 +static void __exit aufs_exit(void)
25915 +{
25916 +       unregister_filesystem(&aufs_fs_type);
25917 +       au_cache_fin();
25918 +       au_sysrq_fin();
25919 +       au_hnotify_fin();
25920 +       au_loopback_fin();
25921 +       au_wkq_fin();
25922 +       au_procfs_fin();
25923 +       dbgaufs_fin();
25924 +       sysaufs_fin();
25925 +       au_dy_fin();
25926 +}
25927 +
25928 +module_init(aufs_init);
25929 +module_exit(aufs_exit);
25930 diff -urN /usr/share/empty/fs/aufs/module.h linux/fs/aufs/module.h
25931 --- /usr/share/empty/fs/aufs/module.h   1970-01-01 01:00:00.000000000 +0100
25932 +++ linux/fs/aufs/module.h      2022-03-21 14:49:05.726633010 +0100
25933 @@ -0,0 +1,166 @@
25934 +/* SPDX-License-Identifier: GPL-2.0 */
25935 +/*
25936 + * Copyright (C) 2005-2021 Junjiro R. Okajima
25937 + *
25938 + * This program, aufs is free software; you can redistribute it and/or modify
25939 + * it under the terms of the GNU General Public License as published by
25940 + * the Free Software Foundation; either version 2 of the License, or
25941 + * (at your option) any later version.
25942 + *
25943 + * This program is distributed in the hope that it will be useful,
25944 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
25945 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25946 + * GNU General Public License for more details.
25947 + *
25948 + * You should have received a copy of the GNU General Public License
25949 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25950 + */
25951 +
25952 +/*
25953 + * module initialization and module-global
25954 + */
25955 +
25956 +#ifndef __AUFS_MODULE_H__
25957 +#define __AUFS_MODULE_H__
25958 +
25959 +#ifdef __KERNEL__
25960 +
25961 +#include <linux/slab.h>
25962 +#include "debug.h"
25963 +#include "dentry.h"
25964 +#include "dir.h"
25965 +#include "file.h"
25966 +#include "inode.h"
25967 +
25968 +struct path;
25969 +struct seq_file;
25970 +
25971 +/* module parameters */
25972 +extern int sysaufs_brs;
25973 +extern bool au_userns;
25974 +
25975 +/* ---------------------------------------------------------------------- */
25976 +
25977 +extern int au_dir_roflags;
25978 +
25979 +void *au_krealloc(void *p, unsigned int new_sz, gfp_t gfp, int may_shrink);
25980 +void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp,
25981 +                  int may_shrink);
25982 +
25983 +/*
25984 + * Comparing the size of the object with sizeof(struct rcu_head)
25985 + * case 1: object is always larger
25986 + *     --> au_kfree_rcu() or au_kfree_do_rcu()
25987 + * case 2: object is always smaller
25988 + *     --> au_kfree_small()
25989 + * case 3: object can be any size
25990 + *     --> au_kfree_try_rcu()
25991 + */
25992 +
25993 +static inline void au_kfree_do_rcu(const void *p)
25994 +{
25995 +       struct {
25996 +               struct rcu_head rcu;
25997 +       } *a = (void *)p;
25998 +
25999 +       kfree_rcu(a, rcu);
26000 +}
26001 +
26002 +#define au_kfree_rcu(_p) do {                                          \
26003 +               typeof(_p) p = (_p);                                    \
26004 +               BUILD_BUG_ON(sizeof(*p) < sizeof(struct rcu_head));     \
26005 +               if (p)                                                  \
26006 +                       au_kfree_do_rcu(p);                             \
26007 +       } while (0)
26008 +
26009 +#define au_kfree_do_sz_test(sz)        (sz >= sizeof(struct rcu_head))
26010 +#define au_kfree_sz_test(p)    (p && au_kfree_do_sz_test(ksize(p)))
26011 +
26012 +static inline void au_kfree_try_rcu(const void *p)
26013 +{
26014 +       if (!p)
26015 +               return;
26016 +       if (au_kfree_sz_test(p))
26017 +               au_kfree_do_rcu(p);
26018 +       else
26019 +               kfree(p);
26020 +}
26021 +
26022 +static inline void au_kfree_small(const void *p)
26023 +{
26024 +       if (!p)
26025 +               return;
26026 +       AuDebugOn(au_kfree_sz_test(p));
26027 +       kfree(p);
26028 +}
26029 +
26030 +static inline int au_kmidx_sub(size_t sz, size_t new_sz)
26031 +{
26032 +#ifndef CONFIG_SLOB
26033 +       return __kmalloc_index(sz, false) - __kmalloc_index(new_sz, false);
26034 +#else
26035 +       return -1; /* SLOB is untested */
26036 +#endif
26037 +}
26038 +
26039 +int au_seq_path(struct seq_file *seq, struct path *path);
26040 +
26041 +#ifdef CONFIG_PROC_FS
26042 +/* procfs.c */
26043 +int __init au_procfs_init(void);
26044 +void au_procfs_fin(void);
26045 +#else
26046 +AuStubInt0(au_procfs_init, void);
26047 +AuStubVoid(au_procfs_fin, void);
26048 +#endif
26049 +
26050 +/* ---------------------------------------------------------------------- */
26051 +
26052 +/* kmem cache */
26053 +enum {
26054 +       AuCache_DINFO,
26055 +       AuCache_ICNTNR,
26056 +       AuCache_FINFO,
26057 +       AuCache_VDIR,
26058 +       AuCache_DEHSTR,
26059 +       AuCache_HNOTIFY, /* must be last */
26060 +       AuCache_Last
26061 +};
26062 +
26063 +extern struct kmem_cache *au_cache[AuCache_Last];
26064 +
26065 +#define AuCacheFlags           (SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD)
26066 +#define AuCache(type)          KMEM_CACHE(type, AuCacheFlags)
26067 +#define AuCacheCtor(type, ctor)        \
26068 +       kmem_cache_create(#type, sizeof(struct type), \
26069 +                         __alignof__(struct type), AuCacheFlags, ctor)
26070 +
26071 +#define AuCacheFuncs(name, index)                                      \
26072 +       static inline struct au_##name *au_cache_alloc_##name(void)     \
26073 +       { return kmem_cache_alloc(au_cache[AuCache_##index], GFP_NOFS); } \
26074 +       static inline void au_cache_free_##name##_norcu(struct au_##name *p) \
26075 +       { kmem_cache_free(au_cache[AuCache_##index], p); }              \
26076 +                                                                       \
26077 +       static inline void au_cache_free_##name##_rcu_cb(struct rcu_head *rcu) \
26078 +       { void *p = rcu;                                                \
26079 +               p -= offsetof(struct au_##name, rcu);                   \
26080 +               kmem_cache_free(au_cache[AuCache_##index], p); }        \
26081 +       static inline void au_cache_free_##name##_rcu(struct au_##name *p) \
26082 +       { BUILD_BUG_ON(sizeof(struct au_##name) < sizeof(struct rcu_head)); \
26083 +               call_rcu(&p->rcu, au_cache_free_##name##_rcu_cb); }     \
26084 +                                                                       \
26085 +       static inline void au_cache_free_##name(struct au_##name *p)    \
26086 +       { /* au_cache_free_##name##_norcu(p); */                        \
26087 +               au_cache_free_##name##_rcu(p); }
26088 +
26089 +AuCacheFuncs(dinfo, DINFO);
26090 +AuCacheFuncs(icntnr, ICNTNR);
26091 +AuCacheFuncs(finfo, FINFO);
26092 +AuCacheFuncs(vdir, VDIR);
26093 +AuCacheFuncs(vdir_dehstr, DEHSTR);
26094 +#ifdef CONFIG_AUFS_HNOTIFY
26095 +AuCacheFuncs(hnotify, HNOTIFY);
26096 +#endif
26097 +
26098 +#endif /* __KERNEL__ */
26099 +#endif /* __AUFS_MODULE_H__ */
26100 diff -urN /usr/share/empty/fs/aufs/mvdown.c linux/fs/aufs/mvdown.c
26101 --- /usr/share/empty/fs/aufs/mvdown.c   1970-01-01 01:00:00.000000000 +0100
26102 +++ linux/fs/aufs/mvdown.c      2022-03-21 14:49:05.729966343 +0100
26103 @@ -0,0 +1,706 @@
26104 +// SPDX-License-Identifier: GPL-2.0
26105 +/*
26106 + * Copyright (C) 2011-2021 Junjiro R. Okajima
26107 + *
26108 + * This program, aufs is free software; you can redistribute it and/or modify
26109 + * it under the terms of the GNU General Public License as published by
26110 + * the Free Software Foundation; either version 2 of the License, or
26111 + * (at your option) any later version.
26112 + *
26113 + * This program is distributed in the hope that it will be useful,
26114 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
26115 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26116 + * GNU General Public License for more details.
26117 + *
26118 + * You should have received a copy of the GNU General Public License
26119 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
26120 + */
26121 +
26122 +/*
26123 + * move-down, opposite of copy-up
26124 + */
26125 +
26126 +#include "aufs.h"
26127 +
26128 +struct au_mvd_args {
26129 +       struct {
26130 +               struct super_block *h_sb;
26131 +               struct dentry *h_parent;
26132 +               struct au_hinode *hdir;
26133 +               struct inode *h_dir, *h_inode;
26134 +               struct au_pin pin;
26135 +       } info[AUFS_MVDOWN_NARRAY];
26136 +
26137 +       struct aufs_mvdown mvdown;
26138 +       struct dentry *dentry, *parent;
26139 +       struct inode *inode, *dir;
26140 +       struct super_block *sb;
26141 +       aufs_bindex_t bopq, bwh, bfound;
26142 +       unsigned char rename_lock;
26143 +};
26144 +
26145 +#define mvd_errno              mvdown.au_errno
26146 +#define mvd_bsrc               mvdown.stbr[AUFS_MVDOWN_UPPER].bindex
26147 +#define mvd_src_brid           mvdown.stbr[AUFS_MVDOWN_UPPER].brid
26148 +#define mvd_bdst               mvdown.stbr[AUFS_MVDOWN_LOWER].bindex
26149 +#define mvd_dst_brid           mvdown.stbr[AUFS_MVDOWN_LOWER].brid
26150 +
26151 +#define mvd_h_src_sb           info[AUFS_MVDOWN_UPPER].h_sb
26152 +#define mvd_h_src_parent       info[AUFS_MVDOWN_UPPER].h_parent
26153 +#define mvd_hdir_src           info[AUFS_MVDOWN_UPPER].hdir
26154 +#define mvd_h_src_dir          info[AUFS_MVDOWN_UPPER].h_dir
26155 +#define mvd_h_src_inode                info[AUFS_MVDOWN_UPPER].h_inode
26156 +#define mvd_pin_src            info[AUFS_MVDOWN_UPPER].pin
26157 +
26158 +#define mvd_h_dst_sb           info[AUFS_MVDOWN_LOWER].h_sb
26159 +#define mvd_h_dst_parent       info[AUFS_MVDOWN_LOWER].h_parent
26160 +#define mvd_hdir_dst           info[AUFS_MVDOWN_LOWER].hdir
26161 +#define mvd_h_dst_dir          info[AUFS_MVDOWN_LOWER].h_dir
26162 +#define mvd_h_dst_inode                info[AUFS_MVDOWN_LOWER].h_inode
26163 +#define mvd_pin_dst            info[AUFS_MVDOWN_LOWER].pin
26164 +
26165 +#define AU_MVD_PR(flag, ...) do {                      \
26166 +               if (flag)                               \
26167 +                       pr_err(__VA_ARGS__);            \
26168 +       } while (0)
26169 +
26170 +static int find_lower_writable(struct au_mvd_args *a)
26171 +{
26172 +       struct super_block *sb;
26173 +       aufs_bindex_t bindex, bbot;
26174 +       struct au_branch *br;
26175 +
26176 +       sb = a->sb;
26177 +       bindex = a->mvd_bsrc;
26178 +       bbot = au_sbbot(sb);
26179 +       if (a->mvdown.flags & AUFS_MVDOWN_FHSM_LOWER)
26180 +               for (bindex++; bindex <= bbot; bindex++) {
26181 +                       br = au_sbr(sb, bindex);
26182 +                       if (au_br_fhsm(br->br_perm)
26183 +                           && !sb_rdonly(au_br_sb(br)))
26184 +                               return bindex;
26185 +               }
26186 +       else if (!(a->mvdown.flags & AUFS_MVDOWN_ROLOWER))
26187 +               for (bindex++; bindex <= bbot; bindex++) {
26188 +                       br = au_sbr(sb, bindex);
26189 +                       if (!au_br_rdonly(br))
26190 +                               return bindex;
26191 +               }
26192 +       else
26193 +               for (bindex++; bindex <= bbot; bindex++) {
26194 +                       br = au_sbr(sb, bindex);
26195 +                       if (!sb_rdonly(au_br_sb(br))) {
26196 +                               if (au_br_rdonly(br))
26197 +                                       a->mvdown.flags
26198 +                                               |= AUFS_MVDOWN_ROLOWER_R;
26199 +                               return bindex;
26200 +                       }
26201 +               }
26202 +
26203 +       return -1;
26204 +}
26205 +
26206 +/* make the parent dir on bdst */
26207 +static int au_do_mkdir(const unsigned char dmsg, struct au_mvd_args *a)
26208 +{
26209 +       int err;
26210 +
26211 +       err = 0;
26212 +       a->mvd_hdir_src = au_hi(a->dir, a->mvd_bsrc);
26213 +       a->mvd_hdir_dst = au_hi(a->dir, a->mvd_bdst);
26214 +       a->mvd_h_src_parent = au_h_dptr(a->parent, a->mvd_bsrc);
26215 +       a->mvd_h_dst_parent = NULL;
26216 +       if (au_dbbot(a->parent) >= a->mvd_bdst)
26217 +               a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
26218 +       if (!a->mvd_h_dst_parent) {
26219 +               err = au_cpdown_dirs(a->dentry, a->mvd_bdst);
26220 +               if (unlikely(err)) {
26221 +                       AU_MVD_PR(dmsg, "cpdown_dirs failed\n");
26222 +                       goto out;
26223 +               }
26224 +               a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
26225 +       }
26226 +
26227 +out:
26228 +       AuTraceErr(err);
26229 +       return err;
26230 +}
26231 +
26232 +/* lock them all */
26233 +static int au_do_lock(const unsigned char dmsg, struct au_mvd_args *a)
26234 +{
26235 +       int err;
26236 +       struct dentry *h_trap;
26237 +
26238 +       a->mvd_h_src_sb = au_sbr_sb(a->sb, a->mvd_bsrc);
26239 +       a->mvd_h_dst_sb = au_sbr_sb(a->sb, a->mvd_bdst);
26240 +       err = au_pin(&a->mvd_pin_dst, a->dentry, a->mvd_bdst,
26241 +                    au_opt_udba(a->sb),
26242 +                    AuPin_MNT_WRITE | AuPin_DI_LOCKED);
26243 +       AuTraceErr(err);
26244 +       if (unlikely(err)) {
26245 +               AU_MVD_PR(dmsg, "pin_dst failed\n");
26246 +               goto out;
26247 +       }
26248 +
26249 +       if (a->mvd_h_src_sb != a->mvd_h_dst_sb) {
26250 +               a->rename_lock = 0;
26251 +               au_pin_init(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
26252 +                           AuLsc_DI_PARENT, AuLsc_I_PARENT3,
26253 +                           au_opt_udba(a->sb),
26254 +                           AuPin_MNT_WRITE | AuPin_DI_LOCKED);
26255 +               err = au_do_pin(&a->mvd_pin_src);
26256 +               AuTraceErr(err);
26257 +               a->mvd_h_src_dir = d_inode(a->mvd_h_src_parent);
26258 +               if (unlikely(err)) {
26259 +                       AU_MVD_PR(dmsg, "pin_src failed\n");
26260 +                       goto out_dst;
26261 +               }
26262 +               goto out; /* success */
26263 +       }
26264 +
26265 +       a->rename_lock = 1;
26266 +       au_pin_hdir_unlock(&a->mvd_pin_dst);
26267 +       err = au_pin(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
26268 +                    au_opt_udba(a->sb),
26269 +                    AuPin_MNT_WRITE | AuPin_DI_LOCKED);
26270 +       AuTraceErr(err);
26271 +       a->mvd_h_src_dir = d_inode(a->mvd_h_src_parent);
26272 +       if (unlikely(err)) {
26273 +               AU_MVD_PR(dmsg, "pin_src failed\n");
26274 +               au_pin_hdir_lock(&a->mvd_pin_dst);
26275 +               goto out_dst;
26276 +       }
26277 +       au_pin_hdir_unlock(&a->mvd_pin_src);
26278 +       h_trap = vfsub_lock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
26279 +                                  a->mvd_h_dst_parent, a->mvd_hdir_dst);
26280 +       if (h_trap) {
26281 +               err = (h_trap != a->mvd_h_src_parent);
26282 +               if (err)
26283 +                       err = (h_trap != a->mvd_h_dst_parent);
26284 +       }
26285 +       BUG_ON(err); /* it should never happen */
26286 +       if (unlikely(a->mvd_h_src_dir != au_pinned_h_dir(&a->mvd_pin_src))) {
26287 +               err = -EBUSY;
26288 +               AuTraceErr(err);
26289 +               vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
26290 +                                   a->mvd_h_dst_parent, a->mvd_hdir_dst);
26291 +               au_pin_hdir_lock(&a->mvd_pin_src);
26292 +               au_unpin(&a->mvd_pin_src);
26293 +               au_pin_hdir_lock(&a->mvd_pin_dst);
26294 +               goto out_dst;
26295 +       }
26296 +       goto out; /* success */
26297 +
26298 +out_dst:
26299 +       au_unpin(&a->mvd_pin_dst);
26300 +out:
26301 +       AuTraceErr(err);
26302 +       return err;
26303 +}
26304 +
26305 +static void au_do_unlock(const unsigned char dmsg, struct au_mvd_args *a)
26306 +{
26307 +       if (!a->rename_lock)
26308 +               au_unpin(&a->mvd_pin_src);
26309 +       else {
26310 +               vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
26311 +                                   a->mvd_h_dst_parent, a->mvd_hdir_dst);
26312 +               au_pin_hdir_lock(&a->mvd_pin_src);
26313 +               au_unpin(&a->mvd_pin_src);
26314 +               au_pin_hdir_lock(&a->mvd_pin_dst);
26315 +       }
26316 +       au_unpin(&a->mvd_pin_dst);
26317 +}
26318 +
26319 +/* copy-down the file */
26320 +static int au_do_cpdown(const unsigned char dmsg, struct au_mvd_args *a)
26321 +{
26322 +       int err;
26323 +       struct au_cp_generic cpg = {
26324 +               .dentry = a->dentry,
26325 +               .bdst   = a->mvd_bdst,
26326 +               .bsrc   = a->mvd_bsrc,
26327 +               .len    = -1,
26328 +               .pin    = &a->mvd_pin_dst,
26329 +               .flags  = AuCpup_DTIME | AuCpup_HOPEN
26330 +       };
26331 +
26332 +       AuDbg("b%d, b%d\n", cpg.bsrc, cpg.bdst);
26333 +       if (a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
26334 +               au_fset_cpup(cpg.flags, OVERWRITE);
26335 +       if (a->mvdown.flags & AUFS_MVDOWN_ROLOWER)
26336 +               au_fset_cpup(cpg.flags, RWDST);
26337 +       err = au_sio_cpdown_simple(&cpg);
26338 +       if (unlikely(err))
26339 +               AU_MVD_PR(dmsg, "cpdown failed\n");
26340 +
26341 +       AuTraceErr(err);
26342 +       return err;
26343 +}
26344 +
26345 +/*
26346 + * unlink the whiteout on bdst if exist which may be created by UDBA while we
26347 + * were sleeping
26348 + */
26349 +static int au_do_unlink_wh(const unsigned char dmsg, struct au_mvd_args *a)
26350 +{
26351 +       int err;
26352 +       struct path h_path;
26353 +       struct au_branch *br;
26354 +       struct inode *delegated;
26355 +
26356 +       br = au_sbr(a->sb, a->mvd_bdst);
26357 +       h_path.dentry = au_wh_lkup(a->mvd_h_dst_parent, &a->dentry->d_name, br);
26358 +       err = PTR_ERR(h_path.dentry);
26359 +       if (IS_ERR(h_path.dentry)) {
26360 +               AU_MVD_PR(dmsg, "wh_lkup failed\n");
26361 +               goto out;
26362 +       }
26363 +
26364 +       err = 0;
26365 +       if (d_is_positive(h_path.dentry)) {
26366 +               h_path.mnt = au_br_mnt(br);
26367 +               delegated = NULL;
26368 +               err = vfsub_unlink(d_inode(a->mvd_h_dst_parent), &h_path,
26369 +                                  &delegated, /*force*/0);
26370 +               if (unlikely(err == -EWOULDBLOCK)) {
26371 +                       pr_warn("cannot retry for NFSv4 delegation"
26372 +                               " for an internal unlink\n");
26373 +                       iput(delegated);
26374 +               }
26375 +               if (unlikely(err))
26376 +                       AU_MVD_PR(dmsg, "wh_unlink failed\n");
26377 +       }
26378 +       dput(h_path.dentry);
26379 +
26380 +out:
26381 +       AuTraceErr(err);
26382 +       return err;
26383 +}
26384 +
26385 +/*
26386 + * unlink the topmost h_dentry
26387 + */
26388 +static int au_do_unlink(const unsigned char dmsg, struct au_mvd_args *a)
26389 +{
26390 +       int err;
26391 +       struct path h_path;
26392 +       struct inode *delegated;
26393 +
26394 +       h_path.mnt = au_sbr_mnt(a->sb, a->mvd_bsrc);
26395 +       h_path.dentry = au_h_dptr(a->dentry, a->mvd_bsrc);
26396 +       delegated = NULL;
26397 +       err = vfsub_unlink(a->mvd_h_src_dir, &h_path, &delegated, /*force*/0);
26398 +       if (unlikely(err == -EWOULDBLOCK)) {
26399 +               pr_warn("cannot retry for NFSv4 delegation"
26400 +                       " for an internal unlink\n");
26401 +               iput(delegated);
26402 +       }
26403 +       if (unlikely(err))
26404 +               AU_MVD_PR(dmsg, "unlink failed\n");
26405 +
26406 +       AuTraceErr(err);
26407 +       return err;
26408 +}
26409 +
26410 +/* Since mvdown succeeded, we ignore an error of this function */
26411 +static void au_do_stfs(const unsigned char dmsg, struct au_mvd_args *a)
26412 +{
26413 +       int err;
26414 +       struct au_branch *br;
26415 +
26416 +       a->mvdown.flags |= AUFS_MVDOWN_STFS_FAILED;
26417 +       br = au_sbr(a->sb, a->mvd_bsrc);
26418 +       err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_UPPER].stfs);
26419 +       if (!err) {
26420 +               br = au_sbr(a->sb, a->mvd_bdst);
26421 +               a->mvdown.stbr[AUFS_MVDOWN_LOWER].brid = br->br_id;
26422 +               err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_LOWER].stfs);
26423 +       }
26424 +       if (!err)
26425 +               a->mvdown.flags &= ~AUFS_MVDOWN_STFS_FAILED;
26426 +       else
26427 +               AU_MVD_PR(dmsg, "statfs failed (%d), ignored\n", err);
26428 +}
26429 +
26430 +/*
26431 + * copy-down the file and unlink the bsrc file.
26432 + * - unlink the bdst whout if exist
26433 + * - copy-down the file (with whtmp name and rename)
26434 + * - unlink the bsrc file
26435 + */
26436 +static int au_do_mvdown(const unsigned char dmsg, struct au_mvd_args *a)
26437 +{
26438 +       int err;
26439 +
26440 +       err = au_do_mkdir(dmsg, a);
26441 +       if (!err)
26442 +               err = au_do_lock(dmsg, a);
26443 +       if (unlikely(err))
26444 +               goto out;
26445 +
26446 +       /*
26447 +        * do not revert the activities we made on bdst since they should be
26448 +        * harmless in aufs.
26449 +        */
26450 +
26451 +       err = au_do_cpdown(dmsg, a);
26452 +       if (!err)
26453 +               err = au_do_unlink_wh(dmsg, a);
26454 +       if (!err && !(a->mvdown.flags & AUFS_MVDOWN_KUPPER))
26455 +               err = au_do_unlink(dmsg, a);
26456 +       if (unlikely(err))
26457 +               goto out_unlock;
26458 +
26459 +       AuDbg("%pd2, 0x%x, %d --> %d\n",
26460 +             a->dentry, a->mvdown.flags, a->mvd_bsrc, a->mvd_bdst);
26461 +       if (find_lower_writable(a) < 0)
26462 +               a->mvdown.flags |= AUFS_MVDOWN_BOTTOM;
26463 +
26464 +       if (a->mvdown.flags & AUFS_MVDOWN_STFS)
26465 +               au_do_stfs(dmsg, a);
26466 +
26467 +       /* maintain internal array */
26468 +       if (!(a->mvdown.flags & AUFS_MVDOWN_KUPPER)) {
26469 +               au_set_h_dptr(a->dentry, a->mvd_bsrc, NULL);
26470 +               au_set_dbtop(a->dentry, a->mvd_bdst);
26471 +               au_set_h_iptr(a->inode, a->mvd_bsrc, NULL, /*flags*/0);
26472 +               au_set_ibtop(a->inode, a->mvd_bdst);
26473 +       } else {
26474 +               /* hide the lower */
26475 +               au_set_h_dptr(a->dentry, a->mvd_bdst, NULL);
26476 +               au_set_dbbot(a->dentry, a->mvd_bsrc);
26477 +               au_set_h_iptr(a->inode, a->mvd_bdst, NULL, /*flags*/0);
26478 +               au_set_ibbot(a->inode, a->mvd_bsrc);
26479 +       }
26480 +       if (au_dbbot(a->dentry) < a->mvd_bdst)
26481 +               au_set_dbbot(a->dentry, a->mvd_bdst);
26482 +       if (au_ibbot(a->inode) < a->mvd_bdst)
26483 +               au_set_ibbot(a->inode, a->mvd_bdst);
26484 +
26485 +out_unlock:
26486 +       au_do_unlock(dmsg, a);
26487 +out:
26488 +       AuTraceErr(err);
26489 +       return err;
26490 +}
26491 +
26492 +/* ---------------------------------------------------------------------- */
26493 +
26494 +/* make sure the file is idle */
26495 +static int au_mvd_args_busy(const unsigned char dmsg, struct au_mvd_args *a)
26496 +{
26497 +       int err, plinked;
26498 +
26499 +       err = 0;
26500 +       plinked = !!au_opt_test(au_mntflags(a->sb), PLINK);
26501 +       if (au_dbtop(a->dentry) == a->mvd_bsrc
26502 +           && au_dcount(a->dentry) == 1
26503 +           && atomic_read(&a->inode->i_count) == 1
26504 +           /* && a->mvd_h_src_inode->i_nlink == 1 */
26505 +           && (!plinked || !au_plink_test(a->inode))
26506 +           && a->inode->i_nlink == 1)
26507 +               goto out;
26508 +
26509 +       err = -EBUSY;
26510 +       AU_MVD_PR(dmsg,
26511 +                 "b%d, d{b%d, c%d?}, i{c%d?, l%u}, hi{l%u}, p{%d, %d}\n",
26512 +                 a->mvd_bsrc, au_dbtop(a->dentry), au_dcount(a->dentry),
26513 +                 atomic_read(&a->inode->i_count), a->inode->i_nlink,
26514 +                 a->mvd_h_src_inode->i_nlink,
26515 +                 plinked, plinked ? au_plink_test(a->inode) : 0);
26516 +
26517 +out:
26518 +       AuTraceErr(err);
26519 +       return err;
26520 +}
26521 +
26522 +/* make sure the parent dir is fine */
26523 +static int au_mvd_args_parent(const unsigned char dmsg,
26524 +                             struct au_mvd_args *a)
26525 +{
26526 +       int err;
26527 +       aufs_bindex_t bindex;
26528 +
26529 +       err = 0;
26530 +       if (unlikely(au_alive_dir(a->parent))) {
26531 +               err = -ENOENT;
26532 +               AU_MVD_PR(dmsg, "parent dir is dead\n");
26533 +               goto out;
26534 +       }
26535 +
26536 +       a->bopq = au_dbdiropq(a->parent);
26537 +       bindex = au_wbr_nonopq(a->dentry, a->mvd_bdst);
26538 +       AuDbg("b%d\n", bindex);
26539 +       if (unlikely((bindex >= 0 && bindex < a->mvd_bdst)
26540 +                    || (a->bopq != -1 && a->bopq < a->mvd_bdst))) {
26541 +               err = -EINVAL;
26542 +               a->mvd_errno = EAU_MVDOWN_OPAQUE;
26543 +               AU_MVD_PR(dmsg, "ancestor is opaque b%d, b%d\n",
26544 +                         a->bopq, a->mvd_bdst);
26545 +       }
26546 +
26547 +out:
26548 +       AuTraceErr(err);
26549 +       return err;
26550 +}
26551 +
26552 +static int au_mvd_args_intermediate(const unsigned char dmsg,
26553 +                                   struct au_mvd_args *a)
26554 +{
26555 +       int err;
26556 +       struct au_dinfo *dinfo, *tmp;
26557 +
26558 +       /* lookup the next lower positive entry */
26559 +       err = -ENOMEM;
26560 +       tmp = au_di_alloc(a->sb, AuLsc_DI_TMP);
26561 +       if (unlikely(!tmp))
26562 +               goto out;
26563 +
26564 +       a->bfound = -1;
26565 +       a->bwh = -1;
26566 +       dinfo = au_di(a->dentry);
26567 +       au_di_cp(tmp, dinfo);
26568 +       au_di_swap(tmp, dinfo);
26569 +
26570 +       /* returns the number of positive dentries */
26571 +       err = au_lkup_dentry(a->dentry, a->mvd_bsrc + 1,
26572 +                            /* AuLkup_IGNORE_PERM */ 0);
26573 +       if (!err)
26574 +               a->bwh = au_dbwh(a->dentry);
26575 +       else if (err > 0)
26576 +               a->bfound = au_dbtop(a->dentry);
26577 +
26578 +       au_di_swap(tmp, dinfo);
26579 +       au_rw_write_unlock(&tmp->di_rwsem);
26580 +       au_di_free(tmp);
26581 +       if (unlikely(err < 0))
26582 +               AU_MVD_PR(dmsg, "failed look-up lower\n");
26583 +
26584 +       /*
26585 +        * here, we have these cases.
26586 +        * bfound == -1
26587 +        *      no positive dentry under bsrc. there are more sub-cases.
26588 +        *      bwh < 0
26589 +        *              there no whiteout, we can safely move-down.
26590 +        *      bwh <= bsrc
26591 +        *              impossible
26592 +        *      bsrc < bwh && bwh < bdst
26593 +        *              there is a whiteout on RO branch. cannot proceed.
26594 +        *      bwh == bdst
26595 +        *              there is a whiteout on the RW target branch. it should
26596 +        *              be removed.
26597 +        *      bdst < bwh
26598 +        *              there is a whiteout somewhere unrelated branch.
26599 +        * -1 < bfound && bfound <= bsrc
26600 +        *      impossible.
26601 +        * bfound < bdst
26602 +        *      found, but it is on RO branch between bsrc and bdst. cannot
26603 +        *      proceed.
26604 +        * bfound == bdst
26605 +        *      found, replace it if AUFS_MVDOWN_FORCE is set. otherwise return
26606 +        *      error.
26607 +        * bdst < bfound
26608 +        *      found, after we create the file on bdst, it will be hidden.
26609 +        */
26610 +
26611 +       AuDebugOn(a->bfound == -1
26612 +                 && a->bwh != -1
26613 +                 && a->bwh <= a->mvd_bsrc);
26614 +       AuDebugOn(-1 < a->bfound
26615 +                 && a->bfound <= a->mvd_bsrc);
26616 +
26617 +       err = -EINVAL;
26618 +       if (a->bfound == -1
26619 +           && a->mvd_bsrc < a->bwh
26620 +           && a->bwh != -1
26621 +           && a->bwh < a->mvd_bdst) {
26622 +               a->mvd_errno = EAU_MVDOWN_WHITEOUT;
26623 +               AU_MVD_PR(dmsg, "bsrc %d, bdst %d, bfound %d, bwh %d\n",
26624 +                         a->mvd_bsrc, a->mvd_bdst, a->bfound, a->bwh);
26625 +               goto out;
26626 +       } else if (a->bfound != -1 && a->bfound < a->mvd_bdst) {
26627 +               a->mvd_errno = EAU_MVDOWN_UPPER;
26628 +               AU_MVD_PR(dmsg, "bdst %d, bfound %d\n",
26629 +                         a->mvd_bdst, a->bfound);
26630 +               goto out;
26631 +       }
26632 +
26633 +       err = 0; /* success */
26634 +
26635 +out:
26636 +       AuTraceErr(err);
26637 +       return err;
26638 +}
26639 +
26640 +static int au_mvd_args_exist(const unsigned char dmsg, struct au_mvd_args *a)
26641 +{
26642 +       int err;
26643 +
26644 +       err = 0;
26645 +       if (!(a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
26646 +           && a->bfound == a->mvd_bdst)
26647 +               err = -EEXIST;
26648 +       AuTraceErr(err);
26649 +       return err;
26650 +}
26651 +
26652 +static int au_mvd_args(const unsigned char dmsg, struct au_mvd_args *a)
26653 +{
26654 +       int err;
26655 +       struct au_branch *br;
26656 +
26657 +       err = -EISDIR;
26658 +       if (unlikely(S_ISDIR(a->inode->i_mode)))
26659 +               goto out;
26660 +
26661 +       err = -EINVAL;
26662 +       if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_UPPER))
26663 +               a->mvd_bsrc = au_ibtop(a->inode);
26664 +       else {
26665 +               a->mvd_bsrc = au_br_index(a->sb, a->mvd_src_brid);
26666 +               if (unlikely(a->mvd_bsrc < 0
26667 +                            || (a->mvd_bsrc < au_dbtop(a->dentry)
26668 +                                || au_dbbot(a->dentry) < a->mvd_bsrc
26669 +                                || !au_h_dptr(a->dentry, a->mvd_bsrc))
26670 +                            || (a->mvd_bsrc < au_ibtop(a->inode)
26671 +                                || au_ibbot(a->inode) < a->mvd_bsrc
26672 +                                || !au_h_iptr(a->inode, a->mvd_bsrc)))) {
26673 +                       a->mvd_errno = EAU_MVDOWN_NOUPPER;
26674 +                       AU_MVD_PR(dmsg, "no upper\n");
26675 +                       goto out;
26676 +               }
26677 +       }
26678 +       if (unlikely(a->mvd_bsrc == au_sbbot(a->sb))) {
26679 +               a->mvd_errno = EAU_MVDOWN_BOTTOM;
26680 +               AU_MVD_PR(dmsg, "on the bottom\n");
26681 +               goto out;
26682 +       }
26683 +       a->mvd_h_src_inode = au_h_iptr(a->inode, a->mvd_bsrc);
26684 +       br = au_sbr(a->sb, a->mvd_bsrc);
26685 +       err = au_br_rdonly(br);
26686 +       if (!(a->mvdown.flags & AUFS_MVDOWN_ROUPPER)) {
26687 +               if (unlikely(err))
26688 +                       goto out;
26689 +       } else if (!(vfsub_native_ro(a->mvd_h_src_inode)
26690 +                    || IS_APPEND(a->mvd_h_src_inode))) {
26691 +               if (err)
26692 +                       a->mvdown.flags |= AUFS_MVDOWN_ROUPPER_R;
26693 +               /* go on */
26694 +       } else
26695 +               goto out;
26696 +
26697 +       err = -EINVAL;
26698 +       if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_LOWER)) {
26699 +               a->mvd_bdst = find_lower_writable(a);
26700 +               if (unlikely(a->mvd_bdst < 0)) {
26701 +                       a->mvd_errno = EAU_MVDOWN_BOTTOM;
26702 +                       AU_MVD_PR(dmsg, "no writable lower branch\n");
26703 +                       goto out;
26704 +               }
26705 +       } else {
26706 +               a->mvd_bdst = au_br_index(a->sb, a->mvd_dst_brid);
26707 +               if (unlikely(a->mvd_bdst < 0
26708 +                            || au_sbbot(a->sb) < a->mvd_bdst)) {
26709 +                       a->mvd_errno = EAU_MVDOWN_NOLOWERBR;
26710 +                       AU_MVD_PR(dmsg, "no lower brid\n");
26711 +                       goto out;
26712 +               }
26713 +       }
26714 +
26715 +       err = au_mvd_args_busy(dmsg, a);
26716 +       if (!err)
26717 +               err = au_mvd_args_parent(dmsg, a);
26718 +       if (!err)
26719 +               err = au_mvd_args_intermediate(dmsg, a);
26720 +       if (!err)
26721 +               err = au_mvd_args_exist(dmsg, a);
26722 +       if (!err)
26723 +               AuDbg("b%d, b%d\n", a->mvd_bsrc, a->mvd_bdst);
26724 +
26725 +out:
26726 +       AuTraceErr(err);
26727 +       return err;
26728 +}
26729 +
26730 +int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *uarg)
26731 +{
26732 +       int err, e;
26733 +       unsigned char dmsg;
26734 +       struct au_mvd_args *args;
26735 +       struct inode *inode;
26736 +
26737 +       inode = d_inode(dentry);
26738 +       err = -EPERM;
26739 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
26740 +               goto out;
26741 +
26742 +       err = -ENOMEM;
26743 +       args = kmalloc(sizeof(*args), GFP_NOFS);
26744 +       if (unlikely(!args))
26745 +               goto out;
26746 +
26747 +       err = copy_from_user(&args->mvdown, uarg, sizeof(args->mvdown));
26748 +       if (!err)
26749 +               /* VERIFY_WRITE */
26750 +               err = !access_ok(uarg, sizeof(*uarg));
26751 +       if (unlikely(err)) {
26752 +               err = -EFAULT;
26753 +               AuTraceErr(err);
26754 +               goto out_free;
26755 +       }
26756 +       AuDbg("flags 0x%x\n", args->mvdown.flags);
26757 +       args->mvdown.flags &= ~(AUFS_MVDOWN_ROLOWER_R | AUFS_MVDOWN_ROUPPER_R);
26758 +       args->mvdown.au_errno = 0;
26759 +       args->dentry = dentry;
26760 +       args->inode = inode;
26761 +       args->sb = dentry->d_sb;
26762 +
26763 +       err = -ENOENT;
26764 +       dmsg = !!(args->mvdown.flags & AUFS_MVDOWN_DMSG);
26765 +       args->parent = dget_parent(dentry);
26766 +       args->dir = d_inode(args->parent);
26767 +       inode_lock_nested(args->dir, I_MUTEX_PARENT);
26768 +       dput(args->parent);
26769 +       if (unlikely(args->parent != dentry->d_parent)) {
26770 +               AU_MVD_PR(dmsg, "parent dir is moved\n");
26771 +               goto out_dir;
26772 +       }
26773 +
26774 +       inode_lock_nested(inode, I_MUTEX_CHILD);
26775 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_NOPLMW);
26776 +       if (unlikely(err))
26777 +               goto out_inode;
26778 +
26779 +       di_write_lock_parent(args->parent);
26780 +       err = au_mvd_args(dmsg, args);
26781 +       if (unlikely(err))
26782 +               goto out_parent;
26783 +
26784 +       err = au_do_mvdown(dmsg, args);
26785 +       if (unlikely(err))
26786 +               goto out_parent;
26787 +
26788 +       au_cpup_attr_timesizes(args->dir);
26789 +       au_cpup_attr_timesizes(inode);
26790 +       if (!(args->mvdown.flags & AUFS_MVDOWN_KUPPER))
26791 +               au_cpup_igen(inode, au_h_iptr(inode, args->mvd_bdst));
26792 +       /* au_digen_dec(dentry); */
26793 +
26794 +out_parent:
26795 +       di_write_unlock(args->parent);
26796 +       aufs_read_unlock(dentry, AuLock_DW);
26797 +out_inode:
26798 +       inode_unlock(inode);
26799 +out_dir:
26800 +       inode_unlock(args->dir);
26801 +out_free:
26802 +       e = copy_to_user(uarg, &args->mvdown, sizeof(args->mvdown));
26803 +       if (unlikely(e))
26804 +               err = -EFAULT;
26805 +       au_kfree_rcu(args);
26806 +out:
26807 +       AuTraceErr(err);
26808 +       return err;
26809 +}
26810 diff -urN /usr/share/empty/fs/aufs/opts.c linux/fs/aufs/opts.c
26811 --- /usr/share/empty/fs/aufs/opts.c     1970-01-01 01:00:00.000000000 +0100
26812 +++ linux/fs/aufs/opts.c        2022-03-21 14:49:05.729966343 +0100
26813 @@ -0,0 +1,1032 @@
26814 +// SPDX-License-Identifier: GPL-2.0
26815 +/*
26816 + * Copyright (C) 2005-2021 Junjiro R. Okajima
26817 + *
26818 + * This program, aufs is free software; you can redistribute it and/or modify
26819 + * it under the terms of the GNU General Public License as published by
26820 + * the Free Software Foundation; either version 2 of the License, or
26821 + * (at your option) any later version.
26822 + *
26823 + * This program is distributed in the hope that it will be useful,
26824 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
26825 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26826 + * GNU General Public License for more details.
26827 + *
26828 + * You should have received a copy of the GNU General Public License
26829 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
26830 + */
26831 +
26832 +/*
26833 + * mount options/flags
26834 + */
26835 +
26836 +#include <linux/types.h> /* a distribution requires */
26837 +#include <linux/parser.h>
26838 +#include "aufs.h"
26839 +
26840 +/* ---------------------------------------------------------------------- */
26841 +
26842 +static const char *au_parser_pattern(int val, match_table_t tbl)
26843 +{
26844 +       struct match_token *p;
26845 +
26846 +       p = tbl;
26847 +       while (p->pattern) {
26848 +               if (p->token == val)
26849 +                       return p->pattern;
26850 +               p++;
26851 +       }
26852 +       BUG();
26853 +       return "??";
26854 +}
26855 +
26856 +static const char *au_optstr(int *val, match_table_t tbl)
26857 +{
26858 +       struct match_token *p;
26859 +       int v;
26860 +
26861 +       v = *val;
26862 +       if (!v)
26863 +               goto out;
26864 +       p = tbl;
26865 +       while (p->pattern) {
26866 +               if (p->token
26867 +                   && (v & p->token) == p->token) {
26868 +                       *val &= ~p->token;
26869 +                       return p->pattern;
26870 +               }
26871 +               p++;
26872 +       }
26873 +
26874 +out:
26875 +       return NULL;
26876 +}
26877 +
26878 +/* ---------------------------------------------------------------------- */
26879 +
26880 +static match_table_t brperm = {
26881 +       {AuBrPerm_RO, AUFS_BRPERM_RO},
26882 +       {AuBrPerm_RR, AUFS_BRPERM_RR},
26883 +       {AuBrPerm_RW, AUFS_BRPERM_RW},
26884 +       {0, NULL}
26885 +};
26886 +
26887 +static match_table_t brattr = {
26888 +       /* general */
26889 +       {AuBrAttr_COO_REG, AUFS_BRATTR_COO_REG},
26890 +       {AuBrAttr_COO_ALL, AUFS_BRATTR_COO_ALL},
26891 +       /* 'unpin' attrib is meaningless since linux-3.18-rc1 */
26892 +       {AuBrAttr_UNPIN, AUFS_BRATTR_UNPIN},
26893 +#ifdef CONFIG_AUFS_FHSM
26894 +       {AuBrAttr_FHSM, AUFS_BRATTR_FHSM},
26895 +#endif
26896 +#ifdef CONFIG_AUFS_XATTR
26897 +       {AuBrAttr_ICEX, AUFS_BRATTR_ICEX},
26898 +       {AuBrAttr_ICEX_SEC, AUFS_BRATTR_ICEX_SEC},
26899 +       {AuBrAttr_ICEX_SYS, AUFS_BRATTR_ICEX_SYS},
26900 +       {AuBrAttr_ICEX_TR, AUFS_BRATTR_ICEX_TR},
26901 +       {AuBrAttr_ICEX_USR, AUFS_BRATTR_ICEX_USR},
26902 +       {AuBrAttr_ICEX_OTH, AUFS_BRATTR_ICEX_OTH},
26903 +#endif
26904 +
26905 +       /* ro/rr branch */
26906 +       {AuBrRAttr_WH, AUFS_BRRATTR_WH},
26907 +
26908 +       /* rw branch */
26909 +       {AuBrWAttr_MOO, AUFS_BRWATTR_MOO},
26910 +       {AuBrWAttr_NoLinkWH, AUFS_BRWATTR_NLWH},
26911 +
26912 +       {0, NULL}
26913 +};
26914 +
26915 +static int br_attr_val(char *str, match_table_t table, substring_t args[])
26916 +{
26917 +       int attr, v;
26918 +       char *p;
26919 +
26920 +       attr = 0;
26921 +       do {
26922 +               p = strchr(str, '+');
26923 +               if (p)
26924 +                       *p = 0;
26925 +               v = match_token(str, table, args);
26926 +               if (v) {
26927 +                       if (v & AuBrAttr_CMOO_Mask)
26928 +                               attr &= ~AuBrAttr_CMOO_Mask;
26929 +                       attr |= v;
26930 +               } else {
26931 +                       if (p)
26932 +                               *p = '+';
26933 +                       pr_warn("ignored branch attribute %s\n", str);
26934 +                       break;
26935 +               }
26936 +               if (p)
26937 +                       str = p + 1;
26938 +       } while (p);
26939 +
26940 +       return attr;
26941 +}
26942 +
26943 +static int au_do_optstr_br_attr(au_br_perm_str_t *str, int perm)
26944 +{
26945 +       int sz;
26946 +       const char *p;
26947 +       char *q;
26948 +
26949 +       q = str->a;
26950 +       *q = 0;
26951 +       p = au_optstr(&perm, brattr);
26952 +       if (p) {
26953 +               sz = strlen(p);
26954 +               memcpy(q, p, sz + 1);
26955 +               q += sz;
26956 +       } else
26957 +               goto out;
26958 +
26959 +       do {
26960 +               p = au_optstr(&perm, brattr);
26961 +               if (p) {
26962 +                       *q++ = '+';
26963 +                       sz = strlen(p);
26964 +                       memcpy(q, p, sz + 1);
26965 +                       q += sz;
26966 +               }
26967 +       } while (p);
26968 +
26969 +out:
26970 +       return q - str->a;
26971 +}
26972 +
26973 +int au_br_perm_val(char *perm)
26974 +{
26975 +       int val, bad, sz;
26976 +       char *p;
26977 +       substring_t args[MAX_OPT_ARGS];
26978 +       au_br_perm_str_t attr;
26979 +
26980 +       p = strchr(perm, '+');
26981 +       if (p)
26982 +               *p = 0;
26983 +       val = match_token(perm, brperm, args);
26984 +       if (!val) {
26985 +               if (p)
26986 +                       *p = '+';
26987 +               pr_warn("ignored branch permission %s\n", perm);
26988 +               val = AuBrPerm_RO;
26989 +               goto out;
26990 +       }
26991 +       if (!p)
26992 +               goto out;
26993 +
26994 +       val |= br_attr_val(p + 1, brattr, args);
26995 +
26996 +       bad = 0;
26997 +       switch (val & AuBrPerm_Mask) {
26998 +       case AuBrPerm_RO:
26999 +       case AuBrPerm_RR:
27000 +               bad = val & AuBrWAttr_Mask;
27001 +               val &= ~AuBrWAttr_Mask;
27002 +               break;
27003 +       case AuBrPerm_RW:
27004 +               bad = val & AuBrRAttr_Mask;
27005 +               val &= ~AuBrRAttr_Mask;
27006 +               break;
27007 +       }
27008 +
27009 +       /*
27010 +        * 'unpin' attrib becomes meaningless since linux-3.18-rc1, but aufs
27011 +        * does not treat it as an error, just warning.
27012 +        * this is a tiny guard for the user operation.
27013 +        */
27014 +       if (val & AuBrAttr_UNPIN) {
27015 +               bad |= AuBrAttr_UNPIN;
27016 +               val &= ~AuBrAttr_UNPIN;
27017 +       }
27018 +
27019 +       if (unlikely(bad)) {
27020 +               sz = au_do_optstr_br_attr(&attr, bad);
27021 +               AuDebugOn(!sz);
27022 +               pr_warn("ignored branch attribute %s\n", attr.a);
27023 +       }
27024 +
27025 +out:
27026 +       return val;
27027 +}
27028 +
27029 +void au_optstr_br_perm(au_br_perm_str_t *str, int perm)
27030 +{
27031 +       au_br_perm_str_t attr;
27032 +       const char *p;
27033 +       char *q;
27034 +       int sz;
27035 +
27036 +       q = str->a;
27037 +       p = au_optstr(&perm, brperm);
27038 +       AuDebugOn(!p || !*p);
27039 +       sz = strlen(p);
27040 +       memcpy(q, p, sz + 1);
27041 +       q += sz;
27042 +
27043 +       sz = au_do_optstr_br_attr(&attr, perm);
27044 +       if (sz) {
27045 +               *q++ = '+';
27046 +               memcpy(q, attr.a, sz + 1);
27047 +       }
27048 +
27049 +       AuDebugOn(strlen(str->a) >= sizeof(str->a));
27050 +}
27051 +
27052 +/* ---------------------------------------------------------------------- */
27053 +
27054 +static match_table_t udbalevel = {
27055 +       {AuOpt_UDBA_REVAL, "reval"},
27056 +       {AuOpt_UDBA_NONE, "none"},
27057 +#ifdef CONFIG_AUFS_HNOTIFY
27058 +       {AuOpt_UDBA_HNOTIFY, "notify"}, /* abstraction */
27059 +#ifdef CONFIG_AUFS_HFSNOTIFY
27060 +       {AuOpt_UDBA_HNOTIFY, "fsnotify"},
27061 +#endif
27062 +#endif
27063 +       {-1, NULL}
27064 +};
27065 +
27066 +int au_udba_val(char *str)
27067 +{
27068 +       substring_t args[MAX_OPT_ARGS];
27069 +
27070 +       return match_token(str, udbalevel, args);
27071 +}
27072 +
27073 +const char *au_optstr_udba(int udba)
27074 +{
27075 +       return au_parser_pattern(udba, udbalevel);
27076 +}
27077 +
27078 +/* ---------------------------------------------------------------------- */
27079 +
27080 +static match_table_t au_wbr_create_policy = {
27081 +       {AuWbrCreate_TDP, "tdp"},
27082 +       {AuWbrCreate_TDP, "top-down-parent"},
27083 +       {AuWbrCreate_RR, "rr"},
27084 +       {AuWbrCreate_RR, "round-robin"},
27085 +       {AuWbrCreate_MFS, "mfs"},
27086 +       {AuWbrCreate_MFS, "most-free-space"},
27087 +       {AuWbrCreate_MFSV, "mfs:%d"},
27088 +       {AuWbrCreate_MFSV, "most-free-space:%d"},
27089 +
27090 +       /* top-down regardless the parent, and then mfs */
27091 +       {AuWbrCreate_TDMFS, "tdmfs:%d"},
27092 +       {AuWbrCreate_TDMFSV, "tdmfs:%d:%d"},
27093 +
27094 +       {AuWbrCreate_MFSRR, "mfsrr:%d"},
27095 +       {AuWbrCreate_MFSRRV, "mfsrr:%d:%d"},
27096 +       {AuWbrCreate_PMFS, "pmfs"},
27097 +       {AuWbrCreate_PMFSV, "pmfs:%d"},
27098 +       {AuWbrCreate_PMFSRR, "pmfsrr:%d"},
27099 +       {AuWbrCreate_PMFSRRV, "pmfsrr:%d:%d"},
27100 +
27101 +       {-1, NULL}
27102 +};
27103 +
27104 +static int au_wbr_mfs_wmark(substring_t *arg, char *str,
27105 +                           struct au_opt_wbr_create *create)
27106 +{
27107 +       int err;
27108 +       unsigned long long ull;
27109 +
27110 +       err = 0;
27111 +       if (!match_u64(arg, &ull))
27112 +               create->mfsrr_watermark = ull;
27113 +       else {
27114 +               pr_err("bad integer in %s\n", str);
27115 +               err = -EINVAL;
27116 +       }
27117 +
27118 +       return err;
27119 +}
27120 +
27121 +static int au_wbr_mfs_sec(substring_t *arg, char *str,
27122 +                         struct au_opt_wbr_create *create)
27123 +{
27124 +       int n, err;
27125 +
27126 +       err = 0;
27127 +       if (!match_int(arg, &n) && 0 <= n && n <= AUFS_MFS_MAX_SEC)
27128 +               create->mfs_second = n;
27129 +       else {
27130 +               pr_err("bad integer in %s\n", str);
27131 +               err = -EINVAL;
27132 +       }
27133 +
27134 +       return err;
27135 +}
27136 +
27137 +int au_wbr_create_val(char *str, struct au_opt_wbr_create *create)
27138 +{
27139 +       int err, e;
27140 +       substring_t args[MAX_OPT_ARGS];
27141 +
27142 +       err = match_token(str, au_wbr_create_policy, args);
27143 +       create->wbr_create = err;
27144 +       switch (err) {
27145 +       case AuWbrCreate_MFSRRV:
27146 +       case AuWbrCreate_TDMFSV:
27147 +       case AuWbrCreate_PMFSRRV:
27148 +               e = au_wbr_mfs_wmark(&args[0], str, create);
27149 +               if (!e)
27150 +                       e = au_wbr_mfs_sec(&args[1], str, create);
27151 +               if (unlikely(e))
27152 +                       err = e;
27153 +               break;
27154 +       case AuWbrCreate_MFSRR:
27155 +       case AuWbrCreate_TDMFS:
27156 +       case AuWbrCreate_PMFSRR:
27157 +               e = au_wbr_mfs_wmark(&args[0], str, create);
27158 +               if (unlikely(e)) {
27159 +                       err = e;
27160 +                       break;
27161 +               }
27162 +               fallthrough;
27163 +       case AuWbrCreate_MFS:
27164 +       case AuWbrCreate_PMFS:
27165 +               create->mfs_second = AUFS_MFS_DEF_SEC;
27166 +               break;
27167 +       case AuWbrCreate_MFSV:
27168 +       case AuWbrCreate_PMFSV:
27169 +               e = au_wbr_mfs_sec(&args[0], str, create);
27170 +               if (unlikely(e))
27171 +                       err = e;
27172 +               break;
27173 +       }
27174 +
27175 +       return err;
27176 +}
27177 +
27178 +const char *au_optstr_wbr_create(int wbr_create)
27179 +{
27180 +       return au_parser_pattern(wbr_create, au_wbr_create_policy);
27181 +}
27182 +
27183 +static match_table_t au_wbr_copyup_policy = {
27184 +       {AuWbrCopyup_TDP, "tdp"},
27185 +       {AuWbrCopyup_TDP, "top-down-parent"},
27186 +       {AuWbrCopyup_BUP, "bup"},
27187 +       {AuWbrCopyup_BUP, "bottom-up-parent"},
27188 +       {AuWbrCopyup_BU, "bu"},
27189 +       {AuWbrCopyup_BU, "bottom-up"},
27190 +       {-1, NULL}
27191 +};
27192 +
27193 +int au_wbr_copyup_val(char *str)
27194 +{
27195 +       substring_t args[MAX_OPT_ARGS];
27196 +
27197 +       return match_token(str, au_wbr_copyup_policy, args);
27198 +}
27199 +
27200 +const char *au_optstr_wbr_copyup(int wbr_copyup)
27201 +{
27202 +       return au_parser_pattern(wbr_copyup, au_wbr_copyup_policy);
27203 +}
27204 +
27205 +/* ---------------------------------------------------------------------- */
27206 +
27207 +int au_opt_add(struct au_opt *opt, char *opt_str, unsigned long sb_flags,
27208 +              aufs_bindex_t bindex)
27209 +{
27210 +       int err;
27211 +       struct au_opt_add *add = &opt->add;
27212 +       char *p;
27213 +
27214 +       add->bindex = bindex;
27215 +       add->perm = AuBrPerm_RO;
27216 +       add->pathname = opt_str;
27217 +       p = strchr(opt_str, '=');
27218 +       if (p) {
27219 +               *p++ = 0;
27220 +               if (*p)
27221 +                       add->perm = au_br_perm_val(p);
27222 +       }
27223 +
27224 +       err = vfsub_kern_path(add->pathname, AuOpt_LkupDirFlags, &add->path);
27225 +       if (!err) {
27226 +               if (!p) {
27227 +                       add->perm = AuBrPerm_RO;
27228 +                       if (au_test_fs_rr(add->path.dentry->d_sb))
27229 +                               add->perm = AuBrPerm_RR;
27230 +                       else if (!bindex && !(sb_flags & SB_RDONLY))
27231 +                               add->perm = AuBrPerm_RW;
27232 +               }
27233 +               opt->type = Opt_add;
27234 +               goto out;
27235 +       }
27236 +       pr_err("lookup failed %s (%d)\n", add->pathname, err);
27237 +       err = -EINVAL;
27238 +
27239 +out:
27240 +       return err;
27241 +}
27242 +
27243 +static int au_opt_wbr_create(struct super_block *sb,
27244 +                            struct au_opt_wbr_create *create)
27245 +{
27246 +       int err;
27247 +       struct au_sbinfo *sbinfo;
27248 +
27249 +       SiMustWriteLock(sb);
27250 +
27251 +       err = 1; /* handled */
27252 +       sbinfo = au_sbi(sb);
27253 +       if (sbinfo->si_wbr_create_ops->fin) {
27254 +               err = sbinfo->si_wbr_create_ops->fin(sb);
27255 +               if (!err)
27256 +                       err = 1;
27257 +       }
27258 +
27259 +       sbinfo->si_wbr_create = create->wbr_create;
27260 +       sbinfo->si_wbr_create_ops = au_wbr_create_ops + create->wbr_create;
27261 +       switch (create->wbr_create) {
27262 +       case AuWbrCreate_MFSRRV:
27263 +       case AuWbrCreate_MFSRR:
27264 +       case AuWbrCreate_TDMFS:
27265 +       case AuWbrCreate_TDMFSV:
27266 +       case AuWbrCreate_PMFSRR:
27267 +       case AuWbrCreate_PMFSRRV:
27268 +               sbinfo->si_wbr_mfs.mfsrr_watermark = create->mfsrr_watermark;
27269 +               fallthrough;
27270 +       case AuWbrCreate_MFS:
27271 +       case AuWbrCreate_MFSV:
27272 +       case AuWbrCreate_PMFS:
27273 +       case AuWbrCreate_PMFSV:
27274 +               sbinfo->si_wbr_mfs.mfs_expire
27275 +                       = msecs_to_jiffies(create->mfs_second * MSEC_PER_SEC);
27276 +               break;
27277 +       }
27278 +
27279 +       if (sbinfo->si_wbr_create_ops->init)
27280 +               sbinfo->si_wbr_create_ops->init(sb); /* ignore */
27281 +
27282 +       return err;
27283 +}
27284 +
27285 +/*
27286 + * returns,
27287 + * plus: processed without an error
27288 + * zero: unprocessed
27289 + */
27290 +static int au_opt_simple(struct super_block *sb, struct au_opt *opt,
27291 +                        struct au_opts *opts)
27292 +{
27293 +       int err;
27294 +       struct au_sbinfo *sbinfo;
27295 +
27296 +       SiMustWriteLock(sb);
27297 +
27298 +       err = 1; /* handled */
27299 +       sbinfo = au_sbi(sb);
27300 +       switch (opt->type) {
27301 +       case Opt_udba:
27302 +               sbinfo->si_mntflags &= ~AuOptMask_UDBA;
27303 +               sbinfo->si_mntflags |= opt->udba;
27304 +               opts->given_udba |= opt->udba;
27305 +               break;
27306 +
27307 +       case Opt_plink:
27308 +               if (opt->tf)
27309 +                       au_opt_set(sbinfo->si_mntflags, PLINK);
27310 +               else {
27311 +                       if (au_opt_test(sbinfo->si_mntflags, PLINK))
27312 +                               au_plink_put(sb, /*verbose*/1);
27313 +                       au_opt_clr(sbinfo->si_mntflags, PLINK);
27314 +               }
27315 +               break;
27316 +       case Opt_list_plink:
27317 +               if (au_opt_test(sbinfo->si_mntflags, PLINK))
27318 +                       au_plink_list(sb);
27319 +               break;
27320 +
27321 +       case Opt_dio:
27322 +               if (opt->tf) {
27323 +                       au_opt_set(sbinfo->si_mntflags, DIO);
27324 +                       au_fset_opts(opts->flags, REFRESH_DYAOP);
27325 +               } else {
27326 +                       au_opt_clr(sbinfo->si_mntflags, DIO);
27327 +                       au_fset_opts(opts->flags, REFRESH_DYAOP);
27328 +               }
27329 +               break;
27330 +
27331 +       case Opt_fhsm_sec:
27332 +               au_fhsm_set(sbinfo, opt->fhsm_second);
27333 +               break;
27334 +
27335 +       case Opt_diropq_a:
27336 +               au_opt_set(sbinfo->si_mntflags, ALWAYS_DIROPQ);
27337 +               break;
27338 +       case Opt_diropq_w:
27339 +               au_opt_clr(sbinfo->si_mntflags, ALWAYS_DIROPQ);
27340 +               break;
27341 +
27342 +       case Opt_warn_perm:
27343 +               if (opt->tf)
27344 +                       au_opt_set(sbinfo->si_mntflags, WARN_PERM);
27345 +               else
27346 +                       au_opt_clr(sbinfo->si_mntflags, WARN_PERM);
27347 +               break;
27348 +
27349 +       case Opt_verbose:
27350 +               if (opt->tf)
27351 +                       au_opt_set(sbinfo->si_mntflags, VERBOSE);
27352 +               else
27353 +                       au_opt_clr(sbinfo->si_mntflags, VERBOSE);
27354 +               break;
27355 +
27356 +       case Opt_sum:
27357 +               if (opt->tf)
27358 +                       au_opt_set(sbinfo->si_mntflags, SUM);
27359 +               else {
27360 +                       au_opt_clr(sbinfo->si_mntflags, SUM);
27361 +                       au_opt_clr(sbinfo->si_mntflags, SUM_W);
27362 +               }
27363 +               break;
27364 +       case Opt_wsum:
27365 +               au_opt_clr(sbinfo->si_mntflags, SUM);
27366 +               au_opt_set(sbinfo->si_mntflags, SUM_W);
27367 +               break;
27368 +
27369 +       case Opt_wbr_create:
27370 +               err = au_opt_wbr_create(sb, &opt->wbr_create);
27371 +               break;
27372 +       case Opt_wbr_copyup:
27373 +               sbinfo->si_wbr_copyup = opt->wbr_copyup;
27374 +               sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + opt->wbr_copyup;
27375 +               break;
27376 +
27377 +       case Opt_dirwh:
27378 +               sbinfo->si_dirwh = opt->dirwh;
27379 +               break;
27380 +
27381 +       case Opt_rdcache:
27382 +               sbinfo->si_rdcache
27383 +                       = msecs_to_jiffies(opt->rdcache * MSEC_PER_SEC);
27384 +               break;
27385 +       case Opt_rdblk:
27386 +               sbinfo->si_rdblk = opt->rdblk;
27387 +               break;
27388 +       case Opt_rdhash:
27389 +               sbinfo->si_rdhash = opt->rdhash;
27390 +               break;
27391 +
27392 +       case Opt_shwh:
27393 +               if (opt->tf)
27394 +                       au_opt_set(sbinfo->si_mntflags, SHWH);
27395 +               else
27396 +                       au_opt_clr(sbinfo->si_mntflags, SHWH);
27397 +               break;
27398 +
27399 +       case Opt_dirperm1:
27400 +               if (opt->tf)
27401 +                       au_opt_set(sbinfo->si_mntflags, DIRPERM1);
27402 +               else
27403 +                       au_opt_clr(sbinfo->si_mntflags, DIRPERM1);
27404 +               break;
27405 +
27406 +       case Opt_trunc_xino:
27407 +               if (opt->tf)
27408 +                       au_opt_set(sbinfo->si_mntflags, TRUNC_XINO);
27409 +               else
27410 +                       au_opt_clr(sbinfo->si_mntflags, TRUNC_XINO);
27411 +               break;
27412 +
27413 +       case Opt_trunc_xino_path:
27414 +       case Opt_itrunc_xino:
27415 +               err = au_xino_trunc(sb, opt->xino_itrunc.bindex,
27416 +                                   /*idx_begin*/0);
27417 +               if (!err)
27418 +                       err = 1;
27419 +               break;
27420 +
27421 +       case Opt_trunc_xib:
27422 +               if (opt->tf)
27423 +                       au_fset_opts(opts->flags, TRUNC_XIB);
27424 +               else
27425 +                       au_fclr_opts(opts->flags, TRUNC_XIB);
27426 +               break;
27427 +
27428 +       case Opt_dirren:
27429 +               err = 1;
27430 +               if (opt->tf) {
27431 +                       if (!au_opt_test(sbinfo->si_mntflags, DIRREN)) {
27432 +                               err = au_dr_opt_set(sb);
27433 +                               if (!err)
27434 +                                       err = 1;
27435 +                       }
27436 +                       if (err == 1)
27437 +                               au_opt_set(sbinfo->si_mntflags, DIRREN);
27438 +               } else {
27439 +                       if (au_opt_test(sbinfo->si_mntflags, DIRREN)) {
27440 +                               err = au_dr_opt_clr(sb, au_ftest_opts(opts->flags,
27441 +                                                                     DR_FLUSHED));
27442 +                               if (!err)
27443 +                                       err = 1;
27444 +                       }
27445 +                       if (err == 1)
27446 +                               au_opt_clr(sbinfo->si_mntflags, DIRREN);
27447 +               }
27448 +               break;
27449 +
27450 +       case Opt_acl:
27451 +               if (opt->tf)
27452 +                       sb->s_flags |= SB_POSIXACL;
27453 +               else
27454 +                       sb->s_flags &= ~SB_POSIXACL;
27455 +               break;
27456 +
27457 +       default:
27458 +               err = 0;
27459 +               break;
27460 +       }
27461 +
27462 +       return err;
27463 +}
27464 +
27465 +/*
27466 + * returns tri-state.
27467 + * plus: processed without an error
27468 + * zero: unprocessed
27469 + * minus: error
27470 + */
27471 +static int au_opt_br(struct super_block *sb, struct au_opt *opt,
27472 +                    struct au_opts *opts)
27473 +{
27474 +       int err, do_refresh;
27475 +
27476 +       err = 0;
27477 +       switch (opt->type) {
27478 +       case Opt_append:
27479 +               opt->add.bindex = au_sbbot(sb) + 1;
27480 +               if (opt->add.bindex < 0)
27481 +                       opt->add.bindex = 0;
27482 +               goto add;
27483 +               /* Always goto add, not fallthrough */
27484 +       case Opt_prepend:
27485 +               opt->add.bindex = 0;
27486 +               fallthrough;
27487 +       add: /* indented label */
27488 +       case Opt_add:
27489 +               err = au_br_add(sb, &opt->add,
27490 +                               au_ftest_opts(opts->flags, REMOUNT));
27491 +               if (!err) {
27492 +                       err = 1;
27493 +                       au_fset_opts(opts->flags, REFRESH);
27494 +               }
27495 +               break;
27496 +
27497 +       case Opt_del:
27498 +       case Opt_idel:
27499 +               err = au_br_del(sb, &opt->del,
27500 +                               au_ftest_opts(opts->flags, REMOUNT));
27501 +               if (!err) {
27502 +                       err = 1;
27503 +                       au_fset_opts(opts->flags, TRUNC_XIB);
27504 +                       au_fset_opts(opts->flags, REFRESH);
27505 +               }
27506 +               break;
27507 +
27508 +       case Opt_mod:
27509 +       case Opt_imod:
27510 +               err = au_br_mod(sb, &opt->mod,
27511 +                               au_ftest_opts(opts->flags, REMOUNT),
27512 +                               &do_refresh);
27513 +               if (!err) {
27514 +                       err = 1;
27515 +                       if (do_refresh)
27516 +                               au_fset_opts(opts->flags, REFRESH);
27517 +               }
27518 +               break;
27519 +       }
27520 +       return err;
27521 +}
27522 +
27523 +static int au_opt_xino(struct super_block *sb, struct au_opt *opt,
27524 +                      struct au_opt_xino **opt_xino,
27525 +                      struct au_opts *opts)
27526 +{
27527 +       int err;
27528 +
27529 +       err = 0;
27530 +       switch (opt->type) {
27531 +       case Opt_xino:
27532 +               err = au_xino_set(sb, &opt->xino,
27533 +                                 !!au_ftest_opts(opts->flags, REMOUNT));
27534 +               if (!err)
27535 +                       *opt_xino = &opt->xino;
27536 +               break;
27537 +       case Opt_noxino:
27538 +               au_xino_clr(sb);
27539 +               *opt_xino = (void *)-1;
27540 +               break;
27541 +       }
27542 +
27543 +       return err;
27544 +}
27545 +
27546 +int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
27547 +                  unsigned int pending)
27548 +{
27549 +       int err, fhsm;
27550 +       aufs_bindex_t bindex, bbot;
27551 +       unsigned char do_plink, skip, do_free, can_no_dreval;
27552 +       struct au_branch *br;
27553 +       struct au_wbr *wbr;
27554 +       struct dentry *root, *dentry;
27555 +       struct inode *dir, *h_dir;
27556 +       struct au_sbinfo *sbinfo;
27557 +       struct au_hinode *hdir;
27558 +
27559 +       SiMustAnyLock(sb);
27560 +
27561 +       sbinfo = au_sbi(sb);
27562 +       AuDebugOn(!(sbinfo->si_mntflags & AuOptMask_UDBA));
27563 +
27564 +       if (!(sb_flags & SB_RDONLY)) {
27565 +               if (unlikely(!au_br_writable(au_sbr_perm(sb, 0))))
27566 +                       pr_warn("first branch should be rw\n");
27567 +               if (unlikely(au_opt_test(sbinfo->si_mntflags, SHWH)))
27568 +                       pr_warn_once("shwh should be used with ro\n");
27569 +       }
27570 +
27571 +       if (au_opt_test((sbinfo->si_mntflags | pending), UDBA_HNOTIFY)
27572 +           && !au_opt_test(sbinfo->si_mntflags, XINO))
27573 +               pr_warn_once("udba=*notify requires xino\n");
27574 +
27575 +       if (au_opt_test(sbinfo->si_mntflags, DIRPERM1))
27576 +               pr_warn_once("dirperm1 breaks the protection"
27577 +                            " by the permission bits on the lower branch\n");
27578 +
27579 +       err = 0;
27580 +       fhsm = 0;
27581 +       root = sb->s_root;
27582 +       dir = d_inode(root);
27583 +       do_plink = !!au_opt_test(sbinfo->si_mntflags, PLINK);
27584 +       can_no_dreval = !!au_opt_test((sbinfo->si_mntflags | pending),
27585 +                                     UDBA_NONE);
27586 +       bbot = au_sbbot(sb);
27587 +       for (bindex = 0; !err && bindex <= bbot; bindex++) {
27588 +               skip = 0;
27589 +               h_dir = au_h_iptr(dir, bindex);
27590 +               br = au_sbr(sb, bindex);
27591 +
27592 +               if ((br->br_perm & AuBrAttr_ICEX)
27593 +                   && !h_dir->i_op->listxattr)
27594 +                       br->br_perm &= ~AuBrAttr_ICEX;
27595 +#if 0 /* untested */
27596 +               if ((br->br_perm & AuBrAttr_ICEX_SEC)
27597 +                   && (au_br_sb(br)->s_flags & SB_NOSEC))
27598 +                       br->br_perm &= ~AuBrAttr_ICEX_SEC;
27599 +#endif
27600 +
27601 +               do_free = 0;
27602 +               wbr = br->br_wbr;
27603 +               if (wbr)
27604 +                       wbr_wh_read_lock(wbr);
27605 +
27606 +               if (!au_br_writable(br->br_perm)) {
27607 +                       do_free = !!wbr;
27608 +                       skip = (!wbr
27609 +                               || (!wbr->wbr_whbase
27610 +                                   && !wbr->wbr_plink
27611 +                                   && !wbr->wbr_orph));
27612 +               } else if (!au_br_wh_linkable(br->br_perm)) {
27613 +                       /* skip = (!br->br_whbase && !br->br_orph); */
27614 +                       skip = (!wbr || !wbr->wbr_whbase);
27615 +                       if (skip && wbr) {
27616 +                               if (do_plink)
27617 +                                       skip = !!wbr->wbr_plink;
27618 +                               else
27619 +                                       skip = !wbr->wbr_plink;
27620 +                       }
27621 +               } else {
27622 +                       /* skip = (br->br_whbase && br->br_ohph); */
27623 +                       skip = (wbr && wbr->wbr_whbase);
27624 +                       if (skip) {
27625 +                               if (do_plink)
27626 +                                       skip = !!wbr->wbr_plink;
27627 +                               else
27628 +                                       skip = !wbr->wbr_plink;
27629 +                       }
27630 +               }
27631 +               if (wbr)
27632 +                       wbr_wh_read_unlock(wbr);
27633 +
27634 +               if (can_no_dreval) {
27635 +                       dentry = br->br_path.dentry;
27636 +                       spin_lock(&dentry->d_lock);
27637 +                       if (dentry->d_flags &
27638 +                           (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE))
27639 +                               can_no_dreval = 0;
27640 +                       spin_unlock(&dentry->d_lock);
27641 +               }
27642 +
27643 +               if (au_br_fhsm(br->br_perm)) {
27644 +                       fhsm++;
27645 +                       AuDebugOn(!br->br_fhsm);
27646 +               }
27647 +
27648 +               if (skip)
27649 +                       continue;
27650 +
27651 +               hdir = au_hi(dir, bindex);
27652 +               au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
27653 +               if (wbr)
27654 +                       wbr_wh_write_lock(wbr);
27655 +               err = au_wh_init(br, sb);
27656 +               if (wbr)
27657 +                       wbr_wh_write_unlock(wbr);
27658 +               au_hn_inode_unlock(hdir);
27659 +
27660 +               if (!err && do_free) {
27661 +                       au_kfree_rcu(wbr);
27662 +                       br->br_wbr = NULL;
27663 +               }
27664 +       }
27665 +
27666 +       if (can_no_dreval)
27667 +               au_fset_si(sbinfo, NO_DREVAL);
27668 +       else
27669 +               au_fclr_si(sbinfo, NO_DREVAL);
27670 +
27671 +       if (fhsm >= 2) {
27672 +               au_fset_si(sbinfo, FHSM);
27673 +               for (bindex = bbot; bindex >= 0; bindex--) {
27674 +                       br = au_sbr(sb, bindex);
27675 +                       if (au_br_fhsm(br->br_perm)) {
27676 +                               au_fhsm_set_bottom(sb, bindex);
27677 +                               break;
27678 +                       }
27679 +               }
27680 +       } else {
27681 +               au_fclr_si(sbinfo, FHSM);
27682 +               au_fhsm_set_bottom(sb, -1);
27683 +       }
27684 +
27685 +       return err;
27686 +}
27687 +
27688 +int au_opts_mount(struct super_block *sb, struct au_opts *opts)
27689 +{
27690 +       int err;
27691 +       unsigned int tmp;
27692 +       aufs_bindex_t bindex, bbot;
27693 +       struct au_opt *opt;
27694 +       struct au_opt_xino *opt_xino, xino;
27695 +       struct au_sbinfo *sbinfo;
27696 +       struct au_branch *br;
27697 +       struct inode *dir;
27698 +
27699 +       SiMustWriteLock(sb);
27700 +
27701 +       err = 0;
27702 +       opt_xino = NULL;
27703 +       opt = opts->opt;
27704 +       while (err >= 0 && opt->type != Opt_tail)
27705 +               err = au_opt_simple(sb, opt++, opts);
27706 +       if (err > 0)
27707 +               err = 0;
27708 +       else if (unlikely(err < 0))
27709 +               goto out;
27710 +
27711 +       /* disable xino and udba temporary */
27712 +       sbinfo = au_sbi(sb);
27713 +       tmp = sbinfo->si_mntflags;
27714 +       au_opt_clr(sbinfo->si_mntflags, XINO);
27715 +       au_opt_set_udba(sbinfo->si_mntflags, UDBA_REVAL);
27716 +
27717 +       opt = opts->opt;
27718 +       while (err >= 0 && opt->type != Opt_tail)
27719 +               err = au_opt_br(sb, opt++, opts);
27720 +       if (err > 0)
27721 +               err = 0;
27722 +       else if (unlikely(err < 0))
27723 +               goto out;
27724 +
27725 +       bbot = au_sbbot(sb);
27726 +       if (unlikely(bbot < 0)) {
27727 +               err = -EINVAL;
27728 +               pr_err("no branches\n");
27729 +               goto out;
27730 +       }
27731 +
27732 +       if (au_opt_test(tmp, XINO))
27733 +               au_opt_set(sbinfo->si_mntflags, XINO);
27734 +       opt = opts->opt;
27735 +       while (!err && opt->type != Opt_tail)
27736 +               err = au_opt_xino(sb, opt++, &opt_xino, opts);
27737 +       if (unlikely(err))
27738 +               goto out;
27739 +
27740 +       err = au_opts_verify(sb, sb->s_flags, tmp);
27741 +       if (unlikely(err))
27742 +               goto out;
27743 +
27744 +       /* restore xino */
27745 +       if (au_opt_test(tmp, XINO) && !opt_xino) {
27746 +               xino.file = au_xino_def(sb);
27747 +               err = PTR_ERR(xino.file);
27748 +               if (IS_ERR(xino.file))
27749 +                       goto out;
27750 +
27751 +               err = au_xino_set(sb, &xino, /*remount*/0);
27752 +               fput(xino.file);
27753 +               if (unlikely(err))
27754 +                       goto out;
27755 +       }
27756 +
27757 +       /* restore udba */
27758 +       tmp &= AuOptMask_UDBA;
27759 +       sbinfo->si_mntflags &= ~AuOptMask_UDBA;
27760 +       sbinfo->si_mntflags |= tmp;
27761 +       bbot = au_sbbot(sb);
27762 +       for (bindex = 0; bindex <= bbot; bindex++) {
27763 +               br = au_sbr(sb, bindex);
27764 +               err = au_hnotify_reset_br(tmp, br, br->br_perm);
27765 +               if (unlikely(err))
27766 +                       AuIOErr("hnotify failed on br %d, %d, ignored\n",
27767 +                               bindex, err);
27768 +               /* go on even if err */
27769 +       }
27770 +       if (au_opt_test(tmp, UDBA_HNOTIFY)) {
27771 +               dir = d_inode(sb->s_root);
27772 +               au_hn_reset(dir, au_hi_flags(dir, /*isdir*/1) & ~AuHi_XINO);
27773 +       }
27774 +
27775 +out:
27776 +       return err;
27777 +}
27778 +
27779 +int au_opts_remount(struct super_block *sb, struct au_opts *opts)
27780 +{
27781 +       int err, rerr;
27782 +       unsigned char no_dreval;
27783 +       struct inode *dir;
27784 +       struct au_opt_xino *opt_xino;
27785 +       struct au_opt *opt;
27786 +       struct au_sbinfo *sbinfo;
27787 +
27788 +       SiMustWriteLock(sb);
27789 +
27790 +       err = au_dr_opt_flush(sb);
27791 +       if (unlikely(err))
27792 +               goto out;
27793 +       au_fset_opts(opts->flags, DR_FLUSHED);
27794 +
27795 +       dir = d_inode(sb->s_root);
27796 +       sbinfo = au_sbi(sb);
27797 +       opt_xino = NULL;
27798 +       opt = opts->opt;
27799 +       while (err >= 0 && opt->type != Opt_tail) {
27800 +               err = au_opt_simple(sb, opt, opts);
27801 +               if (!err)
27802 +                       err = au_opt_br(sb, opt, opts);
27803 +               if (!err)
27804 +                       err = au_opt_xino(sb, opt, &opt_xino, opts);
27805 +               opt++;
27806 +       }
27807 +       if (err > 0)
27808 +               err = 0;
27809 +       AuTraceErr(err);
27810 +       /* go on even err */
27811 +
27812 +       no_dreval = !!au_ftest_si(sbinfo, NO_DREVAL);
27813 +       rerr = au_opts_verify(sb, opts->sb_flags, /*pending*/0);
27814 +       if (unlikely(rerr && !err))
27815 +               err = rerr;
27816 +
27817 +       if (no_dreval != !!au_ftest_si(sbinfo, NO_DREVAL))
27818 +               au_fset_opts(opts->flags, REFRESH_IDOP);
27819 +
27820 +       if (au_ftest_opts(opts->flags, TRUNC_XIB)) {
27821 +               rerr = au_xib_trunc(sb);
27822 +               if (unlikely(rerr && !err))
27823 +                       err = rerr;
27824 +       }
27825 +
27826 +       /* will be handled by the caller */
27827 +       if (!au_ftest_opts(opts->flags, REFRESH)
27828 +           && (opts->given_udba
27829 +               || au_opt_test(sbinfo->si_mntflags, XINO)
27830 +               || au_ftest_opts(opts->flags, REFRESH_IDOP)
27831 +                   ))
27832 +               au_fset_opts(opts->flags, REFRESH);
27833 +
27834 +       AuDbg("status 0x%x\n", opts->flags);
27835 +
27836 +out:
27837 +       return err;
27838 +}
27839 +
27840 +/* ---------------------------------------------------------------------- */
27841 +
27842 +unsigned int au_opt_udba(struct super_block *sb)
27843 +{
27844 +       return au_mntflags(sb) & AuOptMask_UDBA;
27845 +}
27846 diff -urN /usr/share/empty/fs/aufs/opts.h linux/fs/aufs/opts.h
27847 --- /usr/share/empty/fs/aufs/opts.h     1970-01-01 01:00:00.000000000 +0100
27848 +++ linux/fs/aufs/opts.h        2022-03-21 14:49:05.729966343 +0100
27849 @@ -0,0 +1,263 @@
27850 +/* SPDX-License-Identifier: GPL-2.0 */
27851 +/*
27852 + * Copyright (C) 2005-2021 Junjiro R. Okajima
27853 + *
27854 + * This program, aufs is free software; you can redistribute it and/or modify
27855 + * it under the terms of the GNU General Public License as published by
27856 + * the Free Software Foundation; either version 2 of the License, or
27857 + * (at your option) any later version.
27858 + *
27859 + * This program is distributed in the hope that it will be useful,
27860 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
27861 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27862 + * GNU General Public License for more details.
27863 + *
27864 + * You should have received a copy of the GNU General Public License
27865 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
27866 + */
27867 +
27868 +/*
27869 + * mount options/flags
27870 + */
27871 +
27872 +#ifndef __AUFS_OPTS_H__
27873 +#define __AUFS_OPTS_H__
27874 +
27875 +#ifdef __KERNEL__
27876 +
27877 +#include <linux/fs_parser.h>
27878 +#include <linux/namei.h>
27879 +#include <linux/path.h>
27880 +
27881 +enum {
27882 +       Opt_br,
27883 +       Opt_add, Opt_del, Opt_mod, Opt_append, Opt_prepend,
27884 +       Opt_idel, Opt_imod,
27885 +       Opt_dirwh, Opt_rdcache, Opt_rdblk, Opt_rdhash,
27886 +       Opt_xino, Opt_noxino,
27887 +       Opt_trunc_xino, Opt_trunc_xino_v,
27888 +       Opt_trunc_xino_path, Opt_itrunc_xino,
27889 +       Opt_trunc_xib,
27890 +       Opt_shwh,
27891 +       Opt_plink, Opt_list_plink,
27892 +       Opt_udba,
27893 +       Opt_dio,
27894 +       Opt_diropq, Opt_diropq_a, Opt_diropq_w,
27895 +       Opt_warn_perm,
27896 +       Opt_wbr_copyup, Opt_wbr_create,
27897 +       Opt_fhsm_sec,
27898 +       Opt_verbose, Opt_noverbose,
27899 +       Opt_sum, Opt_wsum,
27900 +       Opt_dirperm1,
27901 +       Opt_dirren,
27902 +       Opt_acl,
27903 +       Opt_tail, Opt_ignore, Opt_ignore_silent, Opt_err
27904 +};
27905 +
27906 +/* ---------------------------------------------------------------------- */
27907 +
27908 +/* mount flags */
27909 +#define AuOpt_XINO             1               /* external inode number bitmap
27910 +                                                  and translation table */
27911 +#define AuOpt_TRUNC_XINO       (1 << 1)        /* truncate xino files */
27912 +#define AuOpt_UDBA_NONE                (1 << 2)        /* users direct branch access */
27913 +#define AuOpt_UDBA_REVAL       (1 << 3)
27914 +#define AuOpt_UDBA_HNOTIFY     (1 << 4)
27915 +#define AuOpt_SHWH             (1 << 5)        /* show whiteout */
27916 +#define AuOpt_PLINK            (1 << 6)        /* pseudo-link */
27917 +#define AuOpt_DIRPERM1         (1 << 7)        /* ignore the lower dir's perm
27918 +                                                  bits */
27919 +#define AuOpt_ALWAYS_DIROPQ    (1 << 9)        /* policy to creating diropq */
27920 +#define AuOpt_SUM              (1 << 10)       /* summation for statfs(2) */
27921 +#define AuOpt_SUM_W            (1 << 11)       /* unimplemented */
27922 +#define AuOpt_WARN_PERM                (1 << 12)       /* warn when add-branch */
27923 +#define AuOpt_VERBOSE          (1 << 13)       /* print the cause of error */
27924 +#define AuOpt_DIO              (1 << 14)       /* direct io */
27925 +#define AuOpt_DIRREN           (1 << 15)       /* directory rename */
27926 +
27927 +#ifndef CONFIG_AUFS_HNOTIFY
27928 +#undef AuOpt_UDBA_HNOTIFY
27929 +#define AuOpt_UDBA_HNOTIFY     0
27930 +#endif
27931 +#ifndef CONFIG_AUFS_DIRREN
27932 +#undef AuOpt_DIRREN
27933 +#define AuOpt_DIRREN           0
27934 +#endif
27935 +#ifndef CONFIG_AUFS_SHWH
27936 +#undef AuOpt_SHWH
27937 +#define AuOpt_SHWH             0
27938 +#endif
27939 +
27940 +#define AuOpt_Def      (AuOpt_XINO \
27941 +                        | AuOpt_UDBA_REVAL \
27942 +                        | AuOpt_PLINK \
27943 +                        /* | AuOpt_DIRPERM1 */ \
27944 +                        | AuOpt_WARN_PERM)
27945 +#define AuOptMask_UDBA (AuOpt_UDBA_NONE \
27946 +                        | AuOpt_UDBA_REVAL \
27947 +                        | AuOpt_UDBA_HNOTIFY)
27948 +
27949 +#define AuOpt_LkupDirFlags     (LOOKUP_FOLLOW | LOOKUP_DIRECTORY)
27950 +
27951 +#define au_opt_test(flags, name)       (flags & AuOpt_##name)
27952 +#define au_opt_set(flags, name) do { \
27953 +       BUILD_BUG_ON(AuOpt_##name & AuOptMask_UDBA); \
27954 +       ((flags) |= AuOpt_##name); \
27955 +} while (0)
27956 +#define au_opt_set_udba(flags, name) do { \
27957 +       (flags) &= ~AuOptMask_UDBA; \
27958 +       ((flags) |= AuOpt_##name); \
27959 +} while (0)
27960 +#define au_opt_clr(flags, name) do { \
27961 +       ((flags) &= ~AuOpt_##name); \
27962 +} while (0)
27963 +
27964 +static inline unsigned int au_opts_plink(unsigned int mntflags)
27965 +{
27966 +#ifdef CONFIG_PROC_FS
27967 +       return mntflags;
27968 +#else
27969 +       return mntflags & ~AuOpt_PLINK;
27970 +#endif
27971 +}
27972 +
27973 +/* ---------------------------------------------------------------------- */
27974 +
27975 +/* policies to select one among multiple writable branches */
27976 +enum {
27977 +       AuWbrCreate_TDP,        /* top down parent */
27978 +       AuWbrCreate_RR,         /* round robin */
27979 +       AuWbrCreate_MFS,        /* most free space */
27980 +       AuWbrCreate_MFSV,       /* mfs with seconds */
27981 +       AuWbrCreate_MFSRR,      /* mfs then rr */
27982 +       AuWbrCreate_MFSRRV,     /* mfs then rr with seconds */
27983 +       AuWbrCreate_TDMFS,      /* top down regardless parent and mfs */
27984 +       AuWbrCreate_TDMFSV,     /* top down regardless parent and mfs */
27985 +       AuWbrCreate_PMFS,       /* parent and mfs */
27986 +       AuWbrCreate_PMFSV,      /* parent and mfs with seconds */
27987 +       AuWbrCreate_PMFSRR,     /* parent, mfs and round-robin */
27988 +       AuWbrCreate_PMFSRRV,    /* plus seconds */
27989 +
27990 +       AuWbrCreate_Def = AuWbrCreate_TDP
27991 +};
27992 +
27993 +enum {
27994 +       AuWbrCopyup_TDP,        /* top down parent */
27995 +       AuWbrCopyup_BUP,        /* bottom up parent */
27996 +       AuWbrCopyup_BU,         /* bottom up */
27997 +
27998 +       AuWbrCopyup_Def = AuWbrCopyup_TDP
27999 +};
28000 +
28001 +/* ---------------------------------------------------------------------- */
28002 +
28003 +struct file;
28004 +
28005 +struct au_opt_add {
28006 +       aufs_bindex_t   bindex;
28007 +       char            *pathname;
28008 +       int             perm;
28009 +       struct path     path;
28010 +};
28011 +
28012 +struct au_opt_del {
28013 +       char            *pathname;
28014 +       struct path     h_path;
28015 +};
28016 +
28017 +struct au_opt_mod {
28018 +       char            *path;
28019 +       int             perm;
28020 +       struct dentry   *h_root;
28021 +};
28022 +
28023 +struct au_opt_xino {
28024 +       char            *path;
28025 +       struct file     *file;
28026 +};
28027 +
28028 +struct au_opt_xino_itrunc {
28029 +       aufs_bindex_t   bindex;
28030 +};
28031 +
28032 +struct au_opt_wbr_create {
28033 +       int                     wbr_create;
28034 +       int                     mfs_second;
28035 +       unsigned long long      mfsrr_watermark;
28036 +};
28037 +
28038 +struct au_opt {
28039 +       int type;
28040 +       union {
28041 +               struct au_opt_xino      xino;
28042 +               struct au_opt_xino_itrunc xino_itrunc;
28043 +               struct au_opt_add       add;
28044 +               struct au_opt_del       del;
28045 +               struct au_opt_mod       mod;
28046 +               int                     dirwh;
28047 +               int                     rdcache;
28048 +               unsigned int            rdblk;
28049 +               unsigned int            rdhash;
28050 +               int                     udba;
28051 +               struct au_opt_wbr_create wbr_create;
28052 +               int                     wbr_copyup;
28053 +               unsigned int            fhsm_second;
28054 +               bool                    tf; /* generic flag, true or false */
28055 +       };
28056 +};
28057 +
28058 +/* opts flags */
28059 +#define AuOpts_REMOUNT         1
28060 +#define AuOpts_REFRESH         (1 << 1)
28061 +#define AuOpts_TRUNC_XIB       (1 << 2)
28062 +#define AuOpts_REFRESH_DYAOP   (1 << 3)
28063 +#define AuOpts_REFRESH_IDOP    (1 << 4)
28064 +#define AuOpts_DR_FLUSHED      (1 << 5)
28065 +#define au_ftest_opts(flags, name)     ((flags) & AuOpts_##name)
28066 +#define au_fset_opts(flags, name) \
28067 +       do { (flags) |= AuOpts_##name; } while (0)
28068 +#define au_fclr_opts(flags, name) \
28069 +       do { (flags) &= ~AuOpts_##name; } while (0)
28070 +
28071 +#ifndef CONFIG_AUFS_DIRREN
28072 +#undef AuOpts_DR_FLUSHED
28073 +#define AuOpts_DR_FLUSHED      0
28074 +#endif
28075 +
28076 +struct au_opts {
28077 +       struct au_opt   *opt;
28078 +       int             max_opt;
28079 +
28080 +       unsigned int    given_udba;
28081 +       unsigned int    flags;
28082 +       unsigned long   sb_flags;
28083 +};
28084 +
28085 +/* ---------------------------------------------------------------------- */
28086 +
28087 +/* opts.c */
28088 +int au_br_perm_val(char *perm);
28089 +void au_optstr_br_perm(au_br_perm_str_t *str, int perm);
28090 +int au_udba_val(char *str);
28091 +const char *au_optstr_udba(int udba);
28092 +int au_wbr_create_val(char *str, struct au_opt_wbr_create *create);
28093 +const char *au_optstr_wbr_create(int wbr_create);
28094 +int au_wbr_copyup_val(char *str);
28095 +const char *au_optstr_wbr_copyup(int wbr_copyup);
28096 +
28097 +int au_opt_add(struct au_opt *opt, char *opt_str, unsigned long sb_flags,
28098 +              aufs_bindex_t bindex);
28099 +struct super_block;
28100 +int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
28101 +                  unsigned int pending);
28102 +int au_opts_mount(struct super_block *sb, struct au_opts *opts);
28103 +int au_opts_remount(struct super_block *sb, struct au_opts *opts);
28104 +
28105 +unsigned int au_opt_udba(struct super_block *sb);
28106 +
28107 +/* fsctx.c */
28108 +int aufs_fsctx_init(struct fs_context *fc);
28109 +extern const struct fs_parameter_spec aufs_fsctx_paramspec[];
28110 +
28111 +#endif /* __KERNEL__ */
28112 +#endif /* __AUFS_OPTS_H__ */
28113 diff -urN /usr/share/empty/fs/aufs/plink.c linux/fs/aufs/plink.c
28114 --- /usr/share/empty/fs/aufs/plink.c    1970-01-01 01:00:00.000000000 +0100
28115 +++ linux/fs/aufs/plink.c       2022-03-21 14:49:05.729966343 +0100
28116 @@ -0,0 +1,516 @@
28117 +// SPDX-License-Identifier: GPL-2.0
28118 +/*
28119 + * Copyright (C) 2005-2021 Junjiro R. Okajima
28120 + *
28121 + * This program, aufs is free software; you can redistribute it and/or modify
28122 + * it under the terms of the GNU General Public License as published by
28123 + * the Free Software Foundation; either version 2 of the License, or
28124 + * (at your option) any later version.
28125 + *
28126 + * This program is distributed in the hope that it will be useful,
28127 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28128 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28129 + * GNU General Public License for more details.
28130 + *
28131 + * You should have received a copy of the GNU General Public License
28132 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28133 + */
28134 +
28135 +/*
28136 + * pseudo-link
28137 + */
28138 +
28139 +#include "aufs.h"
28140 +
28141 +/*
28142 + * the pseudo-link maintenance mode.
28143 + * during a user process maintains the pseudo-links,
28144 + * prohibit adding a new plink and branch manipulation.
28145 + *
28146 + * Flags
28147 + * NOPLM:
28148 + *     For entry functions which will handle plink, and i_mutex is already held
28149 + *     in VFS.
28150 + *     They cannot wait and should return an error at once.
28151 + *     Callers has to check the error.
28152 + * NOPLMW:
28153 + *     For entry functions which will handle plink, but i_mutex is not held
28154 + *     in VFS.
28155 + *     They can wait the plink maintenance mode to finish.
28156 + *
28157 + * They behave like F_SETLK and F_SETLKW.
28158 + * If the caller never handle plink, then both flags are unnecessary.
28159 + */
28160 +
28161 +int au_plink_maint(struct super_block *sb, int flags)
28162 +{
28163 +       int err;
28164 +       pid_t pid, ppid;
28165 +       struct task_struct *parent, *prev;
28166 +       struct au_sbinfo *sbi;
28167 +
28168 +       SiMustAnyLock(sb);
28169 +
28170 +       err = 0;
28171 +       if (!au_opt_test(au_mntflags(sb), PLINK))
28172 +               goto out;
28173 +
28174 +       sbi = au_sbi(sb);
28175 +       pid = sbi->si_plink_maint_pid;
28176 +       if (!pid || pid == current->pid)
28177 +               goto out;
28178 +
28179 +       /* todo: it highly depends upon /sbin/mount.aufs */
28180 +       prev = NULL;
28181 +       parent = current;
28182 +       ppid = 0;
28183 +       rcu_read_lock();
28184 +       while (1) {
28185 +               parent = rcu_dereference(parent->real_parent);
28186 +               if (parent == prev)
28187 +                       break;
28188 +               ppid = task_pid_vnr(parent);
28189 +               if (pid == ppid) {
28190 +                       rcu_read_unlock();
28191 +                       goto out;
28192 +               }
28193 +               prev = parent;
28194 +       }
28195 +       rcu_read_unlock();
28196 +
28197 +       if (au_ftest_lock(flags, NOPLMW)) {
28198 +               /* if there is no i_mutex lock in VFS, we don't need to wait */
28199 +               /* AuDebugOn(!lockdep_depth(current)); */
28200 +               while (sbi->si_plink_maint_pid) {
28201 +                       si_read_unlock(sb);
28202 +                       /* gave up wake_up_bit() */
28203 +                       wait_event(sbi->si_plink_wq, !sbi->si_plink_maint_pid);
28204 +
28205 +                       if (au_ftest_lock(flags, FLUSH))
28206 +                               au_nwt_flush(&sbi->si_nowait);
28207 +                       si_noflush_read_lock(sb);
28208 +               }
28209 +       } else if (au_ftest_lock(flags, NOPLM)) {
28210 +               AuDbg("ppid %d, pid %d\n", ppid, pid);
28211 +               err = -EAGAIN;
28212 +       }
28213 +
28214 +out:
28215 +       return err;
28216 +}
28217 +
28218 +void au_plink_maint_leave(struct au_sbinfo *sbinfo)
28219 +{
28220 +       spin_lock(&sbinfo->si_plink_maint_lock);
28221 +       sbinfo->si_plink_maint_pid = 0;
28222 +       spin_unlock(&sbinfo->si_plink_maint_lock);
28223 +       wake_up_all(&sbinfo->si_plink_wq);
28224 +}
28225 +
28226 +int au_plink_maint_enter(struct super_block *sb)
28227 +{
28228 +       int err;
28229 +       struct au_sbinfo *sbinfo;
28230 +
28231 +       err = 0;
28232 +       sbinfo = au_sbi(sb);
28233 +       /* make sure i am the only one in this fs */
28234 +       si_write_lock(sb, AuLock_FLUSH);
28235 +       if (au_opt_test(au_mntflags(sb), PLINK)) {
28236 +               spin_lock(&sbinfo->si_plink_maint_lock);
28237 +               if (!sbinfo->si_plink_maint_pid)
28238 +                       sbinfo->si_plink_maint_pid = current->pid;
28239 +               else
28240 +                       err = -EBUSY;
28241 +               spin_unlock(&sbinfo->si_plink_maint_lock);
28242 +       }
28243 +       si_write_unlock(sb);
28244 +
28245 +       return err;
28246 +}
28247 +
28248 +/* ---------------------------------------------------------------------- */
28249 +
28250 +#ifdef CONFIG_AUFS_DEBUG
28251 +void au_plink_list(struct super_block *sb)
28252 +{
28253 +       int i;
28254 +       struct au_sbinfo *sbinfo;
28255 +       struct hlist_bl_head *hbl;
28256 +       struct hlist_bl_node *pos;
28257 +       struct au_icntnr *icntnr;
28258 +
28259 +       SiMustAnyLock(sb);
28260 +
28261 +       sbinfo = au_sbi(sb);
28262 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
28263 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
28264 +
28265 +       for (i = 0; i < AuPlink_NHASH; i++) {
28266 +               hbl = sbinfo->si_plink + i;
28267 +               hlist_bl_lock(hbl);
28268 +               hlist_bl_for_each_entry(icntnr, pos, hbl, plink)
28269 +                       AuDbg("%lu\n", icntnr->vfs_inode.i_ino);
28270 +               hlist_bl_unlock(hbl);
28271 +       }
28272 +}
28273 +#endif
28274 +
28275 +/* is the inode pseudo-linked? */
28276 +int au_plink_test(struct inode *inode)
28277 +{
28278 +       int found, i;
28279 +       struct au_sbinfo *sbinfo;
28280 +       struct hlist_bl_head *hbl;
28281 +       struct hlist_bl_node *pos;
28282 +       struct au_icntnr *icntnr;
28283 +
28284 +       sbinfo = au_sbi(inode->i_sb);
28285 +       AuRwMustAnyLock(&sbinfo->si_rwsem);
28286 +       AuDebugOn(!au_opt_test(au_mntflags(inode->i_sb), PLINK));
28287 +       AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
28288 +
28289 +       found = 0;
28290 +       i = au_plink_hash(inode->i_ino);
28291 +       hbl =  sbinfo->si_plink + i;
28292 +       hlist_bl_lock(hbl);
28293 +       hlist_bl_for_each_entry(icntnr, pos, hbl, plink)
28294 +               if (&icntnr->vfs_inode == inode) {
28295 +                       found = 1;
28296 +                       break;
28297 +               }
28298 +       hlist_bl_unlock(hbl);
28299 +       return found;
28300 +}
28301 +
28302 +/* ---------------------------------------------------------------------- */
28303 +
28304 +/*
28305 + * generate a name for plink.
28306 + * the file will be stored under AUFS_WH_PLINKDIR.
28307 + */
28308 +/* 20 is max digits length of ulong 64 */
28309 +#define PLINK_NAME_LEN ((20 + 1) * 2)
28310 +
28311 +static int plink_name(char *name, int len, struct inode *inode,
28312 +                     aufs_bindex_t bindex)
28313 +{
28314 +       int rlen;
28315 +       struct inode *h_inode;
28316 +
28317 +       h_inode = au_h_iptr(inode, bindex);
28318 +       rlen = snprintf(name, len, "%lu.%lu", inode->i_ino, h_inode->i_ino);
28319 +       return rlen;
28320 +}
28321 +
28322 +struct au_do_plink_lkup_args {
28323 +       struct dentry **errp;
28324 +       struct qstr *tgtname;
28325 +       struct path *h_ppath;
28326 +};
28327 +
28328 +static struct dentry *au_do_plink_lkup(struct qstr *tgtname,
28329 +                                      struct path *h_ppath)
28330 +{
28331 +       struct dentry *h_dentry;
28332 +       struct inode *h_inode;
28333 +
28334 +       h_inode = d_inode(h_ppath->dentry);
28335 +       inode_lock_shared_nested(h_inode, AuLsc_I_CHILD2);
28336 +       h_dentry = vfsub_lkup_one(tgtname, h_ppath);
28337 +       inode_unlock_shared(h_inode);
28338 +
28339 +       return h_dentry;
28340 +}
28341 +
28342 +static void au_call_do_plink_lkup(void *args)
28343 +{
28344 +       struct au_do_plink_lkup_args *a = args;
28345 +       *a->errp = au_do_plink_lkup(a->tgtname, a->h_ppath);
28346 +}
28347 +
28348 +/* lookup the plink-ed @inode under the branch at @bindex */
28349 +struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex)
28350 +{
28351 +       struct dentry *h_dentry;
28352 +       struct au_branch *br;
28353 +       struct path h_ppath;
28354 +       int wkq_err;
28355 +       char a[PLINK_NAME_LEN];
28356 +       struct qstr tgtname = QSTR_INIT(a, 0);
28357 +
28358 +       AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
28359 +
28360 +       br = au_sbr(inode->i_sb, bindex);
28361 +       h_ppath.dentry = br->br_wbr->wbr_plink;
28362 +       h_ppath.mnt = au_br_mnt(br);
28363 +       tgtname.len = plink_name(a, sizeof(a), inode, bindex);
28364 +
28365 +       if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
28366 +               struct au_do_plink_lkup_args args = {
28367 +                       .errp           = &h_dentry,
28368 +                       .tgtname        = &tgtname,
28369 +                       .h_ppath        = &h_ppath
28370 +               };
28371 +
28372 +               wkq_err = au_wkq_wait(au_call_do_plink_lkup, &args);
28373 +               if (unlikely(wkq_err))
28374 +                       h_dentry = ERR_PTR(wkq_err);
28375 +       } else
28376 +               h_dentry = au_do_plink_lkup(&tgtname, &h_ppath);
28377 +
28378 +       return h_dentry;
28379 +}
28380 +
28381 +/* create a pseudo-link */
28382 +static int do_whplink(struct qstr *tgt, struct path *h_ppath,
28383 +                     struct dentry *h_dentry)
28384 +{
28385 +       int err;
28386 +       struct path h_path;
28387 +       struct inode *h_dir, *delegated;
28388 +
28389 +       h_dir = d_inode(h_ppath->dentry);
28390 +       inode_lock_nested(h_dir, AuLsc_I_CHILD2);
28391 +       h_path.mnt = h_ppath->mnt;
28392 +again:
28393 +       h_path.dentry = vfsub_lkup_one(tgt, h_ppath);
28394 +       err = PTR_ERR(h_path.dentry);
28395 +       if (IS_ERR(h_path.dentry))
28396 +               goto out;
28397 +
28398 +       err = 0;
28399 +       /* wh.plink dir is not monitored */
28400 +       /* todo: is it really safe? */
28401 +       if (d_is_positive(h_path.dentry)
28402 +           && d_inode(h_path.dentry) != d_inode(h_dentry)) {
28403 +               delegated = NULL;
28404 +               err = vfsub_unlink(h_dir, &h_path, &delegated, /*force*/0);
28405 +               if (unlikely(err == -EWOULDBLOCK)) {
28406 +                       pr_warn("cannot retry for NFSv4 delegation"
28407 +                               " for an internal unlink\n");
28408 +                       iput(delegated);
28409 +               }
28410 +               dput(h_path.dentry);
28411 +               h_path.dentry = NULL;
28412 +               if (!err)
28413 +                       goto again;
28414 +       }
28415 +       if (!err && d_is_negative(h_path.dentry)) {
28416 +               delegated = NULL;
28417 +               err = vfsub_link(h_dentry, h_dir, &h_path, &delegated);
28418 +               if (unlikely(err == -EWOULDBLOCK)) {
28419 +                       pr_warn("cannot retry for NFSv4 delegation"
28420 +                               " for an internal link\n");
28421 +                       iput(delegated);
28422 +               }
28423 +       }
28424 +       dput(h_path.dentry);
28425 +
28426 +out:
28427 +       inode_unlock(h_dir);
28428 +       return err;
28429 +}
28430 +
28431 +struct do_whplink_args {
28432 +       int *errp;
28433 +       struct qstr *tgt;
28434 +       struct path *h_ppath;
28435 +       struct dentry *h_dentry;
28436 +};
28437 +
28438 +static void call_do_whplink(void *args)
28439 +{
28440 +       struct do_whplink_args *a = args;
28441 +       *a->errp = do_whplink(a->tgt, a->h_ppath, a->h_dentry);
28442 +}
28443 +
28444 +static int whplink(struct dentry *h_dentry, struct inode *inode,
28445 +                  aufs_bindex_t bindex)
28446 +{
28447 +       int err, wkq_err;
28448 +       struct au_branch *br;
28449 +       struct au_wbr *wbr;
28450 +       struct path h_ppath;
28451 +       char a[PLINK_NAME_LEN];
28452 +       struct qstr tgtname = QSTR_INIT(a, 0);
28453 +
28454 +       br = au_sbr(inode->i_sb, bindex);
28455 +       wbr = br->br_wbr;
28456 +       h_ppath.dentry = wbr->wbr_plink;
28457 +       h_ppath.mnt = au_br_mnt(br);
28458 +       tgtname.len = plink_name(a, sizeof(a), inode, bindex);
28459 +
28460 +       /* always superio. */
28461 +       if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
28462 +               struct do_whplink_args args = {
28463 +                       .errp           = &err,
28464 +                       .tgt            = &tgtname,
28465 +                       .h_ppath        = &h_ppath,
28466 +                       .h_dentry       = h_dentry
28467 +               };
28468 +               wkq_err = au_wkq_wait(call_do_whplink, &args);
28469 +               if (unlikely(wkq_err))
28470 +                       err = wkq_err;
28471 +       } else
28472 +               err = do_whplink(&tgtname, &h_ppath, h_dentry);
28473 +
28474 +       return err;
28475 +}
28476 +
28477 +/*
28478 + * create a new pseudo-link for @h_dentry on @bindex.
28479 + * the linked inode is held in aufs @inode.
28480 + */
28481 +void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
28482 +                    struct dentry *h_dentry)
28483 +{
28484 +       struct super_block *sb;
28485 +       struct au_sbinfo *sbinfo;
28486 +       struct hlist_bl_head *hbl;
28487 +       struct hlist_bl_node *pos;
28488 +       struct au_icntnr *icntnr;
28489 +       int found, err, cnt, i;
28490 +
28491 +       sb = inode->i_sb;
28492 +       sbinfo = au_sbi(sb);
28493 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
28494 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
28495 +
28496 +       found = au_plink_test(inode);
28497 +       if (found)
28498 +               return;
28499 +
28500 +       i = au_plink_hash(inode->i_ino);
28501 +       hbl = sbinfo->si_plink + i;
28502 +       au_igrab(inode);
28503 +
28504 +       hlist_bl_lock(hbl);
28505 +       hlist_bl_for_each_entry(icntnr, pos, hbl, plink) {
28506 +               if (&icntnr->vfs_inode == inode) {
28507 +                       found = 1;
28508 +                       break;
28509 +               }
28510 +       }
28511 +       if (!found) {
28512 +               icntnr = container_of(inode, struct au_icntnr, vfs_inode);
28513 +               hlist_bl_add_head(&icntnr->plink, hbl);
28514 +       }
28515 +       hlist_bl_unlock(hbl);
28516 +       if (!found) {
28517 +               cnt = au_hbl_count(hbl);
28518 +#define msg "unexpectedly unbalanced or too many pseudo-links"
28519 +               if (cnt > AUFS_PLINK_WARN)
28520 +                       AuWarn1(msg ", %d\n", cnt);
28521 +#undef msg
28522 +               err = whplink(h_dentry, inode, bindex);
28523 +               if (unlikely(err)) {
28524 +                       pr_warn("err %d, damaged pseudo link.\n", err);
28525 +                       au_hbl_del(&icntnr->plink, hbl);
28526 +                       iput(&icntnr->vfs_inode);
28527 +               }
28528 +       } else
28529 +               iput(&icntnr->vfs_inode);
28530 +}
28531 +
28532 +/* free all plinks */
28533 +void au_plink_put(struct super_block *sb, int verbose)
28534 +{
28535 +       int i, warned;
28536 +       struct au_sbinfo *sbinfo;
28537 +       struct hlist_bl_head *hbl;
28538 +       struct hlist_bl_node *pos, *tmp;
28539 +       struct au_icntnr *icntnr;
28540 +
28541 +       SiMustWriteLock(sb);
28542 +
28543 +       sbinfo = au_sbi(sb);
28544 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
28545 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
28546 +
28547 +       /* no spin_lock since sbinfo is write-locked */
28548 +       warned = 0;
28549 +       for (i = 0; i < AuPlink_NHASH; i++) {
28550 +               hbl = sbinfo->si_plink + i;
28551 +               if (!warned && verbose && !hlist_bl_empty(hbl)) {
28552 +                       pr_warn("pseudo-link is not flushed");
28553 +                       warned = 1;
28554 +               }
28555 +               hlist_bl_for_each_entry_safe(icntnr, pos, tmp, hbl, plink)
28556 +                       iput(&icntnr->vfs_inode);
28557 +               INIT_HLIST_BL_HEAD(hbl);
28558 +       }
28559 +}
28560 +
28561 +void au_plink_clean(struct super_block *sb, int verbose)
28562 +{
28563 +       struct dentry *root;
28564 +
28565 +       root = sb->s_root;
28566 +       aufs_write_lock(root);
28567 +       if (au_opt_test(au_mntflags(sb), PLINK))
28568 +               au_plink_put(sb, verbose);
28569 +       aufs_write_unlock(root);
28570 +}
28571 +
28572 +static int au_plink_do_half_refresh(struct inode *inode, aufs_bindex_t br_id)
28573 +{
28574 +       int do_put;
28575 +       aufs_bindex_t btop, bbot, bindex;
28576 +
28577 +       do_put = 0;
28578 +       btop = au_ibtop(inode);
28579 +       bbot = au_ibbot(inode);
28580 +       if (btop >= 0) {
28581 +               for (bindex = btop; bindex <= bbot; bindex++) {
28582 +                       if (!au_h_iptr(inode, bindex)
28583 +                           || au_ii_br_id(inode, bindex) != br_id)
28584 +                               continue;
28585 +                       au_set_h_iptr(inode, bindex, NULL, 0);
28586 +                       do_put = 1;
28587 +                       break;
28588 +               }
28589 +               if (do_put)
28590 +                       for (bindex = btop; bindex <= bbot; bindex++)
28591 +                               if (au_h_iptr(inode, bindex)) {
28592 +                                       do_put = 0;
28593 +                                       break;
28594 +                               }
28595 +       } else
28596 +               do_put = 1;
28597 +
28598 +       return do_put;
28599 +}
28600 +
28601 +/* free the plinks on a branch specified by @br_id */
28602 +void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id)
28603 +{
28604 +       struct au_sbinfo *sbinfo;
28605 +       struct hlist_bl_head *hbl;
28606 +       struct hlist_bl_node *pos, *tmp;
28607 +       struct au_icntnr *icntnr;
28608 +       struct inode *inode;
28609 +       int i, do_put;
28610 +
28611 +       SiMustWriteLock(sb);
28612 +
28613 +       sbinfo = au_sbi(sb);
28614 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
28615 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
28616 +
28617 +       /* no bit_lock since sbinfo is write-locked */
28618 +       for (i = 0; i < AuPlink_NHASH; i++) {
28619 +               hbl = sbinfo->si_plink + i;
28620 +               hlist_bl_for_each_entry_safe(icntnr, pos, tmp, hbl, plink) {
28621 +                       inode = au_igrab(&icntnr->vfs_inode);
28622 +                       ii_write_lock_child(inode);
28623 +                       do_put = au_plink_do_half_refresh(inode, br_id);
28624 +                       if (do_put) {
28625 +                               hlist_bl_del(&icntnr->plink);
28626 +                               iput(inode);
28627 +                       }
28628 +                       ii_write_unlock(inode);
28629 +                       iput(inode);
28630 +               }
28631 +       }
28632 +}
28633 diff -urN /usr/share/empty/fs/aufs/poll.c linux/fs/aufs/poll.c
28634 --- /usr/share/empty/fs/aufs/poll.c     1970-01-01 01:00:00.000000000 +0100
28635 +++ linux/fs/aufs/poll.c        2022-03-21 14:49:05.729966343 +0100
28636 @@ -0,0 +1,51 @@
28637 +// SPDX-License-Identifier: GPL-2.0
28638 +/*
28639 + * Copyright (C) 2005-2021 Junjiro R. Okajima
28640 + *
28641 + * This program, aufs is free software; you can redistribute it and/or modify
28642 + * it under the terms of the GNU General Public License as published by
28643 + * the Free Software Foundation; either version 2 of the License, or
28644 + * (at your option) any later version.
28645 + *
28646 + * This program is distributed in the hope that it will be useful,
28647 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28648 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28649 + * GNU General Public License for more details.
28650 + *
28651 + * You should have received a copy of the GNU General Public License
28652 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28653 + */
28654 +
28655 +/*
28656 + * poll operation
28657 + * There is only one filesystem which implements ->poll operation, currently.
28658 + */
28659 +
28660 +#include "aufs.h"
28661 +
28662 +__poll_t aufs_poll(struct file *file, struct poll_table_struct *pt)
28663 +{
28664 +       __poll_t mask;
28665 +       struct file *h_file;
28666 +       struct super_block *sb;
28667 +
28668 +       /* We should pretend an error happened. */
28669 +       mask = EPOLLERR /* | EPOLLIN | EPOLLOUT */;
28670 +       sb = file->f_path.dentry->d_sb;
28671 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
28672 +
28673 +       h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
28674 +       if (IS_ERR(h_file)) {
28675 +               AuDbg("h_file %ld\n", PTR_ERR(h_file));
28676 +               goto out;
28677 +       }
28678 +
28679 +       mask = vfs_poll(h_file, pt);
28680 +       fput(h_file); /* instead of au_read_post() */
28681 +
28682 +out:
28683 +       si_read_unlock(sb);
28684 +       if (mask & EPOLLERR)
28685 +               AuDbg("mask 0x%x\n", mask);
28686 +       return mask;
28687 +}
28688 diff -urN /usr/share/empty/fs/aufs/posix_acl.c linux/fs/aufs/posix_acl.c
28689 --- /usr/share/empty/fs/aufs/posix_acl.c        1970-01-01 01:00:00.000000000 +0100
28690 +++ linux/fs/aufs/posix_acl.c   2022-03-21 14:49:05.729966343 +0100
28691 @@ -0,0 +1,111 @@
28692 +// SPDX-License-Identifier: GPL-2.0
28693 +/*
28694 + * Copyright (C) 2014-2021 Junjiro R. Okajima
28695 + *
28696 + * This program, aufs is free software; you can redistribute it and/or modify
28697 + * it under the terms of the GNU General Public License as published by
28698 + * the Free Software Foundation; either version 2 of the License, or
28699 + * (at your option) any later version.
28700 + *
28701 + * This program is distributed in the hope that it will be useful,
28702 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28703 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28704 + * GNU General Public License for more details.
28705 + *
28706 + * You should have received a copy of the GNU General Public License
28707 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28708 + */
28709 +
28710 +/*
28711 + * posix acl operations
28712 + */
28713 +
28714 +#include <linux/fs.h>
28715 +#include "aufs.h"
28716 +
28717 +struct posix_acl *aufs_get_acl(struct inode *inode, int type, bool rcu)
28718 +{
28719 +       struct posix_acl *acl;
28720 +       int err;
28721 +       aufs_bindex_t bindex;
28722 +       struct inode *h_inode;
28723 +       struct super_block *sb;
28724 +
28725 +       acl = ERR_PTR(-ECHILD);
28726 +       if (rcu)
28727 +               goto out;
28728 +
28729 +       acl = NULL;
28730 +       sb = inode->i_sb;
28731 +       si_read_lock(sb, AuLock_FLUSH);
28732 +       ii_read_lock_child(inode);
28733 +       if (!(sb->s_flags & SB_POSIXACL))
28734 +               goto unlock;
28735 +
28736 +       bindex = au_ibtop(inode);
28737 +       h_inode = au_h_iptr(inode, bindex);
28738 +       if (unlikely(!h_inode
28739 +                    || ((h_inode->i_mode & S_IFMT)
28740 +                        != (inode->i_mode & S_IFMT)))) {
28741 +               err = au_busy_or_stale();
28742 +               acl = ERR_PTR(err);
28743 +               goto unlock;
28744 +       }
28745 +
28746 +       /* always topmost only */
28747 +       acl = get_acl(h_inode, type);
28748 +       if (IS_ERR(acl))
28749 +               forget_cached_acl(inode, type);
28750 +       else
28751 +               set_cached_acl(inode, type, acl);
28752 +
28753 +unlock:
28754 +       ii_read_unlock(inode);
28755 +       si_read_unlock(sb);
28756 +
28757 +out:
28758 +       AuTraceErrPtr(acl);
28759 +       return acl;
28760 +}
28761 +
28762 +int aufs_set_acl(struct user_namespace *userns, struct inode *inode,
28763 +                struct posix_acl *acl, int type)
28764 +{
28765 +       int err;
28766 +       ssize_t ssz;
28767 +       struct dentry *dentry;
28768 +       struct au_sxattr arg = {
28769 +               .type = AU_ACL_SET,
28770 +               .u.acl_set = {
28771 +                       .acl    = acl,
28772 +                       .type   = type
28773 +               },
28774 +       };
28775 +
28776 +       IMustLock(inode);
28777 +
28778 +       if (inode->i_ino == AUFS_ROOT_INO)
28779 +               dentry = dget(inode->i_sb->s_root);
28780 +       else {
28781 +               dentry = d_find_alias(inode);
28782 +               if (!dentry)
28783 +                       dentry = d_find_any_alias(inode);
28784 +               if (!dentry) {
28785 +                       pr_warn("cannot handle this inode, "
28786 +                               "please report to aufs-users ML\n");
28787 +                       err = -ENOENT;
28788 +                       goto out;
28789 +               }
28790 +       }
28791 +
28792 +       ssz = au_sxattr(dentry, inode, &arg);
28793 +       /* forget even it if succeeds since the branch might set differently */
28794 +       forget_cached_acl(inode, type);
28795 +       dput(dentry);
28796 +       err = ssz;
28797 +       if (ssz >= 0)
28798 +               err = 0;
28799 +
28800 +out:
28801 +       return err;
28802 +}
28803 diff -urN /usr/share/empty/fs/aufs/procfs.c linux/fs/aufs/procfs.c
28804 --- /usr/share/empty/fs/aufs/procfs.c   1970-01-01 01:00:00.000000000 +0100
28805 +++ linux/fs/aufs/procfs.c      2022-03-21 14:49:05.729966343 +0100
28806 @@ -0,0 +1,170 @@
28807 +// SPDX-License-Identifier: GPL-2.0
28808 +/*
28809 + * Copyright (C) 2010-2021 Junjiro R. Okajima
28810 + *
28811 + * This program, aufs is free software; you can redistribute it and/or modify
28812 + * it under the terms of the GNU General Public License as published by
28813 + * the Free Software Foundation; either version 2 of the License, or
28814 + * (at your option) any later version.
28815 + *
28816 + * This program is distributed in the hope that it will be useful,
28817 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28818 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28819 + * GNU General Public License for more details.
28820 + *
28821 + * You should have received a copy of the GNU General Public License
28822 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28823 + */
28824 +
28825 +/*
28826 + * procfs interfaces
28827 + */
28828 +
28829 +#include <linux/proc_fs.h>
28830 +#include "aufs.h"
28831 +
28832 +static int au_procfs_plm_release(struct inode *inode, struct file *file)
28833 +{
28834 +       struct au_sbinfo *sbinfo;
28835 +
28836 +       sbinfo = file->private_data;
28837 +       if (sbinfo) {
28838 +               au_plink_maint_leave(sbinfo);
28839 +               kobject_put(&sbinfo->si_kobj);
28840 +       }
28841 +
28842 +       return 0;
28843 +}
28844 +
28845 +static void au_procfs_plm_write_clean(struct file *file)
28846 +{
28847 +       struct au_sbinfo *sbinfo;
28848 +
28849 +       sbinfo = file->private_data;
28850 +       if (sbinfo)
28851 +               au_plink_clean(sbinfo->si_sb, /*verbose*/0);
28852 +}
28853 +
28854 +static int au_procfs_plm_write_si(struct file *file, unsigned long id)
28855 +{
28856 +       int err;
28857 +       struct super_block *sb;
28858 +       struct au_sbinfo *sbinfo;
28859 +       struct hlist_bl_node *pos;
28860 +
28861 +       err = -EBUSY;
28862 +       if (unlikely(file->private_data))
28863 +               goto out;
28864 +
28865 +       sb = NULL;
28866 +       /* don't use au_sbilist_lock() here */
28867 +       hlist_bl_lock(&au_sbilist);
28868 +       hlist_bl_for_each_entry(sbinfo, pos, &au_sbilist, si_list)
28869 +               if (id == sysaufs_si_id(sbinfo)) {
28870 +                       if (kobject_get_unless_zero(&sbinfo->si_kobj))
28871 +                               sb = sbinfo->si_sb;
28872 +                       break;
28873 +               }
28874 +       hlist_bl_unlock(&au_sbilist);
28875 +
28876 +       err = -EINVAL;
28877 +       if (unlikely(!sb))
28878 +               goto out;
28879 +
28880 +       err = au_plink_maint_enter(sb);
28881 +       if (!err)
28882 +               /* keep kobject_get() */
28883 +               file->private_data = sbinfo;
28884 +       else
28885 +               kobject_put(&sbinfo->si_kobj);
28886 +out:
28887 +       return err;
28888 +}
28889 +
28890 +/*
28891 + * Accept a valid "si=xxxx" only.
28892 + * Once it is accepted successfully, accept "clean" too.
28893 + */
28894 +static ssize_t au_procfs_plm_write(struct file *file, const char __user *ubuf,
28895 +                                  size_t count, loff_t *ppos)
28896 +{
28897 +       ssize_t err;
28898 +       unsigned long id;
28899 +       /* last newline is allowed */
28900 +       char buf[3 + sizeof(unsigned long) * 2 + 1];
28901 +
28902 +       err = -EACCES;
28903 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
28904 +               goto out;
28905 +
28906 +       err = -EINVAL;
28907 +       if (unlikely(count > sizeof(buf)))
28908 +               goto out;
28909 +
28910 +       err = copy_from_user(buf, ubuf, count);
28911 +       if (unlikely(err)) {
28912 +               err = -EFAULT;
28913 +               goto out;
28914 +       }
28915 +       buf[count] = 0;
28916 +
28917 +       err = -EINVAL;
28918 +       if (!strcmp("clean", buf)) {
28919 +               au_procfs_plm_write_clean(file);
28920 +               goto out_success;
28921 +       } else if (unlikely(strncmp("si=", buf, 3)))
28922 +               goto out;
28923 +
28924 +       err = kstrtoul(buf + 3, 16, &id);
28925 +       if (unlikely(err))
28926 +               goto out;
28927 +
28928 +       err = au_procfs_plm_write_si(file, id);
28929 +       if (unlikely(err))
28930 +               goto out;
28931 +
28932 +out_success:
28933 +       err = count; /* success */
28934 +out:
28935 +       return err;
28936 +}
28937 +
28938 +static const struct proc_ops au_procfs_plm_op = {
28939 +       .proc_write     = au_procfs_plm_write,
28940 +       .proc_release   = au_procfs_plm_release
28941 +};
28942 +
28943 +/* ---------------------------------------------------------------------- */
28944 +
28945 +static struct proc_dir_entry *au_procfs_dir;
28946 +
28947 +void au_procfs_fin(void)
28948 +{
28949 +       remove_proc_entry(AUFS_PLINK_MAINT_NAME, au_procfs_dir);
28950 +       remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
28951 +}
28952 +
28953 +int __init au_procfs_init(void)
28954 +{
28955 +       int err;
28956 +       struct proc_dir_entry *entry;
28957 +
28958 +       err = -ENOMEM;
28959 +       au_procfs_dir = proc_mkdir(AUFS_PLINK_MAINT_DIR, NULL);
28960 +       if (unlikely(!au_procfs_dir))
28961 +               goto out;
28962 +
28963 +       entry = proc_create(AUFS_PLINK_MAINT_NAME, S_IFREG | 0200,
28964 +                           au_procfs_dir, &au_procfs_plm_op);
28965 +       if (unlikely(!entry))
28966 +               goto out_dir;
28967 +
28968 +       err = 0;
28969 +       goto out; /* success */
28970 +
28971 +
28972 +out_dir:
28973 +       remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
28974 +out:
28975 +       return err;
28976 +}
28977 diff -urN /usr/share/empty/fs/aufs/rdu.c linux/fs/aufs/rdu.c
28978 --- /usr/share/empty/fs/aufs/rdu.c      1970-01-01 01:00:00.000000000 +0100
28979 +++ linux/fs/aufs/rdu.c 2022-03-21 14:49:05.729966343 +0100
28980 @@ -0,0 +1,384 @@
28981 +// SPDX-License-Identifier: GPL-2.0
28982 +/*
28983 + * Copyright (C) 2005-2021 Junjiro R. Okajima
28984 + *
28985 + * This program, aufs is free software; you can redistribute it and/or modify
28986 + * it under the terms of the GNU General Public License as published by
28987 + * the Free Software Foundation; either version 2 of the License, or
28988 + * (at your option) any later version.
28989 + *
28990 + * This program is distributed in the hope that it will be useful,
28991 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28992 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28993 + * GNU General Public License for more details.
28994 + *
28995 + * You should have received a copy of the GNU General Public License
28996 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28997 + */
28998 +
28999 +/*
29000 + * readdir in userspace.
29001 + */
29002 +
29003 +#include <linux/compat.h>
29004 +#include <linux/fs_stack.h>
29005 +#include <linux/security.h>
29006 +#include "aufs.h"
29007 +
29008 +/* bits for struct aufs_rdu.flags */
29009 +#define        AuRdu_CALLED    1
29010 +#define        AuRdu_CONT      (1 << 1)
29011 +#define        AuRdu_FULL      (1 << 2)
29012 +#define au_ftest_rdu(flags, name)      ((flags) & AuRdu_##name)
29013 +#define au_fset_rdu(flags, name) \
29014 +       do { (flags) |= AuRdu_##name; } while (0)
29015 +#define au_fclr_rdu(flags, name) \
29016 +       do { (flags) &= ~AuRdu_##name; } while (0)
29017 +
29018 +struct au_rdu_arg {
29019 +       struct dir_context              ctx;
29020 +       struct aufs_rdu                 *rdu;
29021 +       union au_rdu_ent_ul             ent;
29022 +       unsigned long                   end;
29023 +
29024 +       struct super_block              *sb;
29025 +       int                             err;
29026 +};
29027 +
29028 +static int au_rdu_fill(struct dir_context *ctx, const char *name, int nlen,
29029 +                      loff_t offset, u64 h_ino, unsigned int d_type)
29030 +{
29031 +       int err, len;
29032 +       struct au_rdu_arg *arg = container_of(ctx, struct au_rdu_arg, ctx);
29033 +       struct aufs_rdu *rdu = arg->rdu;
29034 +       struct au_rdu_ent ent;
29035 +
29036 +       err = 0;
29037 +       arg->err = 0;
29038 +       au_fset_rdu(rdu->cookie.flags, CALLED);
29039 +       len = au_rdu_len(nlen);
29040 +       if (arg->ent.ul + len  < arg->end) {
29041 +               ent.ino = h_ino;
29042 +               ent.bindex = rdu->cookie.bindex;
29043 +               ent.type = d_type;
29044 +               ent.nlen = nlen;
29045 +               if (unlikely(nlen > AUFS_MAX_NAMELEN))
29046 +                       ent.type = DT_UNKNOWN;
29047 +
29048 +               /* unnecessary to support mmap_sem since this is a dir */
29049 +               err = -EFAULT;
29050 +               if (copy_to_user(arg->ent.e, &ent, sizeof(ent)))
29051 +                       goto out;
29052 +               if (copy_to_user(arg->ent.e->name, name, nlen))
29053 +                       goto out;
29054 +               /* the terminating NULL */
29055 +               if (__put_user(0, arg->ent.e->name + nlen))
29056 +                       goto out;
29057 +               err = 0;
29058 +               /* AuDbg("%p, %.*s\n", arg->ent.p, nlen, name); */
29059 +               arg->ent.ul += len;
29060 +               rdu->rent++;
29061 +       } else {
29062 +               err = -EFAULT;
29063 +               au_fset_rdu(rdu->cookie.flags, FULL);
29064 +               rdu->full = 1;
29065 +               rdu->tail = arg->ent;
29066 +       }
29067 +
29068 +out:
29069 +       /* AuTraceErr(err); */
29070 +       return err;
29071 +}
29072 +
29073 +static int au_rdu_do(struct file *h_file, struct au_rdu_arg *arg)
29074 +{
29075 +       int err;
29076 +       loff_t offset;
29077 +       struct au_rdu_cookie *cookie = &arg->rdu->cookie;
29078 +
29079 +       /* we don't have to care (FMODE_32BITHASH | FMODE_64BITHASH) for ext4 */
29080 +       offset = vfsub_llseek(h_file, cookie->h_pos, SEEK_SET);
29081 +       err = offset;
29082 +       if (unlikely(offset != cookie->h_pos))
29083 +               goto out;
29084 +
29085 +       err = 0;
29086 +       do {
29087 +               arg->err = 0;
29088 +               au_fclr_rdu(cookie->flags, CALLED);
29089 +               /* smp_mb(); */
29090 +               err = vfsub_iterate_dir(h_file, &arg->ctx);
29091 +               if (err >= 0)
29092 +                       err = arg->err;
29093 +       } while (!err
29094 +                && au_ftest_rdu(cookie->flags, CALLED)
29095 +                && !au_ftest_rdu(cookie->flags, FULL));
29096 +       cookie->h_pos = h_file->f_pos;
29097 +
29098 +out:
29099 +       AuTraceErr(err);
29100 +       return err;
29101 +}
29102 +
29103 +static int au_rdu(struct file *file, struct aufs_rdu *rdu)
29104 +{
29105 +       int err;
29106 +       aufs_bindex_t bbot;
29107 +       struct au_rdu_arg arg = {
29108 +               .ctx = {
29109 +                       .actor = au_rdu_fill
29110 +               }
29111 +       };
29112 +       struct dentry *dentry;
29113 +       struct inode *inode;
29114 +       struct file *h_file;
29115 +       struct au_rdu_cookie *cookie = &rdu->cookie;
29116 +
29117 +       /* VERIFY_WRITE */
29118 +       err = !access_ok(rdu->ent.e, rdu->sz);
29119 +       if (unlikely(err)) {
29120 +               err = -EFAULT;
29121 +               AuTraceErr(err);
29122 +               goto out;
29123 +       }
29124 +       rdu->rent = 0;
29125 +       rdu->tail = rdu->ent;
29126 +       rdu->full = 0;
29127 +       arg.rdu = rdu;
29128 +       arg.ent = rdu->ent;
29129 +       arg.end = arg.ent.ul;
29130 +       arg.end += rdu->sz;
29131 +
29132 +       err = -ENOTDIR;
29133 +       if (unlikely(!file->f_op->iterate && !file->f_op->iterate_shared))
29134 +               goto out;
29135 +
29136 +       err = security_file_permission(file, MAY_READ);
29137 +       AuTraceErr(err);
29138 +       if (unlikely(err))
29139 +               goto out;
29140 +
29141 +       dentry = file->f_path.dentry;
29142 +       inode = d_inode(dentry);
29143 +       inode_lock_shared(inode);
29144 +
29145 +       arg.sb = inode->i_sb;
29146 +       err = si_read_lock(arg.sb, AuLock_FLUSH | AuLock_NOPLM);
29147 +       if (unlikely(err))
29148 +               goto out_mtx;
29149 +       err = au_alive_dir(dentry);
29150 +       if (unlikely(err))
29151 +               goto out_si;
29152 +       /* todo: reval? */
29153 +       fi_read_lock(file);
29154 +
29155 +       err = -EAGAIN;
29156 +       if (unlikely(au_ftest_rdu(cookie->flags, CONT)
29157 +                    && cookie->generation != au_figen(file)))
29158 +               goto out_unlock;
29159 +
29160 +       err = 0;
29161 +       if (!rdu->blk) {
29162 +               rdu->blk = au_sbi(arg.sb)->si_rdblk;
29163 +               if (!rdu->blk)
29164 +                       rdu->blk = au_dir_size(file, /*dentry*/NULL);
29165 +       }
29166 +       bbot = au_fbtop(file);
29167 +       if (cookie->bindex < bbot)
29168 +               cookie->bindex = bbot;
29169 +       bbot = au_fbbot_dir(file);
29170 +       /* AuDbg("b%d, b%d\n", cookie->bindex, bbot); */
29171 +       for (; !err && cookie->bindex <= bbot;
29172 +            cookie->bindex++, cookie->h_pos = 0) {
29173 +               h_file = au_hf_dir(file, cookie->bindex);
29174 +               if (!h_file)
29175 +                       continue;
29176 +
29177 +               au_fclr_rdu(cookie->flags, FULL);
29178 +               err = au_rdu_do(h_file, &arg);
29179 +               AuTraceErr(err);
29180 +               if (unlikely(au_ftest_rdu(cookie->flags, FULL) || err))
29181 +                       break;
29182 +       }
29183 +       AuDbg("rent %llu\n", rdu->rent);
29184 +
29185 +       if (!err && !au_ftest_rdu(cookie->flags, CONT)) {
29186 +               rdu->shwh = !!au_opt_test(au_sbi(arg.sb)->si_mntflags, SHWH);
29187 +               au_fset_rdu(cookie->flags, CONT);
29188 +               cookie->generation = au_figen(file);
29189 +       }
29190 +
29191 +       ii_read_lock_child(inode);
29192 +       fsstack_copy_attr_atime(inode, au_h_iptr(inode, au_ibtop(inode)));
29193 +       ii_read_unlock(inode);
29194 +
29195 +out_unlock:
29196 +       fi_read_unlock(file);
29197 +out_si:
29198 +       si_read_unlock(arg.sb);
29199 +out_mtx:
29200 +       inode_unlock_shared(inode);
29201 +out:
29202 +       AuTraceErr(err);
29203 +       return err;
29204 +}
29205 +
29206 +static int au_rdu_ino(struct file *file, struct aufs_rdu *rdu)
29207 +{
29208 +       int err;
29209 +       ino_t ino;
29210 +       unsigned long long nent;
29211 +       union au_rdu_ent_ul *u;
29212 +       struct au_rdu_ent ent;
29213 +       struct super_block *sb;
29214 +
29215 +       err = 0;
29216 +       nent = rdu->nent;
29217 +       u = &rdu->ent;
29218 +       sb = file->f_path.dentry->d_sb;
29219 +       si_read_lock(sb, AuLock_FLUSH);
29220 +       while (nent-- > 0) {
29221 +               /* unnecessary to support mmap_sem since this is a dir */
29222 +               err = copy_from_user(&ent, u->e, sizeof(ent));
29223 +               if (!err)
29224 +                       /* VERIFY_WRITE */
29225 +                       err = !access_ok(&u->e->ino, sizeof(ino));
29226 +               if (unlikely(err)) {
29227 +                       err = -EFAULT;
29228 +                       AuTraceErr(err);
29229 +                       break;
29230 +               }
29231 +
29232 +               /* AuDbg("b%d, i%llu\n", ent.bindex, ent.ino); */
29233 +               if (!ent.wh)
29234 +                       err = au_ino(sb, ent.bindex, ent.ino, ent.type, &ino);
29235 +               else
29236 +                       err = au_wh_ino(sb, ent.bindex, ent.ino, ent.type,
29237 +                                       &ino);
29238 +               if (unlikely(err)) {
29239 +                       AuTraceErr(err);
29240 +                       break;
29241 +               }
29242 +
29243 +               err = __put_user(ino, &u->e->ino);
29244 +               if (unlikely(err)) {
29245 +                       err = -EFAULT;
29246 +                       AuTraceErr(err);
29247 +                       break;
29248 +               }
29249 +               u->ul += au_rdu_len(ent.nlen);
29250 +       }
29251 +       si_read_unlock(sb);
29252 +
29253 +       return err;
29254 +}
29255 +
29256 +/* ---------------------------------------------------------------------- */
29257 +
29258 +static int au_rdu_verify(struct aufs_rdu *rdu)
29259 +{
29260 +       AuDbg("rdu{%llu, %p, %u | %u | %llu, %u, %u | "
29261 +             "%llu, b%d, 0x%x, g%u}\n",
29262 +             rdu->sz, rdu->ent.e, rdu->verify[AufsCtlRduV_SZ],
29263 +             rdu->blk,
29264 +             rdu->rent, rdu->shwh, rdu->full,
29265 +             rdu->cookie.h_pos, rdu->cookie.bindex, rdu->cookie.flags,
29266 +             rdu->cookie.generation);
29267 +
29268 +       if (rdu->verify[AufsCtlRduV_SZ] == sizeof(*rdu))
29269 +               return 0;
29270 +
29271 +       AuDbg("%u:%u\n",
29272 +             rdu->verify[AufsCtlRduV_SZ], (unsigned int)sizeof(*rdu));
29273 +       return -EINVAL;
29274 +}
29275 +
29276 +long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
29277 +{
29278 +       long err, e;
29279 +       struct aufs_rdu rdu;
29280 +       void __user *p = (void __user *)arg;
29281 +
29282 +       err = copy_from_user(&rdu, p, sizeof(rdu));
29283 +       if (unlikely(err)) {
29284 +               err = -EFAULT;
29285 +               AuTraceErr(err);
29286 +               goto out;
29287 +       }
29288 +       err = au_rdu_verify(&rdu);
29289 +       if (unlikely(err))
29290 +               goto out;
29291 +
29292 +       switch (cmd) {
29293 +       case AUFS_CTL_RDU:
29294 +               err = au_rdu(file, &rdu);
29295 +               if (unlikely(err))
29296 +                       break;
29297 +
29298 +               e = copy_to_user(p, &rdu, sizeof(rdu));
29299 +               if (unlikely(e)) {
29300 +                       err = -EFAULT;
29301 +                       AuTraceErr(err);
29302 +               }
29303 +               break;
29304 +       case AUFS_CTL_RDU_INO:
29305 +               err = au_rdu_ino(file, &rdu);
29306 +               break;
29307 +
29308 +       default:
29309 +               /* err = -ENOTTY; */
29310 +               err = -EINVAL;
29311 +       }
29312 +
29313 +out:
29314 +       AuTraceErr(err);
29315 +       return err;
29316 +}
29317 +
29318 +#ifdef CONFIG_COMPAT
29319 +long au_rdu_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
29320 +{
29321 +       long err, e;
29322 +       struct aufs_rdu rdu;
29323 +       void __user *p = compat_ptr(arg);
29324 +
29325 +       /* todo: get_user()? */
29326 +       err = copy_from_user(&rdu, p, sizeof(rdu));
29327 +       if (unlikely(err)) {
29328 +               err = -EFAULT;
29329 +               AuTraceErr(err);
29330 +               goto out;
29331 +       }
29332 +       rdu.ent.e = compat_ptr(rdu.ent.ul);
29333 +       err = au_rdu_verify(&rdu);
29334 +       if (unlikely(err))
29335 +               goto out;
29336 +
29337 +       switch (cmd) {
29338 +       case AUFS_CTL_RDU:
29339 +               err = au_rdu(file, &rdu);
29340 +               if (unlikely(err))
29341 +                       break;
29342 +
29343 +               rdu.ent.ul = ptr_to_compat(rdu.ent.e);
29344 +               rdu.tail.ul = ptr_to_compat(rdu.tail.e);
29345 +               e = copy_to_user(p, &rdu, sizeof(rdu));
29346 +               if (unlikely(e)) {
29347 +                       err = -EFAULT;
29348 +                       AuTraceErr(err);
29349 +               }
29350 +               break;
29351 +       case AUFS_CTL_RDU_INO:
29352 +               err = au_rdu_ino(file, &rdu);
29353 +               break;
29354 +
29355 +       default:
29356 +               /* err = -ENOTTY; */
29357 +               err = -EINVAL;
29358 +       }
29359 +
29360 +out:
29361 +       AuTraceErr(err);
29362 +       return err;
29363 +}
29364 +#endif
29365 diff -urN /usr/share/empty/fs/aufs/rwsem.h linux/fs/aufs/rwsem.h
29366 --- /usr/share/empty/fs/aufs/rwsem.h    1970-01-01 01:00:00.000000000 +0100
29367 +++ linux/fs/aufs/rwsem.h       2022-03-21 14:49:05.729966343 +0100
29368 @@ -0,0 +1,85 @@
29369 +/* SPDX-License-Identifier: GPL-2.0 */
29370 +/*
29371 + * Copyright (C) 2005-2021 Junjiro R. Okajima
29372 + *
29373 + * This program, aufs is free software; you can redistribute it and/or modify
29374 + * it under the terms of the GNU General Public License as published by
29375 + * the Free Software Foundation; either version 2 of the License, or
29376 + * (at your option) any later version.
29377 + *
29378 + * This program is distributed in the hope that it will be useful,
29379 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
29380 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29381 + * GNU General Public License for more details.
29382 + *
29383 + * You should have received a copy of the GNU General Public License
29384 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
29385 + */
29386 +
29387 +/*
29388 + * simple read-write semaphore wrappers
29389 + */
29390 +
29391 +#ifndef __AUFS_RWSEM_H__
29392 +#define __AUFS_RWSEM_H__
29393 +
29394 +#ifdef __KERNEL__
29395 +
29396 +#include "debug.h"
29397 +
29398 +/* in the future, the name 'au_rwsem' will be totally gone */
29399 +#define au_rwsem       rw_semaphore
29400 +
29401 +/* to debug easier, do not make them inlined functions */
29402 +#define AuRwMustNoWaiters(rw)  AuDebugOn(rwsem_is_contended(rw))
29403 +
29404 +#ifdef CONFIG_LOCKDEP
29405 +/* rwsem_is_locked() is unusable */
29406 +#define AuRwMustReadLock(rw)   AuDebugOn(IS_ENABLED(CONFIG_LOCKDEP)    \
29407 +                                         && !lockdep_recursing(current) \
29408 +                                         && debug_locks                \
29409 +                                         && !lockdep_is_held_type(rw, 1))
29410 +#define AuRwMustWriteLock(rw)  AuDebugOn(IS_ENABLED(CONFIG_LOCKDEP)    \
29411 +                                         && !lockdep_recursing(current) \
29412 +                                         && debug_locks                \
29413 +                                         && !lockdep_is_held_type(rw, 0))
29414 +#define AuRwMustAnyLock(rw)    AuDebugOn(IS_ENABLED(CONFIG_LOCKDEP)    \
29415 +                                         && !lockdep_recursing(current) \
29416 +                                         && debug_locks                \
29417 +                                         && !lockdep_is_held(rw))
29418 +#define AuRwDestroy(rw)                AuDebugOn(IS_ENABLED(CONFIG_LOCKDEP)    \
29419 +                                         && !lockdep_recursing(current) \
29420 +                                         && debug_locks                \
29421 +                                         && lockdep_is_held(rw))
29422 +#else
29423 +#define AuRwMustReadLock(rw)   do {} while (0)
29424 +#define AuRwMustWriteLock(rw)  do {} while (0)
29425 +#define AuRwMustAnyLock(rw)    do {} while (0)
29426 +#define AuRwDestroy(rw)                do {} while (0)
29427 +#endif
29428 +
29429 +#define au_rw_init(rw) init_rwsem(rw)
29430 +
29431 +#define au_rw_init_wlock(rw) do {              \
29432 +               au_rw_init(rw);                 \
29433 +               down_write(rw);                 \
29434 +       } while (0)
29435 +
29436 +#define au_rw_init_wlock_nested(rw, lsc) do {  \
29437 +               au_rw_init(rw);                 \
29438 +               down_write_nested(rw, lsc);     \
29439 +       } while (0)
29440 +
29441 +#define au_rw_read_lock(rw)            down_read(rw)
29442 +#define au_rw_read_lock_nested(rw, lsc)        down_read_nested(rw, lsc)
29443 +#define au_rw_read_unlock(rw)          up_read(rw)
29444 +#define au_rw_dgrade_lock(rw)          downgrade_write(rw)
29445 +#define au_rw_write_lock(rw)           down_write(rw)
29446 +#define au_rw_write_lock_nested(rw, lsc) down_write_nested(rw, lsc)
29447 +#define au_rw_write_unlock(rw)         up_write(rw)
29448 +/* why is not _nested version defined? */
29449 +#define au_rw_read_trylock(rw)         down_read_trylock(rw)
29450 +#define au_rw_write_trylock(rw)                down_write_trylock(rw)
29451 +
29452 +#endif /* __KERNEL__ */
29453 +#endif /* __AUFS_RWSEM_H__ */
29454 diff -urN /usr/share/empty/fs/aufs/sbinfo.c linux/fs/aufs/sbinfo.c
29455 --- /usr/share/empty/fs/aufs/sbinfo.c   1970-01-01 01:00:00.000000000 +0100
29456 +++ linux/fs/aufs/sbinfo.c      2022-03-21 14:49:05.729966343 +0100
29457 @@ -0,0 +1,316 @@
29458 +// SPDX-License-Identifier: GPL-2.0
29459 +/*
29460 + * Copyright (C) 2005-2021 Junjiro R. Okajima
29461 + *
29462 + * This program, aufs is free software; you can redistribute it and/or modify
29463 + * it under the terms of the GNU General Public License as published by
29464 + * the Free Software Foundation; either version 2 of the License, or
29465 + * (at your option) any later version.
29466 + *
29467 + * This program is distributed in the hope that it will be useful,
29468 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
29469 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29470 + * GNU General Public License for more details.
29471 + *
29472 + * You should have received a copy of the GNU General Public License
29473 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
29474 + */
29475 +
29476 +/*
29477 + * superblock private data
29478 + */
29479 +
29480 +#include <linux/iversion.h>
29481 +#include "aufs.h"
29482 +
29483 +/*
29484 + * they are necessary regardless sysfs is disabled.
29485 + */
29486 +void au_si_free(struct kobject *kobj)
29487 +{
29488 +       int i;
29489 +       struct au_sbinfo *sbinfo;
29490 +       char *locked __maybe_unused; /* debug only */
29491 +
29492 +       sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
29493 +       for (i = 0; i < AuPlink_NHASH; i++)
29494 +               AuDebugOn(!hlist_bl_empty(sbinfo->si_plink + i));
29495 +       AuDebugOn(atomic_read(&sbinfo->si_nowait.nw_len));
29496 +
29497 +       AuLCntZero(au_lcnt_read(&sbinfo->si_ninodes, /*do_rev*/0));
29498 +       au_lcnt_fin(&sbinfo->si_ninodes, /*do_sync*/0);
29499 +       AuLCntZero(au_lcnt_read(&sbinfo->si_nfiles, /*do_rev*/0));
29500 +       au_lcnt_fin(&sbinfo->si_nfiles, /*do_sync*/0);
29501 +
29502 +       dbgaufs_si_fin(sbinfo);
29503 +       au_rw_write_lock(&sbinfo->si_rwsem);
29504 +       au_br_free(sbinfo);
29505 +       au_rw_write_unlock(&sbinfo->si_rwsem);
29506 +
29507 +       au_kfree_try_rcu(sbinfo->si_branch);
29508 +       mutex_destroy(&sbinfo->si_xib_mtx);
29509 +       AuRwDestroy(&sbinfo->si_rwsem);
29510 +
29511 +       au_lcnt_wait_for_fin(&sbinfo->si_ninodes);
29512 +       /* si_nfiles is waited too */
29513 +       au_kfree_rcu(sbinfo);
29514 +}
29515 +
29516 +struct au_sbinfo *au_si_alloc(struct super_block *sb)
29517 +{
29518 +       struct au_sbinfo *sbinfo;
29519 +       int err, i;
29520 +
29521 +       err = -ENOMEM;
29522 +       sbinfo = kzalloc(sizeof(*sbinfo), GFP_NOFS);
29523 +       if (unlikely(!sbinfo))
29524 +               goto out;
29525 +
29526 +       /* will be reallocated separately */
29527 +       sbinfo->si_branch = kzalloc(sizeof(*sbinfo->si_branch), GFP_NOFS);
29528 +       if (unlikely(!sbinfo->si_branch))
29529 +               goto out_sbinfo;
29530 +
29531 +       err = sysaufs_si_init(sbinfo);
29532 +       if (!err) {
29533 +               dbgaufs_si_null(sbinfo);
29534 +               err = dbgaufs_si_init(sbinfo);
29535 +               if (unlikely(err))
29536 +                       kobject_put(&sbinfo->si_kobj);
29537 +       }
29538 +       if (unlikely(err))
29539 +               goto out_br;
29540 +
29541 +       au_nwt_init(&sbinfo->si_nowait);
29542 +       au_rw_init_wlock(&sbinfo->si_rwsem);
29543 +
29544 +       au_lcnt_init(&sbinfo->si_ninodes, /*release*/NULL);
29545 +       au_lcnt_init(&sbinfo->si_nfiles, /*release*/NULL);
29546 +
29547 +       sbinfo->si_bbot = -1;
29548 +       sbinfo->si_last_br_id = AUFS_BRANCH_MAX / 2;
29549 +
29550 +       sbinfo->si_wbr_copyup = AuWbrCopyup_Def;
29551 +       sbinfo->si_wbr_create = AuWbrCreate_Def;
29552 +       sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + sbinfo->si_wbr_copyup;
29553 +       sbinfo->si_wbr_create_ops = au_wbr_create_ops + sbinfo->si_wbr_create;
29554 +
29555 +       au_fhsm_init(sbinfo);
29556 +
29557 +       sbinfo->si_mntflags = au_opts_plink(AuOpt_Def);
29558 +
29559 +       sbinfo->si_xino_jiffy = jiffies;
29560 +       sbinfo->si_xino_expire
29561 +               = msecs_to_jiffies(AUFS_XINO_DEF_SEC * MSEC_PER_SEC);
29562 +       mutex_init(&sbinfo->si_xib_mtx);
29563 +       /* leave si_xib_last_pindex and si_xib_next_bit */
29564 +
29565 +       INIT_HLIST_BL_HEAD(&sbinfo->si_aopen);
29566 +
29567 +       sbinfo->si_rdcache = msecs_to_jiffies(AUFS_RDCACHE_DEF * MSEC_PER_SEC);
29568 +       sbinfo->si_rdblk = AUFS_RDBLK_DEF;
29569 +       sbinfo->si_rdhash = AUFS_RDHASH_DEF;
29570 +       sbinfo->si_dirwh = AUFS_DIRWH_DEF;
29571 +
29572 +       for (i = 0; i < AuPlink_NHASH; i++)
29573 +               INIT_HLIST_BL_HEAD(sbinfo->si_plink + i);
29574 +       init_waitqueue_head(&sbinfo->si_plink_wq);
29575 +       spin_lock_init(&sbinfo->si_plink_maint_lock);
29576 +
29577 +       INIT_HLIST_BL_HEAD(&sbinfo->si_files);
29578 +
29579 +       /* with getattr by default */
29580 +       sbinfo->si_iop_array = aufs_iop;
29581 +
29582 +       /* leave other members for sysaufs and si_mnt. */
29583 +       sbinfo->si_sb = sb;
29584 +       if (sb) {
29585 +               sb->s_fs_info = sbinfo;
29586 +               si_pid_set(sb);
29587 +       }
29588 +       return sbinfo; /* success */
29589 +
29590 +out_br:
29591 +       au_kfree_try_rcu(sbinfo->si_branch);
29592 +out_sbinfo:
29593 +       au_kfree_rcu(sbinfo);
29594 +out:
29595 +       return ERR_PTR(err);
29596 +}
29597 +
29598 +int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr, int may_shrink)
29599 +{
29600 +       int err, sz;
29601 +       struct au_branch **brp;
29602 +
29603 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
29604 +
29605 +       err = -ENOMEM;
29606 +       sz = sizeof(*brp) * (sbinfo->si_bbot + 1);
29607 +       if (unlikely(!sz))
29608 +               sz = sizeof(*brp);
29609 +       brp = au_kzrealloc(sbinfo->si_branch, sz, sizeof(*brp) * nbr, GFP_NOFS,
29610 +                          may_shrink);
29611 +       if (brp) {
29612 +               sbinfo->si_branch = brp;
29613 +               err = 0;
29614 +       }
29615 +
29616 +       return err;
29617 +}
29618 +
29619 +/* ---------------------------------------------------------------------- */
29620 +
29621 +unsigned int au_sigen_inc(struct super_block *sb)
29622 +{
29623 +       unsigned int gen;
29624 +       struct inode *inode;
29625 +
29626 +       SiMustWriteLock(sb);
29627 +
29628 +       gen = ++au_sbi(sb)->si_generation;
29629 +       au_update_digen(sb->s_root);
29630 +       inode = d_inode(sb->s_root);
29631 +       au_update_iigen(inode, /*half*/0);
29632 +       inode_inc_iversion(inode);
29633 +       return gen;
29634 +}
29635 +
29636 +aufs_bindex_t au_new_br_id(struct super_block *sb)
29637 +{
29638 +       aufs_bindex_t br_id;
29639 +       int i;
29640 +       struct au_sbinfo *sbinfo;
29641 +
29642 +       SiMustWriteLock(sb);
29643 +
29644 +       sbinfo = au_sbi(sb);
29645 +       for (i = 0; i <= AUFS_BRANCH_MAX; i++) {
29646 +               br_id = ++sbinfo->si_last_br_id;
29647 +               AuDebugOn(br_id < 0);
29648 +               if (br_id && au_br_index(sb, br_id) < 0)
29649 +                       return br_id;
29650 +       }
29651 +
29652 +       return -1;
29653 +}
29654 +
29655 +/* ---------------------------------------------------------------------- */
29656 +
29657 +/* it is ok that new 'nwt' tasks are appended while we are sleeping */
29658 +int si_read_lock(struct super_block *sb, int flags)
29659 +{
29660 +       int err;
29661 +
29662 +       err = 0;
29663 +       if (au_ftest_lock(flags, FLUSH))
29664 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
29665 +
29666 +       si_noflush_read_lock(sb);
29667 +       err = au_plink_maint(sb, flags);
29668 +       if (unlikely(err))
29669 +               si_read_unlock(sb);
29670 +
29671 +       return err;
29672 +}
29673 +
29674 +int si_write_lock(struct super_block *sb, int flags)
29675 +{
29676 +       int err;
29677 +
29678 +       if (au_ftest_lock(flags, FLUSH))
29679 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
29680 +
29681 +       si_noflush_write_lock(sb);
29682 +       err = au_plink_maint(sb, flags);
29683 +       if (unlikely(err))
29684 +               si_write_unlock(sb);
29685 +
29686 +       return err;
29687 +}
29688 +
29689 +/* dentry and super_block lock. call at entry point */
29690 +int aufs_read_lock(struct dentry *dentry, int flags)
29691 +{
29692 +       int err;
29693 +       struct super_block *sb;
29694 +
29695 +       sb = dentry->d_sb;
29696 +       err = si_read_lock(sb, flags);
29697 +       if (unlikely(err))
29698 +               goto out;
29699 +
29700 +       if (au_ftest_lock(flags, DW))
29701 +               di_write_lock_child(dentry);
29702 +       else
29703 +               di_read_lock_child(dentry, flags);
29704 +
29705 +       if (au_ftest_lock(flags, GEN)) {
29706 +               err = au_digen_test(dentry, au_sigen(sb));
29707 +               if (!au_opt_test(au_mntflags(sb), UDBA_NONE))
29708 +                       AuDebugOn(!err && au_dbrange_test(dentry));
29709 +               else if (!err)
29710 +                       err = au_dbrange_test(dentry);
29711 +               if (unlikely(err))
29712 +                       aufs_read_unlock(dentry, flags);
29713 +       }
29714 +
29715 +out:
29716 +       return err;
29717 +}
29718 +
29719 +void aufs_read_unlock(struct dentry *dentry, int flags)
29720 +{
29721 +       if (au_ftest_lock(flags, DW))
29722 +               di_write_unlock(dentry);
29723 +       else
29724 +               di_read_unlock(dentry, flags);
29725 +       si_read_unlock(dentry->d_sb);
29726 +}
29727 +
29728 +void aufs_write_lock(struct dentry *dentry)
29729 +{
29730 +       si_write_lock(dentry->d_sb, AuLock_FLUSH | AuLock_NOPLMW);
29731 +       di_write_lock_child(dentry);
29732 +}
29733 +
29734 +void aufs_write_unlock(struct dentry *dentry)
29735 +{
29736 +       di_write_unlock(dentry);
29737 +       si_write_unlock(dentry->d_sb);
29738 +}
29739 +
29740 +int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags)
29741 +{
29742 +       int err;
29743 +       unsigned int sigen;
29744 +       struct super_block *sb;
29745 +
29746 +       sb = d1->d_sb;
29747 +       err = si_read_lock(sb, flags);
29748 +       if (unlikely(err))
29749 +               goto out;
29750 +
29751 +       di_write_lock2_child(d1, d2, au_ftest_lock(flags, DIRS));
29752 +
29753 +       if (au_ftest_lock(flags, GEN)) {
29754 +               sigen = au_sigen(sb);
29755 +               err = au_digen_test(d1, sigen);
29756 +               AuDebugOn(!err && au_dbrange_test(d1));
29757 +               if (!err) {
29758 +                       err = au_digen_test(d2, sigen);
29759 +                       AuDebugOn(!err && au_dbrange_test(d2));
29760 +               }
29761 +               if (unlikely(err))
29762 +                       aufs_read_and_write_unlock2(d1, d2);
29763 +       }
29764 +
29765 +out:
29766 +       return err;
29767 +}
29768 +
29769 +void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2)
29770 +{
29771 +       di_write_unlock2(d1, d2);
29772 +       si_read_unlock(d1->d_sb);
29773 +}
29774 diff -urN /usr/share/empty/fs/aufs/super.c linux/fs/aufs/super.c
29775 --- /usr/share/empty/fs/aufs/super.c    1970-01-01 01:00:00.000000000 +0100
29776 +++ linux/fs/aufs/super.c       2022-03-21 14:49:05.729966343 +0100
29777 @@ -0,0 +1,871 @@
29778 +// SPDX-License-Identifier: GPL-2.0
29779 +/*
29780 + * Copyright (C) 2005-2021 Junjiro R. Okajima
29781 + *
29782 + * This program, aufs is free software; you can redistribute it and/or modify
29783 + * it under the terms of the GNU General Public License as published by
29784 + * the Free Software Foundation; either version 2 of the License, or
29785 + * (at your option) any later version.
29786 + *
29787 + * This program is distributed in the hope that it will be useful,
29788 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
29789 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29790 + * GNU General Public License for more details.
29791 + *
29792 + * You should have received a copy of the GNU General Public License
29793 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
29794 + */
29795 +
29796 +/*
29797 + * mount and super_block operations
29798 + */
29799 +
29800 +#include <linux/iversion.h>
29801 +#include <linux/mm.h>
29802 +#include <linux/seq_file.h>
29803 +#include <linux/statfs.h>
29804 +#include <linux/vmalloc.h>
29805 +#include "aufs.h"
29806 +
29807 +/*
29808 + * super_operations
29809 + */
29810 +static struct inode *aufs_alloc_inode(struct super_block *sb __maybe_unused)
29811 +{
29812 +       struct au_icntnr *c;
29813 +
29814 +       c = au_cache_alloc_icntnr();
29815 +       if (c) {
29816 +               au_icntnr_init(c);
29817 +               inode_set_iversion(&c->vfs_inode, 1); /* sigen(sb); */
29818 +               c->iinfo.ii_hinode = NULL;
29819 +               return &c->vfs_inode;
29820 +       }
29821 +       return NULL;
29822 +}
29823 +
29824 +static void aufs_destroy_inode(struct inode *inode)
29825 +{
29826 +       if (!au_is_bad_inode(inode))
29827 +               au_iinfo_fin(inode);
29828 +}
29829 +
29830 +static void aufs_free_inode(struct inode *inode)
29831 +{
29832 +       au_cache_free_icntnr(container_of(inode, struct au_icntnr, vfs_inode));
29833 +}
29834 +
29835 +struct inode *au_iget_locked(struct super_block *sb, ino_t ino)
29836 +{
29837 +       struct inode *inode;
29838 +       int err;
29839 +
29840 +       inode = iget_locked(sb, ino);
29841 +       if (unlikely(!inode)) {
29842 +               inode = ERR_PTR(-ENOMEM);
29843 +               goto out;
29844 +       }
29845 +       if (!(inode->i_state & I_NEW))
29846 +               goto out;
29847 +
29848 +       err = au_xigen_new(inode);
29849 +       if (!err)
29850 +               err = au_iinfo_init(inode);
29851 +       if (!err)
29852 +               inode_inc_iversion(inode);
29853 +       else {
29854 +               iget_failed(inode);
29855 +               inode = ERR_PTR(err);
29856 +       }
29857 +
29858 +out:
29859 +       /* never return NULL */
29860 +       AuDebugOn(!inode);
29861 +       AuTraceErrPtr(inode);
29862 +       return inode;
29863 +}
29864 +
29865 +/* lock free root dinfo */
29866 +static int au_show_brs(struct seq_file *seq, struct super_block *sb)
29867 +{
29868 +       int err;
29869 +       aufs_bindex_t bindex, bbot;
29870 +       struct path path;
29871 +       struct au_hdentry *hdp;
29872 +       struct au_branch *br;
29873 +       au_br_perm_str_t perm;
29874 +
29875 +       err = 0;
29876 +       bbot = au_sbbot(sb);
29877 +       bindex = 0;
29878 +       hdp = au_hdentry(au_di(sb->s_root), bindex);
29879 +       for (; !err && bindex <= bbot; bindex++, hdp++) {
29880 +               br = au_sbr(sb, bindex);
29881 +               path.mnt = au_br_mnt(br);
29882 +               path.dentry = hdp->hd_dentry;
29883 +               err = au_seq_path(seq, &path);
29884 +               if (!err) {
29885 +                       au_optstr_br_perm(&perm, br->br_perm);
29886 +                       seq_printf(seq, "=%s", perm.a);
29887 +                       if (bindex != bbot)
29888 +                               seq_putc(seq, ':');
29889 +               }
29890 +       }
29891 +       if (unlikely(err || seq_has_overflowed(seq)))
29892 +               err = -E2BIG;
29893 +
29894 +       return err;
29895 +}
29896 +
29897 +static void au_gen_fmt(char *fmt, int len __maybe_unused, const char *pat,
29898 +                      const char *append)
29899 +{
29900 +       char *p;
29901 +
29902 +       p = fmt;
29903 +       while (*pat != ':')
29904 +               *p++ = *pat++;
29905 +       *p++ = *pat++;
29906 +       strcpy(p, append);
29907 +       AuDebugOn(strlen(fmt) >= len);
29908 +}
29909 +
29910 +static void au_show_wbr_create(struct seq_file *m, int v,
29911 +                              struct au_sbinfo *sbinfo)
29912 +{
29913 +       const char *pat;
29914 +       char fmt[32];
29915 +       struct au_wbr_mfs *mfs;
29916 +
29917 +       AuRwMustAnyLock(&sbinfo->si_rwsem);
29918 +
29919 +       seq_puts(m, ",create=");
29920 +       pat = au_optstr_wbr_create(v);
29921 +       mfs = &sbinfo->si_wbr_mfs;
29922 +       switch (v) {
29923 +       case AuWbrCreate_TDP:
29924 +       case AuWbrCreate_RR:
29925 +       case AuWbrCreate_MFS:
29926 +       case AuWbrCreate_PMFS:
29927 +               seq_puts(m, pat);
29928 +               break;
29929 +       case AuWbrCreate_MFSRR:
29930 +       case AuWbrCreate_TDMFS:
29931 +       case AuWbrCreate_PMFSRR:
29932 +               au_gen_fmt(fmt, sizeof(fmt), pat, "%llu");
29933 +               seq_printf(m, fmt, mfs->mfsrr_watermark);
29934 +               break;
29935 +       case AuWbrCreate_MFSV:
29936 +       case AuWbrCreate_PMFSV:
29937 +               au_gen_fmt(fmt, sizeof(fmt), pat, "%lu");
29938 +               seq_printf(m, fmt,
29939 +                          jiffies_to_msecs(mfs->mfs_expire)
29940 +                          / MSEC_PER_SEC);
29941 +               break;
29942 +       case AuWbrCreate_MFSRRV:
29943 +       case AuWbrCreate_TDMFSV:
29944 +       case AuWbrCreate_PMFSRRV:
29945 +               au_gen_fmt(fmt, sizeof(fmt), pat, "%llu:%lu");
29946 +               seq_printf(m, fmt, mfs->mfsrr_watermark,
29947 +                          jiffies_to_msecs(mfs->mfs_expire) / MSEC_PER_SEC);
29948 +               break;
29949 +       default:
29950 +               BUG();
29951 +       }
29952 +}
29953 +
29954 +static int au_show_xino(struct seq_file *seq, struct super_block *sb)
29955 +{
29956 +#ifdef CONFIG_SYSFS
29957 +       return 0;
29958 +#else
29959 +       int err;
29960 +       const int len = sizeof(AUFS_XINO_FNAME) - 1;
29961 +       aufs_bindex_t bindex, brid;
29962 +       struct qstr *name;
29963 +       struct file *f;
29964 +       struct dentry *d, *h_root;
29965 +       struct au_branch *br;
29966 +
29967 +       AuRwMustAnyLock(&sbinfo->si_rwsem);
29968 +
29969 +       err = 0;
29970 +       f = au_sbi(sb)->si_xib;
29971 +       if (!f)
29972 +               goto out;
29973 +
29974 +       /* stop printing the default xino path on the first writable branch */
29975 +       h_root = NULL;
29976 +       bindex = au_xi_root(sb, f->f_path.dentry);
29977 +       if (bindex >= 0) {
29978 +               br = au_sbr_sb(sb, bindex);
29979 +               h_root = au_br_dentry(br);
29980 +       }
29981 +
29982 +       d = f->f_path.dentry;
29983 +       name = &d->d_name;
29984 +       /* safe ->d_parent because the file is unlinked */
29985 +       if (d->d_parent == h_root
29986 +           && name->len == len
29987 +           && !memcmp(name->name, AUFS_XINO_FNAME, len))
29988 +               goto out;
29989 +
29990 +       seq_puts(seq, ",xino=");
29991 +       err = au_xino_path(seq, f);
29992 +
29993 +out:
29994 +       return err;
29995 +#endif
29996 +}
29997 +
29998 +/* seq_file will re-call me in case of too long string */
29999 +static int aufs_show_options(struct seq_file *m, struct dentry *dentry)
30000 +{
30001 +       int err;
30002 +       unsigned int mnt_flags, v;
30003 +       struct super_block *sb;
30004 +       struct au_sbinfo *sbinfo;
30005 +
30006 +#define AuBool(name, str) do { \
30007 +       v = au_opt_test(mnt_flags, name); \
30008 +       if (v != au_opt_test(AuOpt_Def, name)) \
30009 +               seq_printf(m, ",%s" #str, v ? "" : "no"); \
30010 +} while (0)
30011 +
30012 +#define AuStr(name, str) do { \
30013 +       v = mnt_flags & AuOptMask_##name; \
30014 +       if (v != (AuOpt_Def & AuOptMask_##name)) \
30015 +               seq_printf(m, "," #str "=%s", au_optstr_##str(v)); \
30016 +} while (0)
30017 +
30018 +#define AuUInt(name, str, val) do { \
30019 +       if (val != AUFS_##name##_DEF) \
30020 +               seq_printf(m, "," #str "=%u", val); \
30021 +} while (0)
30022 +
30023 +       sb = dentry->d_sb;
30024 +       if (sb->s_flags & SB_POSIXACL)
30025 +               seq_puts(m, ",acl");
30026 +#if 0 /* reserved for future use */
30027 +       if (sb->s_flags & SB_I_VERSION)
30028 +               seq_puts(m, ",i_version");
30029 +#endif
30030 +
30031 +       /* lock free root dinfo */
30032 +       si_noflush_read_lock(sb);
30033 +       sbinfo = au_sbi(sb);
30034 +       seq_printf(m, ",si=%lx", sysaufs_si_id(sbinfo));
30035 +
30036 +       mnt_flags = au_mntflags(sb);
30037 +       if (au_opt_test(mnt_flags, XINO)) {
30038 +               err = au_show_xino(m, sb);
30039 +               if (unlikely(err))
30040 +                       goto out;
30041 +       } else
30042 +               seq_puts(m, ",noxino");
30043 +
30044 +       AuBool(TRUNC_XINO, trunc_xino);
30045 +       AuStr(UDBA, udba);
30046 +       AuBool(SHWH, shwh);
30047 +       AuBool(PLINK, plink);
30048 +       AuBool(DIO, dio);
30049 +       AuBool(DIRPERM1, dirperm1);
30050 +
30051 +       v = sbinfo->si_wbr_create;
30052 +       if (v != AuWbrCreate_Def)
30053 +               au_show_wbr_create(m, v, sbinfo);
30054 +
30055 +       v = sbinfo->si_wbr_copyup;
30056 +       if (v != AuWbrCopyup_Def)
30057 +               seq_printf(m, ",cpup=%s", au_optstr_wbr_copyup(v));
30058 +
30059 +       v = au_opt_test(mnt_flags, ALWAYS_DIROPQ);
30060 +       if (v != au_opt_test(AuOpt_Def, ALWAYS_DIROPQ))
30061 +               seq_printf(m, ",diropq=%c", v ? 'a' : 'w');
30062 +
30063 +       AuUInt(DIRWH, dirwh, sbinfo->si_dirwh);
30064 +
30065 +       v = jiffies_to_msecs(sbinfo->si_rdcache) / MSEC_PER_SEC;
30066 +       AuUInt(RDCACHE, rdcache, v);
30067 +
30068 +       AuUInt(RDBLK, rdblk, sbinfo->si_rdblk);
30069 +       AuUInt(RDHASH, rdhash, sbinfo->si_rdhash);
30070 +
30071 +       au_fhsm_show(m, sbinfo);
30072 +
30073 +       AuBool(DIRREN, dirren);
30074 +       AuBool(SUM, sum);
30075 +       /* AuBool(SUM_W, wsum); */
30076 +       AuBool(WARN_PERM, warn_perm);
30077 +       AuBool(VERBOSE, verbose);
30078 +
30079 +out:
30080 +       /* be sure to print "br:" last */
30081 +       if (!sysaufs_brs) {
30082 +               seq_puts(m, ",br:");
30083 +               au_show_brs(m, sb);
30084 +       }
30085 +       si_read_unlock(sb);
30086 +       return 0;
30087 +
30088 +#undef AuBool
30089 +#undef AuStr
30090 +#undef AuUInt
30091 +}
30092 +
30093 +/* ---------------------------------------------------------------------- */
30094 +
30095 +/* sum mode which returns the summation for statfs(2) */
30096 +
30097 +static u64 au_add_till_max(u64 a, u64 b)
30098 +{
30099 +       u64 old;
30100 +
30101 +       old = a;
30102 +       a += b;
30103 +       if (old <= a)
30104 +               return a;
30105 +       return ULLONG_MAX;
30106 +}
30107 +
30108 +static u64 au_mul_till_max(u64 a, long mul)
30109 +{
30110 +       u64 old;
30111 +
30112 +       old = a;
30113 +       a *= mul;
30114 +       if (old <= a)
30115 +               return a;
30116 +       return ULLONG_MAX;
30117 +}
30118 +
30119 +static int au_statfs_sum(struct super_block *sb, struct kstatfs *buf)
30120 +{
30121 +       int err;
30122 +       long bsize, factor;
30123 +       u64 blocks, bfree, bavail, files, ffree;
30124 +       aufs_bindex_t bbot, bindex, i;
30125 +       unsigned char shared;
30126 +       struct path h_path;
30127 +       struct super_block *h_sb;
30128 +
30129 +       err = 0;
30130 +       bsize = LONG_MAX;
30131 +       files = 0;
30132 +       ffree = 0;
30133 +       blocks = 0;
30134 +       bfree = 0;
30135 +       bavail = 0;
30136 +       bbot = au_sbbot(sb);
30137 +       for (bindex = 0; bindex <= bbot; bindex++) {
30138 +               h_path.mnt = au_sbr_mnt(sb, bindex);
30139 +               h_sb = h_path.mnt->mnt_sb;
30140 +               shared = 0;
30141 +               for (i = 0; !shared && i < bindex; i++)
30142 +                       shared = (au_sbr_sb(sb, i) == h_sb);
30143 +               if (shared)
30144 +                       continue;
30145 +
30146 +               /* sb->s_root for NFS is unreliable */
30147 +               h_path.dentry = h_path.mnt->mnt_root;
30148 +               err = vfs_statfs(&h_path, buf);
30149 +               if (unlikely(err))
30150 +                       goto out;
30151 +
30152 +               if (bsize > buf->f_bsize) {
30153 +                       /*
30154 +                        * we will reduce bsize, so we have to expand blocks
30155 +                        * etc. to match them again
30156 +                        */
30157 +                       factor = (bsize / buf->f_bsize);
30158 +                       blocks = au_mul_till_max(blocks, factor);
30159 +                       bfree = au_mul_till_max(bfree, factor);
30160 +                       bavail = au_mul_till_max(bavail, factor);
30161 +                       bsize = buf->f_bsize;
30162 +               }
30163 +
30164 +               factor = (buf->f_bsize / bsize);
30165 +               blocks = au_add_till_max(blocks,
30166 +                               au_mul_till_max(buf->f_blocks, factor));
30167 +               bfree = au_add_till_max(bfree,
30168 +                               au_mul_till_max(buf->f_bfree, factor));
30169 +               bavail = au_add_till_max(bavail,
30170 +                               au_mul_till_max(buf->f_bavail, factor));
30171 +               files = au_add_till_max(files, buf->f_files);
30172 +               ffree = au_add_till_max(ffree, buf->f_ffree);
30173 +       }
30174 +
30175 +       buf->f_bsize = bsize;
30176 +       buf->f_blocks = blocks;
30177 +       buf->f_bfree = bfree;
30178 +       buf->f_bavail = bavail;
30179 +       buf->f_files = files;
30180 +       buf->f_ffree = ffree;
30181 +       buf->f_frsize = 0;
30182 +
30183 +out:
30184 +       return err;
30185 +}
30186 +
30187 +static int aufs_statfs(struct dentry *dentry, struct kstatfs *buf)
30188 +{
30189 +       int err;
30190 +       struct path h_path;
30191 +       struct super_block *sb;
30192 +
30193 +       /* lock free root dinfo */
30194 +       sb = dentry->d_sb;
30195 +       si_noflush_read_lock(sb);
30196 +       if (!au_opt_test(au_mntflags(sb), SUM)) {
30197 +               /* sb->s_root for NFS is unreliable */
30198 +               h_path.mnt = au_sbr_mnt(sb, 0);
30199 +               h_path.dentry = h_path.mnt->mnt_root;
30200 +               err = vfs_statfs(&h_path, buf);
30201 +       } else
30202 +               err = au_statfs_sum(sb, buf);
30203 +       si_read_unlock(sb);
30204 +
30205 +       if (!err) {
30206 +               buf->f_type = AUFS_SUPER_MAGIC;
30207 +               buf->f_namelen = AUFS_MAX_NAMELEN;
30208 +               memset(&buf->f_fsid, 0, sizeof(buf->f_fsid));
30209 +       }
30210 +       /* buf->f_bsize = buf->f_blocks = buf->f_bfree = buf->f_bavail = -1; */
30211 +
30212 +       return err;
30213 +}
30214 +
30215 +/* ---------------------------------------------------------------------- */
30216 +
30217 +static int aufs_sync_fs(struct super_block *sb, int wait)
30218 +{
30219 +       int err, e;
30220 +       aufs_bindex_t bbot, bindex;
30221 +       struct au_branch *br;
30222 +       struct super_block *h_sb;
30223 +
30224 +       err = 0;
30225 +       si_noflush_read_lock(sb);
30226 +       bbot = au_sbbot(sb);
30227 +       for (bindex = 0; bindex <= bbot; bindex++) {
30228 +               br = au_sbr(sb, bindex);
30229 +               if (!au_br_writable(br->br_perm))
30230 +                       continue;
30231 +
30232 +               h_sb = au_sbr_sb(sb, bindex);
30233 +               e = vfsub_sync_filesystem(h_sb);
30234 +               if (unlikely(e && !err))
30235 +                       err = e;
30236 +               /* go on even if an error happens */
30237 +       }
30238 +       si_read_unlock(sb);
30239 +
30240 +       return err;
30241 +}
30242 +
30243 +/* ---------------------------------------------------------------------- */
30244 +
30245 +/* final actions when unmounting a file system */
30246 +static void aufs_put_super(struct super_block *sb)
30247 +{
30248 +       struct au_sbinfo *sbinfo;
30249 +
30250 +       sbinfo = au_sbi(sb);
30251 +       if (sbinfo)
30252 +               kobject_put(&sbinfo->si_kobj);
30253 +}
30254 +
30255 +/* ---------------------------------------------------------------------- */
30256 +
30257 +void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb,
30258 +                    struct super_block *sb, void *arg)
30259 +{
30260 +       void *array;
30261 +       unsigned long long n, sz;
30262 +
30263 +       array = NULL;
30264 +       n = 0;
30265 +       if (!*hint)
30266 +               goto out;
30267 +
30268 +       if (*hint > ULLONG_MAX / sizeof(array)) {
30269 +               array = ERR_PTR(-EMFILE);
30270 +               pr_err("hint %llu\n", *hint);
30271 +               goto out;
30272 +       }
30273 +
30274 +       sz = sizeof(array) * *hint;
30275 +       array = kzalloc(sz, GFP_NOFS);
30276 +       if (unlikely(!array))
30277 +               array = vzalloc(sz);
30278 +       if (unlikely(!array)) {
30279 +               array = ERR_PTR(-ENOMEM);
30280 +               goto out;
30281 +       }
30282 +
30283 +       n = cb(sb, array, *hint, arg);
30284 +       AuDebugOn(n > *hint);
30285 +
30286 +out:
30287 +       *hint = n;
30288 +       return array;
30289 +}
30290 +
30291 +static unsigned long long au_iarray_cb(struct super_block *sb, void *a,
30292 +                                      unsigned long long max __maybe_unused,
30293 +                                      void *arg)
30294 +{
30295 +       unsigned long long n;
30296 +       struct inode **p, *inode;
30297 +       struct list_head *head;
30298 +
30299 +       n = 0;
30300 +       p = a;
30301 +       head = arg;
30302 +       spin_lock(&sb->s_inode_list_lock);
30303 +       list_for_each_entry(inode, head, i_sb_list) {
30304 +               if (!au_is_bad_inode(inode)
30305 +                   && au_ii(inode)->ii_btop >= 0) {
30306 +                       spin_lock(&inode->i_lock);
30307 +                       if (atomic_read(&inode->i_count)) {
30308 +                               au_igrab(inode);
30309 +                               *p++ = inode;
30310 +                               n++;
30311 +                               AuDebugOn(n > max);
30312 +                       }
30313 +                       spin_unlock(&inode->i_lock);
30314 +               }
30315 +       }
30316 +       spin_unlock(&sb->s_inode_list_lock);
30317 +
30318 +       return n;
30319 +}
30320 +
30321 +struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max)
30322 +{
30323 +       struct au_sbinfo *sbi;
30324 +
30325 +       sbi = au_sbi(sb);
30326 +       *max = au_lcnt_read(&sbi->si_ninodes, /*do_rev*/1);
30327 +       return au_array_alloc(max, au_iarray_cb, sb, &sb->s_inodes);
30328 +}
30329 +
30330 +void au_iarray_free(struct inode **a, unsigned long long max)
30331 +{
30332 +       unsigned long long ull;
30333 +
30334 +       for (ull = 0; ull < max; ull++)
30335 +               iput(a[ull]);
30336 +       kvfree(a);
30337 +}
30338 +
30339 +/* ---------------------------------------------------------------------- */
30340 +
30341 +/*
30342 + * refresh dentry and inode at remount time.
30343 + */
30344 +/* todo: consolidate with simple_reval_dpath() and au_reval_for_attr() */
30345 +static int au_do_refresh(struct dentry *dentry, unsigned int dir_flags,
30346 +                     struct dentry *parent)
30347 +{
30348 +       int err;
30349 +
30350 +       di_write_lock_child(dentry);
30351 +       di_read_lock_parent(parent, AuLock_IR);
30352 +       err = au_refresh_dentry(dentry, parent);
30353 +       if (!err && dir_flags)
30354 +               au_hn_reset(d_inode(dentry), dir_flags);
30355 +       di_read_unlock(parent, AuLock_IR);
30356 +       di_write_unlock(dentry);
30357 +
30358 +       return err;
30359 +}
30360 +
30361 +static int au_do_refresh_d(struct dentry *dentry, unsigned int sigen,
30362 +                          struct au_sbinfo *sbinfo,
30363 +                          const unsigned int dir_flags, unsigned int do_idop)
30364 +{
30365 +       int err;
30366 +       struct dentry *parent;
30367 +
30368 +       err = 0;
30369 +       parent = dget_parent(dentry);
30370 +       if (!au_digen_test(parent, sigen) && au_digen_test(dentry, sigen)) {
30371 +               if (d_really_is_positive(dentry)) {
30372 +                       if (!d_is_dir(dentry))
30373 +                               err = au_do_refresh(dentry, /*dir_flags*/0,
30374 +                                                parent);
30375 +                       else {
30376 +                               err = au_do_refresh(dentry, dir_flags, parent);
30377 +                               if (unlikely(err))
30378 +                                       au_fset_si(sbinfo, FAILED_REFRESH_DIR);
30379 +                       }
30380 +               } else
30381 +                       err = au_do_refresh(dentry, /*dir_flags*/0, parent);
30382 +               AuDbgDentry(dentry);
30383 +       }
30384 +       dput(parent);
30385 +
30386 +       if (!err) {
30387 +               if (do_idop)
30388 +                       au_refresh_dop(dentry, /*force_reval*/0);
30389 +       } else
30390 +               au_refresh_dop(dentry, /*force_reval*/1);
30391 +
30392 +       AuTraceErr(err);
30393 +       return err;
30394 +}
30395 +
30396 +static int au_refresh_d(struct super_block *sb, unsigned int do_idop)
30397 +{
30398 +       int err, i, j, ndentry, e;
30399 +       unsigned int sigen;
30400 +       struct au_dcsub_pages dpages;
30401 +       struct au_dpage *dpage;
30402 +       struct dentry **dentries, *d;
30403 +       struct au_sbinfo *sbinfo;
30404 +       struct dentry *root = sb->s_root;
30405 +       const unsigned int dir_flags = au_hi_flags(d_inode(root), /*isdir*/1);
30406 +
30407 +       if (do_idop)
30408 +               au_refresh_dop(root, /*force_reval*/0);
30409 +
30410 +       err = au_dpages_init(&dpages, GFP_NOFS);
30411 +       if (unlikely(err))
30412 +               goto out;
30413 +       err = au_dcsub_pages(&dpages, root, NULL, NULL);
30414 +       if (unlikely(err))
30415 +               goto out_dpages;
30416 +
30417 +       sigen = au_sigen(sb);
30418 +       sbinfo = au_sbi(sb);
30419 +       for (i = 0; i < dpages.ndpage; i++) {
30420 +               dpage = dpages.dpages + i;
30421 +               dentries = dpage->dentries;
30422 +               ndentry = dpage->ndentry;
30423 +               for (j = 0; j < ndentry; j++) {
30424 +                       d = dentries[j];
30425 +                       e = au_do_refresh_d(d, sigen, sbinfo, dir_flags,
30426 +                                           do_idop);
30427 +                       if (unlikely(e && !err))
30428 +                               err = e;
30429 +                       /* go on even err */
30430 +               }
30431 +       }
30432 +
30433 +out_dpages:
30434 +       au_dpages_free(&dpages);
30435 +out:
30436 +       return err;
30437 +}
30438 +
30439 +static int au_refresh_i(struct super_block *sb, unsigned int do_idop)
30440 +{
30441 +       int err, e;
30442 +       unsigned int sigen;
30443 +       unsigned long long max, ull;
30444 +       struct inode *inode, **array;
30445 +
30446 +       array = au_iarray_alloc(sb, &max);
30447 +       err = PTR_ERR(array);
30448 +       if (IS_ERR(array))
30449 +               goto out;
30450 +
30451 +       err = 0;
30452 +       sigen = au_sigen(sb);
30453 +       for (ull = 0; ull < max; ull++) {
30454 +               inode = array[ull];
30455 +               if (unlikely(!inode))
30456 +                       break;
30457 +
30458 +               e = 0;
30459 +               ii_write_lock_child(inode);
30460 +               if (au_iigen(inode, NULL) != sigen) {
30461 +                       e = au_refresh_hinode_self(inode);
30462 +                       if (unlikely(e)) {
30463 +                               au_refresh_iop(inode, /*force_getattr*/1);
30464 +                               pr_err("error %d, i%lu\n", e, inode->i_ino);
30465 +                               if (!err)
30466 +                                       err = e;
30467 +                               /* go on even if err */
30468 +                       }
30469 +               }
30470 +               if (!e && do_idop)
30471 +                       au_refresh_iop(inode, /*force_getattr*/0);
30472 +               ii_write_unlock(inode);
30473 +       }
30474 +
30475 +       au_iarray_free(array, max);
30476 +
30477 +out:
30478 +       return err;
30479 +}
30480 +
30481 +void au_remount_refresh(struct super_block *sb, unsigned int do_idop)
30482 +{
30483 +       int err, e;
30484 +       unsigned int udba;
30485 +       aufs_bindex_t bindex, bbot;
30486 +       struct dentry *root;
30487 +       struct inode *inode;
30488 +       struct au_branch *br;
30489 +       struct au_sbinfo *sbi;
30490 +
30491 +       au_sigen_inc(sb);
30492 +       sbi = au_sbi(sb);
30493 +       au_fclr_si(sbi, FAILED_REFRESH_DIR);
30494 +
30495 +       root = sb->s_root;
30496 +       DiMustNoWaiters(root);
30497 +       inode = d_inode(root);
30498 +       IiMustNoWaiters(inode);
30499 +
30500 +       udba = au_opt_udba(sb);
30501 +       bbot = au_sbbot(sb);
30502 +       for (bindex = 0; bindex <= bbot; bindex++) {
30503 +               br = au_sbr(sb, bindex);
30504 +               err = au_hnotify_reset_br(udba, br, br->br_perm);
30505 +               if (unlikely(err))
30506 +                       AuIOErr("hnotify failed on br %d, %d, ignored\n",
30507 +                               bindex, err);
30508 +               /* go on even if err */
30509 +       }
30510 +       au_hn_reset(inode, au_hi_flags(inode, /*isdir*/1));
30511 +
30512 +       if (do_idop) {
30513 +               if (au_ftest_si(sbi, NO_DREVAL)) {
30514 +                       AuDebugOn(sb->s_d_op == &aufs_dop_noreval);
30515 +                       sb->s_d_op = &aufs_dop_noreval;
30516 +                       AuDebugOn(sbi->si_iop_array == aufs_iop_nogetattr);
30517 +                       sbi->si_iop_array = aufs_iop_nogetattr;
30518 +               } else {
30519 +                       AuDebugOn(sb->s_d_op == &aufs_dop);
30520 +                       sb->s_d_op = &aufs_dop;
30521 +                       AuDebugOn(sbi->si_iop_array == aufs_iop);
30522 +                       sbi->si_iop_array = aufs_iop;
30523 +               }
30524 +               pr_info("reset to %ps and %ps\n",
30525 +                       sb->s_d_op, sbi->si_iop_array);
30526 +       }
30527 +
30528 +       di_write_unlock(root);
30529 +       err = au_refresh_d(sb, do_idop);
30530 +       e = au_refresh_i(sb, do_idop);
30531 +       if (unlikely(e && !err))
30532 +               err = e;
30533 +       /* aufs_write_lock() calls ..._child() */
30534 +       di_write_lock_child(root);
30535 +
30536 +       au_cpup_attr_all(inode, /*force*/1);
30537 +
30538 +       if (unlikely(err))
30539 +               AuIOErr("refresh failed, ignored, %d\n", err);
30540 +}
30541 +
30542 +const struct super_operations aufs_sop = {
30543 +       .alloc_inode    = aufs_alloc_inode,
30544 +       .destroy_inode  = aufs_destroy_inode,
30545 +       .free_inode     = aufs_free_inode,
30546 +       /* always deleting, no clearing */
30547 +       .drop_inode     = generic_delete_inode,
30548 +       .show_options   = aufs_show_options,
30549 +       .statfs         = aufs_statfs,
30550 +       .put_super      = aufs_put_super,
30551 +       .sync_fs        = aufs_sync_fs
30552 +};
30553 +
30554 +/* ---------------------------------------------------------------------- */
30555 +
30556 +int au_alloc_root(struct super_block *sb)
30557 +{
30558 +       int err;
30559 +       struct inode *inode;
30560 +       struct dentry *root;
30561 +
30562 +       err = -ENOMEM;
30563 +       inode = au_iget_locked(sb, AUFS_ROOT_INO);
30564 +       err = PTR_ERR(inode);
30565 +       if (IS_ERR(inode))
30566 +               goto out;
30567 +
30568 +       inode->i_op = aufs_iop + AuIop_DIR; /* with getattr by default */
30569 +       inode->i_fop = &aufs_dir_fop;
30570 +       inode->i_mode = S_IFDIR;
30571 +       set_nlink(inode, 2);
30572 +       unlock_new_inode(inode);
30573 +
30574 +       root = d_make_root(inode);
30575 +       if (unlikely(!root))
30576 +               goto out;
30577 +       err = PTR_ERR(root);
30578 +       if (IS_ERR(root))
30579 +               goto out;
30580 +
30581 +       err = au_di_init(root);
30582 +       if (!err) {
30583 +               sb->s_root = root;
30584 +               return 0; /* success */
30585 +       }
30586 +       dput(root);
30587 +
30588 +out:
30589 +       return err;
30590 +}
30591 +
30592 +/* ---------------------------------------------------------------------- */
30593 +
30594 +static void aufs_kill_sb(struct super_block *sb)
30595 +{
30596 +       struct au_sbinfo *sbinfo;
30597 +       struct dentry *root;
30598 +
30599 +       sbinfo = au_sbi(sb);
30600 +       if (!sbinfo)
30601 +               goto out;
30602 +
30603 +       au_sbilist_del(sb);
30604 +
30605 +       root = sb->s_root;
30606 +       if (root)
30607 +               aufs_write_lock(root);
30608 +       else
30609 +               __si_write_lock(sb);
30610 +
30611 +       au_fhsm_fin(sb);
30612 +       if (sbinfo->si_wbr_create_ops->fin)
30613 +               sbinfo->si_wbr_create_ops->fin(sb);
30614 +       if (au_opt_test(sbinfo->si_mntflags, UDBA_HNOTIFY)) {
30615 +               au_opt_set_udba(sbinfo->si_mntflags, UDBA_NONE);
30616 +               au_remount_refresh(sb, /*do_idop*/0);
30617 +       }
30618 +       if (au_opt_test(sbinfo->si_mntflags, PLINK))
30619 +               au_plink_put(sb, /*verbose*/1);
30620 +       au_xino_clr(sb);
30621 +       if (root)
30622 +               au_dr_opt_flush(sb);
30623 +
30624 +       if (root)
30625 +               aufs_write_unlock(root);
30626 +       else
30627 +               __si_write_unlock(sb);
30628 +
30629 +       sbinfo->si_sb = NULL;
30630 +       au_nwt_flush(&sbinfo->si_nowait);
30631 +
30632 +out:
30633 +       kill_anon_super(sb);
30634 +}
30635 +
30636 +struct file_system_type aufs_fs_type = {
30637 +       .name           = AUFS_FSTYPE,
30638 +       /* a race between rename and others */
30639 +       .fs_flags       = FS_RENAME_DOES_D_MOVE
30640 +                               /* untested */
30641 +                               /*| FS_ALLOW_IDMAP*/
30642 +                               ,
30643 +       .init_fs_context = aufs_fsctx_init,
30644 +       .parameters     = aufs_fsctx_paramspec,
30645 +       .kill_sb        = aufs_kill_sb,
30646 +       /* no need to __module_get() and module_put(). */
30647 +       .owner          = THIS_MODULE,
30648 +};
30649 diff -urN /usr/share/empty/fs/aufs/super.h linux/fs/aufs/super.h
30650 --- /usr/share/empty/fs/aufs/super.h    1970-01-01 01:00:00.000000000 +0100
30651 +++ linux/fs/aufs/super.h       2022-03-21 14:49:05.729966343 +0100
30652 @@ -0,0 +1,592 @@
30653 +/* SPDX-License-Identifier: GPL-2.0 */
30654 +/*
30655 + * Copyright (C) 2005-2021 Junjiro R. Okajima
30656 + *
30657 + * This program, aufs is free software; you can redistribute it and/or modify
30658 + * it under the terms of the GNU General Public License as published by
30659 + * the Free Software Foundation; either version 2 of the License, or
30660 + * (at your option) any later version.
30661 + *
30662 + * This program is distributed in the hope that it will be useful,
30663 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
30664 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30665 + * GNU General Public License for more details.
30666 + *
30667 + * You should have received a copy of the GNU General Public License
30668 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
30669 + */
30670 +
30671 +/*
30672 + * super_block operations
30673 + */
30674 +
30675 +#ifndef __AUFS_SUPER_H__
30676 +#define __AUFS_SUPER_H__
30677 +
30678 +#ifdef __KERNEL__
30679 +
30680 +#include <linux/fs.h>
30681 +#include <linux/kobject.h>
30682 +#include "hbl.h"
30683 +#include "lcnt.h"
30684 +#include "rwsem.h"
30685 +#include "wkq.h"
30686 +
30687 +/* policies to select one among multiple writable branches */
30688 +struct au_wbr_copyup_operations {
30689 +       int (*copyup)(struct dentry *dentry);
30690 +};
30691 +
30692 +#define AuWbr_DIR      1               /* target is a dir */
30693 +#define AuWbr_PARENT   (1 << 1)        /* always require a parent */
30694 +
30695 +#define au_ftest_wbr(flags, name)      ((flags) & AuWbr_##name)
30696 +#define au_fset_wbr(flags, name)       { (flags) |= AuWbr_##name; }
30697 +#define au_fclr_wbr(flags, name)       { (flags) &= ~AuWbr_##name; }
30698 +
30699 +struct au_wbr_create_operations {
30700 +       int (*create)(struct dentry *dentry, unsigned int flags);
30701 +       int (*init)(struct super_block *sb);
30702 +       int (*fin)(struct super_block *sb);
30703 +};
30704 +
30705 +struct au_wbr_mfs {
30706 +       struct mutex    mfs_lock; /* protect this structure */
30707 +       unsigned long   mfs_jiffy;
30708 +       unsigned long   mfs_expire;
30709 +       aufs_bindex_t   mfs_bindex;
30710 +
30711 +       unsigned long long      mfsrr_bytes;
30712 +       unsigned long long      mfsrr_watermark;
30713 +};
30714 +
30715 +#define AuPlink_NHASH 100
30716 +static inline int au_plink_hash(ino_t ino)
30717 +{
30718 +       return ino % AuPlink_NHASH;
30719 +}
30720 +
30721 +/* File-based Hierarchical Storage Management */
30722 +struct au_fhsm {
30723 +#ifdef CONFIG_AUFS_FHSM
30724 +       /* allow only one process who can receive the notification */
30725 +       spinlock_t              fhsm_spin;
30726 +       pid_t                   fhsm_pid;
30727 +       wait_queue_head_t       fhsm_wqh;
30728 +       atomic_t                fhsm_readable;
30729 +
30730 +       /* these are protected by si_rwsem */
30731 +       unsigned long           fhsm_expire;
30732 +       aufs_bindex_t           fhsm_bottom;
30733 +#endif
30734 +};
30735 +
30736 +struct au_branch;
30737 +struct au_sbinfo {
30738 +       /* nowait tasks in the system-wide workqueue */
30739 +       struct au_nowait_tasks  si_nowait;
30740 +
30741 +       /*
30742 +        * tried sb->s_umount, but failed due to the dependency between i_mutex.
30743 +        * rwsem for au_sbinfo is necessary.
30744 +        */
30745 +       struct au_rwsem         si_rwsem;
30746 +
30747 +       /*
30748 +        * dirty approach to protect sb->sb_inodes and ->s_files (gone) from
30749 +        * remount.
30750 +        */
30751 +       au_lcnt_t               si_ninodes, si_nfiles;
30752 +
30753 +       /* branch management */
30754 +       unsigned int            si_generation;
30755 +
30756 +       /* see AuSi_ flags */
30757 +       unsigned char           au_si_status;
30758 +
30759 +       aufs_bindex_t           si_bbot;
30760 +
30761 +       /* dirty trick to keep br_id plus */
30762 +       unsigned int            si_last_br_id :
30763 +                               sizeof(aufs_bindex_t) * BITS_PER_BYTE - 1;
30764 +       struct au_branch        **si_branch;
30765 +
30766 +       /* policy to select a writable branch */
30767 +       unsigned char           si_wbr_copyup;
30768 +       unsigned char           si_wbr_create;
30769 +       struct au_wbr_copyup_operations *si_wbr_copyup_ops;
30770 +       struct au_wbr_create_operations *si_wbr_create_ops;
30771 +
30772 +       /* round robin */
30773 +       atomic_t                si_wbr_rr_next;
30774 +
30775 +       /* most free space */
30776 +       struct au_wbr_mfs       si_wbr_mfs;
30777 +
30778 +       /* File-based Hierarchical Storage Management */
30779 +       struct au_fhsm          si_fhsm;
30780 +
30781 +       /* mount flags */
30782 +       /* include/asm-ia64/siginfo.h defines a macro named si_flags */
30783 +       unsigned int            si_mntflags;
30784 +
30785 +       /* external inode number (bitmap and translation table) */
30786 +       loff_t                  si_ximaxent;    /* max entries in a xino */
30787 +
30788 +       struct file             *si_xib;
30789 +       struct mutex            si_xib_mtx; /* protect xib members */
30790 +       unsigned long           *si_xib_buf;
30791 +       unsigned long           si_xib_last_pindex;
30792 +       int                     si_xib_next_bit;
30793 +
30794 +       unsigned long           si_xino_jiffy;
30795 +       unsigned long           si_xino_expire;
30796 +       /* reserved for future use */
30797 +       /* unsigned long long   si_xib_limit; */        /* Max xib file size */
30798 +
30799 +#ifdef CONFIG_AUFS_EXPORT
30800 +       /* i_generation */
30801 +       /* todo: make xigen file an array to support many inode numbers */
30802 +       struct file             *si_xigen;
30803 +       atomic_t                si_xigen_next;
30804 +#endif
30805 +
30806 +       /* dirty trick to support atomic_open */
30807 +       struct hlist_bl_head    si_aopen;
30808 +
30809 +       /* vdir parameters */
30810 +       unsigned long           si_rdcache;     /* max cache time in jiffies */
30811 +       unsigned int            si_rdblk;       /* deblk size */
30812 +       unsigned int            si_rdhash;      /* hash size */
30813 +
30814 +       /*
30815 +        * If the number of whiteouts are larger than si_dirwh, leave all of
30816 +        * them after au_whtmp_ren to reduce the cost of rmdir(2).
30817 +        * future fsck.aufs or kernel thread will remove them later.
30818 +        * Otherwise, remove all whiteouts and the dir in rmdir(2).
30819 +        */
30820 +       unsigned int            si_dirwh;
30821 +
30822 +       /* pseudo_link list */
30823 +       struct hlist_bl_head    si_plink[AuPlink_NHASH];
30824 +       wait_queue_head_t       si_plink_wq;
30825 +       spinlock_t              si_plink_maint_lock;
30826 +       pid_t                   si_plink_maint_pid;
30827 +
30828 +       /* file list */
30829 +       struct hlist_bl_head    si_files;
30830 +
30831 +       /* with/without getattr, brother of sb->s_d_op */
30832 +       const struct inode_operations *si_iop_array;
30833 +
30834 +       /*
30835 +        * sysfs and lifetime management.
30836 +        * this is not a small structure and it may be a waste of memory in case
30837 +        * of sysfs is disabled, particularly when many aufs-es are mounted.
30838 +        * but using sysfs is majority.
30839 +        */
30840 +       struct kobject          si_kobj;
30841 +#ifdef CONFIG_DEBUG_FS
30842 +       struct dentry            *si_dbgaufs;
30843 +       struct dentry            *si_dbgaufs_plink;
30844 +       struct dentry            *si_dbgaufs_xib;
30845 +#ifdef CONFIG_AUFS_EXPORT
30846 +       struct dentry            *si_dbgaufs_xigen;
30847 +#endif
30848 +#endif
30849 +
30850 +#ifdef CONFIG_AUFS_SBILIST
30851 +       struct hlist_bl_node    si_list;
30852 +#endif
30853 +
30854 +       /* dirty, necessary for unmounting, sysfs and sysrq */
30855 +       struct super_block      *si_sb;
30856 +};
30857 +
30858 +/* sbinfo status flags */
30859 +/*
30860 + * set true when refresh_dirs() failed at remount time.
30861 + * then try refreshing dirs at access time again.
30862 + * if it is false, refreshing dirs at access time is unnecessary
30863 + */
30864 +#define AuSi_FAILED_REFRESH_DIR        1
30865 +#define AuSi_FHSM              (1 << 1)        /* fhsm is active now */
30866 +#define AuSi_NO_DREVAL         (1 << 2)        /* disable all d_revalidate */
30867 +
30868 +#ifndef CONFIG_AUFS_FHSM
30869 +#undef AuSi_FHSM
30870 +#define AuSi_FHSM              0
30871 +#endif
30872 +
30873 +static inline unsigned char au_do_ftest_si(struct au_sbinfo *sbi,
30874 +                                          unsigned int flag)
30875 +{
30876 +       AuRwMustAnyLock(&sbi->si_rwsem);
30877 +       return sbi->au_si_status & flag;
30878 +}
30879 +#define au_ftest_si(sbinfo, name)      au_do_ftest_si(sbinfo, AuSi_##name)
30880 +#define au_fset_si(sbinfo, name) do { \
30881 +       AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
30882 +       (sbinfo)->au_si_status |= AuSi_##name; \
30883 +} while (0)
30884 +#define au_fclr_si(sbinfo, name) do { \
30885 +       AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
30886 +       (sbinfo)->au_si_status &= ~AuSi_##name; \
30887 +} while (0)
30888 +
30889 +/* ---------------------------------------------------------------------- */
30890 +
30891 +/* policy to select one among writable branches */
30892 +#define AuWbrCopyup(sbinfo, ...) \
30893 +       ((sbinfo)->si_wbr_copyup_ops->copyup(__VA_ARGS__))
30894 +#define AuWbrCreate(sbinfo, ...) \
30895 +       ((sbinfo)->si_wbr_create_ops->create(__VA_ARGS__))
30896 +
30897 +/* flags for si_read_lock()/aufs_read_lock()/di_read_lock() */
30898 +#define AuLock_DW              1               /* write-lock dentry */
30899 +#define AuLock_IR              (1 << 1)        /* read-lock inode */
30900 +#define AuLock_IW              (1 << 2)        /* write-lock inode */
30901 +#define AuLock_FLUSH           (1 << 3)        /* wait for 'nowait' tasks */
30902 +#define AuLock_DIRS            (1 << 4)        /* target is a pair of dirs */
30903 +                                               /* except RENAME_EXCHANGE */
30904 +#define AuLock_NOPLM           (1 << 5)        /* return err in plm mode */
30905 +#define AuLock_NOPLMW          (1 << 6)        /* wait for plm mode ends */
30906 +#define AuLock_GEN             (1 << 7)        /* test digen/iigen */
30907 +#define au_ftest_lock(flags, name)     ((flags) & AuLock_##name)
30908 +#define au_fset_lock(flags, name) \
30909 +       do { (flags) |= AuLock_##name; } while (0)
30910 +#define au_fclr_lock(flags, name) \
30911 +       do { (flags) &= ~AuLock_##name; } while (0)
30912 +
30913 +/* ---------------------------------------------------------------------- */
30914 +
30915 +/* super.c */
30916 +struct inode *au_iget_locked(struct super_block *sb, ino_t ino);
30917 +
30918 +typedef unsigned long long (*au_arraycb_t)(struct super_block *sb, void *array,
30919 +                                          unsigned long long max, void *arg);
30920 +void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb,
30921 +                    struct super_block *sb, void *arg);
30922 +struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max);
30923 +void au_iarray_free(struct inode **a, unsigned long long max);
30924 +
30925 +void au_remount_refresh(struct super_block *sb, unsigned int do_idop);
30926 +extern const struct super_operations aufs_sop;
30927 +int au_alloc_root(struct super_block *sb);
30928 +extern struct file_system_type aufs_fs_type;
30929 +
30930 +/* sbinfo.c */
30931 +void au_si_free(struct kobject *kobj);
30932 +struct au_sbinfo *au_si_alloc(struct super_block *sb);
30933 +int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr, int may_shrink);
30934 +
30935 +unsigned int au_sigen_inc(struct super_block *sb);
30936 +aufs_bindex_t au_new_br_id(struct super_block *sb);
30937 +
30938 +int si_read_lock(struct super_block *sb, int flags);
30939 +int si_write_lock(struct super_block *sb, int flags);
30940 +int aufs_read_lock(struct dentry *dentry, int flags);
30941 +void aufs_read_unlock(struct dentry *dentry, int flags);
30942 +void aufs_write_lock(struct dentry *dentry);
30943 +void aufs_write_unlock(struct dentry *dentry);
30944 +int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags);
30945 +void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2);
30946 +
30947 +/* wbr_policy.c */
30948 +extern struct au_wbr_copyup_operations au_wbr_copyup_ops[];
30949 +extern struct au_wbr_create_operations au_wbr_create_ops[];
30950 +int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst);
30951 +int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex);
30952 +int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t btop);
30953 +
30954 +/* mvdown.c */
30955 +int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *arg);
30956 +
30957 +#ifdef CONFIG_AUFS_FHSM
30958 +/* fhsm.c */
30959 +
30960 +static inline pid_t au_fhsm_pid(struct au_fhsm *fhsm)
30961 +{
30962 +       pid_t pid;
30963 +
30964 +       spin_lock(&fhsm->fhsm_spin);
30965 +       pid = fhsm->fhsm_pid;
30966 +       spin_unlock(&fhsm->fhsm_spin);
30967 +
30968 +       return pid;
30969 +}
30970 +
30971 +void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force);
30972 +void au_fhsm_wrote_all(struct super_block *sb, int force);
30973 +int au_fhsm_fd(struct super_block *sb, int oflags);
30974 +int au_fhsm_br_alloc(struct au_branch *br);
30975 +void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex);
30976 +void au_fhsm_fin(struct super_block *sb);
30977 +void au_fhsm_init(struct au_sbinfo *sbinfo);
30978 +void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec);
30979 +void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo);
30980 +#else
30981 +AuStubVoid(au_fhsm_wrote, struct super_block *sb, aufs_bindex_t bindex,
30982 +          int force)
30983 +AuStubVoid(au_fhsm_wrote_all, struct super_block *sb, int force)
30984 +AuStub(int, au_fhsm_fd, return -EOPNOTSUPP, struct super_block *sb, int oflags)
30985 +AuStub(pid_t, au_fhsm_pid, return 0, struct au_fhsm *fhsm)
30986 +AuStubInt0(au_fhsm_br_alloc, struct au_branch *br)
30987 +AuStubVoid(au_fhsm_set_bottom, struct super_block *sb, aufs_bindex_t bindex)
30988 +AuStubVoid(au_fhsm_fin, struct super_block *sb)
30989 +AuStubVoid(au_fhsm_init, struct au_sbinfo *sbinfo)
30990 +AuStubVoid(au_fhsm_set, struct au_sbinfo *sbinfo, unsigned int sec)
30991 +AuStubVoid(au_fhsm_show, struct seq_file *seq, struct au_sbinfo *sbinfo)
30992 +#endif
30993 +
30994 +/* ---------------------------------------------------------------------- */
30995 +
30996 +static inline struct au_sbinfo *au_sbi(struct super_block *sb)
30997 +{
30998 +       return sb->s_fs_info;
30999 +}
31000 +
31001 +/* ---------------------------------------------------------------------- */
31002 +
31003 +#ifdef CONFIG_AUFS_EXPORT
31004 +int au_test_nfsd(void);
31005 +void au_export_init(struct super_block *sb);
31006 +void au_xigen_inc(struct inode *inode);
31007 +int au_xigen_new(struct inode *inode);
31008 +int au_xigen_set(struct super_block *sb, struct path *path);
31009 +void au_xigen_clr(struct super_block *sb);
31010 +
31011 +static inline int au_busy_or_stale(void)
31012 +{
31013 +       if (!au_test_nfsd())
31014 +               return -EBUSY;
31015 +       return -ESTALE;
31016 +}
31017 +#else
31018 +AuStubInt0(au_test_nfsd, void)
31019 +AuStubVoid(au_export_init, struct super_block *sb)
31020 +AuStubVoid(au_xigen_inc, struct inode *inode)
31021 +AuStubInt0(au_xigen_new, struct inode *inode)
31022 +AuStubInt0(au_xigen_set, struct super_block *sb, struct path *path)
31023 +AuStubVoid(au_xigen_clr, struct super_block *sb)
31024 +AuStub(int, au_busy_or_stale, return -EBUSY, void)
31025 +#endif /* CONFIG_AUFS_EXPORT */
31026 +
31027 +/* ---------------------------------------------------------------------- */
31028 +
31029 +#ifdef CONFIG_AUFS_SBILIST
31030 +/* module.c */
31031 +extern struct hlist_bl_head au_sbilist;
31032 +
31033 +static inline void au_sbilist_init(void)
31034 +{
31035 +       INIT_HLIST_BL_HEAD(&au_sbilist);
31036 +}
31037 +
31038 +static inline void au_sbilist_add(struct super_block *sb)
31039 +{
31040 +       au_hbl_add(&au_sbi(sb)->si_list, &au_sbilist);
31041 +}
31042 +
31043 +static inline void au_sbilist_del(struct super_block *sb)
31044 +{
31045 +       au_hbl_del(&au_sbi(sb)->si_list, &au_sbilist);
31046 +}
31047 +
31048 +#ifdef CONFIG_AUFS_MAGIC_SYSRQ
31049 +static inline void au_sbilist_lock(void)
31050 +{
31051 +       hlist_bl_lock(&au_sbilist);
31052 +}
31053 +
31054 +static inline void au_sbilist_unlock(void)
31055 +{
31056 +       hlist_bl_unlock(&au_sbilist);
31057 +}
31058 +#define AuGFP_SBILIST  GFP_ATOMIC
31059 +#else
31060 +AuStubVoid(au_sbilist_lock, void)
31061 +AuStubVoid(au_sbilist_unlock, void)
31062 +#define AuGFP_SBILIST  GFP_NOFS
31063 +#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
31064 +#else
31065 +AuStubVoid(au_sbilist_init, void)
31066 +AuStubVoid(au_sbilist_add, struct super_block *sb)
31067 +AuStubVoid(au_sbilist_del, struct super_block *sb)
31068 +AuStubVoid(au_sbilist_lock, void)
31069 +AuStubVoid(au_sbilist_unlock, void)
31070 +#define AuGFP_SBILIST  GFP_NOFS
31071 +#endif
31072 +
31073 +/* ---------------------------------------------------------------------- */
31074 +
31075 +static inline void dbgaufs_si_null(struct au_sbinfo *sbinfo)
31076 +{
31077 +       /*
31078 +        * This function is a dynamic '__init' function actually,
31079 +        * so the tiny check for si_rwsem is unnecessary.
31080 +        */
31081 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
31082 +#ifdef CONFIG_DEBUG_FS
31083 +       sbinfo->si_dbgaufs = NULL;
31084 +       sbinfo->si_dbgaufs_plink = NULL;
31085 +       sbinfo->si_dbgaufs_xib = NULL;
31086 +#ifdef CONFIG_AUFS_EXPORT
31087 +       sbinfo->si_dbgaufs_xigen = NULL;
31088 +#endif
31089 +#endif
31090 +}
31091 +
31092 +/* ---------------------------------------------------------------------- */
31093 +
31094 +/* current->atomic_flags */
31095 +/* this value should never corrupt the ones defined in linux/sched.h */
31096 +#define PFA_AUFS       0x10
31097 +
31098 +TASK_PFA_TEST(AUFS, test_aufs) /* task_test_aufs */
31099 +TASK_PFA_SET(AUFS, aufs)       /* task_set_aufs */
31100 +TASK_PFA_CLEAR(AUFS, aufs)     /* task_clear_aufs */
31101 +
31102 +static inline int si_pid_test(struct super_block *sb)
31103 +{
31104 +       return !!task_test_aufs(current);
31105 +}
31106 +
31107 +static inline void si_pid_clr(struct super_block *sb)
31108 +{
31109 +       AuDebugOn(!task_test_aufs(current));
31110 +       task_clear_aufs(current);
31111 +}
31112 +
31113 +static inline void si_pid_set(struct super_block *sb)
31114 +{
31115 +       AuDebugOn(task_test_aufs(current));
31116 +       task_set_aufs(current);
31117 +}
31118 +
31119 +/* ---------------------------------------------------------------------- */
31120 +
31121 +/* lock superblock. mainly for entry point functions */
31122 +#define __si_read_lock(sb)     au_rw_read_lock(&au_sbi(sb)->si_rwsem)
31123 +#define __si_write_lock(sb)    au_rw_write_lock(&au_sbi(sb)->si_rwsem)
31124 +#define __si_read_trylock(sb)  au_rw_read_trylock(&au_sbi(sb)->si_rwsem)
31125 +#define __si_write_trylock(sb) au_rw_write_trylock(&au_sbi(sb)->si_rwsem)
31126 +/*
31127 +#define __si_read_trylock_nested(sb) \
31128 +       au_rw_read_trylock_nested(&au_sbi(sb)->si_rwsem)
31129 +#define __si_write_trylock_nested(sb) \
31130 +       au_rw_write_trylock_nested(&au_sbi(sb)->si_rwsem)
31131 +*/
31132 +
31133 +#define __si_read_unlock(sb)   au_rw_read_unlock(&au_sbi(sb)->si_rwsem)
31134 +#define __si_write_unlock(sb)  au_rw_write_unlock(&au_sbi(sb)->si_rwsem)
31135 +#define __si_downgrade_lock(sb)        au_rw_dgrade_lock(&au_sbi(sb)->si_rwsem)
31136 +
31137 +#define SiMustNoWaiters(sb)    AuRwMustNoWaiters(&au_sbi(sb)->si_rwsem)
31138 +#define SiMustAnyLock(sb)      AuRwMustAnyLock(&au_sbi(sb)->si_rwsem)
31139 +#define SiMustWriteLock(sb)    AuRwMustWriteLock(&au_sbi(sb)->si_rwsem)
31140 +
31141 +static inline void si_noflush_read_lock(struct super_block *sb)
31142 +{
31143 +       __si_read_lock(sb);
31144 +       si_pid_set(sb);
31145 +}
31146 +
31147 +static inline int si_noflush_read_trylock(struct super_block *sb)
31148 +{
31149 +       int locked;
31150 +
31151 +       locked = __si_read_trylock(sb);
31152 +       if (locked)
31153 +               si_pid_set(sb);
31154 +       return locked;
31155 +}
31156 +
31157 +static inline void si_noflush_write_lock(struct super_block *sb)
31158 +{
31159 +       __si_write_lock(sb);
31160 +       si_pid_set(sb);
31161 +}
31162 +
31163 +static inline int si_noflush_write_trylock(struct super_block *sb)
31164 +{
31165 +       int locked;
31166 +
31167 +       locked = __si_write_trylock(sb);
31168 +       if (locked)
31169 +               si_pid_set(sb);
31170 +       return locked;
31171 +}
31172 +
31173 +#if 0 /* reserved */
31174 +static inline int si_read_trylock(struct super_block *sb, int flags)
31175 +{
31176 +       if (au_ftest_lock(flags, FLUSH))
31177 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
31178 +       return si_noflush_read_trylock(sb);
31179 +}
31180 +#endif
31181 +
31182 +static inline void si_read_unlock(struct super_block *sb)
31183 +{
31184 +       si_pid_clr(sb);
31185 +       __si_read_unlock(sb);
31186 +}
31187 +
31188 +#if 0 /* reserved */
31189 +static inline int si_write_trylock(struct super_block *sb, int flags)
31190 +{
31191 +       if (au_ftest_lock(flags, FLUSH))
31192 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
31193 +       return si_noflush_write_trylock(sb);
31194 +}
31195 +#endif
31196 +
31197 +static inline void si_write_unlock(struct super_block *sb)
31198 +{
31199 +       si_pid_clr(sb);
31200 +       __si_write_unlock(sb);
31201 +}
31202 +
31203 +#if 0 /* reserved */
31204 +static inline void si_downgrade_lock(struct super_block *sb)
31205 +{
31206 +       __si_downgrade_lock(sb);
31207 +}
31208 +#endif
31209 +
31210 +/* ---------------------------------------------------------------------- */
31211 +
31212 +static inline aufs_bindex_t au_sbbot(struct super_block *sb)
31213 +{
31214 +       SiMustAnyLock(sb);
31215 +       return au_sbi(sb)->si_bbot;
31216 +}
31217 +
31218 +static inline unsigned int au_mntflags(struct super_block *sb)
31219 +{
31220 +       SiMustAnyLock(sb);
31221 +       return au_sbi(sb)->si_mntflags;
31222 +}
31223 +
31224 +static inline unsigned int au_sigen(struct super_block *sb)
31225 +{
31226 +       SiMustAnyLock(sb);
31227 +       return au_sbi(sb)->si_generation;
31228 +}
31229 +
31230 +static inline struct au_branch *au_sbr(struct super_block *sb,
31231 +                                      aufs_bindex_t bindex)
31232 +{
31233 +       SiMustAnyLock(sb);
31234 +       return au_sbi(sb)->si_branch[0 + bindex];
31235 +}
31236 +
31237 +static inline loff_t au_xi_maxent(struct super_block *sb)
31238 +{
31239 +       SiMustAnyLock(sb);
31240 +       return au_sbi(sb)->si_ximaxent;
31241 +}
31242 +
31243 +#endif /* __KERNEL__ */
31244 +#endif /* __AUFS_SUPER_H__ */
31245 diff -urN /usr/share/empty/fs/aufs/sysaufs.c linux/fs/aufs/sysaufs.c
31246 --- /usr/share/empty/fs/aufs/sysaufs.c  1970-01-01 01:00:00.000000000 +0100
31247 +++ linux/fs/aufs/sysaufs.c     2022-03-21 14:49:05.729966343 +0100
31248 @@ -0,0 +1,93 @@
31249 +// SPDX-License-Identifier: GPL-2.0
31250 +/*
31251 + * Copyright (C) 2005-2021 Junjiro R. Okajima
31252 + *
31253 + * This program, aufs is free software; you can redistribute it and/or modify
31254 + * it under the terms of the GNU General Public License as published by
31255 + * the Free Software Foundation; either version 2 of the License, or
31256 + * (at your option) any later version.
31257 + *
31258 + * This program is distributed in the hope that it will be useful,
31259 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31260 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31261 + * GNU General Public License for more details.
31262 + *
31263 + * You should have received a copy of the GNU General Public License
31264 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31265 + */
31266 +
31267 +/*
31268 + * sysfs interface and lifetime management
31269 + * they are necessary regardless sysfs is disabled.
31270 + */
31271 +
31272 +#include <linux/random.h>
31273 +#include "aufs.h"
31274 +
31275 +unsigned long sysaufs_si_mask;
31276 +struct kset *sysaufs_kset;
31277 +
31278 +#define AuSiAttr(_name) { \
31279 +       .attr   = { .name = __stringify(_name), .mode = 0444 }, \
31280 +       .show   = sysaufs_si_##_name,                           \
31281 +}
31282 +
31283 +static struct sysaufs_si_attr sysaufs_si_attr_xi_path = AuSiAttr(xi_path);
31284 +struct attribute *sysaufs_si_attrs[] = {
31285 +       &sysaufs_si_attr_xi_path.attr,
31286 +       NULL,
31287 +};
31288 +
31289 +static const struct sysfs_ops au_sbi_ops = {
31290 +       .show   = sysaufs_si_show
31291 +};
31292 +
31293 +static struct kobj_type au_sbi_ktype = {
31294 +       .release        = au_si_free,
31295 +       .sysfs_ops      = &au_sbi_ops,
31296 +       .default_attrs  = sysaufs_si_attrs
31297 +};
31298 +
31299 +/* ---------------------------------------------------------------------- */
31300 +
31301 +int sysaufs_si_init(struct au_sbinfo *sbinfo)
31302 +{
31303 +       int err;
31304 +
31305 +       sbinfo->si_kobj.kset = sysaufs_kset;
31306 +       /* cf. sysaufs_name() */
31307 +       err = kobject_init_and_add
31308 +               (&sbinfo->si_kobj, &au_sbi_ktype, /*&sysaufs_kset->kobj*/NULL,
31309 +                SysaufsSiNamePrefix "%lx", sysaufs_si_id(sbinfo));
31310 +
31311 +       return err;
31312 +}
31313 +
31314 +void sysaufs_fin(void)
31315 +{
31316 +       sysfs_remove_group(&sysaufs_kset->kobj, sysaufs_attr_group);
31317 +       kset_unregister(sysaufs_kset);
31318 +}
31319 +
31320 +int __init sysaufs_init(void)
31321 +{
31322 +       int err;
31323 +
31324 +       do {
31325 +               get_random_bytes(&sysaufs_si_mask, sizeof(sysaufs_si_mask));
31326 +       } while (!sysaufs_si_mask);
31327 +
31328 +       err = -EINVAL;
31329 +       sysaufs_kset = kset_create_and_add(AUFS_NAME, NULL, fs_kobj);
31330 +       if (unlikely(!sysaufs_kset))
31331 +               goto out;
31332 +       err = PTR_ERR(sysaufs_kset);
31333 +       if (IS_ERR(sysaufs_kset))
31334 +               goto out;
31335 +       err = sysfs_create_group(&sysaufs_kset->kobj, sysaufs_attr_group);
31336 +       if (unlikely(err))
31337 +               kset_unregister(sysaufs_kset);
31338 +
31339 +out:
31340 +       return err;
31341 +}
31342 diff -urN /usr/share/empty/fs/aufs/sysaufs.h linux/fs/aufs/sysaufs.h
31343 --- /usr/share/empty/fs/aufs/sysaufs.h  1970-01-01 01:00:00.000000000 +0100
31344 +++ linux/fs/aufs/sysaufs.h     2022-03-21 14:49:05.729966343 +0100
31345 @@ -0,0 +1,102 @@
31346 +/* SPDX-License-Identifier: GPL-2.0 */
31347 +/*
31348 + * Copyright (C) 2005-2021 Junjiro R. Okajima
31349 + *
31350 + * This program, aufs is free software; you can redistribute it and/or modify
31351 + * it under the terms of the GNU General Public License as published by
31352 + * the Free Software Foundation; either version 2 of the License, or
31353 + * (at your option) any later version.
31354 + *
31355 + * This program is distributed in the hope that it will be useful,
31356 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31357 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31358 + * GNU General Public License for more details.
31359 + *
31360 + * You should have received a copy of the GNU General Public License
31361 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31362 + */
31363 +
31364 +/*
31365 + * sysfs interface and mount lifetime management
31366 + */
31367 +
31368 +#ifndef __SYSAUFS_H__
31369 +#define __SYSAUFS_H__
31370 +
31371 +#ifdef __KERNEL__
31372 +
31373 +#include <linux/sysfs.h>
31374 +#include "module.h"
31375 +
31376 +struct super_block;
31377 +struct au_sbinfo;
31378 +
31379 +struct sysaufs_si_attr {
31380 +       struct attribute attr;
31381 +       int (*show)(struct seq_file *seq, struct super_block *sb);
31382 +};
31383 +
31384 +/* ---------------------------------------------------------------------- */
31385 +
31386 +/* sysaufs.c */
31387 +extern unsigned long sysaufs_si_mask;
31388 +extern struct kset *sysaufs_kset;
31389 +extern struct attribute *sysaufs_si_attrs[];
31390 +int sysaufs_si_init(struct au_sbinfo *sbinfo);
31391 +int __init sysaufs_init(void);
31392 +void sysaufs_fin(void);
31393 +
31394 +/* ---------------------------------------------------------------------- */
31395 +
31396 +/* some people doesn't like to show a pointer in kernel */
31397 +static inline unsigned long sysaufs_si_id(struct au_sbinfo *sbinfo)
31398 +{
31399 +       return sysaufs_si_mask ^ (unsigned long)sbinfo;
31400 +}
31401 +
31402 +#define SysaufsSiNamePrefix    "si_"
31403 +#define SysaufsSiNameLen       (sizeof(SysaufsSiNamePrefix) + 16)
31404 +static inline void sysaufs_name(struct au_sbinfo *sbinfo, char *name)
31405 +{
31406 +       snprintf(name, SysaufsSiNameLen, SysaufsSiNamePrefix "%lx",
31407 +                sysaufs_si_id(sbinfo));
31408 +}
31409 +
31410 +struct au_branch;
31411 +#ifdef CONFIG_SYSFS
31412 +/* sysfs.c */
31413 +extern struct attribute_group *sysaufs_attr_group;
31414 +
31415 +int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb);
31416 +ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
31417 +                        char *buf);
31418 +long au_brinfo_ioctl(struct file *file, unsigned long arg);
31419 +#ifdef CONFIG_COMPAT
31420 +long au_brinfo_compat_ioctl(struct file *file, unsigned long arg);
31421 +#endif
31422 +
31423 +void sysaufs_br_init(struct au_branch *br);
31424 +void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex);
31425 +void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
31426 +
31427 +#define sysaufs_brs_init()     do {} while (0)
31428 +
31429 +#else
31430 +#define sysaufs_attr_group     NULL
31431 +
31432 +AuStubInt0(sysaufs_si_xi_path, struct seq_file *seq, struct super_block *sb)
31433 +AuStub(ssize_t, sysaufs_si_show, return 0, struct kobject *kobj,
31434 +       struct attribute *attr, char *buf)
31435 +AuStubVoid(sysaufs_br_init, struct au_branch *br)
31436 +AuStubVoid(sysaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex)
31437 +AuStubVoid(sysaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
31438 +
31439 +static inline void sysaufs_brs_init(void)
31440 +{
31441 +       sysaufs_brs = 0;
31442 +}
31443 +
31444 +#endif /* CONFIG_SYSFS */
31445 +
31446 +#endif /* __KERNEL__ */
31447 +#endif /* __SYSAUFS_H__ */
31448 diff -urN /usr/share/empty/fs/aufs/sysfs.c linux/fs/aufs/sysfs.c
31449 --- /usr/share/empty/fs/aufs/sysfs.c    1970-01-01 01:00:00.000000000 +0100
31450 +++ linux/fs/aufs/sysfs.c       2022-03-21 14:49:05.729966343 +0100
31451 @@ -0,0 +1,374 @@
31452 +// SPDX-License-Identifier: GPL-2.0
31453 +/*
31454 + * Copyright (C) 2005-2021 Junjiro R. Okajima
31455 + *
31456 + * This program, aufs is free software; you can redistribute it and/or modify
31457 + * it under the terms of the GNU General Public License as published by
31458 + * the Free Software Foundation; either version 2 of the License, or
31459 + * (at your option) any later version.
31460 + *
31461 + * This program is distributed in the hope that it will be useful,
31462 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31463 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31464 + * GNU General Public License for more details.
31465 + *
31466 + * You should have received a copy of the GNU General Public License
31467 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31468 + */
31469 +
31470 +/*
31471 + * sysfs interface
31472 + */
31473 +
31474 +#include <linux/compat.h>
31475 +#include <linux/seq_file.h>
31476 +#include "aufs.h"
31477 +
31478 +#ifdef CONFIG_AUFS_FS_MODULE
31479 +/* this entry violates the "one line per file" policy of sysfs */
31480 +static ssize_t config_show(struct kobject *kobj, struct kobj_attribute *attr,
31481 +                          char *buf)
31482 +{
31483 +       ssize_t err;
31484 +       static char *conf =
31485 +/* this file is generated at compiling */
31486 +#include "conf.str"
31487 +               ;
31488 +
31489 +       err = snprintf(buf, PAGE_SIZE, conf);
31490 +       if (unlikely(err >= PAGE_SIZE))
31491 +               err = -EFBIG;
31492 +       return err;
31493 +}
31494 +
31495 +static struct kobj_attribute au_config_attr = __ATTR_RO(config);
31496 +#endif
31497 +
31498 +static struct attribute *au_attr[] = {
31499 +#ifdef CONFIG_AUFS_FS_MODULE
31500 +       &au_config_attr.attr,
31501 +#endif
31502 +       NULL,   /* need to NULL terminate the list of attributes */
31503 +};
31504 +
31505 +static struct attribute_group sysaufs_attr_group_body = {
31506 +       .attrs = au_attr
31507 +};
31508 +
31509 +struct attribute_group *sysaufs_attr_group = &sysaufs_attr_group_body;
31510 +
31511 +/* ---------------------------------------------------------------------- */
31512 +
31513 +int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb)
31514 +{
31515 +       int err;
31516 +
31517 +       SiMustAnyLock(sb);
31518 +
31519 +       err = 0;
31520 +       if (au_opt_test(au_mntflags(sb), XINO)) {
31521 +               err = au_xino_path(seq, au_sbi(sb)->si_xib);
31522 +               seq_putc(seq, '\n');
31523 +       }
31524 +       return err;
31525 +}
31526 +
31527 +/*
31528 + * the lifetime of branch is independent from the entry under sysfs.
31529 + * sysfs handles the lifetime of the entry, and never call ->show() after it is
31530 + * unlinked.
31531 + */
31532 +static int sysaufs_si_br(struct seq_file *seq, struct super_block *sb,
31533 +                        aufs_bindex_t bindex, int idx)
31534 +{
31535 +       int err;
31536 +       struct path path;
31537 +       struct dentry *root;
31538 +       struct au_branch *br;
31539 +       au_br_perm_str_t perm;
31540 +
31541 +       AuDbg("b%d\n", bindex);
31542 +
31543 +       err = 0;
31544 +       root = sb->s_root;
31545 +       di_read_lock_parent(root, !AuLock_IR);
31546 +       br = au_sbr(sb, bindex);
31547 +
31548 +       switch (idx) {
31549 +       case AuBrSysfs_BR:
31550 +               path.mnt = au_br_mnt(br);
31551 +               path.dentry = au_h_dptr(root, bindex);
31552 +               err = au_seq_path(seq, &path);
31553 +               if (!err) {
31554 +                       au_optstr_br_perm(&perm, br->br_perm);
31555 +                       seq_printf(seq, "=%s\n", perm.a);
31556 +               }
31557 +               break;
31558 +       case AuBrSysfs_BRID:
31559 +               seq_printf(seq, "%d\n", br->br_id);
31560 +               break;
31561 +       }
31562 +       di_read_unlock(root, !AuLock_IR);
31563 +       if (unlikely(err || seq_has_overflowed(seq)))
31564 +               err = -E2BIG;
31565 +
31566 +       return err;
31567 +}
31568 +
31569 +/* ---------------------------------------------------------------------- */
31570 +
31571 +static struct seq_file *au_seq(char *p, ssize_t len)
31572 +{
31573 +       struct seq_file *seq;
31574 +
31575 +       seq = kzalloc(sizeof(*seq), GFP_NOFS);
31576 +       if (seq) {
31577 +               /* mutex_init(&seq.lock); */
31578 +               seq->buf = p;
31579 +               seq->size = len;
31580 +               return seq; /* success */
31581 +       }
31582 +
31583 +       seq = ERR_PTR(-ENOMEM);
31584 +       return seq;
31585 +}
31586 +
31587 +#define SysaufsBr_PREFIX       "br"
31588 +#define SysaufsBrid_PREFIX     "brid"
31589 +
31590 +/* todo: file size may exceed PAGE_SIZE */
31591 +ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
31592 +                       char *buf)
31593 +{
31594 +       ssize_t err;
31595 +       int idx;
31596 +       long l;
31597 +       aufs_bindex_t bbot;
31598 +       struct au_sbinfo *sbinfo;
31599 +       struct super_block *sb;
31600 +       struct seq_file *seq;
31601 +       char *name;
31602 +       struct attribute **cattr;
31603 +
31604 +       sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
31605 +       sb = sbinfo->si_sb;
31606 +
31607 +       /*
31608 +        * prevent a race condition between sysfs and aufs.
31609 +        * for instance, sysfs_file_read() calls sysfs_get_active_two() which
31610 +        * prohibits maintaining the sysfs entries.
31611 +        * hew we acquire read lock after sysfs_get_active_two().
31612 +        * on the other hand, the remount process may maintain the sysfs/aufs
31613 +        * entries after acquiring write lock.
31614 +        * it can cause a deadlock.
31615 +        * simply we gave up processing read here.
31616 +        */
31617 +       err = -EBUSY;
31618 +       if (unlikely(!si_noflush_read_trylock(sb)))
31619 +               goto out;
31620 +
31621 +       seq = au_seq(buf, PAGE_SIZE);
31622 +       err = PTR_ERR(seq);
31623 +       if (IS_ERR(seq))
31624 +               goto out_unlock;
31625 +
31626 +       name = (void *)attr->name;
31627 +       cattr = sysaufs_si_attrs;
31628 +       while (*cattr) {
31629 +               if (!strcmp(name, (*cattr)->name)) {
31630 +                       err = container_of(*cattr, struct sysaufs_si_attr, attr)
31631 +                               ->show(seq, sb);
31632 +                       goto out_seq;
31633 +               }
31634 +               cattr++;
31635 +       }
31636 +
31637 +       if (!strncmp(name, SysaufsBrid_PREFIX,
31638 +                    sizeof(SysaufsBrid_PREFIX) - 1)) {
31639 +               idx = AuBrSysfs_BRID;
31640 +               name += sizeof(SysaufsBrid_PREFIX) - 1;
31641 +       } else if (!strncmp(name, SysaufsBr_PREFIX,
31642 +                           sizeof(SysaufsBr_PREFIX) - 1)) {
31643 +               idx = AuBrSysfs_BR;
31644 +               name += sizeof(SysaufsBr_PREFIX) - 1;
31645 +       } else
31646 +                 BUG();
31647 +
31648 +       err = kstrtol(name, 10, &l);
31649 +       if (!err) {
31650 +               bbot = au_sbbot(sb);
31651 +               if (l <= bbot)
31652 +                       err = sysaufs_si_br(seq, sb, (aufs_bindex_t)l, idx);
31653 +               else
31654 +                       err = -ENOENT;
31655 +       }
31656 +
31657 +out_seq:
31658 +       if (!err) {
31659 +               err = seq->count;
31660 +               /* sysfs limit */
31661 +               if (unlikely(err == PAGE_SIZE))
31662 +                       err = -EFBIG;
31663 +       }
31664 +       au_kfree_rcu(seq);
31665 +out_unlock:
31666 +       si_read_unlock(sb);
31667 +out:
31668 +       return err;
31669 +}
31670 +
31671 +/* ---------------------------------------------------------------------- */
31672 +
31673 +static int au_brinfo(struct super_block *sb, union aufs_brinfo __user *arg)
31674 +{
31675 +       int err;
31676 +       int16_t brid;
31677 +       aufs_bindex_t bindex, bbot;
31678 +       size_t sz;
31679 +       char *buf;
31680 +       struct seq_file *seq;
31681 +       struct au_branch *br;
31682 +
31683 +       si_read_lock(sb, AuLock_FLUSH);
31684 +       bbot = au_sbbot(sb);
31685 +       err = bbot + 1;
31686 +       if (!arg)
31687 +               goto out;
31688 +
31689 +       err = -ENOMEM;
31690 +       buf = (void *)__get_free_page(GFP_NOFS);
31691 +       if (unlikely(!buf))
31692 +               goto out;
31693 +
31694 +       seq = au_seq(buf, PAGE_SIZE);
31695 +       err = PTR_ERR(seq);
31696 +       if (IS_ERR(seq))
31697 +               goto out_buf;
31698 +
31699 +       sz = sizeof(*arg) - offsetof(union aufs_brinfo, path);
31700 +       for (bindex = 0; bindex <= bbot; bindex++, arg++) {
31701 +               /* VERIFY_WRITE */
31702 +               err = !access_ok(arg, sizeof(*arg));
31703 +               if (unlikely(err))
31704 +                       break;
31705 +
31706 +               br = au_sbr(sb, bindex);
31707 +               brid = br->br_id;
31708 +               BUILD_BUG_ON(sizeof(brid) != sizeof(arg->id));
31709 +               err = __put_user(brid, &arg->id);
31710 +               if (unlikely(err))
31711 +                       break;
31712 +
31713 +               BUILD_BUG_ON(sizeof(br->br_perm) != sizeof(arg->perm));
31714 +               err = __put_user(br->br_perm, &arg->perm);
31715 +               if (unlikely(err))
31716 +                       break;
31717 +
31718 +               err = au_seq_path(seq, &br->br_path);
31719 +               if (unlikely(err))
31720 +                       break;
31721 +               seq_putc(seq, '\0');
31722 +               if (!seq_has_overflowed(seq)) {
31723 +                       err = copy_to_user(arg->path, seq->buf, seq->count);
31724 +                       seq->count = 0;
31725 +                       if (unlikely(err))
31726 +                               break;
31727 +               } else {
31728 +                       err = -E2BIG;
31729 +                       goto out_seq;
31730 +               }
31731 +       }
31732 +       if (unlikely(err))
31733 +               err = -EFAULT;
31734 +
31735 +out_seq:
31736 +       au_kfree_rcu(seq);
31737 +out_buf:
31738 +       free_page((unsigned long)buf);
31739 +out:
31740 +       si_read_unlock(sb);
31741 +       return err;
31742 +}
31743 +
31744 +long au_brinfo_ioctl(struct file *file, unsigned long arg)
31745 +{
31746 +       return au_brinfo(file->f_path.dentry->d_sb, (void __user *)arg);
31747 +}
31748 +
31749 +#ifdef CONFIG_COMPAT
31750 +long au_brinfo_compat_ioctl(struct file *file, unsigned long arg)
31751 +{
31752 +       return au_brinfo(file->f_path.dentry->d_sb, compat_ptr(arg));
31753 +}
31754 +#endif
31755 +
31756 +/* ---------------------------------------------------------------------- */
31757 +
31758 +void sysaufs_br_init(struct au_branch *br)
31759 +{
31760 +       int i;
31761 +       struct au_brsysfs *br_sysfs;
31762 +       struct attribute *attr;
31763 +
31764 +       br_sysfs = br->br_sysfs;
31765 +       for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31766 +               attr = &br_sysfs->attr;
31767 +               sysfs_attr_init(attr);
31768 +               attr->name = br_sysfs->name;
31769 +               attr->mode = 0444;
31770 +               br_sysfs++;
31771 +       }
31772 +}
31773 +
31774 +void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
31775 +{
31776 +       struct au_branch *br;
31777 +       struct kobject *kobj;
31778 +       struct au_brsysfs *br_sysfs;
31779 +       int i;
31780 +       aufs_bindex_t bbot;
31781 +
31782 +       if (!sysaufs_brs)
31783 +               return;
31784 +
31785 +       kobj = &au_sbi(sb)->si_kobj;
31786 +       bbot = au_sbbot(sb);
31787 +       for (; bindex <= bbot; bindex++) {
31788 +               br = au_sbr(sb, bindex);
31789 +               br_sysfs = br->br_sysfs;
31790 +               for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31791 +                       sysfs_remove_file(kobj, &br_sysfs->attr);
31792 +                       br_sysfs++;
31793 +               }
31794 +       }
31795 +}
31796 +
31797 +void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex)
31798 +{
31799 +       int err, i;
31800 +       aufs_bindex_t bbot;
31801 +       struct kobject *kobj;
31802 +       struct au_branch *br;
31803 +       struct au_brsysfs *br_sysfs;
31804 +
31805 +       if (!sysaufs_brs)
31806 +               return;
31807 +
31808 +       kobj = &au_sbi(sb)->si_kobj;
31809 +       bbot = au_sbbot(sb);
31810 +       for (; bindex <= bbot; bindex++) {
31811 +               br = au_sbr(sb, bindex);
31812 +               br_sysfs = br->br_sysfs;
31813 +               snprintf(br_sysfs[AuBrSysfs_BR].name, sizeof(br_sysfs->name),
31814 +                        SysaufsBr_PREFIX "%d", bindex);
31815 +               snprintf(br_sysfs[AuBrSysfs_BRID].name, sizeof(br_sysfs->name),
31816 +                        SysaufsBrid_PREFIX "%d", bindex);
31817 +               for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31818 +                       err = sysfs_create_file(kobj, &br_sysfs->attr);
31819 +                       if (unlikely(err))
31820 +                               pr_warn("failed %s under sysfs(%d)\n",
31821 +                                       br_sysfs->name, err);
31822 +                       br_sysfs++;
31823 +               }
31824 +       }
31825 +}
31826 diff -urN /usr/share/empty/fs/aufs/sysrq.c linux/fs/aufs/sysrq.c
31827 --- /usr/share/empty/fs/aufs/sysrq.c    1970-01-01 01:00:00.000000000 +0100
31828 +++ linux/fs/aufs/sysrq.c       2022-03-21 14:49:05.729966343 +0100
31829 @@ -0,0 +1,149 @@
31830 +// SPDX-License-Identifier: GPL-2.0
31831 +/*
31832 + * Copyright (C) 2005-2021 Junjiro R. Okajima
31833 + *
31834 + * This program, aufs is free software; you can redistribute it and/or modify
31835 + * it under the terms of the GNU General Public License as published by
31836 + * the Free Software Foundation; either version 2 of the License, or
31837 + * (at your option) any later version.
31838 + *
31839 + * This program is distributed in the hope that it will be useful,
31840 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31841 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31842 + * GNU General Public License for more details.
31843 + *
31844 + * You should have received a copy of the GNU General Public License
31845 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31846 + */
31847 +
31848 +/*
31849 + * magic sysrq handler
31850 + */
31851 +
31852 +/* #include <linux/sysrq.h> */
31853 +#include <linux/writeback.h>
31854 +#include "aufs.h"
31855 +
31856 +/* ---------------------------------------------------------------------- */
31857 +
31858 +static void sysrq_sb(struct super_block *sb)
31859 +{
31860 +       char *plevel;
31861 +       struct au_sbinfo *sbinfo;
31862 +       struct file *file;
31863 +       struct hlist_bl_head *files;
31864 +       struct hlist_bl_node *pos;
31865 +       struct au_finfo *finfo;
31866 +       struct inode *i;
31867 +
31868 +       plevel = au_plevel;
31869 +       au_plevel = KERN_WARNING;
31870 +
31871 +       /* since we define pr_fmt, call printk directly */
31872 +#define pr(str) printk(KERN_WARNING AUFS_NAME ": " str)
31873 +
31874 +       sbinfo = au_sbi(sb);
31875 +       printk(KERN_WARNING "si=%lx\n", sysaufs_si_id(sbinfo));
31876 +       pr("superblock\n");
31877 +       au_dpri_sb(sb);
31878 +
31879 +#if 0 /* reserved */
31880 +       do {
31881 +               int err, i, j, ndentry;
31882 +               struct au_dcsub_pages dpages;
31883 +               struct au_dpage *dpage;
31884 +
31885 +               err = au_dpages_init(&dpages, GFP_ATOMIC);
31886 +               if (unlikely(err))
31887 +                       break;
31888 +               err = au_dcsub_pages(&dpages, sb->s_root, NULL, NULL);
31889 +               if (!err)
31890 +                       for (i = 0; i < dpages.ndpage; i++) {
31891 +                               dpage = dpages.dpages + i;
31892 +                               ndentry = dpage->ndentry;
31893 +                               for (j = 0; j < ndentry; j++)
31894 +                                       au_dpri_dentry(dpage->dentries[j]);
31895 +                       }
31896 +               au_dpages_free(&dpages);
31897 +       } while (0);
31898 +#endif
31899 +
31900 +       pr("isolated inode\n");
31901 +       spin_lock(&sb->s_inode_list_lock);
31902 +       list_for_each_entry(i, &sb->s_inodes, i_sb_list) {
31903 +               spin_lock(&i->i_lock);
31904 +               if (hlist_empty(&i->i_dentry))
31905 +                       au_dpri_inode(i);
31906 +               spin_unlock(&i->i_lock);
31907 +       }
31908 +       spin_unlock(&sb->s_inode_list_lock);
31909 +
31910 +       pr("files\n");
31911 +       files = &au_sbi(sb)->si_files;
31912 +       hlist_bl_lock(files);
31913 +       hlist_bl_for_each_entry(finfo, pos, files, fi_hlist) {
31914 +               umode_t mode;
31915 +
31916 +               file = finfo->fi_file;
31917 +               mode = file_inode(file)->i_mode;
31918 +               if (!special_file(mode))
31919 +                       au_dpri_file(file);
31920 +       }
31921 +       hlist_bl_unlock(files);
31922 +       pr("done\n");
31923 +
31924 +#undef pr
31925 +       au_plevel = plevel;
31926 +}
31927 +
31928 +/* ---------------------------------------------------------------------- */
31929 +
31930 +/* module parameter */
31931 +static char *aufs_sysrq_key = "a";
31932 +module_param_named(sysrq, aufs_sysrq_key, charp, 0444);
31933 +MODULE_PARM_DESC(sysrq, "MagicSysRq key for " AUFS_NAME);
31934 +
31935 +static void au_sysrq(int key __maybe_unused)
31936 +{
31937 +       struct au_sbinfo *sbinfo;
31938 +       struct hlist_bl_node *pos;
31939 +
31940 +       lockdep_off();
31941 +       au_sbilist_lock();
31942 +       hlist_bl_for_each_entry(sbinfo, pos, &au_sbilist, si_list)
31943 +               sysrq_sb(sbinfo->si_sb);
31944 +       au_sbilist_unlock();
31945 +       lockdep_on();
31946 +}
31947 +
31948 +static struct sysrq_key_op au_sysrq_op = {
31949 +       .handler        = au_sysrq,
31950 +       .help_msg       = "Aufs",
31951 +       .action_msg     = "Aufs",
31952 +       .enable_mask    = SYSRQ_ENABLE_DUMP
31953 +};
31954 +
31955 +/* ---------------------------------------------------------------------- */
31956 +
31957 +int __init au_sysrq_init(void)
31958 +{
31959 +       int err;
31960 +       char key;
31961 +
31962 +       err = -1;
31963 +       key = *aufs_sysrq_key;
31964 +       if ('a' <= key && key <= 'z')
31965 +               err = register_sysrq_key(key, &au_sysrq_op);
31966 +       if (unlikely(err))
31967 +               pr_err("err %d, sysrq=%c\n", err, key);
31968 +       return err;
31969 +}
31970 +
31971 +void au_sysrq_fin(void)
31972 +{
31973 +       int err;
31974 +
31975 +       err = unregister_sysrq_key(*aufs_sysrq_key, &au_sysrq_op);
31976 +       if (unlikely(err))
31977 +               pr_err("err %d (ignored)\n", err);
31978 +}
31979 diff -urN /usr/share/empty/fs/aufs/vdir.c linux/fs/aufs/vdir.c
31980 --- /usr/share/empty/fs/aufs/vdir.c     1970-01-01 01:00:00.000000000 +0100
31981 +++ linux/fs/aufs/vdir.c        2022-03-21 14:49:05.733299677 +0100
31982 @@ -0,0 +1,896 @@
31983 +// SPDX-License-Identifier: GPL-2.0
31984 +/*
31985 + * Copyright (C) 2005-2021 Junjiro R. Okajima
31986 + *
31987 + * This program, aufs is free software; you can redistribute it and/or modify
31988 + * it under the terms of the GNU General Public License as published by
31989 + * the Free Software Foundation; either version 2 of the License, or
31990 + * (at your option) any later version.
31991 + *
31992 + * This program is distributed in the hope that it will be useful,
31993 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31994 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31995 + * GNU General Public License for more details.
31996 + *
31997 + * You should have received a copy of the GNU General Public License
31998 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31999 + */
32000 +
32001 +/*
32002 + * virtual or vertical directory
32003 + */
32004 +
32005 +#include <linux/iversion.h>
32006 +#include "aufs.h"
32007 +
32008 +static unsigned int calc_size(int nlen)
32009 +{
32010 +       return ALIGN(sizeof(struct au_vdir_de) + nlen, sizeof(ino_t));
32011 +}
32012 +
32013 +static int set_deblk_end(union au_vdir_deblk_p *p,
32014 +                        union au_vdir_deblk_p *deblk_end)
32015 +{
32016 +       if (calc_size(0) <= deblk_end->deblk - p->deblk) {
32017 +               p->de->de_str.len = 0;
32018 +               /* smp_mb(); */
32019 +               return 0;
32020 +       }
32021 +       return -1; /* error */
32022 +}
32023 +
32024 +/* returns true or false */
32025 +static int is_deblk_end(union au_vdir_deblk_p *p,
32026 +                       union au_vdir_deblk_p *deblk_end)
32027 +{
32028 +       if (calc_size(0) <= deblk_end->deblk - p->deblk)
32029 +               return !p->de->de_str.len;
32030 +       return 1;
32031 +}
32032 +
32033 +static unsigned char *last_deblk(struct au_vdir *vdir)
32034 +{
32035 +       return vdir->vd_deblk[vdir->vd_nblk - 1];
32036 +}
32037 +
32038 +/* ---------------------------------------------------------------------- */
32039 +
32040 +/* estimate the appropriate size for name hash table */
32041 +unsigned int au_rdhash_est(loff_t sz)
32042 +{
32043 +       unsigned int n;
32044 +
32045 +       n = UINT_MAX;
32046 +       sz >>= 10;
32047 +       if (sz < n)
32048 +               n = sz;
32049 +       if (sz < AUFS_RDHASH_DEF)
32050 +               n = AUFS_RDHASH_DEF;
32051 +       /* pr_info("n %u\n", n); */
32052 +       return n;
32053 +}
32054 +
32055 +/*
32056 + * the allocated memory has to be freed by
32057 + * au_nhash_wh_free() or au_nhash_de_free().
32058 + */
32059 +int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp)
32060 +{
32061 +       struct hlist_head *head;
32062 +       unsigned int u;
32063 +       size_t sz;
32064 +
32065 +       sz = sizeof(*nhash->nh_head) * num_hash;
32066 +       head = kmalloc(sz, gfp);
32067 +       if (head) {
32068 +               nhash->nh_num = num_hash;
32069 +               nhash->nh_head = head;
32070 +               for (u = 0; u < num_hash; u++)
32071 +                       INIT_HLIST_HEAD(head++);
32072 +               return 0; /* success */
32073 +       }
32074 +
32075 +       return -ENOMEM;
32076 +}
32077 +
32078 +static void nhash_count(struct hlist_head *head)
32079 +{
32080 +#if 0 /* debugging */
32081 +       unsigned long n;
32082 +       struct hlist_node *pos;
32083 +
32084 +       n = 0;
32085 +       hlist_for_each(pos, head)
32086 +               n++;
32087 +       pr_info("%lu\n", n);
32088 +#endif
32089 +}
32090 +
32091 +static void au_nhash_wh_do_free(struct hlist_head *head)
32092 +{
32093 +       struct au_vdir_wh *pos;
32094 +       struct hlist_node *node;
32095 +
32096 +       hlist_for_each_entry_safe(pos, node, head, wh_hash)
32097 +               au_kfree_rcu(pos);
32098 +}
32099 +
32100 +static void au_nhash_de_do_free(struct hlist_head *head)
32101 +{
32102 +       struct au_vdir_dehstr *pos;
32103 +       struct hlist_node *node;
32104 +
32105 +       hlist_for_each_entry_safe(pos, node, head, hash)
32106 +               au_cache_free_vdir_dehstr(pos);
32107 +}
32108 +
32109 +static void au_nhash_do_free(struct au_nhash *nhash,
32110 +                            void (*free)(struct hlist_head *head))
32111 +{
32112 +       unsigned int n;
32113 +       struct hlist_head *head;
32114 +
32115 +       n = nhash->nh_num;
32116 +       if (!n)
32117 +               return;
32118 +
32119 +       head = nhash->nh_head;
32120 +       while (n-- > 0) {
32121 +               nhash_count(head);
32122 +               free(head++);
32123 +       }
32124 +       au_kfree_try_rcu(nhash->nh_head);
32125 +}
32126 +
32127 +void au_nhash_wh_free(struct au_nhash *whlist)
32128 +{
32129 +       au_nhash_do_free(whlist, au_nhash_wh_do_free);
32130 +}
32131 +
32132 +static void au_nhash_de_free(struct au_nhash *delist)
32133 +{
32134 +       au_nhash_do_free(delist, au_nhash_de_do_free);
32135 +}
32136 +
32137 +/* ---------------------------------------------------------------------- */
32138 +
32139 +int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
32140 +                           int limit)
32141 +{
32142 +       int num;
32143 +       unsigned int u, n;
32144 +       struct hlist_head *head;
32145 +       struct au_vdir_wh *pos;
32146 +
32147 +       num = 0;
32148 +       n = whlist->nh_num;
32149 +       head = whlist->nh_head;
32150 +       for (u = 0; u < n; u++, head++)
32151 +               hlist_for_each_entry(pos, head, wh_hash)
32152 +                       if (pos->wh_bindex == btgt && ++num > limit)
32153 +                               return 1;
32154 +       return 0;
32155 +}
32156 +
32157 +static struct hlist_head *au_name_hash(struct au_nhash *nhash,
32158 +                                      unsigned char *name,
32159 +                                      unsigned int len)
32160 +{
32161 +       unsigned int v;
32162 +       /* const unsigned int magic_bit = 12; */
32163 +
32164 +       AuDebugOn(!nhash->nh_num || !nhash->nh_head);
32165 +
32166 +       v = 0;
32167 +       if (len > 8)
32168 +               len = 8;
32169 +       while (len--)
32170 +               v += *name++;
32171 +       /* v = hash_long(v, magic_bit); */
32172 +       v %= nhash->nh_num;
32173 +       return nhash->nh_head + v;
32174 +}
32175 +
32176 +static int au_nhash_test_name(struct au_vdir_destr *str, const char *name,
32177 +                             int nlen)
32178 +{
32179 +       return str->len == nlen && !memcmp(str->name, name, nlen);
32180 +}
32181 +
32182 +/* returns found or not */
32183 +int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen)
32184 +{
32185 +       struct hlist_head *head;
32186 +       struct au_vdir_wh *pos;
32187 +       struct au_vdir_destr *str;
32188 +
32189 +       head = au_name_hash(whlist, name, nlen);
32190 +       hlist_for_each_entry(pos, head, wh_hash) {
32191 +               str = &pos->wh_str;
32192 +               AuDbg("%.*s\n", str->len, str->name);
32193 +               if (au_nhash_test_name(str, name, nlen))
32194 +                       return 1;
32195 +       }
32196 +       return 0;
32197 +}
32198 +
32199 +/* returns found(true) or not */
32200 +static int test_known(struct au_nhash *delist, char *name, int nlen)
32201 +{
32202 +       struct hlist_head *head;
32203 +       struct au_vdir_dehstr *pos;
32204 +       struct au_vdir_destr *str;
32205 +
32206 +       head = au_name_hash(delist, name, nlen);
32207 +       hlist_for_each_entry(pos, head, hash) {
32208 +               str = pos->str;
32209 +               AuDbg("%.*s\n", str->len, str->name);
32210 +               if (au_nhash_test_name(str, name, nlen))
32211 +                       return 1;
32212 +       }
32213 +       return 0;
32214 +}
32215 +
32216 +static void au_shwh_init_wh(struct au_vdir_wh *wh, ino_t ino,
32217 +                           unsigned char d_type)
32218 +{
32219 +#ifdef CONFIG_AUFS_SHWH
32220 +       wh->wh_ino = ino;
32221 +       wh->wh_type = d_type;
32222 +#endif
32223 +}
32224 +
32225 +/* ---------------------------------------------------------------------- */
32226 +
32227 +int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
32228 +                      unsigned int d_type, aufs_bindex_t bindex,
32229 +                      unsigned char shwh)
32230 +{
32231 +       int err;
32232 +       struct au_vdir_destr *str;
32233 +       struct au_vdir_wh *wh;
32234 +
32235 +       AuDbg("%.*s\n", nlen, name);
32236 +       AuDebugOn(!whlist->nh_num || !whlist->nh_head);
32237 +
32238 +       err = -ENOMEM;
32239 +       wh = kmalloc(sizeof(*wh) + nlen, GFP_NOFS);
32240 +       if (unlikely(!wh))
32241 +               goto out;
32242 +
32243 +       err = 0;
32244 +       wh->wh_bindex = bindex;
32245 +       if (shwh)
32246 +               au_shwh_init_wh(wh, ino, d_type);
32247 +       str = &wh->wh_str;
32248 +       str->len = nlen;
32249 +       memcpy(str->name, name, nlen);
32250 +       hlist_add_head(&wh->wh_hash, au_name_hash(whlist, name, nlen));
32251 +       /* smp_mb(); */
32252 +
32253 +out:
32254 +       return err;
32255 +}
32256 +
32257 +static int append_deblk(struct au_vdir *vdir)
32258 +{
32259 +       int err;
32260 +       unsigned long ul;
32261 +       const unsigned int deblk_sz = vdir->vd_deblk_sz;
32262 +       union au_vdir_deblk_p p, deblk_end;
32263 +       unsigned char **o;
32264 +
32265 +       err = -ENOMEM;
32266 +       o = au_krealloc(vdir->vd_deblk, sizeof(*o) * (vdir->vd_nblk + 1),
32267 +                       GFP_NOFS, /*may_shrink*/0);
32268 +       if (unlikely(!o))
32269 +               goto out;
32270 +
32271 +       vdir->vd_deblk = o;
32272 +       p.deblk = kmalloc(deblk_sz, GFP_NOFS);
32273 +       if (p.deblk) {
32274 +               ul = vdir->vd_nblk++;
32275 +               vdir->vd_deblk[ul] = p.deblk;
32276 +               vdir->vd_last.ul = ul;
32277 +               vdir->vd_last.p.deblk = p.deblk;
32278 +               deblk_end.deblk = p.deblk + deblk_sz;
32279 +               err = set_deblk_end(&p, &deblk_end);
32280 +       }
32281 +
32282 +out:
32283 +       return err;
32284 +}
32285 +
32286 +static int append_de(struct au_vdir *vdir, char *name, int nlen, ino_t ino,
32287 +                    unsigned int d_type, struct au_nhash *delist)
32288 +{
32289 +       int err;
32290 +       unsigned int sz;
32291 +       const unsigned int deblk_sz = vdir->vd_deblk_sz;
32292 +       union au_vdir_deblk_p p, *room, deblk_end;
32293 +       struct au_vdir_dehstr *dehstr;
32294 +
32295 +       p.deblk = last_deblk(vdir);
32296 +       deblk_end.deblk = p.deblk + deblk_sz;
32297 +       room = &vdir->vd_last.p;
32298 +       AuDebugOn(room->deblk < p.deblk || deblk_end.deblk <= room->deblk
32299 +                 || !is_deblk_end(room, &deblk_end));
32300 +
32301 +       sz = calc_size(nlen);
32302 +       if (unlikely(sz > deblk_end.deblk - room->deblk)) {
32303 +               err = append_deblk(vdir);
32304 +               if (unlikely(err))
32305 +                       goto out;
32306 +
32307 +               p.deblk = last_deblk(vdir);
32308 +               deblk_end.deblk = p.deblk + deblk_sz;
32309 +               /* smp_mb(); */
32310 +               AuDebugOn(room->deblk != p.deblk);
32311 +       }
32312 +
32313 +       err = -ENOMEM;
32314 +       dehstr = au_cache_alloc_vdir_dehstr();
32315 +       if (unlikely(!dehstr))
32316 +               goto out;
32317 +
32318 +       dehstr->str = &room->de->de_str;
32319 +       hlist_add_head(&dehstr->hash, au_name_hash(delist, name, nlen));
32320 +       room->de->de_ino = ino;
32321 +       room->de->de_type = d_type;
32322 +       room->de->de_str.len = nlen;
32323 +       memcpy(room->de->de_str.name, name, nlen);
32324 +
32325 +       err = 0;
32326 +       room->deblk += sz;
32327 +       if (unlikely(set_deblk_end(room, &deblk_end)))
32328 +               err = append_deblk(vdir);
32329 +       /* smp_mb(); */
32330 +
32331 +out:
32332 +       return err;
32333 +}
32334 +
32335 +/* ---------------------------------------------------------------------- */
32336 +
32337 +void au_vdir_free(struct au_vdir *vdir)
32338 +{
32339 +       unsigned char **deblk;
32340 +
32341 +       deblk = vdir->vd_deblk;
32342 +       while (vdir->vd_nblk--)
32343 +               au_kfree_try_rcu(*deblk++);
32344 +       au_kfree_try_rcu(vdir->vd_deblk);
32345 +       au_cache_free_vdir(vdir);
32346 +}
32347 +
32348 +static struct au_vdir *alloc_vdir(struct file *file)
32349 +{
32350 +       struct au_vdir *vdir;
32351 +       struct super_block *sb;
32352 +       int err;
32353 +
32354 +       sb = file->f_path.dentry->d_sb;
32355 +       SiMustAnyLock(sb);
32356 +
32357 +       err = -ENOMEM;
32358 +       vdir = au_cache_alloc_vdir();
32359 +       if (unlikely(!vdir))
32360 +               goto out;
32361 +
32362 +       vdir->vd_deblk = kzalloc(sizeof(*vdir->vd_deblk), GFP_NOFS);
32363 +       if (unlikely(!vdir->vd_deblk))
32364 +               goto out_free;
32365 +
32366 +       vdir->vd_deblk_sz = au_sbi(sb)->si_rdblk;
32367 +       if (!vdir->vd_deblk_sz) {
32368 +               /* estimate the appropriate size for deblk */
32369 +               vdir->vd_deblk_sz = au_dir_size(file, /*dentry*/NULL);
32370 +               /* pr_info("vd_deblk_sz %u\n", vdir->vd_deblk_sz); */
32371 +       }
32372 +       vdir->vd_nblk = 0;
32373 +       vdir->vd_version = 0;
32374 +       vdir->vd_jiffy = 0;
32375 +       err = append_deblk(vdir);
32376 +       if (!err)
32377 +               return vdir; /* success */
32378 +
32379 +       au_kfree_try_rcu(vdir->vd_deblk);
32380 +
32381 +out_free:
32382 +       au_cache_free_vdir(vdir);
32383 +out:
32384 +       vdir = ERR_PTR(err);
32385 +       return vdir;
32386 +}
32387 +
32388 +static int reinit_vdir(struct au_vdir *vdir)
32389 +{
32390 +       int err;
32391 +       union au_vdir_deblk_p p, deblk_end;
32392 +
32393 +       while (vdir->vd_nblk > 1) {
32394 +               au_kfree_try_rcu(vdir->vd_deblk[vdir->vd_nblk - 1]);
32395 +               /* vdir->vd_deblk[vdir->vd_nblk - 1] = NULL; */
32396 +               vdir->vd_nblk--;
32397 +       }
32398 +       p.deblk = vdir->vd_deblk[0];
32399 +       deblk_end.deblk = p.deblk + vdir->vd_deblk_sz;
32400 +       err = set_deblk_end(&p, &deblk_end);
32401 +       /* keep vd_dblk_sz */
32402 +       vdir->vd_last.ul = 0;
32403 +       vdir->vd_last.p.deblk = vdir->vd_deblk[0];
32404 +       vdir->vd_version = 0;
32405 +       vdir->vd_jiffy = 0;
32406 +       /* smp_mb(); */
32407 +       return err;
32408 +}
32409 +
32410 +/* ---------------------------------------------------------------------- */
32411 +
32412 +#define AuFillVdir_CALLED      1
32413 +#define AuFillVdir_WHABLE      (1 << 1)
32414 +#define AuFillVdir_SHWH                (1 << 2)
32415 +#define au_ftest_fillvdir(flags, name) ((flags) & AuFillVdir_##name)
32416 +#define au_fset_fillvdir(flags, name) \
32417 +       do { (flags) |= AuFillVdir_##name; } while (0)
32418 +#define au_fclr_fillvdir(flags, name) \
32419 +       do { (flags) &= ~AuFillVdir_##name; } while (0)
32420 +
32421 +#ifndef CONFIG_AUFS_SHWH
32422 +#undef AuFillVdir_SHWH
32423 +#define AuFillVdir_SHWH                0
32424 +#endif
32425 +
32426 +struct fillvdir_arg {
32427 +       struct dir_context      ctx;
32428 +       struct file             *file;
32429 +       struct au_vdir          *vdir;
32430 +       struct au_nhash         delist;
32431 +       struct au_nhash         whlist;
32432 +       aufs_bindex_t           bindex;
32433 +       unsigned int            flags;
32434 +       int                     err;
32435 +};
32436 +
32437 +static int fillvdir(struct dir_context *ctx, const char *__name, int nlen,
32438 +                   loff_t offset __maybe_unused, u64 h_ino,
32439 +                   unsigned int d_type)
32440 +{
32441 +       struct fillvdir_arg *arg = container_of(ctx, struct fillvdir_arg, ctx);
32442 +       char *name = (void *)__name;
32443 +       struct super_block *sb;
32444 +       ino_t ino;
32445 +       const unsigned char shwh = !!au_ftest_fillvdir(arg->flags, SHWH);
32446 +
32447 +       arg->err = 0;
32448 +       sb = arg->file->f_path.dentry->d_sb;
32449 +       au_fset_fillvdir(arg->flags, CALLED);
32450 +       /* smp_mb(); */
32451 +       if (nlen <= AUFS_WH_PFX_LEN
32452 +           || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
32453 +               if (test_known(&arg->delist, name, nlen)
32454 +                   || au_nhash_test_known_wh(&arg->whlist, name, nlen))
32455 +                       goto out; /* already exists or whiteouted */
32456 +
32457 +               arg->err = au_ino(sb, arg->bindex, h_ino, d_type, &ino);
32458 +               if (!arg->err) {
32459 +                       if (unlikely(nlen > AUFS_MAX_NAMELEN))
32460 +                               d_type = DT_UNKNOWN;
32461 +                       arg->err = append_de(arg->vdir, name, nlen, ino,
32462 +                                            d_type, &arg->delist);
32463 +               }
32464 +       } else if (au_ftest_fillvdir(arg->flags, WHABLE)) {
32465 +               name += AUFS_WH_PFX_LEN;
32466 +               nlen -= AUFS_WH_PFX_LEN;
32467 +               if (au_nhash_test_known_wh(&arg->whlist, name, nlen))
32468 +                       goto out; /* already whiteouted */
32469 +
32470 +               ino = 0; /* just to suppress a warning */
32471 +               if (shwh)
32472 +                       arg->err = au_wh_ino(sb, arg->bindex, h_ino, d_type,
32473 +                                            &ino);
32474 +               if (!arg->err) {
32475 +                       if (nlen <= AUFS_MAX_NAMELEN + AUFS_WH_PFX_LEN)
32476 +                               d_type = DT_UNKNOWN;
32477 +                       arg->err = au_nhash_append_wh
32478 +                               (&arg->whlist, name, nlen, ino, d_type,
32479 +                                arg->bindex, shwh);
32480 +               }
32481 +       }
32482 +
32483 +out:
32484 +       if (!arg->err)
32485 +               arg->vdir->vd_jiffy = jiffies;
32486 +       /* smp_mb(); */
32487 +       AuTraceErr(arg->err);
32488 +       return arg->err;
32489 +}
32490 +
32491 +static int au_handle_shwh(struct super_block *sb, struct au_vdir *vdir,
32492 +                         struct au_nhash *whlist, struct au_nhash *delist)
32493 +{
32494 +#ifdef CONFIG_AUFS_SHWH
32495 +       int err;
32496 +       unsigned int nh, u;
32497 +       struct hlist_head *head;
32498 +       struct au_vdir_wh *pos;
32499 +       struct hlist_node *n;
32500 +       char *p, *o;
32501 +       struct au_vdir_destr *destr;
32502 +
32503 +       AuDebugOn(!au_opt_test(au_mntflags(sb), SHWH));
32504 +
32505 +       err = -ENOMEM;
32506 +       o = p = (void *)__get_free_page(GFP_NOFS);
32507 +       if (unlikely(!p))
32508 +               goto out;
32509 +
32510 +       err = 0;
32511 +       nh = whlist->nh_num;
32512 +       memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
32513 +       p += AUFS_WH_PFX_LEN;
32514 +       for (u = 0; u < nh; u++) {
32515 +               head = whlist->nh_head + u;
32516 +               hlist_for_each_entry_safe(pos, n, head, wh_hash) {
32517 +                       destr = &pos->wh_str;
32518 +                       memcpy(p, destr->name, destr->len);
32519 +                       err = append_de(vdir, o, destr->len + AUFS_WH_PFX_LEN,
32520 +                                       pos->wh_ino, pos->wh_type, delist);
32521 +                       if (unlikely(err))
32522 +                               break;
32523 +               }
32524 +       }
32525 +
32526 +       free_page((unsigned long)o);
32527 +
32528 +out:
32529 +       AuTraceErr(err);
32530 +       return err;
32531 +#else
32532 +       return 0;
32533 +#endif
32534 +}
32535 +
32536 +static int au_do_read_vdir(struct fillvdir_arg *arg)
32537 +{
32538 +       int err;
32539 +       unsigned int rdhash;
32540 +       loff_t offset;
32541 +       aufs_bindex_t bbot, bindex, btop;
32542 +       unsigned char shwh;
32543 +       struct file *hf, *file;
32544 +       struct super_block *sb;
32545 +
32546 +       file = arg->file;
32547 +       sb = file->f_path.dentry->d_sb;
32548 +       SiMustAnyLock(sb);
32549 +
32550 +       rdhash = au_sbi(sb)->si_rdhash;
32551 +       if (!rdhash)
32552 +               rdhash = au_rdhash_est(au_dir_size(file, /*dentry*/NULL));
32553 +       err = au_nhash_alloc(&arg->delist, rdhash, GFP_NOFS);
32554 +       if (unlikely(err))
32555 +               goto out;
32556 +       err = au_nhash_alloc(&arg->whlist, rdhash, GFP_NOFS);
32557 +       if (unlikely(err))
32558 +               goto out_delist;
32559 +
32560 +       err = 0;
32561 +       arg->flags = 0;
32562 +       shwh = 0;
32563 +       if (au_opt_test(au_mntflags(sb), SHWH)) {
32564 +               shwh = 1;
32565 +               au_fset_fillvdir(arg->flags, SHWH);
32566 +       }
32567 +       btop = au_fbtop(file);
32568 +       bbot = au_fbbot_dir(file);
32569 +       for (bindex = btop; !err && bindex <= bbot; bindex++) {
32570 +               hf = au_hf_dir(file, bindex);
32571 +               if (!hf)
32572 +                       continue;
32573 +
32574 +               offset = vfsub_llseek(hf, 0, SEEK_SET);
32575 +               err = offset;
32576 +               if (unlikely(offset))
32577 +                       break;
32578 +
32579 +               arg->bindex = bindex;
32580 +               au_fclr_fillvdir(arg->flags, WHABLE);
32581 +               if (shwh
32582 +                   || (bindex != bbot
32583 +                       && au_br_whable(au_sbr_perm(sb, bindex))))
32584 +                       au_fset_fillvdir(arg->flags, WHABLE);
32585 +               do {
32586 +                       arg->err = 0;
32587 +                       au_fclr_fillvdir(arg->flags, CALLED);
32588 +                       /* smp_mb(); */
32589 +                       err = vfsub_iterate_dir(hf, &arg->ctx);
32590 +                       if (err >= 0)
32591 +                               err = arg->err;
32592 +               } while (!err && au_ftest_fillvdir(arg->flags, CALLED));
32593 +
32594 +               /*
32595 +                * dir_relax() may be good for concurrency, but aufs should not
32596 +                * use it since it will cause a lockdep problem.
32597 +                */
32598 +       }
32599 +
32600 +       if (!err && shwh)
32601 +               err = au_handle_shwh(sb, arg->vdir, &arg->whlist, &arg->delist);
32602 +
32603 +       au_nhash_wh_free(&arg->whlist);
32604 +
32605 +out_delist:
32606 +       au_nhash_de_free(&arg->delist);
32607 +out:
32608 +       return err;
32609 +}
32610 +
32611 +static int read_vdir(struct file *file, int may_read)
32612 +{
32613 +       int err;
32614 +       unsigned long expire;
32615 +       unsigned char do_read;
32616 +       struct fillvdir_arg arg = {
32617 +               .ctx = {
32618 +                       .actor = fillvdir
32619 +               }
32620 +       };
32621 +       struct inode *inode;
32622 +       struct au_vdir *vdir, *allocated;
32623 +
32624 +       err = 0;
32625 +       inode = file_inode(file);
32626 +       IMustLock(inode);
32627 +       IiMustWriteLock(inode);
32628 +       SiMustAnyLock(inode->i_sb);
32629 +
32630 +       allocated = NULL;
32631 +       do_read = 0;
32632 +       expire = au_sbi(inode->i_sb)->si_rdcache;
32633 +       vdir = au_ivdir(inode);
32634 +       if (!vdir) {
32635 +               do_read = 1;
32636 +               vdir = alloc_vdir(file);
32637 +               err = PTR_ERR(vdir);
32638 +               if (IS_ERR(vdir))
32639 +                       goto out;
32640 +               err = 0;
32641 +               allocated = vdir;
32642 +       } else if (may_read
32643 +                  && (!inode_eq_iversion(inode, vdir->vd_version)
32644 +                      || time_after(jiffies, vdir->vd_jiffy + expire))) {
32645 +               do_read = 1;
32646 +               err = reinit_vdir(vdir);
32647 +               if (unlikely(err))
32648 +                       goto out;
32649 +       }
32650 +
32651 +       if (!do_read)
32652 +               return 0; /* success */
32653 +
32654 +       arg.file = file;
32655 +       arg.vdir = vdir;
32656 +       err = au_do_read_vdir(&arg);
32657 +       if (!err) {
32658 +               /* file->f_pos = 0; */ /* todo: ctx->pos? */
32659 +               vdir->vd_version = inode_query_iversion(inode);
32660 +               vdir->vd_last.ul = 0;
32661 +               vdir->vd_last.p.deblk = vdir->vd_deblk[0];
32662 +               if (allocated)
32663 +                       au_set_ivdir(inode, allocated);
32664 +       } else if (allocated)
32665 +               au_vdir_free(allocated);
32666 +
32667 +out:
32668 +       return err;
32669 +}
32670 +
32671 +static int copy_vdir(struct au_vdir *tgt, struct au_vdir *src)
32672 +{
32673 +       int err, rerr;
32674 +       unsigned long ul, n;
32675 +       const unsigned int deblk_sz = src->vd_deblk_sz;
32676 +
32677 +       AuDebugOn(tgt->vd_nblk != 1);
32678 +
32679 +       err = -ENOMEM;
32680 +       if (tgt->vd_nblk < src->vd_nblk) {
32681 +               unsigned char **p;
32682 +
32683 +               p = au_krealloc(tgt->vd_deblk, sizeof(*p) * src->vd_nblk,
32684 +                               GFP_NOFS, /*may_shrink*/0);
32685 +               if (unlikely(!p))
32686 +                       goto out;
32687 +               tgt->vd_deblk = p;
32688 +       }
32689 +
32690 +       if (tgt->vd_deblk_sz != deblk_sz) {
32691 +               unsigned char *p;
32692 +
32693 +               tgt->vd_deblk_sz = deblk_sz;
32694 +               p = au_krealloc(tgt->vd_deblk[0], deblk_sz, GFP_NOFS,
32695 +                               /*may_shrink*/1);
32696 +               if (unlikely(!p))
32697 +                       goto out;
32698 +               tgt->vd_deblk[0] = p;
32699 +       }
32700 +       memcpy(tgt->vd_deblk[0], src->vd_deblk[0], deblk_sz);
32701 +       tgt->vd_version = src->vd_version;
32702 +       tgt->vd_jiffy = src->vd_jiffy;
32703 +
32704 +       n = src->vd_nblk;
32705 +       for (ul = 1; ul < n; ul++) {
32706 +               tgt->vd_deblk[ul] = kmemdup(src->vd_deblk[ul], deblk_sz,
32707 +                                           GFP_NOFS);
32708 +               if (unlikely(!tgt->vd_deblk[ul]))
32709 +                       goto out;
32710 +               tgt->vd_nblk++;
32711 +       }
32712 +       tgt->vd_nblk = n;
32713 +       tgt->vd_last.ul = tgt->vd_last.ul;
32714 +       tgt->vd_last.p.deblk = tgt->vd_deblk[tgt->vd_last.ul];
32715 +       tgt->vd_last.p.deblk += src->vd_last.p.deblk
32716 +               - src->vd_deblk[src->vd_last.ul];
32717 +       /* smp_mb(); */
32718 +       return 0; /* success */
32719 +
32720 +out:
32721 +       rerr = reinit_vdir(tgt);
32722 +       BUG_ON(rerr);
32723 +       return err;
32724 +}
32725 +
32726 +int au_vdir_init(struct file *file)
32727 +{
32728 +       int err;
32729 +       struct inode *inode;
32730 +       struct au_vdir *vdir_cache, *allocated;
32731 +
32732 +       /* test file->f_pos here instead of ctx->pos */
32733 +       err = read_vdir(file, !file->f_pos);
32734 +       if (unlikely(err))
32735 +               goto out;
32736 +
32737 +       allocated = NULL;
32738 +       vdir_cache = au_fvdir_cache(file);
32739 +       if (!vdir_cache) {
32740 +               vdir_cache = alloc_vdir(file);
32741 +               err = PTR_ERR(vdir_cache);
32742 +               if (IS_ERR(vdir_cache))
32743 +                       goto out;
32744 +               allocated = vdir_cache;
32745 +       } else if (!file->f_pos && vdir_cache->vd_version != file->f_version) {
32746 +               /* test file->f_pos here instead of ctx->pos */
32747 +               err = reinit_vdir(vdir_cache);
32748 +               if (unlikely(err))
32749 +                       goto out;
32750 +       } else
32751 +               return 0; /* success */
32752 +
32753 +       inode = file_inode(file);
32754 +       err = copy_vdir(vdir_cache, au_ivdir(inode));
32755 +       if (!err) {
32756 +               file->f_version = inode_query_iversion(inode);
32757 +               if (allocated)
32758 +                       au_set_fvdir_cache(file, allocated);
32759 +       } else if (allocated)
32760 +               au_vdir_free(allocated);
32761 +
32762 +out:
32763 +       return err;
32764 +}
32765 +
32766 +static loff_t calc_offset(struct au_vdir *vdir)
32767 +{
32768 +       loff_t offset;
32769 +       union au_vdir_deblk_p p;
32770 +
32771 +       p.deblk = vdir->vd_deblk[vdir->vd_last.ul];
32772 +       offset = vdir->vd_last.p.deblk - p.deblk;
32773 +       offset += vdir->vd_deblk_sz * vdir->vd_last.ul;
32774 +       return offset;
32775 +}
32776 +
32777 +/* returns true or false */
32778 +static int seek_vdir(struct file *file, struct dir_context *ctx)
32779 +{
32780 +       int valid;
32781 +       unsigned int deblk_sz;
32782 +       unsigned long ul, n;
32783 +       loff_t offset;
32784 +       union au_vdir_deblk_p p, deblk_end;
32785 +       struct au_vdir *vdir_cache;
32786 +
32787 +       valid = 1;
32788 +       vdir_cache = au_fvdir_cache(file);
32789 +       offset = calc_offset(vdir_cache);
32790 +       AuDbg("offset %lld\n", offset);
32791 +       if (ctx->pos == offset)
32792 +               goto out;
32793 +
32794 +       vdir_cache->vd_last.ul = 0;
32795 +       vdir_cache->vd_last.p.deblk = vdir_cache->vd_deblk[0];
32796 +       if (!ctx->pos)
32797 +               goto out;
32798 +
32799 +       valid = 0;
32800 +       deblk_sz = vdir_cache->vd_deblk_sz;
32801 +       ul = div64_u64(ctx->pos, deblk_sz);
32802 +       AuDbg("ul %lu\n", ul);
32803 +       if (ul >= vdir_cache->vd_nblk)
32804 +               goto out;
32805 +
32806 +       n = vdir_cache->vd_nblk;
32807 +       for (; ul < n; ul++) {
32808 +               p.deblk = vdir_cache->vd_deblk[ul];
32809 +               deblk_end.deblk = p.deblk + deblk_sz;
32810 +               offset = ul;
32811 +               offset *= deblk_sz;
32812 +               while (!is_deblk_end(&p, &deblk_end) && offset < ctx->pos) {
32813 +                       unsigned int l;
32814 +
32815 +                       l = calc_size(p.de->de_str.len);
32816 +                       offset += l;
32817 +                       p.deblk += l;
32818 +               }
32819 +               if (!is_deblk_end(&p, &deblk_end)) {
32820 +                       valid = 1;
32821 +                       vdir_cache->vd_last.ul = ul;
32822 +                       vdir_cache->vd_last.p = p;
32823 +                       break;
32824 +               }
32825 +       }
32826 +
32827 +out:
32828 +       /* smp_mb(); */
32829 +       if (!valid)
32830 +               AuDbg("valid %d\n", !valid);
32831 +       return valid;
32832 +}
32833 +
32834 +int au_vdir_fill_de(struct file *file, struct dir_context *ctx)
32835 +{
32836 +       unsigned int l, deblk_sz;
32837 +       union au_vdir_deblk_p deblk_end;
32838 +       struct au_vdir *vdir_cache;
32839 +       struct au_vdir_de *de;
32840 +
32841 +       if (!seek_vdir(file, ctx))
32842 +               return 0;
32843 +
32844 +       vdir_cache = au_fvdir_cache(file);
32845 +       deblk_sz = vdir_cache->vd_deblk_sz;
32846 +       while (1) {
32847 +               deblk_end.deblk = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
32848 +               deblk_end.deblk += deblk_sz;
32849 +               while (!is_deblk_end(&vdir_cache->vd_last.p, &deblk_end)) {
32850 +                       de = vdir_cache->vd_last.p.de;
32851 +                       AuDbg("%.*s, off%lld, i%lu, dt%d\n",
32852 +                             de->de_str.len, de->de_str.name, ctx->pos,
32853 +                             (unsigned long)de->de_ino, de->de_type);
32854 +                       if (unlikely(!dir_emit(ctx, de->de_str.name,
32855 +                                              de->de_str.len, de->de_ino,
32856 +                                              de->de_type))) {
32857 +                               /* todo: ignore the error caused by udba? */
32858 +                               /* return err; */
32859 +                               return 0;
32860 +                       }
32861 +
32862 +                       l = calc_size(de->de_str.len);
32863 +                       vdir_cache->vd_last.p.deblk += l;
32864 +                       ctx->pos += l;
32865 +               }
32866 +               if (vdir_cache->vd_last.ul < vdir_cache->vd_nblk - 1) {
32867 +                       vdir_cache->vd_last.ul++;
32868 +                       vdir_cache->vd_last.p.deblk
32869 +                               = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
32870 +                       ctx->pos = deblk_sz * vdir_cache->vd_last.ul;
32871 +                       continue;
32872 +               }
32873 +               break;
32874 +       }
32875 +
32876 +       /* smp_mb(); */
32877 +       return 0;
32878 +}
32879 diff -urN /usr/share/empty/fs/aufs/vfsub.c linux/fs/aufs/vfsub.c
32880 --- /usr/share/empty/fs/aufs/vfsub.c    1970-01-01 01:00:00.000000000 +0100
32881 +++ linux/fs/aufs/vfsub.c       2022-03-21 14:49:05.733299677 +0100
32882 @@ -0,0 +1,918 @@
32883 +// SPDX-License-Identifier: GPL-2.0
32884 +/*
32885 + * Copyright (C) 2005-2021 Junjiro R. Okajima
32886 + *
32887 + * This program, aufs is free software; you can redistribute it and/or modify
32888 + * it under the terms of the GNU General Public License as published by
32889 + * the Free Software Foundation; either version 2 of the License, or
32890 + * (at your option) any later version.
32891 + *
32892 + * This program is distributed in the hope that it will be useful,
32893 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
32894 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
32895 + * GNU General Public License for more details.
32896 + *
32897 + * You should have received a copy of the GNU General Public License
32898 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
32899 + */
32900 +
32901 +/*
32902 + * sub-routines for VFS
32903 + */
32904 +
32905 +#include <linux/mnt_namespace.h>
32906 +#include <linux/nsproxy.h>
32907 +#include <linux/security.h>
32908 +#include <linux/splice.h>
32909 +#include "aufs.h"
32910 +
32911 +#ifdef CONFIG_AUFS_BR_FUSE
32912 +int vfsub_test_mntns(struct vfsmount *mnt, struct super_block *h_sb)
32913 +{
32914 +       if (!au_test_fuse(h_sb) || !au_userns)
32915 +               return 0;
32916 +
32917 +       return is_current_mnt_ns(mnt) ? 0 : -EACCES;
32918 +}
32919 +#endif
32920 +
32921 +int vfsub_sync_filesystem(struct super_block *h_sb)
32922 +{
32923 +       int err;
32924 +
32925 +       lockdep_off();
32926 +       down_read(&h_sb->s_umount);
32927 +       err = sync_filesystem(h_sb);
32928 +       up_read(&h_sb->s_umount);
32929 +       lockdep_on();
32930 +
32931 +       return err;
32932 +}
32933 +
32934 +/* ---------------------------------------------------------------------- */
32935 +
32936 +int vfsub_update_h_iattr(struct path *h_path, int *did)
32937 +{
32938 +       int err;
32939 +       struct kstat st;
32940 +       struct super_block *h_sb;
32941 +
32942 +       /*
32943 +        * Always needs h_path->mnt for LSM or FUSE branch.
32944 +        */
32945 +       AuDebugOn(!h_path->mnt);
32946 +
32947 +       /* for remote fs, leave work for its getattr or d_revalidate */
32948 +       /* for bad i_attr fs, handle them in aufs_getattr() */
32949 +       /* still some fs may acquire i_mutex. we need to skip them */
32950 +       err = 0;
32951 +       if (!did)
32952 +               did = &err;
32953 +       h_sb = h_path->dentry->d_sb;
32954 +       *did = (!au_test_fs_remote(h_sb) && au_test_fs_refresh_iattr(h_sb));
32955 +       if (*did)
32956 +               err = vfsub_getattr(h_path, &st);
32957 +
32958 +       return err;
32959 +}
32960 +
32961 +/* ---------------------------------------------------------------------- */
32962 +
32963 +struct file *vfsub_dentry_open(struct path *path, int flags)
32964 +{
32965 +       return dentry_open(path, flags /* | __FMODE_NONOTIFY */,
32966 +                          current_cred());
32967 +}
32968 +
32969 +struct file *vfsub_filp_open(const char *path, int oflags, int mode)
32970 +{
32971 +       struct file *file;
32972 +
32973 +       lockdep_off();
32974 +       file = filp_open(path,
32975 +                        oflags /* | __FMODE_NONOTIFY */,
32976 +                        mode);
32977 +       lockdep_on();
32978 +       if (IS_ERR(file))
32979 +               goto out;
32980 +       vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
32981 +
32982 +out:
32983 +       return file;
32984 +}
32985 +
32986 +/*
32987 + * Ideally this function should call VFS:do_last() in order to keep all its
32988 + * checkings. But it is very hard for aufs to regenerate several VFS internal
32989 + * structure such as nameidata. This is a second (or third) best approach.
32990 + * cf. linux/fs/namei.c:do_last(), lookup_open() and atomic_open().
32991 + */
32992 +int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
32993 +                     struct vfsub_aopen_args *args)
32994 +{
32995 +       int err;
32996 +       struct au_branch *br = args->br;
32997 +       struct file *file = args->file;
32998 +       /* copied from linux/fs/namei.c:atomic_open() */
32999 +       struct dentry *const DENTRY_NOT_SET = (void *)-1UL;
33000 +
33001 +       IMustLock(dir);
33002 +       AuDebugOn(!dir->i_op->atomic_open);
33003 +
33004 +       err = au_br_test_oflag(args->open_flag, br);
33005 +       if (unlikely(err))
33006 +               goto out;
33007 +
33008 +       au_lcnt_inc(&br->br_nfiles);
33009 +       file->f_path.dentry = DENTRY_NOT_SET;
33010 +       file->f_path.mnt = au_br_mnt(br);
33011 +       AuDbg("%ps\n", dir->i_op->atomic_open);
33012 +       err = dir->i_op->atomic_open(dir, dentry, file, args->open_flag,
33013 +                                    args->create_mode);
33014 +       if (unlikely(err < 0)) {
33015 +               au_lcnt_dec(&br->br_nfiles);
33016 +               goto out;
33017 +       }
33018 +
33019 +       /* temporary workaround for nfsv4 branch */
33020 +       if (au_test_nfs(dir->i_sb))
33021 +               nfs_mark_for_revalidate(dir);
33022 +
33023 +       if (file->f_mode & FMODE_CREATED)
33024 +               fsnotify_create(dir, dentry);
33025 +       if (!(file->f_mode & FMODE_OPENED)) {
33026 +               au_lcnt_dec(&br->br_nfiles);
33027 +               goto out;
33028 +       }
33029 +
33030 +       /* todo: call VFS:may_open() here */
33031 +       /* todo: ima_file_check() too? */
33032 +       if (!err && (args->open_flag & __FMODE_EXEC))
33033 +               err = deny_write_access(file);
33034 +       if (!err)
33035 +               fsnotify_open(file);
33036 +       else
33037 +               au_lcnt_dec(&br->br_nfiles);
33038 +       /* note that the file is created and still opened */
33039 +
33040 +out:
33041 +       return err;
33042 +}
33043 +
33044 +int vfsub_kern_path(const char *name, unsigned int flags, struct path *path)
33045 +{
33046 +       int err;
33047 +
33048 +       err = kern_path(name, flags, path);
33049 +       if (!err && d_is_positive(path->dentry))
33050 +               vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
33051 +       return err;
33052 +}
33053 +
33054 +struct dentry *vfsub_lookup_one_len_unlocked(const char *name,
33055 +                                            struct path *ppath, int len)
33056 +{
33057 +       struct path path;
33058 +
33059 +       path.dentry = lookup_one_len_unlocked(name, ppath->dentry, len);
33060 +       if (IS_ERR(path.dentry))
33061 +               goto out;
33062 +       if (d_is_positive(path.dentry)) {
33063 +               path.mnt = ppath->mnt;
33064 +               vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
33065 +       }
33066 +
33067 +out:
33068 +       AuTraceErrPtr(path.dentry);
33069 +       return path.dentry;
33070 +}
33071 +
33072 +struct dentry *vfsub_lookup_one_len(const char *name, struct path *ppath,
33073 +                                   int len)
33074 +{
33075 +       struct path path;
33076 +
33077 +       /* VFS checks it too, but by WARN_ON_ONCE() */
33078 +       IMustLock(d_inode(ppath->dentry));
33079 +
33080 +       path.dentry = lookup_one_len(name, ppath->dentry, len);
33081 +       if (IS_ERR(path.dentry))
33082 +               goto out;
33083 +       if (d_is_positive(path.dentry)) {
33084 +               path.mnt = ppath->mnt;
33085 +               vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
33086 +       }
33087 +
33088 +out:
33089 +       AuTraceErrPtr(path.dentry);
33090 +       return path.dentry;
33091 +}
33092 +
33093 +void vfsub_call_lkup_one(void *args)
33094 +{
33095 +       struct vfsub_lkup_one_args *a = args;
33096 +       *a->errp = vfsub_lkup_one(a->name, a->ppath);
33097 +}
33098 +
33099 +/* ---------------------------------------------------------------------- */
33100 +
33101 +struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
33102 +                                struct dentry *d2, struct au_hinode *hdir2)
33103 +{
33104 +       struct dentry *d;
33105 +
33106 +       lockdep_off();
33107 +       d = lock_rename(d1, d2);
33108 +       lockdep_on();
33109 +       au_hn_suspend(hdir1);
33110 +       if (hdir1 != hdir2)
33111 +               au_hn_suspend(hdir2);
33112 +
33113 +       return d;
33114 +}
33115 +
33116 +void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
33117 +                        struct dentry *d2, struct au_hinode *hdir2)
33118 +{
33119 +       au_hn_resume(hdir1);
33120 +       if (hdir1 != hdir2)
33121 +               au_hn_resume(hdir2);
33122 +       lockdep_off();
33123 +       unlock_rename(d1, d2);
33124 +       lockdep_on();
33125 +}
33126 +
33127 +/* ---------------------------------------------------------------------- */
33128 +
33129 +int vfsub_create(struct inode *dir, struct path *path, int mode, bool want_excl)
33130 +{
33131 +       int err;
33132 +       struct dentry *d;
33133 +       struct user_namespace *userns;
33134 +
33135 +       IMustLock(dir);
33136 +
33137 +       d = path->dentry;
33138 +       path->dentry = d->d_parent;
33139 +       err = security_path_mknod(path, d, mode, 0);
33140 +       path->dentry = d;
33141 +       if (unlikely(err))
33142 +               goto out;
33143 +       userns = mnt_user_ns(path->mnt);
33144 +
33145 +       lockdep_off();
33146 +       err = vfs_create(userns, dir, path->dentry, mode, want_excl);
33147 +       lockdep_on();
33148 +       if (!err) {
33149 +               struct path tmp = *path;
33150 +               int did;
33151 +
33152 +               vfsub_update_h_iattr(&tmp, &did);
33153 +               if (did) {
33154 +                       tmp.dentry = path->dentry->d_parent;
33155 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33156 +               }
33157 +               /*ignore*/
33158 +       }
33159 +
33160 +out:
33161 +       return err;
33162 +}
33163 +
33164 +int vfsub_symlink(struct inode *dir, struct path *path, const char *symname)
33165 +{
33166 +       int err;
33167 +       struct dentry *d;
33168 +       struct user_namespace *userns;
33169 +
33170 +       IMustLock(dir);
33171 +
33172 +       d = path->dentry;
33173 +       path->dentry = d->d_parent;
33174 +       err = security_path_symlink(path, d, symname);
33175 +       path->dentry = d;
33176 +       if (unlikely(err))
33177 +               goto out;
33178 +       userns = mnt_user_ns(path->mnt);
33179 +
33180 +       lockdep_off();
33181 +       err = vfs_symlink(userns, dir, path->dentry, symname);
33182 +       lockdep_on();
33183 +       if (!err) {
33184 +               struct path tmp = *path;
33185 +               int did;
33186 +
33187 +               vfsub_update_h_iattr(&tmp, &did);
33188 +               if (did) {
33189 +                       tmp.dentry = path->dentry->d_parent;
33190 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33191 +               }
33192 +               /*ignore*/
33193 +       }
33194 +
33195 +out:
33196 +       return err;
33197 +}
33198 +
33199 +int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev)
33200 +{
33201 +       int err;
33202 +       struct dentry *d;
33203 +       struct user_namespace *userns;
33204 +
33205 +       IMustLock(dir);
33206 +
33207 +       d = path->dentry;
33208 +       path->dentry = d->d_parent;
33209 +       err = security_path_mknod(path, d, mode, new_encode_dev(dev));
33210 +       path->dentry = d;
33211 +       if (unlikely(err))
33212 +               goto out;
33213 +       userns = mnt_user_ns(path->mnt);
33214 +
33215 +       lockdep_off();
33216 +       err = vfs_mknod(userns, dir, path->dentry, mode, dev);
33217 +       lockdep_on();
33218 +       if (!err) {
33219 +               struct path tmp = *path;
33220 +               int did;
33221 +
33222 +               vfsub_update_h_iattr(&tmp, &did);
33223 +               if (did) {
33224 +                       tmp.dentry = path->dentry->d_parent;
33225 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33226 +               }
33227 +               /*ignore*/
33228 +       }
33229 +
33230 +out:
33231 +       return err;
33232 +}
33233 +
33234 +static int au_test_nlink(struct inode *inode)
33235 +{
33236 +       const unsigned int link_max = UINT_MAX >> 1; /* rough margin */
33237 +
33238 +       if (!au_test_fs_no_limit_nlink(inode->i_sb)
33239 +           || inode->i_nlink < link_max)
33240 +               return 0;
33241 +       return -EMLINK;
33242 +}
33243 +
33244 +int vfsub_link(struct dentry *src_dentry, struct inode *dir, struct path *path,
33245 +              struct inode **delegated_inode)
33246 +{
33247 +       int err;
33248 +       struct dentry *d;
33249 +       struct user_namespace *userns;
33250 +
33251 +       IMustLock(dir);
33252 +
33253 +       err = au_test_nlink(d_inode(src_dentry));
33254 +       if (unlikely(err))
33255 +               return err;
33256 +
33257 +       /* we don't call may_linkat() */
33258 +       d = path->dentry;
33259 +       path->dentry = d->d_parent;
33260 +       err = security_path_link(src_dentry, path, d);
33261 +       path->dentry = d;
33262 +       if (unlikely(err))
33263 +               goto out;
33264 +       userns = mnt_user_ns(path->mnt);
33265 +
33266 +       lockdep_off();
33267 +       err = vfs_link(src_dentry, userns, dir, path->dentry, delegated_inode);
33268 +       lockdep_on();
33269 +       if (!err) {
33270 +               struct path tmp = *path;
33271 +               int did;
33272 +
33273 +               /* fuse has different memory inode for the same inumber */
33274 +               vfsub_update_h_iattr(&tmp, &did);
33275 +               if (did) {
33276 +                       tmp.dentry = path->dentry->d_parent;
33277 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33278 +                       tmp.dentry = src_dentry;
33279 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33280 +               }
33281 +               /*ignore*/
33282 +       }
33283 +
33284 +out:
33285 +       return err;
33286 +}
33287 +
33288 +int vfsub_rename(struct inode *src_dir, struct dentry *src_dentry,
33289 +                struct inode *dir, struct path *path,
33290 +                struct inode **delegated_inode, unsigned int flags)
33291 +{
33292 +       int err;
33293 +       struct renamedata rd;
33294 +       struct path tmp = {
33295 +               .mnt    = path->mnt
33296 +       };
33297 +       struct dentry *d;
33298 +
33299 +       IMustLock(dir);
33300 +       IMustLock(src_dir);
33301 +
33302 +       d = path->dentry;
33303 +       path->dentry = d->d_parent;
33304 +       tmp.dentry = src_dentry->d_parent;
33305 +       err = security_path_rename(&tmp, src_dentry, path, d, /*flags*/0);
33306 +       path->dentry = d;
33307 +       if (unlikely(err))
33308 +               goto out;
33309 +
33310 +       rd.old_mnt_userns = mnt_user_ns(path->mnt);
33311 +       rd.old_dir = src_dir;
33312 +       rd.old_dentry = src_dentry;
33313 +       rd.new_mnt_userns = rd.old_mnt_userns;
33314 +       rd.new_dir = dir;
33315 +       rd.new_dentry = path->dentry;
33316 +       rd.delegated_inode = delegated_inode;
33317 +       rd.flags = flags;
33318 +       lockdep_off();
33319 +       err = vfs_rename(&rd);
33320 +       lockdep_on();
33321 +       if (!err) {
33322 +               int did;
33323 +
33324 +               tmp.dentry = d->d_parent;
33325 +               vfsub_update_h_iattr(&tmp, &did);
33326 +               if (did) {
33327 +                       tmp.dentry = src_dentry;
33328 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33329 +                       tmp.dentry = src_dentry->d_parent;
33330 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33331 +               }
33332 +               /*ignore*/
33333 +       }
33334 +
33335 +out:
33336 +       return err;
33337 +}
33338 +
33339 +int vfsub_mkdir(struct inode *dir, struct path *path, int mode)
33340 +{
33341 +       int err;
33342 +       struct dentry *d;
33343 +       struct user_namespace *userns;
33344 +
33345 +       IMustLock(dir);
33346 +
33347 +       d = path->dentry;
33348 +       path->dentry = d->d_parent;
33349 +       err = security_path_mkdir(path, d, mode);
33350 +       path->dentry = d;
33351 +       if (unlikely(err))
33352 +               goto out;
33353 +       userns = mnt_user_ns(path->mnt);
33354 +
33355 +       lockdep_off();
33356 +       err = vfs_mkdir(userns, dir, path->dentry, mode);
33357 +       lockdep_on();
33358 +       if (!err) {
33359 +               struct path tmp = *path;
33360 +               int did;
33361 +
33362 +               vfsub_update_h_iattr(&tmp, &did);
33363 +               if (did) {
33364 +                       tmp.dentry = path->dentry->d_parent;
33365 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33366 +               }
33367 +               /*ignore*/
33368 +       }
33369 +
33370 +out:
33371 +       return err;
33372 +}
33373 +
33374 +int vfsub_rmdir(struct inode *dir, struct path *path)
33375 +{
33376 +       int err;
33377 +       struct dentry *d;
33378 +       struct user_namespace *userns;
33379 +
33380 +       IMustLock(dir);
33381 +
33382 +       d = path->dentry;
33383 +       path->dentry = d->d_parent;
33384 +       err = security_path_rmdir(path, d);
33385 +       path->dentry = d;
33386 +       if (unlikely(err))
33387 +               goto out;
33388 +       userns = mnt_user_ns(path->mnt);
33389 +
33390 +       lockdep_off();
33391 +       err = vfs_rmdir(userns, dir, path->dentry);
33392 +       lockdep_on();
33393 +       if (!err) {
33394 +               struct path tmp = {
33395 +                       .dentry = path->dentry->d_parent,
33396 +                       .mnt    = path->mnt
33397 +               };
33398 +
33399 +               vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
33400 +       }
33401 +
33402 +out:
33403 +       return err;
33404 +}
33405 +
33406 +/* ---------------------------------------------------------------------- */
33407 +
33408 +/* todo: support mmap_sem? */
33409 +ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
33410 +                    loff_t *ppos)
33411 +{
33412 +       ssize_t err;
33413 +
33414 +       lockdep_off();
33415 +       err = vfs_read(file, ubuf, count, ppos);
33416 +       lockdep_on();
33417 +       if (err >= 0)
33418 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33419 +       return err;
33420 +}
33421 +
33422 +ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
33423 +                    loff_t *ppos)
33424 +{
33425 +       ssize_t err;
33426 +
33427 +       lockdep_off();
33428 +       err = kernel_read(file, kbuf, count, ppos);
33429 +       lockdep_on();
33430 +       AuTraceErr(err);
33431 +       if (err >= 0)
33432 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33433 +       return err;
33434 +}
33435 +
33436 +ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
33437 +                     loff_t *ppos)
33438 +{
33439 +       ssize_t err;
33440 +
33441 +       lockdep_off();
33442 +       err = vfs_write(file, ubuf, count, ppos);
33443 +       lockdep_on();
33444 +       if (err >= 0)
33445 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33446 +       return err;
33447 +}
33448 +
33449 +ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count, loff_t *ppos)
33450 +{
33451 +       ssize_t err;
33452 +
33453 +       lockdep_off();
33454 +       err = kernel_write(file, kbuf, count, ppos);
33455 +       lockdep_on();
33456 +       if (err >= 0)
33457 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33458 +       return err;
33459 +}
33460 +
33461 +int vfsub_flush(struct file *file, fl_owner_t id)
33462 +{
33463 +       int err;
33464 +
33465 +       err = 0;
33466 +       if (file->f_op->flush) {
33467 +               if (!au_test_nfs(file->f_path.dentry->d_sb))
33468 +                       err = file->f_op->flush(file, id);
33469 +               else {
33470 +                       lockdep_off();
33471 +                       err = file->f_op->flush(file, id);
33472 +                       lockdep_on();
33473 +               }
33474 +               if (!err)
33475 +                       vfsub_update_h_iattr(&file->f_path, /*did*/NULL);
33476 +               /*ignore*/
33477 +       }
33478 +       return err;
33479 +}
33480 +
33481 +int vfsub_iterate_dir(struct file *file, struct dir_context *ctx)
33482 +{
33483 +       int err;
33484 +
33485 +       AuDbg("%pD, ctx{%ps, %llu}\n", file, ctx->actor, ctx->pos);
33486 +
33487 +       lockdep_off();
33488 +       err = iterate_dir(file, ctx);
33489 +       lockdep_on();
33490 +       if (err >= 0)
33491 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33492 +
33493 +       return err;
33494 +}
33495 +
33496 +long vfsub_splice_to(struct file *in, loff_t *ppos,
33497 +                    struct pipe_inode_info *pipe, size_t len,
33498 +                    unsigned int flags)
33499 +{
33500 +       long err;
33501 +
33502 +       lockdep_off();
33503 +       err = do_splice_to(in, ppos, pipe, len, flags);
33504 +       lockdep_on();
33505 +       file_accessed(in);
33506 +       if (err >= 0)
33507 +               vfsub_update_h_iattr(&in->f_path, /*did*/NULL); /*ignore*/
33508 +       return err;
33509 +}
33510 +
33511 +long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
33512 +                      loff_t *ppos, size_t len, unsigned int flags)
33513 +{
33514 +       long err;
33515 +
33516 +       lockdep_off();
33517 +       err = do_splice_from(pipe, out, ppos, len, flags);
33518 +       lockdep_on();
33519 +       if (err >= 0)
33520 +               vfsub_update_h_iattr(&out->f_path, /*did*/NULL); /*ignore*/
33521 +       return err;
33522 +}
33523 +
33524 +int vfsub_fsync(struct file *file, struct path *path, int datasync)
33525 +{
33526 +       int err;
33527 +
33528 +       /* file can be NULL */
33529 +       lockdep_off();
33530 +       err = vfs_fsync(file, datasync);
33531 +       lockdep_on();
33532 +       if (!err) {
33533 +               if (!path) {
33534 +                       AuDebugOn(!file);
33535 +                       path = &file->f_path;
33536 +               }
33537 +               vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
33538 +       }
33539 +       return err;
33540 +}
33541 +
33542 +/* cf. open.c:do_sys_truncate() and do_sys_ftruncate() */
33543 +int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
33544 +               struct file *h_file)
33545 +{
33546 +       int err;
33547 +       struct inode *h_inode;
33548 +       struct super_block *h_sb;
33549 +       struct user_namespace *h_userns;
33550 +
33551 +       if (!h_file) {
33552 +               err = vfsub_truncate(h_path, length);
33553 +               goto out;
33554 +       }
33555 +
33556 +       h_inode = d_inode(h_path->dentry);
33557 +       h_sb = h_inode->i_sb;
33558 +       lockdep_off();
33559 +       sb_start_write(h_sb);
33560 +       lockdep_on();
33561 +       err = security_path_truncate(h_path);
33562 +       if (!err) {
33563 +               h_userns = mnt_user_ns(h_path->mnt);
33564 +               lockdep_off();
33565 +               err = do_truncate(h_userns, h_path->dentry, length, attr,
33566 +                                 h_file);
33567 +               lockdep_on();
33568 +       }
33569 +       lockdep_off();
33570 +       sb_end_write(h_sb);
33571 +       lockdep_on();
33572 +
33573 +out:
33574 +       return err;
33575 +}
33576 +
33577 +/* ---------------------------------------------------------------------- */
33578 +
33579 +struct au_vfsub_mkdir_args {
33580 +       int *errp;
33581 +       struct inode *dir;
33582 +       struct path *path;
33583 +       int mode;
33584 +};
33585 +
33586 +static void au_call_vfsub_mkdir(void *args)
33587 +{
33588 +       struct au_vfsub_mkdir_args *a = args;
33589 +       *a->errp = vfsub_mkdir(a->dir, a->path, a->mode);
33590 +}
33591 +
33592 +int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode)
33593 +{
33594 +       int err, do_sio, wkq_err;
33595 +       struct user_namespace *userns;
33596 +
33597 +       userns = mnt_user_ns(path->mnt);
33598 +       do_sio = au_test_h_perm_sio(userns, dir, MAY_EXEC | MAY_WRITE);
33599 +       if (!do_sio) {
33600 +               lockdep_off();
33601 +               err = vfsub_mkdir(dir, path, mode);
33602 +               lockdep_on();
33603 +       } else {
33604 +               struct au_vfsub_mkdir_args args = {
33605 +                       .errp   = &err,
33606 +                       .dir    = dir,
33607 +                       .path   = path,
33608 +                       .mode   = mode
33609 +               };
33610 +               wkq_err = au_wkq_wait(au_call_vfsub_mkdir, &args);
33611 +               if (unlikely(wkq_err))
33612 +                       err = wkq_err;
33613 +       }
33614 +
33615 +       return err;
33616 +}
33617 +
33618 +struct au_vfsub_rmdir_args {
33619 +       int *errp;
33620 +       struct inode *dir;
33621 +       struct path *path;
33622 +};
33623 +
33624 +static void au_call_vfsub_rmdir(void *args)
33625 +{
33626 +       struct au_vfsub_rmdir_args *a = args;
33627 +       *a->errp = vfsub_rmdir(a->dir, a->path);
33628 +}
33629 +
33630 +int vfsub_sio_rmdir(struct inode *dir, struct path *path)
33631 +{
33632 +       int err, do_sio, wkq_err;
33633 +       struct user_namespace *userns;
33634 +
33635 +       userns = mnt_user_ns(path->mnt);
33636 +       do_sio = au_test_h_perm_sio(userns, dir, MAY_EXEC | MAY_WRITE);
33637 +       if (!do_sio) {
33638 +               lockdep_off();
33639 +               err = vfsub_rmdir(dir, path);
33640 +               lockdep_on();
33641 +       } else {
33642 +               struct au_vfsub_rmdir_args args = {
33643 +                       .errp   = &err,
33644 +                       .dir    = dir,
33645 +                       .path   = path
33646 +               };
33647 +               wkq_err = au_wkq_wait(au_call_vfsub_rmdir, &args);
33648 +               if (unlikely(wkq_err))
33649 +                       err = wkq_err;
33650 +       }
33651 +
33652 +       return err;
33653 +}
33654 +
33655 +/* ---------------------------------------------------------------------- */
33656 +
33657 +struct notify_change_args {
33658 +       int *errp;
33659 +       struct path *path;
33660 +       struct iattr *ia;
33661 +       struct inode **delegated_inode;
33662 +};
33663 +
33664 +static void call_notify_change(void *args)
33665 +{
33666 +       struct notify_change_args *a = args;
33667 +       struct inode *h_inode;
33668 +       struct user_namespace *userns;
33669 +
33670 +       h_inode = d_inode(a->path->dentry);
33671 +       IMustLock(h_inode);
33672 +
33673 +       *a->errp = -EPERM;
33674 +       if (!IS_IMMUTABLE(h_inode) && !IS_APPEND(h_inode)) {
33675 +               userns = mnt_user_ns(a->path->mnt);
33676 +               lockdep_off();
33677 +               *a->errp = notify_change(userns, a->path->dentry, a->ia,
33678 +                                        a->delegated_inode);
33679 +               lockdep_on();
33680 +               if (!*a->errp)
33681 +                       vfsub_update_h_iattr(a->path, /*did*/NULL); /*ignore*/
33682 +       }
33683 +       AuTraceErr(*a->errp);
33684 +}
33685 +
33686 +int vfsub_notify_change(struct path *path, struct iattr *ia,
33687 +                       struct inode **delegated_inode)
33688 +{
33689 +       int err;
33690 +       struct notify_change_args args = {
33691 +               .errp                   = &err,
33692 +               .path                   = path,
33693 +               .ia                     = ia,
33694 +               .delegated_inode        = delegated_inode
33695 +       };
33696 +
33697 +       call_notify_change(&args);
33698 +
33699 +       return err;
33700 +}
33701 +
33702 +int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
33703 +                           struct inode **delegated_inode)
33704 +{
33705 +       int err, wkq_err;
33706 +       struct notify_change_args args = {
33707 +               .errp                   = &err,
33708 +               .path                   = path,
33709 +               .ia                     = ia,
33710 +               .delegated_inode        = delegated_inode
33711 +       };
33712 +
33713 +       wkq_err = au_wkq_wait(call_notify_change, &args);
33714 +       if (unlikely(wkq_err))
33715 +               err = wkq_err;
33716 +
33717 +       return err;
33718 +}
33719 +
33720 +/* ---------------------------------------------------------------------- */
33721 +
33722 +struct unlink_args {
33723 +       int *errp;
33724 +       struct inode *dir;
33725 +       struct path *path;
33726 +       struct inode **delegated_inode;
33727 +};
33728 +
33729 +static void call_unlink(void *args)
33730 +{
33731 +       struct unlink_args *a = args;
33732 +       struct dentry *d = a->path->dentry;
33733 +       struct inode *h_inode;
33734 +       struct user_namespace *userns;
33735 +       const int stop_sillyrename = (au_test_nfs(d->d_sb)
33736 +                                     && au_dcount(d) == 1);
33737 +
33738 +       IMustLock(a->dir);
33739 +
33740 +       a->path->dentry = d->d_parent;
33741 +       *a->errp = security_path_unlink(a->path, d);
33742 +       a->path->dentry = d;
33743 +       if (unlikely(*a->errp))
33744 +               return;
33745 +
33746 +       if (!stop_sillyrename)
33747 +               dget(d);
33748 +       h_inode = NULL;
33749 +       if (d_is_positive(d)) {
33750 +               h_inode = d_inode(d);
33751 +               ihold(h_inode);
33752 +       }
33753 +
33754 +       userns = mnt_user_ns(a->path->mnt);
33755 +       lockdep_off();
33756 +       *a->errp = vfs_unlink(userns, a->dir, d, a->delegated_inode);
33757 +       lockdep_on();
33758 +       if (!*a->errp) {
33759 +               struct path tmp = {
33760 +                       .dentry = d->d_parent,
33761 +                       .mnt    = a->path->mnt
33762 +               };
33763 +               vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
33764 +       }
33765 +
33766 +       if (!stop_sillyrename)
33767 +               dput(d);
33768 +       if (h_inode)
33769 +               iput(h_inode);
33770 +
33771 +       AuTraceErr(*a->errp);
33772 +}
33773 +
33774 +/*
33775 + * @dir: must be locked.
33776 + * @dentry: target dentry.
33777 + */
33778 +int vfsub_unlink(struct inode *dir, struct path *path,
33779 +                struct inode **delegated_inode, int force)
33780 +{
33781 +       int err;
33782 +       struct unlink_args args = {
33783 +               .errp                   = &err,
33784 +               .dir                    = dir,
33785 +               .path                   = path,
33786 +               .delegated_inode        = delegated_inode
33787 +       };
33788 +
33789 +       if (!force)
33790 +               call_unlink(&args);
33791 +       else {
33792 +               int wkq_err;
33793 +
33794 +               wkq_err = au_wkq_wait(call_unlink, &args);
33795 +               if (unlikely(wkq_err))
33796 +                       err = wkq_err;
33797 +       }
33798 +
33799 +       return err;
33800 +}
33801 diff -urN /usr/share/empty/fs/aufs/vfsub.h linux/fs/aufs/vfsub.h
33802 --- /usr/share/empty/fs/aufs/vfsub.h    1970-01-01 01:00:00.000000000 +0100
33803 +++ linux/fs/aufs/vfsub.h       2022-03-21 14:49:05.733299677 +0100
33804 @@ -0,0 +1,358 @@
33805 +/* SPDX-License-Identifier: GPL-2.0 */
33806 +/*
33807 + * Copyright (C) 2005-2021 Junjiro R. Okajima
33808 + *
33809 + * This program, aufs is free software; you can redistribute it and/or modify
33810 + * it under the terms of the GNU General Public License as published by
33811 + * the Free Software Foundation; either version 2 of the License, or
33812 + * (at your option) any later version.
33813 + *
33814 + * This program is distributed in the hope that it will be useful,
33815 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
33816 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33817 + * GNU General Public License for more details.
33818 + *
33819 + * You should have received a copy of the GNU General Public License
33820 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
33821 + */
33822 +
33823 +/*
33824 + * sub-routines for VFS
33825 + */
33826 +
33827 +#ifndef __AUFS_VFSUB_H__
33828 +#define __AUFS_VFSUB_H__
33829 +
33830 +#ifdef __KERNEL__
33831 +
33832 +#include <linux/fs.h>
33833 +#include <linux/mount.h>
33834 +#include <linux/posix_acl.h>
33835 +#include <linux/xattr.h>
33836 +#include "debug.h"
33837 +
33838 +/* copied from linux/fs/internal.h */
33839 +/* todo: BAD approach!! */
33840 +extern void __mnt_drop_write(struct vfsmount *);
33841 +extern struct file *alloc_empty_file(int, const struct cred *);
33842 +
33843 +/* ---------------------------------------------------------------------- */
33844 +
33845 +/* lock subclass for lower inode */
33846 +/* default MAX_LOCKDEP_SUBCLASSES(8) is not enough */
33847 +/* reduce? gave up. */
33848 +enum {
33849 +       AuLsc_I_Begin = I_MUTEX_PARENT2, /* 5 */
33850 +       AuLsc_I_PARENT,         /* lower inode, parent first */
33851 +       AuLsc_I_PARENT2,        /* copyup dirs */
33852 +       AuLsc_I_PARENT3,        /* copyup wh */
33853 +       AuLsc_I_CHILD,
33854 +       AuLsc_I_CHILD2,
33855 +       AuLsc_I_End
33856 +};
33857 +
33858 +/* to debug easier, do not make them inlined functions */
33859 +#define MtxMustLock(mtx)       AuDebugOn(!mutex_is_locked(mtx))
33860 +#define IMustLock(i)           AuDebugOn(!inode_is_locked(i))
33861 +
33862 +/* ---------------------------------------------------------------------- */
33863 +
33864 +static inline void vfsub_drop_nlink(struct inode *inode)
33865 +{
33866 +       AuDebugOn(!inode->i_nlink);
33867 +       drop_nlink(inode);
33868 +}
33869 +
33870 +static inline void vfsub_dead_dir(struct inode *inode)
33871 +{
33872 +       AuDebugOn(!S_ISDIR(inode->i_mode));
33873 +       inode->i_flags |= S_DEAD;
33874 +       clear_nlink(inode);
33875 +}
33876 +
33877 +static inline int vfsub_native_ro(struct inode *inode)
33878 +{
33879 +       return sb_rdonly(inode->i_sb)
33880 +               || IS_RDONLY(inode)
33881 +               /* || IS_APPEND(inode) */
33882 +               || IS_IMMUTABLE(inode);
33883 +}
33884 +
33885 +#ifdef CONFIG_AUFS_BR_FUSE
33886 +int vfsub_test_mntns(struct vfsmount *mnt, struct super_block *h_sb);
33887 +#else
33888 +AuStubInt0(vfsub_test_mntns, struct vfsmount *mnt, struct super_block *h_sb);
33889 +#endif
33890 +
33891 +int vfsub_sync_filesystem(struct super_block *h_sb);
33892 +
33893 +/* ---------------------------------------------------------------------- */
33894 +
33895 +int vfsub_update_h_iattr(struct path *h_path, int *did);
33896 +struct file *vfsub_dentry_open(struct path *path, int flags);
33897 +struct file *vfsub_filp_open(const char *path, int oflags, int mode);
33898 +struct au_branch;
33899 +struct vfsub_aopen_args {
33900 +       struct file             *file;
33901 +       unsigned int            open_flag;
33902 +       umode_t                 create_mode;
33903 +       struct au_branch        *br;
33904 +};
33905 +int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
33906 +                     struct vfsub_aopen_args *args);
33907 +int vfsub_kern_path(const char *name, unsigned int flags, struct path *path);
33908 +
33909 +struct dentry *vfsub_lookup_one_len_unlocked(const char *name,
33910 +                                            struct path *ppath, int len);
33911 +struct dentry *vfsub_lookup_one_len(const char *name, struct path *ppath,
33912 +                                   int len);
33913 +
33914 +struct vfsub_lkup_one_args {
33915 +       struct dentry **errp;
33916 +       struct qstr *name;
33917 +       struct path *ppath;
33918 +};
33919 +
33920 +static inline struct dentry *vfsub_lkup_one(struct qstr *name,
33921 +                                           struct path *ppath)
33922 +{
33923 +       return vfsub_lookup_one_len(name->name, ppath, name->len);
33924 +}
33925 +
33926 +void vfsub_call_lkup_one(void *args);
33927 +
33928 +/* ---------------------------------------------------------------------- */
33929 +
33930 +static inline int vfsub_mnt_want_write(struct vfsmount *mnt)
33931 +{
33932 +       int err;
33933 +
33934 +       lockdep_off();
33935 +       err = mnt_want_write(mnt);
33936 +       lockdep_on();
33937 +       return err;
33938 +}
33939 +
33940 +static inline void vfsub_mnt_drop_write(struct vfsmount *mnt)
33941 +{
33942 +       lockdep_off();
33943 +       mnt_drop_write(mnt);
33944 +       lockdep_on();
33945 +}
33946 +
33947 +#if 0 /* reserved */
33948 +static inline void vfsub_mnt_drop_write_file(struct file *file)
33949 +{
33950 +       lockdep_off();
33951 +       mnt_drop_write_file(file);
33952 +       lockdep_on();
33953 +}
33954 +#endif
33955 +
33956 +/* ---------------------------------------------------------------------- */
33957 +
33958 +struct au_hinode;
33959 +struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
33960 +                                struct dentry *d2, struct au_hinode *hdir2);
33961 +void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
33962 +                        struct dentry *d2, struct au_hinode *hdir2);
33963 +
33964 +int vfsub_create(struct inode *dir, struct path *path, int mode,
33965 +                bool want_excl);
33966 +int vfsub_symlink(struct inode *dir, struct path *path,
33967 +                 const char *symname);
33968 +int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev);
33969 +int vfsub_link(struct dentry *src_dentry, struct inode *dir,
33970 +              struct path *path, struct inode **delegated_inode);
33971 +int vfsub_rename(struct inode *src_hdir, struct dentry *src_dentry,
33972 +                struct inode *hdir, struct path *path,
33973 +                struct inode **delegated_inode, unsigned int flags);
33974 +int vfsub_mkdir(struct inode *dir, struct path *path, int mode);
33975 +int vfsub_rmdir(struct inode *dir, struct path *path);
33976 +
33977 +/* ---------------------------------------------------------------------- */
33978 +
33979 +ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
33980 +                    loff_t *ppos);
33981 +ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
33982 +                       loff_t *ppos);
33983 +ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
33984 +                     loff_t *ppos);
33985 +ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count,
33986 +                     loff_t *ppos);
33987 +int vfsub_flush(struct file *file, fl_owner_t id);
33988 +int vfsub_iterate_dir(struct file *file, struct dir_context *ctx);
33989 +
33990 +static inline loff_t vfsub_f_size_read(struct file *file)
33991 +{
33992 +       return i_size_read(file_inode(file));
33993 +}
33994 +
33995 +static inline unsigned int vfsub_file_flags(struct file *file)
33996 +{
33997 +       unsigned int flags;
33998 +
33999 +       spin_lock(&file->f_lock);
34000 +       flags = file->f_flags;
34001 +       spin_unlock(&file->f_lock);
34002 +
34003 +       return flags;
34004 +}
34005 +
34006 +static inline int vfsub_file_execed(struct file *file)
34007 +{
34008 +       /* todo: direct access f_flags */
34009 +       return !!(vfsub_file_flags(file) & __FMODE_EXEC);
34010 +}
34011 +
34012 +#if 0 /* reserved */
34013 +static inline void vfsub_file_accessed(struct file *h_file)
34014 +{
34015 +       file_accessed(h_file);
34016 +       vfsub_update_h_iattr(&h_file->f_path, /*did*/NULL); /*ignore*/
34017 +}
34018 +#endif
34019 +
34020 +#if 0 /* reserved */
34021 +static inline void vfsub_touch_atime(struct vfsmount *h_mnt,
34022 +                                    struct dentry *h_dentry)
34023 +{
34024 +       struct path h_path = {
34025 +               .dentry = h_dentry,
34026 +               .mnt    = h_mnt
34027 +       };
34028 +       touch_atime(&h_path);
34029 +       vfsub_update_h_iattr(&h_path, /*did*/NULL); /*ignore*/
34030 +}
34031 +#endif
34032 +
34033 +static inline int vfsub_update_time(struct inode *h_inode,
34034 +                                   struct timespec64 *ts, int flags)
34035 +{
34036 +       return inode_update_time(h_inode, ts, flags);
34037 +       /* no vfsub_update_h_iattr() since we don't have struct path */
34038 +}
34039 +
34040 +#ifdef CONFIG_FS_POSIX_ACL
34041 +static inline int vfsub_acl_chmod(struct user_namespace *h_userns,
34042 +                                 struct inode *h_inode, umode_t h_mode)
34043 +{
34044 +       int err;
34045 +
34046 +       err = posix_acl_chmod(h_userns, h_inode, h_mode);
34047 +       if (err == -EOPNOTSUPP)
34048 +               err = 0;
34049 +       return err;
34050 +}
34051 +#else
34052 +AuStubInt0(vfsub_acl_chmod, struct user_namespace *h_userns,
34053 +          struct inode *h_inode, umode_t h_mode);
34054 +#endif
34055 +
34056 +long vfsub_splice_to(struct file *in, loff_t *ppos,
34057 +                    struct pipe_inode_info *pipe, size_t len,
34058 +                    unsigned int flags);
34059 +long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
34060 +                      loff_t *ppos, size_t len, unsigned int flags);
34061 +
34062 +static inline long vfsub_truncate(struct path *path, loff_t length)
34063 +{
34064 +       long err;
34065 +
34066 +       lockdep_off();
34067 +       err = vfs_truncate(path, length);
34068 +       lockdep_on();
34069 +       return err;
34070 +}
34071 +
34072 +int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
34073 +               struct file *h_file);
34074 +int vfsub_fsync(struct file *file, struct path *path, int datasync);
34075 +
34076 +/*
34077 + * re-use branch fs's ioctl(FICLONE) while aufs itself doesn't support such
34078 + * ioctl.
34079 + */
34080 +static inline loff_t vfsub_clone_file_range(struct file *src, struct file *dst,
34081 +                                           loff_t len)
34082 +{
34083 +       loff_t err;
34084 +
34085 +       lockdep_off();
34086 +       err = vfs_clone_file_range(src, 0, dst, 0, len, /*remap_flags*/0);
34087 +       lockdep_on();
34088 +
34089 +       return err;
34090 +}
34091 +
34092 +/* copy_file_range(2) is a systemcall */
34093 +static inline ssize_t vfsub_copy_file_range(struct file *src, loff_t src_pos,
34094 +                                           struct file *dst, loff_t dst_pos,
34095 +                                           size_t len, unsigned int flags)
34096 +{
34097 +       ssize_t ssz;
34098 +
34099 +       lockdep_off();
34100 +       ssz = vfs_copy_file_range(src, src_pos, dst, dst_pos, len, flags);
34101 +       lockdep_on();
34102 +
34103 +       return ssz;
34104 +}
34105 +
34106 +/* ---------------------------------------------------------------------- */
34107 +
34108 +static inline loff_t vfsub_llseek(struct file *file, loff_t offset, int origin)
34109 +{
34110 +       loff_t err;
34111 +
34112 +       lockdep_off();
34113 +       err = vfs_llseek(file, offset, origin);
34114 +       lockdep_on();
34115 +       return err;
34116 +}
34117 +
34118 +/* ---------------------------------------------------------------------- */
34119 +
34120 +int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode);
34121 +int vfsub_sio_rmdir(struct inode *dir, struct path *path);
34122 +int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
34123 +                           struct inode **delegated_inode);
34124 +int vfsub_notify_change(struct path *path, struct iattr *ia,
34125 +                       struct inode **delegated_inode);
34126 +int vfsub_unlink(struct inode *dir, struct path *path,
34127 +                struct inode **delegated_inode, int force);
34128 +
34129 +static inline int vfsub_getattr(const struct path *path, struct kstat *st)
34130 +{
34131 +       return vfs_getattr(path, st, STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
34132 +}
34133 +
34134 +/* ---------------------------------------------------------------------- */
34135 +
34136 +static inline int vfsub_setxattr(struct user_namespace *userns,
34137 +                                struct dentry *dentry, const char *name,
34138 +                                const void *value, size_t size, int flags)
34139 +{
34140 +       int err;
34141 +
34142 +       lockdep_off();
34143 +       err = vfs_setxattr(userns, dentry, name, value, size, flags);
34144 +       lockdep_on();
34145 +
34146 +       return err;
34147 +}
34148 +
34149 +static inline int vfsub_removexattr(struct user_namespace *userns,
34150 +                                   struct dentry *dentry, const char *name)
34151 +{
34152 +       int err;
34153 +
34154 +       lockdep_off();
34155 +       err = vfs_removexattr(userns, dentry, name);
34156 +       lockdep_on();
34157 +
34158 +       return err;
34159 +}
34160 +
34161 +#endif /* __KERNEL__ */
34162 +#endif /* __AUFS_VFSUB_H__ */
34163 diff -urN /usr/share/empty/fs/aufs/wbr_policy.c linux/fs/aufs/wbr_policy.c
34164 --- /usr/share/empty/fs/aufs/wbr_policy.c       1970-01-01 01:00:00.000000000 +0100
34165 +++ linux/fs/aufs/wbr_policy.c  2022-03-21 14:49:05.733299677 +0100
34166 @@ -0,0 +1,830 @@
34167 +// SPDX-License-Identifier: GPL-2.0
34168 +/*
34169 + * Copyright (C) 2005-2021 Junjiro R. Okajima
34170 + *
34171 + * This program, aufs is free software; you can redistribute it and/or modify
34172 + * it under the terms of the GNU General Public License as published by
34173 + * the Free Software Foundation; either version 2 of the License, or
34174 + * (at your option) any later version.
34175 + *
34176 + * This program is distributed in the hope that it will be useful,
34177 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
34178 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
34179 + * GNU General Public License for more details.
34180 + *
34181 + * You should have received a copy of the GNU General Public License
34182 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
34183 + */
34184 +
34185 +/*
34186 + * policies for selecting one among multiple writable branches
34187 + */
34188 +
34189 +#include <linux/statfs.h>
34190 +#include "aufs.h"
34191 +
34192 +/* subset of cpup_attr() */
34193 +static noinline_for_stack
34194 +int au_cpdown_attr(struct path *h_path, struct dentry *h_src)
34195 +{
34196 +       int err, sbits;
34197 +       struct iattr ia;
34198 +       struct inode *h_isrc;
34199 +
34200 +       h_isrc = d_inode(h_src);
34201 +       ia.ia_valid = ATTR_FORCE | ATTR_MODE | ATTR_UID | ATTR_GID;
34202 +       ia.ia_mode = h_isrc->i_mode;
34203 +       ia.ia_uid = h_isrc->i_uid;
34204 +       ia.ia_gid = h_isrc->i_gid;
34205 +       sbits = !!(ia.ia_mode & (S_ISUID | S_ISGID));
34206 +       au_cpup_attr_flags(d_inode(h_path->dentry), h_isrc->i_flags);
34207 +       /* no delegation since it is just created */
34208 +       err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
34209 +
34210 +       /* is this nfs only? */
34211 +       if (!err && sbits && au_test_nfs(h_path->dentry->d_sb)) {
34212 +               ia.ia_valid = ATTR_FORCE | ATTR_MODE;
34213 +               ia.ia_mode = h_isrc->i_mode;
34214 +               err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
34215 +       }
34216 +
34217 +       return err;
34218 +}
34219 +
34220 +#define AuCpdown_PARENT_OPQ    1
34221 +#define AuCpdown_WHED          (1 << 1)
34222 +#define AuCpdown_MADE_DIR      (1 << 2)
34223 +#define AuCpdown_DIROPQ                (1 << 3)
34224 +#define au_ftest_cpdown(flags, name)   ((flags) & AuCpdown_##name)
34225 +#define au_fset_cpdown(flags, name) \
34226 +       do { (flags) |= AuCpdown_##name; } while (0)
34227 +#define au_fclr_cpdown(flags, name) \
34228 +       do { (flags) &= ~AuCpdown_##name; } while (0)
34229 +
34230 +static int au_cpdown_dir_opq(struct dentry *dentry, aufs_bindex_t bdst,
34231 +                            unsigned int *flags)
34232 +{
34233 +       int err;
34234 +       struct dentry *opq_dentry;
34235 +
34236 +       opq_dentry = au_diropq_create(dentry, bdst);
34237 +       err = PTR_ERR(opq_dentry);
34238 +       if (IS_ERR(opq_dentry))
34239 +               goto out;
34240 +       dput(opq_dentry);
34241 +       au_fset_cpdown(*flags, DIROPQ);
34242 +
34243 +out:
34244 +       return err;
34245 +}
34246 +
34247 +static int au_cpdown_dir_wh(struct dentry *dentry, struct dentry *h_parent,
34248 +                           struct inode *dir, aufs_bindex_t bdst)
34249 +{
34250 +       int err;
34251 +       struct path h_path;
34252 +       struct au_branch *br;
34253 +
34254 +       br = au_sbr(dentry->d_sb, bdst);
34255 +       h_path.dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
34256 +       err = PTR_ERR(h_path.dentry);
34257 +       if (IS_ERR(h_path.dentry))
34258 +               goto out;
34259 +
34260 +       err = 0;
34261 +       if (d_is_positive(h_path.dentry)) {
34262 +               h_path.mnt = au_br_mnt(br);
34263 +               err = au_wh_unlink_dentry(au_h_iptr(dir, bdst), &h_path,
34264 +                                         dentry);
34265 +       }
34266 +       dput(h_path.dentry);
34267 +
34268 +out:
34269 +       return err;
34270 +}
34271 +
34272 +static int au_cpdown_dir(struct dentry *dentry, aufs_bindex_t bdst,
34273 +                        struct au_pin *pin,
34274 +                        struct dentry *h_parent, void *arg)
34275 +{
34276 +       int err, rerr;
34277 +       aufs_bindex_t bopq, btop;
34278 +       struct path h_path;
34279 +       struct dentry *parent;
34280 +       struct inode *h_dir, *h_inode, *inode, *dir;
34281 +       unsigned int *flags = arg;
34282 +
34283 +       btop = au_dbtop(dentry);
34284 +       /* dentry is di-locked */
34285 +       parent = dget_parent(dentry);
34286 +       dir = d_inode(parent);
34287 +       h_dir = d_inode(h_parent);
34288 +       AuDebugOn(h_dir != au_h_iptr(dir, bdst));
34289 +       IMustLock(h_dir);
34290 +
34291 +       err = au_lkup_neg(dentry, bdst, /*wh*/0);
34292 +       if (unlikely(err < 0))
34293 +               goto out;
34294 +       h_path.dentry = au_h_dptr(dentry, bdst);
34295 +       h_path.mnt = au_sbr_mnt(dentry->d_sb, bdst);
34296 +       err = vfsub_sio_mkdir(au_h_iptr(dir, bdst), &h_path, 0755);
34297 +       if (unlikely(err))
34298 +               goto out_put;
34299 +       au_fset_cpdown(*flags, MADE_DIR);
34300 +
34301 +       bopq = au_dbdiropq(dentry);
34302 +       au_fclr_cpdown(*flags, WHED);
34303 +       au_fclr_cpdown(*flags, DIROPQ);
34304 +       if (au_dbwh(dentry) == bdst)
34305 +               au_fset_cpdown(*flags, WHED);
34306 +       if (!au_ftest_cpdown(*flags, PARENT_OPQ) && bopq <= bdst)
34307 +               au_fset_cpdown(*flags, PARENT_OPQ);
34308 +       h_inode = d_inode(h_path.dentry);
34309 +       inode_lock_nested(h_inode, AuLsc_I_CHILD);
34310 +       if (au_ftest_cpdown(*flags, WHED)) {
34311 +               err = au_cpdown_dir_opq(dentry, bdst, flags);
34312 +               if (unlikely(err)) {
34313 +                       inode_unlock(h_inode);
34314 +                       goto out_dir;
34315 +               }
34316 +       }
34317 +
34318 +       err = au_cpdown_attr(&h_path, au_h_dptr(dentry, btop));
34319 +       inode_unlock(h_inode);
34320 +       if (unlikely(err))
34321 +               goto out_opq;
34322 +
34323 +       if (au_ftest_cpdown(*flags, WHED)) {
34324 +               err = au_cpdown_dir_wh(dentry, h_parent, dir, bdst);
34325 +               if (unlikely(err))
34326 +                       goto out_opq;
34327 +       }
34328 +
34329 +       inode = d_inode(dentry);
34330 +       if (au_ibbot(inode) < bdst)
34331 +               au_set_ibbot(inode, bdst);
34332 +       au_set_h_iptr(inode, bdst, au_igrab(h_inode),
34333 +                     au_hi_flags(inode, /*isdir*/1));
34334 +       au_fhsm_wrote(dentry->d_sb, bdst, /*force*/0);
34335 +       goto out; /* success */
34336 +
34337 +       /* revert */
34338 +out_opq:
34339 +       if (au_ftest_cpdown(*flags, DIROPQ)) {
34340 +               inode_lock_nested(h_inode, AuLsc_I_CHILD);
34341 +               rerr = au_diropq_remove(dentry, bdst);
34342 +               inode_unlock(h_inode);
34343 +               if (unlikely(rerr)) {
34344 +                       AuIOErr("failed removing diropq for %pd b%d (%d)\n",
34345 +                               dentry, bdst, rerr);
34346 +                       err = -EIO;
34347 +                       goto out;
34348 +               }
34349 +       }
34350 +out_dir:
34351 +       if (au_ftest_cpdown(*flags, MADE_DIR)) {
34352 +               rerr = vfsub_sio_rmdir(au_h_iptr(dir, bdst), &h_path);
34353 +               if (unlikely(rerr)) {
34354 +                       AuIOErr("failed removing %pd b%d (%d)\n",
34355 +                               dentry, bdst, rerr);
34356 +                       err = -EIO;
34357 +               }
34358 +       }
34359 +out_put:
34360 +       au_set_h_dptr(dentry, bdst, NULL);
34361 +       if (au_dbbot(dentry) == bdst)
34362 +               au_update_dbbot(dentry);
34363 +out:
34364 +       dput(parent);
34365 +       return err;
34366 +}
34367 +
34368 +int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst)
34369 +{
34370 +       int err;
34371 +       unsigned int flags;
34372 +
34373 +       flags = 0;
34374 +       err = au_cp_dirs(dentry, bdst, au_cpdown_dir, &flags);
34375 +
34376 +       return err;
34377 +}
34378 +
34379 +/* ---------------------------------------------------------------------- */
34380 +
34381 +/* policies for create */
34382 +
34383 +int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex)
34384 +{
34385 +       int err, i, j, ndentry;
34386 +       aufs_bindex_t bopq;
34387 +       struct au_dcsub_pages dpages;
34388 +       struct au_dpage *dpage;
34389 +       struct dentry **dentries, *parent, *d;
34390 +
34391 +       err = au_dpages_init(&dpages, GFP_NOFS);
34392 +       if (unlikely(err))
34393 +               goto out;
34394 +       parent = dget_parent(dentry);
34395 +       err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/0);
34396 +       if (unlikely(err))
34397 +               goto out_free;
34398 +
34399 +       err = bindex;
34400 +       for (i = 0; i < dpages.ndpage; i++) {
34401 +               dpage = dpages.dpages + i;
34402 +               dentries = dpage->dentries;
34403 +               ndentry = dpage->ndentry;
34404 +               for (j = 0; j < ndentry; j++) {
34405 +                       d = dentries[j];
34406 +                       di_read_lock_parent2(d, !AuLock_IR);
34407 +                       bopq = au_dbdiropq(d);
34408 +                       di_read_unlock(d, !AuLock_IR);
34409 +                       if (bopq >= 0 && bopq < err)
34410 +                               err = bopq;
34411 +               }
34412 +       }
34413 +
34414 +out_free:
34415 +       dput(parent);
34416 +       au_dpages_free(&dpages);
34417 +out:
34418 +       return err;
34419 +}
34420 +
34421 +static int au_wbr_bu(struct super_block *sb, aufs_bindex_t bindex)
34422 +{
34423 +       for (; bindex >= 0; bindex--)
34424 +               if (!au_br_rdonly(au_sbr(sb, bindex)))
34425 +                       return bindex;
34426 +       return -EROFS;
34427 +}
34428 +
34429 +/* top down parent */
34430 +static int au_wbr_create_tdp(struct dentry *dentry,
34431 +                            unsigned int flags __maybe_unused)
34432 +{
34433 +       int err;
34434 +       aufs_bindex_t btop, bindex;
34435 +       struct super_block *sb;
34436 +       struct dentry *parent, *h_parent;
34437 +
34438 +       sb = dentry->d_sb;
34439 +       btop = au_dbtop(dentry);
34440 +       err = btop;
34441 +       if (!au_br_rdonly(au_sbr(sb, btop)))
34442 +               goto out;
34443 +
34444 +       err = -EROFS;
34445 +       parent = dget_parent(dentry);
34446 +       for (bindex = au_dbtop(parent); bindex < btop; bindex++) {
34447 +               h_parent = au_h_dptr(parent, bindex);
34448 +               if (!h_parent || d_is_negative(h_parent))
34449 +                       continue;
34450 +
34451 +               if (!au_br_rdonly(au_sbr(sb, bindex))) {
34452 +                       err = bindex;
34453 +                       break;
34454 +               }
34455 +       }
34456 +       dput(parent);
34457 +
34458 +       /* bottom up here */
34459 +       if (unlikely(err < 0)) {
34460 +               err = au_wbr_bu(sb, btop - 1);
34461 +               if (err >= 0)
34462 +                       err = au_wbr_nonopq(dentry, err);
34463 +       }
34464 +
34465 +out:
34466 +       AuDbg("b%d\n", err);
34467 +       return err;
34468 +}
34469 +
34470 +/* ---------------------------------------------------------------------- */
34471 +
34472 +/* an exception for the policy other than tdp */
34473 +static int au_wbr_create_exp(struct dentry *dentry)
34474 +{
34475 +       int err;
34476 +       aufs_bindex_t bwh, bdiropq;
34477 +       struct dentry *parent;
34478 +
34479 +       err = -1;
34480 +       bwh = au_dbwh(dentry);
34481 +       parent = dget_parent(dentry);
34482 +       bdiropq = au_dbdiropq(parent);
34483 +       if (bwh >= 0) {
34484 +               if (bdiropq >= 0)
34485 +                       err = min(bdiropq, bwh);
34486 +               else
34487 +                       err = bwh;
34488 +               AuDbg("%d\n", err);
34489 +       } else if (bdiropq >= 0) {
34490 +               err = bdiropq;
34491 +               AuDbg("%d\n", err);
34492 +       }
34493 +       dput(parent);
34494 +
34495 +       if (err >= 0)
34496 +               err = au_wbr_nonopq(dentry, err);
34497 +
34498 +       if (err >= 0 && au_br_rdonly(au_sbr(dentry->d_sb, err)))
34499 +               err = -1;
34500 +
34501 +       AuDbg("%d\n", err);
34502 +       return err;
34503 +}
34504 +
34505 +/* ---------------------------------------------------------------------- */
34506 +
34507 +/* round robin */
34508 +static int au_wbr_create_init_rr(struct super_block *sb)
34509 +{
34510 +       int err;
34511 +
34512 +       err = au_wbr_bu(sb, au_sbbot(sb));
34513 +       atomic_set(&au_sbi(sb)->si_wbr_rr_next, -err); /* less important */
34514 +       /* smp_mb(); */
34515 +
34516 +       AuDbg("b%d\n", err);
34517 +       return err;
34518 +}
34519 +
34520 +static int au_wbr_create_rr(struct dentry *dentry, unsigned int flags)
34521 +{
34522 +       int err, nbr;
34523 +       unsigned int u;
34524 +       aufs_bindex_t bindex, bbot;
34525 +       struct super_block *sb;
34526 +       atomic_t *next;
34527 +
34528 +       err = au_wbr_create_exp(dentry);
34529 +       if (err >= 0)
34530 +               goto out;
34531 +
34532 +       sb = dentry->d_sb;
34533 +       next = &au_sbi(sb)->si_wbr_rr_next;
34534 +       bbot = au_sbbot(sb);
34535 +       nbr = bbot + 1;
34536 +       for (bindex = 0; bindex <= bbot; bindex++) {
34537 +               if (!au_ftest_wbr(flags, DIR)) {
34538 +                       err = atomic_dec_return(next) + 1;
34539 +                       /* modulo for 0 is meaningless */
34540 +                       if (unlikely(!err))
34541 +                               err = atomic_dec_return(next) + 1;
34542 +               } else
34543 +                       err = atomic_read(next);
34544 +               AuDbg("%d\n", err);
34545 +               u = err;
34546 +               err = u % nbr;
34547 +               AuDbg("%d\n", err);
34548 +               if (!au_br_rdonly(au_sbr(sb, err)))
34549 +                       break;
34550 +               err = -EROFS;
34551 +       }
34552 +
34553 +       if (err >= 0)
34554 +               err = au_wbr_nonopq(dentry, err);
34555 +
34556 +out:
34557 +       AuDbg("%d\n", err);
34558 +       return err;
34559 +}
34560 +
34561 +/* ---------------------------------------------------------------------- */
34562 +
34563 +/* most free space */
34564 +static void au_mfs(struct dentry *dentry, struct dentry *parent)
34565 +{
34566 +       struct super_block *sb;
34567 +       struct au_branch *br;
34568 +       struct au_wbr_mfs *mfs;
34569 +       struct dentry *h_parent;
34570 +       aufs_bindex_t bindex, bbot;
34571 +       int err;
34572 +       unsigned long long b, bavail;
34573 +       struct path h_path;
34574 +       /* reduce the stack usage */
34575 +       struct kstatfs *st;
34576 +
34577 +       st = kmalloc(sizeof(*st), GFP_NOFS);
34578 +       if (unlikely(!st)) {
34579 +               AuWarn1("failed updating mfs(%d), ignored\n", -ENOMEM);
34580 +               return;
34581 +       }
34582 +
34583 +       bavail = 0;
34584 +       sb = dentry->d_sb;
34585 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34586 +       MtxMustLock(&mfs->mfs_lock);
34587 +       mfs->mfs_bindex = -EROFS;
34588 +       mfs->mfsrr_bytes = 0;
34589 +       if (!parent) {
34590 +               bindex = 0;
34591 +               bbot = au_sbbot(sb);
34592 +       } else {
34593 +               bindex = au_dbtop(parent);
34594 +               bbot = au_dbtaildir(parent);
34595 +       }
34596 +
34597 +       for (; bindex <= bbot; bindex++) {
34598 +               if (parent) {
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))
34605 +                       continue;
34606 +
34607 +               /* sb->s_root for NFS is unreliable */
34608 +               h_path.mnt = au_br_mnt(br);
34609 +               h_path.dentry = h_path.mnt->mnt_root;
34610 +               err = vfs_statfs(&h_path, st);
34611 +               if (unlikely(err)) {
34612 +                       AuWarn1("failed statfs, b%d, %d\n", bindex, err);
34613 +                       continue;
34614 +               }
34615 +
34616 +               /* when the available size is equal, select the lower one */
34617 +               BUILD_BUG_ON(sizeof(b) < sizeof(st->f_bavail)
34618 +                            || sizeof(b) < sizeof(st->f_bsize));
34619 +               b = st->f_bavail * st->f_bsize;
34620 +               br->br_wbr->wbr_bytes = b;
34621 +               if (b >= bavail) {
34622 +                       bavail = b;
34623 +                       mfs->mfs_bindex = bindex;
34624 +                       mfs->mfs_jiffy = jiffies;
34625 +               }
34626 +       }
34627 +
34628 +       mfs->mfsrr_bytes = bavail;
34629 +       AuDbg("b%d\n", mfs->mfs_bindex);
34630 +       au_kfree_rcu(st);
34631 +}
34632 +
34633 +static int au_wbr_create_mfs(struct dentry *dentry, unsigned int flags)
34634 +{
34635 +       int err;
34636 +       struct dentry *parent;
34637 +       struct super_block *sb;
34638 +       struct au_wbr_mfs *mfs;
34639 +
34640 +       err = au_wbr_create_exp(dentry);
34641 +       if (err >= 0)
34642 +               goto out;
34643 +
34644 +       sb = dentry->d_sb;
34645 +       parent = NULL;
34646 +       if (au_ftest_wbr(flags, PARENT))
34647 +               parent = dget_parent(dentry);
34648 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34649 +       mutex_lock(&mfs->mfs_lock);
34650 +       if (time_after(jiffies, mfs->mfs_jiffy + mfs->mfs_expire)
34651 +           || mfs->mfs_bindex < 0
34652 +           || au_br_rdonly(au_sbr(sb, mfs->mfs_bindex)))
34653 +               au_mfs(dentry, parent);
34654 +       mutex_unlock(&mfs->mfs_lock);
34655 +       err = mfs->mfs_bindex;
34656 +       dput(parent);
34657 +
34658 +       if (err >= 0)
34659 +               err = au_wbr_nonopq(dentry, err);
34660 +
34661 +out:
34662 +       AuDbg("b%d\n", err);
34663 +       return err;
34664 +}
34665 +
34666 +static int au_wbr_create_init_mfs(struct super_block *sb)
34667 +{
34668 +       struct au_wbr_mfs *mfs;
34669 +
34670 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34671 +       mutex_init(&mfs->mfs_lock);
34672 +       mfs->mfs_jiffy = 0;
34673 +       mfs->mfs_bindex = -EROFS;
34674 +
34675 +       return 0;
34676 +}
34677 +
34678 +static int au_wbr_create_fin_mfs(struct super_block *sb __maybe_unused)
34679 +{
34680 +       mutex_destroy(&au_sbi(sb)->si_wbr_mfs.mfs_lock);
34681 +       return 0;
34682 +}
34683 +
34684 +/* ---------------------------------------------------------------------- */
34685 +
34686 +/* top down regardless parent, and then mfs */
34687 +static int au_wbr_create_tdmfs(struct dentry *dentry,
34688 +                              unsigned int flags __maybe_unused)
34689 +{
34690 +       int err;
34691 +       aufs_bindex_t bwh, btail, bindex, bfound, bmfs;
34692 +       unsigned long long watermark;
34693 +       struct super_block *sb;
34694 +       struct au_wbr_mfs *mfs;
34695 +       struct au_branch *br;
34696 +       struct dentry *parent;
34697 +
34698 +       sb = dentry->d_sb;
34699 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34700 +       mutex_lock(&mfs->mfs_lock);
34701 +       if (time_after(jiffies, mfs->mfs_jiffy + mfs->mfs_expire)
34702 +           || mfs->mfs_bindex < 0)
34703 +               au_mfs(dentry, /*parent*/NULL);
34704 +       watermark = mfs->mfsrr_watermark;
34705 +       bmfs = mfs->mfs_bindex;
34706 +       mutex_unlock(&mfs->mfs_lock);
34707 +
34708 +       /* another style of au_wbr_create_exp() */
34709 +       bwh = au_dbwh(dentry);
34710 +       parent = dget_parent(dentry);
34711 +       btail = au_dbtaildir(parent);
34712 +       if (bwh >= 0 && bwh < btail)
34713 +               btail = bwh;
34714 +
34715 +       err = au_wbr_nonopq(dentry, btail);
34716 +       if (unlikely(err < 0))
34717 +               goto out;
34718 +       btail = err;
34719 +       bfound = -1;
34720 +       for (bindex = 0; bindex <= btail; bindex++) {
34721 +               br = au_sbr(sb, bindex);
34722 +               if (au_br_rdonly(br))
34723 +                       continue;
34724 +               if (br->br_wbr->wbr_bytes > watermark) {
34725 +                       bfound = bindex;
34726 +                       break;
34727 +               }
34728 +       }
34729 +       err = bfound;
34730 +       if (err < 0)
34731 +               err = bmfs;
34732 +
34733 +out:
34734 +       dput(parent);
34735 +       AuDbg("b%d\n", err);
34736 +       return err;
34737 +}
34738 +
34739 +/* ---------------------------------------------------------------------- */
34740 +
34741 +/* most free space and then round robin */
34742 +static int au_wbr_create_mfsrr(struct dentry *dentry, unsigned int flags)
34743 +{
34744 +       int err;
34745 +       struct au_wbr_mfs *mfs;
34746 +
34747 +       err = au_wbr_create_mfs(dentry, flags);
34748 +       if (err >= 0) {
34749 +               mfs = &au_sbi(dentry->d_sb)->si_wbr_mfs;
34750 +               mutex_lock(&mfs->mfs_lock);
34751 +               if (mfs->mfsrr_bytes < mfs->mfsrr_watermark)
34752 +                       err = au_wbr_create_rr(dentry, flags);
34753 +               mutex_unlock(&mfs->mfs_lock);
34754 +       }
34755 +
34756 +       AuDbg("b%d\n", err);
34757 +       return err;
34758 +}
34759 +
34760 +static int au_wbr_create_init_mfsrr(struct super_block *sb)
34761 +{
34762 +       int err;
34763 +
34764 +       au_wbr_create_init_mfs(sb); /* ignore */
34765 +       err = au_wbr_create_init_rr(sb);
34766 +
34767 +       return err;
34768 +}
34769 +
34770 +/* ---------------------------------------------------------------------- */
34771 +
34772 +/* top down parent and most free space */
34773 +static int au_wbr_create_pmfs(struct dentry *dentry, unsigned int flags)
34774 +{
34775 +       int err, e2;
34776 +       unsigned long long b;
34777 +       aufs_bindex_t bindex, btop, bbot;
34778 +       struct super_block *sb;
34779 +       struct dentry *parent, *h_parent;
34780 +       struct au_branch *br;
34781 +
34782 +       err = au_wbr_create_tdp(dentry, flags);
34783 +       if (unlikely(err < 0))
34784 +               goto out;
34785 +       parent = dget_parent(dentry);
34786 +       btop = au_dbtop(parent);
34787 +       bbot = au_dbtaildir(parent);
34788 +       if (btop == bbot)
34789 +               goto out_parent; /* success */
34790 +
34791 +       e2 = au_wbr_create_mfs(dentry, flags);
34792 +       if (e2 < 0)
34793 +               goto out_parent; /* success */
34794 +
34795 +       /* when the available size is equal, select upper one */
34796 +       sb = dentry->d_sb;
34797 +       br = au_sbr(sb, err);
34798 +       b = br->br_wbr->wbr_bytes;
34799 +       AuDbg("b%d, %llu\n", err, b);
34800 +
34801 +       for (bindex = btop; bindex <= bbot; bindex++) {
34802 +               h_parent = au_h_dptr(parent, bindex);
34803 +               if (!h_parent || d_is_negative(h_parent))
34804 +                       continue;
34805 +
34806 +               br = au_sbr(sb, bindex);
34807 +               if (!au_br_rdonly(br) && br->br_wbr->wbr_bytes > b) {
34808 +                       b = br->br_wbr->wbr_bytes;
34809 +                       err = bindex;
34810 +                       AuDbg("b%d, %llu\n", err, b);
34811 +               }
34812 +       }
34813 +
34814 +       if (err >= 0)
34815 +               err = au_wbr_nonopq(dentry, err);
34816 +
34817 +out_parent:
34818 +       dput(parent);
34819 +out:
34820 +       AuDbg("b%d\n", err);
34821 +       return err;
34822 +}
34823 +
34824 +/* ---------------------------------------------------------------------- */
34825 +
34826 +/*
34827 + * - top down parent
34828 + * - most free space with parent
34829 + * - most free space round-robin regardless parent
34830 + */
34831 +static int au_wbr_create_pmfsrr(struct dentry *dentry, unsigned int flags)
34832 +{
34833 +       int err;
34834 +       unsigned long long watermark;
34835 +       struct super_block *sb;
34836 +       struct au_branch *br;
34837 +       struct au_wbr_mfs *mfs;
34838 +
34839 +       err = au_wbr_create_pmfs(dentry, flags | AuWbr_PARENT);
34840 +       if (unlikely(err < 0))
34841 +               goto out;
34842 +
34843 +       sb = dentry->d_sb;
34844 +       br = au_sbr(sb, err);
34845 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34846 +       mutex_lock(&mfs->mfs_lock);
34847 +       watermark = mfs->mfsrr_watermark;
34848 +       mutex_unlock(&mfs->mfs_lock);
34849 +       if (br->br_wbr->wbr_bytes < watermark)
34850 +               /* regardless the parent dir */
34851 +               err = au_wbr_create_mfsrr(dentry, flags);
34852 +
34853 +out:
34854 +       AuDbg("b%d\n", err);
34855 +       return err;
34856 +}
34857 +
34858 +/* ---------------------------------------------------------------------- */
34859 +
34860 +/* policies for copyup */
34861 +
34862 +/* top down parent */
34863 +static int au_wbr_copyup_tdp(struct dentry *dentry)
34864 +{
34865 +       return au_wbr_create_tdp(dentry, /*flags, anything is ok*/0);
34866 +}
34867 +
34868 +/* bottom up parent */
34869 +static int au_wbr_copyup_bup(struct dentry *dentry)
34870 +{
34871 +       int err;
34872 +       aufs_bindex_t bindex, btop;
34873 +       struct dentry *parent, *h_parent;
34874 +       struct super_block *sb;
34875 +
34876 +       err = -EROFS;
34877 +       sb = dentry->d_sb;
34878 +       parent = dget_parent(dentry);
34879 +       btop = au_dbtop(parent);
34880 +       for (bindex = au_dbtop(dentry); bindex >= btop; bindex--) {
34881 +               h_parent = au_h_dptr(parent, bindex);
34882 +               if (!h_parent || d_is_negative(h_parent))
34883 +                       continue;
34884 +
34885 +               if (!au_br_rdonly(au_sbr(sb, bindex))) {
34886 +                       err = bindex;
34887 +                       break;
34888 +               }
34889 +       }
34890 +       dput(parent);
34891 +
34892 +       /* bottom up here */
34893 +       if (unlikely(err < 0))
34894 +               err = au_wbr_bu(sb, btop - 1);
34895 +
34896 +       AuDbg("b%d\n", err);
34897 +       return err;
34898 +}
34899 +
34900 +/* bottom up */
34901 +int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t btop)
34902 +{
34903 +       int err;
34904 +
34905 +       err = au_wbr_bu(dentry->d_sb, btop);
34906 +       AuDbg("b%d\n", err);
34907 +       if (err > btop)
34908 +               err = au_wbr_nonopq(dentry, err);
34909 +
34910 +       AuDbg("b%d\n", err);
34911 +       return err;
34912 +}
34913 +
34914 +static int au_wbr_copyup_bu(struct dentry *dentry)
34915 +{
34916 +       int err;
34917 +       aufs_bindex_t btop;
34918 +
34919 +       btop = au_dbtop(dentry);
34920 +       err = au_wbr_do_copyup_bu(dentry, btop);
34921 +       return err;
34922 +}
34923 +
34924 +/* ---------------------------------------------------------------------- */
34925 +
34926 +struct au_wbr_copyup_operations au_wbr_copyup_ops[] = {
34927 +       [AuWbrCopyup_TDP] = {
34928 +               .copyup = au_wbr_copyup_tdp
34929 +       },
34930 +       [AuWbrCopyup_BUP] = {
34931 +               .copyup = au_wbr_copyup_bup
34932 +       },
34933 +       [AuWbrCopyup_BU] = {
34934 +               .copyup = au_wbr_copyup_bu
34935 +       }
34936 +};
34937 +
34938 +struct au_wbr_create_operations au_wbr_create_ops[] = {
34939 +       [AuWbrCreate_TDP] = {
34940 +               .create = au_wbr_create_tdp
34941 +       },
34942 +       [AuWbrCreate_RR] = {
34943 +               .create = au_wbr_create_rr,
34944 +               .init   = au_wbr_create_init_rr
34945 +       },
34946 +       [AuWbrCreate_MFS] = {
34947 +               .create = au_wbr_create_mfs,
34948 +               .init   = au_wbr_create_init_mfs,
34949 +               .fin    = au_wbr_create_fin_mfs
34950 +       },
34951 +       [AuWbrCreate_MFSV] = {
34952 +               .create = au_wbr_create_mfs,
34953 +               .init   = au_wbr_create_init_mfs,
34954 +               .fin    = au_wbr_create_fin_mfs
34955 +       },
34956 +       [AuWbrCreate_MFSRR] = {
34957 +               .create = au_wbr_create_mfsrr,
34958 +               .init   = au_wbr_create_init_mfsrr,
34959 +               .fin    = au_wbr_create_fin_mfs
34960 +       },
34961 +       [AuWbrCreate_MFSRRV] = {
34962 +               .create = au_wbr_create_mfsrr,
34963 +               .init   = au_wbr_create_init_mfsrr,
34964 +               .fin    = au_wbr_create_fin_mfs
34965 +       },
34966 +       [AuWbrCreate_TDMFS] = {
34967 +               .create = au_wbr_create_tdmfs,
34968 +               .init   = au_wbr_create_init_mfs,
34969 +               .fin    = au_wbr_create_fin_mfs
34970 +       },
34971 +       [AuWbrCreate_TDMFSV] = {
34972 +               .create = au_wbr_create_tdmfs,
34973 +               .init   = au_wbr_create_init_mfs,
34974 +               .fin    = au_wbr_create_fin_mfs
34975 +       },
34976 +       [AuWbrCreate_PMFS] = {
34977 +               .create = au_wbr_create_pmfs,
34978 +               .init   = au_wbr_create_init_mfs,
34979 +               .fin    = au_wbr_create_fin_mfs
34980 +       },
34981 +       [AuWbrCreate_PMFSV] = {
34982 +               .create = au_wbr_create_pmfs,
34983 +               .init   = au_wbr_create_init_mfs,
34984 +               .fin    = au_wbr_create_fin_mfs
34985 +       },
34986 +       [AuWbrCreate_PMFSRR] = {
34987 +               .create = au_wbr_create_pmfsrr,
34988 +               .init   = au_wbr_create_init_mfsrr,
34989 +               .fin    = au_wbr_create_fin_mfs
34990 +       },
34991 +       [AuWbrCreate_PMFSRRV] = {
34992 +               .create = au_wbr_create_pmfsrr,
34993 +               .init   = au_wbr_create_init_mfsrr,
34994 +               .fin    = au_wbr_create_fin_mfs
34995 +       }
34996 +};
34997 diff -urN /usr/share/empty/fs/aufs/whout.c linux/fs/aufs/whout.c
34998 --- /usr/share/empty/fs/aufs/whout.c    1970-01-01 01:00:00.000000000 +0100
34999 +++ linux/fs/aufs/whout.c       2022-03-21 14:49:05.733299677 +0100
35000 @@ -0,0 +1,1072 @@
35001 +// SPDX-License-Identifier: GPL-2.0
35002 +/*
35003 + * Copyright (C) 2005-2021 Junjiro R. Okajima
35004 + *
35005 + * This program, aufs is free software; you can redistribute it and/or modify
35006 + * it under the terms of the GNU General Public License as published by
35007 + * the Free Software Foundation; either version 2 of the License, or
35008 + * (at your option) any later version.
35009 + *
35010 + * This program is distributed in the hope that it will be useful,
35011 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
35012 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
35013 + * GNU General Public License for more details.
35014 + *
35015 + * You should have received a copy of the GNU General Public License
35016 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
35017 + */
35018 +
35019 +/*
35020 + * whiteout for logical deletion and opaque directory
35021 + */
35022 +
35023 +#include "aufs.h"
35024 +
35025 +#define WH_MASK                        0444
35026 +
35027 +/*
35028 + * If a directory contains this file, then it is opaque.  We start with the
35029 + * .wh. flag so that it is blocked by lookup.
35030 + */
35031 +static struct qstr diropq_name = QSTR_INIT(AUFS_WH_DIROPQ,
35032 +                                          sizeof(AUFS_WH_DIROPQ) - 1);
35033 +
35034 +/*
35035 + * generate whiteout name, which is NOT terminated by NULL.
35036 + * @name: original d_name.name
35037 + * @len: original d_name.len
35038 + * @wh: whiteout qstr
35039 + * returns zero when succeeds, otherwise error.
35040 + * succeeded value as wh->name should be freed by kfree().
35041 + */
35042 +int au_wh_name_alloc(struct qstr *wh, const struct qstr *name)
35043 +{
35044 +       char *p;
35045 +
35046 +       if (unlikely(name->len > PATH_MAX - AUFS_WH_PFX_LEN))
35047 +               return -ENAMETOOLONG;
35048 +
35049 +       wh->len = name->len + AUFS_WH_PFX_LEN;
35050 +       p = kmalloc(wh->len, GFP_NOFS);
35051 +       wh->name = p;
35052 +       if (p) {
35053 +               memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
35054 +               memcpy(p + AUFS_WH_PFX_LEN, name->name, name->len);
35055 +               /* smp_mb(); */
35056 +               return 0;
35057 +       }
35058 +       return -ENOMEM;
35059 +}
35060 +
35061 +/* ---------------------------------------------------------------------- */
35062 +
35063 +/*
35064 + * test if the @wh_name exists under @h_ppath.
35065 + * @try_sio specifies the necessary of super-io.
35066 + */
35067 +int au_wh_test(struct user_namespace *h_userns, struct path *h_ppath,
35068 +              struct qstr *wh_name, int try_sio)
35069 +{
35070 +       int err;
35071 +       struct dentry *wh_dentry;
35072 +
35073 +       if (!try_sio)
35074 +               wh_dentry = vfsub_lkup_one(wh_name, h_ppath);
35075 +       else
35076 +               wh_dentry = au_sio_lkup_one(h_userns, wh_name, h_ppath);
35077 +       err = PTR_ERR(wh_dentry);
35078 +       if (IS_ERR(wh_dentry)) {
35079 +               if (err == -ENAMETOOLONG)
35080 +                       err = 0;
35081 +               goto out;
35082 +       }
35083 +
35084 +       err = 0;
35085 +       if (d_is_negative(wh_dentry))
35086 +               goto out_wh; /* success */
35087 +
35088 +       err = 1;
35089 +       if (d_is_reg(wh_dentry))
35090 +               goto out_wh; /* success */
35091 +
35092 +       err = -EIO;
35093 +       AuIOErr("%pd Invalid whiteout entry type 0%o.\n",
35094 +               wh_dentry, d_inode(wh_dentry)->i_mode);
35095 +
35096 +out_wh:
35097 +       dput(wh_dentry);
35098 +out:
35099 +       return err;
35100 +}
35101 +
35102 +/*
35103 + * test if the @h_path->dentry sets opaque or not.
35104 + */
35105 +int au_diropq_test(struct user_namespace *h_userns, struct path *h_path)
35106 +{
35107 +       int err;
35108 +       struct inode *h_dir;
35109 +
35110 +       h_dir = d_inode(h_path->dentry);
35111 +       err = au_wh_test(h_userns, h_path, &diropq_name,
35112 +                        au_test_h_perm_sio(h_userns, h_dir, MAY_EXEC));
35113 +       return err;
35114 +}
35115 +
35116 +/*
35117 + * returns a negative dentry whose name is unique and temporary.
35118 + */
35119 +struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
35120 +                            struct qstr *prefix)
35121 +{
35122 +       struct dentry *dentry;
35123 +       int i;
35124 +       char defname[NAME_MAX - AUFS_MAX_NAMELEN + DNAME_INLINE_LEN + 1],
35125 +               *name, *p;
35126 +       /* strict atomic_t is unnecessary here */
35127 +       static unsigned short cnt;
35128 +       struct qstr qs;
35129 +       struct path h_ppath;
35130 +       struct user_namespace *h_userns;
35131 +
35132 +       BUILD_BUG_ON(sizeof(cnt) * 2 > AUFS_WH_TMP_LEN);
35133 +
35134 +       name = defname;
35135 +       qs.len = sizeof(defname) - DNAME_INLINE_LEN + prefix->len - 1;
35136 +       if (unlikely(prefix->len > DNAME_INLINE_LEN)) {
35137 +               dentry = ERR_PTR(-ENAMETOOLONG);
35138 +               if (unlikely(qs.len > NAME_MAX))
35139 +                       goto out;
35140 +               dentry = ERR_PTR(-ENOMEM);
35141 +               name = kmalloc(qs.len + 1, GFP_NOFS);
35142 +               if (unlikely(!name))
35143 +                       goto out;
35144 +       }
35145 +
35146 +       /* doubly whiteout-ed */
35147 +       memcpy(name, AUFS_WH_PFX AUFS_WH_PFX, AUFS_WH_PFX_LEN * 2);
35148 +       p = name + AUFS_WH_PFX_LEN * 2;
35149 +       memcpy(p, prefix->name, prefix->len);
35150 +       p += prefix->len;
35151 +       *p++ = '.';
35152 +       AuDebugOn(name + qs.len + 1 - p <= AUFS_WH_TMP_LEN);
35153 +
35154 +       h_ppath.dentry = h_parent;
35155 +       h_ppath.mnt = au_br_mnt(br);
35156 +       h_userns = au_br_userns(br);
35157 +       qs.name = name;
35158 +       for (i = 0; i < 3; i++) {
35159 +               sprintf(p, "%.*x", AUFS_WH_TMP_LEN, cnt++);
35160 +               dentry = au_sio_lkup_one(h_userns, &qs, &h_ppath);
35161 +               if (IS_ERR(dentry) || d_is_negative(dentry))
35162 +                       goto out_name;
35163 +               dput(dentry);
35164 +       }
35165 +       /* pr_warn("could not get random name\n"); */
35166 +       dentry = ERR_PTR(-EEXIST);
35167 +       AuDbg("%.*s\n", AuLNPair(&qs));
35168 +       BUG();
35169 +
35170 +out_name:
35171 +       if (name != defname)
35172 +               au_kfree_try_rcu(name);
35173 +out:
35174 +       AuTraceErrPtr(dentry);
35175 +       return dentry;
35176 +}
35177 +
35178 +/*
35179 + * rename the @h_dentry on @br to the whiteouted temporary name.
35180 + */
35181 +int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br)
35182 +{
35183 +       int err;
35184 +       struct path h_path = {
35185 +               .mnt = au_br_mnt(br)
35186 +       };
35187 +       struct inode *h_dir, *delegated;
35188 +       struct dentry *h_parent;
35189 +
35190 +       h_parent = h_dentry->d_parent; /* dir inode is locked */
35191 +       h_dir = d_inode(h_parent);
35192 +       IMustLock(h_dir);
35193 +
35194 +       h_path.dentry = au_whtmp_lkup(h_parent, br, &h_dentry->d_name);
35195 +       err = PTR_ERR(h_path.dentry);
35196 +       if (IS_ERR(h_path.dentry))
35197 +               goto out;
35198 +
35199 +       /* under the same dir, no need to lock_rename() */
35200 +       delegated = NULL;
35201 +       err = vfsub_rename(h_dir, h_dentry, h_dir, &h_path, &delegated,
35202 +                          /*flags*/0);
35203 +       AuTraceErr(err);
35204 +       if (unlikely(err == -EWOULDBLOCK)) {
35205 +               pr_warn("cannot retry for NFSv4 delegation"
35206 +                       " for an internal rename\n");
35207 +               iput(delegated);
35208 +       }
35209 +       dput(h_path.dentry);
35210 +
35211 +out:
35212 +       AuTraceErr(err);
35213 +       return err;
35214 +}
35215 +
35216 +/* ---------------------------------------------------------------------- */
35217 +/*
35218 + * functions for removing a whiteout
35219 + */
35220 +
35221 +static int do_unlink_wh(struct inode *h_dir, struct path *h_path)
35222 +{
35223 +       int err, force;
35224 +       struct inode *delegated;
35225 +
35226 +       /*
35227 +        * forces superio when the dir has a sticky bit.
35228 +        * this may be a violation of unix fs semantics.
35229 +        */
35230 +       force = (h_dir->i_mode & S_ISVTX)
35231 +               && !uid_eq(current_fsuid(), d_inode(h_path->dentry)->i_uid);
35232 +       delegated = NULL;
35233 +       err = vfsub_unlink(h_dir, h_path, &delegated, force);
35234 +       if (unlikely(err == -EWOULDBLOCK)) {
35235 +               pr_warn("cannot retry for NFSv4 delegation"
35236 +                       " for an internal unlink\n");
35237 +               iput(delegated);
35238 +       }
35239 +       return err;
35240 +}
35241 +
35242 +int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
35243 +                       struct dentry *dentry)
35244 +{
35245 +       int err;
35246 +
35247 +       err = do_unlink_wh(h_dir, h_path);
35248 +       if (!err && dentry)
35249 +               au_set_dbwh(dentry, -1);
35250 +
35251 +       return err;
35252 +}
35253 +
35254 +static int unlink_wh_name(struct path *h_ppath, struct qstr *wh)
35255 +{
35256 +       int err;
35257 +       struct path h_path;
35258 +
35259 +       err = 0;
35260 +       h_path.dentry = vfsub_lkup_one(wh, h_ppath);
35261 +       if (IS_ERR(h_path.dentry))
35262 +               err = PTR_ERR(h_path.dentry);
35263 +       else {
35264 +               if (d_is_reg(h_path.dentry)) {
35265 +                       h_path.mnt = h_ppath->mnt;
35266 +                       err = do_unlink_wh(d_inode(h_ppath->dentry), &h_path);
35267 +               }
35268 +               dput(h_path.dentry);
35269 +       }
35270 +
35271 +       return err;
35272 +}
35273 +
35274 +/* ---------------------------------------------------------------------- */
35275 +/*
35276 + * initialize/clean whiteout for a branch
35277 + */
35278 +
35279 +static void au_wh_clean(struct inode *h_dir, struct path *whpath,
35280 +                       const int isdir)
35281 +{
35282 +       int err;
35283 +       struct inode *delegated;
35284 +
35285 +       if (d_is_negative(whpath->dentry))
35286 +               return;
35287 +
35288 +       if (isdir)
35289 +               err = vfsub_rmdir(h_dir, whpath);
35290 +       else {
35291 +               delegated = NULL;
35292 +               err = vfsub_unlink(h_dir, whpath, &delegated, /*force*/0);
35293 +               if (unlikely(err == -EWOULDBLOCK)) {
35294 +                       pr_warn("cannot retry for NFSv4 delegation"
35295 +                               " for an internal unlink\n");
35296 +                       iput(delegated);
35297 +               }
35298 +       }
35299 +       if (unlikely(err))
35300 +               pr_warn("failed removing %pd (%d), ignored.\n",
35301 +                       whpath->dentry, err);
35302 +}
35303 +
35304 +static int test_linkable(struct dentry *h_root)
35305 +{
35306 +       struct inode *h_dir = d_inode(h_root);
35307 +
35308 +       if (h_dir->i_op->link)
35309 +               return 0;
35310 +
35311 +       pr_err("%pd (%s) doesn't support link(2), use noplink and rw+nolwh\n",
35312 +              h_root, au_sbtype(h_root->d_sb));
35313 +       return -ENOSYS; /* the branch doesn't have its ->link() */
35314 +}
35315 +
35316 +/* todo: should this mkdir be done in /sbin/mount.aufs helper? */
35317 +static int au_whdir(struct inode *h_dir, struct path *path)
35318 +{
35319 +       int err;
35320 +
35321 +       err = -EEXIST;
35322 +       if (d_is_negative(path->dentry)) {
35323 +               int mode = 0700;
35324 +
35325 +               if (au_test_nfs(path->dentry->d_sb))
35326 +                       mode |= 0111;
35327 +               err = vfsub_mkdir(h_dir, path, mode);
35328 +       } else if (d_is_dir(path->dentry))
35329 +               err = 0;
35330 +       else
35331 +               pr_err("unknown %pd exists\n", path->dentry);
35332 +
35333 +       return err;
35334 +}
35335 +
35336 +struct au_wh_base {
35337 +       const struct qstr *name;
35338 +       struct dentry *dentry;
35339 +};
35340 +
35341 +static void au_wh_init_ro(struct inode *h_dir, struct au_wh_base base[],
35342 +                         struct path *h_path)
35343 +{
35344 +       h_path->dentry = base[AuBrWh_BASE].dentry;
35345 +       au_wh_clean(h_dir, h_path, /*isdir*/0);
35346 +       h_path->dentry = base[AuBrWh_PLINK].dentry;
35347 +       au_wh_clean(h_dir, h_path, /*isdir*/1);
35348 +       h_path->dentry = base[AuBrWh_ORPH].dentry;
35349 +       au_wh_clean(h_dir, h_path, /*isdir*/1);
35350 +}
35351 +
35352 +/*
35353 + * returns tri-state,
35354 + * minus: error, caller should print the message
35355 + * zero: success
35356 + * plus: error, caller should NOT print the message
35357 + */
35358 +static int au_wh_init_rw_nolink(struct dentry *h_root, struct au_wbr *wbr,
35359 +                               int do_plink, struct au_wh_base base[],
35360 +                               struct path *h_path)
35361 +{
35362 +       int err;
35363 +       struct inode *h_dir;
35364 +
35365 +       h_dir = d_inode(h_root);
35366 +       h_path->dentry = base[AuBrWh_BASE].dentry;
35367 +       au_wh_clean(h_dir, h_path, /*isdir*/0);
35368 +       h_path->dentry = base[AuBrWh_PLINK].dentry;
35369 +       if (do_plink) {
35370 +               err = test_linkable(h_root);
35371 +               if (unlikely(err)) {
35372 +                       err = 1;
35373 +                       goto out;
35374 +               }
35375 +
35376 +               err = au_whdir(h_dir, h_path);
35377 +               if (unlikely(err))
35378 +                       goto out;
35379 +               wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
35380 +       } else
35381 +               au_wh_clean(h_dir, h_path, /*isdir*/1);
35382 +       h_path->dentry = base[AuBrWh_ORPH].dentry;
35383 +       err = au_whdir(h_dir, h_path);
35384 +       if (unlikely(err))
35385 +               goto out;
35386 +       wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
35387 +
35388 +out:
35389 +       return err;
35390 +}
35391 +
35392 +/*
35393 + * for the moment, aufs supports the branch filesystem which does not support
35394 + * link(2). testing on FAT which does not support i_op->setattr() fully either,
35395 + * copyup failed. finally, such filesystem will not be used as the writable
35396 + * branch.
35397 + *
35398 + * returns tri-state, see above.
35399 + */
35400 +static int au_wh_init_rw(struct dentry *h_root, struct au_wbr *wbr,
35401 +                        int do_plink, struct au_wh_base base[],
35402 +                        struct path *h_path)
35403 +{
35404 +       int err;
35405 +       struct inode *h_dir;
35406 +
35407 +       WbrWhMustWriteLock(wbr);
35408 +
35409 +       err = test_linkable(h_root);
35410 +       if (unlikely(err)) {
35411 +               err = 1;
35412 +               goto out;
35413 +       }
35414 +
35415 +       /*
35416 +        * todo: should this create be done in /sbin/mount.aufs helper?
35417 +        */
35418 +       err = -EEXIST;
35419 +       h_dir = d_inode(h_root);
35420 +       if (d_is_negative(base[AuBrWh_BASE].dentry)) {
35421 +               h_path->dentry = base[AuBrWh_BASE].dentry;
35422 +               err = vfsub_create(h_dir, h_path, WH_MASK, /*want_excl*/true);
35423 +       } else if (d_is_reg(base[AuBrWh_BASE].dentry))
35424 +               err = 0;
35425 +       else
35426 +               pr_err("unknown %pd2 exists\n", base[AuBrWh_BASE].dentry);
35427 +       if (unlikely(err))
35428 +               goto out;
35429 +
35430 +       h_path->dentry = base[AuBrWh_PLINK].dentry;
35431 +       if (do_plink) {
35432 +               err = au_whdir(h_dir, h_path);
35433 +               if (unlikely(err))
35434 +                       goto out;
35435 +               wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
35436 +       } else
35437 +               au_wh_clean(h_dir, h_path, /*isdir*/1);
35438 +       wbr->wbr_whbase = dget(base[AuBrWh_BASE].dentry);
35439 +
35440 +       h_path->dentry = base[AuBrWh_ORPH].dentry;
35441 +       err = au_whdir(h_dir, h_path);
35442 +       if (unlikely(err))
35443 +               goto out;
35444 +       wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
35445 +
35446 +out:
35447 +       return err;
35448 +}
35449 +
35450 +/*
35451 + * initialize the whiteout base file/dir for @br.
35452 + */
35453 +int au_wh_init(struct au_branch *br, struct super_block *sb)
35454 +{
35455 +       int err, i;
35456 +       const unsigned char do_plink
35457 +               = !!au_opt_test(au_mntflags(sb), PLINK);
35458 +       struct inode *h_dir;
35459 +       struct path path = br->br_path;
35460 +       struct dentry *h_root = path.dentry;
35461 +       struct au_wbr *wbr = br->br_wbr;
35462 +       static const struct qstr base_name[] = {
35463 +               [AuBrWh_BASE] = QSTR_INIT(AUFS_BASE_NAME,
35464 +                                         sizeof(AUFS_BASE_NAME) - 1),
35465 +               [AuBrWh_PLINK] = QSTR_INIT(AUFS_PLINKDIR_NAME,
35466 +                                          sizeof(AUFS_PLINKDIR_NAME) - 1),
35467 +               [AuBrWh_ORPH] = QSTR_INIT(AUFS_ORPHDIR_NAME,
35468 +                                         sizeof(AUFS_ORPHDIR_NAME) - 1)
35469 +       };
35470 +       struct au_wh_base base[] = {
35471 +               [AuBrWh_BASE] = {
35472 +                       .name   = base_name + AuBrWh_BASE,
35473 +                       .dentry = NULL
35474 +               },
35475 +               [AuBrWh_PLINK] = {
35476 +                       .name   = base_name + AuBrWh_PLINK,
35477 +                       .dentry = NULL
35478 +               },
35479 +               [AuBrWh_ORPH] = {
35480 +                       .name   = base_name + AuBrWh_ORPH,
35481 +                       .dentry = NULL
35482 +               }
35483 +       };
35484 +
35485 +       if (wbr)
35486 +               WbrWhMustWriteLock(wbr);
35487 +
35488 +       for (i = 0; i < AuBrWh_Last; i++) {
35489 +               /* doubly whiteouted */
35490 +               struct dentry *d;
35491 +
35492 +               d = au_wh_lkup(h_root, (void *)base[i].name, br);
35493 +               err = PTR_ERR(d);
35494 +               if (IS_ERR(d))
35495 +                       goto out;
35496 +
35497 +               base[i].dentry = d;
35498 +               AuDebugOn(wbr
35499 +                         && wbr->wbr_wh[i]
35500 +                         && wbr->wbr_wh[i] != base[i].dentry);
35501 +       }
35502 +
35503 +       if (wbr)
35504 +               for (i = 0; i < AuBrWh_Last; i++) {
35505 +                       dput(wbr->wbr_wh[i]);
35506 +                       wbr->wbr_wh[i] = NULL;
35507 +               }
35508 +
35509 +       err = 0;
35510 +       if (!au_br_writable(br->br_perm)) {
35511 +               h_dir = d_inode(h_root);
35512 +               au_wh_init_ro(h_dir, base, &path);
35513 +       } else if (!au_br_wh_linkable(br->br_perm)) {
35514 +               err = au_wh_init_rw_nolink(h_root, wbr, do_plink, base, &path);
35515 +               if (err > 0)
35516 +                       goto out;
35517 +               else if (err)
35518 +                       goto out_err;
35519 +       } else {
35520 +               err = au_wh_init_rw(h_root, wbr, do_plink, base, &path);
35521 +               if (err > 0)
35522 +                       goto out;
35523 +               else if (err)
35524 +                       goto out_err;
35525 +       }
35526 +       goto out; /* success */
35527 +
35528 +out_err:
35529 +       pr_err("an error(%d) on the writable branch %pd(%s)\n",
35530 +              err, h_root, au_sbtype(h_root->d_sb));
35531 +out:
35532 +       for (i = 0; i < AuBrWh_Last; i++)
35533 +               dput(base[i].dentry);
35534 +       return err;
35535 +}
35536 +
35537 +/* ---------------------------------------------------------------------- */
35538 +/*
35539 + * whiteouts are all hard-linked usually.
35540 + * when its link count reaches a ceiling, we create a new whiteout base
35541 + * asynchronously.
35542 + */
35543 +
35544 +struct reinit_br_wh {
35545 +       struct super_block *sb;
35546 +       struct au_branch *br;
35547 +};
35548 +
35549 +static void reinit_br_wh(void *arg)
35550 +{
35551 +       int err;
35552 +       aufs_bindex_t bindex;
35553 +       struct path h_path;
35554 +       struct reinit_br_wh *a = arg;
35555 +       struct au_wbr *wbr;
35556 +       struct inode *dir, *delegated;
35557 +       struct dentry *h_root;
35558 +       struct au_hinode *hdir;
35559 +
35560 +       err = 0;
35561 +       wbr = a->br->br_wbr;
35562 +       /* big aufs lock */
35563 +       si_noflush_write_lock(a->sb);
35564 +       if (!au_br_writable(a->br->br_perm))
35565 +               goto out;
35566 +       bindex = au_br_index(a->sb, a->br->br_id);
35567 +       if (unlikely(bindex < 0))
35568 +               goto out;
35569 +
35570 +       di_read_lock_parent(a->sb->s_root, AuLock_IR);
35571 +       dir = d_inode(a->sb->s_root);
35572 +       hdir = au_hi(dir, bindex);
35573 +       h_root = au_h_dptr(a->sb->s_root, bindex);
35574 +       AuDebugOn(h_root != au_br_dentry(a->br));
35575 +
35576 +       au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
35577 +       wbr_wh_write_lock(wbr);
35578 +       err = au_h_verify(wbr->wbr_whbase, au_opt_udba(a->sb), hdir->hi_inode,
35579 +                         h_root, a->br);
35580 +       if (!err) {
35581 +               h_path.dentry = wbr->wbr_whbase;
35582 +               h_path.mnt = au_br_mnt(a->br);
35583 +               delegated = NULL;
35584 +               err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated,
35585 +                                  /*force*/0);
35586 +               if (unlikely(err == -EWOULDBLOCK)) {
35587 +                       pr_warn("cannot retry for NFSv4 delegation"
35588 +                               " for an internal unlink\n");
35589 +                       iput(delegated);
35590 +               }
35591 +       } else {
35592 +               pr_warn("%pd is moved, ignored\n", wbr->wbr_whbase);
35593 +               err = 0;
35594 +       }
35595 +       dput(wbr->wbr_whbase);
35596 +       wbr->wbr_whbase = NULL;
35597 +       if (!err)
35598 +               err = au_wh_init(a->br, a->sb);
35599 +       wbr_wh_write_unlock(wbr);
35600 +       au_hn_inode_unlock(hdir);
35601 +       di_read_unlock(a->sb->s_root, AuLock_IR);
35602 +       if (!err)
35603 +               au_fhsm_wrote(a->sb, bindex, /*force*/0);
35604 +
35605 +out:
35606 +       if (wbr)
35607 +               atomic_dec(&wbr->wbr_wh_running);
35608 +       au_lcnt_dec(&a->br->br_count);
35609 +       si_write_unlock(a->sb);
35610 +       au_nwt_done(&au_sbi(a->sb)->si_nowait);
35611 +       au_kfree_rcu(a);
35612 +       if (unlikely(err))
35613 +               AuIOErr("err %d\n", err);
35614 +}
35615 +
35616 +static void kick_reinit_br_wh(struct super_block *sb, struct au_branch *br)
35617 +{
35618 +       int do_dec, wkq_err;
35619 +       struct reinit_br_wh *arg;
35620 +
35621 +       do_dec = 1;
35622 +       if (atomic_inc_return(&br->br_wbr->wbr_wh_running) != 1)
35623 +               goto out;
35624 +
35625 +       /* ignore ENOMEM */
35626 +       arg = kmalloc(sizeof(*arg), GFP_NOFS);
35627 +       if (arg) {
35628 +               /*
35629 +                * dec(wh_running), kfree(arg) and dec(br_count)
35630 +                * in reinit function
35631 +                */
35632 +               arg->sb = sb;
35633 +               arg->br = br;
35634 +               au_lcnt_inc(&br->br_count);
35635 +               wkq_err = au_wkq_nowait(reinit_br_wh, arg, sb, /*flags*/0);
35636 +               if (unlikely(wkq_err)) {
35637 +                       atomic_dec(&br->br_wbr->wbr_wh_running);
35638 +                       au_lcnt_dec(&br->br_count);
35639 +                       au_kfree_rcu(arg);
35640 +               }
35641 +               do_dec = 0;
35642 +       }
35643 +
35644 +out:
35645 +       if (do_dec)
35646 +               atomic_dec(&br->br_wbr->wbr_wh_running);
35647 +}
35648 +
35649 +/* ---------------------------------------------------------------------- */
35650 +
35651 +/*
35652 + * create the whiteout @wh.
35653 + */
35654 +static int link_or_create_wh(struct super_block *sb, aufs_bindex_t bindex,
35655 +                            struct dentry *wh)
35656 +{
35657 +       int err;
35658 +       struct path h_path = {
35659 +               .dentry = wh
35660 +       };
35661 +       struct au_branch *br;
35662 +       struct au_wbr *wbr;
35663 +       struct dentry *h_parent;
35664 +       struct inode *h_dir, *delegated;
35665 +
35666 +       h_parent = wh->d_parent; /* dir inode is locked */
35667 +       h_dir = d_inode(h_parent);
35668 +       IMustLock(h_dir);
35669 +
35670 +       br = au_sbr(sb, bindex);
35671 +       h_path.mnt = au_br_mnt(br);
35672 +       wbr = br->br_wbr;
35673 +       wbr_wh_read_lock(wbr);
35674 +       if (wbr->wbr_whbase) {
35675 +               delegated = NULL;
35676 +               err = vfsub_link(wbr->wbr_whbase, h_dir, &h_path, &delegated);
35677 +               if (unlikely(err == -EWOULDBLOCK)) {
35678 +                       pr_warn("cannot retry for NFSv4 delegation"
35679 +                               " for an internal link\n");
35680 +                       iput(delegated);
35681 +               }
35682 +               if (!err || err != -EMLINK)
35683 +                       goto out;
35684 +
35685 +               /* link count full. re-initialize br_whbase. */
35686 +               kick_reinit_br_wh(sb, br);
35687 +       }
35688 +
35689 +       /* return this error in this context */
35690 +       err = vfsub_create(h_dir, &h_path, WH_MASK, /*want_excl*/true);
35691 +       if (!err)
35692 +               au_fhsm_wrote(sb, bindex, /*force*/0);
35693 +
35694 +out:
35695 +       wbr_wh_read_unlock(wbr);
35696 +       return err;
35697 +}
35698 +
35699 +/* ---------------------------------------------------------------------- */
35700 +
35701 +/*
35702 + * create or remove the diropq.
35703 + */
35704 +static struct dentry *do_diropq(struct dentry *dentry, aufs_bindex_t bindex,
35705 +                               unsigned int flags)
35706 +{
35707 +       struct dentry *opq_dentry;
35708 +       struct super_block *sb;
35709 +       struct au_branch *br;
35710 +       struct path h_path;
35711 +       int err;
35712 +
35713 +       sb = dentry->d_sb;
35714 +       br = au_sbr(sb, bindex);
35715 +       h_path.dentry = au_h_dptr(dentry, bindex);
35716 +       h_path.mnt = au_br_mnt(br);
35717 +       opq_dentry = vfsub_lkup_one(&diropq_name, &h_path);
35718 +       if (IS_ERR(opq_dentry))
35719 +               goto out;
35720 +
35721 +       if (au_ftest_diropq(flags, CREATE)) {
35722 +               err = link_or_create_wh(sb, bindex, opq_dentry);
35723 +               if (!err) {
35724 +                       au_set_dbdiropq(dentry, bindex);
35725 +                       goto out; /* success */
35726 +               }
35727 +       } else {
35728 +               h_path.dentry = opq_dentry;
35729 +               err = do_unlink_wh(au_h_iptr(d_inode(dentry), bindex), &h_path);
35730 +               if (!err)
35731 +                       au_set_dbdiropq(dentry, -1);
35732 +       }
35733 +       dput(opq_dentry);
35734 +       opq_dentry = ERR_PTR(err);
35735 +
35736 +out:
35737 +       return opq_dentry;
35738 +}
35739 +
35740 +struct do_diropq_args {
35741 +       struct dentry **errp;
35742 +       struct dentry *dentry;
35743 +       aufs_bindex_t bindex;
35744 +       unsigned int flags;
35745 +};
35746 +
35747 +static void call_do_diropq(void *args)
35748 +{
35749 +       struct do_diropq_args *a = args;
35750 +       *a->errp = do_diropq(a->dentry, a->bindex, a->flags);
35751 +}
35752 +
35753 +struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
35754 +                            unsigned int flags)
35755 +{
35756 +       struct dentry *diropq, *h_dentry;
35757 +       struct user_namespace *h_userns;
35758 +
35759 +       h_userns = au_sbr_userns(dentry->d_sb, bindex);
35760 +       h_dentry = au_h_dptr(dentry, bindex);
35761 +       if (!au_test_h_perm_sio(h_userns, d_inode(h_dentry),
35762 +                               MAY_EXEC | MAY_WRITE))
35763 +               diropq = do_diropq(dentry, bindex, flags);
35764 +       else {
35765 +               int wkq_err;
35766 +               struct do_diropq_args args = {
35767 +                       .errp           = &diropq,
35768 +                       .dentry         = dentry,
35769 +                       .bindex         = bindex,
35770 +                       .flags          = flags
35771 +               };
35772 +
35773 +               wkq_err = au_wkq_wait(call_do_diropq, &args);
35774 +               if (unlikely(wkq_err))
35775 +                       diropq = ERR_PTR(wkq_err);
35776 +       }
35777 +
35778 +       return diropq;
35779 +}
35780 +
35781 +/* ---------------------------------------------------------------------- */
35782 +
35783 +/*
35784 + * lookup whiteout dentry.
35785 + * @h_parent: lower parent dentry which must exist and be locked
35786 + * @base_name: name of dentry which will be whiteouted
35787 + * returns dentry for whiteout.
35788 + */
35789 +struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
35790 +                         struct au_branch *br)
35791 +{
35792 +       int err;
35793 +       struct qstr wh_name;
35794 +       struct dentry *wh_dentry;
35795 +       struct path h_path;
35796 +
35797 +       err = au_wh_name_alloc(&wh_name, base_name);
35798 +       wh_dentry = ERR_PTR(err);
35799 +       if (!err) {
35800 +               h_path.dentry = h_parent;
35801 +               h_path.mnt = au_br_mnt(br);
35802 +               wh_dentry = vfsub_lkup_one(&wh_name, &h_path);
35803 +               au_kfree_try_rcu(wh_name.name);
35804 +       }
35805 +       return wh_dentry;
35806 +}
35807 +
35808 +/*
35809 + * link/create a whiteout for @dentry on @bindex.
35810 + */
35811 +struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
35812 +                           struct dentry *h_parent)
35813 +{
35814 +       struct dentry *wh_dentry;
35815 +       struct super_block *sb;
35816 +       int err;
35817 +
35818 +       sb = dentry->d_sb;
35819 +       wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, au_sbr(sb, bindex));
35820 +       if (!IS_ERR(wh_dentry) && d_is_negative(wh_dentry)) {
35821 +               err = link_or_create_wh(sb, bindex, wh_dentry);
35822 +               if (!err) {
35823 +                       au_set_dbwh(dentry, bindex);
35824 +                       au_fhsm_wrote(sb, bindex, /*force*/0);
35825 +               } else {
35826 +                       dput(wh_dentry);
35827 +                       wh_dentry = ERR_PTR(err);
35828 +               }
35829 +       }
35830 +
35831 +       return wh_dentry;
35832 +}
35833 +
35834 +/* ---------------------------------------------------------------------- */
35835 +
35836 +/* Delete all whiteouts in this directory on branch bindex. */
35837 +static int del_wh_children(struct path *h_path, struct au_nhash *whlist,
35838 +                          aufs_bindex_t bindex)
35839 +{
35840 +       int err;
35841 +       unsigned long ul, n;
35842 +       struct qstr wh_name;
35843 +       char *p;
35844 +       struct hlist_head *head;
35845 +       struct au_vdir_wh *pos;
35846 +       struct au_vdir_destr *str;
35847 +
35848 +       err = -ENOMEM;
35849 +       p = (void *)__get_free_page(GFP_NOFS);
35850 +       wh_name.name = p;
35851 +       if (unlikely(!wh_name.name))
35852 +               goto out;
35853 +
35854 +       err = 0;
35855 +       memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
35856 +       p += AUFS_WH_PFX_LEN;
35857 +       n = whlist->nh_num;
35858 +       head = whlist->nh_head;
35859 +       for (ul = 0; !err && ul < n; ul++, head++) {
35860 +               hlist_for_each_entry(pos, head, wh_hash) {
35861 +                       if (pos->wh_bindex != bindex)
35862 +                               continue;
35863 +
35864 +                       str = &pos->wh_str;
35865 +                       if (str->len + AUFS_WH_PFX_LEN <= PATH_MAX) {
35866 +                               memcpy(p, str->name, str->len);
35867 +                               wh_name.len = AUFS_WH_PFX_LEN + str->len;
35868 +                               err = unlink_wh_name(h_path, &wh_name);
35869 +                               if (!err)
35870 +                                       continue;
35871 +                               break;
35872 +                       }
35873 +                       AuIOErr("whiteout name too long %.*s\n",
35874 +                               str->len, str->name);
35875 +                       err = -EIO;
35876 +                       break;
35877 +               }
35878 +       }
35879 +       free_page((unsigned long)wh_name.name);
35880 +
35881 +out:
35882 +       return err;
35883 +}
35884 +
35885 +struct del_wh_children_args {
35886 +       int *errp;
35887 +       struct path *h_path;
35888 +       struct au_nhash *whlist;
35889 +       aufs_bindex_t bindex;
35890 +};
35891 +
35892 +static void call_del_wh_children(void *args)
35893 +{
35894 +       struct del_wh_children_args *a = args;
35895 +       *a->errp = del_wh_children(a->h_path, a->whlist, a->bindex);
35896 +}
35897 +
35898 +/* ---------------------------------------------------------------------- */
35899 +
35900 +struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp)
35901 +{
35902 +       struct au_whtmp_rmdir *whtmp;
35903 +       int err;
35904 +       unsigned int rdhash;
35905 +
35906 +       SiMustAnyLock(sb);
35907 +
35908 +       whtmp = kzalloc(sizeof(*whtmp), gfp);
35909 +       if (unlikely(!whtmp)) {
35910 +               whtmp = ERR_PTR(-ENOMEM);
35911 +               goto out;
35912 +       }
35913 +
35914 +       /* no estimation for dir size */
35915 +       rdhash = au_sbi(sb)->si_rdhash;
35916 +       if (!rdhash)
35917 +               rdhash = AUFS_RDHASH_DEF;
35918 +       err = au_nhash_alloc(&whtmp->whlist, rdhash, gfp);
35919 +       if (unlikely(err)) {
35920 +               au_kfree_rcu(whtmp);
35921 +               whtmp = ERR_PTR(err);
35922 +       }
35923 +
35924 +out:
35925 +       return whtmp;
35926 +}
35927 +
35928 +void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp)
35929 +{
35930 +       if (whtmp->br)
35931 +               au_lcnt_dec(&whtmp->br->br_count);
35932 +       dput(whtmp->wh_dentry);
35933 +       iput(whtmp->dir);
35934 +       au_nhash_wh_free(&whtmp->whlist);
35935 +       au_kfree_rcu(whtmp);
35936 +}
35937 +
35938 +/*
35939 + * rmdir the whiteouted temporary named dir @h_dentry.
35940 + * @whlist: whiteouted children.
35941 + */
35942 +int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
35943 +                  struct dentry *wh_dentry, struct au_nhash *whlist)
35944 +{
35945 +       int err;
35946 +       unsigned int h_nlink;
35947 +       struct path wh_path;
35948 +       struct inode *wh_inode, *h_dir;
35949 +       struct au_branch *br;
35950 +       struct user_namespace *h_userns;
35951 +
35952 +       h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */
35953 +       IMustLock(h_dir);
35954 +
35955 +       br = au_sbr(dir->i_sb, bindex);
35956 +       wh_path.dentry = wh_dentry;
35957 +       wh_path.mnt = au_br_mnt(br);
35958 +       h_userns = au_br_userns(br);
35959 +       wh_inode = d_inode(wh_dentry);
35960 +       inode_lock_nested(wh_inode, AuLsc_I_CHILD);
35961 +
35962 +       /*
35963 +        * someone else might change some whiteouts while we were sleeping.
35964 +        * it means this whlist may have an obsoleted entry.
35965 +        */
35966 +       if (!au_test_h_perm_sio(h_userns, wh_inode, MAY_EXEC | MAY_WRITE))
35967 +               err = del_wh_children(&wh_path, whlist, bindex);
35968 +       else {
35969 +               int wkq_err;
35970 +               struct del_wh_children_args args = {
35971 +                       .errp           = &err,
35972 +                       .h_path         = &wh_path,
35973 +                       .whlist         = whlist,
35974 +                       .bindex         = bindex
35975 +               };
35976 +
35977 +               wkq_err = au_wkq_wait(call_del_wh_children, &args);
35978 +               if (unlikely(wkq_err))
35979 +                       err = wkq_err;
35980 +       }
35981 +       inode_unlock(wh_inode);
35982 +
35983 +       if (!err) {
35984 +               h_nlink = h_dir->i_nlink;
35985 +               err = vfsub_rmdir(h_dir, &wh_path);
35986 +               /* some fs doesn't change the parent nlink in some cases */
35987 +               h_nlink -= h_dir->i_nlink;
35988 +       }
35989 +
35990 +       if (!err) {
35991 +               if (au_ibtop(dir) == bindex) {
35992 +                       /* todo: dir->i_mutex is necessary */
35993 +                       au_cpup_attr_timesizes(dir);
35994 +                       if (h_nlink)
35995 +                               vfsub_drop_nlink(dir);
35996 +               }
35997 +               return 0; /* success */
35998 +       }
35999 +
36000 +       pr_warn("failed removing %pd(%d), ignored\n", wh_dentry, err);
36001 +       return err;
36002 +}
36003 +
36004 +static void call_rmdir_whtmp(void *args)
36005 +{
36006 +       int err;
36007 +       aufs_bindex_t bindex;
36008 +       struct au_whtmp_rmdir *a = args;
36009 +       struct super_block *sb;
36010 +       struct dentry *h_parent;
36011 +       struct inode *h_dir;
36012 +       struct au_hinode *hdir;
36013 +
36014 +       /* rmdir by nfsd may cause deadlock with this i_mutex */
36015 +       /* inode_lock(a->dir); */
36016 +       err = -EROFS;
36017 +       sb = a->dir->i_sb;
36018 +       si_read_lock(sb, !AuLock_FLUSH);
36019 +       if (!au_br_writable(a->br->br_perm))
36020 +               goto out;
36021 +       bindex = au_br_index(sb, a->br->br_id);
36022 +       if (unlikely(bindex < 0))
36023 +               goto out;
36024 +
36025 +       err = -EIO;
36026 +       ii_write_lock_parent(a->dir);
36027 +       h_parent = dget_parent(a->wh_dentry);
36028 +       h_dir = d_inode(h_parent);
36029 +       hdir = au_hi(a->dir, bindex);
36030 +       err = vfsub_mnt_want_write(au_br_mnt(a->br));
36031 +       if (unlikely(err))
36032 +               goto out_mnt;
36033 +       au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
36034 +       err = au_h_verify(a->wh_dentry, au_opt_udba(sb), h_dir, h_parent,
36035 +                         a->br);
36036 +       if (!err)
36037 +               err = au_whtmp_rmdir(a->dir, bindex, a->wh_dentry, &a->whlist);
36038 +       au_hn_inode_unlock(hdir);
36039 +       vfsub_mnt_drop_write(au_br_mnt(a->br));
36040 +
36041 +out_mnt:
36042 +       dput(h_parent);
36043 +       ii_write_unlock(a->dir);
36044 +out:
36045 +       /* inode_unlock(a->dir); */
36046 +       au_whtmp_rmdir_free(a);
36047 +       si_read_unlock(sb);
36048 +       au_nwt_done(&au_sbi(sb)->si_nowait);
36049 +       if (unlikely(err))
36050 +               AuIOErr("err %d\n", err);
36051 +}
36052 +
36053 +void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
36054 +                        struct dentry *wh_dentry, struct au_whtmp_rmdir *args)
36055 +{
36056 +       int wkq_err;
36057 +       struct super_block *sb;
36058 +
36059 +       IMustLock(dir);
36060 +
36061 +       /* all post-process will be done in do_rmdir_whtmp(). */
36062 +       sb = dir->i_sb;
36063 +       args->dir = au_igrab(dir);
36064 +       args->br = au_sbr(sb, bindex);
36065 +       au_lcnt_inc(&args->br->br_count);
36066 +       args->wh_dentry = dget(wh_dentry);
36067 +       wkq_err = au_wkq_nowait(call_rmdir_whtmp, args, sb, /*flags*/0);
36068 +       if (unlikely(wkq_err)) {
36069 +               pr_warn("rmdir error %pd (%d), ignored\n", wh_dentry, wkq_err);
36070 +               au_whtmp_rmdir_free(args);
36071 +       }
36072 +}
36073 diff -urN /usr/share/empty/fs/aufs/whout.h linux/fs/aufs/whout.h
36074 --- /usr/share/empty/fs/aufs/whout.h    1970-01-01 01:00:00.000000000 +0100
36075 +++ linux/fs/aufs/whout.h       2022-03-21 14:49:05.733299677 +0100
36076 @@ -0,0 +1,87 @@
36077 +/* SPDX-License-Identifier: GPL-2.0 */
36078 +/*
36079 + * Copyright (C) 2005-2021 Junjiro R. Okajima
36080 + *
36081 + * This program, aufs is free software; you can redistribute it and/or modify
36082 + * it under the terms of the GNU General Public License as published by
36083 + * the Free Software Foundation; either version 2 of the License, or
36084 + * (at your option) any later version.
36085 + *
36086 + * This program is distributed in the hope that it will be useful,
36087 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
36088 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36089 + * GNU General Public License for more details.
36090 + *
36091 + * You should have received a copy of the GNU General Public License
36092 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
36093 + */
36094 +
36095 +/*
36096 + * whiteout for logical deletion and opaque directory
36097 + */
36098 +
36099 +#ifndef __AUFS_WHOUT_H__
36100 +#define __AUFS_WHOUT_H__
36101 +
36102 +#ifdef __KERNEL__
36103 +
36104 +#include "dir.h"
36105 +
36106 +/* whout.c */
36107 +int au_wh_name_alloc(struct qstr *wh, const struct qstr *name);
36108 +int au_wh_test(struct user_namespace *h_userns, struct path *h_ppath,
36109 +              struct qstr *wh_name, int try_sio);
36110 +int au_diropq_test(struct user_namespace *h_userns, struct path *h_path);
36111 +struct au_branch;
36112 +struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
36113 +                            struct qstr *prefix);
36114 +int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br);
36115 +int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
36116 +                       struct dentry *dentry);
36117 +int au_wh_init(struct au_branch *br, struct super_block *sb);
36118 +
36119 +/* diropq flags */
36120 +#define AuDiropq_CREATE        1
36121 +#define au_ftest_diropq(flags, name)   ((flags) & AuDiropq_##name)
36122 +#define au_fset_diropq(flags, name) \
36123 +       do { (flags) |= AuDiropq_##name; } while (0)
36124 +#define au_fclr_diropq(flags, name) \
36125 +       do { (flags) &= ~AuDiropq_##name; } while (0)
36126 +
36127 +struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
36128 +                            unsigned int flags);
36129 +struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
36130 +                         struct au_branch *br);
36131 +struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
36132 +                           struct dentry *h_parent);
36133 +
36134 +/* real rmdir for the whiteout-ed dir */
36135 +struct au_whtmp_rmdir {
36136 +       struct inode *dir;
36137 +       struct au_branch *br;
36138 +       struct dentry *wh_dentry;
36139 +       struct au_nhash whlist;
36140 +};
36141 +
36142 +struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp);
36143 +void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp);
36144 +int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
36145 +                  struct dentry *wh_dentry, struct au_nhash *whlist);
36146 +void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
36147 +                        struct dentry *wh_dentry, struct au_whtmp_rmdir *args);
36148 +
36149 +/* ---------------------------------------------------------------------- */
36150 +
36151 +static inline struct dentry *au_diropq_create(struct dentry *dentry,
36152 +                                             aufs_bindex_t bindex)
36153 +{
36154 +       return au_diropq_sio(dentry, bindex, AuDiropq_CREATE);
36155 +}
36156 +
36157 +static inline int au_diropq_remove(struct dentry *dentry, aufs_bindex_t bindex)
36158 +{
36159 +       return PTR_ERR(au_diropq_sio(dentry, bindex, !AuDiropq_CREATE));
36160 +}
36161 +
36162 +#endif /* __KERNEL__ */
36163 +#endif /* __AUFS_WHOUT_H__ */
36164 diff -urN /usr/share/empty/fs/aufs/wkq.c linux/fs/aufs/wkq.c
36165 --- /usr/share/empty/fs/aufs/wkq.c      1970-01-01 01:00:00.000000000 +0100
36166 +++ linux/fs/aufs/wkq.c 2022-03-21 14:49:05.733299677 +0100
36167 @@ -0,0 +1,372 @@
36168 +// SPDX-License-Identifier: GPL-2.0
36169 +/*
36170 + * Copyright (C) 2005-2021 Junjiro R. Okajima
36171 + *
36172 + * This program, aufs is free software; you can redistribute it and/or modify
36173 + * it under the terms of the GNU General Public License as published by
36174 + * the Free Software Foundation; either version 2 of the License, or
36175 + * (at your option) any later version.
36176 + *
36177 + * This program is distributed in the hope that it will be useful,
36178 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
36179 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36180 + * GNU General Public License for more details.
36181 + *
36182 + * You should have received a copy of the GNU General Public License
36183 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
36184 + */
36185 +
36186 +/*
36187 + * workqueue for asynchronous/super-io operations
36188 + * todo: try new credential scheme
36189 + */
36190 +
36191 +#include <linux/module.h>
36192 +#include "aufs.h"
36193 +
36194 +/* internal workqueue named AUFS_WKQ_NAME */
36195 +
36196 +static struct workqueue_struct *au_wkq;
36197 +
36198 +struct au_wkinfo {
36199 +       struct work_struct wk;
36200 +       struct kobject *kobj;
36201 +
36202 +       unsigned int flags; /* see wkq.h */
36203 +
36204 +       au_wkq_func_t func;
36205 +       void *args;
36206 +
36207 +#ifdef CONFIG_LOCKDEP
36208 +       int dont_check;
36209 +       struct held_lock **hlock;
36210 +#endif
36211 +
36212 +       struct completion *comp;
36213 +};
36214 +
36215 +/* ---------------------------------------------------------------------- */
36216 +/*
36217 + * Aufs passes some operations to the workqueue such as the internal copyup.
36218 + * This scheme looks rather unnatural for LOCKDEP debugging feature, since the
36219 + * job run by workqueue depends upon the locks acquired in the other task.
36220 + * Delegating a small operation to the workqueue, aufs passes its lockdep
36221 + * information too. And the job in the workqueue restores the info in order to
36222 + * pretend as if it acquired those locks. This is just to make LOCKDEP work
36223 + * correctly and expectedly.
36224 + */
36225 +
36226 +#ifndef CONFIG_LOCKDEP
36227 +AuStubInt0(au_wkq_lockdep_alloc, struct au_wkinfo *wkinfo);
36228 +AuStubVoid(au_wkq_lockdep_free, struct au_wkinfo *wkinfo);
36229 +AuStubVoid(au_wkq_lockdep_pre, struct au_wkinfo *wkinfo);
36230 +AuStubVoid(au_wkq_lockdep_post, struct au_wkinfo *wkinfo);
36231 +AuStubVoid(au_wkq_lockdep_init, struct au_wkinfo *wkinfo);
36232 +#else
36233 +static void au_wkq_lockdep_init(struct au_wkinfo *wkinfo)
36234 +{
36235 +       wkinfo->hlock = NULL;
36236 +       wkinfo->dont_check = 0;
36237 +}
36238 +
36239 +/*
36240 + * 1: matched
36241 + * 0: unmatched
36242 + */
36243 +static int au_wkq_lockdep_test(struct lock_class_key *key, const char *name)
36244 +{
36245 +       static DEFINE_SPINLOCK(spin);
36246 +       static struct {
36247 +               char *name;
36248 +               struct lock_class_key *key;
36249 +       } a[] = {
36250 +               { .name = "&sbinfo->si_rwsem" },
36251 +               { .name = "&finfo->fi_rwsem" },
36252 +               { .name = "&dinfo->di_rwsem" },
36253 +               { .name = "&iinfo->ii_rwsem" }
36254 +       };
36255 +       static int set;
36256 +       int i;
36257 +
36258 +       /* lockless read from 'set.' see below */
36259 +       if (set == ARRAY_SIZE(a)) {
36260 +               for (i = 0; i < ARRAY_SIZE(a); i++)
36261 +                       if (a[i].key == key)
36262 +                               goto match;
36263 +               goto unmatch;
36264 +       }
36265 +
36266 +       spin_lock(&spin);
36267 +       if (set)
36268 +               for (i = 0; i < ARRAY_SIZE(a); i++)
36269 +                       if (a[i].key == key) {
36270 +                               spin_unlock(&spin);
36271 +                               goto match;
36272 +                       }
36273 +       for (i = 0; i < ARRAY_SIZE(a); i++) {
36274 +               if (a[i].key) {
36275 +                       if (unlikely(a[i].key == key)) { /* rare but possible */
36276 +                               spin_unlock(&spin);
36277 +                               goto match;
36278 +                       } else
36279 +                               continue;
36280 +               }
36281 +               if (strstr(a[i].name, name)) {
36282 +                       /*
36283 +                        * the order of these three lines is important for the
36284 +                        * lockless read above.
36285 +                        */
36286 +                       a[i].key = key;
36287 +                       spin_unlock(&spin);
36288 +                       set++;
36289 +                       /* AuDbg("%d, %s\n", set, name); */
36290 +                       goto match;
36291 +               }
36292 +       }
36293 +       spin_unlock(&spin);
36294 +       goto unmatch;
36295 +
36296 +match:
36297 +       return 1;
36298 +unmatch:
36299 +       return 0;
36300 +}
36301 +
36302 +static int au_wkq_lockdep_alloc(struct au_wkinfo *wkinfo)
36303 +{
36304 +       int err, n;
36305 +       struct task_struct *curr;
36306 +       struct held_lock **hl, *held_locks, *p;
36307 +
36308 +       err = 0;
36309 +       curr = current;
36310 +       wkinfo->dont_check = lockdep_recursing(curr);
36311 +       if (wkinfo->dont_check)
36312 +               goto out;
36313 +       n = curr->lockdep_depth;
36314 +       if (!n)
36315 +               goto out;
36316 +
36317 +       err = -ENOMEM;
36318 +       wkinfo->hlock = kmalloc_array(n + 1, sizeof(*wkinfo->hlock), GFP_NOFS);
36319 +       if (unlikely(!wkinfo->hlock))
36320 +               goto out;
36321 +
36322 +       err = 0;
36323 +#if 0 /* left for debugging */
36324 +       if (0 && au_debug_test())
36325 +               lockdep_print_held_locks(curr);
36326 +#endif
36327 +       held_locks = curr->held_locks;
36328 +       hl = wkinfo->hlock;
36329 +       while (n--) {
36330 +               p = held_locks++;
36331 +               if (au_wkq_lockdep_test(p->instance->key, p->instance->name))
36332 +                       *hl++ = p;
36333 +       }
36334 +       *hl = NULL;
36335 +
36336 +out:
36337 +       return err;
36338 +}
36339 +
36340 +static void au_wkq_lockdep_free(struct au_wkinfo *wkinfo)
36341 +{
36342 +       au_kfree_try_rcu(wkinfo->hlock);
36343 +}
36344 +
36345 +static void au_wkq_lockdep_pre(struct au_wkinfo *wkinfo)
36346 +{
36347 +       struct held_lock *p, **hl = wkinfo->hlock;
36348 +       int subclass;
36349 +
36350 +       if (wkinfo->dont_check)
36351 +               lockdep_off();
36352 +       if (!hl)
36353 +               return;
36354 +       while ((p = *hl++)) { /* assignment */
36355 +               subclass = lockdep_hlock_class(p)->subclass;
36356 +               /* AuDbg("%s, %d\n", p->instance->name, subclass); */
36357 +               if (p->read)
36358 +                       rwsem_acquire_read(p->instance, subclass, 0,
36359 +                                          /*p->acquire_ip*/_RET_IP_);
36360 +               else
36361 +                       rwsem_acquire(p->instance, subclass, 0,
36362 +                                     /*p->acquire_ip*/_RET_IP_);
36363 +       }
36364 +}
36365 +
36366 +static void au_wkq_lockdep_post(struct au_wkinfo *wkinfo)
36367 +{
36368 +       struct held_lock *p, **hl = wkinfo->hlock;
36369 +
36370 +       if (wkinfo->dont_check)
36371 +               lockdep_on();
36372 +       if (!hl)
36373 +               return;
36374 +       while ((p = *hl++)) /* assignment */
36375 +               rwsem_release(p->instance, /*p->acquire_ip*/_RET_IP_);
36376 +}
36377 +#endif
36378 +
36379 +static void wkq_func(struct work_struct *wk)
36380 +{
36381 +       struct au_wkinfo *wkinfo = container_of(wk, struct au_wkinfo, wk);
36382 +
36383 +       AuDebugOn(!uid_eq(current_fsuid(), GLOBAL_ROOT_UID));
36384 +       AuDebugOn(rlimit(RLIMIT_FSIZE) != RLIM_INFINITY);
36385 +
36386 +       au_wkq_lockdep_pre(wkinfo);
36387 +       wkinfo->func(wkinfo->args);
36388 +       au_wkq_lockdep_post(wkinfo);
36389 +       if (au_ftest_wkq(wkinfo->flags, WAIT))
36390 +               complete(wkinfo->comp);
36391 +       else {
36392 +               kobject_put(wkinfo->kobj);
36393 +               module_put(THIS_MODULE); /* todo: ?? */
36394 +               au_kfree_rcu(wkinfo);
36395 +       }
36396 +}
36397 +
36398 +/*
36399 + * Since struct completion is large, try allocating it dynamically.
36400 + */
36401 +#define AuWkqCompDeclare(name) struct completion *comp = NULL
36402 +
36403 +static int au_wkq_comp_alloc(struct au_wkinfo *wkinfo, struct completion **comp)
36404 +{
36405 +       *comp = kmalloc(sizeof(**comp), GFP_NOFS);
36406 +       if (*comp) {
36407 +               init_completion(*comp);
36408 +               wkinfo->comp = *comp;
36409 +               return 0;
36410 +       }
36411 +       return -ENOMEM;
36412 +}
36413 +
36414 +static void au_wkq_comp_free(struct completion *comp)
36415 +{
36416 +       au_kfree_rcu(comp);
36417 +}
36418 +
36419 +static void au_wkq_run(struct au_wkinfo *wkinfo)
36420 +{
36421 +       if (au_ftest_wkq(wkinfo->flags, NEST)) {
36422 +               if (au_wkq_test()) {
36423 +                       AuWarn1("wkq from wkq, unless silly-rename on NFS,"
36424 +                               " due to a dead dir by UDBA,"
36425 +                               " or async xino write?\n");
36426 +                       AuDebugOn(au_ftest_wkq(wkinfo->flags, WAIT));
36427 +               }
36428 +       } else
36429 +               au_dbg_verify_kthread();
36430 +
36431 +       if (au_ftest_wkq(wkinfo->flags, WAIT)) {
36432 +               INIT_WORK_ONSTACK(&wkinfo->wk, wkq_func);
36433 +               queue_work(au_wkq, &wkinfo->wk);
36434 +       } else {
36435 +               INIT_WORK(&wkinfo->wk, wkq_func);
36436 +               schedule_work(&wkinfo->wk);
36437 +       }
36438 +}
36439 +
36440 +/*
36441 + * Be careful. It is easy to make deadlock happen.
36442 + * processA: lock, wkq and wait
36443 + * processB: wkq and wait, lock in wkq
36444 + * --> deadlock
36445 + */
36446 +int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args)
36447 +{
36448 +       int err;
36449 +       AuWkqCompDeclare(comp);
36450 +       struct au_wkinfo wkinfo = {
36451 +               .flags  = flags,
36452 +               .func   = func,
36453 +               .args   = args
36454 +       };
36455 +
36456 +       err = au_wkq_comp_alloc(&wkinfo, &comp);
36457 +       if (unlikely(err))
36458 +               goto out;
36459 +       err = au_wkq_lockdep_alloc(&wkinfo);
36460 +       if (unlikely(err))
36461 +               goto out_comp;
36462 +       if (!err) {
36463 +               au_wkq_run(&wkinfo);
36464 +               /* no timeout, no interrupt */
36465 +               wait_for_completion(wkinfo.comp);
36466 +       }
36467 +       au_wkq_lockdep_free(&wkinfo);
36468 +
36469 +out_comp:
36470 +       au_wkq_comp_free(comp);
36471 +out:
36472 +       destroy_work_on_stack(&wkinfo.wk);
36473 +       return err;
36474 +}
36475 +
36476 +/*
36477 + * Note: dget/dput() in func for aufs dentries are not supported. It will be a
36478 + * problem in a concurrent umounting.
36479 + */
36480 +int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
36481 +                 unsigned int flags)
36482 +{
36483 +       int err;
36484 +       struct au_wkinfo *wkinfo;
36485 +
36486 +       atomic_inc(&au_sbi(sb)->si_nowait.nw_len);
36487 +
36488 +       /*
36489 +        * wkq_func() must free this wkinfo.
36490 +        * it highly depends upon the implementation of workqueue.
36491 +        */
36492 +       err = 0;
36493 +       wkinfo = kmalloc(sizeof(*wkinfo), GFP_NOFS);
36494 +       if (wkinfo) {
36495 +               wkinfo->kobj = &au_sbi(sb)->si_kobj;
36496 +               wkinfo->flags = flags & ~AuWkq_WAIT;
36497 +               wkinfo->func = func;
36498 +               wkinfo->args = args;
36499 +               wkinfo->comp = NULL;
36500 +               au_wkq_lockdep_init(wkinfo);
36501 +               kobject_get(wkinfo->kobj);
36502 +               __module_get(THIS_MODULE); /* todo: ?? */
36503 +
36504 +               au_wkq_run(wkinfo);
36505 +       } else {
36506 +               err = -ENOMEM;
36507 +               au_nwt_done(&au_sbi(sb)->si_nowait);
36508 +       }
36509 +
36510 +       return err;
36511 +}
36512 +
36513 +/* ---------------------------------------------------------------------- */
36514 +
36515 +void au_nwt_init(struct au_nowait_tasks *nwt)
36516 +{
36517 +       atomic_set(&nwt->nw_len, 0);
36518 +       /* smp_mb(); */ /* atomic_set */
36519 +       init_waitqueue_head(&nwt->nw_wq);
36520 +}
36521 +
36522 +void au_wkq_fin(void)
36523 +{
36524 +       destroy_workqueue(au_wkq);
36525 +}
36526 +
36527 +int __init au_wkq_init(void)
36528 +{
36529 +       int err;
36530 +
36531 +       err = 0;
36532 +       au_wkq = alloc_workqueue(AUFS_WKQ_NAME, 0, WQ_DFL_ACTIVE);
36533 +       if (IS_ERR(au_wkq))
36534 +               err = PTR_ERR(au_wkq);
36535 +       else if (!au_wkq)
36536 +               err = -ENOMEM;
36537 +
36538 +       return err;
36539 +}
36540 diff -urN /usr/share/empty/fs/aufs/wkq.h linux/fs/aufs/wkq.h
36541 --- /usr/share/empty/fs/aufs/wkq.h      1970-01-01 01:00:00.000000000 +0100
36542 +++ linux/fs/aufs/wkq.h 2022-03-21 14:49:05.733299677 +0100
36543 @@ -0,0 +1,89 @@
36544 +/* SPDX-License-Identifier: GPL-2.0 */
36545 +/*
36546 + * Copyright (C) 2005-2021 Junjiro R. Okajima
36547 + *
36548 + * This program, aufs is free software; you can redistribute it and/or modify
36549 + * it under the terms of the GNU General Public License as published by
36550 + * the Free Software Foundation; either version 2 of the License, or
36551 + * (at your option) any later version.
36552 + *
36553 + * This program is distributed in the hope that it will be useful,
36554 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
36555 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36556 + * GNU General Public License for more details.
36557 + *
36558 + * You should have received a copy of the GNU General Public License
36559 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
36560 + */
36561 +
36562 +/*
36563 + * workqueue for asynchronous/super-io operations
36564 + * todo: try new credentials management scheme
36565 + */
36566 +
36567 +#ifndef __AUFS_WKQ_H__
36568 +#define __AUFS_WKQ_H__
36569 +
36570 +#ifdef __KERNEL__
36571 +
36572 +#include <linux/wait.h>
36573 +
36574 +struct super_block;
36575 +
36576 +/* ---------------------------------------------------------------------- */
36577 +
36578 +/*
36579 + * in the next operation, wait for the 'nowait' tasks in system-wide workqueue
36580 + */
36581 +struct au_nowait_tasks {
36582 +       atomic_t                nw_len;
36583 +       wait_queue_head_t       nw_wq;
36584 +};
36585 +
36586 +/* ---------------------------------------------------------------------- */
36587 +
36588 +typedef void (*au_wkq_func_t)(void *args);
36589 +
36590 +/* wkq flags */
36591 +#define AuWkq_WAIT     1
36592 +#define AuWkq_NEST     (1 << 1)
36593 +#define au_ftest_wkq(flags, name)      ((flags) & AuWkq_##name)
36594 +#define au_fset_wkq(flags, name) \
36595 +       do { (flags) |= AuWkq_##name; } while (0)
36596 +#define au_fclr_wkq(flags, name) \
36597 +       do { (flags) &= ~AuWkq_##name; } while (0)
36598 +
36599 +/* wkq.c */
36600 +int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args);
36601 +int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
36602 +                 unsigned int flags);
36603 +void au_nwt_init(struct au_nowait_tasks *nwt);
36604 +int __init au_wkq_init(void);
36605 +void au_wkq_fin(void);
36606 +
36607 +/* ---------------------------------------------------------------------- */
36608 +
36609 +static inline int au_wkq_test(void)
36610 +{
36611 +       return current->flags & PF_WQ_WORKER;
36612 +}
36613 +
36614 +static inline int au_wkq_wait(au_wkq_func_t func, void *args)
36615 +{
36616 +       return au_wkq_do_wait(AuWkq_WAIT, func, args);
36617 +}
36618 +
36619 +static inline void au_nwt_done(struct au_nowait_tasks *nwt)
36620 +{
36621 +       if (atomic_dec_and_test(&nwt->nw_len))
36622 +               wake_up_all(&nwt->nw_wq);
36623 +}
36624 +
36625 +static inline int au_nwt_flush(struct au_nowait_tasks *nwt)
36626 +{
36627 +       wait_event(nwt->nw_wq, !atomic_read(&nwt->nw_len));
36628 +       return 0;
36629 +}
36630 +
36631 +#endif /* __KERNEL__ */
36632 +#endif /* __AUFS_WKQ_H__ */
36633 diff -urN /usr/share/empty/fs/aufs/xattr.c linux/fs/aufs/xattr.c
36634 --- /usr/share/empty/fs/aufs/xattr.c    1970-01-01 01:00:00.000000000 +0100
36635 +++ linux/fs/aufs/xattr.c       2022-03-21 14:49:05.733299677 +0100
36636 @@ -0,0 +1,368 @@
36637 +// SPDX-License-Identifier: GPL-2.0
36638 +/*
36639 + * Copyright (C) 2014-2021 Junjiro R. Okajima
36640 + *
36641 + * This program, aufs is free software; you can redistribute it and/or modify
36642 + * it under the terms of the GNU General Public License as published by
36643 + * the Free Software Foundation; either version 2 of the License, or
36644 + * (at your option) any later version.
36645 + *
36646 + * This program is distributed in the hope that it will be useful,
36647 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
36648 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36649 + * GNU General Public License for more details.
36650 + *
36651 + * You should have received a copy of the GNU General Public License
36652 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
36653 + */
36654 +
36655 +/*
36656 + * handling xattr functions
36657 + */
36658 +
36659 +#include <linux/fs.h>
36660 +#include <linux/posix_acl_xattr.h>
36661 +#include <linux/xattr.h>
36662 +#include "aufs.h"
36663 +
36664 +static int au_xattr_ignore(int err, char *name, unsigned int ignore_flags)
36665 +{
36666 +       if (!ignore_flags)
36667 +               goto out;
36668 +       switch (err) {
36669 +       case -ENOMEM:
36670 +       case -EDQUOT:
36671 +               goto out;
36672 +       }
36673 +
36674 +       if ((ignore_flags & AuBrAttr_ICEX) == AuBrAttr_ICEX) {
36675 +               err = 0;
36676 +               goto out;
36677 +       }
36678 +
36679 +#define cmp(brattr, prefix) do {                                       \
36680 +               if (!strncmp(name, XATTR_##prefix##_PREFIX,             \
36681 +                            XATTR_##prefix##_PREFIX_LEN)) {            \
36682 +                       if (ignore_flags & AuBrAttr_ICEX_##brattr)      \
36683 +                               err = 0;                                \
36684 +                       goto out;                                       \
36685 +               }                                                       \
36686 +       } while (0)
36687 +
36688 +       cmp(SEC, SECURITY);
36689 +       cmp(SYS, SYSTEM);
36690 +       cmp(TR, TRUSTED);
36691 +       cmp(USR, USER);
36692 +#undef cmp
36693 +
36694 +       if (ignore_flags & AuBrAttr_ICEX_OTH)
36695 +               err = 0;
36696 +
36697 +out:
36698 +       return err;
36699 +}
36700 +
36701 +static const int au_xattr_out_of_list = AuBrAttr_ICEX_OTH << 1;
36702 +
36703 +static int au_do_cpup_xattr(struct path *h_dst, struct path *h_src,
36704 +                           char *name, char **buf, unsigned int ignore_flags,
36705 +                           unsigned int verbose)
36706 +{
36707 +       int err;
36708 +       ssize_t ssz;
36709 +       struct inode *h_idst;
36710 +       struct dentry *h_dst_dentry, *h_src_dentry;
36711 +       struct user_namespace *h_dst_userns, *h_src_userns;
36712 +
36713 +       h_src_userns = mnt_user_ns(h_src->mnt);
36714 +       h_src_dentry = h_src->dentry;
36715 +       ssz = vfs_getxattr_alloc(h_src_userns, h_src_dentry, name, buf, 0,
36716 +                                GFP_NOFS);
36717 +       err = ssz;
36718 +       if (unlikely(err <= 0)) {
36719 +               if (err == -ENODATA
36720 +                   || (err == -EOPNOTSUPP
36721 +                       && ((ignore_flags & au_xattr_out_of_list)
36722 +                           || (au_test_nfs_noacl(d_inode(h_src_dentry))
36723 +                               && (!strcmp(name, XATTR_NAME_POSIX_ACL_ACCESS)
36724 +                                   || !strcmp(name,
36725 +                                              XATTR_NAME_POSIX_ACL_DEFAULT))))
36726 +                           ))
36727 +                       err = 0;
36728 +               if (err && (verbose || au_debug_test()))
36729 +                       pr_err("%s, err %d\n", name, err);
36730 +               goto out;
36731 +       }
36732 +
36733 +       /* unlock it temporary */
36734 +       h_dst_userns = mnt_user_ns(h_dst->mnt);
36735 +       h_dst_dentry = h_dst->dentry;
36736 +       h_idst = d_inode(h_dst_dentry);
36737 +       inode_unlock(h_idst);
36738 +       err = vfsub_setxattr(h_dst_userns, h_dst_dentry, name, *buf, ssz,
36739 +                            /*flags*/0);
36740 +       inode_lock_nested(h_idst, AuLsc_I_CHILD2);
36741 +       if (unlikely(err)) {
36742 +               if (verbose || au_debug_test())
36743 +                       pr_err("%s, err %d\n", name, err);
36744 +               err = au_xattr_ignore(err, name, ignore_flags);
36745 +       }
36746 +
36747 +out:
36748 +       return err;
36749 +}
36750 +
36751 +int au_cpup_xattr(struct path *h_dst, struct path *h_src, int ignore_flags,
36752 +                 unsigned int verbose)
36753 +{
36754 +       int err, unlocked, acl_access, acl_default;
36755 +       ssize_t ssz;
36756 +       struct dentry *h_dst_dentry, *h_src_dentry;
36757 +       struct inode *h_isrc, *h_idst;
36758 +       char *value, *p, *o, *e;
36759 +
36760 +       /* try stopping to update the source inode while we are referencing */
36761 +       /* there should not be the parent-child relationship between them */
36762 +       h_dst_dentry = h_dst->dentry;
36763 +       h_idst = d_inode(h_dst_dentry);
36764 +       h_src_dentry = h_src->dentry;
36765 +       h_isrc = d_inode(h_src_dentry);
36766 +       inode_unlock(h_idst);
36767 +       inode_lock_shared_nested(h_isrc, AuLsc_I_CHILD);
36768 +       inode_lock_nested(h_idst, AuLsc_I_CHILD2);
36769 +       unlocked = 0;
36770 +
36771 +       /* some filesystems don't list POSIX ACL, for example tmpfs */
36772 +       ssz = vfs_listxattr(h_src_dentry, NULL, 0);
36773 +       err = ssz;
36774 +       if (unlikely(err < 0)) {
36775 +               AuTraceErr(err);
36776 +               if (err == -ENODATA
36777 +                   || err == -EOPNOTSUPP)
36778 +                       err = 0;        /* ignore */
36779 +               goto out;
36780 +       }
36781 +
36782 +       err = 0;
36783 +       p = NULL;
36784 +       o = NULL;
36785 +       if (ssz) {
36786 +               err = -ENOMEM;
36787 +               p = kmalloc(ssz, GFP_NOFS);
36788 +               o = p;
36789 +               if (unlikely(!p))
36790 +                       goto out;
36791 +               err = vfs_listxattr(h_src_dentry, p, ssz);
36792 +       }
36793 +       inode_unlock_shared(h_isrc);
36794 +       unlocked = 1;
36795 +       AuDbg("err %d, ssz %zd\n", err, ssz);
36796 +       if (unlikely(err < 0))
36797 +               goto out_free;
36798 +
36799 +       err = 0;
36800 +       e = p + ssz;
36801 +       value = NULL;
36802 +       acl_access = 0;
36803 +       acl_default = 0;
36804 +       while (!err && p < e) {
36805 +               acl_access |= !strncmp(p, XATTR_NAME_POSIX_ACL_ACCESS,
36806 +                                      sizeof(XATTR_NAME_POSIX_ACL_ACCESS) - 1);
36807 +               acl_default |= !strncmp(p, XATTR_NAME_POSIX_ACL_DEFAULT,
36808 +                                       sizeof(XATTR_NAME_POSIX_ACL_DEFAULT)
36809 +                                       - 1);
36810 +               err = au_do_cpup_xattr(h_dst, h_src, p, &value, ignore_flags,
36811 +                                      verbose);
36812 +               p += strlen(p) + 1;
36813 +       }
36814 +       AuTraceErr(err);
36815 +       ignore_flags |= au_xattr_out_of_list;
36816 +       if (!err && !acl_access) {
36817 +               err = au_do_cpup_xattr(h_dst, h_src,
36818 +                                      XATTR_NAME_POSIX_ACL_ACCESS, &value,
36819 +                                      ignore_flags, verbose);
36820 +               AuTraceErr(err);
36821 +       }
36822 +       if (!err && !acl_default) {
36823 +               err = au_do_cpup_xattr(h_dst, h_src,
36824 +                                      XATTR_NAME_POSIX_ACL_DEFAULT, &value,
36825 +                                      ignore_flags, verbose);
36826 +               AuTraceErr(err);
36827 +       }
36828 +
36829 +       au_kfree_try_rcu(value);
36830 +
36831 +out_free:
36832 +       au_kfree_try_rcu(o);
36833 +out:
36834 +       if (!unlocked)
36835 +               inode_unlock_shared(h_isrc);
36836 +       AuTraceErr(err);
36837 +       return err;
36838 +}
36839 +
36840 +/* ---------------------------------------------------------------------- */
36841 +
36842 +static int au_smack_reentering(struct super_block *sb)
36843 +{
36844 +#if IS_ENABLED(CONFIG_SECURITY_SMACK) || IS_ENABLED(CONFIG_SECURITY_SELINUX)
36845 +       /*
36846 +        * as a part of lookup, smack_d_instantiate() is called, and it calls
36847 +        * i_op->getxattr(). ouch.
36848 +        */
36849 +       return si_pid_test(sb);
36850 +#else
36851 +       return 0;
36852 +#endif
36853 +}
36854 +
36855 +enum {
36856 +       AU_XATTR_LIST,
36857 +       AU_XATTR_GET
36858 +};
36859 +
36860 +struct au_lgxattr {
36861 +       int type;
36862 +       union {
36863 +               struct {
36864 +                       char    *list;
36865 +                       size_t  size;
36866 +               } list;
36867 +               struct {
36868 +                       const char      *name;
36869 +                       void            *value;
36870 +                       size_t          size;
36871 +               } get;
36872 +       } u;
36873 +};
36874 +
36875 +static ssize_t au_lgxattr(struct dentry *dentry, struct inode *inode,
36876 +                         struct au_lgxattr *arg)
36877 +{
36878 +       ssize_t err;
36879 +       int reenter;
36880 +       struct path h_path;
36881 +       struct super_block *sb;
36882 +
36883 +       sb = dentry->d_sb;
36884 +       reenter = au_smack_reentering(sb);
36885 +       if (!reenter) {
36886 +               err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
36887 +               if (unlikely(err))
36888 +                       goto out;
36889 +       }
36890 +       err = au_h_path_getattr(dentry, inode, /*force*/1, &h_path, reenter);
36891 +       if (unlikely(err))
36892 +               goto out_si;
36893 +       if (unlikely(!h_path.dentry))
36894 +               /* illegally overlapped or something */
36895 +               goto out_di; /* pretending success */
36896 +
36897 +       /* always topmost entry only */
36898 +       switch (arg->type) {
36899 +       case AU_XATTR_LIST:
36900 +               err = vfs_listxattr(h_path.dentry,
36901 +                                   arg->u.list.list, arg->u.list.size);
36902 +               break;
36903 +       case AU_XATTR_GET:
36904 +               AuDebugOn(d_is_negative(h_path.dentry));
36905 +               err = vfs_getxattr(mnt_user_ns(h_path.mnt), h_path.dentry,
36906 +                                  arg->u.get.name, arg->u.get.value,
36907 +                                  arg->u.get.size);
36908 +               break;
36909 +       }
36910 +
36911 +out_di:
36912 +       if (!reenter)
36913 +               di_read_unlock(dentry, AuLock_IR);
36914 +out_si:
36915 +       if (!reenter)
36916 +               si_read_unlock(sb);
36917 +out:
36918 +       AuTraceErr(err);
36919 +       return err;
36920 +}
36921 +
36922 +ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size)
36923 +{
36924 +       struct au_lgxattr arg = {
36925 +               .type = AU_XATTR_LIST,
36926 +               .u.list = {
36927 +                       .list   = list,
36928 +                       .size   = size
36929 +               },
36930 +       };
36931 +
36932 +       return au_lgxattr(dentry, /*inode*/NULL, &arg);
36933 +}
36934 +
36935 +static ssize_t au_getxattr(struct dentry *dentry, struct inode *inode,
36936 +                          const char *name, void *value, size_t size)
36937 +{
36938 +       struct au_lgxattr arg = {
36939 +               .type = AU_XATTR_GET,
36940 +               .u.get = {
36941 +                       .name   = name,
36942 +                       .value  = value,
36943 +                       .size   = size
36944 +               },
36945 +       };
36946 +
36947 +       return au_lgxattr(dentry, inode, &arg);
36948 +}
36949 +
36950 +static int au_setxattr(struct dentry *dentry, struct inode *inode,
36951 +                      const char *name, const void *value, size_t size,
36952 +                      int flags)
36953 +{
36954 +       struct au_sxattr arg = {
36955 +               .type = AU_XATTR_SET,
36956 +               .u.set = {
36957 +                       .name   = name,
36958 +                       .value  = value,
36959 +                       .size   = size,
36960 +                       .flags  = flags
36961 +               },
36962 +       };
36963 +
36964 +       return au_sxattr(dentry, inode, &arg);
36965 +}
36966 +
36967 +/* ---------------------------------------------------------------------- */
36968 +
36969 +static int au_xattr_get(const struct xattr_handler *handler,
36970 +                       struct dentry *dentry, struct inode *inode,
36971 +                       const char *name, void *buffer, size_t size)
36972 +{
36973 +       return au_getxattr(dentry, inode, name, buffer, size);
36974 +}
36975 +
36976 +static int au_xattr_set(const struct xattr_handler *handler,
36977 +                       struct user_namespace *userns,
36978 +                       struct dentry *dentry, struct inode *inode,
36979 +                       const char *name, const void *value, size_t size,
36980 +                       int flags)
36981 +{
36982 +       return au_setxattr(dentry, inode, name, value, size, flags);
36983 +}
36984 +
36985 +static const struct xattr_handler au_xattr_handler = {
36986 +       .name   = "",
36987 +       .prefix = "",
36988 +       .get    = au_xattr_get,
36989 +       .set    = au_xattr_set
36990 +};
36991 +
36992 +static const struct xattr_handler *au_xattr_handlers[] = {
36993 +#ifdef CONFIG_FS_POSIX_ACL
36994 +       &posix_acl_access_xattr_handler,
36995 +       &posix_acl_default_xattr_handler,
36996 +#endif
36997 +       &au_xattr_handler, /* must be last */
36998 +       NULL
36999 +};
37000 +
37001 +void au_xattr_init(struct super_block *sb)
37002 +{
37003 +       sb->s_xattr = au_xattr_handlers;
37004 +}
37005 diff -urN /usr/share/empty/fs/aufs/xino.c linux/fs/aufs/xino.c
37006 --- /usr/share/empty/fs/aufs/xino.c     1970-01-01 01:00:00.000000000 +0100
37007 +++ linux/fs/aufs/xino.c        2022-03-21 14:49:05.733299677 +0100
37008 @@ -0,0 +1,1926 @@
37009 +// SPDX-License-Identifier: GPL-2.0
37010 +/*
37011 + * Copyright (C) 2005-2021 Junjiro R. Okajima
37012 + *
37013 + * This program, aufs is free software; you can redistribute it and/or modify
37014 + * it under the terms of the GNU General Public License as published by
37015 + * the Free Software Foundation; either version 2 of the License, or
37016 + * (at your option) any later version.
37017 + *
37018 + * This program is distributed in the hope that it will be useful,
37019 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
37020 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
37021 + * GNU General Public License for more details.
37022 + *
37023 + * You should have received a copy of the GNU General Public License
37024 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
37025 + */
37026 +
37027 +/*
37028 + * external inode number translation table and bitmap
37029 + *
37030 + * things to consider
37031 + * - the lifetime
37032 + *   + au_xino object
37033 + *   + XINO files (xino, xib, xigen)
37034 + *   + dynamic debugfs entries (xiN)
37035 + *   + static debugfs entries (xib, xigen)
37036 + *   + static sysfs entry (xi_path)
37037 + * - several entry points to handle them.
37038 + *   + mount(2) without xino option (default)
37039 + *   + mount(2) with xino option
37040 + *   + mount(2) with noxino option
37041 + *   + umount(2)
37042 + *   + remount with add/del branches
37043 + *   + remount with xino/noxino options
37044 + */
37045 +
37046 +#include <linux/seq_file.h>
37047 +#include <linux/statfs.h>
37048 +#include "aufs.h"
37049 +
37050 +static aufs_bindex_t sbr_find_shared(struct super_block *sb, aufs_bindex_t btop,
37051 +                                    aufs_bindex_t bbot,
37052 +                                    struct super_block *h_sb)
37053 +{
37054 +       /* todo: try binary-search if the branches are many */
37055 +       for (; btop <= bbot; btop++)
37056 +               if (h_sb == au_sbr_sb(sb, btop))
37057 +                       return btop;
37058 +       return -1;
37059 +}
37060 +
37061 +/*
37062 + * find another branch who is on the same filesystem of the specified
37063 + * branch{@btgt}. search until @bbot.
37064 + */
37065 +static aufs_bindex_t is_sb_shared(struct super_block *sb, aufs_bindex_t btgt,
37066 +                                 aufs_bindex_t bbot)
37067 +{
37068 +       aufs_bindex_t bindex;
37069 +       struct super_block *tgt_sb;
37070 +
37071 +       tgt_sb = au_sbr_sb(sb, btgt);
37072 +       bindex = sbr_find_shared(sb, /*btop*/0, btgt - 1, tgt_sb);
37073 +       if (bindex < 0)
37074 +               bindex = sbr_find_shared(sb, btgt + 1, bbot, tgt_sb);
37075 +
37076 +       return bindex;
37077 +}
37078 +
37079 +/* ---------------------------------------------------------------------- */
37080 +
37081 +/*
37082 + * stop unnecessary notify events at creating xino files
37083 + */
37084 +
37085 +aufs_bindex_t au_xi_root(struct super_block *sb, struct dentry *dentry)
37086 +{
37087 +       aufs_bindex_t bfound, bindex, bbot;
37088 +       struct dentry *parent;
37089 +       struct au_branch *br;
37090 +
37091 +       bfound = -1;
37092 +       parent = dentry->d_parent; /* safe d_parent access */
37093 +       bbot = au_sbbot(sb);
37094 +       for (bindex = 0; bindex <= bbot; bindex++) {
37095 +               br = au_sbr(sb, bindex);
37096 +               if (au_br_dentry(br) == parent) {
37097 +                       bfound = bindex;
37098 +                       break;
37099 +               }
37100 +       }
37101 +
37102 +       AuDbg("bfound b%d\n", bfound);
37103 +       return bfound;
37104 +}
37105 +
37106 +struct au_xino_lock_dir {
37107 +       struct au_hinode *hdir;
37108 +       struct dentry *parent;
37109 +       struct inode *dir;
37110 +};
37111 +
37112 +static struct dentry *au_dget_parent_lock(struct dentry *dentry,
37113 +                                         unsigned int lsc)
37114 +{
37115 +       struct dentry *parent;
37116 +       struct inode *dir;
37117 +
37118 +       parent = dget_parent(dentry);
37119 +       dir = d_inode(parent);
37120 +       inode_lock_nested(dir, lsc);
37121 +#if 0 /* it should not happen */
37122 +       spin_lock(&dentry->d_lock);
37123 +       if (unlikely(dentry->d_parent != parent)) {
37124 +               spin_unlock(&dentry->d_lock);
37125 +               inode_unlock(dir);
37126 +               dput(parent);
37127 +               parent = NULL;
37128 +               goto out;
37129 +       }
37130 +       spin_unlock(&dentry->d_lock);
37131 +
37132 +out:
37133 +#endif
37134 +       return parent;
37135 +}
37136 +
37137 +static void au_xino_lock_dir(struct super_block *sb, struct path *xipath,
37138 +                            struct au_xino_lock_dir *ldir)
37139 +{
37140 +       aufs_bindex_t bindex;
37141 +
37142 +       ldir->hdir = NULL;
37143 +       bindex = au_xi_root(sb, xipath->dentry);
37144 +       if (bindex >= 0) {
37145 +               /* rw branch root */
37146 +               ldir->hdir = au_hi(d_inode(sb->s_root), bindex);
37147 +               au_hn_inode_lock_nested(ldir->hdir, AuLsc_I_PARENT);
37148 +       } else {
37149 +               /* other */
37150 +               ldir->parent = au_dget_parent_lock(xipath->dentry,
37151 +                                                  AuLsc_I_PARENT);
37152 +               ldir->dir = d_inode(ldir->parent);
37153 +       }
37154 +}
37155 +
37156 +static void au_xino_unlock_dir(struct au_xino_lock_dir *ldir)
37157 +{
37158 +       if (ldir->hdir)
37159 +               au_hn_inode_unlock(ldir->hdir);
37160 +       else {
37161 +               inode_unlock(ldir->dir);
37162 +               dput(ldir->parent);
37163 +       }
37164 +}
37165 +
37166 +/* ---------------------------------------------------------------------- */
37167 +
37168 +/*
37169 + * create and set a new xino file
37170 + */
37171 +struct file *au_xino_create(struct super_block *sb, char *fpath, int silent,
37172 +                           int wbrtop)
37173 +{
37174 +       struct file *file;
37175 +       struct dentry *h_parent, *d;
37176 +       struct inode *h_dir, *inode;
37177 +       int err;
37178 +       static DEFINE_MUTEX(mtx);
37179 +
37180 +       /*
37181 +        * at mount-time, and the xino file is the default path,
37182 +        * hnotify is disabled so we have no notify events to ignore.
37183 +        * when a user specified the xino, we cannot get au_hdir to be ignored.
37184 +        */
37185 +       if (!wbrtop)
37186 +               mutex_lock(&mtx);
37187 +       file = vfsub_filp_open(fpath, O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
37188 +                              /* | __FMODE_NONOTIFY */,
37189 +                              0666);
37190 +       if (IS_ERR(file)) {
37191 +               if (!wbrtop)
37192 +                       mutex_unlock(&mtx);
37193 +               if (!silent)
37194 +                       pr_err("open %s(%ld)\n", fpath, PTR_ERR(file));
37195 +               return file;
37196 +       }
37197 +
37198 +       /* keep file count */
37199 +       err = 0;
37200 +       d = file->f_path.dentry;
37201 +       h_parent = au_dget_parent_lock(d, AuLsc_I_PARENT);
37202 +       if (!wbrtop)
37203 +               mutex_unlock(&mtx);
37204 +       /* mnt_want_write() is unnecessary here */
37205 +       h_dir = d_inode(h_parent);
37206 +       inode = file_inode(file);
37207 +       /* no delegation since it is just created */
37208 +       if (inode->i_nlink)
37209 +               err = vfsub_unlink(h_dir, &file->f_path, /*delegated*/NULL,
37210 +                                  /*force*/0);
37211 +       inode_unlock(h_dir);
37212 +       dput(h_parent);
37213 +       if (unlikely(err)) {
37214 +               if (!silent)
37215 +                       pr_err("unlink %s(%d)\n", fpath, err);
37216 +               goto out;
37217 +       }
37218 +
37219 +       err = -EINVAL;
37220 +       if (unlikely(sb && sb == d->d_sb)) {
37221 +               if (!silent)
37222 +                       pr_err("%s must be outside\n", fpath);
37223 +               goto out;
37224 +       }
37225 +       if (unlikely(au_test_fs_bad_xino(d->d_sb))) {
37226 +               if (!silent)
37227 +                       pr_err("xino doesn't support %s(%s)\n",
37228 +                              fpath, au_sbtype(d->d_sb));
37229 +               goto out;
37230 +       }
37231 +       return file; /* success */
37232 +
37233 +out:
37234 +       fput(file);
37235 +       file = ERR_PTR(err);
37236 +       return file;
37237 +}
37238 +
37239 +/*
37240 + * create a new xinofile at the same place/path as @base.
37241 + */
37242 +struct file *au_xino_create2(struct super_block *sb, struct path *base,
37243 +                            struct file *copy_src)
37244 +{
37245 +       struct file *file;
37246 +       struct dentry *dentry;
37247 +       struct inode *dir, *delegated;
37248 +       struct qstr *name;
37249 +       struct path ppath, path;
37250 +       int err, do_unlock;
37251 +       struct au_xino_lock_dir ldir;
37252 +
37253 +       do_unlock = 1;
37254 +       au_xino_lock_dir(sb, base, &ldir);
37255 +       dentry = base->dentry;
37256 +       ppath.dentry = dentry->d_parent; /* dir inode is locked */
37257 +       ppath.mnt = base->mnt;
37258 +       dir = d_inode(ppath.dentry);
37259 +       IMustLock(dir);
37260 +
37261 +       name = &dentry->d_name;
37262 +       path.dentry = vfsub_lookup_one_len(name->name, &ppath, name->len);
37263 +       if (IS_ERR(path.dentry)) {
37264 +               file = (void *)path.dentry;
37265 +               pr_err("%pd lookup err %ld\n", dentry, PTR_ERR(path.dentry));
37266 +               goto out;
37267 +       }
37268 +
37269 +       /* no need to mnt_want_write() since we call dentry_open() later */
37270 +       err = vfs_create(mnt_user_ns(base->mnt), dir, path.dentry, 0666, NULL);
37271 +       if (unlikely(err)) {
37272 +               file = ERR_PTR(err);
37273 +               pr_err("%pd create err %d\n", dentry, err);
37274 +               goto out_dput;
37275 +       }
37276 +
37277 +       path.mnt = base->mnt;
37278 +       file = vfsub_dentry_open(&path,
37279 +                                O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
37280 +                                /* | __FMODE_NONOTIFY */);
37281 +       if (IS_ERR(file)) {
37282 +               pr_err("%pd open err %ld\n", dentry, PTR_ERR(file));
37283 +               goto out_dput;
37284 +       }
37285 +
37286 +       delegated = NULL;
37287 +       err = vfsub_unlink(dir, &file->f_path, &delegated, /*force*/0);
37288 +       au_xino_unlock_dir(&ldir);
37289 +       do_unlock = 0;
37290 +       if (unlikely(err == -EWOULDBLOCK)) {
37291 +               pr_warn("cannot retry for NFSv4 delegation"
37292 +                       " for an internal unlink\n");
37293 +               iput(delegated);
37294 +       }
37295 +       if (unlikely(err)) {
37296 +               pr_err("%pd unlink err %d\n", dentry, err);
37297 +               goto out_fput;
37298 +       }
37299 +
37300 +       if (copy_src) {
37301 +               /* no one can touch copy_src xino */
37302 +               err = au_copy_file(file, copy_src, vfsub_f_size_read(copy_src));
37303 +               if (unlikely(err)) {
37304 +                       pr_err("%pd copy err %d\n", dentry, err);
37305 +                       goto out_fput;
37306 +               }
37307 +       }
37308 +       goto out_dput; /* success */
37309 +
37310 +out_fput:
37311 +       fput(file);
37312 +       file = ERR_PTR(err);
37313 +out_dput:
37314 +       dput(path.dentry);
37315 +out:
37316 +       if (do_unlock)
37317 +               au_xino_unlock_dir(&ldir);
37318 +       return file;
37319 +}
37320 +
37321 +struct file *au_xino_file1(struct au_xino *xi)
37322 +{
37323 +       struct file *file;
37324 +       unsigned int u, nfile;
37325 +
37326 +       file = NULL;
37327 +       nfile = xi->xi_nfile;
37328 +       for (u = 0; u < nfile; u++) {
37329 +               file = xi->xi_file[u];
37330 +               if (file)
37331 +                       break;
37332 +       }
37333 +
37334 +       return file;
37335 +}
37336 +
37337 +static int au_xino_file_set(struct au_xino *xi, int idx, struct file *file)
37338 +{
37339 +       int err;
37340 +       struct file *f;
37341 +       void *p;
37342 +
37343 +       if (file)
37344 +               get_file(file);
37345 +
37346 +       err = 0;
37347 +       f = NULL;
37348 +       if (idx < xi->xi_nfile) {
37349 +               f = xi->xi_file[idx];
37350 +               if (f)
37351 +                       fput(f);
37352 +       } else {
37353 +               p = au_kzrealloc(xi->xi_file,
37354 +                                sizeof(*xi->xi_file) * xi->xi_nfile,
37355 +                                sizeof(*xi->xi_file) * (idx + 1),
37356 +                                GFP_NOFS, /*may_shrink*/0);
37357 +               if (p) {
37358 +                       MtxMustLock(&xi->xi_mtx);
37359 +                       xi->xi_file = p;
37360 +                       xi->xi_nfile = idx + 1;
37361 +               } else {
37362 +                       err = -ENOMEM;
37363 +                       if (file)
37364 +                               fput(file);
37365 +                       goto out;
37366 +               }
37367 +       }
37368 +       xi->xi_file[idx] = file;
37369 +
37370 +out:
37371 +       return err;
37372 +}
37373 +
37374 +/*
37375 + * if @xinew->xi is not set, then create new xigen file.
37376 + */
37377 +struct file *au_xi_new(struct super_block *sb, struct au_xi_new *xinew)
37378 +{
37379 +       struct file *file;
37380 +       int err;
37381 +
37382 +       SiMustAnyLock(sb);
37383 +
37384 +       file = au_xino_create2(sb, xinew->base, xinew->copy_src);
37385 +       if (IS_ERR(file)) {
37386 +               err = PTR_ERR(file);
37387 +               pr_err("%s[%d], err %d\n",
37388 +                      xinew->xi ? "xino" : "xigen",
37389 +                      xinew->idx, err);
37390 +               goto out;
37391 +       }
37392 +
37393 +       if (xinew->xi)
37394 +               err = au_xino_file_set(xinew->xi, xinew->idx, file);
37395 +       else {
37396 +               BUG();
37397 +               /* todo: make xigen file an array */
37398 +               /* err = au_xigen_file_set(sb, xinew->idx, file); */
37399 +       }
37400 +       fput(file);
37401 +       if (unlikely(err))
37402 +               file = ERR_PTR(err);
37403 +
37404 +out:
37405 +       return file;
37406 +}
37407 +
37408 +/* ---------------------------------------------------------------------- */
37409 +
37410 +/*
37411 + * truncate xino files
37412 + */
37413 +static int au_xino_do_trunc(struct super_block *sb, aufs_bindex_t bindex,
37414 +                           int idx, struct kstatfs *st)
37415 +{
37416 +       int err;
37417 +       blkcnt_t blocks;
37418 +       struct file *file, *new_xino;
37419 +       struct au_xi_new xinew = {
37420 +               .idx = idx
37421 +       };
37422 +
37423 +       err = 0;
37424 +       xinew.xi = au_sbr(sb, bindex)->br_xino;
37425 +       file = au_xino_file(xinew.xi, idx);
37426 +       if (!file)
37427 +               goto out;
37428 +
37429 +       xinew.base = &file->f_path;
37430 +       err = vfs_statfs(xinew.base, st);
37431 +       if (unlikely(err)) {
37432 +               AuErr1("statfs err %d, ignored\n", err);
37433 +               err = 0;
37434 +               goto out;
37435 +       }
37436 +
37437 +       blocks = file_inode(file)->i_blocks;
37438 +       pr_info("begin truncating xino(b%d-%d), ib%llu, %llu/%llu free blks\n",
37439 +               bindex, idx, (u64)blocks, st->f_bfree, st->f_blocks);
37440 +
37441 +       xinew.copy_src = file;
37442 +       new_xino = au_xi_new(sb, &xinew);
37443 +       if (IS_ERR(new_xino)) {
37444 +               err = PTR_ERR(new_xino);
37445 +               pr_err("xino(b%d-%d), err %d, ignored\n", bindex, idx, err);
37446 +               goto out;
37447 +       }
37448 +
37449 +       err = vfs_statfs(&new_xino->f_path, st);
37450 +       if (!err)
37451 +               pr_info("end truncating xino(b%d-%d), ib%llu, %llu/%llu free blks\n",
37452 +                       bindex, idx, (u64)file_inode(new_xino)->i_blocks,
37453 +                       st->f_bfree, st->f_blocks);
37454 +       else {
37455 +               AuErr1("statfs err %d, ignored\n", err);
37456 +               err = 0;
37457 +       }
37458 +
37459 +out:
37460 +       return err;
37461 +}
37462 +
37463 +int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex, int idx_begin)
37464 +{
37465 +       int err, i;
37466 +       unsigned long jiffy;
37467 +       aufs_bindex_t bbot;
37468 +       struct kstatfs *st;
37469 +       struct au_branch *br;
37470 +       struct au_xino *xi;
37471 +
37472 +       err = -ENOMEM;
37473 +       st = kmalloc(sizeof(*st), GFP_NOFS);
37474 +       if (unlikely(!st))
37475 +               goto out;
37476 +
37477 +       err = -EINVAL;
37478 +       bbot = au_sbbot(sb);
37479 +       if (unlikely(bindex < 0 || bbot < bindex))
37480 +               goto out_st;
37481 +
37482 +       err = 0;
37483 +       jiffy = jiffies;
37484 +       br = au_sbr(sb, bindex);
37485 +       xi = br->br_xino;
37486 +       for (i = idx_begin; !err && i < xi->xi_nfile; i++)
37487 +               err = au_xino_do_trunc(sb, bindex, i, st);
37488 +       if (!err)
37489 +               au_sbi(sb)->si_xino_jiffy = jiffy;
37490 +
37491 +out_st:
37492 +       au_kfree_rcu(st);
37493 +out:
37494 +       return err;
37495 +}
37496 +
37497 +struct xino_do_trunc_args {
37498 +       struct super_block *sb;
37499 +       struct au_branch *br;
37500 +       int idx;
37501 +};
37502 +
37503 +static void xino_do_trunc(void *_args)
37504 +{
37505 +       struct xino_do_trunc_args *args = _args;
37506 +       struct super_block *sb;
37507 +       struct au_branch *br;
37508 +       struct inode *dir;
37509 +       int err, idx;
37510 +       aufs_bindex_t bindex;
37511 +
37512 +       err = 0;
37513 +       sb = args->sb;
37514 +       dir = d_inode(sb->s_root);
37515 +       br = args->br;
37516 +       idx = args->idx;
37517 +
37518 +       si_noflush_write_lock(sb);
37519 +       ii_read_lock_parent(dir);
37520 +       bindex = au_br_index(sb, br->br_id);
37521 +       err = au_xino_trunc(sb, bindex, idx);
37522 +       ii_read_unlock(dir);
37523 +       if (unlikely(err))
37524 +               pr_warn("err b%d, (%d)\n", bindex, err);
37525 +       atomic_dec(&br->br_xino->xi_truncating);
37526 +       au_lcnt_dec(&br->br_count);
37527 +       si_write_unlock(sb);
37528 +       au_nwt_done(&au_sbi(sb)->si_nowait);
37529 +       au_kfree_rcu(args);
37530 +}
37531 +
37532 +/*
37533 + * returns the index in the xi_file array whose corresponding file is necessary
37534 + * to truncate, or -1 which means no need to truncate.
37535 + */
37536 +static int xino_trunc_test(struct super_block *sb, struct au_branch *br)
37537 +{
37538 +       int err;
37539 +       unsigned int u;
37540 +       struct kstatfs st;
37541 +       struct au_sbinfo *sbinfo;
37542 +       struct au_xino *xi;
37543 +       struct file *file;
37544 +
37545 +       /* todo: si_xino_expire and the ratio should be customizable */
37546 +       sbinfo = au_sbi(sb);
37547 +       if (time_before(jiffies,
37548 +                       sbinfo->si_xino_jiffy + sbinfo->si_xino_expire))
37549 +               return -1;
37550 +
37551 +       /* truncation border */
37552 +       xi = br->br_xino;
37553 +       for (u = 0; u < xi->xi_nfile; u++) {
37554 +               file = au_xino_file(xi, u);
37555 +               if (!file)
37556 +                       continue;
37557 +
37558 +               err = vfs_statfs(&file->f_path, &st);
37559 +               if (unlikely(err)) {
37560 +                       AuErr1("statfs err %d, ignored\n", err);
37561 +                       return -1;
37562 +               }
37563 +               if (div64_u64(st.f_bfree * 100, st.f_blocks)
37564 +                   >= AUFS_XINO_DEF_TRUNC)
37565 +                       return u;
37566 +       }
37567 +
37568 +       return -1;
37569 +}
37570 +
37571 +static void xino_try_trunc(struct super_block *sb, struct au_branch *br)
37572 +{
37573 +       int idx;
37574 +       struct xino_do_trunc_args *args;
37575 +       int wkq_err;
37576 +
37577 +       idx = xino_trunc_test(sb, br);
37578 +       if (idx < 0)
37579 +               return;
37580 +
37581 +       if (atomic_inc_return(&br->br_xino->xi_truncating) > 1)
37582 +               goto out;
37583 +
37584 +       /* lock and kfree() will be called in trunc_xino() */
37585 +       args = kmalloc(sizeof(*args), GFP_NOFS);
37586 +       if (unlikely(!args)) {
37587 +               AuErr1("no memory\n");
37588 +               goto out;
37589 +       }
37590 +
37591 +       au_lcnt_inc(&br->br_count);
37592 +       args->sb = sb;
37593 +       args->br = br;
37594 +       args->idx = idx;
37595 +       wkq_err = au_wkq_nowait(xino_do_trunc, args, sb, /*flags*/0);
37596 +       if (!wkq_err)
37597 +               return; /* success */
37598 +
37599 +       pr_err("wkq %d\n", wkq_err);
37600 +       au_lcnt_dec(&br->br_count);
37601 +       au_kfree_rcu(args);
37602 +
37603 +out:
37604 +       atomic_dec(&br->br_xino->xi_truncating);
37605 +}
37606 +
37607 +/* ---------------------------------------------------------------------- */
37608 +
37609 +struct au_xi_calc {
37610 +       int idx;
37611 +       loff_t pos;
37612 +};
37613 +
37614 +static void au_xi_calc(struct super_block *sb, ino_t h_ino,
37615 +                      struct au_xi_calc *calc)
37616 +{
37617 +       loff_t maxent;
37618 +
37619 +       maxent = au_xi_maxent(sb);
37620 +       calc->idx = div64_u64_rem(h_ino, maxent, &calc->pos);
37621 +       calc->pos *= sizeof(ino_t);
37622 +}
37623 +
37624 +static int au_xino_do_new_async(struct super_block *sb, struct au_branch *br,
37625 +                               struct au_xi_calc *calc)
37626 +{
37627 +       int err;
37628 +       struct file *file;
37629 +       struct au_xino *xi = br->br_xino;
37630 +       struct au_xi_new xinew = {
37631 +               .xi = xi
37632 +       };
37633 +
37634 +       SiMustAnyLock(sb);
37635 +
37636 +       err = 0;
37637 +       if (!xi)
37638 +               goto out;
37639 +
37640 +       mutex_lock(&xi->xi_mtx);
37641 +       file = au_xino_file(xi, calc->idx);
37642 +       if (file)
37643 +               goto out_mtx;
37644 +
37645 +       file = au_xino_file(xi, /*idx*/-1);
37646 +       AuDebugOn(!file);
37647 +       xinew.idx = calc->idx;
37648 +       xinew.base = &file->f_path;
37649 +       /* xinew.copy_src = NULL; */
37650 +       file = au_xi_new(sb, &xinew);
37651 +       if (IS_ERR(file))
37652 +               err = PTR_ERR(file);
37653 +
37654 +out_mtx:
37655 +       mutex_unlock(&xi->xi_mtx);
37656 +out:
37657 +       return err;
37658 +}
37659 +
37660 +struct au_xino_do_new_async_args {
37661 +       struct super_block *sb;
37662 +       struct au_branch *br;
37663 +       struct au_xi_calc calc;
37664 +       ino_t ino;
37665 +};
37666 +
37667 +struct au_xi_writing {
37668 +       struct hlist_bl_node node;
37669 +       ino_t h_ino, ino;
37670 +};
37671 +
37672 +static int au_xino_do_write(struct file *file, struct au_xi_calc *calc,
37673 +                           ino_t ino);
37674 +
37675 +static void au_xino_call_do_new_async(void *args)
37676 +{
37677 +       struct au_xino_do_new_async_args *a = args;
37678 +       struct au_branch *br;
37679 +       struct super_block *sb;
37680 +       struct au_sbinfo *sbi;
37681 +       struct inode *root;
37682 +       struct file *file;
37683 +       struct au_xi_writing *del, *p;
37684 +       struct hlist_bl_head *hbl;
37685 +       struct hlist_bl_node *pos;
37686 +       int err;
37687 +
37688 +       br = a->br;
37689 +       sb = a->sb;
37690 +       sbi = au_sbi(sb);
37691 +       si_noflush_read_lock(sb);
37692 +       root = d_inode(sb->s_root);
37693 +       ii_read_lock_child(root);
37694 +       err = au_xino_do_new_async(sb, br, &a->calc);
37695 +       if (unlikely(err)) {
37696 +               AuIOErr("err %d\n", err);
37697 +               goto out;
37698 +       }
37699 +
37700 +       file = au_xino_file(br->br_xino, a->calc.idx);
37701 +       AuDebugOn(!file);
37702 +       err = au_xino_do_write(file, &a->calc, a->ino);
37703 +       if (unlikely(err)) {
37704 +               AuIOErr("err %d\n", err);
37705 +               goto out;
37706 +       }
37707 +
37708 +       del = NULL;
37709 +       hbl = &br->br_xino->xi_writing;
37710 +       hlist_bl_lock(hbl);
37711 +       au_hbl_for_each(pos, hbl) {
37712 +               p = container_of(pos, struct au_xi_writing, node);
37713 +               if (p->ino == a->ino) {
37714 +                       del = p;
37715 +                       hlist_bl_del(&p->node);
37716 +                       break;
37717 +               }
37718 +       }
37719 +       hlist_bl_unlock(hbl);
37720 +       au_kfree_rcu(del);
37721 +
37722 +out:
37723 +       au_lcnt_dec(&br->br_count);
37724 +       ii_read_unlock(root);
37725 +       si_read_unlock(sb);
37726 +       au_nwt_done(&sbi->si_nowait);
37727 +       au_kfree_rcu(a);
37728 +}
37729 +
37730 +/*
37731 + * create a new xino file asynchronously
37732 + */
37733 +static int au_xino_new_async(struct super_block *sb, struct au_branch *br,
37734 +                            struct au_xi_calc *calc, ino_t ino)
37735 +{
37736 +       int err;
37737 +       struct au_xino_do_new_async_args *arg;
37738 +
37739 +       err = -ENOMEM;
37740 +       arg = kmalloc(sizeof(*arg), GFP_NOFS);
37741 +       if (unlikely(!arg))
37742 +               goto out;
37743 +
37744 +       arg->sb = sb;
37745 +       arg->br = br;
37746 +       arg->calc = *calc;
37747 +       arg->ino = ino;
37748 +       au_lcnt_inc(&br->br_count);
37749 +       err = au_wkq_nowait(au_xino_call_do_new_async, arg, sb, AuWkq_NEST);
37750 +       if (unlikely(err)) {
37751 +               pr_err("wkq %d\n", err);
37752 +               au_lcnt_dec(&br->br_count);
37753 +               au_kfree_rcu(arg);
37754 +       }
37755 +
37756 +out:
37757 +       return err;
37758 +}
37759 +
37760 +/*
37761 + * read @ino from xinofile for the specified branch{@sb, @bindex}
37762 + * at the position of @h_ino.
37763 + */
37764 +int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
37765 +                ino_t *ino)
37766 +{
37767 +       int err;
37768 +       ssize_t sz;
37769 +       struct au_xi_calc calc;
37770 +       struct au_sbinfo *sbinfo;
37771 +       struct file *file;
37772 +       struct au_xino *xi;
37773 +       struct hlist_bl_head *hbl;
37774 +       struct hlist_bl_node *pos;
37775 +       struct au_xi_writing *p;
37776 +
37777 +       *ino = 0;
37778 +       if (!au_opt_test(au_mntflags(sb), XINO))
37779 +               return 0; /* no xino */
37780 +
37781 +       err = 0;
37782 +       au_xi_calc(sb, h_ino, &calc);
37783 +       xi = au_sbr(sb, bindex)->br_xino;
37784 +       file = au_xino_file(xi, calc.idx);
37785 +       if (!file) {
37786 +               hbl = &xi->xi_writing;
37787 +               hlist_bl_lock(hbl);
37788 +               au_hbl_for_each(pos, hbl) {
37789 +                       p = container_of(pos, struct au_xi_writing, node);
37790 +                       if (p->h_ino == h_ino) {
37791 +                               AuDbg("hi%llu, i%llu, found\n",
37792 +                                     (u64)p->h_ino, (u64)p->ino);
37793 +                               *ino = p->ino;
37794 +                               break;
37795 +                       }
37796 +               }
37797 +               hlist_bl_unlock(hbl);
37798 +               return 0;
37799 +       } else if (vfsub_f_size_read(file) < calc.pos + sizeof(*ino))
37800 +               return 0; /* no xino */
37801 +
37802 +       sbinfo = au_sbi(sb);
37803 +       sz = xino_fread(file, ino, sizeof(*ino), &calc.pos);
37804 +       if (sz == sizeof(*ino))
37805 +               return 0; /* success */
37806 +
37807 +       err = sz;
37808 +       if (unlikely(sz >= 0)) {
37809 +               err = -EIO;
37810 +               AuIOErr("xino read error (%zd)\n", sz);
37811 +       }
37812 +       return err;
37813 +}
37814 +
37815 +static int au_xino_do_write(struct file *file, struct au_xi_calc *calc,
37816 +                           ino_t ino)
37817 +{
37818 +       ssize_t sz;
37819 +
37820 +       sz = xino_fwrite(file, &ino, sizeof(ino), &calc->pos);
37821 +       if (sz == sizeof(ino))
37822 +               return 0; /* success */
37823 +
37824 +       AuIOErr("write failed (%zd)\n", sz);
37825 +       return -EIO;
37826 +}
37827 +
37828 +/*
37829 + * write @ino to the xinofile for the specified branch{@sb, @bindex}
37830 + * at the position of @h_ino.
37831 + * even if @ino is zero, it is written to the xinofile and means no entry.
37832 + * if the size of the xino file on a specific filesystem exceeds the watermark,
37833 + * try truncating it.
37834 + */
37835 +int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
37836 +                 ino_t ino)
37837 +{
37838 +       int err;
37839 +       unsigned int mnt_flags;
37840 +       struct au_xi_calc calc;
37841 +       struct file *file;
37842 +       struct au_branch *br;
37843 +       struct au_xino *xi;
37844 +       struct au_xi_writing *p;
37845 +
37846 +       SiMustAnyLock(sb);
37847 +
37848 +       mnt_flags = au_mntflags(sb);
37849 +       if (!au_opt_test(mnt_flags, XINO))
37850 +               return 0;
37851 +
37852 +       au_xi_calc(sb, h_ino, &calc);
37853 +       br = au_sbr(sb, bindex);
37854 +       xi = br->br_xino;
37855 +       file = au_xino_file(xi, calc.idx);
37856 +       if (!file) {
37857 +               /* store the inum pair into the list */
37858 +               p = kmalloc(sizeof(*p), GFP_NOFS | __GFP_NOFAIL);
37859 +               p->h_ino = h_ino;
37860 +               p->ino = ino;
37861 +               au_hbl_add(&p->node, &xi->xi_writing);
37862 +
37863 +               /* create and write a new xino file asynchronously */
37864 +               err = au_xino_new_async(sb, br, &calc, ino);
37865 +               if (!err)
37866 +                       return 0; /* success */
37867 +               goto out;
37868 +       }
37869 +
37870 +       err = au_xino_do_write(file, &calc, ino);
37871 +       if (!err) {
37872 +               br = au_sbr(sb, bindex);
37873 +               if (au_opt_test(mnt_flags, TRUNC_XINO)
37874 +                   && au_test_fs_trunc_xino(au_br_sb(br)))
37875 +                       xino_try_trunc(sb, br);
37876 +               return 0; /* success */
37877 +       }
37878 +
37879 +out:
37880 +       AuIOErr("write failed (%d)\n", err);
37881 +       return -EIO;
37882 +}
37883 +
37884 +static ssize_t xino_fread_wkq(struct file *file, void *buf, size_t size,
37885 +                             loff_t *pos);
37886 +
37887 +/* todo: unnecessary to support mmap_sem since kernel-space? */
37888 +ssize_t xino_fread(struct file *file, void *kbuf, size_t size, loff_t *pos)
37889 +{
37890 +       ssize_t err;
37891 +       int i;
37892 +       const int prevent_endless = 10;
37893 +
37894 +       i = 0;
37895 +       do {
37896 +               err = vfsub_read_k(file, kbuf, size, pos);
37897 +               if (err == -EINTR
37898 +                   && !au_wkq_test()
37899 +                   && fatal_signal_pending(current)) {
37900 +                       err = xino_fread_wkq(file, kbuf, size, pos);
37901 +                       BUG_ON(err == -EINTR);
37902 +               }
37903 +       } while (i++ < prevent_endless
37904 +                && (err == -EAGAIN || err == -EINTR));
37905 +
37906 +#if 0 /* reserved for future use */
37907 +       if (err > 0)
37908 +               fsnotify_access(file->f_path.dentry);
37909 +#endif
37910 +
37911 +       return err;
37912 +}
37913 +
37914 +struct xino_fread_args {
37915 +       ssize_t *errp;
37916 +       struct file *file;
37917 +       void *buf;
37918 +       size_t size;
37919 +       loff_t *pos;
37920 +};
37921 +
37922 +static void call_xino_fread(void *args)
37923 +{
37924 +       struct xino_fread_args *a = args;
37925 +       *a->errp = xino_fread(a->file, a->buf, a->size, a->pos);
37926 +}
37927 +
37928 +static ssize_t xino_fread_wkq(struct file *file, void *buf, size_t size,
37929 +                             loff_t *pos)
37930 +{
37931 +       ssize_t err;
37932 +       int wkq_err;
37933 +       struct xino_fread_args args = {
37934 +               .errp   = &err,
37935 +               .file   = file,
37936 +               .buf    = buf,
37937 +               .size   = size,
37938 +               .pos    = pos
37939 +       };
37940 +
37941 +       wkq_err = au_wkq_wait(call_xino_fread, &args);
37942 +       if (unlikely(wkq_err))
37943 +               err = wkq_err;
37944 +
37945 +       return err;
37946 +}
37947 +
37948 +static ssize_t xino_fwrite_wkq(struct file *file, void *buf, size_t size,
37949 +                              loff_t *pos);
37950 +
37951 +static ssize_t do_xino_fwrite(struct file *file, void *kbuf, size_t size,
37952 +                             loff_t *pos)
37953 +{
37954 +       ssize_t err;
37955 +       int i;
37956 +       const int prevent_endless = 10;
37957 +
37958 +       i = 0;
37959 +       do {
37960 +               err = vfsub_write_k(file, kbuf, size, pos);
37961 +               if (err == -EINTR
37962 +                   && !au_wkq_test()
37963 +                   && fatal_signal_pending(current)) {
37964 +                       err = xino_fwrite_wkq(file, kbuf, size, pos);
37965 +                       BUG_ON(err == -EINTR);
37966 +               }
37967 +       } while (i++ < prevent_endless
37968 +                && (err == -EAGAIN || err == -EINTR));
37969 +
37970 +#if 0 /* reserved for future use */
37971 +       if (err > 0)
37972 +               fsnotify_modify(file->f_path.dentry);
37973 +#endif
37974 +
37975 +       return err;
37976 +}
37977 +
37978 +struct do_xino_fwrite_args {
37979 +       ssize_t *errp;
37980 +       struct file *file;
37981 +       void *buf;
37982 +       size_t size;
37983 +       loff_t *pos;
37984 +};
37985 +
37986 +static void call_do_xino_fwrite(void *args)
37987 +{
37988 +       struct do_xino_fwrite_args *a = args;
37989 +       *a->errp = do_xino_fwrite(a->file, a->buf, a->size, a->pos);
37990 +}
37991 +
37992 +static ssize_t xino_fwrite_wkq(struct file *file, void *buf, size_t size,
37993 +                              loff_t *pos)
37994 +{
37995 +       ssize_t err;
37996 +       int wkq_err;
37997 +       struct do_xino_fwrite_args args = {
37998 +               .errp   = &err,
37999 +               .file   = file,
38000 +               .buf    = buf,
38001 +               .size   = size,
38002 +               .pos    = pos
38003 +       };
38004 +
38005 +       /*
38006 +        * it breaks RLIMIT_FSIZE and normal user's limit,
38007 +        * users should care about quota and real 'filesystem full.'
38008 +        */
38009 +       wkq_err = au_wkq_wait(call_do_xino_fwrite, &args);
38010 +       if (unlikely(wkq_err))
38011 +               err = wkq_err;
38012 +
38013 +       return err;
38014 +}
38015 +
38016 +ssize_t xino_fwrite(struct file *file, void *buf, size_t size, loff_t *pos)
38017 +{
38018 +       ssize_t err;
38019 +
38020 +       if (rlimit(RLIMIT_FSIZE) == RLIM_INFINITY) {
38021 +               lockdep_off();
38022 +               err = do_xino_fwrite(file, buf, size, pos);
38023 +               lockdep_on();
38024 +       } else {
38025 +               lockdep_off();
38026 +               err = xino_fwrite_wkq(file, buf, size, pos);
38027 +               lockdep_on();
38028 +       }
38029 +
38030 +       return err;
38031 +}
38032 +
38033 +/* ---------------------------------------------------------------------- */
38034 +
38035 +/*
38036 + * inode number bitmap
38037 + */
38038 +static const int page_bits = (int)PAGE_SIZE * BITS_PER_BYTE;
38039 +static ino_t xib_calc_ino(unsigned long pindex, int bit)
38040 +{
38041 +       ino_t ino;
38042 +
38043 +       AuDebugOn(bit < 0 || page_bits <= bit);
38044 +       ino = AUFS_FIRST_INO + pindex * page_bits + bit;
38045 +       return ino;
38046 +}
38047 +
38048 +static void xib_calc_bit(ino_t ino, unsigned long *pindex, int *bit)
38049 +{
38050 +       AuDebugOn(ino < AUFS_FIRST_INO);
38051 +       ino -= AUFS_FIRST_INO;
38052 +       *pindex = ino / page_bits;
38053 +       *bit = ino % page_bits;
38054 +}
38055 +
38056 +static int xib_pindex(struct super_block *sb, unsigned long pindex)
38057 +{
38058 +       int err;
38059 +       loff_t pos;
38060 +       ssize_t sz;
38061 +       struct au_sbinfo *sbinfo;
38062 +       struct file *xib;
38063 +       unsigned long *p;
38064 +
38065 +       sbinfo = au_sbi(sb);
38066 +       MtxMustLock(&sbinfo->si_xib_mtx);
38067 +       AuDebugOn(pindex > ULONG_MAX / PAGE_SIZE
38068 +                 || !au_opt_test(sbinfo->si_mntflags, XINO));
38069 +
38070 +       if (pindex == sbinfo->si_xib_last_pindex)
38071 +               return 0;
38072 +
38073 +       xib = sbinfo->si_xib;
38074 +       p = sbinfo->si_xib_buf;
38075 +       pos = sbinfo->si_xib_last_pindex;
38076 +       pos *= PAGE_SIZE;
38077 +       sz = xino_fwrite(xib, p, PAGE_SIZE, &pos);
38078 +       if (unlikely(sz != PAGE_SIZE))
38079 +               goto out;
38080 +
38081 +       pos = pindex;
38082 +       pos *= PAGE_SIZE;
38083 +       if (vfsub_f_size_read(xib) >= pos + PAGE_SIZE)
38084 +               sz = xino_fread(xib, p, PAGE_SIZE, &pos);
38085 +       else {
38086 +               memset(p, 0, PAGE_SIZE);
38087 +               sz = xino_fwrite(xib, p, PAGE_SIZE, &pos);
38088 +       }
38089 +       if (sz == PAGE_SIZE) {
38090 +               sbinfo->si_xib_last_pindex = pindex;
38091 +               return 0; /* success */
38092 +       }
38093 +
38094 +out:
38095 +       AuIOErr1("write failed (%zd)\n", sz);
38096 +       err = sz;
38097 +       if (sz >= 0)
38098 +               err = -EIO;
38099 +       return err;
38100 +}
38101 +
38102 +static void au_xib_clear_bit(struct inode *inode)
38103 +{
38104 +       int err, bit;
38105 +       unsigned long pindex;
38106 +       struct super_block *sb;
38107 +       struct au_sbinfo *sbinfo;
38108 +
38109 +       AuDebugOn(inode->i_nlink);
38110 +
38111 +       sb = inode->i_sb;
38112 +       xib_calc_bit(inode->i_ino, &pindex, &bit);
38113 +       AuDebugOn(page_bits <= bit);
38114 +       sbinfo = au_sbi(sb);
38115 +       mutex_lock(&sbinfo->si_xib_mtx);
38116 +       err = xib_pindex(sb, pindex);
38117 +       if (!err) {
38118 +               clear_bit(bit, sbinfo->si_xib_buf);
38119 +               sbinfo->si_xib_next_bit = bit;
38120 +       }
38121 +       mutex_unlock(&sbinfo->si_xib_mtx);
38122 +}
38123 +
38124 +/* ---------------------------------------------------------------------- */
38125 +
38126 +/*
38127 + * truncate a xino bitmap file
38128 + */
38129 +
38130 +/* todo: slow */
38131 +static int do_xib_restore(struct super_block *sb, struct file *file, void *page)
38132 +{
38133 +       int err, bit;
38134 +       ssize_t sz;
38135 +       unsigned long pindex;
38136 +       loff_t pos, pend;
38137 +       struct au_sbinfo *sbinfo;
38138 +       ino_t *ino;
38139 +       unsigned long *p;
38140 +
38141 +       err = 0;
38142 +       sbinfo = au_sbi(sb);
38143 +       MtxMustLock(&sbinfo->si_xib_mtx);
38144 +       p = sbinfo->si_xib_buf;
38145 +       pend = vfsub_f_size_read(file);
38146 +       pos = 0;
38147 +       while (pos < pend) {
38148 +               sz = xino_fread(file, page, PAGE_SIZE, &pos);
38149 +               err = sz;
38150 +               if (unlikely(sz <= 0))
38151 +                       goto out;
38152 +
38153 +               err = 0;
38154 +               for (ino = page; sz > 0; ino++, sz -= sizeof(ino)) {
38155 +                       if (unlikely(*ino < AUFS_FIRST_INO))
38156 +                               continue;
38157 +
38158 +                       xib_calc_bit(*ino, &pindex, &bit);
38159 +                       AuDebugOn(page_bits <= bit);
38160 +                       err = xib_pindex(sb, pindex);
38161 +                       if (!err)
38162 +                               set_bit(bit, p);
38163 +                       else
38164 +                               goto out;
38165 +               }
38166 +       }
38167 +
38168 +out:
38169 +       return err;
38170 +}
38171 +
38172 +static int xib_restore(struct super_block *sb)
38173 +{
38174 +       int err, i;
38175 +       unsigned int nfile;
38176 +       aufs_bindex_t bindex, bbot;
38177 +       void *page;
38178 +       struct au_branch *br;
38179 +       struct au_xino *xi;
38180 +       struct file *file;
38181 +
38182 +       err = -ENOMEM;
38183 +       page = (void *)__get_free_page(GFP_NOFS);
38184 +       if (unlikely(!page))
38185 +               goto out;
38186 +
38187 +       err = 0;
38188 +       bbot = au_sbbot(sb);
38189 +       for (bindex = 0; !err && bindex <= bbot; bindex++)
38190 +               if (!bindex || is_sb_shared(sb, bindex, bindex - 1) < 0) {
38191 +                       br = au_sbr(sb, bindex);
38192 +                       xi = br->br_xino;
38193 +                       nfile = xi->xi_nfile;
38194 +                       for (i = 0; i < nfile; i++) {
38195 +                               file = au_xino_file(xi, i);
38196 +                               if (file)
38197 +                                       err = do_xib_restore(sb, file, page);
38198 +                       }
38199 +               } else
38200 +                       AuDbg("skip shared b%d\n", bindex);
38201 +       free_page((unsigned long)page);
38202 +
38203 +out:
38204 +       return err;
38205 +}
38206 +
38207 +int au_xib_trunc(struct super_block *sb)
38208 +{
38209 +       int err;
38210 +       ssize_t sz;
38211 +       loff_t pos;
38212 +       struct au_sbinfo *sbinfo;
38213 +       unsigned long *p;
38214 +       struct file *file;
38215 +
38216 +       SiMustWriteLock(sb);
38217 +
38218 +       err = 0;
38219 +       sbinfo = au_sbi(sb);
38220 +       if (!au_opt_test(sbinfo->si_mntflags, XINO))
38221 +               goto out;
38222 +
38223 +       file = sbinfo->si_xib;
38224 +       if (vfsub_f_size_read(file) <= PAGE_SIZE)
38225 +               goto out;
38226 +
38227 +       file = au_xino_create2(sb, &sbinfo->si_xib->f_path, NULL);
38228 +       err = PTR_ERR(file);
38229 +       if (IS_ERR(file))
38230 +               goto out;
38231 +       fput(sbinfo->si_xib);
38232 +       sbinfo->si_xib = file;
38233 +
38234 +       p = sbinfo->si_xib_buf;
38235 +       memset(p, 0, PAGE_SIZE);
38236 +       pos = 0;
38237 +       sz = xino_fwrite(sbinfo->si_xib, p, PAGE_SIZE, &pos);
38238 +       if (unlikely(sz != PAGE_SIZE)) {
38239 +               err = sz;
38240 +               AuIOErr("err %d\n", err);
38241 +               if (sz >= 0)
38242 +                       err = -EIO;
38243 +               goto out;
38244 +       }
38245 +
38246 +       mutex_lock(&sbinfo->si_xib_mtx);
38247 +       /* mnt_want_write() is unnecessary here */
38248 +       err = xib_restore(sb);
38249 +       mutex_unlock(&sbinfo->si_xib_mtx);
38250 +
38251 +out:
38252 +       return err;
38253 +}
38254 +
38255 +/* ---------------------------------------------------------------------- */
38256 +
38257 +struct au_xino *au_xino_alloc(unsigned int nfile)
38258 +{
38259 +       struct au_xino *xi;
38260 +
38261 +       xi = kzalloc(sizeof(*xi), GFP_NOFS);
38262 +       if (unlikely(!xi))
38263 +               goto out;
38264 +       xi->xi_nfile = nfile;
38265 +       xi->xi_file = kcalloc(nfile, sizeof(*xi->xi_file), GFP_NOFS);
38266 +       if (unlikely(!xi->xi_file))
38267 +               goto out_free;
38268 +
38269 +       xi->xi_nondir.total = 8; /* initial size */
38270 +       xi->xi_nondir.array = kcalloc(xi->xi_nondir.total, sizeof(ino_t),
38271 +                                     GFP_NOFS);
38272 +       if (unlikely(!xi->xi_nondir.array))
38273 +               goto out_file;
38274 +
38275 +       spin_lock_init(&xi->xi_nondir.spin);
38276 +       init_waitqueue_head(&xi->xi_nondir.wqh);
38277 +       mutex_init(&xi->xi_mtx);
38278 +       INIT_HLIST_BL_HEAD(&xi->xi_writing);
38279 +       atomic_set(&xi->xi_truncating, 0);
38280 +       kref_init(&xi->xi_kref);
38281 +       goto out; /* success */
38282 +
38283 +out_file:
38284 +       au_kfree_try_rcu(xi->xi_file);
38285 +out_free:
38286 +       au_kfree_rcu(xi);
38287 +       xi = NULL;
38288 +out:
38289 +       return xi;
38290 +}
38291 +
38292 +static int au_xino_init(struct au_branch *br, int idx, struct file *file)
38293 +{
38294 +       int err;
38295 +       struct au_xino *xi;
38296 +
38297 +       err = 0;
38298 +       xi = au_xino_alloc(idx + 1);
38299 +       if (unlikely(!xi)) {
38300 +               err = -ENOMEM;
38301 +               goto out;
38302 +       }
38303 +
38304 +       if (file)
38305 +               get_file(file);
38306 +       xi->xi_file[idx] = file;
38307 +       AuDebugOn(br->br_xino);
38308 +       br->br_xino = xi;
38309 +
38310 +out:
38311 +       return err;
38312 +}
38313 +
38314 +static void au_xino_release(struct kref *kref)
38315 +{
38316 +       struct au_xino *xi;
38317 +       int i;
38318 +       unsigned long ul;
38319 +       struct hlist_bl_head *hbl;
38320 +       struct hlist_bl_node *pos, *n;
38321 +       struct au_xi_writing *p;
38322 +
38323 +       xi = container_of(kref, struct au_xino, xi_kref);
38324 +       for (i = 0; i < xi->xi_nfile; i++)
38325 +               if (xi->xi_file[i])
38326 +                       fput(xi->xi_file[i]);
38327 +       for (i = xi->xi_nondir.total - 1; i >= 0; i--)
38328 +               AuDebugOn(xi->xi_nondir.array[i]);
38329 +       mutex_destroy(&xi->xi_mtx);
38330 +       hbl = &xi->xi_writing;
38331 +       ul = au_hbl_count(hbl);
38332 +       if (unlikely(ul)) {
38333 +               pr_warn("xi_writing %lu\n", ul);
38334 +               hlist_bl_lock(hbl);
38335 +               hlist_bl_for_each_entry_safe(p, pos, n, hbl, node) {
38336 +                       hlist_bl_del(&p->node);
38337 +                       /* kmemleak reported au_kfree_rcu() doesn't free it */
38338 +                       kfree(p);
38339 +               }
38340 +               hlist_bl_unlock(hbl);
38341 +       }
38342 +       au_kfree_try_rcu(xi->xi_file);
38343 +       au_kfree_try_rcu(xi->xi_nondir.array);
38344 +       au_kfree_rcu(xi);
38345 +}
38346 +
38347 +int au_xino_put(struct au_branch *br)
38348 +{
38349 +       int ret;
38350 +       struct au_xino *xi;
38351 +
38352 +       ret = 0;
38353 +       xi = br->br_xino;
38354 +       if (xi) {
38355 +               br->br_xino = NULL;
38356 +               ret = kref_put(&xi->xi_kref, au_xino_release);
38357 +       }
38358 +
38359 +       return ret;
38360 +}
38361 +
38362 +/* ---------------------------------------------------------------------- */
38363 +
38364 +/*
38365 + * xino mount option handlers
38366 + */
38367 +
38368 +/* xino bitmap */
38369 +static void xino_clear_xib(struct super_block *sb)
38370 +{
38371 +       struct au_sbinfo *sbinfo;
38372 +
38373 +       SiMustWriteLock(sb);
38374 +
38375 +       sbinfo = au_sbi(sb);
38376 +       if (sbinfo->si_xib)
38377 +               fput(sbinfo->si_xib);
38378 +       sbinfo->si_xib = NULL;
38379 +       if (sbinfo->si_xib_buf)
38380 +               free_page((unsigned long)sbinfo->si_xib_buf);
38381 +       sbinfo->si_xib_buf = NULL;
38382 +}
38383 +
38384 +static int au_xino_set_xib(struct super_block *sb, struct path *path)
38385 +{
38386 +       int err;
38387 +       loff_t pos;
38388 +       struct au_sbinfo *sbinfo;
38389 +       struct file *file;
38390 +       struct super_block *xi_sb;
38391 +
38392 +       SiMustWriteLock(sb);
38393 +
38394 +       sbinfo = au_sbi(sb);
38395 +       file = au_xino_create2(sb, path, sbinfo->si_xib);
38396 +       err = PTR_ERR(file);
38397 +       if (IS_ERR(file))
38398 +               goto out;
38399 +       if (sbinfo->si_xib)
38400 +               fput(sbinfo->si_xib);
38401 +       sbinfo->si_xib = file;
38402 +       xi_sb = file_inode(file)->i_sb;
38403 +       sbinfo->si_ximaxent = xi_sb->s_maxbytes;
38404 +       if (unlikely(sbinfo->si_ximaxent < PAGE_SIZE)) {
38405 +               err = -EIO;
38406 +               pr_err("s_maxbytes(%llu) on %s is too small\n",
38407 +                      (u64)sbinfo->si_ximaxent, au_sbtype(xi_sb));
38408 +               goto out_unset;
38409 +       }
38410 +       sbinfo->si_ximaxent /= sizeof(ino_t);
38411 +
38412 +       err = -ENOMEM;
38413 +       if (!sbinfo->si_xib_buf)
38414 +               sbinfo->si_xib_buf = (void *)get_zeroed_page(GFP_NOFS);
38415 +       if (unlikely(!sbinfo->si_xib_buf))
38416 +               goto out_unset;
38417 +
38418 +       sbinfo->si_xib_last_pindex = 0;
38419 +       sbinfo->si_xib_next_bit = 0;
38420 +       if (vfsub_f_size_read(file) < PAGE_SIZE) {
38421 +               pos = 0;
38422 +               err = xino_fwrite(file, sbinfo->si_xib_buf, PAGE_SIZE, &pos);
38423 +               if (unlikely(err != PAGE_SIZE))
38424 +                       goto out_free;
38425 +       }
38426 +       err = 0;
38427 +       goto out; /* success */
38428 +
38429 +out_free:
38430 +       if (sbinfo->si_xib_buf)
38431 +               free_page((unsigned long)sbinfo->si_xib_buf);
38432 +       sbinfo->si_xib_buf = NULL;
38433 +       if (err >= 0)
38434 +               err = -EIO;
38435 +out_unset:
38436 +       fput(sbinfo->si_xib);
38437 +       sbinfo->si_xib = NULL;
38438 +out:
38439 +       AuTraceErr(err);
38440 +       return err;
38441 +}
38442 +
38443 +/* xino for each branch */
38444 +static void xino_clear_br(struct super_block *sb)
38445 +{
38446 +       aufs_bindex_t bindex, bbot;
38447 +       struct au_branch *br;
38448 +
38449 +       bbot = au_sbbot(sb);
38450 +       for (bindex = 0; bindex <= bbot; bindex++) {
38451 +               br = au_sbr(sb, bindex);
38452 +               AuDebugOn(!br);
38453 +               au_xino_put(br);
38454 +       }
38455 +}
38456 +
38457 +static void au_xino_set_br_shared(struct super_block *sb, struct au_branch *br,
38458 +                                 aufs_bindex_t bshared)
38459 +{
38460 +       struct au_branch *brshared;
38461 +
38462 +       brshared = au_sbr(sb, bshared);
38463 +       AuDebugOn(!brshared->br_xino);
38464 +       AuDebugOn(!brshared->br_xino->xi_file);
38465 +       if (br->br_xino != brshared->br_xino) {
38466 +               au_xino_get(brshared);
38467 +               au_xino_put(br);
38468 +               br->br_xino = brshared->br_xino;
38469 +       }
38470 +}
38471 +
38472 +struct au_xino_do_set_br {
38473 +       struct au_branch *br;
38474 +       ino_t h_ino;
38475 +       aufs_bindex_t bshared;
38476 +};
38477 +
38478 +static int au_xino_do_set_br(struct super_block *sb, struct path *path,
38479 +                            struct au_xino_do_set_br *args)
38480 +{
38481 +       int err;
38482 +       struct au_xi_calc calc;
38483 +       struct file *file;
38484 +       struct au_branch *br;
38485 +       struct au_xi_new xinew = {
38486 +               .base = path
38487 +       };
38488 +
38489 +       br = args->br;
38490 +       xinew.xi = br->br_xino;
38491 +       au_xi_calc(sb, args->h_ino, &calc);
38492 +       xinew.copy_src = au_xino_file(xinew.xi, calc.idx);
38493 +       if (args->bshared >= 0)
38494 +               /* shared xino */
38495 +               au_xino_set_br_shared(sb, br, args->bshared);
38496 +       else if (!xinew.xi) {
38497 +               /* new xino */
38498 +               err = au_xino_init(br, calc.idx, xinew.copy_src);
38499 +               if (unlikely(err))
38500 +                       goto out;
38501 +       }
38502 +
38503 +       /* force re-creating */
38504 +       xinew.xi = br->br_xino;
38505 +       xinew.idx = calc.idx;
38506 +       mutex_lock(&xinew.xi->xi_mtx);
38507 +       file = au_xi_new(sb, &xinew);
38508 +       mutex_unlock(&xinew.xi->xi_mtx);
38509 +       err = PTR_ERR(file);
38510 +       if (IS_ERR(file))
38511 +               goto out;
38512 +       AuDebugOn(!file);
38513 +
38514 +       err = au_xino_do_write(file, &calc, AUFS_ROOT_INO);
38515 +       if (unlikely(err))
38516 +               au_xino_put(br);
38517 +
38518 +out:
38519 +       AuTraceErr(err);
38520 +       return err;
38521 +}
38522 +
38523 +static int au_xino_set_br(struct super_block *sb, struct path *path)
38524 +{
38525 +       int err;
38526 +       aufs_bindex_t bindex, bbot;
38527 +       struct au_xino_do_set_br args;
38528 +       struct inode *inode;
38529 +
38530 +       SiMustWriteLock(sb);
38531 +
38532 +       bbot = au_sbbot(sb);
38533 +       inode = d_inode(sb->s_root);
38534 +       for (bindex = 0; bindex <= bbot; bindex++) {
38535 +               args.h_ino = au_h_iptr(inode, bindex)->i_ino;
38536 +               args.br = au_sbr(sb, bindex);
38537 +               args.bshared = is_sb_shared(sb, bindex, bindex - 1);
38538 +               err = au_xino_do_set_br(sb, path, &args);
38539 +               if (unlikely(err))
38540 +                       break;
38541 +       }
38542 +
38543 +       AuTraceErr(err);
38544 +       return err;
38545 +}
38546 +
38547 +void au_xino_clr(struct super_block *sb)
38548 +{
38549 +       struct au_sbinfo *sbinfo;
38550 +
38551 +       au_xigen_clr(sb);
38552 +       xino_clear_xib(sb);
38553 +       xino_clear_br(sb);
38554 +       dbgaufs_brs_del(sb, 0);
38555 +       sbinfo = au_sbi(sb);
38556 +       /* lvalue, do not call au_mntflags() */
38557 +       au_opt_clr(sbinfo->si_mntflags, XINO);
38558 +}
38559 +
38560 +int au_xino_set(struct super_block *sb, struct au_opt_xino *xiopt, int remount)
38561 +{
38562 +       int err, skip;
38563 +       struct dentry *dentry, *parent, *cur_dentry, *cur_parent;
38564 +       struct qstr *dname, *cur_name;
38565 +       struct file *cur_xino;
38566 +       struct au_sbinfo *sbinfo;
38567 +       struct path *path, *cur_path;
38568 +
38569 +       SiMustWriteLock(sb);
38570 +
38571 +       err = 0;
38572 +       sbinfo = au_sbi(sb);
38573 +       path = &xiopt->file->f_path;
38574 +       dentry = path->dentry;
38575 +       parent = dget_parent(dentry);
38576 +       if (remount) {
38577 +               skip = 0;
38578 +               cur_xino = sbinfo->si_xib;
38579 +               if (cur_xino) {
38580 +                       cur_path = &cur_xino->f_path;
38581 +                       cur_dentry = cur_path->dentry;
38582 +                       cur_parent = dget_parent(cur_dentry);
38583 +                       cur_name = &cur_dentry->d_name;
38584 +                       dname = &dentry->d_name;
38585 +                       skip = (cur_parent == parent
38586 +                               && au_qstreq(dname, cur_name));
38587 +                       dput(cur_parent);
38588 +               }
38589 +               if (skip)
38590 +                       goto out;
38591 +       }
38592 +
38593 +       au_opt_set(sbinfo->si_mntflags, XINO);
38594 +       err = au_xino_set_xib(sb, path);
38595 +       /* si_x{read,write} are set */
38596 +       if (!err)
38597 +               err = au_xigen_set(sb, path);
38598 +       if (!err)
38599 +               err = au_xino_set_br(sb, path);
38600 +       if (!err) {
38601 +               dbgaufs_brs_add(sb, 0, /*topdown*/1);
38602 +               goto out; /* success */
38603 +       }
38604 +
38605 +       /* reset all */
38606 +       AuIOErr("failed setting xino(%d).\n", err);
38607 +       au_xino_clr(sb);
38608 +
38609 +out:
38610 +       dput(parent);
38611 +       return err;
38612 +}
38613 +
38614 +/*
38615 + * create a xinofile at the default place/path.
38616 + */
38617 +struct file *au_xino_def(struct super_block *sb)
38618 +{
38619 +       struct file *file;
38620 +       char *page, *p;
38621 +       struct au_branch *br;
38622 +       struct super_block *h_sb;
38623 +       struct path path;
38624 +       aufs_bindex_t bbot, bindex, bwr;
38625 +
38626 +       br = NULL;
38627 +       bbot = au_sbbot(sb);
38628 +       bwr = -1;
38629 +       for (bindex = 0; bindex <= bbot; bindex++) {
38630 +               br = au_sbr(sb, bindex);
38631 +               if (au_br_writable(br->br_perm)
38632 +                   && !au_test_fs_bad_xino(au_br_sb(br))) {
38633 +                       bwr = bindex;
38634 +                       break;
38635 +               }
38636 +       }
38637 +
38638 +       if (bwr >= 0) {
38639 +               file = ERR_PTR(-ENOMEM);
38640 +               page = (void *)__get_free_page(GFP_NOFS);
38641 +               if (unlikely(!page))
38642 +                       goto out;
38643 +               path.mnt = au_br_mnt(br);
38644 +               path.dentry = au_h_dptr(sb->s_root, bwr);
38645 +               p = d_path(&path, page, PATH_MAX - sizeof(AUFS_XINO_FNAME));
38646 +               file = (void *)p;
38647 +               if (!IS_ERR(p)) {
38648 +                       strcat(p, "/" AUFS_XINO_FNAME);
38649 +                       AuDbg("%s\n", p);
38650 +                       file = au_xino_create(sb, p, /*silent*/0, /*wbrtop*/1);
38651 +               }
38652 +               free_page((unsigned long)page);
38653 +       } else {
38654 +               file = au_xino_create(sb, AUFS_XINO_DEFPATH, /*silent*/0,
38655 +                                     /*wbrtop*/0);
38656 +               if (IS_ERR(file))
38657 +                       goto out;
38658 +               h_sb = file->f_path.dentry->d_sb;
38659 +               if (unlikely(au_test_fs_bad_xino(h_sb))) {
38660 +                       pr_err("xino doesn't support %s(%s)\n",
38661 +                              AUFS_XINO_DEFPATH, au_sbtype(h_sb));
38662 +                       fput(file);
38663 +                       file = ERR_PTR(-EINVAL);
38664 +               }
38665 +       }
38666 +
38667 +out:
38668 +       return file;
38669 +}
38670 +
38671 +/* ---------------------------------------------------------------------- */
38672 +
38673 +/*
38674 + * initialize the xinofile for the specified branch @br
38675 + * at the place/path where @base_file indicates.
38676 + * test whether another branch is on the same filesystem or not,
38677 + * if found then share the xinofile with another branch.
38678 + */
38679 +int au_xino_init_br(struct super_block *sb, struct au_branch *br, ino_t h_ino,
38680 +                   struct path *base)
38681 +{
38682 +       int err;
38683 +       struct au_xino_do_set_br args = {
38684 +               .h_ino  = h_ino,
38685 +               .br     = br
38686 +       };
38687 +
38688 +       args.bshared = sbr_find_shared(sb, /*btop*/0, au_sbbot(sb),
38689 +                                      au_br_sb(br));
38690 +       err = au_xino_do_set_br(sb, base, &args);
38691 +       if (unlikely(err))
38692 +               au_xino_put(br);
38693 +
38694 +       return err;
38695 +}
38696 +
38697 +/* ---------------------------------------------------------------------- */
38698 +
38699 +/*
38700 + * get an unused inode number from bitmap
38701 + */
38702 +ino_t au_xino_new_ino(struct super_block *sb)
38703 +{
38704 +       ino_t ino;
38705 +       unsigned long *p, pindex, ul, pend;
38706 +       struct au_sbinfo *sbinfo;
38707 +       struct file *file;
38708 +       int free_bit, err;
38709 +
38710 +       if (!au_opt_test(au_mntflags(sb), XINO))
38711 +               return iunique(sb, AUFS_FIRST_INO);
38712 +
38713 +       sbinfo = au_sbi(sb);
38714 +       mutex_lock(&sbinfo->si_xib_mtx);
38715 +       p = sbinfo->si_xib_buf;
38716 +       free_bit = sbinfo->si_xib_next_bit;
38717 +       if (free_bit < page_bits && !test_bit(free_bit, p))
38718 +               goto out; /* success */
38719 +       free_bit = find_first_zero_bit(p, page_bits);
38720 +       if (free_bit < page_bits)
38721 +               goto out; /* success */
38722 +
38723 +       pindex = sbinfo->si_xib_last_pindex;
38724 +       for (ul = pindex - 1; ul < ULONG_MAX; ul--) {
38725 +               err = xib_pindex(sb, ul);
38726 +               if (unlikely(err))
38727 +                       goto out_err;
38728 +               free_bit = find_first_zero_bit(p, page_bits);
38729 +               if (free_bit < page_bits)
38730 +                       goto out; /* success */
38731 +       }
38732 +
38733 +       file = sbinfo->si_xib;
38734 +       pend = vfsub_f_size_read(file) / PAGE_SIZE;
38735 +       for (ul = pindex + 1; ul <= pend; ul++) {
38736 +               err = xib_pindex(sb, ul);
38737 +               if (unlikely(err))
38738 +                       goto out_err;
38739 +               free_bit = find_first_zero_bit(p, page_bits);
38740 +               if (free_bit < page_bits)
38741 +                       goto out; /* success */
38742 +       }
38743 +       BUG();
38744 +
38745 +out:
38746 +       set_bit(free_bit, p);
38747 +       sbinfo->si_xib_next_bit = free_bit + 1;
38748 +       pindex = sbinfo->si_xib_last_pindex;
38749 +       mutex_unlock(&sbinfo->si_xib_mtx);
38750 +       ino = xib_calc_ino(pindex, free_bit);
38751 +       AuDbg("i%lu\n", (unsigned long)ino);
38752 +       return ino;
38753 +out_err:
38754 +       mutex_unlock(&sbinfo->si_xib_mtx);
38755 +       AuDbg("i0\n");
38756 +       return 0;
38757 +}
38758 +
38759 +/* for s_op->delete_inode() */
38760 +void au_xino_delete_inode(struct inode *inode, const int unlinked)
38761 +{
38762 +       int err;
38763 +       unsigned int mnt_flags;
38764 +       aufs_bindex_t bindex, bbot, bi;
38765 +       unsigned char try_trunc;
38766 +       struct au_iinfo *iinfo;
38767 +       struct super_block *sb;
38768 +       struct au_hinode *hi;
38769 +       struct inode *h_inode;
38770 +       struct au_branch *br;
38771 +       struct au_xi_calc calc;
38772 +       struct file *file;
38773 +
38774 +       AuDebugOn(au_is_bad_inode(inode));
38775 +
38776 +       sb = inode->i_sb;
38777 +       mnt_flags = au_mntflags(sb);
38778 +       if (!au_opt_test(mnt_flags, XINO)
38779 +           || inode->i_ino == AUFS_ROOT_INO)
38780 +               return;
38781 +
38782 +       if (unlinked) {
38783 +               au_xigen_inc(inode);
38784 +               au_xib_clear_bit(inode);
38785 +       }
38786 +
38787 +       iinfo = au_ii(inode);
38788 +       bindex = iinfo->ii_btop;
38789 +       if (bindex < 0)
38790 +               return;
38791 +
38792 +       try_trunc = !!au_opt_test(mnt_flags, TRUNC_XINO);
38793 +       hi = au_hinode(iinfo, bindex);
38794 +       bbot = iinfo->ii_bbot;
38795 +       for (; bindex <= bbot; bindex++, hi++) {
38796 +               h_inode = hi->hi_inode;
38797 +               if (!h_inode
38798 +                   || (!unlinked && h_inode->i_nlink))
38799 +                       continue;
38800 +
38801 +               /* inode may not be revalidated */
38802 +               bi = au_br_index(sb, hi->hi_id);
38803 +               if (bi < 0)
38804 +                       continue;
38805 +
38806 +               br = au_sbr(sb, bi);
38807 +               au_xi_calc(sb, h_inode->i_ino, &calc);
38808 +               file = au_xino_file(br->br_xino, calc.idx);
38809 +               if (IS_ERR_OR_NULL(file))
38810 +                       continue;
38811 +
38812 +               err = au_xino_do_write(file, &calc, /*ino*/0);
38813 +               if (!err && try_trunc
38814 +                   && au_test_fs_trunc_xino(au_br_sb(br)))
38815 +                       xino_try_trunc(sb, br);
38816 +       }
38817 +}
38818 +
38819 +/* ---------------------------------------------------------------------- */
38820 +
38821 +static int au_xinondir_find(struct au_xino *xi, ino_t h_ino)
38822 +{
38823 +       int found, total, i;
38824 +
38825 +       found = -1;
38826 +       total = xi->xi_nondir.total;
38827 +       for (i = 0; i < total; i++) {
38828 +               if (xi->xi_nondir.array[i] != h_ino)
38829 +                       continue;
38830 +               found = i;
38831 +               break;
38832 +       }
38833 +
38834 +       return found;
38835 +}
38836 +
38837 +static int au_xinondir_expand(struct au_xino *xi)
38838 +{
38839 +       int err, sz;
38840 +       ino_t *p;
38841 +
38842 +       BUILD_BUG_ON(KMALLOC_MAX_SIZE > INT_MAX);
38843 +
38844 +       err = -ENOMEM;
38845 +       sz = xi->xi_nondir.total * sizeof(ino_t);
38846 +       if (unlikely(sz > KMALLOC_MAX_SIZE / 2))
38847 +               goto out;
38848 +       p = au_kzrealloc(xi->xi_nondir.array, sz, sz << 1, GFP_ATOMIC,
38849 +                        /*may_shrink*/0);
38850 +       if (p) {
38851 +               xi->xi_nondir.array = p;
38852 +               xi->xi_nondir.total <<= 1;
38853 +               AuDbg("xi_nondir.total %d\n", xi->xi_nondir.total);
38854 +               err = 0;
38855 +       }
38856 +
38857 +out:
38858 +       return err;
38859 +}
38860 +
38861 +void au_xinondir_leave(struct super_block *sb, aufs_bindex_t bindex,
38862 +                      ino_t h_ino, int idx)
38863 +{
38864 +       struct au_xino *xi;
38865 +
38866 +       AuDebugOn(!au_opt_test(au_mntflags(sb), XINO));
38867 +       xi = au_sbr(sb, bindex)->br_xino;
38868 +       AuDebugOn(idx < 0 || xi->xi_nondir.total <= idx);
38869 +
38870 +       spin_lock(&xi->xi_nondir.spin);
38871 +       AuDebugOn(xi->xi_nondir.array[idx] != h_ino);
38872 +       xi->xi_nondir.array[idx] = 0;
38873 +       spin_unlock(&xi->xi_nondir.spin);
38874 +       wake_up_all(&xi->xi_nondir.wqh);
38875 +}
38876 +
38877 +int au_xinondir_enter(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
38878 +                     int *idx)
38879 +{
38880 +       int err, found, empty;
38881 +       struct au_xino *xi;
38882 +
38883 +       err = 0;
38884 +       *idx = -1;
38885 +       if (!au_opt_test(au_mntflags(sb), XINO))
38886 +               goto out; /* no xino */
38887 +
38888 +       xi = au_sbr(sb, bindex)->br_xino;
38889 +
38890 +again:
38891 +       spin_lock(&xi->xi_nondir.spin);
38892 +       found = au_xinondir_find(xi, h_ino);
38893 +       if (found == -1) {
38894 +               empty = au_xinondir_find(xi, /*h_ino*/0);
38895 +               if (empty == -1) {
38896 +                       empty = xi->xi_nondir.total;
38897 +                       err = au_xinondir_expand(xi);
38898 +                       if (unlikely(err))
38899 +                               goto out_unlock;
38900 +               }
38901 +               xi->xi_nondir.array[empty] = h_ino;
38902 +               *idx = empty;
38903 +       } else {
38904 +               spin_unlock(&xi->xi_nondir.spin);
38905 +               wait_event(xi->xi_nondir.wqh,
38906 +                          xi->xi_nondir.array[found] != h_ino);
38907 +               goto again;
38908 +       }
38909 +
38910 +out_unlock:
38911 +       spin_unlock(&xi->xi_nondir.spin);
38912 +out:
38913 +       return err;
38914 +}
38915 +
38916 +/* ---------------------------------------------------------------------- */
38917 +
38918 +int au_xino_path(struct seq_file *seq, struct file *file)
38919 +{
38920 +       int err;
38921 +
38922 +       err = au_seq_path(seq, &file->f_path);
38923 +       if (unlikely(err))
38924 +               goto out;
38925 +
38926 +#define Deleted "\\040(deleted)"
38927 +       seq->count -= sizeof(Deleted) - 1;
38928 +       AuDebugOn(memcmp(seq->buf + seq->count, Deleted,
38929 +                        sizeof(Deleted) - 1));
38930 +#undef Deleted
38931 +
38932 +out:
38933 +       return err;
38934 +}
38935 diff -urN /usr/share/empty/include/uapi/linux/aufs_type.h linux/include/uapi/linux/aufs_type.h
38936 --- /usr/share/empty/include/uapi/linux/aufs_type.h     1970-01-01 01:00:00.000000000 +0100
38937 +++ linux/include/uapi/linux/aufs_type.h        2022-03-21 14:49:05.733299677 +0100
38938 @@ -0,0 +1,452 @@
38939 +/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
38940 +/*
38941 + * Copyright (C) 2005-2021 Junjiro R. Okajima
38942 + *
38943 + * This program, aufs is free software; you can redistribute it and/or modify
38944 + * it under the terms of the GNU General Public License as published by
38945 + * the Free Software Foundation; either version 2 of the License, or
38946 + * (at your option) any later version.
38947 + *
38948 + * This program is distributed in the hope that it will be useful,
38949 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
38950 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
38951 + * GNU General Public License for more details.
38952 + *
38953 + * You should have received a copy of the GNU General Public License
38954 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
38955 + */
38956 +
38957 +#ifndef __AUFS_TYPE_H__
38958 +#define __AUFS_TYPE_H__
38959 +
38960 +#define AUFS_NAME      "aufs"
38961 +
38962 +#ifdef __KERNEL__
38963 +/*
38964 + * define it before including all other headers.
38965 + * sched.h may use pr_* macros before defining "current", so define the
38966 + * no-current version first, and re-define later.
38967 + */
38968 +#define pr_fmt(fmt)    AUFS_NAME " %s:%d: " fmt, __func__, __LINE__
38969 +#include <linux/sched.h>
38970 +#undef pr_fmt
38971 +#define pr_fmt(fmt) \
38972 +               AUFS_NAME " %s:%d:%.*s[%d]: " fmt, __func__, __LINE__, \
38973 +               (int)sizeof(current->comm), current->comm, current->pid
38974 +#include <linux/limits.h>
38975 +#else
38976 +#include <stdint.h>
38977 +#include <sys/types.h>
38978 +#include <limits.h>
38979 +#endif /* __KERNEL__ */
38980 +
38981 +#define AUFS_VERSION   "5.x-rcN-20220314"
38982 +
38983 +/* todo? move this to linux-2.6.19/include/magic.h */
38984 +#define AUFS_SUPER_MAGIC       ('a' << 24 | 'u' << 16 | 'f' << 8 | 's')
38985 +
38986 +/* ---------------------------------------------------------------------- */
38987 +
38988 +#ifdef __KERNEL__
38989 +#ifdef CONFIG_AUFS_BRANCH_MAX_127
38990 +typedef int8_t aufs_bindex_t;
38991 +#define AUFS_BRANCH_MAX 127
38992 +#else
38993 +typedef int16_t aufs_bindex_t;
38994 +#ifdef CONFIG_AUFS_BRANCH_MAX_511
38995 +#define AUFS_BRANCH_MAX 511
38996 +#elif defined(CONFIG_AUFS_BRANCH_MAX_1023)
38997 +#define AUFS_BRANCH_MAX 1023
38998 +#elif defined(CONFIG_AUFS_BRANCH_MAX_32767)
38999 +#define AUFS_BRANCH_MAX 32767
39000 +#endif
39001 +#endif
39002 +
39003 +#ifndef AUFS_BRANCH_MAX
39004 +#error unknown CONFIG_AUFS_BRANCH_MAX value
39005 +#endif
39006 +#endif /* __KERNEL__ */
39007 +
39008 +/* ---------------------------------------------------------------------- */
39009 +
39010 +#define AUFS_FSTYPE            AUFS_NAME
39011 +
39012 +#define AUFS_ROOT_INO          2
39013 +#define AUFS_FIRST_INO         11
39014 +
39015 +#define AUFS_WH_PFX            ".wh."
39016 +#define AUFS_WH_PFX_LEN                ((int)sizeof(AUFS_WH_PFX) - 1)
39017 +#define AUFS_WH_TMP_LEN                4
39018 +/* a limit for rmdir/rename a dir and copyup */
39019 +#define AUFS_MAX_NAMELEN       (NAME_MAX \
39020 +                               - AUFS_WH_PFX_LEN * 2   /* doubly whiteouted */\
39021 +                               - 1                     /* dot */\
39022 +                               - AUFS_WH_TMP_LEN)      /* hex */
39023 +#define AUFS_XINO_FNAME                "." AUFS_NAME ".xino"
39024 +#define AUFS_XINO_DEFPATH      "/tmp/" AUFS_XINO_FNAME
39025 +#define AUFS_XINO_DEF_SEC      30 /* seconds */
39026 +#define AUFS_XINO_DEF_TRUNC    45 /* percentage */
39027 +#define AUFS_DIRWH_DEF         3
39028 +#define AUFS_RDCACHE_DEF       10 /* seconds */
39029 +#define AUFS_RDCACHE_MAX       3600 /* seconds */
39030 +#define AUFS_RDBLK_DEF         512 /* bytes */
39031 +#define AUFS_RDHASH_DEF                32
39032 +#define AUFS_WKQ_NAME          AUFS_NAME "d"
39033 +#define AUFS_MFS_DEF_SEC       30 /* seconds */
39034 +#define AUFS_MFS_MAX_SEC       3600 /* seconds */
39035 +#define AUFS_FHSM_CACHE_DEF_SEC        30 /* seconds */
39036 +#define AUFS_PLINK_WARN                50 /* number of plinks in a single bucket */
39037 +
39038 +/* pseudo-link maintenace under /proc */
39039 +#define AUFS_PLINK_MAINT_NAME  "plink_maint"
39040 +#define AUFS_PLINK_MAINT_DIR   "fs/" AUFS_NAME
39041 +#define AUFS_PLINK_MAINT_PATH  AUFS_PLINK_MAINT_DIR "/" AUFS_PLINK_MAINT_NAME
39042 +
39043 +/* dirren, renamed dir */
39044 +#define AUFS_DR_INFO_PFX       AUFS_WH_PFX ".dr."
39045 +#define AUFS_DR_BRHINO_NAME    AUFS_WH_PFX "hino"
39046 +/* whiteouted doubly */
39047 +#define AUFS_WH_DR_INFO_PFX    AUFS_WH_PFX AUFS_DR_INFO_PFX
39048 +#define AUFS_WH_DR_BRHINO      AUFS_WH_PFX AUFS_DR_BRHINO_NAME
39049 +
39050 +#define AUFS_DIROPQ_NAME       AUFS_WH_PFX ".opq" /* whiteouted doubly */
39051 +#define AUFS_WH_DIROPQ         AUFS_WH_PFX AUFS_DIROPQ_NAME
39052 +
39053 +#define AUFS_BASE_NAME         AUFS_WH_PFX AUFS_NAME
39054 +#define AUFS_PLINKDIR_NAME     AUFS_WH_PFX "plnk"
39055 +#define AUFS_ORPHDIR_NAME      AUFS_WH_PFX "orph"
39056 +
39057 +/* doubly whiteouted */
39058 +#define AUFS_WH_BASE           AUFS_WH_PFX AUFS_BASE_NAME
39059 +#define AUFS_WH_PLINKDIR       AUFS_WH_PFX AUFS_PLINKDIR_NAME
39060 +#define AUFS_WH_ORPHDIR                AUFS_WH_PFX AUFS_ORPHDIR_NAME
39061 +
39062 +/* branch permissions and attributes */
39063 +#define AUFS_BRPERM_RW         "rw"
39064 +#define AUFS_BRPERM_RO         "ro"
39065 +#define AUFS_BRPERM_RR         "rr"
39066 +#define AUFS_BRATTR_COO_REG    "coo_reg"
39067 +#define AUFS_BRATTR_COO_ALL    "coo_all"
39068 +#define AUFS_BRATTR_FHSM       "fhsm"
39069 +#define AUFS_BRATTR_UNPIN      "unpin"
39070 +#define AUFS_BRATTR_ICEX       "icex"
39071 +#define AUFS_BRATTR_ICEX_SEC   "icexsec"
39072 +#define AUFS_BRATTR_ICEX_SYS   "icexsys"
39073 +#define AUFS_BRATTR_ICEX_TR    "icextr"
39074 +#define AUFS_BRATTR_ICEX_USR   "icexusr"
39075 +#define AUFS_BRATTR_ICEX_OTH   "icexoth"
39076 +#define AUFS_BRRATTR_WH                "wh"
39077 +#define AUFS_BRWATTR_NLWH      "nolwh"
39078 +#define AUFS_BRWATTR_MOO       "moo"
39079 +
39080 +#define AuBrPerm_RW            1               /* writable, hardlinkable wh */
39081 +#define AuBrPerm_RO            (1 << 1)        /* readonly */
39082 +#define AuBrPerm_RR            (1 << 2)        /* natively readonly */
39083 +#define AuBrPerm_Mask          (AuBrPerm_RW | AuBrPerm_RO | AuBrPerm_RR)
39084 +
39085 +#define AuBrAttr_COO_REG       (1 << 3)        /* copy-up on open */
39086 +#define AuBrAttr_COO_ALL       (1 << 4)
39087 +#define AuBrAttr_COO_Mask      (AuBrAttr_COO_REG | AuBrAttr_COO_ALL)
39088 +
39089 +#define AuBrAttr_FHSM          (1 << 5)        /* file-based hsm */
39090 +#define AuBrAttr_UNPIN         (1 << 6)        /* rename-able top dir of
39091 +                                                  branch. meaningless since
39092 +                                                  linux-3.18-rc1 */
39093 +
39094 +/* ignore error in copying XATTR */
39095 +#define AuBrAttr_ICEX_SEC      (1 << 7)
39096 +#define AuBrAttr_ICEX_SYS      (1 << 8)
39097 +#define AuBrAttr_ICEX_TR       (1 << 9)
39098 +#define AuBrAttr_ICEX_USR      (1 << 10)
39099 +#define AuBrAttr_ICEX_OTH      (1 << 11)
39100 +#define AuBrAttr_ICEX          (AuBrAttr_ICEX_SEC      \
39101 +                                | AuBrAttr_ICEX_SYS    \
39102 +                                | AuBrAttr_ICEX_TR     \
39103 +                                | AuBrAttr_ICEX_USR    \
39104 +                                | AuBrAttr_ICEX_OTH)
39105 +
39106 +#define AuBrRAttr_WH           (1 << 12)       /* whiteout-able */
39107 +#define AuBrRAttr_Mask         AuBrRAttr_WH
39108 +
39109 +#define AuBrWAttr_NoLinkWH     (1 << 13)       /* un-hardlinkable whiteouts */
39110 +#define AuBrWAttr_MOO          (1 << 14)       /* move-up on open */
39111 +#define AuBrWAttr_Mask         (AuBrWAttr_NoLinkWH | AuBrWAttr_MOO)
39112 +
39113 +#define AuBrAttr_CMOO_Mask     (AuBrAttr_COO_Mask | AuBrWAttr_MOO)
39114 +
39115 +/* #warning test userspace */
39116 +#ifdef __KERNEL__
39117 +#ifndef CONFIG_AUFS_FHSM
39118 +#undef AuBrAttr_FHSM
39119 +#define AuBrAttr_FHSM          0
39120 +#endif
39121 +#ifndef CONFIG_AUFS_XATTR
39122 +#undef AuBrAttr_ICEX
39123 +#define AuBrAttr_ICEX          0
39124 +#undef AuBrAttr_ICEX_SEC
39125 +#define AuBrAttr_ICEX_SEC      0
39126 +#undef AuBrAttr_ICEX_SYS
39127 +#define AuBrAttr_ICEX_SYS      0
39128 +#undef AuBrAttr_ICEX_TR
39129 +#define AuBrAttr_ICEX_TR       0
39130 +#undef AuBrAttr_ICEX_USR
39131 +#define AuBrAttr_ICEX_USR      0
39132 +#undef AuBrAttr_ICEX_OTH
39133 +#define AuBrAttr_ICEX_OTH      0
39134 +#endif
39135 +#endif
39136 +
39137 +/* the longest combination */
39138 +/* AUFS_BRATTR_ICEX and AUFS_BRATTR_ICEX_TR don't affect here */
39139 +#define AuBrPermStrSz  sizeof(AUFS_BRPERM_RW                   \
39140 +                              "+" AUFS_BRATTR_COO_REG          \
39141 +                              "+" AUFS_BRATTR_FHSM             \
39142 +                              "+" AUFS_BRATTR_UNPIN            \
39143 +                              "+" AUFS_BRATTR_ICEX_SEC         \
39144 +                              "+" AUFS_BRATTR_ICEX_SYS         \
39145 +                              "+" AUFS_BRATTR_ICEX_USR         \
39146 +                              "+" AUFS_BRATTR_ICEX_OTH         \
39147 +                              "+" AUFS_BRWATTR_NLWH)
39148 +
39149 +typedef struct {
39150 +       char a[AuBrPermStrSz];
39151 +} au_br_perm_str_t;
39152 +
39153 +static inline int au_br_writable(int brperm)
39154 +{
39155 +       return brperm & AuBrPerm_RW;
39156 +}
39157 +
39158 +static inline int au_br_whable(int brperm)
39159 +{
39160 +       return brperm & (AuBrPerm_RW | AuBrRAttr_WH);
39161 +}
39162 +
39163 +static inline int au_br_wh_linkable(int brperm)
39164 +{
39165 +       return !(brperm & AuBrWAttr_NoLinkWH);
39166 +}
39167 +
39168 +static inline int au_br_cmoo(int brperm)
39169 +{
39170 +       return brperm & AuBrAttr_CMOO_Mask;
39171 +}
39172 +
39173 +static inline int au_br_fhsm(int brperm)
39174 +{
39175 +       return brperm & AuBrAttr_FHSM;
39176 +}
39177 +
39178 +/* ---------------------------------------------------------------------- */
39179 +
39180 +/* ioctl */
39181 +enum {
39182 +       /* readdir in userspace */
39183 +       AuCtl_RDU,
39184 +       AuCtl_RDU_INO,
39185 +
39186 +       AuCtl_WBR_FD,   /* pathconf wrapper */
39187 +       AuCtl_IBUSY,    /* busy inode */
39188 +       AuCtl_MVDOWN,   /* move-down */
39189 +       AuCtl_BR,       /* info about branches */
39190 +       AuCtl_FHSM_FD   /* connection for fhsm */
39191 +};
39192 +
39193 +/* borrowed from linux/include/linux/kernel.h */
39194 +#ifndef ALIGN
39195 +#ifdef _GNU_SOURCE
39196 +#define ALIGN(x, a)            __ALIGN_MASK(x, (typeof(x))(a)-1)
39197 +#else
39198 +#define ALIGN(x, a)            (((x) + (a) - 1) & ~((a) - 1))
39199 +#endif
39200 +#define __ALIGN_MASK(x, mask)  (((x)+(mask))&~(mask))
39201 +#endif
39202 +
39203 +/* borrowed from linux/include/linux/compiler-gcc3.h */
39204 +#ifndef __aligned
39205 +#define __aligned(x)                   __attribute__((aligned(x)))
39206 +#endif
39207 +
39208 +#ifdef __KERNEL__
39209 +#ifndef __packed
39210 +#define __packed                       __attribute__((packed))
39211 +#endif
39212 +#endif
39213 +
39214 +struct au_rdu_cookie {
39215 +       uint64_t        h_pos;
39216 +       int16_t         bindex;
39217 +       uint8_t         flags;
39218 +       uint8_t         pad;
39219 +       uint32_t        generation;
39220 +} __aligned(8);
39221 +
39222 +struct au_rdu_ent {
39223 +       uint64_t        ino;
39224 +       int16_t         bindex;
39225 +       uint8_t         type;
39226 +       uint8_t         nlen;
39227 +       uint8_t         wh;
39228 +       char            name[];
39229 +} __aligned(8);
39230 +
39231 +static inline int au_rdu_len(int nlen)
39232 +{
39233 +       /* include the terminating NULL */
39234 +       return ALIGN(sizeof(struct au_rdu_ent) + nlen + 1,
39235 +                    sizeof(uint64_t));
39236 +}
39237 +
39238 +union au_rdu_ent_ul {
39239 +       struct au_rdu_ent __user        *e;
39240 +       uint64_t                        ul;
39241 +};
39242 +
39243 +enum {
39244 +       AufsCtlRduV_SZ,
39245 +       AufsCtlRduV_End
39246 +};
39247 +
39248 +struct aufs_rdu {
39249 +       /* input */
39250 +       union {
39251 +               uint64_t        sz;     /* AuCtl_RDU */
39252 +               uint64_t        nent;   /* AuCtl_RDU_INO */
39253 +       };
39254 +       union au_rdu_ent_ul     ent;
39255 +       uint16_t                verify[AufsCtlRduV_End];
39256 +
39257 +       /* input/output */
39258 +       uint32_t                blk;
39259 +
39260 +       /* output */
39261 +       union au_rdu_ent_ul     tail;
39262 +       /* number of entries which were added in a single call */
39263 +       uint64_t                rent;
39264 +       uint8_t                 full;
39265 +       uint8_t                 shwh;
39266 +
39267 +       struct au_rdu_cookie    cookie;
39268 +} __aligned(8);
39269 +
39270 +/* ---------------------------------------------------------------------- */
39271 +
39272 +/* dirren. the branch is identified by the filename who contains this */
39273 +struct au_drinfo {
39274 +       uint64_t ino;
39275 +       union {
39276 +               uint8_t oldnamelen;
39277 +               uint64_t _padding;
39278 +       };
39279 +       uint8_t oldname[];
39280 +} __aligned(8);
39281 +
39282 +struct au_drinfo_fdata {
39283 +       uint32_t magic;
39284 +       struct au_drinfo drinfo;
39285 +} __aligned(8);
39286 +
39287 +#define AUFS_DRINFO_MAGIC_V1   ('a' << 24 | 'd' << 16 | 'r' << 8 | 0x01)
39288 +/* future */
39289 +#define AUFS_DRINFO_MAGIC_V2   ('a' << 24 | 'd' << 16 | 'r' << 8 | 0x02)
39290 +
39291 +/* ---------------------------------------------------------------------- */
39292 +
39293 +struct aufs_wbr_fd {
39294 +       uint32_t        oflags;
39295 +       int16_t         brid;
39296 +} __aligned(8);
39297 +
39298 +/* ---------------------------------------------------------------------- */
39299 +
39300 +struct aufs_ibusy {
39301 +       uint64_t        ino, h_ino;
39302 +       int16_t         bindex;
39303 +} __aligned(8);
39304 +
39305 +/* ---------------------------------------------------------------------- */
39306 +
39307 +/* error code for move-down */
39308 +/* the actual message strings are implemented in aufs-util.git */
39309 +enum {
39310 +       EAU_MVDOWN_OPAQUE = 1,
39311 +       EAU_MVDOWN_WHITEOUT,
39312 +       EAU_MVDOWN_UPPER,
39313 +       EAU_MVDOWN_BOTTOM,
39314 +       EAU_MVDOWN_NOUPPER,
39315 +       EAU_MVDOWN_NOLOWERBR,
39316 +       EAU_Last
39317 +};
39318 +
39319 +/* flags for move-down */
39320 +#define AUFS_MVDOWN_DMSG       1
39321 +#define AUFS_MVDOWN_OWLOWER    (1 << 1)        /* overwrite lower */
39322 +#define AUFS_MVDOWN_KUPPER     (1 << 2)        /* keep upper */
39323 +#define AUFS_MVDOWN_ROLOWER    (1 << 3)        /* do even if lower is RO */
39324 +#define AUFS_MVDOWN_ROLOWER_R  (1 << 4)        /* did on lower RO */
39325 +#define AUFS_MVDOWN_ROUPPER    (1 << 5)        /* do even if upper is RO */
39326 +#define AUFS_MVDOWN_ROUPPER_R  (1 << 6)        /* did on upper RO */
39327 +#define AUFS_MVDOWN_BRID_UPPER (1 << 7)        /* upper brid */
39328 +#define AUFS_MVDOWN_BRID_LOWER (1 << 8)        /* lower brid */
39329 +#define AUFS_MVDOWN_FHSM_LOWER (1 << 9)        /* find fhsm attr for lower */
39330 +#define AUFS_MVDOWN_STFS       (1 << 10)       /* req. stfs */
39331 +#define AUFS_MVDOWN_STFS_FAILED        (1 << 11)       /* output: stfs is unusable */
39332 +#define AUFS_MVDOWN_BOTTOM     (1 << 12)       /* output: no more lowers */
39333 +
39334 +/* index for move-down */
39335 +enum {
39336 +       AUFS_MVDOWN_UPPER,
39337 +       AUFS_MVDOWN_LOWER,
39338 +       AUFS_MVDOWN_NARRAY
39339 +};
39340 +
39341 +/*
39342 + * additional info of move-down
39343 + * number of free blocks and inodes.
39344 + * subset of struct kstatfs, but smaller and always 64bit.
39345 + */
39346 +struct aufs_stfs {
39347 +       uint64_t        f_blocks;
39348 +       uint64_t        f_bavail;
39349 +       uint64_t        f_files;
39350 +       uint64_t        f_ffree;
39351 +};
39352 +
39353 +struct aufs_stbr {
39354 +       int16_t                 brid;   /* optional input */
39355 +       int16_t                 bindex; /* output */
39356 +       struct aufs_stfs        stfs;   /* output when AUFS_MVDOWN_STFS set */
39357 +} __aligned(8);
39358 +
39359 +struct aufs_mvdown {
39360 +       uint32_t                flags;                  /* input/output */
39361 +       struct aufs_stbr        stbr[AUFS_MVDOWN_NARRAY]; /* input/output */
39362 +       int8_t                  au_errno;               /* output */
39363 +} __aligned(8);
39364 +
39365 +/* ---------------------------------------------------------------------- */
39366 +
39367 +union aufs_brinfo {
39368 +       /* PATH_MAX may differ between kernel-space and user-space */
39369 +       char    _spacer[4096];
39370 +       struct {
39371 +               int16_t id;
39372 +               int     perm;
39373 +               char    path[];
39374 +       };
39375 +} __aligned(8);
39376 +
39377 +/* ---------------------------------------------------------------------- */
39378 +
39379 +#define AuCtlType              'A'
39380 +#define AUFS_CTL_RDU           _IOWR(AuCtlType, AuCtl_RDU, struct aufs_rdu)
39381 +#define AUFS_CTL_RDU_INO       _IOWR(AuCtlType, AuCtl_RDU_INO, struct aufs_rdu)
39382 +#define AUFS_CTL_WBR_FD                _IOW(AuCtlType, AuCtl_WBR_FD, \
39383 +                                    struct aufs_wbr_fd)
39384 +#define AUFS_CTL_IBUSY         _IOWR(AuCtlType, AuCtl_IBUSY, struct aufs_ibusy)
39385 +#define AUFS_CTL_MVDOWN                _IOWR(AuCtlType, AuCtl_MVDOWN, \
39386 +                                     struct aufs_mvdown)
39387 +#define AUFS_CTL_BRINFO                _IOW(AuCtlType, AuCtl_BR, union aufs_brinfo)
39388 +#define AUFS_CTL_FHSM_FD       _IOW(AuCtlType, AuCtl_FHSM_FD, int)
39389 +
39390 +#endif /* __AUFS_TYPE_H__ */
39391 SPDX-License-Identifier: GPL-2.0
39392 aufs5.x-rcN loopback patch
39393
39394 diff --git a/drivers/block/loop.c b/drivers/block/loop.c
39395 index 9009a0efc4b28..60307b90fbead 100644
39396 --- a/drivers/block/loop.c
39397 +++ b/drivers/block/loop.c
39398 @@ -510,6 +510,15 @@ static inline void loop_update_dio(struct loop_device *lo)
39399                                 lo->use_dio);
39400  }
39401  
39402 +static struct file *loop_real_file(struct file *file)
39403 +{
39404 +       struct file *f = NULL;
39405 +
39406 +       if (file->f_path.dentry->d_sb->s_op->real_loop)
39407 +               f = file->f_path.dentry->d_sb->s_op->real_loop(file);
39408 +       return f;
39409 +}
39410 +
39411  static void loop_reread_partitions(struct loop_device *lo)
39412  {
39413         int rc;
39414 @@ -567,6 +576,7 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39415  {
39416         struct file *file = fget(arg);
39417         struct file *old_file;
39418 +       struct file *f, *virt_file = NULL, *old_virt_file;
39419         int error;
39420         bool partscan;
39421         bool is_loop;
39422 @@ -586,11 +596,19 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39423         if (!(lo->lo_flags & LO_FLAGS_READ_ONLY))
39424                 goto out_err;
39425  
39426 +       f = loop_real_file(file);
39427 +       if (f) {
39428 +               virt_file = file;
39429 +               file = f;
39430 +               get_file(file);
39431 +       }
39432 +
39433         error = loop_validate_file(file, bdev);
39434         if (error)
39435                 goto out_err;
39436  
39437         old_file = lo->lo_backing_file;
39438 +       old_virt_file = lo->lo_backing_virt_file;
39439  
39440         error = -EINVAL;
39441  
39442 @@ -603,6 +621,7 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39443         blk_mq_freeze_queue(lo->lo_queue);
39444         mapping_set_gfp_mask(old_file->f_mapping, lo->old_gfp_mask);
39445         lo->lo_backing_file = file;
39446 +       lo->lo_backing_virt_file = virt_file;
39447         lo->old_gfp_mask = mapping_gfp_mask(file->f_mapping);
39448         mapping_set_gfp_mask(file->f_mapping,
39449                              lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
39450 @@ -625,6 +644,8 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39451          * dependency.
39452          */
39453         fput(old_file);
39454 +       if (old_virt_file)
39455 +               fput(old_virt_file);
39456         if (partscan)
39457                 loop_reread_partitions(lo);
39458         return 0;
39459 @@ -633,6 +654,8 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39460         loop_global_unlock(lo, is_loop);
39461  out_putf:
39462         fput(file);
39463 +       if (virt_file)
39464 +               fput(virt_file);
39465         return error;
39466  }
39467  
39468 @@ -971,6 +994,7 @@ static int loop_configure(struct loop_device *lo, fmode_t mode,
39469                           const struct loop_config *config)
39470  {
39471         struct file *file = fget(config->fd);
39472 +       struct file *f, *virt_file = NULL;
39473         struct inode *inode;
39474         struct address_space *mapping;
39475         int error;
39476 @@ -986,6 +1010,13 @@ static int loop_configure(struct loop_device *lo, fmode_t mode,
39477         /* This is safe, since we have a reference from open(). */
39478         __module_get(THIS_MODULE);
39479  
39480 +       f = loop_real_file(file);
39481 +       if (f) {
39482 +               virt_file = file;
39483 +               file = f;
39484 +               get_file(file);
39485 +       }
39486 +
39487         /*
39488          * If we don't hold exclusive handle for the device, upgrade to it
39489          * here to avoid changing device under exclusive owner.
39490 @@ -1051,6 +1082,7 @@ static int loop_configure(struct loop_device *lo, fmode_t mode,
39491         lo->use_dio = lo->lo_flags & LO_FLAGS_DIRECT_IO;
39492         lo->lo_device = bdev;
39493         lo->lo_backing_file = file;
39494 +       lo->lo_backing_virt_file = virt_file;
39495         lo->old_gfp_mask = mapping_gfp_mask(mapping);
39496         mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
39497  
39498 @@ -1101,6 +1133,8 @@ static int loop_configure(struct loop_device *lo, fmode_t mode,
39499                 bd_abort_claiming(bdev, loop_configure);
39500  out_putf:
39501         fput(file);
39502 +       if (virt_file)
39503 +               fput(virt_file);
39504         /* This is safe: open() is still holding a reference. */
39505         module_put(THIS_MODULE);
39506         return error;
39507 @@ -1109,6 +1143,7 @@ static int loop_configure(struct loop_device *lo, fmode_t mode,
39508  static void __loop_clr_fd(struct loop_device *lo, bool release)
39509  {
39510         struct file *filp;
39511 +       struct file *virt_filp = lo->lo_backing_virt_file;
39512         gfp_t gfp = lo->old_gfp_mask;
39513         struct loop_worker *pos, *worker;
39514  
39515 @@ -1154,6 +1189,7 @@ static void __loop_clr_fd(struct loop_device *lo, bool release)
39516         spin_lock_irq(&lo->lo_lock);
39517         filp = lo->lo_backing_file;
39518         lo->lo_backing_file = NULL;
39519 +       lo->lo_backing_virt_file = NULL;
39520         spin_unlock_irq(&lo->lo_lock);
39521  
39522         lo->lo_device = NULL;
39523 @@ -1215,6 +1251,8 @@ static void __loop_clr_fd(struct loop_device *lo, bool release)
39524          * fput can take open_mutex which is usually taken before lo_mutex.
39525          */
39526         fput(filp);
39527 +       if (virt_filp)
39528 +               fput(virt_filp);
39529  }
39530  
39531  static int loop_clr_fd(struct loop_device *lo)
39532 diff --git a/drivers/block/loop.h b/drivers/block/loop.h
39533 index 082d4b6bfc6a6..61feca64688df 100644
39534 --- a/drivers/block/loop.h
39535 +++ b/drivers/block/loop.h
39536 @@ -34,7 +34,7 @@ struct loop_device {
39537         int             lo_flags;
39538         char            lo_file_name[LO_NAME_SIZE];
39539  
39540 -       struct file *   lo_backing_file;
39541 +       struct file     *lo_backing_file, *lo_backing_virt_file;
39542         struct block_device *lo_device;
39543  
39544         gfp_t           old_gfp_mask;
39545 diff --git a/fs/aufs/f_op.c b/fs/aufs/f_op.c
39546 index 99ceca1440449..e49dfe8550329 100644
39547 --- a/fs/aufs/f_op.c
39548 +++ b/fs/aufs/f_op.c
39549 @@ -304,7 +304,7 @@ static ssize_t aufs_read_iter(struct kiocb *kio, struct iov_iter *iov_iter)
39550         if (IS_ERR(h_file))
39551                 goto out;
39552  
39553 -       if (au_test_loopback_kthread()) {
39554 +       if (0 && au_test_loopback_kthread()) {
39555                 au_warn_loopback(h_file->f_path.dentry->d_sb);
39556                 if (file->f_mapping != h_file->f_mapping) {
39557                         file->f_mapping = h_file->f_mapping;
39558 diff --git a/fs/aufs/loop.c b/fs/aufs/loop.c
39559 index 74347bd75b380..5ef888a1d53f4 100644
39560 --- a/fs/aufs/loop.c
39561 +++ b/fs/aufs/loop.c
39562 @@ -133,3 +133,19 @@ void au_loopback_fin(void)
39563                 symbol_put(loop_backing_file);
39564         au_kfree_try_rcu(au_warn_loopback_array);
39565  }
39566 +
39567 +/* ---------------------------------------------------------------------- */
39568 +
39569 +/* support the loopback block device insude aufs */
39570 +
39571 +struct file *aufs_real_loop(struct file *file)
39572 +{
39573 +       struct file *f;
39574 +
39575 +       BUG_ON(!au_test_aufs(file->f_path.dentry->d_sb));
39576 +       fi_read_lock(file);
39577 +       f = au_hf_top(file);
39578 +       fi_read_unlock(file);
39579 +       AuDebugOn(!f);
39580 +       return f;
39581 +}
39582 diff --git a/fs/aufs/loop.h b/fs/aufs/loop.h
39583 index 7293bee427f96..3345c098d0d47 100644
39584 --- a/fs/aufs/loop.h
39585 +++ b/fs/aufs/loop.h
39586 @@ -26,6 +26,8 @@ void au_warn_loopback(struct super_block *h_sb);
39587  
39588  int au_loopback_init(void);
39589  void au_loopback_fin(void);
39590 +
39591 +struct file *aufs_real_loop(struct file *file);
39592  #else
39593  AuStub(struct file *, loop_backing_file, return NULL, struct super_block *sb)
39594  
39595 @@ -36,6 +38,8 @@ AuStubVoid(au_warn_loopback, struct super_block *h_sb)
39596  
39597  AuStubInt0(au_loopback_init, void)
39598  AuStubVoid(au_loopback_fin, void)
39599 +
39600 +AuStub(struct file *, aufs_real_loop, return NULL, struct file *file)
39601  #endif /* BLK_DEV_LOOP */
39602  
39603  #endif /* __KERNEL__ */
39604 diff --git a/fs/aufs/super.c b/fs/aufs/super.c
39605 index 90043afec51c1..0835f6da42d97 100644
39606 --- a/fs/aufs/super.c
39607 +++ b/fs/aufs/super.c
39608 @@ -758,7 +758,10 @@ const struct super_operations aufs_sop = {
39609         .show_options   = aufs_show_options,
39610         .statfs         = aufs_statfs,
39611         .put_super      = aufs_put_super,
39612 -       .sync_fs        = aufs_sync_fs
39613 +       .sync_fs        = aufs_sync_fs,
39614 +#ifdef CONFIG_AUFS_BDEV_LOOP
39615 +       .real_loop      = aufs_real_loop
39616 +#endif
39617  };
39618  
39619  /* ---------------------------------------------------------------------- */
39620 diff --git a/include/linux/fs.h b/include/linux/fs.h
39621 index 5479e6dc18a23..05fe92d076c19 100644
39622 --- a/include/linux/fs.h
39623 +++ b/include/linux/fs.h
39624 @@ -2137,6 +2137,10 @@ struct super_operations {
39625                                   struct shrink_control *);
39626         long (*free_cached_objects)(struct super_block *,
39627                                     struct shrink_control *);
39628 +#if IS_ENABLED(CONFIG_BLK_DEV_LOOP) || IS_ENABLED(CONFIG_BLK_DEV_LOOP_MODULE)
39629 +       /* and aufs */
39630 +       struct file *(*real_loop)(struct file *);
39631 +#endif
39632  };
39633  
39634  /*
This page took 3.144639 seconds and 2 git commands to generate.