]> git.pld-linux.org Git - packages/kernel.git/blob - kernel-aufs5.patch
8d2e3fc578d290d0489b0894b521afc8d9fd3b9d
[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 @@ -412,6 +413,7 @@ void __fput_sync(struct file *file)
723  }
724  
725  EXPORT_SYMBOL(fput);
726 +EXPORT_SYMBOL_GPL(__fput_sync);
727  
728  void __init files_init(void)
729  {
730 diff --git a/fs/namespace.c b/fs/namespace.c
731 index e189393b34b14..5d7d122d1067a 100644
732 --- a/fs/namespace.c
733 +++ b/fs/namespace.c
734 @@ -440,6 +440,7 @@ void __mnt_drop_write(struct vfsmount *mnt)
735         mnt_dec_writers(real_mount(mnt));
736         preempt_enable();
737  }
738 +EXPORT_SYMBOL_GPL(__mnt_drop_write);
739  
740  /**
741   * mnt_drop_write - give up write access to a mount
742 @@ -844,6 +845,7 @@ int is_current_mnt_ns(struct vfsmount *mnt)
743  {
744         return check_mnt(real_mount(mnt));
745  }
746 +EXPORT_SYMBOL_GPL(is_current_mnt_ns);
747  
748  /*
749   * vfsmount lock must be held for write
750 @@ -2047,6 +2049,7 @@ int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg,
751         }
752         return 0;
753  }
754 +EXPORT_SYMBOL_GPL(iterate_mounts);
755  
756  static void lock_mnt_tree(struct mount *mnt)
757  {
758 diff --git a/fs/notify/group.c b/fs/notify/group.c
759 index b7d4d64f87c29..2d2d2c6d33e41 100644
760 --- a/fs/notify/group.c
761 +++ b/fs/notify/group.c
762 @@ -100,6 +100,7 @@ void fsnotify_get_group(struct fsnotify_group *group)
763  {
764         refcount_inc(&group->refcnt);
765  }
766 +EXPORT_SYMBOL_GPL(fsnotify_get_group);
767  
768  /*
769   * Drop a reference to a group.  Free it if it's through.
770 diff --git a/fs/open.c b/fs/open.c
771 index 9ff2f621b760b..3b8deceee506f 100644
772 --- a/fs/open.c
773 +++ b/fs/open.c
774 @@ -66,6 +66,7 @@ int do_truncate(struct user_namespace *mnt_userns, struct dentry *dentry,
775         inode_unlock(dentry->d_inode);
776         return ret;
777  }
778 +EXPORT_SYMBOL_GPL(do_truncate);
779  
780  long vfs_truncate(const struct path *path, loff_t length)
781  {
782 diff --git a/fs/read_write.c b/fs/read_write.c
783 index 0074afa7ecb3e..612cf04d9f6b9 100644
784 --- a/fs/read_write.c
785 +++ b/fs/read_write.c
786 @@ -488,6 +488,7 @@ ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos)
787         inc_syscr(current);
788         return ret;
789  }
790 +EXPORT_SYMBOL_GPL(vfs_read);
791  
792  static ssize_t new_sync_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos)
793  {
794 @@ -598,6 +599,7 @@ ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_
795         file_end_write(file);
796         return ret;
797  }
798 +EXPORT_SYMBOL_GPL(vfs_write);
799  
800  /* file_ppos returns &file->f_pos or NULL if file is stream */
801  static inline loff_t *file_ppos(struct file *file)
802 diff --git a/fs/splice.c b/fs/splice.c
803 index 3e6ba363b7775..7c1be373eb7cd 100644
804 --- a/fs/splice.c
805 +++ b/fs/splice.c
806 @@ -766,6 +766,7 @@ long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
807                 return warn_unsupported(out, "write");
808         return out->f_op->splice_write(pipe, out, ppos, len, flags);
809  }
810 +EXPORT_SYMBOL_GPL(do_splice_from);
811  
812  /*
813   * Attempt to initiate a splice from a file to a pipe.
814 @@ -795,6 +796,7 @@ long do_splice_to(struct file *in, loff_t *ppos,
815                 return warn_unsupported(in, "read");
816         return in->f_op->splice_read(in, ppos, pipe, len, flags);
817  }
818 +EXPORT_SYMBOL_GPL(do_splice_to);
819  
820  /**
821   * splice_direct_to_actor - splices data directly between two non-pipes
822 diff --git a/fs/xattr.c b/fs/xattr.c
823 index 5c8c5175b385c..ff7e9ff774b73 100644
824 --- a/fs/xattr.c
825 +++ b/fs/xattr.c
826 @@ -384,6 +384,7 @@ vfs_getxattr_alloc(struct user_namespace *mnt_userns, struct dentry *dentry,
827         *xattr_value = value;
828         return error;
829  }
830 +EXPORT_SYMBOL_GPL(vfs_getxattr_alloc);
831  
832  ssize_t
833  __vfs_getxattr(struct dentry *dentry, struct inode *inode, const char *name,
834 diff --git a/kernel/locking/lockdep.c b/kernel/locking/lockdep.c
835 index 43e3d1fdd570b..5d6f33f1f2f28 100644
836 --- a/kernel/locking/lockdep.c
837 +++ b/kernel/locking/lockdep.c
838 @@ -210,6 +210,7 @@ inline struct lock_class *lockdep_hlock_class(struct held_lock *hlock)
839          */
840         return lock_classes + class_idx;
841  }
842 +EXPORT_SYMBOL_GPL(lockdep_hlock_class);
843  #define hlock_class(hlock) lockdep_hlock_class(hlock)
844  
845  #ifdef CONFIG_LOCK_STAT
846 diff --git a/kernel/task_work.c b/kernel/task_work.c
847 index 1698fbe6f0e13..081b05acadf82 100644
848 --- a/kernel/task_work.c
849 +++ b/kernel/task_work.c
850 @@ -167,3 +167,4 @@ void task_work_run(void)
851                 } while (work);
852         }
853  }
854 +EXPORT_SYMBOL_GPL(task_work_run);
855 diff --git a/security/security.c b/security/security.c
856 index 22261d79f3333..732e4c219ea29 100644
857 --- a/security/security.c
858 +++ b/security/security.c
859 @@ -1151,6 +1151,7 @@ int security_path_rmdir(const struct path *dir, struct dentry *dentry)
860                 return 0;
861         return call_int_hook(path_rmdir, 0, dir, dentry);
862  }
863 +EXPORT_SYMBOL_GPL(security_path_rmdir);
864  
865  int security_path_unlink(const struct path *dir, struct dentry *dentry)
866  {
867 @@ -1167,6 +1168,7 @@ int security_path_symlink(const struct path *dir, struct dentry *dentry,
868                 return 0;
869         return call_int_hook(path_symlink, 0, dir, dentry, old_name);
870  }
871 +EXPORT_SYMBOL_GPL(security_path_symlink);
872  
873  int security_path_link(struct dentry *old_dentry, const struct path *new_dir,
874                        struct dentry *new_dentry)
875 @@ -1175,6 +1177,7 @@ int security_path_link(struct dentry *old_dentry, const struct path *new_dir,
876                 return 0;
877         return call_int_hook(path_link, 0, old_dentry, new_dir, new_dentry);
878  }
879 +EXPORT_SYMBOL_GPL(security_path_link);
880  
881  int security_path_rename(const struct path *old_dir, struct dentry *old_dentry,
882                          const struct path *new_dir, struct dentry *new_dentry,
883 @@ -1202,6 +1205,7 @@ int security_path_truncate(const struct path *path)
884                 return 0;
885         return call_int_hook(path_truncate, 0, path);
886  }
887 +EXPORT_SYMBOL_GPL(security_path_truncate);
888  
889  int security_path_chmod(const struct path *path, umode_t mode)
890  {
891 @@ -1209,6 +1213,7 @@ int security_path_chmod(const struct path *path, umode_t mode)
892                 return 0;
893         return call_int_hook(path_chmod, 0, path, mode);
894  }
895 +EXPORT_SYMBOL_GPL(security_path_chmod);
896  
897  int security_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
898  {
899 @@ -1216,6 +1221,7 @@ int security_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
900                 return 0;
901         return call_int_hook(path_chown, 0, path, uid, gid);
902  }
903 +EXPORT_SYMBOL_GPL(security_path_chown);
904  
905  int security_path_chroot(const struct path *path)
906  {
907 @@ -1316,6 +1322,7 @@ int security_inode_permission(struct inode *inode, int mask)
908                 return 0;
909         return call_int_hook(inode_permission, 0, inode, mask);
910  }
911 +EXPORT_SYMBOL_GPL(security_inode_permission);
912  
913  int security_inode_setattr(struct dentry *dentry, struct iattr *attr)
914  {
915 @@ -1513,6 +1520,7 @@ int security_file_permission(struct file *file, int mask)
916  
917         return fsnotify_perm(file, mask);
918  }
919 +EXPORT_SYMBOL_GPL(security_file_permission);
920  
921  int security_file_alloc(struct file *file)
922  {
923 diff -urN /usr/share/empty/Documentation/ABI/testing/debugfs-aufs linux/Documentation/ABI/testing/debugfs-aufs
924 --- /usr/share/empty/Documentation/ABI/testing/debugfs-aufs     1970-01-01 01:00:00.000000000 +0100
925 +++ linux/Documentation/ABI/testing/debugfs-aufs        2022-03-21 14:49:05.713299677 +0100
926 @@ -0,0 +1,55 @@
927 +What:          /debug/aufs/si_<id>/
928 +Date:          March 2009
929 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
930 +Description:
931 +               Under /debug/aufs, a directory named si_<id> is created
932 +               per aufs mount, where <id> is a unique id generated
933 +               internally.
934 +
935 +What:          /debug/aufs/si_<id>/plink
936 +Date:          Apr 2013
937 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
938 +Description:
939 +               It has three lines and shows the information about the
940 +               pseudo-link. The first line is a single number
941 +               representing a number of buckets. The second line is a
942 +               number of pseudo-links per buckets (separated by a
943 +               blank). The last line is a single number representing a
944 +               total number of psedo-links.
945 +               When the aufs mount option 'noplink' is specified, it
946 +               will show "1\n0\n0\n".
947 +
948 +What:          /debug/aufs/si_<id>/xib
949 +Date:          March 2009
950 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
951 +Description:
952 +               It shows the consumed blocks by xib (External Inode Number
953 +               Bitmap), its block size and file size.
954 +               When the aufs mount option 'noxino' is specified, it
955 +               will be empty. About XINO files, see the aufs manual.
956 +
957 +What:          /debug/aufs/si_<id>/xi<branch-index>
958 +Date:          March 2009
959 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
960 +Description:
961 +               It shows the consumed blocks by xino (External Inode Number
962 +               Translation Table), its link count, block size and file
963 +               size.
964 +               Due to the file size limit, there may exist multiple
965 +               xino files per branch.  In this case, "-N" is added to
966 +               the filename and it corresponds to the index of the
967 +               internal xino array.  "-0" is omitted.
968 +               When the aufs mount option 'noxino' is specified, Those
969 +               entries won't exist.  About XINO files, see the aufs
970 +               manual.
971 +
972 +What:          /debug/aufs/si_<id>/xigen
973 +Date:          March 2009
974 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
975 +Description:
976 +               It shows the consumed blocks by xigen (External Inode
977 +               Generation Table), its block size and file size.
978 +               If CONFIG_AUFS_EXPORT is disabled, this entry will not
979 +               be created.
980 +               When the aufs mount option 'noxino' is specified, it
981 +               will be empty. About XINO files, see the aufs manual.
982 diff -urN /usr/share/empty/Documentation/ABI/testing/sysfs-aufs linux/Documentation/ABI/testing/sysfs-aufs
983 --- /usr/share/empty/Documentation/ABI/testing/sysfs-aufs       1970-01-01 01:00:00.000000000 +0100
984 +++ linux/Documentation/ABI/testing/sysfs-aufs  2022-03-21 14:49:05.713299677 +0100
985 @@ -0,0 +1,31 @@
986 +What:          /sys/fs/aufs/si_<id>/
987 +Date:          March 2009
988 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
989 +Description:
990 +               Under /sys/fs/aufs, a directory named si_<id> is created
991 +               per aufs mount, where <id> is a unique id generated
992 +               internally.
993 +
994 +What:          /sys/fs/aufs/si_<id>/br<idx>
995 +Date:          March 2009
996 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
997 +Description:
998 +               It shows the abolute path of a member directory (which
999 +               is called branch) in aufs, and its permission.
1000 +
1001 +What:          /sys/fs/aufs/si_<id>/brid<idx>
1002 +Date:          July 2013
1003 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1004 +Description:
1005 +               It shows the id of a member directory (which is called
1006 +               branch) in aufs.
1007 +
1008 +What:          /sys/fs/aufs/si_<id>/xi_path
1009 +Date:          March 2009
1010 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1011 +Description:
1012 +               It shows the abolute path of XINO (External Inode Number
1013 +               Bitmap, Translation Table and Generation Table) file
1014 +               even if it is the default path.
1015 +               When the aufs mount option 'noxino' is specified, it
1016 +               will be empty. About XINO files, see the aufs manual.
1017 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/01intro.txt linux/Documentation/filesystems/aufs/design/01intro.txt
1018 --- /usr/share/empty/Documentation/filesystems/aufs/design/01intro.txt  1970-01-01 01:00:00.000000000 +0100
1019 +++ linux/Documentation/filesystems/aufs/design/01intro.txt     2022-03-21 14:49:05.713299677 +0100
1020 @@ -0,0 +1,171 @@
1021 +
1022 +# Copyright (C) 2005-2021 Junjiro R. Okajima
1023 +# 
1024 +# This program is free software; you can redistribute it and/or modify
1025 +# it under the terms of the GNU General Public License as published by
1026 +# the Free Software Foundation; either version 2 of the License, or
1027 +# (at your option) any later version.
1028 +# 
1029 +# This program is distributed in the hope that it will be useful,
1030 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1031 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1032 +# GNU General Public License for more details.
1033 +# 
1034 +# You should have received a copy of the GNU General Public License
1035 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1036 +
1037 +Introduction
1038 +----------------------------------------
1039 +
1040 +aufs [ei ju: ef es] | /ey-yoo-ef-es/ | [a u f s]
1041 +1. abbrev. for "advanced multi-layered unification filesystem".
1042 +2. abbrev. for "another unionfs".
1043 +3. abbrev. for "auf das" in German which means "on the" in English.
1044 +   Ex. "Butter aufs Brot"(G) means "butter onto bread"(E).
1045 +   But "Filesystem aufs Filesystem" is hard to understand.
1046 +4. abbrev. for "African Urban Fashion Show".
1047 +
1048 +AUFS is a filesystem with features:
1049 +- multi layered stackable unification filesystem, the member directory
1050 +  is called as a branch.
1051 +- branch permission and attribute, 'readonly', 'real-readonly',
1052 +  'readwrite', 'whiteout-able', 'link-able whiteout', etc. and their
1053 +  combination.
1054 +- internal "file copy-on-write".
1055 +- logical deletion, whiteout.
1056 +- dynamic branch manipulation, adding, deleting and changing permission.
1057 +- allow bypassing aufs, user's direct branch access.
1058 +- external inode number translation table and bitmap which maintains the
1059 +  persistent aufs inode number.
1060 +- seekable directory, including NFS readdir.
1061 +- file mapping, mmap and sharing pages.
1062 +- pseudo-link, hardlink over branches.
1063 +- loopback mounted filesystem as a branch.
1064 +- several policies to select one among multiple writable branches.
1065 +- revert a single systemcall when an error occurs in aufs.
1066 +- and more...
1067 +
1068 +
1069 +Multi Layered Stackable Unification Filesystem
1070 +----------------------------------------------------------------------
1071 +Most people already knows what it is.
1072 +It is a filesystem which unifies several directories and provides a
1073 +merged single directory. When users access a file, the access will be
1074 +passed/re-directed/converted (sorry, I am not sure which English word is
1075 +correct) to the real file on the member filesystem. The member
1076 +filesystem is called 'lower filesystem' or 'branch' and has a mode
1077 +'readonly' and 'readwrite.' And the deletion for a file on the lower
1078 +readonly branch is handled by creating 'whiteout' on the upper writable
1079 +branch.
1080 +
1081 +On LKML, there have been discussions about UnionMount (Jan Blunck,
1082 +Bharata B Rao and Valerie Aurora) and Unionfs (Erez Zadok). They took
1083 +different approaches to implement the merged-view.
1084 +The former tries putting it into VFS, and the latter implements as a
1085 +separate filesystem.
1086 +(If I misunderstand about these implementations, please let me know and
1087 +I shall correct it. Because it is a long time ago when I read their
1088 +source files last time).
1089 +
1090 +UnionMount's approach will be able to small, but may be hard to share
1091 +branches between several UnionMount since the whiteout in it is
1092 +implemented in the inode on branch filesystem and always
1093 +shared. According to Bharata's post, readdir does not seems to be
1094 +finished yet.
1095 +There are several missing features known in this implementations such as
1096 +- for users, the inode number may change silently. eg. copy-up.
1097 +- link(2) may break by copy-up.
1098 +- read(2) may get an obsoleted filedata (fstat(2) too).
1099 +- fcntl(F_SETLK) may be broken by copy-up.
1100 +- unnecessary copy-up may happen, for example mmap(MAP_PRIVATE) after
1101 +  open(O_RDWR).
1102 +
1103 +In linux-3.18, "overlay" filesystem (formerly known as "overlayfs") was
1104 +merged into mainline. This is another implementation of UnionMount as a
1105 +separated filesystem. All the limitations and known problems which
1106 +UnionMount are equally inherited to "overlay" filesystem.
1107 +
1108 +Unionfs has a longer history. When I started implementing a stackable
1109 +filesystem (Aug 2005), it already existed. It has virtual super_block,
1110 +inode, dentry and file objects and they have an array pointing lower
1111 +same kind objects. After contributing many patches for Unionfs, I
1112 +re-started my project AUFS (Jun 2006).
1113 +
1114 +In AUFS, the structure of filesystem resembles to Unionfs, but I
1115 +implemented my own ideas, approaches and enhancements and it became
1116 +totally different one.
1117 +
1118 +Comparing DM snapshot and fs based implementation
1119 +- the number of bytes to be copied between devices is much smaller.
1120 +- the type of filesystem must be one and only.
1121 +- the fs must be writable, no readonly fs, even for the lower original
1122 +  device. so the compression fs will not be usable. but if we use
1123 +  loopback mount, we may address this issue.
1124 +  for instance,
1125 +       mount /cdrom/squashfs.img /sq
1126 +       losetup /sq/ext2.img
1127 +       losetup /somewhere/cow
1128 +       dmsetup "snapshot /dev/loop0 /dev/loop1 ..."
1129 +- it will be difficult (or needs more operations) to extract the
1130 +  difference between the original device and COW.
1131 +- DM snapshot-merge may help a lot when users try merging. in the
1132 +  fs-layer union, users will use rsync(1).
1133 +
1134 +You may want to read my old paper "Filesystems in LiveCD"
1135 +(http://aufs.sourceforge.net/aufs2/report/sq/sq.pdf).
1136 +
1137 +
1138 +Several characters/aspects/persona of aufs
1139 +----------------------------------------------------------------------
1140 +
1141 +Aufs has several characters, aspects or persona.
1142 +1. a filesystem, callee of VFS helper
1143 +2. sub-VFS, caller of VFS helper for branches
1144 +3. a virtual filesystem which maintains persistent inode number
1145 +4. reader/writer of files on branches such like an application
1146 +
1147 +1. Callee of VFS Helper
1148 +As an ordinary linux filesystem, aufs is a callee of VFS. For instance,
1149 +unlink(2) from an application reaches sys_unlink() kernel function and
1150 +then vfs_unlink() is called. vfs_unlink() is one of VFS helper and it
1151 +calls filesystem specific unlink operation. Actually aufs implements the
1152 +unlink operation but it behaves like a redirector.
1153 +
1154 +2. Caller of VFS Helper for Branches
1155 +aufs_unlink() passes the unlink request to the branch filesystem as if
1156 +it were called from VFS. So the called unlink operation of the branch
1157 +filesystem acts as usual. As a caller of VFS helper, aufs should handle
1158 +every necessary pre/post operation for the branch filesystem.
1159 +- acquire the lock for the parent dir on a branch
1160 +- lookup in a branch
1161 +- revalidate dentry on a branch
1162 +- mnt_want_write() for a branch
1163 +- vfs_unlink() for a branch
1164 +- mnt_drop_write() for a branch
1165 +- release the lock on a branch
1166 +
1167 +3. Persistent Inode Number
1168 +One of the most important issue for a filesystem is to maintain inode
1169 +numbers. This is particularly important to support exporting a
1170 +filesystem via NFS. Aufs is a virtual filesystem which doesn't have a
1171 +backend block device for its own. But some storage is necessary to
1172 +keep and maintain the inode numbers. It may be a large space and may not
1173 +suit to keep in memory. Aufs rents some space from its first writable
1174 +branch filesystem (by default) and creates file(s) on it. These files
1175 +are created by aufs internally and removed soon (currently) keeping
1176 +opened.
1177 +Note: Because these files are removed, they are totally gone after
1178 +      unmounting aufs. It means the inode numbers are not persistent
1179 +      across unmount or reboot. I have a plan to make them really
1180 +      persistent which will be important for aufs on NFS server.
1181 +
1182 +4. Read/Write Files Internally (copy-on-write)
1183 +Because a branch can be readonly, when you write a file on it, aufs will
1184 +"copy-up" it to the upper writable branch internally. And then write the
1185 +originally requested thing to the file. Generally kernel doesn't
1186 +open/read/write file actively. In aufs, even a single write may cause a
1187 +internal "file copy". This behaviour is very similar to cp(1) command.
1188 +
1189 +Some people may think it is better to pass such work to user space
1190 +helper, instead of doing in kernel space. Actually I am still thinking
1191 +about it. But currently I have implemented it in kernel space.
1192 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/02struct.txt linux/Documentation/filesystems/aufs/design/02struct.txt
1193 --- /usr/share/empty/Documentation/filesystems/aufs/design/02struct.txt 1970-01-01 01:00:00.000000000 +0100
1194 +++ linux/Documentation/filesystems/aufs/design/02struct.txt    2022-03-21 14:49:05.713299677 +0100
1195 @@ -0,0 +1,258 @@
1196 +
1197 +# Copyright (C) 2005-2021 Junjiro R. Okajima
1198 +# 
1199 +# This program is free software; you can redistribute it and/or modify
1200 +# it under the terms of the GNU General Public License as published by
1201 +# the Free Software Foundation; either version 2 of the License, or
1202 +# (at your option) any later version.
1203 +# 
1204 +# This program is distributed in the hope that it will be useful,
1205 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1206 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1207 +# GNU General Public License for more details.
1208 +# 
1209 +# You should have received a copy of the GNU General Public License
1210 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1211 +
1212 +Basic Aufs Internal Structure
1213 +
1214 +Superblock/Inode/Dentry/File Objects
1215 +----------------------------------------------------------------------
1216 +As like an ordinary filesystem, aufs has its own
1217 +superblock/inode/dentry/file objects. All these objects have a
1218 +dynamically allocated array and store the same kind of pointers to the
1219 +lower filesystem, branch.
1220 +For example, when you build a union with one readwrite branch and one
1221 +readonly, mounted /au, /rw and /ro respectively.
1222 +- /au = /rw + /ro
1223 +- /ro/fileA exists but /rw/fileA
1224 +
1225 +Aufs lookup operation finds /ro/fileA and gets dentry for that. These
1226 +pointers are stored in a aufs dentry. The array in aufs dentry will be,
1227 +- [0] = NULL (because /rw/fileA doesn't exist)
1228 +- [1] = /ro/fileA
1229 +
1230 +This style of an array is essentially same to the aufs
1231 +superblock/inode/dentry/file objects.
1232 +
1233 +Because aufs supports manipulating branches, ie. add/delete/change
1234 +branches dynamically, these objects has its own generation. When
1235 +branches are changed, the generation in aufs superblock is
1236 +incremented. And a generation in other object are compared when it is
1237 +accessed. When a generation in other objects are obsoleted, aufs
1238 +refreshes the internal array.
1239 +
1240 +
1241 +Superblock
1242 +----------------------------------------------------------------------
1243 +Additionally aufs superblock has some data for policies to select one
1244 +among multiple writable branches, XIB files, pseudo-links and kobject.
1245 +See below in detail.
1246 +About the policies which supports copy-down a directory, see
1247 +wbr_policy.txt too.
1248 +
1249 +
1250 +Branch and XINO(External Inode Number Translation Table)
1251 +----------------------------------------------------------------------
1252 +Every branch has its own xino (external inode number translation table)
1253 +file. The xino file is created and unlinked by aufs internally. When two
1254 +members of a union exist on the same filesystem, they share the single
1255 +xino file.
1256 +The struct of a xino file is simple, just a sequence of aufs inode
1257 +numbers which is indexed by the lower inode number.
1258 +In the above sample, assume the inode number of /ro/fileA is i111 and
1259 +aufs assigns the inode number i999 for fileA. Then aufs writes 999 as
1260 +4(8) bytes at 111 * 4(8) bytes offset in the xino file.
1261 +
1262 +When the inode numbers are not contiguous, the xino file will be sparse
1263 +which has a hole in it and doesn't consume as much disk space as it
1264 +might appear. If your branch filesystem consumes disk space for such
1265 +holes, then you should specify 'xino=' option at mounting aufs.
1266 +
1267 +Aufs has a mount option to free the disk blocks for such holes in XINO
1268 +files on tmpfs or ramdisk. But it is not so effective actually. If you
1269 +meet a problem of disk shortage due to XINO files, then you should try
1270 +"tmpfs-ino.patch" (and "vfs-ino.patch" too) in aufs4-standalone.git.
1271 +The patch localizes the assignment inumbers per tmpfs-mount and avoid
1272 +the holes in XINO files.
1273 +
1274 +Also a writable branch has three kinds of "whiteout bases". All these
1275 +are existed when the branch is joined to aufs, and their names are
1276 +whiteout-ed doubly, so that users will never see their names in aufs
1277 +hierarchy.
1278 +1. a regular file which will be hardlinked to all whiteouts.
1279 +2. a directory to store a pseudo-link.
1280 +3. a directory to store an "orphan"-ed file temporary.
1281 +
1282 +1. Whiteout Base
1283 +   When you remove a file on a readonly branch, aufs handles it as a
1284 +   logical deletion and creates a whiteout on the upper writable branch
1285 +   as a hardlink of this file in order not to consume inode on the
1286 +   writable branch.
1287 +2. Pseudo-link Dir
1288 +   See below, Pseudo-link.
1289 +3. Step-Parent Dir
1290 +   When "fileC" exists on the lower readonly branch only and it is
1291 +   opened and removed with its parent dir, and then user writes
1292 +   something into it, then aufs copies-up fileC to this
1293 +   directory. Because there is no other dir to store fileC. After
1294 +   creating a file under this dir, the file is unlinked.
1295 +
1296 +Because aufs supports manipulating branches, ie. add/delete/change
1297 +dynamically, a branch has its own id. When the branch order changes,
1298 +aufs finds the new index by searching the branch id.
1299 +
1300 +
1301 +Pseudo-link
1302 +----------------------------------------------------------------------
1303 +Assume "fileA" exists on the lower readonly branch only and it is
1304 +hardlinked to "fileB" on the branch. When you write something to fileA,
1305 +aufs copies-up it to the upper writable branch. Additionally aufs
1306 +creates a hardlink under the Pseudo-link Directory of the writable
1307 +branch. The inode of a pseudo-link is kept in aufs super_block as a
1308 +simple list. If fileB is read after unlinking fileA, aufs returns
1309 +filedata from the pseudo-link instead of the lower readonly
1310 +branch. Because the pseudo-link is based upon the inode, to keep the
1311 +inode number by xino (see above) is essentially necessary.
1312 +
1313 +All the hardlinks under the Pseudo-link Directory of the writable branch
1314 +should be restored in a proper location later. Aufs provides a utility
1315 +to do this. The userspace helpers executed at remounting and unmounting
1316 +aufs by default.
1317 +During this utility is running, it puts aufs into the pseudo-link
1318 +maintenance mode. In this mode, only the process which began the
1319 +maintenance mode (and its child processes) is allowed to operate in
1320 +aufs. Some other processes which are not related to the pseudo-link will
1321 +be allowed to run too, but the rest have to return an error or wait
1322 +until the maintenance mode ends. If a process already acquires an inode
1323 +mutex (in VFS), it has to return an error.
1324 +
1325 +
1326 +XIB(external inode number bitmap)
1327 +----------------------------------------------------------------------
1328 +Addition to the xino file per a branch, aufs has an external inode number
1329 +bitmap in a superblock object. It is also an internal file such like a
1330 +xino file.
1331 +It is a simple bitmap to mark whether the aufs inode number is in-use or
1332 +not.
1333 +To reduce the file I/O, aufs prepares a single memory page to cache xib.
1334 +
1335 +As well as XINO files, aufs has a feature to truncate/refresh XIB to
1336 +reduce the number of consumed disk blocks for these files.
1337 +
1338 +
1339 +Virtual or Vertical Dir, and Readdir in Userspace
1340 +----------------------------------------------------------------------
1341 +In order to support multiple layers (branches), aufs readdir operation
1342 +constructs a virtual dir block on memory. For readdir, aufs calls
1343 +vfs_readdir() internally for each dir on branches, merges their entries
1344 +with eliminating the whiteout-ed ones, and sets it to file (dir)
1345 +object. So the file object has its entry list until it is closed. The
1346 +entry list will be updated when the file position is zero and becomes
1347 +obsoleted. This decision is made in aufs automatically.
1348 +
1349 +The dynamically allocated memory block for the name of entries has a
1350 +unit of 512 bytes (by default) and stores the names contiguously (no
1351 +padding). Another block for each entry is handled by kmem_cache too.
1352 +During building dir blocks, aufs creates hash list and judging whether
1353 +the entry is whiteouted by its upper branch or already listed.
1354 +The merged result is cached in the corresponding inode object and
1355 +maintained by a customizable life-time option.
1356 +
1357 +Some people may call it can be a security hole or invite DoS attack
1358 +since the opened and once readdir-ed dir (file object) holds its entry
1359 +list and becomes a pressure for system memory. But I'd say it is similar
1360 +to files under /proc or /sys. The virtual files in them also holds a
1361 +memory page (generally) while they are opened. When an idea to reduce
1362 +memory for them is introduced, it will be applied to aufs too.
1363 +For those who really hate this situation, I've developed readdir(3)
1364 +library which operates this merging in userspace. You just need to set
1365 +LD_PRELOAD environment variable, and aufs will not consume no memory in
1366 +kernel space for readdir(3).
1367 +
1368 +
1369 +Workqueue
1370 +----------------------------------------------------------------------
1371 +Aufs sometimes requires privilege access to a branch. For instance,
1372 +in copy-up/down operation. When a user process is going to make changes
1373 +to a file which exists in the lower readonly branch only, and the mode
1374 +of one of ancestor directories may not be writable by a user
1375 +process. Here aufs copy-up the file with its ancestors and they may
1376 +require privilege to set its owner/group/mode/etc.
1377 +This is a typical case of a application character of aufs (see
1378 +Introduction).
1379 +
1380 +Aufs uses workqueue synchronously for this case. It creates its own
1381 +workqueue. The workqueue is a kernel thread and has privilege. Aufs
1382 +passes the request to call mkdir or write (for example), and wait for
1383 +its completion. This approach solves a problem of a signal handler
1384 +simply.
1385 +If aufs didn't adopt the workqueue and changed the privilege of the
1386 +process, then the process may receive the unexpected SIGXFSZ or other
1387 +signals.
1388 +
1389 +Also aufs uses the system global workqueue ("events" kernel thread) too
1390 +for asynchronous tasks, such like handling inotify/fsnotify, re-creating a
1391 +whiteout base and etc. This is unrelated to a privilege.
1392 +Most of aufs operation tries acquiring a rw_semaphore for aufs
1393 +superblock at the beginning, at the same time waits for the completion
1394 +of all queued asynchronous tasks.
1395 +
1396 +
1397 +Whiteout
1398 +----------------------------------------------------------------------
1399 +The whiteout in aufs is very similar to Unionfs's. That is represented
1400 +by its filename. UnionMount takes an approach of a file mode, but I am
1401 +afraid several utilities (find(1) or something) will have to support it.
1402 +
1403 +Basically the whiteout represents "logical deletion" which stops aufs to
1404 +lookup further, but also it represents "dir is opaque" which also stop
1405 +further lookup.
1406 +
1407 +In aufs, rmdir(2) and rename(2) for dir uses whiteout alternatively.
1408 +In order to make several functions in a single systemcall to be
1409 +revertible, aufs adopts an approach to rename a directory to a temporary
1410 +unique whiteouted name.
1411 +For example, in rename(2) dir where the target dir already existed, aufs
1412 +renames the target dir to a temporary unique whiteouted name before the
1413 +actual rename on a branch, and then handles other actions (make it opaque,
1414 +update the attributes, etc). If an error happens in these actions, aufs
1415 +simply renames the whiteouted name back and returns an error. If all are
1416 +succeeded, aufs registers a function to remove the whiteouted unique
1417 +temporary name completely and asynchronously to the system global
1418 +workqueue.
1419 +
1420 +
1421 +Copy-up
1422 +----------------------------------------------------------------------
1423 +It is a well-known feature or concept.
1424 +When user modifies a file on a readonly branch, aufs operate "copy-up"
1425 +internally and makes change to the new file on the upper writable branch.
1426 +When the trigger systemcall does not update the timestamps of the parent
1427 +dir, aufs reverts it after copy-up.
1428 +
1429 +
1430 +Move-down (aufs3.9 and later)
1431 +----------------------------------------------------------------------
1432 +"Copy-up" is one of the essential feature in aufs. It copies a file from
1433 +the lower readonly branch to the upper writable branch when a user
1434 +changes something about the file.
1435 +"Move-down" is an opposite action of copy-up. Basically this action is
1436 +ran manually instead of automatically and internally.
1437 +For desgin and implementation, aufs has to consider these issues.
1438 +- whiteout for the file may exist on the lower branch.
1439 +- ancestor directories may not exist on the lower branch.
1440 +- diropq for the ancestor directories may exist on the upper branch.
1441 +- free space on the lower branch will reduce.
1442 +- another access to the file may happen during moving-down, including
1443 +  UDBA (see "Revalidate Dentry and UDBA").
1444 +- the file should not be hard-linked nor pseudo-linked. they should be
1445 +  handled by auplink utility later.
1446 +
1447 +Sometimes users want to move-down a file from the upper writable branch
1448 +to the lower readonly or writable branch. For instance,
1449 +- the free space of the upper writable branch is going to run out.
1450 +- create a new intermediate branch between the upper and lower branch.
1451 +- etc.
1452 +
1453 +For this purpose, use "aumvdown" command in aufs-util.git.
1454 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/03atomic_open.txt linux/Documentation/filesystems/aufs/design/03atomic_open.txt
1455 --- /usr/share/empty/Documentation/filesystems/aufs/design/03atomic_open.txt    1970-01-01 01:00:00.000000000 +0100
1456 +++ linux/Documentation/filesystems/aufs/design/03atomic_open.txt       2022-03-21 14:49:05.713299677 +0100
1457 @@ -0,0 +1,85 @@
1458 +
1459 +# Copyright (C) 2015-2021 Junjiro R. Okajima
1460 +# 
1461 +# This program is free software; you can redistribute it and/or modify
1462 +# it under the terms of the GNU General Public License as published by
1463 +# the Free Software Foundation; either version 2 of the License, or
1464 +# (at your option) any later version.
1465 +# 
1466 +# This program is distributed in the hope that it will be useful,
1467 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1468 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1469 +# GNU General Public License for more details.
1470 +# 
1471 +# You should have received a copy of the GNU General Public License
1472 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1473 +
1474 +Support for a branch who has its ->atomic_open()
1475 +----------------------------------------------------------------------
1476 +The filesystems who implement its ->atomic_open() are not majority. For
1477 +example NFSv4 does, and aufs should call NFSv4 ->atomic_open,
1478 +particularly for open(O_CREAT|O_EXCL, 0400) case. Other than
1479 +->atomic_open(), NFSv4 returns an error for this open(2). While I am not
1480 +sure whether all filesystems who have ->atomic_open() behave like this,
1481 +but NFSv4 surely returns the error.
1482 +
1483 +In order to support ->atomic_open() for aufs, there are a few
1484 +approaches.
1485 +
1486 +A. Introduce aufs_atomic_open()
1487 +   - calls one of VFS:do_last(), lookup_open() or atomic_open() for
1488 +     branch fs.
1489 +B. Introduce aufs_atomic_open() calling create, open and chmod. this is
1490 +   an aufs user Pip Cet's approach
1491 +   - calls aufs_create(), VFS finish_open() and notify_change().
1492 +   - pass fake-mode to finish_open(), and then correct the mode by
1493 +     notify_change().
1494 +C. Extend aufs_open() to call branch fs's ->atomic_open()
1495 +   - no aufs_atomic_open().
1496 +   - aufs_lookup() registers the TID to an aufs internal object.
1497 +   - aufs_create() does nothing when the matching TID is registered, but
1498 +     registers the mode.
1499 +   - aufs_open() calls branch fs's ->atomic_open() when the matching
1500 +     TID is registered.
1501 +D. Extend aufs_open() to re-try branch fs's ->open() with superuser's
1502 +   credential
1503 +   - no aufs_atomic_open().
1504 +   - aufs_create() registers the TID to an internal object. this info
1505 +     represents "this process created this file just now."
1506 +   - when aufs gets EACCES from branch fs's ->open(), then confirm the
1507 +     registered TID and re-try open() with superuser's credential.
1508 +
1509 +Pros and cons for each approach.
1510 +
1511 +A.
1512 +   - straightforward but highly depends upon VFS internal.
1513 +   - the atomic behavaiour is kept.
1514 +   - some of parameters such as nameidata are hard to reproduce for
1515 +     branch fs.
1516 +   - large overhead.
1517 +B.
1518 +   - easy to implement.
1519 +   - the atomic behavaiour is lost.
1520 +C.
1521 +   - the atomic behavaiour is kept.
1522 +   - dirty and tricky.
1523 +   - VFS checks whether the file is created correctly after calling
1524 +     ->create(), which means this approach doesn't work.
1525 +D.
1526 +   - easy to implement.
1527 +   - the atomic behavaiour is lost.
1528 +   - to open a file with superuser's credential and give it to a user
1529 +     process is a bad idea, since the file object keeps the credential
1530 +     in it. It may affect LSM or something. This approach doesn't work
1531 +     either.
1532 +
1533 +The approach A is ideal, but it hard to implement. So here is a
1534 +variation of A, which is to be implemented.
1535 +
1536 +A-1. Introduce aufs_atomic_open()
1537 +     - calls branch fs ->atomic_open() if exists. otherwise calls
1538 +       vfs_create() and finish_open().
1539 +     - the demerit is that the several checks after branch fs
1540 +       ->atomic_open() are lost. in the ordinary case, the checks are
1541 +       done by VFS:do_last(), lookup_open() and atomic_open(). some can
1542 +       be implemented in aufs, but not all I am afraid.
1543 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/03lookup.txt linux/Documentation/filesystems/aufs/design/03lookup.txt
1544 --- /usr/share/empty/Documentation/filesystems/aufs/design/03lookup.txt 1970-01-01 01:00:00.000000000 +0100
1545 +++ linux/Documentation/filesystems/aufs/design/03lookup.txt    2022-03-21 14:49:05.713299677 +0100
1546 @@ -0,0 +1,113 @@
1547 +
1548 +# Copyright (C) 2005-2021 Junjiro R. Okajima
1549 +# 
1550 +# This program is free software; you can redistribute it and/or modify
1551 +# it under the terms of the GNU General Public License as published by
1552 +# the Free Software Foundation; either version 2 of the License, or
1553 +# (at your option) any later version.
1554 +# 
1555 +# This program is distributed in the hope that it will be useful,
1556 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1557 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1558 +# GNU General Public License for more details.
1559 +# 
1560 +# You should have received a copy of the GNU General Public License
1561 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1562 +
1563 +Lookup in a Branch
1564 +----------------------------------------------------------------------
1565 +Since aufs has a character of sub-VFS (see Introduction), it operates
1566 +lookup for branches as VFS does. It may be a heavy work. But almost all
1567 +lookup operation in aufs is the simplest case, ie. lookup only an entry
1568 +directly connected to its parent. Digging down the directory hierarchy
1569 +is unnecessary. VFS has a function lookup_one_len() for that use, and
1570 +aufs calls it.
1571 +
1572 +When a branch is a remote filesystem, aufs basically relies upon its
1573 +->d_revalidate(), also aufs forces the hardest revalidate tests for
1574 +them.
1575 +For d_revalidate, aufs implements three levels of revalidate tests. See
1576 +"Revalidate Dentry and UDBA" in detail.
1577 +
1578 +
1579 +Test Only the Highest One for the Directory Permission (dirperm1 option)
1580 +----------------------------------------------------------------------
1581 +Let's try case study.
1582 +- aufs has two branches, upper readwrite and lower readonly.
1583 +  /au = /rw + /ro
1584 +- "dirA" exists under /ro, but /rw. and its mode is 0700.
1585 +- user invoked "chmod a+rx /au/dirA"
1586 +- the internal copy-up is activated and "/rw/dirA" is created and its
1587 +  permission bits are set to world readable.
1588 +- then "/au/dirA" becomes world readable?
1589 +
1590 +In this case, /ro/dirA is still 0700 since it exists in readonly branch,
1591 +or it may be a natively readonly filesystem. If aufs respects the lower
1592 +branch, it should not respond readdir request from other users. But user
1593 +allowed it by chmod. Should really aufs rejects showing the entries
1594 +under /ro/dirA?
1595 +
1596 +To be honest, I don't have a good solution for this case. So aufs
1597 +implements 'dirperm1' and 'nodirperm1' mount options, and leave it to
1598 +users.
1599 +When dirperm1 is specified, aufs checks only the highest one for the
1600 +directory permission, and shows the entries. Otherwise, as usual, checks
1601 +every dir existing on all branches and rejects the request.
1602 +
1603 +As a side effect, dirperm1 option improves the performance of aufs
1604 +because the number of permission check is reduced when the number of
1605 +branch is many.
1606 +
1607 +
1608 +Revalidate Dentry and UDBA (User's Direct Branch Access)
1609 +----------------------------------------------------------------------
1610 +Generally VFS helpers re-validate a dentry as a part of lookup.
1611 +0. digging down the directory hierarchy.
1612 +1. lock the parent dir by its i_mutex.
1613 +2. lookup the final (child) entry.
1614 +3. revalidate it.
1615 +4. call the actual operation (create, unlink, etc.)
1616 +5. unlock the parent dir
1617 +
1618 +If the filesystem implements its ->d_revalidate() (step 3), then it is
1619 +called. Actually aufs implements it and checks the dentry on a branch is
1620 +still valid.
1621 +But it is not enough. Because aufs has to release the lock for the
1622 +parent dir on a branch at the end of ->lookup() (step 2) and
1623 +->d_revalidate() (step 3) while the i_mutex of the aufs dir is still
1624 +held by VFS.
1625 +If the file on a branch is changed directly, eg. bypassing aufs, after
1626 +aufs released the lock, then the subsequent operation may cause
1627 +something unpleasant result.
1628 +
1629 +This situation is a result of VFS architecture, ->lookup() and
1630 +->d_revalidate() is separated. But I never say it is wrong. It is a good
1631 +design from VFS's point of view. It is just not suitable for sub-VFS
1632 +character in aufs.
1633 +
1634 +Aufs supports such case by three level of revalidation which is
1635 +selectable by user.
1636 +1. Simple Revalidate
1637 +   Addition to the native flow in VFS's, confirm the child-parent
1638 +   relationship on the branch just after locking the parent dir on the
1639 +   branch in the "actual operation" (step 4). When this validation
1640 +   fails, aufs returns EBUSY. ->d_revalidate() (step 3) in aufs still
1641 +   checks the validation of the dentry on branches.
1642 +2. Monitor Changes Internally by Inotify/Fsnotify
1643 +   Addition to above, in the "actual operation" (step 4) aufs re-lookup
1644 +   the dentry on the branch, and returns EBUSY if it finds different
1645 +   dentry.
1646 +   Additionally, aufs sets the inotify/fsnotify watch for every dir on branches
1647 +   during it is in cache. When the event is notified, aufs registers a
1648 +   function to kernel 'events' thread by schedule_work(). And the
1649 +   function sets some special status to the cached aufs dentry and inode
1650 +   private data. If they are not cached, then aufs has nothing to
1651 +   do. When the same file is accessed through aufs (step 0-3) later,
1652 +   aufs will detect the status and refresh all necessary data.
1653 +   In this mode, aufs has to ignore the event which is fired by aufs
1654 +   itself.
1655 +3. No Extra Validation
1656 +   This is the simplest test and doesn't add any additional revalidation
1657 +   test, and skip the revalidation in step 4. It is useful and improves
1658 +   aufs performance when system surely hide the aufs branches from user,
1659 +   by over-mounting something (or another method).
1660 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/04branch.txt linux/Documentation/filesystems/aufs/design/04branch.txt
1661 --- /usr/share/empty/Documentation/filesystems/aufs/design/04branch.txt 1970-01-01 01:00:00.000000000 +0100
1662 +++ linux/Documentation/filesystems/aufs/design/04branch.txt    2022-03-21 14:49:05.713299677 +0100
1663 @@ -0,0 +1,74 @@
1664 +
1665 +# Copyright (C) 2005-2021 Junjiro R. Okajima
1666 +# 
1667 +# This program is free software; you can redistribute it and/or modify
1668 +# it under the terms of the GNU General Public License as published by
1669 +# the Free Software Foundation; either version 2 of the License, or
1670 +# (at your option) any later version.
1671 +# 
1672 +# This program is distributed in the hope that it will be useful,
1673 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1674 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1675 +# GNU General Public License for more details.
1676 +# 
1677 +# You should have received a copy of the GNU General Public License
1678 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1679 +
1680 +Branch Manipulation
1681 +
1682 +Since aufs supports dynamic branch manipulation, ie. add/remove a branch
1683 +and changing its permission/attribute, there are a lot of works to do.
1684 +
1685 +
1686 +Add a Branch
1687 +----------------------------------------------------------------------
1688 +o Confirm the adding dir exists outside of aufs, including loopback
1689 +  mount, and its various attributes.
1690 +o Initialize the xino file and whiteout bases if necessary.
1691 +  See struct.txt.
1692 +
1693 +o Check the owner/group/mode of the directory
1694 +  When the owner/group/mode of the adding directory differs from the
1695 +  existing branch, aufs issues a warning because it may impose a
1696 +  security risk.
1697 +  For example, when a upper writable branch has a world writable empty
1698 +  top directory, a malicious user can create any files on the writable
1699 +  branch directly, like copy-up and modify manually. If something like
1700 +  /etc/{passwd,shadow} exists on the lower readonly branch but the upper
1701 +  writable branch, and the writable branch is world-writable, then a
1702 +  malicious guy may create /etc/passwd on the writable branch directly
1703 +  and the infected file will be valid in aufs.
1704 +  I am afraid it can be a security issue, but aufs can do nothing except
1705 +  producing a warning.
1706 +
1707 +
1708 +Delete a Branch
1709 +----------------------------------------------------------------------
1710 +o Confirm the deleting branch is not busy
1711 +  To be general, there is one merit to adopt "remount" interface to
1712 +  manipulate branches. It is to discard caches. At deleting a branch,
1713 +  aufs checks the still cached (and connected) dentries and inodes. If
1714 +  there are any, then they are all in-use. An inode without its
1715 +  corresponding dentry can be alive alone (for example, inotify/fsnotify case).
1716 +
1717 +  For the cached one, aufs checks whether the same named entry exists on
1718 +  other branches.
1719 +  If the cached one is a directory, because aufs provides a merged view
1720 +  to users, as long as one dir is left on any branch aufs can show the
1721 +  dir to users. In this case, the branch can be removed from aufs.
1722 +  Otherwise aufs rejects deleting the branch.
1723 +
1724 +  If any file on the deleting branch is opened by aufs, then aufs
1725 +  rejects deleting.
1726 +
1727 +
1728 +Modify the Permission of a Branch
1729 +----------------------------------------------------------------------
1730 +o Re-initialize or remove the xino file and whiteout bases if necessary.
1731 +  See struct.txt.
1732 +
1733 +o rw --> ro: Confirm the modifying branch is not busy
1734 +  Aufs rejects the request if any of these conditions are true.
1735 +  - a file on the branch is mmap-ed.
1736 +  - a regular file on the branch is opened for write and there is no
1737 +    same named entry on the upper branch.
1738 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/05wbr_policy.txt linux/Documentation/filesystems/aufs/design/05wbr_policy.txt
1739 --- /usr/share/empty/Documentation/filesystems/aufs/design/05wbr_policy.txt     1970-01-01 01:00:00.000000000 +0100
1740 +++ linux/Documentation/filesystems/aufs/design/05wbr_policy.txt        2022-03-21 14:49:05.713299677 +0100
1741 @@ -0,0 +1,64 @@
1742 +
1743 +# Copyright (C) 2005-2021 Junjiro R. Okajima
1744 +# 
1745 +# This program is free software; you can redistribute it and/or modify
1746 +# it under the terms of the GNU General Public License as published by
1747 +# the Free Software Foundation; either version 2 of the License, or
1748 +# (at your option) any later version.
1749 +# 
1750 +# This program is distributed in the hope that it will be useful,
1751 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1752 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1753 +# GNU General Public License for more details.
1754 +# 
1755 +# You should have received a copy of the GNU General Public License
1756 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1757 +
1758 +Policies to Select One among Multiple Writable Branches
1759 +----------------------------------------------------------------------
1760 +When the number of writable branch is more than one, aufs has to decide
1761 +the target branch for file creation or copy-up. By default, the highest
1762 +writable branch which has the parent (or ancestor) dir of the target
1763 +file is chosen (top-down-parent policy).
1764 +By user's request, aufs implements some other policies to select the
1765 +writable branch, for file creation several policies, round-robin,
1766 +most-free-space, and other policies. For copy-up, top-down-parent,
1767 +bottom-up-parent, bottom-up and others.
1768 +
1769 +As expected, the round-robin policy selects the branch in circular. When
1770 +you have two writable branches and creates 10 new files, 5 files will be
1771 +created for each branch. mkdir(2) systemcall is an exception. When you
1772 +create 10 new directories, all will be created on the same branch.
1773 +And the most-free-space policy selects the one which has most free
1774 +space among the writable branches. The amount of free space will be
1775 +checked by aufs internally, and users can specify its time interval.
1776 +
1777 +The policies for copy-up is more simple,
1778 +top-down-parent is equivalent to the same named on in create policy,
1779 +bottom-up-parent selects the writable branch where the parent dir
1780 +exists and the nearest upper one from the copyup-source,
1781 +bottom-up selects the nearest upper writable branch from the
1782 +copyup-source, regardless the existence of the parent dir.
1783 +
1784 +There are some rules or exceptions to apply these policies.
1785 +- If there is a readonly branch above the policy-selected branch and
1786 +  the parent dir is marked as opaque (a variation of whiteout), or the
1787 +  target (creating) file is whiteout-ed on the upper readonly branch,
1788 +  then the result of the policy is ignored and the target file will be
1789 +  created on the nearest upper writable branch than the readonly branch.
1790 +- If there is a writable branch above the policy-selected branch and
1791 +  the parent dir is marked as opaque or the target file is whiteouted
1792 +  on the branch, then the result of the policy is ignored and the target
1793 +  file will be created on the highest one among the upper writable
1794 +  branches who has diropq or whiteout. In case of whiteout, aufs removes
1795 +  it as usual.
1796 +- link(2) and rename(2) systemcalls are exceptions in every policy.
1797 +  They try selecting the branch where the source exists as possible
1798 +  since copyup a large file will take long time. If it can't be,
1799 +  ie. the branch where the source exists is readonly, then they will
1800 +  follow the copyup policy.
1801 +- There is an exception for rename(2) when the target exists.
1802 +  If the rename target exists, aufs compares the index of the branches
1803 +  where the source and the target exists and selects the higher
1804 +  one. If the selected branch is readonly, then aufs follows the
1805 +  copyup policy.
1806 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.dot linux/Documentation/filesystems/aufs/design/06dirren.dot
1807 --- /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.dot 1970-01-01 01:00:00.000000000 +0100
1808 +++ linux/Documentation/filesystems/aufs/design/06dirren.dot    2022-03-21 14:49:05.713299677 +0100
1809 @@ -0,0 +1,31 @@
1810 +
1811 +// to view this graph, run dot(1) command in GRAPHVIZ.
1812 +
1813 +digraph G {
1814 +node [shape=box];
1815 +whinfo [label="detailed info file\n(lower_brid_root-hinum, h_inum, namelen, old name)"];
1816 +
1817 +node [shape=oval];
1818 +
1819 +aufs_rename -> whinfo [label="store/remove"];
1820 +
1821 +node [shape=oval];
1822 +inode_list [label="h_inum list in branch\ncache"];
1823 +
1824 +node [shape=box];
1825 +whinode [label="h_inum list file"];
1826 +
1827 +node [shape=oval];
1828 +brmgmt [label="br_add/del/mod/umount"];
1829 +
1830 +brmgmt -> inode_list [label="create/remove"];
1831 +brmgmt -> whinode [label="load/store"];
1832 +
1833 +inode_list -> whinode [style=dashed,dir=both];
1834 +
1835 +aufs_rename -> inode_list [label="add/del"];
1836 +
1837 +aufs_lookup -> inode_list [label="search"];
1838 +
1839 +aufs_lookup -> whinfo [label="load/remove"];
1840 +}
1841 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.txt linux/Documentation/filesystems/aufs/design/06dirren.txt
1842 --- /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.txt 1970-01-01 01:00:00.000000000 +0100
1843 +++ linux/Documentation/filesystems/aufs/design/06dirren.txt    2022-03-21 14:49:05.713299677 +0100
1844 @@ -0,0 +1,102 @@
1845 +
1846 +# Copyright (C) 2017-2021 Junjiro R. Okajima
1847 +#
1848 +# This program is free software; you can redistribute it and/or modify
1849 +# it under the terms of the GNU General Public License as published by
1850 +# the Free Software Foundation; either version 2 of the License, or
1851 +# (at your option) any later version.
1852 +#
1853 +# This program is distributed in the hope that it will be useful,
1854 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1855 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1856 +# GNU General Public License for more details.
1857 +#
1858 +# You should have received a copy of the GNU General Public License
1859 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1860 +
1861 +Special handling for renaming a directory (DIRREN)
1862 +----------------------------------------------------------------------
1863 +First, let's assume we have a simple usecase.
1864 +
1865 +- /u = /rw + /ro
1866 +- /rw/dirA exists
1867 +- /ro/dirA and /ro/dirA/file exist too
1868 +- there is no dirB on both branches
1869 +- a user issues rename("dirA", "dirB")
1870 +
1871 +Now, what should aufs behave against this rename(2)?
1872 +There are a few possible cases.
1873 +
1874 +A. returns EROFS.
1875 +   since dirA exists on a readonly branch which cannot be renamed.
1876 +B. returns EXDEV.
1877 +   it is possible to copy-up dirA (only the dir itself), but the child
1878 +   entries ("file" in this case) should not be. it must be a bad
1879 +   approach to copy-up recursively.
1880 +C. returns a success.
1881 +   even the branch /ro is readonly, aufs tries renaming it. Obviously it
1882 +   is a violation of aufs' policy.
1883 +D. construct an extra information which indicates that /ro/dirA should
1884 +   be handled as the name of dirB.
1885 +   overlayfs has a similar feature called REDIRECT.
1886 +
1887 +Until now, aufs implements the case B only which returns EXDEV, and
1888 +expects the userspace application behaves like mv(1) which tries
1889 +issueing rename(2) recursively.
1890 +
1891 +A new aufs feature called DIRREN is introduced which implements the case
1892 +D. There are several "extra information" added.
1893 +
1894 +1. detailed info per renamed directory
1895 +   path: /rw/dirB/$AUFS_WH_DR_INFO_PFX.<lower branch-id>
1896 +2. the inode-number list of directories on a branch
1897 +   path: /rw/dirB/$AUFS_WH_DR_BRHINO
1898 +
1899 +The filename of "detailed info per directory" represents the lower
1900 +branch, and its format is
1901 +- a type of the branch id
1902 +  one of these.
1903 +  + uuid (not implemented yet)
1904 +  + fsid
1905 +  + dev
1906 +- the inode-number of the branch root dir
1907 +
1908 +And it contains these info in a single regular file.
1909 +- magic number
1910 +- branch's inode-number of the logically renamed dir
1911 +- the name of the before-renamed dir
1912 +
1913 +The "detailed info per directory" file is created in aufs rename(2), and
1914 +loaded in any lookup.
1915 +The info is considered in lookup for the matching case only. Here
1916 +"matching" means that the root of branch (in the info filename) is same
1917 +to the current looking-up branch. After looking-up the before-renamed
1918 +name, the inode-number is compared. And the matched dentry is used.
1919 +
1920 +The "inode-number list of directories" is a regular file which contains
1921 +simply the inode-numbers on the branch. The file is created or updated
1922 +in removing the branch, and loaded in adding the branch. Its lifetime is
1923 +equal to the branch.
1924 +The list is refered in lookup, and when the current target inode is
1925 +found in the list, the aufs tries loading the "detailed info per
1926 +directory" and get the changed and valid name of the dir.
1927 +
1928 +Theoretically these "extra informaiton" may be able to be put into XATTR
1929 +in the dir inode. But aufs doesn't choose this way because
1930 +1. XATTR may not be supported by the branch (or its configuration)
1931 +2. XATTR may have its size limit.
1932 +3. XATTR may be less easy to convert than a regular file, when the
1933 +   format of the info is changed in the future.
1934 +At the same time, I agree that the regular file approach is much slower
1935 +than XATTR approach. So, in the future, aufs may take the XATTR or other
1936 +better approach.
1937 +
1938 +This DIRREN feature is enabled by aufs configuration, and is activated
1939 +by a new mount option.
1940 +
1941 +For the more complicated case, there is a work with UDBA option, which
1942 +is to dected the direct access to the branches (by-passing aufs) and to
1943 +maintain the cashes in aufs. Since a single cached aufs dentry may
1944 +contains two names, before- and after-rename, the name comparision in
1945 +UDBA handler may not work correctly. In this case, the behaviour will be
1946 +equivalen to udba=reval case.
1947 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06fhsm.txt linux/Documentation/filesystems/aufs/design/06fhsm.txt
1948 --- /usr/share/empty/Documentation/filesystems/aufs/design/06fhsm.txt   1970-01-01 01:00:00.000000000 +0100
1949 +++ linux/Documentation/filesystems/aufs/design/06fhsm.txt      2022-03-21 14:49:05.716633010 +0100
1950 @@ -0,0 +1,120 @@
1951 +
1952 +# Copyright (C) 2011-2021 Junjiro R. Okajima
1953 +# 
1954 +# This program is free software; you can redistribute it and/or modify
1955 +# it under the terms of the GNU General Public License as published by
1956 +# the Free Software Foundation; either version 2 of the License, or
1957 +# (at your option) any later version.
1958 +# 
1959 +# This program is distributed in the hope that it will be useful,
1960 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1961 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1962 +# GNU General Public License for more details.
1963 +# 
1964 +# You should have received a copy of the GNU General Public License
1965 +# along with this program; if not, write to the Free Software
1966 +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
1967 +
1968 +
1969 +File-based Hierarchical Storage Management (FHSM)
1970 +----------------------------------------------------------------------
1971 +Hierarchical Storage Management (or HSM) is a well-known feature in the
1972 +storage world. Aufs provides this feature as file-based with multiple
1973 +writable branches, based upon the principle of "Colder, the Lower".
1974 +Here the word "colder" means that the less used files, and "lower" means
1975 +that the position in the order of the stacked branches vertically.
1976 +These multiple writable branches are prioritized, ie. the topmost one
1977 +should be the fastest drive and be used heavily.
1978 +
1979 +o Characters in aufs FHSM story
1980 +- aufs itself and a new branch attribute.
1981 +- a new ioctl interface to move-down and to establish a connection with
1982 +  the daemon ("move-down" is a converse of "copy-up").
1983 +- userspace tool and daemon.
1984 +
1985 +The userspace daemon establishes a connection with aufs and waits for
1986 +the notification. The notified information is very similar to struct
1987 +statfs containing the number of consumed blocks and inodes.
1988 +When the consumed blocks/inodes of a branch exceeds the user-specified
1989 +upper watermark, the daemon activates its move-down process until the
1990 +consumed blocks/inodes reaches the user-specified lower watermark.
1991 +
1992 +The actual move-down is done by aufs based upon the request from
1993 +user-space since we need to maintain the inode number and the internal
1994 +pointer arrays in aufs.
1995 +
1996 +Currently aufs FHSM handles the regular files only. Additionally they
1997 +must not be hard-linked nor pseudo-linked.
1998 +
1999 +
2000 +o Cowork of aufs and the user-space daemon
2001 +  During the userspace daemon established the connection, aufs sends a
2002 +  small notification to it whenever aufs writes something into the
2003 +  writable branch. But it may cost high since aufs issues statfs(2)
2004 +  internally. So user can specify a new option to cache the
2005 +  info. Actually the notification is controlled by these factors.
2006 +  + the specified cache time.
2007 +  + classified as "force" by aufs internally.
2008 +  Until the specified time expires, aufs doesn't send the info
2009 +  except the forced cases. When aufs decide forcing, the info is always
2010 +  notified to userspace.
2011 +  For example, the number of free inodes is generally large enough and
2012 +  the shortage of it happens rarely. So aufs doesn't force the
2013 +  notification when creating a new file, directory and others. This is
2014 +  the typical case which aufs doesn't force.
2015 +  When aufs writes the actual filedata and the files consumes any of new
2016 +  blocks, the aufs forces notifying.
2017 +
2018 +
2019 +o Interfaces in aufs
2020 +- New branch attribute.
2021 +  + fhsm
2022 +    Specifies that the branch is managed by FHSM feature. In other word,
2023 +    participant in the FHSM.
2024 +    When nofhsm is set to the branch, it will not be the source/target
2025 +    branch of the move-down operation. This attribute is set
2026 +    independently from coo and moo attributes, and if you want full
2027 +    FHSM, you should specify them as well.
2028 +- New mount option.
2029 +  + fhsm_sec
2030 +    Specifies a second to suppress many less important info to be
2031 +    notified.
2032 +- New ioctl.
2033 +  + AUFS_CTL_FHSM_FD
2034 +    create a new file descriptor which userspace can read the notification
2035 +    (a subset of struct statfs) from aufs.
2036 +- Module parameter 'brs'
2037 +  It has to be set to 1. Otherwise the new mount option 'fhsm' will not
2038 +  be set.
2039 +- mount helpers /sbin/mount.aufs and /sbin/umount.aufs
2040 +  When there are two or more branches with fhsm attributes,
2041 +  /sbin/mount.aufs invokes the user-space daemon and /sbin/umount.aufs
2042 +  terminates it. As a result of remounting and branch-manipulation, the
2043 +  number of branches with fhsm attribute can be one. In this case,
2044 +  /sbin/mount.aufs will terminate the user-space daemon.
2045 +
2046 +
2047 +Finally the operation is done as these steps in kernel-space.
2048 +- make sure that,
2049 +  + no one else is using the file.
2050 +  + the file is not hard-linked.
2051 +  + the file is not pseudo-linked.
2052 +  + the file is a regular file.
2053 +  + the parent dir is not opaqued.
2054 +- find the target writable branch.
2055 +- make sure the file is not whiteout-ed by the upper (than the target)
2056 +  branch.
2057 +- make the parent dir on the target branch.
2058 +- mutex lock the inode on the branch.
2059 +- unlink the whiteout on the target branch (if exists).
2060 +- lookup and create the whiteout-ed temporary name on the target branch.
2061 +- copy the file as the whiteout-ed temporary name on the target branch.
2062 +- rename the whiteout-ed temporary name to the original name.
2063 +- unlink the file on the source branch.
2064 +- maintain the internal pointer array and the external inode number
2065 +  table (XINO).
2066 +- maintain the timestamps and other attributes of the parent dir and the
2067 +  file.
2068 +
2069 +And of course, in every step, an error may happen. So the operation
2070 +should restore the original file state after an error happens.
2071 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06mmap.txt linux/Documentation/filesystems/aufs/design/06mmap.txt
2072 --- /usr/share/empty/Documentation/filesystems/aufs/design/06mmap.txt   1970-01-01 01:00:00.000000000 +0100
2073 +++ linux/Documentation/filesystems/aufs/design/06mmap.txt      2022-03-21 14:49:05.716633010 +0100
2074 @@ -0,0 +1,72 @@
2075 +
2076 +# Copyright (C) 2005-2021 Junjiro R. Okajima
2077 +# 
2078 +# This program is free software; you can redistribute it and/or modify
2079 +# it under the terms of the GNU General Public License as published by
2080 +# the Free Software Foundation; either version 2 of the License, or
2081 +# (at your option) any later version.
2082 +# 
2083 +# This program is distributed in the hope that it will be useful,
2084 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2085 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2086 +# GNU General Public License for more details.
2087 +# 
2088 +# You should have received a copy of the GNU General Public License
2089 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2090 +
2091 +mmap(2) -- File Memory Mapping
2092 +----------------------------------------------------------------------
2093 +In aufs, the file-mapped pages are handled by a branch fs directly, no
2094 +interaction with aufs. It means aufs_mmap() calls the branch fs's
2095 +->mmap().
2096 +This approach is simple and good, but there is one problem.
2097 +Under /proc, several entries show the mmapped files by its path (with
2098 +device and inode number), and the printed path will be the path on the
2099 +branch fs's instead of virtual aufs's.
2100 +This is not a problem in most cases, but some utilities lsof(1) (and its
2101 +user) may expect the path on aufs.
2102 +
2103 +To address this issue, aufs adds a new member called vm_prfile in struct
2104 +vm_area_struct (and struct vm_region). The original vm_file points to
2105 +the file on the branch fs in order to handle everything correctly as
2106 +usual. The new vm_prfile points to a virtual file in aufs, and the
2107 +show-functions in procfs refers to vm_prfile if it is set.
2108 +Also we need to maintain several other places where touching vm_file
2109 +such like
2110 +- fork()/clone() copies vma and the reference count of vm_file is
2111 +  incremented.
2112 +- merging vma maintains the ref count too.
2113 +
2114 +This is not a good approach. It just fakes the printed path. But it
2115 +leaves all behaviour around f_mapping unchanged. This is surely an
2116 +advantage.
2117 +Actually aufs had adopted another complicated approach which calls
2118 +generic_file_mmap() and handles struct vm_operations_struct. In this
2119 +approach, aufs met a hard problem and I could not solve it without
2120 +switching the approach.
2121 +
2122 +There may be one more another approach which is
2123 +- bind-mount the branch-root onto the aufs-root internally
2124 +- grab the new vfsmount (ie. struct mount)
2125 +- lazy-umount the branch-root internally
2126 +- in open(2) the aufs-file, open the branch-file with the hidden
2127 +  vfsmount (instead of the original branch's vfsmount)
2128 +- ideally this "bind-mount and lazy-umount" should be done atomically,
2129 +  but it may be possible from userspace by the mount helper.
2130 +
2131 +Adding the internal hidden vfsmount and using it in opening a file, the
2132 +file path under /proc will be printed correctly. This approach looks
2133 +smarter, but is not possible I am afraid.
2134 +- aufs-root may be bind-mount later. when it happens, another hidden
2135 +  vfsmount will be required.
2136 +- it is hard to get the chance to bind-mount and lazy-umount
2137 +  + in kernel-space, FS can have vfsmount in open(2) via
2138 +    file->f_path, and aufs can know its vfsmount. But several locks are
2139 +    already acquired, and if aufs tries to bind-mount and lazy-umount
2140 +    here, then it may cause a deadlock.
2141 +  + in user-space, bind-mount doesn't invoke the mount helper.
2142 +- since /proc shows dev and ino, aufs has to give vma these info. it
2143 +  means a new member vm_prinode will be necessary. this is essentially
2144 +  equivalent to vm_prfile described above.
2145 +
2146 +I have to give up this "looks-smater" approach.
2147 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06xattr.txt linux/Documentation/filesystems/aufs/design/06xattr.txt
2148 --- /usr/share/empty/Documentation/filesystems/aufs/design/06xattr.txt  1970-01-01 01:00:00.000000000 +0100
2149 +++ linux/Documentation/filesystems/aufs/design/06xattr.txt     2022-03-21 14:49:05.716633010 +0100
2150 @@ -0,0 +1,96 @@
2151 +
2152 +# Copyright (C) 2014-2021 Junjiro R. Okajima
2153 +#
2154 +# This program is free software; you can redistribute it and/or modify
2155 +# it under the terms of the GNU General Public License as published by
2156 +# the Free Software Foundation; either version 2 of the License, or
2157 +# (at your option) any later version.
2158 +#
2159 +# This program is distributed in the hope that it will be useful,
2160 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2161 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2162 +# GNU General Public License for more details.
2163 +#
2164 +# You should have received a copy of the GNU General Public License
2165 +# along with this program; if not, write to the Free Software
2166 +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
2167 +
2168 +
2169 +Listing XATTR/EA and getting the value
2170 +----------------------------------------------------------------------
2171 +For the inode standard attributes (owner, group, timestamps, etc.), aufs
2172 +shows the values from the topmost existing file. This behaviour is good
2173 +for the non-dir entries since the bahaviour exactly matches the shown
2174 +information. But for the directories, aufs considers all the same named
2175 +entries on the lower branches. Which means, if one of the lower entry
2176 +rejects readdir call, then aufs returns an error even if the topmost
2177 +entry allows it. This behaviour is necessary to respect the branch fs's
2178 +security, but can make users confused since the user-visible standard
2179 +attributes don't match the behaviour.
2180 +To address this issue, aufs has a mount option called dirperm1 which
2181 +checks the permission for the topmost entry only, and ignores the lower
2182 +entry's permission.
2183 +
2184 +A similar issue can happen around XATTR.
2185 +getxattr(2) and listxattr(2) families behave as if dirperm1 option is
2186 +always set. Otherwise these very unpleasant situation would happen.
2187 +- listxattr(2) may return the duplicated entries.
2188 +- users may not be able to remove or reset the XATTR forever,
2189 +
2190 +
2191 +XATTR/EA support in the internal (copy,move)-(up,down)
2192 +----------------------------------------------------------------------
2193 +Generally the extended attributes of inode are categorized as these.
2194 +- "security" for LSM and capability.
2195 +- "system" for posix ACL, 'acl' mount option is required for the branch
2196 +  fs generally.
2197 +- "trusted" for userspace, CAP_SYS_ADMIN is required.
2198 +- "user" for userspace, 'user_xattr' mount option is required for the
2199 +  branch fs generally.
2200 +
2201 +Moreover there are some other categories. Aufs handles these rather
2202 +unpopular categories as the ordinary ones, ie. there is no special
2203 +condition nor exception.
2204 +
2205 +In copy-up, the support for XATTR on the dst branch may differ from the
2206 +src branch. In this case, the copy-up operation will get an error and
2207 +the original user operation which triggered the copy-up will fail. It
2208 +can happen that even all copy-up will fail.
2209 +When both of src and dst branches support XATTR and if an error occurs
2210 +during copying XATTR, then the copy-up should fail obviously. That is a
2211 +good reason and aufs should return an error to userspace. But when only
2212 +the src branch support that XATTR, aufs should not return an error.
2213 +For example, the src branch supports ACL but the dst branch doesn't
2214 +because the dst branch may natively un-support it or temporary
2215 +un-support it due to "noacl" mount option. Of course, the dst branch fs
2216 +may NOT return an error even if the XATTR is not supported. It is
2217 +totally up to the branch fs.
2218 +
2219 +Anyway when the aufs internal copy-up gets an error from the dst branch
2220 +fs, then aufs tries removing the just copied entry and returns the error
2221 +to the userspace. The worst case of this situation will be all copy-up
2222 +will fail.
2223 +
2224 +For the copy-up operation, there two basic approaches.
2225 +- copy the specified XATTR only (by category above), and return the
2226 +  error unconditionally if it happens.
2227 +- copy all XATTR, and ignore the error on the specified category only.
2228 +
2229 +In order to support XATTR and to implement the correct behaviour, aufs
2230 +chooses the latter approach and introduces some new branch attributes,
2231 +"icexsec", "icexsys", "icextr", "icexusr", and "icexoth".
2232 +They correspond to the XATTR namespaces (see above). Additionally, to be
2233 +convenient, "icex" is also provided which means all "icex*" attributes
2234 +are set (here the word "icex" stands for "ignore copy-error on XATTR").
2235 +
2236 +The meaning of these attributes is to ignore the error from setting
2237 +XATTR on that branch.
2238 +Note that aufs tries copying all XATTR unconditionally, and ignores the
2239 +error from the dst branch according to the specified attributes.
2240 +
2241 +Some XATTR may have its default value. The default value may come from
2242 +the parent dir or the environment. If the default value is set at the
2243 +file creating-time, it will be overwritten by copy-up.
2244 +Some contradiction may happen I am afraid.
2245 +Do we need another attribute to stop copying XATTR? I am unsure. For
2246 +now, aufs implements the branch attributes to ignore the error.
2247 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/07export.txt linux/Documentation/filesystems/aufs/design/07export.txt
2248 --- /usr/share/empty/Documentation/filesystems/aufs/design/07export.txt 1970-01-01 01:00:00.000000000 +0100
2249 +++ linux/Documentation/filesystems/aufs/design/07export.txt    2022-03-21 14:49:05.716633010 +0100
2250 @@ -0,0 +1,58 @@
2251 +
2252 +# Copyright (C) 2005-2021 Junjiro R. Okajima
2253 +# 
2254 +# This program is free software; you can redistribute it and/or modify
2255 +# it under the terms of the GNU General Public License as published by
2256 +# the Free Software Foundation; either version 2 of the License, or
2257 +# (at your option) any later version.
2258 +# 
2259 +# This program is distributed in the hope that it will be useful,
2260 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2261 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2262 +# GNU General Public License for more details.
2263 +# 
2264 +# You should have received a copy of the GNU General Public License
2265 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2266 +
2267 +Export Aufs via NFS
2268 +----------------------------------------------------------------------
2269 +Here is an approach.
2270 +- like xino/xib, add a new file 'xigen' which stores aufs inode
2271 +  generation.
2272 +- iget_locked(): initialize aufs inode generation for a new inode, and
2273 +  store it in xigen file.
2274 +- destroy_inode(): increment aufs inode generation and store it in xigen
2275 +  file. it is necessary even if it is not unlinked, because any data of
2276 +  inode may be changed by UDBA.
2277 +- encode_fh(): for a root dir, simply return FILEID_ROOT. otherwise
2278 +  build file handle by
2279 +  + branch id (4 bytes)
2280 +  + superblock generation (4 bytes)
2281 +  + inode number (4 or 8 bytes)
2282 +  + parent dir inode number (4 or 8 bytes)
2283 +  + inode generation (4 bytes))
2284 +  + return value of exportfs_encode_fh() for the parent on a branch (4
2285 +    bytes)
2286 +  + file handle for a branch (by exportfs_encode_fh())
2287 +- fh_to_dentry():
2288 +  + find the index of a branch from its id in handle, and check it is
2289 +    still exist in aufs.
2290 +  + 1st level: get the inode number from handle and search it in cache.
2291 +  + 2nd level: if not found in cache, get the parent inode number from
2292 +    the handle and search it in cache. and then open the found parent
2293 +    dir, find the matching inode number by vfs_readdir() and get its
2294 +    name, and call lookup_one_len() for the target dentry.
2295 +  + 3rd level: if the parent dir is not cached, call
2296 +    exportfs_decode_fh() for a branch and get the parent on a branch,
2297 +    build a pathname of it, convert it a pathname in aufs, call
2298 +    path_lookup(). now aufs gets a parent dir dentry, then handle it as
2299 +    the 2nd level.
2300 +  + to open the dir, aufs needs struct vfsmount. aufs keeps vfsmount
2301 +    for every branch, but not itself. to get this, (currently) aufs
2302 +    searches in current->nsproxy->mnt_ns list. it may not be a good
2303 +    idea, but I didn't get other approach.
2304 +  + test the generation of the gotten inode.
2305 +- every inode operation: they may get EBUSY due to UDBA. in this case,
2306 +  convert it into ESTALE for NFSD.
2307 +- readdir(): call lockdep_on/off() because filldir in NFSD calls
2308 +  lookup_one_len(), vfs_getattr(), encode_fh() and others.
2309 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/08shwh.txt linux/Documentation/filesystems/aufs/design/08shwh.txt
2310 --- /usr/share/empty/Documentation/filesystems/aufs/design/08shwh.txt   1970-01-01 01:00:00.000000000 +0100
2311 +++ linux/Documentation/filesystems/aufs/design/08shwh.txt      2022-03-21 14:49:05.716633010 +0100
2312 @@ -0,0 +1,52 @@
2313 +
2314 +# Copyright (C) 2005-2021 Junjiro R. Okajima
2315 +# 
2316 +# This program is free software; you can redistribute it and/or modify
2317 +# it under the terms of the GNU General Public License as published by
2318 +# the Free Software Foundation; either version 2 of the License, or
2319 +# (at your option) any later version.
2320 +# 
2321 +# This program is distributed in the hope that it will be useful,
2322 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2323 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2324 +# GNU General Public License for more details.
2325 +# 
2326 +# You should have received a copy of the GNU General Public License
2327 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2328 +
2329 +Show Whiteout Mode (shwh)
2330 +----------------------------------------------------------------------
2331 +Generally aufs hides the name of whiteouts. But in some cases, to show
2332 +them is very useful for users. For instance, creating a new middle layer
2333 +(branch) by merging existing layers.
2334 +
2335 +(borrowing aufs1 HOW-TO from a user, Michael Towers)
2336 +When you have three branches,
2337 +- Bottom: 'system', squashfs (underlying base system), read-only
2338 +- Middle: 'mods', squashfs, read-only
2339 +- Top: 'overlay', ram (tmpfs), read-write
2340 +
2341 +The top layer is loaded at boot time and saved at shutdown, to preserve
2342 +the changes made to the system during the session.
2343 +When larger changes have been made, or smaller changes have accumulated,
2344 +the size of the saved top layer data grows. At this point, it would be
2345 +nice to be able to merge the two overlay branches ('mods' and 'overlay')
2346 +and rewrite the 'mods' squashfs, clearing the top layer and thus
2347 +restoring save and load speed.
2348 +
2349 +This merging is simplified by the use of another aufs mount, of just the
2350 +two overlay branches using the 'shwh' option.
2351 +# mount -t aufs -o ro,shwh,br:/livesys/overlay=ro+wh:/livesys/mods=rr+wh \
2352 +       aufs /livesys/merge_union
2353 +
2354 +A merged view of these two branches is then available at
2355 +/livesys/merge_union, and the new feature is that the whiteouts are
2356 +visible!
2357 +Note that in 'shwh' mode the aufs mount must be 'ro', which will disable
2358 +writing to all branches. Also the default mode for all branches is 'ro'.
2359 +It is now possible to save the combined contents of the two overlay
2360 +branches to a new squashfs, e.g.:
2361 +# mksquashfs /livesys/merge_union /path/to/newmods.squash
2362 +
2363 +This new squashfs archive can be stored on the boot device and the
2364 +initramfs will use it to replace the old one at the next boot.
2365 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/10dynop.txt linux/Documentation/filesystems/aufs/design/10dynop.txt
2366 --- /usr/share/empty/Documentation/filesystems/aufs/design/10dynop.txt  1970-01-01 01:00:00.000000000 +0100
2367 +++ linux/Documentation/filesystems/aufs/design/10dynop.txt     2022-03-21 14:49:05.716633010 +0100
2368 @@ -0,0 +1,47 @@
2369 +
2370 +# Copyright (C) 2010-2021 Junjiro R. Okajima
2371 +#
2372 +# This program is free software; you can redistribute it and/or modify
2373 +# it under the terms of the GNU General Public License as published by
2374 +# the Free Software Foundation; either version 2 of the License, or
2375 +# (at your option) any later version.
2376 +#
2377 +# This program is distributed in the hope that it will be useful,
2378 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2379 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2380 +# GNU General Public License for more details.
2381 +#
2382 +# You should have received a copy of the GNU General Public License
2383 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2384 +
2385 +Dynamically customizable FS operations
2386 +----------------------------------------------------------------------
2387 +Generally FS operations (struct inode_operations, struct
2388 +address_space_operations, struct file_operations, etc.) are defined as
2389 +"static const", but it never means that FS have only one set of
2390 +operation. Some FS have multiple sets of them. For instance, ext2 has
2391 +three sets, one for XIP, for NOBH, and for normal.
2392 +Since aufs overrides and redirects these operations, sometimes aufs has
2393 +to change its behaviour according to the branch FS type. More importantly
2394 +VFS acts differently if a function (member in the struct) is set or
2395 +not. It means aufs should have several sets of operations and select one
2396 +among them according to the branch FS definition.
2397 +
2398 +In order to solve this problem and not to affect the behaviour of VFS,
2399 +aufs defines these operations dynamically. For instance, aufs defines
2400 +dummy direct_IO function for struct address_space_operations, but it may
2401 +not be set to the address_space_operations actually. When the branch FS
2402 +doesn't have it, aufs doesn't set it to its address_space_operations
2403 +while the function definition itself is still alive. So the behaviour
2404 +itself will not change, and it will return an error when direct_IO is
2405 +not set.
2406 +
2407 +The lifetime of these dynamically generated operation object is
2408 +maintained by aufs branch object. When the branch is removed from aufs,
2409 +the reference counter of the object is decremented. When it reaches
2410 +zero, the dynamically generated operation object will be freed.
2411 +
2412 +This approach is designed to support AIO (io_submit), Direct I/O and
2413 +XIP (DAX) mainly.
2414 +Currently this approach is applied to address_space_operations for
2415 +regular files only.
2416 diff -urN /usr/share/empty/Documentation/filesystems/aufs/README linux/Documentation/filesystems/aufs/README
2417 --- /usr/share/empty/Documentation/filesystems/aufs/README      1970-01-01 01:00:00.000000000 +0100
2418 +++ linux/Documentation/filesystems/aufs/README 2022-03-21 14:49:05.713299677 +0100
2419 @@ -0,0 +1,396 @@
2420 +
2421 +Aufs5 -- advanced multi layered unification filesystem version 5.x
2422 +http://aufs.sf.net
2423 +Junjiro R. Okajima
2424 +
2425 +
2426 +0. Introduction
2427 +----------------------------------------
2428 +In the early days, aufs was entirely re-designed and re-implemented
2429 +Unionfs Version 1.x series. Adding many original ideas, approaches,
2430 +improvements and implementations, it became totally different from
2431 +Unionfs while keeping the basic features.
2432 +Later, Unionfs Version 2.x series began taking some of the same
2433 +approaches to aufs1's.
2434 +Unionfs was being developed by Professor Erez Zadok at Stony Brook
2435 +University and his team.
2436 +
2437 +Aufs5 supports linux-v5.0 and later, If you want older kernel version
2438 +support,
2439 +- for linux-v4.x series, try aufs4-linux.git or aufs4-standalone.git
2440 +- for linux-v3.x series, try aufs3-linux.git or aufs3-standalone.git
2441 +- for linux-v2.6.16 and later, try aufs2-2.6.git, aufs2-standalone.git
2442 +  or aufs1 from CVS on SourceForge.
2443 +
2444 +Note: it becomes clear that "Aufs was rejected. Let's give it up."
2445 +      According to Christoph Hellwig, linux rejects all union-type
2446 +      filesystems but UnionMount.
2447 +<http://marc.info/?l=linux-kernel&m=123938533724484&w=2>
2448 +
2449 +PS. Al Viro seems have a plan to merge aufs as well as overlayfs and
2450 +    UnionMount, and he pointed out an issue around a directory mutex
2451 +    lock and aufs addressed it. But it is still unsure whether aufs will
2452 +    be merged (or any other union solution).
2453 +<http://marc.info/?l=linux-kernel&m=136312705029295&w=1>
2454 +
2455 +
2456 +1. Features
2457 +----------------------------------------
2458 +- unite several directories into a single virtual filesystem. The member
2459 +  directory is called as a branch.
2460 +- you can specify the permission flags to the branch, which are 'readonly',
2461 +  'readwrite' and 'whiteout-able.'
2462 +- by upper writable branch, internal copyup and whiteout, files/dirs on
2463 +  readonly branch are modifiable logically.
2464 +- dynamic branch manipulation, add, del.
2465 +- etc...
2466 +
2467 +Also there are many enhancements in aufs, such as:
2468 +- test only the highest one for the directory permission (dirperm1)
2469 +- copyup on open (coo=)
2470 +- 'move' policy for copy-up between two writable branches, after
2471 +  checking free space.
2472 +- xattr, acl
2473 +- readdir(3) in userspace.
2474 +- keep inode number by external inode number table
2475 +- keep the timestamps of file/dir in internal copyup operation
2476 +- seekable directory, supporting NFS readdir.
2477 +- whiteout is hardlinked in order to reduce the consumption of inodes
2478 +  on branch
2479 +- do not copyup, nor create a whiteout when it is unnecessary
2480 +- revert a single systemcall when an error occurs in aufs
2481 +- remount interface instead of ioctl
2482 +- maintain /etc/mtab by an external command, /sbin/mount.aufs.
2483 +- loopback mounted filesystem as a branch
2484 +- kernel thread for removing the dir who has a plenty of whiteouts
2485 +- support copyup sparse file (a file which has a 'hole' in it)
2486 +- default permission flags for branches
2487 +- selectable permission flags for ro branch, whether whiteout can
2488 +  exist or not
2489 +- export via NFS.
2490 +- support <sysfs>/fs/aufs and <debugfs>/aufs.
2491 +- support multiple writable branches, some policies to select one
2492 +  among multiple writable branches.
2493 +- a new semantics for link(2) and rename(2) to support multiple
2494 +  writable branches.
2495 +- no glibc changes are required.
2496 +- pseudo hardlink (hardlink over branches)
2497 +- allow a direct access manually to a file on branch, e.g. bypassing aufs.
2498 +  including NFS or remote filesystem branch.
2499 +- userspace wrapper for pathconf(3)/fpathconf(3) with _PC_LINK_MAX.
2500 +- and more...
2501 +
2502 +Currently these features are dropped temporary from aufs5.
2503 +See design/08plan.txt in detail.
2504 +- nested mount, i.e. aufs as readonly no-whiteout branch of another aufs
2505 +  (robr)
2506 +- statistics of aufs thread (/sys/fs/aufs/stat)
2507 +
2508 +Features or just an idea in the future (see also design/*.txt),
2509 +- reorder the branch index without del/re-add.
2510 +- permanent xino files for NFSD
2511 +- an option for refreshing the opened files after add/del branches
2512 +- light version, without branch manipulation. (unnecessary?)
2513 +- copyup in userspace
2514 +- inotify in userspace
2515 +- readv/writev
2516 +
2517 +
2518 +2. Download
2519 +----------------------------------------
2520 +There are three GIT trees for aufs5, aufs5-linux.git,
2521 +aufs5-standalone.git, and aufs-util.git. Note that there is no "5" in
2522 +"aufs-util.git."
2523 +While the aufs-util is always necessary, you need either of aufs5-linux
2524 +or aufs5-standalone.
2525 +
2526 +The aufs5-linux tree includes the whole linux mainline GIT tree,
2527 +git://git.kernel.org/.../torvalds/linux.git.
2528 +And you cannot select CONFIG_AUFS_FS=m for this version, eg. you cannot
2529 +build aufs5 as an external kernel module.
2530 +Several extra patches are not included in this tree. Only
2531 +aufs5-standalone tree contains them. They are described in the later
2532 +section "Configuration and Compilation."
2533 +
2534 +On the other hand, the aufs5-standalone tree has only aufs source files
2535 +and necessary patches, and you can select CONFIG_AUFS_FS=m.
2536 +But you need to apply all aufs patches manually.
2537 +
2538 +You will find GIT branches whose name is in form of "aufs5.x" where "x"
2539 +represents the linux kernel version, "linux-5.x". For instance,
2540 +"aufs5.0" is for linux-5.0. For latest "linux-5.x-rcN", use
2541 +"aufs5.x-rcN" branch.
2542 +
2543 +o aufs5-linux tree
2544 +$ git clone --reference /your/linux/git/tree \
2545 +       git://github.com/sfjro/aufs5-linux.git aufs5-linux.git
2546 +- if you don't have linux GIT tree, then remove "--reference ..."
2547 +$ cd aufs5-linux.git
2548 +$ git checkout origin/aufs5.0
2549 +
2550 +Or You may want to directly git-pull aufs into your linux GIT tree, and
2551 +leave the patch-work to GIT.
2552 +$ cd /your/linux/git/tree
2553 +$ git remote add aufs5 git://github.com/sfjro/aufs5-linux.git
2554 +$ git fetch aufs5
2555 +$ git checkout -b my5.0 v5.0
2556 +$ (add your local change...)
2557 +$ git pull aufs5 aufs5.0
2558 +- now you have v5.0 + your_changes + aufs5.0 in you my5.0 branch.
2559 +- you may need to solve some conflicts between your_changes and
2560 +  aufs5.0. in this case, git-rerere is recommended so that you can
2561 +  solve the similar conflicts automatically when you upgrade to 5.1 or
2562 +  later in the future.
2563 +
2564 +o aufs5-standalone tree
2565 +$ git clone git://github.com/sfjro/aufs5-standalone.git aufs5-standalone.git
2566 +$ cd aufs5-standalone.git
2567 +$ git checkout origin/aufs5.0
2568 +
2569 +o aufs-util tree
2570 +$ git clone git://git.code.sf.net/p/aufs/aufs-util aufs-util.git
2571 +- note that the public aufs-util.git is on SourceForge instead of
2572 +  GitHUB.
2573 +$ cd aufs-util.git
2574 +$ git checkout origin/aufs5.0
2575 +
2576 +Note: The 5.x-rcN branch is to be used with `rc' kernel versions ONLY.
2577 +The minor version number, 'x' in '5.x', of aufs may not always
2578 +follow the minor version number of the kernel.
2579 +Because changes in the kernel that cause the use of a new
2580 +minor version number do not always require changes to aufs-util.
2581 +
2582 +Since aufs-util has its own minor version number, you may not be
2583 +able to find a GIT branch in aufs-util for your kernel's
2584 +exact minor version number.
2585 +In this case, you should git-checkout the branch for the
2586 +nearest lower number.
2587 +
2588 +For (an unreleased) example:
2589 +If you are using "linux-5.10" and the "aufs5.10" branch
2590 +does not exist in aufs-util repository, then "aufs5.9", "aufs5.8"
2591 +or something numerically smaller is the branch for your kernel.
2592 +
2593 +Also you can view all branches by
2594 +       $ git branch -a
2595 +
2596 +
2597 +3. Configuration and Compilation
2598 +----------------------------------------
2599 +Make sure you have git-checkout'ed the correct branch.
2600 +
2601 +For aufs5-linux tree,
2602 +- enable CONFIG_AUFS_FS.
2603 +- set other aufs configurations if necessary.
2604 +- for aufs5.13 and later
2605 +  Because aufs is not only an ordinary filesystem (callee of VFS), but
2606 +  also a caller of VFS functions for branch filesystems, subclassing of
2607 +  the internal locks for LOCKDEP is necessary. LOCKDEP is a debugging
2608 +  feature of linux kernel. If you enable CONFIG_LOCKDEP, then you will
2609 +  need to customize some LOCKDEP numbers. Here are what I use on my
2610 +  test environment.
2611 +       CONFIG_LOCKDEP_BITS=21
2612 +       CONFIG_LOCKDEP_CHAINS_BITS=21
2613 +       CONFIG_LOCKDEP_STACK_TRACE_BITS=24
2614 +
2615 +For aufs5-standalone tree,
2616 +There are several ways to build.
2617 +
2618 +1.
2619 +- apply ./aufs5-kbuild.patch to your kernel source files.
2620 +- apply ./aufs5-base.patch too.
2621 +- apply ./aufs5-mmap.patch too.
2622 +- apply ./aufs5-standalone.patch too, if you have a plan to set
2623 +  CONFIG_AUFS_FS=m. otherwise you don't need ./aufs5-standalone.patch.
2624 +- copy ./{Documentation,fs,include/uapi/linux/aufs_type.h} files to your
2625 +  kernel source tree. Never copy $PWD/include/uapi/linux/Kbuild.
2626 +- enable CONFIG_AUFS_FS, you can select either
2627 +  =m or =y.
2628 +- and build your kernel as usual.
2629 +- install the built kernel.
2630 +- install the header files too by "make headers_install" to the
2631 +  directory where you specify. By default, it is $PWD/usr.
2632 +  "make help" shows a brief note for headers_install.
2633 +- and reboot your system.
2634 +
2635 +2.
2636 +- module only (CONFIG_AUFS_FS=m).
2637 +- apply ./aufs5-base.patch to your kernel source files.
2638 +- apply ./aufs5-mmap.patch too.
2639 +- apply ./aufs5-standalone.patch too.
2640 +- build your kernel, don't forget "make headers_install", and reboot.
2641 +- edit ./config.mk and set other aufs configurations if necessary.
2642 +  Note: You should read $PWD/fs/aufs/Kconfig carefully which describes
2643 +  every aufs configurations.
2644 +- build the module by simple "make".
2645 +- you can specify ${KDIR} make variable which points to your kernel
2646 +  source tree.
2647 +- install the files
2648 +  + run "make install" to install the aufs module, or copy the built
2649 +    $PWD/aufs.ko to /lib/modules/... and run depmod -a (or reboot simply).
2650 +  + run "make install_headers" (instead of headers_install) to install
2651 +    the modified aufs header file (you can specify DESTDIR which is
2652 +    available in aufs standalone version's Makefile only), or copy
2653 +    $PWD/usr/include/linux/aufs_type.h to /usr/include/linux or wherever
2654 +    you like manually. By default, the target directory is $PWD/usr.
2655 +- no need to apply aufs5-kbuild.patch, nor copying source files to your
2656 +  kernel source tree.
2657 +
2658 +Note: The header file aufs_type.h is necessary to build aufs-util
2659 +      as well as "make headers_install" in the kernel source tree.
2660 +      headers_install is subject to be forgotten, but it is essentially
2661 +      necessary, not only for building aufs-util.
2662 +      You may not meet problems without headers_install in some older
2663 +      version though.
2664 +
2665 +And then,
2666 +- read README in aufs-util, build and install it
2667 +- note that your distribution may contain an obsoleted version of
2668 +  aufs_type.h in /usr/include/linux or something. When you build aufs
2669 +  utilities, make sure that your compiler refers the correct aufs header
2670 +  file which is built by "make headers_install."
2671 +- if you want to use readdir(3) in userspace or pathconf(3) wrapper,
2672 +  then run "make install_ulib" too. And refer to the aufs manual in
2673 +  detail.
2674 +
2675 +There several other patches in aufs5-standalone.git. They are all
2676 +optional. When you meet some problems, they will help you.
2677 +- aufs5-loopback.patch
2678 +  Supports a nested loopback mount in a branch-fs. This patch is
2679 +  unnecessary until aufs produces a message like "you may want to try
2680 +  another patch for loopback file".
2681 +- vfs-ino.patch
2682 +  Modifies a system global kernel internal function get_next_ino() in
2683 +  order to stop assigning 0 for an inode-number. Not directly related to
2684 +  aufs, but recommended generally.
2685 +- tmpfs-idr.patch
2686 +  Keeps the tmpfs inode number as the lowest value. Effective to reduce
2687 +  the size of aufs XINO files for tmpfs branch. Also it prevents the
2688 +  duplication of inode number, which is important for backup tools and
2689 +  other utilities. When you find aufs XINO files for tmpfs branch
2690 +  growing too much, try this patch.
2691 +
2692 +
2693 +4. Usage
2694 +----------------------------------------
2695 +At first, make sure aufs-util are installed, and please read the aufs
2696 +manual, aufs.5 in aufs-util.git tree.
2697 +$ man -l aufs.5
2698 +
2699 +And then,
2700 +$ mkdir /tmp/rw /tmp/aufs
2701 +# mount -t aufs -o br=/tmp/rw:${HOME} none /tmp/aufs
2702 +
2703 +Here is another example. The result is equivalent.
2704 +# mount -t aufs -o br=/tmp/rw=rw:${HOME}=ro none /tmp/aufs
2705 +  Or
2706 +# mount -t aufs -o br:/tmp/rw none /tmp/aufs
2707 +# mount -o remount,append:${HOME} /tmp/aufs
2708 +
2709 +Then, you can see whole tree of your home dir through /tmp/aufs. If
2710 +you modify a file under /tmp/aufs, the one on your home directory is
2711 +not affected, instead the same named file will be newly created under
2712 +/tmp/rw. And all of your modification to a file will be applied to
2713 +the one under /tmp/rw. This is called the file based Copy on Write
2714 +(COW) method.
2715 +Aufs mount options are described in aufs.5.
2716 +If you run chroot or something and make your aufs as a root directory,
2717 +then you need to customize the shutdown script. See the aufs manual in
2718 +detail.
2719 +
2720 +Additionally, there are some sample usages of aufs which are a
2721 +diskless system with network booting, and LiveCD over NFS.
2722 +See sample dir in CVS tree on SourceForge.
2723 +
2724 +
2725 +5. Contact
2726 +----------------------------------------
2727 +When you have any problems or strange behaviour in aufs, please let me
2728 +know with:
2729 +- /proc/mounts (instead of the output of mount(8))
2730 +- /sys/module/aufs/*
2731 +- /sys/fs/aufs/* (if you have them)
2732 +- /debug/aufs/* (if you have them)
2733 +- linux kernel version
2734 +  if your kernel is not plain, for example modified by distributor,
2735 +  the url where i can download its source is necessary too.
2736 +- aufs version which was printed at loading the module or booting the
2737 +  system, instead of the date you downloaded.
2738 +- configuration (define/undefine CONFIG_AUFS_xxx)
2739 +- kernel configuration or /proc/config.gz (if you have it)
2740 +- LSM (linux security module, if you are using)
2741 +- behaviour which you think to be incorrect
2742 +- actual operation, reproducible one is better
2743 +- mailto: aufs-users at lists.sourceforge.net
2744 +
2745 +Usually, I don't watch the Public Areas(Bugs, Support Requests, Patches,
2746 +and Feature Requests) on SourceForge. Please join and write to
2747 +aufs-users ML.
2748 +
2749 +
2750 +6. Acknowledgements
2751 +----------------------------------------
2752 +Thanks to everyone who have tried and are using aufs, whoever
2753 +have reported a bug or any feedback.
2754 +
2755 +Especially donators:
2756 +Tomas Matejicek(slax.org) made a donation (much more than once).
2757 +       Since Apr 2010, Tomas M (the author of Slax and Linux Live
2758 +       scripts) is making "doubling" donations.
2759 +       Unfortunately I cannot list all of the donators, but I really
2760 +       appreciate.
2761 +       It ends Aug 2010, but the ordinary donation URL is still available.
2762 +       <http://sourceforge.net/donate/index.php?group_id=167503>
2763 +Dai Itasaka made a donation (2007/8).
2764 +Chuck Smith made a donation (2008/4, 10 and 12).
2765 +Henk Schoneveld made a donation (2008/9).
2766 +Chih-Wei Huang, ASUS, CTC donated Eee PC 4G (2008/10).
2767 +Francois Dupoux made a donation (2008/11).
2768 +Bruno Cesar Ribas and Luis Carlos Erpen de Bona, C3SL serves public
2769 +       aufs2 GIT tree (2009/2).
2770 +William Grant made a donation (2009/3).
2771 +Patrick Lane made a donation (2009/4).
2772 +The Mail Archive (mail-archive.com) made donations (2009/5).
2773 +Nippy Networks (Ed Wildgoose) made a donation (2009/7).
2774 +New Dream Network, LLC (www.dreamhost.com) made a donation (2009/11).
2775 +Pavel Pronskiy made a donation (2011/2).
2776 +Iridium and Inmarsat satellite phone retailer (www.mailasail.com), Nippy
2777 +       Networks (Ed Wildgoose) made a donation for hardware (2011/3).
2778 +Max Lekomcev (DOM-TV project) made a donation (2011/7, 12, 2012/3, 6 and
2779 +11).
2780 +Sam Liddicott made a donation (2011/9).
2781 +Era Scarecrow made a donation (2013/4).
2782 +Bor Ratajc made a donation (2013/4).
2783 +Alessandro Gorreta made a donation (2013/4).
2784 +POIRETTE Marc made a donation (2013/4).
2785 +Alessandro Gorreta made a donation (2013/4).
2786 +lauri kasvandik made a donation (2013/5).
2787 +"pemasu from Finland" made a donation (2013/7).
2788 +The Parted Magic Project made a donation (2013/9 and 11).
2789 +Pavel Barta made a donation (2013/10).
2790 +Nikolay Pertsev made a donation (2014/5).
2791 +James B made a donation (2014/7, 2015/7, and 2021/12).
2792 +Stefano Di Biase made a donation (2014/8).
2793 +Daniel Epellei made a donation (2015/1).
2794 +OmegaPhil made a donation (2016/1, 2018/4).
2795 +Tomasz Szewczyk made a donation (2016/4).
2796 +James Burry made a donation (2016/12).
2797 +Carsten Rose made a donation (2018/9).
2798 +Porteus Kiosk made a donation (2018/10).
2799 +
2800 +Thank you very much.
2801 +Donations are always, including future donations, very important and
2802 +helpful for me to keep on developing aufs.
2803 +
2804 +
2805 +7.
2806 +----------------------------------------
2807 +If you are an experienced user, no explanation is needed. Aufs is
2808 +just a linux filesystem.
2809 +
2810 +
2811 +Enjoy!
2812 +
2813 +# Local variables: ;
2814 +# mode: text;
2815 +# End: ;
2816 diff -urN /usr/share/empty/fs/aufs/aufs.h linux/fs/aufs/aufs.h
2817 --- /usr/share/empty/fs/aufs/aufs.h     1970-01-01 01:00:00.000000000 +0100
2818 +++ linux/fs/aufs/aufs.h        2022-03-21 14:49:05.716633010 +0100
2819 @@ -0,0 +1,62 @@
2820 +/* SPDX-License-Identifier: GPL-2.0 */
2821 +/*
2822 + * Copyright (C) 2005-2021 Junjiro R. Okajima
2823 + *
2824 + * This program, aufs is free software; you can redistribute it and/or modify
2825 + * it under the terms of the GNU General Public License as published by
2826 + * the Free Software Foundation; either version 2 of the License, or
2827 + * (at your option) any later version.
2828 + *
2829 + * This program is distributed in the hope that it will be useful,
2830 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2831 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2832 + * GNU General Public License for more details.
2833 + *
2834 + * You should have received a copy of the GNU General Public License
2835 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
2836 + */
2837 +
2838 +/*
2839 + * all header files
2840 + */
2841 +
2842 +#ifndef __AUFS_H__
2843 +#define __AUFS_H__
2844 +
2845 +#ifdef __KERNEL__
2846 +
2847 +#define AuStub(type, name, body, ...) \
2848 +       static inline type name(__VA_ARGS__) { body; }
2849 +
2850 +#define AuStubVoid(name, ...) \
2851 +       AuStub(void, name, , __VA_ARGS__)
2852 +#define AuStubInt0(name, ...) \
2853 +       AuStub(int, name, return 0, __VA_ARGS__)
2854 +
2855 +#include "debug.h"
2856 +
2857 +#include "branch.h"
2858 +#include "cpup.h"
2859 +#include "dcsub.h"
2860 +#include "dbgaufs.h"
2861 +#include "dentry.h"
2862 +#include "dir.h"
2863 +#include "dirren.h"
2864 +#include "dynop.h"
2865 +#include "file.h"
2866 +#include "fstype.h"
2867 +#include "hbl.h"
2868 +#include "inode.h"
2869 +#include "lcnt.h"
2870 +#include "loop.h"
2871 +#include "module.h"
2872 +#include "opts.h"
2873 +#include "rwsem.h"
2874 +#include "super.h"
2875 +#include "sysaufs.h"
2876 +#include "vfsub.h"
2877 +#include "whout.h"
2878 +#include "wkq.h"
2879 +
2880 +#endif /* __KERNEL__ */
2881 +#endif /* __AUFS_H__ */
2882 diff -urN /usr/share/empty/fs/aufs/branch.c linux/fs/aufs/branch.c
2883 --- /usr/share/empty/fs/aufs/branch.c   1970-01-01 01:00:00.000000000 +0100
2884 +++ linux/fs/aufs/branch.c      2022-03-21 14:49:05.719966344 +0100
2885 @@ -0,0 +1,1427 @@
2886 +// SPDX-License-Identifier: GPL-2.0
2887 +/*
2888 + * Copyright (C) 2005-2021 Junjiro R. Okajima
2889 + *
2890 + * This program, aufs is free software; you can redistribute it and/or modify
2891 + * it under the terms of the GNU General Public License as published by
2892 + * the Free Software Foundation; either version 2 of the License, or
2893 + * (at your option) any later version.
2894 + *
2895 + * This program is distributed in the hope that it will be useful,
2896 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2897 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2898 + * GNU General Public License for more details.
2899 + *
2900 + * You should have received a copy of the GNU General Public License
2901 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
2902 + */
2903 +
2904 +/*
2905 + * branch management
2906 + */
2907 +
2908 +#include <linux/compat.h>
2909 +#include <linux/statfs.h>
2910 +#include "aufs.h"
2911 +
2912 +/*
2913 + * free a single branch
2914 + */
2915 +static void au_br_do_free(struct au_branch *br)
2916 +{
2917 +       int i;
2918 +       struct au_wbr *wbr;
2919 +       struct au_dykey **key;
2920 +
2921 +       au_hnotify_fin_br(br);
2922 +       /* always, regardless the mount option */
2923 +       au_dr_hino_free(&br->br_dirren);
2924 +       au_xino_put(br);
2925 +
2926 +       AuLCntZero(au_lcnt_read(&br->br_nfiles, /*do_rev*/0));
2927 +       au_lcnt_fin(&br->br_nfiles, /*do_sync*/0);
2928 +       AuLCntZero(au_lcnt_read(&br->br_count, /*do_rev*/0));
2929 +       au_lcnt_fin(&br->br_count, /*do_sync*/0);
2930 +
2931 +       wbr = br->br_wbr;
2932 +       if (wbr) {
2933 +               for (i = 0; i < AuBrWh_Last; i++)
2934 +                       dput(wbr->wbr_wh[i]);
2935 +               AuDebugOn(atomic_read(&wbr->wbr_wh_running));
2936 +               AuRwDestroy(&wbr->wbr_wh_rwsem);
2937 +       }
2938 +
2939 +       if (br->br_fhsm) {
2940 +               au_br_fhsm_fin(br->br_fhsm);
2941 +               au_kfree_try_rcu(br->br_fhsm);
2942 +       }
2943 +
2944 +       key = br->br_dykey;
2945 +       for (i = 0; i < AuBrDynOp; i++, key++)
2946 +               if (*key)
2947 +                       au_dy_put(*key);
2948 +               else
2949 +                       break;
2950 +
2951 +       /* recursive lock, s_umount of branch's */
2952 +       /* synchronize_rcu(); */ /* why? */
2953 +       lockdep_off();
2954 +       path_put(&br->br_path);
2955 +       lockdep_on();
2956 +       au_kfree_rcu(wbr);
2957 +       au_lcnt_wait_for_fin(&br->br_nfiles);
2958 +       au_lcnt_wait_for_fin(&br->br_count);
2959 +       /* I don't know why, but percpu_refcount requires this */
2960 +       /* synchronize_rcu(); */
2961 +       au_kfree_rcu(br);
2962 +}
2963 +
2964 +/*
2965 + * frees all branches
2966 + */
2967 +void au_br_free(struct au_sbinfo *sbinfo)
2968 +{
2969 +       aufs_bindex_t bmax;
2970 +       struct au_branch **br;
2971 +
2972 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
2973 +
2974 +       bmax = sbinfo->si_bbot + 1;
2975 +       br = sbinfo->si_branch;
2976 +       while (bmax--)
2977 +               au_br_do_free(*br++);
2978 +}
2979 +
2980 +/*
2981 + * find the index of a branch which is specified by @br_id.
2982 + */
2983 +int au_br_index(struct super_block *sb, aufs_bindex_t br_id)
2984 +{
2985 +       aufs_bindex_t bindex, bbot;
2986 +
2987 +       bbot = au_sbbot(sb);
2988 +       for (bindex = 0; bindex <= bbot; bindex++)
2989 +               if (au_sbr_id(sb, bindex) == br_id)
2990 +                       return bindex;
2991 +       return -1;
2992 +}
2993 +
2994 +/* ---------------------------------------------------------------------- */
2995 +
2996 +/*
2997 + * add a branch
2998 + */
2999 +
3000 +static int test_overlap(struct super_block *sb, struct dentry *h_adding,
3001 +                       struct dentry *h_root)
3002 +{
3003 +       if (unlikely(h_adding == h_root
3004 +                    || au_test_loopback_overlap(sb, h_adding)))
3005 +               return 1;
3006 +       if (h_adding->d_sb != h_root->d_sb)
3007 +               return 0;
3008 +       return au_test_subdir(h_adding, h_root)
3009 +               || au_test_subdir(h_root, h_adding);
3010 +}
3011 +
3012 +/*
3013 + * returns a newly allocated branch. @new_nbranch is a number of branches
3014 + * after adding a branch.
3015 + */
3016 +static struct au_branch *au_br_alloc(struct super_block *sb, int new_nbranch,
3017 +                                    int perm)
3018 +{
3019 +       struct au_branch *add_branch;
3020 +       struct dentry *root;
3021 +       struct inode *inode;
3022 +       int err;
3023 +
3024 +       err = -ENOMEM;
3025 +       add_branch = kzalloc(sizeof(*add_branch), GFP_NOFS);
3026 +       if (unlikely(!add_branch))
3027 +               goto out;
3028 +       add_branch->br_xino = au_xino_alloc(/*nfile*/1);
3029 +       if (unlikely(!add_branch->br_xino))
3030 +               goto out_br;
3031 +       err = au_hnotify_init_br(add_branch, perm);
3032 +       if (unlikely(err))
3033 +               goto out_xino;
3034 +
3035 +       if (au_br_writable(perm)) {
3036 +               /* may be freed separately at changing the branch permission */
3037 +               add_branch->br_wbr = kzalloc(sizeof(*add_branch->br_wbr),
3038 +                                            GFP_NOFS);
3039 +               if (unlikely(!add_branch->br_wbr))
3040 +                       goto out_hnotify;
3041 +       }
3042 +
3043 +       if (au_br_fhsm(perm)) {
3044 +               err = au_fhsm_br_alloc(add_branch);
3045 +               if (unlikely(err))
3046 +                       goto out_wbr;
3047 +       }
3048 +
3049 +       root = sb->s_root;
3050 +       err = au_sbr_realloc(au_sbi(sb), new_nbranch, /*may_shrink*/0);
3051 +       if (!err)
3052 +               err = au_di_realloc(au_di(root), new_nbranch, /*may_shrink*/0);
3053 +       if (!err) {
3054 +               inode = d_inode(root);
3055 +               err = au_hinode_realloc(au_ii(inode), new_nbranch,
3056 +                                       /*may_shrink*/0);
3057 +       }
3058 +       if (!err)
3059 +               return add_branch; /* success */
3060 +
3061 +out_wbr:
3062 +       au_kfree_rcu(add_branch->br_wbr);
3063 +out_hnotify:
3064 +       au_hnotify_fin_br(add_branch);
3065 +out_xino:
3066 +       au_xino_put(add_branch);
3067 +out_br:
3068 +       au_kfree_rcu(add_branch);
3069 +out:
3070 +       return ERR_PTR(err);
3071 +}
3072 +
3073 +/*
3074 + * test if the branch permission is legal or not.
3075 + */
3076 +static int test_br(struct inode *inode, int brperm, char *path)
3077 +{
3078 +       int err;
3079 +
3080 +       err = (au_br_writable(brperm) && IS_RDONLY(inode));
3081 +       if (!err)
3082 +               goto out;
3083 +
3084 +       err = -EINVAL;
3085 +       pr_err("write permission for readonly mount or inode, %s\n", path);
3086 +
3087 +out:
3088 +       return err;
3089 +}
3090 +
3091 +/*
3092 + * returns:
3093 + * 0: success, the caller will add it
3094 + * plus: success, it is already unified, the caller should ignore it
3095 + * minus: error
3096 + */
3097 +static int test_add(struct super_block *sb, struct au_opt_add *add, int remount)
3098 +{
3099 +       int err;
3100 +       aufs_bindex_t bbot, bindex;
3101 +       struct dentry *root, *h_dentry;
3102 +       struct inode *inode, *h_inode;
3103 +
3104 +       root = sb->s_root;
3105 +       bbot = au_sbbot(sb);
3106 +       if (unlikely(bbot >= 0
3107 +                    && au_find_dbindex(root, add->path.dentry) >= 0)) {
3108 +               err = 1;
3109 +               if (!remount) {
3110 +                       err = -EINVAL;
3111 +                       pr_err("%s duplicated\n", add->pathname);
3112 +               }
3113 +               goto out;
3114 +       }
3115 +
3116 +       err = -ENOSPC; /* -E2BIG; */
3117 +       if (unlikely(AUFS_BRANCH_MAX <= add->bindex
3118 +                    || AUFS_BRANCH_MAX - 1 <= bbot)) {
3119 +               pr_err("number of branches exceeded %s\n", add->pathname);
3120 +               goto out;
3121 +       }
3122 +
3123 +       err = -EDOM;
3124 +       if (unlikely(add->bindex < 0 || bbot + 1 < add->bindex)) {
3125 +               pr_err("bad index %d\n", add->bindex);
3126 +               goto out;
3127 +       }
3128 +
3129 +       inode = d_inode(add->path.dentry);
3130 +       err = -ENOENT;
3131 +       if (unlikely(!inode->i_nlink)) {
3132 +               pr_err("no existence %s\n", add->pathname);
3133 +               goto out;
3134 +       }
3135 +
3136 +       err = -EINVAL;
3137 +       if (unlikely(inode->i_sb == sb)) {
3138 +               pr_err("%s must be outside\n", add->pathname);
3139 +               goto out;
3140 +       }
3141 +
3142 +       if (unlikely(au_test_fs_unsuppoted(inode->i_sb))) {
3143 +               pr_err("unsupported filesystem, %s (%s)\n",
3144 +                      add->pathname, au_sbtype(inode->i_sb));
3145 +               goto out;
3146 +       }
3147 +
3148 +       if (unlikely(inode->i_sb->s_stack_depth)) {
3149 +               pr_err("already stacked, %s (%s)\n",
3150 +                      add->pathname, au_sbtype(inode->i_sb));
3151 +               goto out;
3152 +       }
3153 +
3154 +       err = test_br(d_inode(add->path.dentry), add->perm, add->pathname);
3155 +       if (unlikely(err))
3156 +               goto out;
3157 +
3158 +       if (bbot < 0)
3159 +               return 0; /* success */
3160 +
3161 +       err = -EINVAL;
3162 +       for (bindex = 0; bindex <= bbot; bindex++)
3163 +               if (unlikely(test_overlap(sb, add->path.dentry,
3164 +                                         au_h_dptr(root, bindex)))) {
3165 +                       pr_err("%s is overlapped\n", add->pathname);
3166 +                       goto out;
3167 +               }
3168 +
3169 +       err = 0;
3170 +       if (au_opt_test(au_mntflags(sb), WARN_PERM)) {
3171 +               h_dentry = au_h_dptr(root, 0);
3172 +               h_inode = d_inode(h_dentry);
3173 +               if ((h_inode->i_mode & S_IALLUGO) != (inode->i_mode & S_IALLUGO)
3174 +                   || !uid_eq(h_inode->i_uid, inode->i_uid)
3175 +                   || !gid_eq(h_inode->i_gid, inode->i_gid))
3176 +                       pr_warn("uid/gid/perm %s %u/%u/0%o, %u/%u/0%o\n",
3177 +                               add->pathname,
3178 +                               i_uid_read(inode), i_gid_read(inode),
3179 +                               (inode->i_mode & S_IALLUGO),
3180 +                               i_uid_read(h_inode), i_gid_read(h_inode),
3181 +                               (h_inode->i_mode & S_IALLUGO));
3182 +       }
3183 +
3184 +out:
3185 +       return err;
3186 +}
3187 +
3188 +/*
3189 + * initialize or clean the whiteouts for an adding branch
3190 + */
3191 +static int au_br_init_wh(struct super_block *sb, struct au_branch *br,
3192 +                        int new_perm)
3193 +{
3194 +       int err, old_perm;
3195 +       aufs_bindex_t bindex;
3196 +       struct inode *h_inode;
3197 +       struct au_wbr *wbr;
3198 +       struct au_hinode *hdir;
3199 +       struct dentry *h_dentry;
3200 +
3201 +       err = vfsub_mnt_want_write(au_br_mnt(br));
3202 +       if (unlikely(err))
3203 +               goto out;
3204 +
3205 +       wbr = br->br_wbr;
3206 +       old_perm = br->br_perm;
3207 +       br->br_perm = new_perm;
3208 +       hdir = NULL;
3209 +       h_inode = NULL;
3210 +       bindex = au_br_index(sb, br->br_id);
3211 +       if (0 <= bindex) {
3212 +               hdir = au_hi(d_inode(sb->s_root), bindex);
3213 +               au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
3214 +       } else {
3215 +               h_dentry = au_br_dentry(br);
3216 +               h_inode = d_inode(h_dentry);
3217 +               inode_lock_nested(h_inode, AuLsc_I_PARENT);
3218 +       }
3219 +       if (!wbr)
3220 +               err = au_wh_init(br, sb);
3221 +       else {
3222 +               wbr_wh_write_lock(wbr);
3223 +               err = au_wh_init(br, sb);
3224 +               wbr_wh_write_unlock(wbr);
3225 +       }
3226 +       if (hdir)
3227 +               au_hn_inode_unlock(hdir);
3228 +       else
3229 +               inode_unlock(h_inode);
3230 +       vfsub_mnt_drop_write(au_br_mnt(br));
3231 +       br->br_perm = old_perm;
3232 +
3233 +       if (!err && wbr && !au_br_writable(new_perm)) {
3234 +               au_kfree_rcu(wbr);
3235 +               br->br_wbr = NULL;
3236 +       }
3237 +
3238 +out:
3239 +       return err;
3240 +}
3241 +
3242 +static int au_wbr_init(struct au_branch *br, struct super_block *sb,
3243 +                      int perm)
3244 +{
3245 +       int err;
3246 +       struct kstatfs kst;
3247 +       struct au_wbr *wbr;
3248 +
3249 +       wbr = br->br_wbr;
3250 +       au_rw_init(&wbr->wbr_wh_rwsem);
3251 +       atomic_set(&wbr->wbr_wh_running, 0);
3252 +
3253 +       /*
3254 +        * a limit for rmdir/rename a dir
3255 +        * cf. AUFS_MAX_NAMELEN in include/uapi/linux/aufs_type.h
3256 +        */
3257 +       err = vfs_statfs(&br->br_path, &kst);
3258 +       if (unlikely(err))
3259 +               goto out;
3260 +       err = -EINVAL;
3261 +       if (kst.f_namelen >= NAME_MAX)
3262 +               err = au_br_init_wh(sb, br, perm);
3263 +       else
3264 +               pr_err("%pd(%s), unsupported namelen %ld\n",
3265 +                      au_br_dentry(br),
3266 +                      au_sbtype(au_br_dentry(br)->d_sb), kst.f_namelen);
3267 +
3268 +out:
3269 +       return err;
3270 +}
3271 +
3272 +/* initialize a new branch */
3273 +static int au_br_init(struct au_branch *br, struct super_block *sb,
3274 +                     struct au_opt_add *add)
3275 +{
3276 +       int err;
3277 +       struct au_branch *brbase;
3278 +       struct file *xf;
3279 +       struct inode *h_inode;
3280 +
3281 +       err = 0;
3282 +       br->br_perm = add->perm;
3283 +       br->br_path = add->path; /* set first, path_get() later */
3284 +       spin_lock_init(&br->br_dykey_lock);
3285 +       au_lcnt_init(&br->br_nfiles, /*release*/NULL);
3286 +       au_lcnt_init(&br->br_count, /*release*/NULL);
3287 +       br->br_id = au_new_br_id(sb);
3288 +       AuDebugOn(br->br_id < 0);
3289 +
3290 +       /* always, regardless the given option */
3291 +       err = au_dr_br_init(sb, br, &add->path);
3292 +       if (unlikely(err))
3293 +               goto out_err;
3294 +
3295 +       if (au_br_writable(add->perm)) {
3296 +               err = au_wbr_init(br, sb, add->perm);
3297 +               if (unlikely(err))
3298 +                       goto out_err;
3299 +       }
3300 +
3301 +       if (au_opt_test(au_mntflags(sb), XINO)) {
3302 +               brbase = au_sbr(sb, 0);
3303 +               xf = au_xino_file(brbase->br_xino, /*idx*/-1);
3304 +               AuDebugOn(!xf);
3305 +               h_inode = d_inode(add->path.dentry);
3306 +               err = au_xino_init_br(sb, br, h_inode->i_ino, &xf->f_path);
3307 +               if (unlikely(err)) {
3308 +                       AuDebugOn(au_xino_file(br->br_xino, /*idx*/-1));
3309 +                       goto out_err;
3310 +               }
3311 +       }
3312 +
3313 +       sysaufs_br_init(br);
3314 +       path_get(&br->br_path);
3315 +       goto out; /* success */
3316 +
3317 +out_err:
3318 +       memset(&br->br_path, 0, sizeof(br->br_path));
3319 +out:
3320 +       return err;
3321 +}
3322 +
3323 +static void au_br_do_add_brp(struct au_sbinfo *sbinfo, aufs_bindex_t bindex,
3324 +                            struct au_branch *br, aufs_bindex_t bbot,
3325 +                            aufs_bindex_t amount)
3326 +{
3327 +       struct au_branch **brp;
3328 +
3329 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
3330 +
3331 +       brp = sbinfo->si_branch + bindex;
3332 +       memmove(brp + 1, brp, sizeof(*brp) * amount);
3333 +       *brp = br;
3334 +       sbinfo->si_bbot++;
3335 +       if (unlikely(bbot < 0))
3336 +               sbinfo->si_bbot = 0;
3337 +}
3338 +
3339 +static void au_br_do_add_hdp(struct au_dinfo *dinfo, aufs_bindex_t bindex,
3340 +                            aufs_bindex_t bbot, aufs_bindex_t amount)
3341 +{
3342 +       struct au_hdentry *hdp;
3343 +
3344 +       AuRwMustWriteLock(&dinfo->di_rwsem);
3345 +
3346 +       hdp = au_hdentry(dinfo, bindex);
3347 +       memmove(hdp + 1, hdp, sizeof(*hdp) * amount);
3348 +       au_h_dentry_init(hdp);
3349 +       dinfo->di_bbot++;
3350 +       if (unlikely(bbot < 0))
3351 +               dinfo->di_btop = 0;
3352 +}
3353 +
3354 +static void au_br_do_add_hip(struct au_iinfo *iinfo, aufs_bindex_t bindex,
3355 +                            aufs_bindex_t bbot, aufs_bindex_t amount)
3356 +{
3357 +       struct au_hinode *hip;
3358 +
3359 +       AuRwMustWriteLock(&iinfo->ii_rwsem);
3360 +
3361 +       hip = au_hinode(iinfo, bindex);
3362 +       memmove(hip + 1, hip, sizeof(*hip) * amount);
3363 +       au_hinode_init(hip);
3364 +       iinfo->ii_bbot++;
3365 +       if (unlikely(bbot < 0))
3366 +               iinfo->ii_btop = 0;
3367 +}
3368 +
3369 +static void au_br_do_add(struct super_block *sb, struct au_branch *br,
3370 +                        aufs_bindex_t bindex)
3371 +{
3372 +       struct dentry *root, *h_dentry;
3373 +       struct inode *root_inode, *h_inode;
3374 +       aufs_bindex_t bbot, amount;
3375 +
3376 +       root = sb->s_root;
3377 +       root_inode = d_inode(root);
3378 +       bbot = au_sbbot(sb);
3379 +       amount = bbot + 1 - bindex;
3380 +       h_dentry = au_br_dentry(br);
3381 +       au_sbilist_lock();
3382 +       au_br_do_add_brp(au_sbi(sb), bindex, br, bbot, amount);
3383 +       au_br_do_add_hdp(au_di(root), bindex, bbot, amount);
3384 +       au_br_do_add_hip(au_ii(root_inode), bindex, bbot, amount);
3385 +       au_set_h_dptr(root, bindex, dget(h_dentry));
3386 +       h_inode = d_inode(h_dentry);
3387 +       au_set_h_iptr(root_inode, bindex, au_igrab(h_inode), /*flags*/0);
3388 +       au_sbilist_unlock();
3389 +}
3390 +
3391 +int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount)
3392 +{
3393 +       int err;
3394 +       aufs_bindex_t bbot, add_bindex;
3395 +       struct dentry *root, *h_dentry;
3396 +       struct inode *root_inode;
3397 +       struct au_branch *add_branch;
3398 +
3399 +       root = sb->s_root;
3400 +       root_inode = d_inode(root);
3401 +       IMustLock(root_inode);
3402 +       IiMustWriteLock(root_inode);
3403 +       err = test_add(sb, add, remount);
3404 +       if (unlikely(err < 0))
3405 +               goto out;
3406 +       if (err) {
3407 +               err = 0;
3408 +               goto out; /* success */
3409 +       }
3410 +
3411 +       bbot = au_sbbot(sb);
3412 +       add_branch = au_br_alloc(sb, bbot + 2, add->perm);
3413 +       err = PTR_ERR(add_branch);
3414 +       if (IS_ERR(add_branch))
3415 +               goto out;
3416 +
3417 +       err = au_br_init(add_branch, sb, add);
3418 +       if (unlikely(err)) {
3419 +               au_br_do_free(add_branch);
3420 +               goto out;
3421 +       }
3422 +
3423 +       add_bindex = add->bindex;
3424 +       sysaufs_brs_del(sb, add_bindex);        /* remove successors */
3425 +       au_br_do_add(sb, add_branch, add_bindex);
3426 +       sysaufs_brs_add(sb, add_bindex);        /* append successors */
3427 +       dbgaufs_brs_add(sb, add_bindex, /*topdown*/0);  /* rename successors */
3428 +
3429 +       h_dentry = add->path.dentry;
3430 +       if (!add_bindex) {
3431 +               au_cpup_attr_all(root_inode, /*force*/1);
3432 +               sb->s_maxbytes = h_dentry->d_sb->s_maxbytes;
3433 +       } else
3434 +               au_add_nlink(root_inode, d_inode(h_dentry));
3435 +
3436 +out:
3437 +       return err;
3438 +}
3439 +
3440 +/* ---------------------------------------------------------------------- */
3441 +
3442 +static unsigned long long au_farray_cb(struct super_block *sb, void *a,
3443 +                                      unsigned long long max __maybe_unused,
3444 +                                      void *arg)
3445 +{
3446 +       unsigned long long n;
3447 +       struct file **p, *f;
3448 +       struct hlist_bl_head *files;
3449 +       struct hlist_bl_node *pos;
3450 +       struct au_finfo *finfo;
3451 +
3452 +       n = 0;
3453 +       p = a;
3454 +       files = &au_sbi(sb)->si_files;
3455 +       hlist_bl_lock(files);
3456 +       hlist_bl_for_each_entry(finfo, pos, files, fi_hlist) {
3457 +               f = finfo->fi_file;
3458 +               if (file_count(f)
3459 +                   && !special_file(file_inode(f)->i_mode)) {
3460 +                       get_file(f);
3461 +                       *p++ = f;
3462 +                       n++;
3463 +                       AuDebugOn(n > max);
3464 +               }
3465 +       }
3466 +       hlist_bl_unlock(files);
3467 +
3468 +       return n;
3469 +}
3470 +
3471 +static struct file **au_farray_alloc(struct super_block *sb,
3472 +                                    unsigned long long *max)
3473 +{
3474 +       struct au_sbinfo *sbi;
3475 +
3476 +       sbi = au_sbi(sb);
3477 +       *max = au_lcnt_read(&sbi->si_nfiles, /*do_rev*/1);
3478 +       return au_array_alloc(max, au_farray_cb, sb, /*arg*/NULL);
3479 +}
3480 +
3481 +static void au_farray_free(struct file **a, unsigned long long max)
3482 +{
3483 +       unsigned long long ull;
3484 +
3485 +       for (ull = 0; ull < max; ull++)
3486 +               if (a[ull])
3487 +                       fput(a[ull]);
3488 +       kvfree(a);
3489 +}
3490 +
3491 +/* ---------------------------------------------------------------------- */
3492 +
3493 +/*
3494 + * delete a branch
3495 + */
3496 +
3497 +/* to show the line number, do not make it inlined function */
3498 +#define AuVerbose(do_info, fmt, ...) do { \
3499 +       if (do_info) \
3500 +               pr_info(fmt, ##__VA_ARGS__); \
3501 +} while (0)
3502 +
3503 +static int au_test_ibusy(struct inode *inode, aufs_bindex_t btop,
3504 +                        aufs_bindex_t bbot)
3505 +{
3506 +       return (inode && !S_ISDIR(inode->i_mode)) || btop == bbot;
3507 +}
3508 +
3509 +static int au_test_dbusy(struct dentry *dentry, aufs_bindex_t btop,
3510 +                        aufs_bindex_t bbot)
3511 +{
3512 +       return au_test_ibusy(d_inode(dentry), btop, bbot);
3513 +}
3514 +
3515 +/*
3516 + * test if the branch is deletable or not.
3517 + */
3518 +static int test_dentry_busy(struct dentry *root, aufs_bindex_t bindex,
3519 +                           unsigned int sigen, const unsigned int verbose)
3520 +{
3521 +       int err, i, j, ndentry;
3522 +       aufs_bindex_t btop, bbot;
3523 +       struct au_dcsub_pages dpages;
3524 +       struct au_dpage *dpage;
3525 +       struct dentry *d;
3526 +
3527 +       err = au_dpages_init(&dpages, GFP_NOFS);
3528 +       if (unlikely(err))
3529 +               goto out;
3530 +       err = au_dcsub_pages(&dpages, root, NULL, NULL);
3531 +       if (unlikely(err))
3532 +               goto out_dpages;
3533 +
3534 +       for (i = 0; !err && i < dpages.ndpage; i++) {
3535 +               dpage = dpages.dpages + i;
3536 +               ndentry = dpage->ndentry;
3537 +               for (j = 0; !err && j < ndentry; j++) {
3538 +                       d = dpage->dentries[j];
3539 +                       AuDebugOn(au_dcount(d) <= 0);
3540 +                       if (!au_digen_test(d, sigen)) {
3541 +                               di_read_lock_child(d, AuLock_IR);
3542 +                               if (unlikely(au_dbrange_test(d))) {
3543 +                                       di_read_unlock(d, AuLock_IR);
3544 +                                       continue;
3545 +                               }
3546 +                       } else {
3547 +                               di_write_lock_child(d);
3548 +                               if (unlikely(au_dbrange_test(d))) {
3549 +                                       di_write_unlock(d);
3550 +                                       continue;
3551 +                               }
3552 +                               err = au_reval_dpath(d, sigen);
3553 +                               if (!err)
3554 +                                       di_downgrade_lock(d, AuLock_IR);
3555 +                               else {
3556 +                                       di_write_unlock(d);
3557 +                                       break;
3558 +                               }
3559 +                       }
3560 +
3561 +                       /* AuDbgDentry(d); */
3562 +                       btop = au_dbtop(d);
3563 +                       bbot = au_dbbot(d);
3564 +                       if (btop <= bindex
3565 +                           && bindex <= bbot
3566 +                           && au_h_dptr(d, bindex)
3567 +                           && au_test_dbusy(d, btop, bbot)) {
3568 +                               err = -EBUSY;
3569 +                               AuVerbose(verbose, "busy %pd\n", d);
3570 +                               AuDbgDentry(d);
3571 +                       }
3572 +                       di_read_unlock(d, AuLock_IR);
3573 +               }
3574 +       }
3575 +
3576 +out_dpages:
3577 +       au_dpages_free(&dpages);
3578 +out:
3579 +       return err;
3580 +}
3581 +
3582 +static int test_inode_busy(struct super_block *sb, aufs_bindex_t bindex,
3583 +                          unsigned int sigen, const unsigned int verbose)
3584 +{
3585 +       int err;
3586 +       unsigned long long max, ull;
3587 +       struct inode *i, **array;
3588 +       aufs_bindex_t btop, bbot;
3589 +
3590 +       array = au_iarray_alloc(sb, &max);
3591 +       err = PTR_ERR(array);
3592 +       if (IS_ERR(array))
3593 +               goto out;
3594 +
3595 +       err = 0;
3596 +       AuDbg("b%d\n", bindex);
3597 +       for (ull = 0; !err && ull < max; ull++) {
3598 +               i = array[ull];
3599 +               if (unlikely(!i))
3600 +                       break;
3601 +               if (i->i_ino == AUFS_ROOT_INO)
3602 +                       continue;
3603 +
3604 +               /* AuDbgInode(i); */
3605 +               if (au_iigen(i, NULL) == sigen)
3606 +                       ii_read_lock_child(i);
3607 +               else {
3608 +                       ii_write_lock_child(i);
3609 +                       err = au_refresh_hinode_self(i);
3610 +                       au_iigen_dec(i);
3611 +                       if (!err)
3612 +                               ii_downgrade_lock(i);
3613 +                       else {
3614 +                               ii_write_unlock(i);
3615 +                               break;
3616 +                       }
3617 +               }
3618 +
3619 +               btop = au_ibtop(i);
3620 +               bbot = au_ibbot(i);
3621 +               if (btop <= bindex
3622 +                   && bindex <= bbot
3623 +                   && au_h_iptr(i, bindex)
3624 +                   && au_test_ibusy(i, btop, bbot)) {
3625 +                       err = -EBUSY;
3626 +                       AuVerbose(verbose, "busy i%lu\n", i->i_ino);
3627 +                       AuDbgInode(i);
3628 +               }
3629 +               ii_read_unlock(i);
3630 +       }
3631 +       au_iarray_free(array, max);
3632 +
3633 +out:
3634 +       return err;
3635 +}
3636 +
3637 +static int test_children_busy(struct dentry *root, aufs_bindex_t bindex,
3638 +                             const unsigned int verbose)
3639 +{
3640 +       int err;
3641 +       unsigned int sigen;
3642 +
3643 +       sigen = au_sigen(root->d_sb);
3644 +       DiMustNoWaiters(root);
3645 +       IiMustNoWaiters(d_inode(root));
3646 +       di_write_unlock(root);
3647 +       err = test_dentry_busy(root, bindex, sigen, verbose);
3648 +       if (!err)
3649 +               err = test_inode_busy(root->d_sb, bindex, sigen, verbose);
3650 +       di_write_lock_child(root); /* aufs_write_lock() calls ..._child() */
3651 +
3652 +       return err;
3653 +}
3654 +
3655 +static int test_dir_busy(struct file *file, aufs_bindex_t br_id,
3656 +                        struct file **to_free, int *idx)
3657 +{
3658 +       int err;
3659 +       unsigned char matched, root;
3660 +       aufs_bindex_t bindex, bbot;
3661 +       struct au_fidir *fidir;
3662 +       struct au_hfile *hfile;
3663 +
3664 +       err = 0;
3665 +       root = IS_ROOT(file->f_path.dentry);
3666 +       if (root) {
3667 +               get_file(file);
3668 +               to_free[*idx] = file;
3669 +               (*idx)++;
3670 +               goto out;
3671 +       }
3672 +
3673 +       matched = 0;
3674 +       fidir = au_fi(file)->fi_hdir;
3675 +       AuDebugOn(!fidir);
3676 +       bbot = au_fbbot_dir(file);
3677 +       for (bindex = au_fbtop(file); bindex <= bbot; bindex++) {
3678 +               hfile = fidir->fd_hfile + bindex;
3679 +               if (!hfile->hf_file)
3680 +                       continue;
3681 +
3682 +               if (hfile->hf_br->br_id == br_id) {
3683 +                       matched = 1;
3684 +                       break;
3685 +               }
3686 +       }
3687 +       if (matched)
3688 +               err = -EBUSY;
3689 +
3690 +out:
3691 +       return err;
3692 +}
3693 +
3694 +static int test_file_busy(struct super_block *sb, aufs_bindex_t br_id,
3695 +                         struct file **to_free, int opened)
3696 +{
3697 +       int err, idx;
3698 +       unsigned long long ull, max;
3699 +       aufs_bindex_t btop;
3700 +       struct file *file, **array;
3701 +       struct dentry *root;
3702 +       struct au_hfile *hfile;
3703 +
3704 +       array = au_farray_alloc(sb, &max);
3705 +       err = PTR_ERR(array);
3706 +       if (IS_ERR(array))
3707 +               goto out;
3708 +
3709 +       err = 0;
3710 +       idx = 0;
3711 +       root = sb->s_root;
3712 +       di_write_unlock(root);
3713 +       for (ull = 0; ull < max; ull++) {
3714 +               file = array[ull];
3715 +               if (unlikely(!file))
3716 +                       break;
3717 +
3718 +               /* AuDbg("%pD\n", file); */
3719 +               fi_read_lock(file);
3720 +               btop = au_fbtop(file);
3721 +               if (!d_is_dir(file->f_path.dentry)) {
3722 +                       hfile = &au_fi(file)->fi_htop;
3723 +                       if (hfile->hf_br->br_id == br_id)
3724 +                               err = -EBUSY;
3725 +               } else
3726 +                       err = test_dir_busy(file, br_id, to_free, &idx);
3727 +               fi_read_unlock(file);
3728 +               if (unlikely(err))
3729 +                       break;
3730 +       }
3731 +       di_write_lock_child(root);
3732 +       au_farray_free(array, max);
3733 +       AuDebugOn(idx > opened);
3734 +
3735 +out:
3736 +       return err;
3737 +}
3738 +
3739 +static void br_del_file(struct file **to_free, unsigned long long opened,
3740 +                       aufs_bindex_t br_id)
3741 +{
3742 +       unsigned long long ull;
3743 +       aufs_bindex_t bindex, btop, bbot, bfound;
3744 +       struct file *file;
3745 +       struct au_fidir *fidir;
3746 +       struct au_hfile *hfile;
3747 +
3748 +       for (ull = 0; ull < opened; ull++) {
3749 +               file = to_free[ull];
3750 +               if (unlikely(!file))
3751 +                       break;
3752 +
3753 +               /* AuDbg("%pD\n", file); */
3754 +               AuDebugOn(!d_is_dir(file->f_path.dentry));
3755 +               bfound = -1;
3756 +               fidir = au_fi(file)->fi_hdir;
3757 +               AuDebugOn(!fidir);
3758 +               fi_write_lock(file);
3759 +               btop = au_fbtop(file);
3760 +               bbot = au_fbbot_dir(file);
3761 +               for (bindex = btop; bindex <= bbot; bindex++) {
3762 +                       hfile = fidir->fd_hfile + bindex;
3763 +                       if (!hfile->hf_file)
3764 +                               continue;
3765 +
3766 +                       if (hfile->hf_br->br_id == br_id) {
3767 +                               bfound = bindex;
3768 +                               break;
3769 +                       }
3770 +               }
3771 +               AuDebugOn(bfound < 0);
3772 +               au_set_h_fptr(file, bfound, NULL);
3773 +               if (bfound == btop) {
3774 +                       for (btop++; btop <= bbot; btop++)
3775 +                               if (au_hf_dir(file, btop)) {
3776 +                                       au_set_fbtop(file, btop);
3777 +                                       break;
3778 +                               }
3779 +               }
3780 +               fi_write_unlock(file);
3781 +       }
3782 +}
3783 +
3784 +static void au_br_do_del_brp(struct au_sbinfo *sbinfo,
3785 +                            const aufs_bindex_t bindex,
3786 +                            const aufs_bindex_t bbot)
3787 +{
3788 +       struct au_branch **brp, **p;
3789 +
3790 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
3791 +
3792 +       brp = sbinfo->si_branch + bindex;
3793 +       if (bindex < bbot)
3794 +               memmove(brp, brp + 1, sizeof(*brp) * (bbot - bindex));
3795 +       sbinfo->si_branch[0 + bbot] = NULL;
3796 +       sbinfo->si_bbot--;
3797 +
3798 +       p = au_krealloc(sbinfo->si_branch, sizeof(*p) * bbot, AuGFP_SBILIST,
3799 +                       /*may_shrink*/1);
3800 +       if (p)
3801 +               sbinfo->si_branch = p;
3802 +       /* harmless error */
3803 +}
3804 +
3805 +static void au_br_do_del_hdp(struct au_dinfo *dinfo, const aufs_bindex_t bindex,
3806 +                            const aufs_bindex_t bbot)
3807 +{
3808 +       struct au_hdentry *hdp, *p;
3809 +
3810 +       AuRwMustWriteLock(&dinfo->di_rwsem);
3811 +
3812 +       hdp = au_hdentry(dinfo, bindex);
3813 +       if (bindex < bbot)
3814 +               memmove(hdp, hdp + 1, sizeof(*hdp) * (bbot - bindex));
3815 +       /* au_h_dentry_init(au_hdentry(dinfo, bbot); */
3816 +       dinfo->di_bbot--;
3817 +
3818 +       p = au_krealloc(dinfo->di_hdentry, sizeof(*p) * bbot, AuGFP_SBILIST,
3819 +                       /*may_shrink*/1);
3820 +       if (p)
3821 +               dinfo->di_hdentry = p;
3822 +       /* harmless error */
3823 +}
3824 +
3825 +static void au_br_do_del_hip(struct au_iinfo *iinfo, const aufs_bindex_t bindex,
3826 +                            const aufs_bindex_t bbot)
3827 +{
3828 +       struct au_hinode *hip, *p;
3829 +
3830 +       AuRwMustWriteLock(&iinfo->ii_rwsem);
3831 +
3832 +       hip = au_hinode(iinfo, bindex);
3833 +       if (bindex < bbot)
3834 +               memmove(hip, hip + 1, sizeof(*hip) * (bbot - bindex));
3835 +       /* au_hinode_init(au_hinode(iinfo, bbot)); */
3836 +       iinfo->ii_bbot--;
3837 +
3838 +       p = au_krealloc(iinfo->ii_hinode, sizeof(*p) * bbot, AuGFP_SBILIST,
3839 +                       /*may_shrink*/1);
3840 +       if (p)
3841 +               iinfo->ii_hinode = p;
3842 +       /* harmless error */
3843 +}
3844 +
3845 +static void au_br_do_del(struct super_block *sb, aufs_bindex_t bindex,
3846 +                        struct au_branch *br)
3847 +{
3848 +       aufs_bindex_t bbot;
3849 +       struct au_sbinfo *sbinfo;
3850 +       struct dentry *root, *h_root;
3851 +       struct inode *inode, *h_inode;
3852 +       struct au_hinode *hinode;
3853 +
3854 +       SiMustWriteLock(sb);
3855 +
3856 +       root = sb->s_root;
3857 +       inode = d_inode(root);
3858 +       sbinfo = au_sbi(sb);
3859 +       bbot = sbinfo->si_bbot;
3860 +
3861 +       h_root = au_h_dptr(root, bindex);
3862 +       hinode = au_hi(inode, bindex);
3863 +       h_inode = au_igrab(hinode->hi_inode);
3864 +       au_hiput(hinode);
3865 +
3866 +       au_sbilist_lock();
3867 +       au_br_do_del_brp(sbinfo, bindex, bbot);
3868 +       au_br_do_del_hdp(au_di(root), bindex, bbot);
3869 +       au_br_do_del_hip(au_ii(inode), bindex, bbot);
3870 +       au_sbilist_unlock();
3871 +
3872 +       /* ignore an error */
3873 +       au_dr_br_fin(sb, br); /* always, regardless the mount option */
3874 +
3875 +       dput(h_root);
3876 +       iput(h_inode);
3877 +       au_br_do_free(br);
3878 +}
3879 +
3880 +static unsigned long long empty_cb(struct super_block *sb, void *array,
3881 +                                  unsigned long long max, void *arg)
3882 +{
3883 +       return max;
3884 +}
3885 +
3886 +int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount)
3887 +{
3888 +       int err, rerr, i;
3889 +       unsigned long long opened;
3890 +       unsigned int mnt_flags;
3891 +       aufs_bindex_t bindex, bbot, br_id;
3892 +       unsigned char do_wh, verbose;
3893 +       struct au_branch *br;
3894 +       struct au_wbr *wbr;
3895 +       struct dentry *root;
3896 +       struct file **to_free;
3897 +
3898 +       err = 0;
3899 +       opened = 0;
3900 +       to_free = NULL;
3901 +       root = sb->s_root;
3902 +       bindex = au_find_dbindex(root, del->h_path.dentry);
3903 +       if (bindex < 0) {
3904 +               if (remount)
3905 +                       goto out; /* success */
3906 +               err = -ENOENT;
3907 +               pr_err("%s no such branch\n", del->pathname);
3908 +               goto out;
3909 +       }
3910 +       AuDbg("bindex b%d\n", bindex);
3911 +
3912 +       err = -EBUSY;
3913 +       mnt_flags = au_mntflags(sb);
3914 +       verbose = !!au_opt_test(mnt_flags, VERBOSE);
3915 +       bbot = au_sbbot(sb);
3916 +       if (unlikely(!bbot)) {
3917 +               AuVerbose(verbose, "no more branches left\n");
3918 +               goto out;
3919 +       }
3920 +
3921 +       br = au_sbr(sb, bindex);
3922 +       AuDebugOn(!path_equal(&br->br_path, &del->h_path));
3923 +       if (unlikely(au_lcnt_read(&br->br_count, /*do_rev*/1))) {
3924 +               AuVerbose(verbose, "br %pd2 is busy now\n", del->h_path.dentry);
3925 +               goto out;
3926 +       }
3927 +
3928 +       br_id = br->br_id;
3929 +       opened = au_lcnt_read(&br->br_nfiles, /*do_rev*/1);
3930 +       if (unlikely(opened)) {
3931 +               to_free = au_array_alloc(&opened, empty_cb, sb, NULL);
3932 +               err = PTR_ERR(to_free);
3933 +               if (IS_ERR(to_free))
3934 +                       goto out;
3935 +
3936 +               err = test_file_busy(sb, br_id, to_free, opened);
3937 +               if (unlikely(err)) {
3938 +                       AuVerbose(verbose, "%llu file(s) opened\n", opened);
3939 +                       goto out;
3940 +               }
3941 +       }
3942 +
3943 +       wbr = br->br_wbr;
3944 +       do_wh = wbr && (wbr->wbr_whbase || wbr->wbr_plink || wbr->wbr_orph);
3945 +       if (do_wh) {
3946 +               /* instead of WbrWhMustWriteLock(wbr) */
3947 +               SiMustWriteLock(sb);
3948 +               for (i = 0; i < AuBrWh_Last; i++) {
3949 +                       dput(wbr->wbr_wh[i]);
3950 +                       wbr->wbr_wh[i] = NULL;
3951 +               }
3952 +       }
3953 +
3954 +       err = test_children_busy(root, bindex, verbose);
3955 +       if (unlikely(err)) {
3956 +               if (do_wh)
3957 +                       goto out_wh;
3958 +               goto out;
3959 +       }
3960 +
3961 +       err = 0;
3962 +       if (to_free) {
3963 +               /*
3964 +                * now we confirmed the branch is deletable.
3965 +                * let's free the remaining opened dirs on the branch.
3966 +                */
3967 +               di_write_unlock(root);
3968 +               br_del_file(to_free, opened, br_id);
3969 +               di_write_lock_child(root);
3970 +       }
3971 +
3972 +       sysaufs_brs_del(sb, bindex);    /* remove successors */
3973 +       dbgaufs_xino_del(br);           /* remove one */
3974 +       au_br_do_del(sb, bindex, br);
3975 +       sysaufs_brs_add(sb, bindex);    /* append successors */
3976 +       dbgaufs_brs_add(sb, bindex, /*topdown*/1);      /* rename successors */
3977 +
3978 +       if (!bindex) {
3979 +               au_cpup_attr_all(d_inode(root), /*force*/1);
3980 +               sb->s_maxbytes = au_sbr_sb(sb, 0)->s_maxbytes;
3981 +       } else
3982 +               au_sub_nlink(d_inode(root), d_inode(del->h_path.dentry));
3983 +       if (au_opt_test(mnt_flags, PLINK))
3984 +               au_plink_half_refresh(sb, br_id);
3985 +
3986 +       goto out; /* success */
3987 +
3988 +out_wh:
3989 +       /* revert */
3990 +       rerr = au_br_init_wh(sb, br, br->br_perm);
3991 +       if (rerr)
3992 +               pr_warn("failed re-creating base whiteout, %s. (%d)\n",
3993 +                       del->pathname, rerr);
3994 +out:
3995 +       if (to_free)
3996 +               au_farray_free(to_free, opened);
3997 +       return err;
3998 +}
3999 +
4000 +/* ---------------------------------------------------------------------- */
4001 +
4002 +static int au_ibusy(struct super_block *sb, struct aufs_ibusy __user *arg)
4003 +{
4004 +       int err;
4005 +       aufs_bindex_t btop, bbot;
4006 +       struct aufs_ibusy ibusy;
4007 +       struct inode *inode, *h_inode;
4008 +
4009 +       err = -EPERM;
4010 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
4011 +               goto out;
4012 +
4013 +       err = copy_from_user(&ibusy, arg, sizeof(ibusy));
4014 +       if (!err)
4015 +               /* VERIFY_WRITE */
4016 +               err = !access_ok(&arg->h_ino, sizeof(arg->h_ino));
4017 +       if (unlikely(err)) {
4018 +               err = -EFAULT;
4019 +               AuTraceErr(err);
4020 +               goto out;
4021 +       }
4022 +
4023 +       err = -EINVAL;
4024 +       si_read_lock(sb, AuLock_FLUSH);
4025 +       if (unlikely(ibusy.bindex < 0 || ibusy.bindex > au_sbbot(sb)))
4026 +               goto out_unlock;
4027 +
4028 +       err = 0;
4029 +       ibusy.h_ino = 0; /* invalid */
4030 +       inode = ilookup(sb, ibusy.ino);
4031 +       if (!inode
4032 +           || inode->i_ino == AUFS_ROOT_INO
4033 +           || au_is_bad_inode(inode))
4034 +               goto out_unlock;
4035 +
4036 +       ii_read_lock_child(inode);
4037 +       btop = au_ibtop(inode);
4038 +       bbot = au_ibbot(inode);
4039 +       if (btop <= ibusy.bindex && ibusy.bindex <= bbot) {
4040 +               h_inode = au_h_iptr(inode, ibusy.bindex);
4041 +               if (h_inode && au_test_ibusy(inode, btop, bbot))
4042 +                       ibusy.h_ino = h_inode->i_ino;
4043 +       }
4044 +       ii_read_unlock(inode);
4045 +       iput(inode);
4046 +
4047 +out_unlock:
4048 +       si_read_unlock(sb);
4049 +       if (!err) {
4050 +               err = __put_user(ibusy.h_ino, &arg->h_ino);
4051 +               if (unlikely(err)) {
4052 +                       err = -EFAULT;
4053 +                       AuTraceErr(err);
4054 +               }
4055 +       }
4056 +out:
4057 +       return err;
4058 +}
4059 +
4060 +long au_ibusy_ioctl(struct file *file, unsigned long arg)
4061 +{
4062 +       return au_ibusy(file->f_path.dentry->d_sb, (void __user *)arg);
4063 +}
4064 +
4065 +#ifdef CONFIG_COMPAT
4066 +long au_ibusy_compat_ioctl(struct file *file, unsigned long arg)
4067 +{
4068 +       return au_ibusy(file->f_path.dentry->d_sb, compat_ptr(arg));
4069 +}
4070 +#endif
4071 +
4072 +/* ---------------------------------------------------------------------- */
4073 +
4074 +/*
4075 + * change a branch permission
4076 + */
4077 +
4078 +static void au_warn_ima(void)
4079 +{
4080 +#ifdef CONFIG_IMA
4081 +       /* since it doesn't support mark_files_ro() */
4082 +       AuWarn1("RW -> RO makes IMA to produce wrong message\n");
4083 +#endif
4084 +}
4085 +
4086 +static int do_need_sigen_inc(int a, int b)
4087 +{
4088 +       return au_br_whable(a) && !au_br_whable(b);
4089 +}
4090 +
4091 +static int need_sigen_inc(int old, int new)
4092 +{
4093 +       return do_need_sigen_inc(old, new)
4094 +               || do_need_sigen_inc(new, old);
4095 +}
4096 +
4097 +static int au_br_mod_files_ro(struct super_block *sb, aufs_bindex_t bindex)
4098 +{
4099 +       int err, do_warn;
4100 +       unsigned int mnt_flags;
4101 +       unsigned long long ull, max;
4102 +       aufs_bindex_t br_id;
4103 +       unsigned char verbose, writer;
4104 +       struct file *file, *hf, **array;
4105 +       struct au_hfile *hfile;
4106 +       struct inode *h_inode;
4107 +
4108 +       mnt_flags = au_mntflags(sb);
4109 +       verbose = !!au_opt_test(mnt_flags, VERBOSE);
4110 +
4111 +       array = au_farray_alloc(sb, &max);
4112 +       err = PTR_ERR(array);
4113 +       if (IS_ERR(array))
4114 +               goto out;
4115 +
4116 +       do_warn = 0;
4117 +       br_id = au_sbr_id(sb, bindex);
4118 +       for (ull = 0; ull < max; ull++) {
4119 +               file = array[ull];
4120 +               if (unlikely(!file))
4121 +                       break;
4122 +
4123 +               /* AuDbg("%pD\n", file); */
4124 +               fi_read_lock(file);
4125 +               if (unlikely(au_test_mmapped(file))) {
4126 +                       err = -EBUSY;
4127 +                       AuVerbose(verbose, "mmapped %pD\n", file);
4128 +                       AuDbgFile(file);
4129 +                       FiMustNoWaiters(file);
4130 +                       fi_read_unlock(file);
4131 +                       goto out_array;
4132 +               }
4133 +
4134 +               hfile = &au_fi(file)->fi_htop;
4135 +               hf = hfile->hf_file;
4136 +               if (!d_is_reg(file->f_path.dentry)
4137 +                   || !(file->f_mode & FMODE_WRITE)
4138 +                   || hfile->hf_br->br_id != br_id
4139 +                   || !(hf->f_mode & FMODE_WRITE))
4140 +                       array[ull] = NULL;
4141 +               else {
4142 +                       do_warn = 1;
4143 +                       get_file(file);
4144 +               }
4145 +
4146 +               FiMustNoWaiters(file);
4147 +               fi_read_unlock(file);
4148 +               fput(file);
4149 +       }
4150 +
4151 +       err = 0;
4152 +       if (do_warn)
4153 +               au_warn_ima();
4154 +
4155 +       for (ull = 0; ull < max; ull++) {
4156 +               file = array[ull];
4157 +               if (!file)
4158 +                       continue;
4159 +
4160 +               /* todo: already flushed? */
4161 +               /*
4162 +                * fs/super.c:mark_files_ro() is gone, but aufs keeps its
4163 +                * approach which resets f_mode and calls mnt_drop_write() and
4164 +                * file_release_write() for each file, because the branch
4165 +                * attribute in aufs world is totally different from the native
4166 +                * fs rw/ro mode.
4167 +               */
4168 +               /* fi_read_lock(file); */
4169 +               hfile = &au_fi(file)->fi_htop;
4170 +               hf = hfile->hf_file;
4171 +               /* fi_read_unlock(file); */
4172 +               spin_lock(&hf->f_lock);
4173 +               writer = !!(hf->f_mode & FMODE_WRITER);
4174 +               hf->f_mode &= ~(FMODE_WRITE | FMODE_WRITER);
4175 +               spin_unlock(&hf->f_lock);
4176 +               if (writer) {
4177 +                       h_inode = file_inode(hf);
4178 +                       if (hf->f_mode & FMODE_READ)
4179 +                               i_readcount_inc(h_inode);
4180 +                       put_write_access(h_inode);
4181 +                       __mnt_drop_write(hf->f_path.mnt);
4182 +               }
4183 +       }
4184 +
4185 +out_array:
4186 +       au_farray_free(array, max);
4187 +out:
4188 +       AuTraceErr(err);
4189 +       return err;
4190 +}
4191 +
4192 +int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
4193 +             int *do_refresh)
4194 +{
4195 +       int err, rerr;
4196 +       aufs_bindex_t bindex;
4197 +       struct dentry *root;
4198 +       struct au_branch *br;
4199 +       struct au_br_fhsm *bf;
4200 +
4201 +       root = sb->s_root;
4202 +       bindex = au_find_dbindex(root, mod->h_root);
4203 +       if (bindex < 0) {
4204 +               if (remount)
4205 +                       return 0; /* success */
4206 +               err = -ENOENT;
4207 +               pr_err("%s no such branch\n", mod->path);
4208 +               goto out;
4209 +       }
4210 +       AuDbg("bindex b%d\n", bindex);
4211 +
4212 +       err = test_br(d_inode(mod->h_root), mod->perm, mod->path);
4213 +       if (unlikely(err))
4214 +               goto out;
4215 +
4216 +       br = au_sbr(sb, bindex);
4217 +       AuDebugOn(mod->h_root != au_br_dentry(br));
4218 +       if (br->br_perm == mod->perm)
4219 +               return 0; /* success */
4220 +
4221 +       /* pre-allocate for non-fhsm --> fhsm */
4222 +       bf = NULL;
4223 +       if (!au_br_fhsm(br->br_perm) && au_br_fhsm(mod->perm)) {
4224 +               err = au_fhsm_br_alloc(br);
4225 +               if (unlikely(err))
4226 +                       goto out;
4227 +               bf = br->br_fhsm;
4228 +               br->br_fhsm = NULL;
4229 +       }
4230 +
4231 +       if (au_br_writable(br->br_perm)) {
4232 +               /* remove whiteout base */
4233 +               err = au_br_init_wh(sb, br, mod->perm);
4234 +               if (unlikely(err))
4235 +                       goto out_bf;
4236 +
4237 +               if (!au_br_writable(mod->perm)) {
4238 +                       /* rw --> ro, file might be mmapped */
4239 +                       DiMustNoWaiters(root);
4240 +                       IiMustNoWaiters(d_inode(root));
4241 +                       di_write_unlock(root);
4242 +                       err = au_br_mod_files_ro(sb, bindex);
4243 +                       /* aufs_write_lock() calls ..._child() */
4244 +                       di_write_lock_child(root);
4245 +
4246 +                       if (unlikely(err)) {
4247 +                               rerr = -ENOMEM;
4248 +                               br->br_wbr = kzalloc(sizeof(*br->br_wbr),
4249 +                                                    GFP_NOFS);
4250 +                               if (br->br_wbr)
4251 +                                       rerr = au_wbr_init(br, sb, br->br_perm);
4252 +                               if (unlikely(rerr)) {
4253 +                                       AuIOErr("nested error %d (%d)\n",
4254 +                                               rerr, err);
4255 +                                       br->br_perm = mod->perm;
4256 +                               }
4257 +                       }
4258 +               }
4259 +       } else if (au_br_writable(mod->perm)) {
4260 +               /* ro --> rw */
4261 +               err = -ENOMEM;
4262 +               br->br_wbr = kzalloc(sizeof(*br->br_wbr), GFP_NOFS);
4263 +               if (br->br_wbr) {
4264 +                       err = au_wbr_init(br, sb, mod->perm);
4265 +                       if (unlikely(err)) {
4266 +                               au_kfree_rcu(br->br_wbr);
4267 +                               br->br_wbr = NULL;
4268 +                       }
4269 +               }
4270 +       }
4271 +       if (unlikely(err))
4272 +               goto out_bf;
4273 +
4274 +       if (au_br_fhsm(br->br_perm)) {
4275 +               if (!au_br_fhsm(mod->perm)) {
4276 +                       /* fhsm --> non-fhsm */
4277 +                       au_br_fhsm_fin(br->br_fhsm);
4278 +                       au_kfree_rcu(br->br_fhsm);
4279 +                       br->br_fhsm = NULL;
4280 +               }
4281 +       } else if (au_br_fhsm(mod->perm))
4282 +               /* non-fhsm --> fhsm */
4283 +               br->br_fhsm = bf;
4284 +
4285 +       *do_refresh |= need_sigen_inc(br->br_perm, mod->perm);
4286 +       br->br_perm = mod->perm;
4287 +       goto out; /* success */
4288 +
4289 +out_bf:
4290 +       au_kfree_try_rcu(bf);
4291 +out:
4292 +       AuTraceErr(err);
4293 +       return err;
4294 +}
4295 +
4296 +/* ---------------------------------------------------------------------- */
4297 +
4298 +int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs)
4299 +{
4300 +       int err;
4301 +       struct kstatfs kstfs;
4302 +
4303 +       err = vfs_statfs(&br->br_path, &kstfs);
4304 +       if (!err) {
4305 +               stfs->f_blocks = kstfs.f_blocks;
4306 +               stfs->f_bavail = kstfs.f_bavail;
4307 +               stfs->f_files = kstfs.f_files;
4308 +               stfs->f_ffree = kstfs.f_ffree;
4309 +       }
4310 +
4311 +       return err;
4312 +}
4313 diff -urN /usr/share/empty/fs/aufs/branch.h linux/fs/aufs/branch.h
4314 --- /usr/share/empty/fs/aufs/branch.h   1970-01-01 01:00:00.000000000 +0100
4315 +++ linux/fs/aufs/branch.h      2022-03-21 14:49:05.719966344 +0100
4316 @@ -0,0 +1,375 @@
4317 +/* SPDX-License-Identifier: GPL-2.0 */
4318 +/*
4319 + * Copyright (C) 2005-2021 Junjiro R. Okajima
4320 + *
4321 + * This program, aufs is free software; you can redistribute it and/or modify
4322 + * it under the terms of the GNU General Public License as published by
4323 + * the Free Software Foundation; either version 2 of the License, or
4324 + * (at your option) any later version.
4325 + *
4326 + * This program is distributed in the hope that it will be useful,
4327 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
4328 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
4329 + * GNU General Public License for more details.
4330 + *
4331 + * You should have received a copy of the GNU General Public License
4332 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
4333 + */
4334 +
4335 +/*
4336 + * branch filesystems and xino for them
4337 + */
4338 +
4339 +#ifndef __AUFS_BRANCH_H__
4340 +#define __AUFS_BRANCH_H__
4341 +
4342 +#ifdef __KERNEL__
4343 +
4344 +#include <linux/mount.h>
4345 +#include "dirren.h"
4346 +#include "dynop.h"
4347 +#include "lcnt.h"
4348 +#include "rwsem.h"
4349 +#include "super.h"
4350 +
4351 +/* ---------------------------------------------------------------------- */
4352 +
4353 +/* a xino file */
4354 +struct au_xino {
4355 +       struct file             **xi_file;
4356 +       unsigned int            xi_nfile;
4357 +
4358 +       struct {
4359 +               spinlock_t              spin;
4360 +               ino_t                   *array;
4361 +               int                     total;
4362 +               /* reserved for future use */
4363 +               /* unsigned long        *bitmap; */
4364 +               wait_queue_head_t       wqh;
4365 +       } xi_nondir;
4366 +
4367 +       struct mutex            xi_mtx; /* protects xi_file array */
4368 +       struct hlist_bl_head    xi_writing;
4369 +
4370 +       atomic_t                xi_truncating;
4371 +
4372 +       struct kref             xi_kref;
4373 +};
4374 +
4375 +/* File-based Hierarchical Storage Management */
4376 +struct au_br_fhsm {
4377 +#ifdef CONFIG_AUFS_FHSM
4378 +       struct mutex            bf_lock;
4379 +       unsigned long           bf_jiffy;
4380 +       struct aufs_stfs        bf_stfs;
4381 +       int                     bf_readable;
4382 +#endif
4383 +};
4384 +
4385 +/* members for writable branch only */
4386 +enum {AuBrWh_BASE, AuBrWh_PLINK, AuBrWh_ORPH, AuBrWh_Last};
4387 +struct au_wbr {
4388 +       struct au_rwsem         wbr_wh_rwsem;
4389 +       struct dentry           *wbr_wh[AuBrWh_Last];
4390 +       atomic_t                wbr_wh_running;
4391 +#define wbr_whbase             wbr_wh[AuBrWh_BASE]     /* whiteout base */
4392 +#define wbr_plink              wbr_wh[AuBrWh_PLINK]    /* pseudo-link dir */
4393 +#define wbr_orph               wbr_wh[AuBrWh_ORPH]     /* dir for orphans */
4394 +
4395 +       /* mfs mode */
4396 +       unsigned long long      wbr_bytes;
4397 +};
4398 +
4399 +/* ext2 has 3 types of operations at least, ext3 has 4 */
4400 +#define AuBrDynOp (AuDyLast * 4)
4401 +
4402 +#ifdef CONFIG_AUFS_HFSNOTIFY
4403 +/* support for asynchronous destruction */
4404 +struct au_br_hfsnotify {
4405 +       struct fsnotify_group   *hfsn_group;
4406 +};
4407 +#endif
4408 +
4409 +/* sysfs entries */
4410 +struct au_brsysfs {
4411 +       char                    name[16];
4412 +       struct attribute        attr;
4413 +};
4414 +
4415 +enum {
4416 +       AuBrSysfs_BR,
4417 +       AuBrSysfs_BRID,
4418 +       AuBrSysfs_Last
4419 +};
4420 +
4421 +/* protected by superblock rwsem */
4422 +struct au_branch {
4423 +       struct au_xino          *br_xino;
4424 +
4425 +       aufs_bindex_t           br_id;
4426 +
4427 +       int                     br_perm;
4428 +       struct path             br_path;
4429 +       spinlock_t              br_dykey_lock;
4430 +       struct au_dykey         *br_dykey[AuBrDynOp];
4431 +       au_lcnt_t               br_nfiles;      /* opened files */
4432 +       au_lcnt_t               br_count;       /* in-use for other */
4433 +
4434 +       struct au_wbr           *br_wbr;
4435 +       struct au_br_fhsm       *br_fhsm;
4436 +
4437 +#ifdef CONFIG_AUFS_HFSNOTIFY
4438 +       struct au_br_hfsnotify  *br_hfsn;
4439 +#endif
4440 +
4441 +#ifdef CONFIG_SYSFS
4442 +       /* entries under sysfs per mount-point */
4443 +       struct au_brsysfs       br_sysfs[AuBrSysfs_Last];
4444 +#endif
4445 +
4446 +#ifdef CONFIG_DEBUG_FS
4447 +       struct dentry            *br_dbgaufs; /* xino */
4448 +#endif
4449 +
4450 +       struct au_dr_br         br_dirren;
4451 +};
4452 +
4453 +/* ---------------------------------------------------------------------- */
4454 +
4455 +static inline struct vfsmount *au_br_mnt(struct au_branch *br)
4456 +{
4457 +       return br->br_path.mnt;
4458 +}
4459 +
4460 +static inline struct dentry *au_br_dentry(struct au_branch *br)
4461 +{
4462 +       return br->br_path.dentry;
4463 +}
4464 +
4465 +static inline struct user_namespace *au_br_userns(struct au_branch *br)
4466 +{
4467 +       return mnt_user_ns(br->br_path.mnt);
4468 +}
4469 +
4470 +static inline struct super_block *au_br_sb(struct au_branch *br)
4471 +{
4472 +       return au_br_mnt(br)->mnt_sb;
4473 +}
4474 +
4475 +static inline int au_br_rdonly(struct au_branch *br)
4476 +{
4477 +       return (sb_rdonly(au_br_sb(br))
4478 +               || !au_br_writable(br->br_perm))
4479 +               ? -EROFS : 0;
4480 +}
4481 +
4482 +static inline int au_br_hnotifyable(int brperm __maybe_unused)
4483 +{
4484 +#ifdef CONFIG_AUFS_HNOTIFY
4485 +       return !(brperm & AuBrPerm_RR);
4486 +#else
4487 +       return 0;
4488 +#endif
4489 +}
4490 +
4491 +static inline int au_br_test_oflag(int oflag, struct au_branch *br)
4492 +{
4493 +       int err, exec_flag;
4494 +
4495 +       err = 0;
4496 +       exec_flag = oflag & __FMODE_EXEC;
4497 +       if (unlikely(exec_flag && path_noexec(&br->br_path)))
4498 +               err = -EACCES;
4499 +
4500 +       return err;
4501 +}
4502 +
4503 +static inline void au_xino_get(struct au_branch *br)
4504 +{
4505 +       struct au_xino *xi;
4506 +
4507 +       xi = br->br_xino;
4508 +       if (xi)
4509 +               kref_get(&xi->xi_kref);
4510 +}
4511 +
4512 +static inline int au_xino_count(struct au_branch *br)
4513 +{
4514 +       int v;
4515 +       struct au_xino *xi;
4516 +
4517 +       v = 0;
4518 +       xi = br->br_xino;
4519 +       if (xi)
4520 +               v = kref_read(&xi->xi_kref);
4521 +
4522 +       return v;
4523 +}
4524 +
4525 +/* ---------------------------------------------------------------------- */
4526 +
4527 +/* branch.c */
4528 +struct au_sbinfo;
4529 +void au_br_free(struct au_sbinfo *sinfo);
4530 +int au_br_index(struct super_block *sb, aufs_bindex_t br_id);
4531 +struct au_opt_add;
4532 +int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount);
4533 +struct au_opt_del;
4534 +int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount);
4535 +long au_ibusy_ioctl(struct file *file, unsigned long arg);
4536 +#ifdef CONFIG_COMPAT
4537 +long au_ibusy_compat_ioctl(struct file *file, unsigned long arg);
4538 +#endif
4539 +struct au_opt_mod;
4540 +int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
4541 +             int *do_refresh);
4542 +struct aufs_stfs;
4543 +int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs);
4544 +
4545 +/* xino.c */
4546 +static const loff_t au_loff_max = LLONG_MAX;
4547 +
4548 +aufs_bindex_t au_xi_root(struct super_block *sb, struct dentry *dentry);
4549 +struct file *au_xino_create(struct super_block *sb, char *fpath, int silent,
4550 +                           int wbrtop);
4551 +struct file *au_xino_create2(struct super_block *sb, struct path *base,
4552 +                            struct file *copy_src);
4553 +struct au_xi_new {
4554 +       struct au_xino *xi;     /* switch between xino and xigen */
4555 +       int idx;
4556 +       struct path *base;
4557 +       struct file *copy_src;
4558 +};
4559 +struct file *au_xi_new(struct super_block *sb, struct au_xi_new *xinew);
4560 +
4561 +int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4562 +                ino_t *ino);
4563 +int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4564 +                 ino_t ino);
4565 +ssize_t xino_fread(struct file *file, void *buf, size_t size, loff_t *pos);
4566 +ssize_t xino_fwrite(struct file *file, void *buf, size_t size, loff_t *pos);
4567 +
4568 +int au_xib_trunc(struct super_block *sb);
4569 +int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex, int idx_begin);
4570 +
4571 +struct au_xino *au_xino_alloc(unsigned int nfile);
4572 +int au_xino_put(struct au_branch *br);
4573 +struct file *au_xino_file1(struct au_xino *xi);
4574 +
4575 +struct au_opt_xino;
4576 +void au_xino_clr(struct super_block *sb);
4577 +int au_xino_set(struct super_block *sb, struct au_opt_xino *xiopt, int remount);
4578 +struct file *au_xino_def(struct super_block *sb);
4579 +int au_xino_init_br(struct super_block *sb, struct au_branch *br, ino_t hino,
4580 +                   struct path *base);
4581 +
4582 +ino_t au_xino_new_ino(struct super_block *sb);
4583 +void au_xino_delete_inode(struct inode *inode, const int unlinked);
4584 +
4585 +void au_xinondir_leave(struct super_block *sb, aufs_bindex_t bindex,
4586 +                      ino_t h_ino, int idx);
4587 +int au_xinondir_enter(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4588 +                     int *idx);
4589 +
4590 +int au_xino_path(struct seq_file *seq, struct file *file);
4591 +
4592 +/* ---------------------------------------------------------------------- */
4593 +
4594 +/* @idx is signed to accept -1 meaning the first file */
4595 +static inline struct file *au_xino_file(struct au_xino *xi, int idx)
4596 +{
4597 +       struct file *file;
4598 +
4599 +       file = NULL;
4600 +       if (!xi)
4601 +               goto out;
4602 +
4603 +       if (idx >= 0) {
4604 +               if (idx < xi->xi_nfile)
4605 +                       file = xi->xi_file[idx];
4606 +       } else
4607 +               file = au_xino_file1(xi);
4608 +
4609 +out:
4610 +       return file;
4611 +}
4612 +
4613 +/* ---------------------------------------------------------------------- */
4614 +
4615 +/* Superblock to branch */
4616 +static inline
4617 +aufs_bindex_t au_sbr_id(struct super_block *sb, aufs_bindex_t bindex)
4618 +{
4619 +       return au_sbr(sb, bindex)->br_id;
4620 +}
4621 +
4622 +static inline
4623 +struct vfsmount *au_sbr_mnt(struct super_block *sb, aufs_bindex_t bindex)
4624 +{
4625 +       return au_br_mnt(au_sbr(sb, bindex));
4626 +}
4627 +
4628 +static inline
4629 +struct user_namespace *au_sbr_userns(struct super_block *sb, aufs_bindex_t bindex)
4630 +{
4631 +       return au_br_userns(au_sbr(sb, bindex));
4632 +}
4633 +
4634 +static inline
4635 +struct super_block *au_sbr_sb(struct super_block *sb, aufs_bindex_t bindex)
4636 +{
4637 +       return au_br_sb(au_sbr(sb, bindex));
4638 +}
4639 +
4640 +static inline int au_sbr_perm(struct super_block *sb, aufs_bindex_t bindex)
4641 +{
4642 +       return au_sbr(sb, bindex)->br_perm;
4643 +}
4644 +
4645 +static inline int au_sbr_whable(struct super_block *sb, aufs_bindex_t bindex)
4646 +{
4647 +       return au_br_whable(au_sbr_perm(sb, bindex));
4648 +}
4649 +
4650 +/* ---------------------------------------------------------------------- */
4651 +
4652 +#define wbr_wh_read_lock(wbr)  au_rw_read_lock(&(wbr)->wbr_wh_rwsem)
4653 +#define wbr_wh_write_lock(wbr) au_rw_write_lock(&(wbr)->wbr_wh_rwsem)
4654 +#define wbr_wh_read_trylock(wbr)       au_rw_read_trylock(&(wbr)->wbr_wh_rwsem)
4655 +#define wbr_wh_write_trylock(wbr) au_rw_write_trylock(&(wbr)->wbr_wh_rwsem)
4656 +/*
4657 +#define wbr_wh_read_trylock_nested(wbr) \
4658 +       au_rw_read_trylock_nested(&(wbr)->wbr_wh_rwsem)
4659 +#define wbr_wh_write_trylock_nested(wbr) \
4660 +       au_rw_write_trylock_nested(&(wbr)->wbr_wh_rwsem)
4661 +*/
4662 +
4663 +#define wbr_wh_read_unlock(wbr)        au_rw_read_unlock(&(wbr)->wbr_wh_rwsem)
4664 +#define wbr_wh_write_unlock(wbr)       au_rw_write_unlock(&(wbr)->wbr_wh_rwsem)
4665 +#define wbr_wh_downgrade_lock(wbr)     au_rw_dgrade_lock(&(wbr)->wbr_wh_rwsem)
4666 +
4667 +#define WbrWhMustNoWaiters(wbr)        AuRwMustNoWaiters(&(wbr)->wbr_wh_rwsem)
4668 +#define WbrWhMustAnyLock(wbr)  AuRwMustAnyLock(&(wbr)->wbr_wh_rwsem)
4669 +#define WbrWhMustWriteLock(wbr)        AuRwMustWriteLock(&(wbr)->wbr_wh_rwsem)
4670 +
4671 +/* ---------------------------------------------------------------------- */
4672 +
4673 +#ifdef CONFIG_AUFS_FHSM
4674 +static inline void au_br_fhsm_init(struct au_br_fhsm *brfhsm)
4675 +{
4676 +       mutex_init(&brfhsm->bf_lock);
4677 +       brfhsm->bf_jiffy = 0;
4678 +       brfhsm->bf_readable = 0;
4679 +}
4680 +
4681 +static inline void au_br_fhsm_fin(struct au_br_fhsm *brfhsm)
4682 +{
4683 +       mutex_destroy(&brfhsm->bf_lock);
4684 +}
4685 +#else
4686 +AuStubVoid(au_br_fhsm_init, struct au_br_fhsm *brfhsm)
4687 +AuStubVoid(au_br_fhsm_fin, struct au_br_fhsm *brfhsm)
4688 +#endif
4689 +
4690 +#endif /* __KERNEL__ */
4691 +#endif /* __AUFS_BRANCH_H__ */
4692 diff -urN /usr/share/empty/fs/aufs/conf.mk linux/fs/aufs/conf.mk
4693 --- /usr/share/empty/fs/aufs/conf.mk    1970-01-01 01:00:00.000000000 +0100
4694 +++ linux/fs/aufs/conf.mk       2022-03-21 14:49:05.719966344 +0100
4695 @@ -0,0 +1,40 @@
4696 +# SPDX-License-Identifier: GPL-2.0
4697 +
4698 +AuConfStr = CONFIG_AUFS_FS=${CONFIG_AUFS_FS}
4699 +
4700 +define AuConf
4701 +ifdef ${1}
4702 +AuConfStr += ${1}=${${1}}
4703 +endif
4704 +endef
4705 +
4706 +AuConfAll = BRANCH_MAX_127 BRANCH_MAX_511 BRANCH_MAX_1023 BRANCH_MAX_32767 \
4707 +       SBILIST \
4708 +       HNOTIFY HFSNOTIFY \
4709 +       EXPORT INO_T_64 \
4710 +       XATTR \
4711 +       FHSM \
4712 +       RDU \
4713 +       DIRREN \
4714 +       SHWH \
4715 +       BR_RAMFS \
4716 +       BR_FUSE POLL \
4717 +       BR_HFSPLUS \
4718 +       BDEV_LOOP \
4719 +       DEBUG MAGIC_SYSRQ
4720 +$(foreach i, ${AuConfAll}, \
4721 +       $(eval $(call AuConf,CONFIG_AUFS_${i})))
4722 +
4723 +AuConfName = ${obj}/conf.str
4724 +${AuConfName}.tmp: FORCE
4725 +       @echo ${AuConfStr} | tr ' ' '\n' | sed -e 's/^/"/' -e 's/$$/\\n"/' > $@
4726 +${AuConfName}: ${AuConfName}.tmp
4727 +       @diff -q $< $@ > /dev/null 2>&1 || { \
4728 +       echo '  GEN    ' $@; \
4729 +       cp -p $< $@; \
4730 +       }
4731 +FORCE:
4732 +clean-files += ${AuConfName} ${AuConfName}.tmp
4733 +${obj}/sysfs.o: ${AuConfName}
4734 +
4735 +-include ${srctree}/${src}/conf_priv.mk
4736 diff -urN /usr/share/empty/fs/aufs/cpup.c linux/fs/aufs/cpup.c
4737 --- /usr/share/empty/fs/aufs/cpup.c     1970-01-01 01:00:00.000000000 +0100
4738 +++ linux/fs/aufs/cpup.c        2022-03-21 14:49:05.719966344 +0100
4739 @@ -0,0 +1,1459 @@
4740 +// SPDX-License-Identifier: GPL-2.0
4741 +/*
4742 + * Copyright (C) 2005-2021 Junjiro R. Okajima
4743 + *
4744 + * This program, aufs is free software; you can redistribute it and/or modify
4745 + * it under the terms of the GNU General Public License as published by
4746 + * the Free Software Foundation; either version 2 of the License, or
4747 + * (at your option) any later version.
4748 + *
4749 + * This program is distributed in the hope that it will be useful,
4750 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
4751 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
4752 + * GNU General Public License for more details.
4753 + *
4754 + * You should have received a copy of the GNU General Public License
4755 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
4756 + */
4757 +
4758 +/*
4759 + * copy-up functions, see wbr_policy.c for copy-down
4760 + */
4761 +
4762 +#include <linux/fs_stack.h>
4763 +#include <linux/mm.h>
4764 +#include <linux/task_work.h>
4765 +#include "aufs.h"
4766 +
4767 +void au_cpup_attr_flags(struct inode *dst, unsigned int iflags)
4768 +{
4769 +       const unsigned int mask = S_DEAD | S_SWAPFILE | S_PRIVATE
4770 +               | S_NOATIME | S_NOCMTIME | S_AUTOMOUNT;
4771 +
4772 +       BUILD_BUG_ON(sizeof(iflags) != sizeof(dst->i_flags));
4773 +
4774 +       dst->i_flags |= iflags & ~mask;
4775 +       if (au_test_fs_notime(dst->i_sb))
4776 +               dst->i_flags |= S_NOATIME | S_NOCMTIME;
4777 +}
4778 +
4779 +void au_cpup_attr_timesizes(struct inode *inode)
4780 +{
4781 +       struct inode *h_inode;
4782 +
4783 +       h_inode = au_h_iptr(inode, au_ibtop(inode));
4784 +       fsstack_copy_attr_times(inode, h_inode);
4785 +       fsstack_copy_inode_size(inode, h_inode);
4786 +}
4787 +
4788 +void au_cpup_attr_nlink(struct inode *inode, int force)
4789 +{
4790 +       struct inode *h_inode;
4791 +       struct super_block *sb;
4792 +       aufs_bindex_t bindex, bbot;
4793 +
4794 +       sb = inode->i_sb;
4795 +       bindex = au_ibtop(inode);
4796 +       h_inode = au_h_iptr(inode, bindex);
4797 +       if (!force
4798 +           && !S_ISDIR(h_inode->i_mode)
4799 +           && au_opt_test(au_mntflags(sb), PLINK)
4800 +           && au_plink_test(inode))
4801 +               return;
4802 +
4803 +       /*
4804 +        * 0 can happen in revalidating.
4805 +        * h_inode->i_mutex may not be held here, but it is harmless since once
4806 +        * i_nlink reaches 0, it will never become positive except O_TMPFILE
4807 +        * case.
4808 +        * todo: O_TMPFILE+linkat(AT_SYMLINK_FOLLOW) bypassing aufs may cause
4809 +        *       the incorrect link count.
4810 +        */
4811 +       set_nlink(inode, h_inode->i_nlink);
4812 +
4813 +       /*
4814 +        * fewer nlink makes find(1) noisy, but larger nlink doesn't.
4815 +        * it may includes whplink directory.
4816 +        */
4817 +       if (S_ISDIR(h_inode->i_mode)) {
4818 +               bbot = au_ibbot(inode);
4819 +               for (bindex++; bindex <= bbot; bindex++) {
4820 +                       h_inode = au_h_iptr(inode, bindex);
4821 +                       if (h_inode)
4822 +                               au_add_nlink(inode, h_inode);
4823 +               }
4824 +       }
4825 +}
4826 +
4827 +void au_cpup_attr_changeable(struct inode *inode)
4828 +{
4829 +       struct inode *h_inode;
4830 +
4831 +       h_inode = au_h_iptr(inode, au_ibtop(inode));
4832 +       inode->i_mode = h_inode->i_mode;
4833 +       inode->i_uid = h_inode->i_uid;
4834 +       inode->i_gid = h_inode->i_gid;
4835 +       au_cpup_attr_timesizes(inode);
4836 +       au_cpup_attr_flags(inode, h_inode->i_flags);
4837 +}
4838 +
4839 +void au_cpup_igen(struct inode *inode, struct inode *h_inode)
4840 +{
4841 +       struct au_iinfo *iinfo = au_ii(inode);
4842 +
4843 +       IiMustWriteLock(inode);
4844 +
4845 +       iinfo->ii_higen = h_inode->i_generation;
4846 +       iinfo->ii_hsb1 = h_inode->i_sb;
4847 +}
4848 +
4849 +void au_cpup_attr_all(struct inode *inode, int force)
4850 +{
4851 +       struct inode *h_inode;
4852 +
4853 +       h_inode = au_h_iptr(inode, au_ibtop(inode));
4854 +       au_cpup_attr_changeable(inode);
4855 +       if (inode->i_nlink > 0)
4856 +               au_cpup_attr_nlink(inode, force);
4857 +       inode->i_rdev = h_inode->i_rdev;
4858 +       inode->i_blkbits = h_inode->i_blkbits;
4859 +       au_cpup_igen(inode, h_inode);
4860 +}
4861 +
4862 +/* ---------------------------------------------------------------------- */
4863 +
4864 +/* Note: dt_dentry and dt_h_dentry are not dget/dput-ed */
4865 +
4866 +/* keep the timestamps of the parent dir when cpup */
4867 +void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
4868 +                   struct path *h_path)
4869 +{
4870 +       struct inode *h_inode;
4871 +
4872 +       dt->dt_dentry = dentry;
4873 +       dt->dt_h_path = *h_path;
4874 +       h_inode = d_inode(h_path->dentry);
4875 +       dt->dt_atime = h_inode->i_atime;
4876 +       dt->dt_mtime = h_inode->i_mtime;
4877 +       /* smp_mb(); */
4878 +}
4879 +
4880 +void au_dtime_revert(struct au_dtime *dt)
4881 +{
4882 +       struct iattr attr;
4883 +       int err;
4884 +
4885 +       attr.ia_atime = dt->dt_atime;
4886 +       attr.ia_mtime = dt->dt_mtime;
4887 +       attr.ia_valid = ATTR_FORCE | ATTR_MTIME | ATTR_MTIME_SET
4888 +               | ATTR_ATIME | ATTR_ATIME_SET;
4889 +
4890 +       /* no delegation since this is a directory */
4891 +       err = vfsub_notify_change(&dt->dt_h_path, &attr, /*delegated*/NULL);
4892 +       if (unlikely(err))
4893 +               pr_warn("restoring timestamps failed(%d). ignored\n", err);
4894 +}
4895 +
4896 +/* ---------------------------------------------------------------------- */
4897 +
4898 +/* internal use only */
4899 +struct au_cpup_reg_attr {
4900 +       int             valid;
4901 +       struct kstat    st;
4902 +       unsigned int    iflags; /* inode->i_flags */
4903 +};
4904 +
4905 +static noinline_for_stack
4906 +int cpup_iattr(struct dentry *dst, aufs_bindex_t bindex, struct path *h_src,
4907 +              struct au_cpup_reg_attr *h_src_attr)
4908 +{
4909 +       int err, sbits, icex;
4910 +       unsigned int mnt_flags;
4911 +       unsigned char verbose;
4912 +       struct iattr ia;
4913 +       struct path h_path;
4914 +       struct inode *h_isrc, *h_idst;
4915 +       struct kstat *h_st;
4916 +       struct au_branch *br;
4917 +
4918 +       br = au_sbr(dst->d_sb, bindex);
4919 +       h_path.mnt = au_br_mnt(br);
4920 +       h_path.dentry = au_h_dptr(dst, bindex);
4921 +       h_idst = d_inode(h_path.dentry);
4922 +       h_isrc = d_inode(h_src->dentry);
4923 +       ia.ia_valid = ATTR_FORCE | ATTR_UID | ATTR_GID
4924 +               | ATTR_ATIME | ATTR_MTIME
4925 +               | ATTR_ATIME_SET | ATTR_MTIME_SET;
4926 +       if (h_src_attr && h_src_attr->valid) {
4927 +               h_st = &h_src_attr->st;
4928 +               ia.ia_uid = h_st->uid;
4929 +               ia.ia_gid = h_st->gid;
4930 +               ia.ia_atime = h_st->atime;
4931 +               ia.ia_mtime = h_st->mtime;
4932 +               if (h_idst->i_mode != h_st->mode
4933 +                   && !S_ISLNK(h_idst->i_mode)) {
4934 +                       ia.ia_valid |= ATTR_MODE;
4935 +                       ia.ia_mode = h_st->mode;
4936 +               }
4937 +               sbits = !!(h_st->mode & (S_ISUID | S_ISGID));
4938 +               au_cpup_attr_flags(h_idst, h_src_attr->iflags);
4939 +       } else {
4940 +               ia.ia_uid = h_isrc->i_uid;
4941 +               ia.ia_gid = h_isrc->i_gid;
4942 +               ia.ia_atime = h_isrc->i_atime;
4943 +               ia.ia_mtime = h_isrc->i_mtime;
4944 +               if (h_idst->i_mode != h_isrc->i_mode
4945 +                   && !S_ISLNK(h_idst->i_mode)) {
4946 +                       ia.ia_valid |= ATTR_MODE;
4947 +                       ia.ia_mode = h_isrc->i_mode;
4948 +               }
4949 +               sbits = !!(h_isrc->i_mode & (S_ISUID | S_ISGID));
4950 +               au_cpup_attr_flags(h_idst, h_isrc->i_flags);
4951 +       }
4952 +       /* no delegation since it is just created */
4953 +       err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
4954 +
4955 +       /* is this nfs only? */
4956 +       if (!err && sbits && au_test_nfs(h_path.dentry->d_sb)) {
4957 +               ia.ia_valid = ATTR_FORCE | ATTR_MODE;
4958 +               ia.ia_mode = h_isrc->i_mode;
4959 +               err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
4960 +       }
4961 +
4962 +       icex = br->br_perm & AuBrAttr_ICEX;
4963 +       if (!err) {
4964 +               mnt_flags = au_mntflags(dst->d_sb);
4965 +               verbose = !!au_opt_test(mnt_flags, VERBOSE);
4966 +               err = au_cpup_xattr(&h_path, h_src, icex, verbose);
4967 +       }
4968 +
4969 +       return err;
4970 +}
4971 +
4972 +/* ---------------------------------------------------------------------- */
4973 +
4974 +static int au_do_copy_file(struct file *dst, struct file *src, loff_t len,
4975 +                          char *buf, unsigned long blksize)
4976 +{
4977 +       int err;
4978 +       size_t sz, rbytes, wbytes;
4979 +       unsigned char all_zero;
4980 +       char *p, *zp;
4981 +       struct inode *h_inode;
4982 +       /* reduce stack usage */
4983 +       struct iattr *ia;
4984 +
4985 +       zp = page_address(ZERO_PAGE(0));
4986 +       if (unlikely(!zp))
4987 +               return -ENOMEM; /* possible? */
4988 +
4989 +       err = 0;
4990 +       all_zero = 0;
4991 +       while (len) {
4992 +               AuDbg("len %lld\n", len);
4993 +               sz = blksize;
4994 +               if (len < blksize)
4995 +                       sz = len;
4996 +
4997 +               rbytes = 0;
4998 +               /* todo: signal_pending? */
4999 +               while (!rbytes || err == -EAGAIN || err == -EINTR) {
5000 +                       rbytes = vfsub_read_k(src, buf, sz, &src->f_pos);
5001 +                       err = rbytes;
5002 +               }
5003 +               if (unlikely(err < 0))
5004 +                       break;
5005 +
5006 +               all_zero = 0;
5007 +               if (len >= rbytes && rbytes == blksize)
5008 +                       all_zero = !memcmp(buf, zp, rbytes);
5009 +               if (!all_zero) {
5010 +                       wbytes = rbytes;
5011 +                       p = buf;
5012 +                       while (wbytes) {
5013 +                               size_t b;
5014 +
5015 +                               b = vfsub_write_k(dst, p, wbytes, &dst->f_pos);
5016 +                               err = b;
5017 +                               /* todo: signal_pending? */
5018 +                               if (unlikely(err == -EAGAIN || err == -EINTR))
5019 +                                       continue;
5020 +                               if (unlikely(err < 0))
5021 +                                       break;
5022 +                               wbytes -= b;
5023 +                               p += b;
5024 +                       }
5025 +                       if (unlikely(err < 0))
5026 +                               break;
5027 +               } else {
5028 +                       loff_t res;
5029 +
5030 +                       AuLabel(hole);
5031 +                       res = vfsub_llseek(dst, rbytes, SEEK_CUR);
5032 +                       err = res;
5033 +                       if (unlikely(res < 0))
5034 +                               break;
5035 +               }
5036 +               len -= rbytes;
5037 +               err = 0;
5038 +       }
5039 +
5040 +       /* the last block may be a hole */
5041 +       if (!err && all_zero) {
5042 +               AuLabel(last hole);
5043 +
5044 +               err = 1;
5045 +               if (au_test_nfs(dst->f_path.dentry->d_sb)) {
5046 +                       /* nfs requires this step to make last hole */
5047 +                       /* is this only nfs? */
5048 +                       do {
5049 +                               /* todo: signal_pending? */
5050 +                               err = vfsub_write_k(dst, "\0", 1, &dst->f_pos);
5051 +                       } while (err == -EAGAIN || err == -EINTR);
5052 +                       if (err == 1)
5053 +                               dst->f_pos--;
5054 +               }
5055 +
5056 +               if (err == 1) {
5057 +                       ia = (void *)buf;
5058 +                       ia->ia_size = dst->f_pos;
5059 +                       ia->ia_valid = ATTR_SIZE | ATTR_FILE;
5060 +                       ia->ia_file = dst;
5061 +                       h_inode = file_inode(dst);
5062 +                       inode_lock_nested(h_inode, AuLsc_I_CHILD2);
5063 +                       /* no delegation since it is just created */
5064 +                       err = vfsub_notify_change(&dst->f_path, ia,
5065 +                                                 /*delegated*/NULL);
5066 +                       inode_unlock(h_inode);
5067 +               }
5068 +       }
5069 +
5070 +       return err;
5071 +}
5072 +
5073 +int au_copy_file(struct file *dst, struct file *src, loff_t len)
5074 +{
5075 +       int err;
5076 +       unsigned long blksize;
5077 +       unsigned char do_kfree;
5078 +       char *buf;
5079 +       struct super_block *h_sb;
5080 +
5081 +       err = -ENOMEM;
5082 +       h_sb = file_inode(dst)->i_sb;
5083 +       blksize = h_sb->s_blocksize;
5084 +       if (!blksize || PAGE_SIZE < blksize)
5085 +               blksize = PAGE_SIZE;
5086 +       AuDbg("blksize %lu\n", blksize);
5087 +       do_kfree = (blksize != PAGE_SIZE && blksize >= sizeof(struct iattr *));
5088 +       if (do_kfree)
5089 +               buf = kmalloc(blksize, GFP_NOFS);
5090 +       else
5091 +               buf = (void *)__get_free_page(GFP_NOFS);
5092 +       if (unlikely(!buf))
5093 +               goto out;
5094 +
5095 +       if (len > (1 << 22))
5096 +               AuDbg("copying a large file %lld\n", (long long)len);
5097 +
5098 +       src->f_pos = 0;
5099 +       dst->f_pos = 0;
5100 +       err = au_do_copy_file(dst, src, len, buf, blksize);
5101 +       if (do_kfree) {
5102 +               AuDebugOn(!au_kfree_do_sz_test(blksize));
5103 +               au_kfree_do_rcu(buf);
5104 +       } else
5105 +               free_page((unsigned long)buf);
5106 +
5107 +out:
5108 +       return err;
5109 +}
5110 +
5111 +static int au_do_copy(struct file *dst, struct file *src, loff_t len)
5112 +{
5113 +       int err;
5114 +       struct super_block *h_src_sb;
5115 +       struct inode *h_src_inode;
5116 +
5117 +       h_src_inode = file_inode(src);
5118 +       h_src_sb = h_src_inode->i_sb;
5119 +
5120 +       /* XFS acquires inode_lock */
5121 +       if (!au_test_xfs(h_src_sb))
5122 +               err = au_copy_file(dst, src, len);
5123 +       else {
5124 +               inode_unlock_shared(h_src_inode);
5125 +               err = au_copy_file(dst, src, len);
5126 +               inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5127 +       }
5128 +
5129 +       return err;
5130 +}
5131 +
5132 +static int au_clone_or_copy(struct file *dst, struct file *src, loff_t len)
5133 +{
5134 +       int err;
5135 +       loff_t lo;
5136 +       struct super_block *h_src_sb;
5137 +       struct inode *h_src_inode;
5138 +
5139 +       h_src_inode = file_inode(src);
5140 +       h_src_sb = h_src_inode->i_sb;
5141 +       if (h_src_sb != file_inode(dst)->i_sb
5142 +           || !dst->f_op->remap_file_range) {
5143 +               err = au_do_copy(dst, src, len);
5144 +               goto out;
5145 +       }
5146 +
5147 +       if (!au_test_nfs(h_src_sb)) {
5148 +               inode_unlock_shared(h_src_inode);
5149 +               lo = vfsub_clone_file_range(src, dst, len);
5150 +               inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5151 +       } else
5152 +               lo = vfsub_clone_file_range(src, dst, len);
5153 +       if (lo == len) {
5154 +               err = 0;
5155 +               goto out; /* success */
5156 +       } else if (lo >= 0)
5157 +               /* todo: possible? */
5158 +               /* paritially succeeded */
5159 +               AuDbg("lo %lld, len %lld. Retrying.\n", lo, len);
5160 +       else if (lo != -EOPNOTSUPP) {
5161 +               /* older XFS has a condition in cloning */
5162 +               err = lo;
5163 +               goto out;
5164 +       }
5165 +
5166 +       /* the backend fs on NFS may not support cloning */
5167 +       err = au_do_copy(dst, src, len);
5168 +
5169 +out:
5170 +       AuTraceErr(err);
5171 +       return err;
5172 +}
5173 +
5174 +/*
5175 + * to support a sparse file which is opened with O_APPEND,
5176 + * we need to close the file.
5177 + */
5178 +static int au_cp_regular(struct au_cp_generic *cpg)
5179 +{
5180 +       int err, i;
5181 +       enum { SRC, DST };
5182 +       struct {
5183 +               aufs_bindex_t bindex;
5184 +               unsigned int flags;
5185 +               struct dentry *dentry;
5186 +               int force_wr;
5187 +               struct file *file;
5188 +       } *f, file[] = {
5189 +               {
5190 +                       .bindex = cpg->bsrc,
5191 +                       .flags = O_RDONLY | O_NOATIME | O_LARGEFILE,
5192 +               },
5193 +               {
5194 +                       .bindex = cpg->bdst,
5195 +                       .flags = O_WRONLY | O_NOATIME | O_LARGEFILE,
5196 +                       .force_wr = !!au_ftest_cpup(cpg->flags, RWDST),
5197 +               }
5198 +       };
5199 +       struct au_branch *br;
5200 +       struct super_block *sb, *h_src_sb;
5201 +       struct inode *h_src_inode;
5202 +       struct task_struct *tsk = current;
5203 +
5204 +       /* bsrc branch can be ro/rw. */
5205 +       sb = cpg->dentry->d_sb;
5206 +       f = file;
5207 +       for (i = 0; i < 2; i++, f++) {
5208 +               f->dentry = au_h_dptr(cpg->dentry, f->bindex);
5209 +               f->file = au_h_open(cpg->dentry, f->bindex, f->flags,
5210 +                                   /*file*/NULL, f->force_wr);
5211 +               if (IS_ERR(f->file)) {
5212 +                       err = PTR_ERR(f->file);
5213 +                       if (i == SRC)
5214 +                               goto out;
5215 +                       else
5216 +                               goto out_src;
5217 +               }
5218 +       }
5219 +
5220 +       /* try stopping to update while we copyup */
5221 +       h_src_inode = d_inode(file[SRC].dentry);
5222 +       h_src_sb = h_src_inode->i_sb;
5223 +       if (!au_test_nfs(h_src_sb))
5224 +               IMustLock(h_src_inode);
5225 +       err = au_clone_or_copy(file[DST].file, file[SRC].file, cpg->len);
5226 +
5227 +       /* i wonder if we had O_NO_DELAY_FPUT flag */
5228 +       if (tsk->flags & PF_KTHREAD)
5229 +               __fput_sync(file[DST].file);
5230 +       else {
5231 +               /* it happened actually */
5232 +               fput(file[DST].file);
5233 +               /*
5234 +                * too bad.
5235 +                * we have to call both since we don't know which place the file
5236 +                * was added to.
5237 +                */
5238 +               task_work_run();
5239 +               flush_delayed_fput();
5240 +       }
5241 +       br = au_sbr(sb, file[DST].bindex);
5242 +       au_lcnt_dec(&br->br_nfiles);
5243 +
5244 +out_src:
5245 +       fput(file[SRC].file);
5246 +       br = au_sbr(sb, file[SRC].bindex);
5247 +       au_lcnt_dec(&br->br_nfiles);
5248 +out:
5249 +       return err;
5250 +}
5251 +
5252 +static int au_do_cpup_regular(struct au_cp_generic *cpg,
5253 +                             struct au_cpup_reg_attr *h_src_attr)
5254 +{
5255 +       int err, rerr;
5256 +       loff_t l;
5257 +       struct path h_path;
5258 +       struct inode *h_src_inode, *h_dst_inode;
5259 +
5260 +       err = 0;
5261 +       h_src_inode = au_h_iptr(d_inode(cpg->dentry), cpg->bsrc);
5262 +       l = i_size_read(h_src_inode);
5263 +       if (cpg->len == -1 || l < cpg->len)
5264 +               cpg->len = l;
5265 +       if (cpg->len) {
5266 +               /* try stopping to update while we are referencing */
5267 +               inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5268 +               au_pin_hdir_unlock(cpg->pin);
5269 +
5270 +               h_path.dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
5271 +               h_path.mnt = au_sbr_mnt(cpg->dentry->d_sb, cpg->bsrc);
5272 +               h_src_attr->iflags = h_src_inode->i_flags;
5273 +               if (!au_test_nfs(h_src_inode->i_sb))
5274 +                       err = vfsub_getattr(&h_path, &h_src_attr->st);
5275 +               else {
5276 +                       inode_unlock_shared(h_src_inode);
5277 +                       err = vfsub_getattr(&h_path, &h_src_attr->st);
5278 +                       inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5279 +               }
5280 +               if (unlikely(err)) {
5281 +                       inode_unlock_shared(h_src_inode);
5282 +                       goto out;
5283 +               }
5284 +               h_src_attr->valid = 1;
5285 +               if (!au_test_nfs(h_src_inode->i_sb)) {
5286 +                       err = au_cp_regular(cpg);
5287 +                       inode_unlock_shared(h_src_inode);
5288 +               } else {
5289 +                       inode_unlock_shared(h_src_inode);
5290 +                       err = au_cp_regular(cpg);
5291 +               }
5292 +               rerr = au_pin_hdir_relock(cpg->pin);
5293 +               if (!err && rerr)
5294 +                       err = rerr;
5295 +       }
5296 +       if (!err && (h_src_inode->i_state & I_LINKABLE)) {
5297 +               h_path.dentry = au_h_dptr(cpg->dentry, cpg->bdst);
5298 +               h_dst_inode = d_inode(h_path.dentry);
5299 +               spin_lock(&h_dst_inode->i_lock);
5300 +               h_dst_inode->i_state |= I_LINKABLE;
5301 +               spin_unlock(&h_dst_inode->i_lock);
5302 +       }
5303 +
5304 +out:
5305 +       return err;
5306 +}
5307 +
5308 +static int au_do_cpup_symlink(struct path *h_path, struct dentry *h_src,
5309 +                             struct inode *h_dir)
5310 +{
5311 +       int err;
5312 +       DEFINE_DELAYED_CALL(done);
5313 +       const char *sym;
5314 +
5315 +       sym = vfs_get_link(h_src, &done);
5316 +       err = PTR_ERR(sym);
5317 +       if (IS_ERR(sym))
5318 +               goto out;
5319 +
5320 +       err = vfsub_symlink(h_dir, h_path, sym);
5321 +
5322 +out:
5323 +       do_delayed_call(&done);
5324 +       return err;
5325 +}
5326 +
5327 +/*
5328 + * regardless 'acl' option, reset all ACL.
5329 + * All ACL will be copied up later from the original entry on the lower branch.
5330 + */
5331 +static int au_reset_acl(struct inode *h_dir, struct path *h_path, umode_t mode)
5332 +{
5333 +       int err;
5334 +       struct dentry *h_dentry;
5335 +       struct inode *h_inode;
5336 +       struct user_namespace *h_userns;
5337 +
5338 +       h_userns = mnt_user_ns(h_path->mnt);
5339 +       h_dentry = h_path->dentry;
5340 +       h_inode = d_inode(h_dentry);
5341 +       /* forget_all_cached_acls(h_inode)); */
5342 +       err = vfsub_removexattr(h_userns, h_dentry, XATTR_NAME_POSIX_ACL_ACCESS);
5343 +       AuTraceErr(err);
5344 +       if (err == -EOPNOTSUPP)
5345 +               err = 0;
5346 +       if (!err)
5347 +               err = vfsub_acl_chmod(h_userns, h_inode, mode);
5348 +
5349 +       AuTraceErr(err);
5350 +       return err;
5351 +}
5352 +
5353 +static int au_do_cpup_dir(struct au_cp_generic *cpg, struct dentry *dst_parent,
5354 +                         struct inode *h_dir, struct path *h_path)
5355 +{
5356 +       int err;
5357 +       struct inode *dir, *inode;
5358 +       struct user_namespace *h_userns;
5359 +
5360 +       h_userns = mnt_user_ns(h_path->mnt);
5361 +       err = vfsub_removexattr(h_userns, h_path->dentry,
5362 +                               XATTR_NAME_POSIX_ACL_DEFAULT);
5363 +       AuTraceErr(err);
5364 +       if (err == -EOPNOTSUPP)
5365 +               err = 0;
5366 +       if (unlikely(err))
5367 +               goto out;
5368 +
5369 +       /*
5370 +        * strange behaviour from the users view,
5371 +        * particularly setattr case
5372 +        */
5373 +       dir = d_inode(dst_parent);
5374 +       if (au_ibtop(dir) == cpg->bdst)
5375 +               au_cpup_attr_nlink(dir, /*force*/1);
5376 +       inode = d_inode(cpg->dentry);
5377 +       au_cpup_attr_nlink(inode, /*force*/1);
5378 +
5379 +out:
5380 +       return err;
5381 +}
5382 +
5383 +static noinline_for_stack
5384 +int cpup_entry(struct au_cp_generic *cpg, struct dentry *dst_parent,
5385 +              struct au_cpup_reg_attr *h_src_attr)
5386 +{
5387 +       int err;
5388 +       umode_t mode;
5389 +       unsigned int mnt_flags;
5390 +       unsigned char isdir, isreg, force;
5391 +       const unsigned char do_dt = !!au_ftest_cpup(cpg->flags, DTIME);
5392 +       struct au_dtime dt;
5393 +       struct path h_path;
5394 +       struct dentry *h_src, *h_dst, *h_parent;
5395 +       struct inode *h_inode, *h_dir;
5396 +       struct super_block *sb;
5397 +
5398 +       /* bsrc branch can be ro/rw. */
5399 +       h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
5400 +       h_inode = d_inode(h_src);
5401 +       AuDebugOn(h_inode != au_h_iptr(d_inode(cpg->dentry), cpg->bsrc));
5402 +
5403 +       /* try stopping to be referenced while we are creating */
5404 +       h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
5405 +       if (au_ftest_cpup(cpg->flags, RENAME))
5406 +               AuDebugOn(strncmp(h_dst->d_name.name, AUFS_WH_PFX,
5407 +                                 AUFS_WH_PFX_LEN));
5408 +       h_parent = h_dst->d_parent; /* dir inode is locked */
5409 +       h_dir = d_inode(h_parent);
5410 +       IMustLock(h_dir);
5411 +       AuDebugOn(h_parent != h_dst->d_parent);
5412 +
5413 +       sb = cpg->dentry->d_sb;
5414 +       h_path.mnt = au_sbr_mnt(sb, cpg->bdst);
5415 +       if (do_dt) {
5416 +               h_path.dentry = h_parent;
5417 +               au_dtime_store(&dt, dst_parent, &h_path);
5418 +       }
5419 +       h_path.dentry = h_dst;
5420 +
5421 +       isreg = 0;
5422 +       isdir = 0;
5423 +       mode = h_inode->i_mode;
5424 +       switch (mode & S_IFMT) {
5425 +       case S_IFREG:
5426 +               isreg = 1;
5427 +               err = vfsub_create(h_dir, &h_path, 0600, /*want_excl*/true);
5428 +               if (!err)
5429 +                       err = au_do_cpup_regular(cpg, h_src_attr);
5430 +               break;
5431 +       case S_IFDIR:
5432 +               isdir = 1;
5433 +               err = vfsub_mkdir(h_dir, &h_path, mode);
5434 +               if (!err)
5435 +                       err = au_do_cpup_dir(cpg, dst_parent, h_dir, &h_path);
5436 +               break;
5437 +       case S_IFLNK:
5438 +               err = au_do_cpup_symlink(&h_path, h_src, h_dir);
5439 +               break;
5440 +       case S_IFCHR:
5441 +       case S_IFBLK:
5442 +               AuDebugOn(!capable(CAP_MKNOD));
5443 +               fallthrough;
5444 +       case S_IFIFO:
5445 +       case S_IFSOCK:
5446 +               err = vfsub_mknod(h_dir, &h_path, mode, h_inode->i_rdev);
5447 +               break;
5448 +       default:
5449 +               AuIOErr("Unknown inode type 0%o\n", mode);
5450 +               err = -EIO;
5451 +       }
5452 +       if (!err)
5453 +               err = au_reset_acl(h_dir, &h_path, mode);
5454 +
5455 +       mnt_flags = au_mntflags(sb);
5456 +       if (!au_opt_test(mnt_flags, UDBA_NONE)
5457 +           && !isdir
5458 +           && au_opt_test(mnt_flags, XINO)
5459 +           && (h_inode->i_nlink == 1
5460 +               || (h_inode->i_state & I_LINKABLE))
5461 +           /* todo: unnecessary? */
5462 +           /* && d_inode(cpg->dentry)->i_nlink == 1 */
5463 +           && cpg->bdst < cpg->bsrc
5464 +           && !au_ftest_cpup(cpg->flags, KEEPLINO))
5465 +               au_xino_write(sb, cpg->bsrc, h_inode->i_ino, /*ino*/0);
5466 +               /* ignore this error */
5467 +
5468 +       if (!err) {
5469 +               force = 0;
5470 +               if (isreg) {
5471 +                       force = !!cpg->len;
5472 +                       if (cpg->len == -1)
5473 +                               force = !!i_size_read(h_inode);
5474 +               }
5475 +               au_fhsm_wrote(sb, cpg->bdst, force);
5476 +       }
5477 +
5478 +       if (do_dt)
5479 +               au_dtime_revert(&dt);
5480 +       return err;
5481 +}
5482 +
5483 +static int au_do_ren_after_cpup(struct au_cp_generic *cpg, struct path *h_path)
5484 +{
5485 +       int err;
5486 +       struct dentry *dentry, *h_dentry, *h_parent, *parent;
5487 +       struct path h_ppath;
5488 +       struct inode *h_dir;
5489 +       aufs_bindex_t bdst;
5490 +
5491 +       dentry = cpg->dentry;
5492 +       bdst = cpg->bdst;
5493 +       h_ppath.mnt = au_sbr_mnt(dentry->d_sb, bdst);
5494 +       h_dentry = au_h_dptr(dentry, bdst);
5495 +       if (!au_ftest_cpup(cpg->flags, OVERWRITE)) {
5496 +               dget(h_dentry);
5497 +               au_set_h_dptr(dentry, bdst, NULL);
5498 +               err = au_lkup_neg(dentry, bdst, /*wh*/0);
5499 +               if (!err)
5500 +                       h_path->dentry = dget(au_h_dptr(dentry, bdst));
5501 +               au_set_h_dptr(dentry, bdst, h_dentry);
5502 +       } else {
5503 +               err = 0;
5504 +               parent = dget_parent(dentry);
5505 +               h_ppath.dentry = au_h_dptr(parent, bdst);
5506 +               dput(parent);
5507 +               h_path->dentry = vfsub_lkup_one(&dentry->d_name, &h_ppath);
5508 +               if (IS_ERR(h_path->dentry))
5509 +                       err = PTR_ERR(h_path->dentry);
5510 +       }
5511 +       if (unlikely(err))
5512 +               goto out;
5513 +
5514 +       h_parent = h_dentry->d_parent; /* dir inode is locked */
5515 +       h_dir = d_inode(h_parent);
5516 +       IMustLock(h_dir);
5517 +       AuDbg("%pd %pd\n", h_dentry, h_path->dentry);
5518 +       /* no delegation since it is just created */
5519 +       err = vfsub_rename(h_dir, h_dentry, h_dir, h_path, /*delegated*/NULL,
5520 +                          /*flags*/0);
5521 +       dput(h_path->dentry);
5522 +
5523 +out:
5524 +       return err;
5525 +}
5526 +
5527 +/*
5528 + * copyup the @dentry from @bsrc to @bdst.
5529 + * the caller must set the both of lower dentries.
5530 + * @len is for truncating when it is -1 copyup the entire file.
5531 + * in link/rename cases, @dst_parent may be different from the real one.
5532 + * basic->bsrc can be larger than basic->bdst.
5533 + * aufs doesn't touch the credential so
5534 + * security_inode_copy_up{,_xattr}() are unnecessary.
5535 + */
5536 +static int au_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
5537 +{
5538 +       int err, rerr;
5539 +       aufs_bindex_t old_ibtop;
5540 +       unsigned char isdir, plink;
5541 +       struct dentry *h_src, *h_dst, *h_parent;
5542 +       struct inode *dst_inode, *h_dir, *inode, *delegated, *src_inode;
5543 +       struct super_block *sb;
5544 +       struct au_branch *br;
5545 +       struct path h_src_path;
5546 +       /* to reduce stack size */
5547 +       struct {
5548 +               struct au_dtime dt;
5549 +               struct path h_path;
5550 +               struct au_cpup_reg_attr h_src_attr;
5551 +       } *a;
5552 +
5553 +       err = -ENOMEM;
5554 +       a = kmalloc(sizeof(*a), GFP_NOFS);
5555 +       if (unlikely(!a))
5556 +               goto out;
5557 +       a->h_src_attr.valid = 0;
5558 +
5559 +       sb = cpg->dentry->d_sb;
5560 +       br = au_sbr(sb, cpg->bdst);
5561 +       a->h_path.mnt = au_br_mnt(br);
5562 +       h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
5563 +       h_parent = h_dst->d_parent; /* dir inode is locked */
5564 +       h_dir = d_inode(h_parent);
5565 +       IMustLock(h_dir);
5566 +
5567 +       h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
5568 +       inode = d_inode(cpg->dentry);
5569 +
5570 +       if (!dst_parent)
5571 +               dst_parent = dget_parent(cpg->dentry);
5572 +       else
5573 +               dget(dst_parent);
5574 +
5575 +       plink = !!au_opt_test(au_mntflags(sb), PLINK);
5576 +       dst_inode = au_h_iptr(inode, cpg->bdst);
5577 +       if (dst_inode) {
5578 +               if (unlikely(!plink)) {
5579 +                       err = -EIO;
5580 +                       AuIOErr("hi%lu(i%lu) exists on b%d "
5581 +                               "but plink is disabled\n",
5582 +                               dst_inode->i_ino, inode->i_ino, cpg->bdst);
5583 +                       goto out_parent;
5584 +               }
5585 +
5586 +               if (dst_inode->i_nlink) {
5587 +                       const int do_dt = au_ftest_cpup(cpg->flags, DTIME);
5588 +
5589 +                       h_src = au_plink_lkup(inode, cpg->bdst);
5590 +                       err = PTR_ERR(h_src);
5591 +                       if (IS_ERR(h_src))
5592 +                               goto out_parent;
5593 +                       if (unlikely(d_is_negative(h_src))) {
5594 +                               err = -EIO;
5595 +                               AuIOErr("i%lu exists on b%d "
5596 +                                       "but not pseudo-linked\n",
5597 +                                       inode->i_ino, cpg->bdst);
5598 +                               dput(h_src);
5599 +                               goto out_parent;
5600 +                       }
5601 +
5602 +                       if (do_dt) {
5603 +                               a->h_path.dentry = h_parent;
5604 +                               au_dtime_store(&a->dt, dst_parent, &a->h_path);
5605 +                       }
5606 +
5607 +                       a->h_path.dentry = h_dst;
5608 +                       delegated = NULL;
5609 +                       err = vfsub_link(h_src, h_dir, &a->h_path, &delegated);
5610 +                       if (!err && au_ftest_cpup(cpg->flags, RENAME))
5611 +                               err = au_do_ren_after_cpup(cpg, &a->h_path);
5612 +                       if (do_dt)
5613 +                               au_dtime_revert(&a->dt);
5614 +                       if (unlikely(err == -EWOULDBLOCK)) {
5615 +                               pr_warn("cannot retry for NFSv4 delegation"
5616 +                                       " for an internal link\n");
5617 +                               iput(delegated);
5618 +                       }
5619 +                       dput(h_src);
5620 +                       goto out_parent;
5621 +               } else
5622 +                       /* todo: cpup_wh_file? */
5623 +                       /* udba work */
5624 +                       au_update_ibrange(inode, /*do_put_zero*/1);
5625 +       }
5626 +
5627 +       isdir = S_ISDIR(inode->i_mode);
5628 +       old_ibtop = au_ibtop(inode);
5629 +       err = cpup_entry(cpg, dst_parent, &a->h_src_attr);
5630 +       if (unlikely(err))
5631 +               goto out_rev;
5632 +       dst_inode = d_inode(h_dst);
5633 +       inode_lock_nested(dst_inode, AuLsc_I_CHILD2);
5634 +       /* todo: necessary? */
5635 +       /* au_pin_hdir_unlock(cpg->pin); */
5636 +
5637 +       h_src_path.dentry = h_src;
5638 +       h_src_path.mnt = au_sbr_mnt(sb, cpg->bsrc);
5639 +       err = cpup_iattr(cpg->dentry, cpg->bdst, &h_src_path, &a->h_src_attr);
5640 +       if (unlikely(err)) {
5641 +               /* todo: necessary? */
5642 +               /* au_pin_hdir_relock(cpg->pin); */ /* ignore an error */
5643 +               inode_unlock(dst_inode);
5644 +               goto out_rev;
5645 +       }
5646 +
5647 +       if (cpg->bdst < old_ibtop) {
5648 +               if (S_ISREG(inode->i_mode)) {
5649 +                       err = au_dy_iaop(inode, cpg->bdst, dst_inode);
5650 +                       if (unlikely(err)) {
5651 +                               /* ignore an error */
5652 +                               /* au_pin_hdir_relock(cpg->pin); */
5653 +                               inode_unlock(dst_inode);
5654 +                               goto out_rev;
5655 +                       }
5656 +               }
5657 +               au_set_ibtop(inode, cpg->bdst);
5658 +       } else
5659 +               au_set_ibbot(inode, cpg->bdst);
5660 +       au_set_h_iptr(inode, cpg->bdst, au_igrab(dst_inode),
5661 +                     au_hi_flags(inode, isdir));
5662 +
5663 +       /* todo: necessary? */
5664 +       /* err = au_pin_hdir_relock(cpg->pin); */
5665 +       inode_unlock(dst_inode);
5666 +       if (unlikely(err))
5667 +               goto out_rev;
5668 +
5669 +       src_inode = d_inode(h_src);
5670 +       if (!isdir
5671 +           && (src_inode->i_nlink > 1
5672 +               || src_inode->i_state & I_LINKABLE)
5673 +           && plink)
5674 +               au_plink_append(inode, cpg->bdst, h_dst);
5675 +
5676 +       if (au_ftest_cpup(cpg->flags, RENAME)) {
5677 +               a->h_path.dentry = h_dst;
5678 +               err = au_do_ren_after_cpup(cpg, &a->h_path);
5679 +       }
5680 +       if (!err)
5681 +               goto out_parent; /* success */
5682 +
5683 +       /* revert */
5684 +out_rev:
5685 +       a->h_path.dentry = h_parent;
5686 +       au_dtime_store(&a->dt, dst_parent, &a->h_path);
5687 +       a->h_path.dentry = h_dst;
5688 +       rerr = 0;
5689 +       if (d_is_positive(h_dst)) {
5690 +               if (!isdir) {
5691 +                       /* no delegation since it is just created */
5692 +                       rerr = vfsub_unlink(h_dir, &a->h_path,
5693 +                                           /*delegated*/NULL, /*force*/0);
5694 +               } else
5695 +                       rerr = vfsub_rmdir(h_dir, &a->h_path);
5696 +       }
5697 +       au_dtime_revert(&a->dt);
5698 +       if (rerr) {
5699 +               AuIOErr("failed removing broken entry(%d, %d)\n", err, rerr);
5700 +               err = -EIO;
5701 +       }
5702 +out_parent:
5703 +       dput(dst_parent);
5704 +       au_kfree_rcu(a);
5705 +out:
5706 +       return err;
5707 +}
5708 +
5709 +#if 0 /* reserved */
5710 +struct au_cpup_single_args {
5711 +       int *errp;
5712 +       struct au_cp_generic *cpg;
5713 +       struct dentry *dst_parent;
5714 +};
5715 +
5716 +static void au_call_cpup_single(void *args)
5717 +{
5718 +       struct au_cpup_single_args *a = args;
5719 +
5720 +       au_pin_hdir_acquire_nest(a->cpg->pin);
5721 +       *a->errp = au_cpup_single(a->cpg, a->dst_parent);
5722 +       au_pin_hdir_release(a->cpg->pin);
5723 +}
5724 +#endif
5725 +
5726 +/*
5727 + * prevent SIGXFSZ in copy-up.
5728 + * testing CAP_MKNOD is for generic fs,
5729 + * but CAP_FSETID is for xfs only, currently.
5730 + */
5731 +static int au_cpup_sio_test(struct au_pin *pin, umode_t mode)
5732 +{
5733 +       int do_sio;
5734 +       struct super_block *sb;
5735 +       struct inode *h_dir;
5736 +
5737 +       do_sio = 0;
5738 +       sb = au_pinned_parent(pin)->d_sb;
5739 +       if (!au_wkq_test()
5740 +           && (!au_sbi(sb)->si_plink_maint_pid
5741 +               || au_plink_maint(sb, AuLock_NOPLM))) {
5742 +               switch (mode & S_IFMT) {
5743 +               case S_IFREG:
5744 +                       /* no condition about RLIMIT_FSIZE and the file size */
5745 +                       do_sio = 1;
5746 +                       break;
5747 +               case S_IFCHR:
5748 +               case S_IFBLK:
5749 +                       do_sio = !capable(CAP_MKNOD);
5750 +                       break;
5751 +               }
5752 +               if (!do_sio)
5753 +                       do_sio = ((mode & (S_ISUID | S_ISGID))
5754 +                                 && !capable(CAP_FSETID));
5755 +               /* this workaround may be removed in the future */
5756 +               if (!do_sio) {
5757 +                       h_dir = au_pinned_h_dir(pin);
5758 +                       do_sio = h_dir->i_mode & S_ISVTX;
5759 +               }
5760 +       }
5761 +
5762 +       return do_sio;
5763 +}
5764 +
5765 +#if 0 /* reserved */
5766 +int au_sio_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
5767 +{
5768 +       int err, wkq_err;
5769 +       struct dentry *h_dentry;
5770 +
5771 +       h_dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
5772 +       if (!au_cpup_sio_test(pin, d_inode(h_dentry)->i_mode))
5773 +               err = au_cpup_single(cpg, dst_parent);
5774 +       else {
5775 +               struct au_cpup_single_args args = {
5776 +                       .errp           = &err,
5777 +                       .cpg            = cpg,
5778 +                       .dst_parent     = dst_parent
5779 +               };
5780 +               wkq_err = au_wkq_wait(au_call_cpup_single, &args);
5781 +               if (unlikely(wkq_err))
5782 +                       err = wkq_err;
5783 +       }
5784 +
5785 +       return err;
5786 +}
5787 +#endif
5788 +
5789 +/*
5790 + * copyup the @dentry from the first active lower branch to @bdst,
5791 + * using au_cpup_single().
5792 + */
5793 +static int au_cpup_simple(struct au_cp_generic *cpg)
5794 +{
5795 +       int err;
5796 +       unsigned int flags_orig;
5797 +       struct dentry *dentry;
5798 +
5799 +       AuDebugOn(cpg->bsrc < 0);
5800 +
5801 +       dentry = cpg->dentry;
5802 +       DiMustWriteLock(dentry);
5803 +
5804 +       err = au_lkup_neg(dentry, cpg->bdst, /*wh*/1);
5805 +       if (!err) {
5806 +               flags_orig = cpg->flags;
5807 +               au_fset_cpup(cpg->flags, RENAME);
5808 +               err = au_cpup_single(cpg, NULL);
5809 +               cpg->flags = flags_orig;
5810 +               if (!err)
5811 +                       return 0; /* success */
5812 +
5813 +               /* revert */
5814 +               au_set_h_dptr(dentry, cpg->bdst, NULL);
5815 +               au_set_dbtop(dentry, cpg->bsrc);
5816 +       }
5817 +
5818 +       return err;
5819 +}
5820 +
5821 +struct au_cpup_simple_args {
5822 +       int *errp;
5823 +       struct au_cp_generic *cpg;
5824 +};
5825 +
5826 +static void au_call_cpup_simple(void *args)
5827 +{
5828 +       struct au_cpup_simple_args *a = args;
5829 +
5830 +       au_pin_hdir_acquire_nest(a->cpg->pin);
5831 +       *a->errp = au_cpup_simple(a->cpg);
5832 +       au_pin_hdir_release(a->cpg->pin);
5833 +}
5834 +
5835 +static int au_do_sio_cpup_simple(struct au_cp_generic *cpg)
5836 +{
5837 +       int err, wkq_err;
5838 +       struct dentry *dentry, *parent;
5839 +       struct file *h_file;
5840 +       struct inode *h_dir;
5841 +       struct user_namespace *h_userns;
5842 +
5843 +       dentry = cpg->dentry;
5844 +       h_file = NULL;
5845 +       if (au_ftest_cpup(cpg->flags, HOPEN)) {
5846 +               AuDebugOn(cpg->bsrc < 0);
5847 +               h_file = au_h_open_pre(dentry, cpg->bsrc, /*force_wr*/0);
5848 +               err = PTR_ERR(h_file);
5849 +               if (IS_ERR(h_file))
5850 +                       goto out;
5851 +       }
5852 +
5853 +       parent = dget_parent(dentry);
5854 +       h_dir = au_h_iptr(d_inode(parent), cpg->bdst);
5855 +       h_userns = au_sbr_userns(dentry->d_sb, cpg->bdst);
5856 +       if (!au_test_h_perm_sio(h_userns, h_dir, MAY_EXEC | MAY_WRITE)
5857 +           && !au_cpup_sio_test(cpg->pin, d_inode(dentry)->i_mode))
5858 +               err = au_cpup_simple(cpg);
5859 +       else {
5860 +               struct au_cpup_simple_args args = {
5861 +                       .errp           = &err,
5862 +                       .cpg            = cpg
5863 +               };
5864 +               wkq_err = au_wkq_wait(au_call_cpup_simple, &args);
5865 +               if (unlikely(wkq_err))
5866 +                       err = wkq_err;
5867 +       }
5868 +
5869 +       dput(parent);
5870 +       if (h_file)
5871 +               au_h_open_post(dentry, cpg->bsrc, h_file);
5872 +
5873 +out:
5874 +       return err;
5875 +}
5876 +
5877 +int au_sio_cpup_simple(struct au_cp_generic *cpg)
5878 +{
5879 +       aufs_bindex_t bsrc, bbot;
5880 +       struct dentry *dentry, *h_dentry;
5881 +
5882 +       if (cpg->bsrc < 0) {
5883 +               dentry = cpg->dentry;
5884 +               bbot = au_dbbot(dentry);
5885 +               for (bsrc = cpg->bdst + 1; bsrc <= bbot; bsrc++) {
5886 +                       h_dentry = au_h_dptr(dentry, bsrc);
5887 +                       if (h_dentry) {
5888 +                               AuDebugOn(d_is_negative(h_dentry));
5889 +                               break;
5890 +                       }
5891 +               }
5892 +               AuDebugOn(bsrc > bbot);
5893 +               cpg->bsrc = bsrc;
5894 +       }
5895 +       AuDebugOn(cpg->bsrc <= cpg->bdst);
5896 +       return au_do_sio_cpup_simple(cpg);
5897 +}
5898 +
5899 +int au_sio_cpdown_simple(struct au_cp_generic *cpg)
5900 +{
5901 +       AuDebugOn(cpg->bdst <= cpg->bsrc);
5902 +       return au_do_sio_cpup_simple(cpg);
5903 +}
5904 +
5905 +/* ---------------------------------------------------------------------- */
5906 +
5907 +/*
5908 + * copyup the deleted file for writing.
5909 + */
5910 +static int au_do_cpup_wh(struct au_cp_generic *cpg, struct dentry *wh_dentry,
5911 +                        struct file *file)
5912 +{
5913 +       int err;
5914 +       unsigned int flags_orig;
5915 +       aufs_bindex_t bsrc_orig;
5916 +       struct au_dinfo *dinfo;
5917 +       struct {
5918 +               struct au_hdentry *hd;
5919 +               struct dentry *h_dentry;
5920 +       } hdst, hsrc;
5921 +
5922 +       dinfo = au_di(cpg->dentry);
5923 +       AuRwMustWriteLock(&dinfo->di_rwsem);
5924 +
5925 +       bsrc_orig = cpg->bsrc;
5926 +       cpg->bsrc = dinfo->di_btop;
5927 +       hdst.hd = au_hdentry(dinfo, cpg->bdst);
5928 +       hdst.h_dentry = hdst.hd->hd_dentry;
5929 +       hdst.hd->hd_dentry = wh_dentry;
5930 +       dinfo->di_btop = cpg->bdst;
5931 +
5932 +       hsrc.h_dentry = NULL;
5933 +       if (file) {
5934 +               hsrc.hd = au_hdentry(dinfo, cpg->bsrc);
5935 +               hsrc.h_dentry = hsrc.hd->hd_dentry;
5936 +               hsrc.hd->hd_dentry = au_hf_top(file)->f_path.dentry;
5937 +       }
5938 +       flags_orig = cpg->flags;
5939 +       cpg->flags = !AuCpup_DTIME;
5940 +       err = au_cpup_single(cpg, /*h_parent*/NULL);
5941 +       cpg->flags = flags_orig;
5942 +       if (file) {
5943 +               if (!err)
5944 +                       err = au_reopen_nondir(file);
5945 +               hsrc.hd->hd_dentry = hsrc.h_dentry;
5946 +       }
5947 +       hdst.hd->hd_dentry = hdst.h_dentry;
5948 +       dinfo->di_btop = cpg->bsrc;
5949 +       cpg->bsrc = bsrc_orig;
5950 +
5951 +       return err;
5952 +}
5953 +
5954 +static int au_cpup_wh(struct au_cp_generic *cpg, struct file *file)
5955 +{
5956 +       int err;
5957 +       aufs_bindex_t bdst;
5958 +       struct au_dtime dt;
5959 +       struct dentry *dentry, *parent, *h_parent, *wh_dentry;
5960 +       struct au_branch *br;
5961 +       struct path h_path;
5962 +
5963 +       dentry = cpg->dentry;
5964 +       bdst = cpg->bdst;
5965 +       br = au_sbr(dentry->d_sb, bdst);
5966 +       parent = dget_parent(dentry);
5967 +       h_parent = au_h_dptr(parent, bdst);
5968 +       wh_dentry = au_whtmp_lkup(h_parent, br, &dentry->d_name);
5969 +       err = PTR_ERR(wh_dentry);
5970 +       if (IS_ERR(wh_dentry))
5971 +               goto out;
5972 +
5973 +       h_path.dentry = h_parent;
5974 +       h_path.mnt = au_br_mnt(br);
5975 +       au_dtime_store(&dt, parent, &h_path);
5976 +       err = au_do_cpup_wh(cpg, wh_dentry, file);
5977 +       if (unlikely(err))
5978 +               goto out_wh;
5979 +
5980 +       dget(wh_dentry);
5981 +       h_path.dentry = wh_dentry;
5982 +       if (!d_is_dir(wh_dentry)) {
5983 +               /* no delegation since it is just created */
5984 +               err = vfsub_unlink(d_inode(h_parent), &h_path,
5985 +                                  /*delegated*/NULL, /*force*/0);
5986 +       } else
5987 +               err = vfsub_rmdir(d_inode(h_parent), &h_path);
5988 +       if (unlikely(err)) {
5989 +               AuIOErr("failed remove copied-up tmp file %pd(%d)\n",
5990 +                       wh_dentry, err);
5991 +               err = -EIO;
5992 +       }
5993 +       au_dtime_revert(&dt);
5994 +       au_set_hi_wh(d_inode(dentry), bdst, wh_dentry);
5995 +
5996 +out_wh:
5997 +       dput(wh_dentry);
5998 +out:
5999 +       dput(parent);
6000 +       return err;
6001 +}
6002 +
6003 +struct au_cpup_wh_args {
6004 +       int *errp;
6005 +       struct au_cp_generic *cpg;
6006 +       struct file *file;
6007 +};
6008 +
6009 +static void au_call_cpup_wh(void *args)
6010 +{
6011 +       struct au_cpup_wh_args *a = args;
6012 +
6013 +       au_pin_hdir_acquire_nest(a->cpg->pin);
6014 +       *a->errp = au_cpup_wh(a->cpg, a->file);
6015 +       au_pin_hdir_release(a->cpg->pin);
6016 +}
6017 +
6018 +int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file)
6019 +{
6020 +       int err, wkq_err;
6021 +       aufs_bindex_t bdst;
6022 +       struct dentry *dentry, *parent, *h_orph, *h_parent;
6023 +       struct inode *dir, *h_dir, *h_tmpdir;
6024 +       struct au_wbr *wbr;
6025 +       struct au_pin wh_pin, *pin_orig;
6026 +       struct user_namespace *h_userns;
6027 +
6028 +       dentry = cpg->dentry;
6029 +       bdst = cpg->bdst;
6030 +       parent = dget_parent(dentry);
6031 +       dir = d_inode(parent);
6032 +       h_orph = NULL;
6033 +       h_parent = NULL;
6034 +       h_dir = au_igrab(au_h_iptr(dir, bdst));
6035 +       h_tmpdir = h_dir;
6036 +       pin_orig = NULL;
6037 +       if (!h_dir->i_nlink) {
6038 +               wbr = au_sbr(dentry->d_sb, bdst)->br_wbr;
6039 +               h_orph = wbr->wbr_orph;
6040 +
6041 +               h_parent = dget(au_h_dptr(parent, bdst));
6042 +               au_set_h_dptr(parent, bdst, dget(h_orph));
6043 +               h_tmpdir = d_inode(h_orph);
6044 +               au_set_h_iptr(dir, bdst, au_igrab(h_tmpdir), /*flags*/0);
6045 +
6046 +               inode_lock_nested(h_tmpdir, AuLsc_I_PARENT3);
6047 +               /* todo: au_h_open_pre()? */
6048 +
6049 +               pin_orig = cpg->pin;
6050 +               au_pin_init(&wh_pin, dentry, bdst, AuLsc_DI_PARENT,
6051 +                           AuLsc_I_PARENT3, cpg->pin->udba, AuPin_DI_LOCKED);
6052 +               cpg->pin = &wh_pin;
6053 +       }
6054 +
6055 +       h_userns = au_sbr_userns(dentry->d_sb, bdst);
6056 +       if (!au_test_h_perm_sio(h_userns, h_tmpdir, MAY_EXEC | MAY_WRITE)
6057 +           && !au_cpup_sio_test(cpg->pin, d_inode(dentry)->i_mode))
6058 +               err = au_cpup_wh(cpg, file);
6059 +       else {
6060 +               struct au_cpup_wh_args args = {
6061 +                       .errp   = &err,
6062 +                       .cpg    = cpg,
6063 +                       .file   = file
6064 +               };
6065 +               wkq_err = au_wkq_wait(au_call_cpup_wh, &args);
6066 +               if (unlikely(wkq_err))
6067 +                       err = wkq_err;
6068 +       }
6069 +
6070 +       if (h_orph) {
6071 +               inode_unlock(h_tmpdir);
6072 +               /* todo: au_h_open_post()? */
6073 +               au_set_h_iptr(dir, bdst, au_igrab(h_dir), /*flags*/0);
6074 +               au_set_h_dptr(parent, bdst, h_parent);
6075 +               AuDebugOn(!pin_orig);
6076 +               cpg->pin = pin_orig;
6077 +       }
6078 +       iput(h_dir);
6079 +       dput(parent);
6080 +
6081 +       return err;
6082 +}
6083 +
6084 +/* ---------------------------------------------------------------------- */
6085 +
6086 +/*
6087 + * generic routine for both of copy-up and copy-down.
6088 + */
6089 +/* cf. revalidate function in file.c */
6090 +int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
6091 +              int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
6092 +                        struct au_pin *pin,
6093 +                        struct dentry *h_parent, void *arg),
6094 +              void *arg)
6095 +{
6096 +       int err;
6097 +       struct au_pin pin;
6098 +       struct dentry *d, *parent, *h_parent, *real_parent, *h_dentry;
6099 +
6100 +       err = 0;
6101 +       parent = dget_parent(dentry);
6102 +       if (IS_ROOT(parent))
6103 +               goto out;
6104 +
6105 +       au_pin_init(&pin, dentry, bdst, AuLsc_DI_PARENT2, AuLsc_I_PARENT2,
6106 +                   au_opt_udba(dentry->d_sb), AuPin_MNT_WRITE);
6107 +
6108 +       /* do not use au_dpage */
6109 +       real_parent = parent;
6110 +       while (1) {
6111 +               dput(parent);
6112 +               parent = dget_parent(dentry);
6113 +               h_parent = au_h_dptr(parent, bdst);
6114 +               if (h_parent)
6115 +                       goto out; /* success */
6116 +
6117 +               /* find top dir which is necessary to cpup */
6118 +               do {
6119 +                       d = parent;
6120 +                       dput(parent);
6121 +                       parent = dget_parent(d);
6122 +                       di_read_lock_parent3(parent, !AuLock_IR);
6123 +                       h_parent = au_h_dptr(parent, bdst);
6124 +                       di_read_unlock(parent, !AuLock_IR);
6125 +               } while (!h_parent);
6126 +
6127 +               if (d != real_parent)
6128 +                       di_write_lock_child3(d);
6129 +
6130 +               /* somebody else might create while we were sleeping */
6131 +               h_dentry = au_h_dptr(d, bdst);
6132 +               if (!h_dentry || d_is_negative(h_dentry)) {
6133 +                       if (h_dentry)
6134 +                               au_update_dbtop(d);
6135 +
6136 +                       au_pin_set_dentry(&pin, d);
6137 +                       err = au_do_pin(&pin);
6138 +                       if (!err) {
6139 +                               err = cp(d, bdst, &pin, h_parent, arg);
6140 +                               au_unpin(&pin);
6141 +                       }
6142 +               }
6143 +
6144 +               if (d != real_parent)
6145 +                       di_write_unlock(d);
6146 +               if (unlikely(err))
6147 +                       break;
6148 +       }
6149 +
6150 +out:
6151 +       dput(parent);
6152 +       return err;
6153 +}
6154 +
6155 +static int au_cpup_dir(struct dentry *dentry, aufs_bindex_t bdst,
6156 +                      struct au_pin *pin,
6157 +                      struct dentry *h_parent __maybe_unused,
6158 +                      void *arg __maybe_unused)
6159 +{
6160 +       struct au_cp_generic cpg = {
6161 +               .dentry = dentry,
6162 +               .bdst   = bdst,
6163 +               .bsrc   = -1,
6164 +               .len    = 0,
6165 +               .pin    = pin,
6166 +               .flags  = AuCpup_DTIME
6167 +       };
6168 +       return au_sio_cpup_simple(&cpg);
6169 +}
6170 +
6171 +int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
6172 +{
6173 +       return au_cp_dirs(dentry, bdst, au_cpup_dir, NULL);
6174 +}
6175 +
6176 +int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
6177 +{
6178 +       int err;
6179 +       struct dentry *parent;
6180 +       struct inode *dir;
6181 +
6182 +       parent = dget_parent(dentry);
6183 +       dir = d_inode(parent);
6184 +       err = 0;
6185 +       if (au_h_iptr(dir, bdst))
6186 +               goto out;
6187 +
6188 +       di_read_unlock(parent, AuLock_IR);
6189 +       di_write_lock_parent(parent);
6190 +       /* someone else might change our inode while we were sleeping */
6191 +       if (!au_h_iptr(dir, bdst))
6192 +               err = au_cpup_dirs(dentry, bdst);
6193 +       di_downgrade_lock(parent, AuLock_IR);
6194 +
6195 +out:
6196 +       dput(parent);
6197 +       return err;
6198 +}
6199 diff -urN /usr/share/empty/fs/aufs/cpup.h linux/fs/aufs/cpup.h
6200 --- /usr/share/empty/fs/aufs/cpup.h     1970-01-01 01:00:00.000000000 +0100
6201 +++ linux/fs/aufs/cpup.h        2022-03-21 14:49:05.719966344 +0100
6202 @@ -0,0 +1,100 @@
6203 +/* SPDX-License-Identifier: GPL-2.0 */
6204 +/*
6205 + * Copyright (C) 2005-2021 Junjiro R. Okajima
6206 + *
6207 + * This program, aufs is free software; you can redistribute it and/or modify
6208 + * it under the terms of the GNU General Public License as published by
6209 + * the Free Software Foundation; either version 2 of the License, or
6210 + * (at your option) any later version.
6211 + *
6212 + * This program is distributed in the hope that it will be useful,
6213 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6214 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6215 + * GNU General Public License for more details.
6216 + *
6217 + * You should have received a copy of the GNU General Public License
6218 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6219 + */
6220 +
6221 +/*
6222 + * copy-up/down functions
6223 + */
6224 +
6225 +#ifndef __AUFS_CPUP_H__
6226 +#define __AUFS_CPUP_H__
6227 +
6228 +#ifdef __KERNEL__
6229 +
6230 +#include <linux/path.h>
6231 +
6232 +struct inode;
6233 +struct file;
6234 +struct au_pin;
6235 +
6236 +void au_cpup_attr_flags(struct inode *dst, unsigned int iflags);
6237 +void au_cpup_attr_timesizes(struct inode *inode);
6238 +void au_cpup_attr_nlink(struct inode *inode, int force);
6239 +void au_cpup_attr_changeable(struct inode *inode);
6240 +void au_cpup_igen(struct inode *inode, struct inode *h_inode);
6241 +void au_cpup_attr_all(struct inode *inode, int force);
6242 +
6243 +/* ---------------------------------------------------------------------- */
6244 +
6245 +struct au_cp_generic {
6246 +       struct dentry   *dentry;
6247 +       aufs_bindex_t   bdst, bsrc;
6248 +       loff_t          len;
6249 +       struct au_pin   *pin;
6250 +       unsigned int    flags;
6251 +};
6252 +
6253 +/* cpup flags */
6254 +#define AuCpup_DTIME           1               /* do dtime_store/revert */
6255 +#define AuCpup_KEEPLINO                (1 << 1)        /* do not clear the lower xino,
6256 +                                                  for link(2) */
6257 +#define AuCpup_RENAME          (1 << 2)        /* rename after cpup */
6258 +#define AuCpup_HOPEN           (1 << 3)        /* call h_open_pre/post() in
6259 +                                                  cpup */
6260 +#define AuCpup_OVERWRITE       (1 << 4)        /* allow overwriting the
6261 +                                                  existing entry */
6262 +#define AuCpup_RWDST           (1 << 5)        /* force write target even if
6263 +                                                  the branch is marked as RO */
6264 +
6265 +#ifndef CONFIG_AUFS_BR_HFSPLUS
6266 +#undef AuCpup_HOPEN
6267 +#define AuCpup_HOPEN           0
6268 +#endif
6269 +
6270 +#define au_ftest_cpup(flags, name)     ((flags) & AuCpup_##name)
6271 +#define au_fset_cpup(flags, name) \
6272 +       do { (flags) |= AuCpup_##name; } while (0)
6273 +#define au_fclr_cpup(flags, name) \
6274 +       do { (flags) &= ~AuCpup_##name; } while (0)
6275 +
6276 +int au_copy_file(struct file *dst, struct file *src, loff_t len);
6277 +int au_sio_cpup_simple(struct au_cp_generic *cpg);
6278 +int au_sio_cpdown_simple(struct au_cp_generic *cpg);
6279 +int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file);
6280 +
6281 +int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
6282 +              int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
6283 +                        struct au_pin *pin,
6284 +                        struct dentry *h_parent, void *arg),
6285 +              void *arg);
6286 +int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
6287 +int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
6288 +
6289 +/* ---------------------------------------------------------------------- */
6290 +
6291 +/* keep timestamps when copyup */
6292 +struct au_dtime {
6293 +       struct dentry *dt_dentry;
6294 +       struct path dt_h_path;
6295 +       struct timespec64 dt_atime, dt_mtime;
6296 +};
6297 +void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
6298 +                   struct path *h_path);
6299 +void au_dtime_revert(struct au_dtime *dt);
6300 +
6301 +#endif /* __KERNEL__ */
6302 +#endif /* __AUFS_CPUP_H__ */
6303 diff -urN /usr/share/empty/fs/aufs/dbgaufs.c linux/fs/aufs/dbgaufs.c
6304 --- /usr/share/empty/fs/aufs/dbgaufs.c  1970-01-01 01:00:00.000000000 +0100
6305 +++ linux/fs/aufs/dbgaufs.c     2022-03-21 14:49:05.719966344 +0100
6306 @@ -0,0 +1,526 @@
6307 +// SPDX-License-Identifier: GPL-2.0
6308 +/*
6309 + * Copyright (C) 2005-2021 Junjiro R. Okajima
6310 + *
6311 + * This program, aufs is free software; you can redistribute it and/or modify
6312 + * it under the terms of the GNU General Public License as published by
6313 + * the Free Software Foundation; either version 2 of the License, or
6314 + * (at your option) any later version.
6315 + *
6316 + * This program is distributed in the hope that it will be useful,
6317 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6318 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6319 + * GNU General Public License for more details.
6320 + *
6321 + * You should have received a copy of the GNU General Public License
6322 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6323 + */
6324 +
6325 +/*
6326 + * debugfs interface
6327 + */
6328 +
6329 +#include <linux/debugfs.h>
6330 +#include "aufs.h"
6331 +
6332 +#ifndef CONFIG_SYSFS
6333 +#error DEBUG_FS depends upon SYSFS
6334 +#endif
6335 +
6336 +static struct dentry *dbgaufs;
6337 +static const mode_t dbgaufs_mode = 0444;
6338 +
6339 +/* 20 is max digits length of ulong 64 */
6340 +struct dbgaufs_arg {
6341 +       int n;
6342 +       char a[20 * 4];
6343 +};
6344 +
6345 +/*
6346 + * common function for all XINO files
6347 + */
6348 +static int dbgaufs_xi_release(struct inode *inode __maybe_unused,
6349 +                             struct file *file)
6350 +{
6351 +       void *p;
6352 +
6353 +       p = file->private_data;
6354 +       if (p) {
6355 +               /* this is struct dbgaufs_arg */
6356 +               AuDebugOn(!au_kfree_sz_test(p));
6357 +               au_kfree_do_rcu(p);
6358 +       }
6359 +       return 0;
6360 +}
6361 +
6362 +static int dbgaufs_xi_open(struct file *xf, struct file *file, int do_fcnt,
6363 +                          int cnt)
6364 +{
6365 +       int err;
6366 +       struct kstat st;
6367 +       struct dbgaufs_arg *p;
6368 +
6369 +       err = -ENOMEM;
6370 +       p = kmalloc(sizeof(*p), GFP_NOFS);
6371 +       if (unlikely(!p))
6372 +               goto out;
6373 +
6374 +       err = 0;
6375 +       p->n = 0;
6376 +       file->private_data = p;
6377 +       if (!xf)
6378 +               goto out;
6379 +
6380 +       err = vfsub_getattr(&xf->f_path, &st);
6381 +       if (!err) {
6382 +               if (do_fcnt)
6383 +                       p->n = snprintf
6384 +                               (p->a, sizeof(p->a), "%d, %llux%u %lld\n",
6385 +                                cnt, st.blocks, st.blksize,
6386 +                                (long long)st.size);
6387 +               else
6388 +                       p->n = snprintf(p->a, sizeof(p->a), "%llux%u %lld\n",
6389 +                                       st.blocks, st.blksize,
6390 +                                       (long long)st.size);
6391 +               AuDebugOn(p->n >= sizeof(p->a));
6392 +       } else {
6393 +               p->n = snprintf(p->a, sizeof(p->a), "err %d\n", err);
6394 +               err = 0;
6395 +       }
6396 +
6397 +out:
6398 +       return err;
6399 +}
6400 +
6401 +static ssize_t dbgaufs_xi_read(struct file *file, char __user *buf,
6402 +                              size_t count, loff_t *ppos)
6403 +{
6404 +       struct dbgaufs_arg *p;
6405 +
6406 +       p = file->private_data;
6407 +       return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
6408 +}
6409 +
6410 +/* ---------------------------------------------------------------------- */
6411 +
6412 +struct dbgaufs_plink_arg {
6413 +       int n;
6414 +       char a[];
6415 +};
6416 +
6417 +static int dbgaufs_plink_release(struct inode *inode __maybe_unused,
6418 +                                struct file *file)
6419 +{
6420 +       free_page((unsigned long)file->private_data);
6421 +       return 0;
6422 +}
6423 +
6424 +static int dbgaufs_plink_open(struct inode *inode, struct file *file)
6425 +{
6426 +       int err, i, limit;
6427 +       unsigned long n, sum;
6428 +       struct dbgaufs_plink_arg *p;
6429 +       struct au_sbinfo *sbinfo;
6430 +       struct super_block *sb;
6431 +       struct hlist_bl_head *hbl;
6432 +
6433 +       err = -ENOMEM;
6434 +       p = (void *)get_zeroed_page(GFP_NOFS);
6435 +       if (unlikely(!p))
6436 +               goto out;
6437 +
6438 +       err = -EFBIG;
6439 +       sbinfo = inode->i_private;
6440 +       sb = sbinfo->si_sb;
6441 +       si_noflush_read_lock(sb);
6442 +       if (au_opt_test(au_mntflags(sb), PLINK)) {
6443 +               limit = PAGE_SIZE - sizeof(p->n);
6444 +
6445 +               /* the number of buckets */
6446 +               n = snprintf(p->a + p->n, limit, "%d\n", AuPlink_NHASH);
6447 +               p->n += n;
6448 +               limit -= n;
6449 +
6450 +               sum = 0;
6451 +               for (i = 0, hbl = sbinfo->si_plink; i < AuPlink_NHASH;
6452 +                    i++, hbl++) {
6453 +                       n = au_hbl_count(hbl);
6454 +                       sum += n;
6455 +
6456 +                       n = snprintf(p->a + p->n, limit, "%lu ", n);
6457 +                       p->n += n;
6458 +                       limit -= n;
6459 +                       if (unlikely(limit <= 0))
6460 +                               goto out_free;
6461 +               }
6462 +               p->a[p->n - 1] = '\n';
6463 +
6464 +               /* the sum of plinks */
6465 +               n = snprintf(p->a + p->n, limit, "%lu\n", sum);
6466 +               p->n += n;
6467 +               limit -= n;
6468 +               if (unlikely(limit <= 0))
6469 +                       goto out_free;
6470 +       } else {
6471 +#define str "1\n0\n0\n"
6472 +               p->n = sizeof(str) - 1;
6473 +               strcpy(p->a, str);
6474 +#undef str
6475 +       }
6476 +       si_read_unlock(sb);
6477 +
6478 +       err = 0;
6479 +       file->private_data = p;
6480 +       goto out; /* success */
6481 +
6482 +out_free:
6483 +       free_page((unsigned long)p);
6484 +out:
6485 +       return err;
6486 +}
6487 +
6488 +static ssize_t dbgaufs_plink_read(struct file *file, char __user *buf,
6489 +                                 size_t count, loff_t *ppos)
6490 +{
6491 +       struct dbgaufs_plink_arg *p;
6492 +
6493 +       p = file->private_data;
6494 +       return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
6495 +}
6496 +
6497 +static const struct file_operations dbgaufs_plink_fop = {
6498 +       .owner          = THIS_MODULE,
6499 +       .open           = dbgaufs_plink_open,
6500 +       .release        = dbgaufs_plink_release,
6501 +       .read           = dbgaufs_plink_read
6502 +};
6503 +
6504 +/* ---------------------------------------------------------------------- */
6505 +
6506 +static int dbgaufs_xib_open(struct inode *inode, struct file *file)
6507 +{
6508 +       int err;
6509 +       struct au_sbinfo *sbinfo;
6510 +       struct super_block *sb;
6511 +
6512 +       sbinfo = inode->i_private;
6513 +       sb = sbinfo->si_sb;
6514 +       si_noflush_read_lock(sb);
6515 +       err = dbgaufs_xi_open(sbinfo->si_xib, file, /*do_fcnt*/0, /*cnt*/0);
6516 +       si_read_unlock(sb);
6517 +       return err;
6518 +}
6519 +
6520 +static const struct file_operations dbgaufs_xib_fop = {
6521 +       .owner          = THIS_MODULE,
6522 +       .open           = dbgaufs_xib_open,
6523 +       .release        = dbgaufs_xi_release,
6524 +       .read           = dbgaufs_xi_read
6525 +};
6526 +
6527 +/* ---------------------------------------------------------------------- */
6528 +
6529 +#define DbgaufsXi_PREFIX "xi"
6530 +
6531 +static int dbgaufs_xino_open(struct inode *inode, struct file *file)
6532 +{
6533 +       int err, idx;
6534 +       long l;
6535 +       aufs_bindex_t bindex;
6536 +       char *p, a[sizeof(DbgaufsXi_PREFIX) + 8];
6537 +       struct au_sbinfo *sbinfo;
6538 +       struct super_block *sb;
6539 +       struct au_xino *xi;
6540 +       struct file *xf;
6541 +       struct qstr *name;
6542 +       struct au_branch *br;
6543 +
6544 +       err = -ENOENT;
6545 +       name = &file->f_path.dentry->d_name;
6546 +       if (unlikely(name->len < sizeof(DbgaufsXi_PREFIX)
6547 +                    || memcmp(name->name, DbgaufsXi_PREFIX,
6548 +                              sizeof(DbgaufsXi_PREFIX) - 1)))
6549 +               goto out;
6550 +
6551 +       AuDebugOn(name->len >= sizeof(a));
6552 +       memcpy(a, name->name, name->len);
6553 +       a[name->len] = '\0';
6554 +       p = strchr(a, '-');
6555 +       if (p)
6556 +               *p = '\0';
6557 +       err = kstrtol(a + sizeof(DbgaufsXi_PREFIX) - 1, 10, &l);
6558 +       if (unlikely(err))
6559 +               goto out;
6560 +       bindex = l;
6561 +       idx = 0;
6562 +       if (p) {
6563 +               err = kstrtol(p + 1, 10, &l);
6564 +               if (unlikely(err))
6565 +                       goto out;
6566 +               idx = l;
6567 +       }
6568 +
6569 +       err = -ENOENT;
6570 +       sbinfo = inode->i_private;
6571 +       sb = sbinfo->si_sb;
6572 +       si_noflush_read_lock(sb);
6573 +       if (unlikely(bindex < 0 || bindex > au_sbbot(sb)))
6574 +               goto out_si;
6575 +       br = au_sbr(sb, bindex);
6576 +       xi = br->br_xino;
6577 +       if (unlikely(idx >= xi->xi_nfile))
6578 +               goto out_si;
6579 +       xf = au_xino_file(xi, idx);
6580 +       if (xf)
6581 +               err = dbgaufs_xi_open(xf, file, /*do_fcnt*/1,
6582 +                                     au_xino_count(br));
6583 +
6584 +out_si:
6585 +       si_read_unlock(sb);
6586 +out:
6587 +       AuTraceErr(err);
6588 +       return err;
6589 +}
6590 +
6591 +static const struct file_operations dbgaufs_xino_fop = {
6592 +       .owner          = THIS_MODULE,
6593 +       .open           = dbgaufs_xino_open,
6594 +       .release        = dbgaufs_xi_release,
6595 +       .read           = dbgaufs_xi_read
6596 +};
6597 +
6598 +void dbgaufs_xino_del(struct au_branch *br)
6599 +{
6600 +       struct dentry *dbgaufs;
6601 +
6602 +       dbgaufs = br->br_dbgaufs;
6603 +       if (!dbgaufs)
6604 +               return;
6605 +
6606 +       br->br_dbgaufs = NULL;
6607 +       /* debugfs acquires the parent i_mutex */
6608 +       lockdep_off();
6609 +       debugfs_remove(dbgaufs);
6610 +       lockdep_on();
6611 +}
6612 +
6613 +void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
6614 +{
6615 +       aufs_bindex_t bbot;
6616 +       struct au_branch *br;
6617 +
6618 +       if (!au_sbi(sb)->si_dbgaufs)
6619 +               return;
6620 +
6621 +       bbot = au_sbbot(sb);
6622 +       for (; bindex <= bbot; bindex++) {
6623 +               br = au_sbr(sb, bindex);
6624 +               dbgaufs_xino_del(br);
6625 +       }
6626 +}
6627 +
6628 +static void dbgaufs_br_do_add(struct super_block *sb, aufs_bindex_t bindex,
6629 +                             unsigned int idx, struct dentry *parent,
6630 +                             struct au_sbinfo *sbinfo)
6631 +{
6632 +       struct au_branch *br;
6633 +       struct dentry *d;
6634 +       /* "xi" bindex(5) "-" idx(2) NULL */
6635 +       char name[sizeof(DbgaufsXi_PREFIX) + 8];
6636 +
6637 +       if (!idx)
6638 +               snprintf(name, sizeof(name), DbgaufsXi_PREFIX "%d", bindex);
6639 +       else
6640 +               snprintf(name, sizeof(name), DbgaufsXi_PREFIX "%d-%u",
6641 +                        bindex, idx);
6642 +       br = au_sbr(sb, bindex);
6643 +       if (br->br_dbgaufs) {
6644 +               struct qstr qstr = QSTR_INIT(name, strlen(name));
6645 +
6646 +               if (!au_qstreq(&br->br_dbgaufs->d_name, &qstr)) {
6647 +                       /* debugfs acquires the parent i_mutex */
6648 +                       lockdep_off();
6649 +                       d = debugfs_rename(parent, br->br_dbgaufs, parent,
6650 +                                          name);
6651 +                       lockdep_on();
6652 +                       if (unlikely(!d))
6653 +                               pr_warn("failed renaming %pd/%s, ignored.\n",
6654 +                                       parent, name);
6655 +               }
6656 +       } else {
6657 +               lockdep_off();
6658 +               br->br_dbgaufs = debugfs_create_file(name, dbgaufs_mode, parent,
6659 +                                                    sbinfo, &dbgaufs_xino_fop);
6660 +               lockdep_on();
6661 +               if (unlikely(!br->br_dbgaufs))
6662 +                       pr_warn("failed creating %pd/%s, ignored.\n",
6663 +                               parent, name);
6664 +       }
6665 +}
6666 +
6667 +static void dbgaufs_br_add(struct super_block *sb, aufs_bindex_t bindex,
6668 +                          struct dentry *parent, struct au_sbinfo *sbinfo)
6669 +{
6670 +       struct au_branch *br;
6671 +       struct au_xino *xi;
6672 +       unsigned int u;
6673 +
6674 +       br = au_sbr(sb, bindex);
6675 +       xi = br->br_xino;
6676 +       for (u = 0; u < xi->xi_nfile; u++)
6677 +               dbgaufs_br_do_add(sb, bindex, u, parent, sbinfo);
6678 +}
6679 +
6680 +void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex, int topdown)
6681 +{
6682 +       struct au_sbinfo *sbinfo;
6683 +       struct dentry *parent;
6684 +       aufs_bindex_t bbot;
6685 +
6686 +       if (!au_opt_test(au_mntflags(sb), XINO))
6687 +               return;
6688 +
6689 +       sbinfo = au_sbi(sb);
6690 +       parent = sbinfo->si_dbgaufs;
6691 +       if (!parent)
6692 +               return;
6693 +
6694 +       bbot = au_sbbot(sb);
6695 +       if (topdown)
6696 +               for (; bindex <= bbot; bindex++)
6697 +                       dbgaufs_br_add(sb, bindex, parent, sbinfo);
6698 +       else
6699 +               for (; bbot >= bindex; bbot--)
6700 +                       dbgaufs_br_add(sb, bbot, parent, sbinfo);
6701 +}
6702 +
6703 +/* ---------------------------------------------------------------------- */
6704 +
6705 +#ifdef CONFIG_AUFS_EXPORT
6706 +static int dbgaufs_xigen_open(struct inode *inode, struct file *file)
6707 +{
6708 +       int err;
6709 +       struct au_sbinfo *sbinfo;
6710 +       struct super_block *sb;
6711 +
6712 +       sbinfo = inode->i_private;
6713 +       sb = sbinfo->si_sb;
6714 +       si_noflush_read_lock(sb);
6715 +       err = dbgaufs_xi_open(sbinfo->si_xigen, file, /*do_fcnt*/0, /*cnt*/0);
6716 +       si_read_unlock(sb);
6717 +       return err;
6718 +}
6719 +
6720 +static const struct file_operations dbgaufs_xigen_fop = {
6721 +       .owner          = THIS_MODULE,
6722 +       .open           = dbgaufs_xigen_open,
6723 +       .release        = dbgaufs_xi_release,
6724 +       .read           = dbgaufs_xi_read
6725 +};
6726 +
6727 +static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
6728 +{
6729 +       int err;
6730 +
6731 +       /*
6732 +        * This function is a dynamic '__init' function actually,
6733 +        * so the tiny check for si_rwsem is unnecessary.
6734 +        */
6735 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6736 +
6737 +       err = -EIO;
6738 +       sbinfo->si_dbgaufs_xigen = debugfs_create_file
6739 +               ("xigen", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6740 +                &dbgaufs_xigen_fop);
6741 +       if (sbinfo->si_dbgaufs_xigen)
6742 +               err = 0;
6743 +
6744 +       return err;
6745 +}
6746 +#else
6747 +static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
6748 +{
6749 +       return 0;
6750 +}
6751 +#endif /* CONFIG_AUFS_EXPORT */
6752 +
6753 +/* ---------------------------------------------------------------------- */
6754 +
6755 +void dbgaufs_si_fin(struct au_sbinfo *sbinfo)
6756 +{
6757 +       /*
6758 +        * This function is a dynamic '__fin' function actually,
6759 +        * so the tiny check for si_rwsem is unnecessary.
6760 +        */
6761 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6762 +
6763 +       debugfs_remove_recursive(sbinfo->si_dbgaufs);
6764 +       sbinfo->si_dbgaufs = NULL;
6765 +}
6766 +
6767 +int dbgaufs_si_init(struct au_sbinfo *sbinfo)
6768 +{
6769 +       int err;
6770 +       char name[SysaufsSiNameLen];
6771 +
6772 +       /*
6773 +        * This function is a dynamic '__init' function actually,
6774 +        * so the tiny check for si_rwsem is unnecessary.
6775 +        */
6776 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6777 +
6778 +       err = -ENOENT;
6779 +       if (!dbgaufs) {
6780 +               AuErr1("/debug/aufs is uninitialized\n");
6781 +               goto out;
6782 +       }
6783 +
6784 +       err = -EIO;
6785 +       sysaufs_name(sbinfo, name);
6786 +       sbinfo->si_dbgaufs = debugfs_create_dir(name, dbgaufs);
6787 +       if (unlikely(!sbinfo->si_dbgaufs))
6788 +               goto out;
6789 +
6790 +       /* regardless plink/noplink option */
6791 +       sbinfo->si_dbgaufs_plink = debugfs_create_file
6792 +               ("plink", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6793 +                &dbgaufs_plink_fop);
6794 +       if (unlikely(!sbinfo->si_dbgaufs_plink))
6795 +               goto out_dir;
6796 +
6797 +       /* regardless xino/noxino option */
6798 +       sbinfo->si_dbgaufs_xib = debugfs_create_file
6799 +               ("xib", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6800 +                &dbgaufs_xib_fop);
6801 +       if (unlikely(!sbinfo->si_dbgaufs_xib))
6802 +               goto out_dir;
6803 +
6804 +       err = dbgaufs_xigen_init(sbinfo);
6805 +       if (!err)
6806 +               goto out; /* success */
6807 +
6808 +out_dir:
6809 +       dbgaufs_si_fin(sbinfo);
6810 +out:
6811 +       if (unlikely(err))
6812 +               pr_err("debugfs/aufs failed\n");
6813 +       return err;
6814 +}
6815 +
6816 +/* ---------------------------------------------------------------------- */
6817 +
6818 +void dbgaufs_fin(void)
6819 +{
6820 +       debugfs_remove(dbgaufs);
6821 +}
6822 +
6823 +int __init dbgaufs_init(void)
6824 +{
6825 +       int err;
6826 +
6827 +       err = -EIO;
6828 +       dbgaufs = debugfs_create_dir(AUFS_NAME, NULL);
6829 +       if (dbgaufs)
6830 +               err = 0;
6831 +       return err;
6832 +}
6833 diff -urN /usr/share/empty/fs/aufs/dbgaufs.h linux/fs/aufs/dbgaufs.h
6834 --- /usr/share/empty/fs/aufs/dbgaufs.h  1970-01-01 01:00:00.000000000 +0100
6835 +++ linux/fs/aufs/dbgaufs.h     2022-03-21 14:49:05.719966344 +0100
6836 @@ -0,0 +1,53 @@
6837 +/* SPDX-License-Identifier: GPL-2.0 */
6838 +/*
6839 + * Copyright (C) 2005-2021 Junjiro R. Okajima
6840 + *
6841 + * This program, aufs is free software; you can redistribute it and/or modify
6842 + * it under the terms of the GNU General Public License as published by
6843 + * the Free Software Foundation; either version 2 of the License, or
6844 + * (at your option) any later version.
6845 + *
6846 + * This program is distributed in the hope that it will be useful,
6847 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6848 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6849 + * GNU General Public License for more details.
6850 + *
6851 + * You should have received a copy of the GNU General Public License
6852 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6853 + */
6854 +
6855 +/*
6856 + * debugfs interface
6857 + */
6858 +
6859 +#ifndef __DBGAUFS_H__
6860 +#define __DBGAUFS_H__
6861 +
6862 +#ifdef __KERNEL__
6863 +
6864 +struct super_block;
6865 +struct au_sbinfo;
6866 +struct au_branch;
6867 +
6868 +#ifdef CONFIG_DEBUG_FS
6869 +/* dbgaufs.c */
6870 +void dbgaufs_xino_del(struct au_branch *br);
6871 +void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
6872 +void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex, int topdown);
6873 +void dbgaufs_si_fin(struct au_sbinfo *sbinfo);
6874 +int dbgaufs_si_init(struct au_sbinfo *sbinfo);
6875 +void dbgaufs_fin(void);
6876 +int __init dbgaufs_init(void);
6877 +#else
6878 +AuStubVoid(dbgaufs_xino_del, struct au_branch *br)
6879 +AuStubVoid(dbgaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
6880 +AuStubVoid(dbgaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex,
6881 +          int topdown)
6882 +AuStubVoid(dbgaufs_si_fin, struct au_sbinfo *sbinfo)
6883 +AuStubInt0(dbgaufs_si_init, struct au_sbinfo *sbinfo)
6884 +AuStubVoid(dbgaufs_fin, void)
6885 +AuStubInt0(__init dbgaufs_init, void)
6886 +#endif /* CONFIG_DEBUG_FS */
6887 +
6888 +#endif /* __KERNEL__ */
6889 +#endif /* __DBGAUFS_H__ */
6890 diff -urN /usr/share/empty/fs/aufs/dcsub.c linux/fs/aufs/dcsub.c
6891 --- /usr/share/empty/fs/aufs/dcsub.c    1970-01-01 01:00:00.000000000 +0100
6892 +++ linux/fs/aufs/dcsub.c       2022-03-21 14:49:05.719966344 +0100
6893 @@ -0,0 +1,225 @@
6894 +// SPDX-License-Identifier: GPL-2.0
6895 +/*
6896 + * Copyright (C) 2005-2021 Junjiro R. Okajima
6897 + *
6898 + * This program, aufs is free software; you can redistribute it and/or modify
6899 + * it under the terms of the GNU General Public License as published by
6900 + * the Free Software Foundation; either version 2 of the License, or
6901 + * (at your option) any later version.
6902 + *
6903 + * This program is distributed in the hope that it will be useful,
6904 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6905 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6906 + * GNU General Public License for more details.
6907 + *
6908 + * You should have received a copy of the GNU General Public License
6909 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6910 + */
6911 +
6912 +/*
6913 + * sub-routines for dentry cache
6914 + */
6915 +
6916 +#include "aufs.h"
6917 +
6918 +static void au_dpage_free(struct au_dpage *dpage)
6919 +{
6920 +       int i;
6921 +       struct dentry **p;
6922 +
6923 +       p = dpage->dentries;
6924 +       for (i = 0; i < dpage->ndentry; i++)
6925 +               dput(*p++);
6926 +       free_page((unsigned long)dpage->dentries);
6927 +}
6928 +
6929 +int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp)
6930 +{
6931 +       int err;
6932 +       void *p;
6933 +
6934 +       err = -ENOMEM;
6935 +       dpages->dpages = kmalloc(sizeof(*dpages->dpages), gfp);
6936 +       if (unlikely(!dpages->dpages))
6937 +               goto out;
6938 +
6939 +       p = (void *)__get_free_page(gfp);
6940 +       if (unlikely(!p))
6941 +               goto out_dpages;
6942 +
6943 +       dpages->dpages[0].ndentry = 0;
6944 +       dpages->dpages[0].dentries = p;
6945 +       dpages->ndpage = 1;
6946 +       return 0; /* success */
6947 +
6948 +out_dpages:
6949 +       au_kfree_try_rcu(dpages->dpages);
6950 +out:
6951 +       return err;
6952 +}
6953 +
6954 +void au_dpages_free(struct au_dcsub_pages *dpages)
6955 +{
6956 +       int i;
6957 +       struct au_dpage *p;
6958 +
6959 +       p = dpages->dpages;
6960 +       for (i = 0; i < dpages->ndpage; i++)
6961 +               au_dpage_free(p++);
6962 +       au_kfree_try_rcu(dpages->dpages);
6963 +}
6964 +
6965 +static int au_dpages_append(struct au_dcsub_pages *dpages,
6966 +                           struct dentry *dentry, gfp_t gfp)
6967 +{
6968 +       int err, sz;
6969 +       struct au_dpage *dpage;
6970 +       void *p;
6971 +
6972 +       dpage = dpages->dpages + dpages->ndpage - 1;
6973 +       sz = PAGE_SIZE / sizeof(dentry);
6974 +       if (unlikely(dpage->ndentry >= sz)) {
6975 +               AuLabel(new dpage);
6976 +               err = -ENOMEM;
6977 +               sz = dpages->ndpage * sizeof(*dpages->dpages);
6978 +               p = au_kzrealloc(dpages->dpages, sz,
6979 +                                sz + sizeof(*dpages->dpages), gfp,
6980 +                                /*may_shrink*/0);
6981 +               if (unlikely(!p))
6982 +                       goto out;
6983 +
6984 +               dpages->dpages = p;
6985 +               dpage = dpages->dpages + dpages->ndpage;
6986 +               p = (void *)__get_free_page(gfp);
6987 +               if (unlikely(!p))
6988 +                       goto out;
6989 +
6990 +               dpage->ndentry = 0;
6991 +               dpage->dentries = p;
6992 +               dpages->ndpage++;
6993 +       }
6994 +
6995 +       AuDebugOn(au_dcount(dentry) <= 0);
6996 +       dpage->dentries[dpage->ndentry++] = dget_dlock(dentry);
6997 +       return 0; /* success */
6998 +
6999 +out:
7000 +       return err;
7001 +}
7002 +
7003 +/* todo: BAD approach */
7004 +/* copied from linux/fs/dcache.c */
7005 +enum d_walk_ret {
7006 +       D_WALK_CONTINUE,
7007 +       D_WALK_QUIT,
7008 +       D_WALK_NORETRY,
7009 +       D_WALK_SKIP,
7010 +};
7011 +
7012 +extern void d_walk(struct dentry *parent, void *data,
7013 +                  enum d_walk_ret (*enter)(void *, struct dentry *));
7014 +
7015 +struct ac_dpages_arg {
7016 +       int err;
7017 +       struct au_dcsub_pages *dpages;
7018 +       struct super_block *sb;
7019 +       au_dpages_test test;
7020 +       void *arg;
7021 +};
7022 +
7023 +static enum d_walk_ret au_call_dpages_append(void *_arg, struct dentry *dentry)
7024 +{
7025 +       enum d_walk_ret ret;
7026 +       struct ac_dpages_arg *arg = _arg;
7027 +
7028 +       ret = D_WALK_CONTINUE;
7029 +       if (dentry->d_sb == arg->sb
7030 +           && !IS_ROOT(dentry)
7031 +           && au_dcount(dentry) > 0
7032 +           && au_di(dentry)
7033 +           && (!arg->test || arg->test(dentry, arg->arg))) {
7034 +               arg->err = au_dpages_append(arg->dpages, dentry, GFP_ATOMIC);
7035 +               if (unlikely(arg->err))
7036 +                       ret = D_WALK_QUIT;
7037 +       }
7038 +
7039 +       return ret;
7040 +}
7041 +
7042 +int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
7043 +                  au_dpages_test test, void *arg)
7044 +{
7045 +       struct ac_dpages_arg args = {
7046 +               .err    = 0,
7047 +               .dpages = dpages,
7048 +               .sb     = root->d_sb,
7049 +               .test   = test,
7050 +               .arg    = arg
7051 +       };
7052 +
7053 +       d_walk(root, &args, au_call_dpages_append);
7054 +
7055 +       return args.err;
7056 +}
7057 +
7058 +int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
7059 +                      int do_include, au_dpages_test test, void *arg)
7060 +{
7061 +       int err;
7062 +
7063 +       err = 0;
7064 +       write_seqlock(&rename_lock);
7065 +       spin_lock(&dentry->d_lock);
7066 +       if (do_include
7067 +           && au_dcount(dentry) > 0
7068 +           && (!test || test(dentry, arg)))
7069 +               err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
7070 +       spin_unlock(&dentry->d_lock);
7071 +       if (unlikely(err))
7072 +               goto out;
7073 +
7074 +       /*
7075 +        * RCU for vfsmount is unnecessary since this is a traverse in a single
7076 +        * mount
7077 +        */
7078 +       while (!IS_ROOT(dentry)) {
7079 +               dentry = dentry->d_parent; /* rename_lock is locked */
7080 +               spin_lock(&dentry->d_lock);
7081 +               if (au_dcount(dentry) > 0
7082 +                   && (!test || test(dentry, arg)))
7083 +                       err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
7084 +               spin_unlock(&dentry->d_lock);
7085 +               if (unlikely(err))
7086 +                       break;
7087 +       }
7088 +
7089 +out:
7090 +       write_sequnlock(&rename_lock);
7091 +       return err;
7092 +}
7093 +
7094 +static inline int au_dcsub_dpages_aufs(struct dentry *dentry, void *arg)
7095 +{
7096 +       return au_di(dentry) && dentry->d_sb == arg;
7097 +}
7098 +
7099 +int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
7100 +                           struct dentry *dentry, int do_include)
7101 +{
7102 +       return au_dcsub_pages_rev(dpages, dentry, do_include,
7103 +                                 au_dcsub_dpages_aufs, dentry->d_sb);
7104 +}
7105 +
7106 +int au_test_subdir(struct dentry *d1, struct dentry *d2)
7107 +{
7108 +       struct path path[2] = {
7109 +               {
7110 +                       .dentry = d1
7111 +               },
7112 +               {
7113 +                       .dentry = d2
7114 +               }
7115 +       };
7116 +
7117 +       return path_is_under(path + 0, path + 1);
7118 +}
7119 diff -urN /usr/share/empty/fs/aufs/dcsub.h linux/fs/aufs/dcsub.h
7120 --- /usr/share/empty/fs/aufs/dcsub.h    1970-01-01 01:00:00.000000000 +0100
7121 +++ linux/fs/aufs/dcsub.h       2022-03-21 14:49:05.719966344 +0100
7122 @@ -0,0 +1,137 @@
7123 +/* SPDX-License-Identifier: GPL-2.0 */
7124 +/*
7125 + * Copyright (C) 2005-2021 Junjiro R. Okajima
7126 + *
7127 + * This program, aufs is free software; you can redistribute it and/or modify
7128 + * it under the terms of the GNU General Public License as published by
7129 + * the Free Software Foundation; either version 2 of the License, or
7130 + * (at your option) any later version.
7131 + *
7132 + * This program is distributed in the hope that it will be useful,
7133 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7134 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7135 + * GNU General Public License for more details.
7136 + *
7137 + * You should have received a copy of the GNU General Public License
7138 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
7139 + */
7140 +
7141 +/*
7142 + * sub-routines for dentry cache
7143 + */
7144 +
7145 +#ifndef __AUFS_DCSUB_H__
7146 +#define __AUFS_DCSUB_H__
7147 +
7148 +#ifdef __KERNEL__
7149 +
7150 +#include <linux/dcache.h>
7151 +#include <linux/fs.h>
7152 +
7153 +struct au_dpage {
7154 +       int ndentry;
7155 +       struct dentry **dentries;
7156 +};
7157 +
7158 +struct au_dcsub_pages {
7159 +       int ndpage;
7160 +       struct au_dpage *dpages;
7161 +};
7162 +
7163 +/* ---------------------------------------------------------------------- */
7164 +
7165 +/* dcsub.c */
7166 +int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp);
7167 +void au_dpages_free(struct au_dcsub_pages *dpages);
7168 +typedef int (*au_dpages_test)(struct dentry *dentry, void *arg);
7169 +int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
7170 +                  au_dpages_test test, void *arg);
7171 +int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
7172 +                      int do_include, au_dpages_test test, void *arg);
7173 +int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
7174 +                           struct dentry *dentry, int do_include);
7175 +int au_test_subdir(struct dentry *d1, struct dentry *d2);
7176 +
7177 +/* ---------------------------------------------------------------------- */
7178 +
7179 +/*
7180 + * todo: in linux-3.13, several similar (but faster) helpers are added to
7181 + * include/linux/dcache.h. Try them (in the future).
7182 + */
7183 +
7184 +static inline int au_d_hashed_positive(struct dentry *d)
7185 +{
7186 +       int err;
7187 +       struct inode *inode = d_inode(d);
7188 +
7189 +       err = 0;
7190 +       if (unlikely(d_unhashed(d)
7191 +                    || d_is_negative(d)
7192 +                    || !inode->i_nlink))
7193 +               err = -ENOENT;
7194 +       return err;
7195 +}
7196 +
7197 +static inline int au_d_linkable(struct dentry *d)
7198 +{
7199 +       int err;
7200 +       struct inode *inode = d_inode(d);
7201 +
7202 +       err = au_d_hashed_positive(d);
7203 +       if (err
7204 +           && d_is_positive(d)
7205 +           && (inode->i_state & I_LINKABLE))
7206 +               err = 0;
7207 +       return err;
7208 +}
7209 +
7210 +static inline int au_d_alive(struct dentry *d)
7211 +{
7212 +       int err;
7213 +       struct inode *inode;
7214 +
7215 +       err = 0;
7216 +       if (!IS_ROOT(d))
7217 +               err = au_d_hashed_positive(d);
7218 +       else {
7219 +               inode = d_inode(d);
7220 +               if (unlikely(d_unlinked(d)
7221 +                            || d_is_negative(d)
7222 +                            || !inode->i_nlink))
7223 +                       err = -ENOENT;
7224 +       }
7225 +       return err;
7226 +}
7227 +
7228 +static inline int au_alive_dir(struct dentry *d)
7229 +{
7230 +       int err;
7231 +
7232 +       err = au_d_alive(d);
7233 +       if (unlikely(err || IS_DEADDIR(d_inode(d))))
7234 +               err = -ENOENT;
7235 +       return err;
7236 +}
7237 +
7238 +static inline int au_qstreq(struct qstr *a, struct qstr *b)
7239 +{
7240 +       return a->len == b->len
7241 +               && !memcmp(a->name, b->name, a->len);
7242 +}
7243 +
7244 +/*
7245 + * by the commit
7246 + * 360f547 2015-01-25 dcache: let the dentry count go down to zero without
7247 + *                     taking d_lock
7248 + * the type of d_lockref.count became int, but the inlined function d_count()
7249 + * still returns unsigned int.
7250 + * I don't know why. Maybe it is for every d_count() users?
7251 + * Anyway au_dcount() lives on.
7252 + */
7253 +static inline int au_dcount(struct dentry *d)
7254 +{
7255 +       return (int)d_count(d);
7256 +}
7257 +
7258 +#endif /* __KERNEL__ */
7259 +#endif /* __AUFS_DCSUB_H__ */
7260 diff -urN /usr/share/empty/fs/aufs/debug.c linux/fs/aufs/debug.c
7261 --- /usr/share/empty/fs/aufs/debug.c    1970-01-01 01:00:00.000000000 +0100
7262 +++ linux/fs/aufs/debug.c       2022-03-21 14:49:05.719966344 +0100
7263 @@ -0,0 +1,444 @@
7264 +// SPDX-License-Identifier: GPL-2.0
7265 +/*
7266 + * Copyright (C) 2005-2021 Junjiro R. Okajima
7267 + *
7268 + * This program, aufs is free software; you can redistribute it and/or modify
7269 + * it under the terms of the GNU General Public License as published by
7270 + * the Free Software Foundation; either version 2 of the License, or
7271 + * (at your option) any later version.
7272 + *
7273 + * This program is distributed in the hope that it will be useful,
7274 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7275 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7276 + * GNU General Public License for more details.
7277 + *
7278 + * You should have received a copy of the GNU General Public License
7279 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
7280 + */
7281 +
7282 +/*
7283 + * debug print functions
7284 + */
7285 +
7286 +#include <linux/iversion.h>
7287 +#include "aufs.h"
7288 +
7289 +/* Returns 0, or -errno.  arg is in kp->arg. */
7290 +static int param_atomic_t_set(const char *val, const struct kernel_param *kp)
7291 +{
7292 +       int err, n;
7293 +
7294 +       err = kstrtoint(val, 0, &n);
7295 +       if (!err) {
7296 +               if (n > 0)
7297 +                       au_debug_on();
7298 +               else
7299 +                       au_debug_off();
7300 +       }
7301 +       return err;
7302 +}
7303 +
7304 +/* Returns length written or -errno.  Buffer is 4k (ie. be short!) */
7305 +static int param_atomic_t_get(char *buffer, const struct kernel_param *kp)
7306 +{
7307 +       atomic_t *a;
7308 +
7309 +       a = kp->arg;
7310 +       return sprintf(buffer, "%d", atomic_read(a));
7311 +}
7312 +
7313 +static struct kernel_param_ops param_ops_atomic_t = {
7314 +       .set = param_atomic_t_set,
7315 +       .get = param_atomic_t_get
7316 +       /* void (*free)(void *arg) */
7317 +};
7318 +
7319 +atomic_t aufs_debug = ATOMIC_INIT(0);
7320 +MODULE_PARM_DESC(debug, "debug print");
7321 +module_param_named(debug, aufs_debug, atomic_t, 0664);
7322 +
7323 +DEFINE_MUTEX(au_dbg_mtx);      /* just to serialize the dbg msgs */
7324 +char *au_plevel = KERN_DEBUG;
7325 +#define dpri(fmt, ...) do {                                    \
7326 +       if ((au_plevel                                          \
7327 +            && strcmp(au_plevel, KERN_DEBUG))                  \
7328 +           || au_debug_test())                                 \
7329 +               printk("%s" fmt, au_plevel, ##__VA_ARGS__);     \
7330 +} while (0)
7331 +
7332 +/* ---------------------------------------------------------------------- */
7333 +
7334 +void au_dpri_whlist(struct au_nhash *whlist)
7335 +{
7336 +       unsigned long ul, n;
7337 +       struct hlist_head *head;
7338 +       struct au_vdir_wh *pos;
7339 +
7340 +       n = whlist->nh_num;
7341 +       head = whlist->nh_head;
7342 +       for (ul = 0; ul < n; ul++) {
7343 +               hlist_for_each_entry(pos, head, wh_hash)
7344 +                       dpri("b%d, %.*s, %d\n",
7345 +                            pos->wh_bindex,
7346 +                            pos->wh_str.len, pos->wh_str.name,
7347 +                            pos->wh_str.len);
7348 +               head++;
7349 +       }
7350 +}
7351 +
7352 +void au_dpri_vdir(struct au_vdir *vdir)
7353 +{
7354 +       unsigned long ul;
7355 +       union au_vdir_deblk_p p;
7356 +       unsigned char *o;
7357 +
7358 +       if (!vdir || IS_ERR(vdir)) {
7359 +               dpri("err %ld\n", PTR_ERR(vdir));
7360 +               return;
7361 +       }
7362 +
7363 +       dpri("deblk %u, nblk %lu, deblk %p, last{%lu, %p}, ver %llu\n",
7364 +            vdir->vd_deblk_sz, vdir->vd_nblk, vdir->vd_deblk,
7365 +            vdir->vd_last.ul, vdir->vd_last.p.deblk, vdir->vd_version);
7366 +       for (ul = 0; ul < vdir->vd_nblk; ul++) {
7367 +               p.deblk = vdir->vd_deblk[ul];
7368 +               o = p.deblk;
7369 +               dpri("[%lu]: %p\n", ul, o);
7370 +       }
7371 +}
7372 +
7373 +static int do_pri_inode(aufs_bindex_t bindex, struct inode *inode, int hn,
7374 +                       struct dentry *wh)
7375 +{
7376 +       char *n = NULL;
7377 +       int l = 0;
7378 +
7379 +       if (!inode || IS_ERR(inode)) {
7380 +               dpri("i%d: err %ld\n", bindex, PTR_ERR(inode));
7381 +               return -1;
7382 +       }
7383 +
7384 +       /* the type of i_blocks depends upon CONFIG_LBDAF */
7385 +       BUILD_BUG_ON(sizeof(inode->i_blocks) != sizeof(unsigned long)
7386 +                    && sizeof(inode->i_blocks) != sizeof(u64));
7387 +       if (wh) {
7388 +               n = (void *)wh->d_name.name;
7389 +               l = wh->d_name.len;
7390 +       }
7391 +
7392 +       dpri("i%d: %p, i%lu, %s, cnt %d, nl %u, 0%o, sz %llu, blk %llu,"
7393 +            " hn %d, ct %lld, np %lu, st 0x%lx, f 0x%x, v %llu, g %x%s%.*s\n",
7394 +            bindex, inode,
7395 +            inode->i_ino, inode->i_sb ? au_sbtype(inode->i_sb) : "??",
7396 +            atomic_read(&inode->i_count), inode->i_nlink, inode->i_mode,
7397 +            i_size_read(inode), (unsigned long long)inode->i_blocks,
7398 +            hn, (long long)timespec64_to_ns(&inode->i_ctime) & 0x0ffff,
7399 +            inode->i_mapping ? inode->i_mapping->nrpages : 0,
7400 +            inode->i_state, inode->i_flags, inode_peek_iversion(inode),
7401 +            inode->i_generation,
7402 +            l ? ", wh " : "", l, n);
7403 +       return 0;
7404 +}
7405 +
7406 +void au_dpri_inode(struct inode *inode)
7407 +{
7408 +       struct au_iinfo *iinfo;
7409 +       struct au_hinode *hi;
7410 +       aufs_bindex_t bindex;
7411 +       int err, hn;
7412 +
7413 +       err = do_pri_inode(-1, inode, -1, NULL);
7414 +       if (err || !au_test_aufs(inode->i_sb) || au_is_bad_inode(inode))
7415 +               return;
7416 +
7417 +       iinfo = au_ii(inode);
7418 +       dpri("i-1: btop %d, bbot %d, gen %d\n",
7419 +            iinfo->ii_btop, iinfo->ii_bbot, au_iigen(inode, NULL));
7420 +       if (iinfo->ii_btop < 0)
7421 +               return;
7422 +       hn = 0;
7423 +       for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot; bindex++) {
7424 +               hi = au_hinode(iinfo, bindex);
7425 +               hn = !!au_hn(hi);
7426 +               do_pri_inode(bindex, hi->hi_inode, hn, hi->hi_whdentry);
7427 +       }
7428 +}
7429 +
7430 +void au_dpri_dalias(struct inode *inode)
7431 +{
7432 +       struct dentry *d;
7433 +
7434 +       spin_lock(&inode->i_lock);
7435 +       hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias)
7436 +               au_dpri_dentry(d);
7437 +       spin_unlock(&inode->i_lock);
7438 +}
7439 +
7440 +static int do_pri_dentry(aufs_bindex_t bindex, struct dentry *dentry)
7441 +{
7442 +       struct dentry *wh = NULL;
7443 +       int hn;
7444 +       struct inode *inode;
7445 +       struct au_iinfo *iinfo;
7446 +       struct au_hinode *hi;
7447 +
7448 +       if (!dentry || IS_ERR(dentry)) {
7449 +               dpri("d%d: err %ld\n", bindex, PTR_ERR(dentry));
7450 +               return -1;
7451 +       }
7452 +       /* do not call dget_parent() here */
7453 +       /* note: access d_xxx without d_lock */
7454 +       dpri("d%d: %p, %pd2?, %s, cnt %d, flags 0x%x, %shashed\n",
7455 +            bindex, dentry, dentry,
7456 +            dentry->d_sb ? au_sbtype(dentry->d_sb) : "??",
7457 +            au_dcount(dentry), dentry->d_flags,
7458 +            d_unhashed(dentry) ? "un" : "");
7459 +       hn = -1;
7460 +       inode = NULL;
7461 +       if (d_is_positive(dentry))
7462 +               inode = d_inode(dentry);
7463 +       if (inode
7464 +           && au_test_aufs(dentry->d_sb)
7465 +           && bindex >= 0
7466 +           && !au_is_bad_inode(inode)) {
7467 +               iinfo = au_ii(inode);
7468 +               hi = au_hinode(iinfo, bindex);
7469 +               hn = !!au_hn(hi);
7470 +               wh = hi->hi_whdentry;
7471 +       }
7472 +       do_pri_inode(bindex, inode, hn, wh);
7473 +       return 0;
7474 +}
7475 +
7476 +void au_dpri_dentry(struct dentry *dentry)
7477 +{
7478 +       struct au_dinfo *dinfo;
7479 +       aufs_bindex_t bindex;
7480 +       int err;
7481 +
7482 +       err = do_pri_dentry(-1, dentry);
7483 +       if (err || !au_test_aufs(dentry->d_sb))
7484 +               return;
7485 +
7486 +       dinfo = au_di(dentry);
7487 +       if (!dinfo)
7488 +               return;
7489 +       dpri("d-1: btop %d, bbot %d, bwh %d, bdiropq %d, gen %d, tmp %d\n",
7490 +            dinfo->di_btop, dinfo->di_bbot,
7491 +            dinfo->di_bwh, dinfo->di_bdiropq, au_digen(dentry),
7492 +            dinfo->di_tmpfile);
7493 +       if (dinfo->di_btop < 0)
7494 +               return;
7495 +       for (bindex = dinfo->di_btop; bindex <= dinfo->di_bbot; bindex++)
7496 +               do_pri_dentry(bindex, au_hdentry(dinfo, bindex)->hd_dentry);
7497 +}
7498 +
7499 +static int do_pri_file(aufs_bindex_t bindex, struct file *file)
7500 +{
7501 +       char a[32];
7502 +
7503 +       if (!file || IS_ERR(file)) {
7504 +               dpri("f%d: err %ld\n", bindex, PTR_ERR(file));
7505 +               return -1;
7506 +       }
7507 +       a[0] = 0;
7508 +       if (bindex < 0
7509 +           && !IS_ERR_OR_NULL(file->f_path.dentry)
7510 +           && au_test_aufs(file->f_path.dentry->d_sb)
7511 +           && au_fi(file))
7512 +               snprintf(a, sizeof(a), ", gen %d, mmapped %d",
7513 +                        au_figen(file), atomic_read(&au_fi(file)->fi_mmapped));
7514 +       dpri("f%d: mode 0x%x, flags 0%o, cnt %ld, v %llu, pos %llu%s\n",
7515 +            bindex, file->f_mode, file->f_flags, (long)file_count(file),
7516 +            file->f_version, file->f_pos, a);
7517 +       if (!IS_ERR_OR_NULL(file->f_path.dentry))
7518 +               do_pri_dentry(bindex, file->f_path.dentry);
7519 +       return 0;
7520 +}
7521 +
7522 +void au_dpri_file(struct file *file)
7523 +{
7524 +       struct au_finfo *finfo;
7525 +       struct au_fidir *fidir;
7526 +       struct au_hfile *hfile;
7527 +       aufs_bindex_t bindex;
7528 +       int err;
7529 +
7530 +       err = do_pri_file(-1, file);
7531 +       if (err
7532 +           || IS_ERR_OR_NULL(file->f_path.dentry)
7533 +           || !au_test_aufs(file->f_path.dentry->d_sb))
7534 +               return;
7535 +
7536 +       finfo = au_fi(file);
7537 +       if (!finfo)
7538 +               return;
7539 +       if (finfo->fi_btop < 0)
7540 +               return;
7541 +       fidir = finfo->fi_hdir;
7542 +       if (!fidir)
7543 +               do_pri_file(finfo->fi_btop, finfo->fi_htop.hf_file);
7544 +       else
7545 +               for (bindex = finfo->fi_btop;
7546 +                    bindex >= 0 && bindex <= fidir->fd_bbot;
7547 +                    bindex++) {
7548 +                       hfile = fidir->fd_hfile + bindex;
7549 +                       do_pri_file(bindex, hfile ? hfile->hf_file : NULL);
7550 +               }
7551 +}
7552 +
7553 +static int do_pri_br(aufs_bindex_t bindex, struct au_branch *br)
7554 +{
7555 +       struct vfsmount *mnt;
7556 +       struct super_block *sb;
7557 +
7558 +       if (!br || IS_ERR(br))
7559 +               goto out;
7560 +       mnt = au_br_mnt(br);
7561 +       if (!mnt || IS_ERR(mnt))
7562 +               goto out;
7563 +       sb = mnt->mnt_sb;
7564 +       if (!sb || IS_ERR(sb))
7565 +               goto out;
7566 +
7567 +       dpri("s%d: {perm 0x%x, id %d, wbr %p}, "
7568 +            "%s, dev 0x%02x%02x, flags 0x%lx, cnt %d, active %d, "
7569 +            "xino %d\n",
7570 +            bindex, br->br_perm, br->br_id, br->br_wbr,
7571 +            au_sbtype(sb), MAJOR(sb->s_dev), MINOR(sb->s_dev),
7572 +            sb->s_flags, sb->s_count,
7573 +            atomic_read(&sb->s_active),
7574 +            !!au_xino_file(br->br_xino, /*idx*/-1));
7575 +       return 0;
7576 +
7577 +out:
7578 +       dpri("s%d: err %ld\n", bindex, PTR_ERR(br));
7579 +       return -1;
7580 +}
7581 +
7582 +void au_dpri_sb(struct super_block *sb)
7583 +{
7584 +       struct au_sbinfo *sbinfo;
7585 +       aufs_bindex_t bindex;
7586 +       int err;
7587 +       /* to reduce stack size */
7588 +       struct {
7589 +               struct vfsmount mnt;
7590 +               struct au_branch fake;
7591 +       } *a;
7592 +
7593 +       /* this function can be called from magic sysrq */
7594 +       a = kzalloc(sizeof(*a), GFP_ATOMIC);
7595 +       if (unlikely(!a)) {
7596 +               dpri("no memory\n");
7597 +               return;
7598 +       }
7599 +
7600 +       a->mnt.mnt_sb = sb;
7601 +       a->fake.br_path.mnt = &a->mnt;
7602 +       err = do_pri_br(-1, &a->fake);
7603 +       au_kfree_rcu(a);
7604 +       dpri("dev 0x%x\n", sb->s_dev);
7605 +       if (err || !au_test_aufs(sb))
7606 +               return;
7607 +
7608 +       sbinfo = au_sbi(sb);
7609 +       if (!sbinfo)
7610 +               return;
7611 +       dpri("nw %d, gen %u, kobj %d\n",
7612 +            atomic_read(&sbinfo->si_nowait.nw_len), sbinfo->si_generation,
7613 +            kref_read(&sbinfo->si_kobj.kref));
7614 +       for (bindex = 0; bindex <= sbinfo->si_bbot; bindex++)
7615 +               do_pri_br(bindex, sbinfo->si_branch[0 + bindex]);
7616 +}
7617 +
7618 +/* ---------------------------------------------------------------------- */
7619 +
7620 +void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line)
7621 +{
7622 +       struct inode *h_inode, *inode = d_inode(dentry);
7623 +       struct dentry *h_dentry;
7624 +       aufs_bindex_t bindex, bbot, bi;
7625 +
7626 +       if (!inode /* || au_di(dentry)->di_lsc == AuLsc_DI_TMP */)
7627 +               return;
7628 +
7629 +       bbot = au_dbbot(dentry);
7630 +       bi = au_ibbot(inode);
7631 +       if (bi < bbot)
7632 +               bbot = bi;
7633 +       bindex = au_dbtop(dentry);
7634 +       bi = au_ibtop(inode);
7635 +       if (bi > bindex)
7636 +               bindex = bi;
7637 +
7638 +       for (; bindex <= bbot; bindex++) {
7639 +               h_dentry = au_h_dptr(dentry, bindex);
7640 +               if (!h_dentry)
7641 +                       continue;
7642 +               h_inode = au_h_iptr(inode, bindex);
7643 +               if (unlikely(h_inode != d_inode(h_dentry))) {
7644 +                       au_debug_on();
7645 +                       AuDbg("b%d, %s:%d\n", bindex, func, line);
7646 +                       AuDbgDentry(dentry);
7647 +                       AuDbgInode(inode);
7648 +                       au_debug_off();
7649 +                       if (au_test_fuse(h_inode->i_sb))
7650 +                               WARN_ON_ONCE(1);
7651 +                       else
7652 +                               BUG();
7653 +               }
7654 +       }
7655 +}
7656 +
7657 +void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen)
7658 +{
7659 +       int err, i, j;
7660 +       struct au_dcsub_pages dpages;
7661 +       struct au_dpage *dpage;
7662 +       struct dentry **dentries;
7663 +
7664 +       err = au_dpages_init(&dpages, GFP_NOFS);
7665 +       AuDebugOn(err);
7666 +       err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/1);
7667 +       AuDebugOn(err);
7668 +       for (i = dpages.ndpage - 1; !err && i >= 0; i--) {
7669 +               dpage = dpages.dpages + i;
7670 +               dentries = dpage->dentries;
7671 +               for (j = dpage->ndentry - 1; !err && j >= 0; j--)
7672 +                       AuDebugOn(au_digen_test(dentries[j], sigen));
7673 +       }
7674 +       au_dpages_free(&dpages);
7675 +}
7676 +
7677 +void au_dbg_verify_kthread(void)
7678 +{
7679 +       if (au_wkq_test()) {
7680 +               au_dbg_blocked();
7681 +               /*
7682 +                * It may be recursive, but udba=notify between two aufs mounts,
7683 +                * where a single ro branch is shared, is not a problem.
7684 +                */
7685 +               /* WARN_ON(1); */
7686 +       }
7687 +}
7688 +
7689 +/* ---------------------------------------------------------------------- */
7690 +
7691 +int __init au_debug_init(void)
7692 +{
7693 +       aufs_bindex_t bindex;
7694 +       struct au_vdir_destr destr;
7695 +
7696 +       bindex = -1;
7697 +       AuDebugOn(bindex >= 0);
7698 +
7699 +       destr.len = -1;
7700 +       AuDebugOn(destr.len < NAME_MAX);
7701 +
7702 +#ifdef CONFIG_4KSTACKS
7703 +       pr_warn("CONFIG_4KSTACKS is defined.\n");
7704 +#endif
7705 +
7706 +       return 0;
7707 +}
7708 diff -urN /usr/share/empty/fs/aufs/debug.h linux/fs/aufs/debug.h
7709 --- /usr/share/empty/fs/aufs/debug.h    1970-01-01 01:00:00.000000000 +0100
7710 +++ linux/fs/aufs/debug.h       2022-03-21 14:49:05.719966344 +0100
7711 @@ -0,0 +1,226 @@
7712 +/* SPDX-License-Identifier: GPL-2.0 */
7713 +/*
7714 + * Copyright (C) 2005-2021 Junjiro R. Okajima
7715 + *
7716 + * This program, aufs is free software; you can redistribute it and/or modify
7717 + * it under the terms of the GNU General Public License as published by
7718 + * the Free Software Foundation; either version 2 of the License, or
7719 + * (at your option) any later version.
7720 + *
7721 + * This program is distributed in the hope that it will be useful,
7722 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7723 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7724 + * GNU General Public License for more details.
7725 + *
7726 + * You should have received a copy of the GNU General Public License
7727 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
7728 + */
7729 +
7730 +/*
7731 + * debug print functions
7732 + */
7733 +
7734 +#ifndef __AUFS_DEBUG_H__
7735 +#define __AUFS_DEBUG_H__
7736 +
7737 +#ifdef __KERNEL__
7738 +
7739 +#include <linux/atomic.h>
7740 +#include <linux/module.h>
7741 +#include <linux/kallsyms.h>
7742 +#include <linux/sysrq.h>
7743 +
7744 +#ifdef CONFIG_AUFS_DEBUG
7745 +#define AuDebugOn(a)           BUG_ON(a)
7746 +
7747 +/* module parameter */
7748 +extern atomic_t aufs_debug;
7749 +static inline void au_debug_on(void)
7750 +{
7751 +       atomic_inc(&aufs_debug);
7752 +}
7753 +static inline void au_debug_off(void)
7754 +{
7755 +       atomic_dec_if_positive(&aufs_debug);
7756 +}
7757 +
7758 +static inline int au_debug_test(void)
7759 +{
7760 +       return atomic_read(&aufs_debug) > 0;
7761 +}
7762 +#else
7763 +#define AuDebugOn(a)           do {} while (0)
7764 +AuStubVoid(au_debug_on, void)
7765 +AuStubVoid(au_debug_off, void)
7766 +AuStubInt0(au_debug_test, void)
7767 +#endif /* CONFIG_AUFS_DEBUG */
7768 +
7769 +#define param_check_atomic_t(name, p) __param_check(name, p, atomic_t)
7770 +
7771 +/* ---------------------------------------------------------------------- */
7772 +
7773 +/* debug print */
7774 +
7775 +#define AuDbg(fmt, ...) do { \
7776 +       if (au_debug_test()) \
7777 +               pr_debug("DEBUG: " fmt, ##__VA_ARGS__); \
7778 +} while (0)
7779 +#define AuLabel(l)             AuDbg(#l "\n")
7780 +#define AuIOErr(fmt, ...)      pr_err("I/O Error, " fmt, ##__VA_ARGS__)
7781 +#define AuWarn1(fmt, ...) do { \
7782 +       static unsigned char _c; \
7783 +       if (!_c++) \
7784 +               pr_warn(fmt, ##__VA_ARGS__); \
7785 +} while (0)
7786 +
7787 +#define AuErr1(fmt, ...) do { \
7788 +       static unsigned char _c; \
7789 +       if (!_c++) \
7790 +               pr_err(fmt, ##__VA_ARGS__); \
7791 +} while (0)
7792 +
7793 +#define AuIOErr1(fmt, ...) do { \
7794 +       static unsigned char _c; \
7795 +       if (!_c++) \
7796 +               AuIOErr(fmt, ##__VA_ARGS__); \
7797 +} while (0)
7798 +
7799 +#define AuUnsupportMsg "This operation is not supported." \
7800 +                       " Please report this application to aufs-users ML."
7801 +#define AuUnsupport(fmt, ...) do { \
7802 +       pr_err(AuUnsupportMsg "\n" fmt, ##__VA_ARGS__); \
7803 +       dump_stack(); \
7804 +} while (0)
7805 +
7806 +#define AuTraceErr(e) do { \
7807 +       if (unlikely((e) < 0)) \
7808 +               AuDbg("err %d\n", (int)(e)); \
7809 +} while (0)
7810 +
7811 +#define AuTraceErrPtr(p) do { \
7812 +       if (IS_ERR(p)) \
7813 +               AuDbg("err %ld\n", PTR_ERR(p)); \
7814 +} while (0)
7815 +
7816 +/* dirty macros for debug print, use with "%.*s" and caution */
7817 +#define AuLNPair(qstr)         (qstr)->len, (qstr)->name
7818 +
7819 +/* ---------------------------------------------------------------------- */
7820 +
7821 +struct dentry;
7822 +#ifdef CONFIG_AUFS_DEBUG
7823 +extern struct mutex au_dbg_mtx;
7824 +extern char *au_plevel;
7825 +struct au_nhash;
7826 +void au_dpri_whlist(struct au_nhash *whlist);
7827 +struct au_vdir;
7828 +void au_dpri_vdir(struct au_vdir *vdir);
7829 +struct inode;
7830 +void au_dpri_inode(struct inode *inode);
7831 +void au_dpri_dalias(struct inode *inode);
7832 +void au_dpri_dentry(struct dentry *dentry);
7833 +struct file;
7834 +void au_dpri_file(struct file *filp);
7835 +struct super_block;
7836 +void au_dpri_sb(struct super_block *sb);
7837 +
7838 +#define au_dbg_verify_dinode(d) __au_dbg_verify_dinode(d, __func__, __LINE__)
7839 +void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line);
7840 +void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen);
7841 +void au_dbg_verify_kthread(void);
7842 +
7843 +int __init au_debug_init(void);
7844 +
7845 +#define AuDbgWhlist(w) do { \
7846 +       mutex_lock(&au_dbg_mtx); \
7847 +       AuDbg(#w "\n"); \
7848 +       au_dpri_whlist(w); \
7849 +       mutex_unlock(&au_dbg_mtx); \
7850 +} while (0)
7851 +
7852 +#define AuDbgVdir(v) do { \
7853 +       mutex_lock(&au_dbg_mtx); \
7854 +       AuDbg(#v "\n"); \
7855 +       au_dpri_vdir(v); \
7856 +       mutex_unlock(&au_dbg_mtx); \
7857 +} while (0)
7858 +
7859 +#define AuDbgInode(i) do { \
7860 +       mutex_lock(&au_dbg_mtx); \
7861 +       AuDbg(#i "\n"); \
7862 +       au_dpri_inode(i); \
7863 +       mutex_unlock(&au_dbg_mtx); \
7864 +} while (0)
7865 +
7866 +#define AuDbgDAlias(i) do { \
7867 +       mutex_lock(&au_dbg_mtx); \
7868 +       AuDbg(#i "\n"); \
7869 +       au_dpri_dalias(i); \
7870 +       mutex_unlock(&au_dbg_mtx); \
7871 +} while (0)
7872 +
7873 +#define AuDbgDentry(d) do { \
7874 +       mutex_lock(&au_dbg_mtx); \
7875 +       AuDbg(#d "\n"); \
7876 +       au_dpri_dentry(d); \
7877 +       mutex_unlock(&au_dbg_mtx); \
7878 +} while (0)
7879 +
7880 +#define AuDbgFile(f) do { \
7881 +       mutex_lock(&au_dbg_mtx); \
7882 +       AuDbg(#f "\n"); \
7883 +       au_dpri_file(f); \
7884 +       mutex_unlock(&au_dbg_mtx); \
7885 +} while (0)
7886 +
7887 +#define AuDbgSb(sb) do { \
7888 +       mutex_lock(&au_dbg_mtx); \
7889 +       AuDbg(#sb "\n"); \
7890 +       au_dpri_sb(sb); \
7891 +       mutex_unlock(&au_dbg_mtx); \
7892 +} while (0)
7893 +
7894 +#define AuDbgSym(addr) do {                            \
7895 +       char sym[KSYM_SYMBOL_LEN];                      \
7896 +       sprint_symbol(sym, (unsigned long)addr);        \
7897 +       AuDbg("%s\n", sym);                             \
7898 +} while (0)
7899 +#else
7900 +AuStubVoid(au_dbg_verify_dinode, struct dentry *dentry)
7901 +AuStubVoid(au_dbg_verify_gen, struct dentry *parent, unsigned int sigen)
7902 +AuStubVoid(au_dbg_verify_kthread, void)
7903 +AuStubInt0(__init au_debug_init, void)
7904 +
7905 +#define AuDbgWhlist(w)         do {} while (0)
7906 +#define AuDbgVdir(v)           do {} while (0)
7907 +#define AuDbgInode(i)          do {} while (0)
7908 +#define AuDbgDAlias(i)         do {} while (0)
7909 +#define AuDbgDentry(d)         do {} while (0)
7910 +#define AuDbgFile(f)           do {} while (0)
7911 +#define AuDbgSb(sb)            do {} while (0)
7912 +#define AuDbgSym(addr)         do {} while (0)
7913 +#endif /* CONFIG_AUFS_DEBUG */
7914 +
7915 +/* ---------------------------------------------------------------------- */
7916 +
7917 +#ifdef CONFIG_AUFS_MAGIC_SYSRQ
7918 +int __init au_sysrq_init(void);
7919 +void au_sysrq_fin(void);
7920 +
7921 +#ifdef CONFIG_HW_CONSOLE
7922 +#define au_dbg_blocked() do { \
7923 +       WARN_ON(1); \
7924 +       handle_sysrq('w'); \
7925 +} while (0)
7926 +#else
7927 +AuStubVoid(au_dbg_blocked, void)
7928 +#endif
7929 +
7930 +#else
7931 +AuStubInt0(__init au_sysrq_init, void)
7932 +AuStubVoid(au_sysrq_fin, void)
7933 +AuStubVoid(au_dbg_blocked, void)
7934 +#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
7935 +
7936 +#endif /* __KERNEL__ */
7937 +#endif /* __AUFS_DEBUG_H__ */
7938 diff -urN /usr/share/empty/fs/aufs/dentry.c linux/fs/aufs/dentry.c
7939 --- /usr/share/empty/fs/aufs/dentry.c   1970-01-01 01:00:00.000000000 +0100
7940 +++ linux/fs/aufs/dentry.c      2022-03-21 14:49:05.719966344 +0100
7941 @@ -0,0 +1,1168 @@
7942 +// SPDX-License-Identifier: GPL-2.0
7943 +/*
7944 + * Copyright (C) 2005-2021 Junjiro R. Okajima
7945 + *
7946 + * This program, aufs is free software; you can redistribute it and/or modify
7947 + * it under the terms of the GNU General Public License as published by
7948 + * the Free Software Foundation; either version 2 of the License, or
7949 + * (at your option) any later version.
7950 + *
7951 + * This program is distributed in the hope that it will be useful,
7952 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7953 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7954 + * GNU General Public License for more details.
7955 + *
7956 + * You should have received a copy of the GNU General Public License
7957 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
7958 + */
7959 +
7960 +/*
7961 + * lookup and dentry operations
7962 + */
7963 +
7964 +#include <linux/iversion.h>
7965 +#include "aufs.h"
7966 +
7967 +/*
7968 + * returns positive/negative dentry, NULL or an error.
7969 + * NULL means whiteout-ed or not-found.
7970 + */
7971 +static struct dentry*
7972 +au_do_lookup(struct dentry *h_parent, struct dentry *dentry,
7973 +            aufs_bindex_t bindex, struct au_do_lookup_args *args)
7974 +{
7975 +       struct dentry *h_dentry;
7976 +       struct inode *h_inode;
7977 +       struct au_branch *br;
7978 +       struct user_namespace *h_userns;
7979 +       struct path h_path;
7980 +       int wh_found, opq;
7981 +       unsigned char wh_able;
7982 +       const unsigned char allow_neg = !!au_ftest_lkup(args->flags, ALLOW_NEG);
7983 +       const unsigned char ignore_perm = !!au_ftest_lkup(args->flags,
7984 +                                                         IGNORE_PERM);
7985 +
7986 +       wh_found = 0;
7987 +       br = au_sbr(dentry->d_sb, bindex);
7988 +       h_path.dentry = h_parent;
7989 +       h_path.mnt = au_br_mnt(br);
7990 +       h_userns = au_br_userns(br);
7991 +       wh_able = !!au_br_whable(br->br_perm);
7992 +       if (wh_able)
7993 +               wh_found = au_wh_test(h_userns, &h_path, &args->whname,
7994 +                                     ignore_perm);
7995 +       h_dentry = ERR_PTR(wh_found);
7996 +       if (!wh_found)
7997 +               goto real_lookup;
7998 +       if (unlikely(wh_found < 0))
7999 +               goto out;
8000 +
8001 +       /* We found a whiteout */
8002 +       /* au_set_dbbot(dentry, bindex); */
8003 +       au_set_dbwh(dentry, bindex);
8004 +       if (!allow_neg)
8005 +               return NULL; /* success */
8006 +
8007 +real_lookup:
8008 +       if (!ignore_perm)
8009 +               h_dentry = vfsub_lkup_one(args->name, &h_path);
8010 +       else
8011 +               h_dentry = au_sio_lkup_one(h_userns, args->name, &h_path);
8012 +       if (IS_ERR(h_dentry)) {
8013 +               if (PTR_ERR(h_dentry) == -ENAMETOOLONG
8014 +                   && !allow_neg)
8015 +                       h_dentry = NULL;
8016 +               goto out;
8017 +       }
8018 +
8019 +       h_inode = d_inode(h_dentry);
8020 +       if (d_is_negative(h_dentry)) {
8021 +               if (!allow_neg)
8022 +                       goto out_neg;
8023 +       } else if (wh_found
8024 +                  || (args->type && args->type != (h_inode->i_mode & S_IFMT)))
8025 +               goto out_neg;
8026 +       else if (au_ftest_lkup(args->flags, DIRREN)
8027 +                /* && h_inode */
8028 +                && !au_dr_lkup_h_ino(args, bindex, h_inode->i_ino)) {
8029 +               AuDbg("b%d %pd ignored hi%llu\n", bindex, h_dentry,
8030 +                     (unsigned long long)h_inode->i_ino);
8031 +               goto out_neg;
8032 +       }
8033 +
8034 +       if (au_dbbot(dentry) <= bindex)
8035 +               au_set_dbbot(dentry, bindex);
8036 +       if (au_dbtop(dentry) < 0 || bindex < au_dbtop(dentry))
8037 +               au_set_dbtop(dentry, bindex);
8038 +       au_set_h_dptr(dentry, bindex, h_dentry);
8039 +
8040 +       if (!d_is_dir(h_dentry)
8041 +           || !wh_able
8042 +           || (d_really_is_positive(dentry) && !d_is_dir(dentry)))
8043 +               goto out; /* success */
8044 +
8045 +       h_path.dentry = h_dentry;
8046 +       inode_lock_shared_nested(h_inode, AuLsc_I_CHILD);
8047 +       opq = au_diropq_test(h_userns, &h_path);
8048 +       inode_unlock_shared(h_inode);
8049 +       if (opq > 0)
8050 +               au_set_dbdiropq(dentry, bindex);
8051 +       else if (unlikely(opq < 0)) {
8052 +               au_set_h_dptr(dentry, bindex, NULL);
8053 +               h_dentry = ERR_PTR(opq);
8054 +       }
8055 +       goto out;
8056 +
8057 +out_neg:
8058 +       dput(h_dentry);
8059 +       h_dentry = NULL;
8060 +out:
8061 +       return h_dentry;
8062 +}
8063 +
8064 +static int au_test_shwh(struct super_block *sb, const struct qstr *name)
8065 +{
8066 +       if (unlikely(!au_opt_test(au_mntflags(sb), SHWH)
8067 +                    && !strncmp(name->name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)))
8068 +               return -EPERM;
8069 +       return 0;
8070 +}
8071 +
8072 +/*
8073 + * returns the number of lower positive dentries,
8074 + * otherwise an error.
8075 + * can be called at unlinking with @type is zero.
8076 + */
8077 +int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t btop,
8078 +                  unsigned int flags)
8079 +{
8080 +       int npositive, err;
8081 +       aufs_bindex_t bindex, btail, bdiropq;
8082 +       unsigned char isdir, dirperm1, dirren;
8083 +       struct au_do_lookup_args args = {
8084 +               .flags          = flags,
8085 +               .name           = &dentry->d_name
8086 +       };
8087 +       struct dentry *parent;
8088 +       struct super_block *sb;
8089 +
8090 +       sb = dentry->d_sb;
8091 +       err = au_test_shwh(sb, args.name);
8092 +       if (unlikely(err))
8093 +               goto out;
8094 +
8095 +       err = au_wh_name_alloc(&args.whname, args.name);
8096 +       if (unlikely(err))
8097 +               goto out;
8098 +
8099 +       isdir = !!d_is_dir(dentry);
8100 +       dirperm1 = !!au_opt_test(au_mntflags(sb), DIRPERM1);
8101 +       dirren = !!au_opt_test(au_mntflags(sb), DIRREN);
8102 +       if (dirren)
8103 +               au_fset_lkup(args.flags, DIRREN);
8104 +
8105 +       npositive = 0;
8106 +       parent = dget_parent(dentry);
8107 +       btail = au_dbtaildir(parent);
8108 +       for (bindex = btop; bindex <= btail; bindex++) {
8109 +               struct dentry *h_parent, *h_dentry;
8110 +               struct inode *h_inode, *h_dir;
8111 +               struct au_branch *br;
8112 +
8113 +               h_dentry = au_h_dptr(dentry, bindex);
8114 +               if (h_dentry) {
8115 +                       if (d_is_positive(h_dentry))
8116 +                               npositive++;
8117 +                       break;
8118 +               }
8119 +               h_parent = au_h_dptr(parent, bindex);
8120 +               if (!h_parent || !d_is_dir(h_parent))
8121 +                       continue;
8122 +
8123 +               if (dirren) {
8124 +                       /* if the inum matches, then use the prepared name */
8125 +                       err = au_dr_lkup_name(&args, bindex);
8126 +                       if (unlikely(err))
8127 +                               goto out_parent;
8128 +               }
8129 +
8130 +               h_dir = d_inode(h_parent);
8131 +               inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
8132 +               h_dentry = au_do_lookup(h_parent, dentry, bindex, &args);
8133 +               inode_unlock_shared(h_dir);
8134 +               err = PTR_ERR(h_dentry);
8135 +               if (IS_ERR(h_dentry))
8136 +                       goto out_parent;
8137 +               if (h_dentry)
8138 +                       au_fclr_lkup(args.flags, ALLOW_NEG);
8139 +               if (dirperm1)
8140 +                       au_fset_lkup(args.flags, IGNORE_PERM);
8141 +
8142 +               if (au_dbwh(dentry) == bindex)
8143 +                       break;
8144 +               if (!h_dentry)
8145 +                       continue;
8146 +               if (d_is_negative(h_dentry))
8147 +                       continue;
8148 +               h_inode = d_inode(h_dentry);
8149 +               npositive++;
8150 +               if (!args.type)
8151 +                       args.type = h_inode->i_mode & S_IFMT;
8152 +               if (args.type != S_IFDIR)
8153 +                       break;
8154 +               else if (isdir) {
8155 +                       /* the type of lower may be different */
8156 +                       bdiropq = au_dbdiropq(dentry);
8157 +                       if (bdiropq >= 0 && bdiropq <= bindex)
8158 +                               break;
8159 +               }
8160 +               br = au_sbr(sb, bindex);
8161 +               if (dirren
8162 +                   && au_dr_hino_test_add(&br->br_dirren, h_inode->i_ino,
8163 +                                          /*add_ent*/NULL)) {
8164 +                       /* prepare next name to lookup */
8165 +                       err = au_dr_lkup(&args, dentry, bindex);
8166 +                       if (unlikely(err))
8167 +                               goto out_parent;
8168 +               }
8169 +       }
8170 +
8171 +       if (npositive) {
8172 +               AuLabel(positive);
8173 +               au_update_dbtop(dentry);
8174 +       }
8175 +       err = npositive;
8176 +       if (unlikely(!au_opt_test(au_mntflags(sb), UDBA_NONE)
8177 +                    && au_dbtop(dentry) < 0)) {
8178 +               err = -EIO;
8179 +               AuIOErr("both of real entry and whiteout found, %pd, err %d\n",
8180 +                       dentry, err);
8181 +       }
8182 +
8183 +out_parent:
8184 +       dput(parent);
8185 +       au_kfree_try_rcu(args.whname.name);
8186 +       if (dirren)
8187 +               au_dr_lkup_fin(&args);
8188 +out:
8189 +       return err;
8190 +}
8191 +
8192 +struct dentry *au_sio_lkup_one(struct user_namespace *userns, struct qstr *name,
8193 +                              struct path *ppath)
8194 +{
8195 +       struct dentry *dentry;
8196 +       int wkq_err;
8197 +
8198 +       if (!au_test_h_perm_sio(userns, d_inode(ppath->dentry), MAY_EXEC))
8199 +               dentry = vfsub_lkup_one(name, ppath);
8200 +       else {
8201 +               struct vfsub_lkup_one_args args = {
8202 +                       .errp   = &dentry,
8203 +                       .name   = name,
8204 +                       .ppath  = ppath
8205 +               };
8206 +
8207 +               wkq_err = au_wkq_wait(vfsub_call_lkup_one, &args);
8208 +               if (unlikely(wkq_err))
8209 +                       dentry = ERR_PTR(wkq_err);
8210 +       }
8211 +
8212 +       return dentry;
8213 +}
8214 +
8215 +/*
8216 + * lookup @dentry on @bindex which should be negative.
8217 + */
8218 +int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh)
8219 +{
8220 +       int err;
8221 +       struct dentry *parent, *h_dentry;
8222 +       struct au_branch *br;
8223 +       struct user_namespace *h_userns;
8224 +       struct path h_ppath;
8225 +
8226 +       parent = dget_parent(dentry);
8227 +       br = au_sbr(dentry->d_sb, bindex);
8228 +       h_ppath.dentry = au_h_dptr(parent, bindex);
8229 +       h_ppath.mnt = au_br_mnt(br);
8230 +       h_userns = au_br_userns(br);
8231 +       if (wh)
8232 +               h_dentry = au_whtmp_lkup(h_ppath.dentry, br, &dentry->d_name);
8233 +       else
8234 +               h_dentry = au_sio_lkup_one(h_userns, &dentry->d_name, &h_ppath);
8235 +       err = PTR_ERR(h_dentry);
8236 +       if (IS_ERR(h_dentry))
8237 +               goto out;
8238 +       if (unlikely(d_is_positive(h_dentry))) {
8239 +               err = -EIO;
8240 +               AuIOErr("%pd should be negative on b%d.\n", h_dentry, bindex);
8241 +               dput(h_dentry);
8242 +               goto out;
8243 +       }
8244 +
8245 +       err = 0;
8246 +       if (bindex < au_dbtop(dentry))
8247 +               au_set_dbtop(dentry, bindex);
8248 +       if (au_dbbot(dentry) < bindex)
8249 +               au_set_dbbot(dentry, bindex);
8250 +       au_set_h_dptr(dentry, bindex, h_dentry);
8251 +
8252 +out:
8253 +       dput(parent);
8254 +       return err;
8255 +}
8256 +
8257 +/* ---------------------------------------------------------------------- */
8258 +
8259 +/* subset of struct inode */
8260 +struct au_iattr {
8261 +       unsigned long           i_ino;
8262 +       /* unsigned int         i_nlink; */
8263 +       kuid_t                  i_uid;
8264 +       kgid_t                  i_gid;
8265 +       u64                     i_version;
8266 +/*
8267 +       loff_t                  i_size;
8268 +       blkcnt_t                i_blocks;
8269 +*/
8270 +       umode_t                 i_mode;
8271 +};
8272 +
8273 +static void au_iattr_save(struct au_iattr *ia, struct inode *h_inode)
8274 +{
8275 +       ia->i_ino = h_inode->i_ino;
8276 +       /* ia->i_nlink = h_inode->i_nlink; */
8277 +       ia->i_uid = h_inode->i_uid;
8278 +       ia->i_gid = h_inode->i_gid;
8279 +       ia->i_version = inode_query_iversion(h_inode);
8280 +/*
8281 +       ia->i_size = h_inode->i_size;
8282 +       ia->i_blocks = h_inode->i_blocks;
8283 +*/
8284 +       ia->i_mode = (h_inode->i_mode & S_IFMT);
8285 +}
8286 +
8287 +static int au_iattr_test(struct au_iattr *ia, struct inode *h_inode)
8288 +{
8289 +       return ia->i_ino != h_inode->i_ino
8290 +               /* || ia->i_nlink != h_inode->i_nlink */
8291 +               || !uid_eq(ia->i_uid, h_inode->i_uid)
8292 +               || !gid_eq(ia->i_gid, h_inode->i_gid)
8293 +               || !inode_eq_iversion(h_inode, ia->i_version)
8294 +/*
8295 +               || ia->i_size != h_inode->i_size
8296 +               || ia->i_blocks != h_inode->i_blocks
8297 +*/
8298 +               || ia->i_mode != (h_inode->i_mode & S_IFMT);
8299 +}
8300 +
8301 +static int au_h_verify_dentry(struct dentry *h_dentry, struct dentry *h_parent,
8302 +                             struct au_branch *br)
8303 +{
8304 +       int err;
8305 +       struct au_iattr ia;
8306 +       struct inode *h_inode;
8307 +       struct dentry *h_d;
8308 +       struct super_block *h_sb;
8309 +       struct path h_ppath;
8310 +
8311 +       err = 0;
8312 +       memset(&ia, -1, sizeof(ia));
8313 +       h_sb = h_dentry->d_sb;
8314 +       h_inode = NULL;
8315 +       if (d_is_positive(h_dentry)) {
8316 +               h_inode = d_inode(h_dentry);
8317 +               au_iattr_save(&ia, h_inode);
8318 +       } else if (au_test_nfs(h_sb) || au_test_fuse(h_sb))
8319 +               /* nfs d_revalidate may return 0 for negative dentry */
8320 +               /* fuse d_revalidate always return 0 for negative dentry */
8321 +               goto out;
8322 +
8323 +       /* main purpose is namei.c:cached_lookup() and d_revalidate */
8324 +       h_ppath.dentry = h_parent;
8325 +       h_ppath.mnt = au_br_mnt(br);
8326 +       h_d = vfsub_lkup_one(&h_dentry->d_name, &h_ppath);
8327 +       err = PTR_ERR(h_d);
8328 +       if (IS_ERR(h_d))
8329 +               goto out;
8330 +
8331 +       err = 0;
8332 +       if (unlikely(h_d != h_dentry
8333 +                    || d_inode(h_d) != h_inode
8334 +                    || (h_inode && au_iattr_test(&ia, h_inode))))
8335 +               err = au_busy_or_stale();
8336 +       dput(h_d);
8337 +
8338 +out:
8339 +       AuTraceErr(err);
8340 +       return err;
8341 +}
8342 +
8343 +int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
8344 +               struct dentry *h_parent, struct au_branch *br)
8345 +{
8346 +       int err;
8347 +
8348 +       err = 0;
8349 +       if (udba == AuOpt_UDBA_REVAL
8350 +           && !au_test_fs_remote(h_dentry->d_sb)) {
8351 +               IMustLock(h_dir);
8352 +               err = (d_inode(h_dentry->d_parent) != h_dir);
8353 +       } else if (udba != AuOpt_UDBA_NONE)
8354 +               err = au_h_verify_dentry(h_dentry, h_parent, br);
8355 +
8356 +       return err;
8357 +}
8358 +
8359 +/* ---------------------------------------------------------------------- */
8360 +
8361 +static int au_do_refresh_hdentry(struct dentry *dentry, struct dentry *parent)
8362 +{
8363 +       int err;
8364 +       aufs_bindex_t new_bindex, bindex, bbot, bwh, bdiropq;
8365 +       struct au_hdentry tmp, *p, *q;
8366 +       struct au_dinfo *dinfo;
8367 +       struct super_block *sb;
8368 +
8369 +       DiMustWriteLock(dentry);
8370 +
8371 +       sb = dentry->d_sb;
8372 +       dinfo = au_di(dentry);
8373 +       bbot = dinfo->di_bbot;
8374 +       bwh = dinfo->di_bwh;
8375 +       bdiropq = dinfo->di_bdiropq;
8376 +       bindex = dinfo->di_btop;
8377 +       p = au_hdentry(dinfo, bindex);
8378 +       for (; bindex <= bbot; bindex++, p++) {
8379 +               if (!p->hd_dentry)
8380 +                       continue;
8381 +
8382 +               new_bindex = au_br_index(sb, p->hd_id);
8383 +               if (new_bindex == bindex)
8384 +                       continue;
8385 +
8386 +               if (dinfo->di_bwh == bindex)
8387 +                       bwh = new_bindex;
8388 +               if (dinfo->di_bdiropq == bindex)
8389 +                       bdiropq = new_bindex;
8390 +               if (new_bindex < 0) {
8391 +                       au_hdput(p);
8392 +                       p->hd_dentry = NULL;
8393 +                       continue;
8394 +               }
8395 +
8396 +               /* swap two lower dentries, and loop again */
8397 +               q = au_hdentry(dinfo, new_bindex);
8398 +               tmp = *q;
8399 +               *q = *p;
8400 +               *p = tmp;
8401 +               if (tmp.hd_dentry) {
8402 +                       bindex--;
8403 +                       p--;
8404 +               }
8405 +       }
8406 +
8407 +       dinfo->di_bwh = -1;
8408 +       if (bwh >= 0 && bwh <= au_sbbot(sb) && au_sbr_whable(sb, bwh))
8409 +               dinfo->di_bwh = bwh;
8410 +
8411 +       dinfo->di_bdiropq = -1;
8412 +       if (bdiropq >= 0
8413 +           && bdiropq <= au_sbbot(sb)
8414 +           && au_sbr_whable(sb, bdiropq))
8415 +               dinfo->di_bdiropq = bdiropq;
8416 +
8417 +       err = -EIO;
8418 +       dinfo->di_btop = -1;
8419 +       dinfo->di_bbot = -1;
8420 +       bbot = au_dbbot(parent);
8421 +       bindex = 0;
8422 +       p = au_hdentry(dinfo, bindex);
8423 +       for (; bindex <= bbot; bindex++, p++)
8424 +               if (p->hd_dentry) {
8425 +                       dinfo->di_btop = bindex;
8426 +                       break;
8427 +               }
8428 +
8429 +       if (dinfo->di_btop >= 0) {
8430 +               bindex = bbot;
8431 +               p = au_hdentry(dinfo, bindex);
8432 +               for (; bindex >= 0; bindex--, p--)
8433 +                       if (p->hd_dentry) {
8434 +                               dinfo->di_bbot = bindex;
8435 +                               err = 0;
8436 +                               break;
8437 +                       }
8438 +       }
8439 +
8440 +       return err;
8441 +}
8442 +
8443 +static void au_do_hide(struct dentry *dentry)
8444 +{
8445 +       struct inode *inode;
8446 +
8447 +       if (d_really_is_positive(dentry)) {
8448 +               inode = d_inode(dentry);
8449 +               if (!d_is_dir(dentry)) {
8450 +                       if (inode->i_nlink && !d_unhashed(dentry))
8451 +                               drop_nlink(inode);
8452 +               } else {
8453 +                       clear_nlink(inode);
8454 +                       /* stop next lookup */
8455 +                       inode->i_flags |= S_DEAD;
8456 +               }
8457 +               smp_mb(); /* necessary? */
8458 +       }
8459 +       d_drop(dentry);
8460 +}
8461 +
8462 +static int au_hide_children(struct dentry *parent)
8463 +{
8464 +       int err, i, j, ndentry;
8465 +       struct au_dcsub_pages dpages;
8466 +       struct au_dpage *dpage;
8467 +       struct dentry *dentry;
8468 +
8469 +       err = au_dpages_init(&dpages, GFP_NOFS);
8470 +       if (unlikely(err))
8471 +               goto out;
8472 +       err = au_dcsub_pages(&dpages, parent, NULL, NULL);
8473 +       if (unlikely(err))
8474 +               goto out_dpages;
8475 +
8476 +       /* in reverse order */
8477 +       for (i = dpages.ndpage - 1; i >= 0; i--) {
8478 +               dpage = dpages.dpages + i;
8479 +               ndentry = dpage->ndentry;
8480 +               for (j = ndentry - 1; j >= 0; j--) {
8481 +                       dentry = dpage->dentries[j];
8482 +                       if (dentry != parent)
8483 +                               au_do_hide(dentry);
8484 +               }
8485 +       }
8486 +
8487 +out_dpages:
8488 +       au_dpages_free(&dpages);
8489 +out:
8490 +       return err;
8491 +}
8492 +
8493 +static void au_hide(struct dentry *dentry)
8494 +{
8495 +       int err;
8496 +
8497 +       AuDbgDentry(dentry);
8498 +       if (d_is_dir(dentry)) {
8499 +               /* shrink_dcache_parent(dentry); */
8500 +               err = au_hide_children(dentry);
8501 +               if (unlikely(err))
8502 +                       AuIOErr("%pd, failed hiding children, ignored %d\n",
8503 +                               dentry, err);
8504 +       }
8505 +       au_do_hide(dentry);
8506 +}
8507 +
8508 +/*
8509 + * By adding a dirty branch, a cached dentry may be affected in various ways.
8510 + *
8511 + * a dirty branch is added
8512 + * - on the top of layers
8513 + * - in the middle of layers
8514 + * - to the bottom of layers
8515 + *
8516 + * on the added branch there exists
8517 + * - a whiteout
8518 + * - a diropq
8519 + * - a same named entry
8520 + *   + exist
8521 + *     * negative --> positive
8522 + *     * positive --> positive
8523 + *      - type is unchanged
8524 + *      - type is changed
8525 + *   + doesn't exist
8526 + *     * negative --> negative
8527 + *     * positive --> negative (rejected by au_br_del() for non-dir case)
8528 + * - none
8529 + */
8530 +static int au_refresh_by_dinfo(struct dentry *dentry, struct au_dinfo *dinfo,
8531 +                              struct au_dinfo *tmp)
8532 +{
8533 +       int err;
8534 +       aufs_bindex_t bindex, bbot;
8535 +       struct {
8536 +               struct dentry *dentry;
8537 +               struct inode *inode;
8538 +               mode_t mode;
8539 +       } orig_h, tmp_h = {
8540 +               .dentry = NULL
8541 +       };
8542 +       struct au_hdentry *hd;
8543 +       struct inode *inode, *h_inode;
8544 +       struct dentry *h_dentry;
8545 +
8546 +       err = 0;
8547 +       AuDebugOn(dinfo->di_btop < 0);
8548 +       orig_h.mode = 0;
8549 +       orig_h.dentry = au_hdentry(dinfo, dinfo->di_btop)->hd_dentry;
8550 +       orig_h.inode = NULL;
8551 +       if (d_is_positive(orig_h.dentry)) {
8552 +               orig_h.inode = d_inode(orig_h.dentry);
8553 +               orig_h.mode = orig_h.inode->i_mode & S_IFMT;
8554 +       }
8555 +       if (tmp->di_btop >= 0) {
8556 +               tmp_h.dentry = au_hdentry(tmp, tmp->di_btop)->hd_dentry;
8557 +               if (d_is_positive(tmp_h.dentry)) {
8558 +                       tmp_h.inode = d_inode(tmp_h.dentry);
8559 +                       tmp_h.mode = tmp_h.inode->i_mode & S_IFMT;
8560 +               }
8561 +       }
8562 +
8563 +       inode = NULL;
8564 +       if (d_really_is_positive(dentry))
8565 +               inode = d_inode(dentry);
8566 +       if (!orig_h.inode) {
8567 +               AuDbg("negative originally\n");
8568 +               if (inode) {
8569 +                       au_hide(dentry);
8570 +                       goto out;
8571 +               }
8572 +               AuDebugOn(inode);
8573 +               AuDebugOn(dinfo->di_btop != dinfo->di_bbot);
8574 +               AuDebugOn(dinfo->di_bdiropq != -1);
8575 +
8576 +               if (!tmp_h.inode) {
8577 +                       AuDbg("negative --> negative\n");
8578 +                       /* should have only one negative lower */
8579 +                       if (tmp->di_btop >= 0
8580 +                           && tmp->di_btop < dinfo->di_btop) {
8581 +                               AuDebugOn(tmp->di_btop != tmp->di_bbot);
8582 +                               AuDebugOn(dinfo->di_btop != dinfo->di_bbot);
8583 +                               au_set_h_dptr(dentry, dinfo->di_btop, NULL);
8584 +                               au_di_cp(dinfo, tmp);
8585 +                               hd = au_hdentry(tmp, tmp->di_btop);
8586 +                               au_set_h_dptr(dentry, tmp->di_btop,
8587 +                                             dget(hd->hd_dentry));
8588 +                       }
8589 +                       au_dbg_verify_dinode(dentry);
8590 +               } else {
8591 +                       AuDbg("negative --> positive\n");
8592 +                       /*
8593 +                        * similar to the behaviour of creating with bypassing
8594 +                        * aufs.
8595 +                        * unhash it in order to force an error in the
8596 +                        * succeeding create operation.
8597 +                        * we should not set S_DEAD here.
8598 +                        */
8599 +                       d_drop(dentry);
8600 +                       /* au_di_swap(tmp, dinfo); */
8601 +                       au_dbg_verify_dinode(dentry);
8602 +               }
8603 +       } else {
8604 +               AuDbg("positive originally\n");
8605 +               /* inode may be NULL */
8606 +               AuDebugOn(inode && (inode->i_mode & S_IFMT) != orig_h.mode);
8607 +               if (!tmp_h.inode) {
8608 +                       AuDbg("positive --> negative\n");
8609 +                       /* or bypassing aufs */
8610 +                       au_hide(dentry);
8611 +                       if (tmp->di_bwh >= 0 && tmp->di_bwh <= dinfo->di_btop)
8612 +                               dinfo->di_bwh = tmp->di_bwh;
8613 +                       if (inode)
8614 +                               err = au_refresh_hinode_self(inode);
8615 +                       au_dbg_verify_dinode(dentry);
8616 +               } else if (orig_h.mode == tmp_h.mode) {
8617 +                       AuDbg("positive --> positive, same type\n");
8618 +                       if (!S_ISDIR(orig_h.mode)
8619 +                           && dinfo->di_btop > tmp->di_btop) {
8620 +                               /*
8621 +                                * similar to the behaviour of removing and
8622 +                                * creating.
8623 +                                */
8624 +                               au_hide(dentry);
8625 +                               if (inode)
8626 +                                       err = au_refresh_hinode_self(inode);
8627 +                               au_dbg_verify_dinode(dentry);
8628 +                       } else {
8629 +                               /* fill empty slots */
8630 +                               if (dinfo->di_btop > tmp->di_btop)
8631 +                                       dinfo->di_btop = tmp->di_btop;
8632 +                               if (dinfo->di_bbot < tmp->di_bbot)
8633 +                                       dinfo->di_bbot = tmp->di_bbot;
8634 +                               dinfo->di_bwh = tmp->di_bwh;
8635 +                               dinfo->di_bdiropq = tmp->di_bdiropq;
8636 +                               bbot = dinfo->di_bbot;
8637 +                               bindex = tmp->di_btop;
8638 +                               hd = au_hdentry(tmp, bindex);
8639 +                               for (; bindex <= bbot; bindex++, hd++) {
8640 +                                       if (au_h_dptr(dentry, bindex))
8641 +                                               continue;
8642 +                                       h_dentry = hd->hd_dentry;
8643 +                                       if (!h_dentry)
8644 +                                               continue;
8645 +                                       AuDebugOn(d_is_negative(h_dentry));
8646 +                                       h_inode = d_inode(h_dentry);
8647 +                                       AuDebugOn(orig_h.mode
8648 +                                                 != (h_inode->i_mode
8649 +                                                     & S_IFMT));
8650 +                                       au_set_h_dptr(dentry, bindex,
8651 +                                                     dget(h_dentry));
8652 +                               }
8653 +                               if (inode)
8654 +                                       err = au_refresh_hinode(inode, dentry);
8655 +                               au_dbg_verify_dinode(dentry);
8656 +                       }
8657 +               } else {
8658 +                       AuDbg("positive --> positive, different type\n");
8659 +                       /* similar to the behaviour of removing and creating */
8660 +                       au_hide(dentry);
8661 +                       if (inode)
8662 +                               err = au_refresh_hinode_self(inode);
8663 +                       au_dbg_verify_dinode(dentry);
8664 +               }
8665 +       }
8666 +
8667 +out:
8668 +       return err;
8669 +}
8670 +
8671 +void au_refresh_dop(struct dentry *dentry, int force_reval)
8672 +{
8673 +       const struct dentry_operations *dop
8674 +               = force_reval ? &aufs_dop : dentry->d_sb->s_d_op;
8675 +       static const unsigned int mask
8676 +               = DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE;
8677 +
8678 +       BUILD_BUG_ON(sizeof(mask) != sizeof(dentry->d_flags));
8679 +
8680 +       if (dentry->d_op == dop)
8681 +               return;
8682 +
8683 +       AuDbg("%pd\n", dentry);
8684 +       spin_lock(&dentry->d_lock);
8685 +       if (dop == &aufs_dop)
8686 +               dentry->d_flags |= mask;
8687 +       else
8688 +               dentry->d_flags &= ~mask;
8689 +       dentry->d_op = dop;
8690 +       spin_unlock(&dentry->d_lock);
8691 +}
8692 +
8693 +int au_refresh_dentry(struct dentry *dentry, struct dentry *parent)
8694 +{
8695 +       int err, ebrange, nbr;
8696 +       unsigned int sigen;
8697 +       struct au_dinfo *dinfo, *tmp;
8698 +       struct super_block *sb;
8699 +       struct inode *inode;
8700 +
8701 +       DiMustWriteLock(dentry);
8702 +       AuDebugOn(IS_ROOT(dentry));
8703 +       AuDebugOn(d_really_is_negative(parent));
8704 +
8705 +       sb = dentry->d_sb;
8706 +       sigen = au_sigen(sb);
8707 +       err = au_digen_test(parent, sigen);
8708 +       if (unlikely(err))
8709 +               goto out;
8710 +
8711 +       nbr = au_sbbot(sb) + 1;
8712 +       dinfo = au_di(dentry);
8713 +       err = au_di_realloc(dinfo, nbr, /*may_shrink*/0);
8714 +       if (unlikely(err))
8715 +               goto out;
8716 +       ebrange = au_dbrange_test(dentry);
8717 +       if (!ebrange)
8718 +               ebrange = au_do_refresh_hdentry(dentry, parent);
8719 +
8720 +       if (d_unhashed(dentry) || ebrange /* || dinfo->di_tmpfile */) {
8721 +               AuDebugOn(au_dbtop(dentry) < 0 && au_dbbot(dentry) >= 0);
8722 +               if (d_really_is_positive(dentry)) {
8723 +                       inode = d_inode(dentry);
8724 +                       err = au_refresh_hinode_self(inode);
8725 +               }
8726 +               au_dbg_verify_dinode(dentry);
8727 +               if (!err)
8728 +                       goto out_dgen; /* success */
8729 +               goto out;
8730 +       }
8731 +
8732 +       /* temporary dinfo */
8733 +       AuDbgDentry(dentry);
8734 +       err = -ENOMEM;
8735 +       tmp = au_di_alloc(sb, AuLsc_DI_TMP);
8736 +       if (unlikely(!tmp))
8737 +               goto out;
8738 +       au_di_swap(tmp, dinfo);
8739 +       /* returns the number of positive dentries */
8740 +       /*
8741 +        * if current working dir is removed, it returns an error.
8742 +        * but the dentry is legal.
8743 +        */
8744 +       err = au_lkup_dentry(dentry, /*btop*/0, AuLkup_ALLOW_NEG);
8745 +       AuDbgDentry(dentry);
8746 +       au_di_swap(tmp, dinfo);
8747 +       if (err == -ENOENT)
8748 +               err = 0;
8749 +       if (err >= 0) {
8750 +               /* compare/refresh by dinfo */
8751 +               AuDbgDentry(dentry);
8752 +               err = au_refresh_by_dinfo(dentry, dinfo, tmp);
8753 +               au_dbg_verify_dinode(dentry);
8754 +               AuTraceErr(err);
8755 +       }
8756 +       au_di_realloc(dinfo, nbr, /*may_shrink*/1); /* harmless if err */
8757 +       au_rw_write_unlock(&tmp->di_rwsem);
8758 +       au_di_free(tmp);
8759 +       if (unlikely(err))
8760 +               goto out;
8761 +
8762 +out_dgen:
8763 +       au_update_digen(dentry);
8764 +out:
8765 +       if (unlikely(err && !(dentry->d_flags & DCACHE_NFSFS_RENAMED))) {
8766 +               AuIOErr("failed refreshing %pd, %d\n", dentry, err);
8767 +               AuDbgDentry(dentry);
8768 +       }
8769 +       AuTraceErr(err);
8770 +       return err;
8771 +}
8772 +
8773 +static int au_do_h_d_reval(struct dentry *h_dentry, unsigned int flags,
8774 +                          struct dentry *dentry, aufs_bindex_t bindex)
8775 +{
8776 +       int err, valid;
8777 +
8778 +       err = 0;
8779 +       if (!(h_dentry->d_flags & DCACHE_OP_REVALIDATE))
8780 +               goto out;
8781 +
8782 +       AuDbg("b%d\n", bindex);
8783 +       /*
8784 +        * gave up supporting LOOKUP_CREATE/OPEN for lower fs,
8785 +        * due to whiteout and branch permission.
8786 +        */
8787 +       flags &= ~(/*LOOKUP_PARENT |*/ LOOKUP_OPEN | LOOKUP_CREATE
8788 +                  | LOOKUP_FOLLOW | LOOKUP_EXCL);
8789 +       /* it may return tri-state */
8790 +       valid = h_dentry->d_op->d_revalidate(h_dentry, flags);
8791 +
8792 +       if (unlikely(valid < 0))
8793 +               err = valid;
8794 +       else if (!valid)
8795 +               err = -EINVAL;
8796 +
8797 +out:
8798 +       AuTraceErr(err);
8799 +       return err;
8800 +}
8801 +
8802 +/* todo: remove this */
8803 +static int h_d_revalidate(struct dentry *dentry, struct inode *inode,
8804 +                         unsigned int flags, int do_udba, int dirren)
8805 +{
8806 +       int err;
8807 +       umode_t mode, h_mode;
8808 +       aufs_bindex_t bindex, btail, btop, ibs, ibe;
8809 +       unsigned char plus, unhashed, is_root, h_plus, h_nfs, tmpfile;
8810 +       struct inode *h_inode, *h_cached_inode;
8811 +       struct dentry *h_dentry;
8812 +       struct qstr *name, *h_name;
8813 +
8814 +       err = 0;
8815 +       plus = 0;
8816 +       mode = 0;
8817 +       ibs = -1;
8818 +       ibe = -1;
8819 +       unhashed = !!d_unhashed(dentry);
8820 +       is_root = !!IS_ROOT(dentry);
8821 +       name = &dentry->d_name;
8822 +       tmpfile = au_di(dentry)->di_tmpfile;
8823 +
8824 +       /*
8825 +        * Theoretically, REVAL test should be unnecessary in case of
8826 +        * {FS,I}NOTIFY.
8827 +        * But {fs,i}notify doesn't fire some necessary events,
8828 +        *      IN_ATTRIB for atime/nlink/pageio
8829 +        * Let's do REVAL test too.
8830 +        */
8831 +       if (do_udba && inode) {
8832 +               mode = (inode->i_mode & S_IFMT);
8833 +               plus = (inode->i_nlink > 0);
8834 +               ibs = au_ibtop(inode);
8835 +               ibe = au_ibbot(inode);
8836 +       }
8837 +
8838 +       btop = au_dbtop(dentry);
8839 +       btail = btop;
8840 +       if (inode && S_ISDIR(inode->i_mode))
8841 +               btail = au_dbtaildir(dentry);
8842 +       for (bindex = btop; bindex <= btail; bindex++) {
8843 +               h_dentry = au_h_dptr(dentry, bindex);
8844 +               if (!h_dentry)
8845 +                       continue;
8846 +
8847 +               AuDbg("b%d, %pd\n", bindex, h_dentry);
8848 +               h_nfs = !!au_test_nfs(h_dentry->d_sb);
8849 +               spin_lock(&h_dentry->d_lock);
8850 +               h_name = &h_dentry->d_name;
8851 +               if (unlikely(do_udba
8852 +                            && !is_root
8853 +                            && ((!h_nfs
8854 +                                 && (unhashed != !!d_unhashed(h_dentry)
8855 +                                     || (!tmpfile && !dirren
8856 +                                         && !au_qstreq(name, h_name))
8857 +                                         ))
8858 +                                || (h_nfs
8859 +                                    && !(flags & LOOKUP_OPEN)
8860 +                                    && (h_dentry->d_flags
8861 +                                        & DCACHE_NFSFS_RENAMED)))
8862 +                           )) {
8863 +                       int h_unhashed;
8864 +
8865 +                       h_unhashed = d_unhashed(h_dentry);
8866 +                       spin_unlock(&h_dentry->d_lock);
8867 +                       AuDbg("unhash 0x%x 0x%x, %pd %pd\n",
8868 +                             unhashed, h_unhashed, dentry, h_dentry);
8869 +                       goto err;
8870 +               }
8871 +               spin_unlock(&h_dentry->d_lock);
8872 +
8873 +               err = au_do_h_d_reval(h_dentry, flags, dentry, bindex);
8874 +               if (unlikely(err))
8875 +                       /* do not goto err, to keep the errno */
8876 +                       break;
8877 +
8878 +               /* todo: plink too? */
8879 +               if (!do_udba)
8880 +                       continue;
8881 +
8882 +               /* UDBA tests */
8883 +               if (unlikely(!!inode != d_is_positive(h_dentry)))
8884 +                       goto err;
8885 +
8886 +               h_inode = NULL;
8887 +               if (d_is_positive(h_dentry))
8888 +                       h_inode = d_inode(h_dentry);
8889 +               h_plus = plus;
8890 +               h_mode = mode;
8891 +               h_cached_inode = h_inode;
8892 +               if (h_inode) {
8893 +                       h_mode = (h_inode->i_mode & S_IFMT);
8894 +                       h_plus = (h_inode->i_nlink > 0);
8895 +               }
8896 +               if (inode && ibs <= bindex && bindex <= ibe)
8897 +                       h_cached_inode = au_h_iptr(inode, bindex);
8898 +
8899 +               if (!h_nfs) {
8900 +                       if (unlikely(plus != h_plus && !tmpfile))
8901 +                               goto err;
8902 +               } else {
8903 +                       if (unlikely(!(h_dentry->d_flags & DCACHE_NFSFS_RENAMED)
8904 +                                    && !is_root
8905 +                                    && !IS_ROOT(h_dentry)
8906 +                                    && unhashed != d_unhashed(h_dentry)))
8907 +                               goto err;
8908 +               }
8909 +               if (unlikely(mode != h_mode
8910 +                            || h_cached_inode != h_inode))
8911 +                       goto err;
8912 +               continue;
8913 +
8914 +err:
8915 +               err = -EINVAL;
8916 +               break;
8917 +       }
8918 +
8919 +       AuTraceErr(err);
8920 +       return err;
8921 +}
8922 +
8923 +/* todo: consolidate with do_refresh() and au_reval_for_attr() */
8924 +static int simple_reval_dpath(struct dentry *dentry, unsigned int sigen)
8925 +{
8926 +       int err;
8927 +       struct dentry *parent;
8928 +
8929 +       if (!au_digen_test(dentry, sigen))
8930 +               return 0;
8931 +
8932 +       parent = dget_parent(dentry);
8933 +       di_read_lock_parent(parent, AuLock_IR);
8934 +       AuDebugOn(au_digen_test(parent, sigen));
8935 +       au_dbg_verify_gen(parent, sigen);
8936 +       err = au_refresh_dentry(dentry, parent);
8937 +       di_read_unlock(parent, AuLock_IR);
8938 +       dput(parent);
8939 +       AuTraceErr(err);
8940 +       return err;
8941 +}
8942 +
8943 +int au_reval_dpath(struct dentry *dentry, unsigned int sigen)
8944 +{
8945 +       int err;
8946 +       struct dentry *d, *parent;
8947 +
8948 +       if (!au_ftest_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR))
8949 +               return simple_reval_dpath(dentry, sigen);
8950 +
8951 +       /* slow loop, keep it simple and stupid */
8952 +       /* cf: au_cpup_dirs() */
8953 +       err = 0;
8954 +       parent = NULL;
8955 +       while (au_digen_test(dentry, sigen)) {
8956 +               d = dentry;
8957 +               while (1) {
8958 +                       dput(parent);
8959 +                       parent = dget_parent(d);
8960 +                       if (!au_digen_test(parent, sigen))
8961 +                               break;
8962 +                       d = parent;
8963 +               }
8964 +
8965 +               if (d != dentry)
8966 +                       di_write_lock_child2(d);
8967 +
8968 +               /* someone might update our dentry while we were sleeping */
8969 +               if (au_digen_test(d, sigen)) {
8970 +                       /*
8971 +                        * todo: consolidate with simple_reval_dpath(),
8972 +                        * do_refresh() and au_reval_for_attr().
8973 +                        */
8974 +                       di_read_lock_parent(parent, AuLock_IR);
8975 +                       err = au_refresh_dentry(d, parent);
8976 +                       di_read_unlock(parent, AuLock_IR);
8977 +               }
8978 +
8979 +               if (d != dentry)
8980 +                       di_write_unlock(d);
8981 +               dput(parent);
8982 +               if (unlikely(err))
8983 +                       break;
8984 +       }
8985 +
8986 +       return err;
8987 +}
8988 +
8989 +/*
8990 + * if valid returns 1, otherwise 0.
8991 + */
8992 +static int aufs_d_revalidate(struct dentry *dentry, unsigned int flags)
8993 +{
8994 +       int valid, err;
8995 +       unsigned int sigen;
8996 +       unsigned char do_udba, dirren;
8997 +       struct super_block *sb;
8998 +       struct inode *inode;
8999 +
9000 +       /* todo: support rcu-walk? */
9001 +       if (flags & LOOKUP_RCU)
9002 +               return -ECHILD;
9003 +
9004 +       valid = 0;
9005 +       if (unlikely(!au_di(dentry)))
9006 +               goto out;
9007 +
9008 +       valid = 1;
9009 +       sb = dentry->d_sb;
9010 +       /*
9011 +        * todo: very ugly
9012 +        * i_mutex of parent dir may be held,
9013 +        * but we should not return 'invalid' due to busy.
9014 +        */
9015 +       err = aufs_read_lock(dentry, AuLock_FLUSH | AuLock_DW | AuLock_NOPLM);
9016 +       if (unlikely(err)) {
9017 +               valid = err;
9018 +               AuTraceErr(err);
9019 +               goto out;
9020 +       }
9021 +       inode = NULL;
9022 +       if (d_really_is_positive(dentry))
9023 +               inode = d_inode(dentry);
9024 +       if (unlikely(inode && au_is_bad_inode(inode))) {
9025 +               err = -EINVAL;
9026 +               AuTraceErr(err);
9027 +               goto out_dgrade;
9028 +       }
9029 +       if (unlikely(au_dbrange_test(dentry))) {
9030 +               err = -EINVAL;
9031 +               AuTraceErr(err);
9032 +               goto out_dgrade;
9033 +       }
9034 +
9035 +       sigen = au_sigen(sb);
9036 +       if (au_digen_test(dentry, sigen)) {
9037 +               AuDebugOn(IS_ROOT(dentry));
9038 +               err = au_reval_dpath(dentry, sigen);
9039 +               if (unlikely(err)) {
9040 +                       AuTraceErr(err);
9041 +                       goto out_dgrade;
9042 +               }
9043 +       }
9044 +       di_downgrade_lock(dentry, AuLock_IR);
9045 +
9046 +       err = -EINVAL;
9047 +       if (!(flags & (LOOKUP_OPEN | LOOKUP_EMPTY))
9048 +           && inode
9049 +           && !(inode->i_state && I_LINKABLE)
9050 +           && (IS_DEADDIR(inode) || !inode->i_nlink)) {
9051 +               AuTraceErr(err);
9052 +               goto out_inval;
9053 +       }
9054 +
9055 +       do_udba = !au_opt_test(au_mntflags(sb), UDBA_NONE);
9056 +       if (do_udba && inode) {
9057 +               aufs_bindex_t btop = au_ibtop(inode);
9058 +               struct inode *h_inode;
9059 +
9060 +               if (btop >= 0) {
9061 +                       h_inode = au_h_iptr(inode, btop);
9062 +                       if (h_inode && au_test_higen(inode, h_inode)) {
9063 +                               AuTraceErr(err);
9064 +                               goto out_inval;
9065 +                       }
9066 +               }
9067 +       }
9068 +
9069 +       dirren = !!au_opt_test(au_mntflags(sb), DIRREN);
9070 +       err = h_d_revalidate(dentry, inode, flags, do_udba, dirren);
9071 +       if (unlikely(!err && do_udba && au_dbtop(dentry) < 0)) {
9072 +               err = -EIO;
9073 +               AuDbg("both of real entry and whiteout found, %p, err %d\n",
9074 +                     dentry, err);
9075 +       }
9076 +       goto out_inval;
9077 +
9078 +out_dgrade:
9079 +       di_downgrade_lock(dentry, AuLock_IR);
9080 +out_inval:
9081 +       aufs_read_unlock(dentry, AuLock_IR);
9082 +       AuTraceErr(err);
9083 +       valid = !err;
9084 +out:
9085 +       if (!valid) {
9086 +               AuDbg("%pd invalid, %d\n", dentry, valid);
9087 +               d_drop(dentry);
9088 +       }
9089 +       return valid;
9090 +}
9091 +
9092 +static void aufs_d_release(struct dentry *dentry)
9093 +{
9094 +       if (au_di(dentry)) {
9095 +               au_di_fin(dentry);
9096 +               au_hn_di_reinit(dentry);
9097 +       }
9098 +}
9099 +
9100 +const struct dentry_operations aufs_dop = {
9101 +       .d_revalidate           = aufs_d_revalidate,
9102 +       .d_weak_revalidate      = aufs_d_revalidate,
9103 +       .d_release              = aufs_d_release
9104 +};
9105 +
9106 +/* aufs_dop without d_revalidate */
9107 +const struct dentry_operations aufs_dop_noreval = {
9108 +       .d_release              = aufs_d_release
9109 +};
9110 diff -urN /usr/share/empty/fs/aufs/dentry.h linux/fs/aufs/dentry.h
9111 --- /usr/share/empty/fs/aufs/dentry.h   1970-01-01 01:00:00.000000000 +0100
9112 +++ linux/fs/aufs/dentry.h      2022-03-21 14:49:05.719966344 +0100
9113 @@ -0,0 +1,269 @@
9114 +/* SPDX-License-Identifier: GPL-2.0 */
9115 +/*
9116 + * Copyright (C) 2005-2021 Junjiro R. Okajima
9117 + *
9118 + * This program, aufs is free software; you can redistribute it and/or modify
9119 + * it under the terms of the GNU General Public License as published by
9120 + * the Free Software Foundation; either version 2 of the License, or
9121 + * (at your option) any later version.
9122 + *
9123 + * This program is distributed in the hope that it will be useful,
9124 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
9125 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9126 + * GNU General Public License for more details.
9127 + *
9128 + * You should have received a copy of the GNU General Public License
9129 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
9130 + */
9131 +
9132 +/*
9133 + * lookup and dentry operations
9134 + */
9135 +
9136 +#ifndef __AUFS_DENTRY_H__
9137 +#define __AUFS_DENTRY_H__
9138 +
9139 +#ifdef __KERNEL__
9140 +
9141 +#include <linux/dcache.h>
9142 +#include "dirren.h"
9143 +#include "rwsem.h"
9144 +
9145 +struct au_hdentry {
9146 +       struct dentry           *hd_dentry;
9147 +       aufs_bindex_t           hd_id;
9148 +};
9149 +
9150 +struct au_dinfo {
9151 +       atomic_t                di_generation;
9152 +
9153 +       struct au_rwsem         di_rwsem;
9154 +       aufs_bindex_t           di_btop, di_bbot, di_bwh, di_bdiropq;
9155 +       unsigned char           di_tmpfile; /* to allow the different name */
9156 +       struct au_hdentry       *di_hdentry;
9157 +       struct rcu_head         rcu;
9158 +} ____cacheline_aligned_in_smp;
9159 +
9160 +/* ---------------------------------------------------------------------- */
9161 +
9162 +/* flags for au_lkup_dentry() */
9163 +#define AuLkup_ALLOW_NEG       1
9164 +#define AuLkup_IGNORE_PERM     (1 << 1)
9165 +#define AuLkup_DIRREN          (1 << 2)
9166 +#define au_ftest_lkup(flags, name)     ((flags) & AuLkup_##name)
9167 +#define au_fset_lkup(flags, name) \
9168 +       do { (flags) |= AuLkup_##name; } while (0)
9169 +#define au_fclr_lkup(flags, name) \
9170 +       do { (flags) &= ~AuLkup_##name; } while (0)
9171 +
9172 +#ifndef CONFIG_AUFS_DIRREN
9173 +#undef AuLkup_DIRREN
9174 +#define AuLkup_DIRREN 0
9175 +#endif
9176 +
9177 +struct au_do_lookup_args {
9178 +       unsigned int            flags;
9179 +       mode_t                  type;
9180 +       struct qstr             whname, *name;
9181 +       struct au_dr_lookup     dirren;
9182 +};
9183 +
9184 +/* ---------------------------------------------------------------------- */
9185 +
9186 +/* dentry.c */
9187 +extern const struct dentry_operations aufs_dop, aufs_dop_noreval;
9188 +struct au_branch;
9189 +struct dentry *au_sio_lkup_one(struct user_namespace *userns, struct qstr *name,
9190 +                              struct path *ppath);
9191 +int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
9192 +               struct dentry *h_parent, struct au_branch *br);
9193 +
9194 +int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t btop,
9195 +                  unsigned int flags);
9196 +int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh);
9197 +int au_refresh_dentry(struct dentry *dentry, struct dentry *parent);
9198 +int au_reval_dpath(struct dentry *dentry, unsigned int sigen);
9199 +void au_refresh_dop(struct dentry *dentry, int force_reval);
9200 +
9201 +/* dinfo.c */
9202 +void au_di_init_once(void *_di);
9203 +struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc);
9204 +void au_di_free(struct au_dinfo *dinfo);
9205 +void au_di_swap(struct au_dinfo *a, struct au_dinfo *b);
9206 +void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src);
9207 +int au_di_init(struct dentry *dentry);
9208 +void au_di_fin(struct dentry *dentry);
9209 +int au_di_realloc(struct au_dinfo *dinfo, int nbr, int may_shrink);
9210 +
9211 +void di_read_lock(struct dentry *d, int flags, unsigned int lsc);
9212 +void di_read_unlock(struct dentry *d, int flags);
9213 +void di_downgrade_lock(struct dentry *d, int flags);
9214 +void di_write_lock(struct dentry *d, unsigned int lsc);
9215 +void di_write_unlock(struct dentry *d);
9216 +void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir);
9217 +void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir);
9218 +void di_write_unlock2(struct dentry *d1, struct dentry *d2);
9219 +
9220 +struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex);
9221 +struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex);
9222 +aufs_bindex_t au_dbtail(struct dentry *dentry);
9223 +aufs_bindex_t au_dbtaildir(struct dentry *dentry);
9224 +
9225 +void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
9226 +                  struct dentry *h_dentry);
9227 +int au_digen_test(struct dentry *dentry, unsigned int sigen);
9228 +int au_dbrange_test(struct dentry *dentry);
9229 +void au_update_digen(struct dentry *dentry);
9230 +void au_update_dbrange(struct dentry *dentry, int do_put_zero);
9231 +void au_update_dbtop(struct dentry *dentry);
9232 +void au_update_dbbot(struct dentry *dentry);
9233 +int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry);
9234 +
9235 +/* ---------------------------------------------------------------------- */
9236 +
9237 +static inline struct au_dinfo *au_di(struct dentry *dentry)
9238 +{
9239 +       return dentry->d_fsdata;
9240 +}
9241 +
9242 +/* ---------------------------------------------------------------------- */
9243 +
9244 +/* lock subclass for dinfo */
9245 +enum {
9246 +       AuLsc_DI_CHILD,         /* child first */
9247 +       AuLsc_DI_CHILD2,        /* rename(2), link(2), and cpup at hnotify */
9248 +       AuLsc_DI_CHILD3,        /* copyup dirs */
9249 +       AuLsc_DI_PARENT,
9250 +       AuLsc_DI_PARENT2,
9251 +       AuLsc_DI_PARENT3,
9252 +       AuLsc_DI_TMP            /* temp for replacing dinfo */
9253 +};
9254 +
9255 +/*
9256 + * di_read_lock_child, di_write_lock_child,
9257 + * di_read_lock_child2, di_write_lock_child2,
9258 + * di_read_lock_child3, di_write_lock_child3,
9259 + * di_read_lock_parent, di_write_lock_parent,
9260 + * di_read_lock_parent2, di_write_lock_parent2,
9261 + * di_read_lock_parent3, di_write_lock_parent3,
9262 + */
9263 +#define AuReadLockFunc(name, lsc) \
9264 +static inline void di_read_lock_##name(struct dentry *d, int flags) \
9265 +{ di_read_lock(d, flags, AuLsc_DI_##lsc); }
9266 +
9267 +#define AuWriteLockFunc(name, lsc) \
9268 +static inline void di_write_lock_##name(struct dentry *d) \
9269 +{ di_write_lock(d, AuLsc_DI_##lsc); }
9270 +
9271 +#define AuRWLockFuncs(name, lsc) \
9272 +       AuReadLockFunc(name, lsc) \
9273 +       AuWriteLockFunc(name, lsc)
9274 +
9275 +AuRWLockFuncs(child, CHILD);
9276 +AuRWLockFuncs(child2, CHILD2);
9277 +AuRWLockFuncs(child3, CHILD3);
9278 +AuRWLockFuncs(parent, PARENT);
9279 +AuRWLockFuncs(parent2, PARENT2);
9280 +AuRWLockFuncs(parent3, PARENT3);
9281 +
9282 +#undef AuReadLockFunc
9283 +#undef AuWriteLockFunc
9284 +#undef AuRWLockFuncs
9285 +
9286 +#define DiMustNoWaiters(d)     AuRwMustNoWaiters(&au_di(d)->di_rwsem)
9287 +#define DiMustAnyLock(d)       AuRwMustAnyLock(&au_di(d)->di_rwsem)
9288 +#define DiMustWriteLock(d)     AuRwMustWriteLock(&au_di(d)->di_rwsem)
9289 +
9290 +/* ---------------------------------------------------------------------- */
9291 +
9292 +/* todo: memory barrier? */
9293 +static inline unsigned int au_digen(struct dentry *d)
9294 +{
9295 +       return atomic_read(&au_di(d)->di_generation);
9296 +}
9297 +
9298 +static inline void au_h_dentry_init(struct au_hdentry *hdentry)
9299 +{
9300 +       hdentry->hd_dentry = NULL;
9301 +}
9302 +
9303 +static inline struct au_hdentry *au_hdentry(struct au_dinfo *di,
9304 +                                           aufs_bindex_t bindex)
9305 +{
9306 +       return di->di_hdentry + bindex;
9307 +}
9308 +
9309 +static inline void au_hdput(struct au_hdentry *hd)
9310 +{
9311 +       if (hd)
9312 +               dput(hd->hd_dentry);
9313 +}
9314 +
9315 +static inline aufs_bindex_t au_dbtop(struct dentry *dentry)
9316 +{
9317 +       DiMustAnyLock(dentry);
9318 +       return au_di(dentry)->di_btop;
9319 +}
9320 +
9321 +static inline aufs_bindex_t au_dbbot(struct dentry *dentry)
9322 +{
9323 +       DiMustAnyLock(dentry);
9324 +       return au_di(dentry)->di_bbot;
9325 +}
9326 +
9327 +static inline aufs_bindex_t au_dbwh(struct dentry *dentry)
9328 +{
9329 +       DiMustAnyLock(dentry);
9330 +       return au_di(dentry)->di_bwh;
9331 +}
9332 +
9333 +static inline aufs_bindex_t au_dbdiropq(struct dentry *dentry)
9334 +{
9335 +       DiMustAnyLock(dentry);
9336 +       return au_di(dentry)->di_bdiropq;
9337 +}
9338 +
9339 +/* todo: hard/soft set? */
9340 +static inline void au_set_dbtop(struct dentry *dentry, aufs_bindex_t bindex)
9341 +{
9342 +       DiMustWriteLock(dentry);
9343 +       au_di(dentry)->di_btop = bindex;
9344 +}
9345 +
9346 +static inline void au_set_dbbot(struct dentry *dentry, aufs_bindex_t bindex)
9347 +{
9348 +       DiMustWriteLock(dentry);
9349 +       au_di(dentry)->di_bbot = bindex;
9350 +}
9351 +
9352 +static inline void au_set_dbwh(struct dentry *dentry, aufs_bindex_t bindex)
9353 +{
9354 +       DiMustWriteLock(dentry);
9355 +       /* dbwh can be outside of btop - bbot range */
9356 +       au_di(dentry)->di_bwh = bindex;
9357 +}
9358 +
9359 +static inline void au_set_dbdiropq(struct dentry *dentry, aufs_bindex_t bindex)
9360 +{
9361 +       DiMustWriteLock(dentry);
9362 +       au_di(dentry)->di_bdiropq = bindex;
9363 +}
9364 +
9365 +/* ---------------------------------------------------------------------- */
9366 +
9367 +#ifdef CONFIG_AUFS_HNOTIFY
9368 +static inline void au_digen_dec(struct dentry *d)
9369 +{
9370 +       atomic_dec(&au_di(d)->di_generation);
9371 +}
9372 +
9373 +static inline void au_hn_di_reinit(struct dentry *dentry)
9374 +{
9375 +       dentry->d_fsdata = NULL;
9376 +}
9377 +#else
9378 +AuStubVoid(au_hn_di_reinit, struct dentry *dentry __maybe_unused)
9379 +#endif /* CONFIG_AUFS_HNOTIFY */
9380 +
9381 +#endif /* __KERNEL__ */
9382 +#endif /* __AUFS_DENTRY_H__ */
9383 diff -urN /usr/share/empty/fs/aufs/dinfo.c linux/fs/aufs/dinfo.c
9384 --- /usr/share/empty/fs/aufs/dinfo.c    1970-01-01 01:00:00.000000000 +0100
9385 +++ linux/fs/aufs/dinfo.c       2022-03-21 14:49:05.719966344 +0100
9386 @@ -0,0 +1,554 @@
9387 +// SPDX-License-Identifier: GPL-2.0
9388 +/*
9389 + * Copyright (C) 2005-2021 Junjiro R. Okajima
9390 + *
9391 + * This program, aufs is free software; you can redistribute it and/or modify
9392 + * it under the terms of the GNU General Public License as published by
9393 + * the Free Software Foundation; either version 2 of the License, or
9394 + * (at your option) any later version.
9395 + *
9396 + * This program is distributed in the hope that it will be useful,
9397 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
9398 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9399 + * GNU General Public License for more details.
9400 + *
9401 + * You should have received a copy of the GNU General Public License
9402 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
9403 + */
9404 +
9405 +/*
9406 + * dentry private data
9407 + */
9408 +
9409 +#include "aufs.h"
9410 +
9411 +void au_di_init_once(void *_dinfo)
9412 +{
9413 +       struct au_dinfo *dinfo = _dinfo;
9414 +
9415 +       au_rw_init(&dinfo->di_rwsem);
9416 +}
9417 +
9418 +struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc)
9419 +{
9420 +       struct au_dinfo *dinfo;
9421 +       int nbr, i;
9422 +
9423 +       dinfo = au_cache_alloc_dinfo();
9424 +       if (unlikely(!dinfo))
9425 +               goto out;
9426 +
9427 +       nbr = au_sbbot(sb) + 1;
9428 +       if (nbr <= 0)
9429 +               nbr = 1;
9430 +       dinfo->di_hdentry = kcalloc(nbr, sizeof(*dinfo->di_hdentry), GFP_NOFS);
9431 +       if (dinfo->di_hdentry) {
9432 +               au_rw_write_lock_nested(&dinfo->di_rwsem, lsc);
9433 +               dinfo->di_btop = -1;
9434 +               dinfo->di_bbot = -1;
9435 +               dinfo->di_bwh = -1;
9436 +               dinfo->di_bdiropq = -1;
9437 +               dinfo->di_tmpfile = 0;
9438 +               for (i = 0; i < nbr; i++)
9439 +                       dinfo->di_hdentry[i].hd_id = -1;
9440 +               goto out;
9441 +       }
9442 +
9443 +       au_cache_free_dinfo(dinfo);
9444 +       dinfo = NULL;
9445 +
9446 +out:
9447 +       return dinfo;
9448 +}
9449 +
9450 +void au_di_free(struct au_dinfo *dinfo)
9451 +{
9452 +       struct au_hdentry *p;
9453 +       aufs_bindex_t bbot, bindex;
9454 +
9455 +       /* dentry may not be revalidated */
9456 +       bindex = dinfo->di_btop;
9457 +       if (bindex >= 0) {
9458 +               bbot = dinfo->di_bbot;
9459 +               p = au_hdentry(dinfo, bindex);
9460 +               while (bindex++ <= bbot)
9461 +                       au_hdput(p++);
9462 +       }
9463 +       au_kfree_try_rcu(dinfo->di_hdentry);
9464 +       au_cache_free_dinfo(dinfo);
9465 +}
9466 +
9467 +void au_di_swap(struct au_dinfo *a, struct au_dinfo *b)
9468 +{
9469 +       struct au_hdentry *p;
9470 +       aufs_bindex_t bi;
9471 +
9472 +       AuRwMustWriteLock(&a->di_rwsem);
9473 +       AuRwMustWriteLock(&b->di_rwsem);
9474 +
9475 +#define DiSwap(v, name)                                \
9476 +       do {                                    \
9477 +               v = a->di_##name;               \
9478 +               a->di_##name = b->di_##name;    \
9479 +               b->di_##name = v;               \
9480 +       } while (0)
9481 +
9482 +       DiSwap(p, hdentry);
9483 +       DiSwap(bi, btop);
9484 +       DiSwap(bi, bbot);
9485 +       DiSwap(bi, bwh);
9486 +       DiSwap(bi, bdiropq);
9487 +       /* smp_mb(); */
9488 +
9489 +#undef DiSwap
9490 +}
9491 +
9492 +void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src)
9493 +{
9494 +       AuRwMustWriteLock(&dst->di_rwsem);
9495 +       AuRwMustWriteLock(&src->di_rwsem);
9496 +
9497 +       dst->di_btop = src->di_btop;
9498 +       dst->di_bbot = src->di_bbot;
9499 +       dst->di_bwh = src->di_bwh;
9500 +       dst->di_bdiropq = src->di_bdiropq;
9501 +       /* smp_mb(); */
9502 +}
9503 +
9504 +int au_di_init(struct dentry *dentry)
9505 +{
9506 +       int err;
9507 +       struct super_block *sb;
9508 +       struct au_dinfo *dinfo;
9509 +
9510 +       err = 0;
9511 +       sb = dentry->d_sb;
9512 +       dinfo = au_di_alloc(sb, AuLsc_DI_CHILD);
9513 +       if (dinfo) {
9514 +               atomic_set(&dinfo->di_generation, au_sigen(sb));
9515 +               /* smp_mb(); */ /* atomic_set */
9516 +               dentry->d_fsdata = dinfo;
9517 +       } else
9518 +               err = -ENOMEM;
9519 +
9520 +       return err;
9521 +}
9522 +
9523 +void au_di_fin(struct dentry *dentry)
9524 +{
9525 +       struct au_dinfo *dinfo;
9526 +
9527 +       dinfo = au_di(dentry);
9528 +       AuRwDestroy(&dinfo->di_rwsem);
9529 +       au_di_free(dinfo);
9530 +}
9531 +
9532 +int au_di_realloc(struct au_dinfo *dinfo, int nbr, int may_shrink)
9533 +{
9534 +       int err, sz;
9535 +       struct au_hdentry *hdp;
9536 +
9537 +       AuRwMustWriteLock(&dinfo->di_rwsem);
9538 +
9539 +       err = -ENOMEM;
9540 +       sz = sizeof(*hdp) * (dinfo->di_bbot + 1);
9541 +       if (!sz)
9542 +               sz = sizeof(*hdp);
9543 +       hdp = au_kzrealloc(dinfo->di_hdentry, sz, sizeof(*hdp) * nbr, GFP_NOFS,
9544 +                          may_shrink);
9545 +       if (hdp) {
9546 +               dinfo->di_hdentry = hdp;
9547 +               err = 0;
9548 +       }
9549 +
9550 +       return err;
9551 +}
9552 +
9553 +/* ---------------------------------------------------------------------- */
9554 +
9555 +static void do_ii_write_lock(struct inode *inode, unsigned int lsc)
9556 +{
9557 +       switch (lsc) {
9558 +       case AuLsc_DI_CHILD:
9559 +               ii_write_lock_child(inode);
9560 +               break;
9561 +       case AuLsc_DI_CHILD2:
9562 +               ii_write_lock_child2(inode);
9563 +               break;
9564 +       case AuLsc_DI_CHILD3:
9565 +               ii_write_lock_child3(inode);
9566 +               break;
9567 +       case AuLsc_DI_PARENT:
9568 +               ii_write_lock_parent(inode);
9569 +               break;
9570 +       case AuLsc_DI_PARENT2:
9571 +               ii_write_lock_parent2(inode);
9572 +               break;
9573 +       case AuLsc_DI_PARENT3:
9574 +               ii_write_lock_parent3(inode);
9575 +               break;
9576 +       default:
9577 +               BUG();
9578 +       }
9579 +}
9580 +
9581 +static void do_ii_read_lock(struct inode *inode, unsigned int lsc)
9582 +{
9583 +       switch (lsc) {
9584 +       case AuLsc_DI_CHILD:
9585 +               ii_read_lock_child(inode);
9586 +               break;
9587 +       case AuLsc_DI_CHILD2:
9588 +               ii_read_lock_child2(inode);
9589 +               break;
9590 +       case AuLsc_DI_CHILD3:
9591 +               ii_read_lock_child3(inode);
9592 +               break;
9593 +       case AuLsc_DI_PARENT:
9594 +               ii_read_lock_parent(inode);
9595 +               break;
9596 +       case AuLsc_DI_PARENT2:
9597 +               ii_read_lock_parent2(inode);
9598 +               break;
9599 +       case AuLsc_DI_PARENT3:
9600 +               ii_read_lock_parent3(inode);
9601 +               break;
9602 +       default:
9603 +               BUG();
9604 +       }
9605 +}
9606 +
9607 +void di_read_lock(struct dentry *d, int flags, unsigned int lsc)
9608 +{
9609 +       struct inode *inode;
9610 +
9611 +       au_rw_read_lock_nested(&au_di(d)->di_rwsem, lsc);
9612 +       if (d_really_is_positive(d)) {
9613 +               inode = d_inode(d);
9614 +               if (au_ftest_lock(flags, IW))
9615 +                       do_ii_write_lock(inode, lsc);
9616 +               else if (au_ftest_lock(flags, IR))
9617 +                       do_ii_read_lock(inode, lsc);
9618 +       }
9619 +}
9620 +
9621 +void di_read_unlock(struct dentry *d, int flags)
9622 +{
9623 +       struct inode *inode;
9624 +
9625 +       if (d_really_is_positive(d)) {
9626 +               inode = d_inode(d);
9627 +               if (au_ftest_lock(flags, IW)) {
9628 +                       au_dbg_verify_dinode(d);
9629 +                       ii_write_unlock(inode);
9630 +               } else if (au_ftest_lock(flags, IR)) {
9631 +                       au_dbg_verify_dinode(d);
9632 +                       ii_read_unlock(inode);
9633 +               }
9634 +       }
9635 +       au_rw_read_unlock(&au_di(d)->di_rwsem);
9636 +}
9637 +
9638 +void di_downgrade_lock(struct dentry *d, int flags)
9639 +{
9640 +       if (d_really_is_positive(d) && au_ftest_lock(flags, IR))
9641 +               ii_downgrade_lock(d_inode(d));
9642 +       au_rw_dgrade_lock(&au_di(d)->di_rwsem);
9643 +}
9644 +
9645 +void di_write_lock(struct dentry *d, unsigned int lsc)
9646 +{
9647 +       au_rw_write_lock_nested(&au_di(d)->di_rwsem, lsc);
9648 +       if (d_really_is_positive(d))
9649 +               do_ii_write_lock(d_inode(d), lsc);
9650 +}
9651 +
9652 +void di_write_unlock(struct dentry *d)
9653 +{
9654 +       au_dbg_verify_dinode(d);
9655 +       if (d_really_is_positive(d))
9656 +               ii_write_unlock(d_inode(d));
9657 +       au_rw_write_unlock(&au_di(d)->di_rwsem);
9658 +}
9659 +
9660 +void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir)
9661 +{
9662 +       AuDebugOn(d1 == d2
9663 +                 || d_inode(d1) == d_inode(d2)
9664 +                 || d1->d_sb != d2->d_sb);
9665 +
9666 +       if ((isdir && au_test_subdir(d1, d2))
9667 +           || d1 < d2) {
9668 +               di_write_lock_child(d1);
9669 +               di_write_lock_child2(d2);
9670 +       } else {
9671 +               di_write_lock_child(d2);
9672 +               di_write_lock_child2(d1);
9673 +       }
9674 +}
9675 +
9676 +void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir)
9677 +{
9678 +       AuDebugOn(d1 == d2
9679 +                 || d_inode(d1) == d_inode(d2)
9680 +                 || d1->d_sb != d2->d_sb);
9681 +
9682 +       if ((isdir && au_test_subdir(d1, d2))
9683 +           || d1 < d2) {
9684 +               di_write_lock_parent(d1);
9685 +               di_write_lock_parent2(d2);
9686 +       } else {
9687 +               di_write_lock_parent(d2);
9688 +               di_write_lock_parent2(d1);
9689 +       }
9690 +}
9691 +
9692 +void di_write_unlock2(struct dentry *d1, struct dentry *d2)
9693 +{
9694 +       di_write_unlock(d1);
9695 +       if (d_inode(d1) == d_inode(d2))
9696 +               au_rw_write_unlock(&au_di(d2)->di_rwsem);
9697 +       else
9698 +               di_write_unlock(d2);
9699 +}
9700 +
9701 +/* ---------------------------------------------------------------------- */
9702 +
9703 +struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex)
9704 +{
9705 +       struct dentry *d;
9706 +
9707 +       DiMustAnyLock(dentry);
9708 +
9709 +       if (au_dbtop(dentry) < 0 || bindex < au_dbtop(dentry))
9710 +               return NULL;
9711 +       AuDebugOn(bindex < 0);
9712 +       d = au_hdentry(au_di(dentry), bindex)->hd_dentry;
9713 +       AuDebugOn(d && au_dcount(d) <= 0);
9714 +       return d;
9715 +}
9716 +
9717 +/*
9718 + * extended version of au_h_dptr().
9719 + * returns a hashed and positive (or linkable) h_dentry in bindex, NULL, or
9720 + * error.
9721 + */
9722 +struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex)
9723 +{
9724 +       struct dentry *h_dentry;
9725 +       struct inode *inode, *h_inode;
9726 +
9727 +       AuDebugOn(d_really_is_negative(dentry));
9728 +
9729 +       h_dentry = NULL;
9730 +       if (au_dbtop(dentry) <= bindex
9731 +           && bindex <= au_dbbot(dentry))
9732 +               h_dentry = au_h_dptr(dentry, bindex);
9733 +       if (h_dentry && !au_d_linkable(h_dentry)) {
9734 +               dget(h_dentry);
9735 +               goto out; /* success */
9736 +       }
9737 +
9738 +       inode = d_inode(dentry);
9739 +       AuDebugOn(bindex < au_ibtop(inode));
9740 +       AuDebugOn(au_ibbot(inode) < bindex);
9741 +       h_inode = au_h_iptr(inode, bindex);
9742 +       h_dentry = d_find_alias(h_inode);
9743 +       if (h_dentry) {
9744 +               if (!IS_ERR(h_dentry)) {
9745 +                       if (!au_d_linkable(h_dentry))
9746 +                               goto out; /* success */
9747 +                       dput(h_dentry);
9748 +               } else
9749 +                       goto out;
9750 +       }
9751 +
9752 +       if (au_opt_test(au_mntflags(dentry->d_sb), PLINK)) {
9753 +               h_dentry = au_plink_lkup(inode, bindex);
9754 +               AuDebugOn(!h_dentry);
9755 +               if (!IS_ERR(h_dentry)) {
9756 +                       if (!au_d_hashed_positive(h_dentry))
9757 +                               goto out; /* success */
9758 +                       dput(h_dentry);
9759 +                       h_dentry = NULL;
9760 +               }
9761 +       }
9762 +
9763 +out:
9764 +       AuDbgDentry(h_dentry);
9765 +       return h_dentry;
9766 +}
9767 +
9768 +aufs_bindex_t au_dbtail(struct dentry *dentry)
9769 +{
9770 +       aufs_bindex_t bbot, bwh;
9771 +
9772 +       bbot = au_dbbot(dentry);
9773 +       if (0 <= bbot) {
9774 +               bwh = au_dbwh(dentry);
9775 +               if (!bwh)
9776 +                       return bwh;
9777 +               if (0 < bwh && bwh < bbot)
9778 +                       return bwh - 1;
9779 +       }
9780 +       return bbot;
9781 +}
9782 +
9783 +aufs_bindex_t au_dbtaildir(struct dentry *dentry)
9784 +{
9785 +       aufs_bindex_t bbot, bopq;
9786 +
9787 +       bbot = au_dbtail(dentry);
9788 +       if (0 <= bbot) {
9789 +               bopq = au_dbdiropq(dentry);
9790 +               if (0 <= bopq && bopq < bbot)
9791 +                       bbot = bopq;
9792 +       }
9793 +       return bbot;
9794 +}
9795 +
9796 +/* ---------------------------------------------------------------------- */
9797 +
9798 +void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
9799 +                  struct dentry *h_dentry)
9800 +{
9801 +       struct au_dinfo *dinfo;
9802 +       struct au_hdentry *hd;
9803 +       struct au_branch *br;
9804 +
9805 +       DiMustWriteLock(dentry);
9806 +
9807 +       dinfo = au_di(dentry);
9808 +       hd = au_hdentry(dinfo, bindex);
9809 +       au_hdput(hd);
9810 +       hd->hd_dentry = h_dentry;
9811 +       if (h_dentry) {
9812 +               br = au_sbr(dentry->d_sb, bindex);
9813 +               hd->hd_id = br->br_id;
9814 +       }
9815 +}
9816 +
9817 +int au_dbrange_test(struct dentry *dentry)
9818 +{
9819 +       int err;
9820 +       aufs_bindex_t btop, bbot;
9821 +
9822 +       err = 0;
9823 +       btop = au_dbtop(dentry);
9824 +       bbot = au_dbbot(dentry);
9825 +       if (btop >= 0)
9826 +               AuDebugOn(bbot < 0 && btop > bbot);
9827 +       else {
9828 +               err = -EIO;
9829 +               AuDebugOn(bbot >= 0);
9830 +       }
9831 +
9832 +       return err;
9833 +}
9834 +
9835 +int au_digen_test(struct dentry *dentry, unsigned int sigen)
9836 +{
9837 +       int err;
9838 +
9839 +       err = 0;
9840 +       if (unlikely(au_digen(dentry) != sigen
9841 +                    || au_iigen_test(d_inode(dentry), sigen)))
9842 +               err = -EIO;
9843 +
9844 +       return err;
9845 +}
9846 +
9847 +void au_update_digen(struct dentry *dentry)
9848 +{
9849 +       atomic_set(&au_di(dentry)->di_generation, au_sigen(dentry->d_sb));
9850 +       /* smp_mb(); */ /* atomic_set */
9851 +}
9852 +
9853 +void au_update_dbrange(struct dentry *dentry, int do_put_zero)
9854 +{
9855 +       struct au_dinfo *dinfo;
9856 +       struct dentry *h_d;
9857 +       struct au_hdentry *hdp;
9858 +       aufs_bindex_t bindex, bbot;
9859 +
9860 +       DiMustWriteLock(dentry);
9861 +
9862 +       dinfo = au_di(dentry);
9863 +       if (!dinfo || dinfo->di_btop < 0)
9864 +               return;
9865 +
9866 +       if (do_put_zero) {
9867 +               bbot = dinfo->di_bbot;
9868 +               bindex = dinfo->di_btop;
9869 +               hdp = au_hdentry(dinfo, bindex);
9870 +               for (; bindex <= bbot; bindex++, hdp++) {
9871 +                       h_d = hdp->hd_dentry;
9872 +                       if (h_d && d_is_negative(h_d))
9873 +                               au_set_h_dptr(dentry, bindex, NULL);
9874 +               }
9875 +       }
9876 +
9877 +       dinfo->di_btop = 0;
9878 +       hdp = au_hdentry(dinfo, dinfo->di_btop);
9879 +       for (; dinfo->di_btop <= dinfo->di_bbot; dinfo->di_btop++, hdp++)
9880 +               if (hdp->hd_dentry)
9881 +                       break;
9882 +       if (dinfo->di_btop > dinfo->di_bbot) {
9883 +               dinfo->di_btop = -1;
9884 +               dinfo->di_bbot = -1;
9885 +               return;
9886 +       }
9887 +
9888 +       hdp = au_hdentry(dinfo, dinfo->di_bbot);
9889 +       for (; dinfo->di_bbot >= 0; dinfo->di_bbot--, hdp--)
9890 +               if (hdp->hd_dentry)
9891 +                       break;
9892 +       AuDebugOn(dinfo->di_btop > dinfo->di_bbot || dinfo->di_bbot < 0);
9893 +}
9894 +
9895 +void au_update_dbtop(struct dentry *dentry)
9896 +{
9897 +       aufs_bindex_t bindex, bbot;
9898 +       struct dentry *h_dentry;
9899 +
9900 +       bbot = au_dbbot(dentry);
9901 +       for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++) {
9902 +               h_dentry = au_h_dptr(dentry, bindex);
9903 +               if (!h_dentry)
9904 +                       continue;
9905 +               if (d_is_positive(h_dentry)) {
9906 +                       au_set_dbtop(dentry, bindex);
9907 +                       return;
9908 +               }
9909 +               au_set_h_dptr(dentry, bindex, NULL);
9910 +       }
9911 +}
9912 +
9913 +void au_update_dbbot(struct dentry *dentry)
9914 +{
9915 +       aufs_bindex_t bindex, btop;
9916 +       struct dentry *h_dentry;
9917 +
9918 +       btop = au_dbtop(dentry);
9919 +       for (bindex = au_dbbot(dentry); bindex >= btop; bindex--) {
9920 +               h_dentry = au_h_dptr(dentry, bindex);
9921 +               if (!h_dentry)
9922 +                       continue;
9923 +               if (d_is_positive(h_dentry)) {
9924 +                       au_set_dbbot(dentry, bindex);
9925 +                       return;
9926 +               }
9927 +               au_set_h_dptr(dentry, bindex, NULL);
9928 +       }
9929 +}
9930 +
9931 +int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry)
9932 +{
9933 +       aufs_bindex_t bindex, bbot;
9934 +
9935 +       bbot = au_dbbot(dentry);
9936 +       for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++)
9937 +               if (au_h_dptr(dentry, bindex) == h_dentry)
9938 +                       return bindex;
9939 +       return -1;
9940 +}
9941 diff -urN /usr/share/empty/fs/aufs/dir.c linux/fs/aufs/dir.c
9942 --- /usr/share/empty/fs/aufs/dir.c      1970-01-01 01:00:00.000000000 +0100
9943 +++ linux/fs/aufs/dir.c 2022-03-21 14:49:05.719966344 +0100
9944 @@ -0,0 +1,765 @@
9945 +// SPDX-License-Identifier: GPL-2.0
9946 +/*
9947 + * Copyright (C) 2005-2021 Junjiro R. Okajima
9948 + *
9949 + * This program, aufs is free software; you can redistribute it and/or modify
9950 + * it under the terms of the GNU General Public License as published by
9951 + * the Free Software Foundation; either version 2 of the License, or
9952 + * (at your option) any later version.
9953 + *
9954 + * This program is distributed in the hope that it will be useful,
9955 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
9956 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9957 + * GNU General Public License for more details.
9958 + *
9959 + * You should have received a copy of the GNU General Public License
9960 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
9961 + */
9962 +
9963 +/*
9964 + * directory operations
9965 + */
9966 +
9967 +#include <linux/fs_stack.h>
9968 +#include <linux/iversion.h>
9969 +#include "aufs.h"
9970 +
9971 +void au_add_nlink(struct inode *dir, struct inode *h_dir)
9972 +{
9973 +       unsigned int nlink;
9974 +
9975 +       AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
9976 +
9977 +       nlink = dir->i_nlink;
9978 +       nlink += h_dir->i_nlink - 2;
9979 +       if (h_dir->i_nlink < 2)
9980 +               nlink += 2;
9981 +       smp_mb(); /* for i_nlink */
9982 +       /* 0 can happen in revaliding */
9983 +       set_nlink(dir, nlink);
9984 +}
9985 +
9986 +void au_sub_nlink(struct inode *dir, struct inode *h_dir)
9987 +{
9988 +       unsigned int nlink;
9989 +
9990 +       AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
9991 +
9992 +       nlink = dir->i_nlink;
9993 +       nlink -= h_dir->i_nlink - 2;
9994 +       if (h_dir->i_nlink < 2)
9995 +               nlink -= 2;
9996 +       smp_mb(); /* for i_nlink */
9997 +       /* nlink == 0 means the branch-fs is broken */
9998 +       set_nlink(dir, nlink);
9999 +}
10000 +
10001 +loff_t au_dir_size(struct file *file, struct dentry *dentry)
10002 +{
10003 +       loff_t sz;
10004 +       aufs_bindex_t bindex, bbot;
10005 +       struct file *h_file;
10006 +       struct dentry *h_dentry;
10007 +
10008 +       sz = 0;
10009 +       if (file) {
10010 +               AuDebugOn(!d_is_dir(file->f_path.dentry));
10011 +
10012 +               bbot = au_fbbot_dir(file);
10013 +               for (bindex = au_fbtop(file);
10014 +                    bindex <= bbot && sz < KMALLOC_MAX_SIZE;
10015 +                    bindex++) {
10016 +                       h_file = au_hf_dir(file, bindex);
10017 +                       if (h_file && file_inode(h_file))
10018 +                               sz += vfsub_f_size_read(h_file);
10019 +               }
10020 +       } else {
10021 +               AuDebugOn(!dentry);
10022 +               AuDebugOn(!d_is_dir(dentry));
10023 +
10024 +               bbot = au_dbtaildir(dentry);
10025 +               for (bindex = au_dbtop(dentry);
10026 +                    bindex <= bbot && sz < KMALLOC_MAX_SIZE;
10027 +                    bindex++) {
10028 +                       h_dentry = au_h_dptr(dentry, bindex);
10029 +                       if (h_dentry && d_is_positive(h_dentry))
10030 +                               sz += i_size_read(d_inode(h_dentry));
10031 +               }
10032 +       }
10033 +       if (sz < KMALLOC_MAX_SIZE)
10034 +               sz = roundup_pow_of_two(sz);
10035 +       if (sz > KMALLOC_MAX_SIZE)
10036 +               sz = KMALLOC_MAX_SIZE;
10037 +       else if (sz < NAME_MAX) {
10038 +               BUILD_BUG_ON(AUFS_RDBLK_DEF < NAME_MAX);
10039 +               sz = AUFS_RDBLK_DEF;
10040 +       }
10041 +       return sz;
10042 +}
10043 +
10044 +struct au_dir_ts_arg {
10045 +       struct dentry *dentry;
10046 +       aufs_bindex_t brid;
10047 +};
10048 +
10049 +static void au_do_dir_ts(void *arg)
10050 +{
10051 +       struct au_dir_ts_arg *a = arg;
10052 +       struct au_dtime dt;
10053 +       struct path h_path;
10054 +       struct inode *dir, *h_dir;
10055 +       struct super_block *sb;
10056 +       struct au_branch *br;
10057 +       struct au_hinode *hdir;
10058 +       int err;
10059 +       aufs_bindex_t btop, bindex;
10060 +
10061 +       sb = a->dentry->d_sb;
10062 +       if (d_really_is_negative(a->dentry))
10063 +               goto out;
10064 +       /* no dir->i_mutex lock */
10065 +       aufs_read_lock(a->dentry, AuLock_DW); /* noflush */
10066 +
10067 +       dir = d_inode(a->dentry);
10068 +       btop = au_ibtop(dir);
10069 +       bindex = au_br_index(sb, a->brid);
10070 +       if (bindex < btop)
10071 +               goto out_unlock;
10072 +
10073 +       br = au_sbr(sb, bindex);
10074 +       h_path.dentry = au_h_dptr(a->dentry, bindex);
10075 +       if (!h_path.dentry)
10076 +               goto out_unlock;
10077 +       h_path.mnt = au_br_mnt(br);
10078 +       au_dtime_store(&dt, a->dentry, &h_path);
10079 +
10080 +       br = au_sbr(sb, btop);
10081 +       if (!au_br_writable(br->br_perm))
10082 +               goto out_unlock;
10083 +       h_path.dentry = au_h_dptr(a->dentry, btop);
10084 +       h_path.mnt = au_br_mnt(br);
10085 +       err = vfsub_mnt_want_write(h_path.mnt);
10086 +       if (err)
10087 +               goto out_unlock;
10088 +       hdir = au_hi(dir, btop);
10089 +       au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
10090 +       h_dir = au_h_iptr(dir, btop);
10091 +       if (h_dir->i_nlink
10092 +           && timespec64_compare(&h_dir->i_mtime, &dt.dt_mtime) < 0) {
10093 +               dt.dt_h_path = h_path;
10094 +               au_dtime_revert(&dt);
10095 +       }
10096 +       au_hn_inode_unlock(hdir);
10097 +       vfsub_mnt_drop_write(h_path.mnt);
10098 +       au_cpup_attr_timesizes(dir);
10099 +
10100 +out_unlock:
10101 +       aufs_read_unlock(a->dentry, AuLock_DW);
10102 +out:
10103 +       dput(a->dentry);
10104 +       au_nwt_done(&au_sbi(sb)->si_nowait);
10105 +       au_kfree_try_rcu(arg);
10106 +}
10107 +
10108 +void au_dir_ts(struct inode *dir, aufs_bindex_t bindex)
10109 +{
10110 +       int perm, wkq_err;
10111 +       aufs_bindex_t btop;
10112 +       struct au_dir_ts_arg *arg;
10113 +       struct dentry *dentry;
10114 +       struct super_block *sb;
10115 +
10116 +       IMustLock(dir);
10117 +
10118 +       dentry = d_find_any_alias(dir);
10119 +       AuDebugOn(!dentry);
10120 +       sb = dentry->d_sb;
10121 +       btop = au_ibtop(dir);
10122 +       if (btop == bindex) {
10123 +               au_cpup_attr_timesizes(dir);
10124 +               goto out;
10125 +       }
10126 +
10127 +       perm = au_sbr_perm(sb, btop);
10128 +       if (!au_br_writable(perm))
10129 +               goto out;
10130 +
10131 +       arg = kmalloc(sizeof(*arg), GFP_NOFS);
10132 +       if (!arg)
10133 +               goto out;
10134 +
10135 +       arg->dentry = dget(dentry); /* will be dput-ted by au_do_dir_ts() */
10136 +       arg->brid = au_sbr_id(sb, bindex);
10137 +       wkq_err = au_wkq_nowait(au_do_dir_ts, arg, sb, /*flags*/0);
10138 +       if (unlikely(wkq_err)) {
10139 +               pr_err("wkq %d\n", wkq_err);
10140 +               dput(dentry);
10141 +               au_kfree_try_rcu(arg);
10142 +       }
10143 +
10144 +out:
10145 +       dput(dentry);
10146 +}
10147 +
10148 +/* ---------------------------------------------------------------------- */
10149 +
10150 +static int reopen_dir(struct file *file)
10151 +{
10152 +       int err;
10153 +       unsigned int flags;
10154 +       aufs_bindex_t bindex, btail, btop;
10155 +       struct dentry *dentry, *h_dentry;
10156 +       struct file *h_file;
10157 +
10158 +       /* open all lower dirs */
10159 +       dentry = file->f_path.dentry;
10160 +       btop = au_dbtop(dentry);
10161 +       for (bindex = au_fbtop(file); bindex < btop; bindex++)
10162 +               au_set_h_fptr(file, bindex, NULL);
10163 +       au_set_fbtop(file, btop);
10164 +
10165 +       btail = au_dbtaildir(dentry);
10166 +       for (bindex = au_fbbot_dir(file); btail < bindex; bindex--)
10167 +               au_set_h_fptr(file, bindex, NULL);
10168 +       au_set_fbbot_dir(file, btail);
10169 +
10170 +       flags = vfsub_file_flags(file);
10171 +       for (bindex = btop; bindex <= btail; bindex++) {
10172 +               h_dentry = au_h_dptr(dentry, bindex);
10173 +               if (!h_dentry)
10174 +                       continue;
10175 +               h_file = au_hf_dir(file, bindex);
10176 +               if (h_file)
10177 +                       continue;
10178 +
10179 +               h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
10180 +               err = PTR_ERR(h_file);
10181 +               if (IS_ERR(h_file))
10182 +                       goto out; /* close all? */
10183 +               au_set_h_fptr(file, bindex, h_file);
10184 +       }
10185 +       au_update_figen(file);
10186 +       /* todo: necessary? */
10187 +       /* file->f_ra = h_file->f_ra; */
10188 +       err = 0;
10189 +
10190 +out:
10191 +       return err;
10192 +}
10193 +
10194 +static int do_open_dir(struct file *file, int flags, struct file *h_file)
10195 +{
10196 +       int err;
10197 +       aufs_bindex_t bindex, btail;
10198 +       struct dentry *dentry, *h_dentry;
10199 +       struct vfsmount *mnt;
10200 +
10201 +       FiMustWriteLock(file);
10202 +       AuDebugOn(h_file);
10203 +
10204 +       err = 0;
10205 +       mnt = file->f_path.mnt;
10206 +       dentry = file->f_path.dentry;
10207 +       file->f_version = inode_query_iversion(d_inode(dentry));
10208 +       bindex = au_dbtop(dentry);
10209 +       au_set_fbtop(file, bindex);
10210 +       btail = au_dbtaildir(dentry);
10211 +       au_set_fbbot_dir(file, btail);
10212 +       for (; !err && bindex <= btail; bindex++) {
10213 +               h_dentry = au_h_dptr(dentry, bindex);
10214 +               if (!h_dentry)
10215 +                       continue;
10216 +
10217 +               err = vfsub_test_mntns(mnt, h_dentry->d_sb);
10218 +               if (unlikely(err))
10219 +                       break;
10220 +               h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
10221 +               if (IS_ERR(h_file)) {
10222 +                       err = PTR_ERR(h_file);
10223 +                       break;
10224 +               }
10225 +               au_set_h_fptr(file, bindex, h_file);
10226 +       }
10227 +       au_update_figen(file);
10228 +       /* todo: necessary? */
10229 +       /* file->f_ra = h_file->f_ra; */
10230 +       if (!err)
10231 +               return 0; /* success */
10232 +
10233 +       /* close all */
10234 +       for (bindex = au_fbtop(file); bindex <= btail; bindex++)
10235 +               au_set_h_fptr(file, bindex, NULL);
10236 +       au_set_fbtop(file, -1);
10237 +       au_set_fbbot_dir(file, -1);
10238 +
10239 +       return err;
10240 +}
10241 +
10242 +static int aufs_open_dir(struct inode *inode __maybe_unused,
10243 +                        struct file *file)
10244 +{
10245 +       int err;
10246 +       struct super_block *sb;
10247 +       struct au_fidir *fidir;
10248 +
10249 +       err = -ENOMEM;
10250 +       sb = file->f_path.dentry->d_sb;
10251 +       si_read_lock(sb, AuLock_FLUSH);
10252 +       fidir = au_fidir_alloc(sb);
10253 +       if (fidir) {
10254 +               struct au_do_open_args args = {
10255 +                       .open   = do_open_dir,
10256 +                       .fidir  = fidir
10257 +               };
10258 +               err = au_do_open(file, &args);
10259 +               if (unlikely(err))
10260 +                       au_kfree_rcu(fidir);
10261 +       }
10262 +       si_read_unlock(sb);
10263 +       return err;
10264 +}
10265 +
10266 +static int aufs_release_dir(struct inode *inode __maybe_unused,
10267 +                           struct file *file)
10268 +{
10269 +       struct au_vdir *vdir_cache;
10270 +       struct au_finfo *finfo;
10271 +       struct au_fidir *fidir;
10272 +       struct au_hfile *hf;
10273 +       aufs_bindex_t bindex, bbot;
10274 +
10275 +       finfo = au_fi(file);
10276 +       fidir = finfo->fi_hdir;
10277 +       if (fidir) {
10278 +               au_hbl_del(&finfo->fi_hlist,
10279 +                          &au_sbi(file->f_path.dentry->d_sb)->si_files);
10280 +               vdir_cache = fidir->fd_vdir_cache; /* lock-free */
10281 +               if (vdir_cache)
10282 +                       au_vdir_free(vdir_cache);
10283 +
10284 +               bindex = finfo->fi_btop;
10285 +               if (bindex >= 0) {
10286 +                       hf = fidir->fd_hfile + bindex;
10287 +                       /*
10288 +                        * calls fput() instead of filp_close(),
10289 +                        * since no dnotify or lock for the lower file.
10290 +                        */
10291 +                       bbot = fidir->fd_bbot;
10292 +                       for (; bindex <= bbot; bindex++, hf++)
10293 +                               if (hf->hf_file)
10294 +                                       au_hfput(hf, /*execed*/0);
10295 +               }
10296 +               au_kfree_rcu(fidir);
10297 +               finfo->fi_hdir = NULL;
10298 +       }
10299 +       au_finfo_fin(file);
10300 +       return 0;
10301 +}
10302 +
10303 +/* ---------------------------------------------------------------------- */
10304 +
10305 +static int au_do_flush_dir(struct file *file, fl_owner_t id)
10306 +{
10307 +       int err;
10308 +       aufs_bindex_t bindex, bbot;
10309 +       struct file *h_file;
10310 +
10311 +       err = 0;
10312 +       bbot = au_fbbot_dir(file);
10313 +       for (bindex = au_fbtop(file); !err && bindex <= bbot; bindex++) {
10314 +               h_file = au_hf_dir(file, bindex);
10315 +               if (h_file)
10316 +                       err = vfsub_flush(h_file, id);
10317 +       }
10318 +       return err;
10319 +}
10320 +
10321 +static int aufs_flush_dir(struct file *file, fl_owner_t id)
10322 +{
10323 +       return au_do_flush(file, id, au_do_flush_dir);
10324 +}
10325 +
10326 +/* ---------------------------------------------------------------------- */
10327 +
10328 +static int au_do_fsync_dir_no_file(struct dentry *dentry, int datasync)
10329 +{
10330 +       int err;
10331 +       aufs_bindex_t bbot, bindex;
10332 +       struct inode *inode;
10333 +       struct super_block *sb;
10334 +
10335 +       err = 0;
10336 +       sb = dentry->d_sb;
10337 +       inode = d_inode(dentry);
10338 +       IMustLock(inode);
10339 +       bbot = au_dbbot(dentry);
10340 +       for (bindex = au_dbtop(dentry); !err && bindex <= bbot; bindex++) {
10341 +               struct path h_path;
10342 +
10343 +               if (au_test_ro(sb, bindex, inode))
10344 +                       continue;
10345 +               h_path.dentry = au_h_dptr(dentry, bindex);
10346 +               if (!h_path.dentry)
10347 +                       continue;
10348 +
10349 +               h_path.mnt = au_sbr_mnt(sb, bindex);
10350 +               err = vfsub_fsync(NULL, &h_path, datasync);
10351 +       }
10352 +
10353 +       return err;
10354 +}
10355 +
10356 +static int au_do_fsync_dir(struct file *file, int datasync)
10357 +{
10358 +       int err;
10359 +       aufs_bindex_t bbot, bindex;
10360 +       struct file *h_file;
10361 +       struct super_block *sb;
10362 +       struct inode *inode;
10363 +
10364 +       err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1, /*fi_lsc*/0);
10365 +       if (unlikely(err))
10366 +               goto out;
10367 +
10368 +       inode = file_inode(file);
10369 +       sb = inode->i_sb;
10370 +       bbot = au_fbbot_dir(file);
10371 +       for (bindex = au_fbtop(file); !err && bindex <= bbot; bindex++) {
10372 +               h_file = au_hf_dir(file, bindex);
10373 +               if (!h_file || au_test_ro(sb, bindex, inode))
10374 +                       continue;
10375 +
10376 +               err = vfsub_fsync(h_file, &h_file->f_path, datasync);
10377 +       }
10378 +
10379 +out:
10380 +       return err;
10381 +}
10382 +
10383 +/*
10384 + * @file may be NULL
10385 + */
10386 +static int aufs_fsync_dir(struct file *file, loff_t start, loff_t end,
10387 +                         int datasync)
10388 +{
10389 +       int err;
10390 +       struct dentry *dentry;
10391 +       struct inode *inode;
10392 +       struct super_block *sb;
10393 +
10394 +       err = 0;
10395 +       dentry = file->f_path.dentry;
10396 +       inode = d_inode(dentry);
10397 +       inode_lock(inode);
10398 +       sb = dentry->d_sb;
10399 +       si_noflush_read_lock(sb);
10400 +       if (file)
10401 +               err = au_do_fsync_dir(file, datasync);
10402 +       else {
10403 +               di_write_lock_child(dentry);
10404 +               err = au_do_fsync_dir_no_file(dentry, datasync);
10405 +       }
10406 +       au_cpup_attr_timesizes(inode);
10407 +       di_write_unlock(dentry);
10408 +       if (file)
10409 +               fi_write_unlock(file);
10410 +
10411 +       si_read_unlock(sb);
10412 +       inode_unlock(inode);
10413 +       return err;
10414 +}
10415 +
10416 +/* ---------------------------------------------------------------------- */
10417 +
10418 +static int aufs_iterate_shared(struct file *file, struct dir_context *ctx)
10419 +{
10420 +       int err;
10421 +       struct dentry *dentry;
10422 +       struct inode *inode, *h_inode;
10423 +       struct super_block *sb;
10424 +
10425 +       AuDbg("%pD, ctx{%ps, %llu}\n", file, ctx->actor, ctx->pos);
10426 +
10427 +       dentry = file->f_path.dentry;
10428 +       inode = d_inode(dentry);
10429 +       IMustLock(inode);
10430 +
10431 +       sb = dentry->d_sb;
10432 +       si_read_lock(sb, AuLock_FLUSH);
10433 +       err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1, /*fi_lsc*/0);
10434 +       if (unlikely(err))
10435 +               goto out;
10436 +       err = au_alive_dir(dentry);
10437 +       if (!err)
10438 +               err = au_vdir_init(file);
10439 +       di_downgrade_lock(dentry, AuLock_IR);
10440 +       if (unlikely(err))
10441 +               goto out_unlock;
10442 +
10443 +       h_inode = au_h_iptr(inode, au_ibtop(inode));
10444 +       if (!au_test_nfsd()) {
10445 +               err = au_vdir_fill_de(file, ctx);
10446 +               fsstack_copy_attr_atime(inode, h_inode);
10447 +       } else {
10448 +               /*
10449 +                * nfsd filldir may call lookup_one_len(), vfs_getattr(),
10450 +                * encode_fh() and others.
10451 +                */
10452 +               atomic_inc(&h_inode->i_count);
10453 +               di_read_unlock(dentry, AuLock_IR);
10454 +               si_read_unlock(sb);
10455 +               err = au_vdir_fill_de(file, ctx);
10456 +               fsstack_copy_attr_atime(inode, h_inode);
10457 +               fi_write_unlock(file);
10458 +               iput(h_inode);
10459 +
10460 +               AuTraceErr(err);
10461 +               return err;
10462 +       }
10463 +
10464 +out_unlock:
10465 +       di_read_unlock(dentry, AuLock_IR);
10466 +       fi_write_unlock(file);
10467 +out:
10468 +       si_read_unlock(sb);
10469 +       return err;
10470 +}
10471 +
10472 +/* ---------------------------------------------------------------------- */
10473 +
10474 +#define AuTestEmpty_WHONLY     1
10475 +#define AuTestEmpty_CALLED     (1 << 1)
10476 +#define AuTestEmpty_SHWH       (1 << 2)
10477 +#define au_ftest_testempty(flags, name)        ((flags) & AuTestEmpty_##name)
10478 +#define au_fset_testempty(flags, name) \
10479 +       do { (flags) |= AuTestEmpty_##name; } while (0)
10480 +#define au_fclr_testempty(flags, name) \
10481 +       do { (flags) &= ~AuTestEmpty_##name; } while (0)
10482 +
10483 +#ifndef CONFIG_AUFS_SHWH
10484 +#undef AuTestEmpty_SHWH
10485 +#define AuTestEmpty_SHWH       0
10486 +#endif
10487 +
10488 +struct test_empty_arg {
10489 +       struct dir_context ctx;
10490 +       struct au_nhash *whlist;
10491 +       unsigned int flags;
10492 +       int err;
10493 +       aufs_bindex_t bindex;
10494 +};
10495 +
10496 +static int test_empty_cb(struct dir_context *ctx, const char *__name,
10497 +                        int namelen, loff_t offset __maybe_unused, u64 ino,
10498 +                        unsigned int d_type)
10499 +{
10500 +       struct test_empty_arg *arg = container_of(ctx, struct test_empty_arg,
10501 +                                                 ctx);
10502 +       char *name = (void *)__name;
10503 +
10504 +       arg->err = 0;
10505 +       au_fset_testempty(arg->flags, CALLED);
10506 +       /* smp_mb(); */
10507 +       if (name[0] == '.'
10508 +           && (namelen == 1 || (name[1] == '.' && namelen == 2)))
10509 +               goto out; /* success */
10510 +
10511 +       if (namelen <= AUFS_WH_PFX_LEN
10512 +           || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
10513 +               if (au_ftest_testempty(arg->flags, WHONLY)
10514 +                   && !au_nhash_test_known_wh(arg->whlist, name, namelen))
10515 +                       arg->err = -ENOTEMPTY;
10516 +               goto out;
10517 +       }
10518 +
10519 +       name += AUFS_WH_PFX_LEN;
10520 +       namelen -= AUFS_WH_PFX_LEN;
10521 +       if (!au_nhash_test_known_wh(arg->whlist, name, namelen))
10522 +               arg->err = au_nhash_append_wh
10523 +                       (arg->whlist, name, namelen, ino, d_type, arg->bindex,
10524 +                        au_ftest_testempty(arg->flags, SHWH));
10525 +
10526 +out:
10527 +       /* smp_mb(); */
10528 +       AuTraceErr(arg->err);
10529 +       return arg->err;
10530 +}
10531 +
10532 +static int do_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
10533 +{
10534 +       int err;
10535 +       struct file *h_file;
10536 +       struct au_branch *br;
10537 +
10538 +       h_file = au_h_open(dentry, arg->bindex,
10539 +                          O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_LARGEFILE,
10540 +                          /*file*/NULL, /*force_wr*/0);
10541 +       err = PTR_ERR(h_file);
10542 +       if (IS_ERR(h_file))
10543 +               goto out;
10544 +
10545 +       err = 0;
10546 +       if (!au_opt_test(au_mntflags(dentry->d_sb), UDBA_NONE)
10547 +           && !file_inode(h_file)->i_nlink)
10548 +               goto out_put;
10549 +
10550 +       do {
10551 +               arg->err = 0;
10552 +               au_fclr_testempty(arg->flags, CALLED);
10553 +               /* smp_mb(); */
10554 +               err = vfsub_iterate_dir(h_file, &arg->ctx);
10555 +               if (err >= 0)
10556 +                       err = arg->err;
10557 +       } while (!err && au_ftest_testempty(arg->flags, CALLED));
10558 +
10559 +out_put:
10560 +       fput(h_file);
10561 +       br = au_sbr(dentry->d_sb, arg->bindex);
10562 +       au_lcnt_dec(&br->br_nfiles);
10563 +out:
10564 +       return err;
10565 +}
10566 +
10567 +struct do_test_empty_args {
10568 +       int *errp;
10569 +       struct dentry *dentry;
10570 +       struct test_empty_arg *arg;
10571 +};
10572 +
10573 +static void call_do_test_empty(void *args)
10574 +{
10575 +       struct do_test_empty_args *a = args;
10576 +       *a->errp = do_test_empty(a->dentry, a->arg);
10577 +}
10578 +
10579 +static int sio_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
10580 +{
10581 +       int err, wkq_err;
10582 +       struct dentry *h_dentry;
10583 +       struct inode *h_inode;
10584 +       struct user_namespace *h_userns;
10585 +
10586 +       h_userns = au_sbr_userns(dentry->d_sb, arg->bindex);
10587 +       h_dentry = au_h_dptr(dentry, arg->bindex);
10588 +       h_inode = d_inode(h_dentry);
10589 +       /* todo: i_mode changes anytime? */
10590 +       inode_lock_shared_nested(h_inode, AuLsc_I_CHILD);
10591 +       err = au_test_h_perm_sio(h_userns, h_inode, MAY_EXEC | MAY_READ);
10592 +       inode_unlock_shared(h_inode);
10593 +       if (!err)
10594 +               err = do_test_empty(dentry, arg);
10595 +       else {
10596 +               struct do_test_empty_args args = {
10597 +                       .errp   = &err,
10598 +                       .dentry = dentry,
10599 +                       .arg    = arg
10600 +               };
10601 +               unsigned int flags = arg->flags;
10602 +
10603 +               wkq_err = au_wkq_wait(call_do_test_empty, &args);
10604 +               if (unlikely(wkq_err))
10605 +                       err = wkq_err;
10606 +               arg->flags = flags;
10607 +       }
10608 +
10609 +       return err;
10610 +}
10611 +
10612 +int au_test_empty_lower(struct dentry *dentry)
10613 +{
10614 +       int err;
10615 +       unsigned int rdhash;
10616 +       aufs_bindex_t bindex, btop, btail;
10617 +       struct au_nhash whlist;
10618 +       struct test_empty_arg arg = {
10619 +               .ctx = {
10620 +                       .actor = test_empty_cb
10621 +               }
10622 +       };
10623 +       int (*test_empty)(struct dentry *dentry, struct test_empty_arg *arg);
10624 +
10625 +       SiMustAnyLock(dentry->d_sb);
10626 +
10627 +       rdhash = au_sbi(dentry->d_sb)->si_rdhash;
10628 +       if (!rdhash)
10629 +               rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, dentry));
10630 +       err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
10631 +       if (unlikely(err))
10632 +               goto out;
10633 +
10634 +       arg.flags = 0;
10635 +       arg.whlist = &whlist;
10636 +       btop = au_dbtop(dentry);
10637 +       if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
10638 +               au_fset_testempty(arg.flags, SHWH);
10639 +       test_empty = do_test_empty;
10640 +       if (au_opt_test(au_mntflags(dentry->d_sb), DIRPERM1))
10641 +               test_empty = sio_test_empty;
10642 +       arg.bindex = btop;
10643 +       err = test_empty(dentry, &arg);
10644 +       if (unlikely(err))
10645 +               goto out_whlist;
10646 +
10647 +       au_fset_testempty(arg.flags, WHONLY);
10648 +       btail = au_dbtaildir(dentry);
10649 +       for (bindex = btop + 1; !err && bindex <= btail; bindex++) {
10650 +               struct dentry *h_dentry;
10651 +
10652 +               h_dentry = au_h_dptr(dentry, bindex);
10653 +               if (h_dentry && d_is_positive(h_dentry)) {
10654 +                       arg.bindex = bindex;
10655 +                       err = test_empty(dentry, &arg);
10656 +               }
10657 +       }
10658 +
10659 +out_whlist:
10660 +       au_nhash_wh_free(&whlist);
10661 +out:
10662 +       return err;
10663 +}
10664 +
10665 +int au_test_empty(struct dentry *dentry, struct au_nhash *whlist)
10666 +{
10667 +       int err;
10668 +       struct test_empty_arg arg = {
10669 +               .ctx = {
10670 +                       .actor = test_empty_cb
10671 +               }
10672 +       };
10673 +       aufs_bindex_t bindex, btail;
10674 +
10675 +       err = 0;
10676 +       arg.whlist = whlist;
10677 +       arg.flags = AuTestEmpty_WHONLY;
10678 +       if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
10679 +               au_fset_testempty(arg.flags, SHWH);
10680 +       btail = au_dbtaildir(dentry);
10681 +       for (bindex = au_dbtop(dentry); !err && bindex <= btail; bindex++) {
10682 +               struct dentry *h_dentry;
10683 +
10684 +               h_dentry = au_h_dptr(dentry, bindex);
10685 +               if (h_dentry && d_is_positive(h_dentry)) {
10686 +                       arg.bindex = bindex;
10687 +                       err = sio_test_empty(dentry, &arg);
10688 +               }
10689 +       }
10690 +
10691 +       return err;
10692 +}
10693 +
10694 +/* ---------------------------------------------------------------------- */
10695 +
10696 +const struct file_operations aufs_dir_fop = {
10697 +       .owner          = THIS_MODULE,
10698 +       .llseek         = default_llseek,
10699 +       .read           = generic_read_dir,
10700 +       .iterate_shared = aufs_iterate_shared,
10701 +       .unlocked_ioctl = aufs_ioctl_dir,
10702 +#ifdef CONFIG_COMPAT
10703 +       .compat_ioctl   = aufs_compat_ioctl_dir,
10704 +#endif
10705 +       .open           = aufs_open_dir,
10706 +       .release        = aufs_release_dir,
10707 +       .flush          = aufs_flush_dir,
10708 +       .fsync          = aufs_fsync_dir
10709 +};
10710 diff -urN /usr/share/empty/fs/aufs/dir.h linux/fs/aufs/dir.h
10711 --- /usr/share/empty/fs/aufs/dir.h      1970-01-01 01:00:00.000000000 +0100
10712 +++ linux/fs/aufs/dir.h 2022-03-21 14:49:05.719966344 +0100
10713 @@ -0,0 +1,134 @@
10714 +/* SPDX-License-Identifier: GPL-2.0 */
10715 +/*
10716 + * Copyright (C) 2005-2021 Junjiro R. Okajima
10717 + *
10718 + * This program, aufs is free software; you can redistribute it and/or modify
10719 + * it under the terms of the GNU General Public License as published by
10720 + * the Free Software Foundation; either version 2 of the License, or
10721 + * (at your option) any later version.
10722 + *
10723 + * This program is distributed in the hope that it will be useful,
10724 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
10725 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10726 + * GNU General Public License for more details.
10727 + *
10728 + * You should have received a copy of the GNU General Public License
10729 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
10730 + */
10731 +
10732 +/*
10733 + * directory operations
10734 + */
10735 +
10736 +#ifndef __AUFS_DIR_H__
10737 +#define __AUFS_DIR_H__
10738 +
10739 +#ifdef __KERNEL__
10740 +
10741 +#include <linux/fs.h>
10742 +
10743 +/* ---------------------------------------------------------------------- */
10744 +
10745 +/* need to be faster and smaller */
10746 +
10747 +struct au_nhash {
10748 +       unsigned int            nh_num;
10749 +       struct hlist_head       *nh_head;
10750 +};
10751 +
10752 +struct au_vdir_destr {
10753 +       unsigned char   len;
10754 +       unsigned char   name[];
10755 +} __packed;
10756 +
10757 +struct au_vdir_dehstr {
10758 +       struct hlist_node       hash;
10759 +       struct au_vdir_destr    *str;
10760 +       struct rcu_head         rcu;
10761 +} ____cacheline_aligned_in_smp;
10762 +
10763 +struct au_vdir_de {
10764 +       ino_t                   de_ino;
10765 +       unsigned char           de_type;
10766 +       /* caution: packed */
10767 +       struct au_vdir_destr    de_str;
10768 +} __packed;
10769 +
10770 +struct au_vdir_wh {
10771 +       struct hlist_node       wh_hash;
10772 +#ifdef CONFIG_AUFS_SHWH
10773 +       ino_t                   wh_ino;
10774 +       aufs_bindex_t           wh_bindex;
10775 +       unsigned char           wh_type;
10776 +#else
10777 +       aufs_bindex_t           wh_bindex;
10778 +#endif
10779 +       /* caution: packed */
10780 +       struct au_vdir_destr    wh_str;
10781 +} __packed;
10782 +
10783 +union au_vdir_deblk_p {
10784 +       unsigned char           *deblk;
10785 +       struct au_vdir_de       *de;
10786 +};
10787 +
10788 +struct au_vdir {
10789 +       unsigned char   **vd_deblk;
10790 +       unsigned long   vd_nblk;
10791 +       struct {
10792 +               unsigned long           ul;
10793 +               union au_vdir_deblk_p   p;
10794 +       } vd_last;
10795 +
10796 +       u64             vd_version;
10797 +       unsigned int    vd_deblk_sz;
10798 +       unsigned long   vd_jiffy;
10799 +       struct rcu_head rcu;
10800 +} ____cacheline_aligned_in_smp;
10801 +
10802 +/* ---------------------------------------------------------------------- */
10803 +
10804 +/* dir.c */
10805 +extern const struct file_operations aufs_dir_fop;
10806 +void au_add_nlink(struct inode *dir, struct inode *h_dir);
10807 +void au_sub_nlink(struct inode *dir, struct inode *h_dir);
10808 +loff_t au_dir_size(struct file *file, struct dentry *dentry);
10809 +void au_dir_ts(struct inode *dir, aufs_bindex_t bsrc);
10810 +int au_test_empty_lower(struct dentry *dentry);
10811 +int au_test_empty(struct dentry *dentry, struct au_nhash *whlist);
10812 +
10813 +/* vdir.c */
10814 +unsigned int au_rdhash_est(loff_t sz);
10815 +int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp);
10816 +void au_nhash_wh_free(struct au_nhash *whlist);
10817 +int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
10818 +                           int limit);
10819 +int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen);
10820 +int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
10821 +                      unsigned int d_type, aufs_bindex_t bindex,
10822 +                      unsigned char shwh);
10823 +void au_vdir_free(struct au_vdir *vdir);
10824 +int au_vdir_init(struct file *file);
10825 +int au_vdir_fill_de(struct file *file, struct dir_context *ctx);
10826 +
10827 +/* ioctl.c */
10828 +long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg);
10829 +
10830 +#ifdef CONFIG_AUFS_RDU
10831 +/* rdu.c */
10832 +long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
10833 +#ifdef CONFIG_COMPAT
10834 +long au_rdu_compat_ioctl(struct file *file, unsigned int cmd,
10835 +                        unsigned long arg);
10836 +#endif
10837 +#else
10838 +AuStub(long, au_rdu_ioctl, return -EINVAL, struct file *file,
10839 +       unsigned int cmd, unsigned long arg)
10840 +#ifdef CONFIG_COMPAT
10841 +AuStub(long, au_rdu_compat_ioctl, return -EINVAL, struct file *file,
10842 +       unsigned int cmd, unsigned long arg)
10843 +#endif
10844 +#endif
10845 +
10846 +#endif /* __KERNEL__ */
10847 +#endif /* __AUFS_DIR_H__ */
10848 diff -urN /usr/share/empty/fs/aufs/dirren.c linux/fs/aufs/dirren.c
10849 --- /usr/share/empty/fs/aufs/dirren.c   1970-01-01 01:00:00.000000000 +0100
10850 +++ linux/fs/aufs/dirren.c      2022-03-21 14:49:05.723299677 +0100
10851 @@ -0,0 +1,1315 @@
10852 +// SPDX-License-Identifier: GPL-2.0
10853 +/*
10854 + * Copyright (C) 2017-2021 Junjiro R. Okajima
10855 + *
10856 + * This program, aufs is free software; you can redistribute it and/or modify
10857 + * it under the terms of the GNU General Public License as published by
10858 + * the Free Software Foundation; either version 2 of the License, or
10859 + * (at your option) any later version.
10860 + *
10861 + * This program is distributed in the hope that it will be useful,
10862 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
10863 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10864 + * GNU General Public License for more details.
10865 + *
10866 + * You should have received a copy of the GNU General Public License
10867 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
10868 + */
10869 +
10870 +/*
10871 + * special handling in renaming a directory
10872 + * in order to support looking-up the before-renamed name on the lower readonly
10873 + * branches
10874 + */
10875 +
10876 +#include <linux/byteorder/generic.h>
10877 +#include "aufs.h"
10878 +
10879 +static void au_dr_hino_del(struct au_dr_br *dr, struct au_dr_hino *ent)
10880 +{
10881 +       int idx;
10882 +
10883 +       idx = au_dr_ihash(ent->dr_h_ino);
10884 +       au_hbl_del(&ent->dr_hnode, dr->dr_h_ino + idx);
10885 +}
10886 +
10887 +static int au_dr_hino_test_empty(struct au_dr_br *dr)
10888 +{
10889 +       int ret, i;
10890 +       struct hlist_bl_head *hbl;
10891 +
10892 +       ret = 1;
10893 +       for (i = 0; ret && i < AuDirren_NHASH; i++) {
10894 +               hbl = dr->dr_h_ino + i;
10895 +               hlist_bl_lock(hbl);
10896 +               ret &= hlist_bl_empty(hbl);
10897 +               hlist_bl_unlock(hbl);
10898 +       }
10899 +
10900 +       return ret;
10901 +}
10902 +
10903 +static struct au_dr_hino *au_dr_hino_find(struct au_dr_br *dr, ino_t ino)
10904 +{
10905 +       struct au_dr_hino *found, *ent;
10906 +       struct hlist_bl_head *hbl;
10907 +       struct hlist_bl_node *pos;
10908 +       int idx;
10909 +
10910 +       found = NULL;
10911 +       idx = au_dr_ihash(ino);
10912 +       hbl = dr->dr_h_ino + idx;
10913 +       hlist_bl_lock(hbl);
10914 +       hlist_bl_for_each_entry(ent, pos, hbl, dr_hnode)
10915 +               if (ent->dr_h_ino == ino) {
10916 +                       found = ent;
10917 +                       break;
10918 +               }
10919 +       hlist_bl_unlock(hbl);
10920 +
10921 +       return found;
10922 +}
10923 +
10924 +int au_dr_hino_test_add(struct au_dr_br *dr, ino_t ino,
10925 +                       struct au_dr_hino *add_ent)
10926 +{
10927 +       int found, idx;
10928 +       struct hlist_bl_head *hbl;
10929 +       struct hlist_bl_node *pos;
10930 +       struct au_dr_hino *ent;
10931 +
10932 +       found = 0;
10933 +       idx = au_dr_ihash(ino);
10934 +       hbl = dr->dr_h_ino + idx;
10935 +#if 0 /* debug print */
10936 +       {
10937 +               struct hlist_bl_node *tmp;
10938 +
10939 +               hlist_bl_for_each_entry_safe(ent, pos, tmp, hbl, dr_hnode)
10940 +                       AuDbg("hi%llu\n", (unsigned long long)ent->dr_h_ino);
10941 +       }
10942 +#endif
10943 +       hlist_bl_lock(hbl);
10944 +       hlist_bl_for_each_entry(ent, pos, hbl, dr_hnode)
10945 +               if (ent->dr_h_ino == ino) {
10946 +                       found = 1;
10947 +                       break;
10948 +               }
10949 +       if (!found && add_ent)
10950 +               hlist_bl_add_head(&add_ent->dr_hnode, hbl);
10951 +       hlist_bl_unlock(hbl);
10952 +
10953 +       if (!found && add_ent)
10954 +               AuDbg("i%llu added\n", (unsigned long long)add_ent->dr_h_ino);
10955 +
10956 +       return found;
10957 +}
10958 +
10959 +void au_dr_hino_free(struct au_dr_br *dr)
10960 +{
10961 +       int i;
10962 +       struct hlist_bl_head *hbl;
10963 +       struct hlist_bl_node *pos, *tmp;
10964 +       struct au_dr_hino *ent;
10965 +
10966 +       /* SiMustWriteLock(sb); */
10967 +
10968 +       for (i = 0; i < AuDirren_NHASH; i++) {
10969 +               hbl = dr->dr_h_ino + i;
10970 +               /* no spinlock since sbinfo must be write-locked */
10971 +               hlist_bl_for_each_entry_safe(ent, pos, tmp, hbl, dr_hnode)
10972 +                       au_kfree_rcu(ent);
10973 +               INIT_HLIST_BL_HEAD(hbl);
10974 +       }
10975 +}
10976 +
10977 +/* returns the number of inodes or an error */
10978 +static int au_dr_hino_store(struct super_block *sb, struct au_branch *br,
10979 +                           struct file *hinofile)
10980 +{
10981 +       int err, i;
10982 +       ssize_t ssz;
10983 +       loff_t pos, oldsize;
10984 +       __be64 u64;
10985 +       struct inode *hinoinode;
10986 +       struct hlist_bl_head *hbl;
10987 +       struct hlist_bl_node *n1, *n2;
10988 +       struct au_dr_hino *ent;
10989 +
10990 +       SiMustWriteLock(sb);
10991 +       AuDebugOn(!au_br_writable(br->br_perm));
10992 +
10993 +       hinoinode = file_inode(hinofile);
10994 +       oldsize = i_size_read(hinoinode);
10995 +
10996 +       err = 0;
10997 +       pos = 0;
10998 +       hbl = br->br_dirren.dr_h_ino;
10999 +       for (i = 0; !err && i < AuDirren_NHASH; i++, hbl++) {
11000 +               /* no bit-lock since sbinfo must be write-locked */
11001 +               hlist_bl_for_each_entry_safe(ent, n1, n2, hbl, dr_hnode) {
11002 +                       AuDbg("hi%llu, %pD2\n",
11003 +                             (unsigned long long)ent->dr_h_ino, hinofile);
11004 +                       u64 = cpu_to_be64(ent->dr_h_ino);
11005 +                       ssz = vfsub_write_k(hinofile, &u64, sizeof(u64), &pos);
11006 +                       if (ssz == sizeof(u64))
11007 +                               continue;
11008 +
11009 +                       /* write error */
11010 +                       pr_err("ssz %zd, %pD2\n", ssz, hinofile);
11011 +                       err = -ENOSPC;
11012 +                       if (ssz < 0)
11013 +                               err = ssz;
11014 +                       break;
11015 +               }
11016 +       }
11017 +       /* regardless the error */
11018 +       if (pos < oldsize) {
11019 +               err = vfsub_trunc(&hinofile->f_path, pos, /*attr*/0, hinofile);
11020 +               AuTraceErr(err);
11021 +       }
11022 +
11023 +       AuTraceErr(err);
11024 +       return err;
11025 +}
11026 +
11027 +static int au_dr_hino_load(struct au_dr_br *dr, struct file *hinofile)
11028 +{
11029 +       int err, hidx;
11030 +       ssize_t ssz;
11031 +       size_t sz, n;
11032 +       loff_t pos;
11033 +       uint64_t u64;
11034 +       struct au_dr_hino *ent;
11035 +       struct inode *hinoinode;
11036 +       struct hlist_bl_head *hbl;
11037 +
11038 +       err = 0;
11039 +       pos = 0;
11040 +       hbl = dr->dr_h_ino;
11041 +       hinoinode = file_inode(hinofile);
11042 +       sz = i_size_read(hinoinode);
11043 +       AuDebugOn(sz % sizeof(u64));
11044 +       n = sz / sizeof(u64);
11045 +       while (n--) {
11046 +               ssz = vfsub_read_k(hinofile, &u64, sizeof(u64), &pos);
11047 +               if (unlikely(ssz != sizeof(u64))) {
11048 +                       pr_err("ssz %zd, %pD2\n", ssz, hinofile);
11049 +                       err = -EINVAL;
11050 +                       if (ssz < 0)
11051 +                               err = ssz;
11052 +                       goto out_free;
11053 +               }
11054 +
11055 +               ent = kmalloc(sizeof(*ent), GFP_NOFS);
11056 +               if (!ent) {
11057 +                       err = -ENOMEM;
11058 +                       AuTraceErr(err);
11059 +                       goto out_free;
11060 +               }
11061 +               ent->dr_h_ino = be64_to_cpu((__force __be64)u64);
11062 +               AuDbg("hi%llu, %pD2\n",
11063 +                     (unsigned long long)ent->dr_h_ino, hinofile);
11064 +               hidx = au_dr_ihash(ent->dr_h_ino);
11065 +               au_hbl_add(&ent->dr_hnode, hbl + hidx);
11066 +       }
11067 +       goto out; /* success */
11068 +
11069 +out_free:
11070 +       au_dr_hino_free(dr);
11071 +out:
11072 +       AuTraceErr(err);
11073 +       return err;
11074 +}
11075 +
11076 +/*
11077 + * @bindex/@br is a switch to distinguish whether suspending hnotify or not.
11078 + * @path is a switch to distinguish load and store.
11079 + */
11080 +static int au_dr_hino(struct super_block *sb, aufs_bindex_t bindex,
11081 +                     struct au_branch *br, const struct path *path)
11082 +{
11083 +       int err, flags;
11084 +       unsigned char load, suspend;
11085 +       struct file *hinofile;
11086 +       struct au_hinode *hdir;
11087 +       struct inode *dir, *delegated;
11088 +       struct path hinopath;
11089 +       struct qstr hinoname = QSTR_INIT(AUFS_WH_DR_BRHINO,
11090 +                                        sizeof(AUFS_WH_DR_BRHINO) - 1);
11091 +
11092 +       AuDebugOn(bindex < 0 && !br);
11093 +       AuDebugOn(bindex >= 0 && br);
11094 +
11095 +       err = -EINVAL;
11096 +       suspend = !br;
11097 +       if (suspend)
11098 +               br = au_sbr(sb, bindex);
11099 +       load = !!path;
11100 +       if (!load) {
11101 +               path = &br->br_path;
11102 +               AuDebugOn(!au_br_writable(br->br_perm));
11103 +               if (unlikely(!au_br_writable(br->br_perm)))
11104 +                       goto out;
11105 +       }
11106 +
11107 +       hdir = NULL;
11108 +       if (suspend) {
11109 +               dir = d_inode(sb->s_root);
11110 +               hdir = au_hinode(au_ii(dir), bindex);
11111 +               dir = hdir->hi_inode;
11112 +               au_hn_inode_lock_nested(hdir, AuLsc_I_CHILD);
11113 +       } else {
11114 +               dir = d_inode(path->dentry);
11115 +               inode_lock_nested(dir, AuLsc_I_CHILD);
11116 +       }
11117 +       hinopath.mnt = path->mnt;
11118 +       hinopath.dentry = vfsub_lkup_one(&hinoname, (struct path *)path);
11119 +       err = PTR_ERR(hinopath.dentry);
11120 +       if (IS_ERR(hinopath.dentry))
11121 +               goto out_unlock;
11122 +
11123 +       err = 0;
11124 +       flags = O_RDONLY;
11125 +       if (load) {
11126 +               if (d_is_negative(hinopath.dentry))
11127 +                       goto out_dput; /* success */
11128 +       } else {
11129 +               if (au_dr_hino_test_empty(&br->br_dirren)) {
11130 +                       if (d_is_positive(hinopath.dentry)) {
11131 +                               delegated = NULL;
11132 +                               err = vfsub_unlink(dir, &hinopath, &delegated,
11133 +                                                  /*force*/0);
11134 +                               AuTraceErr(err);
11135 +                               if (unlikely(err))
11136 +                                       pr_err("ignored err %d, %pd2\n",
11137 +                                              err, hinopath.dentry);
11138 +                               if (unlikely(err == -EWOULDBLOCK))
11139 +                                       iput(delegated);
11140 +                               err = 0;
11141 +                       }
11142 +                       goto out_dput;
11143 +               } else if (!d_is_positive(hinopath.dentry)) {
11144 +                       err = vfsub_create(dir, &hinopath, 0600,
11145 +                                          /*want_excl*/false);
11146 +                       AuTraceErr(err);
11147 +                       if (unlikely(err))
11148 +                               goto out_dput;
11149 +               }
11150 +               flags = O_WRONLY;
11151 +       }
11152 +       hinofile = vfsub_dentry_open(&hinopath, flags);
11153 +       if (suspend)
11154 +               au_hn_inode_unlock(hdir);
11155 +       else
11156 +               inode_unlock(dir);
11157 +       dput(hinopath.dentry);
11158 +       AuTraceErrPtr(hinofile);
11159 +       if (IS_ERR(hinofile)) {
11160 +               err = PTR_ERR(hinofile);
11161 +               goto out;
11162 +       }
11163 +
11164 +       if (load)
11165 +               err = au_dr_hino_load(&br->br_dirren, hinofile);
11166 +       else
11167 +               err = au_dr_hino_store(sb, br, hinofile);
11168 +       fput(hinofile);
11169 +       goto out;
11170 +
11171 +out_dput:
11172 +       dput(hinopath.dentry);
11173 +out_unlock:
11174 +       if (suspend)
11175 +               au_hn_inode_unlock(hdir);
11176 +       else
11177 +               inode_unlock(dir);
11178 +out:
11179 +       AuTraceErr(err);
11180 +       return err;
11181 +}
11182 +
11183 +/* ---------------------------------------------------------------------- */
11184 +
11185 +static int au_dr_brid_init(struct au_dr_brid *brid, const struct path *path)
11186 +{
11187 +       int err;
11188 +       struct kstatfs kstfs;
11189 +       dev_t dev;
11190 +       struct dentry *dentry;
11191 +       struct super_block *sb;
11192 +
11193 +       err = vfs_statfs((void *)path, &kstfs);
11194 +       AuTraceErr(err);
11195 +       if (unlikely(err))
11196 +               goto out;
11197 +
11198 +       /* todo: support for UUID */
11199 +
11200 +       if (kstfs.f_fsid.val[0] || kstfs.f_fsid.val[1]) {
11201 +               brid->type = AuBrid_FSID;
11202 +               brid->fsid = kstfs.f_fsid;
11203 +       } else {
11204 +               dentry = path->dentry;
11205 +               sb = dentry->d_sb;
11206 +               dev = sb->s_dev;
11207 +               if (dev) {
11208 +                       brid->type = AuBrid_DEV;
11209 +                       brid->dev = dev;
11210 +               }
11211 +       }
11212 +
11213 +out:
11214 +       return err;
11215 +}
11216 +
11217 +int au_dr_br_init(struct super_block *sb, struct au_branch *br,
11218 +                 const struct path *path)
11219 +{
11220 +       int err, i;
11221 +       struct au_dr_br *dr;
11222 +       struct hlist_bl_head *hbl;
11223 +
11224 +       dr = &br->br_dirren;
11225 +       hbl = dr->dr_h_ino;
11226 +       for (i = 0; i < AuDirren_NHASH; i++, hbl++)
11227 +               INIT_HLIST_BL_HEAD(hbl);
11228 +
11229 +       err = au_dr_brid_init(&dr->dr_brid, path);
11230 +       if (unlikely(err))
11231 +               goto out;
11232 +
11233 +       if (au_opt_test(au_mntflags(sb), DIRREN))
11234 +               err = au_dr_hino(sb, /*bindex*/-1, br, path);
11235 +
11236 +out:
11237 +       AuTraceErr(err);
11238 +       return err;
11239 +}
11240 +
11241 +int au_dr_br_fin(struct super_block *sb, struct au_branch *br)
11242 +{
11243 +       int err;
11244 +
11245 +       err = 0;
11246 +       if (au_br_writable(br->br_perm))
11247 +               err = au_dr_hino(sb, /*bindex*/-1, br, /*path*/NULL);
11248 +       if (!err)
11249 +               au_dr_hino_free(&br->br_dirren);
11250 +
11251 +       return err;
11252 +}
11253 +
11254 +/* ---------------------------------------------------------------------- */
11255 +
11256 +static int au_brid_str(struct au_dr_brid *brid, struct inode *h_inode,
11257 +                      char *buf, size_t sz)
11258 +{
11259 +       int err;
11260 +       unsigned int major, minor;
11261 +       char *p;
11262 +
11263 +       p = buf;
11264 +       err = snprintf(p, sz, "%d_", brid->type);
11265 +       AuDebugOn(err > sz);
11266 +       p += err;
11267 +       sz -= err;
11268 +       switch (brid->type) {
11269 +       case AuBrid_Unset:
11270 +               return -EINVAL;
11271 +       case AuBrid_UUID:
11272 +               err = snprintf(p, sz, "%pU", brid->uuid.b);
11273 +               break;
11274 +       case AuBrid_FSID:
11275 +               err = snprintf(p, sz, "%08x-%08x",
11276 +                              brid->fsid.val[0], brid->fsid.val[1]);
11277 +               break;
11278 +       case AuBrid_DEV:
11279 +               major = MAJOR(brid->dev);
11280 +               minor = MINOR(brid->dev);
11281 +               if (major <= 0xff && minor <= 0xff)
11282 +                       err = snprintf(p, sz, "%02x%02x", major, minor);
11283 +               else
11284 +                       err = snprintf(p, sz, "%03x:%05x", major, minor);
11285 +               break;
11286 +       }
11287 +       AuDebugOn(err > sz);
11288 +       p += err;
11289 +       sz -= err;
11290 +       err = snprintf(p, sz, "_%llu", (unsigned long long)h_inode->i_ino);
11291 +       AuDebugOn(err > sz);
11292 +       p += err;
11293 +       sz -= err;
11294 +
11295 +       return p - buf;
11296 +}
11297 +
11298 +static int au_drinfo_name(struct au_branch *br, char *name, int len)
11299 +{
11300 +       int rlen;
11301 +       struct dentry *br_dentry;
11302 +       struct inode *br_inode;
11303 +
11304 +       br_dentry = au_br_dentry(br);
11305 +       br_inode = d_inode(br_dentry);
11306 +       rlen = au_brid_str(&br->br_dirren.dr_brid, br_inode, name, len);
11307 +       AuDebugOn(rlen >= AUFS_DIRREN_ENV_VAL_SZ);
11308 +       AuDebugOn(rlen > len);
11309 +
11310 +       return rlen;
11311 +}
11312 +
11313 +/* ---------------------------------------------------------------------- */
11314 +
11315 +/*
11316 + * from the given @h_dentry, construct drinfo at @*fdata.
11317 + * when the size of @*fdata is not enough, reallocate and return new @fdata and
11318 + * @allocated.
11319 + */
11320 +static int au_drinfo_construct(struct au_drinfo_fdata **fdata,
11321 +                              struct dentry *h_dentry,
11322 +                              unsigned char *allocated)
11323 +{
11324 +       int err, v;
11325 +       struct au_drinfo_fdata *f, *p;
11326 +       struct au_drinfo *drinfo;
11327 +       struct inode *h_inode;
11328 +       struct qstr *qname;
11329 +
11330 +       err = 0;
11331 +       f = *fdata;
11332 +       h_inode = d_inode(h_dentry);
11333 +       qname = &h_dentry->d_name;
11334 +       drinfo = &f->drinfo;
11335 +       drinfo->ino = (__force uint64_t)cpu_to_be64(h_inode->i_ino);
11336 +       drinfo->oldnamelen = qname->len;
11337 +       if (*allocated < sizeof(*f) + qname->len) {
11338 +               v = roundup_pow_of_two(*allocated + qname->len);
11339 +               p = au_krealloc(f, v, GFP_NOFS, /*may_shrink*/0);
11340 +               if (unlikely(!p)) {
11341 +                       err = -ENOMEM;
11342 +                       AuTraceErr(err);
11343 +                       goto out;
11344 +               }
11345 +               f = p;
11346 +               *fdata = f;
11347 +               *allocated = v;
11348 +               drinfo = &f->drinfo;
11349 +       }
11350 +       memcpy(drinfo->oldname, qname->name, qname->len);
11351 +       AuDbg("i%llu, %.*s\n",
11352 +             be64_to_cpu((__force __be64)drinfo->ino), drinfo->oldnamelen,
11353 +             drinfo->oldname);
11354 +
11355 +out:
11356 +       AuTraceErr(err);
11357 +       return err;
11358 +}
11359 +
11360 +/* callers have to free the return value */
11361 +static struct au_drinfo *au_drinfo_read_k(struct file *file, ino_t h_ino)
11362 +{
11363 +       struct au_drinfo *ret, *drinfo;
11364 +       struct au_drinfo_fdata fdata;
11365 +       int len;
11366 +       loff_t pos;
11367 +       ssize_t ssz;
11368 +
11369 +       ret = ERR_PTR(-EIO);
11370 +       pos = 0;
11371 +       ssz = vfsub_read_k(file, &fdata, sizeof(fdata), &pos);
11372 +       if (unlikely(ssz != sizeof(fdata))) {
11373 +               AuIOErr("ssz %zd, %u, %pD2\n",
11374 +                       ssz, (unsigned int)sizeof(fdata), file);
11375 +               goto out;
11376 +       }
11377 +
11378 +       fdata.magic = ntohl((__force __be32)fdata.magic);
11379 +       switch (fdata.magic) {
11380 +       case AUFS_DRINFO_MAGIC_V1:
11381 +               break;
11382 +       default:
11383 +               AuIOErr("magic-num 0x%x, 0x%x, %pD2\n",
11384 +                       fdata.magic, AUFS_DRINFO_MAGIC_V1, file);
11385 +               goto out;
11386 +       }
11387 +
11388 +       drinfo = &fdata.drinfo;
11389 +       len = drinfo->oldnamelen;
11390 +       if (!len) {
11391 +               AuIOErr("broken drinfo %pD2\n", file);
11392 +               goto out;
11393 +       }
11394 +
11395 +       ret = NULL;
11396 +       drinfo->ino = be64_to_cpu((__force __be64)drinfo->ino);
11397 +       if (unlikely(h_ino && drinfo->ino != h_ino)) {
11398 +               AuDbg("ignored i%llu, i%llu, %pD2\n",
11399 +                     (unsigned long long)drinfo->ino,
11400 +                     (unsigned long long)h_ino, file);
11401 +               goto out; /* success */
11402 +       }
11403 +
11404 +       ret = kmalloc(sizeof(*ret) + len, GFP_NOFS);
11405 +       if (unlikely(!ret)) {
11406 +               ret = ERR_PTR(-ENOMEM);
11407 +               AuTraceErrPtr(ret);
11408 +               goto out;
11409 +       }
11410 +
11411 +       *ret = *drinfo;
11412 +       ssz = vfsub_read_k(file, (void *)ret->oldname, len, &pos);
11413 +       if (unlikely(ssz != len)) {
11414 +               au_kfree_rcu(ret);
11415 +               ret = ERR_PTR(-EIO);
11416 +               AuIOErr("ssz %zd, %u, %pD2\n", ssz, len, file);
11417 +               goto out;
11418 +       }
11419 +
11420 +       AuDbg("oldname %.*s\n", ret->oldnamelen, ret->oldname);
11421 +
11422 +out:
11423 +       return ret;
11424 +}
11425 +
11426 +/* ---------------------------------------------------------------------- */
11427 +
11428 +/* in order to be revertible */
11429 +struct au_drinfo_rev_elm {
11430 +       int                     created;
11431 +       struct dentry           *info_dentry;
11432 +       struct au_drinfo        *info_last;
11433 +};
11434 +
11435 +struct au_drinfo_rev {
11436 +       unsigned char                   already;
11437 +       aufs_bindex_t                   nelm;
11438 +       struct au_drinfo_rev_elm        elm[];
11439 +};
11440 +
11441 +/* todo: isn't it too large? */
11442 +struct au_drinfo_store {
11443 +       struct path h_ppath;
11444 +       struct dentry *h_dentry;
11445 +       struct au_drinfo_fdata *fdata;
11446 +       char *infoname;                 /* inside of whname, just after PFX */
11447 +       char whname[sizeof(AUFS_WH_DR_INFO_PFX) + AUFS_DIRREN_ENV_VAL_SZ];
11448 +       aufs_bindex_t btgt, btail;
11449 +       unsigned char no_sio,
11450 +               allocated,              /* current size of *fdata */
11451 +               infonamelen,            /* room size for p */
11452 +               whnamelen,              /* length of the generated name */
11453 +               renameback;             /* renamed back */
11454 +};
11455 +
11456 +/* on rename(2) error, the caller should revert it using @elm */
11457 +static int au_drinfo_do_store(struct au_drinfo_store *w,
11458 +                             struct au_drinfo_rev_elm *elm)
11459 +{
11460 +       int err, len;
11461 +       ssize_t ssz;
11462 +       loff_t pos;
11463 +       struct path infopath = {
11464 +               .mnt = w->h_ppath.mnt
11465 +       };
11466 +       struct inode *h_dir, *h_inode, *delegated;
11467 +       struct file *infofile;
11468 +       struct qstr *qname;
11469 +
11470 +       AuDebugOn(elm
11471 +                 && memcmp(elm, page_address(ZERO_PAGE(0)), sizeof(*elm)));
11472 +
11473 +       infopath.dentry = vfsub_lookup_one_len(w->whname, &w->h_ppath,
11474 +                                              w->whnamelen);
11475 +       AuTraceErrPtr(infopath.dentry);
11476 +       if (IS_ERR(infopath.dentry)) {
11477 +               err = PTR_ERR(infopath.dentry);
11478 +               goto out;
11479 +       }
11480 +
11481 +       err = 0;
11482 +       h_dir = d_inode(w->h_ppath.dentry);
11483 +       if (elm && d_is_negative(infopath.dentry)) {
11484 +               err = vfsub_create(h_dir, &infopath, 0600, /*want_excl*/true);
11485 +               AuTraceErr(err);
11486 +               if (unlikely(err))
11487 +                       goto out_dput;
11488 +               elm->created = 1;
11489 +               elm->info_dentry = dget(infopath.dentry);
11490 +       }
11491 +
11492 +       infofile = vfsub_dentry_open(&infopath, O_RDWR);
11493 +       AuTraceErrPtr(infofile);
11494 +       if (IS_ERR(infofile)) {
11495 +               err = PTR_ERR(infofile);
11496 +               goto out_dput;
11497 +       }
11498 +
11499 +       h_inode = d_inode(infopath.dentry);
11500 +       if (elm && i_size_read(h_inode)) {
11501 +               h_inode = d_inode(w->h_dentry);
11502 +               elm->info_last = au_drinfo_read_k(infofile, h_inode->i_ino);
11503 +               AuTraceErrPtr(elm->info_last);
11504 +               if (IS_ERR(elm->info_last)) {
11505 +                       err = PTR_ERR(elm->info_last);
11506 +                       elm->info_last = NULL;
11507 +                       AuDebugOn(elm->info_dentry);
11508 +                       goto out_fput;
11509 +               }
11510 +       }
11511 +
11512 +       if (elm && w->renameback) {
11513 +               delegated = NULL;
11514 +               err = vfsub_unlink(h_dir, &infopath, &delegated, /*force*/0);
11515 +               AuTraceErr(err);
11516 +               if (unlikely(err == -EWOULDBLOCK))
11517 +                       iput(delegated);
11518 +               goto out_fput;
11519 +       }
11520 +
11521 +       pos = 0;
11522 +       qname = &w->h_dentry->d_name;
11523 +       len = sizeof(*w->fdata) + qname->len;
11524 +       if (!elm)
11525 +               len = sizeof(*w->fdata) + w->fdata->drinfo.oldnamelen;
11526 +       ssz = vfsub_write_k(infofile, w->fdata, len, &pos);
11527 +       if (ssz == len) {
11528 +               AuDbg("hi%llu, %.*s\n", w->fdata->drinfo.ino,
11529 +                     w->fdata->drinfo.oldnamelen, w->fdata->drinfo.oldname);
11530 +               goto out_fput; /* success */
11531 +       } else {
11532 +               err = -EIO;
11533 +               if (ssz < 0)
11534 +                       err = ssz;
11535 +               /* the caller should revert it using @elm */
11536 +       }
11537 +
11538 +out_fput:
11539 +       fput(infofile);
11540 +out_dput:
11541 +       dput(infopath.dentry);
11542 +out:
11543 +       AuTraceErr(err);
11544 +       return err;
11545 +}
11546 +
11547 +struct au_call_drinfo_do_store_args {
11548 +       int *errp;
11549 +       struct au_drinfo_store *w;
11550 +       struct au_drinfo_rev_elm *elm;
11551 +};
11552 +
11553 +static void au_call_drinfo_do_store(void *args)
11554 +{
11555 +       struct au_call_drinfo_do_store_args *a = args;
11556 +
11557 +       *a->errp = au_drinfo_do_store(a->w, a->elm);
11558 +}
11559 +
11560 +static int au_drinfo_store_sio(struct au_drinfo_store *w,
11561 +                              struct au_drinfo_rev_elm *elm)
11562 +{
11563 +       int err, wkq_err;
11564 +
11565 +       if (w->no_sio)
11566 +               err = au_drinfo_do_store(w, elm);
11567 +       else {
11568 +               struct au_call_drinfo_do_store_args a = {
11569 +                       .errp   = &err,
11570 +                       .w      = w,
11571 +                       .elm    = elm
11572 +               };
11573 +               wkq_err = au_wkq_wait(au_call_drinfo_do_store, &a);
11574 +               if (unlikely(wkq_err))
11575 +                       err = wkq_err;
11576 +       }
11577 +       AuTraceErr(err);
11578 +
11579 +       return err;
11580 +}
11581 +
11582 +static int au_drinfo_store_work_init(struct au_drinfo_store *w,
11583 +                                    aufs_bindex_t btgt)
11584 +{
11585 +       int err;
11586 +
11587 +       memset(w, 0, sizeof(*w));
11588 +       w->allocated = roundup_pow_of_two(sizeof(*w->fdata) + 40);
11589 +       strcpy(w->whname, AUFS_WH_DR_INFO_PFX);
11590 +       w->infoname = w->whname + sizeof(AUFS_WH_DR_INFO_PFX) - 1;
11591 +       w->infonamelen = sizeof(w->whname) - sizeof(AUFS_WH_DR_INFO_PFX);
11592 +       w->btgt = btgt;
11593 +       w->no_sio = !!uid_eq(current_fsuid(), GLOBAL_ROOT_UID);
11594 +
11595 +       err = -ENOMEM;
11596 +       w->fdata = kcalloc(1, w->allocated, GFP_NOFS);
11597 +       if (unlikely(!w->fdata)) {
11598 +               AuTraceErr(err);
11599 +               goto out;
11600 +       }
11601 +       w->fdata->magic = (__force uint32_t)htonl(AUFS_DRINFO_MAGIC_V1);
11602 +       err = 0;
11603 +
11604 +out:
11605 +       return err;
11606 +}
11607 +
11608 +static void au_drinfo_store_work_fin(struct au_drinfo_store *w)
11609 +{
11610 +       au_kfree_rcu(w->fdata);
11611 +}
11612 +
11613 +static void au_drinfo_store_rev(struct au_drinfo_rev *rev,
11614 +                               struct au_drinfo_store *w)
11615 +{
11616 +       struct au_drinfo_rev_elm *elm;
11617 +       struct inode *h_dir, *delegated;
11618 +       int err, nelm;
11619 +       struct path infopath = {
11620 +               .mnt = w->h_ppath.mnt
11621 +       };
11622 +
11623 +       h_dir = d_inode(w->h_ppath.dentry);
11624 +       IMustLock(h_dir);
11625 +
11626 +       err = 0;
11627 +       elm = rev->elm;
11628 +       for (nelm = rev->nelm; nelm > 0; nelm--, elm++) {
11629 +               AuDebugOn(elm->created && elm->info_last);
11630 +               if (elm->created) {
11631 +                       AuDbg("here\n");
11632 +                       delegated = NULL;
11633 +                       infopath.dentry = elm->info_dentry;
11634 +                       err = vfsub_unlink(h_dir, &infopath, &delegated,
11635 +                                          !w->no_sio);
11636 +                       AuTraceErr(err);
11637 +                       if (unlikely(err == -EWOULDBLOCK))
11638 +                               iput(delegated);
11639 +                       dput(elm->info_dentry);
11640 +               } else if (elm->info_last) {
11641 +                       AuDbg("here\n");
11642 +                       w->fdata->drinfo = *elm->info_last;
11643 +                       memcpy(w->fdata->drinfo.oldname,
11644 +                              elm->info_last->oldname,
11645 +                              elm->info_last->oldnamelen);
11646 +                       err = au_drinfo_store_sio(w, /*elm*/NULL);
11647 +                       au_kfree_rcu(elm->info_last);
11648 +               }
11649 +               if (unlikely(err))
11650 +                       AuIOErr("%d, %s\n", err, w->whname);
11651 +               /* go on even if err */
11652 +       }
11653 +}
11654 +
11655 +/* caller has to call au_dr_rename_fin() later */
11656 +static int au_drinfo_store(struct dentry *dentry, aufs_bindex_t btgt,
11657 +                          struct qstr *dst_name, void *_rev)
11658 +{
11659 +       int err, sz, nelm;
11660 +       aufs_bindex_t bindex, btail;
11661 +       struct au_drinfo_store work;
11662 +       struct au_drinfo_rev *rev, **p;
11663 +       struct au_drinfo_rev_elm *elm;
11664 +       struct super_block *sb;
11665 +       struct au_branch *br;
11666 +       struct au_hinode *hdir;
11667 +
11668 +       err = au_drinfo_store_work_init(&work, btgt);
11669 +       AuTraceErr(err);
11670 +       if (unlikely(err))
11671 +               goto out;
11672 +
11673 +       err = -ENOMEM;
11674 +       btail = au_dbtaildir(dentry);
11675 +       nelm = btail - btgt;
11676 +       sz = sizeof(*rev) + sizeof(*elm) * nelm;
11677 +       rev = kcalloc(1, sz, GFP_NOFS);
11678 +       if (unlikely(!rev)) {
11679 +               AuTraceErr(err);
11680 +               goto out_args;
11681 +       }
11682 +       rev->nelm = nelm;
11683 +       elm = rev->elm;
11684 +       p = _rev;
11685 +       *p = rev;
11686 +
11687 +       err = 0;
11688 +       sb = dentry->d_sb;
11689 +       work.h_ppath.dentry = au_h_dptr(dentry, btgt);
11690 +       work.h_ppath.mnt = au_sbr_mnt(sb, btgt);
11691 +       hdir = au_hi(d_inode(dentry), btgt);
11692 +       au_hn_inode_lock_nested(hdir, AuLsc_I_CHILD);
11693 +       for (bindex = btgt + 1; bindex <= btail; bindex++, elm++) {
11694 +               work.h_dentry = au_h_dptr(dentry, bindex);
11695 +               if (!work.h_dentry)
11696 +                       continue;
11697 +
11698 +               err = au_drinfo_construct(&work.fdata, work.h_dentry,
11699 +                                         &work.allocated);
11700 +               AuTraceErr(err);
11701 +               if (unlikely(err))
11702 +                       break;
11703 +
11704 +               work.renameback = au_qstreq(&work.h_dentry->d_name, dst_name);
11705 +               br = au_sbr(sb, bindex);
11706 +               work.whnamelen = sizeof(AUFS_WH_DR_INFO_PFX) - 1;
11707 +               work.whnamelen += au_drinfo_name(br, work.infoname,
11708 +                                                work.infonamelen);
11709 +               AuDbg("whname %.*s, i%llu, %.*s\n",
11710 +                     work.whnamelen, work.whname,
11711 +                     be64_to_cpu((__force __be64)work.fdata->drinfo.ino),
11712 +                     work.fdata->drinfo.oldnamelen,
11713 +                     work.fdata->drinfo.oldname);
11714 +
11715 +               err = au_drinfo_store_sio(&work, elm);
11716 +               AuTraceErr(err);
11717 +               if (unlikely(err))
11718 +                       break;
11719 +       }
11720 +       if (unlikely(err)) {
11721 +               /* revert all drinfo */
11722 +               au_drinfo_store_rev(rev, &work);
11723 +               au_kfree_try_rcu(rev);
11724 +               *p = NULL;
11725 +       }
11726 +       au_hn_inode_unlock(hdir);
11727 +
11728 +out_args:
11729 +       au_drinfo_store_work_fin(&work);
11730 +out:
11731 +       return err;
11732 +}
11733 +
11734 +/* ---------------------------------------------------------------------- */
11735 +
11736 +int au_dr_rename(struct dentry *src, aufs_bindex_t bindex,
11737 +                struct qstr *dst_name, void *_rev)
11738 +{
11739 +       int err, already;
11740 +       ino_t ino;
11741 +       struct super_block *sb;
11742 +       struct au_branch *br;
11743 +       struct au_dr_br *dr;
11744 +       struct dentry *h_dentry;
11745 +       struct inode *h_inode;
11746 +       struct au_dr_hino *ent;
11747 +       struct au_drinfo_rev *rev, **p;
11748 +
11749 +       AuDbg("bindex %d\n", bindex);
11750 +
11751 +       err = -ENOMEM;
11752 +       ent = kmalloc(sizeof(*ent), GFP_NOFS);
11753 +       if (unlikely(!ent))
11754 +               goto out;
11755 +
11756 +       sb = src->d_sb;
11757 +       br = au_sbr(sb, bindex);
11758 +       dr = &br->br_dirren;
11759 +       h_dentry = au_h_dptr(src, bindex);
11760 +       h_inode = d_inode(h_dentry);
11761 +       ino = h_inode->i_ino;
11762 +       ent->dr_h_ino = ino;
11763 +       already = au_dr_hino_test_add(dr, ino, ent);
11764 +       AuDbg("b%d, hi%llu, already %d\n",
11765 +             bindex, (unsigned long long)ino, already);
11766 +
11767 +       err = au_drinfo_store(src, bindex, dst_name, _rev);
11768 +       AuTraceErr(err);
11769 +       if (!err) {
11770 +               p = _rev;
11771 +               rev = *p;
11772 +               rev->already = already;
11773 +               goto out; /* success */
11774 +       }
11775 +
11776 +       /* revert */
11777 +       if (!already)
11778 +               au_dr_hino_del(dr, ent);
11779 +       au_kfree_rcu(ent);
11780 +
11781 +out:
11782 +       AuTraceErr(err);
11783 +       return err;
11784 +}
11785 +
11786 +void au_dr_rename_fin(struct dentry *src, aufs_bindex_t btgt, void *_rev)
11787 +{
11788 +       struct au_drinfo_rev *rev;
11789 +       struct au_drinfo_rev_elm *elm;
11790 +       int nelm;
11791 +
11792 +       rev = _rev;
11793 +       elm = rev->elm;
11794 +       for (nelm = rev->nelm; nelm > 0; nelm--, elm++) {
11795 +               dput(elm->info_dentry);
11796 +               au_kfree_rcu(elm->info_last);
11797 +       }
11798 +       au_kfree_try_rcu(rev);
11799 +}
11800 +
11801 +void au_dr_rename_rev(struct dentry *src, aufs_bindex_t btgt, void *_rev)
11802 +{
11803 +       int err;
11804 +       struct au_drinfo_store work;
11805 +       struct au_drinfo_rev *rev = _rev;
11806 +       struct super_block *sb;
11807 +       struct au_branch *br;
11808 +       struct inode *h_inode;
11809 +       struct au_dr_br *dr;
11810 +       struct au_dr_hino *ent;
11811 +
11812 +       err = au_drinfo_store_work_init(&work, btgt);
11813 +       if (unlikely(err))
11814 +               goto out;
11815 +
11816 +       sb = src->d_sb;
11817 +       br = au_sbr(sb, btgt);
11818 +       work.h_ppath.dentry = au_h_dptr(src, btgt);
11819 +       work.h_ppath.mnt = au_br_mnt(br);
11820 +       au_drinfo_store_rev(rev, &work);
11821 +       au_drinfo_store_work_fin(&work);
11822 +       if (rev->already)
11823 +               goto out;
11824 +
11825 +       dr = &br->br_dirren;
11826 +       h_inode = d_inode(work.h_ppath.dentry);
11827 +       ent = au_dr_hino_find(dr, h_inode->i_ino);
11828 +       BUG_ON(!ent);
11829 +       au_dr_hino_del(dr, ent);
11830 +       au_kfree_rcu(ent);
11831 +
11832 +out:
11833 +       au_kfree_try_rcu(rev);
11834 +       if (unlikely(err))
11835 +               pr_err("failed to remove dirren info\n");
11836 +}
11837 +
11838 +/* ---------------------------------------------------------------------- */
11839 +
11840 +static struct au_drinfo *au_drinfo_do_load(struct path *h_ppath,
11841 +                                          char *whname, int whnamelen,
11842 +                                          struct dentry **info_dentry)
11843 +{
11844 +       struct au_drinfo *drinfo;
11845 +       struct file *f;
11846 +       struct inode *h_dir;
11847 +       struct path infopath;
11848 +       int unlocked;
11849 +
11850 +       AuDbg("%pd/%.*s\n", h_ppath->dentry, whnamelen, whname);
11851 +
11852 +       *info_dentry = NULL;
11853 +       drinfo = NULL;
11854 +       unlocked = 0;
11855 +       h_dir = d_inode(h_ppath->dentry);
11856 +       inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
11857 +       infopath.dentry = vfsub_lookup_one_len(whname, h_ppath, whnamelen);
11858 +       if (IS_ERR(infopath.dentry)) {
11859 +               drinfo = (void *)infopath.dentry;
11860 +               goto out;
11861 +       }
11862 +
11863 +       if (d_is_negative(infopath.dentry))
11864 +               goto out_dput; /* success */
11865 +
11866 +       infopath.mnt = h_ppath->mnt;
11867 +       f = vfsub_dentry_open(&infopath, O_RDONLY);
11868 +       inode_unlock_shared(h_dir);
11869 +       unlocked = 1;
11870 +       if (IS_ERR(f)) {
11871 +               drinfo = (void *)f;
11872 +               goto out_dput;
11873 +       }
11874 +
11875 +       drinfo = au_drinfo_read_k(f, /*h_ino*/0);
11876 +       if (IS_ERR_OR_NULL(drinfo))
11877 +               goto out_fput;
11878 +
11879 +       AuDbg("oldname %.*s\n", drinfo->oldnamelen, drinfo->oldname);
11880 +       *info_dentry = dget(infopath.dentry); /* keep it alive */
11881 +
11882 +out_fput:
11883 +       fput(f);
11884 +out_dput:
11885 +       dput(infopath.dentry);
11886 +out:
11887 +       if (!unlocked)
11888 +               inode_unlock_shared(h_dir);
11889 +       AuTraceErrPtr(drinfo);
11890 +       return drinfo;
11891 +}
11892 +
11893 +struct au_drinfo_do_load_args {
11894 +       struct au_drinfo **drinfop;
11895 +       struct path *h_ppath;
11896 +       char *whname;
11897 +       int whnamelen;
11898 +       struct dentry **info_dentry;
11899 +};
11900 +
11901 +static void au_call_drinfo_do_load(void *args)
11902 +{
11903 +       struct au_drinfo_do_load_args *a = args;
11904 +
11905 +       *a->drinfop = au_drinfo_do_load(a->h_ppath, a->whname, a->whnamelen,
11906 +                                       a->info_dentry);
11907 +}
11908 +
11909 +struct au_drinfo_load {
11910 +       struct path h_ppath;
11911 +       struct qstr *qname;
11912 +       unsigned char no_sio;
11913 +
11914 +       aufs_bindex_t ninfo;
11915 +       struct au_drinfo **drinfo;
11916 +};
11917 +
11918 +static int au_drinfo_load(struct au_drinfo_load *w, aufs_bindex_t bindex,
11919 +                         struct au_branch *br)
11920 +{
11921 +       int err, wkq_err, whnamelen, e;
11922 +       char whname[sizeof(AUFS_WH_DR_INFO_PFX) + AUFS_DIRREN_ENV_VAL_SZ]
11923 +               = AUFS_WH_DR_INFO_PFX;
11924 +       struct au_drinfo *drinfo;
11925 +       struct qstr oldname;
11926 +       struct inode *h_dir, *delegated;
11927 +       struct dentry *info_dentry;
11928 +       struct path infopath;
11929 +
11930 +       whnamelen = sizeof(AUFS_WH_DR_INFO_PFX) - 1;
11931 +       whnamelen += au_drinfo_name(br, whname + whnamelen,
11932 +                                   sizeof(whname) - whnamelen);
11933 +       if (w->no_sio)
11934 +               drinfo = au_drinfo_do_load(&w->h_ppath, whname, whnamelen,
11935 +                                          &info_dentry);
11936 +       else {
11937 +               struct au_drinfo_do_load_args args = {
11938 +                       .drinfop        = &drinfo,
11939 +                       .h_ppath        = &w->h_ppath,
11940 +                       .whname         = whname,
11941 +                       .whnamelen      = whnamelen,
11942 +                       .info_dentry    = &info_dentry
11943 +               };
11944 +               wkq_err = au_wkq_wait(au_call_drinfo_do_load, &args);
11945 +               if (unlikely(wkq_err))
11946 +                       drinfo = ERR_PTR(wkq_err);
11947 +       }
11948 +       err = PTR_ERR(drinfo);
11949 +       if (IS_ERR_OR_NULL(drinfo))
11950 +               goto out;
11951 +
11952 +       err = 0;
11953 +       oldname.len = drinfo->oldnamelen;
11954 +       oldname.name = drinfo->oldname;
11955 +       if (au_qstreq(w->qname, &oldname)) {
11956 +               /* the name is renamed back */
11957 +               au_kfree_rcu(drinfo);
11958 +               drinfo = NULL;
11959 +
11960 +               infopath.dentry = info_dentry;
11961 +               infopath.mnt = w->h_ppath.mnt;
11962 +               h_dir = d_inode(w->h_ppath.dentry);
11963 +               delegated = NULL;
11964 +               inode_lock_nested(h_dir, AuLsc_I_PARENT);
11965 +               e = vfsub_unlink(h_dir, &infopath, &delegated, !w->no_sio);
11966 +               inode_unlock(h_dir);
11967 +               if (unlikely(e))
11968 +                       AuIOErr("ignored %d, %pd2\n", e, &infopath.dentry);
11969 +               if (unlikely(e == -EWOULDBLOCK))
11970 +                       iput(delegated);
11971 +       }
11972 +       au_kfree_rcu(w->drinfo[bindex]);
11973 +       w->drinfo[bindex] = drinfo;
11974 +       dput(info_dentry);
11975 +
11976 +out:
11977 +       AuTraceErr(err);
11978 +       return err;
11979 +}
11980 +
11981 +/* ---------------------------------------------------------------------- */
11982 +
11983 +static void au_dr_lkup_free(struct au_drinfo **drinfo, int n)
11984 +{
11985 +       struct au_drinfo **p = drinfo;
11986 +
11987 +       while (n-- > 0)
11988 +               au_kfree_rcu(*drinfo++);
11989 +       au_kfree_try_rcu(p);
11990 +}
11991 +
11992 +int au_dr_lkup(struct au_do_lookup_args *lkup, struct dentry *dentry,
11993 +              aufs_bindex_t btgt)
11994 +{
11995 +       int err, ninfo;
11996 +       struct au_drinfo_load w;
11997 +       aufs_bindex_t bindex, bbot;
11998 +       struct au_branch *br;
11999 +       struct inode *h_dir;
12000 +       struct au_dr_hino *ent;
12001 +       struct super_block *sb;
12002 +
12003 +       AuDbg("%.*s, name %.*s, whname %.*s, b%d\n",
12004 +             AuLNPair(&dentry->d_name), AuLNPair(&lkup->dirren.dr_name),
12005 +             AuLNPair(&lkup->whname), btgt);
12006 +
12007 +       sb = dentry->d_sb;
12008 +       bbot = au_sbbot(sb);
12009 +       w.ninfo = bbot + 1;
12010 +       if (!lkup->dirren.drinfo) {
12011 +               lkup->dirren.drinfo = kcalloc(w.ninfo,
12012 +                                             sizeof(*lkup->dirren.drinfo),
12013 +                                             GFP_NOFS);
12014 +               if (unlikely(!lkup->dirren.drinfo)) {
12015 +                       err = -ENOMEM;
12016 +                       goto out;
12017 +               }
12018 +               lkup->dirren.ninfo = w.ninfo;
12019 +       }
12020 +       w.drinfo = lkup->dirren.drinfo;
12021 +       w.no_sio = !!uid_eq(current_fsuid(), GLOBAL_ROOT_UID);
12022 +       w.h_ppath.dentry = au_h_dptr(dentry, btgt);
12023 +       AuDebugOn(!w.h_ppath.dentry);
12024 +       w.h_ppath.mnt = au_sbr_mnt(sb, btgt);
12025 +       w.qname = &dentry->d_name;
12026 +
12027 +       ninfo = 0;
12028 +       for (bindex = btgt + 1; bindex <= bbot; bindex++) {
12029 +               br = au_sbr(sb, bindex);
12030 +               err = au_drinfo_load(&w, bindex, br);
12031 +               if (unlikely(err))
12032 +                       goto out_free;
12033 +               if (w.drinfo[bindex])
12034 +                       ninfo++;
12035 +       }
12036 +       if (!ninfo) {
12037 +               br = au_sbr(sb, btgt);
12038 +               h_dir = d_inode(w.h_ppath.dentry);
12039 +               ent = au_dr_hino_find(&br->br_dirren, h_dir->i_ino);
12040 +               AuDebugOn(!ent);
12041 +               au_dr_hino_del(&br->br_dirren, ent);
12042 +               au_kfree_rcu(ent);
12043 +       }
12044 +       goto out; /* success */
12045 +
12046 +out_free:
12047 +       au_dr_lkup_free(lkup->dirren.drinfo, lkup->dirren.ninfo);
12048 +       lkup->dirren.ninfo = 0;
12049 +       lkup->dirren.drinfo = NULL;
12050 +out:
12051 +       AuTraceErr(err);
12052 +       return err;
12053 +}
12054 +
12055 +void au_dr_lkup_fin(struct au_do_lookup_args *lkup)
12056 +{
12057 +       au_dr_lkup_free(lkup->dirren.drinfo, lkup->dirren.ninfo);
12058 +}
12059 +
12060 +int au_dr_lkup_name(struct au_do_lookup_args *lkup, aufs_bindex_t btgt)
12061 +{
12062 +       int err;
12063 +       struct au_drinfo *drinfo;
12064 +
12065 +       err = 0;
12066 +       if (!lkup->dirren.drinfo)
12067 +               goto out;
12068 +       AuDebugOn(lkup->dirren.ninfo <= btgt);
12069 +       drinfo = lkup->dirren.drinfo[btgt];
12070 +       if (!drinfo)
12071 +               goto out;
12072 +
12073 +       au_kfree_try_rcu(lkup->whname.name);
12074 +       lkup->whname.name = NULL;
12075 +       lkup->dirren.dr_name.len = drinfo->oldnamelen;
12076 +       lkup->dirren.dr_name.name = drinfo->oldname;
12077 +       lkup->name = &lkup->dirren.dr_name;
12078 +       err = au_wh_name_alloc(&lkup->whname, lkup->name);
12079 +       if (!err)
12080 +               AuDbg("name %.*s, whname %.*s, b%d\n",
12081 +                     AuLNPair(lkup->name), AuLNPair(&lkup->whname),
12082 +                     btgt);
12083 +
12084 +out:
12085 +       AuTraceErr(err);
12086 +       return err;
12087 +}
12088 +
12089 +int au_dr_lkup_h_ino(struct au_do_lookup_args *lkup, aufs_bindex_t bindex,
12090 +                    ino_t h_ino)
12091 +{
12092 +       int match;
12093 +       struct au_drinfo *drinfo;
12094 +
12095 +       match = 1;
12096 +       if (!lkup->dirren.drinfo)
12097 +               goto out;
12098 +       AuDebugOn(lkup->dirren.ninfo <= bindex);
12099 +       drinfo = lkup->dirren.drinfo[bindex];
12100 +       if (!drinfo)
12101 +               goto out;
12102 +
12103 +       match = (drinfo->ino == h_ino);
12104 +       AuDbg("match %d\n", match);
12105 +
12106 +out:
12107 +       return match;
12108 +}
12109 +
12110 +/* ---------------------------------------------------------------------- */
12111 +
12112 +int au_dr_opt_set(struct super_block *sb)
12113 +{
12114 +       int err;
12115 +       aufs_bindex_t bindex, bbot;
12116 +       struct au_branch *br;
12117 +
12118 +       err = 0;
12119 +       bbot = au_sbbot(sb);
12120 +       for (bindex = 0; !err && bindex <= bbot; bindex++) {
12121 +               br = au_sbr(sb, bindex);
12122 +               err = au_dr_hino(sb, bindex, /*br*/NULL, &br->br_path);
12123 +       }
12124 +
12125 +       return err;
12126 +}
12127 +
12128 +int au_dr_opt_flush(struct super_block *sb)
12129 +{
12130 +       int err;
12131 +       aufs_bindex_t bindex, bbot;
12132 +       struct au_branch *br;
12133 +
12134 +       err = 0;
12135 +       bbot = au_sbbot(sb);
12136 +       for (bindex = 0; !err && bindex <= bbot; bindex++) {
12137 +               br = au_sbr(sb, bindex);
12138 +               if (au_br_writable(br->br_perm))
12139 +                       err = au_dr_hino(sb, bindex, /*br*/NULL, /*path*/NULL);
12140 +       }
12141 +
12142 +       return err;
12143 +}
12144 +
12145 +int au_dr_opt_clr(struct super_block *sb, int no_flush)
12146 +{
12147 +       int err;
12148 +       aufs_bindex_t bindex, bbot;
12149 +       struct au_branch *br;
12150 +
12151 +       err = 0;
12152 +       if (!no_flush) {
12153 +               err = au_dr_opt_flush(sb);
12154 +               if (unlikely(err))
12155 +                       goto out;
12156 +       }
12157 +
12158 +       bbot = au_sbbot(sb);
12159 +       for (bindex = 0; bindex <= bbot; bindex++) {
12160 +               br = au_sbr(sb, bindex);
12161 +               au_dr_hino_free(&br->br_dirren);
12162 +       }
12163 +
12164 +out:
12165 +       return err;
12166 +}
12167 diff -urN /usr/share/empty/fs/aufs/dirren.h linux/fs/aufs/dirren.h
12168 --- /usr/share/empty/fs/aufs/dirren.h   1970-01-01 01:00:00.000000000 +0100
12169 +++ linux/fs/aufs/dirren.h      2022-03-21 14:49:05.723299677 +0100
12170 @@ -0,0 +1,140 @@
12171 +/* SPDX-License-Identifier: GPL-2.0 */
12172 +/*
12173 + * Copyright (C) 2017-2021 Junjiro R. Okajima
12174 + *
12175 + * This program, aufs is free software; you can redistribute it and/or modify
12176 + * it under the terms of the GNU General Public License as published by
12177 + * the Free Software Foundation; either version 2 of the License, or
12178 + * (at your option) any later version.
12179 + *
12180 + * This program is distributed in the hope that it will be useful,
12181 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12182 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12183 + * GNU General Public License for more details.
12184 + *
12185 + * You should have received a copy of the GNU General Public License
12186 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
12187 + */
12188 +
12189 +/*
12190 + * renamed dir info
12191 + */
12192 +
12193 +#ifndef __AUFS_DIRREN_H__
12194 +#define __AUFS_DIRREN_H__
12195 +
12196 +#ifdef __KERNEL__
12197 +
12198 +#include <linux/dcache.h>
12199 +#include <linux/statfs.h>
12200 +#include <linux/uuid.h>
12201 +#include "hbl.h"
12202 +
12203 +#define AuDirren_NHASH 100
12204 +
12205 +#ifdef CONFIG_AUFS_DIRREN
12206 +enum au_brid_type {
12207 +       AuBrid_Unset,
12208 +       AuBrid_UUID,
12209 +       AuBrid_FSID,
12210 +       AuBrid_DEV
12211 +};
12212 +
12213 +struct au_dr_brid {
12214 +       enum au_brid_type       type;
12215 +       union {
12216 +               uuid_t  uuid;   /* unimplemented yet */
12217 +               fsid_t  fsid;
12218 +               dev_t   dev;
12219 +       };
12220 +};
12221 +
12222 +/* 20 is the max digits length of ulong 64 */
12223 +/* brid-type "_" uuid "_" inum */
12224 +#define AUFS_DIRREN_FNAME_SZ   (1 + 1 + UUID_STRING_LEN + 20)
12225 +#define AUFS_DIRREN_ENV_VAL_SZ (AUFS_DIRREN_FNAME_SZ + 1 + 20)
12226 +
12227 +struct au_dr_hino {
12228 +       struct hlist_bl_node    dr_hnode;
12229 +       ino_t                   dr_h_ino;
12230 +};
12231 +
12232 +struct au_dr_br {
12233 +       struct hlist_bl_head    dr_h_ino[AuDirren_NHASH];
12234 +       struct au_dr_brid       dr_brid;
12235 +};
12236 +
12237 +struct au_dr_lookup {
12238 +       /* dr_name is pointed by struct au_do_lookup_args.name */
12239 +       struct qstr             dr_name; /* subset of dr_info */
12240 +       aufs_bindex_t           ninfo;
12241 +       struct au_drinfo        **drinfo;
12242 +};
12243 +#else
12244 +struct au_dr_hino;
12245 +/* empty */
12246 +struct au_dr_br { };
12247 +struct au_dr_lookup { };
12248 +#endif
12249 +
12250 +/* ---------------------------------------------------------------------- */
12251 +
12252 +struct au_branch;
12253 +struct au_do_lookup_args;
12254 +struct au_hinode;
12255 +#ifdef CONFIG_AUFS_DIRREN
12256 +int au_dr_hino_test_add(struct au_dr_br *dr, ino_t h_ino,
12257 +                       struct au_dr_hino *add_ent);
12258 +void au_dr_hino_free(struct au_dr_br *dr);
12259 +int au_dr_br_init(struct super_block *sb, struct au_branch *br,
12260 +                 const struct path *path);
12261 +int au_dr_br_fin(struct super_block *sb, struct au_branch *br);
12262 +int au_dr_rename(struct dentry *src, aufs_bindex_t bindex,
12263 +                struct qstr *dst_name, void *_rev);
12264 +void au_dr_rename_fin(struct dentry *src, aufs_bindex_t btgt, void *rev);
12265 +void au_dr_rename_rev(struct dentry *src, aufs_bindex_t bindex, void *rev);
12266 +int au_dr_lkup(struct au_do_lookup_args *lkup, struct dentry *dentry,
12267 +              aufs_bindex_t bindex);
12268 +int au_dr_lkup_name(struct au_do_lookup_args *lkup, aufs_bindex_t btgt);
12269 +int au_dr_lkup_h_ino(struct au_do_lookup_args *lkup, aufs_bindex_t bindex,
12270 +                    ino_t h_ino);
12271 +void au_dr_lkup_fin(struct au_do_lookup_args *lkup);
12272 +int au_dr_opt_set(struct super_block *sb);
12273 +int au_dr_opt_flush(struct super_block *sb);
12274 +int au_dr_opt_clr(struct super_block *sb, int no_flush);
12275 +#else
12276 +AuStubInt0(au_dr_hino_test_add, struct au_dr_br *dr, ino_t h_ino,
12277 +          struct au_dr_hino *add_ent);
12278 +AuStubVoid(au_dr_hino_free, struct au_dr_br *dr);
12279 +AuStubInt0(au_dr_br_init, struct super_block *sb, struct au_branch *br,
12280 +          const struct path *path);
12281 +AuStubInt0(au_dr_br_fin, struct super_block *sb, struct au_branch *br);
12282 +AuStubInt0(au_dr_rename, struct dentry *src, aufs_bindex_t bindex,
12283 +          struct qstr *dst_name, void *_rev);
12284 +AuStubVoid(au_dr_rename_fin, struct dentry *src, aufs_bindex_t btgt, void *rev);
12285 +AuStubVoid(au_dr_rename_rev, struct dentry *src, aufs_bindex_t bindex,
12286 +          void *rev);
12287 +AuStubInt0(au_dr_lkup, struct au_do_lookup_args *lkup, struct dentry *dentry,
12288 +          aufs_bindex_t bindex);
12289 +AuStubInt0(au_dr_lkup_name, struct au_do_lookup_args *lkup, aufs_bindex_t btgt);
12290 +AuStubInt0(au_dr_lkup_h_ino, struct au_do_lookup_args *lkup,
12291 +          aufs_bindex_t bindex, ino_t h_ino);
12292 +AuStubVoid(au_dr_lkup_fin, struct au_do_lookup_args *lkup);
12293 +AuStubInt0(au_dr_opt_set, struct super_block *sb);
12294 +AuStubInt0(au_dr_opt_flush, struct super_block *sb);
12295 +AuStubInt0(au_dr_opt_clr, struct super_block *sb, int no_flush);
12296 +#endif
12297 +
12298 +/* ---------------------------------------------------------------------- */
12299 +
12300 +#ifdef CONFIG_AUFS_DIRREN
12301 +static inline int au_dr_ihash(ino_t h_ino)
12302 +{
12303 +       return h_ino % AuDirren_NHASH;
12304 +}
12305 +#else
12306 +AuStubInt0(au_dr_ihash, ino_t h_ino);
12307 +#endif
12308 +
12309 +#endif /* __KERNEL__ */
12310 +#endif /* __AUFS_DIRREN_H__ */
12311 diff -urN /usr/share/empty/fs/aufs/dynop.c linux/fs/aufs/dynop.c
12312 --- /usr/share/empty/fs/aufs/dynop.c    1970-01-01 01:00:00.000000000 +0100
12313 +++ linux/fs/aufs/dynop.c       2022-03-21 14:49:05.723299677 +0100
12314 @@ -0,0 +1,368 @@
12315 +// SPDX-License-Identifier: GPL-2.0
12316 +/*
12317 + * Copyright (C) 2010-2021 Junjiro R. Okajima
12318 + *
12319 + * This program, aufs is free software; you can redistribute it and/or modify
12320 + * it under the terms of the GNU General Public License as published by
12321 + * the Free Software Foundation; either version 2 of the License, or
12322 + * (at your option) any later version.
12323 + *
12324 + * This program is distributed in the hope that it will be useful,
12325 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12326 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12327 + * GNU General Public License for more details.
12328 + *
12329 + * You should have received a copy of the GNU General Public License
12330 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
12331 + */
12332 +
12333 +/*
12334 + * dynamically customizable operations for regular files
12335 + */
12336 +
12337 +#include "aufs.h"
12338 +
12339 +#define DyPrSym(key)   AuDbgSym(key->dk_op.dy_hop)
12340 +
12341 +/*
12342 + * How large will these lists be?
12343 + * Usually just a few elements, 20-30 at most for each, I guess.
12344 + */
12345 +static struct hlist_bl_head dynop[AuDyLast];
12346 +
12347 +static struct au_dykey *dy_gfind_get(struct hlist_bl_head *hbl,
12348 +                                    const void *h_op)
12349 +{
12350 +       struct au_dykey *key, *tmp;
12351 +       struct hlist_bl_node *pos;
12352 +
12353 +       key = NULL;
12354 +       hlist_bl_lock(hbl);
12355 +       hlist_bl_for_each_entry(tmp, pos, hbl, dk_hnode)
12356 +               if (tmp->dk_op.dy_hop == h_op) {
12357 +                       if (kref_get_unless_zero(&tmp->dk_kref))
12358 +                               key = tmp;
12359 +                       break;
12360 +               }
12361 +       hlist_bl_unlock(hbl);
12362 +
12363 +       return key;
12364 +}
12365 +
12366 +static struct au_dykey *dy_bradd(struct au_branch *br, struct au_dykey *key)
12367 +{
12368 +       struct au_dykey **k, *found;
12369 +       const void *h_op = key->dk_op.dy_hop;
12370 +       int i;
12371 +
12372 +       found = NULL;
12373 +       k = br->br_dykey;
12374 +       for (i = 0; i < AuBrDynOp; i++)
12375 +               if (k[i]) {
12376 +                       if (k[i]->dk_op.dy_hop == h_op) {
12377 +                               found = k[i];
12378 +                               break;
12379 +                       }
12380 +               } else
12381 +                       break;
12382 +       if (!found) {
12383 +               spin_lock(&br->br_dykey_lock);
12384 +               for (; i < AuBrDynOp; i++)
12385 +                       if (k[i]) {
12386 +                               if (k[i]->dk_op.dy_hop == h_op) {
12387 +                                       found = k[i];
12388 +                                       break;
12389 +                               }
12390 +                       } else {
12391 +                               k[i] = key;
12392 +                               break;
12393 +                       }
12394 +               spin_unlock(&br->br_dykey_lock);
12395 +               BUG_ON(i == AuBrDynOp); /* expand the array */
12396 +       }
12397 +
12398 +       return found;
12399 +}
12400 +
12401 +/* kref_get() if @key is already added */
12402 +static struct au_dykey *dy_gadd(struct hlist_bl_head *hbl, struct au_dykey *key)
12403 +{
12404 +       struct au_dykey *tmp, *found;
12405 +       struct hlist_bl_node *pos;
12406 +       const void *h_op = key->dk_op.dy_hop;
12407 +
12408 +       found = NULL;
12409 +       hlist_bl_lock(hbl);
12410 +       hlist_bl_for_each_entry(tmp, pos, hbl, dk_hnode)
12411 +               if (tmp->dk_op.dy_hop == h_op) {
12412 +                       if (kref_get_unless_zero(&tmp->dk_kref))
12413 +                               found = tmp;
12414 +                       break;
12415 +               }
12416 +       if (!found)
12417 +               hlist_bl_add_head(&key->dk_hnode, hbl);
12418 +       hlist_bl_unlock(hbl);
12419 +
12420 +       if (!found)
12421 +               DyPrSym(key);
12422 +       return found;
12423 +}
12424 +
12425 +static void dy_free_rcu(struct rcu_head *rcu)
12426 +{
12427 +       struct au_dykey *key;
12428 +
12429 +       key = container_of(rcu, struct au_dykey, dk_rcu);
12430 +       DyPrSym(key);
12431 +       kfree(key);
12432 +}
12433 +
12434 +static void dy_free(struct kref *kref)
12435 +{
12436 +       struct au_dykey *key;
12437 +       struct hlist_bl_head *hbl;
12438 +
12439 +       key = container_of(kref, struct au_dykey, dk_kref);
12440 +       hbl = dynop + key->dk_op.dy_type;
12441 +       au_hbl_del(&key->dk_hnode, hbl);
12442 +       call_rcu(&key->dk_rcu, dy_free_rcu);
12443 +}
12444 +
12445 +void au_dy_put(struct au_dykey *key)
12446 +{
12447 +       kref_put(&key->dk_kref, dy_free);
12448 +}
12449 +
12450 +/* ---------------------------------------------------------------------- */
12451 +
12452 +#define DyDbgSize(cnt, op)     AuDebugOn(cnt != sizeof(op)/sizeof(void *))
12453 +
12454 +#ifdef CONFIG_AUFS_DEBUG
12455 +#define DyDbgDeclare(cnt)      unsigned int cnt = 0
12456 +#define DyDbgInc(cnt)          do { cnt++; } while (0)
12457 +#else
12458 +#define DyDbgDeclare(cnt)      do {} while (0)
12459 +#define DyDbgInc(cnt)          do {} while (0)
12460 +#endif
12461 +
12462 +#define DySet(func, dst, src, h_op, h_sb) do {                         \
12463 +       DyDbgInc(cnt);                                                  \
12464 +       if (h_op->func) {                                               \
12465 +               if (src.func)                                           \
12466 +                       dst.func = src.func;                            \
12467 +               else                                                    \
12468 +                       AuDbg("%s %s\n", au_sbtype(h_sb), #func);       \
12469 +       }                                                               \
12470 +} while (0)
12471 +
12472 +#define DySetForce(func, dst, src) do {                \
12473 +       AuDebugOn(!src.func);                   \
12474 +       DyDbgInc(cnt);                          \
12475 +       dst.func = src.func;                    \
12476 +} while (0)
12477 +
12478 +#define DySetAop(func) \
12479 +       DySet(func, dyaop->da_op, aufs_aop, h_aop, h_sb)
12480 +#define DySetAopForce(func) \
12481 +       DySetForce(func, dyaop->da_op, aufs_aop)
12482 +
12483 +static void dy_aop(struct au_dykey *key, const void *h_op,
12484 +                  struct super_block *h_sb __maybe_unused)
12485 +{
12486 +       struct au_dyaop *dyaop = (void *)key;
12487 +       const struct address_space_operations *h_aop = h_op;
12488 +       DyDbgDeclare(cnt);
12489 +
12490 +       AuDbg("%s\n", au_sbtype(h_sb));
12491 +
12492 +       DySetAop(writepage);
12493 +       DySetAopForce(readpage);        /* force */
12494 +       DySetAop(writepages);
12495 +       DySetAop(set_page_dirty);
12496 +       DySetAop(readpages);
12497 +       DySetAop(readahead);
12498 +       DySetAop(write_begin);
12499 +       DySetAop(write_end);
12500 +       DySetAop(bmap);
12501 +       DySetAop(invalidatepage);
12502 +       DySetAop(releasepage);
12503 +       DySetAop(freepage);
12504 +       /* this one will be changed according to an aufs mount option */
12505 +       DySetAop(direct_IO);
12506 +       DySetAop(migratepage);
12507 +       DySetAop(isolate_page);
12508 +       DySetAop(putback_page);
12509 +       DySetAop(launder_page);
12510 +       DySetAop(is_partially_uptodate);
12511 +       DySetAop(is_dirty_writeback);
12512 +       DySetAop(error_remove_page);
12513 +       DySetAop(swap_activate);
12514 +       DySetAop(swap_deactivate);
12515 +
12516 +       DyDbgSize(cnt, *h_aop);
12517 +}
12518 +
12519 +/* ---------------------------------------------------------------------- */
12520 +
12521 +static void dy_bug(struct kref *kref)
12522 +{
12523 +       BUG();
12524 +}
12525 +
12526 +static struct au_dykey *dy_get(struct au_dynop *op, struct au_branch *br)
12527 +{
12528 +       struct au_dykey *key, *old;
12529 +       struct hlist_bl_head *hbl;
12530 +       struct op {
12531 +               unsigned int sz;
12532 +               void (*set)(struct au_dykey *key, const void *h_op,
12533 +                           struct super_block *h_sb __maybe_unused);
12534 +       };
12535 +       static const struct op a[] = {
12536 +               [AuDy_AOP] = {
12537 +                       .sz     = sizeof(struct au_dyaop),
12538 +                       .set    = dy_aop
12539 +               }
12540 +       };
12541 +       const struct op *p;
12542 +
12543 +       hbl = dynop + op->dy_type;
12544 +       key = dy_gfind_get(hbl, op->dy_hop);
12545 +       if (key)
12546 +               goto out_add; /* success */
12547 +
12548 +       p = a + op->dy_type;
12549 +       key = kzalloc(p->sz, GFP_NOFS);
12550 +       if (unlikely(!key)) {
12551 +               key = ERR_PTR(-ENOMEM);
12552 +               goto out;
12553 +       }
12554 +
12555 +       key->dk_op.dy_hop = op->dy_hop;
12556 +       kref_init(&key->dk_kref);
12557 +       p->set(key, op->dy_hop, au_br_sb(br));
12558 +       old = dy_gadd(hbl, key);
12559 +       if (old) {
12560 +               au_kfree_rcu(key);
12561 +               key = old;
12562 +       }
12563 +
12564 +out_add:
12565 +       old = dy_bradd(br, key);
12566 +       if (old)
12567 +               /* its ref-count should never be zero here */
12568 +               kref_put(&key->dk_kref, dy_bug);
12569 +out:
12570 +       return key;
12571 +}
12572 +
12573 +/* ---------------------------------------------------------------------- */
12574 +/*
12575 + * Aufs prohibits O_DIRECT by default even if the branch supports it.
12576 + * This behaviour is necessary to return an error from open(O_DIRECT) instead
12577 + * of the succeeding I/O. The dio mount option enables O_DIRECT and makes
12578 + * open(O_DIRECT) always succeed, but the succeeding I/O may return an error.
12579 + * See the aufs manual in detail.
12580 + */
12581 +static void dy_adx(struct au_dyaop *dyaop, int do_dx)
12582 +{
12583 +       if (!do_dx)
12584 +               dyaop->da_op.direct_IO = NULL;
12585 +       else
12586 +               dyaop->da_op.direct_IO = aufs_aop.direct_IO;
12587 +}
12588 +
12589 +static struct au_dyaop *dy_aget(struct au_branch *br,
12590 +                               const struct address_space_operations *h_aop,
12591 +                               int do_dx)
12592 +{
12593 +       struct au_dyaop *dyaop;
12594 +       struct au_dynop op;
12595 +
12596 +       op.dy_type = AuDy_AOP;
12597 +       op.dy_haop = h_aop;
12598 +       dyaop = (void *)dy_get(&op, br);
12599 +       if (IS_ERR(dyaop))
12600 +               goto out;
12601 +       dy_adx(dyaop, do_dx);
12602 +
12603 +out:
12604 +       return dyaop;
12605 +}
12606 +
12607 +int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
12608 +               struct inode *h_inode)
12609 +{
12610 +       int err, do_dx;
12611 +       struct super_block *sb;
12612 +       struct au_branch *br;
12613 +       struct au_dyaop *dyaop;
12614 +
12615 +       AuDebugOn(!S_ISREG(h_inode->i_mode));
12616 +       IiMustWriteLock(inode);
12617 +
12618 +       sb = inode->i_sb;
12619 +       br = au_sbr(sb, bindex);
12620 +       do_dx = !!au_opt_test(au_mntflags(sb), DIO);
12621 +       dyaop = dy_aget(br, h_inode->i_mapping->a_ops, do_dx);
12622 +       err = PTR_ERR(dyaop);
12623 +       if (IS_ERR(dyaop))
12624 +               /* unnecessary to call dy_fput() */
12625 +               goto out;
12626 +
12627 +       err = 0;
12628 +       inode->i_mapping->a_ops = &dyaop->da_op;
12629 +
12630 +out:
12631 +       return err;
12632 +}
12633 +
12634 +/*
12635 + * Is it safe to replace a_ops during the inode/file is in operation?
12636 + * Yes, I hope so.
12637 + */
12638 +int au_dy_irefresh(struct inode *inode)
12639 +{
12640 +       int err;
12641 +       aufs_bindex_t btop;
12642 +       struct inode *h_inode;
12643 +
12644 +       err = 0;
12645 +       if (S_ISREG(inode->i_mode)) {
12646 +               btop = au_ibtop(inode);
12647 +               h_inode = au_h_iptr(inode, btop);
12648 +               err = au_dy_iaop(inode, btop, h_inode);
12649 +       }
12650 +       return err;
12651 +}
12652 +
12653 +void au_dy_arefresh(int do_dx)
12654 +{
12655 +       struct hlist_bl_head *hbl;
12656 +       struct hlist_bl_node *pos;
12657 +       struct au_dykey *key;
12658 +
12659 +       hbl = dynop + AuDy_AOP;
12660 +       hlist_bl_lock(hbl);
12661 +       hlist_bl_for_each_entry(key, pos, hbl, dk_hnode)
12662 +               dy_adx((void *)key, do_dx);
12663 +       hlist_bl_unlock(hbl);
12664 +}
12665 +
12666 +/* ---------------------------------------------------------------------- */
12667 +
12668 +void __init au_dy_init(void)
12669 +{
12670 +       int i;
12671 +
12672 +       for (i = 0; i < AuDyLast; i++)
12673 +               INIT_HLIST_BL_HEAD(dynop + i);
12674 +}
12675 +
12676 +void au_dy_fin(void)
12677 +{
12678 +       int i;
12679 +
12680 +       for (i = 0; i < AuDyLast; i++)
12681 +               WARN_ON(!hlist_bl_empty(dynop + i));
12682 +}
12683 diff -urN /usr/share/empty/fs/aufs/dynop.h linux/fs/aufs/dynop.h
12684 --- /usr/share/empty/fs/aufs/dynop.h    1970-01-01 01:00:00.000000000 +0100
12685 +++ linux/fs/aufs/dynop.h       2022-03-21 14:49:05.723299677 +0100
12686 @@ -0,0 +1,77 @@
12687 +/* SPDX-License-Identifier: GPL-2.0 */
12688 +/*
12689 + * Copyright (C) 2010-2021 Junjiro R. Okajima
12690 + *
12691 + * This program, aufs is free software; you can redistribute it and/or modify
12692 + * it under the terms of the GNU General Public License as published by
12693 + * the Free Software Foundation; either version 2 of the License, or
12694 + * (at your option) any later version.
12695 + *
12696 + * This program is distributed in the hope that it will be useful,
12697 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12698 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12699 + * GNU General Public License for more details.
12700 + *
12701 + * You should have received a copy of the GNU General Public License
12702 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
12703 + */
12704 +
12705 +/*
12706 + * dynamically customizable operations (for regular files only)
12707 + */
12708 +
12709 +#ifndef __AUFS_DYNOP_H__
12710 +#define __AUFS_DYNOP_H__
12711 +
12712 +#ifdef __KERNEL__
12713 +
12714 +#include <linux/fs.h>
12715 +#include <linux/kref.h>
12716 +
12717 +enum {AuDy_AOP, AuDyLast};
12718 +
12719 +struct au_dynop {
12720 +       int                                             dy_type;
12721 +       union {
12722 +               const void                              *dy_hop;
12723 +               const struct address_space_operations   *dy_haop;
12724 +       };
12725 +};
12726 +
12727 +struct au_dykey {
12728 +       union {
12729 +               struct hlist_bl_node    dk_hnode;
12730 +               struct rcu_head         dk_rcu;
12731 +       };
12732 +       struct au_dynop         dk_op;
12733 +
12734 +       /*
12735 +        * during I am in the branch local array, kref is gotten. when the
12736 +        * branch is removed, kref is put.
12737 +        */
12738 +       struct kref             dk_kref;
12739 +};
12740 +
12741 +/* stop unioning since their sizes are very different from each other */
12742 +struct au_dyaop {
12743 +       struct au_dykey                 da_key;
12744 +       struct address_space_operations da_op; /* not const */
12745 +};
12746 +/* make sure that 'struct au_dykey *' can be any type */
12747 +static_assert(!offsetof(struct au_dyaop, da_key));
12748 +
12749 +/* ---------------------------------------------------------------------- */
12750 +
12751 +/* dynop.c */
12752 +struct au_branch;
12753 +void au_dy_put(struct au_dykey *key);
12754 +int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
12755 +               struct inode *h_inode);
12756 +int au_dy_irefresh(struct inode *inode);
12757 +void au_dy_arefresh(int do_dio);
12758 +
12759 +void __init au_dy_init(void);
12760 +void au_dy_fin(void);
12761 +
12762 +#endif /* __KERNEL__ */
12763 +#endif /* __AUFS_DYNOP_H__ */
12764 diff -urN /usr/share/empty/fs/aufs/export.c linux/fs/aufs/export.c
12765 --- /usr/share/empty/fs/aufs/export.c   1970-01-01 01:00:00.000000000 +0100
12766 +++ linux/fs/aufs/export.c      2022-03-21 14:49:05.723299677 +0100
12767 @@ -0,0 +1,830 @@
12768 +// SPDX-License-Identifier: GPL-2.0
12769 +/*
12770 + * Copyright (C) 2005-2021 Junjiro R. Okajima
12771 + *
12772 + * This program, aufs is free software; you can redistribute it and/or modify
12773 + * it under the terms of the GNU General Public License as published by
12774 + * the Free Software Foundation; either version 2 of the License, or
12775 + * (at your option) any later version.
12776 + *
12777 + * This program is distributed in the hope that it will be useful,
12778 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12779 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12780 + * GNU General Public License for more details.
12781 + *
12782 + * You should have received a copy of the GNU General Public License
12783 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
12784 + */
12785 +
12786 +/*
12787 + * export via nfs
12788 + */
12789 +
12790 +#include <linux/exportfs.h>
12791 +#include <linux/fs_struct.h>
12792 +#include <linux/nsproxy.h>
12793 +#include <linux/random.h>
12794 +#include <linux/writeback.h>
12795 +#include "aufs.h"
12796 +
12797 +union conv {
12798 +#ifdef CONFIG_AUFS_INO_T_64
12799 +       __u32 a[2];
12800 +#else
12801 +       __u32 a[1];
12802 +#endif
12803 +       ino_t ino;
12804 +};
12805 +
12806 +static ino_t decode_ino(__u32 *a)
12807 +{
12808 +       union conv u;
12809 +
12810 +       BUILD_BUG_ON(sizeof(u.ino) != sizeof(u.a));
12811 +       u.a[0] = a[0];
12812 +#ifdef CONFIG_AUFS_INO_T_64
12813 +       u.a[1] = a[1];
12814 +#endif
12815 +       return u.ino;
12816 +}
12817 +
12818 +static void encode_ino(__u32 *a, ino_t ino)
12819 +{
12820 +       union conv u;
12821 +
12822 +       u.ino = ino;
12823 +       a[0] = u.a[0];
12824 +#ifdef CONFIG_AUFS_INO_T_64
12825 +       a[1] = u.a[1];
12826 +#endif
12827 +}
12828 +
12829 +/* NFS file handle */
12830 +enum {
12831 +       Fh_br_id,
12832 +       Fh_sigen,
12833 +#ifdef CONFIG_AUFS_INO_T_64
12834 +       /* support 64bit inode number */
12835 +       Fh_ino1,
12836 +       Fh_ino2,
12837 +       Fh_dir_ino1,
12838 +       Fh_dir_ino2,
12839 +#else
12840 +       Fh_ino1,
12841 +       Fh_dir_ino1,
12842 +#endif
12843 +       Fh_igen,
12844 +       Fh_h_type,
12845 +       Fh_tail,
12846 +
12847 +       Fh_ino = Fh_ino1,
12848 +       Fh_dir_ino = Fh_dir_ino1
12849 +};
12850 +
12851 +static int au_test_anon(struct dentry *dentry)
12852 +{
12853 +       /* note: read d_flags without d_lock */
12854 +       return !!(dentry->d_flags & DCACHE_DISCONNECTED);
12855 +}
12856 +
12857 +int au_test_nfsd(void)
12858 +{
12859 +       int ret;
12860 +       struct task_struct *tsk = current;
12861 +       char comm[sizeof(tsk->comm)];
12862 +
12863 +       ret = 0;
12864 +       if (tsk->flags & PF_KTHREAD) {
12865 +               get_task_comm(comm, tsk);
12866 +               ret = !strcmp(comm, "nfsd");
12867 +       }
12868 +
12869 +       return ret;
12870 +}
12871 +
12872 +/* ---------------------------------------------------------------------- */
12873 +/* inode generation external table */
12874 +
12875 +void au_xigen_inc(struct inode *inode)
12876 +{
12877 +       loff_t pos;
12878 +       ssize_t sz;
12879 +       __u32 igen;
12880 +       struct super_block *sb;
12881 +       struct au_sbinfo *sbinfo;
12882 +
12883 +       sb = inode->i_sb;
12884 +       AuDebugOn(!au_opt_test(au_mntflags(sb), XINO));
12885 +
12886 +       sbinfo = au_sbi(sb);
12887 +       pos = inode->i_ino;
12888 +       pos *= sizeof(igen);
12889 +       igen = inode->i_generation + 1;
12890 +       sz = xino_fwrite(sbinfo->si_xigen, &igen, sizeof(igen), &pos);
12891 +       if (sz == sizeof(igen))
12892 +               return; /* success */
12893 +
12894 +       if (unlikely(sz >= 0))
12895 +               AuIOErr("xigen error (%zd)\n", sz);
12896 +}
12897 +
12898 +int au_xigen_new(struct inode *inode)
12899 +{
12900 +       int err;
12901 +       loff_t pos;
12902 +       ssize_t sz;
12903 +       struct super_block *sb;
12904 +       struct au_sbinfo *sbinfo;
12905 +       struct file *file;
12906 +
12907 +       err = 0;
12908 +       /* todo: dirty, at mount time */
12909 +       if (inode->i_ino == AUFS_ROOT_INO)
12910 +               goto out;
12911 +       sb = inode->i_sb;
12912 +       SiMustAnyLock(sb);
12913 +       if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
12914 +               goto out;
12915 +
12916 +       err = -EFBIG;
12917 +       pos = inode->i_ino;
12918 +       if (unlikely(au_loff_max / sizeof(inode->i_generation) - 1 < pos)) {
12919 +               AuIOErr1("too large i%lld\n", pos);
12920 +               goto out;
12921 +       }
12922 +       pos *= sizeof(inode->i_generation);
12923 +
12924 +       err = 0;
12925 +       sbinfo = au_sbi(sb);
12926 +       file = sbinfo->si_xigen;
12927 +       BUG_ON(!file);
12928 +
12929 +       if (vfsub_f_size_read(file)
12930 +           < pos + sizeof(inode->i_generation)) {
12931 +               inode->i_generation = atomic_inc_return(&sbinfo->si_xigen_next);
12932 +               sz = xino_fwrite(file, &inode->i_generation,
12933 +                                sizeof(inode->i_generation), &pos);
12934 +       } else
12935 +               sz = xino_fread(file, &inode->i_generation,
12936 +                               sizeof(inode->i_generation), &pos);
12937 +       if (sz == sizeof(inode->i_generation))
12938 +               goto out; /* success */
12939 +
12940 +       err = sz;
12941 +       if (unlikely(sz >= 0)) {
12942 +               err = -EIO;
12943 +               AuIOErr("xigen error (%zd)\n", sz);
12944 +       }
12945 +
12946 +out:
12947 +       return err;
12948 +}
12949 +
12950 +int au_xigen_set(struct super_block *sb, struct path *path)
12951 +{
12952 +       int err;
12953 +       struct au_sbinfo *sbinfo;
12954 +       struct file *file;
12955 +
12956 +       SiMustWriteLock(sb);
12957 +
12958 +       sbinfo = au_sbi(sb);
12959 +       file = au_xino_create2(sb, path, sbinfo->si_xigen);
12960 +       err = PTR_ERR(file);
12961 +       if (IS_ERR(file))
12962 +               goto out;
12963 +       err = 0;
12964 +       if (sbinfo->si_xigen)
12965 +               fput(sbinfo->si_xigen);
12966 +       sbinfo->si_xigen = file;
12967 +
12968 +out:
12969 +       AuTraceErr(err);
12970 +       return err;
12971 +}
12972 +
12973 +void au_xigen_clr(struct super_block *sb)
12974 +{
12975 +       struct au_sbinfo *sbinfo;
12976 +
12977 +       SiMustWriteLock(sb);
12978 +
12979 +       sbinfo = au_sbi(sb);
12980 +       if (sbinfo->si_xigen) {
12981 +               fput(sbinfo->si_xigen);
12982 +               sbinfo->si_xigen = NULL;
12983 +       }
12984 +}
12985 +
12986 +/* ---------------------------------------------------------------------- */
12987 +
12988 +static struct dentry *decode_by_ino(struct super_block *sb, ino_t ino,
12989 +                                   ino_t dir_ino)
12990 +{
12991 +       struct dentry *dentry, *d;
12992 +       struct inode *inode;
12993 +       unsigned int sigen;
12994 +
12995 +       dentry = NULL;
12996 +       inode = ilookup(sb, ino);
12997 +       if (!inode)
12998 +               goto out;
12999 +
13000 +       dentry = ERR_PTR(-ESTALE);
13001 +       sigen = au_sigen(sb);
13002 +       if (unlikely(au_is_bad_inode(inode)
13003 +                    || IS_DEADDIR(inode)
13004 +                    || sigen != au_iigen(inode, NULL)))
13005 +               goto out_iput;
13006 +
13007 +       dentry = NULL;
13008 +       if (!dir_ino || S_ISDIR(inode->i_mode))
13009 +               dentry = d_find_alias(inode);
13010 +       else {
13011 +               spin_lock(&inode->i_lock);
13012 +               hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
13013 +                       spin_lock(&d->d_lock);
13014 +                       if (!au_test_anon(d)
13015 +                           && d_inode(d->d_parent)->i_ino == dir_ino) {
13016 +                               dentry = dget_dlock(d);
13017 +                               spin_unlock(&d->d_lock);
13018 +                               break;
13019 +                       }
13020 +                       spin_unlock(&d->d_lock);
13021 +               }
13022 +               spin_unlock(&inode->i_lock);
13023 +       }
13024 +       if (unlikely(dentry && au_digen_test(dentry, sigen))) {
13025 +               /* need to refresh */
13026 +               dput(dentry);
13027 +               dentry = NULL;
13028 +       }
13029 +
13030 +out_iput:
13031 +       iput(inode);
13032 +out:
13033 +       AuTraceErrPtr(dentry);
13034 +       return dentry;
13035 +}
13036 +
13037 +/* ---------------------------------------------------------------------- */
13038 +
13039 +/* todo: dirty? */
13040 +/* if exportfs_decode_fh() passed vfsmount*, we could be happy */
13041 +
13042 +struct au_compare_mnt_args {
13043 +       /* input */
13044 +       struct super_block *sb;
13045 +
13046 +       /* output */
13047 +       struct vfsmount *mnt;
13048 +};
13049 +
13050 +static int au_compare_mnt(struct vfsmount *mnt, void *arg)
13051 +{
13052 +       struct au_compare_mnt_args *a = arg;
13053 +
13054 +       if (mnt->mnt_sb != a->sb)
13055 +               return 0;
13056 +       a->mnt = mntget(mnt);
13057 +       return 1;
13058 +}
13059 +
13060 +static struct vfsmount *au_mnt_get(struct super_block *sb)
13061 +{
13062 +       int err;
13063 +       struct path root;
13064 +       struct au_compare_mnt_args args = {
13065 +               .sb = sb
13066 +       };
13067 +
13068 +       get_fs_root(current->fs, &root);
13069 +       rcu_read_lock();
13070 +       err = iterate_mounts(au_compare_mnt, &args, root.mnt);
13071 +       rcu_read_unlock();
13072 +       path_put(&root);
13073 +       AuDebugOn(!err);
13074 +       AuDebugOn(!args.mnt);
13075 +       return args.mnt;
13076 +}
13077 +
13078 +struct au_nfsd_si_lock {
13079 +       unsigned int sigen;
13080 +       aufs_bindex_t bindex, br_id;
13081 +       unsigned char force_lock;
13082 +};
13083 +
13084 +static int si_nfsd_read_lock(struct super_block *sb,
13085 +                            struct au_nfsd_si_lock *nsi_lock)
13086 +{
13087 +       int err;
13088 +       aufs_bindex_t bindex;
13089 +
13090 +       si_read_lock(sb, AuLock_FLUSH);
13091 +
13092 +       /* branch id may be wrapped around */
13093 +       err = 0;
13094 +       bindex = au_br_index(sb, nsi_lock->br_id);
13095 +       if (bindex >= 0 && nsi_lock->sigen + AUFS_BRANCH_MAX > au_sigen(sb))
13096 +               goto out; /* success */
13097 +
13098 +       err = -ESTALE;
13099 +       bindex = -1;
13100 +       if (!nsi_lock->force_lock)
13101 +               si_read_unlock(sb);
13102 +
13103 +out:
13104 +       nsi_lock->bindex = bindex;
13105 +       return err;
13106 +}
13107 +
13108 +struct find_name_by_ino {
13109 +       struct dir_context ctx;
13110 +       int called, found;
13111 +       ino_t ino;
13112 +       char *name;
13113 +       int namelen;
13114 +};
13115 +
13116 +static int
13117 +find_name_by_ino(struct dir_context *ctx, const char *name, int namelen,
13118 +                loff_t offset, u64 ino, unsigned int d_type)
13119 +{
13120 +       struct find_name_by_ino *a = container_of(ctx, struct find_name_by_ino,
13121 +                                                 ctx);
13122 +
13123 +       a->called++;
13124 +       if (a->ino != ino)
13125 +               return 0;
13126 +
13127 +       memcpy(a->name, name, namelen);
13128 +       a->namelen = namelen;
13129 +       a->found = 1;
13130 +       return 1;
13131 +}
13132 +
13133 +static struct dentry *au_lkup_by_ino(struct path *path, ino_t ino,
13134 +                                    struct au_nfsd_si_lock *nsi_lock)
13135 +{
13136 +       struct dentry *dentry, *parent;
13137 +       struct file *file;
13138 +       struct inode *dir;
13139 +       struct find_name_by_ino arg = {
13140 +               .ctx = {
13141 +                       .actor = find_name_by_ino
13142 +               }
13143 +       };
13144 +       int err;
13145 +
13146 +       parent = path->dentry;
13147 +       if (nsi_lock)
13148 +               si_read_unlock(parent->d_sb);
13149 +       file = vfsub_dentry_open(path, au_dir_roflags);
13150 +       dentry = (void *)file;
13151 +       if (IS_ERR(file))
13152 +               goto out;
13153 +
13154 +       dentry = ERR_PTR(-ENOMEM);
13155 +       arg.name = (void *)__get_free_page(GFP_NOFS);
13156 +       if (unlikely(!arg.name))
13157 +               goto out_file;
13158 +       arg.ino = ino;
13159 +       arg.found = 0;
13160 +       do {
13161 +               arg.called = 0;
13162 +               /* smp_mb(); */
13163 +               err = vfsub_iterate_dir(file, &arg.ctx);
13164 +       } while (!err && !arg.found && arg.called);
13165 +       dentry = ERR_PTR(err);
13166 +       if (unlikely(err))
13167 +               goto out_name;
13168 +       /* instead of ENOENT */
13169 +       dentry = ERR_PTR(-ESTALE);
13170 +       if (!arg.found)
13171 +               goto out_name;
13172 +
13173 +       /* do not call vfsub_lkup_one() */
13174 +       dir = d_inode(parent);
13175 +       dentry = vfsub_lookup_one_len_unlocked(arg.name, path, arg.namelen);
13176 +       AuTraceErrPtr(dentry);
13177 +       if (IS_ERR(dentry))
13178 +               goto out_name;
13179 +       AuDebugOn(au_test_anon(dentry));
13180 +       if (unlikely(d_really_is_negative(dentry))) {
13181 +               dput(dentry);
13182 +               dentry = ERR_PTR(-ENOENT);
13183 +       }
13184 +
13185 +out_name:
13186 +       free_page((unsigned long)arg.name);
13187 +out_file:
13188 +       fput(file);
13189 +out:
13190 +       if (unlikely(nsi_lock
13191 +                    && si_nfsd_read_lock(parent->d_sb, nsi_lock) < 0))
13192 +               if (!IS_ERR(dentry)) {
13193 +                       dput(dentry);
13194 +                       dentry = ERR_PTR(-ESTALE);
13195 +               }
13196 +       AuTraceErrPtr(dentry);
13197 +       return dentry;
13198 +}
13199 +
13200 +static struct dentry *decode_by_dir_ino(struct super_block *sb, ino_t ino,
13201 +                                       ino_t dir_ino,
13202 +                                       struct au_nfsd_si_lock *nsi_lock)
13203 +{
13204 +       struct dentry *dentry;
13205 +       struct path path;
13206 +
13207 +       if (dir_ino != AUFS_ROOT_INO) {
13208 +               path.dentry = decode_by_ino(sb, dir_ino, 0);
13209 +               dentry = path.dentry;
13210 +               if (!path.dentry || IS_ERR(path.dentry))
13211 +                       goto out;
13212 +               AuDebugOn(au_test_anon(path.dentry));
13213 +       } else
13214 +               path.dentry = dget(sb->s_root);
13215 +
13216 +       path.mnt = au_mnt_get(sb);
13217 +       dentry = au_lkup_by_ino(&path, ino, nsi_lock);
13218 +       path_put(&path);
13219 +
13220 +out:
13221 +       AuTraceErrPtr(dentry);
13222 +       return dentry;
13223 +}
13224 +
13225 +/* ---------------------------------------------------------------------- */
13226 +
13227 +static int h_acceptable(void *expv, struct dentry *dentry)
13228 +{
13229 +       return 1;
13230 +}
13231 +
13232 +static char *au_build_path(struct dentry *h_parent, struct path *h_rootpath,
13233 +                          char *buf, int len, struct super_block *sb)
13234 +{
13235 +       char *p;
13236 +       int n;
13237 +       struct path path;
13238 +
13239 +       p = d_path(h_rootpath, buf, len);
13240 +       if (IS_ERR(p))
13241 +               goto out;
13242 +       n = strlen(p);
13243 +
13244 +       path.mnt = h_rootpath->mnt;
13245 +       path.dentry = h_parent;
13246 +       p = d_path(&path, buf, len);
13247 +       if (IS_ERR(p))
13248 +               goto out;
13249 +       if (n != 1)
13250 +               p += n;
13251 +
13252 +       path.mnt = au_mnt_get(sb);
13253 +       path.dentry = sb->s_root;
13254 +       p = d_path(&path, buf, len - strlen(p));
13255 +       mntput(path.mnt);
13256 +       if (IS_ERR(p))
13257 +               goto out;
13258 +       if (n != 1)
13259 +               p[strlen(p)] = '/';
13260 +
13261 +out:
13262 +       AuTraceErrPtr(p);
13263 +       return p;
13264 +}
13265 +
13266 +static
13267 +struct dentry *decode_by_path(struct super_block *sb, ino_t ino, __u32 *fh,
13268 +                             int fh_len, struct au_nfsd_si_lock *nsi_lock)
13269 +{
13270 +       struct dentry *dentry, *h_parent, *root;
13271 +       struct super_block *h_sb;
13272 +       char *pathname, *p;
13273 +       struct vfsmount *h_mnt;
13274 +       struct au_branch *br;
13275 +       int err;
13276 +       struct path path;
13277 +
13278 +       br = au_sbr(sb, nsi_lock->bindex);
13279 +       h_mnt = au_br_mnt(br);
13280 +       h_sb = h_mnt->mnt_sb;
13281 +       /* todo: call lower fh_to_dentry()? fh_to_parent()? */
13282 +       lockdep_off();
13283 +       h_parent = exportfs_decode_fh(h_mnt, (void *)(fh + Fh_tail),
13284 +                                     fh_len - Fh_tail, fh[Fh_h_type],
13285 +                                     h_acceptable, /*context*/NULL);
13286 +       lockdep_on();
13287 +       dentry = h_parent;
13288 +       if (unlikely(!h_parent || IS_ERR(h_parent))) {
13289 +               AuWarn1("%s decode_fh failed, %ld\n",
13290 +                       au_sbtype(h_sb), PTR_ERR(h_parent));
13291 +               goto out;
13292 +       }
13293 +       dentry = NULL;
13294 +       if (unlikely(au_test_anon(h_parent))) {
13295 +               AuWarn1("%s decode_fh returned a disconnected dentry\n",
13296 +                       au_sbtype(h_sb));
13297 +               goto out_h_parent;
13298 +       }
13299 +
13300 +       dentry = ERR_PTR(-ENOMEM);
13301 +       pathname = (void *)__get_free_page(GFP_NOFS);
13302 +       if (unlikely(!pathname))
13303 +               goto out_h_parent;
13304 +
13305 +       root = sb->s_root;
13306 +       path.mnt = h_mnt;
13307 +       di_read_lock_parent(root, !AuLock_IR);
13308 +       path.dentry = au_h_dptr(root, nsi_lock->bindex);
13309 +       di_read_unlock(root, !AuLock_IR);
13310 +       p = au_build_path(h_parent, &path, pathname, PAGE_SIZE, sb);
13311 +       dentry = (void *)p;
13312 +       if (IS_ERR(p))
13313 +               goto out_pathname;
13314 +
13315 +       si_read_unlock(sb);
13316 +       err = vfsub_kern_path(p, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &path);
13317 +       dentry = ERR_PTR(err);
13318 +       if (unlikely(err))
13319 +               goto out_relock;
13320 +
13321 +       dentry = ERR_PTR(-ENOENT);
13322 +       AuDebugOn(au_test_anon(path.dentry));
13323 +       if (unlikely(d_really_is_negative(path.dentry)))
13324 +               goto out_path;
13325 +
13326 +       if (ino != d_inode(path.dentry)->i_ino)
13327 +               dentry = au_lkup_by_ino(&path, ino, /*nsi_lock*/NULL);
13328 +       else
13329 +               dentry = dget(path.dentry);
13330 +
13331 +out_path:
13332 +       path_put(&path);
13333 +out_relock:
13334 +       if (unlikely(si_nfsd_read_lock(sb, nsi_lock) < 0))
13335 +               if (!IS_ERR(dentry)) {
13336 +                       dput(dentry);
13337 +                       dentry = ERR_PTR(-ESTALE);
13338 +               }
13339 +out_pathname:
13340 +       free_page((unsigned long)pathname);
13341 +out_h_parent:
13342 +       dput(h_parent);
13343 +out:
13344 +       AuTraceErrPtr(dentry);
13345 +       return dentry;
13346 +}
13347 +
13348 +/* ---------------------------------------------------------------------- */
13349 +
13350 +static struct dentry *
13351 +aufs_fh_to_dentry(struct super_block *sb, struct fid *fid, int fh_len,
13352 +                 int fh_type)
13353 +{
13354 +       struct dentry *dentry;
13355 +       __u32 *fh = fid->raw;
13356 +       struct au_branch *br;
13357 +       ino_t ino, dir_ino;
13358 +       struct au_nfsd_si_lock nsi_lock = {
13359 +               .force_lock     = 0
13360 +       };
13361 +
13362 +       dentry = ERR_PTR(-ESTALE);
13363 +       /* it should never happen, but the file handle is unreliable */
13364 +       if (unlikely(fh_len < Fh_tail))
13365 +               goto out;
13366 +       nsi_lock.sigen = fh[Fh_sigen];
13367 +       nsi_lock.br_id = fh[Fh_br_id];
13368 +
13369 +       /* branch id may be wrapped around */
13370 +       br = NULL;
13371 +       if (unlikely(si_nfsd_read_lock(sb, &nsi_lock)))
13372 +               goto out;
13373 +       nsi_lock.force_lock = 1;
13374 +
13375 +       /* is this inode still cached? */
13376 +       ino = decode_ino(fh + Fh_ino);
13377 +       /* it should never happen */
13378 +       if (unlikely(ino == AUFS_ROOT_INO))
13379 +               goto out_unlock;
13380 +
13381 +       dir_ino = decode_ino(fh + Fh_dir_ino);
13382 +       dentry = decode_by_ino(sb, ino, dir_ino);
13383 +       if (IS_ERR(dentry))
13384 +               goto out_unlock;
13385 +       if (dentry)
13386 +               goto accept;
13387 +
13388 +       /* is the parent dir cached? */
13389 +       br = au_sbr(sb, nsi_lock.bindex);
13390 +       au_lcnt_inc(&br->br_nfiles);
13391 +       dentry = decode_by_dir_ino(sb, ino, dir_ino, &nsi_lock);
13392 +       if (IS_ERR(dentry))
13393 +               goto out_unlock;
13394 +       if (dentry)
13395 +               goto accept;
13396 +
13397 +       /* lookup path */
13398 +       dentry = decode_by_path(sb, ino, fh, fh_len, &nsi_lock);
13399 +       if (IS_ERR(dentry))
13400 +               goto out_unlock;
13401 +       if (unlikely(!dentry))
13402 +               /* todo?: make it ESTALE */
13403 +               goto out_unlock;
13404 +
13405 +accept:
13406 +       if (!au_digen_test(dentry, au_sigen(sb))
13407 +           && d_inode(dentry)->i_generation == fh[Fh_igen])
13408 +               goto out_unlock; /* success */
13409 +
13410 +       dput(dentry);
13411 +       dentry = ERR_PTR(-ESTALE);
13412 +out_unlock:
13413 +       if (br)
13414 +               au_lcnt_dec(&br->br_nfiles);
13415 +       si_read_unlock(sb);
13416 +out:
13417 +       AuTraceErrPtr(dentry);
13418 +       return dentry;
13419 +}
13420 +
13421 +#if 0 /* reserved for future use */
13422 +/* support subtreecheck option */
13423 +static struct dentry *aufs_fh_to_parent(struct super_block *sb, struct fid *fid,
13424 +                                       int fh_len, int fh_type)
13425 +{
13426 +       struct dentry *parent;
13427 +       __u32 *fh = fid->raw;
13428 +       ino_t dir_ino;
13429 +
13430 +       dir_ino = decode_ino(fh + Fh_dir_ino);
13431 +       parent = decode_by_ino(sb, dir_ino, 0);
13432 +       if (IS_ERR(parent))
13433 +               goto out;
13434 +       if (!parent)
13435 +               parent = decode_by_path(sb, au_br_index(sb, fh[Fh_br_id]),
13436 +                                       dir_ino, fh, fh_len);
13437 +
13438 +out:
13439 +       AuTraceErrPtr(parent);
13440 +       return parent;
13441 +}
13442 +#endif
13443 +
13444 +/* ---------------------------------------------------------------------- */
13445 +
13446 +static int aufs_encode_fh(struct inode *inode, __u32 *fh, int *max_len,
13447 +                         struct inode *dir)
13448 +{
13449 +       int err;
13450 +       aufs_bindex_t bindex;
13451 +       struct super_block *sb, *h_sb;
13452 +       struct dentry *dentry, *parent, *h_parent;
13453 +       struct inode *h_dir;
13454 +       struct au_branch *br;
13455 +
13456 +       err = -ENOSPC;
13457 +       if (unlikely(*max_len <= Fh_tail)) {
13458 +               AuWarn1("NFSv2 client (max_len %d)?\n", *max_len);
13459 +               goto out;
13460 +       }
13461 +
13462 +       err = FILEID_ROOT;
13463 +       if (inode->i_ino == AUFS_ROOT_INO) {
13464 +               AuDebugOn(inode->i_ino != AUFS_ROOT_INO);
13465 +               goto out;
13466 +       }
13467 +
13468 +       h_parent = NULL;
13469 +       sb = inode->i_sb;
13470 +       err = si_read_lock(sb, AuLock_FLUSH);
13471 +       if (unlikely(err))
13472 +               goto out;
13473 +
13474 +#ifdef CONFIG_AUFS_DEBUG
13475 +       if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
13476 +               AuWarn1("NFS-exporting requires xino\n");
13477 +#endif
13478 +       err = -EIO;
13479 +       parent = NULL;
13480 +       ii_read_lock_child(inode);
13481 +       bindex = au_ibtop(inode);
13482 +       if (!dir) {
13483 +               dentry = d_find_any_alias(inode);
13484 +               if (unlikely(!dentry))
13485 +                       goto out_unlock;
13486 +               AuDebugOn(au_test_anon(dentry));
13487 +               parent = dget_parent(dentry);
13488 +               dput(dentry);
13489 +               if (unlikely(!parent))
13490 +                       goto out_unlock;
13491 +               if (d_really_is_positive(parent))
13492 +                       dir = d_inode(parent);
13493 +       }
13494 +
13495 +       ii_read_lock_parent(dir);
13496 +       h_dir = au_h_iptr(dir, bindex);
13497 +       ii_read_unlock(dir);
13498 +       if (unlikely(!h_dir))
13499 +               goto out_parent;
13500 +       h_parent = d_find_any_alias(h_dir);
13501 +       if (unlikely(!h_parent))
13502 +               goto out_hparent;
13503 +
13504 +       err = -EPERM;
13505 +       br = au_sbr(sb, bindex);
13506 +       h_sb = au_br_sb(br);
13507 +       if (unlikely(!h_sb->s_export_op)) {
13508 +               AuErr1("%s branch is not exportable\n", au_sbtype(h_sb));
13509 +               goto out_hparent;
13510 +       }
13511 +
13512 +       fh[Fh_br_id] = br->br_id;
13513 +       fh[Fh_sigen] = au_sigen(sb);
13514 +       encode_ino(fh + Fh_ino, inode->i_ino);
13515 +       encode_ino(fh + Fh_dir_ino, dir->i_ino);
13516 +       fh[Fh_igen] = inode->i_generation;
13517 +
13518 +       *max_len -= Fh_tail;
13519 +       fh[Fh_h_type] = exportfs_encode_fh(h_parent, (void *)(fh + Fh_tail),
13520 +                                          max_len,
13521 +                                          /*connectable or subtreecheck*/0);
13522 +       err = fh[Fh_h_type];
13523 +       *max_len += Fh_tail;
13524 +       /* todo: macros? */
13525 +       if (err != FILEID_INVALID)
13526 +               err = 99;
13527 +       else
13528 +               AuWarn1("%s encode_fh failed\n", au_sbtype(h_sb));
13529 +
13530 +out_hparent:
13531 +       dput(h_parent);
13532 +out_parent:
13533 +       dput(parent);
13534 +out_unlock:
13535 +       ii_read_unlock(inode);
13536 +       si_read_unlock(sb);
13537 +out:
13538 +       if (unlikely(err < 0))
13539 +               err = FILEID_INVALID;
13540 +       return err;
13541 +}
13542 +
13543 +/* ---------------------------------------------------------------------- */
13544 +
13545 +static int aufs_commit_metadata(struct inode *inode)
13546 +{
13547 +       int err;
13548 +       aufs_bindex_t bindex;
13549 +       struct super_block *sb;
13550 +       struct inode *h_inode;
13551 +       int (*f)(struct inode *inode);
13552 +
13553 +       sb = inode->i_sb;
13554 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
13555 +       ii_write_lock_child(inode);
13556 +       bindex = au_ibtop(inode);
13557 +       AuDebugOn(bindex < 0);
13558 +       h_inode = au_h_iptr(inode, bindex);
13559 +
13560 +       f = h_inode->i_sb->s_export_op->commit_metadata;
13561 +       if (f)
13562 +               err = f(h_inode);
13563 +       else
13564 +               err = sync_inode_metadata(h_inode, /*wait*/1);
13565 +
13566 +       au_cpup_attr_timesizes(inode);
13567 +       ii_write_unlock(inode);
13568 +       si_read_unlock(sb);
13569 +       return err;
13570 +}
13571 +
13572 +/* ---------------------------------------------------------------------- */
13573 +
13574 +static struct export_operations aufs_export_op = {
13575 +       .fh_to_dentry           = aufs_fh_to_dentry,
13576 +       /* .fh_to_parent        = aufs_fh_to_parent, */
13577 +       .encode_fh              = aufs_encode_fh,
13578 +       .commit_metadata        = aufs_commit_metadata
13579 +};
13580 +
13581 +void au_export_init(struct super_block *sb)
13582 +{
13583 +       struct au_sbinfo *sbinfo;
13584 +       __u32 u;
13585 +
13586 +       BUILD_BUG_ON_MSG(IS_BUILTIN(CONFIG_AUFS_FS)
13587 +                        && IS_MODULE(CONFIG_EXPORTFS),
13588 +                        AUFS_NAME ": unsupported configuration "
13589 +                        "CONFIG_EXPORTFS=m and CONFIG_AUFS_FS=y");
13590 +
13591 +       sb->s_export_op = &aufs_export_op;
13592 +       sbinfo = au_sbi(sb);
13593 +       sbinfo->si_xigen = NULL;
13594 +       get_random_bytes(&u, sizeof(u));
13595 +       BUILD_BUG_ON(sizeof(u) != sizeof(int));
13596 +       atomic_set(&sbinfo->si_xigen_next, u);
13597 +}
13598 diff -urN /usr/share/empty/fs/aufs/fhsm.c linux/fs/aufs/fhsm.c
13599 --- /usr/share/empty/fs/aufs/fhsm.c     1970-01-01 01:00:00.000000000 +0100
13600 +++ linux/fs/aufs/fhsm.c        2022-03-21 14:49:05.723299677 +0100
13601 @@ -0,0 +1,427 @@
13602 +// SPDX-License-Identifier: GPL-2.0
13603 +/*
13604 + * Copyright (C) 2011-2021 Junjiro R. Okajima
13605 + *
13606 + * This program, aufs is free software; you can redistribute it and/or modify
13607 + * it under the terms of the GNU General Public License as published by
13608 + * the Free Software Foundation; either version 2 of the License, or
13609 + * (at your option) any later version.
13610 + *
13611 + * This program is distributed in the hope that it will be useful,
13612 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
13613 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13614 + * GNU General Public License for more details.
13615 + *
13616 + * You should have received a copy of the GNU General Public License
13617 + * along with this program; if not, write to the Free Software
13618 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
13619 + */
13620 +
13621 +/*
13622 + * File-based Hierarchy Storage Management
13623 + */
13624 +
13625 +#include <linux/anon_inodes.h>
13626 +#include <linux/poll.h>
13627 +#include <linux/seq_file.h>
13628 +#include <linux/statfs.h>
13629 +#include "aufs.h"
13630 +
13631 +static aufs_bindex_t au_fhsm_bottom(struct super_block *sb)
13632 +{
13633 +       struct au_sbinfo *sbinfo;
13634 +       struct au_fhsm *fhsm;
13635 +
13636 +       SiMustAnyLock(sb);
13637 +
13638 +       sbinfo = au_sbi(sb);
13639 +       fhsm = &sbinfo->si_fhsm;
13640 +       AuDebugOn(!fhsm);
13641 +       return fhsm->fhsm_bottom;
13642 +}
13643 +
13644 +void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex)
13645 +{
13646 +       struct au_sbinfo *sbinfo;
13647 +       struct au_fhsm *fhsm;
13648 +
13649 +       SiMustWriteLock(sb);
13650 +
13651 +       sbinfo = au_sbi(sb);
13652 +       fhsm = &sbinfo->si_fhsm;
13653 +       AuDebugOn(!fhsm);
13654 +       fhsm->fhsm_bottom = bindex;
13655 +}
13656 +
13657 +/* ---------------------------------------------------------------------- */
13658 +
13659 +static int au_fhsm_test_jiffy(struct au_sbinfo *sbinfo, struct au_branch *br)
13660 +{
13661 +       struct au_br_fhsm *bf;
13662 +
13663 +       bf = br->br_fhsm;
13664 +       MtxMustLock(&bf->bf_lock);
13665 +
13666 +       return !bf->bf_readable
13667 +               || time_after(jiffies,
13668 +                             bf->bf_jiffy + sbinfo->si_fhsm.fhsm_expire);
13669 +}
13670 +
13671 +/* ---------------------------------------------------------------------- */
13672 +
13673 +static void au_fhsm_notify(struct super_block *sb, int val)
13674 +{
13675 +       struct au_sbinfo *sbinfo;
13676 +       struct au_fhsm *fhsm;
13677 +
13678 +       SiMustAnyLock(sb);
13679 +
13680 +       sbinfo = au_sbi(sb);
13681 +       fhsm = &sbinfo->si_fhsm;
13682 +       if (au_fhsm_pid(fhsm)
13683 +           && atomic_read(&fhsm->fhsm_readable) != -1) {
13684 +               atomic_set(&fhsm->fhsm_readable, val);
13685 +               if (val)
13686 +                       wake_up(&fhsm->fhsm_wqh);
13687 +       }
13688 +}
13689 +
13690 +static int au_fhsm_stfs(struct super_block *sb, aufs_bindex_t bindex,
13691 +                       struct aufs_stfs *rstfs, int do_lock, int do_notify)
13692 +{
13693 +       int err;
13694 +       struct au_branch *br;
13695 +       struct au_br_fhsm *bf;
13696 +
13697 +       br = au_sbr(sb, bindex);
13698 +       AuDebugOn(au_br_rdonly(br));
13699 +       bf = br->br_fhsm;
13700 +       AuDebugOn(!bf);
13701 +
13702 +       if (do_lock)
13703 +               mutex_lock(&bf->bf_lock);
13704 +       else
13705 +               MtxMustLock(&bf->bf_lock);
13706 +
13707 +       /* sb->s_root for NFS is unreliable */
13708 +       err = au_br_stfs(br, &bf->bf_stfs);
13709 +       if (unlikely(err)) {
13710 +               AuErr1("FHSM failed (%d), b%d, ignored.\n", bindex, err);
13711 +               goto out;
13712 +       }
13713 +
13714 +       bf->bf_jiffy = jiffies;
13715 +       bf->bf_readable = 1;
13716 +       if (do_notify)
13717 +               au_fhsm_notify(sb, /*val*/1);
13718 +       if (rstfs)
13719 +               *rstfs = bf->bf_stfs;
13720 +
13721 +out:
13722 +       if (do_lock)
13723 +               mutex_unlock(&bf->bf_lock);
13724 +       au_fhsm_notify(sb, /*val*/1);
13725 +
13726 +       return err;
13727 +}
13728 +
13729 +void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force)
13730 +{
13731 +       int err;
13732 +       struct au_sbinfo *sbinfo;
13733 +       struct au_fhsm *fhsm;
13734 +       struct au_branch *br;
13735 +       struct au_br_fhsm *bf;
13736 +
13737 +       AuDbg("b%d, force %d\n", bindex, force);
13738 +       SiMustAnyLock(sb);
13739 +
13740 +       sbinfo = au_sbi(sb);
13741 +       fhsm = &sbinfo->si_fhsm;
13742 +       if (!au_ftest_si(sbinfo, FHSM)
13743 +           || fhsm->fhsm_bottom == bindex)
13744 +               return;
13745 +
13746 +       br = au_sbr(sb, bindex);
13747 +       bf = br->br_fhsm;
13748 +       AuDebugOn(!bf);
13749 +       mutex_lock(&bf->bf_lock);
13750 +       if (force
13751 +           || au_fhsm_pid(fhsm)
13752 +           || au_fhsm_test_jiffy(sbinfo, br))
13753 +               err = au_fhsm_stfs(sb, bindex, /*rstfs*/NULL, /*do_lock*/0,
13754 +                                 /*do_notify*/1);
13755 +       mutex_unlock(&bf->bf_lock);
13756 +}
13757 +
13758 +void au_fhsm_wrote_all(struct super_block *sb, int force)
13759 +{
13760 +       aufs_bindex_t bindex, bbot;
13761 +       struct au_branch *br;
13762 +
13763 +       /* exclude the bottom */
13764 +       bbot = au_fhsm_bottom(sb);
13765 +       for (bindex = 0; bindex < bbot; bindex++) {
13766 +               br = au_sbr(sb, bindex);
13767 +               if (au_br_fhsm(br->br_perm))
13768 +                       au_fhsm_wrote(sb, bindex, force);
13769 +       }
13770 +}
13771 +
13772 +/* ---------------------------------------------------------------------- */
13773 +
13774 +static __poll_t au_fhsm_poll(struct file *file, struct poll_table_struct *wait)
13775 +{
13776 +       __poll_t mask;
13777 +       struct au_sbinfo *sbinfo;
13778 +       struct au_fhsm *fhsm;
13779 +
13780 +       mask = 0;
13781 +       sbinfo = file->private_data;
13782 +       fhsm = &sbinfo->si_fhsm;
13783 +       poll_wait(file, &fhsm->fhsm_wqh, wait);
13784 +       if (atomic_read(&fhsm->fhsm_readable))
13785 +               mask = EPOLLIN /* | EPOLLRDNORM */;
13786 +
13787 +       if (!mask)
13788 +               AuDbg("mask 0x%x\n", mask);
13789 +       return mask;
13790 +}
13791 +
13792 +static int au_fhsm_do_read_one(struct aufs_stbr __user *stbr,
13793 +                             struct aufs_stfs *stfs, __s16 brid)
13794 +{
13795 +       int err;
13796 +
13797 +       err = copy_to_user(&stbr->stfs, stfs, sizeof(*stfs));
13798 +       if (!err)
13799 +               err = __put_user(brid, &stbr->brid);
13800 +       if (unlikely(err))
13801 +               err = -EFAULT;
13802 +
13803 +       return err;
13804 +}
13805 +
13806 +static ssize_t au_fhsm_do_read(struct super_block *sb,
13807 +                              struct aufs_stbr __user *stbr, size_t count)
13808 +{
13809 +       ssize_t err;
13810 +       int nstbr;
13811 +       aufs_bindex_t bindex, bbot;
13812 +       struct au_branch *br;
13813 +       struct au_br_fhsm *bf;
13814 +
13815 +       /* except the bottom branch */
13816 +       err = 0;
13817 +       nstbr = 0;
13818 +       bbot = au_fhsm_bottom(sb);
13819 +       for (bindex = 0; !err && bindex < bbot; bindex++) {
13820 +               br = au_sbr(sb, bindex);
13821 +               if (!au_br_fhsm(br->br_perm))
13822 +                       continue;
13823 +
13824 +               bf = br->br_fhsm;
13825 +               mutex_lock(&bf->bf_lock);
13826 +               if (bf->bf_readable) {
13827 +                       err = -EFAULT;
13828 +                       if (count >= sizeof(*stbr))
13829 +                               err = au_fhsm_do_read_one(stbr++, &bf->bf_stfs,
13830 +                                                         br->br_id);
13831 +                       if (!err) {
13832 +                               bf->bf_readable = 0;
13833 +                               count -= sizeof(*stbr);
13834 +                               nstbr++;
13835 +                       }
13836 +               }
13837 +               mutex_unlock(&bf->bf_lock);
13838 +       }
13839 +       if (!err)
13840 +               err = sizeof(*stbr) * nstbr;
13841 +
13842 +       return err;
13843 +}
13844 +
13845 +static ssize_t au_fhsm_read(struct file *file, char __user *buf, size_t count,
13846 +                          loff_t *pos)
13847 +{
13848 +       ssize_t err;
13849 +       int readable;
13850 +       aufs_bindex_t nfhsm, bindex, bbot;
13851 +       struct au_sbinfo *sbinfo;
13852 +       struct au_fhsm *fhsm;
13853 +       struct au_branch *br;
13854 +       struct super_block *sb;
13855 +
13856 +       err = 0;
13857 +       sbinfo = file->private_data;
13858 +       fhsm = &sbinfo->si_fhsm;
13859 +need_data:
13860 +       spin_lock_irq(&fhsm->fhsm_wqh.lock);
13861 +       if (!atomic_read(&fhsm->fhsm_readable)) {
13862 +               if (vfsub_file_flags(file) & O_NONBLOCK)
13863 +                       err = -EAGAIN;
13864 +               else
13865 +                       err = wait_event_interruptible_locked_irq
13866 +                               (fhsm->fhsm_wqh,
13867 +                                atomic_read(&fhsm->fhsm_readable));
13868 +       }
13869 +       spin_unlock_irq(&fhsm->fhsm_wqh.lock);
13870 +       if (unlikely(err))
13871 +               goto out;
13872 +
13873 +       /* sb may already be dead */
13874 +       au_rw_read_lock(&sbinfo->si_rwsem);
13875 +       readable = atomic_read(&fhsm->fhsm_readable);
13876 +       if (readable > 0) {
13877 +               sb = sbinfo->si_sb;
13878 +               AuDebugOn(!sb);
13879 +               /* exclude the bottom branch */
13880 +               nfhsm = 0;
13881 +               bbot = au_fhsm_bottom(sb);
13882 +               for (bindex = 0; bindex < bbot; bindex++) {
13883 +                       br = au_sbr(sb, bindex);
13884 +                       if (au_br_fhsm(br->br_perm))
13885 +                               nfhsm++;
13886 +               }
13887 +               err = -EMSGSIZE;
13888 +               if (nfhsm * sizeof(struct aufs_stbr) <= count) {
13889 +                       atomic_set(&fhsm->fhsm_readable, 0);
13890 +                       err = au_fhsm_do_read(sbinfo->si_sb, (void __user *)buf,
13891 +                                            count);
13892 +               }
13893 +       }
13894 +       au_rw_read_unlock(&sbinfo->si_rwsem);
13895 +       if (!readable)
13896 +               goto need_data;
13897 +
13898 +out:
13899 +       return err;
13900 +}
13901 +
13902 +static int au_fhsm_release(struct inode *inode, struct file *file)
13903 +{
13904 +       struct au_sbinfo *sbinfo;
13905 +       struct au_fhsm *fhsm;
13906 +
13907 +       /* sb may already be dead */
13908 +       sbinfo = file->private_data;
13909 +       fhsm = &sbinfo->si_fhsm;
13910 +       spin_lock(&fhsm->fhsm_spin);
13911 +       fhsm->fhsm_pid = 0;
13912 +       spin_unlock(&fhsm->fhsm_spin);
13913 +       kobject_put(&sbinfo->si_kobj);
13914 +
13915 +       return 0;
13916 +}
13917 +
13918 +static const struct file_operations au_fhsm_fops = {
13919 +       .owner          = THIS_MODULE,
13920 +       .llseek         = noop_llseek,
13921 +       .read           = au_fhsm_read,
13922 +       .poll           = au_fhsm_poll,
13923 +       .release        = au_fhsm_release
13924 +};
13925 +
13926 +int au_fhsm_fd(struct super_block *sb, int oflags)
13927 +{
13928 +       int err, fd;
13929 +       struct au_sbinfo *sbinfo;
13930 +       struct au_fhsm *fhsm;
13931 +
13932 +       err = -EPERM;
13933 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
13934 +               goto out;
13935 +
13936 +       err = -EINVAL;
13937 +       if (unlikely(oflags & ~(O_CLOEXEC | O_NONBLOCK)))
13938 +               goto out;
13939 +
13940 +       err = 0;
13941 +       sbinfo = au_sbi(sb);
13942 +       fhsm = &sbinfo->si_fhsm;
13943 +       spin_lock(&fhsm->fhsm_spin);
13944 +       if (!fhsm->fhsm_pid)
13945 +               fhsm->fhsm_pid = current->pid;
13946 +       else
13947 +               err = -EBUSY;
13948 +       spin_unlock(&fhsm->fhsm_spin);
13949 +       if (unlikely(err))
13950 +               goto out;
13951 +
13952 +       oflags |= O_RDONLY;
13953 +       /* oflags |= FMODE_NONOTIFY; */
13954 +       fd = anon_inode_getfd("[aufs_fhsm]", &au_fhsm_fops, sbinfo, oflags);
13955 +       err = fd;
13956 +       if (unlikely(fd < 0))
13957 +               goto out_pid;
13958 +
13959 +       /* succeed regardless 'fhsm' status */
13960 +       kobject_get(&sbinfo->si_kobj);
13961 +       si_noflush_read_lock(sb);
13962 +       if (au_ftest_si(sbinfo, FHSM))
13963 +               au_fhsm_wrote_all(sb, /*force*/0);
13964 +       si_read_unlock(sb);
13965 +       goto out; /* success */
13966 +
13967 +out_pid:
13968 +       spin_lock(&fhsm->fhsm_spin);
13969 +       fhsm->fhsm_pid = 0;
13970 +       spin_unlock(&fhsm->fhsm_spin);
13971 +out:
13972 +       AuTraceErr(err);
13973 +       return err;
13974 +}
13975 +
13976 +/* ---------------------------------------------------------------------- */
13977 +
13978 +int au_fhsm_br_alloc(struct au_branch *br)
13979 +{
13980 +       int err;
13981 +
13982 +       err = 0;
13983 +       br->br_fhsm = kmalloc(sizeof(*br->br_fhsm), GFP_NOFS);
13984 +       if (br->br_fhsm)
13985 +               au_br_fhsm_init(br->br_fhsm);
13986 +       else
13987 +               err = -ENOMEM;
13988 +
13989 +       return err;
13990 +}
13991 +
13992 +/* ---------------------------------------------------------------------- */
13993 +
13994 +void au_fhsm_fin(struct super_block *sb)
13995 +{
13996 +       au_fhsm_notify(sb, /*val*/-1);
13997 +}
13998 +
13999 +void au_fhsm_init(struct au_sbinfo *sbinfo)
14000 +{
14001 +       struct au_fhsm *fhsm;
14002 +
14003 +       fhsm = &sbinfo->si_fhsm;
14004 +       spin_lock_init(&fhsm->fhsm_spin);
14005 +       init_waitqueue_head(&fhsm->fhsm_wqh);
14006 +       atomic_set(&fhsm->fhsm_readable, 0);
14007 +       fhsm->fhsm_expire
14008 +               = msecs_to_jiffies(AUFS_FHSM_CACHE_DEF_SEC * MSEC_PER_SEC);
14009 +       fhsm->fhsm_bottom = -1;
14010 +}
14011 +
14012 +void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec)
14013 +{
14014 +       sbinfo->si_fhsm.fhsm_expire
14015 +               = msecs_to_jiffies(sec * MSEC_PER_SEC);
14016 +}
14017 +
14018 +void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo)
14019 +{
14020 +       unsigned int u;
14021 +
14022 +       if (!au_ftest_si(sbinfo, FHSM))
14023 +               return;
14024 +
14025 +       u = jiffies_to_msecs(sbinfo->si_fhsm.fhsm_expire) / MSEC_PER_SEC;
14026 +       if (u != AUFS_FHSM_CACHE_DEF_SEC)
14027 +               seq_printf(seq, ",fhsm_sec=%u", u);
14028 +}
14029 diff -urN /usr/share/empty/fs/aufs/file.c linux/fs/aufs/file.c
14030 --- /usr/share/empty/fs/aufs/file.c     1970-01-01 01:00:00.000000000 +0100
14031 +++ linux/fs/aufs/file.c        2022-03-21 14:49:05.723299677 +0100
14032 @@ -0,0 +1,863 @@
14033 +// SPDX-License-Identifier: GPL-2.0
14034 +/*
14035 + * Copyright (C) 2005-2021 Junjiro R. Okajima
14036 + *
14037 + * This program, aufs is free software; you can redistribute it and/or modify
14038 + * it under the terms of the GNU General Public License as published by
14039 + * the Free Software Foundation; either version 2 of the License, or
14040 + * (at your option) any later version.
14041 + *
14042 + * This program is distributed in the hope that it will be useful,
14043 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
14044 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14045 + * GNU General Public License for more details.
14046 + *
14047 + * You should have received a copy of the GNU General Public License
14048 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
14049 + */
14050 +
14051 +/*
14052 + * handling file/dir, and address_space operation
14053 + */
14054 +
14055 +#ifdef CONFIG_AUFS_DEBUG
14056 +#include <linux/migrate.h>
14057 +#endif
14058 +#include <linux/pagemap.h>
14059 +#include "aufs.h"
14060 +
14061 +/* drop flags for writing */
14062 +unsigned int au_file_roflags(unsigned int flags)
14063 +{
14064 +       flags &= ~(O_WRONLY | O_RDWR | O_APPEND | O_CREAT | O_TRUNC);
14065 +       flags |= O_RDONLY | O_NOATIME;
14066 +       return flags;
14067 +}
14068 +
14069 +/* common functions to regular file and dir */
14070 +struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
14071 +                      struct file *file, int force_wr)
14072 +{
14073 +       struct file *h_file;
14074 +       struct dentry *h_dentry;
14075 +       struct inode *h_inode;
14076 +       struct super_block *sb;
14077 +       struct au_branch *br;
14078 +       struct path h_path;
14079 +       int err;
14080 +
14081 +       /* a race condition can happen between open and unlink/rmdir */
14082 +       h_file = ERR_PTR(-ENOENT);
14083 +       h_dentry = au_h_dptr(dentry, bindex);
14084 +       if (au_test_nfsd() && (!h_dentry || d_is_negative(h_dentry)))
14085 +               goto out;
14086 +       h_inode = d_inode(h_dentry);
14087 +       spin_lock(&h_dentry->d_lock);
14088 +       err = (!d_unhashed(dentry) && d_unlinked(h_dentry))
14089 +               /* || !d_inode(dentry)->i_nlink */
14090 +               ;
14091 +       spin_unlock(&h_dentry->d_lock);
14092 +       if (unlikely(err))
14093 +               goto out;
14094 +
14095 +       sb = dentry->d_sb;
14096 +       br = au_sbr(sb, bindex);
14097 +       err = au_br_test_oflag(flags, br);
14098 +       h_file = ERR_PTR(err);
14099 +       if (unlikely(err))
14100 +               goto out;
14101 +
14102 +       /* drop flags for writing */
14103 +       if (au_test_ro(sb, bindex, d_inode(dentry))) {
14104 +               if (force_wr && !(flags & O_WRONLY))
14105 +                       force_wr = 0;
14106 +               flags = au_file_roflags(flags);
14107 +               if (force_wr) {
14108 +                       h_file = ERR_PTR(-EROFS);
14109 +                       flags = au_file_roflags(flags);
14110 +                       if (unlikely(vfsub_native_ro(h_inode)
14111 +                                    || IS_APPEND(h_inode)))
14112 +                               goto out;
14113 +                       flags &= ~O_ACCMODE;
14114 +                       flags |= O_WRONLY;
14115 +               }
14116 +       }
14117 +       flags &= ~O_CREAT;
14118 +       au_lcnt_inc(&br->br_nfiles);
14119 +       h_path.dentry = h_dentry;
14120 +       h_path.mnt = au_br_mnt(br);
14121 +       h_file = vfsub_dentry_open(&h_path, flags);
14122 +       if (IS_ERR(h_file))
14123 +               goto out_br;
14124 +
14125 +       if (flags & __FMODE_EXEC) {
14126 +               err = deny_write_access(h_file);
14127 +               if (unlikely(err)) {
14128 +                       fput(h_file);
14129 +                       h_file = ERR_PTR(err);
14130 +                       goto out_br;
14131 +               }
14132 +       }
14133 +       fsnotify_open(h_file);
14134 +       goto out; /* success */
14135 +
14136 +out_br:
14137 +       au_lcnt_dec(&br->br_nfiles);
14138 +out:
14139 +       return h_file;
14140 +}
14141 +
14142 +static int au_cmoo(struct dentry *dentry)
14143 +{
14144 +       int err, cmoo, matched;
14145 +       unsigned int udba;
14146 +       struct path h_path;
14147 +       struct au_pin pin;
14148 +       struct au_cp_generic cpg = {
14149 +               .dentry = dentry,
14150 +               .bdst   = -1,
14151 +               .bsrc   = -1,
14152 +               .len    = -1,
14153 +               .pin    = &pin,
14154 +               .flags  = AuCpup_DTIME | AuCpup_HOPEN
14155 +       };
14156 +       struct inode *delegated;
14157 +       struct super_block *sb;
14158 +       struct au_sbinfo *sbinfo;
14159 +       struct au_fhsm *fhsm;
14160 +       pid_t pid;
14161 +       struct au_branch *br;
14162 +       struct dentry *parent;
14163 +       struct au_hinode *hdir;
14164 +
14165 +       DiMustWriteLock(dentry);
14166 +       IiMustWriteLock(d_inode(dentry));
14167 +
14168 +       err = 0;
14169 +       if (IS_ROOT(dentry))
14170 +               goto out;
14171 +       cpg.bsrc = au_dbtop(dentry);
14172 +       if (!cpg.bsrc)
14173 +               goto out;
14174 +
14175 +       sb = dentry->d_sb;
14176 +       sbinfo = au_sbi(sb);
14177 +       fhsm = &sbinfo->si_fhsm;
14178 +       pid = au_fhsm_pid(fhsm);
14179 +       rcu_read_lock();
14180 +       matched = (pid
14181 +                  && (current->pid == pid
14182 +                      || rcu_dereference(current->real_parent)->pid == pid));
14183 +       rcu_read_unlock();
14184 +       if (matched)
14185 +               goto out;
14186 +
14187 +       br = au_sbr(sb, cpg.bsrc);
14188 +       cmoo = au_br_cmoo(br->br_perm);
14189 +       if (!cmoo)
14190 +               goto out;
14191 +       if (!d_is_reg(dentry))
14192 +               cmoo &= AuBrAttr_COO_ALL;
14193 +       if (!cmoo)
14194 +               goto out;
14195 +
14196 +       parent = dget_parent(dentry);
14197 +       di_write_lock_parent(parent);
14198 +       err = au_wbr_do_copyup_bu(dentry, cpg.bsrc - 1);
14199 +       cpg.bdst = err;
14200 +       if (unlikely(err < 0)) {
14201 +               err = 0;        /* there is no upper writable branch */
14202 +               goto out_dgrade;
14203 +       }
14204 +       AuDbg("bsrc %d, bdst %d\n", cpg.bsrc, cpg.bdst);
14205 +
14206 +       /* do not respect the coo attrib for the target branch */
14207 +       err = au_cpup_dirs(dentry, cpg.bdst);
14208 +       if (unlikely(err))
14209 +               goto out_dgrade;
14210 +
14211 +       di_downgrade_lock(parent, AuLock_IR);
14212 +       udba = au_opt_udba(sb);
14213 +       err = au_pin(&pin, dentry, cpg.bdst, udba,
14214 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14215 +       if (unlikely(err))
14216 +               goto out_parent;
14217 +
14218 +       err = au_sio_cpup_simple(&cpg);
14219 +       au_unpin(&pin);
14220 +       if (unlikely(err))
14221 +               goto out_parent;
14222 +       if (!(cmoo & AuBrWAttr_MOO))
14223 +               goto out_parent; /* success */
14224 +
14225 +       err = au_pin(&pin, dentry, cpg.bsrc, udba,
14226 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14227 +       if (unlikely(err))
14228 +               goto out_parent;
14229 +
14230 +       h_path.mnt = au_br_mnt(br);
14231 +       h_path.dentry = au_h_dptr(dentry, cpg.bsrc);
14232 +       hdir = au_hi(d_inode(parent), cpg.bsrc);
14233 +       delegated = NULL;
14234 +       err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated, /*force*/1);
14235 +       au_unpin(&pin);
14236 +       /* todo: keep h_dentry or not? */
14237 +       if (unlikely(err == -EWOULDBLOCK)) {
14238 +               pr_warn("cannot retry for NFSv4 delegation"
14239 +                       " for an internal unlink\n");
14240 +               iput(delegated);
14241 +       }
14242 +       if (unlikely(err)) {
14243 +               pr_err("unlink %pd after coo failed (%d), ignored\n",
14244 +                      dentry, err);
14245 +               err = 0;
14246 +       }
14247 +       goto out_parent; /* success */
14248 +
14249 +out_dgrade:
14250 +       di_downgrade_lock(parent, AuLock_IR);
14251 +out_parent:
14252 +       di_read_unlock(parent, AuLock_IR);
14253 +       dput(parent);
14254 +out:
14255 +       AuTraceErr(err);
14256 +       return err;
14257 +}
14258 +
14259 +int au_do_open(struct file *file, struct au_do_open_args *args)
14260 +{
14261 +       int err, aopen = args->aopen;
14262 +       struct dentry *dentry;
14263 +       struct au_finfo *finfo;
14264 +
14265 +       if (!aopen)
14266 +               err = au_finfo_init(file, args->fidir);
14267 +       else {
14268 +               lockdep_off();
14269 +               err = au_finfo_init(file, args->fidir);
14270 +               lockdep_on();
14271 +       }
14272 +       if (unlikely(err))
14273 +               goto out;
14274 +
14275 +       dentry = file->f_path.dentry;
14276 +       AuDebugOn(IS_ERR_OR_NULL(dentry));
14277 +       di_write_lock_child(dentry);
14278 +       err = au_cmoo(dentry);
14279 +       di_downgrade_lock(dentry, AuLock_IR);
14280 +       if (!err) {
14281 +               if (!aopen)
14282 +                       err = args->open(file, vfsub_file_flags(file), NULL);
14283 +               else {
14284 +                       lockdep_off();
14285 +                       err = args->open(file, vfsub_file_flags(file),
14286 +                                        args->h_file);
14287 +                       lockdep_on();
14288 +               }
14289 +       }
14290 +       di_read_unlock(dentry, AuLock_IR);
14291 +
14292 +       finfo = au_fi(file);
14293 +       if (!err) {
14294 +               finfo->fi_file = file;
14295 +               au_hbl_add(&finfo->fi_hlist,
14296 +                          &au_sbi(file->f_path.dentry->d_sb)->si_files);
14297 +       }
14298 +       if (!aopen)
14299 +               fi_write_unlock(file);
14300 +       else {
14301 +               lockdep_off();
14302 +               fi_write_unlock(file);
14303 +               lockdep_on();
14304 +       }
14305 +       if (unlikely(err)) {
14306 +               finfo->fi_hdir = NULL;
14307 +               au_finfo_fin(file);
14308 +       }
14309 +
14310 +out:
14311 +       AuTraceErr(err);
14312 +       return err;
14313 +}
14314 +
14315 +int au_reopen_nondir(struct file *file)
14316 +{
14317 +       int err;
14318 +       aufs_bindex_t btop;
14319 +       struct dentry *dentry;
14320 +       struct au_branch *br;
14321 +       struct file *h_file, *h_file_tmp;
14322 +
14323 +       dentry = file->f_path.dentry;
14324 +       btop = au_dbtop(dentry);
14325 +       br = au_sbr(dentry->d_sb, btop);
14326 +       h_file_tmp = NULL;
14327 +       if (au_fbtop(file) == btop) {
14328 +               h_file = au_hf_top(file);
14329 +               if (file->f_mode == h_file->f_mode)
14330 +                       return 0; /* success */
14331 +               h_file_tmp = h_file;
14332 +               get_file(h_file_tmp);
14333 +               au_lcnt_inc(&br->br_nfiles);
14334 +               au_set_h_fptr(file, btop, NULL);
14335 +       }
14336 +       AuDebugOn(au_fi(file)->fi_hdir);
14337 +       /*
14338 +        * it can happen
14339 +        * file exists on both of rw and ro
14340 +        * open --> dbtop and fbtop are both 0
14341 +        * prepend a branch as rw, "rw" become ro
14342 +        * remove rw/file
14343 +        * delete the top branch, "rw" becomes rw again
14344 +        *      --> dbtop is 1, fbtop is still 0
14345 +        * write --> fbtop is 0 but dbtop is 1
14346 +        */
14347 +       /* AuDebugOn(au_fbtop(file) < btop); */
14348 +
14349 +       h_file = au_h_open(dentry, btop, vfsub_file_flags(file) & ~O_TRUNC,
14350 +                          file, /*force_wr*/0);
14351 +       err = PTR_ERR(h_file);
14352 +       if (IS_ERR(h_file)) {
14353 +               if (h_file_tmp) {
14354 +                       /* revert */
14355 +                       au_set_h_fptr(file, btop, h_file_tmp);
14356 +                       h_file_tmp = NULL;
14357 +               }
14358 +               goto out; /* todo: close all? */
14359 +       }
14360 +
14361 +       err = 0;
14362 +       au_set_fbtop(file, btop);
14363 +       au_set_h_fptr(file, btop, h_file);
14364 +       au_update_figen(file);
14365 +       /* todo: necessary? */
14366 +       /* file->f_ra = h_file->f_ra; */
14367 +
14368 +out:
14369 +       if (h_file_tmp) {
14370 +               fput(h_file_tmp);
14371 +               au_lcnt_dec(&br->br_nfiles);
14372 +       }
14373 +       return err;
14374 +}
14375 +
14376 +/* ---------------------------------------------------------------------- */
14377 +
14378 +static int au_reopen_wh(struct file *file, aufs_bindex_t btgt,
14379 +                       struct dentry *hi_wh)
14380 +{
14381 +       int err;
14382 +       aufs_bindex_t btop;
14383 +       struct au_dinfo *dinfo;
14384 +       struct dentry *h_dentry;
14385 +       struct au_hdentry *hdp;
14386 +
14387 +       dinfo = au_di(file->f_path.dentry);
14388 +       AuRwMustWriteLock(&dinfo->di_rwsem);
14389 +
14390 +       btop = dinfo->di_btop;
14391 +       dinfo->di_btop = btgt;
14392 +       hdp = au_hdentry(dinfo, btgt);
14393 +       h_dentry = hdp->hd_dentry;
14394 +       hdp->hd_dentry = hi_wh;
14395 +       err = au_reopen_nondir(file);
14396 +       hdp->hd_dentry = h_dentry;
14397 +       dinfo->di_btop = btop;
14398 +
14399 +       return err;
14400 +}
14401 +
14402 +static int au_ready_to_write_wh(struct file *file, loff_t len,
14403 +                               aufs_bindex_t bcpup, struct au_pin *pin)
14404 +{
14405 +       int err;
14406 +       struct inode *inode, *h_inode;
14407 +       struct dentry *h_dentry, *hi_wh;
14408 +       struct au_cp_generic cpg = {
14409 +               .dentry = file->f_path.dentry,
14410 +               .bdst   = bcpup,
14411 +               .bsrc   = -1,
14412 +               .len    = len,
14413 +               .pin    = pin
14414 +       };
14415 +
14416 +       au_update_dbtop(cpg.dentry);
14417 +       inode = d_inode(cpg.dentry);
14418 +       h_inode = NULL;
14419 +       if (au_dbtop(cpg.dentry) <= bcpup
14420 +           && au_dbbot(cpg.dentry) >= bcpup) {
14421 +               h_dentry = au_h_dptr(cpg.dentry, bcpup);
14422 +               if (h_dentry && d_is_positive(h_dentry))
14423 +                       h_inode = d_inode(h_dentry);
14424 +       }
14425 +       hi_wh = au_hi_wh(inode, bcpup);
14426 +       if (!hi_wh && !h_inode)
14427 +               err = au_sio_cpup_wh(&cpg, file);
14428 +       else
14429 +               /* already copied-up after unlink */
14430 +               err = au_reopen_wh(file, bcpup, hi_wh);
14431 +
14432 +       if (!err
14433 +           && (inode->i_nlink > 1
14434 +               || (inode->i_state & I_LINKABLE))
14435 +           && au_opt_test(au_mntflags(cpg.dentry->d_sb), PLINK))
14436 +               au_plink_append(inode, bcpup, au_h_dptr(cpg.dentry, bcpup));
14437 +
14438 +       return err;
14439 +}
14440 +
14441 +/*
14442 + * prepare the @file for writing.
14443 + */
14444 +int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin)
14445 +{
14446 +       int err;
14447 +       aufs_bindex_t dbtop;
14448 +       struct dentry *parent;
14449 +       struct inode *inode;
14450 +       struct super_block *sb;
14451 +       struct file *h_file;
14452 +       struct au_cp_generic cpg = {
14453 +               .dentry = file->f_path.dentry,
14454 +               .bdst   = -1,
14455 +               .bsrc   = -1,
14456 +               .len    = len,
14457 +               .pin    = pin,
14458 +               .flags  = AuCpup_DTIME
14459 +       };
14460 +
14461 +       sb = cpg.dentry->d_sb;
14462 +       inode = d_inode(cpg.dentry);
14463 +       cpg.bsrc = au_fbtop(file);
14464 +       err = au_test_ro(sb, cpg.bsrc, inode);
14465 +       if (!err && (au_hf_top(file)->f_mode & FMODE_WRITE)) {
14466 +               err = au_pin(pin, cpg.dentry, cpg.bsrc, AuOpt_UDBA_NONE,
14467 +                            /*flags*/0);
14468 +               goto out;
14469 +       }
14470 +
14471 +       /* need to cpup or reopen */
14472 +       parent = dget_parent(cpg.dentry);
14473 +       di_write_lock_parent(parent);
14474 +       err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
14475 +       cpg.bdst = err;
14476 +       if (unlikely(err < 0))
14477 +               goto out_dgrade;
14478 +       err = 0;
14479 +
14480 +       if (!d_unhashed(cpg.dentry) && !au_h_dptr(parent, cpg.bdst)) {
14481 +               err = au_cpup_dirs(cpg.dentry, cpg.bdst);
14482 +               if (unlikely(err))
14483 +                       goto out_dgrade;
14484 +       }
14485 +
14486 +       err = au_pin(pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
14487 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14488 +       if (unlikely(err))
14489 +               goto out_dgrade;
14490 +
14491 +       dbtop = au_dbtop(cpg.dentry);
14492 +       if (dbtop <= cpg.bdst)
14493 +               cpg.bsrc = cpg.bdst;
14494 +
14495 +       if (dbtop <= cpg.bdst           /* just reopen */
14496 +           || !d_unhashed(cpg.dentry)  /* copyup and reopen */
14497 +               ) {
14498 +               h_file = au_h_open_pre(cpg.dentry, cpg.bsrc, /*force_wr*/0);
14499 +               if (IS_ERR(h_file))
14500 +                       err = PTR_ERR(h_file);
14501 +               else {
14502 +                       di_downgrade_lock(parent, AuLock_IR);
14503 +                       if (dbtop > cpg.bdst)
14504 +                               err = au_sio_cpup_simple(&cpg);
14505 +                       if (!err)
14506 +                               err = au_reopen_nondir(file);
14507 +                       au_h_open_post(cpg.dentry, cpg.bsrc, h_file);
14508 +               }
14509 +       } else {                        /* copyup as wh and reopen */
14510 +               /*
14511 +                * since writable hfsplus branch is not supported,
14512 +                * h_open_pre/post() are unnecessary.
14513 +                */
14514 +               err = au_ready_to_write_wh(file, len, cpg.bdst, pin);
14515 +               di_downgrade_lock(parent, AuLock_IR);
14516 +       }
14517 +
14518 +       if (!err) {
14519 +               au_pin_set_parent_lflag(pin, /*lflag*/0);
14520 +               goto out_dput; /* success */
14521 +       }
14522 +       au_unpin(pin);
14523 +       goto out_unlock;
14524 +
14525 +out_dgrade:
14526 +       di_downgrade_lock(parent, AuLock_IR);
14527 +out_unlock:
14528 +       di_read_unlock(parent, AuLock_IR);
14529 +out_dput:
14530 +       dput(parent);
14531 +out:
14532 +       return err;
14533 +}
14534 +
14535 +/* ---------------------------------------------------------------------- */
14536 +
14537 +int au_do_flush(struct file *file, fl_owner_t id,
14538 +               int (*flush)(struct file *file, fl_owner_t id))
14539 +{
14540 +       int err;
14541 +       struct super_block *sb;
14542 +       struct inode *inode;
14543 +
14544 +       inode = file_inode(file);
14545 +       sb = inode->i_sb;
14546 +       si_noflush_read_lock(sb);
14547 +       fi_read_lock(file);
14548 +       ii_read_lock_child(inode);
14549 +
14550 +       err = flush(file, id);
14551 +       au_cpup_attr_timesizes(inode);
14552 +
14553 +       ii_read_unlock(inode);
14554 +       fi_read_unlock(file);
14555 +       si_read_unlock(sb);
14556 +       return err;
14557 +}
14558 +
14559 +/* ---------------------------------------------------------------------- */
14560 +
14561 +static int au_file_refresh_by_inode(struct file *file, int *need_reopen)
14562 +{
14563 +       int err;
14564 +       struct au_pin pin;
14565 +       struct au_finfo *finfo;
14566 +       struct dentry *parent, *hi_wh;
14567 +       struct inode *inode;
14568 +       struct super_block *sb;
14569 +       struct au_cp_generic cpg = {
14570 +               .dentry = file->f_path.dentry,
14571 +               .bdst   = -1,
14572 +               .bsrc   = -1,
14573 +               .len    = -1,
14574 +               .pin    = &pin,
14575 +               .flags  = AuCpup_DTIME
14576 +       };
14577 +
14578 +       FiMustWriteLock(file);
14579 +
14580 +       err = 0;
14581 +       finfo = au_fi(file);
14582 +       sb = cpg.dentry->d_sb;
14583 +       inode = d_inode(cpg.dentry);
14584 +       cpg.bdst = au_ibtop(inode);
14585 +       if (cpg.bdst == finfo->fi_btop || IS_ROOT(cpg.dentry))
14586 +               goto out;
14587 +
14588 +       parent = dget_parent(cpg.dentry);
14589 +       if (au_test_ro(sb, cpg.bdst, inode)) {
14590 +               di_read_lock_parent(parent, !AuLock_IR);
14591 +               err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
14592 +               cpg.bdst = err;
14593 +               di_read_unlock(parent, !AuLock_IR);
14594 +               if (unlikely(err < 0))
14595 +                       goto out_parent;
14596 +               err = 0;
14597 +       }
14598 +
14599 +       di_read_lock_parent(parent, AuLock_IR);
14600 +       hi_wh = au_hi_wh(inode, cpg.bdst);
14601 +       if (!S_ISDIR(inode->i_mode)
14602 +           && au_opt_test(au_mntflags(sb), PLINK)
14603 +           && au_plink_test(inode)
14604 +           && !d_unhashed(cpg.dentry)
14605 +           && cpg.bdst < au_dbtop(cpg.dentry)) {
14606 +               err = au_test_and_cpup_dirs(cpg.dentry, cpg.bdst);
14607 +               if (unlikely(err))
14608 +                       goto out_unlock;
14609 +
14610 +               /* always superio. */
14611 +               err = au_pin(&pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
14612 +                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14613 +               if (!err) {
14614 +                       err = au_sio_cpup_simple(&cpg);
14615 +                       au_unpin(&pin);
14616 +               }
14617 +       } else if (hi_wh) {
14618 +               /* already copied-up after unlink */
14619 +               err = au_reopen_wh(file, cpg.bdst, hi_wh);
14620 +               *need_reopen = 0;
14621 +       }
14622 +
14623 +out_unlock:
14624 +       di_read_unlock(parent, AuLock_IR);
14625 +out_parent:
14626 +       dput(parent);
14627 +out:
14628 +       return err;
14629 +}
14630 +
14631 +static void au_do_refresh_dir(struct file *file)
14632 +{
14633 +       aufs_bindex_t bindex, bbot, new_bindex, brid;
14634 +       struct au_hfile *p, tmp, *q;
14635 +       struct au_finfo *finfo;
14636 +       struct super_block *sb;
14637 +       struct au_fidir *fidir;
14638 +
14639 +       FiMustWriteLock(file);
14640 +
14641 +       sb = file->f_path.dentry->d_sb;
14642 +       finfo = au_fi(file);
14643 +       fidir = finfo->fi_hdir;
14644 +       AuDebugOn(!fidir);
14645 +       p = fidir->fd_hfile + finfo->fi_btop;
14646 +       brid = p->hf_br->br_id;
14647 +       bbot = fidir->fd_bbot;
14648 +       for (bindex = finfo->fi_btop; bindex <= bbot; bindex++, p++) {
14649 +               if (!p->hf_file)
14650 +                       continue;
14651 +
14652 +               new_bindex = au_br_index(sb, p->hf_br->br_id);
14653 +               if (new_bindex == bindex)
14654 +                       continue;
14655 +               if (new_bindex < 0) {
14656 +                       au_set_h_fptr(file, bindex, NULL);
14657 +                       continue;
14658 +               }
14659 +
14660 +               /* swap two lower inode, and loop again */
14661 +               q = fidir->fd_hfile + new_bindex;
14662 +               tmp = *q;
14663 +               *q = *p;
14664 +               *p = tmp;
14665 +               if (tmp.hf_file) {
14666 +                       bindex--;
14667 +                       p--;
14668 +               }
14669 +       }
14670 +
14671 +       p = fidir->fd_hfile;
14672 +       if (!au_test_mmapped(file) && !d_unlinked(file->f_path.dentry)) {
14673 +               bbot = au_sbbot(sb);
14674 +               for (finfo->fi_btop = 0; finfo->fi_btop <= bbot;
14675 +                    finfo->fi_btop++, p++)
14676 +                       if (p->hf_file) {
14677 +                               if (file_inode(p->hf_file))
14678 +                                       break;
14679 +                               au_hfput(p, /*execed*/0);
14680 +                       }
14681 +       } else {
14682 +               bbot = au_br_index(sb, brid);
14683 +               for (finfo->fi_btop = 0; finfo->fi_btop < bbot;
14684 +                    finfo->fi_btop++, p++)
14685 +                       if (p->hf_file)
14686 +                               au_hfput(p, /*execed*/0);
14687 +               bbot = au_sbbot(sb);
14688 +       }
14689 +
14690 +       p = fidir->fd_hfile + bbot;
14691 +       for (fidir->fd_bbot = bbot; fidir->fd_bbot >= finfo->fi_btop;
14692 +            fidir->fd_bbot--, p--)
14693 +               if (p->hf_file) {
14694 +                       if (file_inode(p->hf_file))
14695 +                               break;
14696 +                       au_hfput(p, /*execed*/0);
14697 +               }
14698 +       AuDebugOn(fidir->fd_bbot < finfo->fi_btop);
14699 +}
14700 +
14701 +/*
14702 + * after branch manipulating, refresh the file.
14703 + */
14704 +static int refresh_file(struct file *file, int (*reopen)(struct file *file))
14705 +{
14706 +       int err, need_reopen, nbr;
14707 +       aufs_bindex_t bbot, bindex;
14708 +       struct dentry *dentry;
14709 +       struct super_block *sb;
14710 +       struct au_finfo *finfo;
14711 +       struct au_hfile *hfile;
14712 +
14713 +       dentry = file->f_path.dentry;
14714 +       sb = dentry->d_sb;
14715 +       nbr = au_sbbot(sb) + 1;
14716 +       finfo = au_fi(file);
14717 +       if (!finfo->fi_hdir) {
14718 +               hfile = &finfo->fi_htop;
14719 +               AuDebugOn(!hfile->hf_file);
14720 +               bindex = au_br_index(sb, hfile->hf_br->br_id);
14721 +               AuDebugOn(bindex < 0);
14722 +               if (bindex != finfo->fi_btop)
14723 +                       au_set_fbtop(file, bindex);
14724 +       } else {
14725 +               err = au_fidir_realloc(finfo, nbr, /*may_shrink*/0);
14726 +               if (unlikely(err))
14727 +                       goto out;
14728 +               au_do_refresh_dir(file);
14729 +       }
14730 +
14731 +       err = 0;
14732 +       need_reopen = 1;
14733 +       if (!au_test_mmapped(file))
14734 +               err = au_file_refresh_by_inode(file, &need_reopen);
14735 +       if (finfo->fi_hdir)
14736 +               /* harmless if err */
14737 +               au_fidir_realloc(finfo, nbr, /*may_shrink*/1);
14738 +       if (!err && need_reopen && !d_unlinked(dentry))
14739 +               err = reopen(file);
14740 +       if (!err) {
14741 +               au_update_figen(file);
14742 +               goto out; /* success */
14743 +       }
14744 +
14745 +       /* error, close all lower files */
14746 +       if (finfo->fi_hdir) {
14747 +               bbot = au_fbbot_dir(file);
14748 +               for (bindex = au_fbtop(file); bindex <= bbot; bindex++)
14749 +                       au_set_h_fptr(file, bindex, NULL);
14750 +       }
14751 +
14752 +out:
14753 +       return err;
14754 +}
14755 +
14756 +/* common function to regular file and dir */
14757 +int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
14758 +                         int wlock, unsigned int fi_lsc)
14759 +{
14760 +       int err;
14761 +       unsigned int sigen, figen;
14762 +       aufs_bindex_t btop;
14763 +       unsigned char pseudo_link;
14764 +       struct dentry *dentry;
14765 +       struct inode *inode;
14766 +
14767 +       err = 0;
14768 +       dentry = file->f_path.dentry;
14769 +       inode = d_inode(dentry);
14770 +       sigen = au_sigen(dentry->d_sb);
14771 +       fi_write_lock_nested(file, fi_lsc);
14772 +       figen = au_figen(file);
14773 +       if (!fi_lsc)
14774 +               di_write_lock_child(dentry);
14775 +       else
14776 +               di_write_lock_child2(dentry);
14777 +       btop = au_dbtop(dentry);
14778 +       pseudo_link = (btop != au_ibtop(inode));
14779 +       if (sigen == figen && !pseudo_link && au_fbtop(file) == btop) {
14780 +               if (!wlock) {
14781 +                       di_downgrade_lock(dentry, AuLock_IR);
14782 +                       fi_downgrade_lock(file);
14783 +               }
14784 +               goto out; /* success */
14785 +       }
14786 +
14787 +       AuDbg("sigen %d, figen %d\n", sigen, figen);
14788 +       if (au_digen_test(dentry, sigen)) {
14789 +               err = au_reval_dpath(dentry, sigen);
14790 +               AuDebugOn(!err && au_digen_test(dentry, sigen));
14791 +       }
14792 +
14793 +       if (!err)
14794 +               err = refresh_file(file, reopen);
14795 +       if (!err) {
14796 +               if (!wlock) {
14797 +                       di_downgrade_lock(dentry, AuLock_IR);
14798 +                       fi_downgrade_lock(file);
14799 +               }
14800 +       } else {
14801 +               di_write_unlock(dentry);
14802 +               fi_write_unlock(file);
14803 +       }
14804 +
14805 +out:
14806 +       return err;
14807 +}
14808 +
14809 +/* ---------------------------------------------------------------------- */
14810 +
14811 +/* cf. aufs_nopage() */
14812 +/* for madvise(2) */
14813 +static int aufs_readpage(struct file *file __maybe_unused, struct page *page)
14814 +{
14815 +       unlock_page(page);
14816 +       return 0;
14817 +}
14818 +
14819 +/* it will never be called, but necessary to support O_DIRECT */
14820 +static ssize_t aufs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
14821 +{ BUG(); return 0; }
14822 +
14823 +/* they will never be called. */
14824 +#ifdef CONFIG_AUFS_DEBUG
14825 +static int aufs_write_begin(struct file *file, struct address_space *mapping,
14826 +                           loff_t pos, unsigned len, unsigned flags,
14827 +                           struct page **pagep, void **fsdata)
14828 +{ AuUnsupport(); return 0; }
14829 +static int aufs_write_end(struct file *file, struct address_space *mapping,
14830 +                         loff_t pos, unsigned len, unsigned copied,
14831 +                         struct page *page, void *fsdata)
14832 +{ AuUnsupport(); return 0; }
14833 +static int aufs_writepage(struct page *page, struct writeback_control *wbc)
14834 +{ AuUnsupport(); return 0; }
14835 +
14836 +static int aufs_set_page_dirty(struct page *page)
14837 +{ AuUnsupport(); return 0; }
14838 +static void aufs_invalidatepage(struct page *page, unsigned int offset,
14839 +                               unsigned int length)
14840 +{ AuUnsupport(); }
14841 +static int aufs_releasepage(struct page *page, gfp_t gfp)
14842 +{ AuUnsupport(); return 0; }
14843 +#if 0 /* called by memory compaction regardless file */
14844 +static int aufs_migratepage(struct address_space *mapping, struct page *newpage,
14845 +                           struct page *page, enum migrate_mode mode)
14846 +{ AuUnsupport(); return 0; }
14847 +#endif
14848 +static bool aufs_isolate_page(struct page *page, isolate_mode_t mode)
14849 +{ AuUnsupport(); return true; }
14850 +static void aufs_putback_page(struct page *page)
14851 +{ AuUnsupport(); }
14852 +static int aufs_launder_page(struct page *page)
14853 +{ AuUnsupport(); return 0; }
14854 +static int aufs_is_partially_uptodate(struct page *page,
14855 +                                     unsigned long from,
14856 +                                     unsigned long count)
14857 +{ AuUnsupport(); return 0; }
14858 +static void aufs_is_dirty_writeback(struct page *page, bool *dirty,
14859 +                                   bool *writeback)
14860 +{ AuUnsupport(); }
14861 +static int aufs_error_remove_page(struct address_space *mapping,
14862 +                                 struct page *page)
14863 +{ AuUnsupport(); return 0; }
14864 +static int aufs_swap_activate(struct swap_info_struct *sis, struct file *file,
14865 +                             sector_t *span)
14866 +{ AuUnsupport(); return 0; }
14867 +static void aufs_swap_deactivate(struct file *file)
14868 +{ AuUnsupport(); }
14869 +#endif /* CONFIG_AUFS_DEBUG */
14870 +
14871 +const struct address_space_operations aufs_aop = {
14872 +       .readpage               = aufs_readpage,
14873 +       .direct_IO              = aufs_direct_IO,
14874 +#ifdef CONFIG_AUFS_DEBUG
14875 +       .writepage              = aufs_writepage,
14876 +       /* no writepages, because of writepage */
14877 +       .set_page_dirty         = aufs_set_page_dirty,
14878 +       /* no readpages, because of readpage */
14879 +       .write_begin            = aufs_write_begin,
14880 +       .write_end              = aufs_write_end,
14881 +       /* no bmap, no block device */
14882 +       .invalidatepage         = aufs_invalidatepage,
14883 +       .releasepage            = aufs_releasepage,
14884 +       /* is fallback_migrate_page ok? */
14885 +       /* .migratepage         = aufs_migratepage, */
14886 +       .isolate_page           = aufs_isolate_page,
14887 +       .putback_page           = aufs_putback_page,
14888 +       .launder_page           = aufs_launder_page,
14889 +       .is_partially_uptodate  = aufs_is_partially_uptodate,
14890 +       .is_dirty_writeback     = aufs_is_dirty_writeback,
14891 +       .error_remove_page      = aufs_error_remove_page,
14892 +       .swap_activate          = aufs_swap_activate,
14893 +       .swap_deactivate        = aufs_swap_deactivate
14894 +#endif /* CONFIG_AUFS_DEBUG */
14895 +};
14896 diff -urN /usr/share/empty/fs/aufs/file.h linux/fs/aufs/file.h
14897 --- /usr/share/empty/fs/aufs/file.h     1970-01-01 01:00:00.000000000 +0100
14898 +++ linux/fs/aufs/file.h        2022-03-21 14:49:05.723299677 +0100
14899 @@ -0,0 +1,342 @@
14900 +/* SPDX-License-Identifier: GPL-2.0 */
14901 +/*
14902 + * Copyright (C) 2005-2021 Junjiro R. Okajima
14903 + *
14904 + * This program, aufs is free software; you can redistribute it and/or modify
14905 + * it under the terms of the GNU General Public License as published by
14906 + * the Free Software Foundation; either version 2 of the License, or
14907 + * (at your option) any later version.
14908 + *
14909 + * This program is distributed in the hope that it will be useful,
14910 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
14911 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14912 + * GNU General Public License for more details.
14913 + *
14914 + * You should have received a copy of the GNU General Public License
14915 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
14916 + */
14917 +
14918 +/*
14919 + * file operations
14920 + */
14921 +
14922 +#ifndef __AUFS_FILE_H__
14923 +#define __AUFS_FILE_H__
14924 +
14925 +#ifdef __KERNEL__
14926 +
14927 +#include <linux/file.h>
14928 +#include <linux/fs.h>
14929 +#include <linux/mm_types.h>
14930 +#include <linux/poll.h>
14931 +#include "rwsem.h"
14932 +
14933 +struct au_branch;
14934 +struct au_hfile {
14935 +       struct file             *hf_file;
14936 +       struct au_branch        *hf_br;
14937 +};
14938 +
14939 +struct au_vdir;
14940 +struct au_fidir {
14941 +       aufs_bindex_t           fd_bbot;
14942 +       aufs_bindex_t           fd_nent;
14943 +       struct au_vdir          *fd_vdir_cache;
14944 +       struct au_hfile         fd_hfile[];
14945 +};
14946 +
14947 +static inline int au_fidir_sz(int nent)
14948 +{
14949 +       AuDebugOn(nent < 0);
14950 +       return sizeof(struct au_fidir) + sizeof(struct au_hfile) * nent;
14951 +}
14952 +
14953 +struct au_finfo {
14954 +       atomic_t                fi_generation;
14955 +
14956 +       struct au_rwsem         fi_rwsem;
14957 +       aufs_bindex_t           fi_btop;
14958 +
14959 +       /* do not union them */
14960 +       struct {                                /* for non-dir */
14961 +               struct au_hfile                 fi_htop;
14962 +               atomic_t                        fi_mmapped;
14963 +       };
14964 +       struct au_fidir         *fi_hdir;       /* for dir only */
14965 +
14966 +       struct hlist_bl_node    fi_hlist;
14967 +       struct file             *fi_file;       /* very ugly */
14968 +       struct rcu_head         rcu;
14969 +} ____cacheline_aligned_in_smp;
14970 +
14971 +/* ---------------------------------------------------------------------- */
14972 +
14973 +/* file.c */
14974 +extern const struct address_space_operations aufs_aop;
14975 +unsigned int au_file_roflags(unsigned int flags);
14976 +struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
14977 +                      struct file *file, int force_wr);
14978 +struct au_do_open_args {
14979 +       int             aopen;
14980 +       int             (*open)(struct file *file, int flags,
14981 +                               struct file *h_file);
14982 +       struct au_fidir *fidir;
14983 +       struct file     *h_file;
14984 +};
14985 +int au_do_open(struct file *file, struct au_do_open_args *args);
14986 +int au_reopen_nondir(struct file *file);
14987 +struct au_pin;
14988 +int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin);
14989 +int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
14990 +                         int wlock, unsigned int fi_lsc);
14991 +int au_do_flush(struct file *file, fl_owner_t id,
14992 +               int (*flush)(struct file *file, fl_owner_t id));
14993 +
14994 +/* poll.c */
14995 +#ifdef CONFIG_AUFS_POLL
14996 +__poll_t aufs_poll(struct file *file, struct poll_table_struct *pt);
14997 +#endif
14998 +
14999 +#ifdef CONFIG_AUFS_BR_HFSPLUS
15000 +/* hfsplus.c */
15001 +struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
15002 +                          int force_wr);
15003 +void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
15004 +                   struct file *h_file);
15005 +#else
15006 +AuStub(struct file *, au_h_open_pre, return NULL, struct dentry *dentry,
15007 +       aufs_bindex_t bindex, int force_wr)
15008 +AuStubVoid(au_h_open_post, struct dentry *dentry, aufs_bindex_t bindex,
15009 +          struct file *h_file);
15010 +#endif
15011 +
15012 +/* f_op.c */
15013 +extern const struct file_operations aufs_file_fop;
15014 +int au_do_open_nondir(struct file *file, int flags, struct file *h_file);
15015 +int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file);
15016 +struct file *au_read_pre(struct file *file, int keep_fi, unsigned int lsc);
15017 +
15018 +/* finfo.c */
15019 +void au_hfput(struct au_hfile *hf, int execed);
15020 +void au_set_h_fptr(struct file *file, aufs_bindex_t bindex,
15021 +                  struct file *h_file);
15022 +
15023 +void au_update_figen(struct file *file);
15024 +struct au_fidir *au_fidir_alloc(struct super_block *sb);
15025 +int au_fidir_realloc(struct au_finfo *finfo, int nbr, int may_shrink);
15026 +
15027 +void au_fi_init_once(void *_fi);
15028 +void au_finfo_fin(struct file *file);
15029 +int au_finfo_init(struct file *file, struct au_fidir *fidir);
15030 +
15031 +/* ioctl.c */
15032 +long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg);
15033 +#ifdef CONFIG_COMPAT
15034 +long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
15035 +                          unsigned long arg);
15036 +long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
15037 +                             unsigned long arg);
15038 +#endif
15039 +
15040 +/* ---------------------------------------------------------------------- */
15041 +
15042 +static inline struct au_finfo *au_fi(struct file *file)
15043 +{
15044 +       return file->private_data;
15045 +}
15046 +
15047 +/* ---------------------------------------------------------------------- */
15048 +
15049 +#define fi_read_lock(f)        au_rw_read_lock(&au_fi(f)->fi_rwsem)
15050 +#define fi_write_lock(f)       au_rw_write_lock(&au_fi(f)->fi_rwsem)
15051 +#define fi_read_trylock(f)     au_rw_read_trylock(&au_fi(f)->fi_rwsem)
15052 +#define fi_write_trylock(f)    au_rw_write_trylock(&au_fi(f)->fi_rwsem)
15053 +/*
15054 +#define fi_read_trylock_nested(f) \
15055 +       au_rw_read_trylock_nested(&au_fi(f)->fi_rwsem)
15056 +#define fi_write_trylock_nested(f) \
15057 +       au_rw_write_trylock_nested(&au_fi(f)->fi_rwsem)
15058 +*/
15059 +
15060 +#define fi_read_unlock(f)      au_rw_read_unlock(&au_fi(f)->fi_rwsem)
15061 +#define fi_write_unlock(f)     au_rw_write_unlock(&au_fi(f)->fi_rwsem)
15062 +#define fi_downgrade_lock(f)   au_rw_dgrade_lock(&au_fi(f)->fi_rwsem)
15063 +
15064 +/* lock subclass for finfo */
15065 +enum {
15066 +       AuLsc_FI_1,
15067 +       AuLsc_FI_2
15068 +};
15069 +
15070 +static inline void fi_read_lock_nested(struct file *f, unsigned int lsc)
15071 +{
15072 +       au_rw_read_lock_nested(&au_fi(f)->fi_rwsem, lsc);
15073 +}
15074 +
15075 +static inline void fi_write_lock_nested(struct file *f, unsigned int lsc)
15076 +{
15077 +       au_rw_write_lock_nested(&au_fi(f)->fi_rwsem, lsc);
15078 +}
15079 +
15080 +/*
15081 + * fi_read_lock_1, fi_write_lock_1,
15082 + * fi_read_lock_2, fi_write_lock_2
15083 + */
15084 +#define AuReadLockFunc(name) \
15085 +static inline void fi_read_lock_##name(struct file *f) \
15086 +{ fi_read_lock_nested(f, AuLsc_FI_##name); }
15087 +
15088 +#define AuWriteLockFunc(name) \
15089 +static inline void fi_write_lock_##name(struct file *f) \
15090 +{ fi_write_lock_nested(f, AuLsc_FI_##name); }
15091 +
15092 +#define AuRWLockFuncs(name) \
15093 +       AuReadLockFunc(name) \
15094 +       AuWriteLockFunc(name)
15095 +
15096 +AuRWLockFuncs(1);
15097 +AuRWLockFuncs(2);
15098 +
15099 +#undef AuReadLockFunc
15100 +#undef AuWriteLockFunc
15101 +#undef AuRWLockFuncs
15102 +
15103 +#define FiMustNoWaiters(f)     AuRwMustNoWaiters(&au_fi(f)->fi_rwsem)
15104 +#define FiMustAnyLock(f)       AuRwMustAnyLock(&au_fi(f)->fi_rwsem)
15105 +#define FiMustWriteLock(f)     AuRwMustWriteLock(&au_fi(f)->fi_rwsem)
15106 +
15107 +/* ---------------------------------------------------------------------- */
15108 +
15109 +/* todo: hard/soft set? */
15110 +static inline aufs_bindex_t au_fbtop(struct file *file)
15111 +{
15112 +       FiMustAnyLock(file);
15113 +       return au_fi(file)->fi_btop;
15114 +}
15115 +
15116 +static inline aufs_bindex_t au_fbbot_dir(struct file *file)
15117 +{
15118 +       FiMustAnyLock(file);
15119 +       AuDebugOn(!au_fi(file)->fi_hdir);
15120 +       return au_fi(file)->fi_hdir->fd_bbot;
15121 +}
15122 +
15123 +static inline struct au_vdir *au_fvdir_cache(struct file *file)
15124 +{
15125 +       FiMustAnyLock(file);
15126 +       AuDebugOn(!au_fi(file)->fi_hdir);
15127 +       return au_fi(file)->fi_hdir->fd_vdir_cache;
15128 +}
15129 +
15130 +static inline void au_set_fbtop(struct file *file, aufs_bindex_t bindex)
15131 +{
15132 +       FiMustWriteLock(file);
15133 +       au_fi(file)->fi_btop = bindex;
15134 +}
15135 +
15136 +static inline void au_set_fbbot_dir(struct file *file, aufs_bindex_t bindex)
15137 +{
15138 +       FiMustWriteLock(file);
15139 +       AuDebugOn(!au_fi(file)->fi_hdir);
15140 +       au_fi(file)->fi_hdir->fd_bbot = bindex;
15141 +}
15142 +
15143 +static inline void au_set_fvdir_cache(struct file *file,
15144 +                                     struct au_vdir *vdir_cache)
15145 +{
15146 +       FiMustWriteLock(file);
15147 +       AuDebugOn(!au_fi(file)->fi_hdir);
15148 +       au_fi(file)->fi_hdir->fd_vdir_cache = vdir_cache;
15149 +}
15150 +
15151 +static inline struct file *au_hf_top(struct file *file)
15152 +{
15153 +       FiMustAnyLock(file);
15154 +       AuDebugOn(au_fi(file)->fi_hdir);
15155 +       return au_fi(file)->fi_htop.hf_file;
15156 +}
15157 +
15158 +static inline struct file *au_hf_dir(struct file *file, aufs_bindex_t bindex)
15159 +{
15160 +       FiMustAnyLock(file);
15161 +       AuDebugOn(!au_fi(file)->fi_hdir);
15162 +       return au_fi(file)->fi_hdir->fd_hfile[0 + bindex].hf_file;
15163 +}
15164 +
15165 +/* todo: memory barrier? */
15166 +static inline unsigned int au_figen(struct file *f)
15167 +{
15168 +       return atomic_read(&au_fi(f)->fi_generation);
15169 +}
15170 +
15171 +static inline void au_set_mmapped(struct file *f)
15172 +{
15173 +       if (atomic_inc_return(&au_fi(f)->fi_mmapped))
15174 +               return;
15175 +       pr_warn("fi_mmapped wrapped around\n");
15176 +       while (!atomic_inc_return(&au_fi(f)->fi_mmapped))
15177 +               ;
15178 +}
15179 +
15180 +static inline void au_unset_mmapped(struct file *f)
15181 +{
15182 +       atomic_dec(&au_fi(f)->fi_mmapped);
15183 +}
15184 +
15185 +static inline int au_test_mmapped(struct file *f)
15186 +{
15187 +       return atomic_read(&au_fi(f)->fi_mmapped);
15188 +}
15189 +
15190 +/* customize vma->vm_file */
15191 +
15192 +static inline void au_do_vm_file_reset(struct vm_area_struct *vma,
15193 +                                      struct file *file)
15194 +{
15195 +       struct file *f;
15196 +
15197 +       f = vma->vm_file;
15198 +       get_file(file);
15199 +       vma->vm_file = file;
15200 +       fput(f);
15201 +}
15202 +
15203 +#ifdef CONFIG_MMU
15204 +#define AuDbgVmRegion(file, vma) do {} while (0)
15205 +
15206 +static inline void au_vm_file_reset(struct vm_area_struct *vma,
15207 +                                   struct file *file)
15208 +{
15209 +       au_do_vm_file_reset(vma, file);
15210 +}
15211 +#else
15212 +#define AuDbgVmRegion(file, vma) \
15213 +       AuDebugOn((vma)->vm_region && (vma)->vm_region->vm_file != (file))
15214 +
15215 +static inline void au_vm_file_reset(struct vm_area_struct *vma,
15216 +                                   struct file *file)
15217 +{
15218 +       struct file *f;
15219 +
15220 +       au_do_vm_file_reset(vma, file);
15221 +       f = vma->vm_region->vm_file;
15222 +       get_file(file);
15223 +       vma->vm_region->vm_file = file;
15224 +       fput(f);
15225 +}
15226 +#endif /* CONFIG_MMU */
15227 +
15228 +/* handle vma->vm_prfile */
15229 +static inline void au_vm_prfile_set(struct vm_area_struct *vma,
15230 +                                   struct file *file)
15231 +{
15232 +       get_file(file);
15233 +       vma->vm_prfile = file;
15234 +#ifndef CONFIG_MMU
15235 +       get_file(file);
15236 +       vma->vm_region->vm_prfile = file;
15237 +#endif
15238 +}
15239 +
15240 +#endif /* __KERNEL__ */
15241 +#endif /* __AUFS_FILE_H__ */
15242 diff -urN /usr/share/empty/fs/aufs/finfo.c linux/fs/aufs/finfo.c
15243 --- /usr/share/empty/fs/aufs/finfo.c    1970-01-01 01:00:00.000000000 +0100
15244 +++ linux/fs/aufs/finfo.c       2022-03-21 14:49:05.723299677 +0100
15245 @@ -0,0 +1,149 @@
15246 +// SPDX-License-Identifier: GPL-2.0
15247 +/*
15248 + * Copyright (C) 2005-2021 Junjiro R. Okajima
15249 + *
15250 + * This program, aufs is free software; you can redistribute it and/or modify
15251 + * it under the terms of the GNU General Public License as published by
15252 + * the Free Software Foundation; either version 2 of the License, or
15253 + * (at your option) any later version.
15254 + *
15255 + * This program is distributed in the hope that it will be useful,
15256 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
15257 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15258 + * GNU General Public License for more details.
15259 + *
15260 + * You should have received a copy of the GNU General Public License
15261 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15262 + */
15263 +
15264 +/*
15265 + * file private data
15266 + */
15267 +
15268 +#include "aufs.h"
15269 +
15270 +void au_hfput(struct au_hfile *hf, int execed)
15271 +{
15272 +       if (execed)
15273 +               allow_write_access(hf->hf_file);
15274 +       fput(hf->hf_file);
15275 +       hf->hf_file = NULL;
15276 +       au_lcnt_dec(&hf->hf_br->br_nfiles);
15277 +       hf->hf_br = NULL;
15278 +}
15279 +
15280 +void au_set_h_fptr(struct file *file, aufs_bindex_t bindex, struct file *val)
15281 +{
15282 +       struct au_finfo *finfo = au_fi(file);
15283 +       struct au_hfile *hf;
15284 +       struct au_fidir *fidir;
15285 +
15286 +       fidir = finfo->fi_hdir;
15287 +       if (!fidir) {
15288 +               AuDebugOn(finfo->fi_btop != bindex);
15289 +               hf = &finfo->fi_htop;
15290 +       } else
15291 +               hf = fidir->fd_hfile + bindex;
15292 +
15293 +       if (hf && hf->hf_file)
15294 +               au_hfput(hf, vfsub_file_execed(file));
15295 +       if (val) {
15296 +               FiMustWriteLock(file);
15297 +               AuDebugOn(IS_ERR_OR_NULL(file->f_path.dentry));
15298 +               hf->hf_file = val;
15299 +               hf->hf_br = au_sbr(file->f_path.dentry->d_sb, bindex);
15300 +       }
15301 +}
15302 +
15303 +void au_update_figen(struct file *file)
15304 +{
15305 +       atomic_set(&au_fi(file)->fi_generation, au_digen(file->f_path.dentry));
15306 +       /* smp_mb(); */ /* atomic_set */
15307 +}
15308 +
15309 +/* ---------------------------------------------------------------------- */
15310 +
15311 +struct au_fidir *au_fidir_alloc(struct super_block *sb)
15312 +{
15313 +       struct au_fidir *fidir;
15314 +       int nbr;
15315 +
15316 +       nbr = au_sbbot(sb) + 1;
15317 +       if (nbr < 2)
15318 +               nbr = 2; /* initial allocate for 2 branches */
15319 +       fidir = kzalloc(au_fidir_sz(nbr), GFP_NOFS);
15320 +       if (fidir) {
15321 +               fidir->fd_bbot = -1;
15322 +               fidir->fd_nent = nbr;
15323 +       }
15324 +
15325 +       return fidir;
15326 +}
15327 +
15328 +int au_fidir_realloc(struct au_finfo *finfo, int nbr, int may_shrink)
15329 +{
15330 +       int err;
15331 +       struct au_fidir *fidir, *p;
15332 +
15333 +       AuRwMustWriteLock(&finfo->fi_rwsem);
15334 +       fidir = finfo->fi_hdir;
15335 +       AuDebugOn(!fidir);
15336 +
15337 +       err = -ENOMEM;
15338 +       p = au_kzrealloc(fidir, au_fidir_sz(fidir->fd_nent), au_fidir_sz(nbr),
15339 +                        GFP_NOFS, may_shrink);
15340 +       if (p) {
15341 +               p->fd_nent = nbr;
15342 +               finfo->fi_hdir = p;
15343 +               err = 0;
15344 +       }
15345 +
15346 +       return err;
15347 +}
15348 +
15349 +/* ---------------------------------------------------------------------- */
15350 +
15351 +void au_finfo_fin(struct file *file)
15352 +{
15353 +       struct au_finfo *finfo;
15354 +
15355 +       au_lcnt_dec(&au_sbi(file->f_path.dentry->d_sb)->si_nfiles);
15356 +
15357 +       finfo = au_fi(file);
15358 +       AuDebugOn(finfo->fi_hdir);
15359 +       AuRwDestroy(&finfo->fi_rwsem);
15360 +       au_cache_free_finfo(finfo);
15361 +}
15362 +
15363 +void au_fi_init_once(void *_finfo)
15364 +{
15365 +       struct au_finfo *finfo = _finfo;
15366 +
15367 +       au_rw_init(&finfo->fi_rwsem);
15368 +}
15369 +
15370 +int au_finfo_init(struct file *file, struct au_fidir *fidir)
15371 +{
15372 +       int err;
15373 +       struct au_finfo *finfo;
15374 +       struct dentry *dentry;
15375 +
15376 +       err = -ENOMEM;
15377 +       dentry = file->f_path.dentry;
15378 +       finfo = au_cache_alloc_finfo();
15379 +       if (unlikely(!finfo))
15380 +               goto out;
15381 +
15382 +       err = 0;
15383 +       au_lcnt_inc(&au_sbi(dentry->d_sb)->si_nfiles);
15384 +       au_rw_write_lock(&finfo->fi_rwsem);
15385 +       finfo->fi_btop = -1;
15386 +       finfo->fi_hdir = fidir;
15387 +       atomic_set(&finfo->fi_generation, au_digen(dentry));
15388 +       /* smp_mb(); */ /* atomic_set */
15389 +
15390 +       file->private_data = finfo;
15391 +
15392 +out:
15393 +       return err;
15394 +}
15395 diff -urN /usr/share/empty/fs/aufs/f_op.c linux/fs/aufs/f_op.c
15396 --- /usr/share/empty/fs/aufs/f_op.c     1970-01-01 01:00:00.000000000 +0100
15397 +++ linux/fs/aufs/f_op.c        2022-03-21 14:49:05.723299677 +0100
15398 @@ -0,0 +1,771 @@
15399 +// SPDX-License-Identifier: GPL-2.0
15400 +/*
15401 + * Copyright (C) 2005-2021 Junjiro R. Okajima
15402 + *
15403 + * This program, aufs is free software; you can redistribute it and/or modify
15404 + * it under the terms of the GNU General Public License as published by
15405 + * the Free Software Foundation; either version 2 of the License, or
15406 + * (at your option) any later version.
15407 + *
15408 + * This program is distributed in the hope that it will be useful,
15409 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
15410 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15411 + * GNU General Public License for more details.
15412 + *
15413 + * You should have received a copy of the GNU General Public License
15414 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15415 + */
15416 +
15417 +/*
15418 + * file and vm operations
15419 + */
15420 +
15421 +#include <linux/aio.h>
15422 +#include <linux/fs_stack.h>
15423 +#include <linux/mman.h>
15424 +#include <linux/security.h>
15425 +#include "aufs.h"
15426 +
15427 +int au_do_open_nondir(struct file *file, int flags, struct file *h_file)
15428 +{
15429 +       int err;
15430 +       aufs_bindex_t bindex;
15431 +       struct dentry *dentry, *h_dentry;
15432 +       struct au_finfo *finfo;
15433 +       struct inode *h_inode;
15434 +
15435 +       FiMustWriteLock(file);
15436 +
15437 +       err = 0;
15438 +       dentry = file->f_path.dentry;
15439 +       AuDebugOn(IS_ERR_OR_NULL(dentry));
15440 +       finfo = au_fi(file);
15441 +       memset(&finfo->fi_htop, 0, sizeof(finfo->fi_htop));
15442 +       atomic_set(&finfo->fi_mmapped, 0);
15443 +       bindex = au_dbtop(dentry);
15444 +       if (!h_file) {
15445 +               h_dentry = au_h_dptr(dentry, bindex);
15446 +               err = vfsub_test_mntns(file->f_path.mnt, h_dentry->d_sb);
15447 +               if (unlikely(err))
15448 +                       goto out;
15449 +               h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
15450 +               if (IS_ERR(h_file)) {
15451 +                       err = PTR_ERR(h_file);
15452 +                       goto out;
15453 +               }
15454 +       } else {
15455 +               h_dentry = h_file->f_path.dentry;
15456 +               err = vfsub_test_mntns(file->f_path.mnt, h_dentry->d_sb);
15457 +               if (unlikely(err))
15458 +                       goto out;
15459 +               /* br ref is already inc-ed */
15460 +       }
15461 +
15462 +       if ((flags & __O_TMPFILE)
15463 +           && !(flags & O_EXCL)) {
15464 +               h_inode = file_inode(h_file);
15465 +               spin_lock(&h_inode->i_lock);
15466 +               h_inode->i_state |= I_LINKABLE;
15467 +               spin_unlock(&h_inode->i_lock);
15468 +       }
15469 +       au_set_fbtop(file, bindex);
15470 +       au_set_h_fptr(file, bindex, h_file);
15471 +       au_update_figen(file);
15472 +       /* todo: necessary? */
15473 +       /* file->f_ra = h_file->f_ra; */
15474 +
15475 +out:
15476 +       return err;
15477 +}
15478 +
15479 +static int aufs_open_nondir(struct inode *inode __maybe_unused,
15480 +                           struct file *file)
15481 +{
15482 +       int err;
15483 +       struct super_block *sb;
15484 +       struct au_do_open_args args = {
15485 +               .open   = au_do_open_nondir
15486 +       };
15487 +
15488 +       AuDbg("%pD, f_flags 0x%x, f_mode 0x%x\n",
15489 +             file, vfsub_file_flags(file), file->f_mode);
15490 +
15491 +       sb = file->f_path.dentry->d_sb;
15492 +       si_read_lock(sb, AuLock_FLUSH);
15493 +       err = au_do_open(file, &args);
15494 +       si_read_unlock(sb);
15495 +       return err;
15496 +}
15497 +
15498 +int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file)
15499 +{
15500 +       struct au_finfo *finfo;
15501 +       aufs_bindex_t bindex;
15502 +
15503 +       finfo = au_fi(file);
15504 +       au_hbl_del(&finfo->fi_hlist,
15505 +                  &au_sbi(file->f_path.dentry->d_sb)->si_files);
15506 +       bindex = finfo->fi_btop;
15507 +       if (bindex >= 0)
15508 +               au_set_h_fptr(file, bindex, NULL);
15509 +
15510 +       au_finfo_fin(file);
15511 +       return 0;
15512 +}
15513 +
15514 +/* ---------------------------------------------------------------------- */
15515 +
15516 +static int au_do_flush_nondir(struct file *file, fl_owner_t id)
15517 +{
15518 +       int err;
15519 +       struct file *h_file;
15520 +
15521 +       err = 0;
15522 +       h_file = au_hf_top(file);
15523 +       if (h_file)
15524 +               err = vfsub_flush(h_file, id);
15525 +       return err;
15526 +}
15527 +
15528 +static int aufs_flush_nondir(struct file *file, fl_owner_t id)
15529 +{
15530 +       return au_do_flush(file, id, au_do_flush_nondir);
15531 +}
15532 +
15533 +/* ---------------------------------------------------------------------- */
15534 +/*
15535 + * read and write functions acquire [fdi]_rwsem once, but release before
15536 + * mmap_sem. This is because to stop a race condition between mmap(2).
15537 + * Releasing these aufs-rwsem should be safe, no branch-management (by keeping
15538 + * si_rwsem), no harmful copy-up should happen. Actually copy-up may happen in
15539 + * read functions after [fdi]_rwsem are released, but it should be harmless.
15540 + */
15541 +
15542 +/* Callers should call au_read_post() or fput() in the end */
15543 +struct file *au_read_pre(struct file *file, int keep_fi, unsigned int lsc)
15544 +{
15545 +       struct file *h_file;
15546 +       int err;
15547 +
15548 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/0, lsc);
15549 +       if (!err) {
15550 +               di_read_unlock(file->f_path.dentry, AuLock_IR);
15551 +               h_file = au_hf_top(file);
15552 +               get_file(h_file);
15553 +               if (!keep_fi)
15554 +                       fi_read_unlock(file);
15555 +       } else
15556 +               h_file = ERR_PTR(err);
15557 +
15558 +       return h_file;
15559 +}
15560 +
15561 +static void au_read_post(struct inode *inode, struct file *h_file)
15562 +{
15563 +       /* update without lock, I don't think it a problem */
15564 +       fsstack_copy_attr_atime(inode, file_inode(h_file));
15565 +       fput(h_file);
15566 +}
15567 +
15568 +struct au_write_pre {
15569 +       /* input */
15570 +       unsigned int lsc;
15571 +
15572 +       /* output */
15573 +       blkcnt_t blks;
15574 +       aufs_bindex_t btop;
15575 +};
15576 +
15577 +/*
15578 + * return with iinfo is write-locked
15579 + * callers should call au_write_post() or iinfo_write_unlock() + fput() in the
15580 + * end
15581 + */
15582 +static struct file *au_write_pre(struct file *file, int do_ready,
15583 +                                struct au_write_pre *wpre)
15584 +{
15585 +       struct file *h_file;
15586 +       struct dentry *dentry;
15587 +       int err;
15588 +       unsigned int lsc;
15589 +       struct au_pin pin;
15590 +
15591 +       lsc = 0;
15592 +       if (wpre)
15593 +               lsc = wpre->lsc;
15594 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1, lsc);
15595 +       h_file = ERR_PTR(err);
15596 +       if (unlikely(err))
15597 +               goto out;
15598 +
15599 +       dentry = file->f_path.dentry;
15600 +       if (do_ready) {
15601 +               err = au_ready_to_write(file, -1, &pin);
15602 +               if (unlikely(err)) {
15603 +                       h_file = ERR_PTR(err);
15604 +                       di_write_unlock(dentry);
15605 +                       goto out_fi;
15606 +               }
15607 +       }
15608 +
15609 +       di_downgrade_lock(dentry, /*flags*/0);
15610 +       if (wpre)
15611 +               wpre->btop = au_fbtop(file);
15612 +       h_file = au_hf_top(file);
15613 +       get_file(h_file);
15614 +       if (wpre)
15615 +               wpre->blks = file_inode(h_file)->i_blocks;
15616 +       if (do_ready)
15617 +               au_unpin(&pin);
15618 +       di_read_unlock(dentry, /*flags*/0);
15619 +
15620 +out_fi:
15621 +       fi_write_unlock(file);
15622 +out:
15623 +       return h_file;
15624 +}
15625 +
15626 +static void au_write_post(struct inode *inode, struct file *h_file,
15627 +                         struct au_write_pre *wpre, ssize_t written)
15628 +{
15629 +       struct inode *h_inode;
15630 +
15631 +       au_cpup_attr_timesizes(inode);
15632 +       AuDebugOn(au_ibtop(inode) != wpre->btop);
15633 +       h_inode = file_inode(h_file);
15634 +       inode->i_mode = h_inode->i_mode;
15635 +       ii_write_unlock(inode);
15636 +       /* AuDbg("blks %llu, %llu\n", (u64)blks, (u64)h_inode->i_blocks); */
15637 +       if (written > 0)
15638 +               au_fhsm_wrote(inode->i_sb, wpre->btop,
15639 +                             /*force*/h_inode->i_blocks > wpre->blks);
15640 +       fput(h_file);
15641 +}
15642 +
15643 +/*
15644 + * todo: very ugly
15645 + * it locks both of i_mutex and si_rwsem for read in safe.
15646 + * if the plink maintenance mode continues forever (that is the problem),
15647 + * may loop forever.
15648 + */
15649 +static void au_mtx_and_read_lock(struct inode *inode)
15650 +{
15651 +       int err;
15652 +       struct super_block *sb = inode->i_sb;
15653 +
15654 +       while (1) {
15655 +               inode_lock(inode);
15656 +               err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
15657 +               if (!err)
15658 +                       break;
15659 +               inode_unlock(inode);
15660 +               si_read_lock(sb, AuLock_NOPLMW);
15661 +               si_read_unlock(sb);
15662 +       }
15663 +}
15664 +
15665 +static ssize_t au_do_iter(struct file *h_file, int rw, struct kiocb *kio,
15666 +                         struct iov_iter *iov_iter)
15667 +{
15668 +       ssize_t err;
15669 +       struct file *file;
15670 +       ssize_t (*iter)(struct kiocb *, struct iov_iter *);
15671 +
15672 +       err = security_file_permission(h_file, rw);
15673 +       if (unlikely(err))
15674 +               goto out;
15675 +
15676 +       err = -ENOSYS;  /* the branch doesn't have its ->(read|write)_iter() */
15677 +       iter = NULL;
15678 +       if (rw == MAY_READ)
15679 +               iter = h_file->f_op->read_iter;
15680 +       else if (rw == MAY_WRITE)
15681 +               iter = h_file->f_op->write_iter;
15682 +
15683 +       file = kio->ki_filp;
15684 +       kio->ki_filp = h_file;
15685 +       if (iter) {
15686 +               lockdep_off();
15687 +               err = iter(kio, iov_iter);
15688 +               lockdep_on();
15689 +       } else
15690 +               /* currently there is no such fs */
15691 +               WARN_ON_ONCE(1);
15692 +       kio->ki_filp = file;
15693 +
15694 +out:
15695 +       return err;
15696 +}
15697 +
15698 +static ssize_t aufs_read_iter(struct kiocb *kio, struct iov_iter *iov_iter)
15699 +{
15700 +       ssize_t err;
15701 +       struct file *file, *h_file;
15702 +       struct inode *inode;
15703 +       struct super_block *sb;
15704 +
15705 +       file = kio->ki_filp;
15706 +       inode = file_inode(file);
15707 +       sb = inode->i_sb;
15708 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
15709 +
15710 +       h_file = au_read_pre(file, /*keep_fi*/1, /*lsc*/0);
15711 +       err = PTR_ERR(h_file);
15712 +       if (IS_ERR(h_file))
15713 +               goto out;
15714 +
15715 +       if (au_test_loopback_kthread()) {
15716 +               au_warn_loopback(h_file->f_path.dentry->d_sb);
15717 +               if (file->f_mapping != h_file->f_mapping) {
15718 +                       file->f_mapping = h_file->f_mapping;
15719 +                       smp_mb(); /* unnecessary? */
15720 +               }
15721 +       }
15722 +       fi_read_unlock(file);
15723 +
15724 +       err = au_do_iter(h_file, MAY_READ, kio, iov_iter);
15725 +       /* todo: necessary? */
15726 +       /* file->f_ra = h_file->f_ra; */
15727 +       au_read_post(inode, h_file);
15728 +
15729 +out:
15730 +       si_read_unlock(sb);
15731 +       return err;
15732 +}
15733 +
15734 +static ssize_t aufs_write_iter(struct kiocb *kio, struct iov_iter *iov_iter)
15735 +{
15736 +       ssize_t err;
15737 +       struct au_write_pre wpre;
15738 +       struct inode *inode;
15739 +       struct file *file, *h_file;
15740 +
15741 +       file = kio->ki_filp;
15742 +       inode = file_inode(file);
15743 +       au_mtx_and_read_lock(inode);
15744 +
15745 +       wpre.lsc = 0;
15746 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15747 +       err = PTR_ERR(h_file);
15748 +       if (IS_ERR(h_file))
15749 +               goto out;
15750 +
15751 +       err = au_do_iter(h_file, MAY_WRITE, kio, iov_iter);
15752 +       au_write_post(inode, h_file, &wpre, err);
15753 +
15754 +out:
15755 +       si_read_unlock(inode->i_sb);
15756 +       inode_unlock(inode);
15757 +       return err;
15758 +}
15759 +
15760 +/*
15761 + * We may be able to remove aufs_splice_{read,write}() since almost all FSes
15762 + * don't have their own .splice_{read,write} implimentations, and they use
15763 + * generic_file_splice_read() and iter_file_splice_write() who can act like the
15764 + * simple converters to f_op->iter_read() and ->iter_write().
15765 + * But we keep our own implementations because some non-mainlined FSes may have
15766 + * their own .splice_{read,write} implimentations and aufs doesn't want to take
15767 + * away an opportunity to co-work with aufs from them.
15768 + */
15769 +static ssize_t aufs_splice_read(struct file *file, loff_t *ppos,
15770 +                               struct pipe_inode_info *pipe, size_t len,
15771 +                               unsigned int flags)
15772 +{
15773 +       ssize_t err;
15774 +       struct file *h_file;
15775 +       struct inode *inode;
15776 +       struct super_block *sb;
15777 +
15778 +       inode = file_inode(file);
15779 +       sb = inode->i_sb;
15780 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
15781 +
15782 +       h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
15783 +       err = PTR_ERR(h_file);
15784 +       if (IS_ERR(h_file))
15785 +               goto out;
15786 +
15787 +       err = vfsub_splice_to(h_file, ppos, pipe, len, flags);
15788 +       /* todo: necessary? */
15789 +       /* file->f_ra = h_file->f_ra; */
15790 +       au_read_post(inode, h_file);
15791 +
15792 +out:
15793 +       si_read_unlock(sb);
15794 +       return err;
15795 +}
15796 +
15797 +static ssize_t
15798 +aufs_splice_write(struct pipe_inode_info *pipe, struct file *file, loff_t *ppos,
15799 +                 size_t len, unsigned int flags)
15800 +{
15801 +       ssize_t err;
15802 +       struct au_write_pre wpre;
15803 +       struct inode *inode;
15804 +       struct file *h_file;
15805 +
15806 +       inode = file_inode(file);
15807 +       au_mtx_and_read_lock(inode);
15808 +
15809 +       wpre.lsc = 0;
15810 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15811 +       err = PTR_ERR(h_file);
15812 +       if (IS_ERR(h_file))
15813 +               goto out;
15814 +
15815 +       err = vfsub_splice_from(pipe, h_file, ppos, len, flags);
15816 +       au_write_post(inode, h_file, &wpre, err);
15817 +
15818 +out:
15819 +       si_read_unlock(inode->i_sb);
15820 +       inode_unlock(inode);
15821 +       return err;
15822 +}
15823 +
15824 +static long aufs_fallocate(struct file *file, int mode, loff_t offset,
15825 +                          loff_t len)
15826 +{
15827 +       long err;
15828 +       struct au_write_pre wpre;
15829 +       struct inode *inode;
15830 +       struct file *h_file;
15831 +
15832 +       inode = file_inode(file);
15833 +       au_mtx_and_read_lock(inode);
15834 +
15835 +       wpre.lsc = 0;
15836 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15837 +       err = PTR_ERR(h_file);
15838 +       if (IS_ERR(h_file))
15839 +               goto out;
15840 +
15841 +       lockdep_off();
15842 +       err = vfs_fallocate(h_file, mode, offset, len);
15843 +       lockdep_on();
15844 +       au_write_post(inode, h_file, &wpre, /*written*/1);
15845 +
15846 +out:
15847 +       si_read_unlock(inode->i_sb);
15848 +       inode_unlock(inode);
15849 +       return err;
15850 +}
15851 +
15852 +static ssize_t aufs_copy_file_range(struct file *src, loff_t src_pos,
15853 +                                   struct file *dst, loff_t dst_pos,
15854 +                                   size_t len, unsigned int flags)
15855 +{
15856 +       ssize_t err;
15857 +       struct au_write_pre wpre;
15858 +       enum { SRC, DST };
15859 +       struct {
15860 +               struct inode *inode;
15861 +               struct file *h_file;
15862 +               struct super_block *h_sb;
15863 +       } a[2];
15864 +#define a_src  a[SRC]
15865 +#define a_dst  a[DST]
15866 +
15867 +       err = -EINVAL;
15868 +       a_src.inode = file_inode(src);
15869 +       if (unlikely(!S_ISREG(a_src.inode->i_mode)))
15870 +               goto out;
15871 +       a_dst.inode = file_inode(dst);
15872 +       if (unlikely(!S_ISREG(a_dst.inode->i_mode)))
15873 +               goto out;
15874 +
15875 +       au_mtx_and_read_lock(a_dst.inode);
15876 +       /*
15877 +        * in order to match the order in di_write_lock2_{child,parent}(),
15878 +        * use f_path.dentry for this comparison.
15879 +        */
15880 +       if (src->f_path.dentry < dst->f_path.dentry) {
15881 +               a_src.h_file = au_read_pre(src, /*keep_fi*/1, AuLsc_FI_1);
15882 +               err = PTR_ERR(a_src.h_file);
15883 +               if (IS_ERR(a_src.h_file))
15884 +                       goto out_si;
15885 +
15886 +               wpre.lsc = AuLsc_FI_2;
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 +                       au_read_post(a_src.inode, a_src.h_file);
15891 +                       goto out_si;
15892 +               }
15893 +       } else {
15894 +               wpre.lsc = AuLsc_FI_1;
15895 +               a_dst.h_file = au_write_pre(dst, /*do_ready*/1, &wpre);
15896 +               err = PTR_ERR(a_dst.h_file);
15897 +               if (IS_ERR(a_dst.h_file))
15898 +                       goto out_si;
15899 +
15900 +               a_src.h_file = au_read_pre(src, /*keep_fi*/1, AuLsc_FI_2);
15901 +               err = PTR_ERR(a_src.h_file);
15902 +               if (IS_ERR(a_src.h_file)) {
15903 +                       au_write_post(a_dst.inode, a_dst.h_file, &wpre,
15904 +                                     /*written*/0);
15905 +                       goto out_si;
15906 +               }
15907 +       }
15908 +
15909 +       err = -EXDEV;
15910 +       a_src.h_sb = file_inode(a_src.h_file)->i_sb;
15911 +       a_dst.h_sb = file_inode(a_dst.h_file)->i_sb;
15912 +       if (unlikely(a_src.h_sb != a_dst.h_sb)) {
15913 +               AuDbgFile(src);
15914 +               AuDbgFile(dst);
15915 +               goto out_file;
15916 +       }
15917 +
15918 +       err = vfsub_copy_file_range(a_src.h_file, src_pos, a_dst.h_file,
15919 +                                   dst_pos, len, flags);
15920 +
15921 +out_file:
15922 +       au_write_post(a_dst.inode, a_dst.h_file, &wpre, err);
15923 +       fi_read_unlock(src);
15924 +       au_read_post(a_src.inode, a_src.h_file);
15925 +out_si:
15926 +       si_read_unlock(a_dst.inode->i_sb);
15927 +       inode_unlock(a_dst.inode);
15928 +out:
15929 +       return err;
15930 +#undef a_src
15931 +#undef a_dst
15932 +}
15933 +
15934 +/* ---------------------------------------------------------------------- */
15935 +
15936 +/*
15937 + * The locking order around current->mmap_sem.
15938 + * - in most and regular cases
15939 + *   file I/O syscall -- aufs_read() or something
15940 + *     -- si_rwsem for read -- mmap_sem
15941 + *     (Note that [fdi]i_rwsem are released before mmap_sem).
15942 + * - in mmap case
15943 + *   mmap(2) -- mmap_sem -- aufs_mmap() -- si_rwsem for read -- [fdi]i_rwsem
15944 + * This AB-BA order is definitely bad, but is not a problem since "si_rwsem for
15945 + * read" allows multiple processes to acquire it and [fdi]i_rwsem are not held
15946 + * in file I/O. Aufs needs to stop lockdep in aufs_mmap() though.
15947 + * It means that when aufs acquires si_rwsem for write, the process should never
15948 + * acquire mmap_sem.
15949 + *
15950 + * Actually aufs_iterate() holds [fdi]i_rwsem before mmap_sem, but this is not a
15951 + * problem either since any directory is not able to be mmap-ed.
15952 + * The similar scenario is applied to aufs_readlink() too.
15953 + */
15954 +
15955 +#if 0 /* stop calling security_file_mmap() */
15956 +/* cf. linux/include/linux/mman.h: calc_vm_prot_bits() */
15957 +#define AuConv_VM_PROT(f, b)   _calc_vm_trans(f, VM_##b, PROT_##b)
15958 +
15959 +static unsigned long au_arch_prot_conv(unsigned long flags)
15960 +{
15961 +       /* currently ppc64 only */
15962 +#ifdef CONFIG_PPC64
15963 +       /* cf. linux/arch/powerpc/include/asm/mman.h */
15964 +       AuDebugOn(arch_calc_vm_prot_bits(-1) != VM_SAO);
15965 +       return AuConv_VM_PROT(flags, SAO);
15966 +#else
15967 +       AuDebugOn(arch_calc_vm_prot_bits(-1));
15968 +       return 0;
15969 +#endif
15970 +}
15971 +
15972 +static unsigned long au_prot_conv(unsigned long flags)
15973 +{
15974 +       return AuConv_VM_PROT(flags, READ)
15975 +               | AuConv_VM_PROT(flags, WRITE)
15976 +               | AuConv_VM_PROT(flags, EXEC)
15977 +               | au_arch_prot_conv(flags);
15978 +}
15979 +
15980 +/* cf. linux/include/linux/mman.h: calc_vm_flag_bits() */
15981 +#define AuConv_VM_MAP(f, b)    _calc_vm_trans(f, VM_##b, MAP_##b)
15982 +
15983 +static unsigned long au_flag_conv(unsigned long flags)
15984 +{
15985 +       return AuConv_VM_MAP(flags, GROWSDOWN)
15986 +               | AuConv_VM_MAP(flags, DENYWRITE)
15987 +               | AuConv_VM_MAP(flags, LOCKED);
15988 +}
15989 +#endif
15990 +
15991 +static int aufs_mmap(struct file *file, struct vm_area_struct *vma)
15992 +{
15993 +       int err;
15994 +       const unsigned char wlock
15995 +               = (file->f_mode & FMODE_WRITE) && (vma->vm_flags & VM_SHARED);
15996 +       struct super_block *sb;
15997 +       struct file *h_file;
15998 +       struct inode *inode;
15999 +
16000 +       AuDbgVmRegion(file, vma);
16001 +
16002 +       inode = file_inode(file);
16003 +       sb = inode->i_sb;
16004 +       lockdep_off();
16005 +       si_read_lock(sb, AuLock_NOPLMW);
16006 +
16007 +       h_file = au_write_pre(file, wlock, /*wpre*/NULL);
16008 +       lockdep_on();
16009 +       err = PTR_ERR(h_file);
16010 +       if (IS_ERR(h_file))
16011 +               goto out;
16012 +
16013 +       err = 0;
16014 +       au_set_mmapped(file);
16015 +       au_vm_file_reset(vma, h_file);
16016 +       /*
16017 +        * we cannot call security_mmap_file() here since it may acquire
16018 +        * mmap_sem or i_mutex.
16019 +        *
16020 +        * err = security_mmap_file(h_file, au_prot_conv(vma->vm_flags),
16021 +        *                       au_flag_conv(vma->vm_flags));
16022 +        */
16023 +       if (!err)
16024 +               err = call_mmap(h_file, vma);
16025 +       if (!err) {
16026 +               au_vm_prfile_set(vma, file);
16027 +               fsstack_copy_attr_atime(inode, file_inode(h_file));
16028 +               goto out_fput; /* success */
16029 +       }
16030 +       au_unset_mmapped(file);
16031 +       au_vm_file_reset(vma, file);
16032 +
16033 +out_fput:
16034 +       lockdep_off();
16035 +       ii_write_unlock(inode);
16036 +       lockdep_on();
16037 +       fput(h_file);
16038 +out:
16039 +       lockdep_off();
16040 +       si_read_unlock(sb);
16041 +       lockdep_on();
16042 +       AuTraceErr(err);
16043 +       return err;
16044 +}
16045 +
16046 +/* ---------------------------------------------------------------------- */
16047 +
16048 +static int aufs_fsync_nondir(struct file *file, loff_t start, loff_t end,
16049 +                            int datasync)
16050 +{
16051 +       int err;
16052 +       struct au_write_pre wpre;
16053 +       struct inode *inode;
16054 +       struct file *h_file;
16055 +
16056 +       err = 0; /* -EBADF; */ /* posix? */
16057 +       if (unlikely(!(file->f_mode & FMODE_WRITE)))
16058 +               goto out;
16059 +
16060 +       inode = file_inode(file);
16061 +       au_mtx_and_read_lock(inode);
16062 +
16063 +       wpre.lsc = 0;
16064 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
16065 +       err = PTR_ERR(h_file);
16066 +       if (IS_ERR(h_file))
16067 +               goto out_unlock;
16068 +
16069 +       err = vfsub_fsync(h_file, &h_file->f_path, datasync);
16070 +       au_write_post(inode, h_file, &wpre, /*written*/0);
16071 +
16072 +out_unlock:
16073 +       si_read_unlock(inode->i_sb);
16074 +       inode_unlock(inode);
16075 +out:
16076 +       return err;
16077 +}
16078 +
16079 +static int aufs_fasync(int fd, struct file *file, int flag)
16080 +{
16081 +       int err;
16082 +       struct file *h_file;
16083 +       struct super_block *sb;
16084 +
16085 +       sb = file->f_path.dentry->d_sb;
16086 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
16087 +
16088 +       h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
16089 +       err = PTR_ERR(h_file);
16090 +       if (IS_ERR(h_file))
16091 +               goto out;
16092 +
16093 +       if (h_file->f_op->fasync)
16094 +               err = h_file->f_op->fasync(fd, h_file, flag);
16095 +       fput(h_file); /* instead of au_read_post() */
16096 +
16097 +out:
16098 +       si_read_unlock(sb);
16099 +       return err;
16100 +}
16101 +
16102 +static int aufs_setfl(struct file *file, unsigned long arg)
16103 +{
16104 +       int err;
16105 +       struct file *h_file;
16106 +       struct super_block *sb;
16107 +
16108 +       sb = file->f_path.dentry->d_sb;
16109 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
16110 +
16111 +       h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
16112 +       err = PTR_ERR(h_file);
16113 +       if (IS_ERR(h_file))
16114 +               goto out;
16115 +
16116 +       /* stop calling h_file->fasync */
16117 +       arg |= vfsub_file_flags(file) & FASYNC;
16118 +       err = setfl(/*unused fd*/-1, h_file, arg);
16119 +       fput(h_file); /* instead of au_read_post() */
16120 +
16121 +out:
16122 +       si_read_unlock(sb);
16123 +       return err;
16124 +}
16125 +
16126 +/* ---------------------------------------------------------------------- */
16127 +
16128 +/* no one supports this operation, currently */
16129 +#if 0 /* reserved for future use */
16130 +static ssize_t aufs_sendpage(struct file *file, struct page *page, int offset,
16131 +                            size_t len, loff_t *pos, int more)
16132 +{
16133 +}
16134 +#endif
16135 +
16136 +/* ---------------------------------------------------------------------- */
16137 +
16138 +const struct file_operations aufs_file_fop = {
16139 +       .owner          = THIS_MODULE,
16140 +
16141 +       .llseek         = default_llseek,
16142 +
16143 +       .read_iter      = aufs_read_iter,
16144 +       .write_iter     = aufs_write_iter,
16145 +
16146 +#ifdef CONFIG_AUFS_POLL
16147 +       .poll           = aufs_poll,
16148 +#endif
16149 +       .unlocked_ioctl = aufs_ioctl_nondir,
16150 +#ifdef CONFIG_COMPAT
16151 +       .compat_ioctl   = aufs_compat_ioctl_nondir,
16152 +#endif
16153 +       .mmap           = aufs_mmap,
16154 +       .open           = aufs_open_nondir,
16155 +       .flush          = aufs_flush_nondir,
16156 +       .release        = aufs_release_nondir,
16157 +       .fsync          = aufs_fsync_nondir,
16158 +       .fasync         = aufs_fasync,
16159 +       /* .sendpage    = aufs_sendpage, */
16160 +       .setfl          = aufs_setfl,
16161 +       .splice_write   = aufs_splice_write,
16162 +       .splice_read    = aufs_splice_read,
16163 +#if 0 /* reserved for future use */
16164 +       .aio_splice_write = aufs_aio_splice_write,
16165 +       .aio_splice_read  = aufs_aio_splice_read,
16166 +#endif
16167 +       .fallocate      = aufs_fallocate,
16168 +       .copy_file_range = aufs_copy_file_range
16169 +};
16170 diff -urN /usr/share/empty/fs/aufs/fsctx.c linux/fs/aufs/fsctx.c
16171 --- /usr/share/empty/fs/aufs/fsctx.c    1970-01-01 01:00:00.000000000 +0100
16172 +++ linux/fs/aufs/fsctx.c       2022-03-21 14:49:05.723299677 +0100
16173 @@ -0,0 +1,1242 @@
16174 +// SPDX-License-Identifier: GPL-2.0
16175 +/*
16176 + * Copyright (C) 2022 Junjiro R. Okajima
16177 + *
16178 + * This program, aufs is free software; you can redistribute it and/or modify
16179 + * it under the terms of the GNU General Public License as published by
16180 + * the Free Software Foundation; either version 2 of the License, or
16181 + * (at your option) any later version.
16182 + *
16183 + * This program is distributed in the hope that it will be useful,
16184 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
16185 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16186 + * GNU General Public License for more details.
16187 + *
16188 + * You should have received a copy of the GNU General Public License
16189 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16190 + */
16191 +
16192 +/*
16193 + * fs context, aka new mount api
16194 + */
16195 +
16196 +#include <linux/fs_context.h>
16197 +#include "aufs.h"
16198 +
16199 +struct au_fsctx_opts {
16200 +       aufs_bindex_t bindex;
16201 +       unsigned char skipped;
16202 +       struct au_opt *opt, *opt_tail;
16203 +       struct super_block *sb;
16204 +       struct au_sbinfo *sbinfo;
16205 +       struct au_opts opts;
16206 +};
16207 +
16208 +/* stop extra interpretation of errno in mount(8), and strange error messages */
16209 +static int cvt_err(int err)
16210 +{
16211 +       AuTraceErr(err);
16212 +
16213 +       switch (err) {
16214 +       case -ENOENT:
16215 +       case -ENOTDIR:
16216 +       case -EEXIST:
16217 +       case -EIO:
16218 +               err = -EINVAL;
16219 +       }
16220 +       return err;
16221 +}
16222 +
16223 +static int au_fsctx_reconfigure(struct fs_context *fc)
16224 +{
16225 +       int err, do_dx;
16226 +       unsigned int mntflags;
16227 +       struct dentry *root;
16228 +       struct super_block *sb;
16229 +       struct inode *inode;
16230 +       struct au_fsctx_opts *a = fc->fs_private;
16231 +
16232 +       AuDbg("fc %p\n", fc);
16233 +
16234 +       root = fc->root;
16235 +       sb = root->d_sb;
16236 +       err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
16237 +       if (!err) {
16238 +               di_write_lock_child(root);
16239 +               err = au_opts_verify(sb, fc->sb_flags, /*pending*/0);
16240 +               aufs_write_unlock(root);
16241 +       }
16242 +
16243 +       inode = d_inode(root);
16244 +       inode_lock(inode);
16245 +       err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
16246 +       if (unlikely(err))
16247 +               goto out;
16248 +       di_write_lock_child(root);
16249 +
16250 +       /* au_opts_remount() may return an error */
16251 +       err = au_opts_remount(sb, &a->opts);
16252 +
16253 +       if (au_ftest_opts(a->opts.flags, REFRESH))
16254 +               au_remount_refresh(sb, au_ftest_opts(a->opts.flags,
16255 +                                                    REFRESH_IDOP));
16256 +
16257 +       if (au_ftest_opts(a->opts.flags, REFRESH_DYAOP)) {
16258 +               mntflags = au_mntflags(sb);
16259 +               do_dx = !!au_opt_test(mntflags, DIO);
16260 +               au_dy_arefresh(do_dx);
16261 +       }
16262 +
16263 +       au_fhsm_wrote_all(sb, /*force*/1); /* ?? */
16264 +       aufs_write_unlock(root);
16265 +
16266 +out:
16267 +       inode_unlock(inode);
16268 +       err = cvt_err(err);
16269 +       AuTraceErr(err);
16270 +
16271 +       return err;
16272 +}
16273 +
16274 +/* ---------------------------------------------------------------------- */
16275 +
16276 +static int au_fsctx_fill_super(struct super_block *sb, struct fs_context *fc)
16277 +{
16278 +       int err;
16279 +       struct au_fsctx_opts *a = fc->fs_private;
16280 +       struct au_sbinfo *sbinfo = a->sbinfo;
16281 +       struct dentry *root;
16282 +       struct inode *inode;
16283 +
16284 +       sbinfo->si_sb = sb;
16285 +       sb->s_fs_info = sbinfo;
16286 +       kobject_get(&sbinfo->si_kobj);
16287 +
16288 +       __si_write_lock(sb);
16289 +       si_pid_set(sb);
16290 +       au_sbilist_add(sb);
16291 +
16292 +       /* all timestamps always follow the ones on the branch */
16293 +       sb->s_flags |= SB_NOATIME | SB_NODIRATIME;
16294 +       sb->s_flags |= SB_I_VERSION; /* do we really need this? */
16295 +       sb->s_op = &aufs_sop;
16296 +       sb->s_d_op = &aufs_dop;
16297 +       sb->s_magic = AUFS_SUPER_MAGIC;
16298 +       sb->s_maxbytes = 0;
16299 +       sb->s_stack_depth = 1;
16300 +       au_export_init(sb);
16301 +       au_xattr_init(sb);
16302 +
16303 +       err = au_alloc_root(sb);
16304 +       if (unlikely(err)) {
16305 +               si_write_unlock(sb);
16306 +               goto out;
16307 +       }
16308 +       root = sb->s_root;
16309 +       inode = d_inode(root);
16310 +       ii_write_lock_parent(inode);
16311 +       aufs_write_unlock(root);
16312 +
16313 +       /* lock vfs_inode first, then aufs. */
16314 +       inode_lock(inode);
16315 +       aufs_write_lock(root);
16316 +       err = au_opts_mount(sb, &a->opts);
16317 +       AuTraceErr(err);
16318 +       if (!err && au_ftest_si(sbinfo, NO_DREVAL)) {
16319 +               sb->s_d_op = &aufs_dop_noreval;
16320 +               /* infofc(fc, "%ps", sb->s_d_op); */
16321 +               pr_info("%ps\n", sb->s_d_op);
16322 +               au_refresh_dop(root, /*force_reval*/0);
16323 +               sbinfo->si_iop_array = aufs_iop_nogetattr;
16324 +               au_refresh_iop(inode, /*force_getattr*/0);
16325 +       }
16326 +       aufs_write_unlock(root);
16327 +       inode_unlock(inode);
16328 +       if (!err)
16329 +               goto out; /* success */
16330 +
16331 +       dput(root);
16332 +       sb->s_root = NULL;
16333 +
16334 +out:
16335 +       if (unlikely(err))
16336 +               kobject_put(&sbinfo->si_kobj);
16337 +       AuTraceErr(err);
16338 +       err = cvt_err(err);
16339 +       AuTraceErr(err);
16340 +       return err;
16341 +}
16342 +
16343 +static int au_fsctx_get_tree(struct fs_context *fc)
16344 +{
16345 +       int err;
16346 +
16347 +       AuDbg("fc %p\n", fc);
16348 +       err = get_tree_nodev(fc, au_fsctx_fill_super);
16349 +
16350 +       AuTraceErr(err);
16351 +       return err;
16352 +}
16353 +
16354 +/* ---------------------------------------------------------------------- */
16355 +
16356 +static void au_fsctx_dump(struct au_opts *opts)
16357 +{
16358 +#ifdef CONFIG_AUFS_DEBUG
16359 +       /* reduce stack space */
16360 +       union {
16361 +               struct au_opt_add *add;
16362 +               struct au_opt_del *del;
16363 +               struct au_opt_mod *mod;
16364 +               struct au_opt_xino *xino;
16365 +               struct au_opt_xino_itrunc *xino_itrunc;
16366 +               struct au_opt_wbr_create *create;
16367 +       } u;
16368 +       struct au_opt *opt;
16369 +
16370 +       opt = opts->opt;
16371 +       while (opt->type != Opt_tail) {
16372 +               switch (opt->type) {
16373 +               case Opt_add:
16374 +                       u.add = &opt->add;
16375 +                       AuDbg("add {b%d, %s, 0x%x, %p}\n",
16376 +                                 u.add->bindex, u.add->pathname, u.add->perm,
16377 +                                 u.add->path.dentry);
16378 +                       break;
16379 +               case Opt_del:
16380 +                       fallthrough;
16381 +               case Opt_idel:
16382 +                       u.del = &opt->del;
16383 +                       AuDbg("del {%s, %p}\n",
16384 +                             u.del->pathname, u.del->h_path.dentry);
16385 +                       break;
16386 +               case Opt_mod:
16387 +                       fallthrough;
16388 +               case Opt_imod:
16389 +                       u.mod = &opt->mod;
16390 +                       AuDbg("mod {%s, 0x%x, %p}\n",
16391 +                                 u.mod->path, u.mod->perm, u.mod->h_root);
16392 +                       break;
16393 +               case Opt_append:
16394 +                       u.add = &opt->add;
16395 +                       AuDbg("append {b%d, %s, 0x%x, %p}\n",
16396 +                                 u.add->bindex, u.add->pathname, u.add->perm,
16397 +                                 u.add->path.dentry);
16398 +                       break;
16399 +               case Opt_prepend:
16400 +                       u.add = &opt->add;
16401 +                       AuDbg("prepend {b%d, %s, 0x%x, %p}\n",
16402 +                                 u.add->bindex, u.add->pathname, u.add->perm,
16403 +                                 u.add->path.dentry);
16404 +                       break;
16405 +
16406 +               case Opt_dirwh:
16407 +                       AuDbg("dirwh %d\n", opt->dirwh);
16408 +                       break;
16409 +               case Opt_rdcache:
16410 +                       AuDbg("rdcache %d\n", opt->rdcache);
16411 +                       break;
16412 +               case Opt_rdblk:
16413 +                       AuDbg("rdblk %d\n", opt->rdblk);
16414 +                       break;
16415 +               case Opt_rdhash:
16416 +                       AuDbg("rdhash %u\n", opt->rdhash);
16417 +                       break;
16418 +
16419 +               case Opt_xino:
16420 +                       u.xino = &opt->xino;
16421 +                       AuDbg("xino {%s %pD}\n", u.xino->path, u.xino->file);
16422 +                       break;
16423 +
16424 +#define au_fsctx_TF(name)                                        \
16425 +                       case Opt_##name:                          \
16426 +                               if (opt->tf)                      \
16427 +                                       AuLabel(name);            \
16428 +                               else                              \
16429 +                                       AuLabel(no##name);        \
16430 +                               break;
16431 +
16432 +               /* simple true/false flag */
16433 +               au_fsctx_TF(trunc_xino);
16434 +               au_fsctx_TF(trunc_xib);
16435 +               au_fsctx_TF(dirperm1);
16436 +               au_fsctx_TF(plink);
16437 +               au_fsctx_TF(shwh);
16438 +               au_fsctx_TF(dio);
16439 +               au_fsctx_TF(warn_perm);
16440 +               au_fsctx_TF(verbose);
16441 +               au_fsctx_TF(sum);
16442 +               au_fsctx_TF(dirren);
16443 +               au_fsctx_TF(acl);
16444 +#undef au_fsctx_TF
16445 +
16446 +               case Opt_trunc_xino_path:
16447 +                       fallthrough;
16448 +               case Opt_itrunc_xino:
16449 +                       u.xino_itrunc = &opt->xino_itrunc;
16450 +                       AuDbg("trunc_xino %d\n", u.xino_itrunc->bindex);
16451 +                       break;
16452 +               case Opt_noxino:
16453 +                       AuLabel(noxino);
16454 +                       break;
16455 +
16456 +               case Opt_list_plink:
16457 +                       AuLabel(list_plink);
16458 +                       break;
16459 +               case Opt_udba:
16460 +                       AuDbg("udba %d, %s\n",
16461 +                                 opt->udba, au_optstr_udba(opt->udba));
16462 +                       break;
16463 +               case Opt_diropq_a:
16464 +                       AuLabel(diropq_a);
16465 +                       break;
16466 +               case Opt_diropq_w:
16467 +                       AuLabel(diropq_w);
16468 +                       break;
16469 +               case Opt_wsum:
16470 +                       AuLabel(wsum);
16471 +                       break;
16472 +               case Opt_wbr_create:
16473 +                       u.create = &opt->wbr_create;
16474 +                       AuDbg("create %d, %s\n", u.create->wbr_create,
16475 +                                 au_optstr_wbr_create(u.create->wbr_create));
16476 +                       switch (u.create->wbr_create) {
16477 +                       case AuWbrCreate_MFSV:
16478 +                               fallthrough;
16479 +                       case AuWbrCreate_PMFSV:
16480 +                               AuDbg("%d sec\n", u.create->mfs_second);
16481 +                               break;
16482 +                       case AuWbrCreate_MFSRR:
16483 +                               fallthrough;
16484 +                       case AuWbrCreate_TDMFS:
16485 +                               AuDbg("%llu watermark\n",
16486 +                                         u.create->mfsrr_watermark);
16487 +                               break;
16488 +                       case AuWbrCreate_MFSRRV:
16489 +                               fallthrough;
16490 +                       case AuWbrCreate_TDMFSV:
16491 +                               fallthrough;
16492 +                       case AuWbrCreate_PMFSRRV:
16493 +                               AuDbg("%llu watermark, %d sec\n",
16494 +                                         u.create->mfsrr_watermark,
16495 +                                         u.create->mfs_second);
16496 +                               break;
16497 +                       }
16498 +                       break;
16499 +               case Opt_wbr_copyup:
16500 +                       AuDbg("copyup %d, %s\n", opt->wbr_copyup,
16501 +                                 au_optstr_wbr_copyup(opt->wbr_copyup));
16502 +                       break;
16503 +               case Opt_fhsm_sec:
16504 +                       AuDbg("fhsm_sec %u\n", opt->fhsm_second);
16505 +                       break;
16506 +
16507 +               default:
16508 +                       AuDbg("type %d\n", opt->type);
16509 +                       BUG();
16510 +               }
16511 +               opt++;
16512 +       }
16513 +#endif
16514 +}
16515 +
16516 +/* ---------------------------------------------------------------------- */
16517 +
16518 +/*
16519 + * For conditionally compiled mount options.
16520 + * Instead of fsparam_flag_no(), use this macro to distinguish ignore_silent.
16521 + */
16522 +#define au_ignore_flag(name, action)           \
16523 +       fsparam_flag(name, action),             \
16524 +       fsparam_flag("no" name, Opt_ignore_silent)
16525 +
16526 +const struct fs_parameter_spec aufs_fsctx_paramspec[] = {
16527 +       fsparam_string("br", Opt_br),
16528 +
16529 +       /* "add=%d:%s" or "ins=%d:%s" */
16530 +       fsparam_string("add", Opt_add),
16531 +       fsparam_string("ins", Opt_add),
16532 +       fsparam_path("append", Opt_append),
16533 +       fsparam_path("prepend", Opt_prepend),
16534 +
16535 +       fsparam_path("del", Opt_del),
16536 +       /* fsparam_s32("idel", Opt_idel), */
16537 +       fsparam_path("mod", Opt_mod),
16538 +       /* fsparam_string("imod", Opt_imod), */
16539 +
16540 +       fsparam_s32("dirwh", Opt_dirwh),
16541 +
16542 +       fsparam_path("xino", Opt_xino),
16543 +       fsparam_flag("noxino", Opt_noxino),
16544 +       fsparam_flag_no("trunc_xino", Opt_trunc_xino),
16545 +       /* "trunc_xino_v=%d:%d" */
16546 +       /* fsparam_string("trunc_xino_v", Opt_trunc_xino_v), */
16547 +       fsparam_path("trunc_xino", Opt_trunc_xino_path),
16548 +       fsparam_s32("itrunc_xino", Opt_itrunc_xino),
16549 +       /* fsparam_path("zxino", Opt_zxino), */
16550 +       fsparam_flag_no("trunc_xib", Opt_trunc_xib),
16551 +
16552 +#ifdef CONFIG_PROC_FS
16553 +       fsparam_flag_no("plink", Opt_plink),
16554 +#else
16555 +       au_ignore_flag("plink", Opt_ignore),
16556 +#endif
16557 +
16558 +#ifdef CONFIG_AUFS_DEBUG
16559 +       fsparam_flag("list_plink", Opt_list_plink),
16560 +#endif
16561 +
16562 +       fsparam_string("udba", Opt_udba),
16563 +
16564 +       fsparam_flag_no("dio", Opt_dio),
16565 +
16566 +#ifdef CONFIG_AUFS_DIRREN
16567 +       fsparam_flag_no("dirren", Opt_dirren),
16568 +#else
16569 +       au_ignore_flag("dirren", Opt_ignore),
16570 +#endif
16571 +
16572 +#ifdef CONFIG_AUFS_FHSM
16573 +       fsparam_s32("fhsm_sec", Opt_fhsm_sec),
16574 +#else
16575 +       fsparam_s32("fhsm_sec", Opt_ignore),
16576 +#endif
16577 +
16578 +       /* always | a | whiteouted | w */
16579 +       fsparam_string("diropq", Opt_diropq),
16580 +
16581 +       fsparam_flag_no("warn_perm", Opt_warn_perm),
16582 +
16583 +#ifdef CONFIG_AUFS_SHWH
16584 +       fsparam_flag_no("shwh", Opt_shwh),
16585 +#else
16586 +       au_ignore_flag("shwh", Opt_err),
16587 +#endif
16588 +
16589 +       fsparam_flag_no("dirperm1", Opt_dirperm1),
16590 +
16591 +       fsparam_flag_no("verbose", Opt_verbose),
16592 +       fsparam_flag("v", Opt_verbose),
16593 +       fsparam_flag("quiet", Opt_noverbose),
16594 +       fsparam_flag("q", Opt_noverbose),
16595 +       /* user-space may handle this */
16596 +       fsparam_flag("silent", Opt_noverbose),
16597 +
16598 +       fsparam_flag_no("sum", Opt_sum),
16599 +       fsparam_flag("wsum", Opt_wsum),
16600 +
16601 +       fsparam_s32("rdcache", Opt_rdcache),
16602 +       /* "def" or s32 */
16603 +       fsparam_string("rdblk", Opt_rdblk),
16604 +       /* "def" or s32 */
16605 +       fsparam_string("rdhash", Opt_rdhash),
16606 +
16607 +       fsparam_string("create", Opt_wbr_create),
16608 +       fsparam_string("create_policy", Opt_wbr_create),
16609 +       fsparam_string("cpup", Opt_wbr_copyup),
16610 +       fsparam_string("copyup", Opt_wbr_copyup),
16611 +       fsparam_string("copyup_policy", Opt_wbr_copyup),
16612 +
16613 +       /* generic VFS flag */
16614 +#ifdef CONFIG_FS_POSIX_ACL
16615 +       fsparam_flag_no("acl", Opt_acl),
16616 +#else
16617 +       au_ignore_flag("acl"),
16618 +#endif
16619 +
16620 +       /* internal use for the scripts */
16621 +       fsparam_string("si", Opt_ignore_silent),
16622 +
16623 +       /* obsoleted, keep them temporary */
16624 +       fsparam_flag("nodlgt", Opt_ignore_silent),
16625 +       fsparam_flag("clean_plink", Opt_ignore),
16626 +       fsparam_string("dirs", Opt_br),
16627 +       fsparam_u32("debug", Opt_ignore),
16628 +       /* "whiteout" or "all" */
16629 +       fsparam_string("delete", Opt_ignore),
16630 +       fsparam_string("imap", Opt_ignore),
16631 +
16632 +       /* temporary workaround, due to old mount(8)? */
16633 +       fsparam_flag("relatime", Opt_ignore_silent),
16634 +
16635 +       {}
16636 +};
16637 +
16638 +static int au_fsctx_parse_do_add(struct fs_context *fc, struct au_opt *opt,
16639 +                                char *brspec, size_t speclen,
16640 +                                aufs_bindex_t bindex)
16641 +{
16642 +       int err;
16643 +       char *p;
16644 +
16645 +       AuDbg("brspec %s\n", brspec);
16646 +
16647 +       err = -ENOMEM;
16648 +       if (!speclen)
16649 +               speclen = strlen(brspec);
16650 +       /* will be freed by au_fsctx_free() */
16651 +       p = kmemdup_nul(brspec, speclen, GFP_NOFS);
16652 +       if (unlikely(!p)) {
16653 +               errorfc(fc, "failed in %s", brspec);
16654 +               goto out;
16655 +       }
16656 +       err = au_opt_add(opt, p, fc->sb_flags, bindex);
16657 +
16658 +out:
16659 +       AuTraceErr(err);
16660 +       return err;
16661 +}
16662 +
16663 +static int au_fsctx_parse_br(struct fs_context *fc, char *brspec)
16664 +{
16665 +       int err;
16666 +       char *p;
16667 +       struct au_fsctx_opts *a = fc->fs_private;
16668 +       struct au_opt *opt = a->opt;
16669 +       aufs_bindex_t bindex = a->bindex;
16670 +
16671 +       AuDbg("brspec %s\n", brspec);
16672 +
16673 +       err = -EINVAL;
16674 +       while ((p = strsep(&brspec, ":")) && *p) {
16675 +               err = au_fsctx_parse_do_add(fc, opt, p, /*len*/0, bindex);
16676 +               AuTraceErr(err);
16677 +               if (unlikely(err))
16678 +                       break;
16679 +               bindex++;
16680 +               opt++;
16681 +               if (unlikely(opt > a->opt_tail)) {
16682 +                       err = -E2BIG;
16683 +                       bindex--;
16684 +                       opt--;
16685 +                       break;
16686 +               }
16687 +               opt->type = Opt_tail;
16688 +               a->skipped = 1;
16689 +       }
16690 +       a->bindex = bindex;
16691 +       a->opt = opt;
16692 +
16693 +       AuTraceErr(err);
16694 +       return err;
16695 +}
16696 +
16697 +static int au_fsctx_parse_add(struct fs_context *fc, char *addspec)
16698 +{
16699 +       int err, n;
16700 +       char *p;
16701 +       struct au_fsctx_opts *a = fc->fs_private;
16702 +       struct au_opt *opt = a->opt;
16703 +
16704 +       err = -EINVAL;
16705 +       p = strchr(addspec, ':');
16706 +       if (unlikely(!p)) {
16707 +               errorfc(fc, "bad arg in %s", addspec);
16708 +               goto out;
16709 +       }
16710 +       *p++ = '\0';
16711 +       err = kstrtoint(addspec, 0, &n);
16712 +       if (unlikely(err)) {
16713 +               errorfc(fc, "bad integer in %s", addspec);
16714 +               goto out;
16715 +       }
16716 +       AuDbg("n %d\n", n);
16717 +       err = au_fsctx_parse_do_add(fc, opt, p, /*len*/0, n);
16718 +
16719 +out:
16720 +       AuTraceErr(err);
16721 +       return err;
16722 +}
16723 +
16724 +static int au_fsctx_parse_del(struct fs_context *fc, struct au_opt_del *del,
16725 +                             struct fs_parameter *param)
16726 +{
16727 +       int err;
16728 +
16729 +       err = -ENOMEM;
16730 +       /* will be freed by au_fsctx_free() */
16731 +       del->pathname = kmemdup_nul(param->string, param->size, GFP_NOFS);
16732 +       if (unlikely(!del->pathname))
16733 +               goto out;
16734 +       AuDbg("del %s\n", del->pathname);
16735 +       err = vfsub_kern_path(del->pathname, AuOpt_LkupDirFlags, &del->h_path);
16736 +       if (unlikely(err))
16737 +               errorfc(fc, "lookup failed %s (%d)", del->pathname, err);
16738 +
16739 +out:
16740 +       AuTraceErr(err);
16741 +       return err;
16742 +}
16743 +
16744 +#if 0 /* reserved for future use */
16745 +static int au_fsctx_parse_idel(struct fs_context *fc, struct au_opt_del *del,
16746 +                              aufs_bindex_t bindex)
16747 +{
16748 +       int err;
16749 +       struct super_block *sb;
16750 +       struct dentry *root;
16751 +       struct au_fsctx_opts *a = fc->fs_private;
16752 +
16753 +       sb = a->sb;
16754 +       AuDebugOn(!sb);
16755 +
16756 +       err = -EINVAL;
16757 +       root = sb->s_root;
16758 +       aufs_read_lock(root, AuLock_FLUSH);
16759 +       if (bindex < 0 || au_sbbot(sb) < bindex) {
16760 +               errorfc(fc, "out of bounds, %d", bindex);
16761 +               goto out;
16762 +       }
16763 +
16764 +       err = 0;
16765 +       del->h_path.dentry = dget(au_h_dptr(root, bindex));
16766 +       del->h_path.mnt = mntget(au_sbr_mnt(sb, bindex));
16767 +
16768 +out:
16769 +       aufs_read_unlock(root, !AuLock_IR);
16770 +       AuTraceErr(err);
16771 +       return err;
16772 +}
16773 +#endif
16774 +
16775 +static int au_fsctx_parse_mod(struct fs_context *fc, struct au_opt_mod *mod,
16776 +                             struct fs_parameter *param)
16777 +{
16778 +       int err;
16779 +       struct path path;
16780 +       char *p;
16781 +
16782 +       err = -ENOMEM;
16783 +       /* will be freed by au_fsctx_free() */
16784 +       mod->path = kmemdup_nul(param->string, param->size, GFP_NOFS);
16785 +       if (unlikely(!mod->path))
16786 +               goto out;
16787 +
16788 +       err = -EINVAL;
16789 +       p = strchr(mod->path, '=');
16790 +       if (unlikely(!p)) {
16791 +               errorfc(fc, "no permission %s", mod->path);
16792 +               goto out;
16793 +       }
16794 +
16795 +       *p++ = 0;
16796 +       err = vfsub_kern_path(mod->path, AuOpt_LkupDirFlags, &path);
16797 +       if (unlikely(err)) {
16798 +               errorfc(fc, "lookup failed %s (%d)", mod->path, err);
16799 +               goto out;
16800 +       }
16801 +
16802 +       mod->perm = au_br_perm_val(p);
16803 +       AuDbg("mod path %s, perm 0x%x, %s", mod->path, mod->perm, p);
16804 +       mod->h_root = dget(path.dentry);
16805 +       path_put(&path);
16806 +
16807 +out:
16808 +       AuTraceErr(err);
16809 +       return err;
16810 +}
16811 +
16812 +#if 0 /* reserved for future use */
16813 +static int au_fsctx_parse_imod(struct fs_context *fc, struct au_opt_mod *mod,
16814 +                              char *ibrspec)
16815 +{
16816 +       int err, n;
16817 +       char *p;
16818 +       struct super_block *sb;
16819 +       struct dentry *root;
16820 +       struct au_fsctx_opts *a = fc->fs_private;
16821 +
16822 +       sb = a->sb;
16823 +       AuDebugOn(!sb);
16824 +
16825 +       err = -EINVAL;
16826 +       p = strchr(ibrspec, ':');
16827 +       if (unlikely(!p)) {
16828 +               errorfc(fc, "no index, %s", ibrspec);
16829 +               goto out;
16830 +       }
16831 +       *p++ = '\0';
16832 +       err = kstrtoint(ibrspec, 0, &n);
16833 +       if (unlikely(err)) {
16834 +               errorfc(fc, "bad integer in %s", ibrspec);
16835 +               goto out;
16836 +       }
16837 +       AuDbg("n %d\n", n);
16838 +
16839 +       root = sb->s_root;
16840 +       aufs_read_lock(root, AuLock_FLUSH);
16841 +       if (n < 0 || au_sbbot(sb) < n) {
16842 +               errorfc(fc, "out of bounds, %d", bindex);
16843 +               goto out_root;
16844 +       }
16845 +
16846 +       err = 0;
16847 +       mod->perm = au_br_perm_val(p);
16848 +       AuDbg("mod path %s, perm 0x%x, %s\n",
16849 +             mod->path, mod->perm, p);
16850 +       mod->h_root = dget(au_h_dptr(root, bindex));
16851 +
16852 +out_root:
16853 +       aufs_read_unlock(root, !AuLock_IR);
16854 +out:
16855 +       AuTraceErr(err);
16856 +       return err;
16857 +}
16858 +#endif
16859 +
16860 +static int au_fsctx_parse_xino(struct fs_context *fc,
16861 +                              struct au_opt_xino *xino,
16862 +                              struct fs_parameter *param)
16863 +{
16864 +       int err;
16865 +       struct au_fsctx_opts *a = fc->fs_private;
16866 +
16867 +       err = -ENOMEM;
16868 +       /* will be freed by au_opts_free() */
16869 +       xino->path = kmemdup_nul(param->string, param->size, GFP_NOFS);
16870 +       if (unlikely(!xino->path))
16871 +               goto out;
16872 +       AuDbg("path %s\n", xino->path);
16873 +
16874 +       xino->file = au_xino_create(a->sb, xino->path, /*silent*/0,
16875 +                                   /*wbrtop*/0);
16876 +       err = PTR_ERR(xino->file);
16877 +       if (IS_ERR(xino->file)) {
16878 +               xino->file = NULL;
16879 +               goto out;
16880 +       }
16881 +
16882 +       err = 0;
16883 +       if (unlikely(a->sb && xino->file->f_path.dentry->d_sb == a->sb)) {
16884 +               err = -EINVAL;
16885 +               errorfc(fc, "%s must be outside", xino->path);
16886 +       }
16887 +
16888 +out:
16889 +       AuTraceErr(err);
16890 +       return err;
16891 +}
16892 +
16893 +static
16894 +int au_fsctx_parse_xino_itrunc_path(struct fs_context *fc,
16895 +                                   struct au_opt_xino_itrunc *xino_itrunc,
16896 +                                   char *pathname)
16897 +{
16898 +       int err;
16899 +       aufs_bindex_t bbot, bindex;
16900 +       struct path path;
16901 +       struct dentry *root;
16902 +       struct au_fsctx_opts *a = fc->fs_private;
16903 +
16904 +       AuDebugOn(!a->sb);
16905 +
16906 +       err = vfsub_kern_path(pathname, AuOpt_LkupDirFlags, &path);
16907 +       if (unlikely(err)) {
16908 +               errorfc(fc, "lookup failed %s (%d)", pathname, err);
16909 +               goto out;
16910 +       }
16911 +
16912 +       xino_itrunc->bindex = -1;
16913 +       root = a->sb->s_root;
16914 +       aufs_read_lock(root, AuLock_FLUSH);
16915 +       bbot = au_sbbot(a->sb);
16916 +       for (bindex = 0; bindex <= bbot; bindex++) {
16917 +               if (au_h_dptr(root, bindex) == path.dentry) {
16918 +                       xino_itrunc->bindex = bindex;
16919 +                       break;
16920 +               }
16921 +       }
16922 +       aufs_read_unlock(root, !AuLock_IR);
16923 +       path_put(&path);
16924 +
16925 +       if (unlikely(xino_itrunc->bindex < 0)) {
16926 +               err = -EINVAL;
16927 +               errorfc(fc, "no such branch %s", pathname);
16928 +       }
16929 +
16930 +out:
16931 +       AuTraceErr(err);
16932 +       return err;
16933 +}
16934 +
16935 +static int au_fsctx_parse_xino_itrunc(struct fs_context *fc,
16936 +                                     struct au_opt_xino_itrunc *xino_itrunc,
16937 +                                     unsigned int bindex)
16938 +{
16939 +       int err;
16940 +       aufs_bindex_t bbot;
16941 +       struct super_block *sb;
16942 +       struct au_fsctx_opts *a = fc->fs_private;
16943 +
16944 +       sb = a->sb;
16945 +       AuDebugOn(!sb);
16946 +
16947 +       err = 0;
16948 +       si_noflush_read_lock(sb);
16949 +       bbot = au_sbbot(sb);
16950 +       si_read_unlock(sb);
16951 +       if (bindex <= bbot)
16952 +               xino_itrunc->bindex = bindex;
16953 +       else {
16954 +               err = -EINVAL;
16955 +               errorfc(fc, "out of bounds, %u", bindex);
16956 +       }
16957 +
16958 +       AuTraceErr(err);
16959 +       return err;
16960 +}
16961 +
16962 +static int au_fsctx_parse_param(struct fs_context *fc, struct fs_parameter *param)
16963 +{
16964 +       int err, token;
16965 +       struct fs_parse_result result;
16966 +       struct au_fsctx_opts *a = fc->fs_private;
16967 +       struct au_opt *opt = a->opt;
16968 +
16969 +       AuDbg("fc %p, param {key %s, string %s}\n",
16970 +             fc, param->key, param->string);
16971 +       err = fs_parse(fc, aufs_fsctx_paramspec, param, &result);
16972 +       if (unlikely(err < 0))
16973 +               goto out;
16974 +       token = err;
16975 +       AuDbg("token %d, res{negated %d, uint64 %llu}\n",
16976 +             token, result.negated, result.uint_64);
16977 +
16978 +       err = -EINVAL;
16979 +       a->skipped = 0;
16980 +       switch (token) {
16981 +       case Opt_br:
16982 +               err = au_fsctx_parse_br(fc, param->string);
16983 +               break;
16984 +       case Opt_add:
16985 +               err = au_fsctx_parse_add(fc, param->string);
16986 +               break;
16987 +       case Opt_append:
16988 +               err = au_fsctx_parse_do_add(fc, opt, param->string, param->size,
16989 +                                           /*dummy bindex*/1);
16990 +               break;
16991 +       case Opt_prepend:
16992 +               err = au_fsctx_parse_do_add(fc, opt, param->string, param->size,
16993 +                                           /*bindex*/0);
16994 +               break;
16995 +
16996 +       case Opt_del:
16997 +               err = au_fsctx_parse_del(fc, &opt->del, param);
16998 +               break;
16999 +#if 0 /* reserved for future use */
17000 +       case Opt_idel:
17001 +               if (!a->sb) {
17002 +                       err = 0;
17003 +                       a->skipped = 1;
17004 +                       break;
17005 +               }
17006 +               del->pathname = "(indexed)";
17007 +               err = au_opts_parse_idel(fc, &opt->del, result.uint_32);
17008 +               break;
17009 +#endif
17010 +
17011 +       case Opt_mod:
17012 +               err = au_fsctx_parse_mod(fc, &opt->mod, param);
17013 +               break;
17014 +#ifdef IMOD /* reserved for future use */
17015 +       case Opt_imod:
17016 +               if (!a->sb) {
17017 +                       err = 0;
17018 +                       a->skipped = 1;
17019 +                       break;
17020 +               }
17021 +               u.mod->path = "(indexed)";
17022 +               err = au_opts_parse_imod(fc, &opt->mod, param->string);
17023 +               break;
17024 +#endif
17025 +
17026 +       case Opt_xino:
17027 +               err = au_fsctx_parse_xino(fc, &opt->xino, param);
17028 +               break;
17029 +       case Opt_trunc_xino_path:
17030 +               if (!a->sb) {
17031 +                       errorfc(fc, "no such branch %s", param->string);
17032 +                       break;
17033 +               }
17034 +               err = au_fsctx_parse_xino_itrunc_path(fc, &opt->xino_itrunc,
17035 +                                                     param->string);
17036 +               break;
17037 +#if 0
17038 +       case Opt_trunc_xino_v:
17039 +               if (!a->sb) {
17040 +                       err = 0;
17041 +                       a->skipped = 1;
17042 +                       break;
17043 +               }
17044 +               err = au_fsctx_parse_xino_itrunc_path(fc, &opt->xino_itrunc,
17045 +                                                     param->string);
17046 +               break;
17047 +#endif
17048 +       case Opt_itrunc_xino:
17049 +               if (!a->sb) {
17050 +                       errorfc(fc, "out of bounds %s", param->string);
17051 +                       break;
17052 +               }
17053 +               err = au_fsctx_parse_xino_itrunc(fc, &opt->xino_itrunc,
17054 +                                                result.int_32);
17055 +               break;
17056 +
17057 +       case Opt_dirwh:
17058 +               err = 0;
17059 +               opt->dirwh = result.int_32;
17060 +               break;
17061 +
17062 +       case Opt_rdcache:
17063 +               if (unlikely(result.int_32 > AUFS_RDCACHE_MAX)) {
17064 +                       errorfc(fc, "rdcache must be smaller than %d",
17065 +                               AUFS_RDCACHE_MAX);
17066 +                       break;
17067 +               }
17068 +               err = 0;
17069 +               opt->rdcache = result.int_32;
17070 +               break;
17071 +
17072 +       case Opt_rdblk:
17073 +               err = 0;
17074 +               opt->rdblk = AUFS_RDBLK_DEF;
17075 +               if (!strcmp(param->string, "def"))
17076 +                       break;
17077 +
17078 +               err = kstrtoint(param->string, 0, &result.int_32);
17079 +               if (unlikely(err)) {
17080 +                       errorfc(fc, "bad value in %s", param->key);
17081 +                       break;
17082 +               }
17083 +               err = -EINVAL;
17084 +               if (unlikely(result.int_32 < 0
17085 +                            || result.int_32 > KMALLOC_MAX_SIZE)) {
17086 +                       errorfc(fc, "bad value in %s", param->key);
17087 +                       break;
17088 +               }
17089 +               if (unlikely(result.int_32 && result.int_32 < NAME_MAX)) {
17090 +                       errorfc(fc, "rdblk must be larger than %d", NAME_MAX);
17091 +                       break;
17092 +               }
17093 +               err = 0;
17094 +               opt->rdblk = result.int_32;
17095 +               break;
17096 +
17097 +       case Opt_rdhash:
17098 +               err = 0;
17099 +               opt->rdhash = AUFS_RDHASH_DEF;
17100 +               if (!strcmp(param->string, "def"))
17101 +                       break;
17102 +
17103 +               err = kstrtoint(param->string, 0, &result.int_32);
17104 +               if (unlikely(err)) {
17105 +                       errorfc(fc, "bad value in %s", param->key);
17106 +                       break;
17107 +               }
17108 +               /* how about zero? */
17109 +               if (result.int_32 < 0
17110 +                   || result.int_32 * sizeof(struct hlist_head)
17111 +                   > KMALLOC_MAX_SIZE) {
17112 +                       err = -EINVAL;
17113 +                       errorfc(fc, "bad integer in %s", param->key);
17114 +                       break;
17115 +               }
17116 +               opt->rdhash = result.int_32;
17117 +               break;
17118 +
17119 +       case Opt_diropq:
17120 +               /*
17121 +                * As other options, fs/aufs/opts.c can handle these strings by
17122 +                * match_token().  But "diropq=" is deprecated now and will
17123 +                * never have other value.  So simple strcmp() is enough here.
17124 +                */
17125 +               if (!strcmp(param->string, "a") ||
17126 +                   !strcmp(param->string, "always")) {
17127 +                       err = 0;
17128 +                       opt->type = Opt_diropq_a;
17129 +               } else if (!strcmp(param->string, "w") ||
17130 +                          !strcmp(param->string, "whiteouted")) {
17131 +                       err = 0;
17132 +                       opt->type = Opt_diropq_w;
17133 +               } else
17134 +                       errorfc(fc, "unknown value %s", param->string);
17135 +               break;
17136 +
17137 +       case Opt_udba:
17138 +               opt->udba = au_udba_val(param->string);
17139 +               if (opt->udba >= 0)
17140 +                       err = 0;
17141 +               else
17142 +                       errorf(fc, "wrong value, %s", param->string);
17143 +               break;
17144 +
17145 +       case Opt_wbr_create:
17146 +               opt->wbr_create.wbr_create
17147 +                       = au_wbr_create_val(param->string, &opt->wbr_create);
17148 +               if (opt->wbr_create.wbr_create >= 0)
17149 +                       err = 0;
17150 +               else
17151 +                       errorf(fc, "wrong value, %s", param->key);
17152 +               break;
17153 +
17154 +       case Opt_wbr_copyup:
17155 +               opt->wbr_copyup = au_wbr_copyup_val(param->string);
17156 +               if (opt->wbr_copyup >= 0)
17157 +                       err = 0;
17158 +               else
17159 +                       errorfc(fc, "wrong value, %s", param->key);
17160 +               break;
17161 +
17162 +       case Opt_fhsm_sec:
17163 +               if (unlikely(result.int_32 < 0)) {
17164 +                       errorfc(fc, "bad integer in %s\n", param->key);
17165 +                       break;
17166 +               }
17167 +               err = 0;
17168 +               if (sysaufs_brs)
17169 +                       opt->fhsm_second = result.int_32;
17170 +               else
17171 +                       warnfc(fc, "ignored %s %s", param->key, param->string);
17172 +               break;
17173 +
17174 +       /* simple true/false flag */
17175 +#define au_fsctx_TF(name)                              \
17176 +               case Opt_##name:                        \
17177 +                       err = 0;                        \
17178 +                       opt->tf = !result.negated;      \
17179 +                       break;
17180 +       au_fsctx_TF(trunc_xino);
17181 +       au_fsctx_TF(trunc_xib);
17182 +       au_fsctx_TF(dirperm1);
17183 +       au_fsctx_TF(plink);
17184 +       au_fsctx_TF(shwh);
17185 +       au_fsctx_TF(dio);
17186 +       au_fsctx_TF(warn_perm);
17187 +       au_fsctx_TF(verbose);
17188 +       au_fsctx_TF(sum);
17189 +       au_fsctx_TF(dirren);
17190 +       au_fsctx_TF(acl);
17191 +#undef au_fsctx_TF
17192 +
17193 +       case Opt_noverbose:
17194 +               err = 0;
17195 +               opt->type = Opt_verbose;
17196 +               opt->tf = false;
17197 +               break;
17198 +
17199 +       case Opt_noxino:
17200 +               fallthrough;
17201 +       case Opt_list_plink:
17202 +               fallthrough;
17203 +       case Opt_wsum:
17204 +               err = 0;
17205 +               break;
17206 +
17207 +       case Opt_ignore:
17208 +               warnfc(fc, "ignored %s", param->key);
17209 +               fallthrough;
17210 +       case Opt_ignore_silent:
17211 +               a->skipped = 1;
17212 +               err = 0;
17213 +               break;
17214 +       default:
17215 +               a->skipped = 1;
17216 +               err = -ENOPARAM;
17217 +               break;
17218 +       }
17219 +       if (unlikely(err))
17220 +               goto out;
17221 +       if (a->skipped)
17222 +               goto out;
17223 +
17224 +       switch (token) {
17225 +       case Opt_br:
17226 +               fallthrough;
17227 +       case Opt_noverbose:
17228 +               fallthrough;
17229 +       case Opt_diropq:
17230 +               break;
17231 +       default:
17232 +               opt->type = token;
17233 +               break;
17234 +       }
17235 +       opt++;
17236 +       if (unlikely(opt > a->opt_tail)) {
17237 +               err = -E2BIG;
17238 +               opt--;
17239 +       }
17240 +       opt->type = Opt_tail;
17241 +       a->opt = opt;
17242 +
17243 +out:
17244 +       return err;
17245 +}
17246 +
17247 +/*
17248 + * these options accept both 'name=val' and 'name:val' form.
17249 + * some accept optional '=' in its value.
17250 + * eg. br:/br1=rw:/br2=ro and br=/br1=rw:/br2=ro
17251 + */
17252 +static inline unsigned int is_colonopt(char *str)
17253 +{
17254 +#define do_test(name)                                  \
17255 +       if (!strncmp(str, name ":", sizeof(name)))      \
17256 +               return sizeof(name) - 1;
17257 +       do_test("br");
17258 +       do_test("add");
17259 +       do_test("ins");
17260 +       do_test("append");
17261 +       do_test("prepend");
17262 +       do_test("del");
17263 +       /* do_test("idel"); */
17264 +       do_test("mod");
17265 +       /* do_test("imod"); */
17266 +#undef do_test
17267 +
17268 +       return 0;
17269 +}
17270 +
17271 +static int au_fsctx_parse_monolithic(struct fs_context *fc, void *data)
17272 +{
17273 +       int err;
17274 +       unsigned int u;
17275 +       char *str;
17276 +       struct au_fsctx_opts *a = fc->fs_private;
17277 +
17278 +       str = data;
17279 +       AuDbg("str %s\n", str);
17280 +       while (str) {
17281 +               u = is_colonopt(str);
17282 +               if (u)
17283 +                       str[u] = '=';
17284 +               str = strchr(str, ',');
17285 +               if (!str)
17286 +                       break;
17287 +               str++;
17288 +       }
17289 +       str = data;
17290 +       AuDbg("str %s\n", str);
17291 +
17292 +       err = generic_parse_monolithic(fc, str);
17293 +       AuTraceErr(err);
17294 +       au_fsctx_dump(&a->opts);
17295 +
17296 +       return err;
17297 +}
17298 +
17299 +/* ---------------------------------------------------------------------- */
17300 +
17301 +static void au_fsctx_opts_free(struct au_opts *opts)
17302 +{
17303 +       struct au_opt *opt;
17304 +
17305 +       opt = opts->opt;
17306 +       while (opt->type != Opt_tail) {
17307 +               switch (opt->type) {
17308 +               case Opt_add:
17309 +                       fallthrough;
17310 +               case Opt_append:
17311 +                       fallthrough;
17312 +               case Opt_prepend:
17313 +                       kfree(opt->add.pathname);
17314 +                       path_put(&opt->add.path);
17315 +                       break;
17316 +               case Opt_del:
17317 +                       kfree(opt->del.pathname);
17318 +                       fallthrough;
17319 +               case Opt_idel:
17320 +                       path_put(&opt->del.h_path);
17321 +                       break;
17322 +               case Opt_mod:
17323 +                       kfree(opt->mod.path);
17324 +                       fallthrough;
17325 +               case Opt_imod:
17326 +                       dput(opt->mod.h_root);
17327 +                       break;
17328 +               case Opt_xino:
17329 +                       kfree(opt->xino.path);
17330 +                       fput(opt->xino.file);
17331 +                       break;
17332 +               }
17333 +               opt++;
17334 +       }
17335 +}
17336 +
17337 +static void au_fsctx_free(struct fs_context *fc)
17338 +{
17339 +       struct au_fsctx_opts *a = fc->fs_private;
17340 +
17341 +       /* fs_type=%p, root=%pD */
17342 +       AuDbg("fc %p{sb_flags 0x%x, sb_flags_mask 0x%x, purpose %u\n",
17343 +             fc, fc->sb_flags, fc->sb_flags_mask, fc->purpose);
17344 +
17345 +       kobject_put(&a->sbinfo->si_kobj);
17346 +       au_fsctx_opts_free(&a->opts);
17347 +       free_page((unsigned long)a->opts.opt);
17348 +       au_kfree_rcu(a);
17349 +}
17350 +
17351 +static const struct fs_context_operations au_fsctx_ops = {
17352 +       .free                   = au_fsctx_free,
17353 +       .parse_param            = au_fsctx_parse_param,
17354 +       .parse_monolithic       = au_fsctx_parse_monolithic,
17355 +       .get_tree               = au_fsctx_get_tree,
17356 +       .reconfigure            = au_fsctx_reconfigure
17357 +       /*
17358 +        * nfs4 requires ->dup()? No.
17359 +        * I don't know what is this ->dup() for.
17360 +        */
17361 +};
17362 +
17363 +int aufs_fsctx_init(struct fs_context *fc)
17364 +{
17365 +       int err;
17366 +       struct au_fsctx_opts *a;
17367 +
17368 +       /* fs_type=%p, root=%pD */
17369 +       AuDbg("fc %p{sb_flags 0x%x, sb_flags_mask 0x%x, purpose %u\n",
17370 +             fc, fc->sb_flags, fc->sb_flags_mask, fc->purpose);
17371 +
17372 +       /* they will be freed by au_fsctx_free() */
17373 +       err = -ENOMEM;
17374 +       a = kzalloc(sizeof(*a), GFP_NOFS);
17375 +       if (unlikely(!a))
17376 +               goto out;
17377 +       a->bindex = 0;
17378 +       a->opts.opt = (void *)__get_free_page(GFP_NOFS);
17379 +       if (unlikely(!a->opts.opt))
17380 +               goto out_a;
17381 +       a->opt = a->opts.opt;
17382 +       a->opt->type = Opt_tail;
17383 +       a->opts.max_opt = PAGE_SIZE / sizeof(*a->opts.opt);
17384 +       a->opt_tail = a->opt + a->opts.max_opt - 1;
17385 +       a->opts.sb_flags = fc->sb_flags;
17386 +
17387 +       a->sb = NULL;
17388 +       if (fc->root) {
17389 +               AuDebugOn(fc->purpose != FS_CONTEXT_FOR_RECONFIGURE);
17390 +               a->opts.flags = AuOpts_REMOUNT;
17391 +               a->sb = fc->root->d_sb;
17392 +               a->sbinfo = au_sbi(a->sb);
17393 +               kobject_get(&a->sbinfo->si_kobj);
17394 +       } else {
17395 +               a->sbinfo = au_si_alloc(a->sb);
17396 +               AuDebugOn(!a->sbinfo);
17397 +               err = PTR_ERR(a->sbinfo);
17398 +               if (IS_ERR(a->sbinfo))
17399 +                       goto out_opt;
17400 +               au_rw_write_unlock(&a->sbinfo->si_rwsem);
17401 +       }
17402 +
17403 +       err = 0;
17404 +       fc->fs_private = a;
17405 +       fc->ops = &au_fsctx_ops;
17406 +       goto out; /* success */
17407 +
17408 +out_opt:
17409 +       free_page((unsigned long)a->opts.opt);
17410 +out_a:
17411 +       au_kfree_rcu(a);
17412 +out:
17413 +       AuTraceErr(err);
17414 +       return err;
17415 +}
17416 diff -urN /usr/share/empty/fs/aufs/fstype.h linux/fs/aufs/fstype.h
17417 --- /usr/share/empty/fs/aufs/fstype.h   1970-01-01 01:00:00.000000000 +0100
17418 +++ linux/fs/aufs/fstype.h      2022-03-21 14:49:05.723299677 +0100
17419 @@ -0,0 +1,401 @@
17420 +/* SPDX-License-Identifier: GPL-2.0 */
17421 +/*
17422 + * Copyright (C) 2005-2021 Junjiro R. Okajima
17423 + *
17424 + * This program, aufs is free software; you can redistribute it and/or modify
17425 + * it under the terms of the GNU General Public License as published by
17426 + * the Free Software Foundation; either version 2 of the License, or
17427 + * (at your option) any later version.
17428 + *
17429 + * This program is distributed in the hope that it will be useful,
17430 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
17431 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17432 + * GNU General Public License for more details.
17433 + *
17434 + * You should have received a copy of the GNU General Public License
17435 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17436 + */
17437 +
17438 +/*
17439 + * judging filesystem type
17440 + */
17441 +
17442 +#ifndef __AUFS_FSTYPE_H__
17443 +#define __AUFS_FSTYPE_H__
17444 +
17445 +#ifdef __KERNEL__
17446 +
17447 +#include <linux/fs.h>
17448 +#include <linux/magic.h>
17449 +#include <linux/nfs_fs.h>
17450 +#include <linux/romfs_fs.h>
17451 +
17452 +static inline int au_test_aufs(struct super_block *sb)
17453 +{
17454 +       return sb->s_magic == AUFS_SUPER_MAGIC;
17455 +}
17456 +
17457 +static inline const char *au_sbtype(struct super_block *sb)
17458 +{
17459 +       return sb->s_type->name;
17460 +}
17461 +
17462 +static inline int au_test_iso9660(struct super_block *sb __maybe_unused)
17463 +{
17464 +#if IS_ENABLED(CONFIG_ISO9660_FS)
17465 +       return sb->s_magic == ISOFS_SUPER_MAGIC;
17466 +#else
17467 +       return 0;
17468 +#endif
17469 +}
17470 +
17471 +static inline int au_test_romfs(struct super_block *sb __maybe_unused)
17472 +{
17473 +#if IS_ENABLED(CONFIG_ROMFS_FS)
17474 +       return sb->s_magic == ROMFS_MAGIC;
17475 +#else
17476 +       return 0;
17477 +#endif
17478 +}
17479 +
17480 +static inline int au_test_cramfs(struct super_block *sb __maybe_unused)
17481 +{
17482 +#if IS_ENABLED(CONFIG_CRAMFS)
17483 +       return sb->s_magic == CRAMFS_MAGIC;
17484 +#endif
17485 +       return 0;
17486 +}
17487 +
17488 +static inline int au_test_nfs(struct super_block *sb __maybe_unused)
17489 +{
17490 +#if IS_ENABLED(CONFIG_NFS_FS)
17491 +       return sb->s_magic == NFS_SUPER_MAGIC;
17492 +#else
17493 +       return 0;
17494 +#endif
17495 +}
17496 +
17497 +static inline int au_test_fuse(struct super_block *sb __maybe_unused)
17498 +{
17499 +#if IS_ENABLED(CONFIG_FUSE_FS)
17500 +       return sb->s_magic == FUSE_SUPER_MAGIC;
17501 +#else
17502 +       return 0;
17503 +#endif
17504 +}
17505 +
17506 +static inline int au_test_xfs(struct super_block *sb __maybe_unused)
17507 +{
17508 +#if IS_ENABLED(CONFIG_XFS_FS)
17509 +       return sb->s_magic == XFS_SB_MAGIC;
17510 +#else
17511 +       return 0;
17512 +#endif
17513 +}
17514 +
17515 +static inline int au_test_tmpfs(struct super_block *sb __maybe_unused)
17516 +{
17517 +#ifdef CONFIG_TMPFS
17518 +       return sb->s_magic == TMPFS_MAGIC;
17519 +#else
17520 +       return 0;
17521 +#endif
17522 +}
17523 +
17524 +static inline int au_test_ecryptfs(struct super_block *sb __maybe_unused)
17525 +{
17526 +#if IS_ENABLED(CONFIG_ECRYPT_FS)
17527 +       return !strcmp(au_sbtype(sb), "ecryptfs");
17528 +#else
17529 +       return 0;
17530 +#endif
17531 +}
17532 +
17533 +static inline int au_test_ramfs(struct super_block *sb)
17534 +{
17535 +       return sb->s_magic == RAMFS_MAGIC;
17536 +}
17537 +
17538 +static inline int au_test_ubifs(struct super_block *sb __maybe_unused)
17539 +{
17540 +#if IS_ENABLED(CONFIG_UBIFS_FS)
17541 +       return sb->s_magic == UBIFS_SUPER_MAGIC;
17542 +#else
17543 +       return 0;
17544 +#endif
17545 +}
17546 +
17547 +static inline int au_test_procfs(struct super_block *sb __maybe_unused)
17548 +{
17549 +#ifdef CONFIG_PROC_FS
17550 +       return sb->s_magic == PROC_SUPER_MAGIC;
17551 +#else
17552 +       return 0;
17553 +#endif
17554 +}
17555 +
17556 +static inline int au_test_sysfs(struct super_block *sb __maybe_unused)
17557 +{
17558 +#ifdef CONFIG_SYSFS
17559 +       return sb->s_magic == SYSFS_MAGIC;
17560 +#else
17561 +       return 0;
17562 +#endif
17563 +}
17564 +
17565 +static inline int au_test_configfs(struct super_block *sb __maybe_unused)
17566 +{
17567 +#if IS_ENABLED(CONFIG_CONFIGFS_FS)
17568 +       return sb->s_magic == CONFIGFS_MAGIC;
17569 +#else
17570 +       return 0;
17571 +#endif
17572 +}
17573 +
17574 +static inline int au_test_minix(struct super_block *sb __maybe_unused)
17575 +{
17576 +#if IS_ENABLED(CONFIG_MINIX_FS)
17577 +       return sb->s_magic == MINIX3_SUPER_MAGIC
17578 +               || sb->s_magic == MINIX2_SUPER_MAGIC
17579 +               || sb->s_magic == MINIX2_SUPER_MAGIC2
17580 +               || sb->s_magic == MINIX_SUPER_MAGIC
17581 +               || sb->s_magic == MINIX_SUPER_MAGIC2;
17582 +#else
17583 +       return 0;
17584 +#endif
17585 +}
17586 +
17587 +static inline int au_test_fat(struct super_block *sb __maybe_unused)
17588 +{
17589 +#if IS_ENABLED(CONFIG_FAT_FS)
17590 +       return sb->s_magic == MSDOS_SUPER_MAGIC;
17591 +#else
17592 +       return 0;
17593 +#endif
17594 +}
17595 +
17596 +static inline int au_test_msdos(struct super_block *sb)
17597 +{
17598 +       return au_test_fat(sb);
17599 +}
17600 +
17601 +static inline int au_test_vfat(struct super_block *sb)
17602 +{
17603 +       return au_test_fat(sb);
17604 +}
17605 +
17606 +static inline int au_test_securityfs(struct super_block *sb __maybe_unused)
17607 +{
17608 +#ifdef CONFIG_SECURITYFS
17609 +       return sb->s_magic == SECURITYFS_MAGIC;
17610 +#else
17611 +       return 0;
17612 +#endif
17613 +}
17614 +
17615 +static inline int au_test_squashfs(struct super_block *sb __maybe_unused)
17616 +{
17617 +#if IS_ENABLED(CONFIG_SQUASHFS)
17618 +       return sb->s_magic == SQUASHFS_MAGIC;
17619 +#else
17620 +       return 0;
17621 +#endif
17622 +}
17623 +
17624 +static inline int au_test_btrfs(struct super_block *sb __maybe_unused)
17625 +{
17626 +#if IS_ENABLED(CONFIG_BTRFS_FS)
17627 +       return sb->s_magic == BTRFS_SUPER_MAGIC;
17628 +#else
17629 +       return 0;
17630 +#endif
17631 +}
17632 +
17633 +static inline int au_test_xenfs(struct super_block *sb __maybe_unused)
17634 +{
17635 +#if IS_ENABLED(CONFIG_XENFS)
17636 +       return sb->s_magic == XENFS_SUPER_MAGIC;
17637 +#else
17638 +       return 0;
17639 +#endif
17640 +}
17641 +
17642 +static inline int au_test_debugfs(struct super_block *sb __maybe_unused)
17643 +{
17644 +#ifdef CONFIG_DEBUG_FS
17645 +       return sb->s_magic == DEBUGFS_MAGIC;
17646 +#else
17647 +       return 0;
17648 +#endif
17649 +}
17650 +
17651 +static inline int au_test_nilfs(struct super_block *sb __maybe_unused)
17652 +{
17653 +#if IS_ENABLED(CONFIG_NILFS)
17654 +       return sb->s_magic == NILFS_SUPER_MAGIC;
17655 +#else
17656 +       return 0;
17657 +#endif
17658 +}
17659 +
17660 +static inline int au_test_hfsplus(struct super_block *sb __maybe_unused)
17661 +{
17662 +#if IS_ENABLED(CONFIG_HFSPLUS_FS)
17663 +       return sb->s_magic == HFSPLUS_SUPER_MAGIC;
17664 +#else
17665 +       return 0;
17666 +#endif
17667 +}
17668 +
17669 +/* ---------------------------------------------------------------------- */
17670 +/*
17671 + * they can't be an aufs branch.
17672 + */
17673 +static inline int au_test_fs_unsuppoted(struct super_block *sb)
17674 +{
17675 +       return
17676 +#ifndef CONFIG_AUFS_BR_RAMFS
17677 +               au_test_ramfs(sb) ||
17678 +#endif
17679 +               au_test_procfs(sb)
17680 +               || au_test_sysfs(sb)
17681 +               || au_test_configfs(sb)
17682 +               || au_test_debugfs(sb)
17683 +               || au_test_securityfs(sb)
17684 +               || au_test_xenfs(sb)
17685 +               || au_test_ecryptfs(sb)
17686 +               /* || !strcmp(au_sbtype(sb), "unionfs") */
17687 +               || au_test_aufs(sb); /* will be supported in next version */
17688 +}
17689 +
17690 +static inline int au_test_fs_remote(struct super_block *sb)
17691 +{
17692 +       return !au_test_tmpfs(sb)
17693 +#ifdef CONFIG_AUFS_BR_RAMFS
17694 +               && !au_test_ramfs(sb)
17695 +#endif
17696 +               && !(sb->s_type->fs_flags & FS_REQUIRES_DEV);
17697 +}
17698 +
17699 +/* ---------------------------------------------------------------------- */
17700 +
17701 +/*
17702 + * Note: these functions (below) are created after reading ->getattr() in all
17703 + * filesystems under linux/fs. it means we have to do so in every update...
17704 + */
17705 +
17706 +/*
17707 + * some filesystems require getattr to refresh the inode attributes before
17708 + * referencing.
17709 + * in most cases, we can rely on the inode attribute in NFS (or every remote fs)
17710 + * and leave the work for d_revalidate()
17711 + */
17712 +static inline int au_test_fs_refresh_iattr(struct super_block *sb)
17713 +{
17714 +       return au_test_nfs(sb)
17715 +               || au_test_fuse(sb)
17716 +               /* || au_test_btrfs(sb) */      /* untested */
17717 +               ;
17718 +}
17719 +
17720 +/*
17721 + * filesystems which don't maintain i_size or i_blocks.
17722 + */
17723 +static inline int au_test_fs_bad_iattr_size(struct super_block *sb)
17724 +{
17725 +       return au_test_xfs(sb)
17726 +               || au_test_btrfs(sb)
17727 +               || au_test_ubifs(sb)
17728 +               || au_test_hfsplus(sb)  /* maintained, but incorrect */
17729 +               /* || au_test_minix(sb) */      /* untested */
17730 +               ;
17731 +}
17732 +
17733 +/*
17734 + * filesystems which don't store the correct value in some of their inode
17735 + * attributes.
17736 + */
17737 +static inline int au_test_fs_bad_iattr(struct super_block *sb)
17738 +{
17739 +       return au_test_fs_bad_iattr_size(sb)
17740 +               || au_test_fat(sb)
17741 +               || au_test_msdos(sb)
17742 +               || au_test_vfat(sb);
17743 +}
17744 +
17745 +/* they don't check i_nlink in link(2) */
17746 +static inline int au_test_fs_no_limit_nlink(struct super_block *sb)
17747 +{
17748 +       return au_test_tmpfs(sb)
17749 +#ifdef CONFIG_AUFS_BR_RAMFS
17750 +               || au_test_ramfs(sb)
17751 +#endif
17752 +               || au_test_ubifs(sb)
17753 +               || au_test_hfsplus(sb);
17754 +}
17755 +
17756 +/*
17757 + * filesystems which sets S_NOATIME and S_NOCMTIME.
17758 + */
17759 +static inline int au_test_fs_notime(struct super_block *sb)
17760 +{
17761 +       return au_test_nfs(sb)
17762 +               || au_test_fuse(sb)
17763 +               || au_test_ubifs(sb)
17764 +               ;
17765 +}
17766 +
17767 +/* temporary support for i#1 in cramfs */
17768 +static inline int au_test_fs_unique_ino(struct inode *inode)
17769 +{
17770 +       if (au_test_cramfs(inode->i_sb))
17771 +               return inode->i_ino != 1;
17772 +       return 1;
17773 +}
17774 +
17775 +/* ---------------------------------------------------------------------- */
17776 +
17777 +/*
17778 + * the filesystem where the xino files placed must support i/o after unlink and
17779 + * maintain i_size and i_blocks.
17780 + */
17781 +static inline int au_test_fs_bad_xino(struct super_block *sb)
17782 +{
17783 +       return au_test_fs_remote(sb)
17784 +               || au_test_fs_bad_iattr_size(sb)
17785 +               /* don't want unnecessary work for xino */
17786 +               || au_test_aufs(sb)
17787 +               || au_test_ecryptfs(sb)
17788 +               || au_test_nilfs(sb);
17789 +}
17790 +
17791 +static inline int au_test_fs_trunc_xino(struct super_block *sb)
17792 +{
17793 +       return au_test_tmpfs(sb)
17794 +               || au_test_ramfs(sb);
17795 +}
17796 +
17797 +/*
17798 + * test if the @sb is real-readonly.
17799 + */
17800 +static inline int au_test_fs_rr(struct super_block *sb)
17801 +{
17802 +       return au_test_squashfs(sb)
17803 +               || au_test_iso9660(sb)
17804 +               || au_test_cramfs(sb)
17805 +               || au_test_romfs(sb);
17806 +}
17807 +
17808 +/*
17809 + * test if the @inode is nfs with 'noacl' option
17810 + * NFS always sets SB_POSIXACL regardless its mount option 'noacl.'
17811 + */
17812 +static inline int au_test_nfs_noacl(struct inode *inode)
17813 +{
17814 +       return au_test_nfs(inode->i_sb)
17815 +               /* && IS_POSIXACL(inode) */
17816 +               && !nfs_server_capable(inode, NFS_CAP_ACLS);
17817 +}
17818 +
17819 +#endif /* __KERNEL__ */
17820 +#endif /* __AUFS_FSTYPE_H__ */
17821 diff -urN /usr/share/empty/fs/aufs/hbl.h linux/fs/aufs/hbl.h
17822 --- /usr/share/empty/fs/aufs/hbl.h      1970-01-01 01:00:00.000000000 +0100
17823 +++ linux/fs/aufs/hbl.h 2022-03-21 14:49:05.723299677 +0100
17824 @@ -0,0 +1,65 @@
17825 +/* SPDX-License-Identifier: GPL-2.0 */
17826 +/*
17827 + * Copyright (C) 2017-2021 Junjiro R. Okajima
17828 + *
17829 + * This program, aufs is free software; you can redistribute it and/or modify
17830 + * it under the terms of the GNU General Public License as published by
17831 + * the Free Software Foundation; either version 2 of the License, or
17832 + * (at your option) any later version.
17833 + *
17834 + * This program is distributed in the hope that it will be useful,
17835 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
17836 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17837 + * GNU General Public License for more details.
17838 + *
17839 + * You should have received a copy of the GNU General Public License
17840 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17841 + */
17842 +
17843 +/*
17844 + * helpers for hlist_bl.h
17845 + */
17846 +
17847 +#ifndef __AUFS_HBL_H__
17848 +#define __AUFS_HBL_H__
17849 +
17850 +#ifdef __KERNEL__
17851 +
17852 +#include <linux/list_bl.h>
17853 +
17854 +static inline void au_hbl_add(struct hlist_bl_node *node,
17855 +                             struct hlist_bl_head *hbl)
17856 +{
17857 +       hlist_bl_lock(hbl);
17858 +       hlist_bl_add_head(node, hbl);
17859 +       hlist_bl_unlock(hbl);
17860 +}
17861 +
17862 +static inline void au_hbl_del(struct hlist_bl_node *node,
17863 +                             struct hlist_bl_head *hbl)
17864 +{
17865 +       hlist_bl_lock(hbl);
17866 +       hlist_bl_del(node);
17867 +       hlist_bl_unlock(hbl);
17868 +}
17869 +
17870 +#define au_hbl_for_each(pos, head)                                     \
17871 +       for (pos = hlist_bl_first(head);                                \
17872 +            pos;                                                       \
17873 +            pos = pos->next)
17874 +
17875 +static inline unsigned long au_hbl_count(struct hlist_bl_head *hbl)
17876 +{
17877 +       unsigned long cnt;
17878 +       struct hlist_bl_node *pos;
17879 +
17880 +       cnt = 0;
17881 +       hlist_bl_lock(hbl);
17882 +       au_hbl_for_each(pos, hbl)
17883 +               cnt++;
17884 +       hlist_bl_unlock(hbl);
17885 +       return cnt;
17886 +}
17887 +
17888 +#endif /* __KERNEL__ */
17889 +#endif /* __AUFS_HBL_H__ */
17890 diff -urN /usr/share/empty/fs/aufs/hfsnotify.c linux/fs/aufs/hfsnotify.c
17891 --- /usr/share/empty/fs/aufs/hfsnotify.c        1970-01-01 01:00:00.000000000 +0100
17892 +++ linux/fs/aufs/hfsnotify.c   2022-03-21 14:49:05.723299677 +0100
17893 @@ -0,0 +1,289 @@
17894 +// SPDX-License-Identifier: GPL-2.0
17895 +/*
17896 + * Copyright (C) 2005-2021 Junjiro R. Okajima
17897 + *
17898 + * This program, aufs is free software; you can redistribute it and/or modify
17899 + * it under the terms of the GNU General Public License as published by
17900 + * the Free Software Foundation; either version 2 of the License, or
17901 + * (at your option) any later version.
17902 + *
17903 + * This program is distributed in the hope that it will be useful,
17904 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
17905 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17906 + * GNU General Public License for more details.
17907 + *
17908 + * You should have received a copy of the GNU General Public License
17909 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17910 + */
17911 +
17912 +/*
17913 + * fsnotify for the lower directories
17914 + */
17915 +
17916 +#include "aufs.h"
17917 +
17918 +/* FS_IN_IGNORED is unnecessary */
17919 +static const __u32 AuHfsnMask = (FS_MOVED_TO | FS_MOVED_FROM | FS_DELETE
17920 +                                | FS_CREATE | FS_EVENT_ON_CHILD);
17921 +static DECLARE_WAIT_QUEUE_HEAD(au_hfsn_wq);
17922 +static __cacheline_aligned_in_smp atomic64_t au_hfsn_ifree = ATOMIC64_INIT(0);
17923 +
17924 +static void au_hfsn_free_mark(struct fsnotify_mark *mark)
17925 +{
17926 +       struct au_hnotify *hn = container_of(mark, struct au_hnotify,
17927 +                                            hn_mark);
17928 +       /* AuDbg("here\n"); */
17929 +       au_cache_free_hnotify(hn);
17930 +       smp_mb__before_atomic(); /* for atomic64_dec */
17931 +       if (atomic64_dec_and_test(&au_hfsn_ifree))
17932 +               wake_up(&au_hfsn_wq);
17933 +}
17934 +
17935 +static int au_hfsn_alloc(struct au_hinode *hinode)
17936 +{
17937 +       int err;
17938 +       struct au_hnotify *hn;
17939 +       struct super_block *sb;
17940 +       struct au_branch *br;
17941 +       struct fsnotify_mark *mark;
17942 +       aufs_bindex_t bindex;
17943 +
17944 +       hn = hinode->hi_notify;
17945 +       sb = hn->hn_aufs_inode->i_sb;
17946 +       bindex = au_br_index(sb, hinode->hi_id);
17947 +       br = au_sbr(sb, bindex);
17948 +       AuDebugOn(!br->br_hfsn);
17949 +
17950 +       mark = &hn->hn_mark;
17951 +       fsnotify_init_mark(mark, br->br_hfsn->hfsn_group);
17952 +       mark->mask = AuHfsnMask;
17953 +       /*
17954 +        * by udba rename or rmdir, aufs assign a new inode to the known
17955 +        * h_inode, so specify 1 to allow dups.
17956 +        */
17957 +       lockdep_off();
17958 +       err = fsnotify_add_inode_mark(mark, hinode->hi_inode, /*allow_dups*/1);
17959 +       lockdep_on();
17960 +
17961 +       return err;
17962 +}
17963 +
17964 +static int au_hfsn_free(struct au_hinode *hinode, struct au_hnotify *hn)
17965 +{
17966 +       struct fsnotify_mark *mark;
17967 +       unsigned long long ull;
17968 +       struct fsnotify_group *group;
17969 +
17970 +       ull = atomic64_inc_return(&au_hfsn_ifree);
17971 +       BUG_ON(!ull);
17972 +
17973 +       mark = &hn->hn_mark;
17974 +       spin_lock(&mark->lock);
17975 +       group = mark->group;
17976 +       fsnotify_get_group(group);
17977 +       spin_unlock(&mark->lock);
17978 +       lockdep_off();
17979 +       fsnotify_destroy_mark(mark, group);
17980 +       fsnotify_put_mark(mark);
17981 +       fsnotify_put_group(group);
17982 +       lockdep_on();
17983 +
17984 +       /* free hn by myself */
17985 +       return 0;
17986 +}
17987 +
17988 +/* ---------------------------------------------------------------------- */
17989 +
17990 +static void au_hfsn_ctl(struct au_hinode *hinode, int do_set)
17991 +{
17992 +       struct fsnotify_mark *mark;
17993 +
17994 +       mark = &hinode->hi_notify->hn_mark;
17995 +       spin_lock(&mark->lock);
17996 +       if (do_set) {
17997 +               AuDebugOn(mark->mask & AuHfsnMask);
17998 +               mark->mask |= AuHfsnMask;
17999 +       } else {
18000 +               AuDebugOn(!(mark->mask & AuHfsnMask));
18001 +               mark->mask &= ~AuHfsnMask;
18002 +       }
18003 +       spin_unlock(&mark->lock);
18004 +       /* fsnotify_recalc_inode_mask(hinode->hi_inode); */
18005 +}
18006 +
18007 +/* ---------------------------------------------------------------------- */
18008 +
18009 +/* #define AuDbgHnotify */
18010 +#ifdef AuDbgHnotify
18011 +static char *au_hfsn_name(u32 mask)
18012 +{
18013 +#ifdef CONFIG_AUFS_DEBUG
18014 +#define test_ret(flag)                         \
18015 +       do {                                    \
18016 +               if (mask & flag)                \
18017 +                       return #flag;           \
18018 +       } while (0)
18019 +       test_ret(FS_ACCESS);
18020 +       test_ret(FS_MODIFY);
18021 +       test_ret(FS_ATTRIB);
18022 +       test_ret(FS_CLOSE_WRITE);
18023 +       test_ret(FS_CLOSE_NOWRITE);
18024 +       test_ret(FS_OPEN);
18025 +       test_ret(FS_MOVED_FROM);
18026 +       test_ret(FS_MOVED_TO);
18027 +       test_ret(FS_CREATE);
18028 +       test_ret(FS_DELETE);
18029 +       test_ret(FS_DELETE_SELF);
18030 +       test_ret(FS_MOVE_SELF);
18031 +       test_ret(FS_UNMOUNT);
18032 +       test_ret(FS_Q_OVERFLOW);
18033 +       test_ret(FS_IN_IGNORED);
18034 +       test_ret(FS_ISDIR);
18035 +       test_ret(FS_IN_ONESHOT);
18036 +       test_ret(FS_EVENT_ON_CHILD);
18037 +       return "";
18038 +#undef test_ret
18039 +#else
18040 +       return "??";
18041 +#endif
18042 +}
18043 +#endif
18044 +
18045 +/* ---------------------------------------------------------------------- */
18046 +
18047 +static void au_hfsn_free_group(struct fsnotify_group *group)
18048 +{
18049 +       struct au_br_hfsnotify *hfsn = group->private;
18050 +
18051 +       /* AuDbg("here\n"); */
18052 +       au_kfree_try_rcu(hfsn);
18053 +}
18054 +
18055 +static int au_hfsn_handle_event(struct fsnotify_group *group,
18056 +                               u32 mask, const void *data, int data_type,
18057 +                               struct inode *dir,
18058 +                               const struct qstr *file_name, u32 cookie,
18059 +                               struct fsnotify_iter_info *iter_info)
18060 +{
18061 +       int err;
18062 +       struct au_hnotify *hnotify;
18063 +       struct inode *h_dir, *h_inode;
18064 +       struct fsnotify_mark *inode_mark;
18065 +
18066 +       AuDebugOn(!(data_type == FSNOTIFY_EVENT_INODE
18067 +                   || data_type == FSNOTIFY_EVENT_DENTRY));
18068 +
18069 +       err = 0;
18070 +       /* if FS_UNMOUNT happens, there must be another bug */
18071 +       AuDebugOn(mask & FS_UNMOUNT);
18072 +       if (mask & (FS_IN_IGNORED | FS_UNMOUNT))
18073 +               goto out;
18074 +
18075 +       h_dir = dir;
18076 +       h_inode = NULL;
18077 +#ifdef AuDbgHnotify
18078 +       au_debug_on();
18079 +       if (1 || h_child_qstr.len != sizeof(AUFS_XINO_FNAME) - 1
18080 +           || strncmp(h_child_qstr.name, AUFS_XINO_FNAME, h_child_qstr.len)) {
18081 +               AuDbg("i%lu, mask 0x%x %s, hcname %.*s, hi%lu\n",
18082 +                     h_dir->i_ino, mask, au_hfsn_name(mask),
18083 +                     AuLNPair(&h_child_qstr), h_inode ? h_inode->i_ino : 0);
18084 +               /* WARN_ON(1); */
18085 +       }
18086 +       au_debug_off();
18087 +#endif
18088 +
18089 +       inode_mark = fsnotify_iter_inode_mark(iter_info);
18090 +       AuDebugOn(!inode_mark);
18091 +       hnotify = container_of(inode_mark, struct au_hnotify, hn_mark);
18092 +       err = au_hnotify(h_dir, hnotify, mask, file_name, h_inode);
18093 +
18094 +out:
18095 +       return err;
18096 +}
18097 +
18098 +static struct fsnotify_ops au_hfsn_ops = {
18099 +       .handle_event           = au_hfsn_handle_event,
18100 +       .free_group_priv        = au_hfsn_free_group,
18101 +       .free_mark              = au_hfsn_free_mark
18102 +};
18103 +
18104 +/* ---------------------------------------------------------------------- */
18105 +
18106 +static void au_hfsn_fin_br(struct au_branch *br)
18107 +{
18108 +       struct au_br_hfsnotify *hfsn;
18109 +
18110 +       hfsn = br->br_hfsn;
18111 +       if (hfsn) {
18112 +               lockdep_off();
18113 +               fsnotify_put_group(hfsn->hfsn_group);
18114 +               lockdep_on();
18115 +       }
18116 +}
18117 +
18118 +static int au_hfsn_init_br(struct au_branch *br, int perm)
18119 +{
18120 +       int err;
18121 +       struct fsnotify_group *group;
18122 +       struct au_br_hfsnotify *hfsn;
18123 +
18124 +       err = 0;
18125 +       br->br_hfsn = NULL;
18126 +       if (!au_br_hnotifyable(perm))
18127 +               goto out;
18128 +
18129 +       err = -ENOMEM;
18130 +       hfsn = kmalloc(sizeof(*hfsn), GFP_NOFS);
18131 +       if (unlikely(!hfsn))
18132 +               goto out;
18133 +
18134 +       err = 0;
18135 +       group = fsnotify_alloc_group(&au_hfsn_ops);
18136 +       if (IS_ERR(group)) {
18137 +               err = PTR_ERR(group);
18138 +               pr_err("fsnotify_alloc_group() failed, %d\n", err);
18139 +               goto out_hfsn;
18140 +       }
18141 +
18142 +       group->private = hfsn;
18143 +       hfsn->hfsn_group = group;
18144 +       br->br_hfsn = hfsn;
18145 +       goto out; /* success */
18146 +
18147 +out_hfsn:
18148 +       au_kfree_try_rcu(hfsn);
18149 +out:
18150 +       return err;
18151 +}
18152 +
18153 +static int au_hfsn_reset_br(unsigned int udba, struct au_branch *br, int perm)
18154 +{
18155 +       int err;
18156 +
18157 +       err = 0;
18158 +       if (!br->br_hfsn)
18159 +               err = au_hfsn_init_br(br, perm);
18160 +
18161 +       return err;
18162 +}
18163 +
18164 +/* ---------------------------------------------------------------------- */
18165 +
18166 +static void au_hfsn_fin(void)
18167 +{
18168 +       AuDbg("au_hfsn_ifree %lld\n", (long long)atomic64_read(&au_hfsn_ifree));
18169 +       wait_event(au_hfsn_wq, !atomic64_read(&au_hfsn_ifree));
18170 +}
18171 +
18172 +const struct au_hnotify_op au_hnotify_op = {
18173 +       .ctl            = au_hfsn_ctl,
18174 +       .alloc          = au_hfsn_alloc,
18175 +       .free           = au_hfsn_free,
18176 +
18177 +       .fin            = au_hfsn_fin,
18178 +
18179 +       .reset_br       = au_hfsn_reset_br,
18180 +       .fin_br         = au_hfsn_fin_br,
18181 +       .init_br        = au_hfsn_init_br
18182 +};
18183 diff -urN /usr/share/empty/fs/aufs/hfsplus.c linux/fs/aufs/hfsplus.c
18184 --- /usr/share/empty/fs/aufs/hfsplus.c  1970-01-01 01:00:00.000000000 +0100
18185 +++ linux/fs/aufs/hfsplus.c     2022-03-21 14:49:05.723299677 +0100
18186 @@ -0,0 +1,60 @@
18187 +// SPDX-License-Identifier: GPL-2.0
18188 +/*
18189 + * Copyright (C) 2010-2021 Junjiro R. Okajima
18190 + *
18191 + * This program, aufs is free software; you can redistribute it and/or modify
18192 + * it under the terms of the GNU General Public License as published by
18193 + * the Free Software Foundation; either version 2 of the License, or
18194 + * (at your option) any later version.
18195 + *
18196 + * This program is distributed in the hope that it will be useful,
18197 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
18198 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18199 + * GNU General Public License for more details.
18200 + *
18201 + * You should have received a copy of the GNU General Public License
18202 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18203 + */
18204 +
18205 +/*
18206 + * special support for filesystems which acquires an inode mutex
18207 + * at final closing a file, eg, hfsplus.
18208 + *
18209 + * This trick is very simple and stupid, just to open the file before really
18210 + * necessary open to tell hfsplus that this is not the final closing.
18211 + * The caller should call au_h_open_pre() after acquiring the inode mutex,
18212 + * and au_h_open_post() after releasing it.
18213 + */
18214 +
18215 +#include "aufs.h"
18216 +
18217 +struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
18218 +                          int force_wr)
18219 +{
18220 +       struct file *h_file;
18221 +       struct dentry *h_dentry;
18222 +
18223 +       h_dentry = au_h_dptr(dentry, bindex);
18224 +       AuDebugOn(!h_dentry);
18225 +       AuDebugOn(d_is_negative(h_dentry));
18226 +
18227 +       h_file = NULL;
18228 +       if (au_test_hfsplus(h_dentry->d_sb)
18229 +           && d_is_reg(h_dentry))
18230 +               h_file = au_h_open(dentry, bindex,
18231 +                                  O_RDONLY | O_NOATIME | O_LARGEFILE,
18232 +                                  /*file*/NULL, force_wr);
18233 +       return h_file;
18234 +}
18235 +
18236 +void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
18237 +                   struct file *h_file)
18238 +{
18239 +       struct au_branch *br;
18240 +
18241 +       if (h_file) {
18242 +               fput(h_file);
18243 +               br = au_sbr(dentry->d_sb, bindex);
18244 +               au_lcnt_dec(&br->br_nfiles);
18245 +       }
18246 +}
18247 diff -urN /usr/share/empty/fs/aufs/hnotify.c linux/fs/aufs/hnotify.c
18248 --- /usr/share/empty/fs/aufs/hnotify.c  1970-01-01 01:00:00.000000000 +0100
18249 +++ linux/fs/aufs/hnotify.c     2022-03-21 14:49:05.723299677 +0100
18250 @@ -0,0 +1,715 @@
18251 +// SPDX-License-Identifier: GPL-2.0
18252 +/*
18253 + * Copyright (C) 2005-2021 Junjiro R. Okajima
18254 + *
18255 + * This program, aufs is free software; you can redistribute it and/or modify
18256 + * it under the terms of the GNU General Public License as published by
18257 + * the Free Software Foundation; either version 2 of the License, or
18258 + * (at your option) any later version.
18259 + *
18260 + * This program is distributed in the hope that it will be useful,
18261 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
18262 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18263 + * GNU General Public License for more details.
18264 + *
18265 + * You should have received a copy of the GNU General Public License
18266 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18267 + */
18268 +
18269 +/*
18270 + * abstraction to notify the direct changes on lower directories
18271 + */
18272 +
18273 +/* #include <linux/iversion.h> */
18274 +#include "aufs.h"
18275 +
18276 +int au_hn_alloc(struct au_hinode *hinode, struct inode *inode)
18277 +{
18278 +       int err;
18279 +       struct au_hnotify *hn;
18280 +
18281 +       err = -ENOMEM;
18282 +       hn = au_cache_alloc_hnotify();
18283 +       if (hn) {
18284 +               hn->hn_aufs_inode = inode;
18285 +               hinode->hi_notify = hn;
18286 +               err = au_hnotify_op.alloc(hinode);
18287 +               AuTraceErr(err);
18288 +               if (unlikely(err)) {
18289 +                       hinode->hi_notify = NULL;
18290 +                       au_cache_free_hnotify(hn);
18291 +                       /*
18292 +                        * The upper dir was removed by udba, but the same named
18293 +                        * dir left. In this case, aufs assigns a new inode
18294 +                        * number and set the monitor again.
18295 +                        * For the lower dir, the old monitor is still left.
18296 +                        */
18297 +                       if (err == -EEXIST)
18298 +                               err = 0;
18299 +               }
18300 +       }
18301 +
18302 +       AuTraceErr(err);
18303 +       return err;
18304 +}
18305 +
18306 +void au_hn_free(struct au_hinode *hinode)
18307 +{
18308 +       struct au_hnotify *hn;
18309 +
18310 +       hn = hinode->hi_notify;
18311 +       if (hn) {
18312 +               hinode->hi_notify = NULL;
18313 +               if (au_hnotify_op.free(hinode, hn))
18314 +                       au_cache_free_hnotify(hn);
18315 +       }
18316 +}
18317 +
18318 +/* ---------------------------------------------------------------------- */
18319 +
18320 +void au_hn_ctl(struct au_hinode *hinode, int do_set)
18321 +{
18322 +       if (hinode->hi_notify)
18323 +               au_hnotify_op.ctl(hinode, do_set);
18324 +}
18325 +
18326 +void au_hn_reset(struct inode *inode, unsigned int flags)
18327 +{
18328 +       aufs_bindex_t bindex, bbot;
18329 +       struct inode *hi;
18330 +       struct dentry *iwhdentry;
18331 +
18332 +       bbot = au_ibbot(inode);
18333 +       for (bindex = au_ibtop(inode); bindex <= bbot; bindex++) {
18334 +               hi = au_h_iptr(inode, bindex);
18335 +               if (!hi)
18336 +                       continue;
18337 +
18338 +               /* inode_lock_nested(hi, AuLsc_I_CHILD); */
18339 +               iwhdentry = au_hi_wh(inode, bindex);
18340 +               if (iwhdentry)
18341 +                       dget(iwhdentry);
18342 +               au_igrab(hi);
18343 +               au_set_h_iptr(inode, bindex, NULL, 0);
18344 +               au_set_h_iptr(inode, bindex, au_igrab(hi),
18345 +                             flags & ~AuHi_XINO);
18346 +               iput(hi);
18347 +               dput(iwhdentry);
18348 +               /* inode_unlock(hi); */
18349 +       }
18350 +}
18351 +
18352 +/* ---------------------------------------------------------------------- */
18353 +
18354 +static int hn_xino(struct inode *inode, struct inode *h_inode)
18355 +{
18356 +       int err;
18357 +       aufs_bindex_t bindex, bbot, bfound, btop;
18358 +       struct inode *h_i;
18359 +
18360 +       err = 0;
18361 +       if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
18362 +               pr_warn("branch root dir was changed\n");
18363 +               goto out;
18364 +       }
18365 +
18366 +       bfound = -1;
18367 +       bbot = au_ibbot(inode);
18368 +       btop = au_ibtop(inode);
18369 +#if 0 /* reserved for future use */
18370 +       if (bindex == bbot) {
18371 +               /* keep this ino in rename case */
18372 +               goto out;
18373 +       }
18374 +#endif
18375 +       for (bindex = btop; bindex <= bbot; bindex++)
18376 +               if (au_h_iptr(inode, bindex) == h_inode) {
18377 +                       bfound = bindex;
18378 +                       break;
18379 +               }
18380 +       if (bfound < 0)
18381 +               goto out;
18382 +
18383 +       for (bindex = btop; bindex <= bbot; bindex++) {
18384 +               h_i = au_h_iptr(inode, bindex);
18385 +               if (!h_i)
18386 +                       continue;
18387 +
18388 +               err = au_xino_write(inode->i_sb, bindex, h_i->i_ino, /*ino*/0);
18389 +               /* ignore this error */
18390 +               /* bad action? */
18391 +       }
18392 +
18393 +       /* children inode number will be broken */
18394 +
18395 +out:
18396 +       AuTraceErr(err);
18397 +       return err;
18398 +}
18399 +
18400 +static int hn_gen_tree(struct dentry *dentry)
18401 +{
18402 +       int err, i, j, ndentry;
18403 +       struct au_dcsub_pages dpages;
18404 +       struct au_dpage *dpage;
18405 +       struct dentry **dentries;
18406 +
18407 +       err = au_dpages_init(&dpages, GFP_NOFS);
18408 +       if (unlikely(err))
18409 +               goto out;
18410 +       err = au_dcsub_pages(&dpages, dentry, NULL, NULL);
18411 +       if (unlikely(err))
18412 +               goto out_dpages;
18413 +
18414 +       for (i = 0; i < dpages.ndpage; i++) {
18415 +               dpage = dpages.dpages + i;
18416 +               dentries = dpage->dentries;
18417 +               ndentry = dpage->ndentry;
18418 +               for (j = 0; j < ndentry; j++) {
18419 +                       struct dentry *d;
18420 +
18421 +                       d = dentries[j];
18422 +                       if (IS_ROOT(d))
18423 +                               continue;
18424 +
18425 +                       au_digen_dec(d);
18426 +                       if (d_really_is_positive(d))
18427 +                               /* todo: reset children xino?
18428 +                                  cached children only? */
18429 +                               au_iigen_dec(d_inode(d));
18430 +               }
18431 +       }
18432 +
18433 +out_dpages:
18434 +       au_dpages_free(&dpages);
18435 +out:
18436 +       return err;
18437 +}
18438 +
18439 +/*
18440 + * return 0 if processed.
18441 + */
18442 +static int hn_gen_by_inode(char *name, unsigned int nlen, struct inode *inode,
18443 +                          const unsigned int isdir)
18444 +{
18445 +       int err;
18446 +       struct dentry *d;
18447 +       struct qstr *dname;
18448 +
18449 +       err = 1;
18450 +       if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
18451 +               pr_warn("branch root dir was changed\n");
18452 +               err = 0;
18453 +               goto out;
18454 +       }
18455 +
18456 +       if (!isdir) {
18457 +               AuDebugOn(!name);
18458 +               au_iigen_dec(inode);
18459 +               spin_lock(&inode->i_lock);
18460 +               hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
18461 +                       spin_lock(&d->d_lock);
18462 +                       dname = &d->d_name;
18463 +                       if (dname->len != nlen
18464 +                           && memcmp(dname->name, name, nlen)) {
18465 +                               spin_unlock(&d->d_lock);
18466 +                               continue;
18467 +                       }
18468 +                       err = 0;
18469 +                       au_digen_dec(d);
18470 +                       spin_unlock(&d->d_lock);
18471 +                       break;
18472 +               }
18473 +               spin_unlock(&inode->i_lock);
18474 +       } else {
18475 +               au_fset_si(au_sbi(inode->i_sb), FAILED_REFRESH_DIR);
18476 +               d = d_find_any_alias(inode);
18477 +               if (!d) {
18478 +                       au_iigen_dec(inode);
18479 +                       goto out;
18480 +               }
18481 +
18482 +               spin_lock(&d->d_lock);
18483 +               dname = &d->d_name;
18484 +               if (dname->len == nlen && !memcmp(dname->name, name, nlen)) {
18485 +                       spin_unlock(&d->d_lock);
18486 +                       err = hn_gen_tree(d);
18487 +                       spin_lock(&d->d_lock);
18488 +               }
18489 +               spin_unlock(&d->d_lock);
18490 +               dput(d);
18491 +       }
18492 +
18493 +out:
18494 +       AuTraceErr(err);
18495 +       return err;
18496 +}
18497 +
18498 +static int hn_gen_by_name(struct dentry *dentry, const unsigned int isdir)
18499 +{
18500 +       int err;
18501 +
18502 +       if (IS_ROOT(dentry)) {
18503 +               pr_warn("branch root dir was changed\n");
18504 +               return 0;
18505 +       }
18506 +
18507 +       err = 0;
18508 +       if (!isdir) {
18509 +               au_digen_dec(dentry);
18510 +               if (d_really_is_positive(dentry))
18511 +                       au_iigen_dec(d_inode(dentry));
18512 +       } else {
18513 +               au_fset_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR);
18514 +               if (d_really_is_positive(dentry))
18515 +                       err = hn_gen_tree(dentry);
18516 +       }
18517 +
18518 +       AuTraceErr(err);
18519 +       return err;
18520 +}
18521 +
18522 +/* ---------------------------------------------------------------------- */
18523 +
18524 +/* hnotify job flags */
18525 +#define AuHnJob_XINO0          1
18526 +#define AuHnJob_GEN            (1 << 1)
18527 +#define AuHnJob_DIRENT         (1 << 2)
18528 +#define AuHnJob_ISDIR          (1 << 3)
18529 +#define AuHnJob_TRYXINO0       (1 << 4)
18530 +#define AuHnJob_MNTPNT         (1 << 5)
18531 +#define au_ftest_hnjob(flags, name)    ((flags) & AuHnJob_##name)
18532 +#define au_fset_hnjob(flags, name) \
18533 +       do { (flags) |= AuHnJob_##name; } while (0)
18534 +#define au_fclr_hnjob(flags, name) \
18535 +       do { (flags) &= ~AuHnJob_##name; } while (0)
18536 +
18537 +enum {
18538 +       AuHn_CHILD,
18539 +       AuHn_PARENT,
18540 +       AuHnLast
18541 +};
18542 +
18543 +struct au_hnotify_args {
18544 +       struct inode *h_dir, *dir, *h_child_inode;
18545 +       u32 mask;
18546 +       unsigned int flags[AuHnLast];
18547 +       unsigned int h_child_nlen;
18548 +       char h_child_name[];
18549 +};
18550 +
18551 +struct hn_job_args {
18552 +       unsigned int flags;
18553 +       struct inode *inode, *h_inode, *dir, *h_dir;
18554 +       struct dentry *dentry;
18555 +       char *h_name;
18556 +       int h_nlen;
18557 +};
18558 +
18559 +static int hn_job(struct hn_job_args *a)
18560 +{
18561 +       const unsigned int isdir = au_ftest_hnjob(a->flags, ISDIR);
18562 +       int e;
18563 +
18564 +       /* reset xino */
18565 +       if (au_ftest_hnjob(a->flags, XINO0) && a->inode)
18566 +               hn_xino(a->inode, a->h_inode); /* ignore this error */
18567 +
18568 +       if (au_ftest_hnjob(a->flags, TRYXINO0)
18569 +           && a->inode
18570 +           && a->h_inode) {
18571 +               inode_lock_shared_nested(a->h_inode, AuLsc_I_CHILD);
18572 +               if (!a->h_inode->i_nlink
18573 +                   && !(a->h_inode->i_state & I_LINKABLE))
18574 +                       hn_xino(a->inode, a->h_inode); /* ignore this error */
18575 +               inode_unlock_shared(a->h_inode);
18576 +       }
18577 +
18578 +       /* make the generation obsolete */
18579 +       if (au_ftest_hnjob(a->flags, GEN)) {
18580 +               e = -1;
18581 +               if (a->inode)
18582 +                       e = hn_gen_by_inode(a->h_name, a->h_nlen, a->inode,
18583 +                                             isdir);
18584 +               if (e && a->dentry)
18585 +                       hn_gen_by_name(a->dentry, isdir);
18586 +               /* ignore this error */
18587 +       }
18588 +
18589 +       /* make dir entries obsolete */
18590 +       if (au_ftest_hnjob(a->flags, DIRENT) && a->inode) {
18591 +               struct au_vdir *vdir;
18592 +
18593 +               vdir = au_ivdir(a->inode);
18594 +               if (vdir)
18595 +                       vdir->vd_jiffy = 0;
18596 +               /* IMustLock(a->inode); */
18597 +               /* inode_inc_iversion(a->inode); */
18598 +       }
18599 +
18600 +       /* can do nothing but warn */
18601 +       if (au_ftest_hnjob(a->flags, MNTPNT)
18602 +           && a->dentry
18603 +           && d_mountpoint(a->dentry))
18604 +               pr_warn("mount-point %pd is removed or renamed\n", a->dentry);
18605 +
18606 +       return 0;
18607 +}
18608 +
18609 +/* ---------------------------------------------------------------------- */
18610 +
18611 +static struct dentry *lookup_wlock_by_name(char *name, unsigned int nlen,
18612 +                                          struct inode *dir)
18613 +{
18614 +       struct dentry *dentry, *d, *parent;
18615 +       struct qstr *dname;
18616 +
18617 +       parent = d_find_any_alias(dir);
18618 +       if (!parent)
18619 +               return NULL;
18620 +
18621 +       dentry = NULL;
18622 +       spin_lock(&parent->d_lock);
18623 +       list_for_each_entry(d, &parent->d_subdirs, d_child) {
18624 +               /* AuDbg("%pd\n", d); */
18625 +               spin_lock_nested(&d->d_lock, DENTRY_D_LOCK_NESTED);
18626 +               dname = &d->d_name;
18627 +               if (dname->len != nlen || memcmp(dname->name, name, nlen))
18628 +                       goto cont_unlock;
18629 +               if (au_di(d))
18630 +                       au_digen_dec(d);
18631 +               else
18632 +                       goto cont_unlock;
18633 +               if (au_dcount(d) > 0) {
18634 +                       dentry = dget_dlock(d);
18635 +                       spin_unlock(&d->d_lock);
18636 +                       break;
18637 +               }
18638 +
18639 +cont_unlock:
18640 +               spin_unlock(&d->d_lock);
18641 +       }
18642 +       spin_unlock(&parent->d_lock);
18643 +       dput(parent);
18644 +
18645 +       if (dentry)
18646 +               di_write_lock_child(dentry);
18647 +
18648 +       return dentry;
18649 +}
18650 +
18651 +static struct inode *lookup_wlock_by_ino(struct super_block *sb,
18652 +                                        aufs_bindex_t bindex, ino_t h_ino)
18653 +{
18654 +       struct inode *inode;
18655 +       ino_t ino;
18656 +       int err;
18657 +
18658 +       inode = NULL;
18659 +       err = au_xino_read(sb, bindex, h_ino, &ino);
18660 +       if (!err && ino)
18661 +               inode = ilookup(sb, ino);
18662 +       if (!inode)
18663 +               goto out;
18664 +
18665 +       if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
18666 +               pr_warn("wrong root branch\n");
18667 +               iput(inode);
18668 +               inode = NULL;
18669 +               goto out;
18670 +       }
18671 +
18672 +       ii_write_lock_child(inode);
18673 +
18674 +out:
18675 +       return inode;
18676 +}
18677 +
18678 +static void au_hn_bh(void *_args)
18679 +{
18680 +       struct au_hnotify_args *a = _args;
18681 +       struct super_block *sb;
18682 +       aufs_bindex_t bindex, bbot, bfound;
18683 +       unsigned char xino, try_iput;
18684 +       int err;
18685 +       struct inode *inode;
18686 +       ino_t h_ino;
18687 +       struct hn_job_args args;
18688 +       struct dentry *dentry;
18689 +       struct au_sbinfo *sbinfo;
18690 +
18691 +       AuDebugOn(!_args);
18692 +       AuDebugOn(!a->h_dir);
18693 +       AuDebugOn(!a->dir);
18694 +       AuDebugOn(!a->mask);
18695 +       AuDbg("mask 0x%x, i%lu, hi%lu, hci%lu\n",
18696 +             a->mask, a->dir->i_ino, a->h_dir->i_ino,
18697 +             a->h_child_inode ? a->h_child_inode->i_ino : 0);
18698 +
18699 +       inode = NULL;
18700 +       dentry = NULL;
18701 +       /*
18702 +        * do not lock a->dir->i_mutex here
18703 +        * because of d_revalidate() may cause a deadlock.
18704 +        */
18705 +       sb = a->dir->i_sb;
18706 +       AuDebugOn(!sb);
18707 +       sbinfo = au_sbi(sb);
18708 +       AuDebugOn(!sbinfo);
18709 +       si_write_lock(sb, AuLock_NOPLMW);
18710 +
18711 +       if (au_opt_test(sbinfo->si_mntflags, DIRREN))
18712 +               switch (a->mask & FS_EVENTS_POSS_ON_CHILD) {
18713 +               case FS_MOVED_FROM:
18714 +               case FS_MOVED_TO:
18715 +                       AuWarn1("DIRREN with UDBA may not work correctly "
18716 +                               "for the direct rename(2)\n");
18717 +               }
18718 +
18719 +       ii_read_lock_parent(a->dir);
18720 +       bfound = -1;
18721 +       bbot = au_ibbot(a->dir);
18722 +       for (bindex = au_ibtop(a->dir); bindex <= bbot; bindex++)
18723 +               if (au_h_iptr(a->dir, bindex) == a->h_dir) {
18724 +                       bfound = bindex;
18725 +                       break;
18726 +               }
18727 +       ii_read_unlock(a->dir);
18728 +       if (unlikely(bfound < 0))
18729 +               goto out;
18730 +
18731 +       xino = !!au_opt_test(au_mntflags(sb), XINO);
18732 +       h_ino = 0;
18733 +       if (a->h_child_inode)
18734 +               h_ino = a->h_child_inode->i_ino;
18735 +
18736 +       if (a->h_child_nlen
18737 +           && (au_ftest_hnjob(a->flags[AuHn_CHILD], GEN)
18738 +               || au_ftest_hnjob(a->flags[AuHn_CHILD], MNTPNT)))
18739 +               dentry = lookup_wlock_by_name(a->h_child_name, a->h_child_nlen,
18740 +                                             a->dir);
18741 +       try_iput = 0;
18742 +       if (dentry && d_really_is_positive(dentry))
18743 +               inode = d_inode(dentry);
18744 +       if (xino && !inode && h_ino
18745 +           && (au_ftest_hnjob(a->flags[AuHn_CHILD], XINO0)
18746 +               || au_ftest_hnjob(a->flags[AuHn_CHILD], TRYXINO0)
18747 +               || au_ftest_hnjob(a->flags[AuHn_CHILD], GEN))) {
18748 +               inode = lookup_wlock_by_ino(sb, bfound, h_ino);
18749 +               try_iput = 1;
18750 +       }
18751 +
18752 +       args.flags = a->flags[AuHn_CHILD];
18753 +       args.dentry = dentry;
18754 +       args.inode = inode;
18755 +       args.h_inode = a->h_child_inode;
18756 +       args.dir = a->dir;
18757 +       args.h_dir = a->h_dir;
18758 +       args.h_name = a->h_child_name;
18759 +       args.h_nlen = a->h_child_nlen;
18760 +       err = hn_job(&args);
18761 +       if (dentry) {
18762 +               if (au_di(dentry))
18763 +                       di_write_unlock(dentry);
18764 +               dput(dentry);
18765 +       }
18766 +       if (inode && try_iput) {
18767 +               ii_write_unlock(inode);
18768 +               iput(inode);
18769 +       }
18770 +
18771 +       ii_write_lock_parent(a->dir);
18772 +       args.flags = a->flags[AuHn_PARENT];
18773 +       args.dentry = NULL;
18774 +       args.inode = a->dir;
18775 +       args.h_inode = a->h_dir;
18776 +       args.dir = NULL;
18777 +       args.h_dir = NULL;
18778 +       args.h_name = NULL;
18779 +       args.h_nlen = 0;
18780 +       err = hn_job(&args);
18781 +       ii_write_unlock(a->dir);
18782 +
18783 +out:
18784 +       iput(a->h_child_inode);
18785 +       iput(a->h_dir);
18786 +       iput(a->dir);
18787 +       si_write_unlock(sb);
18788 +       au_nwt_done(&sbinfo->si_nowait);
18789 +       au_kfree_rcu(a);
18790 +}
18791 +
18792 +/* ---------------------------------------------------------------------- */
18793 +
18794 +int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
18795 +              const struct qstr *h_child_qstr, struct inode *h_child_inode)
18796 +{
18797 +       int err, len;
18798 +       unsigned int flags[AuHnLast], f;
18799 +       unsigned char isdir, isroot, wh;
18800 +       struct inode *dir;
18801 +       struct au_hnotify_args *args;
18802 +       char *p, *h_child_name;
18803 +
18804 +       err = 0;
18805 +       AuDebugOn(!hnotify || !hnotify->hn_aufs_inode);
18806 +       dir = igrab(hnotify->hn_aufs_inode);
18807 +       if (!dir)
18808 +               goto out;
18809 +
18810 +       isroot = (dir->i_ino == AUFS_ROOT_INO);
18811 +       wh = 0;
18812 +       h_child_name = (void *)h_child_qstr->name;
18813 +       len = h_child_qstr->len;
18814 +       if (h_child_name) {
18815 +               if (len > AUFS_WH_PFX_LEN
18816 +                   && !memcmp(h_child_name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
18817 +                       h_child_name += AUFS_WH_PFX_LEN;
18818 +                       len -= AUFS_WH_PFX_LEN;
18819 +                       wh = 1;
18820 +               }
18821 +       }
18822 +
18823 +       isdir = 0;
18824 +       if (h_child_inode)
18825 +               isdir = !!S_ISDIR(h_child_inode->i_mode);
18826 +       flags[AuHn_PARENT] = AuHnJob_ISDIR;
18827 +       flags[AuHn_CHILD] = 0;
18828 +       if (isdir)
18829 +               flags[AuHn_CHILD] = AuHnJob_ISDIR;
18830 +       au_fset_hnjob(flags[AuHn_PARENT], DIRENT);
18831 +       au_fset_hnjob(flags[AuHn_CHILD], GEN);
18832 +       switch (mask & ALL_FSNOTIFY_DIRENT_EVENTS) {
18833 +       case FS_MOVED_FROM:
18834 +       case FS_MOVED_TO:
18835 +               au_fset_hnjob(flags[AuHn_CHILD], XINO0);
18836 +               au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
18837 +               fallthrough;
18838 +       case FS_CREATE:
18839 +               AuDebugOn(!h_child_name);
18840 +               break;
18841 +
18842 +       case FS_DELETE:
18843 +               /*
18844 +                * aufs never be able to get this child inode.
18845 +                * revalidation should be in d_revalidate()
18846 +                * by checking i_nlink, i_generation or d_unhashed().
18847 +                */
18848 +               AuDebugOn(!h_child_name);
18849 +               au_fset_hnjob(flags[AuHn_CHILD], TRYXINO0);
18850 +               au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
18851 +               break;
18852 +
18853 +       default:
18854 +               AuDebugOn(1);
18855 +       }
18856 +
18857 +       if (wh)
18858 +               h_child_inode = NULL;
18859 +
18860 +       err = -ENOMEM;
18861 +       /* iput() and kfree() will be called in au_hnotify() */
18862 +       args = kmalloc(sizeof(*args) + len + 1, GFP_NOFS);
18863 +       if (unlikely(!args)) {
18864 +               AuErr1("no memory\n");
18865 +               iput(dir);
18866 +               goto out;
18867 +       }
18868 +       args->flags[AuHn_PARENT] = flags[AuHn_PARENT];
18869 +       args->flags[AuHn_CHILD] = flags[AuHn_CHILD];
18870 +       args->mask = mask;
18871 +       args->dir = dir;
18872 +       args->h_dir = igrab(h_dir);
18873 +       if (h_child_inode)
18874 +               h_child_inode = igrab(h_child_inode); /* can be NULL */
18875 +       args->h_child_inode = h_child_inode;
18876 +       args->h_child_nlen = len;
18877 +       if (len) {
18878 +               p = (void *)args;
18879 +               p += sizeof(*args);
18880 +               memcpy(p, h_child_name, len);
18881 +               p[len] = 0;
18882 +       }
18883 +
18884 +       /* NFS fires the event for silly-renamed one from kworker */
18885 +       f = 0;
18886 +       if (!dir->i_nlink
18887 +           || (au_test_nfs(h_dir->i_sb) && (mask & FS_DELETE)))
18888 +               f = AuWkq_NEST;
18889 +       err = au_wkq_nowait(au_hn_bh, args, dir->i_sb, f);
18890 +       if (unlikely(err)) {
18891 +               pr_err("wkq %d\n", err);
18892 +               iput(args->h_child_inode);
18893 +               iput(args->h_dir);
18894 +               iput(args->dir);
18895 +               au_kfree_rcu(args);
18896 +       }
18897 +
18898 +out:
18899 +       return err;
18900 +}
18901 +
18902 +/* ---------------------------------------------------------------------- */
18903 +
18904 +int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm)
18905 +{
18906 +       int err;
18907 +
18908 +       AuDebugOn(!(udba & AuOptMask_UDBA));
18909 +
18910 +       err = 0;
18911 +       if (au_hnotify_op.reset_br)
18912 +               err = au_hnotify_op.reset_br(udba, br, perm);
18913 +
18914 +       return err;
18915 +}
18916 +
18917 +int au_hnotify_init_br(struct au_branch *br, int perm)
18918 +{
18919 +       int err;
18920 +
18921 +       err = 0;
18922 +       if (au_hnotify_op.init_br)
18923 +               err = au_hnotify_op.init_br(br, perm);
18924 +
18925 +       return err;
18926 +}
18927 +
18928 +void au_hnotify_fin_br(struct au_branch *br)
18929 +{
18930 +       if (au_hnotify_op.fin_br)
18931 +               au_hnotify_op.fin_br(br);
18932 +}
18933 +
18934 +static void au_hn_destroy_cache(void)
18935 +{
18936 +       kmem_cache_destroy(au_cache[AuCache_HNOTIFY]);
18937 +       au_cache[AuCache_HNOTIFY] = NULL;
18938 +}
18939 +
18940 +int __init au_hnotify_init(void)
18941 +{
18942 +       int err;
18943 +
18944 +       err = -ENOMEM;
18945 +       au_cache[AuCache_HNOTIFY] = AuCache(au_hnotify);
18946 +       if (au_cache[AuCache_HNOTIFY]) {
18947 +               err = 0;
18948 +               if (au_hnotify_op.init)
18949 +                       err = au_hnotify_op.init();
18950 +               if (unlikely(err))
18951 +                       au_hn_destroy_cache();
18952 +       }
18953 +       AuTraceErr(err);
18954 +       return err;
18955 +}
18956 +
18957 +void au_hnotify_fin(void)
18958 +{
18959 +       if (au_hnotify_op.fin)
18960 +               au_hnotify_op.fin();
18961 +
18962 +       /* cf. au_cache_fin() */
18963 +       if (au_cache[AuCache_HNOTIFY])
18964 +               au_hn_destroy_cache();
18965 +}
18966 diff -urN /usr/share/empty/fs/aufs/iinfo.c linux/fs/aufs/iinfo.c
18967 --- /usr/share/empty/fs/aufs/iinfo.c    1970-01-01 01:00:00.000000000 +0100
18968 +++ linux/fs/aufs/iinfo.c       2022-03-21 14:49:05.726633010 +0100
18969 @@ -0,0 +1,286 @@
18970 +// SPDX-License-Identifier: GPL-2.0
18971 +/*
18972 + * Copyright (C) 2005-2021 Junjiro R. Okajima
18973 + *
18974 + * This program, aufs is free software; you can redistribute it and/or modify
18975 + * it under the terms of the GNU General Public License as published by
18976 + * the Free Software Foundation; either version 2 of the License, or
18977 + * (at your option) any later version.
18978 + *
18979 + * This program is distributed in the hope that it will be useful,
18980 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
18981 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18982 + * GNU General Public License for more details.
18983 + *
18984 + * You should have received a copy of the GNU General Public License
18985 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18986 + */
18987 +
18988 +/*
18989 + * inode private data
18990 + */
18991 +
18992 +#include "aufs.h"
18993 +
18994 +struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex)
18995 +{
18996 +       struct inode *h_inode;
18997 +       struct au_hinode *hinode;
18998 +
18999 +       IiMustAnyLock(inode);
19000 +
19001 +       hinode = au_hinode(au_ii(inode), bindex);
19002 +       h_inode = hinode->hi_inode;
19003 +       AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
19004 +       return h_inode;
19005 +}
19006 +
19007 +/* todo: hard/soft set? */
19008 +void au_hiput(struct au_hinode *hinode)
19009 +{
19010 +       au_hn_free(hinode);
19011 +       dput(hinode->hi_whdentry);
19012 +       iput(hinode->hi_inode);
19013 +}
19014 +
19015 +unsigned int au_hi_flags(struct inode *inode, int isdir)
19016 +{
19017 +       unsigned int flags;
19018 +       const unsigned int mnt_flags = au_mntflags(inode->i_sb);
19019 +
19020 +       flags = 0;
19021 +       if (au_opt_test(mnt_flags, XINO))
19022 +               au_fset_hi(flags, XINO);
19023 +       if (isdir && au_opt_test(mnt_flags, UDBA_HNOTIFY))
19024 +               au_fset_hi(flags, HNOTIFY);
19025 +       return flags;
19026 +}
19027 +
19028 +void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
19029 +                  struct inode *h_inode, unsigned int flags)
19030 +{
19031 +       struct au_hinode *hinode;
19032 +       struct inode *hi;
19033 +       struct au_iinfo *iinfo = au_ii(inode);
19034 +
19035 +       IiMustWriteLock(inode);
19036 +
19037 +       hinode = au_hinode(iinfo, bindex);
19038 +       hi = hinode->hi_inode;
19039 +       AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
19040 +
19041 +       if (hi)
19042 +               au_hiput(hinode);
19043 +       hinode->hi_inode = h_inode;
19044 +       if (h_inode) {
19045 +               int err;
19046 +               struct super_block *sb = inode->i_sb;
19047 +               struct au_branch *br;
19048 +
19049 +               AuDebugOn(inode->i_mode
19050 +                         && (h_inode->i_mode & S_IFMT)
19051 +                         != (inode->i_mode & S_IFMT));
19052 +               if (bindex == iinfo->ii_btop)
19053 +                       au_cpup_igen(inode, h_inode);
19054 +               br = au_sbr(sb, bindex);
19055 +               hinode->hi_id = br->br_id;
19056 +               if (au_ftest_hi(flags, XINO)) {
19057 +                       err = au_xino_write(sb, bindex, h_inode->i_ino,
19058 +                                           inode->i_ino);
19059 +                       if (unlikely(err))
19060 +                               AuIOErr1("failed au_xino_write() %d\n", err);
19061 +               }
19062 +
19063 +               if (au_ftest_hi(flags, HNOTIFY)
19064 +                   && au_br_hnotifyable(br->br_perm)) {
19065 +                       err = au_hn_alloc(hinode, inode);
19066 +                       if (unlikely(err))
19067 +                               AuIOErr1("au_hn_alloc() %d\n", err);
19068 +               }
19069 +       }
19070 +}
19071 +
19072 +void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
19073 +                 struct dentry *h_wh)
19074 +{
19075 +       struct au_hinode *hinode;
19076 +
19077 +       IiMustWriteLock(inode);
19078 +
19079 +       hinode = au_hinode(au_ii(inode), bindex);
19080 +       AuDebugOn(hinode->hi_whdentry);
19081 +       hinode->hi_whdentry = h_wh;
19082 +}
19083 +
19084 +void au_update_iigen(struct inode *inode, int half)
19085 +{
19086 +       struct au_iinfo *iinfo;
19087 +       struct au_iigen *iigen;
19088 +       unsigned int sigen;
19089 +
19090 +       sigen = au_sigen(inode->i_sb);
19091 +       iinfo = au_ii(inode);
19092 +       iigen = &iinfo->ii_generation;
19093 +       spin_lock(&iigen->ig_spin);
19094 +       iigen->ig_generation = sigen;
19095 +       if (half)
19096 +               au_ig_fset(iigen->ig_flags, HALF_REFRESHED);
19097 +       else
19098 +               au_ig_fclr(iigen->ig_flags, HALF_REFRESHED);
19099 +       spin_unlock(&iigen->ig_spin);
19100 +}
19101 +
19102 +/* it may be called at remount time, too */
19103 +void au_update_ibrange(struct inode *inode, int do_put_zero)
19104 +{
19105 +       struct au_iinfo *iinfo;
19106 +       aufs_bindex_t bindex, bbot;
19107 +
19108 +       AuDebugOn(au_is_bad_inode(inode));
19109 +       IiMustWriteLock(inode);
19110 +
19111 +       iinfo = au_ii(inode);
19112 +       if (do_put_zero && iinfo->ii_btop >= 0) {
19113 +               for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot;
19114 +                    bindex++) {
19115 +                       struct inode *h_i;
19116 +
19117 +                       h_i = au_hinode(iinfo, bindex)->hi_inode;
19118 +                       if (h_i
19119 +                           && !h_i->i_nlink
19120 +                           && !(h_i->i_state & I_LINKABLE))
19121 +                               au_set_h_iptr(inode, bindex, NULL, 0);
19122 +               }
19123 +       }
19124 +
19125 +       iinfo->ii_btop = -1;
19126 +       iinfo->ii_bbot = -1;
19127 +       bbot = au_sbbot(inode->i_sb);
19128 +       for (bindex = 0; bindex <= bbot; bindex++)
19129 +               if (au_hinode(iinfo, bindex)->hi_inode) {
19130 +                       iinfo->ii_btop = bindex;
19131 +                       break;
19132 +               }
19133 +       if (iinfo->ii_btop >= 0)
19134 +               for (bindex = bbot; bindex >= iinfo->ii_btop; bindex--)
19135 +                       if (au_hinode(iinfo, bindex)->hi_inode) {
19136 +                               iinfo->ii_bbot = bindex;
19137 +                               break;
19138 +                       }
19139 +       AuDebugOn(iinfo->ii_btop > iinfo->ii_bbot);
19140 +}
19141 +
19142 +/* ---------------------------------------------------------------------- */
19143 +
19144 +void au_icntnr_init_once(void *_c)
19145 +{
19146 +       struct au_icntnr *c = _c;
19147 +       struct au_iinfo *iinfo = &c->iinfo;
19148 +
19149 +       spin_lock_init(&iinfo->ii_generation.ig_spin);
19150 +       au_rw_init(&iinfo->ii_rwsem);
19151 +       inode_init_once(&c->vfs_inode);
19152 +}
19153 +
19154 +void au_hinode_init(struct au_hinode *hinode)
19155 +{
19156 +       hinode->hi_inode = NULL;
19157 +       hinode->hi_id = -1;
19158 +       au_hn_init(hinode);
19159 +       hinode->hi_whdentry = NULL;
19160 +}
19161 +
19162 +int au_iinfo_init(struct inode *inode)
19163 +{
19164 +       struct au_iinfo *iinfo;
19165 +       struct super_block *sb;
19166 +       struct au_hinode *hi;
19167 +       int nbr, i;
19168 +
19169 +       sb = inode->i_sb;
19170 +       iinfo = &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
19171 +       nbr = au_sbbot(sb) + 1;
19172 +       if (unlikely(nbr <= 0))
19173 +               nbr = 1;
19174 +       hi = kmalloc_array(nbr, sizeof(*iinfo->ii_hinode), GFP_NOFS);
19175 +       if (hi) {
19176 +               au_lcnt_inc(&au_sbi(sb)->si_ninodes);
19177 +
19178 +               iinfo->ii_hinode = hi;
19179 +               for (i = 0; i < nbr; i++, hi++)
19180 +                       au_hinode_init(hi);
19181 +
19182 +               iinfo->ii_generation.ig_generation = au_sigen(sb);
19183 +               iinfo->ii_btop = -1;
19184 +               iinfo->ii_bbot = -1;
19185 +               iinfo->ii_vdir = NULL;
19186 +               return 0;
19187 +       }
19188 +       return -ENOMEM;
19189 +}
19190 +
19191 +int au_hinode_realloc(struct au_iinfo *iinfo, int nbr, int may_shrink)
19192 +{
19193 +       int err, i;
19194 +       struct au_hinode *hip;
19195 +
19196 +       AuRwMustWriteLock(&iinfo->ii_rwsem);
19197 +
19198 +       err = -ENOMEM;
19199 +       hip = au_krealloc(iinfo->ii_hinode, sizeof(*hip) * nbr, GFP_NOFS,
19200 +                         may_shrink);
19201 +       if (hip) {
19202 +               iinfo->ii_hinode = hip;
19203 +               i = iinfo->ii_bbot + 1;
19204 +               hip += i;
19205 +               for (; i < nbr; i++, hip++)
19206 +                       au_hinode_init(hip);
19207 +               err = 0;
19208 +       }
19209 +
19210 +       return err;
19211 +}
19212 +
19213 +void au_iinfo_fin(struct inode *inode)
19214 +{
19215 +       struct au_iinfo *iinfo;
19216 +       struct au_hinode *hi;
19217 +       struct super_block *sb;
19218 +       aufs_bindex_t bindex, bbot;
19219 +       const unsigned char unlinked = !inode->i_nlink;
19220 +
19221 +       AuDebugOn(au_is_bad_inode(inode));
19222 +
19223 +       sb = inode->i_sb;
19224 +       au_lcnt_dec(&au_sbi(sb)->si_ninodes);
19225 +       if (si_pid_test(sb))
19226 +               au_xino_delete_inode(inode, unlinked);
19227 +       else {
19228 +               /*
19229 +                * it is safe to hide the dependency between sbinfo and
19230 +                * sb->s_umount.
19231 +                */
19232 +               lockdep_off();
19233 +               si_noflush_read_lock(sb);
19234 +               au_xino_delete_inode(inode, unlinked);
19235 +               si_read_unlock(sb);
19236 +               lockdep_on();
19237 +       }
19238 +
19239 +       iinfo = au_ii(inode);
19240 +       if (iinfo->ii_vdir)
19241 +               au_vdir_free(iinfo->ii_vdir);
19242 +
19243 +       bindex = iinfo->ii_btop;
19244 +       if (bindex >= 0) {
19245 +               hi = au_hinode(iinfo, bindex);
19246 +               bbot = iinfo->ii_bbot;
19247 +               while (bindex++ <= bbot) {
19248 +                       if (hi->hi_inode)
19249 +                               au_hiput(hi);
19250 +                       hi++;
19251 +               }
19252 +       }
19253 +       au_kfree_rcu(iinfo->ii_hinode);
19254 +       AuRwDestroy(&iinfo->ii_rwsem);
19255 +}
19256 diff -urN /usr/share/empty/fs/aufs/inode.c linux/fs/aufs/inode.c
19257 --- /usr/share/empty/fs/aufs/inode.c    1970-01-01 01:00:00.000000000 +0100
19258 +++ linux/fs/aufs/inode.c       2022-03-21 14:49:05.726633010 +0100
19259 @@ -0,0 +1,531 @@
19260 +// SPDX-License-Identifier: GPL-2.0
19261 +/*
19262 + * Copyright (C) 2005-2021 Junjiro R. Okajima
19263 + *
19264 + * This program, aufs is free software; you can redistribute it and/or modify
19265 + * it under the terms of the GNU General Public License as published by
19266 + * the Free Software Foundation; either version 2 of the License, or
19267 + * (at your option) any later version.
19268 + *
19269 + * This program is distributed in the hope that it will be useful,
19270 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
19271 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19272 + * GNU General Public License for more details.
19273 + *
19274 + * You should have received a copy of the GNU General Public License
19275 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19276 + */
19277 +
19278 +/*
19279 + * inode functions
19280 + */
19281 +
19282 +#include <linux/iversion.h>
19283 +#include "aufs.h"
19284 +
19285 +struct inode *au_igrab(struct inode *inode)
19286 +{
19287 +       if (inode) {
19288 +               AuDebugOn(!atomic_read(&inode->i_count));
19289 +               ihold(inode);
19290 +       }
19291 +       return inode;
19292 +}
19293 +
19294 +static void au_refresh_hinode_attr(struct inode *inode, int do_version)
19295 +{
19296 +       au_cpup_attr_all(inode, /*force*/0);
19297 +       au_update_iigen(inode, /*half*/1);
19298 +       if (do_version)
19299 +               inode_inc_iversion(inode);
19300 +}
19301 +
19302 +static int au_ii_refresh(struct inode *inode, int *update)
19303 +{
19304 +       int err, e, nbr;
19305 +       umode_t type;
19306 +       aufs_bindex_t bindex, new_bindex;
19307 +       struct super_block *sb;
19308 +       struct au_iinfo *iinfo;
19309 +       struct au_hinode *p, *q, tmp;
19310 +
19311 +       AuDebugOn(au_is_bad_inode(inode));
19312 +       IiMustWriteLock(inode);
19313 +
19314 +       *update = 0;
19315 +       sb = inode->i_sb;
19316 +       nbr = au_sbbot(sb) + 1;
19317 +       type = inode->i_mode & S_IFMT;
19318 +       iinfo = au_ii(inode);
19319 +       err = au_hinode_realloc(iinfo, nbr, /*may_shrink*/0);
19320 +       if (unlikely(err))
19321 +               goto out;
19322 +
19323 +       AuDebugOn(iinfo->ii_btop < 0);
19324 +       p = au_hinode(iinfo, iinfo->ii_btop);
19325 +       for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot;
19326 +            bindex++, p++) {
19327 +               if (!p->hi_inode)
19328 +                       continue;
19329 +
19330 +               AuDebugOn(type != (p->hi_inode->i_mode & S_IFMT));
19331 +               new_bindex = au_br_index(sb, p->hi_id);
19332 +               if (new_bindex == bindex)
19333 +                       continue;
19334 +
19335 +               if (new_bindex < 0) {
19336 +                       *update = 1;
19337 +                       au_hiput(p);
19338 +                       p->hi_inode = NULL;
19339 +                       continue;
19340 +               }
19341 +
19342 +               if (new_bindex < iinfo->ii_btop)
19343 +                       iinfo->ii_btop = new_bindex;
19344 +               if (iinfo->ii_bbot < new_bindex)
19345 +                       iinfo->ii_bbot = new_bindex;
19346 +               /* swap two lower inode, and loop again */
19347 +               q = au_hinode(iinfo, new_bindex);
19348 +               tmp = *q;
19349 +               *q = *p;
19350 +               *p = tmp;
19351 +               if (tmp.hi_inode) {
19352 +                       bindex--;
19353 +                       p--;
19354 +               }
19355 +       }
19356 +       au_update_ibrange(inode, /*do_put_zero*/0);
19357 +       au_hinode_realloc(iinfo, nbr, /*may_shrink*/1); /* harmless if err */
19358 +       e = au_dy_irefresh(inode);
19359 +       if (unlikely(e && !err))
19360 +               err = e;
19361 +
19362 +out:
19363 +       AuTraceErr(err);
19364 +       return err;
19365 +}
19366 +
19367 +void au_refresh_iop(struct inode *inode, int force_getattr)
19368 +{
19369 +       int type;
19370 +       struct au_sbinfo *sbi = au_sbi(inode->i_sb);
19371 +       const struct inode_operations *iop
19372 +               = force_getattr ? aufs_iop : sbi->si_iop_array;
19373 +
19374 +       if (inode->i_op == iop)
19375 +               return;
19376 +
19377 +       switch (inode->i_mode & S_IFMT) {
19378 +       case S_IFDIR:
19379 +               type = AuIop_DIR;
19380 +               break;
19381 +       case S_IFLNK:
19382 +               type = AuIop_SYMLINK;
19383 +               break;
19384 +       default:
19385 +               type = AuIop_OTHER;
19386 +               break;
19387 +       }
19388 +
19389 +       inode->i_op = iop + type;
19390 +       /* unnecessary smp_wmb() */
19391 +}
19392 +
19393 +int au_refresh_hinode_self(struct inode *inode)
19394 +{
19395 +       int err, update;
19396 +
19397 +       err = au_ii_refresh(inode, &update);
19398 +       if (!err)
19399 +               au_refresh_hinode_attr(inode, update && S_ISDIR(inode->i_mode));
19400 +
19401 +       AuTraceErr(err);
19402 +       return err;
19403 +}
19404 +
19405 +int au_refresh_hinode(struct inode *inode, struct dentry *dentry)
19406 +{
19407 +       int err, e, update;
19408 +       unsigned int flags;
19409 +       umode_t mode;
19410 +       aufs_bindex_t bindex, bbot;
19411 +       unsigned char isdir;
19412 +       struct au_hinode *p;
19413 +       struct au_iinfo *iinfo;
19414 +
19415 +       err = au_ii_refresh(inode, &update);
19416 +       if (unlikely(err))
19417 +               goto out;
19418 +
19419 +       update = 0;
19420 +       iinfo = au_ii(inode);
19421 +       p = au_hinode(iinfo, iinfo->ii_btop);
19422 +       mode = (inode->i_mode & S_IFMT);
19423 +       isdir = S_ISDIR(mode);
19424 +       flags = au_hi_flags(inode, isdir);
19425 +       bbot = au_dbbot(dentry);
19426 +       for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++) {
19427 +               struct inode *h_i, *h_inode;
19428 +               struct dentry *h_d;
19429 +
19430 +               h_d = au_h_dptr(dentry, bindex);
19431 +               if (!h_d || d_is_negative(h_d))
19432 +                       continue;
19433 +
19434 +               h_inode = d_inode(h_d);
19435 +               AuDebugOn(mode != (h_inode->i_mode & S_IFMT));
19436 +               if (iinfo->ii_btop <= bindex && bindex <= iinfo->ii_bbot) {
19437 +                       h_i = au_h_iptr(inode, bindex);
19438 +                       if (h_i) {
19439 +                               if (h_i == h_inode)
19440 +                                       continue;
19441 +                               err = -EIO;
19442 +                               break;
19443 +                       }
19444 +               }
19445 +               if (bindex < iinfo->ii_btop)
19446 +                       iinfo->ii_btop = bindex;
19447 +               if (iinfo->ii_bbot < bindex)
19448 +                       iinfo->ii_bbot = bindex;
19449 +               au_set_h_iptr(inode, bindex, au_igrab(h_inode), flags);
19450 +               update = 1;
19451 +       }
19452 +       au_update_ibrange(inode, /*do_put_zero*/0);
19453 +       e = au_dy_irefresh(inode);
19454 +       if (unlikely(e && !err))
19455 +               err = e;
19456 +       if (!err)
19457 +               au_refresh_hinode_attr(inode, update && isdir);
19458 +
19459 +out:
19460 +       AuTraceErr(err);
19461 +       return err;
19462 +}
19463 +
19464 +static int set_inode(struct inode *inode, struct dentry *dentry)
19465 +{
19466 +       int err;
19467 +       unsigned int flags;
19468 +       umode_t mode;
19469 +       aufs_bindex_t bindex, btop, btail;
19470 +       unsigned char isdir;
19471 +       struct dentry *h_dentry;
19472 +       struct inode *h_inode;
19473 +       struct au_iinfo *iinfo;
19474 +       const struct inode_operations *iop;
19475 +
19476 +       IiMustWriteLock(inode);
19477 +
19478 +       err = 0;
19479 +       isdir = 0;
19480 +       iop = au_sbi(inode->i_sb)->si_iop_array;
19481 +       btop = au_dbtop(dentry);
19482 +       h_dentry = au_h_dptr(dentry, btop);
19483 +       h_inode = d_inode(h_dentry);
19484 +       mode = h_inode->i_mode;
19485 +       switch (mode & S_IFMT) {
19486 +       case S_IFREG:
19487 +               btail = au_dbtail(dentry);
19488 +               inode->i_op = iop + AuIop_OTHER;
19489 +               inode->i_fop = &aufs_file_fop;
19490 +               err = au_dy_iaop(inode, btop, h_inode);
19491 +               if (unlikely(err))
19492 +                       goto out;
19493 +               break;
19494 +       case S_IFDIR:
19495 +               isdir = 1;
19496 +               btail = au_dbtaildir(dentry);
19497 +               inode->i_op = iop + AuIop_DIR;
19498 +               inode->i_fop = &aufs_dir_fop;
19499 +               break;
19500 +       case S_IFLNK:
19501 +               btail = au_dbtail(dentry);
19502 +               inode->i_op = iop + AuIop_SYMLINK;
19503 +               break;
19504 +       case S_IFBLK:
19505 +       case S_IFCHR:
19506 +       case S_IFIFO:
19507 +       case S_IFSOCK:
19508 +               btail = au_dbtail(dentry);
19509 +               inode->i_op = iop + AuIop_OTHER;
19510 +               init_special_inode(inode, mode, h_inode->i_rdev);
19511 +               break;
19512 +       default:
19513 +               AuIOErr("Unknown file type 0%o\n", mode);
19514 +               err = -EIO;
19515 +               goto out;
19516 +       }
19517 +
19518 +       /* do not set hnotify for whiteouted dirs (SHWH mode) */
19519 +       flags = au_hi_flags(inode, isdir);
19520 +       if (au_opt_test(au_mntflags(dentry->d_sb), SHWH)
19521 +           && au_ftest_hi(flags, HNOTIFY)
19522 +           && dentry->d_name.len > AUFS_WH_PFX_LEN
19523 +           && !memcmp(dentry->d_name.name, AUFS_WH_PFX, AUFS_WH_PFX_LEN))
19524 +               au_fclr_hi(flags, HNOTIFY);
19525 +       iinfo = au_ii(inode);
19526 +       iinfo->ii_btop = btop;
19527 +       iinfo->ii_bbot = btail;
19528 +       for (bindex = btop; bindex <= btail; bindex++) {
19529 +               h_dentry = au_h_dptr(dentry, bindex);
19530 +               if (h_dentry)
19531 +                       au_set_h_iptr(inode, bindex,
19532 +                                     au_igrab(d_inode(h_dentry)), flags);
19533 +       }
19534 +       au_cpup_attr_all(inode, /*force*/1);
19535 +       /*
19536 +        * to force calling aufs_get_acl() every time,
19537 +        * do not call cache_no_acl() for aufs inode.
19538 +        */
19539 +
19540 +out:
19541 +       return err;
19542 +}
19543 +
19544 +/*
19545 + * successful returns with iinfo write_locked
19546 + * minus: errno
19547 + * zero: success, matched
19548 + * plus: no error, but unmatched
19549 + */
19550 +static int reval_inode(struct inode *inode, struct dentry *dentry)
19551 +{
19552 +       int err;
19553 +       unsigned int gen, igflags;
19554 +       aufs_bindex_t bindex, bbot;
19555 +       struct inode *h_inode, *h_dinode;
19556 +       struct dentry *h_dentry;
19557 +
19558 +       /*
19559 +        * before this function, if aufs got any iinfo lock, it must be only
19560 +        * one, the parent dir.
19561 +        * it can happen by UDBA and the obsoleted inode number.
19562 +        */
19563 +       err = -EIO;
19564 +       if (unlikely(inode->i_ino == parent_ino(dentry)))
19565 +               goto out;
19566 +
19567 +       err = 1;
19568 +       ii_write_lock_new_child(inode);
19569 +       h_dentry = au_h_dptr(dentry, au_dbtop(dentry));
19570 +       h_dinode = d_inode(h_dentry);
19571 +       bbot = au_ibbot(inode);
19572 +       for (bindex = au_ibtop(inode); bindex <= bbot; bindex++) {
19573 +               h_inode = au_h_iptr(inode, bindex);
19574 +               if (!h_inode || h_inode != h_dinode)
19575 +                       continue;
19576 +
19577 +               err = 0;
19578 +               gen = au_iigen(inode, &igflags);
19579 +               if (gen == au_digen(dentry)
19580 +                   && !au_ig_ftest(igflags, HALF_REFRESHED))
19581 +                       break;
19582 +
19583 +               /* fully refresh inode using dentry */
19584 +               err = au_refresh_hinode(inode, dentry);
19585 +               if (!err)
19586 +                       au_update_iigen(inode, /*half*/0);
19587 +               break;
19588 +       }
19589 +
19590 +       if (unlikely(err))
19591 +               ii_write_unlock(inode);
19592 +out:
19593 +       return err;
19594 +}
19595 +
19596 +int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
19597 +          unsigned int d_type, ino_t *ino)
19598 +{
19599 +       int err, idx;
19600 +       const int isnondir = d_type != DT_DIR;
19601 +
19602 +       /* prevent hardlinked inode number from race condition */
19603 +       if (isnondir) {
19604 +               err = au_xinondir_enter(sb, bindex, h_ino, &idx);
19605 +               if (unlikely(err))
19606 +                       goto out;
19607 +       }
19608 +
19609 +       err = au_xino_read(sb, bindex, h_ino, ino);
19610 +       if (unlikely(err))
19611 +               goto out_xinondir;
19612 +
19613 +       if (!*ino) {
19614 +               err = -EIO;
19615 +               *ino = au_xino_new_ino(sb);
19616 +               if (unlikely(!*ino))
19617 +                       goto out_xinondir;
19618 +               err = au_xino_write(sb, bindex, h_ino, *ino);
19619 +               if (unlikely(err))
19620 +                       goto out_xinondir;
19621 +       }
19622 +
19623 +out_xinondir:
19624 +       if (isnondir && idx >= 0)
19625 +               au_xinondir_leave(sb, bindex, h_ino, idx);
19626 +out:
19627 +       return err;
19628 +}
19629 +
19630 +/* successful returns with iinfo write_locked */
19631 +/* todo: return with unlocked? */
19632 +struct inode *au_new_inode(struct dentry *dentry, int must_new)
19633 +{
19634 +       struct inode *inode, *h_inode;
19635 +       struct dentry *h_dentry;
19636 +       struct super_block *sb;
19637 +       ino_t h_ino, ino;
19638 +       int err, idx, hlinked;
19639 +       aufs_bindex_t btop;
19640 +
19641 +       sb = dentry->d_sb;
19642 +       btop = au_dbtop(dentry);
19643 +       h_dentry = au_h_dptr(dentry, btop);
19644 +       h_inode = d_inode(h_dentry);
19645 +       h_ino = h_inode->i_ino;
19646 +       hlinked = !d_is_dir(h_dentry) && h_inode->i_nlink > 1;
19647 +
19648 +new_ino:
19649 +       /*
19650 +        * stop 'race'-ing between hardlinks under different
19651 +        * parents.
19652 +        */
19653 +       if (hlinked) {
19654 +               err = au_xinondir_enter(sb, btop, h_ino, &idx);
19655 +               inode = ERR_PTR(err);
19656 +               if (unlikely(err))
19657 +                       goto out;
19658 +       }
19659 +
19660 +       err = au_xino_read(sb, btop, h_ino, &ino);
19661 +       inode = ERR_PTR(err);
19662 +       if (unlikely(err))
19663 +               goto out_xinondir;
19664 +
19665 +       if (!ino) {
19666 +               ino = au_xino_new_ino(sb);
19667 +               if (unlikely(!ino)) {
19668 +                       inode = ERR_PTR(-EIO);
19669 +                       goto out_xinondir;
19670 +               }
19671 +       }
19672 +
19673 +       AuDbg("i%lu\n", (unsigned long)ino);
19674 +       inode = au_iget_locked(sb, ino);
19675 +       err = PTR_ERR(inode);
19676 +       if (IS_ERR(inode))
19677 +               goto out_xinondir;
19678 +
19679 +       AuDbg("%lx, new %d\n", inode->i_state, !!(inode->i_state & I_NEW));
19680 +       if (inode->i_state & I_NEW) {
19681 +               ii_write_lock_new_child(inode);
19682 +               err = set_inode(inode, dentry);
19683 +               if (!err) {
19684 +                       unlock_new_inode(inode);
19685 +                       goto out_xinondir; /* success */
19686 +               }
19687 +
19688 +               /*
19689 +                * iget_failed() calls iput(), but we need to call
19690 +                * ii_write_unlock() after iget_failed(). so dirty hack for
19691 +                * i_count.
19692 +                */
19693 +               atomic_inc(&inode->i_count);
19694 +               iget_failed(inode);
19695 +               ii_write_unlock(inode);
19696 +               au_xino_write(sb, btop, h_ino, /*ino*/0);
19697 +               /* ignore this error */
19698 +               goto out_iput;
19699 +       } else if (!must_new && !IS_DEADDIR(inode) && inode->i_nlink) {
19700 +               /*
19701 +                * horrible race condition between lookup, readdir and copyup
19702 +                * (or something).
19703 +                */
19704 +               if (hlinked && idx >= 0)
19705 +                       au_xinondir_leave(sb, btop, h_ino, idx);
19706 +               err = reval_inode(inode, dentry);
19707 +               if (unlikely(err < 0)) {
19708 +                       hlinked = 0;
19709 +                       goto out_iput;
19710 +               }
19711 +               if (!err)
19712 +                       goto out; /* success */
19713 +               else if (hlinked && idx >= 0) {
19714 +                       err = au_xinondir_enter(sb, btop, h_ino, &idx);
19715 +                       if (unlikely(err)) {
19716 +                               iput(inode);
19717 +                               inode = ERR_PTR(err);
19718 +                               goto out;
19719 +                       }
19720 +               }
19721 +       }
19722 +
19723 +       if (unlikely(au_test_fs_unique_ino(h_inode)))
19724 +               AuWarn1("Warning: Un-notified UDBA or repeatedly renamed dir,"
19725 +                       " b%d, %s, %pd, hi%lu, i%lu.\n",
19726 +                       btop, au_sbtype(h_dentry->d_sb), dentry,
19727 +                       (unsigned long)h_ino, (unsigned long)ino);
19728 +       ino = 0;
19729 +       err = au_xino_write(sb, btop, h_ino, /*ino*/0);
19730 +       if (!err) {
19731 +               iput(inode);
19732 +               if (hlinked && idx >= 0)
19733 +                       au_xinondir_leave(sb, btop, h_ino, idx);
19734 +               goto new_ino;
19735 +       }
19736 +
19737 +out_iput:
19738 +       iput(inode);
19739 +       inode = ERR_PTR(err);
19740 +out_xinondir:
19741 +       if (hlinked && idx >= 0)
19742 +               au_xinondir_leave(sb, btop, h_ino, idx);
19743 +out:
19744 +       return inode;
19745 +}
19746 +
19747 +/* ---------------------------------------------------------------------- */
19748 +
19749 +int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
19750 +              struct inode *inode)
19751 +{
19752 +       int err;
19753 +       struct inode *hi;
19754 +
19755 +       err = au_br_rdonly(au_sbr(sb, bindex));
19756 +
19757 +       /* pseudo-link after flushed may happen out of bounds */
19758 +       if (!err
19759 +           && inode
19760 +           && au_ibtop(inode) <= bindex
19761 +           && bindex <= au_ibbot(inode)) {
19762 +               /*
19763 +                * permission check is unnecessary since vfsub routine
19764 +                * will be called later
19765 +                */
19766 +               hi = au_h_iptr(inode, bindex);
19767 +               if (hi)
19768 +                       err = IS_IMMUTABLE(hi) ? -EROFS : 0;
19769 +       }
19770 +
19771 +       return err;
19772 +}
19773 +
19774 +int au_test_h_perm(struct user_namespace *h_userns, struct inode *h_inode,
19775 +                  int mask)
19776 +{
19777 +       if (uid_eq(current_fsuid(), GLOBAL_ROOT_UID))
19778 +               return 0;
19779 +       return inode_permission(h_userns, h_inode, mask);
19780 +}
19781 +
19782 +int au_test_h_perm_sio(struct user_namespace *h_userns, struct inode *h_inode,
19783 +                      int mask)
19784 +{
19785 +       if (au_test_nfs(h_inode->i_sb)
19786 +           && (mask & MAY_WRITE)
19787 +           && S_ISDIR(h_inode->i_mode))
19788 +               mask |= MAY_READ; /* force permission check */
19789 +       return au_test_h_perm(h_userns, h_inode, mask);
19790 +}
19791 diff -urN /usr/share/empty/fs/aufs/inode.h linux/fs/aufs/inode.h
19792 --- /usr/share/empty/fs/aufs/inode.h    1970-01-01 01:00:00.000000000 +0100
19793 +++ linux/fs/aufs/inode.h       2022-03-21 14:49:05.726633010 +0100
19794 @@ -0,0 +1,705 @@
19795 +/* SPDX-License-Identifier: GPL-2.0 */
19796 +/*
19797 + * Copyright (C) 2005-2021 Junjiro R. Okajima
19798 + *
19799 + * This program, aufs is free software; you can redistribute it and/or modify
19800 + * it under the terms of the GNU General Public License as published by
19801 + * the Free Software Foundation; either version 2 of the License, or
19802 + * (at your option) any later version.
19803 + *
19804 + * This program is distributed in the hope that it will be useful,
19805 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
19806 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19807 + * GNU General Public License for more details.
19808 + *
19809 + * You should have received a copy of the GNU General Public License
19810 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19811 + */
19812 +
19813 +/*
19814 + * inode operations
19815 + */
19816 +
19817 +#ifndef __AUFS_INODE_H__
19818 +#define __AUFS_INODE_H__
19819 +
19820 +#ifdef __KERNEL__
19821 +
19822 +#include <linux/fsnotify.h>
19823 +#include "rwsem.h"
19824 +
19825 +struct vfsmount;
19826 +
19827 +struct au_hnotify {
19828 +#ifdef CONFIG_AUFS_HNOTIFY
19829 +#ifdef CONFIG_AUFS_HFSNOTIFY
19830 +       /* never use fsnotify_add_vfsmount_mark() */
19831 +       struct fsnotify_mark            hn_mark;
19832 +#endif
19833 +       struct inode            *hn_aufs_inode; /* no get/put */
19834 +       struct rcu_head         rcu;
19835 +#endif
19836 +} ____cacheline_aligned_in_smp;
19837 +
19838 +struct au_hinode {
19839 +       struct inode            *hi_inode;
19840 +       aufs_bindex_t           hi_id;
19841 +#ifdef CONFIG_AUFS_HNOTIFY
19842 +       struct au_hnotify       *hi_notify;
19843 +#endif
19844 +
19845 +       /* reference to the copied-up whiteout with get/put */
19846 +       struct dentry           *hi_whdentry;
19847 +};
19848 +
19849 +/* ig_flags */
19850 +#define AuIG_HALF_REFRESHED            1
19851 +#define au_ig_ftest(flags, name)       ((flags) & AuIG_##name)
19852 +#define au_ig_fset(flags, name) \
19853 +       do { (flags) |= AuIG_##name; } while (0)
19854 +#define au_ig_fclr(flags, name) \
19855 +       do { (flags) &= ~AuIG_##name; } while (0)
19856 +
19857 +struct au_iigen {
19858 +       spinlock_t      ig_spin;
19859 +       __u32           ig_generation, ig_flags;
19860 +};
19861 +
19862 +struct au_vdir;
19863 +struct au_iinfo {
19864 +       struct au_iigen         ii_generation;
19865 +       struct super_block      *ii_hsb1;       /* no get/put */
19866 +
19867 +       struct au_rwsem         ii_rwsem;
19868 +       aufs_bindex_t           ii_btop, ii_bbot;
19869 +       __u32                   ii_higen;
19870 +       struct au_hinode        *ii_hinode;
19871 +       struct au_vdir          *ii_vdir;
19872 +};
19873 +
19874 +struct au_icntnr {
19875 +       struct au_iinfo         iinfo;
19876 +       struct inode            vfs_inode;
19877 +       struct hlist_bl_node    plink;
19878 +       struct rcu_head         rcu;
19879 +} ____cacheline_aligned_in_smp;
19880 +
19881 +/* au_pin flags */
19882 +#define AuPin_DI_LOCKED                1
19883 +#define AuPin_MNT_WRITE                (1 << 1)
19884 +#define au_ftest_pin(flags, name)      ((flags) & AuPin_##name)
19885 +#define au_fset_pin(flags, name) \
19886 +       do { (flags) |= AuPin_##name; } while (0)
19887 +#define au_fclr_pin(flags, name) \
19888 +       do { (flags) &= ~AuPin_##name; } while (0)
19889 +
19890 +struct au_pin {
19891 +       /* input */
19892 +       struct dentry *dentry;
19893 +       unsigned int udba;
19894 +       unsigned char lsc_di, lsc_hi, flags;
19895 +       aufs_bindex_t bindex;
19896 +
19897 +       /* output */
19898 +       struct dentry *parent;
19899 +       struct au_hinode *hdir;
19900 +       struct vfsmount *h_mnt;
19901 +
19902 +       /* temporary unlock/relock for copyup */
19903 +       struct dentry *h_dentry, *h_parent;
19904 +       struct au_branch *br;
19905 +       struct task_struct *task;
19906 +};
19907 +
19908 +void au_pin_hdir_unlock(struct au_pin *p);
19909 +int au_pin_hdir_lock(struct au_pin *p);
19910 +int au_pin_hdir_relock(struct au_pin *p);
19911 +void au_pin_hdir_acquire_nest(struct au_pin *p);
19912 +void au_pin_hdir_release(struct au_pin *p);
19913 +
19914 +/* ---------------------------------------------------------------------- */
19915 +
19916 +static inline struct au_iinfo *au_ii(struct inode *inode)
19917 +{
19918 +       BUG_ON(is_bad_inode(inode));
19919 +       return &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
19920 +}
19921 +
19922 +/* ---------------------------------------------------------------------- */
19923 +
19924 +/* inode.c */
19925 +struct inode *au_igrab(struct inode *inode);
19926 +void au_refresh_iop(struct inode *inode, int force_getattr);
19927 +int au_refresh_hinode_self(struct inode *inode);
19928 +int au_refresh_hinode(struct inode *inode, struct dentry *dentry);
19929 +int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
19930 +          unsigned int d_type, ino_t *ino);
19931 +struct inode *au_new_inode(struct dentry *dentry, int must_new);
19932 +int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
19933 +              struct inode *inode);
19934 +int au_test_h_perm(struct user_namespace *h_userns, struct inode *h_inode,
19935 +                  int mask);
19936 +int au_test_h_perm_sio(struct user_namespace *h_userns, struct inode *h_inode,
19937 +                      int mask);
19938 +
19939 +static inline int au_wh_ino(struct super_block *sb, aufs_bindex_t bindex,
19940 +                           ino_t h_ino, unsigned int d_type, ino_t *ino)
19941 +{
19942 +#ifdef CONFIG_AUFS_SHWH
19943 +       return au_ino(sb, bindex, h_ino, d_type, ino);
19944 +#else
19945 +       return 0;
19946 +#endif
19947 +}
19948 +
19949 +/* i_op.c */
19950 +enum {
19951 +       AuIop_SYMLINK,
19952 +       AuIop_DIR,
19953 +       AuIop_OTHER,
19954 +       AuIop_Last
19955 +};
19956 +extern struct inode_operations aufs_iop[AuIop_Last], /* not const */
19957 +       aufs_iop_nogetattr[AuIop_Last];
19958 +
19959 +/* au_wr_dir flags */
19960 +#define AuWrDir_ADD_ENTRY      1
19961 +#define AuWrDir_ISDIR          (1 << 1)
19962 +#define AuWrDir_TMPFILE                (1 << 2)
19963 +#define au_ftest_wrdir(flags, name)    ((flags) & AuWrDir_##name)
19964 +#define au_fset_wrdir(flags, name) \
19965 +       do { (flags) |= AuWrDir_##name; } while (0)
19966 +#define au_fclr_wrdir(flags, name) \
19967 +       do { (flags) &= ~AuWrDir_##name; } while (0)
19968 +
19969 +struct au_wr_dir_args {
19970 +       aufs_bindex_t force_btgt;
19971 +       unsigned char flags;
19972 +};
19973 +int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
19974 +             struct au_wr_dir_args *args);
19975 +
19976 +struct dentry *au_pinned_h_parent(struct au_pin *pin);
19977 +void au_pin_init(struct au_pin *pin, struct dentry *dentry,
19978 +                aufs_bindex_t bindex, int lsc_di, int lsc_hi,
19979 +                unsigned int udba, unsigned char flags);
19980 +int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
19981 +          unsigned int udba, unsigned char flags) __must_check;
19982 +int au_do_pin(struct au_pin *pin) __must_check;
19983 +void au_unpin(struct au_pin *pin);
19984 +int au_reval_for_attr(struct dentry *dentry, unsigned int sigen);
19985 +
19986 +#define AuIcpup_DID_CPUP       1
19987 +#define au_ftest_icpup(flags, name)    ((flags) & AuIcpup_##name)
19988 +#define au_fset_icpup(flags, name) \
19989 +       do { (flags) |= AuIcpup_##name; } while (0)
19990 +#define au_fclr_icpup(flags, name) \
19991 +       do { (flags) &= ~AuIcpup_##name; } while (0)
19992 +
19993 +struct au_icpup_args {
19994 +       unsigned char flags;
19995 +       unsigned char pin_flags;
19996 +       aufs_bindex_t btgt;
19997 +       unsigned int udba;
19998 +       struct au_pin pin;
19999 +       struct path h_path;
20000 +       struct inode *h_inode;
20001 +};
20002 +
20003 +int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
20004 +                    struct au_icpup_args *a);
20005 +
20006 +int au_h_path_getattr(struct dentry *dentry, struct inode *inode, int force,
20007 +                     struct path *h_path, int locked);
20008 +
20009 +/* i_op_add.c */
20010 +int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
20011 +              struct dentry *h_parent, int isdir);
20012 +int aufs_mknod(struct user_namespace *userns, struct inode *dir,
20013 +              struct dentry *dentry, umode_t mode, dev_t dev);
20014 +int aufs_symlink(struct user_namespace *userns, struct inode *dir,
20015 +                struct dentry *dentry, const char *symname);
20016 +int aufs_create(struct user_namespace *userns, struct inode *dir,
20017 +               struct dentry *dentry, umode_t mode, bool want_excl);
20018 +struct vfsub_aopen_args;
20019 +int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
20020 +                      struct vfsub_aopen_args *args);
20021 +int aufs_tmpfile(struct user_namespace *userns, struct inode *dir,
20022 +                struct dentry *dentry, umode_t mode);
20023 +int aufs_link(struct dentry *src_dentry, struct inode *dir,
20024 +             struct dentry *dentry);
20025 +int aufs_mkdir(struct user_namespace *userns, struct inode *dir,
20026 +              struct dentry *dentry, umode_t mode);
20027 +
20028 +/* i_op_del.c */
20029 +int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup);
20030 +int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
20031 +              struct dentry *h_parent, int isdir);
20032 +int aufs_unlink(struct inode *dir, struct dentry *dentry);
20033 +int aufs_rmdir(struct inode *dir, struct dentry *dentry);
20034 +
20035 +/* i_op_ren.c */
20036 +int au_wbr(struct dentry *dentry, aufs_bindex_t btgt);
20037 +int aufs_rename(struct user_namespace *userns,
20038 +               struct inode *_src_dir, struct dentry *_src_dentry,
20039 +               struct inode *_dst_dir, struct dentry *_dst_dentry,
20040 +               unsigned int _flags);
20041 +
20042 +/* iinfo.c */
20043 +struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex);
20044 +void au_hiput(struct au_hinode *hinode);
20045 +void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
20046 +                 struct dentry *h_wh);
20047 +unsigned int au_hi_flags(struct inode *inode, int isdir);
20048 +
20049 +/* hinode flags */
20050 +#define AuHi_XINO      1
20051 +#define AuHi_HNOTIFY   (1 << 1)
20052 +#define au_ftest_hi(flags, name)       ((flags) & AuHi_##name)
20053 +#define au_fset_hi(flags, name) \
20054 +       do { (flags) |= AuHi_##name; } while (0)
20055 +#define au_fclr_hi(flags, name) \
20056 +       do { (flags) &= ~AuHi_##name; } while (0)
20057 +
20058 +#ifndef CONFIG_AUFS_HNOTIFY
20059 +#undef AuHi_HNOTIFY
20060 +#define AuHi_HNOTIFY   0
20061 +#endif
20062 +
20063 +void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
20064 +                  struct inode *h_inode, unsigned int flags);
20065 +
20066 +void au_update_iigen(struct inode *inode, int half);
20067 +void au_update_ibrange(struct inode *inode, int do_put_zero);
20068 +
20069 +void au_icntnr_init_once(void *_c);
20070 +void au_hinode_init(struct au_hinode *hinode);
20071 +int au_iinfo_init(struct inode *inode);
20072 +void au_iinfo_fin(struct inode *inode);
20073 +int au_hinode_realloc(struct au_iinfo *iinfo, int nbr, int may_shrink);
20074 +
20075 +#ifdef CONFIG_PROC_FS
20076 +/* plink.c */
20077 +int au_plink_maint(struct super_block *sb, int flags);
20078 +struct au_sbinfo;
20079 +void au_plink_maint_leave(struct au_sbinfo *sbinfo);
20080 +int au_plink_maint_enter(struct super_block *sb);
20081 +#ifdef CONFIG_AUFS_DEBUG
20082 +void au_plink_list(struct super_block *sb);
20083 +#else
20084 +AuStubVoid(au_plink_list, struct super_block *sb)
20085 +#endif
20086 +int au_plink_test(struct inode *inode);
20087 +struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex);
20088 +void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
20089 +                    struct dentry *h_dentry);
20090 +void au_plink_put(struct super_block *sb, int verbose);
20091 +void au_plink_clean(struct super_block *sb, int verbose);
20092 +void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id);
20093 +#else
20094 +AuStubInt0(au_plink_maint, struct super_block *sb, int flags);
20095 +AuStubVoid(au_plink_maint_leave, struct au_sbinfo *sbinfo);
20096 +AuStubInt0(au_plink_maint_enter, struct super_block *sb);
20097 +AuStubVoid(au_plink_list, struct super_block *sb);
20098 +AuStubInt0(au_plink_test, struct inode *inode);
20099 +AuStub(struct dentry *, au_plink_lkup, return NULL,
20100 +       struct inode *inode, aufs_bindex_t bindex);
20101 +AuStubVoid(au_plink_append, struct inode *inode, aufs_bindex_t bindex,
20102 +          struct dentry *h_dentry);
20103 +AuStubVoid(au_plink_put, struct super_block *sb, int verbose);
20104 +AuStubVoid(au_plink_clean, struct super_block *sb, int verbose);
20105 +AuStubVoid(au_plink_half_refresh, struct super_block *sb, aufs_bindex_t br_id);
20106 +#endif /* CONFIG_PROC_FS */
20107 +
20108 +#ifdef CONFIG_AUFS_XATTR
20109 +/* xattr.c */
20110 +int au_cpup_xattr(struct path *h_dst, struct path *h_src, int ignore_flags,
20111 +                 unsigned int verbose);
20112 +ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size);
20113 +void au_xattr_init(struct super_block *sb);
20114 +#else
20115 +AuStubInt0(au_cpup_xattr, struct path *h_dst, struct path *h_src,
20116 +          int ignore_flags, unsigned int verbose);
20117 +AuStubVoid(au_xattr_init, struct super_block *sb);
20118 +#endif
20119 +
20120 +#ifdef CONFIG_FS_POSIX_ACL
20121 +struct posix_acl *aufs_get_acl(struct inode *inode, int type, bool rcu);
20122 +int aufs_set_acl(struct user_namespace *userns, struct inode *inode,
20123 +                struct posix_acl *acl, int type);
20124 +#endif
20125 +
20126 +#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
20127 +enum {
20128 +       AU_XATTR_SET,
20129 +       AU_ACL_SET
20130 +};
20131 +
20132 +struct au_sxattr {
20133 +       int type;
20134 +       union {
20135 +               struct {
20136 +                       const char      *name;
20137 +                       const void      *value;
20138 +                       size_t          size;
20139 +                       int             flags;
20140 +               } set;
20141 +               struct {
20142 +                       struct posix_acl *acl;
20143 +                       int             type;
20144 +               } acl_set;
20145 +       } u;
20146 +};
20147 +ssize_t au_sxattr(struct dentry *dentry, struct inode *inode,
20148 +                 struct au_sxattr *arg);
20149 +#endif
20150 +
20151 +/* ---------------------------------------------------------------------- */
20152 +
20153 +/* lock subclass for iinfo */
20154 +enum {
20155 +       AuLsc_II_CHILD,         /* child first */
20156 +       AuLsc_II_CHILD2,        /* rename(2), link(2), and cpup at hnotify */
20157 +       AuLsc_II_CHILD3,        /* copyup dirs */
20158 +       AuLsc_II_PARENT,        /* see AuLsc_I_PARENT in vfsub.h */
20159 +       AuLsc_II_PARENT2,
20160 +       AuLsc_II_PARENT3,       /* copyup dirs */
20161 +       AuLsc_II_NEW_CHILD
20162 +};
20163 +
20164 +/*
20165 + * ii_read_lock_child, ii_write_lock_child,
20166 + * ii_read_lock_child2, ii_write_lock_child2,
20167 + * ii_read_lock_child3, ii_write_lock_child3,
20168 + * ii_read_lock_parent, ii_write_lock_parent,
20169 + * ii_read_lock_parent2, ii_write_lock_parent2,
20170 + * ii_read_lock_parent3, ii_write_lock_parent3,
20171 + * ii_read_lock_new_child, ii_write_lock_new_child,
20172 + */
20173 +#define AuReadLockFunc(name, lsc) \
20174 +static inline void ii_read_lock_##name(struct inode *i) \
20175 +{ \
20176 +       au_rw_read_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
20177 +}
20178 +
20179 +#define AuWriteLockFunc(name, lsc) \
20180 +static inline void ii_write_lock_##name(struct inode *i) \
20181 +{ \
20182 +       au_rw_write_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
20183 +}
20184 +
20185 +#define AuRWLockFuncs(name, lsc) \
20186 +       AuReadLockFunc(name, lsc) \
20187 +       AuWriteLockFunc(name, lsc)
20188 +
20189 +AuRWLockFuncs(child, CHILD);
20190 +AuRWLockFuncs(child2, CHILD2);
20191 +AuRWLockFuncs(child3, CHILD3);
20192 +AuRWLockFuncs(parent, PARENT);
20193 +AuRWLockFuncs(parent2, PARENT2);
20194 +AuRWLockFuncs(parent3, PARENT3);
20195 +AuRWLockFuncs(new_child, NEW_CHILD);
20196 +
20197 +#undef AuReadLockFunc
20198 +#undef AuWriteLockFunc
20199 +#undef AuRWLockFuncs
20200 +
20201 +#define ii_read_unlock(i)      au_rw_read_unlock(&au_ii(i)->ii_rwsem)
20202 +#define ii_write_unlock(i)     au_rw_write_unlock(&au_ii(i)->ii_rwsem)
20203 +#define ii_downgrade_lock(i)   au_rw_dgrade_lock(&au_ii(i)->ii_rwsem)
20204 +
20205 +#define IiMustNoWaiters(i)     AuRwMustNoWaiters(&au_ii(i)->ii_rwsem)
20206 +#define IiMustAnyLock(i)       AuRwMustAnyLock(&au_ii(i)->ii_rwsem)
20207 +#define IiMustWriteLock(i)     AuRwMustWriteLock(&au_ii(i)->ii_rwsem)
20208 +
20209 +/* ---------------------------------------------------------------------- */
20210 +
20211 +static inline void au_icntnr_init(struct au_icntnr *c)
20212 +{
20213 +#ifdef CONFIG_AUFS_DEBUG
20214 +       c->vfs_inode.i_mode = 0;
20215 +#endif
20216 +}
20217 +
20218 +static inline unsigned int au_iigen(struct inode *inode, unsigned int *igflags)
20219 +{
20220 +       unsigned int gen;
20221 +       struct au_iinfo *iinfo;
20222 +       struct au_iigen *iigen;
20223 +
20224 +       iinfo = au_ii(inode);
20225 +       iigen = &iinfo->ii_generation;
20226 +       spin_lock(&iigen->ig_spin);
20227 +       if (igflags)
20228 +               *igflags = iigen->ig_flags;
20229 +       gen = iigen->ig_generation;
20230 +       spin_unlock(&iigen->ig_spin);
20231 +
20232 +       return gen;
20233 +}
20234 +
20235 +/* tiny test for inode number */
20236 +/* tmpfs generation is too rough */
20237 +static inline int au_test_higen(struct inode *inode, struct inode *h_inode)
20238 +{
20239 +       struct au_iinfo *iinfo;
20240 +
20241 +       iinfo = au_ii(inode);
20242 +       AuRwMustAnyLock(&iinfo->ii_rwsem);
20243 +       return !(iinfo->ii_hsb1 == h_inode->i_sb
20244 +                && iinfo->ii_higen == h_inode->i_generation);
20245 +}
20246 +
20247 +static inline void au_iigen_dec(struct inode *inode)
20248 +{
20249 +       struct au_iinfo *iinfo;
20250 +       struct au_iigen *iigen;
20251 +
20252 +       iinfo = au_ii(inode);
20253 +       iigen = &iinfo->ii_generation;
20254 +       spin_lock(&iigen->ig_spin);
20255 +       iigen->ig_generation--;
20256 +       spin_unlock(&iigen->ig_spin);
20257 +}
20258 +
20259 +static inline int au_iigen_test(struct inode *inode, unsigned int sigen)
20260 +{
20261 +       int err;
20262 +
20263 +       err = 0;
20264 +       if (unlikely(inode && au_iigen(inode, NULL) != sigen))
20265 +               err = -EIO;
20266 +
20267 +       return err;
20268 +}
20269 +
20270 +/* ---------------------------------------------------------------------- */
20271 +
20272 +static inline struct au_hinode *au_hinode(struct au_iinfo *iinfo,
20273 +                                         aufs_bindex_t bindex)
20274 +{
20275 +       return iinfo->ii_hinode + bindex;
20276 +}
20277 +
20278 +static inline int au_is_bad_inode(struct inode *inode)
20279 +{
20280 +       return !!(is_bad_inode(inode) || !au_hinode(au_ii(inode), 0));
20281 +}
20282 +
20283 +static inline aufs_bindex_t au_ii_br_id(struct inode *inode,
20284 +                                       aufs_bindex_t bindex)
20285 +{
20286 +       IiMustAnyLock(inode);
20287 +       return au_hinode(au_ii(inode), bindex)->hi_id;
20288 +}
20289 +
20290 +static inline aufs_bindex_t au_ibtop(struct inode *inode)
20291 +{
20292 +       IiMustAnyLock(inode);
20293 +       return au_ii(inode)->ii_btop;
20294 +}
20295 +
20296 +static inline aufs_bindex_t au_ibbot(struct inode *inode)
20297 +{
20298 +       IiMustAnyLock(inode);
20299 +       return au_ii(inode)->ii_bbot;
20300 +}
20301 +
20302 +static inline struct au_vdir *au_ivdir(struct inode *inode)
20303 +{
20304 +       IiMustAnyLock(inode);
20305 +       return au_ii(inode)->ii_vdir;
20306 +}
20307 +
20308 +static inline struct dentry *au_hi_wh(struct inode *inode, aufs_bindex_t bindex)
20309 +{
20310 +       IiMustAnyLock(inode);
20311 +       return au_hinode(au_ii(inode), bindex)->hi_whdentry;
20312 +}
20313 +
20314 +static inline void au_set_ibtop(struct inode *inode, aufs_bindex_t bindex)
20315 +{
20316 +       IiMustWriteLock(inode);
20317 +       au_ii(inode)->ii_btop = bindex;
20318 +}
20319 +
20320 +static inline void au_set_ibbot(struct inode *inode, aufs_bindex_t bindex)
20321 +{
20322 +       IiMustWriteLock(inode);
20323 +       au_ii(inode)->ii_bbot = bindex;
20324 +}
20325 +
20326 +static inline void au_set_ivdir(struct inode *inode, struct au_vdir *vdir)
20327 +{
20328 +       IiMustWriteLock(inode);
20329 +       au_ii(inode)->ii_vdir = vdir;
20330 +}
20331 +
20332 +static inline struct au_hinode *au_hi(struct inode *inode, aufs_bindex_t bindex)
20333 +{
20334 +       IiMustAnyLock(inode);
20335 +       return au_hinode(au_ii(inode), bindex);
20336 +}
20337 +
20338 +/* ---------------------------------------------------------------------- */
20339 +
20340 +static inline struct dentry *au_pinned_parent(struct au_pin *pin)
20341 +{
20342 +       if (pin)
20343 +               return pin->parent;
20344 +       return NULL;
20345 +}
20346 +
20347 +static inline struct inode *au_pinned_h_dir(struct au_pin *pin)
20348 +{
20349 +       if (pin && pin->hdir)
20350 +               return pin->hdir->hi_inode;
20351 +       return NULL;
20352 +}
20353 +
20354 +static inline struct au_hinode *au_pinned_hdir(struct au_pin *pin)
20355 +{
20356 +       if (pin)
20357 +               return pin->hdir;
20358 +       return NULL;
20359 +}
20360 +
20361 +static inline void au_pin_set_dentry(struct au_pin *pin, struct dentry *dentry)
20362 +{
20363 +       if (pin)
20364 +               pin->dentry = dentry;
20365 +}
20366 +
20367 +static inline void au_pin_set_parent_lflag(struct au_pin *pin,
20368 +                                          unsigned char lflag)
20369 +{
20370 +       if (pin) {
20371 +               if (lflag)
20372 +                       au_fset_pin(pin->flags, DI_LOCKED);
20373 +               else
20374 +                       au_fclr_pin(pin->flags, DI_LOCKED);
20375 +       }
20376 +}
20377 +
20378 +#if 0 /* reserved */
20379 +static inline void au_pin_set_parent(struct au_pin *pin, struct dentry *parent)
20380 +{
20381 +       if (pin) {
20382 +               dput(pin->parent);
20383 +               pin->parent = dget(parent);
20384 +       }
20385 +}
20386 +#endif
20387 +
20388 +/* ---------------------------------------------------------------------- */
20389 +
20390 +struct au_branch;
20391 +#ifdef CONFIG_AUFS_HNOTIFY
20392 +struct au_hnotify_op {
20393 +       void (*ctl)(struct au_hinode *hinode, int do_set);
20394 +       int (*alloc)(struct au_hinode *hinode);
20395 +
20396 +       /*
20397 +        * if it returns true, the caller should free hinode->hi_notify,
20398 +        * otherwise ->free() frees it.
20399 +        */
20400 +       int (*free)(struct au_hinode *hinode,
20401 +                   struct au_hnotify *hn) __must_check;
20402 +
20403 +       void (*fin)(void);
20404 +       int (*init)(void);
20405 +
20406 +       int (*reset_br)(unsigned int udba, struct au_branch *br, int perm);
20407 +       void (*fin_br)(struct au_branch *br);
20408 +       int (*init_br)(struct au_branch *br, int perm);
20409 +};
20410 +
20411 +/* hnotify.c */
20412 +int au_hn_alloc(struct au_hinode *hinode, struct inode *inode);
20413 +void au_hn_free(struct au_hinode *hinode);
20414 +void au_hn_ctl(struct au_hinode *hinode, int do_set);
20415 +void au_hn_reset(struct inode *inode, unsigned int flags);
20416 +int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
20417 +              const struct qstr *h_child_qstr, struct inode *h_child_inode);
20418 +int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm);
20419 +int au_hnotify_init_br(struct au_branch *br, int perm);
20420 +void au_hnotify_fin_br(struct au_branch *br);
20421 +int __init au_hnotify_init(void);
20422 +void au_hnotify_fin(void);
20423 +
20424 +/* hfsnotify.c */
20425 +extern const struct au_hnotify_op au_hnotify_op;
20426 +
20427 +static inline
20428 +void au_hn_init(struct au_hinode *hinode)
20429 +{
20430 +       hinode->hi_notify = NULL;
20431 +}
20432 +
20433 +static inline struct au_hnotify *au_hn(struct au_hinode *hinode)
20434 +{
20435 +       return hinode->hi_notify;
20436 +}
20437 +
20438 +#else
20439 +AuStub(int, au_hn_alloc, return -EOPNOTSUPP,
20440 +       struct au_hinode *hinode __maybe_unused,
20441 +       struct inode *inode __maybe_unused)
20442 +AuStub(struct au_hnotify *, au_hn, return NULL, struct au_hinode *hinode)
20443 +AuStubVoid(au_hn_free, struct au_hinode *hinode __maybe_unused)
20444 +AuStubVoid(au_hn_ctl, struct au_hinode *hinode __maybe_unused,
20445 +          int do_set __maybe_unused)
20446 +AuStubVoid(au_hn_reset, struct inode *inode __maybe_unused,
20447 +          unsigned int flags __maybe_unused)
20448 +AuStubInt0(au_hnotify_reset_br, unsigned int udba __maybe_unused,
20449 +          struct au_branch *br __maybe_unused,
20450 +          int perm __maybe_unused)
20451 +AuStubInt0(au_hnotify_init_br, struct au_branch *br __maybe_unused,
20452 +          int perm __maybe_unused)
20453 +AuStubVoid(au_hnotify_fin_br, struct au_branch *br __maybe_unused)
20454 +AuStubInt0(__init au_hnotify_init, void)
20455 +AuStubVoid(au_hnotify_fin, void)
20456 +AuStubVoid(au_hn_init, struct au_hinode *hinode __maybe_unused)
20457 +#endif /* CONFIG_AUFS_HNOTIFY */
20458 +
20459 +static inline void au_hn_suspend(struct au_hinode *hdir)
20460 +{
20461 +       au_hn_ctl(hdir, /*do_set*/0);
20462 +}
20463 +
20464 +static inline void au_hn_resume(struct au_hinode *hdir)
20465 +{
20466 +       au_hn_ctl(hdir, /*do_set*/1);
20467 +}
20468 +
20469 +static inline void au_hn_inode_lock(struct au_hinode *hdir)
20470 +{
20471 +       inode_lock(hdir->hi_inode);
20472 +       au_hn_suspend(hdir);
20473 +}
20474 +
20475 +static inline void au_hn_inode_lock_nested(struct au_hinode *hdir,
20476 +                                         unsigned int sc __maybe_unused)
20477 +{
20478 +       inode_lock_nested(hdir->hi_inode, sc);
20479 +       au_hn_suspend(hdir);
20480 +}
20481 +
20482 +#if 0 /* unused */
20483 +#include "vfsub.h"
20484 +static inline void au_hn_inode_lock_shared_nested(struct au_hinode *hdir,
20485 +                                                 unsigned int sc)
20486 +{
20487 +       inode_lock_shared_nested(hdir->hi_inode, sc);
20488 +       au_hn_suspend(hdir);
20489 +}
20490 +#endif
20491 +
20492 +static inline void au_hn_inode_unlock(struct au_hinode *hdir)
20493 +{
20494 +       au_hn_resume(hdir);
20495 +       inode_unlock(hdir->hi_inode);
20496 +}
20497 +
20498 +#endif /* __KERNEL__ */
20499 +#endif /* __AUFS_INODE_H__ */
20500 diff -urN /usr/share/empty/fs/aufs/ioctl.c linux/fs/aufs/ioctl.c
20501 --- /usr/share/empty/fs/aufs/ioctl.c    1970-01-01 01:00:00.000000000 +0100
20502 +++ linux/fs/aufs/ioctl.c       2022-03-21 14:49:05.726633010 +0100
20503 @@ -0,0 +1,220 @@
20504 +// SPDX-License-Identifier: GPL-2.0
20505 +/*
20506 + * Copyright (C) 2005-2021 Junjiro R. Okajima
20507 + *
20508 + * This program, aufs is free software; you can redistribute it and/or modify
20509 + * it under the terms of the GNU General Public License as published by
20510 + * the Free Software Foundation; either version 2 of the License, or
20511 + * (at your option) any later version.
20512 + *
20513 + * This program is distributed in the hope that it will be useful,
20514 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
20515 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20516 + * GNU General Public License for more details.
20517 + *
20518 + * You should have received a copy of the GNU General Public License
20519 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20520 + */
20521 +
20522 +/*
20523 + * ioctl
20524 + * plink-management and readdir in userspace.
20525 + * assist the pathconf(3) wrapper library.
20526 + * move-down
20527 + * File-based Hierarchical Storage Management.
20528 + */
20529 +
20530 +#include <linux/compat.h>
20531 +#include <linux/file.h>
20532 +#include "aufs.h"
20533 +
20534 +static int au_wbr_fd(struct path *path, struct aufs_wbr_fd __user *arg)
20535 +{
20536 +       int err, fd;
20537 +       aufs_bindex_t wbi, bindex, bbot;
20538 +       struct file *h_file;
20539 +       struct super_block *sb;
20540 +       struct dentry *root;
20541 +       struct au_branch *br;
20542 +       struct aufs_wbr_fd wbrfd = {
20543 +               .oflags = au_dir_roflags,
20544 +               .brid   = -1
20545 +       };
20546 +       const int valid = O_RDONLY | O_NONBLOCK | O_LARGEFILE | O_DIRECTORY
20547 +               | O_NOATIME | O_CLOEXEC;
20548 +
20549 +       AuDebugOn(wbrfd.oflags & ~valid);
20550 +
20551 +       if (arg) {
20552 +               err = copy_from_user(&wbrfd, arg, sizeof(wbrfd));
20553 +               if (unlikely(err)) {
20554 +                       err = -EFAULT;
20555 +                       goto out;
20556 +               }
20557 +
20558 +               err = -EINVAL;
20559 +               AuDbg("wbrfd{0%o, %d}\n", wbrfd.oflags, wbrfd.brid);
20560 +               wbrfd.oflags |= au_dir_roflags;
20561 +               AuDbg("0%o\n", wbrfd.oflags);
20562 +               if (unlikely(wbrfd.oflags & ~valid))
20563 +                       goto out;
20564 +       }
20565 +
20566 +       fd = get_unused_fd_flags(0);
20567 +       err = fd;
20568 +       if (unlikely(fd < 0))
20569 +               goto out;
20570 +
20571 +       h_file = ERR_PTR(-EINVAL);
20572 +       wbi = 0;
20573 +       br = NULL;
20574 +       sb = path->dentry->d_sb;
20575 +       root = sb->s_root;
20576 +       aufs_read_lock(root, AuLock_IR);
20577 +       bbot = au_sbbot(sb);
20578 +       if (wbrfd.brid >= 0) {
20579 +               wbi = au_br_index(sb, wbrfd.brid);
20580 +               if (unlikely(wbi < 0 || wbi > bbot))
20581 +                       goto out_unlock;
20582 +       }
20583 +
20584 +       h_file = ERR_PTR(-ENOENT);
20585 +       br = au_sbr(sb, wbi);
20586 +       if (!au_br_writable(br->br_perm)) {
20587 +               if (arg)
20588 +                       goto out_unlock;
20589 +
20590 +               bindex = wbi + 1;
20591 +               wbi = -1;
20592 +               for (; bindex <= bbot; bindex++) {
20593 +                       br = au_sbr(sb, bindex);
20594 +                       if (au_br_writable(br->br_perm)) {
20595 +                               wbi = bindex;
20596 +                               br = au_sbr(sb, wbi);
20597 +                               break;
20598 +                       }
20599 +               }
20600 +       }
20601 +       AuDbg("wbi %d\n", wbi);
20602 +       if (wbi >= 0)
20603 +               h_file = au_h_open(root, wbi, wbrfd.oflags, NULL,
20604 +                                  /*force_wr*/0);
20605 +
20606 +out_unlock:
20607 +       aufs_read_unlock(root, AuLock_IR);
20608 +       err = PTR_ERR(h_file);
20609 +       if (IS_ERR(h_file))
20610 +               goto out_fd;
20611 +
20612 +       au_lcnt_dec(&br->br_nfiles); /* cf. au_h_open() */
20613 +       fd_install(fd, h_file);
20614 +       err = fd;
20615 +       goto out; /* success */
20616 +
20617 +out_fd:
20618 +       put_unused_fd(fd);
20619 +out:
20620 +       AuTraceErr(err);
20621 +       return err;
20622 +}
20623 +
20624 +/* ---------------------------------------------------------------------- */
20625 +
20626 +long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg)
20627 +{
20628 +       long err;
20629 +       struct dentry *dentry;
20630 +
20631 +       switch (cmd) {
20632 +       case AUFS_CTL_RDU:
20633 +       case AUFS_CTL_RDU_INO:
20634 +               err = au_rdu_ioctl(file, cmd, arg);
20635 +               break;
20636 +
20637 +       case AUFS_CTL_WBR_FD:
20638 +               err = au_wbr_fd(&file->f_path, (void __user *)arg);
20639 +               break;
20640 +
20641 +       case AUFS_CTL_IBUSY:
20642 +               err = au_ibusy_ioctl(file, arg);
20643 +               break;
20644 +
20645 +       case AUFS_CTL_BRINFO:
20646 +               err = au_brinfo_ioctl(file, arg);
20647 +               break;
20648 +
20649 +       case AUFS_CTL_FHSM_FD:
20650 +               dentry = file->f_path.dentry;
20651 +               if (IS_ROOT(dentry))
20652 +                       err = au_fhsm_fd(dentry->d_sb, arg);
20653 +               else
20654 +                       err = -ENOTTY;
20655 +               break;
20656 +
20657 +       default:
20658 +               /* do not call the lower */
20659 +               AuDbg("0x%x\n", cmd);
20660 +               err = -ENOTTY;
20661 +       }
20662 +
20663 +       AuTraceErr(err);
20664 +       return err;
20665 +}
20666 +
20667 +long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg)
20668 +{
20669 +       long err;
20670 +
20671 +       switch (cmd) {
20672 +       case AUFS_CTL_MVDOWN:
20673 +               err = au_mvdown(file->f_path.dentry, (void __user *)arg);
20674 +               break;
20675 +
20676 +       case AUFS_CTL_WBR_FD:
20677 +               err = au_wbr_fd(&file->f_path, (void __user *)arg);
20678 +               break;
20679 +
20680 +       default:
20681 +               /* do not call the lower */
20682 +               AuDbg("0x%x\n", cmd);
20683 +               err = -ENOTTY;
20684 +       }
20685 +
20686 +       AuTraceErr(err);
20687 +       return err;
20688 +}
20689 +
20690 +#ifdef CONFIG_COMPAT
20691 +long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
20692 +                          unsigned long arg)
20693 +{
20694 +       long err;
20695 +
20696 +       switch (cmd) {
20697 +       case AUFS_CTL_RDU:
20698 +       case AUFS_CTL_RDU_INO:
20699 +               err = au_rdu_compat_ioctl(file, cmd, arg);
20700 +               break;
20701 +
20702 +       case AUFS_CTL_IBUSY:
20703 +               err = au_ibusy_compat_ioctl(file, arg);
20704 +               break;
20705 +
20706 +       case AUFS_CTL_BRINFO:
20707 +               err = au_brinfo_compat_ioctl(file, arg);
20708 +               break;
20709 +
20710 +       default:
20711 +               err = aufs_ioctl_dir(file, cmd, arg);
20712 +       }
20713 +
20714 +       AuTraceErr(err);
20715 +       return err;
20716 +}
20717 +
20718 +long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
20719 +                             unsigned long arg)
20720 +{
20721 +       return aufs_ioctl_nondir(file, cmd, (unsigned long)compat_ptr(arg));
20722 +}
20723 +#endif
20724 diff -urN /usr/share/empty/fs/aufs/i_op_add.c linux/fs/aufs/i_op_add.c
20725 --- /usr/share/empty/fs/aufs/i_op_add.c 1970-01-01 01:00:00.000000000 +0100
20726 +++ linux/fs/aufs/i_op_add.c    2022-03-21 14:49:05.726633010 +0100
20727 @@ -0,0 +1,941 @@
20728 +// SPDX-License-Identifier: GPL-2.0
20729 +/*
20730 + * Copyright (C) 2005-2021 Junjiro R. Okajima
20731 + *
20732 + * This program, aufs is free software; you can redistribute it and/or modify
20733 + * it under the terms of the GNU General Public License as published by
20734 + * the Free Software Foundation; either version 2 of the License, or
20735 + * (at your option) any later version.
20736 + *
20737 + * This program is distributed in the hope that it will be useful,
20738 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
20739 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20740 + * GNU General Public License for more details.
20741 + *
20742 + * You should have received a copy of the GNU General Public License
20743 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20744 + */
20745 +
20746 +/*
20747 + * inode operations (add entry)
20748 + */
20749 +
20750 +#include <linux/iversion.h>
20751 +#include "aufs.h"
20752 +
20753 +/*
20754 + * final procedure of adding a new entry, except link(2).
20755 + * remove whiteout, instantiate, copyup the parent dir's times and size
20756 + * and update version.
20757 + * if it failed, re-create the removed whiteout.
20758 + */
20759 +static int epilog(struct inode *dir, aufs_bindex_t bindex,
20760 +                 struct dentry *wh_dentry, struct dentry *dentry)
20761 +{
20762 +       int err, rerr;
20763 +       aufs_bindex_t bwh;
20764 +       struct path h_path;
20765 +       struct super_block *sb;
20766 +       struct inode *inode, *h_dir;
20767 +       struct dentry *wh;
20768 +
20769 +       bwh = -1;
20770 +       sb = dir->i_sb;
20771 +       if (wh_dentry) {
20772 +               h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */
20773 +               IMustLock(h_dir);
20774 +               AuDebugOn(au_h_iptr(dir, bindex) != h_dir);
20775 +               bwh = au_dbwh(dentry);
20776 +               h_path.dentry = wh_dentry;
20777 +               h_path.mnt = au_sbr_mnt(sb, bindex);
20778 +               err = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path,
20779 +                                         dentry);
20780 +               if (unlikely(err))
20781 +                       goto out;
20782 +       }
20783 +
20784 +       inode = au_new_inode(dentry, /*must_new*/1);
20785 +       if (!IS_ERR(inode)) {
20786 +               d_instantiate(dentry, inode);
20787 +               dir = d_inode(dentry->d_parent); /* dir inode is locked */
20788 +               IMustLock(dir);
20789 +               au_dir_ts(dir, bindex);
20790 +               inode_inc_iversion(dir);
20791 +               au_fhsm_wrote(sb, bindex, /*force*/0);
20792 +               return 0; /* success */
20793 +       }
20794 +
20795 +       err = PTR_ERR(inode);
20796 +       if (!wh_dentry)
20797 +               goto out;
20798 +
20799 +       /* revert */
20800 +       /* dir inode is locked */
20801 +       wh = au_wh_create(dentry, bwh, wh_dentry->d_parent);
20802 +       rerr = PTR_ERR(wh);
20803 +       if (IS_ERR(wh)) {
20804 +               AuIOErr("%pd reverting whiteout failed(%d, %d)\n",
20805 +                       dentry, err, rerr);
20806 +               err = -EIO;
20807 +       } else
20808 +               dput(wh);
20809 +
20810 +out:
20811 +       return err;
20812 +}
20813 +
20814 +static int au_d_may_add(struct dentry *dentry)
20815 +{
20816 +       int err;
20817 +
20818 +       err = 0;
20819 +       if (unlikely(d_unhashed(dentry)))
20820 +               err = -ENOENT;
20821 +       if (unlikely(d_really_is_positive(dentry)))
20822 +               err = -EEXIST;
20823 +       return err;
20824 +}
20825 +
20826 +/*
20827 + * simple tests for the adding inode operations.
20828 + * following the checks in vfs, plus the parent-child relationship.
20829 + */
20830 +int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
20831 +              struct dentry *h_parent, int isdir)
20832 +{
20833 +       int err;
20834 +       umode_t h_mode;
20835 +       struct dentry *h_dentry;
20836 +       struct inode *h_inode;
20837 +
20838 +       err = -ENAMETOOLONG;
20839 +       if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
20840 +               goto out;
20841 +
20842 +       h_dentry = au_h_dptr(dentry, bindex);
20843 +       if (d_really_is_negative(dentry)) {
20844 +               err = -EEXIST;
20845 +               if (unlikely(d_is_positive(h_dentry)))
20846 +                       goto out;
20847 +       } else {
20848 +               /* rename(2) case */
20849 +               err = -EIO;
20850 +               if (unlikely(d_is_negative(h_dentry)))
20851 +                       goto out;
20852 +               h_inode = d_inode(h_dentry);
20853 +               if (unlikely(!h_inode->i_nlink))
20854 +                       goto out;
20855 +
20856 +               h_mode = h_inode->i_mode;
20857 +               if (!isdir) {
20858 +                       err = -EISDIR;
20859 +                       if (unlikely(S_ISDIR(h_mode)))
20860 +                               goto out;
20861 +               } else if (unlikely(!S_ISDIR(h_mode))) {
20862 +                       err = -ENOTDIR;
20863 +                       goto out;
20864 +               }
20865 +       }
20866 +
20867 +       err = 0;
20868 +       /* expected parent dir is locked */
20869 +       if (unlikely(h_parent != h_dentry->d_parent))
20870 +               err = -EIO;
20871 +
20872 +out:
20873 +       AuTraceErr(err);
20874 +       return err;
20875 +}
20876 +
20877 +/*
20878 + * initial procedure of adding a new entry.
20879 + * prepare writable branch and the parent dir, lock it,
20880 + * and lookup whiteout for the new entry.
20881 + */
20882 +static struct dentry*
20883 +lock_hdir_lkup_wh(struct dentry *dentry, struct au_dtime *dt,
20884 +                 struct dentry *src_dentry, struct au_pin *pin,
20885 +                 struct au_wr_dir_args *wr_dir_args)
20886 +{
20887 +       struct dentry *wh_dentry, *h_parent;
20888 +       struct super_block *sb;
20889 +       struct au_branch *br;
20890 +       int err;
20891 +       unsigned int udba;
20892 +       aufs_bindex_t bcpup;
20893 +
20894 +       AuDbg("%pd\n", dentry);
20895 +
20896 +       err = au_wr_dir(dentry, src_dentry, wr_dir_args);
20897 +       bcpup = err;
20898 +       wh_dentry = ERR_PTR(err);
20899 +       if (unlikely(err < 0))
20900 +               goto out;
20901 +
20902 +       sb = dentry->d_sb;
20903 +       udba = au_opt_udba(sb);
20904 +       err = au_pin(pin, dentry, bcpup, udba,
20905 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
20906 +       wh_dentry = ERR_PTR(err);
20907 +       if (unlikely(err))
20908 +               goto out;
20909 +
20910 +       h_parent = au_pinned_h_parent(pin);
20911 +       if (udba != AuOpt_UDBA_NONE
20912 +           && au_dbtop(dentry) == bcpup)
20913 +               err = au_may_add(dentry, bcpup, h_parent,
20914 +                                au_ftest_wrdir(wr_dir_args->flags, ISDIR));
20915 +       else if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
20916 +               err = -ENAMETOOLONG;
20917 +       wh_dentry = ERR_PTR(err);
20918 +       if (unlikely(err))
20919 +               goto out_unpin;
20920 +
20921 +       br = au_sbr(sb, bcpup);
20922 +       if (dt) {
20923 +               struct path tmp = {
20924 +                       .dentry = h_parent,
20925 +                       .mnt    = au_br_mnt(br)
20926 +               };
20927 +               au_dtime_store(dt, au_pinned_parent(pin), &tmp);
20928 +       }
20929 +
20930 +       wh_dentry = NULL;
20931 +       if (bcpup != au_dbwh(dentry))
20932 +               goto out; /* success */
20933 +
20934 +       /*
20935 +        * ENAMETOOLONG here means that if we allowed create such name, then it
20936 +        * would not be able to removed in the future. So we don't allow such
20937 +        * name here and we don't handle ENAMETOOLONG differently here.
20938 +        */
20939 +       wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
20940 +
20941 +out_unpin:
20942 +       if (IS_ERR(wh_dentry))
20943 +               au_unpin(pin);
20944 +out:
20945 +       return wh_dentry;
20946 +}
20947 +
20948 +/* ---------------------------------------------------------------------- */
20949 +
20950 +enum { Mknod, Symlink, Creat };
20951 +struct simple_arg {
20952 +       int type;
20953 +       union {
20954 +               struct {
20955 +                       umode_t                 mode;
20956 +                       bool                    want_excl;
20957 +                       bool                    try_aopen;
20958 +                       struct vfsub_aopen_args *aopen;
20959 +               } c;
20960 +               struct {
20961 +                       const char *symname;
20962 +               } s;
20963 +               struct {
20964 +                       umode_t mode;
20965 +                       dev_t dev;
20966 +               } m;
20967 +       } u;
20968 +};
20969 +
20970 +static int add_simple(struct inode *dir, struct dentry *dentry,
20971 +                     struct simple_arg *arg)
20972 +{
20973 +       int err, rerr;
20974 +       aufs_bindex_t btop;
20975 +       unsigned char created;
20976 +       const unsigned char try_aopen
20977 +               = (arg->type == Creat && arg->u.c.try_aopen);
20978 +       struct vfsub_aopen_args *aopen = arg->u.c.aopen;
20979 +       struct dentry *wh_dentry, *parent;
20980 +       struct inode *h_dir;
20981 +       struct super_block *sb;
20982 +       struct au_branch *br;
20983 +       /* to reduce stack size */
20984 +       struct {
20985 +               struct au_dtime dt;
20986 +               struct au_pin pin;
20987 +               struct path h_path;
20988 +               struct au_wr_dir_args wr_dir_args;
20989 +       } *a;
20990 +
20991 +       AuDbg("%pd\n", dentry);
20992 +       IMustLock(dir);
20993 +
20994 +       err = -ENOMEM;
20995 +       a = kmalloc(sizeof(*a), GFP_NOFS);
20996 +       if (unlikely(!a))
20997 +               goto out;
20998 +       a->wr_dir_args.force_btgt = -1;
20999 +       a->wr_dir_args.flags = AuWrDir_ADD_ENTRY;
21000 +
21001 +       parent = dentry->d_parent; /* dir inode is locked */
21002 +       if (!try_aopen) {
21003 +               err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
21004 +               if (unlikely(err))
21005 +                       goto out_free;
21006 +       }
21007 +       err = au_d_may_add(dentry);
21008 +       if (unlikely(err))
21009 +               goto out_unlock;
21010 +       if (!try_aopen)
21011 +               di_write_lock_parent(parent);
21012 +       wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
21013 +                                     &a->pin, &a->wr_dir_args);
21014 +       err = PTR_ERR(wh_dentry);
21015 +       if (IS_ERR(wh_dentry))
21016 +               goto out_parent;
21017 +
21018 +       btop = au_dbtop(dentry);
21019 +       sb = dentry->d_sb;
21020 +       br = au_sbr(sb, btop);
21021 +       a->h_path.dentry = au_h_dptr(dentry, btop);
21022 +       a->h_path.mnt = au_br_mnt(br);
21023 +       h_dir = au_pinned_h_dir(&a->pin);
21024 +       switch (arg->type) {
21025 +       case Creat:
21026 +               if (!try_aopen || !h_dir->i_op->atomic_open) {
21027 +                       err = vfsub_create(h_dir, &a->h_path, arg->u.c.mode,
21028 +                                          arg->u.c.want_excl);
21029 +                       created = !err;
21030 +                       if (!err && try_aopen)
21031 +                               aopen->file->f_mode |= FMODE_CREATED;
21032 +               } else {
21033 +                       aopen->br = br;
21034 +                       err = vfsub_atomic_open(h_dir, a->h_path.dentry, aopen);
21035 +                       AuDbg("err %d\n", err);
21036 +                       AuDbgFile(aopen->file);
21037 +                       created = err >= 0
21038 +                               && !!(aopen->file->f_mode & FMODE_CREATED);
21039 +               }
21040 +               break;
21041 +       case Symlink:
21042 +               err = vfsub_symlink(h_dir, &a->h_path, arg->u.s.symname);
21043 +               created = !err;
21044 +               break;
21045 +       case Mknod:
21046 +               err = vfsub_mknod(h_dir, &a->h_path, arg->u.m.mode,
21047 +                                 arg->u.m.dev);
21048 +               created = !err;
21049 +               break;
21050 +       default:
21051 +               BUG();
21052 +       }
21053 +       if (unlikely(err < 0))
21054 +               goto out_unpin;
21055 +
21056 +       err = epilog(dir, btop, wh_dentry, dentry);
21057 +       if (!err)
21058 +               goto out_unpin; /* success */
21059 +
21060 +       /* revert */
21061 +       if (created /* && d_is_positive(a->h_path.dentry) */) {
21062 +               /* no delegation since it is just created */
21063 +               rerr = vfsub_unlink(h_dir, &a->h_path, /*delegated*/NULL,
21064 +                                   /*force*/0);
21065 +               if (rerr) {
21066 +                       AuIOErr("%pd revert failure(%d, %d)\n",
21067 +                               dentry, err, rerr);
21068 +                       err = -EIO;
21069 +               }
21070 +               au_dtime_revert(&a->dt);
21071 +       }
21072 +       if (try_aopen && h_dir->i_op->atomic_open
21073 +           && (aopen->file->f_mode & FMODE_OPENED))
21074 +               /* aopen->file is still opened */
21075 +               au_lcnt_dec(&aopen->br->br_nfiles);
21076 +
21077 +out_unpin:
21078 +       au_unpin(&a->pin);
21079 +       dput(wh_dentry);
21080 +out_parent:
21081 +       if (!try_aopen)
21082 +               di_write_unlock(parent);
21083 +out_unlock:
21084 +       if (unlikely(err)) {
21085 +               au_update_dbtop(dentry);
21086 +               d_drop(dentry);
21087 +       }
21088 +       if (!try_aopen)
21089 +               aufs_read_unlock(dentry, AuLock_DW);
21090 +out_free:
21091 +       au_kfree_rcu(a);
21092 +out:
21093 +       return err;
21094 +}
21095 +
21096 +int aufs_mknod(struct user_namespace *userns, struct inode *dir,
21097 +              struct dentry *dentry, umode_t mode, dev_t dev)
21098 +{
21099 +       struct simple_arg arg = {
21100 +               .type = Mknod,
21101 +               .u.m = {
21102 +                       .mode   = mode,
21103 +                       .dev    = dev
21104 +               }
21105 +       };
21106 +       return add_simple(dir, dentry, &arg);
21107 +}
21108 +
21109 +int aufs_symlink(struct user_namespace *userns, struct inode *dir,
21110 +                struct dentry *dentry, const char *symname)
21111 +{
21112 +       struct simple_arg arg = {
21113 +               .type = Symlink,
21114 +               .u.s.symname = symname
21115 +       };
21116 +       return add_simple(dir, dentry, &arg);
21117 +}
21118 +
21119 +int aufs_create(struct user_namespace *userns, struct inode *dir,
21120 +               struct dentry *dentry, umode_t mode, bool want_excl)
21121 +{
21122 +       struct simple_arg arg = {
21123 +               .type = Creat,
21124 +               .u.c = {
21125 +                       .mode           = mode,
21126 +                       .want_excl      = want_excl
21127 +               }
21128 +       };
21129 +       return add_simple(dir, dentry, &arg);
21130 +}
21131 +
21132 +int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
21133 +                      struct vfsub_aopen_args *aopen_args)
21134 +{
21135 +       struct simple_arg arg = {
21136 +               .type = Creat,
21137 +               .u.c = {
21138 +                       .mode           = aopen_args->create_mode,
21139 +                       .want_excl      = aopen_args->open_flag & O_EXCL,
21140 +                       .try_aopen      = true,
21141 +                       .aopen          = aopen_args
21142 +               }
21143 +       };
21144 +       return add_simple(dir, dentry, &arg);
21145 +}
21146 +
21147 +int aufs_tmpfile(struct user_namespace *userns, struct inode *dir,
21148 +                struct dentry *dentry, umode_t mode)
21149 +{
21150 +       int err;
21151 +       aufs_bindex_t bindex;
21152 +       struct super_block *sb;
21153 +       struct dentry *parent, *h_parent, *h_dentry;
21154 +       struct inode *h_dir, *inode;
21155 +       struct vfsmount *h_mnt;
21156 +       struct user_namespace *h_userns;
21157 +       struct au_wr_dir_args wr_dir_args = {
21158 +               .force_btgt     = -1,
21159 +               .flags          = AuWrDir_TMPFILE
21160 +       };
21161 +
21162 +       /* copy-up may happen */
21163 +       inode_lock(dir);
21164 +
21165 +       sb = dir->i_sb;
21166 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
21167 +       if (unlikely(err))
21168 +               goto out;
21169 +
21170 +       err = au_di_init(dentry);
21171 +       if (unlikely(err))
21172 +               goto out_si;
21173 +
21174 +       err = -EBUSY;
21175 +       parent = d_find_any_alias(dir);
21176 +       AuDebugOn(!parent);
21177 +       di_write_lock_parent(parent);
21178 +       if (unlikely(d_inode(parent) != dir))
21179 +               goto out_parent;
21180 +
21181 +       err = au_digen_test(parent, au_sigen(sb));
21182 +       if (unlikely(err))
21183 +               goto out_parent;
21184 +
21185 +       bindex = au_dbtop(parent);
21186 +       au_set_dbtop(dentry, bindex);
21187 +       au_set_dbbot(dentry, bindex);
21188 +       err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
21189 +       bindex = err;
21190 +       if (unlikely(err < 0))
21191 +               goto out_parent;
21192 +
21193 +       err = -EOPNOTSUPP;
21194 +       h_dir = au_h_iptr(dir, bindex);
21195 +       if (unlikely(!h_dir->i_op->tmpfile))
21196 +               goto out_parent;
21197 +
21198 +       h_mnt = au_sbr_mnt(sb, bindex);
21199 +       err = vfsub_mnt_want_write(h_mnt);
21200 +       if (unlikely(err))
21201 +               goto out_parent;
21202 +
21203 +       h_userns = mnt_user_ns(h_mnt);
21204 +       h_parent = au_h_dptr(parent, bindex);
21205 +       h_dentry = vfs_tmpfile(h_userns, h_parent, mode, /*open_flag*/0);
21206 +       if (IS_ERR(h_dentry)) {
21207 +               err = PTR_ERR(h_dentry);
21208 +               goto out_mnt;
21209 +       }
21210 +
21211 +       au_set_dbtop(dentry, bindex);
21212 +       au_set_dbbot(dentry, bindex);
21213 +       au_set_h_dptr(dentry, bindex, dget(h_dentry));
21214 +       inode = au_new_inode(dentry, /*must_new*/1);
21215 +       if (IS_ERR(inode)) {
21216 +               err = PTR_ERR(inode);
21217 +               au_set_h_dptr(dentry, bindex, NULL);
21218 +               au_set_dbtop(dentry, -1);
21219 +               au_set_dbbot(dentry, -1);
21220 +       } else {
21221 +               if (!inode->i_nlink)
21222 +                       set_nlink(inode, 1);
21223 +               d_tmpfile(dentry, inode);
21224 +               au_di(dentry)->di_tmpfile = 1;
21225 +
21226 +               /* update without i_mutex */
21227 +               if (au_ibtop(dir) == au_dbtop(dentry))
21228 +                       au_cpup_attr_timesizes(dir);
21229 +       }
21230 +       dput(h_dentry);
21231 +
21232 +out_mnt:
21233 +       vfsub_mnt_drop_write(h_mnt);
21234 +out_parent:
21235 +       di_write_unlock(parent);
21236 +       dput(parent);
21237 +       di_write_unlock(dentry);
21238 +       if (unlikely(err)) {
21239 +               au_di_fin(dentry);
21240 +               dentry->d_fsdata = NULL;
21241 +       }
21242 +out_si:
21243 +       si_read_unlock(sb);
21244 +out:
21245 +       inode_unlock(dir);
21246 +       return err;
21247 +}
21248 +
21249 +/* ---------------------------------------------------------------------- */
21250 +
21251 +struct au_link_args {
21252 +       aufs_bindex_t bdst, bsrc;
21253 +       struct au_pin pin;
21254 +       struct path h_path;
21255 +       struct dentry *src_parent, *parent;
21256 +};
21257 +
21258 +static int au_cpup_before_link(struct dentry *src_dentry,
21259 +                              struct au_link_args *a)
21260 +{
21261 +       int err;
21262 +       struct dentry *h_src_dentry;
21263 +       struct au_cp_generic cpg = {
21264 +               .dentry = src_dentry,
21265 +               .bdst   = a->bdst,
21266 +               .bsrc   = a->bsrc,
21267 +               .len    = -1,
21268 +               .pin    = &a->pin,
21269 +               .flags  = AuCpup_DTIME | AuCpup_HOPEN /* | AuCpup_KEEPLINO */
21270 +       };
21271 +
21272 +       di_read_lock_parent(a->src_parent, AuLock_IR);
21273 +       err = au_test_and_cpup_dirs(src_dentry, a->bdst);
21274 +       if (unlikely(err))
21275 +               goto out;
21276 +
21277 +       h_src_dentry = au_h_dptr(src_dentry, a->bsrc);
21278 +       err = au_pin(&a->pin, src_dentry, a->bdst,
21279 +                    au_opt_udba(src_dentry->d_sb),
21280 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
21281 +       if (unlikely(err))
21282 +               goto out;
21283 +
21284 +       err = au_sio_cpup_simple(&cpg);
21285 +       au_unpin(&a->pin);
21286 +
21287 +out:
21288 +       di_read_unlock(a->src_parent, AuLock_IR);
21289 +       return err;
21290 +}
21291 +
21292 +static int au_cpup_or_link(struct dentry *src_dentry, struct dentry *dentry,
21293 +                          struct au_link_args *a)
21294 +{
21295 +       int err;
21296 +       unsigned char plink;
21297 +       aufs_bindex_t bbot;
21298 +       struct dentry *h_src_dentry;
21299 +       struct inode *h_inode, *inode, *delegated;
21300 +       struct super_block *sb;
21301 +       struct file *h_file;
21302 +
21303 +       plink = 0;
21304 +       h_inode = NULL;
21305 +       sb = src_dentry->d_sb;
21306 +       inode = d_inode(src_dentry);
21307 +       if (au_ibtop(inode) <= a->bdst)
21308 +               h_inode = au_h_iptr(inode, a->bdst);
21309 +       if (!h_inode || !h_inode->i_nlink) {
21310 +               /* copyup src_dentry as the name of dentry. */
21311 +               bbot = au_dbbot(dentry);
21312 +               if (bbot < a->bsrc)
21313 +                       au_set_dbbot(dentry, a->bsrc);
21314 +               au_set_h_dptr(dentry, a->bsrc,
21315 +                             dget(au_h_dptr(src_dentry, a->bsrc)));
21316 +               dget(a->h_path.dentry);
21317 +               au_set_h_dptr(dentry, a->bdst, NULL);
21318 +               AuDbg("temporary d_inode...\n");
21319 +               spin_lock(&dentry->d_lock);
21320 +               dentry->d_inode = d_inode(src_dentry); /* tmp */
21321 +               spin_unlock(&dentry->d_lock);
21322 +               h_file = au_h_open_pre(dentry, a->bsrc, /*force_wr*/0);
21323 +               if (IS_ERR(h_file))
21324 +                       err = PTR_ERR(h_file);
21325 +               else {
21326 +                       struct au_cp_generic cpg = {
21327 +                               .dentry = dentry,
21328 +                               .bdst   = a->bdst,
21329 +                               .bsrc   = -1,
21330 +                               .len    = -1,
21331 +                               .pin    = &a->pin,
21332 +                               .flags  = AuCpup_KEEPLINO
21333 +                       };
21334 +                       err = au_sio_cpup_simple(&cpg);
21335 +                       au_h_open_post(dentry, a->bsrc, h_file);
21336 +                       if (!err) {
21337 +                               dput(a->h_path.dentry);
21338 +                               a->h_path.dentry = au_h_dptr(dentry, a->bdst);
21339 +                       } else
21340 +                               au_set_h_dptr(dentry, a->bdst,
21341 +                                             a->h_path.dentry);
21342 +               }
21343 +               spin_lock(&dentry->d_lock);
21344 +               dentry->d_inode = NULL; /* restore */
21345 +               spin_unlock(&dentry->d_lock);
21346 +               AuDbg("temporary d_inode...done\n");
21347 +               au_set_h_dptr(dentry, a->bsrc, NULL);
21348 +               au_set_dbbot(dentry, bbot);
21349 +       } else {
21350 +               /* the inode of src_dentry already exists on a.bdst branch */
21351 +               h_src_dentry = d_find_alias(h_inode);
21352 +               if (!h_src_dentry && au_plink_test(inode)) {
21353 +                       plink = 1;
21354 +                       h_src_dentry = au_plink_lkup(inode, a->bdst);
21355 +                       err = PTR_ERR(h_src_dentry);
21356 +                       if (IS_ERR(h_src_dentry))
21357 +                               goto out;
21358 +
21359 +                       if (unlikely(d_is_negative(h_src_dentry))) {
21360 +                               dput(h_src_dentry);
21361 +                               h_src_dentry = NULL;
21362 +                       }
21363 +
21364 +               }
21365 +               if (h_src_dentry) {
21366 +                       delegated = NULL;
21367 +                       err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
21368 +                                        &a->h_path, &delegated);
21369 +                       if (unlikely(err == -EWOULDBLOCK)) {
21370 +                               pr_warn("cannot retry for NFSv4 delegation"
21371 +                                       " for an internal link\n");
21372 +                               iput(delegated);
21373 +                       }
21374 +                       dput(h_src_dentry);
21375 +               } else {
21376 +                       AuIOErr("no dentry found for hi%lu on b%d\n",
21377 +                               h_inode->i_ino, a->bdst);
21378 +                       err = -EIO;
21379 +               }
21380 +       }
21381 +
21382 +       if (!err && !plink)
21383 +               au_plink_append(inode, a->bdst, a->h_path.dentry);
21384 +
21385 +out:
21386 +       AuTraceErr(err);
21387 +       return err;
21388 +}
21389 +
21390 +int aufs_link(struct dentry *src_dentry, struct inode *dir,
21391 +             struct dentry *dentry)
21392 +{
21393 +       int err, rerr;
21394 +       struct au_dtime dt;
21395 +       struct au_link_args *a;
21396 +       struct dentry *wh_dentry, *h_src_dentry;
21397 +       struct inode *inode, *delegated;
21398 +       struct super_block *sb;
21399 +       struct au_wr_dir_args wr_dir_args = {
21400 +               /* .force_btgt  = -1, */
21401 +               .flags          = AuWrDir_ADD_ENTRY
21402 +       };
21403 +
21404 +       IMustLock(dir);
21405 +       inode = d_inode(src_dentry);
21406 +       IMustLock(inode);
21407 +
21408 +       err = -ENOMEM;
21409 +       a = kzalloc(sizeof(*a), GFP_NOFS);
21410 +       if (unlikely(!a))
21411 +               goto out;
21412 +
21413 +       a->parent = dentry->d_parent; /* dir inode is locked */
21414 +       err = aufs_read_and_write_lock2(dentry, src_dentry,
21415 +                                       AuLock_NOPLM | AuLock_GEN);
21416 +       if (unlikely(err))
21417 +               goto out_kfree;
21418 +       err = au_d_linkable(src_dentry);
21419 +       if (unlikely(err))
21420 +               goto out_unlock;
21421 +       err = au_d_may_add(dentry);
21422 +       if (unlikely(err))
21423 +               goto out_unlock;
21424 +
21425 +       a->src_parent = dget_parent(src_dentry);
21426 +       wr_dir_args.force_btgt = au_ibtop(inode);
21427 +
21428 +       di_write_lock_parent(a->parent);
21429 +       wr_dir_args.force_btgt = au_wbr(dentry, wr_dir_args.force_btgt);
21430 +       wh_dentry = lock_hdir_lkup_wh(dentry, &dt, src_dentry, &a->pin,
21431 +                                     &wr_dir_args);
21432 +       err = PTR_ERR(wh_dentry);
21433 +       if (IS_ERR(wh_dentry))
21434 +               goto out_parent;
21435 +
21436 +       err = 0;
21437 +       sb = dentry->d_sb;
21438 +       a->bdst = au_dbtop(dentry);
21439 +       a->h_path.dentry = au_h_dptr(dentry, a->bdst);
21440 +       a->h_path.mnt = au_sbr_mnt(sb, a->bdst);
21441 +       a->bsrc = au_ibtop(inode);
21442 +       h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
21443 +       if (!h_src_dentry && au_di(src_dentry)->di_tmpfile)
21444 +               h_src_dentry = dget(au_hi_wh(inode, a->bsrc));
21445 +       if (!h_src_dentry) {
21446 +               a->bsrc = au_dbtop(src_dentry);
21447 +               h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
21448 +               AuDebugOn(!h_src_dentry);
21449 +       } else if (IS_ERR(h_src_dentry)) {
21450 +               err = PTR_ERR(h_src_dentry);
21451 +               goto out_parent;
21452 +       }
21453 +
21454 +       /*
21455 +        * aufs doesn't touch the credential so
21456 +        * security_dentry_create_files_as() is unnecessary.
21457 +        */
21458 +       if (au_opt_test(au_mntflags(sb), PLINK)) {
21459 +               if (a->bdst < a->bsrc
21460 +                   /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */)
21461 +                       err = au_cpup_or_link(src_dentry, dentry, a);
21462 +               else {
21463 +                       delegated = NULL;
21464 +                       err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
21465 +                                        &a->h_path, &delegated);
21466 +                       if (unlikely(err == -EWOULDBLOCK)) {
21467 +                               pr_warn("cannot retry for NFSv4 delegation"
21468 +                                       " for an internal link\n");
21469 +                               iput(delegated);
21470 +                       }
21471 +               }
21472 +               dput(h_src_dentry);
21473 +       } else {
21474 +               /*
21475 +                * copyup src_dentry to the branch we process,
21476 +                * and then link(2) to it.
21477 +                */
21478 +               dput(h_src_dentry);
21479 +               if (a->bdst < a->bsrc
21480 +                   /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */) {
21481 +                       au_unpin(&a->pin);
21482 +                       di_write_unlock(a->parent);
21483 +                       err = au_cpup_before_link(src_dentry, a);
21484 +                       di_write_lock_parent(a->parent);
21485 +                       if (!err)
21486 +                               err = au_pin(&a->pin, dentry, a->bdst,
21487 +                                            au_opt_udba(sb),
21488 +                                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
21489 +                       if (unlikely(err))
21490 +                               goto out_wh;
21491 +               }
21492 +               if (!err) {
21493 +                       h_src_dentry = au_h_dptr(src_dentry, a->bdst);
21494 +                       err = -ENOENT;
21495 +                       if (h_src_dentry && d_is_positive(h_src_dentry)) {
21496 +                               delegated = NULL;
21497 +                               err = vfsub_link(h_src_dentry,
21498 +                                                au_pinned_h_dir(&a->pin),
21499 +                                                &a->h_path, &delegated);
21500 +                               if (unlikely(err == -EWOULDBLOCK)) {
21501 +                                       pr_warn("cannot retry"
21502 +                                               " for NFSv4 delegation"
21503 +                                               " for an internal link\n");
21504 +                                       iput(delegated);
21505 +                               }
21506 +                       }
21507 +               }
21508 +       }
21509 +       if (unlikely(err))
21510 +               goto out_unpin;
21511 +
21512 +       if (wh_dentry) {
21513 +               a->h_path.dentry = wh_dentry;
21514 +               err = au_wh_unlink_dentry(au_pinned_h_dir(&a->pin), &a->h_path,
21515 +                                         dentry);
21516 +               if (unlikely(err))
21517 +                       goto out_revert;
21518 +       }
21519 +
21520 +       au_dir_ts(dir, a->bdst);
21521 +       inode_inc_iversion(dir);
21522 +       inc_nlink(inode);
21523 +       inode->i_ctime = dir->i_ctime;
21524 +       d_instantiate(dentry, au_igrab(inode));
21525 +       if (d_unhashed(a->h_path.dentry))
21526 +               /* some filesystem calls d_drop() */
21527 +               d_drop(dentry);
21528 +       /* some filesystems consume an inode even hardlink */
21529 +       au_fhsm_wrote(sb, a->bdst, /*force*/0);
21530 +       goto out_unpin; /* success */
21531 +
21532 +out_revert:
21533 +       /* no delegation since it is just created */
21534 +       rerr = vfsub_unlink(au_pinned_h_dir(&a->pin), &a->h_path,
21535 +                           /*delegated*/NULL, /*force*/0);
21536 +       if (unlikely(rerr)) {
21537 +               AuIOErr("%pd reverting failed(%d, %d)\n", dentry, err, rerr);
21538 +               err = -EIO;
21539 +       }
21540 +       au_dtime_revert(&dt);
21541 +out_unpin:
21542 +       au_unpin(&a->pin);
21543 +out_wh:
21544 +       dput(wh_dentry);
21545 +out_parent:
21546 +       di_write_unlock(a->parent);
21547 +       dput(a->src_parent);
21548 +out_unlock:
21549 +       if (unlikely(err)) {
21550 +               au_update_dbtop(dentry);
21551 +               d_drop(dentry);
21552 +       }
21553 +       aufs_read_and_write_unlock2(dentry, src_dentry);
21554 +out_kfree:
21555 +       au_kfree_rcu(a);
21556 +out:
21557 +       AuTraceErr(err);
21558 +       return err;
21559 +}
21560 +
21561 +int aufs_mkdir(struct user_namespace *userns, struct inode *dir,
21562 +              struct dentry *dentry, umode_t mode)
21563 +{
21564 +       int err, rerr;
21565 +       aufs_bindex_t bindex;
21566 +       unsigned char diropq;
21567 +       struct path h_path;
21568 +       struct dentry *wh_dentry, *parent, *opq_dentry;
21569 +       struct inode *h_inode;
21570 +       struct super_block *sb;
21571 +       struct {
21572 +               struct au_pin pin;
21573 +               struct au_dtime dt;
21574 +       } *a; /* reduce the stack usage */
21575 +       struct au_wr_dir_args wr_dir_args = {
21576 +               .force_btgt     = -1,
21577 +               .flags          = AuWrDir_ADD_ENTRY | AuWrDir_ISDIR
21578 +       };
21579 +
21580 +       IMustLock(dir);
21581 +
21582 +       err = -ENOMEM;
21583 +       a = kmalloc(sizeof(*a), GFP_NOFS);
21584 +       if (unlikely(!a))
21585 +               goto out;
21586 +
21587 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
21588 +       if (unlikely(err))
21589 +               goto out_free;
21590 +       err = au_d_may_add(dentry);
21591 +       if (unlikely(err))
21592 +               goto out_unlock;
21593 +
21594 +       parent = dentry->d_parent; /* dir inode is locked */
21595 +       di_write_lock_parent(parent);
21596 +       wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
21597 +                                     &a->pin, &wr_dir_args);
21598 +       err = PTR_ERR(wh_dentry);
21599 +       if (IS_ERR(wh_dentry))
21600 +               goto out_parent;
21601 +
21602 +       sb = dentry->d_sb;
21603 +       bindex = au_dbtop(dentry);
21604 +       h_path.dentry = au_h_dptr(dentry, bindex);
21605 +       h_path.mnt = au_sbr_mnt(sb, bindex);
21606 +       err = vfsub_mkdir(au_pinned_h_dir(&a->pin), &h_path, mode);
21607 +       if (unlikely(err))
21608 +               goto out_unpin;
21609 +
21610 +       /* make the dir opaque */
21611 +       diropq = 0;
21612 +       h_inode = d_inode(h_path.dentry);
21613 +       if (wh_dentry
21614 +           || au_opt_test(au_mntflags(sb), ALWAYS_DIROPQ)) {
21615 +               inode_lock_nested(h_inode, AuLsc_I_CHILD);
21616 +               opq_dentry = au_diropq_create(dentry, bindex);
21617 +               inode_unlock(h_inode);
21618 +               err = PTR_ERR(opq_dentry);
21619 +               if (IS_ERR(opq_dentry))
21620 +                       goto out_dir;
21621 +               dput(opq_dentry);
21622 +               diropq = 1;
21623 +       }
21624 +
21625 +       err = epilog(dir, bindex, wh_dentry, dentry);
21626 +       if (!err) {
21627 +               inc_nlink(dir);
21628 +               goto out_unpin; /* success */
21629 +       }
21630 +
21631 +       /* revert */
21632 +       if (diropq) {
21633 +               AuLabel(revert opq);
21634 +               inode_lock_nested(h_inode, AuLsc_I_CHILD);
21635 +               rerr = au_diropq_remove(dentry, bindex);
21636 +               inode_unlock(h_inode);
21637 +               if (rerr) {
21638 +                       AuIOErr("%pd reverting diropq failed(%d, %d)\n",
21639 +                               dentry, err, rerr);
21640 +                       err = -EIO;
21641 +               }
21642 +       }
21643 +
21644 +out_dir:
21645 +       AuLabel(revert dir);
21646 +       rerr = vfsub_rmdir(au_pinned_h_dir(&a->pin), &h_path);
21647 +       if (rerr) {
21648 +               AuIOErr("%pd reverting dir failed(%d, %d)\n",
21649 +                       dentry, err, rerr);
21650 +               err = -EIO;
21651 +       }
21652 +       au_dtime_revert(&a->dt);
21653 +out_unpin:
21654 +       au_unpin(&a->pin);
21655 +       dput(wh_dentry);
21656 +out_parent:
21657 +       di_write_unlock(parent);
21658 +out_unlock:
21659 +       if (unlikely(err)) {
21660 +               au_update_dbtop(dentry);
21661 +               d_drop(dentry);
21662 +       }
21663 +       aufs_read_unlock(dentry, AuLock_DW);
21664 +out_free:
21665 +       au_kfree_rcu(a);
21666 +out:
21667 +       return err;
21668 +}
21669 diff -urN /usr/share/empty/fs/aufs/i_op.c linux/fs/aufs/i_op.c
21670 --- /usr/share/empty/fs/aufs/i_op.c     1970-01-01 01:00:00.000000000 +0100
21671 +++ linux/fs/aufs/i_op.c        2022-03-21 14:49:05.726633010 +0100
21672 @@ -0,0 +1,1512 @@
21673 +// SPDX-License-Identifier: GPL-2.0
21674 +/*
21675 + * Copyright (C) 2005-2021 Junjiro R. Okajima
21676 + *
21677 + * This program, aufs is free software; you can redistribute it and/or modify
21678 + * it under the terms of the GNU General Public License as published by
21679 + * the Free Software Foundation; either version 2 of the License, or
21680 + * (at your option) any later version.
21681 + *
21682 + * This program is distributed in the hope that it will be useful,
21683 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
21684 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21685 + * GNU General Public License for more details.
21686 + *
21687 + * You should have received a copy of the GNU General Public License
21688 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21689 + */
21690 +
21691 +/*
21692 + * inode operations (except add/del/rename)
21693 + */
21694 +
21695 +#include <linux/device_cgroup.h>
21696 +#include <linux/fs_stack.h>
21697 +#include <linux/iversion.h>
21698 +#include <linux/security.h>
21699 +#include "aufs.h"
21700 +
21701 +static int h_permission(struct inode *h_inode, int mask,
21702 +                       struct path *h_path, int brperm)
21703 +{
21704 +       int err;
21705 +       const unsigned char write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
21706 +       struct user_namespace *h_userns;
21707 +
21708 +       err = -EPERM;
21709 +       if (write_mask && IS_IMMUTABLE(h_inode))
21710 +               goto out;
21711 +
21712 +       err = -EACCES;
21713 +       if (((mask & MAY_EXEC)
21714 +            && S_ISREG(h_inode->i_mode)
21715 +            && (path_noexec(h_path)
21716 +                || !(h_inode->i_mode & 0111))))
21717 +               goto out;
21718 +
21719 +       /*
21720 +        * - skip the lower fs test in the case of write to ro branch.
21721 +        * - nfs dir permission write check is optimized, but a policy for
21722 +        *   link/rename requires a real check.
21723 +        * - nfs always sets SB_POSIXACL regardless its mount option 'noacl.'
21724 +        *   in this case, generic_permission() returns -EOPNOTSUPP.
21725 +        */
21726 +       h_userns = mnt_user_ns(h_path->mnt);
21727 +       if ((write_mask && !au_br_writable(brperm))
21728 +           || (au_test_nfs(h_inode->i_sb) && S_ISDIR(h_inode->i_mode)
21729 +               && write_mask && !(mask & MAY_READ))
21730 +           || !h_inode->i_op->permission) {
21731 +               /* AuLabel(generic_permission); */
21732 +               /* AuDbg("get_acl %ps\n", h_inode->i_op->get_acl); */
21733 +               err = generic_permission(h_userns, h_inode, mask);
21734 +               if (err == -EOPNOTSUPP && au_test_nfs_noacl(h_inode))
21735 +                       err = h_inode->i_op->permission(h_userns, h_inode,
21736 +                                                       mask);
21737 +               AuTraceErr(err);
21738 +       } else {
21739 +               /* AuLabel(h_inode->permission); */
21740 +               err = h_inode->i_op->permission(h_userns, h_inode, mask);
21741 +               AuTraceErr(err);
21742 +       }
21743 +
21744 +       if (!err)
21745 +               err = devcgroup_inode_permission(h_inode, mask);
21746 +       if (!err)
21747 +               err = security_inode_permission(h_inode, mask);
21748 +
21749 +out:
21750 +       return err;
21751 +}
21752 +
21753 +static int aufs_permission(struct user_namespace *userns, struct inode *inode,
21754 +                          int mask)
21755 +{
21756 +       int err;
21757 +       aufs_bindex_t bindex, bbot;
21758 +       const unsigned char isdir = !!S_ISDIR(inode->i_mode),
21759 +               write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
21760 +       struct inode *h_inode;
21761 +       struct super_block *sb;
21762 +       struct au_branch *br;
21763 +
21764 +       /* todo: support rcu-walk? */
21765 +       if (mask & MAY_NOT_BLOCK)
21766 +               return -ECHILD;
21767 +
21768 +       sb = inode->i_sb;
21769 +       si_read_lock(sb, AuLock_FLUSH);
21770 +       ii_read_lock_child(inode);
21771 +#if 0 /* reserved for future use */
21772 +       /*
21773 +        * This test may be rather 'too much' since the test is essentially done
21774 +        * in the aufs_lookup().  Theoretically it is possible that the inode
21775 +        * generation doesn't match to the superblock's here.  But it isn't a
21776 +        * big deal I suppose.
21777 +        */
21778 +       err = au_iigen_test(inode, au_sigen(sb));
21779 +       if (unlikely(err))
21780 +               goto out;
21781 +#endif
21782 +
21783 +       if (!isdir
21784 +           || write_mask
21785 +           || au_opt_test(au_mntflags(sb), DIRPERM1)) {
21786 +               err = au_busy_or_stale();
21787 +               h_inode = au_h_iptr(inode, au_ibtop(inode));
21788 +               if (unlikely(!h_inode
21789 +                            || (h_inode->i_mode & S_IFMT)
21790 +                            != (inode->i_mode & S_IFMT)))
21791 +                       goto out;
21792 +
21793 +               err = 0;
21794 +               bindex = au_ibtop(inode);
21795 +               br = au_sbr(sb, bindex);
21796 +               err = h_permission(h_inode, mask, &br->br_path, br->br_perm);
21797 +               if (write_mask
21798 +                   && !err
21799 +                   && !special_file(h_inode->i_mode)) {
21800 +                       /* test whether the upper writable branch exists */
21801 +                       err = -EROFS;
21802 +                       for (; bindex >= 0; bindex--)
21803 +                               if (!au_br_rdonly(au_sbr(sb, bindex))) {
21804 +                                       err = 0;
21805 +                                       break;
21806 +                               }
21807 +               }
21808 +               goto out;
21809 +       }
21810 +
21811 +       /* non-write to dir */
21812 +       err = 0;
21813 +       bbot = au_ibbot(inode);
21814 +       for (bindex = au_ibtop(inode); !err && bindex <= bbot; bindex++) {
21815 +               h_inode = au_h_iptr(inode, bindex);
21816 +               if (h_inode) {
21817 +                       err = au_busy_or_stale();
21818 +                       if (unlikely(!S_ISDIR(h_inode->i_mode)))
21819 +                               break;
21820 +
21821 +                       br = au_sbr(sb, bindex);
21822 +                       err = h_permission(h_inode, mask, &br->br_path,
21823 +                                          br->br_perm);
21824 +               }
21825 +       }
21826 +
21827 +out:
21828 +       ii_read_unlock(inode);
21829 +       si_read_unlock(sb);
21830 +       return err;
21831 +}
21832 +
21833 +/* ---------------------------------------------------------------------- */
21834 +
21835 +static struct dentry *aufs_lookup(struct inode *dir, struct dentry *dentry,
21836 +                                 unsigned int flags)
21837 +{
21838 +       struct dentry *ret, *parent;
21839 +       struct inode *inode;
21840 +       struct super_block *sb;
21841 +       int err, npositive;
21842 +
21843 +       IMustLock(dir);
21844 +
21845 +       /* todo: support rcu-walk? */
21846 +       ret = ERR_PTR(-ECHILD);
21847 +       if (flags & LOOKUP_RCU)
21848 +               goto out;
21849 +
21850 +       ret = ERR_PTR(-ENAMETOOLONG);
21851 +       if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
21852 +               goto out;
21853 +
21854 +       sb = dir->i_sb;
21855 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
21856 +       ret = ERR_PTR(err);
21857 +       if (unlikely(err))
21858 +               goto out;
21859 +
21860 +       err = au_di_init(dentry);
21861 +       ret = ERR_PTR(err);
21862 +       if (unlikely(err))
21863 +               goto out_si;
21864 +
21865 +       inode = NULL;
21866 +       npositive = 0; /* suppress a warning */
21867 +       parent = dentry->d_parent; /* dir inode is locked */
21868 +       di_read_lock_parent(parent, AuLock_IR);
21869 +       err = au_alive_dir(parent);
21870 +       if (!err)
21871 +               err = au_digen_test(parent, au_sigen(sb));
21872 +       if (!err) {
21873 +               /* regardless LOOKUP_CREATE, always ALLOW_NEG */
21874 +               npositive = au_lkup_dentry(dentry, au_dbtop(parent),
21875 +                                          AuLkup_ALLOW_NEG);
21876 +               err = npositive;
21877 +       }
21878 +       di_read_unlock(parent, AuLock_IR);
21879 +       ret = ERR_PTR(err);
21880 +       if (unlikely(err < 0))
21881 +               goto out_unlock;
21882 +
21883 +       if (npositive) {
21884 +               inode = au_new_inode(dentry, /*must_new*/0);
21885 +               if (IS_ERR(inode)) {
21886 +                       ret = (void *)inode;
21887 +                       inode = NULL;
21888 +                       goto out_unlock;
21889 +               }
21890 +       }
21891 +
21892 +       if (inode)
21893 +               atomic_inc(&inode->i_count);
21894 +       ret = d_splice_alias(inode, dentry);
21895 +#if 0 /* reserved for future use */
21896 +       if (unlikely(d_need_lookup(dentry))) {
21897 +               spin_lock(&dentry->d_lock);
21898 +               dentry->d_flags &= ~DCACHE_NEED_LOOKUP;
21899 +               spin_unlock(&dentry->d_lock);
21900 +       } else
21901 +#endif
21902 +       if (inode) {
21903 +               if (!IS_ERR(ret)) {
21904 +                       iput(inode);
21905 +                       if (ret && ret != dentry)
21906 +                               ii_write_unlock(inode);
21907 +               } else {
21908 +                       ii_write_unlock(inode);
21909 +                       iput(inode);
21910 +                       inode = NULL;
21911 +               }
21912 +       }
21913 +
21914 +out_unlock:
21915 +       di_write_unlock(dentry);
21916 +out_si:
21917 +       si_read_unlock(sb);
21918 +out:
21919 +       return ret;
21920 +}
21921 +
21922 +/* ---------------------------------------------------------------------- */
21923 +
21924 +/*
21925 + * very dirty and complicated aufs ->atomic_open().
21926 + * aufs_atomic_open()
21927 + * + au_aopen_or_create()
21928 + *   + add_simple()
21929 + *     + vfsub_atomic_open()
21930 + *       + branch fs ->atomic_open()
21931 + *        may call the actual 'open' for h_file
21932 + *       + inc br_nfiles only if opened
21933 + * + au_aopen_no_open() or au_aopen_do_open()
21934 + *
21935 + * au_aopen_do_open()
21936 + * + finish_open()
21937 + *   + au_do_aopen()
21938 + *     + au_do_open() the body of all 'open'
21939 + *       + au_do_open_nondir()
21940 + *        set the passed h_file
21941 + *
21942 + * au_aopen_no_open()
21943 + * + finish_no_open()
21944 + */
21945 +
21946 +struct aopen_node {
21947 +       struct hlist_bl_node hblist;
21948 +       struct file *file, *h_file;
21949 +};
21950 +
21951 +static int au_do_aopen(struct inode *inode, struct file *file)
21952 +{
21953 +       struct hlist_bl_head *aopen;
21954 +       struct hlist_bl_node *pos;
21955 +       struct aopen_node *node;
21956 +       struct au_do_open_args args = {
21957 +               .aopen  = 1,
21958 +               .open   = au_do_open_nondir
21959 +       };
21960 +
21961 +       aopen = &au_sbi(inode->i_sb)->si_aopen;
21962 +       hlist_bl_lock(aopen);
21963 +       hlist_bl_for_each_entry(node, pos, aopen, hblist)
21964 +               if (node->file == file) {
21965 +                       args.h_file = node->h_file;
21966 +                       break;
21967 +               }
21968 +       hlist_bl_unlock(aopen);
21969 +       /* AuDebugOn(!args.h_file); */
21970 +
21971 +       return au_do_open(file, &args);
21972 +}
21973 +
21974 +static int au_aopen_do_open(struct file *file, struct dentry *dentry,
21975 +                           struct aopen_node *aopen_node)
21976 +{
21977 +       int err;
21978 +       struct hlist_bl_head *aopen;
21979 +
21980 +       AuLabel(here);
21981 +       aopen = &au_sbi(dentry->d_sb)->si_aopen;
21982 +       au_hbl_add(&aopen_node->hblist, aopen);
21983 +       err = finish_open(file, dentry, au_do_aopen);
21984 +       au_hbl_del(&aopen_node->hblist, aopen);
21985 +       /* AuDbgFile(file); */
21986 +       AuDbg("%pd%s%s\n", dentry,
21987 +             (file->f_mode & FMODE_CREATED) ? " created" : "",
21988 +             (file->f_mode & FMODE_OPENED) ? " opened" : "");
21989 +
21990 +       AuTraceErr(err);
21991 +       return err;
21992 +}
21993 +
21994 +static int au_aopen_no_open(struct file *file, struct dentry *dentry)
21995 +{
21996 +       int err;
21997 +
21998 +       AuLabel(here);
21999 +       dget(dentry);
22000 +       err = finish_no_open(file, dentry);
22001 +
22002 +       AuTraceErr(err);
22003 +       return err;
22004 +}
22005 +
22006 +static int aufs_atomic_open(struct inode *dir, struct dentry *dentry,
22007 +                           struct file *file, unsigned int open_flag,
22008 +                           umode_t create_mode)
22009 +{
22010 +       int err, did_open;
22011 +       unsigned int lkup_flags;
22012 +       aufs_bindex_t bindex;
22013 +       struct super_block *sb;
22014 +       struct dentry *parent, *d;
22015 +       struct vfsub_aopen_args args = {
22016 +               .open_flag      = open_flag,
22017 +               .create_mode    = create_mode
22018 +       };
22019 +       struct aopen_node aopen_node = {
22020 +               .file   = file
22021 +       };
22022 +
22023 +       IMustLock(dir);
22024 +       AuDbg("open_flag 0%o\n", open_flag);
22025 +       AuDbgDentry(dentry);
22026 +
22027 +       err = 0;
22028 +       if (!au_di(dentry)) {
22029 +               lkup_flags = LOOKUP_OPEN;
22030 +               if (open_flag & O_CREAT)
22031 +                       lkup_flags |= LOOKUP_CREATE;
22032 +               d = aufs_lookup(dir, dentry, lkup_flags);
22033 +               if (IS_ERR(d)) {
22034 +                       err = PTR_ERR(d);
22035 +                       AuTraceErr(err);
22036 +                       goto out;
22037 +               } else if (d) {
22038 +                       /*
22039 +                        * obsoleted dentry found.
22040 +                        * another error will be returned later.
22041 +                        */
22042 +                       d_drop(d);
22043 +                       AuDbgDentry(d);
22044 +                       dput(d);
22045 +               }
22046 +               AuDbgDentry(dentry);
22047 +       }
22048 +
22049 +       if (d_is_positive(dentry)
22050 +           || d_unhashed(dentry)
22051 +           || d_unlinked(dentry)
22052 +           || !(open_flag & O_CREAT)) {
22053 +               err = au_aopen_no_open(file, dentry);
22054 +               goto out; /* success */
22055 +       }
22056 +
22057 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
22058 +       if (unlikely(err))
22059 +               goto out;
22060 +
22061 +       sb = dentry->d_sb;
22062 +       parent = dentry->d_parent;      /* dir is locked */
22063 +       di_write_lock_parent(parent);
22064 +       err = au_lkup_dentry(dentry, /*btop*/0, AuLkup_ALLOW_NEG);
22065 +       if (unlikely(err < 0))
22066 +               goto out_parent;
22067 +
22068 +       AuDbgDentry(dentry);
22069 +       if (d_is_positive(dentry)) {
22070 +               err = au_aopen_no_open(file, dentry);
22071 +               goto out_parent; /* success */
22072 +       }
22073 +
22074 +       args.file = alloc_empty_file(file->f_flags, current_cred());
22075 +       err = PTR_ERR(args.file);
22076 +       if (IS_ERR(args.file))
22077 +               goto out_parent;
22078 +
22079 +       bindex = au_dbtop(dentry);
22080 +       err = au_aopen_or_create(dir, dentry, &args);
22081 +       AuTraceErr(err);
22082 +       AuDbgFile(args.file);
22083 +       file->f_mode = args.file->f_mode & ~FMODE_OPENED;
22084 +       did_open = !!(args.file->f_mode & FMODE_OPENED);
22085 +       if (!did_open) {
22086 +               fput(args.file);
22087 +               args.file = NULL;
22088 +       }
22089 +       di_write_unlock(parent);
22090 +       di_write_unlock(dentry);
22091 +       if (unlikely(err < 0)) {
22092 +               if (args.file)
22093 +                       fput(args.file);
22094 +               goto out_sb;
22095 +       }
22096 +
22097 +       if (!did_open)
22098 +               err = au_aopen_no_open(file, dentry);
22099 +       else {
22100 +               aopen_node.h_file = args.file;
22101 +               err = au_aopen_do_open(file, dentry, &aopen_node);
22102 +       }
22103 +       if (unlikely(err < 0)) {
22104 +               if (args.file)
22105 +                       fput(args.file);
22106 +               if (did_open)
22107 +                       au_lcnt_dec(&args.br->br_nfiles);
22108 +       }
22109 +       goto out_sb; /* success */
22110 +
22111 +out_parent:
22112 +       di_write_unlock(parent);
22113 +       di_write_unlock(dentry);
22114 +out_sb:
22115 +       si_read_unlock(sb);
22116 +out:
22117 +       AuTraceErr(err);
22118 +       AuDbgFile(file);
22119 +       return err;
22120 +}
22121 +
22122 +
22123 +/* ---------------------------------------------------------------------- */
22124 +
22125 +static int au_wr_dir_cpup(struct dentry *dentry, struct dentry *parent,
22126 +                         const unsigned char add_entry, aufs_bindex_t bcpup,
22127 +                         aufs_bindex_t btop)
22128 +{
22129 +       int err;
22130 +       struct dentry *h_parent;
22131 +       struct inode *h_dir;
22132 +
22133 +       if (add_entry)
22134 +               IMustLock(d_inode(parent));
22135 +       else
22136 +               di_write_lock_parent(parent);
22137 +
22138 +       err = 0;
22139 +       if (!au_h_dptr(parent, bcpup)) {
22140 +               if (btop > bcpup)
22141 +                       err = au_cpup_dirs(dentry, bcpup);
22142 +               else if (btop < bcpup)
22143 +                       err = au_cpdown_dirs(dentry, bcpup);
22144 +               else
22145 +                       BUG();
22146 +       }
22147 +       if (!err && add_entry && !au_ftest_wrdir(add_entry, TMPFILE)) {
22148 +               h_parent = au_h_dptr(parent, bcpup);
22149 +               h_dir = d_inode(h_parent);
22150 +               inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
22151 +               err = au_lkup_neg(dentry, bcpup, /*wh*/0);
22152 +               /* todo: no unlock here */
22153 +               inode_unlock_shared(h_dir);
22154 +
22155 +               AuDbg("bcpup %d\n", bcpup);
22156 +               if (!err) {
22157 +                       if (d_really_is_negative(dentry))
22158 +                               au_set_h_dptr(dentry, btop, NULL);
22159 +                       au_update_dbrange(dentry, /*do_put_zero*/0);
22160 +               }
22161 +       }
22162 +
22163 +       if (!add_entry)
22164 +               di_write_unlock(parent);
22165 +       if (!err)
22166 +               err = bcpup; /* success */
22167 +
22168 +       AuTraceErr(err);
22169 +       return err;
22170 +}
22171 +
22172 +/*
22173 + * decide the branch and the parent dir where we will create a new entry.
22174 + * returns new bindex or an error.
22175 + * copyup the parent dir if needed.
22176 + */
22177 +int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
22178 +             struct au_wr_dir_args *args)
22179 +{
22180 +       int err;
22181 +       unsigned int flags;
22182 +       aufs_bindex_t bcpup, btop, src_btop;
22183 +       const unsigned char add_entry
22184 +               = au_ftest_wrdir(args->flags, ADD_ENTRY)
22185 +               | au_ftest_wrdir(args->flags, TMPFILE);
22186 +       struct super_block *sb;
22187 +       struct dentry *parent;
22188 +       struct au_sbinfo *sbinfo;
22189 +
22190 +       sb = dentry->d_sb;
22191 +       sbinfo = au_sbi(sb);
22192 +       parent = dget_parent(dentry);
22193 +       btop = au_dbtop(dentry);
22194 +       bcpup = btop;
22195 +       if (args->force_btgt < 0) {
22196 +               if (src_dentry) {
22197 +                       src_btop = au_dbtop(src_dentry);
22198 +                       if (src_btop < btop)
22199 +                               bcpup = src_btop;
22200 +               } else if (add_entry) {
22201 +                       flags = 0;
22202 +                       if (au_ftest_wrdir(args->flags, ISDIR))
22203 +                               au_fset_wbr(flags, DIR);
22204 +                       err = AuWbrCreate(sbinfo, dentry, flags);
22205 +                       bcpup = err;
22206 +               }
22207 +
22208 +               if (bcpup < 0 || au_test_ro(sb, bcpup, d_inode(dentry))) {
22209 +                       if (add_entry)
22210 +                               err = AuWbrCopyup(sbinfo, dentry);
22211 +                       else {
22212 +                               if (!IS_ROOT(dentry)) {
22213 +                                       di_read_lock_parent(parent, !AuLock_IR);
22214 +                                       err = AuWbrCopyup(sbinfo, dentry);
22215 +                                       di_read_unlock(parent, !AuLock_IR);
22216 +                               } else
22217 +                                       err = AuWbrCopyup(sbinfo, dentry);
22218 +                       }
22219 +                       bcpup = err;
22220 +                       if (unlikely(err < 0))
22221 +                               goto out;
22222 +               }
22223 +       } else {
22224 +               bcpup = args->force_btgt;
22225 +               AuDebugOn(au_test_ro(sb, bcpup, d_inode(dentry)));
22226 +       }
22227 +
22228 +       AuDbg("btop %d, bcpup %d\n", btop, bcpup);
22229 +       err = bcpup;
22230 +       if (bcpup == btop)
22231 +               goto out; /* success */
22232 +
22233 +       /* copyup the new parent into the branch we process */
22234 +       err = au_wr_dir_cpup(dentry, parent, add_entry, bcpup, btop);
22235 +       if (err >= 0) {
22236 +               if (d_really_is_negative(dentry)) {
22237 +                       au_set_h_dptr(dentry, btop, NULL);
22238 +                       au_set_dbtop(dentry, bcpup);
22239 +                       au_set_dbbot(dentry, bcpup);
22240 +               }
22241 +               AuDebugOn(add_entry
22242 +                         && !au_ftest_wrdir(args->flags, TMPFILE)
22243 +                         && !au_h_dptr(dentry, bcpup));
22244 +       }
22245 +
22246 +out:
22247 +       dput(parent);
22248 +       return err;
22249 +}
22250 +
22251 +/* ---------------------------------------------------------------------- */
22252 +
22253 +void au_pin_hdir_unlock(struct au_pin *p)
22254 +{
22255 +       if (p->hdir)
22256 +               au_hn_inode_unlock(p->hdir);
22257 +}
22258 +
22259 +int au_pin_hdir_lock(struct au_pin *p)
22260 +{
22261 +       int err;
22262 +
22263 +       err = 0;
22264 +       if (!p->hdir)
22265 +               goto out;
22266 +
22267 +       /* even if an error happens later, keep this lock */
22268 +       au_hn_inode_lock_nested(p->hdir, p->lsc_hi);
22269 +
22270 +       err = -EBUSY;
22271 +       if (unlikely(p->hdir->hi_inode != d_inode(p->h_parent)))
22272 +               goto out;
22273 +
22274 +       err = 0;
22275 +       if (p->h_dentry)
22276 +               err = au_h_verify(p->h_dentry, p->udba, p->hdir->hi_inode,
22277 +                                 p->h_parent, p->br);
22278 +
22279 +out:
22280 +       return err;
22281 +}
22282 +
22283 +int au_pin_hdir_relock(struct au_pin *p)
22284 +{
22285 +       int err, i;
22286 +       struct inode *h_i;
22287 +       struct dentry *h_d[] = {
22288 +               p->h_dentry,
22289 +               p->h_parent
22290 +       };
22291 +
22292 +       err = au_pin_hdir_lock(p);
22293 +       if (unlikely(err))
22294 +               goto out;
22295 +
22296 +       for (i = 0; !err && i < sizeof(h_d)/sizeof(*h_d); i++) {
22297 +               if (!h_d[i])
22298 +                       continue;
22299 +               if (d_is_positive(h_d[i])) {
22300 +                       h_i = d_inode(h_d[i]);
22301 +                       err = !h_i->i_nlink;
22302 +               }
22303 +       }
22304 +
22305 +out:
22306 +       return err;
22307 +}
22308 +
22309 +static void au_pin_hdir_set_owner(struct au_pin *p, struct task_struct *task)
22310 +{
22311 +       atomic_long_set(&p->hdir->hi_inode->i_rwsem.owner, (long)task);
22312 +}
22313 +
22314 +void au_pin_hdir_acquire_nest(struct au_pin *p)
22315 +{
22316 +       if (p->hdir) {
22317 +               rwsem_acquire_nest(&p->hdir->hi_inode->i_rwsem.dep_map,
22318 +                                  p->lsc_hi, 0, NULL, _RET_IP_);
22319 +               au_pin_hdir_set_owner(p, current);
22320 +       }
22321 +}
22322 +
22323 +void au_pin_hdir_release(struct au_pin *p)
22324 +{
22325 +       if (p->hdir) {
22326 +               au_pin_hdir_set_owner(p, p->task);
22327 +               rwsem_release(&p->hdir->hi_inode->i_rwsem.dep_map, _RET_IP_);
22328 +       }
22329 +}
22330 +
22331 +struct dentry *au_pinned_h_parent(struct au_pin *pin)
22332 +{
22333 +       if (pin && pin->parent)
22334 +               return au_h_dptr(pin->parent, pin->bindex);
22335 +       return NULL;
22336 +}
22337 +
22338 +void au_unpin(struct au_pin *p)
22339 +{
22340 +       if (p->hdir)
22341 +               au_pin_hdir_unlock(p);
22342 +       if (p->h_mnt && au_ftest_pin(p->flags, MNT_WRITE))
22343 +               vfsub_mnt_drop_write(p->h_mnt);
22344 +       if (!p->hdir)
22345 +               return;
22346 +
22347 +       if (!au_ftest_pin(p->flags, DI_LOCKED))
22348 +               di_read_unlock(p->parent, AuLock_IR);
22349 +       iput(p->hdir->hi_inode);
22350 +       dput(p->parent);
22351 +       p->parent = NULL;
22352 +       p->hdir = NULL;
22353 +       p->h_mnt = NULL;
22354 +       /* do not clear p->task */
22355 +}
22356 +
22357 +int au_do_pin(struct au_pin *p)
22358 +{
22359 +       int err;
22360 +       struct super_block *sb;
22361 +       struct inode *h_dir;
22362 +
22363 +       err = 0;
22364 +       sb = p->dentry->d_sb;
22365 +       p->br = au_sbr(sb, p->bindex);
22366 +       if (IS_ROOT(p->dentry)) {
22367 +               if (au_ftest_pin(p->flags, MNT_WRITE)) {
22368 +                       p->h_mnt = au_br_mnt(p->br);
22369 +                       err = vfsub_mnt_want_write(p->h_mnt);
22370 +                       if (unlikely(err)) {
22371 +                               au_fclr_pin(p->flags, MNT_WRITE);
22372 +                               goto out_err;
22373 +                       }
22374 +               }
22375 +               goto out;
22376 +       }
22377 +
22378 +       p->h_dentry = NULL;
22379 +       if (p->bindex <= au_dbbot(p->dentry))
22380 +               p->h_dentry = au_h_dptr(p->dentry, p->bindex);
22381 +
22382 +       p->parent = dget_parent(p->dentry);
22383 +       if (!au_ftest_pin(p->flags, DI_LOCKED))
22384 +               di_read_lock(p->parent, AuLock_IR, p->lsc_di);
22385 +
22386 +       h_dir = NULL;
22387 +       p->h_parent = au_h_dptr(p->parent, p->bindex);
22388 +       p->hdir = au_hi(d_inode(p->parent), p->bindex);
22389 +       if (p->hdir)
22390 +               h_dir = p->hdir->hi_inode;
22391 +
22392 +       /*
22393 +        * udba case, or
22394 +        * if DI_LOCKED is not set, then p->parent may be different
22395 +        * and h_parent can be NULL.
22396 +        */
22397 +       if (unlikely(!p->hdir || !h_dir || !p->h_parent)) {
22398 +               err = -EBUSY;
22399 +               if (!au_ftest_pin(p->flags, DI_LOCKED))
22400 +                       di_read_unlock(p->parent, AuLock_IR);
22401 +               dput(p->parent);
22402 +               p->parent = NULL;
22403 +               goto out_err;
22404 +       }
22405 +
22406 +       if (au_ftest_pin(p->flags, MNT_WRITE)) {
22407 +               p->h_mnt = au_br_mnt(p->br);
22408 +               err = vfsub_mnt_want_write(p->h_mnt);
22409 +               if (unlikely(err)) {
22410 +                       au_fclr_pin(p->flags, MNT_WRITE);
22411 +                       if (!au_ftest_pin(p->flags, DI_LOCKED))
22412 +                               di_read_unlock(p->parent, AuLock_IR);
22413 +                       dput(p->parent);
22414 +                       p->parent = NULL;
22415 +                       goto out_err;
22416 +               }
22417 +       }
22418 +
22419 +       au_igrab(h_dir);
22420 +       err = au_pin_hdir_lock(p);
22421 +       if (!err)
22422 +               goto out; /* success */
22423 +
22424 +       au_unpin(p);
22425 +
22426 +out_err:
22427 +       pr_err("err %d\n", err);
22428 +       err = au_busy_or_stale();
22429 +out:
22430 +       return err;
22431 +}
22432 +
22433 +void au_pin_init(struct au_pin *p, struct dentry *dentry,
22434 +                aufs_bindex_t bindex, int lsc_di, int lsc_hi,
22435 +                unsigned int udba, unsigned char flags)
22436 +{
22437 +       p->dentry = dentry;
22438 +       p->udba = udba;
22439 +       p->lsc_di = lsc_di;
22440 +       p->lsc_hi = lsc_hi;
22441 +       p->flags = flags;
22442 +       p->bindex = bindex;
22443 +
22444 +       p->parent = NULL;
22445 +       p->hdir = NULL;
22446 +       p->h_mnt = NULL;
22447 +
22448 +       p->h_dentry = NULL;
22449 +       p->h_parent = NULL;
22450 +       p->br = NULL;
22451 +       p->task = current;
22452 +}
22453 +
22454 +int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
22455 +          unsigned int udba, unsigned char flags)
22456 +{
22457 +       au_pin_init(pin, dentry, bindex, AuLsc_DI_PARENT, AuLsc_I_PARENT2,
22458 +                   udba, flags);
22459 +       return au_do_pin(pin);
22460 +}
22461 +
22462 +/* ---------------------------------------------------------------------- */
22463 +
22464 +/*
22465 + * ->setattr() and ->getattr() are called in various cases.
22466 + * chmod, stat: dentry is revalidated.
22467 + * fchmod, fstat: file and dentry are not revalidated, additionally they may be
22468 + *               unhashed.
22469 + * for ->setattr(), ia->ia_file is passed from ftruncate only.
22470 + */
22471 +/* todo: consolidate with do_refresh() and simple_reval_dpath() */
22472 +int au_reval_for_attr(struct dentry *dentry, unsigned int sigen)
22473 +{
22474 +       int err;
22475 +       struct dentry *parent;
22476 +
22477 +       err = 0;
22478 +       if (au_digen_test(dentry, sigen)) {
22479 +               parent = dget_parent(dentry);
22480 +               di_read_lock_parent(parent, AuLock_IR);
22481 +               err = au_refresh_dentry(dentry, parent);
22482 +               di_read_unlock(parent, AuLock_IR);
22483 +               dput(parent);
22484 +       }
22485 +
22486 +       AuTraceErr(err);
22487 +       return err;
22488 +}
22489 +
22490 +int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
22491 +                    struct au_icpup_args *a)
22492 +{
22493 +       int err;
22494 +       loff_t sz;
22495 +       aufs_bindex_t btop, ibtop;
22496 +       struct dentry *hi_wh, *parent;
22497 +       struct inode *inode;
22498 +       struct au_wr_dir_args wr_dir_args = {
22499 +               .force_btgt     = -1,
22500 +               .flags          = 0
22501 +       };
22502 +
22503 +       if (d_is_dir(dentry))
22504 +               au_fset_wrdir(wr_dir_args.flags, ISDIR);
22505 +       /* plink or hi_wh() case */
22506 +       btop = au_dbtop(dentry);
22507 +       inode = d_inode(dentry);
22508 +       ibtop = au_ibtop(inode);
22509 +       if (btop != ibtop && !au_test_ro(inode->i_sb, ibtop, inode))
22510 +               wr_dir_args.force_btgt = ibtop;
22511 +       err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
22512 +       if (unlikely(err < 0))
22513 +               goto out;
22514 +       a->btgt = err;
22515 +       if (err != btop)
22516 +               au_fset_icpup(a->flags, DID_CPUP);
22517 +
22518 +       err = 0;
22519 +       a->pin_flags = AuPin_MNT_WRITE;
22520 +       parent = NULL;
22521 +       if (!IS_ROOT(dentry)) {
22522 +               au_fset_pin(a->pin_flags, DI_LOCKED);
22523 +               parent = dget_parent(dentry);
22524 +               di_write_lock_parent(parent);
22525 +       }
22526 +
22527 +       err = au_pin(&a->pin, dentry, a->btgt, a->udba, a->pin_flags);
22528 +       if (unlikely(err))
22529 +               goto out_parent;
22530 +
22531 +       sz = -1;
22532 +       a->h_path.dentry = au_h_dptr(dentry, btop);
22533 +       a->h_inode = d_inode(a->h_path.dentry);
22534 +       if (ia && (ia->ia_valid & ATTR_SIZE)) {
22535 +               inode_lock_shared_nested(a->h_inode, AuLsc_I_CHILD);
22536 +               if (ia->ia_size < i_size_read(a->h_inode))
22537 +                       sz = ia->ia_size;
22538 +               inode_unlock_shared(a->h_inode);
22539 +       }
22540 +
22541 +       hi_wh = NULL;
22542 +       if (au_ftest_icpup(a->flags, DID_CPUP) && d_unlinked(dentry)) {
22543 +               hi_wh = au_hi_wh(inode, a->btgt);
22544 +               if (!hi_wh) {
22545 +                       struct au_cp_generic cpg = {
22546 +                               .dentry = dentry,
22547 +                               .bdst   = a->btgt,
22548 +                               .bsrc   = -1,
22549 +                               .len    = sz,
22550 +                               .pin    = &a->pin
22551 +                       };
22552 +                       err = au_sio_cpup_wh(&cpg, /*file*/NULL);
22553 +                       if (unlikely(err))
22554 +                               goto out_unlock;
22555 +                       hi_wh = au_hi_wh(inode, a->btgt);
22556 +                       /* todo: revalidate hi_wh? */
22557 +               }
22558 +       }
22559 +
22560 +       if (parent) {
22561 +               au_pin_set_parent_lflag(&a->pin, /*lflag*/0);
22562 +               di_downgrade_lock(parent, AuLock_IR);
22563 +               dput(parent);
22564 +               parent = NULL;
22565 +       }
22566 +       if (!au_ftest_icpup(a->flags, DID_CPUP))
22567 +               goto out; /* success */
22568 +
22569 +       if (!d_unhashed(dentry)) {
22570 +               struct au_cp_generic cpg = {
22571 +                       .dentry = dentry,
22572 +                       .bdst   = a->btgt,
22573 +                       .bsrc   = btop,
22574 +                       .len    = sz,
22575 +                       .pin    = &a->pin,
22576 +                       .flags  = AuCpup_DTIME | AuCpup_HOPEN
22577 +               };
22578 +               err = au_sio_cpup_simple(&cpg);
22579 +               if (!err)
22580 +                       a->h_path.dentry = au_h_dptr(dentry, a->btgt);
22581 +       } else if (!hi_wh)
22582 +               a->h_path.dentry = au_h_dptr(dentry, a->btgt);
22583 +       else
22584 +               a->h_path.dentry = hi_wh; /* do not dget here */
22585 +
22586 +out_unlock:
22587 +       a->h_inode = d_inode(a->h_path.dentry);
22588 +       if (!err)
22589 +               goto out; /* success */
22590 +       au_unpin(&a->pin);
22591 +out_parent:
22592 +       if (parent) {
22593 +               di_write_unlock(parent);
22594 +               dput(parent);
22595 +       }
22596 +out:
22597 +       if (!err)
22598 +               inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
22599 +       return err;
22600 +}
22601 +
22602 +static int aufs_setattr(struct user_namespace *userns, struct dentry *dentry,
22603 +                       struct iattr *ia)
22604 +{
22605 +       int err;
22606 +       struct inode *inode, *delegated;
22607 +       struct super_block *sb;
22608 +       struct file *file;
22609 +       struct au_icpup_args *a;
22610 +       struct user_namespace *h_userns;
22611 +
22612 +       inode = d_inode(dentry);
22613 +       IMustLock(inode);
22614 +
22615 +       err = setattr_prepare(userns, dentry, ia);
22616 +       if (unlikely(err))
22617 +               goto out;
22618 +
22619 +       err = -ENOMEM;
22620 +       a = kzalloc(sizeof(*a), GFP_NOFS);
22621 +       if (unlikely(!a))
22622 +               goto out;
22623 +
22624 +       if (ia->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
22625 +               ia->ia_valid &= ~ATTR_MODE;
22626 +
22627 +       file = NULL;
22628 +       sb = dentry->d_sb;
22629 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
22630 +       if (unlikely(err))
22631 +               goto out_kfree;
22632 +
22633 +       if (ia->ia_valid & ATTR_FILE) {
22634 +               /* currently ftruncate(2) only */
22635 +               AuDebugOn(!d_is_reg(dentry));
22636 +               file = ia->ia_file;
22637 +               err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1,
22638 +                                           /*fi_lsc*/0);
22639 +               if (unlikely(err))
22640 +                       goto out_si;
22641 +               ia->ia_file = au_hf_top(file);
22642 +               a->udba = AuOpt_UDBA_NONE;
22643 +       } else {
22644 +               /* fchmod() doesn't pass ia_file */
22645 +               a->udba = au_opt_udba(sb);
22646 +               di_write_lock_child(dentry);
22647 +               /* no d_unlinked(), to set UDBA_NONE for root */
22648 +               if (d_unhashed(dentry))
22649 +                       a->udba = AuOpt_UDBA_NONE;
22650 +               if (a->udba != AuOpt_UDBA_NONE) {
22651 +                       AuDebugOn(IS_ROOT(dentry));
22652 +                       err = au_reval_for_attr(dentry, au_sigen(sb));
22653 +                       if (unlikely(err))
22654 +                               goto out_dentry;
22655 +               }
22656 +       }
22657 +
22658 +       err = au_pin_and_icpup(dentry, ia, a);
22659 +       if (unlikely(err < 0))
22660 +               goto out_dentry;
22661 +       if (au_ftest_icpup(a->flags, DID_CPUP)) {
22662 +               ia->ia_file = NULL;
22663 +               ia->ia_valid &= ~ATTR_FILE;
22664 +       }
22665 +
22666 +       a->h_path.mnt = au_sbr_mnt(sb, a->btgt);
22667 +       if ((ia->ia_valid & (ATTR_MODE | ATTR_CTIME))
22668 +           == (ATTR_MODE | ATTR_CTIME)) {
22669 +               err = security_path_chmod(&a->h_path, ia->ia_mode);
22670 +               if (unlikely(err))
22671 +                       goto out_unlock;
22672 +       } else if ((ia->ia_valid & (ATTR_UID | ATTR_GID))
22673 +                  && (ia->ia_valid & ATTR_CTIME)) {
22674 +               err = security_path_chown(&a->h_path, ia->ia_uid, ia->ia_gid);
22675 +               if (unlikely(err))
22676 +                       goto out_unlock;
22677 +       }
22678 +
22679 +       if (ia->ia_valid & ATTR_SIZE) {
22680 +               struct file *f;
22681 +
22682 +               if (ia->ia_size < i_size_read(inode))
22683 +                       /* unmap only */
22684 +                       truncate_setsize(inode, ia->ia_size);
22685 +
22686 +               f = NULL;
22687 +               if (ia->ia_valid & ATTR_FILE)
22688 +                       f = ia->ia_file;
22689 +               inode_unlock(a->h_inode);
22690 +               err = vfsub_trunc(&a->h_path, ia->ia_size, ia->ia_valid, f);
22691 +               inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
22692 +       } else {
22693 +               delegated = NULL;
22694 +               while (1) {
22695 +                       err = vfsub_notify_change(&a->h_path, ia, &delegated);
22696 +                       if (delegated) {
22697 +                               err = break_deleg_wait(&delegated);
22698 +                               if (!err)
22699 +                                       continue;
22700 +                       }
22701 +                       break;
22702 +               }
22703 +       }
22704 +       /*
22705 +        * regardless aufs 'acl' option setting.
22706 +        * why don't all acl-aware fs call this func from their ->setattr()?
22707 +        */
22708 +       if (!err && (ia->ia_valid & ATTR_MODE)) {
22709 +               h_userns = mnt_user_ns(a->h_path.mnt);
22710 +               err = vfsub_acl_chmod(h_userns, a->h_inode, ia->ia_mode);
22711 +       }
22712 +       if (!err)
22713 +               au_cpup_attr_changeable(inode);
22714 +
22715 +out_unlock:
22716 +       inode_unlock(a->h_inode);
22717 +       au_unpin(&a->pin);
22718 +       if (unlikely(err))
22719 +               au_update_dbtop(dentry);
22720 +out_dentry:
22721 +       di_write_unlock(dentry);
22722 +       if (file) {
22723 +               fi_write_unlock(file);
22724 +               ia->ia_file = file;
22725 +               ia->ia_valid |= ATTR_FILE;
22726 +       }
22727 +out_si:
22728 +       si_read_unlock(sb);
22729 +out_kfree:
22730 +       au_kfree_rcu(a);
22731 +out:
22732 +       AuTraceErr(err);
22733 +       return err;
22734 +}
22735 +
22736 +#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
22737 +static int au_h_path_to_set_attr(struct dentry *dentry,
22738 +                                struct au_icpup_args *a, struct path *h_path)
22739 +{
22740 +       int err;
22741 +       struct super_block *sb;
22742 +
22743 +       sb = dentry->d_sb;
22744 +       a->udba = au_opt_udba(sb);
22745 +       /* no d_unlinked(), to set UDBA_NONE for root */
22746 +       if (d_unhashed(dentry))
22747 +               a->udba = AuOpt_UDBA_NONE;
22748 +       if (a->udba != AuOpt_UDBA_NONE) {
22749 +               AuDebugOn(IS_ROOT(dentry));
22750 +               err = au_reval_for_attr(dentry, au_sigen(sb));
22751 +               if (unlikely(err))
22752 +                       goto out;
22753 +       }
22754 +       err = au_pin_and_icpup(dentry, /*ia*/NULL, a);
22755 +       if (unlikely(err < 0))
22756 +               goto out;
22757 +
22758 +       h_path->dentry = a->h_path.dentry;
22759 +       h_path->mnt = au_sbr_mnt(sb, a->btgt);
22760 +
22761 +out:
22762 +       return err;
22763 +}
22764 +
22765 +ssize_t au_sxattr(struct dentry *dentry, struct inode *inode,
22766 +                 struct au_sxattr *arg)
22767 +{
22768 +       int err;
22769 +       struct path h_path;
22770 +       struct super_block *sb;
22771 +       struct au_icpup_args *a;
22772 +       struct inode *h_inode;
22773 +       struct user_namespace *h_userns;
22774 +
22775 +       IMustLock(inode);
22776 +
22777 +       err = -ENOMEM;
22778 +       a = kzalloc(sizeof(*a), GFP_NOFS);
22779 +       if (unlikely(!a))
22780 +               goto out;
22781 +
22782 +       sb = dentry->d_sb;
22783 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
22784 +       if (unlikely(err))
22785 +               goto out_kfree;
22786 +
22787 +       h_path.dentry = NULL;   /* silence gcc */
22788 +       di_write_lock_child(dentry);
22789 +       err = au_h_path_to_set_attr(dentry, a, &h_path);
22790 +       if (unlikely(err))
22791 +               goto out_di;
22792 +       h_userns = mnt_user_ns(h_path.mnt);
22793 +
22794 +       inode_unlock(a->h_inode);
22795 +       switch (arg->type) {
22796 +       case AU_XATTR_SET:
22797 +               AuDebugOn(d_is_negative(h_path.dentry));
22798 +               err = vfsub_setxattr(h_userns, h_path.dentry,
22799 +                                    arg->u.set.name, arg->u.set.value,
22800 +                                    arg->u.set.size, arg->u.set.flags);
22801 +               break;
22802 +       case AU_ACL_SET:
22803 +               err = -EOPNOTSUPP;
22804 +               h_inode = d_inode(h_path.dentry);
22805 +               if (h_inode->i_op->set_acl) {
22806 +                       /* this will call posix_acl_update_mode */
22807 +                       err = h_inode->i_op->set_acl(h_userns, h_inode,
22808 +                                                    arg->u.acl_set.acl,
22809 +                                                    arg->u.acl_set.type);
22810 +               }
22811 +               break;
22812 +       }
22813 +       if (!err)
22814 +               au_cpup_attr_timesizes(inode);
22815 +
22816 +       au_unpin(&a->pin);
22817 +       if (unlikely(err))
22818 +               au_update_dbtop(dentry);
22819 +
22820 +out_di:
22821 +       di_write_unlock(dentry);
22822 +       si_read_unlock(sb);
22823 +out_kfree:
22824 +       au_kfree_rcu(a);
22825 +out:
22826 +       AuTraceErr(err);
22827 +       return err;
22828 +}
22829 +#endif
22830 +
22831 +static void au_refresh_iattr(struct inode *inode, struct kstat *st,
22832 +                            unsigned int nlink)
22833 +{
22834 +       unsigned int n;
22835 +
22836 +       inode->i_mode = st->mode;
22837 +       /* don't i_[ug]id_write() here */
22838 +       inode->i_uid = st->uid;
22839 +       inode->i_gid = st->gid;
22840 +       inode->i_atime = st->atime;
22841 +       inode->i_mtime = st->mtime;
22842 +       inode->i_ctime = st->ctime;
22843 +
22844 +       au_cpup_attr_nlink(inode, /*force*/0);
22845 +       if (S_ISDIR(inode->i_mode)) {
22846 +               n = inode->i_nlink;
22847 +               n -= nlink;
22848 +               n += st->nlink;
22849 +               smp_mb(); /* for i_nlink */
22850 +               /* 0 can happen */
22851 +               set_nlink(inode, n);
22852 +       }
22853 +
22854 +       spin_lock(&inode->i_lock);
22855 +       inode->i_blocks = st->blocks;
22856 +       i_size_write(inode, st->size);
22857 +       spin_unlock(&inode->i_lock);
22858 +}
22859 +
22860 +/*
22861 + * common routine for aufs_getattr() and au_getxattr().
22862 + * returns zero or negative (an error).
22863 + * @dentry will be read-locked in success.
22864 + */
22865 +int au_h_path_getattr(struct dentry *dentry, struct inode *inode, int force,
22866 +                     struct path *h_path, int locked)
22867 +{
22868 +       int err;
22869 +       unsigned int mnt_flags, sigen;
22870 +       unsigned char udba_none;
22871 +       aufs_bindex_t bindex;
22872 +       struct super_block *sb, *h_sb;
22873 +
22874 +       h_path->mnt = NULL;
22875 +       h_path->dentry = NULL;
22876 +
22877 +       err = 0;
22878 +       sb = dentry->d_sb;
22879 +       mnt_flags = au_mntflags(sb);
22880 +       udba_none = !!au_opt_test(mnt_flags, UDBA_NONE);
22881 +
22882 +       if (unlikely(locked))
22883 +               goto body; /* skip locking dinfo */
22884 +
22885 +       /* support fstat(2) */
22886 +       if (!d_unlinked(dentry) && !udba_none) {
22887 +               sigen = au_sigen(sb);
22888 +               err = au_digen_test(dentry, sigen);
22889 +               if (!err) {
22890 +                       di_read_lock_child(dentry, AuLock_IR);
22891 +                       err = au_dbrange_test(dentry);
22892 +                       if (unlikely(err)) {
22893 +                               di_read_unlock(dentry, AuLock_IR);
22894 +                               goto out;
22895 +                       }
22896 +               } else {
22897 +                       AuDebugOn(IS_ROOT(dentry));
22898 +                       di_write_lock_child(dentry);
22899 +                       err = au_dbrange_test(dentry);
22900 +                       if (!err)
22901 +                               err = au_reval_for_attr(dentry, sigen);
22902 +                       if (!err)
22903 +                               di_downgrade_lock(dentry, AuLock_IR);
22904 +                       else {
22905 +                               di_write_unlock(dentry);
22906 +                               goto out;
22907 +                       }
22908 +               }
22909 +       } else
22910 +               di_read_lock_child(dentry, AuLock_IR);
22911 +
22912 +body:
22913 +       if (!inode) {
22914 +               inode = d_inode(dentry);
22915 +               if (unlikely(!inode))
22916 +                       goto out;
22917 +       }
22918 +       bindex = au_ibtop(inode);
22919 +       h_path->mnt = au_sbr_mnt(sb, bindex);
22920 +       h_sb = h_path->mnt->mnt_sb;
22921 +       if (!force
22922 +           && !au_test_fs_bad_iattr(h_sb)
22923 +           && udba_none)
22924 +               goto out; /* success */
22925 +
22926 +       if (au_dbtop(dentry) == bindex)
22927 +               h_path->dentry = au_h_dptr(dentry, bindex);
22928 +       else if (au_opt_test(mnt_flags, PLINK) && au_plink_test(inode)) {
22929 +               h_path->dentry = au_plink_lkup(inode, bindex);
22930 +               if (IS_ERR(h_path->dentry))
22931 +                       /* pretending success */
22932 +                       h_path->dentry = NULL;
22933 +               else
22934 +                       dput(h_path->dentry);
22935 +       }
22936 +
22937 +out:
22938 +       return err;
22939 +}
22940 +
22941 +static int aufs_getattr(struct user_namespace *userns, const struct path *path,
22942 +                       struct kstat *st, u32 request, unsigned int query)
22943 +{
22944 +       int err;
22945 +       unsigned char positive;
22946 +       struct path h_path;
22947 +       struct dentry *dentry;
22948 +       struct inode *inode;
22949 +       struct super_block *sb;
22950 +
22951 +       dentry = path->dentry;
22952 +       inode = d_inode(dentry);
22953 +       sb = dentry->d_sb;
22954 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
22955 +       if (unlikely(err))
22956 +               goto out;
22957 +       err = au_h_path_getattr(dentry, /*inode*/NULL, /*force*/0, &h_path,
22958 +                               /*locked*/0);
22959 +       if (unlikely(err))
22960 +               goto out_si;
22961 +       if (unlikely(!h_path.dentry))
22962 +               /* illegally overlapped or something */
22963 +               goto out_fill; /* pretending success */
22964 +
22965 +       positive = d_is_positive(h_path.dentry);
22966 +       if (positive)
22967 +               /* no vfsub version */
22968 +               err = vfs_getattr(&h_path, st, request, query);
22969 +       if (!err) {
22970 +               if (positive)
22971 +                       au_refresh_iattr(inode, st,
22972 +                                        d_inode(h_path.dentry)->i_nlink);
22973 +               goto out_fill; /* success */
22974 +       }
22975 +       AuTraceErr(err);
22976 +       goto out_di;
22977 +
22978 +out_fill:
22979 +       generic_fillattr(userns, inode, st);
22980 +out_di:
22981 +       di_read_unlock(dentry, AuLock_IR);
22982 +out_si:
22983 +       si_read_unlock(sb);
22984 +out:
22985 +       AuTraceErr(err);
22986 +       return err;
22987 +}
22988 +
22989 +/* ---------------------------------------------------------------------- */
22990 +
22991 +static const char *aufs_get_link(struct dentry *dentry, struct inode *inode,
22992 +                                struct delayed_call *done)
22993 +{
22994 +       const char *ret;
22995 +       struct dentry *h_dentry;
22996 +       struct inode *h_inode;
22997 +       int err;
22998 +       aufs_bindex_t bindex;
22999 +
23000 +       ret = NULL; /* suppress a warning */
23001 +       err = -ECHILD;
23002 +       if (!dentry)
23003 +               goto out;
23004 +
23005 +       err = aufs_read_lock(dentry, AuLock_IR | AuLock_GEN);
23006 +       if (unlikely(err))
23007 +               goto out;
23008 +
23009 +       err = au_d_hashed_positive(dentry);
23010 +       if (unlikely(err))
23011 +               goto out_unlock;
23012 +
23013 +       err = -EINVAL;
23014 +       inode = d_inode(dentry);
23015 +       bindex = au_ibtop(inode);
23016 +       h_inode = au_h_iptr(inode, bindex);
23017 +       if (unlikely(!h_inode->i_op->get_link))
23018 +               goto out_unlock;
23019 +
23020 +       err = -EBUSY;
23021 +       h_dentry = NULL;
23022 +       if (au_dbtop(dentry) <= bindex) {
23023 +               h_dentry = au_h_dptr(dentry, bindex);
23024 +               if (h_dentry)
23025 +                       dget(h_dentry);
23026 +       }
23027 +       if (!h_dentry) {
23028 +               h_dentry = d_find_any_alias(h_inode);
23029 +               if (IS_ERR(h_dentry)) {
23030 +                       err = PTR_ERR(h_dentry);
23031 +                       goto out_unlock;
23032 +               }
23033 +       }
23034 +       if (unlikely(!h_dentry))
23035 +               goto out_unlock;
23036 +
23037 +       err = 0;
23038 +       AuDbg("%ps\n", h_inode->i_op->get_link);
23039 +       AuDbgDentry(h_dentry);
23040 +       ret = vfs_get_link(h_dentry, done);
23041 +       dput(h_dentry);
23042 +       if (IS_ERR(ret))
23043 +               err = PTR_ERR(ret);
23044 +
23045 +out_unlock:
23046 +       aufs_read_unlock(dentry, AuLock_IR);
23047 +out:
23048 +       if (unlikely(err))
23049 +               ret = ERR_PTR(err);
23050 +       AuTraceErrPtr(ret);
23051 +       return ret;
23052 +}
23053 +
23054 +/* ---------------------------------------------------------------------- */
23055 +
23056 +static int au_is_special(struct inode *inode)
23057 +{
23058 +       return (inode->i_mode & (S_IFBLK | S_IFCHR | S_IFIFO | S_IFSOCK));
23059 +}
23060 +
23061 +static int aufs_update_time(struct inode *inode, struct timespec64 *ts,
23062 +                           int flags)
23063 +{
23064 +       int err;
23065 +       aufs_bindex_t bindex;
23066 +       struct super_block *sb;
23067 +       struct inode *h_inode;
23068 +       struct vfsmount *h_mnt;
23069 +
23070 +       sb = inode->i_sb;
23071 +       WARN_ONCE((flags & S_ATIME) && !IS_NOATIME(inode),
23072 +                 "unexpected s_flags 0x%lx", sb->s_flags);
23073 +
23074 +       /* mmap_sem might be acquired already, cf. aufs_mmap() */
23075 +       lockdep_off();
23076 +       si_read_lock(sb, AuLock_FLUSH);
23077 +       ii_write_lock_child(inode);
23078 +
23079 +       err = 0;
23080 +       bindex = au_ibtop(inode);
23081 +       h_inode = au_h_iptr(inode, bindex);
23082 +       if (!au_test_ro(sb, bindex, inode)) {
23083 +               h_mnt = au_sbr_mnt(sb, bindex);
23084 +               err = vfsub_mnt_want_write(h_mnt);
23085 +               if (!err) {
23086 +                       err = vfsub_update_time(h_inode, ts, flags);
23087 +                       vfsub_mnt_drop_write(h_mnt);
23088 +               }
23089 +       } else if (au_is_special(h_inode)) {
23090 +               /*
23091 +                * Never copy-up here.
23092 +                * These special files may already be opened and used for
23093 +                * communicating. If we copied it up, then the communication
23094 +                * would be corrupted.
23095 +                */
23096 +               AuWarn1("timestamps for i%lu are ignored "
23097 +                       "since it is on readonly branch (hi%lu).\n",
23098 +                       inode->i_ino, h_inode->i_ino);
23099 +       } else if (flags & ~S_ATIME) {
23100 +               err = -EIO;
23101 +               AuIOErr1("unexpected flags 0x%x\n", flags);
23102 +               AuDebugOn(1);
23103 +       }
23104 +
23105 +       if (!err)
23106 +               au_cpup_attr_timesizes(inode);
23107 +       ii_write_unlock(inode);
23108 +       si_read_unlock(sb);
23109 +       lockdep_on();
23110 +
23111 +       if (!err && (flags & S_VERSION))
23112 +               inode_inc_iversion(inode);
23113 +
23114 +       return err;
23115 +}
23116 +
23117 +/* ---------------------------------------------------------------------- */
23118 +
23119 +/* no getattr version will be set by module.c:aufs_init() */
23120 +struct inode_operations aufs_iop_nogetattr[AuIop_Last],
23121 +       aufs_iop[] = {
23122 +       [AuIop_SYMLINK] = {
23123 +               .permission     = aufs_permission,
23124 +#ifdef CONFIG_FS_POSIX_ACL
23125 +               .get_acl        = aufs_get_acl,
23126 +               .set_acl        = aufs_set_acl, /* unsupport for symlink? */
23127 +#endif
23128 +
23129 +               .setattr        = aufs_setattr,
23130 +               .getattr        = aufs_getattr,
23131 +
23132 +#ifdef CONFIG_AUFS_XATTR
23133 +               .listxattr      = aufs_listxattr,
23134 +#endif
23135 +
23136 +               .get_link       = aufs_get_link,
23137 +
23138 +               /* .update_time = aufs_update_time */
23139 +       },
23140 +       [AuIop_DIR] = {
23141 +               .create         = aufs_create,
23142 +               .lookup         = aufs_lookup,
23143 +               .link           = aufs_link,
23144 +               .unlink         = aufs_unlink,
23145 +               .symlink        = aufs_symlink,
23146 +               .mkdir          = aufs_mkdir,
23147 +               .rmdir          = aufs_rmdir,
23148 +               .mknod          = aufs_mknod,
23149 +               .rename         = aufs_rename,
23150 +
23151 +               .permission     = aufs_permission,
23152 +#ifdef CONFIG_FS_POSIX_ACL
23153 +               .get_acl        = aufs_get_acl,
23154 +               .set_acl        = aufs_set_acl,
23155 +#endif
23156 +
23157 +               .setattr        = aufs_setattr,
23158 +               .getattr        = aufs_getattr,
23159 +
23160 +#ifdef CONFIG_AUFS_XATTR
23161 +               .listxattr      = aufs_listxattr,
23162 +#endif
23163 +
23164 +               .update_time    = aufs_update_time,
23165 +               .atomic_open    = aufs_atomic_open,
23166 +               .tmpfile        = aufs_tmpfile
23167 +       },
23168 +       [AuIop_OTHER] = {
23169 +               .permission     = aufs_permission,
23170 +#ifdef CONFIG_FS_POSIX_ACL
23171 +               .get_acl        = aufs_get_acl,
23172 +               .set_acl        = aufs_set_acl,
23173 +#endif
23174 +
23175 +               .setattr        = aufs_setattr,
23176 +               .getattr        = aufs_getattr,
23177 +
23178 +#ifdef CONFIG_AUFS_XATTR
23179 +               .listxattr      = aufs_listxattr,
23180 +#endif
23181 +
23182 +               .update_time    = aufs_update_time
23183 +       }
23184 +};
23185 diff -urN /usr/share/empty/fs/aufs/i_op_del.c linux/fs/aufs/i_op_del.c
23186 --- /usr/share/empty/fs/aufs/i_op_del.c 1970-01-01 01:00:00.000000000 +0100
23187 +++ linux/fs/aufs/i_op_del.c    2022-03-21 14:49:05.726633010 +0100
23188 @@ -0,0 +1,522 @@
23189 +// SPDX-License-Identifier: GPL-2.0
23190 +/*
23191 + * Copyright (C) 2005-2021 Junjiro R. Okajima
23192 + *
23193 + * This program, aufs is free software; you can redistribute it and/or modify
23194 + * it under the terms of the GNU General Public License as published by
23195 + * the Free Software Foundation; either version 2 of the License, or
23196 + * (at your option) any later version.
23197 + *
23198 + * This program is distributed in the hope that it will be useful,
23199 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
23200 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23201 + * GNU General Public License for more details.
23202 + *
23203 + * You should have received a copy of the GNU General Public License
23204 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23205 + */
23206 +
23207 +/*
23208 + * inode operations (del entry)
23209 + */
23210 +
23211 +#include <linux/iversion.h>
23212 +#include "aufs.h"
23213 +
23214 +/*
23215 + * decide if a new whiteout for @dentry is necessary or not.
23216 + * when it is necessary, prepare the parent dir for the upper branch whose
23217 + * branch index is @bcpup for creation. the actual creation of the whiteout will
23218 + * be done by caller.
23219 + * return value:
23220 + * 0: wh is unnecessary
23221 + * plus: wh is necessary
23222 + * minus: error
23223 + */
23224 +int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup)
23225 +{
23226 +       int need_wh, err;
23227 +       aufs_bindex_t btop;
23228 +       struct super_block *sb;
23229 +
23230 +       sb = dentry->d_sb;
23231 +       btop = au_dbtop(dentry);
23232 +       if (*bcpup < 0) {
23233 +               *bcpup = btop;
23234 +               if (au_test_ro(sb, btop, d_inode(dentry))) {
23235 +                       err = AuWbrCopyup(au_sbi(sb), dentry);
23236 +                       *bcpup = err;
23237 +                       if (unlikely(err < 0))
23238 +                               goto out;
23239 +               }
23240 +       } else
23241 +               AuDebugOn(btop < *bcpup
23242 +                         || au_test_ro(sb, *bcpup, d_inode(dentry)));
23243 +       AuDbg("bcpup %d, btop %d\n", *bcpup, btop);
23244 +
23245 +       if (*bcpup != btop) {
23246 +               err = au_cpup_dirs(dentry, *bcpup);
23247 +               if (unlikely(err))
23248 +                       goto out;
23249 +               need_wh = 1;
23250 +       } else {
23251 +               struct au_dinfo *dinfo, *tmp;
23252 +
23253 +               need_wh = -ENOMEM;
23254 +               dinfo = au_di(dentry);
23255 +               tmp = au_di_alloc(sb, AuLsc_DI_TMP);
23256 +               if (tmp) {
23257 +                       au_di_cp(tmp, dinfo);
23258 +                       au_di_swap(tmp, dinfo);
23259 +                       /* returns the number of positive dentries */
23260 +                       need_wh = au_lkup_dentry(dentry, btop + 1,
23261 +                                                /* AuLkup_IGNORE_PERM */ 0);
23262 +                       au_di_swap(tmp, dinfo);
23263 +                       au_rw_write_unlock(&tmp->di_rwsem);
23264 +                       au_di_free(tmp);
23265 +               }
23266 +       }
23267 +       AuDbg("need_wh %d\n", need_wh);
23268 +       err = need_wh;
23269 +
23270 +out:
23271 +       return err;
23272 +}
23273 +
23274 +/*
23275 + * simple tests for the del-entry operations.
23276 + * following the checks in vfs, plus the parent-child relationship.
23277 + */
23278 +int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
23279 +              struct dentry *h_parent, int isdir)
23280 +{
23281 +       int err;
23282 +       umode_t h_mode;
23283 +       struct dentry *h_dentry, *h_latest;
23284 +       struct inode *h_inode;
23285 +       struct path h_ppath;
23286 +       struct super_block *sb;
23287 +       struct au_branch *br;
23288 +       struct user_namespace *h_userns;
23289 +
23290 +       h_dentry = au_h_dptr(dentry, bindex);
23291 +       if (d_really_is_positive(dentry)) {
23292 +               err = -ENOENT;
23293 +               if (unlikely(d_is_negative(h_dentry)))
23294 +                       goto out;
23295 +               h_inode = d_inode(h_dentry);
23296 +               if (unlikely(!h_inode->i_nlink))
23297 +                       goto out;
23298 +
23299 +               h_mode = h_inode->i_mode;
23300 +               if (!isdir) {
23301 +                       err = -EISDIR;
23302 +                       if (unlikely(S_ISDIR(h_mode)))
23303 +                               goto out;
23304 +               } else if (unlikely(!S_ISDIR(h_mode))) {
23305 +                       err = -ENOTDIR;
23306 +                       goto out;
23307 +               }
23308 +       } else {
23309 +               /* rename(2) case */
23310 +               err = -EIO;
23311 +               if (unlikely(d_is_positive(h_dentry)))
23312 +                       goto out;
23313 +       }
23314 +
23315 +       err = -ENOENT;
23316 +       /* expected parent dir is locked */
23317 +       if (unlikely(h_parent != h_dentry->d_parent))
23318 +               goto out;
23319 +       err = 0;
23320 +
23321 +       /*
23322 +        * rmdir a dir may break the consistency on some filesystem.
23323 +        * let's try heavy test.
23324 +        */
23325 +       err = -EACCES;
23326 +       sb = dentry->d_sb;
23327 +       br = au_sbr(sb, bindex);
23328 +       h_userns = au_br_userns(br);
23329 +       if (unlikely(!au_opt_test(au_mntflags(sb), DIRPERM1)
23330 +                    && au_test_h_perm(h_userns, d_inode(h_parent),
23331 +                                      MAY_EXEC | MAY_WRITE)))
23332 +               goto out;
23333 +
23334 +       h_ppath.dentry = h_parent;
23335 +       h_ppath.mnt = au_br_mnt(br);
23336 +       h_latest = au_sio_lkup_one(h_userns, &dentry->d_name, &h_ppath);
23337 +       err = -EIO;
23338 +       if (IS_ERR(h_latest))
23339 +               goto out;
23340 +       if (h_latest == h_dentry)
23341 +               err = 0;
23342 +       dput(h_latest);
23343 +
23344 +out:
23345 +       return err;
23346 +}
23347 +
23348 +/*
23349 + * decide the branch where we operate for @dentry. the branch index will be set
23350 + * @rbcpup. after deciding it, 'pin' it and store the timestamps of the parent
23351 + * dir for reverting.
23352 + * when a new whiteout is necessary, create it.
23353 + */
23354 +static struct dentry*
23355 +lock_hdir_create_wh(struct dentry *dentry, int isdir, aufs_bindex_t *rbcpup,
23356 +                   struct au_dtime *dt, struct au_pin *pin)
23357 +{
23358 +       struct dentry *wh_dentry;
23359 +       struct super_block *sb;
23360 +       struct path h_path;
23361 +       int err, need_wh;
23362 +       unsigned int udba;
23363 +       aufs_bindex_t bcpup;
23364 +
23365 +       need_wh = au_wr_dir_need_wh(dentry, isdir, rbcpup);
23366 +       wh_dentry = ERR_PTR(need_wh);
23367 +       if (unlikely(need_wh < 0))
23368 +               goto out;
23369 +
23370 +       sb = dentry->d_sb;
23371 +       udba = au_opt_udba(sb);
23372 +       bcpup = *rbcpup;
23373 +       err = au_pin(pin, dentry, bcpup, udba,
23374 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
23375 +       wh_dentry = ERR_PTR(err);
23376 +       if (unlikely(err))
23377 +               goto out;
23378 +
23379 +       h_path.dentry = au_pinned_h_parent(pin);
23380 +       if (udba != AuOpt_UDBA_NONE
23381 +           && au_dbtop(dentry) == bcpup) {
23382 +               err = au_may_del(dentry, bcpup, h_path.dentry, isdir);
23383 +               wh_dentry = ERR_PTR(err);
23384 +               if (unlikely(err))
23385 +                       goto out_unpin;
23386 +       }
23387 +
23388 +       h_path.mnt = au_sbr_mnt(sb, bcpup);
23389 +       au_dtime_store(dt, au_pinned_parent(pin), &h_path);
23390 +       wh_dentry = NULL;
23391 +       if (!need_wh)
23392 +               goto out; /* success, no need to create whiteout */
23393 +
23394 +       wh_dentry = au_wh_create(dentry, bcpup, h_path.dentry);
23395 +       if (IS_ERR(wh_dentry))
23396 +               goto out_unpin;
23397 +
23398 +       /* returns with the parent is locked and wh_dentry is dget-ed */
23399 +       goto out; /* success */
23400 +
23401 +out_unpin:
23402 +       au_unpin(pin);
23403 +out:
23404 +       return wh_dentry;
23405 +}
23406 +
23407 +/*
23408 + * when removing a dir, rename it to a unique temporary whiteout-ed name first
23409 + * in order to be revertible and save time for removing many child whiteouts
23410 + * under the dir.
23411 + * returns 1 when there are too many child whiteout and caller should remove
23412 + * them asynchronously. returns 0 when the number of children is enough small to
23413 + * remove now or the branch fs is a remote fs.
23414 + * otherwise return an error.
23415 + */
23416 +static int renwh_and_rmdir(struct dentry *dentry, aufs_bindex_t bindex,
23417 +                          struct au_nhash *whlist, struct inode *dir)
23418 +{
23419 +       int rmdir_later, err, dirwh;
23420 +       struct dentry *h_dentry;
23421 +       struct super_block *sb;
23422 +       struct inode *inode;
23423 +
23424 +       sb = dentry->d_sb;
23425 +       SiMustAnyLock(sb);
23426 +       h_dentry = au_h_dptr(dentry, bindex);
23427 +       err = au_whtmp_ren(h_dentry, au_sbr(sb, bindex));
23428 +       if (unlikely(err))
23429 +               goto out;
23430 +
23431 +       /* stop monitoring */
23432 +       inode = d_inode(dentry);
23433 +       au_hn_free(au_hi(inode, bindex));
23434 +
23435 +       if (!au_test_fs_remote(h_dentry->d_sb)) {
23436 +               dirwh = au_sbi(sb)->si_dirwh;
23437 +               rmdir_later = (dirwh <= 1);
23438 +               if (!rmdir_later)
23439 +                       rmdir_later = au_nhash_test_longer_wh(whlist, bindex,
23440 +                                                             dirwh);
23441 +               if (rmdir_later)
23442 +                       return rmdir_later;
23443 +       }
23444 +
23445 +       err = au_whtmp_rmdir(dir, bindex, h_dentry, whlist);
23446 +       if (unlikely(err)) {
23447 +               AuIOErr("rmdir %pd, b%d failed, %d. ignored\n",
23448 +                       h_dentry, bindex, err);
23449 +               err = 0;
23450 +       }
23451 +
23452 +out:
23453 +       AuTraceErr(err);
23454 +       return err;
23455 +}
23456 +
23457 +/*
23458 + * final procedure for deleting a entry.
23459 + * maintain dentry and iattr.
23460 + */
23461 +static void epilog(struct inode *dir, struct dentry *dentry,
23462 +                  aufs_bindex_t bindex)
23463 +{
23464 +       struct inode *inode;
23465 +
23466 +       inode = d_inode(dentry);
23467 +       d_drop(dentry);
23468 +       inode->i_ctime = dir->i_ctime;
23469 +
23470 +       au_dir_ts(dir, bindex);
23471 +       inode_inc_iversion(dir);
23472 +}
23473 +
23474 +/*
23475 + * when an error happened, remove the created whiteout and revert everything.
23476 + */
23477 +static int do_revert(int err, struct inode *dir, aufs_bindex_t bindex,
23478 +                    aufs_bindex_t bwh, struct dentry *wh_dentry,
23479 +                    struct dentry *dentry, struct au_dtime *dt)
23480 +{
23481 +       int rerr;
23482 +       struct path h_path = {
23483 +               .dentry = wh_dentry,
23484 +               .mnt    = au_sbr_mnt(dir->i_sb, bindex)
23485 +       };
23486 +
23487 +       rerr = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path, dentry);
23488 +       if (!rerr) {
23489 +               au_set_dbwh(dentry, bwh);
23490 +               au_dtime_revert(dt);
23491 +               return 0;
23492 +       }
23493 +
23494 +       AuIOErr("%pd reverting whiteout failed(%d, %d)\n", dentry, err, rerr);
23495 +       return -EIO;
23496 +}
23497 +
23498 +/* ---------------------------------------------------------------------- */
23499 +
23500 +int aufs_unlink(struct inode *dir, struct dentry *dentry)
23501 +{
23502 +       int err;
23503 +       aufs_bindex_t bwh, bindex, btop;
23504 +       struct inode *inode, *h_dir, *delegated;
23505 +       struct dentry *parent, *wh_dentry;
23506 +       /* to reduce stack size */
23507 +       struct {
23508 +               struct au_dtime dt;
23509 +               struct au_pin pin;
23510 +               struct path h_path;
23511 +       } *a;
23512 +
23513 +       IMustLock(dir);
23514 +
23515 +       err = -ENOMEM;
23516 +       a = kmalloc(sizeof(*a), GFP_NOFS);
23517 +       if (unlikely(!a))
23518 +               goto out;
23519 +
23520 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
23521 +       if (unlikely(err))
23522 +               goto out_free;
23523 +       err = au_d_hashed_positive(dentry);
23524 +       if (unlikely(err))
23525 +               goto out_unlock;
23526 +       inode = d_inode(dentry);
23527 +       IMustLock(inode);
23528 +       err = -EISDIR;
23529 +       if (unlikely(d_is_dir(dentry)))
23530 +               goto out_unlock; /* possible? */
23531 +
23532 +       btop = au_dbtop(dentry);
23533 +       bwh = au_dbwh(dentry);
23534 +       bindex = -1;
23535 +       parent = dentry->d_parent; /* dir inode is locked */
23536 +       di_write_lock_parent(parent);
23537 +       wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/0, &bindex, &a->dt,
23538 +                                       &a->pin);
23539 +       err = PTR_ERR(wh_dentry);
23540 +       if (IS_ERR(wh_dentry))
23541 +               goto out_parent;
23542 +
23543 +       a->h_path.mnt = au_sbr_mnt(dentry->d_sb, btop);
23544 +       a->h_path.dentry = au_h_dptr(dentry, btop);
23545 +       dget(a->h_path.dentry);
23546 +       if (bindex == btop) {
23547 +               h_dir = au_pinned_h_dir(&a->pin);
23548 +               delegated = NULL;
23549 +               err = vfsub_unlink(h_dir, &a->h_path, &delegated, /*force*/0);
23550 +               if (unlikely(err == -EWOULDBLOCK)) {
23551 +                       pr_warn("cannot retry for NFSv4 delegation"
23552 +                               " for an internal unlink\n");
23553 +                       iput(delegated);
23554 +               }
23555 +       } else {
23556 +               /* dir inode is locked */
23557 +               h_dir = d_inode(wh_dentry->d_parent);
23558 +               IMustLock(h_dir);
23559 +               err = 0;
23560 +       }
23561 +
23562 +       if (!err) {
23563 +               vfsub_drop_nlink(inode);
23564 +               epilog(dir, dentry, bindex);
23565 +
23566 +               /* update target timestamps */
23567 +               if (bindex == btop) {
23568 +                       vfsub_update_h_iattr(&a->h_path, /*did*/NULL);
23569 +                       /*ignore*/
23570 +                       inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
23571 +               } else
23572 +                       /* todo: this timestamp may be reverted later */
23573 +                       inode->i_ctime = h_dir->i_ctime;
23574 +               goto out_unpin; /* success */
23575 +       }
23576 +
23577 +       /* revert */
23578 +       if (wh_dentry) {
23579 +               int rerr;
23580 +
23581 +               rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
23582 +                                &a->dt);
23583 +               if (rerr)
23584 +                       err = rerr;
23585 +       }
23586 +
23587 +out_unpin:
23588 +       au_unpin(&a->pin);
23589 +       dput(wh_dentry);
23590 +       dput(a->h_path.dentry);
23591 +out_parent:
23592 +       di_write_unlock(parent);
23593 +out_unlock:
23594 +       aufs_read_unlock(dentry, AuLock_DW);
23595 +out_free:
23596 +       au_kfree_rcu(a);
23597 +out:
23598 +       return err;
23599 +}
23600 +
23601 +int aufs_rmdir(struct inode *dir, struct dentry *dentry)
23602 +{
23603 +       int err, rmdir_later;
23604 +       aufs_bindex_t bwh, bindex, btop;
23605 +       struct inode *inode;
23606 +       struct dentry *parent, *wh_dentry, *h_dentry;
23607 +       struct au_whtmp_rmdir *args;
23608 +       /* to reduce stack size */
23609 +       struct {
23610 +               struct au_dtime dt;
23611 +               struct au_pin pin;
23612 +       } *a;
23613 +
23614 +       IMustLock(dir);
23615 +
23616 +       err = -ENOMEM;
23617 +       a = kmalloc(sizeof(*a), GFP_NOFS);
23618 +       if (unlikely(!a))
23619 +               goto out;
23620 +
23621 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
23622 +       if (unlikely(err))
23623 +               goto out_free;
23624 +       err = au_alive_dir(dentry);
23625 +       if (unlikely(err))
23626 +               goto out_unlock;
23627 +       inode = d_inode(dentry);
23628 +       IMustLock(inode);
23629 +       err = -ENOTDIR;
23630 +       if (unlikely(!d_is_dir(dentry)))
23631 +               goto out_unlock; /* possible? */
23632 +
23633 +       err = -ENOMEM;
23634 +       args = au_whtmp_rmdir_alloc(dir->i_sb, GFP_NOFS);
23635 +       if (unlikely(!args))
23636 +               goto out_unlock;
23637 +
23638 +       parent = dentry->d_parent; /* dir inode is locked */
23639 +       di_write_lock_parent(parent);
23640 +       err = au_test_empty(dentry, &args->whlist);
23641 +       if (unlikely(err))
23642 +               goto out_parent;
23643 +
23644 +       btop = au_dbtop(dentry);
23645 +       bwh = au_dbwh(dentry);
23646 +       bindex = -1;
23647 +       wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/1, &bindex, &a->dt,
23648 +                                       &a->pin);
23649 +       err = PTR_ERR(wh_dentry);
23650 +       if (IS_ERR(wh_dentry))
23651 +               goto out_parent;
23652 +
23653 +       h_dentry = au_h_dptr(dentry, btop);
23654 +       dget(h_dentry);
23655 +       rmdir_later = 0;
23656 +       if (bindex == btop) {
23657 +               err = renwh_and_rmdir(dentry, btop, &args->whlist, dir);
23658 +               if (err > 0) {
23659 +                       rmdir_later = err;
23660 +                       err = 0;
23661 +               }
23662 +       } else {
23663 +               /* stop monitoring */
23664 +               au_hn_free(au_hi(inode, btop));
23665 +
23666 +               /* dir inode is locked */
23667 +               IMustLock(d_inode(wh_dentry->d_parent));
23668 +               err = 0;
23669 +       }
23670 +
23671 +       if (!err) {
23672 +               vfsub_dead_dir(inode);
23673 +               au_set_dbdiropq(dentry, -1);
23674 +               epilog(dir, dentry, bindex);
23675 +
23676 +               if (rmdir_later) {
23677 +                       au_whtmp_kick_rmdir(dir, btop, h_dentry, args);
23678 +                       args = NULL;
23679 +               }
23680 +
23681 +               goto out_unpin; /* success */
23682 +       }
23683 +
23684 +       /* revert */
23685 +       AuLabel(revert);
23686 +       if (wh_dentry) {
23687 +               int rerr;
23688 +
23689 +               rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
23690 +                                &a->dt);
23691 +               if (rerr)
23692 +                       err = rerr;
23693 +       }
23694 +
23695 +out_unpin:
23696 +       au_unpin(&a->pin);
23697 +       dput(wh_dentry);
23698 +       dput(h_dentry);
23699 +out_parent:
23700 +       di_write_unlock(parent);
23701 +       if (args)
23702 +               au_whtmp_rmdir_free(args);
23703 +out_unlock:
23704 +       aufs_read_unlock(dentry, AuLock_DW);
23705 +out_free:
23706 +       au_kfree_rcu(a);
23707 +out:
23708 +       AuTraceErr(err);
23709 +       return err;
23710 +}
23711 diff -urN /usr/share/empty/fs/aufs/i_op_ren.c linux/fs/aufs/i_op_ren.c
23712 --- /usr/share/empty/fs/aufs/i_op_ren.c 1970-01-01 01:00:00.000000000 +0100
23713 +++ linux/fs/aufs/i_op_ren.c    2022-03-21 14:49:05.726633010 +0100
23714 @@ -0,0 +1,1257 @@
23715 +// SPDX-License-Identifier: GPL-2.0
23716 +/*
23717 + * Copyright (C) 2005-2021 Junjiro R. Okajima
23718 + *
23719 + * This program, aufs is free software; you can redistribute it and/or modify
23720 + * it under the terms of the GNU General Public License as published by
23721 + * the Free Software Foundation; either version 2 of the License, or
23722 + * (at your option) any later version.
23723 + *
23724 + * This program is distributed in the hope that it will be useful,
23725 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
23726 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23727 + * GNU General Public License for more details.
23728 + *
23729 + * You should have received a copy of the GNU General Public License
23730 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23731 + */
23732 +
23733 +/*
23734 + * inode operation (rename entry)
23735 + * todo: this is crazy monster
23736 + */
23737 +
23738 +#include <linux/iversion.h>
23739 +#include "aufs.h"
23740 +
23741 +enum { AuSRC, AuDST, AuSrcDst };
23742 +enum { AuPARENT, AuCHILD, AuParentChild };
23743 +
23744 +#define AuRen_ISDIR_SRC                1
23745 +#define AuRen_ISDIR_DST                (1 << 1)
23746 +#define AuRen_ISSAMEDIR                (1 << 2)
23747 +#define AuRen_WHSRC            (1 << 3)
23748 +#define AuRen_WHDST            (1 << 4)
23749 +#define AuRen_MNT_WRITE                (1 << 5)
23750 +#define AuRen_DT_DSTDIR                (1 << 6)
23751 +#define AuRen_DIROPQ_SRC       (1 << 7)
23752 +#define AuRen_DIROPQ_DST       (1 << 8)
23753 +#define AuRen_DIRREN           (1 << 9)
23754 +#define AuRen_DROPPED_SRC      (1 << 10)
23755 +#define AuRen_DROPPED_DST      (1 << 11)
23756 +#define au_ftest_ren(flags, name)      ((flags) & AuRen_##name)
23757 +#define au_fset_ren(flags, name) \
23758 +       do { (flags) |= AuRen_##name; } while (0)
23759 +#define au_fclr_ren(flags, name) \
23760 +       do { (flags) &= ~AuRen_##name; } while (0)
23761 +
23762 +#ifndef CONFIG_AUFS_DIRREN
23763 +#undef AuRen_DIRREN
23764 +#define AuRen_DIRREN           0
23765 +#endif
23766 +
23767 +struct au_ren_args {
23768 +       struct {
23769 +               struct dentry *dentry, *h_dentry, *parent, *h_parent,
23770 +                       *wh_dentry;
23771 +               struct inode *dir, *inode;
23772 +               struct au_hinode *hdir, *hinode;
23773 +               struct au_dtime dt[AuParentChild];
23774 +               aufs_bindex_t btop, bdiropq;
23775 +       } sd[AuSrcDst];
23776 +
23777 +#define src_dentry     sd[AuSRC].dentry
23778 +#define src_dir                sd[AuSRC].dir
23779 +#define src_inode      sd[AuSRC].inode
23780 +#define src_h_dentry   sd[AuSRC].h_dentry
23781 +#define src_parent     sd[AuSRC].parent
23782 +#define src_h_parent   sd[AuSRC].h_parent
23783 +#define src_wh_dentry  sd[AuSRC].wh_dentry
23784 +#define src_hdir       sd[AuSRC].hdir
23785 +#define src_hinode     sd[AuSRC].hinode
23786 +#define src_h_dir      sd[AuSRC].hdir->hi_inode
23787 +#define src_dt         sd[AuSRC].dt
23788 +#define src_btop       sd[AuSRC].btop
23789 +#define src_bdiropq    sd[AuSRC].bdiropq
23790 +
23791 +#define dst_dentry     sd[AuDST].dentry
23792 +#define dst_dir                sd[AuDST].dir
23793 +#define dst_inode      sd[AuDST].inode
23794 +#define dst_h_dentry   sd[AuDST].h_dentry
23795 +#define dst_parent     sd[AuDST].parent
23796 +#define dst_h_parent   sd[AuDST].h_parent
23797 +#define dst_wh_dentry  sd[AuDST].wh_dentry
23798 +#define dst_hdir       sd[AuDST].hdir
23799 +#define dst_hinode     sd[AuDST].hinode
23800 +#define dst_h_dir      sd[AuDST].hdir->hi_inode
23801 +#define dst_dt         sd[AuDST].dt
23802 +#define dst_btop       sd[AuDST].btop
23803 +#define dst_bdiropq    sd[AuDST].bdiropq
23804 +
23805 +       struct dentry *h_trap;
23806 +       struct au_branch *br;
23807 +       struct path h_path;
23808 +       struct au_nhash whlist;
23809 +       aufs_bindex_t btgt, src_bwh;
23810 +
23811 +       struct {
23812 +               unsigned short auren_flags;
23813 +               unsigned char flags;    /* syscall parameter */
23814 +               unsigned char exchange;
23815 +       } __packed;
23816 +
23817 +       struct au_whtmp_rmdir *thargs;
23818 +       struct dentry *h_dst;
23819 +       struct au_hinode *h_root;
23820 +};
23821 +
23822 +/* ---------------------------------------------------------------------- */
23823 +
23824 +/*
23825 + * functions for reverting.
23826 + * when an error happened in a single rename systemcall, we should revert
23827 + * everything as if nothing happened.
23828 + * we don't need to revert the copied-up/down the parent dir since they are
23829 + * harmless.
23830 + */
23831 +
23832 +#define RevertFailure(fmt, ...) do { \
23833 +       AuIOErr("revert failure: " fmt " (%d, %d)\n", \
23834 +               ##__VA_ARGS__, err, rerr); \
23835 +       err = -EIO; \
23836 +} while (0)
23837 +
23838 +static void au_ren_do_rev_diropq(int err, struct au_ren_args *a, int idx)
23839 +{
23840 +       int rerr;
23841 +       struct dentry *d;
23842 +#define src_or_dst(member) a->sd[idx].member
23843 +
23844 +       d = src_or_dst(dentry); /* {src,dst}_dentry */
23845 +       au_hn_inode_lock_nested(src_or_dst(hinode), AuLsc_I_CHILD);
23846 +       rerr = au_diropq_remove(d, a->btgt);
23847 +       au_hn_inode_unlock(src_or_dst(hinode));
23848 +       au_set_dbdiropq(d, src_or_dst(bdiropq));
23849 +       if (rerr)
23850 +               RevertFailure("remove diropq %pd", d);
23851 +
23852 +#undef src_or_dst_
23853 +}
23854 +
23855 +static void au_ren_rev_diropq(int err, struct au_ren_args *a)
23856 +{
23857 +       if (au_ftest_ren(a->auren_flags, DIROPQ_SRC))
23858 +               au_ren_do_rev_diropq(err, a, AuSRC);
23859 +       if (au_ftest_ren(a->auren_flags, DIROPQ_DST))
23860 +               au_ren_do_rev_diropq(err, a, AuDST);
23861 +}
23862 +
23863 +static void au_ren_rev_rename(int err, struct au_ren_args *a)
23864 +{
23865 +       int rerr;
23866 +       struct inode *delegated;
23867 +       struct path h_ppath = {
23868 +               .dentry = a->src_h_parent,
23869 +               .mnt    = a->h_path.mnt
23870 +       };
23871 +
23872 +       a->h_path.dentry = vfsub_lkup_one(&a->src_dentry->d_name, &h_ppath);
23873 +       rerr = PTR_ERR(a->h_path.dentry);
23874 +       if (IS_ERR(a->h_path.dentry)) {
23875 +               RevertFailure("lkup one %pd", a->src_dentry);
23876 +               return;
23877 +       }
23878 +
23879 +       delegated = NULL;
23880 +       rerr = vfsub_rename(a->dst_h_dir,
23881 +                           au_h_dptr(a->src_dentry, a->btgt),
23882 +                           a->src_h_dir, &a->h_path, &delegated, a->flags);
23883 +       if (unlikely(rerr == -EWOULDBLOCK)) {
23884 +               pr_warn("cannot retry for NFSv4 delegation"
23885 +                       " for an internal rename\n");
23886 +               iput(delegated);
23887 +       }
23888 +       d_drop(a->h_path.dentry);
23889 +       dput(a->h_path.dentry);
23890 +       /* au_set_h_dptr(a->src_dentry, a->btgt, NULL); */
23891 +       if (rerr)
23892 +               RevertFailure("rename %pd", a->src_dentry);
23893 +}
23894 +
23895 +static void au_ren_rev_whtmp(int err, struct au_ren_args *a)
23896 +{
23897 +       int rerr;
23898 +       struct inode *delegated;
23899 +       struct path h_ppath = {
23900 +               .dentry = a->dst_h_parent,
23901 +               .mnt    = a->h_path.mnt
23902 +       };
23903 +
23904 +       a->h_path.dentry = vfsub_lkup_one(&a->dst_dentry->d_name, &h_ppath);
23905 +       rerr = PTR_ERR(a->h_path.dentry);
23906 +       if (IS_ERR(a->h_path.dentry)) {
23907 +               RevertFailure("lkup one %pd", a->dst_dentry);
23908 +               return;
23909 +       }
23910 +       if (d_is_positive(a->h_path.dentry)) {
23911 +               d_drop(a->h_path.dentry);
23912 +               dput(a->h_path.dentry);
23913 +               return;
23914 +       }
23915 +
23916 +       delegated = NULL;
23917 +       rerr = vfsub_rename(a->dst_h_dir, a->h_dst, a->dst_h_dir, &a->h_path,
23918 +                           &delegated, a->flags);
23919 +       if (unlikely(rerr == -EWOULDBLOCK)) {
23920 +               pr_warn("cannot retry for NFSv4 delegation"
23921 +                       " for an internal rename\n");
23922 +               iput(delegated);
23923 +       }
23924 +       d_drop(a->h_path.dentry);
23925 +       dput(a->h_path.dentry);
23926 +       if (!rerr)
23927 +               au_set_h_dptr(a->dst_dentry, a->btgt, dget(a->h_dst));
23928 +       else
23929 +               RevertFailure("rename %pd", a->h_dst);
23930 +}
23931 +
23932 +static void au_ren_rev_whsrc(int err, struct au_ren_args *a)
23933 +{
23934 +       int rerr;
23935 +
23936 +       a->h_path.dentry = a->src_wh_dentry;
23937 +       rerr = au_wh_unlink_dentry(a->src_h_dir, &a->h_path, a->src_dentry);
23938 +       au_set_dbwh(a->src_dentry, a->src_bwh);
23939 +       if (rerr)
23940 +               RevertFailure("unlink %pd", a->src_wh_dentry);
23941 +}
23942 +#undef RevertFailure
23943 +
23944 +/* ---------------------------------------------------------------------- */
23945 +
23946 +/*
23947 + * when we have to copyup the renaming entry, do it with the rename-target name
23948 + * in order to minimize the cost (the later actual rename is unnecessary).
23949 + * otherwise rename it on the target branch.
23950 + */
23951 +static int au_ren_or_cpup(struct au_ren_args *a)
23952 +{
23953 +       int err;
23954 +       struct dentry *d;
23955 +       struct inode *delegated;
23956 +
23957 +       d = a->src_dentry;
23958 +       if (au_dbtop(d) == a->btgt) {
23959 +               a->h_path.dentry = a->dst_h_dentry;
23960 +               AuDebugOn(au_dbtop(d) != a->btgt);
23961 +               delegated = NULL;
23962 +               err = vfsub_rename(a->src_h_dir, au_h_dptr(d, a->btgt),
23963 +                                  a->dst_h_dir, &a->h_path, &delegated,
23964 +                                  a->flags);
23965 +               if (unlikely(err == -EWOULDBLOCK)) {
23966 +                       pr_warn("cannot retry for NFSv4 delegation"
23967 +                               " for an internal rename\n");
23968 +                       iput(delegated);
23969 +               }
23970 +       } else
23971 +               BUG();
23972 +
23973 +       if (!err && a->h_dst)
23974 +               /* it will be set to dinfo later */
23975 +               dget(a->h_dst);
23976 +
23977 +       return err;
23978 +}
23979 +
23980 +/* cf. aufs_rmdir() */
23981 +static int au_ren_del_whtmp(struct au_ren_args *a)
23982 +{
23983 +       int err;
23984 +       struct inode *dir;
23985 +
23986 +       dir = a->dst_dir;
23987 +       SiMustAnyLock(dir->i_sb);
23988 +       if (!au_nhash_test_longer_wh(&a->whlist, a->btgt,
23989 +                                    au_sbi(dir->i_sb)->si_dirwh)
23990 +           || au_test_fs_remote(a->h_dst->d_sb)) {
23991 +               err = au_whtmp_rmdir(dir, a->btgt, a->h_dst, &a->whlist);
23992 +               if (unlikely(err))
23993 +                       pr_warn("failed removing whtmp dir %pd (%d), "
23994 +                               "ignored.\n", a->h_dst, err);
23995 +       } else {
23996 +               au_nhash_wh_free(&a->thargs->whlist);
23997 +               a->thargs->whlist = a->whlist;
23998 +               a->whlist.nh_num = 0;
23999 +               au_whtmp_kick_rmdir(dir, a->btgt, a->h_dst, a->thargs);
24000 +               dput(a->h_dst);
24001 +               a->thargs = NULL;
24002 +       }
24003 +
24004 +       return 0;
24005 +}
24006 +
24007 +/* make it 'opaque' dir. */
24008 +static int au_ren_do_diropq(struct au_ren_args *a, int idx)
24009 +{
24010 +       int err;
24011 +       struct dentry *d, *diropq;
24012 +#define src_or_dst(member) a->sd[idx].member
24013 +
24014 +       err = 0;
24015 +       d = src_or_dst(dentry); /* {src,dst}_dentry */
24016 +       src_or_dst(bdiropq) = au_dbdiropq(d);
24017 +       src_or_dst(hinode) = au_hi(src_or_dst(inode), a->btgt);
24018 +       au_hn_inode_lock_nested(src_or_dst(hinode), AuLsc_I_CHILD);
24019 +       diropq = au_diropq_create(d, a->btgt);
24020 +       au_hn_inode_unlock(src_or_dst(hinode));
24021 +       if (IS_ERR(diropq))
24022 +               err = PTR_ERR(diropq);
24023 +       else
24024 +               dput(diropq);
24025 +
24026 +#undef src_or_dst_
24027 +       return err;
24028 +}
24029 +
24030 +static int au_ren_diropq(struct au_ren_args *a)
24031 +{
24032 +       int err;
24033 +       unsigned char always;
24034 +       struct dentry *d;
24035 +
24036 +       err = 0;
24037 +       d = a->dst_dentry; /* already renamed on the branch */
24038 +       always = !!au_opt_test(au_mntflags(d->d_sb), ALWAYS_DIROPQ);
24039 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC)
24040 +           && !au_ftest_ren(a->auren_flags, DIRREN)
24041 +           && a->btgt != au_dbdiropq(a->src_dentry)
24042 +           && (a->dst_wh_dentry
24043 +               || a->btgt <= au_dbdiropq(d)
24044 +               /* hide the lower to keep xino */
24045 +               /* the lowers may not be a dir, but we hide them anyway */
24046 +               || a->btgt < au_dbbot(d)
24047 +               || always)) {
24048 +               AuDbg("here\n");
24049 +               err = au_ren_do_diropq(a, AuSRC);
24050 +               if (unlikely(err))
24051 +                       goto out;
24052 +               au_fset_ren(a->auren_flags, DIROPQ_SRC);
24053 +       }
24054 +       if (!a->exchange)
24055 +               goto out; /* success */
24056 +
24057 +       d = a->src_dentry; /* already renamed on the branch */
24058 +       if (au_ftest_ren(a->auren_flags, ISDIR_DST)
24059 +           && a->btgt != au_dbdiropq(a->dst_dentry)
24060 +           && (a->btgt < au_dbdiropq(d)
24061 +               || a->btgt < au_dbbot(d)
24062 +               || always)) {
24063 +               AuDbgDentry(a->src_dentry);
24064 +               AuDbgDentry(a->dst_dentry);
24065 +               err = au_ren_do_diropq(a, AuDST);
24066 +               if (unlikely(err))
24067 +                       goto out_rev_src;
24068 +               au_fset_ren(a->auren_flags, DIROPQ_DST);
24069 +       }
24070 +       goto out; /* success */
24071 +
24072 +out_rev_src:
24073 +       AuDbg("err %d, reverting src\n", err);
24074 +       au_ren_rev_diropq(err, a);
24075 +out:
24076 +       return err;
24077 +}
24078 +
24079 +static int do_rename(struct au_ren_args *a)
24080 +{
24081 +       int err;
24082 +       struct dentry *d, *h_d;
24083 +
24084 +       if (!a->exchange) {
24085 +               /* prepare workqueue args for asynchronous rmdir */
24086 +               h_d = a->dst_h_dentry;
24087 +               if (au_ftest_ren(a->auren_flags, ISDIR_DST)
24088 +                   /* && !au_ftest_ren(a->auren_flags, DIRREN) */
24089 +                   && d_is_positive(h_d)) {
24090 +                       err = -ENOMEM;
24091 +                       a->thargs = au_whtmp_rmdir_alloc(a->src_dentry->d_sb,
24092 +                                                        GFP_NOFS);
24093 +                       if (unlikely(!a->thargs))
24094 +                               goto out;
24095 +                       a->h_dst = dget(h_d);
24096 +               }
24097 +
24098 +               /* create whiteout for src_dentry */
24099 +               if (au_ftest_ren(a->auren_flags, WHSRC)) {
24100 +                       a->src_bwh = au_dbwh(a->src_dentry);
24101 +                       AuDebugOn(a->src_bwh >= 0);
24102 +                       a->src_wh_dentry = au_wh_create(a->src_dentry, a->btgt,
24103 +                                                       a->src_h_parent);
24104 +                       err = PTR_ERR(a->src_wh_dentry);
24105 +                       if (IS_ERR(a->src_wh_dentry))
24106 +                               goto out_thargs;
24107 +               }
24108 +
24109 +               /* lookup whiteout for dentry */
24110 +               if (au_ftest_ren(a->auren_flags, WHDST)) {
24111 +                       h_d = au_wh_lkup(a->dst_h_parent,
24112 +                                        &a->dst_dentry->d_name, a->br);
24113 +                       err = PTR_ERR(h_d);
24114 +                       if (IS_ERR(h_d))
24115 +                               goto out_whsrc;
24116 +                       if (d_is_negative(h_d))
24117 +                               dput(h_d);
24118 +                       else
24119 +                               a->dst_wh_dentry = h_d;
24120 +               }
24121 +
24122 +               /* rename dentry to tmpwh */
24123 +               if (a->thargs) {
24124 +                       err = au_whtmp_ren(a->dst_h_dentry, a->br);
24125 +                       if (unlikely(err))
24126 +                               goto out_whdst;
24127 +
24128 +                       d = a->dst_dentry;
24129 +                       au_set_h_dptr(d, a->btgt, NULL);
24130 +                       err = au_lkup_neg(d, a->btgt, /*wh*/0);
24131 +                       if (unlikely(err))
24132 +                               goto out_whtmp;
24133 +                       a->dst_h_dentry = au_h_dptr(d, a->btgt);
24134 +               }
24135 +       }
24136 +
24137 +       BUG_ON(d_is_positive(a->dst_h_dentry) && a->src_btop != a->btgt);
24138 +#if 0 /* debugging */
24139 +       BUG_ON(!au_ftest_ren(a->auren_flags, DIRREN)
24140 +              && d_is_positive(a->dst_h_dentry)
24141 +              && a->src_btop != a->btgt);
24142 +#endif
24143 +
24144 +       /* rename by vfs_rename or cpup */
24145 +       err = au_ren_or_cpup(a);
24146 +       if (unlikely(err))
24147 +               /* leave the copied-up one */
24148 +               goto out_whtmp;
24149 +
24150 +       /* make dir opaque */
24151 +       err = au_ren_diropq(a);
24152 +       if (unlikely(err))
24153 +               goto out_rename;
24154 +
24155 +       /* update target timestamps */
24156 +       if (a->exchange) {
24157 +               AuDebugOn(au_dbtop(a->dst_dentry) != a->btgt);
24158 +               a->h_path.dentry = au_h_dptr(a->dst_dentry, a->btgt);
24159 +               vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
24160 +               a->dst_inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
24161 +       }
24162 +       AuDebugOn(au_dbtop(a->src_dentry) != a->btgt);
24163 +       a->h_path.dentry = au_h_dptr(a->src_dentry, a->btgt);
24164 +       vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
24165 +       a->src_inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
24166 +
24167 +       if (!a->exchange) {
24168 +               /* remove whiteout for dentry */
24169 +               if (a->dst_wh_dentry) {
24170 +                       a->h_path.dentry = a->dst_wh_dentry;
24171 +                       err = au_wh_unlink_dentry(a->dst_h_dir, &a->h_path,
24172 +                                                 a->dst_dentry);
24173 +                       if (unlikely(err))
24174 +                               goto out_diropq;
24175 +               }
24176 +
24177 +               /* remove whtmp */
24178 +               if (a->thargs)
24179 +                       au_ren_del_whtmp(a); /* ignore this error */
24180 +
24181 +               au_fhsm_wrote(a->src_dentry->d_sb, a->btgt, /*force*/0);
24182 +       }
24183 +       err = 0;
24184 +       goto out_success;
24185 +
24186 +out_diropq:
24187 +       au_ren_rev_diropq(err, a);
24188 +out_rename:
24189 +       au_ren_rev_rename(err, a);
24190 +       dput(a->h_dst);
24191 +out_whtmp:
24192 +       if (a->thargs)
24193 +               au_ren_rev_whtmp(err, a);
24194 +out_whdst:
24195 +       dput(a->dst_wh_dentry);
24196 +       a->dst_wh_dentry = NULL;
24197 +out_whsrc:
24198 +       if (a->src_wh_dentry)
24199 +               au_ren_rev_whsrc(err, a);
24200 +out_success:
24201 +       dput(a->src_wh_dentry);
24202 +       dput(a->dst_wh_dentry);
24203 +out_thargs:
24204 +       if (a->thargs) {
24205 +               dput(a->h_dst);
24206 +               au_whtmp_rmdir_free(a->thargs);
24207 +               a->thargs = NULL;
24208 +       }
24209 +out:
24210 +       return err;
24211 +}
24212 +
24213 +/* ---------------------------------------------------------------------- */
24214 +
24215 +/*
24216 + * test if @dentry dir can be rename destination or not.
24217 + * success means, it is a logically empty dir.
24218 + */
24219 +static int may_rename_dstdir(struct dentry *dentry, struct au_nhash *whlist)
24220 +{
24221 +       return au_test_empty(dentry, whlist);
24222 +}
24223 +
24224 +/*
24225 + * test if @a->src_dentry dir can be rename source or not.
24226 + * if it can, return 0.
24227 + * success means,
24228 + * - it is a logically empty dir.
24229 + * - or, it exists on writable branch and has no children including whiteouts
24230 + *   on the lower branch unless DIRREN is on.
24231 + */
24232 +static int may_rename_srcdir(struct au_ren_args *a)
24233 +{
24234 +       int err;
24235 +       unsigned int rdhash;
24236 +       aufs_bindex_t btop, btgt;
24237 +       struct dentry *dentry;
24238 +       struct super_block *sb;
24239 +       struct au_sbinfo *sbinfo;
24240 +
24241 +       dentry = a->src_dentry;
24242 +       sb = dentry->d_sb;
24243 +       sbinfo = au_sbi(sb);
24244 +       if (au_opt_test(sbinfo->si_mntflags, DIRREN))
24245 +               au_fset_ren(a->auren_flags, DIRREN);
24246 +
24247 +       btgt = a->btgt;
24248 +       btop = au_dbtop(dentry);
24249 +       if (btop != btgt) {
24250 +               struct au_nhash whlist;
24251 +
24252 +               SiMustAnyLock(sb);
24253 +               rdhash = sbinfo->si_rdhash;
24254 +               if (!rdhash)
24255 +                       rdhash = au_rdhash_est(au_dir_size(/*file*/NULL,
24256 +                                                          dentry));
24257 +               err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
24258 +               if (unlikely(err))
24259 +                       goto out;
24260 +               err = au_test_empty(dentry, &whlist);
24261 +               au_nhash_wh_free(&whlist);
24262 +               goto out;
24263 +       }
24264 +
24265 +       if (btop == au_dbtaildir(dentry))
24266 +               return 0; /* success */
24267 +
24268 +       err = au_test_empty_lower(dentry);
24269 +
24270 +out:
24271 +       if (err == -ENOTEMPTY) {
24272 +               if (au_ftest_ren(a->auren_flags, DIRREN)) {
24273 +                       err = 0;
24274 +               } else {
24275 +                       AuWarn1("renaming dir who has child(ren) on multiple "
24276 +                               "branches, is not supported\n");
24277 +                       err = -EXDEV;
24278 +               }
24279 +       }
24280 +       return err;
24281 +}
24282 +
24283 +/* side effect: sets whlist and h_dentry */
24284 +static int au_ren_may_dir(struct au_ren_args *a)
24285 +{
24286 +       int err;
24287 +       unsigned int rdhash;
24288 +       struct dentry *d;
24289 +
24290 +       d = a->dst_dentry;
24291 +       SiMustAnyLock(d->d_sb);
24292 +
24293 +       err = 0;
24294 +       if (au_ftest_ren(a->auren_flags, ISDIR_DST) && a->dst_inode) {
24295 +               rdhash = au_sbi(d->d_sb)->si_rdhash;
24296 +               if (!rdhash)
24297 +                       rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, d));
24298 +               err = au_nhash_alloc(&a->whlist, rdhash, GFP_NOFS);
24299 +               if (unlikely(err))
24300 +                       goto out;
24301 +
24302 +               if (!a->exchange) {
24303 +                       au_set_dbtop(d, a->dst_btop);
24304 +                       err = may_rename_dstdir(d, &a->whlist);
24305 +                       au_set_dbtop(d, a->btgt);
24306 +               } else
24307 +                       err = may_rename_srcdir(a);
24308 +       }
24309 +       a->dst_h_dentry = au_h_dptr(d, au_dbtop(d));
24310 +       if (unlikely(err))
24311 +               goto out;
24312 +
24313 +       d = a->src_dentry;
24314 +       a->src_h_dentry = au_h_dptr(d, au_dbtop(d));
24315 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC)) {
24316 +               err = may_rename_srcdir(a);
24317 +               if (unlikely(err)) {
24318 +                       au_nhash_wh_free(&a->whlist);
24319 +                       a->whlist.nh_num = 0;
24320 +               }
24321 +       }
24322 +out:
24323 +       return err;
24324 +}
24325 +
24326 +/* ---------------------------------------------------------------------- */
24327 +
24328 +/*
24329 + * simple tests for rename.
24330 + * following the checks in vfs, plus the parent-child relationship.
24331 + */
24332 +static int au_may_ren(struct au_ren_args *a)
24333 +{
24334 +       int err, isdir;
24335 +       struct inode *h_inode;
24336 +
24337 +       if (a->src_btop == a->btgt) {
24338 +               err = au_may_del(a->src_dentry, a->btgt, a->src_h_parent,
24339 +                                au_ftest_ren(a->auren_flags, ISDIR_SRC));
24340 +               if (unlikely(err))
24341 +                       goto out;
24342 +               err = -EINVAL;
24343 +               if (unlikely(a->src_h_dentry == a->h_trap))
24344 +                       goto out;
24345 +       }
24346 +
24347 +       err = 0;
24348 +       if (a->dst_btop != a->btgt)
24349 +               goto out;
24350 +
24351 +       err = -ENOTEMPTY;
24352 +       if (unlikely(a->dst_h_dentry == a->h_trap))
24353 +               goto out;
24354 +
24355 +       err = -EIO;
24356 +       isdir = !!au_ftest_ren(a->auren_flags, ISDIR_DST);
24357 +       if (d_really_is_negative(a->dst_dentry)) {
24358 +               if (d_is_negative(a->dst_h_dentry))
24359 +                       err = au_may_add(a->dst_dentry, a->btgt,
24360 +                                        a->dst_h_parent, isdir);
24361 +       } else {
24362 +               if (unlikely(d_is_negative(a->dst_h_dentry)))
24363 +                       goto out;
24364 +               h_inode = d_inode(a->dst_h_dentry);
24365 +               if (h_inode->i_nlink)
24366 +                       err = au_may_del(a->dst_dentry, a->btgt,
24367 +                                        a->dst_h_parent, isdir);
24368 +       }
24369 +
24370 +out:
24371 +       if (unlikely(err == -ENOENT || err == -EEXIST))
24372 +               err = -EIO;
24373 +       AuTraceErr(err);
24374 +       return err;
24375 +}
24376 +
24377 +/* ---------------------------------------------------------------------- */
24378 +
24379 +/*
24380 + * locking order
24381 + * (VFS)
24382 + * - src_dir and dir by lock_rename()
24383 + * - inode if exists
24384 + * (aufs)
24385 + * - lock all
24386 + *   + src_dentry and dentry by aufs_read_and_write_lock2() which calls,
24387 + *     + si_read_lock
24388 + *     + di_write_lock2_child()
24389 + *       + di_write_lock_child()
24390 + *        + ii_write_lock_child()
24391 + *       + di_write_lock_child2()
24392 + *        + ii_write_lock_child2()
24393 + *     + src_parent and parent
24394 + *       + di_write_lock_parent()
24395 + *        + ii_write_lock_parent()
24396 + *       + di_write_lock_parent2()
24397 + *        + ii_write_lock_parent2()
24398 + *   + lower src_dir and dir by vfsub_lock_rename()
24399 + *   + verify the every relationships between child and parent. if any
24400 + *     of them failed, unlock all and return -EBUSY.
24401 + */
24402 +static void au_ren_unlock(struct au_ren_args *a)
24403 +{
24404 +       vfsub_unlock_rename(a->src_h_parent, a->src_hdir,
24405 +                           a->dst_h_parent, a->dst_hdir);
24406 +       if (au_ftest_ren(a->auren_flags, DIRREN)
24407 +           && a->h_root)
24408 +               au_hn_inode_unlock(a->h_root);
24409 +       if (au_ftest_ren(a->auren_flags, MNT_WRITE))
24410 +               vfsub_mnt_drop_write(au_br_mnt(a->br));
24411 +}
24412 +
24413 +static int au_ren_lock(struct au_ren_args *a)
24414 +{
24415 +       int err;
24416 +       unsigned int udba;
24417 +
24418 +       err = 0;
24419 +       a->src_h_parent = au_h_dptr(a->src_parent, a->btgt);
24420 +       a->src_hdir = au_hi(a->src_dir, a->btgt);
24421 +       a->dst_h_parent = au_h_dptr(a->dst_parent, a->btgt);
24422 +       a->dst_hdir = au_hi(a->dst_dir, a->btgt);
24423 +
24424 +       err = vfsub_mnt_want_write(au_br_mnt(a->br));
24425 +       if (unlikely(err))
24426 +               goto out;
24427 +       au_fset_ren(a->auren_flags, MNT_WRITE);
24428 +       if (au_ftest_ren(a->auren_flags, DIRREN)) {
24429 +               struct dentry *root;
24430 +               struct inode *dir;
24431 +
24432 +               /*
24433 +                * sbinfo is already locked, so this ii_read_lock is
24434 +                * unnecessary. but our debugging feature checks it.
24435 +                */
24436 +               root = a->src_inode->i_sb->s_root;
24437 +               if (root != a->src_parent && root != a->dst_parent) {
24438 +                       dir = d_inode(root);
24439 +                       ii_read_lock_parent3(dir);
24440 +                       a->h_root = au_hi(dir, a->btgt);
24441 +                       ii_read_unlock(dir);
24442 +                       au_hn_inode_lock_nested(a->h_root, AuLsc_I_PARENT3);
24443 +               }
24444 +       }
24445 +       a->h_trap = vfsub_lock_rename(a->src_h_parent, a->src_hdir,
24446 +                                     a->dst_h_parent, a->dst_hdir);
24447 +       udba = au_opt_udba(a->src_dentry->d_sb);
24448 +       if (unlikely(a->src_hdir->hi_inode != d_inode(a->src_h_parent)
24449 +                    || a->dst_hdir->hi_inode != d_inode(a->dst_h_parent)))
24450 +               err = au_busy_or_stale();
24451 +       if (!err && au_dbtop(a->src_dentry) == a->btgt)
24452 +               err = au_h_verify(a->src_h_dentry, udba,
24453 +                                 d_inode(a->src_h_parent), a->src_h_parent,
24454 +                                 a->br);
24455 +       if (!err && au_dbtop(a->dst_dentry) == a->btgt)
24456 +               err = au_h_verify(a->dst_h_dentry, udba,
24457 +                                 d_inode(a->dst_h_parent), a->dst_h_parent,
24458 +                                 a->br);
24459 +       if (!err)
24460 +               goto out; /* success */
24461 +
24462 +       err = au_busy_or_stale();
24463 +       au_ren_unlock(a);
24464 +
24465 +out:
24466 +       return err;
24467 +}
24468 +
24469 +/* ---------------------------------------------------------------------- */
24470 +
24471 +static void au_ren_refresh_dir(struct au_ren_args *a)
24472 +{
24473 +       struct inode *dir;
24474 +
24475 +       dir = a->dst_dir;
24476 +       inode_inc_iversion(dir);
24477 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC)) {
24478 +               /* is this updating defined in POSIX? */
24479 +               au_cpup_attr_timesizes(a->src_inode);
24480 +               au_cpup_attr_nlink(dir, /*force*/1);
24481 +       }
24482 +       au_dir_ts(dir, a->btgt);
24483 +
24484 +       if (a->exchange) {
24485 +               dir = a->src_dir;
24486 +               inode_inc_iversion(dir);
24487 +               if (au_ftest_ren(a->auren_flags, ISDIR_DST)) {
24488 +                       /* is this updating defined in POSIX? */
24489 +                       au_cpup_attr_timesizes(a->dst_inode);
24490 +                       au_cpup_attr_nlink(dir, /*force*/1);
24491 +               }
24492 +               au_dir_ts(dir, a->btgt);
24493 +       }
24494 +
24495 +       if (au_ftest_ren(a->auren_flags, ISSAMEDIR))
24496 +               return;
24497 +
24498 +       dir = a->src_dir;
24499 +       inode_inc_iversion(dir);
24500 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC))
24501 +               au_cpup_attr_nlink(dir, /*force*/1);
24502 +       au_dir_ts(dir, a->btgt);
24503 +}
24504 +
24505 +static void au_ren_refresh(struct au_ren_args *a)
24506 +{
24507 +       aufs_bindex_t bbot, bindex;
24508 +       struct dentry *d, *h_d;
24509 +       struct inode *i, *h_i;
24510 +       struct super_block *sb;
24511 +
24512 +       d = a->dst_dentry;
24513 +       d_drop(d);
24514 +       if (a->h_dst)
24515 +               /* already dget-ed by au_ren_or_cpup() */
24516 +               au_set_h_dptr(d, a->btgt, a->h_dst);
24517 +
24518 +       i = a->dst_inode;
24519 +       if (i) {
24520 +               if (!a->exchange) {
24521 +                       if (!au_ftest_ren(a->auren_flags, ISDIR_DST))
24522 +                               vfsub_drop_nlink(i);
24523 +                       else {
24524 +                               vfsub_dead_dir(i);
24525 +                               au_cpup_attr_timesizes(i);
24526 +                       }
24527 +                       au_update_dbrange(d, /*do_put_zero*/1);
24528 +               } else
24529 +                       au_cpup_attr_nlink(i, /*force*/1);
24530 +       } else {
24531 +               bbot = a->btgt;
24532 +               for (bindex = au_dbtop(d); bindex < bbot; bindex++)
24533 +                       au_set_h_dptr(d, bindex, NULL);
24534 +               bbot = au_dbbot(d);
24535 +               for (bindex = a->btgt + 1; bindex <= bbot; bindex++)
24536 +                       au_set_h_dptr(d, bindex, NULL);
24537 +               au_update_dbrange(d, /*do_put_zero*/0);
24538 +       }
24539 +
24540 +       if (a->exchange
24541 +           || au_ftest_ren(a->auren_flags, DIRREN)) {
24542 +               d_drop(a->src_dentry);
24543 +               if (au_ftest_ren(a->auren_flags, DIRREN))
24544 +                       au_set_dbwh(a->src_dentry, -1);
24545 +               return;
24546 +       }
24547 +
24548 +       d = a->src_dentry;
24549 +       au_set_dbwh(d, -1);
24550 +       bbot = au_dbbot(d);
24551 +       for (bindex = a->btgt + 1; bindex <= bbot; bindex++) {
24552 +               h_d = au_h_dptr(d, bindex);
24553 +               if (h_d)
24554 +                       au_set_h_dptr(d, bindex, NULL);
24555 +       }
24556 +       au_set_dbbot(d, a->btgt);
24557 +
24558 +       sb = d->d_sb;
24559 +       i = a->src_inode;
24560 +       if (au_opt_test(au_mntflags(sb), PLINK) && au_plink_test(i))
24561 +               return; /* success */
24562 +
24563 +       bbot = au_ibbot(i);
24564 +       for (bindex = a->btgt + 1; bindex <= bbot; bindex++) {
24565 +               h_i = au_h_iptr(i, bindex);
24566 +               if (h_i) {
24567 +                       au_xino_write(sb, bindex, h_i->i_ino, /*ino*/0);
24568 +                       /* ignore this error */
24569 +                       au_set_h_iptr(i, bindex, NULL, 0);
24570 +               }
24571 +       }
24572 +       au_set_ibbot(i, a->btgt);
24573 +}
24574 +
24575 +/* ---------------------------------------------------------------------- */
24576 +
24577 +/* mainly for link(2) and rename(2) */
24578 +int au_wbr(struct dentry *dentry, aufs_bindex_t btgt)
24579 +{
24580 +       aufs_bindex_t bdiropq, bwh;
24581 +       struct dentry *parent;
24582 +       struct au_branch *br;
24583 +
24584 +       parent = dentry->d_parent;
24585 +       IMustLock(d_inode(parent)); /* dir is locked */
24586 +
24587 +       bdiropq = au_dbdiropq(parent);
24588 +       bwh = au_dbwh(dentry);
24589 +       br = au_sbr(dentry->d_sb, btgt);
24590 +       if (au_br_rdonly(br)
24591 +           || (0 <= bdiropq && bdiropq < btgt)
24592 +           || (0 <= bwh && bwh < btgt))
24593 +               btgt = -1;
24594 +
24595 +       AuDbg("btgt %d\n", btgt);
24596 +       return btgt;
24597 +}
24598 +
24599 +/* sets src_btop, dst_btop and btgt */
24600 +static int au_ren_wbr(struct au_ren_args *a)
24601 +{
24602 +       int err;
24603 +       struct au_wr_dir_args wr_dir_args = {
24604 +               /* .force_btgt  = -1, */
24605 +               .flags          = AuWrDir_ADD_ENTRY
24606 +       };
24607 +
24608 +       a->src_btop = au_dbtop(a->src_dentry);
24609 +       a->dst_btop = au_dbtop(a->dst_dentry);
24610 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC)
24611 +           || au_ftest_ren(a->auren_flags, ISDIR_DST))
24612 +               au_fset_wrdir(wr_dir_args.flags, ISDIR);
24613 +       wr_dir_args.force_btgt = a->src_btop;
24614 +       if (a->dst_inode && a->dst_btop < a->src_btop)
24615 +               wr_dir_args.force_btgt = a->dst_btop;
24616 +       wr_dir_args.force_btgt = au_wbr(a->dst_dentry, wr_dir_args.force_btgt);
24617 +       err = au_wr_dir(a->dst_dentry, a->src_dentry, &wr_dir_args);
24618 +       a->btgt = err;
24619 +       if (a->exchange)
24620 +               au_update_dbtop(a->dst_dentry);
24621 +
24622 +       return err;
24623 +}
24624 +
24625 +static void au_ren_dt(struct au_ren_args *a)
24626 +{
24627 +       a->h_path.dentry = a->src_h_parent;
24628 +       au_dtime_store(a->src_dt + AuPARENT, a->src_parent, &a->h_path);
24629 +       if (!au_ftest_ren(a->auren_flags, ISSAMEDIR)) {
24630 +               a->h_path.dentry = a->dst_h_parent;
24631 +               au_dtime_store(a->dst_dt + AuPARENT, a->dst_parent, &a->h_path);
24632 +       }
24633 +
24634 +       au_fclr_ren(a->auren_flags, DT_DSTDIR);
24635 +       if (!au_ftest_ren(a->auren_flags, ISDIR_SRC)
24636 +           && !a->exchange)
24637 +               return;
24638 +
24639 +       a->h_path.dentry = a->src_h_dentry;
24640 +       au_dtime_store(a->src_dt + AuCHILD, a->src_dentry, &a->h_path);
24641 +       if (d_is_positive(a->dst_h_dentry)) {
24642 +               au_fset_ren(a->auren_flags, DT_DSTDIR);
24643 +               a->h_path.dentry = a->dst_h_dentry;
24644 +               au_dtime_store(a->dst_dt + AuCHILD, a->dst_dentry, &a->h_path);
24645 +       }
24646 +}
24647 +
24648 +static void au_ren_rev_dt(int err, struct au_ren_args *a)
24649 +{
24650 +       struct dentry *h_d;
24651 +       struct inode *h_inode;
24652 +
24653 +       au_dtime_revert(a->src_dt + AuPARENT);
24654 +       if (!au_ftest_ren(a->auren_flags, ISSAMEDIR))
24655 +               au_dtime_revert(a->dst_dt + AuPARENT);
24656 +
24657 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC) && err != -EIO) {
24658 +               h_d = a->src_dt[AuCHILD].dt_h_path.dentry;
24659 +               h_inode = d_inode(h_d);
24660 +               inode_lock_nested(h_inode, AuLsc_I_CHILD);
24661 +               au_dtime_revert(a->src_dt + AuCHILD);
24662 +               inode_unlock(h_inode);
24663 +
24664 +               if (au_ftest_ren(a->auren_flags, DT_DSTDIR)) {
24665 +                       h_d = a->dst_dt[AuCHILD].dt_h_path.dentry;
24666 +                       h_inode = d_inode(h_d);
24667 +                       inode_lock_nested(h_inode, AuLsc_I_CHILD);
24668 +                       au_dtime_revert(a->dst_dt + AuCHILD);
24669 +                       inode_unlock(h_inode);
24670 +               }
24671 +       }
24672 +}
24673 +
24674 +/* ---------------------------------------------------------------------- */
24675 +
24676 +int aufs_rename(struct user_namespace *userns,
24677 +               struct inode *_src_dir, struct dentry *_src_dentry,
24678 +               struct inode *_dst_dir, struct dentry *_dst_dentry,
24679 +               unsigned int _flags)
24680 +{
24681 +       int err, lock_flags;
24682 +       void *rev;
24683 +       /* reduce stack space */
24684 +       struct au_ren_args *a;
24685 +       struct au_pin pin;
24686 +
24687 +       AuDbg("%pd, %pd, 0x%x\n", _src_dentry, _dst_dentry, _flags);
24688 +       IMustLock(_src_dir);
24689 +       IMustLock(_dst_dir);
24690 +
24691 +       err = -EINVAL;
24692 +       if (unlikely(_flags & RENAME_WHITEOUT))
24693 +               goto out;
24694 +
24695 +       err = -ENOMEM;
24696 +       BUILD_BUG_ON(sizeof(*a) > PAGE_SIZE);
24697 +       a = kzalloc(sizeof(*a), GFP_NOFS);
24698 +       if (unlikely(!a))
24699 +               goto out;
24700 +
24701 +       a->flags = _flags;
24702 +       BUILD_BUG_ON(sizeof(a->exchange) == sizeof(u8)
24703 +                    && RENAME_EXCHANGE > U8_MAX);
24704 +       a->exchange = _flags & RENAME_EXCHANGE;
24705 +       a->src_dir = _src_dir;
24706 +       a->src_dentry = _src_dentry;
24707 +       a->src_inode = NULL;
24708 +       if (d_really_is_positive(a->src_dentry))
24709 +               a->src_inode = d_inode(a->src_dentry);
24710 +       a->src_parent = a->src_dentry->d_parent; /* dir inode is locked */
24711 +       a->dst_dir = _dst_dir;
24712 +       a->dst_dentry = _dst_dentry;
24713 +       a->dst_inode = NULL;
24714 +       if (d_really_is_positive(a->dst_dentry))
24715 +               a->dst_inode = d_inode(a->dst_dentry);
24716 +       a->dst_parent = a->dst_dentry->d_parent; /* dir inode is locked */
24717 +       if (a->dst_inode) {
24718 +               /*
24719 +                * if EXCHANGE && src is non-dir && dst is dir,
24720 +                * dst is not locked.
24721 +                */
24722 +               /* IMustLock(a->dst_inode); */
24723 +               au_igrab(a->dst_inode);
24724 +       }
24725 +
24726 +       err = -ENOTDIR;
24727 +       lock_flags = AuLock_FLUSH | AuLock_NOPLM | AuLock_GEN;
24728 +       if (d_is_dir(a->src_dentry)) {
24729 +               au_fset_ren(a->auren_flags, ISDIR_SRC);
24730 +               if (unlikely(!a->exchange
24731 +                            && d_really_is_positive(a->dst_dentry)
24732 +                            && !d_is_dir(a->dst_dentry)))
24733 +                       goto out_free;
24734 +               lock_flags |= AuLock_DIRS;
24735 +       }
24736 +       if (a->dst_inode && d_is_dir(a->dst_dentry)) {
24737 +               au_fset_ren(a->auren_flags, ISDIR_DST);
24738 +               if (unlikely(!a->exchange
24739 +                            && d_really_is_positive(a->src_dentry)
24740 +                            && !d_is_dir(a->src_dentry)))
24741 +                       goto out_free;
24742 +               lock_flags |= AuLock_DIRS;
24743 +       }
24744 +       err = aufs_read_and_write_lock2(a->dst_dentry, a->src_dentry,
24745 +                                       lock_flags);
24746 +       if (unlikely(err))
24747 +               goto out_free;
24748 +
24749 +       err = au_d_hashed_positive(a->src_dentry);
24750 +       if (unlikely(err))
24751 +               goto out_unlock;
24752 +       err = -ENOENT;
24753 +       if (a->dst_inode) {
24754 +               /*
24755 +                * If it is a dir, VFS unhash it before this
24756 +                * function. It means we cannot rely upon d_unhashed().
24757 +                */
24758 +               if (unlikely(!a->dst_inode->i_nlink))
24759 +                       goto out_unlock;
24760 +               if (!au_ftest_ren(a->auren_flags, ISDIR_DST)) {
24761 +                       err = au_d_hashed_positive(a->dst_dentry);
24762 +                       if (unlikely(err && !a->exchange))
24763 +                               goto out_unlock;
24764 +               } else if (unlikely(IS_DEADDIR(a->dst_inode)))
24765 +                       goto out_unlock;
24766 +       } else if (unlikely(d_unhashed(a->dst_dentry)))
24767 +               goto out_unlock;
24768 +
24769 +       /*
24770 +        * is it possible?
24771 +        * yes, it happened (in linux-3.3-rcN) but I don't know why.
24772 +        * there may exist a problem somewhere else.
24773 +        */
24774 +       err = -EINVAL;
24775 +       if (unlikely(d_inode(a->dst_parent) == d_inode(a->src_dentry)))
24776 +               goto out_unlock;
24777 +
24778 +       au_fset_ren(a->auren_flags, ISSAMEDIR); /* temporary */
24779 +       di_write_lock_parent(a->dst_parent);
24780 +
24781 +       /* which branch we process */
24782 +       err = au_ren_wbr(a);
24783 +       if (unlikely(err < 0))
24784 +               goto out_parent;
24785 +       a->br = au_sbr(a->dst_dentry->d_sb, a->btgt);
24786 +       a->h_path.mnt = au_br_mnt(a->br);
24787 +
24788 +       /* are they available to be renamed */
24789 +       err = au_ren_may_dir(a);
24790 +       if (unlikely(err))
24791 +               goto out_children;
24792 +
24793 +       /* prepare the writable parent dir on the same branch */
24794 +       if (a->dst_btop == a->btgt) {
24795 +               au_fset_ren(a->auren_flags, WHDST);
24796 +       } else {
24797 +               err = au_cpup_dirs(a->dst_dentry, a->btgt);
24798 +               if (unlikely(err))
24799 +                       goto out_children;
24800 +       }
24801 +
24802 +       err = 0;
24803 +       if (!a->exchange) {
24804 +               if (a->src_dir != a->dst_dir) {
24805 +                       /*
24806 +                        * this temporary unlock is safe,
24807 +                        * because both dir->i_mutex are locked.
24808 +                        */
24809 +                       di_write_unlock(a->dst_parent);
24810 +                       di_write_lock_parent(a->src_parent);
24811 +                       err = au_wr_dir_need_wh(a->src_dentry,
24812 +                                               au_ftest_ren(a->auren_flags,
24813 +                                                            ISDIR_SRC),
24814 +                                               &a->btgt);
24815 +                       di_write_unlock(a->src_parent);
24816 +                       di_write_lock2_parent(a->src_parent, a->dst_parent,
24817 +                                             /*isdir*/1);
24818 +                       au_fclr_ren(a->auren_flags, ISSAMEDIR);
24819 +               } else
24820 +                       err = au_wr_dir_need_wh(a->src_dentry,
24821 +                                               au_ftest_ren(a->auren_flags,
24822 +                                                            ISDIR_SRC),
24823 +                                               &a->btgt);
24824 +       }
24825 +       if (unlikely(err < 0))
24826 +               goto out_children;
24827 +       if (err)
24828 +               au_fset_ren(a->auren_flags, WHSRC);
24829 +
24830 +       /* cpup src */
24831 +       if (a->src_btop != a->btgt) {
24832 +               err = au_pin(&pin, a->src_dentry, a->btgt,
24833 +                            au_opt_udba(a->src_dentry->d_sb),
24834 +                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
24835 +               if (!err) {
24836 +                       struct au_cp_generic cpg = {
24837 +                               .dentry = a->src_dentry,
24838 +                               .bdst   = a->btgt,
24839 +                               .bsrc   = a->src_btop,
24840 +                               .len    = -1,
24841 +                               .pin    = &pin,
24842 +                               .flags  = AuCpup_DTIME | AuCpup_HOPEN
24843 +                       };
24844 +                       AuDebugOn(au_dbtop(a->src_dentry) != a->src_btop);
24845 +                       err = au_sio_cpup_simple(&cpg);
24846 +                       au_unpin(&pin);
24847 +               }
24848 +               if (unlikely(err))
24849 +                       goto out_children;
24850 +               a->src_btop = a->btgt;
24851 +               a->src_h_dentry = au_h_dptr(a->src_dentry, a->btgt);
24852 +               if (!a->exchange)
24853 +                       au_fset_ren(a->auren_flags, WHSRC);
24854 +       }
24855 +
24856 +       /* cpup dst */
24857 +       if (a->exchange && a->dst_inode
24858 +           && a->dst_btop != a->btgt) {
24859 +               err = au_pin(&pin, a->dst_dentry, a->btgt,
24860 +                            au_opt_udba(a->dst_dentry->d_sb),
24861 +                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
24862 +               if (!err) {
24863 +                       struct au_cp_generic cpg = {
24864 +                               .dentry = a->dst_dentry,
24865 +                               .bdst   = a->btgt,
24866 +                               .bsrc   = a->dst_btop,
24867 +                               .len    = -1,
24868 +                               .pin    = &pin,
24869 +                               .flags  = AuCpup_DTIME | AuCpup_HOPEN
24870 +                       };
24871 +                       err = au_sio_cpup_simple(&cpg);
24872 +                       au_unpin(&pin);
24873 +               }
24874 +               if (unlikely(err))
24875 +                       goto out_children;
24876 +               a->dst_btop = a->btgt;
24877 +               a->dst_h_dentry = au_h_dptr(a->dst_dentry, a->btgt);
24878 +       }
24879 +
24880 +       /* lock them all */
24881 +       err = au_ren_lock(a);
24882 +       if (unlikely(err))
24883 +               /* leave the copied-up one */
24884 +               goto out_children;
24885 +
24886 +       if (!a->exchange) {
24887 +               if (!au_opt_test(au_mntflags(a->dst_dir->i_sb), UDBA_NONE))
24888 +                       err = au_may_ren(a);
24889 +               else if (unlikely(a->dst_dentry->d_name.len > AUFS_MAX_NAMELEN))
24890 +                       err = -ENAMETOOLONG;
24891 +               if (unlikely(err))
24892 +                       goto out_hdir;
24893 +       }
24894 +
24895 +       /* store timestamps to be revertible */
24896 +       au_ren_dt(a);
24897 +
24898 +       /* store dirren info */
24899 +       if (au_ftest_ren(a->auren_flags, DIRREN)) {
24900 +               err = au_dr_rename(a->src_dentry, a->btgt,
24901 +                                  &a->dst_dentry->d_name, &rev);
24902 +               AuTraceErr(err);
24903 +               if (unlikely(err))
24904 +                       goto out_dt;
24905 +       }
24906 +
24907 +       /* here we go */
24908 +       err = do_rename(a);
24909 +       if (unlikely(err))
24910 +               goto out_dirren;
24911 +
24912 +       if (au_ftest_ren(a->auren_flags, DIRREN))
24913 +               au_dr_rename_fin(a->src_dentry, a->btgt, rev);
24914 +
24915 +       /* update dir attributes */
24916 +       au_ren_refresh_dir(a);
24917 +
24918 +       /* dput/iput all lower dentries */
24919 +       au_ren_refresh(a);
24920 +
24921 +       goto out_hdir; /* success */
24922 +
24923 +out_dirren:
24924 +       if (au_ftest_ren(a->auren_flags, DIRREN))
24925 +               au_dr_rename_rev(a->src_dentry, a->btgt, rev);
24926 +out_dt:
24927 +       au_ren_rev_dt(err, a);
24928 +out_hdir:
24929 +       au_ren_unlock(a);
24930 +out_children:
24931 +       au_nhash_wh_free(&a->whlist);
24932 +       if (err && a->dst_inode && a->dst_btop != a->btgt) {
24933 +               AuDbg("btop %d, btgt %d\n", a->dst_btop, a->btgt);
24934 +               au_set_h_dptr(a->dst_dentry, a->btgt, NULL);
24935 +               au_set_dbtop(a->dst_dentry, a->dst_btop);
24936 +       }
24937 +out_parent:
24938 +       if (!err) {
24939 +               if (d_unhashed(a->src_dentry))
24940 +                       au_fset_ren(a->auren_flags, DROPPED_SRC);
24941 +               if (d_unhashed(a->dst_dentry))
24942 +                       au_fset_ren(a->auren_flags, DROPPED_DST);
24943 +               if (!a->exchange)
24944 +                       d_move(a->src_dentry, a->dst_dentry);
24945 +               else {
24946 +                       d_exchange(a->src_dentry, a->dst_dentry);
24947 +                       if (au_ftest_ren(a->auren_flags, DROPPED_DST))
24948 +                               d_drop(a->dst_dentry);
24949 +               }
24950 +               if (au_ftest_ren(a->auren_flags, DROPPED_SRC))
24951 +                       d_drop(a->src_dentry);
24952 +       } else {
24953 +               au_update_dbtop(a->dst_dentry);
24954 +               if (!a->dst_inode)
24955 +                       d_drop(a->dst_dentry);
24956 +       }
24957 +       if (au_ftest_ren(a->auren_flags, ISSAMEDIR))
24958 +               di_write_unlock(a->dst_parent);
24959 +       else
24960 +               di_write_unlock2(a->src_parent, a->dst_parent);
24961 +out_unlock:
24962 +       aufs_read_and_write_unlock2(a->dst_dentry, a->src_dentry);
24963 +out_free:
24964 +       iput(a->dst_inode);
24965 +       if (a->thargs)
24966 +               au_whtmp_rmdir_free(a->thargs);
24967 +       au_kfree_rcu(a);
24968 +out:
24969 +       AuTraceErr(err);
24970 +       return err;
24971 +}
24972 diff -urN /usr/share/empty/fs/aufs/Kconfig linux/fs/aufs/Kconfig
24973 --- /usr/share/empty/fs/aufs/Kconfig    1970-01-01 01:00:00.000000000 +0100
24974 +++ linux/fs/aufs/Kconfig       2022-03-21 14:49:05.716633010 +0100
24975 @@ -0,0 +1,199 @@
24976 +# SPDX-License-Identifier: GPL-2.0
24977 +config AUFS_FS
24978 +       tristate "Aufs (Advanced multi layered unification filesystem) support"
24979 +       help
24980 +       Aufs is a stackable unification filesystem such as Unionfs,
24981 +       which unifies several directories and provides a merged single
24982 +       directory.
24983 +       In the early days, aufs was entirely re-designed and
24984 +       re-implemented Unionfs Version 1.x series. Introducing many
24985 +       original ideas, approaches and improvements, it becomes totally
24986 +       different from Unionfs while keeping the basic features.
24987 +
24988 +if AUFS_FS
24989 +choice
24990 +       prompt "Maximum number of branches"
24991 +       default AUFS_BRANCH_MAX_127
24992 +       help
24993 +       Specifies the maximum number of branches (or member directories)
24994 +       in a single aufs. The larger value consumes more system
24995 +       resources and has a minor impact to performance.
24996 +config AUFS_BRANCH_MAX_127
24997 +       bool "127"
24998 +       help
24999 +       Specifies the maximum number of branches (or member directories)
25000 +       in a single aufs. The larger value consumes more system
25001 +       resources and has a minor impact to performance.
25002 +config AUFS_BRANCH_MAX_511
25003 +       bool "511"
25004 +       help
25005 +       Specifies the maximum number of branches (or member directories)
25006 +       in a single aufs. The larger value consumes more system
25007 +       resources and has a minor impact to performance.
25008 +config AUFS_BRANCH_MAX_1023
25009 +       bool "1023"
25010 +       help
25011 +       Specifies the maximum number of branches (or member directories)
25012 +       in a single aufs. The larger value consumes more system
25013 +       resources and has a minor impact to performance.
25014 +config AUFS_BRANCH_MAX_32767
25015 +       bool "32767"
25016 +       help
25017 +       Specifies the maximum number of branches (or member directories)
25018 +       in a single aufs. The larger value consumes more system
25019 +       resources and has a minor impact to performance.
25020 +endchoice
25021 +
25022 +config AUFS_SBILIST
25023 +       bool
25024 +       depends on AUFS_MAGIC_SYSRQ || PROC_FS
25025 +       default y
25026 +       help
25027 +       Automatic configuration for internal use.
25028 +       When aufs supports Magic SysRq or /proc, enabled automatically.
25029 +
25030 +config AUFS_HNOTIFY
25031 +       bool "Detect direct branch access (bypassing aufs)"
25032 +       help
25033 +       If you want to modify files on branches directly, eg. bypassing aufs,
25034 +       and want aufs to detect the changes of them fully, then enable this
25035 +       option and use 'udba=notify' mount option.
25036 +       Currently there is only one available configuration, "fsnotify".
25037 +       It will have a negative impact to the performance.
25038 +       See detail in aufs.5.
25039 +
25040 +choice
25041 +       prompt "method" if AUFS_HNOTIFY
25042 +       default AUFS_HFSNOTIFY
25043 +config AUFS_HFSNOTIFY
25044 +       bool "fsnotify"
25045 +       select FSNOTIFY
25046 +endchoice
25047 +
25048 +config AUFS_EXPORT
25049 +       bool "NFS-exportable aufs"
25050 +       depends on EXPORTFS
25051 +       help
25052 +       If you want to export your mounted aufs via NFS, then enable this
25053 +       option. There are several requirements for this configuration.
25054 +       See detail in aufs.5.
25055 +
25056 +config AUFS_INO_T_64
25057 +       bool
25058 +       depends on AUFS_EXPORT
25059 +       depends on 64BIT && !(ALPHA || S390)
25060 +       default y
25061 +       help
25062 +       Automatic configuration for internal use.
25063 +       /* typedef unsigned long/int __kernel_ino_t */
25064 +       /* alpha and s390x are int */
25065 +
25066 +config AUFS_XATTR
25067 +       bool "support for XATTR/EA (including Security Labels)"
25068 +       help
25069 +       If your branch fs supports XATTR/EA and you want to make them
25070 +       available in aufs too, then enable this opsion and specify the
25071 +       branch attributes for EA.
25072 +       See detail in aufs.5.
25073 +
25074 +config AUFS_FHSM
25075 +       bool "File-based Hierarchical Storage Management"
25076 +       help
25077 +       Hierarchical Storage Management (or HSM) is a well-known feature
25078 +       in the storage world. Aufs provides this feature as file-based.
25079 +       with multiple branches.
25080 +       These multiple branches are prioritized, ie. the topmost one
25081 +       should be the fastest drive and be used heavily.
25082 +
25083 +config AUFS_RDU
25084 +       bool "Readdir in userspace"
25085 +       help
25086 +       Aufs has two methods to provide a merged view for a directory,
25087 +       by a user-space library and by kernel-space natively. The latter
25088 +       is always enabled but sometimes large and slow.
25089 +       If you enable this option, install the library in aufs2-util
25090 +       package, and set some environment variables for your readdir(3),
25091 +       then the work will be handled in user-space which generally
25092 +       shows better performance in most cases.
25093 +       See detail in aufs.5.
25094 +
25095 +config AUFS_DIRREN
25096 +       bool "Workaround for rename(2)-ing a directory"
25097 +       help
25098 +       By default, aufs returns EXDEV error in renameing a dir who has
25099 +       his child on the lower branch, since it is a bad idea to issue
25100 +       rename(2) internally for every lower branch. But user may not
25101 +       accept this behaviour. So here is a workaround to allow such
25102 +       rename(2) and store some extra infromation on the writable
25103 +       branch. Obviously this costs high (and I don't like it).
25104 +       To use this feature, you need to enable this configuration AND
25105 +       to specify the mount option `dirren.'
25106 +       See details in aufs.5 and the design documents.
25107 +
25108 +config AUFS_SHWH
25109 +       bool "Show whiteouts"
25110 +       help
25111 +       If you want to make the whiteouts in aufs visible, then enable
25112 +       this option and specify 'shwh' mount option. Although it may
25113 +       sounds like philosophy or something, but in technically it
25114 +       simply shows the name of whiteout with keeping its behaviour.
25115 +
25116 +config AUFS_BR_RAMFS
25117 +       bool "Ramfs (initramfs/rootfs) as an aufs branch"
25118 +       help
25119 +       If you want to use ramfs as an aufs branch fs, then enable this
25120 +       option. Generally tmpfs is recommended.
25121 +       Aufs prohibited them to be a branch fs by default, because
25122 +       initramfs becomes unusable after switch_root or something
25123 +       generally. If you sets initramfs as an aufs branch and boot your
25124 +       system by switch_root, you will meet a problem easily since the
25125 +       files in initramfs may be inaccessible.
25126 +       Unless you are going to use ramfs as an aufs branch fs without
25127 +       switch_root or something, leave it N.
25128 +
25129 +config AUFS_BR_FUSE
25130 +       bool "Fuse fs as an aufs branch"
25131 +       depends on FUSE_FS
25132 +       select AUFS_POLL
25133 +       help
25134 +       If you want to use fuse-based userspace filesystem as an aufs
25135 +       branch fs, then enable this option.
25136 +       It implements the internal poll(2) operation which is
25137 +       implemented by fuse only (curretnly).
25138 +
25139 +config AUFS_POLL
25140 +       bool
25141 +       help
25142 +       Automatic configuration for internal use.
25143 +
25144 +config AUFS_BR_HFSPLUS
25145 +       bool "Hfsplus as an aufs branch"
25146 +       depends on HFSPLUS_FS
25147 +       default y
25148 +       help
25149 +       If you want to use hfsplus fs as an aufs branch fs, then enable
25150 +       this option. This option introduces a small overhead at
25151 +       copying-up a file on hfsplus.
25152 +
25153 +config AUFS_BDEV_LOOP
25154 +       bool
25155 +       depends on BLK_DEV_LOOP
25156 +       default y
25157 +       help
25158 +       Automatic configuration for internal use.
25159 +       Convert =[ym] into =y.
25160 +
25161 +config AUFS_DEBUG
25162 +       bool "Debug aufs"
25163 +       help
25164 +       Enable this to compile aufs internal debug code.
25165 +       It will have a negative impact to the performance.
25166 +
25167 +config AUFS_MAGIC_SYSRQ
25168 +       bool
25169 +       depends on AUFS_DEBUG && MAGIC_SYSRQ
25170 +       default y
25171 +       help
25172 +       Automatic configuration for internal use.
25173 +       When aufs supports Magic SysRq, enabled automatically.
25174 +endif
25175 diff -urN /usr/share/empty/fs/aufs/lcnt.h linux/fs/aufs/lcnt.h
25176 --- /usr/share/empty/fs/aufs/lcnt.h     1970-01-01 01:00:00.000000000 +0100
25177 +++ linux/fs/aufs/lcnt.h        2022-03-21 14:49:05.726633010 +0100
25178 @@ -0,0 +1,186 @@
25179 +/* SPDX-License-Identifier: GPL-2.0 */
25180 +/*
25181 + * Copyright (C) 2018-2021 Junjiro R. Okajima
25182 + *
25183 + * This program, aufs is free software; you can redistribute it and/or modify
25184 + * it under the terms of the GNU General Public License as published by
25185 + * the Free Software Foundation; either version 2 of the License, or
25186 + * (at your option) any later version.
25187 + *
25188 + * This program is distributed in the hope that it will be useful,
25189 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
25190 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25191 + * GNU General Public License for more details.
25192 + *
25193 + * You should have received a copy of the GNU General Public License
25194 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25195 + */
25196 +
25197 +/*
25198 + * simple long counter wrapper
25199 + */
25200 +
25201 +#ifndef __AUFS_LCNT_H__
25202 +#define __AUFS_LCNT_H__
25203 +
25204 +#ifdef __KERNEL__
25205 +
25206 +#include "debug.h"
25207 +
25208 +#define AuLCntATOMIC   1
25209 +#define AuLCntPCPUCNT  2
25210 +/*
25211 + * why does percpu_refcount require extra synchronize_rcu()s in
25212 + * au_br_do_free()
25213 + */
25214 +#define AuLCntPCPUREF  3
25215 +
25216 +/* #define AuLCntChosen        AuLCntATOMIC */
25217 +#define AuLCntChosen   AuLCntPCPUCNT
25218 +/* #define AuLCntChosen        AuLCntPCPUREF */
25219 +
25220 +#if AuLCntChosen == AuLCntATOMIC
25221 +#include <linux/atomic.h>
25222 +
25223 +typedef atomic_long_t au_lcnt_t;
25224 +
25225 +static inline int au_lcnt_init(au_lcnt_t *cnt, void *release __maybe_unused)
25226 +{
25227 +       atomic_long_set(cnt, 0);
25228 +       return 0;
25229 +}
25230 +
25231 +static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
25232 +{
25233 +       /* empty */
25234 +}
25235 +
25236 +static inline void au_lcnt_fin(au_lcnt_t *cnt __maybe_unused,
25237 +                              int do_sync __maybe_unused)
25238 +{
25239 +       /* empty */
25240 +}
25241 +
25242 +static inline void au_lcnt_inc(au_lcnt_t *cnt)
25243 +{
25244 +       atomic_long_inc(cnt);
25245 +}
25246 +
25247 +static inline void au_lcnt_dec(au_lcnt_t *cnt)
25248 +{
25249 +       atomic_long_dec(cnt);
25250 +}
25251 +
25252 +static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev __maybe_unused)
25253 +{
25254 +       return atomic_long_read(cnt);
25255 +}
25256 +#endif
25257 +
25258 +#if AuLCntChosen == AuLCntPCPUCNT
25259 +#include <linux/percpu_counter.h>
25260 +
25261 +typedef struct percpu_counter au_lcnt_t;
25262 +
25263 +static inline int au_lcnt_init(au_lcnt_t *cnt, void *release __maybe_unused)
25264 +{
25265 +       return percpu_counter_init(cnt, 0, GFP_NOFS);
25266 +}
25267 +
25268 +static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
25269 +{
25270 +       /* empty */
25271 +}
25272 +
25273 +static inline void au_lcnt_fin(au_lcnt_t *cnt, int do_sync __maybe_unused)
25274 +{
25275 +       percpu_counter_destroy(cnt);
25276 +}
25277 +
25278 +static inline void au_lcnt_inc(au_lcnt_t *cnt)
25279 +{
25280 +       percpu_counter_inc(cnt);
25281 +}
25282 +
25283 +static inline void au_lcnt_dec(au_lcnt_t *cnt)
25284 +{
25285 +       percpu_counter_dec(cnt);
25286 +}
25287 +
25288 +static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev __maybe_unused)
25289 +{
25290 +       s64 n;
25291 +
25292 +       n = percpu_counter_sum(cnt);
25293 +       BUG_ON(n < 0);
25294 +       if (LONG_MAX != LLONG_MAX
25295 +           && n > LONG_MAX)
25296 +               AuWarn1("%s\n", "wrap-around");
25297 +
25298 +       return n;
25299 +}
25300 +#endif
25301 +
25302 +#if AuLCntChosen == AuLCntPCPUREF
25303 +#include <linux/percpu-refcount.h>
25304 +
25305 +typedef struct percpu_ref au_lcnt_t;
25306 +
25307 +static inline int au_lcnt_init(au_lcnt_t *cnt, percpu_ref_func_t *release)
25308 +{
25309 +       if (!release)
25310 +               release = percpu_ref_exit;
25311 +       return percpu_ref_init(cnt, release, /*percpu mode*/0, GFP_NOFS);
25312 +}
25313 +
25314 +static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
25315 +{
25316 +       synchronize_rcu();
25317 +}
25318 +
25319 +static inline void au_lcnt_fin(au_lcnt_t *cnt, int do_sync)
25320 +{
25321 +       percpu_ref_kill(cnt);
25322 +       if (do_sync)
25323 +               au_lcnt_wait_for_fin(cnt);
25324 +}
25325 +
25326 +static inline void au_lcnt_inc(au_lcnt_t *cnt)
25327 +{
25328 +       percpu_ref_get(cnt);
25329 +}
25330 +
25331 +static inline void au_lcnt_dec(au_lcnt_t *cnt)
25332 +{
25333 +       percpu_ref_put(cnt);
25334 +}
25335 +
25336 +/*
25337 + * avoid calling this func as possible.
25338 + */
25339 +static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev)
25340 +{
25341 +       long l;
25342 +
25343 +       percpu_ref_switch_to_atomic_sync(cnt);
25344 +       l = atomic_long_read(&cnt->count);
25345 +       if (do_rev)
25346 +               percpu_ref_switch_to_percpu(cnt);
25347 +
25348 +       /* percpu_ref is initialized by 1 instead of 0 */
25349 +       return l - 1;
25350 +}
25351 +#endif
25352 +
25353 +#ifdef CONFIG_AUFS_DEBUG
25354 +#define AuLCntZero(val) do {                   \
25355 +       long l = val;                           \
25356 +       if (l)                                  \
25357 +               AuDbg("%s = %ld\n", #val, l);   \
25358 +} while (0)
25359 +#else
25360 +#define AuLCntZero(val)                do {} while (0)
25361 +#endif
25362 +
25363 +#endif /* __KERNEL__ */
25364 +#endif /* __AUFS_LCNT_H__ */
25365 diff -urN /usr/share/empty/fs/aufs/loop.c linux/fs/aufs/loop.c
25366 --- /usr/share/empty/fs/aufs/loop.c     1970-01-01 01:00:00.000000000 +0100
25367 +++ linux/fs/aufs/loop.c        2022-03-21 14:49:05.726633010 +0100
25368 @@ -0,0 +1,148 @@
25369 +// SPDX-License-Identifier: GPL-2.0
25370 +/*
25371 + * Copyright (C) 2005-2021 Junjiro R. Okajima
25372 + *
25373 + * This program, aufs is free software; you can redistribute it and/or modify
25374 + * it under the terms of the GNU General Public License as published by
25375 + * the Free Software Foundation; either version 2 of the License, or
25376 + * (at your option) any later version.
25377 + *
25378 + * This program is distributed in the hope that it will be useful,
25379 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
25380 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25381 + * GNU General Public License for more details.
25382 + *
25383 + * You should have received a copy of the GNU General Public License
25384 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25385 + */
25386 +
25387 +/*
25388 + * support for loopback block device as a branch
25389 + */
25390 +
25391 +#include "aufs.h"
25392 +
25393 +/* added into drivers/block/loop.c */
25394 +static struct file *(*backing_file_func)(struct super_block *sb);
25395 +
25396 +/*
25397 + * test if two lower dentries have overlapping branches.
25398 + */
25399 +int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding)
25400 +{
25401 +       struct super_block *h_sb;
25402 +       struct file *backing_file;
25403 +
25404 +       if (unlikely(!backing_file_func)) {
25405 +               /* don't load "loop" module here */
25406 +               backing_file_func = symbol_get(loop_backing_file);
25407 +               if (unlikely(!backing_file_func))
25408 +                       /* "loop" module is not loaded */
25409 +                       return 0;
25410 +       }
25411 +
25412 +       h_sb = h_adding->d_sb;
25413 +       backing_file = backing_file_func(h_sb);
25414 +       if (!backing_file)
25415 +               return 0;
25416 +
25417 +       h_adding = backing_file->f_path.dentry;
25418 +       /*
25419 +        * h_adding can be local NFS.
25420 +        * in this case aufs cannot detect the loop.
25421 +        */
25422 +       if (unlikely(h_adding->d_sb == sb))
25423 +               return 1;
25424 +       return !!au_test_subdir(h_adding, sb->s_root);
25425 +}
25426 +
25427 +/* true if a kernel thread named 'loop[0-9].*' accesses a file */
25428 +int au_test_loopback_kthread(void)
25429 +{
25430 +       int ret;
25431 +       struct task_struct *tsk = current;
25432 +       char c, comm[sizeof(tsk->comm)];
25433 +
25434 +       ret = 0;
25435 +       if (tsk->flags & PF_KTHREAD) {
25436 +               get_task_comm(comm, tsk);
25437 +               c = comm[4];
25438 +               ret = ('0' <= c && c <= '9'
25439 +                      && !strncmp(comm, "loop", 4));
25440 +       }
25441 +
25442 +       return ret;
25443 +}
25444 +
25445 +/* ---------------------------------------------------------------------- */
25446 +
25447 +#define au_warn_loopback_step  16
25448 +static int au_warn_loopback_nelem = au_warn_loopback_step;
25449 +static unsigned long *au_warn_loopback_array;
25450 +
25451 +void au_warn_loopback(struct super_block *h_sb)
25452 +{
25453 +       int i, new_nelem;
25454 +       unsigned long *a, magic;
25455 +       static DEFINE_SPINLOCK(spin);
25456 +
25457 +       magic = h_sb->s_magic;
25458 +       spin_lock(&spin);
25459 +       a = au_warn_loopback_array;
25460 +       for (i = 0; i < au_warn_loopback_nelem && *a; i++)
25461 +               if (a[i] == magic) {
25462 +                       spin_unlock(&spin);
25463 +                       return;
25464 +               }
25465 +
25466 +       /* h_sb is new to us, print it */
25467 +       if (i < au_warn_loopback_nelem) {
25468 +               a[i] = magic;
25469 +               goto pr;
25470 +       }
25471 +
25472 +       /* expand the array */
25473 +       new_nelem = au_warn_loopback_nelem + au_warn_loopback_step;
25474 +       a = au_kzrealloc(au_warn_loopback_array,
25475 +                        au_warn_loopback_nelem * sizeof(unsigned long),
25476 +                        new_nelem * sizeof(unsigned long), GFP_ATOMIC,
25477 +                        /*may_shrink*/0);
25478 +       if (a) {
25479 +               au_warn_loopback_nelem = new_nelem;
25480 +               au_warn_loopback_array = a;
25481 +               a[i] = magic;
25482 +               goto pr;
25483 +       }
25484 +
25485 +       spin_unlock(&spin);
25486 +       AuWarn1("realloc failed, ignored\n");
25487 +       return;
25488 +
25489 +pr:
25490 +       spin_unlock(&spin);
25491 +       pr_warn("you may want to try another patch for loopback file "
25492 +               "on %s(0x%lx) branch\n", au_sbtype(h_sb), magic);
25493 +}
25494 +
25495 +int au_loopback_init(void)
25496 +{
25497 +       int err;
25498 +       struct super_block *sb __maybe_unused;
25499 +
25500 +       BUILD_BUG_ON(sizeof(sb->s_magic) != sizeof(*au_warn_loopback_array));
25501 +
25502 +       err = 0;
25503 +       au_warn_loopback_array = kcalloc(au_warn_loopback_step,
25504 +                                        sizeof(unsigned long), GFP_NOFS);
25505 +       if (unlikely(!au_warn_loopback_array))
25506 +               err = -ENOMEM;
25507 +
25508 +       return err;
25509 +}
25510 +
25511 +void au_loopback_fin(void)
25512 +{
25513 +       if (backing_file_func)
25514 +               symbol_put(loop_backing_file);
25515 +       au_kfree_try_rcu(au_warn_loopback_array);
25516 +}
25517 diff -urN /usr/share/empty/fs/aufs/loop.h linux/fs/aufs/loop.h
25518 --- /usr/share/empty/fs/aufs/loop.h     1970-01-01 01:00:00.000000000 +0100
25519 +++ linux/fs/aufs/loop.h        2022-03-21 14:49:05.726633010 +0100
25520 @@ -0,0 +1,55 @@
25521 +/* SPDX-License-Identifier: GPL-2.0 */
25522 +/*
25523 + * Copyright (C) 2005-2021 Junjiro R. Okajima
25524 + *
25525 + * This program, aufs is free software; you can redistribute it and/or modify
25526 + * it under the terms of the GNU General Public License as published by
25527 + * the Free Software Foundation; either version 2 of the License, or
25528 + * (at your option) any later version.
25529 + *
25530 + * This program is distributed in the hope that it will be useful,
25531 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
25532 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25533 + * GNU General Public License for more details.
25534 + *
25535 + * You should have received a copy of the GNU General Public License
25536 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25537 + */
25538 +
25539 +/*
25540 + * support for loopback mount as a branch
25541 + */
25542 +
25543 +#ifndef __AUFS_LOOP_H__
25544 +#define __AUFS_LOOP_H__
25545 +
25546 +#ifdef __KERNEL__
25547 +
25548 +struct dentry;
25549 +struct super_block;
25550 +
25551 +#ifdef CONFIG_AUFS_BDEV_LOOP
25552 +/* drivers/block/loop.c */
25553 +struct file *loop_backing_file(struct super_block *sb);
25554 +
25555 +/* loop.c */
25556 +int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding);
25557 +int au_test_loopback_kthread(void);
25558 +void au_warn_loopback(struct super_block *h_sb);
25559 +
25560 +int au_loopback_init(void);
25561 +void au_loopback_fin(void);
25562 +#else
25563 +AuStub(struct file *, loop_backing_file, return NULL, struct super_block *sb)
25564 +
25565 +AuStubInt0(au_test_loopback_overlap, struct super_block *sb,
25566 +          struct dentry *h_adding)
25567 +AuStubInt0(au_test_loopback_kthread, void)
25568 +AuStubVoid(au_warn_loopback, struct super_block *h_sb)
25569 +
25570 +AuStubInt0(au_loopback_init, void)
25571 +AuStubVoid(au_loopback_fin, void)
25572 +#endif /* BLK_DEV_LOOP */
25573 +
25574 +#endif /* __KERNEL__ */
25575 +#endif /* __AUFS_LOOP_H__ */
25576 diff -urN /usr/share/empty/fs/aufs/magic.mk linux/fs/aufs/magic.mk
25577 --- /usr/share/empty/fs/aufs/magic.mk   1970-01-01 01:00:00.000000000 +0100
25578 +++ linux/fs/aufs/magic.mk      2022-03-21 14:49:05.726633010 +0100
25579 @@ -0,0 +1,31 @@
25580 +# SPDX-License-Identifier: GPL-2.0
25581 +
25582 +# defined in ${srctree}/fs/fuse/inode.c
25583 +# tristate
25584 +ifdef CONFIG_FUSE_FS
25585 +ccflags-y += -DFUSE_SUPER_MAGIC=0x65735546
25586 +endif
25587 +
25588 +# defined in ${srctree}/fs/xfs/xfs_sb.h
25589 +# tristate
25590 +ifdef CONFIG_XFS_FS
25591 +ccflags-y += -DXFS_SB_MAGIC=0x58465342
25592 +endif
25593 +
25594 +# defined in ${srctree}/fs/configfs/mount.c
25595 +# tristate
25596 +ifdef CONFIG_CONFIGFS_FS
25597 +ccflags-y += -DCONFIGFS_MAGIC=0x62656570
25598 +endif
25599 +
25600 +# defined in ${srctree}/fs/ubifs/ubifs.h
25601 +# tristate
25602 +ifdef CONFIG_UBIFS_FS
25603 +ccflags-y += -DUBIFS_SUPER_MAGIC=0x24051905
25604 +endif
25605 +
25606 +# defined in ${srctree}/fs/hfsplus/hfsplus_raw.h
25607 +# tristate
25608 +ifdef CONFIG_HFSPLUS_FS
25609 +ccflags-y += -DHFSPLUS_SUPER_MAGIC=0x482b
25610 +endif
25611 diff -urN /usr/share/empty/fs/aufs/Makefile linux/fs/aufs/Makefile
25612 --- /usr/share/empty/fs/aufs/Makefile   1970-01-01 01:00:00.000000000 +0100
25613 +++ linux/fs/aufs/Makefile      2022-03-21 14:49:05.716633010 +0100
25614 @@ -0,0 +1,46 @@
25615 +# SPDX-License-Identifier: GPL-2.0
25616 +
25617 +include ${src}/magic.mk
25618 +ifeq (${CONFIG_AUFS_FS},m)
25619 +include ${src}/conf.mk
25620 +endif
25621 +-include ${src}/priv_def.mk
25622 +
25623 +# cf. include/linux/kernel.h
25624 +# enable pr_debug
25625 +ccflags-y += -DDEBUG
25626 +# sparse requires the full pathname
25627 +ifdef M
25628 +ccflags-y += -include ${M}/../../include/uapi/linux/aufs_type.h
25629 +else
25630 +ccflags-y += -include ${srctree}/include/uapi/linux/aufs_type.h
25631 +endif
25632 +
25633 +obj-$(CONFIG_AUFS_FS) += aufs.o
25634 +aufs-y := module.o sbinfo.o super.o branch.o xino.o sysaufs.o opts.o fsctx.o \
25635 +       wkq.o vfsub.o dcsub.o \
25636 +       cpup.o whout.o wbr_policy.o \
25637 +       dinfo.o dentry.o \
25638 +       dynop.o \
25639 +       finfo.o file.o f_op.o \
25640 +       dir.o vdir.o \
25641 +       iinfo.o inode.o i_op.o i_op_add.o i_op_del.o i_op_ren.o \
25642 +       mvdown.o ioctl.o
25643 +
25644 +# all are boolean
25645 +aufs-$(CONFIG_PROC_FS) += procfs.o plink.o
25646 +aufs-$(CONFIG_SYSFS) += sysfs.o
25647 +aufs-$(CONFIG_DEBUG_FS) += dbgaufs.o
25648 +aufs-$(CONFIG_AUFS_BDEV_LOOP) += loop.o
25649 +aufs-$(CONFIG_AUFS_HNOTIFY) += hnotify.o
25650 +aufs-$(CONFIG_AUFS_HFSNOTIFY) += hfsnotify.o
25651 +aufs-$(CONFIG_AUFS_EXPORT) += export.o
25652 +aufs-$(CONFIG_AUFS_XATTR) += xattr.o
25653 +aufs-$(CONFIG_FS_POSIX_ACL) += posix_acl.o
25654 +aufs-$(CONFIG_AUFS_DIRREN) += dirren.o
25655 +aufs-$(CONFIG_AUFS_FHSM) += fhsm.o
25656 +aufs-$(CONFIG_AUFS_POLL) += poll.o
25657 +aufs-$(CONFIG_AUFS_RDU) += rdu.o
25658 +aufs-$(CONFIG_AUFS_BR_HFSPLUS) += hfsplus.o
25659 +aufs-$(CONFIG_AUFS_DEBUG) += debug.o
25660 +aufs-$(CONFIG_AUFS_MAGIC_SYSRQ) += sysrq.o
25661 diff -urN /usr/share/empty/fs/aufs/module.c linux/fs/aufs/module.c
25662 --- /usr/share/empty/fs/aufs/module.c   1970-01-01 01:00:00.000000000 +0100
25663 +++ linux/fs/aufs/module.c      2022-03-21 14:49:05.726633010 +0100
25664 @@ -0,0 +1,273 @@
25665 +// SPDX-License-Identifier: GPL-2.0
25666 +/*
25667 + * Copyright (C) 2005-2021 Junjiro R. Okajima
25668 + *
25669 + * This program, aufs is free software; you can redistribute it and/or modify
25670 + * it under the terms of the GNU General Public License as published by
25671 + * the Free Software Foundation; either version 2 of the License, or
25672 + * (at your option) any later version.
25673 + *
25674 + * This program is distributed in the hope that it will be useful,
25675 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
25676 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25677 + * GNU General Public License for more details.
25678 + *
25679 + * You should have received a copy of the GNU General Public License
25680 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25681 + */
25682 +
25683 +/*
25684 + * module global variables and operations
25685 + */
25686 +
25687 +#include <linux/module.h>
25688 +#include <linux/seq_file.h>
25689 +#include "aufs.h"
25690 +
25691 +/* shrinkable realloc */
25692 +void *au_krealloc(void *p, unsigned int new_sz, gfp_t gfp, int may_shrink)
25693 +{
25694 +       size_t sz;
25695 +       int diff;
25696 +
25697 +       sz = 0;
25698 +       diff = -1;
25699 +       if (p) {
25700 +#if 0 /* unused */
25701 +               if (!new_sz) {
25702 +                       au_kfree_rcu(p);
25703 +                       p = NULL;
25704 +                       goto out;
25705 +               }
25706 +#else
25707 +               AuDebugOn(!new_sz);
25708 +#endif
25709 +               sz = ksize(p);
25710 +               diff = au_kmidx_sub(sz, new_sz);
25711 +       }
25712 +       if (sz && !diff)
25713 +               goto out;
25714 +
25715 +       if (sz < new_sz)
25716 +               /* expand or SLOB */
25717 +               p = krealloc(p, new_sz, gfp);
25718 +       else if (new_sz < sz && may_shrink) {
25719 +               /* shrink */
25720 +               void *q;
25721 +
25722 +               q = kmalloc(new_sz, gfp);
25723 +               if (q) {
25724 +                       if (p) {
25725 +                               memcpy(q, p, new_sz);
25726 +                               au_kfree_try_rcu(p);
25727 +                       }
25728 +                       p = q;
25729 +               } else
25730 +                       p = NULL;
25731 +       }
25732 +
25733 +out:
25734 +       return p;
25735 +}
25736 +
25737 +void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp,
25738 +                  int may_shrink)
25739 +{
25740 +       p = au_krealloc(p, new_sz, gfp, may_shrink);
25741 +       if (p && new_sz > nused)
25742 +               memset(p + nused, 0, new_sz - nused);
25743 +       return p;
25744 +}
25745 +
25746 +/* ---------------------------------------------------------------------- */
25747 +/*
25748 + * aufs caches
25749 + */
25750 +struct kmem_cache *au_cache[AuCache_Last];
25751 +
25752 +static void au_cache_fin(void)
25753 +{
25754 +       int i;
25755 +
25756 +       /*
25757 +        * Make sure all delayed rcu free inodes are flushed before we
25758 +        * destroy cache.
25759 +        */
25760 +       rcu_barrier();
25761 +
25762 +       /* excluding AuCache_HNOTIFY */
25763 +       BUILD_BUG_ON(AuCache_HNOTIFY + 1 != AuCache_Last);
25764 +       for (i = 0; i < AuCache_HNOTIFY; i++) {
25765 +               kmem_cache_destroy(au_cache[i]);
25766 +               au_cache[i] = NULL;
25767 +       }
25768 +}
25769 +
25770 +static int __init au_cache_init(void)
25771 +{
25772 +       au_cache[AuCache_DINFO] = AuCacheCtor(au_dinfo, au_di_init_once);
25773 +       if (au_cache[AuCache_DINFO])
25774 +               /* SLAB_DESTROY_BY_RCU */
25775 +               au_cache[AuCache_ICNTNR] = AuCacheCtor(au_icntnr,
25776 +                                                      au_icntnr_init_once);
25777 +       if (au_cache[AuCache_ICNTNR])
25778 +               au_cache[AuCache_FINFO] = AuCacheCtor(au_finfo,
25779 +                                                     au_fi_init_once);
25780 +       if (au_cache[AuCache_FINFO])
25781 +               au_cache[AuCache_VDIR] = AuCache(au_vdir);
25782 +       if (au_cache[AuCache_VDIR])
25783 +               au_cache[AuCache_DEHSTR] = AuCache(au_vdir_dehstr);
25784 +       if (au_cache[AuCache_DEHSTR])
25785 +               return 0;
25786 +
25787 +       au_cache_fin();
25788 +       return -ENOMEM;
25789 +}
25790 +
25791 +/* ---------------------------------------------------------------------- */
25792 +
25793 +int au_dir_roflags;
25794 +
25795 +#ifdef CONFIG_AUFS_SBILIST
25796 +/*
25797 + * iterate_supers_type() doesn't protect us from
25798 + * remounting (branch management)
25799 + */
25800 +struct hlist_bl_head au_sbilist;
25801 +#endif
25802 +
25803 +/*
25804 + * functions for module interface.
25805 + */
25806 +MODULE_LICENSE("GPL");
25807 +/* MODULE_LICENSE("GPL v2"); */
25808 +MODULE_AUTHOR("Junjiro R. Okajima <aufs-users@lists.sourceforge.net>");
25809 +MODULE_DESCRIPTION(AUFS_NAME
25810 +       " -- Advanced multi layered unification filesystem");
25811 +MODULE_VERSION(AUFS_VERSION);
25812 +MODULE_ALIAS_FS(AUFS_NAME);
25813 +
25814 +/* this module parameter has no meaning when SYSFS is disabled */
25815 +int sysaufs_brs = 1;
25816 +MODULE_PARM_DESC(brs, "use <sysfs>/fs/aufs/si_*/brN");
25817 +module_param_named(brs, sysaufs_brs, int, 0444);
25818 +
25819 +/* this module parameter has no meaning when USER_NS is disabled */
25820 +bool au_userns;
25821 +MODULE_PARM_DESC(allow_userns, "allow unprivileged to mount under userns");
25822 +module_param_named(allow_userns, au_userns, bool, 0444);
25823 +
25824 +/* ---------------------------------------------------------------------- */
25825 +
25826 +static char au_esc_chars[0x20 + 3]; /* 0x01-0x20, backslash, del, and NULL */
25827 +
25828 +int au_seq_path(struct seq_file *seq, struct path *path)
25829 +{
25830 +       int err;
25831 +
25832 +       err = seq_path(seq, path, au_esc_chars);
25833 +       if (err >= 0)
25834 +               err = 0;
25835 +       else
25836 +               err = -ENOMEM;
25837 +
25838 +       return err;
25839 +}
25840 +
25841 +/* ---------------------------------------------------------------------- */
25842 +
25843 +static int __init aufs_init(void)
25844 +{
25845 +       int err, i;
25846 +       char *p;
25847 +
25848 +       p = au_esc_chars;
25849 +       for (i = 1; i <= ' '; i++)
25850 +               *p++ = i;
25851 +       *p++ = '\\';
25852 +       *p++ = '\x7f';
25853 +       *p = 0;
25854 +
25855 +       au_dir_roflags = au_file_roflags(O_DIRECTORY | O_LARGEFILE);
25856 +
25857 +       memcpy(aufs_iop_nogetattr, aufs_iop, sizeof(aufs_iop));
25858 +       for (i = 0; i < AuIop_Last; i++)
25859 +               aufs_iop_nogetattr[i].getattr = NULL;
25860 +
25861 +       memset(au_cache, 0, sizeof(au_cache));  /* including hnotify */
25862 +
25863 +       au_sbilist_init();
25864 +       sysaufs_brs_init();
25865 +       au_debug_init();
25866 +       au_dy_init();
25867 +       err = sysaufs_init();
25868 +       if (unlikely(err))
25869 +               goto out;
25870 +       err = dbgaufs_init();
25871 +       if (unlikely(err))
25872 +               goto out_sysaufs;
25873 +       err = au_procfs_init();
25874 +       if (unlikely(err))
25875 +               goto out_dbgaufs;
25876 +       err = au_wkq_init();
25877 +       if (unlikely(err))
25878 +               goto out_procfs;
25879 +       err = au_loopback_init();
25880 +       if (unlikely(err))
25881 +               goto out_wkq;
25882 +       err = au_hnotify_init();
25883 +       if (unlikely(err))
25884 +               goto out_loopback;
25885 +       err = au_sysrq_init();
25886 +       if (unlikely(err))
25887 +               goto out_hin;
25888 +       err = au_cache_init();
25889 +       if (unlikely(err))
25890 +               goto out_sysrq;
25891 +
25892 +       aufs_fs_type.fs_flags |= au_userns ? FS_USERNS_MOUNT : 0;
25893 +       err = register_filesystem(&aufs_fs_type);
25894 +       if (unlikely(err))
25895 +               goto out_cache;
25896 +
25897 +       /* since we define pr_fmt, call printk directly */
25898 +       printk(KERN_INFO AUFS_NAME " " AUFS_VERSION "\n");
25899 +       goto out; /* success */
25900 +
25901 +out_cache:
25902 +       au_cache_fin();
25903 +out_sysrq:
25904 +       au_sysrq_fin();
25905 +out_hin:
25906 +       au_hnotify_fin();
25907 +out_loopback:
25908 +       au_loopback_fin();
25909 +out_wkq:
25910 +       au_wkq_fin();
25911 +out_procfs:
25912 +       au_procfs_fin();
25913 +out_dbgaufs:
25914 +       dbgaufs_fin();
25915 +out_sysaufs:
25916 +       sysaufs_fin();
25917 +       au_dy_fin();
25918 +out:
25919 +       return err;
25920 +}
25921 +
25922 +static void __exit aufs_exit(void)
25923 +{
25924 +       unregister_filesystem(&aufs_fs_type);
25925 +       au_cache_fin();
25926 +       au_sysrq_fin();
25927 +       au_hnotify_fin();
25928 +       au_loopback_fin();
25929 +       au_wkq_fin();
25930 +       au_procfs_fin();
25931 +       dbgaufs_fin();
25932 +       sysaufs_fin();
25933 +       au_dy_fin();
25934 +}
25935 +
25936 +module_init(aufs_init);
25937 +module_exit(aufs_exit);
25938 diff -urN /usr/share/empty/fs/aufs/module.h linux/fs/aufs/module.h
25939 --- /usr/share/empty/fs/aufs/module.h   1970-01-01 01:00:00.000000000 +0100
25940 +++ linux/fs/aufs/module.h      2022-03-21 14:49:05.726633010 +0100
25941 @@ -0,0 +1,166 @@
25942 +/* SPDX-License-Identifier: GPL-2.0 */
25943 +/*
25944 + * Copyright (C) 2005-2021 Junjiro R. Okajima
25945 + *
25946 + * This program, aufs is free software; you can redistribute it and/or modify
25947 + * it under the terms of the GNU General Public License as published by
25948 + * the Free Software Foundation; either version 2 of the License, or
25949 + * (at your option) any later version.
25950 + *
25951 + * This program is distributed in the hope that it will be useful,
25952 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
25953 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25954 + * GNU General Public License for more details.
25955 + *
25956 + * You should have received a copy of the GNU General Public License
25957 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25958 + */
25959 +
25960 +/*
25961 + * module initialization and module-global
25962 + */
25963 +
25964 +#ifndef __AUFS_MODULE_H__
25965 +#define __AUFS_MODULE_H__
25966 +
25967 +#ifdef __KERNEL__
25968 +
25969 +#include <linux/slab.h>
25970 +#include "debug.h"
25971 +#include "dentry.h"
25972 +#include "dir.h"
25973 +#include "file.h"
25974 +#include "inode.h"
25975 +
25976 +struct path;
25977 +struct seq_file;
25978 +
25979 +/* module parameters */
25980 +extern int sysaufs_brs;
25981 +extern bool au_userns;
25982 +
25983 +/* ---------------------------------------------------------------------- */
25984 +
25985 +extern int au_dir_roflags;
25986 +
25987 +void *au_krealloc(void *p, unsigned int new_sz, gfp_t gfp, int may_shrink);
25988 +void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp,
25989 +                  int may_shrink);
25990 +
25991 +/*
25992 + * Comparing the size of the object with sizeof(struct rcu_head)
25993 + * case 1: object is always larger
25994 + *     --> au_kfree_rcu() or au_kfree_do_rcu()
25995 + * case 2: object is always smaller
25996 + *     --> au_kfree_small()
25997 + * case 3: object can be any size
25998 + *     --> au_kfree_try_rcu()
25999 + */
26000 +
26001 +static inline void au_kfree_do_rcu(const void *p)
26002 +{
26003 +       struct {
26004 +               struct rcu_head rcu;
26005 +       } *a = (void *)p;
26006 +
26007 +       kfree_rcu(a, rcu);
26008 +}
26009 +
26010 +#define au_kfree_rcu(_p) do {                                          \
26011 +               typeof(_p) p = (_p);                                    \
26012 +               BUILD_BUG_ON(sizeof(*p) < sizeof(struct rcu_head));     \
26013 +               if (p)                                                  \
26014 +                       au_kfree_do_rcu(p);                             \
26015 +       } while (0)
26016 +
26017 +#define au_kfree_do_sz_test(sz)        (sz >= sizeof(struct rcu_head))
26018 +#define au_kfree_sz_test(p)    (p && au_kfree_do_sz_test(ksize(p)))
26019 +
26020 +static inline void au_kfree_try_rcu(const void *p)
26021 +{
26022 +       if (!p)
26023 +               return;
26024 +       if (au_kfree_sz_test(p))
26025 +               au_kfree_do_rcu(p);
26026 +       else
26027 +               kfree(p);
26028 +}
26029 +
26030 +static inline void au_kfree_small(const void *p)
26031 +{
26032 +       if (!p)
26033 +               return;
26034 +       AuDebugOn(au_kfree_sz_test(p));
26035 +       kfree(p);
26036 +}
26037 +
26038 +static inline int au_kmidx_sub(size_t sz, size_t new_sz)
26039 +{
26040 +#ifndef CONFIG_SLOB
26041 +       return __kmalloc_index(sz, false) - __kmalloc_index(new_sz, false);
26042 +#else
26043 +       return -1; /* SLOB is untested */
26044 +#endif
26045 +}
26046 +
26047 +int au_seq_path(struct seq_file *seq, struct path *path);
26048 +
26049 +#ifdef CONFIG_PROC_FS
26050 +/* procfs.c */
26051 +int __init au_procfs_init(void);
26052 +void au_procfs_fin(void);
26053 +#else
26054 +AuStubInt0(au_procfs_init, void);
26055 +AuStubVoid(au_procfs_fin, void);
26056 +#endif
26057 +
26058 +/* ---------------------------------------------------------------------- */
26059 +
26060 +/* kmem cache */
26061 +enum {
26062 +       AuCache_DINFO,
26063 +       AuCache_ICNTNR,
26064 +       AuCache_FINFO,
26065 +       AuCache_VDIR,
26066 +       AuCache_DEHSTR,
26067 +       AuCache_HNOTIFY, /* must be last */
26068 +       AuCache_Last
26069 +};
26070 +
26071 +extern struct kmem_cache *au_cache[AuCache_Last];
26072 +
26073 +#define AuCacheFlags           (SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD)
26074 +#define AuCache(type)          KMEM_CACHE(type, AuCacheFlags)
26075 +#define AuCacheCtor(type, ctor)        \
26076 +       kmem_cache_create(#type, sizeof(struct type), \
26077 +                         __alignof__(struct type), AuCacheFlags, ctor)
26078 +
26079 +#define AuCacheFuncs(name, index)                                      \
26080 +       static inline struct au_##name *au_cache_alloc_##name(void)     \
26081 +       { return kmem_cache_alloc(au_cache[AuCache_##index], GFP_NOFS); } \
26082 +       static inline void au_cache_free_##name##_norcu(struct au_##name *p) \
26083 +       { kmem_cache_free(au_cache[AuCache_##index], p); }              \
26084 +                                                                       \
26085 +       static inline void au_cache_free_##name##_rcu_cb(struct rcu_head *rcu) \
26086 +       { void *p = rcu;                                                \
26087 +               p -= offsetof(struct au_##name, rcu);                   \
26088 +               kmem_cache_free(au_cache[AuCache_##index], p); }        \
26089 +       static inline void au_cache_free_##name##_rcu(struct au_##name *p) \
26090 +       { BUILD_BUG_ON(sizeof(struct au_##name) < sizeof(struct rcu_head)); \
26091 +               call_rcu(&p->rcu, au_cache_free_##name##_rcu_cb); }     \
26092 +                                                                       \
26093 +       static inline void au_cache_free_##name(struct au_##name *p)    \
26094 +       { /* au_cache_free_##name##_norcu(p); */                        \
26095 +               au_cache_free_##name##_rcu(p); }
26096 +
26097 +AuCacheFuncs(dinfo, DINFO);
26098 +AuCacheFuncs(icntnr, ICNTNR);
26099 +AuCacheFuncs(finfo, FINFO);
26100 +AuCacheFuncs(vdir, VDIR);
26101 +AuCacheFuncs(vdir_dehstr, DEHSTR);
26102 +#ifdef CONFIG_AUFS_HNOTIFY
26103 +AuCacheFuncs(hnotify, HNOTIFY);
26104 +#endif
26105 +
26106 +#endif /* __KERNEL__ */
26107 +#endif /* __AUFS_MODULE_H__ */
26108 diff -urN /usr/share/empty/fs/aufs/mvdown.c linux/fs/aufs/mvdown.c
26109 --- /usr/share/empty/fs/aufs/mvdown.c   1970-01-01 01:00:00.000000000 +0100
26110 +++ linux/fs/aufs/mvdown.c      2022-03-21 14:49:05.729966343 +0100
26111 @@ -0,0 +1,706 @@
26112 +// SPDX-License-Identifier: GPL-2.0
26113 +/*
26114 + * Copyright (C) 2011-2021 Junjiro R. Okajima
26115 + *
26116 + * This program, aufs is free software; you can redistribute it and/or modify
26117 + * it under the terms of the GNU General Public License as published by
26118 + * the Free Software Foundation; either version 2 of the License, or
26119 + * (at your option) any later version.
26120 + *
26121 + * This program is distributed in the hope that it will be useful,
26122 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
26123 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26124 + * GNU General Public License for more details.
26125 + *
26126 + * You should have received a copy of the GNU General Public License
26127 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
26128 + */
26129 +
26130 +/*
26131 + * move-down, opposite of copy-up
26132 + */
26133 +
26134 +#include "aufs.h"
26135 +
26136 +struct au_mvd_args {
26137 +       struct {
26138 +               struct super_block *h_sb;
26139 +               struct dentry *h_parent;
26140 +               struct au_hinode *hdir;
26141 +               struct inode *h_dir, *h_inode;
26142 +               struct au_pin pin;
26143 +       } info[AUFS_MVDOWN_NARRAY];
26144 +
26145 +       struct aufs_mvdown mvdown;
26146 +       struct dentry *dentry, *parent;
26147 +       struct inode *inode, *dir;
26148 +       struct super_block *sb;
26149 +       aufs_bindex_t bopq, bwh, bfound;
26150 +       unsigned char rename_lock;
26151 +};
26152 +
26153 +#define mvd_errno              mvdown.au_errno
26154 +#define mvd_bsrc               mvdown.stbr[AUFS_MVDOWN_UPPER].bindex
26155 +#define mvd_src_brid           mvdown.stbr[AUFS_MVDOWN_UPPER].brid
26156 +#define mvd_bdst               mvdown.stbr[AUFS_MVDOWN_LOWER].bindex
26157 +#define mvd_dst_brid           mvdown.stbr[AUFS_MVDOWN_LOWER].brid
26158 +
26159 +#define mvd_h_src_sb           info[AUFS_MVDOWN_UPPER].h_sb
26160 +#define mvd_h_src_parent       info[AUFS_MVDOWN_UPPER].h_parent
26161 +#define mvd_hdir_src           info[AUFS_MVDOWN_UPPER].hdir
26162 +#define mvd_h_src_dir          info[AUFS_MVDOWN_UPPER].h_dir
26163 +#define mvd_h_src_inode                info[AUFS_MVDOWN_UPPER].h_inode
26164 +#define mvd_pin_src            info[AUFS_MVDOWN_UPPER].pin
26165 +
26166 +#define mvd_h_dst_sb           info[AUFS_MVDOWN_LOWER].h_sb
26167 +#define mvd_h_dst_parent       info[AUFS_MVDOWN_LOWER].h_parent
26168 +#define mvd_hdir_dst           info[AUFS_MVDOWN_LOWER].hdir
26169 +#define mvd_h_dst_dir          info[AUFS_MVDOWN_LOWER].h_dir
26170 +#define mvd_h_dst_inode                info[AUFS_MVDOWN_LOWER].h_inode
26171 +#define mvd_pin_dst            info[AUFS_MVDOWN_LOWER].pin
26172 +
26173 +#define AU_MVD_PR(flag, ...) do {                      \
26174 +               if (flag)                               \
26175 +                       pr_err(__VA_ARGS__);            \
26176 +       } while (0)
26177 +
26178 +static int find_lower_writable(struct au_mvd_args *a)
26179 +{
26180 +       struct super_block *sb;
26181 +       aufs_bindex_t bindex, bbot;
26182 +       struct au_branch *br;
26183 +
26184 +       sb = a->sb;
26185 +       bindex = a->mvd_bsrc;
26186 +       bbot = au_sbbot(sb);
26187 +       if (a->mvdown.flags & AUFS_MVDOWN_FHSM_LOWER)
26188 +               for (bindex++; bindex <= bbot; bindex++) {
26189 +                       br = au_sbr(sb, bindex);
26190 +                       if (au_br_fhsm(br->br_perm)
26191 +                           && !sb_rdonly(au_br_sb(br)))
26192 +                               return bindex;
26193 +               }
26194 +       else if (!(a->mvdown.flags & AUFS_MVDOWN_ROLOWER))
26195 +               for (bindex++; bindex <= bbot; bindex++) {
26196 +                       br = au_sbr(sb, bindex);
26197 +                       if (!au_br_rdonly(br))
26198 +                               return bindex;
26199 +               }
26200 +       else
26201 +               for (bindex++; bindex <= bbot; bindex++) {
26202 +                       br = au_sbr(sb, bindex);
26203 +                       if (!sb_rdonly(au_br_sb(br))) {
26204 +                               if (au_br_rdonly(br))
26205 +                                       a->mvdown.flags
26206 +                                               |= AUFS_MVDOWN_ROLOWER_R;
26207 +                               return bindex;
26208 +                       }
26209 +               }
26210 +
26211 +       return -1;
26212 +}
26213 +
26214 +/* make the parent dir on bdst */
26215 +static int au_do_mkdir(const unsigned char dmsg, struct au_mvd_args *a)
26216 +{
26217 +       int err;
26218 +
26219 +       err = 0;
26220 +       a->mvd_hdir_src = au_hi(a->dir, a->mvd_bsrc);
26221 +       a->mvd_hdir_dst = au_hi(a->dir, a->mvd_bdst);
26222 +       a->mvd_h_src_parent = au_h_dptr(a->parent, a->mvd_bsrc);
26223 +       a->mvd_h_dst_parent = NULL;
26224 +       if (au_dbbot(a->parent) >= a->mvd_bdst)
26225 +               a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
26226 +       if (!a->mvd_h_dst_parent) {
26227 +               err = au_cpdown_dirs(a->dentry, a->mvd_bdst);
26228 +               if (unlikely(err)) {
26229 +                       AU_MVD_PR(dmsg, "cpdown_dirs failed\n");
26230 +                       goto out;
26231 +               }
26232 +               a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
26233 +       }
26234 +
26235 +out:
26236 +       AuTraceErr(err);
26237 +       return err;
26238 +}
26239 +
26240 +/* lock them all */
26241 +static int au_do_lock(const unsigned char dmsg, struct au_mvd_args *a)
26242 +{
26243 +       int err;
26244 +       struct dentry *h_trap;
26245 +
26246 +       a->mvd_h_src_sb = au_sbr_sb(a->sb, a->mvd_bsrc);
26247 +       a->mvd_h_dst_sb = au_sbr_sb(a->sb, a->mvd_bdst);
26248 +       err = au_pin(&a->mvd_pin_dst, a->dentry, a->mvd_bdst,
26249 +                    au_opt_udba(a->sb),
26250 +                    AuPin_MNT_WRITE | AuPin_DI_LOCKED);
26251 +       AuTraceErr(err);
26252 +       if (unlikely(err)) {
26253 +               AU_MVD_PR(dmsg, "pin_dst failed\n");
26254 +               goto out;
26255 +       }
26256 +
26257 +       if (a->mvd_h_src_sb != a->mvd_h_dst_sb) {
26258 +               a->rename_lock = 0;
26259 +               au_pin_init(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
26260 +                           AuLsc_DI_PARENT, AuLsc_I_PARENT3,
26261 +                           au_opt_udba(a->sb),
26262 +                           AuPin_MNT_WRITE | AuPin_DI_LOCKED);
26263 +               err = au_do_pin(&a->mvd_pin_src);
26264 +               AuTraceErr(err);
26265 +               a->mvd_h_src_dir = d_inode(a->mvd_h_src_parent);
26266 +               if (unlikely(err)) {
26267 +                       AU_MVD_PR(dmsg, "pin_src failed\n");
26268 +                       goto out_dst;
26269 +               }
26270 +               goto out; /* success */
26271 +       }
26272 +
26273 +       a->rename_lock = 1;
26274 +       au_pin_hdir_unlock(&a->mvd_pin_dst);
26275 +       err = au_pin(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
26276 +                    au_opt_udba(a->sb),
26277 +                    AuPin_MNT_WRITE | AuPin_DI_LOCKED);
26278 +       AuTraceErr(err);
26279 +       a->mvd_h_src_dir = d_inode(a->mvd_h_src_parent);
26280 +       if (unlikely(err)) {
26281 +               AU_MVD_PR(dmsg, "pin_src failed\n");
26282 +               au_pin_hdir_lock(&a->mvd_pin_dst);
26283 +               goto out_dst;
26284 +       }
26285 +       au_pin_hdir_unlock(&a->mvd_pin_src);
26286 +       h_trap = vfsub_lock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
26287 +                                  a->mvd_h_dst_parent, a->mvd_hdir_dst);
26288 +       if (h_trap) {
26289 +               err = (h_trap != a->mvd_h_src_parent);
26290 +               if (err)
26291 +                       err = (h_trap != a->mvd_h_dst_parent);
26292 +       }
26293 +       BUG_ON(err); /* it should never happen */
26294 +       if (unlikely(a->mvd_h_src_dir != au_pinned_h_dir(&a->mvd_pin_src))) {
26295 +               err = -EBUSY;
26296 +               AuTraceErr(err);
26297 +               vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
26298 +                                   a->mvd_h_dst_parent, a->mvd_hdir_dst);
26299 +               au_pin_hdir_lock(&a->mvd_pin_src);
26300 +               au_unpin(&a->mvd_pin_src);
26301 +               au_pin_hdir_lock(&a->mvd_pin_dst);
26302 +               goto out_dst;
26303 +       }
26304 +       goto out; /* success */
26305 +
26306 +out_dst:
26307 +       au_unpin(&a->mvd_pin_dst);
26308 +out:
26309 +       AuTraceErr(err);
26310 +       return err;
26311 +}
26312 +
26313 +static void au_do_unlock(const unsigned char dmsg, struct au_mvd_args *a)
26314 +{
26315 +       if (!a->rename_lock)
26316 +               au_unpin(&a->mvd_pin_src);
26317 +       else {
26318 +               vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
26319 +                                   a->mvd_h_dst_parent, a->mvd_hdir_dst);
26320 +               au_pin_hdir_lock(&a->mvd_pin_src);
26321 +               au_unpin(&a->mvd_pin_src);
26322 +               au_pin_hdir_lock(&a->mvd_pin_dst);
26323 +       }
26324 +       au_unpin(&a->mvd_pin_dst);
26325 +}
26326 +
26327 +/* copy-down the file */
26328 +static int au_do_cpdown(const unsigned char dmsg, struct au_mvd_args *a)
26329 +{
26330 +       int err;
26331 +       struct au_cp_generic cpg = {
26332 +               .dentry = a->dentry,
26333 +               .bdst   = a->mvd_bdst,
26334 +               .bsrc   = a->mvd_bsrc,
26335 +               .len    = -1,
26336 +               .pin    = &a->mvd_pin_dst,
26337 +               .flags  = AuCpup_DTIME | AuCpup_HOPEN
26338 +       };
26339 +
26340 +       AuDbg("b%d, b%d\n", cpg.bsrc, cpg.bdst);
26341 +       if (a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
26342 +               au_fset_cpup(cpg.flags, OVERWRITE);
26343 +       if (a->mvdown.flags & AUFS_MVDOWN_ROLOWER)
26344 +               au_fset_cpup(cpg.flags, RWDST);
26345 +       err = au_sio_cpdown_simple(&cpg);
26346 +       if (unlikely(err))
26347 +               AU_MVD_PR(dmsg, "cpdown failed\n");
26348 +
26349 +       AuTraceErr(err);
26350 +       return err;
26351 +}
26352 +
26353 +/*
26354 + * unlink the whiteout on bdst if exist which may be created by UDBA while we
26355 + * were sleeping
26356 + */
26357 +static int au_do_unlink_wh(const unsigned char dmsg, struct au_mvd_args *a)
26358 +{
26359 +       int err;
26360 +       struct path h_path;
26361 +       struct au_branch *br;
26362 +       struct inode *delegated;
26363 +
26364 +       br = au_sbr(a->sb, a->mvd_bdst);
26365 +       h_path.dentry = au_wh_lkup(a->mvd_h_dst_parent, &a->dentry->d_name, br);
26366 +       err = PTR_ERR(h_path.dentry);
26367 +       if (IS_ERR(h_path.dentry)) {
26368 +               AU_MVD_PR(dmsg, "wh_lkup failed\n");
26369 +               goto out;
26370 +       }
26371 +
26372 +       err = 0;
26373 +       if (d_is_positive(h_path.dentry)) {
26374 +               h_path.mnt = au_br_mnt(br);
26375 +               delegated = NULL;
26376 +               err = vfsub_unlink(d_inode(a->mvd_h_dst_parent), &h_path,
26377 +                                  &delegated, /*force*/0);
26378 +               if (unlikely(err == -EWOULDBLOCK)) {
26379 +                       pr_warn("cannot retry for NFSv4 delegation"
26380 +                               " for an internal unlink\n");
26381 +                       iput(delegated);
26382 +               }
26383 +               if (unlikely(err))
26384 +                       AU_MVD_PR(dmsg, "wh_unlink failed\n");
26385 +       }
26386 +       dput(h_path.dentry);
26387 +
26388 +out:
26389 +       AuTraceErr(err);
26390 +       return err;
26391 +}
26392 +
26393 +/*
26394 + * unlink the topmost h_dentry
26395 + */
26396 +static int au_do_unlink(const unsigned char dmsg, struct au_mvd_args *a)
26397 +{
26398 +       int err;
26399 +       struct path h_path;
26400 +       struct inode *delegated;
26401 +
26402 +       h_path.mnt = au_sbr_mnt(a->sb, a->mvd_bsrc);
26403 +       h_path.dentry = au_h_dptr(a->dentry, a->mvd_bsrc);
26404 +       delegated = NULL;
26405 +       err = vfsub_unlink(a->mvd_h_src_dir, &h_path, &delegated, /*force*/0);
26406 +       if (unlikely(err == -EWOULDBLOCK)) {
26407 +               pr_warn("cannot retry for NFSv4 delegation"
26408 +                       " for an internal unlink\n");
26409 +               iput(delegated);
26410 +       }
26411 +       if (unlikely(err))
26412 +               AU_MVD_PR(dmsg, "unlink failed\n");
26413 +
26414 +       AuTraceErr(err);
26415 +       return err;
26416 +}
26417 +
26418 +/* Since mvdown succeeded, we ignore an error of this function */
26419 +static void au_do_stfs(const unsigned char dmsg, struct au_mvd_args *a)
26420 +{
26421 +       int err;
26422 +       struct au_branch *br;
26423 +
26424 +       a->mvdown.flags |= AUFS_MVDOWN_STFS_FAILED;
26425 +       br = au_sbr(a->sb, a->mvd_bsrc);
26426 +       err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_UPPER].stfs);
26427 +       if (!err) {
26428 +               br = au_sbr(a->sb, a->mvd_bdst);
26429 +               a->mvdown.stbr[AUFS_MVDOWN_LOWER].brid = br->br_id;
26430 +               err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_LOWER].stfs);
26431 +       }
26432 +       if (!err)
26433 +               a->mvdown.flags &= ~AUFS_MVDOWN_STFS_FAILED;
26434 +       else
26435 +               AU_MVD_PR(dmsg, "statfs failed (%d), ignored\n", err);
26436 +}
26437 +
26438 +/*
26439 + * copy-down the file and unlink the bsrc file.
26440 + * - unlink the bdst whout if exist
26441 + * - copy-down the file (with whtmp name and rename)
26442 + * - unlink the bsrc file
26443 + */
26444 +static int au_do_mvdown(const unsigned char dmsg, struct au_mvd_args *a)
26445 +{
26446 +       int err;
26447 +
26448 +       err = au_do_mkdir(dmsg, a);
26449 +       if (!err)
26450 +               err = au_do_lock(dmsg, a);
26451 +       if (unlikely(err))
26452 +               goto out;
26453 +
26454 +       /*
26455 +        * do not revert the activities we made on bdst since they should be
26456 +        * harmless in aufs.
26457 +        */
26458 +
26459 +       err = au_do_cpdown(dmsg, a);
26460 +       if (!err)
26461 +               err = au_do_unlink_wh(dmsg, a);
26462 +       if (!err && !(a->mvdown.flags & AUFS_MVDOWN_KUPPER))
26463 +               err = au_do_unlink(dmsg, a);
26464 +       if (unlikely(err))
26465 +               goto out_unlock;
26466 +
26467 +       AuDbg("%pd2, 0x%x, %d --> %d\n",
26468 +             a->dentry, a->mvdown.flags, a->mvd_bsrc, a->mvd_bdst);
26469 +       if (find_lower_writable(a) < 0)
26470 +               a->mvdown.flags |= AUFS_MVDOWN_BOTTOM;
26471 +
26472 +       if (a->mvdown.flags & AUFS_MVDOWN_STFS)
26473 +               au_do_stfs(dmsg, a);
26474 +
26475 +       /* maintain internal array */
26476 +       if (!(a->mvdown.flags & AUFS_MVDOWN_KUPPER)) {
26477 +               au_set_h_dptr(a->dentry, a->mvd_bsrc, NULL);
26478 +               au_set_dbtop(a->dentry, a->mvd_bdst);
26479 +               au_set_h_iptr(a->inode, a->mvd_bsrc, NULL, /*flags*/0);
26480 +               au_set_ibtop(a->inode, a->mvd_bdst);
26481 +       } else {
26482 +               /* hide the lower */
26483 +               au_set_h_dptr(a->dentry, a->mvd_bdst, NULL);
26484 +               au_set_dbbot(a->dentry, a->mvd_bsrc);
26485 +               au_set_h_iptr(a->inode, a->mvd_bdst, NULL, /*flags*/0);
26486 +               au_set_ibbot(a->inode, a->mvd_bsrc);
26487 +       }
26488 +       if (au_dbbot(a->dentry) < a->mvd_bdst)
26489 +               au_set_dbbot(a->dentry, a->mvd_bdst);
26490 +       if (au_ibbot(a->inode) < a->mvd_bdst)
26491 +               au_set_ibbot(a->inode, a->mvd_bdst);
26492 +
26493 +out_unlock:
26494 +       au_do_unlock(dmsg, a);
26495 +out:
26496 +       AuTraceErr(err);
26497 +       return err;
26498 +}
26499 +
26500 +/* ---------------------------------------------------------------------- */
26501 +
26502 +/* make sure the file is idle */
26503 +static int au_mvd_args_busy(const unsigned char dmsg, struct au_mvd_args *a)
26504 +{
26505 +       int err, plinked;
26506 +
26507 +       err = 0;
26508 +       plinked = !!au_opt_test(au_mntflags(a->sb), PLINK);
26509 +       if (au_dbtop(a->dentry) == a->mvd_bsrc
26510 +           && au_dcount(a->dentry) == 1
26511 +           && atomic_read(&a->inode->i_count) == 1
26512 +           /* && a->mvd_h_src_inode->i_nlink == 1 */
26513 +           && (!plinked || !au_plink_test(a->inode))
26514 +           && a->inode->i_nlink == 1)
26515 +               goto out;
26516 +
26517 +       err = -EBUSY;
26518 +       AU_MVD_PR(dmsg,
26519 +                 "b%d, d{b%d, c%d?}, i{c%d?, l%u}, hi{l%u}, p{%d, %d}\n",
26520 +                 a->mvd_bsrc, au_dbtop(a->dentry), au_dcount(a->dentry),
26521 +                 atomic_read(&a->inode->i_count), a->inode->i_nlink,
26522 +                 a->mvd_h_src_inode->i_nlink,
26523 +                 plinked, plinked ? au_plink_test(a->inode) : 0);
26524 +
26525 +out:
26526 +       AuTraceErr(err);
26527 +       return err;
26528 +}
26529 +
26530 +/* make sure the parent dir is fine */
26531 +static int au_mvd_args_parent(const unsigned char dmsg,
26532 +                             struct au_mvd_args *a)
26533 +{
26534 +       int err;
26535 +       aufs_bindex_t bindex;
26536 +
26537 +       err = 0;
26538 +       if (unlikely(au_alive_dir(a->parent))) {
26539 +               err = -ENOENT;
26540 +               AU_MVD_PR(dmsg, "parent dir is dead\n");
26541 +               goto out;
26542 +       }
26543 +
26544 +       a->bopq = au_dbdiropq(a->parent);
26545 +       bindex = au_wbr_nonopq(a->dentry, a->mvd_bdst);
26546 +       AuDbg("b%d\n", bindex);
26547 +       if (unlikely((bindex >= 0 && bindex < a->mvd_bdst)
26548 +                    || (a->bopq != -1 && a->bopq < a->mvd_bdst))) {
26549 +               err = -EINVAL;
26550 +               a->mvd_errno = EAU_MVDOWN_OPAQUE;
26551 +               AU_MVD_PR(dmsg, "ancestor is opaque b%d, b%d\n",
26552 +                         a->bopq, a->mvd_bdst);
26553 +       }
26554 +
26555 +out:
26556 +       AuTraceErr(err);
26557 +       return err;
26558 +}
26559 +
26560 +static int au_mvd_args_intermediate(const unsigned char dmsg,
26561 +                                   struct au_mvd_args *a)
26562 +{
26563 +       int err;
26564 +       struct au_dinfo *dinfo, *tmp;
26565 +
26566 +       /* lookup the next lower positive entry */
26567 +       err = -ENOMEM;
26568 +       tmp = au_di_alloc(a->sb, AuLsc_DI_TMP);
26569 +       if (unlikely(!tmp))
26570 +               goto out;
26571 +
26572 +       a->bfound = -1;
26573 +       a->bwh = -1;
26574 +       dinfo = au_di(a->dentry);
26575 +       au_di_cp(tmp, dinfo);
26576 +       au_di_swap(tmp, dinfo);
26577 +
26578 +       /* returns the number of positive dentries */
26579 +       err = au_lkup_dentry(a->dentry, a->mvd_bsrc + 1,
26580 +                            /* AuLkup_IGNORE_PERM */ 0);
26581 +       if (!err)
26582 +               a->bwh = au_dbwh(a->dentry);
26583 +       else if (err > 0)
26584 +               a->bfound = au_dbtop(a->dentry);
26585 +
26586 +       au_di_swap(tmp, dinfo);
26587 +       au_rw_write_unlock(&tmp->di_rwsem);
26588 +       au_di_free(tmp);
26589 +       if (unlikely(err < 0))
26590 +               AU_MVD_PR(dmsg, "failed look-up lower\n");
26591 +
26592 +       /*
26593 +        * here, we have these cases.
26594 +        * bfound == -1
26595 +        *      no positive dentry under bsrc. there are more sub-cases.
26596 +        *      bwh < 0
26597 +        *              there no whiteout, we can safely move-down.
26598 +        *      bwh <= bsrc
26599 +        *              impossible
26600 +        *      bsrc < bwh && bwh < bdst
26601 +        *              there is a whiteout on RO branch. cannot proceed.
26602 +        *      bwh == bdst
26603 +        *              there is a whiteout on the RW target branch. it should
26604 +        *              be removed.
26605 +        *      bdst < bwh
26606 +        *              there is a whiteout somewhere unrelated branch.
26607 +        * -1 < bfound && bfound <= bsrc
26608 +        *      impossible.
26609 +        * bfound < bdst
26610 +        *      found, but it is on RO branch between bsrc and bdst. cannot
26611 +        *      proceed.
26612 +        * bfound == bdst
26613 +        *      found, replace it if AUFS_MVDOWN_FORCE is set. otherwise return
26614 +        *      error.
26615 +        * bdst < bfound
26616 +        *      found, after we create the file on bdst, it will be hidden.
26617 +        */
26618 +
26619 +       AuDebugOn(a->bfound == -1
26620 +                 && a->bwh != -1
26621 +                 && a->bwh <= a->mvd_bsrc);
26622 +       AuDebugOn(-1 < a->bfound
26623 +                 && a->bfound <= a->mvd_bsrc);
26624 +
26625 +       err = -EINVAL;
26626 +       if (a->bfound == -1
26627 +           && a->mvd_bsrc < a->bwh
26628 +           && a->bwh != -1
26629 +           && a->bwh < a->mvd_bdst) {
26630 +               a->mvd_errno = EAU_MVDOWN_WHITEOUT;
26631 +               AU_MVD_PR(dmsg, "bsrc %d, bdst %d, bfound %d, bwh %d\n",
26632 +                         a->mvd_bsrc, a->mvd_bdst, a->bfound, a->bwh);
26633 +               goto out;
26634 +       } else if (a->bfound != -1 && a->bfound < a->mvd_bdst) {
26635 +               a->mvd_errno = EAU_MVDOWN_UPPER;
26636 +               AU_MVD_PR(dmsg, "bdst %d, bfound %d\n",
26637 +                         a->mvd_bdst, a->bfound);
26638 +               goto out;
26639 +       }
26640 +
26641 +       err = 0; /* success */
26642 +
26643 +out:
26644 +       AuTraceErr(err);
26645 +       return err;
26646 +}
26647 +
26648 +static int au_mvd_args_exist(const unsigned char dmsg, struct au_mvd_args *a)
26649 +{
26650 +       int err;
26651 +
26652 +       err = 0;
26653 +       if (!(a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
26654 +           && a->bfound == a->mvd_bdst)
26655 +               err = -EEXIST;
26656 +       AuTraceErr(err);
26657 +       return err;
26658 +}
26659 +
26660 +static int au_mvd_args(const unsigned char dmsg, struct au_mvd_args *a)
26661 +{
26662 +       int err;
26663 +       struct au_branch *br;
26664 +
26665 +       err = -EISDIR;
26666 +       if (unlikely(S_ISDIR(a->inode->i_mode)))
26667 +               goto out;
26668 +
26669 +       err = -EINVAL;
26670 +       if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_UPPER))
26671 +               a->mvd_bsrc = au_ibtop(a->inode);
26672 +       else {
26673 +               a->mvd_bsrc = au_br_index(a->sb, a->mvd_src_brid);
26674 +               if (unlikely(a->mvd_bsrc < 0
26675 +                            || (a->mvd_bsrc < au_dbtop(a->dentry)
26676 +                                || au_dbbot(a->dentry) < a->mvd_bsrc
26677 +                                || !au_h_dptr(a->dentry, a->mvd_bsrc))
26678 +                            || (a->mvd_bsrc < au_ibtop(a->inode)
26679 +                                || au_ibbot(a->inode) < a->mvd_bsrc
26680 +                                || !au_h_iptr(a->inode, a->mvd_bsrc)))) {
26681 +                       a->mvd_errno = EAU_MVDOWN_NOUPPER;
26682 +                       AU_MVD_PR(dmsg, "no upper\n");
26683 +                       goto out;
26684 +               }
26685 +       }
26686 +       if (unlikely(a->mvd_bsrc == au_sbbot(a->sb))) {
26687 +               a->mvd_errno = EAU_MVDOWN_BOTTOM;
26688 +               AU_MVD_PR(dmsg, "on the bottom\n");
26689 +               goto out;
26690 +       }
26691 +       a->mvd_h_src_inode = au_h_iptr(a->inode, a->mvd_bsrc);
26692 +       br = au_sbr(a->sb, a->mvd_bsrc);
26693 +       err = au_br_rdonly(br);
26694 +       if (!(a->mvdown.flags & AUFS_MVDOWN_ROUPPER)) {
26695 +               if (unlikely(err))
26696 +                       goto out;
26697 +       } else if (!(vfsub_native_ro(a->mvd_h_src_inode)
26698 +                    || IS_APPEND(a->mvd_h_src_inode))) {
26699 +               if (err)
26700 +                       a->mvdown.flags |= AUFS_MVDOWN_ROUPPER_R;
26701 +               /* go on */
26702 +       } else
26703 +               goto out;
26704 +
26705 +       err = -EINVAL;
26706 +       if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_LOWER)) {
26707 +               a->mvd_bdst = find_lower_writable(a);
26708 +               if (unlikely(a->mvd_bdst < 0)) {
26709 +                       a->mvd_errno = EAU_MVDOWN_BOTTOM;
26710 +                       AU_MVD_PR(dmsg, "no writable lower branch\n");
26711 +                       goto out;
26712 +               }
26713 +       } else {
26714 +               a->mvd_bdst = au_br_index(a->sb, a->mvd_dst_brid);
26715 +               if (unlikely(a->mvd_bdst < 0
26716 +                            || au_sbbot(a->sb) < a->mvd_bdst)) {
26717 +                       a->mvd_errno = EAU_MVDOWN_NOLOWERBR;
26718 +                       AU_MVD_PR(dmsg, "no lower brid\n");
26719 +                       goto out;
26720 +               }
26721 +       }
26722 +
26723 +       err = au_mvd_args_busy(dmsg, a);
26724 +       if (!err)
26725 +               err = au_mvd_args_parent(dmsg, a);
26726 +       if (!err)
26727 +               err = au_mvd_args_intermediate(dmsg, a);
26728 +       if (!err)
26729 +               err = au_mvd_args_exist(dmsg, a);
26730 +       if (!err)
26731 +               AuDbg("b%d, b%d\n", a->mvd_bsrc, a->mvd_bdst);
26732 +
26733 +out:
26734 +       AuTraceErr(err);
26735 +       return err;
26736 +}
26737 +
26738 +int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *uarg)
26739 +{
26740 +       int err, e;
26741 +       unsigned char dmsg;
26742 +       struct au_mvd_args *args;
26743 +       struct inode *inode;
26744 +
26745 +       inode = d_inode(dentry);
26746 +       err = -EPERM;
26747 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
26748 +               goto out;
26749 +
26750 +       err = -ENOMEM;
26751 +       args = kmalloc(sizeof(*args), GFP_NOFS);
26752 +       if (unlikely(!args))
26753 +               goto out;
26754 +
26755 +       err = copy_from_user(&args->mvdown, uarg, sizeof(args->mvdown));
26756 +       if (!err)
26757 +               /* VERIFY_WRITE */
26758 +               err = !access_ok(uarg, sizeof(*uarg));
26759 +       if (unlikely(err)) {
26760 +               err = -EFAULT;
26761 +               AuTraceErr(err);
26762 +               goto out_free;
26763 +       }
26764 +       AuDbg("flags 0x%x\n", args->mvdown.flags);
26765 +       args->mvdown.flags &= ~(AUFS_MVDOWN_ROLOWER_R | AUFS_MVDOWN_ROUPPER_R);
26766 +       args->mvdown.au_errno = 0;
26767 +       args->dentry = dentry;
26768 +       args->inode = inode;
26769 +       args->sb = dentry->d_sb;
26770 +
26771 +       err = -ENOENT;
26772 +       dmsg = !!(args->mvdown.flags & AUFS_MVDOWN_DMSG);
26773 +       args->parent = dget_parent(dentry);
26774 +       args->dir = d_inode(args->parent);
26775 +       inode_lock_nested(args->dir, I_MUTEX_PARENT);
26776 +       dput(args->parent);
26777 +       if (unlikely(args->parent != dentry->d_parent)) {
26778 +               AU_MVD_PR(dmsg, "parent dir is moved\n");
26779 +               goto out_dir;
26780 +       }
26781 +
26782 +       inode_lock_nested(inode, I_MUTEX_CHILD);
26783 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_NOPLMW);
26784 +       if (unlikely(err))
26785 +               goto out_inode;
26786 +
26787 +       di_write_lock_parent(args->parent);
26788 +       err = au_mvd_args(dmsg, args);
26789 +       if (unlikely(err))
26790 +               goto out_parent;
26791 +
26792 +       err = au_do_mvdown(dmsg, args);
26793 +       if (unlikely(err))
26794 +               goto out_parent;
26795 +
26796 +       au_cpup_attr_timesizes(args->dir);
26797 +       au_cpup_attr_timesizes(inode);
26798 +       if (!(args->mvdown.flags & AUFS_MVDOWN_KUPPER))
26799 +               au_cpup_igen(inode, au_h_iptr(inode, args->mvd_bdst));
26800 +       /* au_digen_dec(dentry); */
26801 +
26802 +out_parent:
26803 +       di_write_unlock(args->parent);
26804 +       aufs_read_unlock(dentry, AuLock_DW);
26805 +out_inode:
26806 +       inode_unlock(inode);
26807 +out_dir:
26808 +       inode_unlock(args->dir);
26809 +out_free:
26810 +       e = copy_to_user(uarg, &args->mvdown, sizeof(args->mvdown));
26811 +       if (unlikely(e))
26812 +               err = -EFAULT;
26813 +       au_kfree_rcu(args);
26814 +out:
26815 +       AuTraceErr(err);
26816 +       return err;
26817 +}
26818 diff -urN /usr/share/empty/fs/aufs/opts.c linux/fs/aufs/opts.c
26819 --- /usr/share/empty/fs/aufs/opts.c     1970-01-01 01:00:00.000000000 +0100
26820 +++ linux/fs/aufs/opts.c        2022-03-21 14:49:05.729966343 +0100
26821 @@ -0,0 +1,1032 @@
26822 +// SPDX-License-Identifier: GPL-2.0
26823 +/*
26824 + * Copyright (C) 2005-2021 Junjiro R. Okajima
26825 + *
26826 + * This program, aufs is free software; you can redistribute it and/or modify
26827 + * it under the terms of the GNU General Public License as published by
26828 + * the Free Software Foundation; either version 2 of the License, or
26829 + * (at your option) any later version.
26830 + *
26831 + * This program is distributed in the hope that it will be useful,
26832 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
26833 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26834 + * GNU General Public License for more details.
26835 + *
26836 + * You should have received a copy of the GNU General Public License
26837 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
26838 + */
26839 +
26840 +/*
26841 + * mount options/flags
26842 + */
26843 +
26844 +#include <linux/types.h> /* a distribution requires */
26845 +#include <linux/parser.h>
26846 +#include "aufs.h"
26847 +
26848 +/* ---------------------------------------------------------------------- */
26849 +
26850 +static const char *au_parser_pattern(int val, match_table_t tbl)
26851 +{
26852 +       struct match_token *p;
26853 +
26854 +       p = tbl;
26855 +       while (p->pattern) {
26856 +               if (p->token == val)
26857 +                       return p->pattern;
26858 +               p++;
26859 +       }
26860 +       BUG();
26861 +       return "??";
26862 +}
26863 +
26864 +static const char *au_optstr(int *val, match_table_t tbl)
26865 +{
26866 +       struct match_token *p;
26867 +       int v;
26868 +
26869 +       v = *val;
26870 +       if (!v)
26871 +               goto out;
26872 +       p = tbl;
26873 +       while (p->pattern) {
26874 +               if (p->token
26875 +                   && (v & p->token) == p->token) {
26876 +                       *val &= ~p->token;
26877 +                       return p->pattern;
26878 +               }
26879 +               p++;
26880 +       }
26881 +
26882 +out:
26883 +       return NULL;
26884 +}
26885 +
26886 +/* ---------------------------------------------------------------------- */
26887 +
26888 +static match_table_t brperm = {
26889 +       {AuBrPerm_RO, AUFS_BRPERM_RO},
26890 +       {AuBrPerm_RR, AUFS_BRPERM_RR},
26891 +       {AuBrPerm_RW, AUFS_BRPERM_RW},
26892 +       {0, NULL}
26893 +};
26894 +
26895 +static match_table_t brattr = {
26896 +       /* general */
26897 +       {AuBrAttr_COO_REG, AUFS_BRATTR_COO_REG},
26898 +       {AuBrAttr_COO_ALL, AUFS_BRATTR_COO_ALL},
26899 +       /* 'unpin' attrib is meaningless since linux-3.18-rc1 */
26900 +       {AuBrAttr_UNPIN, AUFS_BRATTR_UNPIN},
26901 +#ifdef CONFIG_AUFS_FHSM
26902 +       {AuBrAttr_FHSM, AUFS_BRATTR_FHSM},
26903 +#endif
26904 +#ifdef CONFIG_AUFS_XATTR
26905 +       {AuBrAttr_ICEX, AUFS_BRATTR_ICEX},
26906 +       {AuBrAttr_ICEX_SEC, AUFS_BRATTR_ICEX_SEC},
26907 +       {AuBrAttr_ICEX_SYS, AUFS_BRATTR_ICEX_SYS},
26908 +       {AuBrAttr_ICEX_TR, AUFS_BRATTR_ICEX_TR},
26909 +       {AuBrAttr_ICEX_USR, AUFS_BRATTR_ICEX_USR},
26910 +       {AuBrAttr_ICEX_OTH, AUFS_BRATTR_ICEX_OTH},
26911 +#endif
26912 +
26913 +       /* ro/rr branch */
26914 +       {AuBrRAttr_WH, AUFS_BRRATTR_WH},
26915 +
26916 +       /* rw branch */
26917 +       {AuBrWAttr_MOO, AUFS_BRWATTR_MOO},
26918 +       {AuBrWAttr_NoLinkWH, AUFS_BRWATTR_NLWH},
26919 +
26920 +       {0, NULL}
26921 +};
26922 +
26923 +static int br_attr_val(char *str, match_table_t table, substring_t args[])
26924 +{
26925 +       int attr, v;
26926 +       char *p;
26927 +
26928 +       attr = 0;
26929 +       do {
26930 +               p = strchr(str, '+');
26931 +               if (p)
26932 +                       *p = 0;
26933 +               v = match_token(str, table, args);
26934 +               if (v) {
26935 +                       if (v & AuBrAttr_CMOO_Mask)
26936 +                               attr &= ~AuBrAttr_CMOO_Mask;
26937 +                       attr |= v;
26938 +               } else {
26939 +                       if (p)
26940 +                               *p = '+';
26941 +                       pr_warn("ignored branch attribute %s\n", str);
26942 +                       break;
26943 +               }
26944 +               if (p)
26945 +                       str = p + 1;
26946 +       } while (p);
26947 +
26948 +       return attr;
26949 +}
26950 +
26951 +static int au_do_optstr_br_attr(au_br_perm_str_t *str, int perm)
26952 +{
26953 +       int sz;
26954 +       const char *p;
26955 +       char *q;
26956 +
26957 +       q = str->a;
26958 +       *q = 0;
26959 +       p = au_optstr(&perm, brattr);
26960 +       if (p) {
26961 +               sz = strlen(p);
26962 +               memcpy(q, p, sz + 1);
26963 +               q += sz;
26964 +       } else
26965 +               goto out;
26966 +
26967 +       do {
26968 +               p = au_optstr(&perm, brattr);
26969 +               if (p) {
26970 +                       *q++ = '+';
26971 +                       sz = strlen(p);
26972 +                       memcpy(q, p, sz + 1);
26973 +                       q += sz;
26974 +               }
26975 +       } while (p);
26976 +
26977 +out:
26978 +       return q - str->a;
26979 +}
26980 +
26981 +int au_br_perm_val(char *perm)
26982 +{
26983 +       int val, bad, sz;
26984 +       char *p;
26985 +       substring_t args[MAX_OPT_ARGS];
26986 +       au_br_perm_str_t attr;
26987 +
26988 +       p = strchr(perm, '+');
26989 +       if (p)
26990 +               *p = 0;
26991 +       val = match_token(perm, brperm, args);
26992 +       if (!val) {
26993 +               if (p)
26994 +                       *p = '+';
26995 +               pr_warn("ignored branch permission %s\n", perm);
26996 +               val = AuBrPerm_RO;
26997 +               goto out;
26998 +       }
26999 +       if (!p)
27000 +               goto out;
27001 +
27002 +       val |= br_attr_val(p + 1, brattr, args);
27003 +
27004 +       bad = 0;
27005 +       switch (val & AuBrPerm_Mask) {
27006 +       case AuBrPerm_RO:
27007 +       case AuBrPerm_RR:
27008 +               bad = val & AuBrWAttr_Mask;
27009 +               val &= ~AuBrWAttr_Mask;
27010 +               break;
27011 +       case AuBrPerm_RW:
27012 +               bad = val & AuBrRAttr_Mask;
27013 +               val &= ~AuBrRAttr_Mask;
27014 +               break;
27015 +       }
27016 +
27017 +       /*
27018 +        * 'unpin' attrib becomes meaningless since linux-3.18-rc1, but aufs
27019 +        * does not treat it as an error, just warning.
27020 +        * this is a tiny guard for the user operation.
27021 +        */
27022 +       if (val & AuBrAttr_UNPIN) {
27023 +               bad |= AuBrAttr_UNPIN;
27024 +               val &= ~AuBrAttr_UNPIN;
27025 +       }
27026 +
27027 +       if (unlikely(bad)) {
27028 +               sz = au_do_optstr_br_attr(&attr, bad);
27029 +               AuDebugOn(!sz);
27030 +               pr_warn("ignored branch attribute %s\n", attr.a);
27031 +       }
27032 +
27033 +out:
27034 +       return val;
27035 +}
27036 +
27037 +void au_optstr_br_perm(au_br_perm_str_t *str, int perm)
27038 +{
27039 +       au_br_perm_str_t attr;
27040 +       const char *p;
27041 +       char *q;
27042 +       int sz;
27043 +
27044 +       q = str->a;
27045 +       p = au_optstr(&perm, brperm);
27046 +       AuDebugOn(!p || !*p);
27047 +       sz = strlen(p);
27048 +       memcpy(q, p, sz + 1);
27049 +       q += sz;
27050 +
27051 +       sz = au_do_optstr_br_attr(&attr, perm);
27052 +       if (sz) {
27053 +               *q++ = '+';
27054 +               memcpy(q, attr.a, sz + 1);
27055 +       }
27056 +
27057 +       AuDebugOn(strlen(str->a) >= sizeof(str->a));
27058 +}
27059 +
27060 +/* ---------------------------------------------------------------------- */
27061 +
27062 +static match_table_t udbalevel = {
27063 +       {AuOpt_UDBA_REVAL, "reval"},
27064 +       {AuOpt_UDBA_NONE, "none"},
27065 +#ifdef CONFIG_AUFS_HNOTIFY
27066 +       {AuOpt_UDBA_HNOTIFY, "notify"}, /* abstraction */
27067 +#ifdef CONFIG_AUFS_HFSNOTIFY
27068 +       {AuOpt_UDBA_HNOTIFY, "fsnotify"},
27069 +#endif
27070 +#endif
27071 +       {-1, NULL}
27072 +};
27073 +
27074 +int au_udba_val(char *str)
27075 +{
27076 +       substring_t args[MAX_OPT_ARGS];
27077 +
27078 +       return match_token(str, udbalevel, args);
27079 +}
27080 +
27081 +const char *au_optstr_udba(int udba)
27082 +{
27083 +       return au_parser_pattern(udba, udbalevel);
27084 +}
27085 +
27086 +/* ---------------------------------------------------------------------- */
27087 +
27088 +static match_table_t au_wbr_create_policy = {
27089 +       {AuWbrCreate_TDP, "tdp"},
27090 +       {AuWbrCreate_TDP, "top-down-parent"},
27091 +       {AuWbrCreate_RR, "rr"},
27092 +       {AuWbrCreate_RR, "round-robin"},
27093 +       {AuWbrCreate_MFS, "mfs"},
27094 +       {AuWbrCreate_MFS, "most-free-space"},
27095 +       {AuWbrCreate_MFSV, "mfs:%d"},
27096 +       {AuWbrCreate_MFSV, "most-free-space:%d"},
27097 +
27098 +       /* top-down regardless the parent, and then mfs */
27099 +       {AuWbrCreate_TDMFS, "tdmfs:%d"},
27100 +       {AuWbrCreate_TDMFSV, "tdmfs:%d:%d"},
27101 +
27102 +       {AuWbrCreate_MFSRR, "mfsrr:%d"},
27103 +       {AuWbrCreate_MFSRRV, "mfsrr:%d:%d"},
27104 +       {AuWbrCreate_PMFS, "pmfs"},
27105 +       {AuWbrCreate_PMFSV, "pmfs:%d"},
27106 +       {AuWbrCreate_PMFSRR, "pmfsrr:%d"},
27107 +       {AuWbrCreate_PMFSRRV, "pmfsrr:%d:%d"},
27108 +
27109 +       {-1, NULL}
27110 +};
27111 +
27112 +static int au_wbr_mfs_wmark(substring_t *arg, char *str,
27113 +                           struct au_opt_wbr_create *create)
27114 +{
27115 +       int err;
27116 +       unsigned long long ull;
27117 +
27118 +       err = 0;
27119 +       if (!match_u64(arg, &ull))
27120 +               create->mfsrr_watermark = ull;
27121 +       else {
27122 +               pr_err("bad integer in %s\n", str);
27123 +               err = -EINVAL;
27124 +       }
27125 +
27126 +       return err;
27127 +}
27128 +
27129 +static int au_wbr_mfs_sec(substring_t *arg, char *str,
27130 +                         struct au_opt_wbr_create *create)
27131 +{
27132 +       int n, err;
27133 +
27134 +       err = 0;
27135 +       if (!match_int(arg, &n) && 0 <= n && n <= AUFS_MFS_MAX_SEC)
27136 +               create->mfs_second = n;
27137 +       else {
27138 +               pr_err("bad integer in %s\n", str);
27139 +               err = -EINVAL;
27140 +       }
27141 +
27142 +       return err;
27143 +}
27144 +
27145 +int au_wbr_create_val(char *str, struct au_opt_wbr_create *create)
27146 +{
27147 +       int err, e;
27148 +       substring_t args[MAX_OPT_ARGS];
27149 +
27150 +       err = match_token(str, au_wbr_create_policy, args);
27151 +       create->wbr_create = err;
27152 +       switch (err) {
27153 +       case AuWbrCreate_MFSRRV:
27154 +       case AuWbrCreate_TDMFSV:
27155 +       case AuWbrCreate_PMFSRRV:
27156 +               e = au_wbr_mfs_wmark(&args[0], str, create);
27157 +               if (!e)
27158 +                       e = au_wbr_mfs_sec(&args[1], str, create);
27159 +               if (unlikely(e))
27160 +                       err = e;
27161 +               break;
27162 +       case AuWbrCreate_MFSRR:
27163 +       case AuWbrCreate_TDMFS:
27164 +       case AuWbrCreate_PMFSRR:
27165 +               e = au_wbr_mfs_wmark(&args[0], str, create);
27166 +               if (unlikely(e)) {
27167 +                       err = e;
27168 +                       break;
27169 +               }
27170 +               fallthrough;
27171 +       case AuWbrCreate_MFS:
27172 +       case AuWbrCreate_PMFS:
27173 +               create->mfs_second = AUFS_MFS_DEF_SEC;
27174 +               break;
27175 +       case AuWbrCreate_MFSV:
27176 +       case AuWbrCreate_PMFSV:
27177 +               e = au_wbr_mfs_sec(&args[0], str, create);
27178 +               if (unlikely(e))
27179 +                       err = e;
27180 +               break;
27181 +       }
27182 +
27183 +       return err;
27184 +}
27185 +
27186 +const char *au_optstr_wbr_create(int wbr_create)
27187 +{
27188 +       return au_parser_pattern(wbr_create, au_wbr_create_policy);
27189 +}
27190 +
27191 +static match_table_t au_wbr_copyup_policy = {
27192 +       {AuWbrCopyup_TDP, "tdp"},
27193 +       {AuWbrCopyup_TDP, "top-down-parent"},
27194 +       {AuWbrCopyup_BUP, "bup"},
27195 +       {AuWbrCopyup_BUP, "bottom-up-parent"},
27196 +       {AuWbrCopyup_BU, "bu"},
27197 +       {AuWbrCopyup_BU, "bottom-up"},
27198 +       {-1, NULL}
27199 +};
27200 +
27201 +int au_wbr_copyup_val(char *str)
27202 +{
27203 +       substring_t args[MAX_OPT_ARGS];
27204 +
27205 +       return match_token(str, au_wbr_copyup_policy, args);
27206 +}
27207 +
27208 +const char *au_optstr_wbr_copyup(int wbr_copyup)
27209 +{
27210 +       return au_parser_pattern(wbr_copyup, au_wbr_copyup_policy);
27211 +}
27212 +
27213 +/* ---------------------------------------------------------------------- */
27214 +
27215 +int au_opt_add(struct au_opt *opt, char *opt_str, unsigned long sb_flags,
27216 +              aufs_bindex_t bindex)
27217 +{
27218 +       int err;
27219 +       struct au_opt_add *add = &opt->add;
27220 +       char *p;
27221 +
27222 +       add->bindex = bindex;
27223 +       add->perm = AuBrPerm_RO;
27224 +       add->pathname = opt_str;
27225 +       p = strchr(opt_str, '=');
27226 +       if (p) {
27227 +               *p++ = 0;
27228 +               if (*p)
27229 +                       add->perm = au_br_perm_val(p);
27230 +       }
27231 +
27232 +       err = vfsub_kern_path(add->pathname, AuOpt_LkupDirFlags, &add->path);
27233 +       if (!err) {
27234 +               if (!p) {
27235 +                       add->perm = AuBrPerm_RO;
27236 +                       if (au_test_fs_rr(add->path.dentry->d_sb))
27237 +                               add->perm = AuBrPerm_RR;
27238 +                       else if (!bindex && !(sb_flags & SB_RDONLY))
27239 +                               add->perm = AuBrPerm_RW;
27240 +               }
27241 +               opt->type = Opt_add;
27242 +               goto out;
27243 +       }
27244 +       pr_err("lookup failed %s (%d)\n", add->pathname, err);
27245 +       err = -EINVAL;
27246 +
27247 +out:
27248 +       return err;
27249 +}
27250 +
27251 +static int au_opt_wbr_create(struct super_block *sb,
27252 +                            struct au_opt_wbr_create *create)
27253 +{
27254 +       int err;
27255 +       struct au_sbinfo *sbinfo;
27256 +
27257 +       SiMustWriteLock(sb);
27258 +
27259 +       err = 1; /* handled */
27260 +       sbinfo = au_sbi(sb);
27261 +       if (sbinfo->si_wbr_create_ops->fin) {
27262 +               err = sbinfo->si_wbr_create_ops->fin(sb);
27263 +               if (!err)
27264 +                       err = 1;
27265 +       }
27266 +
27267 +       sbinfo->si_wbr_create = create->wbr_create;
27268 +       sbinfo->si_wbr_create_ops = au_wbr_create_ops + create->wbr_create;
27269 +       switch (create->wbr_create) {
27270 +       case AuWbrCreate_MFSRRV:
27271 +       case AuWbrCreate_MFSRR:
27272 +       case AuWbrCreate_TDMFS:
27273 +       case AuWbrCreate_TDMFSV:
27274 +       case AuWbrCreate_PMFSRR:
27275 +       case AuWbrCreate_PMFSRRV:
27276 +               sbinfo->si_wbr_mfs.mfsrr_watermark = create->mfsrr_watermark;
27277 +               fallthrough;
27278 +       case AuWbrCreate_MFS:
27279 +       case AuWbrCreate_MFSV:
27280 +       case AuWbrCreate_PMFS:
27281 +       case AuWbrCreate_PMFSV:
27282 +               sbinfo->si_wbr_mfs.mfs_expire
27283 +                       = msecs_to_jiffies(create->mfs_second * MSEC_PER_SEC);
27284 +               break;
27285 +       }
27286 +
27287 +       if (sbinfo->si_wbr_create_ops->init)
27288 +               sbinfo->si_wbr_create_ops->init(sb); /* ignore */
27289 +
27290 +       return err;
27291 +}
27292 +
27293 +/*
27294 + * returns,
27295 + * plus: processed without an error
27296 + * zero: unprocessed
27297 + */
27298 +static int au_opt_simple(struct super_block *sb, struct au_opt *opt,
27299 +                        struct au_opts *opts)
27300 +{
27301 +       int err;
27302 +       struct au_sbinfo *sbinfo;
27303 +
27304 +       SiMustWriteLock(sb);
27305 +
27306 +       err = 1; /* handled */
27307 +       sbinfo = au_sbi(sb);
27308 +       switch (opt->type) {
27309 +       case Opt_udba:
27310 +               sbinfo->si_mntflags &= ~AuOptMask_UDBA;
27311 +               sbinfo->si_mntflags |= opt->udba;
27312 +               opts->given_udba |= opt->udba;
27313 +               break;
27314 +
27315 +       case Opt_plink:
27316 +               if (opt->tf)
27317 +                       au_opt_set(sbinfo->si_mntflags, PLINK);
27318 +               else {
27319 +                       if (au_opt_test(sbinfo->si_mntflags, PLINK))
27320 +                               au_plink_put(sb, /*verbose*/1);
27321 +                       au_opt_clr(sbinfo->si_mntflags, PLINK);
27322 +               }
27323 +               break;
27324 +       case Opt_list_plink:
27325 +               if (au_opt_test(sbinfo->si_mntflags, PLINK))
27326 +                       au_plink_list(sb);
27327 +               break;
27328 +
27329 +       case Opt_dio:
27330 +               if (opt->tf) {
27331 +                       au_opt_set(sbinfo->si_mntflags, DIO);
27332 +                       au_fset_opts(opts->flags, REFRESH_DYAOP);
27333 +               } else {
27334 +                       au_opt_clr(sbinfo->si_mntflags, DIO);
27335 +                       au_fset_opts(opts->flags, REFRESH_DYAOP);
27336 +               }
27337 +               break;
27338 +
27339 +       case Opt_fhsm_sec:
27340 +               au_fhsm_set(sbinfo, opt->fhsm_second);
27341 +               break;
27342 +
27343 +       case Opt_diropq_a:
27344 +               au_opt_set(sbinfo->si_mntflags, ALWAYS_DIROPQ);
27345 +               break;
27346 +       case Opt_diropq_w:
27347 +               au_opt_clr(sbinfo->si_mntflags, ALWAYS_DIROPQ);
27348 +               break;
27349 +
27350 +       case Opt_warn_perm:
27351 +               if (opt->tf)
27352 +                       au_opt_set(sbinfo->si_mntflags, WARN_PERM);
27353 +               else
27354 +                       au_opt_clr(sbinfo->si_mntflags, WARN_PERM);
27355 +               break;
27356 +
27357 +       case Opt_verbose:
27358 +               if (opt->tf)
27359 +                       au_opt_set(sbinfo->si_mntflags, VERBOSE);
27360 +               else
27361 +                       au_opt_clr(sbinfo->si_mntflags, VERBOSE);
27362 +               break;
27363 +
27364 +       case Opt_sum:
27365 +               if (opt->tf)
27366 +                       au_opt_set(sbinfo->si_mntflags, SUM);
27367 +               else {
27368 +                       au_opt_clr(sbinfo->si_mntflags, SUM);
27369 +                       au_opt_clr(sbinfo->si_mntflags, SUM_W);
27370 +               }
27371 +               break;
27372 +       case Opt_wsum:
27373 +               au_opt_clr(sbinfo->si_mntflags, SUM);
27374 +               au_opt_set(sbinfo->si_mntflags, SUM_W);
27375 +               break;
27376 +
27377 +       case Opt_wbr_create:
27378 +               err = au_opt_wbr_create(sb, &opt->wbr_create);
27379 +               break;
27380 +       case Opt_wbr_copyup:
27381 +               sbinfo->si_wbr_copyup = opt->wbr_copyup;
27382 +               sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + opt->wbr_copyup;
27383 +               break;
27384 +
27385 +       case Opt_dirwh:
27386 +               sbinfo->si_dirwh = opt->dirwh;
27387 +               break;
27388 +
27389 +       case Opt_rdcache:
27390 +               sbinfo->si_rdcache
27391 +                       = msecs_to_jiffies(opt->rdcache * MSEC_PER_SEC);
27392 +               break;
27393 +       case Opt_rdblk:
27394 +               sbinfo->si_rdblk = opt->rdblk;
27395 +               break;
27396 +       case Opt_rdhash:
27397 +               sbinfo->si_rdhash = opt->rdhash;
27398 +               break;
27399 +
27400 +       case Opt_shwh:
27401 +               if (opt->tf)
27402 +                       au_opt_set(sbinfo->si_mntflags, SHWH);
27403 +               else
27404 +                       au_opt_clr(sbinfo->si_mntflags, SHWH);
27405 +               break;
27406 +
27407 +       case Opt_dirperm1:
27408 +               if (opt->tf)
27409 +                       au_opt_set(sbinfo->si_mntflags, DIRPERM1);
27410 +               else
27411 +                       au_opt_clr(sbinfo->si_mntflags, DIRPERM1);
27412 +               break;
27413 +
27414 +       case Opt_trunc_xino:
27415 +               if (opt->tf)
27416 +                       au_opt_set(sbinfo->si_mntflags, TRUNC_XINO);
27417 +               else
27418 +                       au_opt_clr(sbinfo->si_mntflags, TRUNC_XINO);
27419 +               break;
27420 +
27421 +       case Opt_trunc_xino_path:
27422 +       case Opt_itrunc_xino:
27423 +               err = au_xino_trunc(sb, opt->xino_itrunc.bindex,
27424 +                                   /*idx_begin*/0);
27425 +               if (!err)
27426 +                       err = 1;
27427 +               break;
27428 +
27429 +       case Opt_trunc_xib:
27430 +               if (opt->tf)
27431 +                       au_fset_opts(opts->flags, TRUNC_XIB);
27432 +               else
27433 +                       au_fclr_opts(opts->flags, TRUNC_XIB);
27434 +               break;
27435 +
27436 +       case Opt_dirren:
27437 +               err = 1;
27438 +               if (opt->tf) {
27439 +                       if (!au_opt_test(sbinfo->si_mntflags, DIRREN)) {
27440 +                               err = au_dr_opt_set(sb);
27441 +                               if (!err)
27442 +                                       err = 1;
27443 +                       }
27444 +                       if (err == 1)
27445 +                               au_opt_set(sbinfo->si_mntflags, DIRREN);
27446 +               } else {
27447 +                       if (au_opt_test(sbinfo->si_mntflags, DIRREN)) {
27448 +                               err = au_dr_opt_clr(sb, au_ftest_opts(opts->flags,
27449 +                                                                     DR_FLUSHED));
27450 +                               if (!err)
27451 +                                       err = 1;
27452 +                       }
27453 +                       if (err == 1)
27454 +                               au_opt_clr(sbinfo->si_mntflags, DIRREN);
27455 +               }
27456 +               break;
27457 +
27458 +       case Opt_acl:
27459 +               if (opt->tf)
27460 +                       sb->s_flags |= SB_POSIXACL;
27461 +               else
27462 +                       sb->s_flags &= ~SB_POSIXACL;
27463 +               break;
27464 +
27465 +       default:
27466 +               err = 0;
27467 +               break;
27468 +       }
27469 +
27470 +       return err;
27471 +}
27472 +
27473 +/*
27474 + * returns tri-state.
27475 + * plus: processed without an error
27476 + * zero: unprocessed
27477 + * minus: error
27478 + */
27479 +static int au_opt_br(struct super_block *sb, struct au_opt *opt,
27480 +                    struct au_opts *opts)
27481 +{
27482 +       int err, do_refresh;
27483 +
27484 +       err = 0;
27485 +       switch (opt->type) {
27486 +       case Opt_append:
27487 +               opt->add.bindex = au_sbbot(sb) + 1;
27488 +               if (opt->add.bindex < 0)
27489 +                       opt->add.bindex = 0;
27490 +               goto add;
27491 +               /* Always goto add, not fallthrough */
27492 +       case Opt_prepend:
27493 +               opt->add.bindex = 0;
27494 +               fallthrough;
27495 +       add: /* indented label */
27496 +       case Opt_add:
27497 +               err = au_br_add(sb, &opt->add,
27498 +                               au_ftest_opts(opts->flags, REMOUNT));
27499 +               if (!err) {
27500 +                       err = 1;
27501 +                       au_fset_opts(opts->flags, REFRESH);
27502 +               }
27503 +               break;
27504 +
27505 +       case Opt_del:
27506 +       case Opt_idel:
27507 +               err = au_br_del(sb, &opt->del,
27508 +                               au_ftest_opts(opts->flags, REMOUNT));
27509 +               if (!err) {
27510 +                       err = 1;
27511 +                       au_fset_opts(opts->flags, TRUNC_XIB);
27512 +                       au_fset_opts(opts->flags, REFRESH);
27513 +               }
27514 +               break;
27515 +
27516 +       case Opt_mod:
27517 +       case Opt_imod:
27518 +               err = au_br_mod(sb, &opt->mod,
27519 +                               au_ftest_opts(opts->flags, REMOUNT),
27520 +                               &do_refresh);
27521 +               if (!err) {
27522 +                       err = 1;
27523 +                       if (do_refresh)
27524 +                               au_fset_opts(opts->flags, REFRESH);
27525 +               }
27526 +               break;
27527 +       }
27528 +       return err;
27529 +}
27530 +
27531 +static int au_opt_xino(struct super_block *sb, struct au_opt *opt,
27532 +                      struct au_opt_xino **opt_xino,
27533 +                      struct au_opts *opts)
27534 +{
27535 +       int err;
27536 +
27537 +       err = 0;
27538 +       switch (opt->type) {
27539 +       case Opt_xino:
27540 +               err = au_xino_set(sb, &opt->xino,
27541 +                                 !!au_ftest_opts(opts->flags, REMOUNT));
27542 +               if (!err)
27543 +                       *opt_xino = &opt->xino;
27544 +               break;
27545 +       case Opt_noxino:
27546 +               au_xino_clr(sb);
27547 +               *opt_xino = (void *)-1;
27548 +               break;
27549 +       }
27550 +
27551 +       return err;
27552 +}
27553 +
27554 +int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
27555 +                  unsigned int pending)
27556 +{
27557 +       int err, fhsm;
27558 +       aufs_bindex_t bindex, bbot;
27559 +       unsigned char do_plink, skip, do_free, can_no_dreval;
27560 +       struct au_branch *br;
27561 +       struct au_wbr *wbr;
27562 +       struct dentry *root, *dentry;
27563 +       struct inode *dir, *h_dir;
27564 +       struct au_sbinfo *sbinfo;
27565 +       struct au_hinode *hdir;
27566 +
27567 +       SiMustAnyLock(sb);
27568 +
27569 +       sbinfo = au_sbi(sb);
27570 +       AuDebugOn(!(sbinfo->si_mntflags & AuOptMask_UDBA));
27571 +
27572 +       if (!(sb_flags & SB_RDONLY)) {
27573 +               if (unlikely(!au_br_writable(au_sbr_perm(sb, 0))))
27574 +                       pr_warn("first branch should be rw\n");
27575 +               if (unlikely(au_opt_test(sbinfo->si_mntflags, SHWH)))
27576 +                       pr_warn_once("shwh should be used with ro\n");
27577 +       }
27578 +
27579 +       if (au_opt_test((sbinfo->si_mntflags | pending), UDBA_HNOTIFY)
27580 +           && !au_opt_test(sbinfo->si_mntflags, XINO))
27581 +               pr_warn_once("udba=*notify requires xino\n");
27582 +
27583 +       if (au_opt_test(sbinfo->si_mntflags, DIRPERM1))
27584 +               pr_warn_once("dirperm1 breaks the protection"
27585 +                            " by the permission bits on the lower branch\n");
27586 +
27587 +       err = 0;
27588 +       fhsm = 0;
27589 +       root = sb->s_root;
27590 +       dir = d_inode(root);
27591 +       do_plink = !!au_opt_test(sbinfo->si_mntflags, PLINK);
27592 +       can_no_dreval = !!au_opt_test((sbinfo->si_mntflags | pending),
27593 +                                     UDBA_NONE);
27594 +       bbot = au_sbbot(sb);
27595 +       for (bindex = 0; !err && bindex <= bbot; bindex++) {
27596 +               skip = 0;
27597 +               h_dir = au_h_iptr(dir, bindex);
27598 +               br = au_sbr(sb, bindex);
27599 +
27600 +               if ((br->br_perm & AuBrAttr_ICEX)
27601 +                   && !h_dir->i_op->listxattr)
27602 +                       br->br_perm &= ~AuBrAttr_ICEX;
27603 +#if 0 /* untested */
27604 +               if ((br->br_perm & AuBrAttr_ICEX_SEC)
27605 +                   && (au_br_sb(br)->s_flags & SB_NOSEC))
27606 +                       br->br_perm &= ~AuBrAttr_ICEX_SEC;
27607 +#endif
27608 +
27609 +               do_free = 0;
27610 +               wbr = br->br_wbr;
27611 +               if (wbr)
27612 +                       wbr_wh_read_lock(wbr);
27613 +
27614 +               if (!au_br_writable(br->br_perm)) {
27615 +                       do_free = !!wbr;
27616 +                       skip = (!wbr
27617 +                               || (!wbr->wbr_whbase
27618 +                                   && !wbr->wbr_plink
27619 +                                   && !wbr->wbr_orph));
27620 +               } else if (!au_br_wh_linkable(br->br_perm)) {
27621 +                       /* skip = (!br->br_whbase && !br->br_orph); */
27622 +                       skip = (!wbr || !wbr->wbr_whbase);
27623 +                       if (skip && wbr) {
27624 +                               if (do_plink)
27625 +                                       skip = !!wbr->wbr_plink;
27626 +                               else
27627 +                                       skip = !wbr->wbr_plink;
27628 +                       }
27629 +               } else {
27630 +                       /* skip = (br->br_whbase && br->br_ohph); */
27631 +                       skip = (wbr && wbr->wbr_whbase);
27632 +                       if (skip) {
27633 +                               if (do_plink)
27634 +                                       skip = !!wbr->wbr_plink;
27635 +                               else
27636 +                                       skip = !wbr->wbr_plink;
27637 +                       }
27638 +               }
27639 +               if (wbr)
27640 +                       wbr_wh_read_unlock(wbr);
27641 +
27642 +               if (can_no_dreval) {
27643 +                       dentry = br->br_path.dentry;
27644 +                       spin_lock(&dentry->d_lock);
27645 +                       if (dentry->d_flags &
27646 +                           (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE))
27647 +                               can_no_dreval = 0;
27648 +                       spin_unlock(&dentry->d_lock);
27649 +               }
27650 +
27651 +               if (au_br_fhsm(br->br_perm)) {
27652 +                       fhsm++;
27653 +                       AuDebugOn(!br->br_fhsm);
27654 +               }
27655 +
27656 +               if (skip)
27657 +                       continue;
27658 +
27659 +               hdir = au_hi(dir, bindex);
27660 +               au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
27661 +               if (wbr)
27662 +                       wbr_wh_write_lock(wbr);
27663 +               err = au_wh_init(br, sb);
27664 +               if (wbr)
27665 +                       wbr_wh_write_unlock(wbr);
27666 +               au_hn_inode_unlock(hdir);
27667 +
27668 +               if (!err && do_free) {
27669 +                       au_kfree_rcu(wbr);
27670 +                       br->br_wbr = NULL;
27671 +               }
27672 +       }
27673 +
27674 +       if (can_no_dreval)
27675 +               au_fset_si(sbinfo, NO_DREVAL);
27676 +       else
27677 +               au_fclr_si(sbinfo, NO_DREVAL);
27678 +
27679 +       if (fhsm >= 2) {
27680 +               au_fset_si(sbinfo, FHSM);
27681 +               for (bindex = bbot; bindex >= 0; bindex--) {
27682 +                       br = au_sbr(sb, bindex);
27683 +                       if (au_br_fhsm(br->br_perm)) {
27684 +                               au_fhsm_set_bottom(sb, bindex);
27685 +                               break;
27686 +                       }
27687 +               }
27688 +       } else {
27689 +               au_fclr_si(sbinfo, FHSM);
27690 +               au_fhsm_set_bottom(sb, -1);
27691 +       }
27692 +
27693 +       return err;
27694 +}
27695 +
27696 +int au_opts_mount(struct super_block *sb, struct au_opts *opts)
27697 +{
27698 +       int err;
27699 +       unsigned int tmp;
27700 +       aufs_bindex_t bindex, bbot;
27701 +       struct au_opt *opt;
27702 +       struct au_opt_xino *opt_xino, xino;
27703 +       struct au_sbinfo *sbinfo;
27704 +       struct au_branch *br;
27705 +       struct inode *dir;
27706 +
27707 +       SiMustWriteLock(sb);
27708 +
27709 +       err = 0;
27710 +       opt_xino = NULL;
27711 +       opt = opts->opt;
27712 +       while (err >= 0 && opt->type != Opt_tail)
27713 +               err = au_opt_simple(sb, opt++, opts);
27714 +       if (err > 0)
27715 +               err = 0;
27716 +       else if (unlikely(err < 0))
27717 +               goto out;
27718 +
27719 +       /* disable xino and udba temporary */
27720 +       sbinfo = au_sbi(sb);
27721 +       tmp = sbinfo->si_mntflags;
27722 +       au_opt_clr(sbinfo->si_mntflags, XINO);
27723 +       au_opt_set_udba(sbinfo->si_mntflags, UDBA_REVAL);
27724 +
27725 +       opt = opts->opt;
27726 +       while (err >= 0 && opt->type != Opt_tail)
27727 +               err = au_opt_br(sb, opt++, opts);
27728 +       if (err > 0)
27729 +               err = 0;
27730 +       else if (unlikely(err < 0))
27731 +               goto out;
27732 +
27733 +       bbot = au_sbbot(sb);
27734 +       if (unlikely(bbot < 0)) {
27735 +               err = -EINVAL;
27736 +               pr_err("no branches\n");
27737 +               goto out;
27738 +       }
27739 +
27740 +       if (au_opt_test(tmp, XINO))
27741 +               au_opt_set(sbinfo->si_mntflags, XINO);
27742 +       opt = opts->opt;
27743 +       while (!err && opt->type != Opt_tail)
27744 +               err = au_opt_xino(sb, opt++, &opt_xino, opts);
27745 +       if (unlikely(err))
27746 +               goto out;
27747 +
27748 +       err = au_opts_verify(sb, sb->s_flags, tmp);
27749 +       if (unlikely(err))
27750 +               goto out;
27751 +
27752 +       /* restore xino */
27753 +       if (au_opt_test(tmp, XINO) && !opt_xino) {
27754 +               xino.file = au_xino_def(sb);
27755 +               err = PTR_ERR(xino.file);
27756 +               if (IS_ERR(xino.file))
27757 +                       goto out;
27758 +
27759 +               err = au_xino_set(sb, &xino, /*remount*/0);
27760 +               fput(xino.file);
27761 +               if (unlikely(err))
27762 +                       goto out;
27763 +       }
27764 +
27765 +       /* restore udba */
27766 +       tmp &= AuOptMask_UDBA;
27767 +       sbinfo->si_mntflags &= ~AuOptMask_UDBA;
27768 +       sbinfo->si_mntflags |= tmp;
27769 +       bbot = au_sbbot(sb);
27770 +       for (bindex = 0; bindex <= bbot; bindex++) {
27771 +               br = au_sbr(sb, bindex);
27772 +               err = au_hnotify_reset_br(tmp, br, br->br_perm);
27773 +               if (unlikely(err))
27774 +                       AuIOErr("hnotify failed on br %d, %d, ignored\n",
27775 +                               bindex, err);
27776 +               /* go on even if err */
27777 +       }
27778 +       if (au_opt_test(tmp, UDBA_HNOTIFY)) {
27779 +               dir = d_inode(sb->s_root);
27780 +               au_hn_reset(dir, au_hi_flags(dir, /*isdir*/1) & ~AuHi_XINO);
27781 +       }
27782 +
27783 +out:
27784 +       return err;
27785 +}
27786 +
27787 +int au_opts_remount(struct super_block *sb, struct au_opts *opts)
27788 +{
27789 +       int err, rerr;
27790 +       unsigned char no_dreval;
27791 +       struct inode *dir;
27792 +       struct au_opt_xino *opt_xino;
27793 +       struct au_opt *opt;
27794 +       struct au_sbinfo *sbinfo;
27795 +
27796 +       SiMustWriteLock(sb);
27797 +
27798 +       err = au_dr_opt_flush(sb);
27799 +       if (unlikely(err))
27800 +               goto out;
27801 +       au_fset_opts(opts->flags, DR_FLUSHED);
27802 +
27803 +       dir = d_inode(sb->s_root);
27804 +       sbinfo = au_sbi(sb);
27805 +       opt_xino = NULL;
27806 +       opt = opts->opt;
27807 +       while (err >= 0 && opt->type != Opt_tail) {
27808 +               err = au_opt_simple(sb, opt, opts);
27809 +               if (!err)
27810 +                       err = au_opt_br(sb, opt, opts);
27811 +               if (!err)
27812 +                       err = au_opt_xino(sb, opt, &opt_xino, opts);
27813 +               opt++;
27814 +       }
27815 +       if (err > 0)
27816 +               err = 0;
27817 +       AuTraceErr(err);
27818 +       /* go on even err */
27819 +
27820 +       no_dreval = !!au_ftest_si(sbinfo, NO_DREVAL);
27821 +       rerr = au_opts_verify(sb, opts->sb_flags, /*pending*/0);
27822 +       if (unlikely(rerr && !err))
27823 +               err = rerr;
27824 +
27825 +       if (no_dreval != !!au_ftest_si(sbinfo, NO_DREVAL))
27826 +               au_fset_opts(opts->flags, REFRESH_IDOP);
27827 +
27828 +       if (au_ftest_opts(opts->flags, TRUNC_XIB)) {
27829 +               rerr = au_xib_trunc(sb);
27830 +               if (unlikely(rerr && !err))
27831 +                       err = rerr;
27832 +       }
27833 +
27834 +       /* will be handled by the caller */
27835 +       if (!au_ftest_opts(opts->flags, REFRESH)
27836 +           && (opts->given_udba
27837 +               || au_opt_test(sbinfo->si_mntflags, XINO)
27838 +               || au_ftest_opts(opts->flags, REFRESH_IDOP)
27839 +                   ))
27840 +               au_fset_opts(opts->flags, REFRESH);
27841 +
27842 +       AuDbg("status 0x%x\n", opts->flags);
27843 +
27844 +out:
27845 +       return err;
27846 +}
27847 +
27848 +/* ---------------------------------------------------------------------- */
27849 +
27850 +unsigned int au_opt_udba(struct super_block *sb)
27851 +{
27852 +       return au_mntflags(sb) & AuOptMask_UDBA;
27853 +}
27854 diff -urN /usr/share/empty/fs/aufs/opts.h linux/fs/aufs/opts.h
27855 --- /usr/share/empty/fs/aufs/opts.h     1970-01-01 01:00:00.000000000 +0100
27856 +++ linux/fs/aufs/opts.h        2022-03-21 14:49:05.729966343 +0100
27857 @@ -0,0 +1,263 @@
27858 +/* SPDX-License-Identifier: GPL-2.0 */
27859 +/*
27860 + * Copyright (C) 2005-2021 Junjiro R. Okajima
27861 + *
27862 + * This program, aufs is free software; you can redistribute it and/or modify
27863 + * it under the terms of the GNU General Public License as published by
27864 + * the Free Software Foundation; either version 2 of the License, or
27865 + * (at your option) any later version.
27866 + *
27867 + * This program is distributed in the hope that it will be useful,
27868 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
27869 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27870 + * GNU General Public License for more details.
27871 + *
27872 + * You should have received a copy of the GNU General Public License
27873 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
27874 + */
27875 +
27876 +/*
27877 + * mount options/flags
27878 + */
27879 +
27880 +#ifndef __AUFS_OPTS_H__
27881 +#define __AUFS_OPTS_H__
27882 +
27883 +#ifdef __KERNEL__
27884 +
27885 +#include <linux/fs_parser.h>
27886 +#include <linux/namei.h>
27887 +#include <linux/path.h>
27888 +
27889 +enum {
27890 +       Opt_br,
27891 +       Opt_add, Opt_del, Opt_mod, Opt_append, Opt_prepend,
27892 +       Opt_idel, Opt_imod,
27893 +       Opt_dirwh, Opt_rdcache, Opt_rdblk, Opt_rdhash,
27894 +       Opt_xino, Opt_noxino,
27895 +       Opt_trunc_xino, Opt_trunc_xino_v,
27896 +       Opt_trunc_xino_path, Opt_itrunc_xino,
27897 +       Opt_trunc_xib,
27898 +       Opt_shwh,
27899 +       Opt_plink, Opt_list_plink,
27900 +       Opt_udba,
27901 +       Opt_dio,
27902 +       Opt_diropq, Opt_diropq_a, Opt_diropq_w,
27903 +       Opt_warn_perm,
27904 +       Opt_wbr_copyup, Opt_wbr_create,
27905 +       Opt_fhsm_sec,
27906 +       Opt_verbose, Opt_noverbose,
27907 +       Opt_sum, Opt_wsum,
27908 +       Opt_dirperm1,
27909 +       Opt_dirren,
27910 +       Opt_acl,
27911 +       Opt_tail, Opt_ignore, Opt_ignore_silent, Opt_err
27912 +};
27913 +
27914 +/* ---------------------------------------------------------------------- */
27915 +
27916 +/* mount flags */
27917 +#define AuOpt_XINO             1               /* external inode number bitmap
27918 +                                                  and translation table */
27919 +#define AuOpt_TRUNC_XINO       (1 << 1)        /* truncate xino files */
27920 +#define AuOpt_UDBA_NONE                (1 << 2)        /* users direct branch access */
27921 +#define AuOpt_UDBA_REVAL       (1 << 3)
27922 +#define AuOpt_UDBA_HNOTIFY     (1 << 4)
27923 +#define AuOpt_SHWH             (1 << 5)        /* show whiteout */
27924 +#define AuOpt_PLINK            (1 << 6)        /* pseudo-link */
27925 +#define AuOpt_DIRPERM1         (1 << 7)        /* ignore the lower dir's perm
27926 +                                                  bits */
27927 +#define AuOpt_ALWAYS_DIROPQ    (1 << 9)        /* policy to creating diropq */
27928 +#define AuOpt_SUM              (1 << 10)       /* summation for statfs(2) */
27929 +#define AuOpt_SUM_W            (1 << 11)       /* unimplemented */
27930 +#define AuOpt_WARN_PERM                (1 << 12)       /* warn when add-branch */
27931 +#define AuOpt_VERBOSE          (1 << 13)       /* print the cause of error */
27932 +#define AuOpt_DIO              (1 << 14)       /* direct io */
27933 +#define AuOpt_DIRREN           (1 << 15)       /* directory rename */
27934 +
27935 +#ifndef CONFIG_AUFS_HNOTIFY
27936 +#undef AuOpt_UDBA_HNOTIFY
27937 +#define AuOpt_UDBA_HNOTIFY     0
27938 +#endif
27939 +#ifndef CONFIG_AUFS_DIRREN
27940 +#undef AuOpt_DIRREN
27941 +#define AuOpt_DIRREN           0
27942 +#endif
27943 +#ifndef CONFIG_AUFS_SHWH
27944 +#undef AuOpt_SHWH
27945 +#define AuOpt_SHWH             0
27946 +#endif
27947 +
27948 +#define AuOpt_Def      (AuOpt_XINO \
27949 +                        | AuOpt_UDBA_REVAL \
27950 +                        | AuOpt_PLINK \
27951 +                        /* | AuOpt_DIRPERM1 */ \
27952 +                        | AuOpt_WARN_PERM)
27953 +#define AuOptMask_UDBA (AuOpt_UDBA_NONE \
27954 +                        | AuOpt_UDBA_REVAL \
27955 +                        | AuOpt_UDBA_HNOTIFY)
27956 +
27957 +#define AuOpt_LkupDirFlags     (LOOKUP_FOLLOW | LOOKUP_DIRECTORY)
27958 +
27959 +#define au_opt_test(flags, name)       (flags & AuOpt_##name)
27960 +#define au_opt_set(flags, name) do { \
27961 +       BUILD_BUG_ON(AuOpt_##name & AuOptMask_UDBA); \
27962 +       ((flags) |= AuOpt_##name); \
27963 +} while (0)
27964 +#define au_opt_set_udba(flags, name) do { \
27965 +       (flags) &= ~AuOptMask_UDBA; \
27966 +       ((flags) |= AuOpt_##name); \
27967 +} while (0)
27968 +#define au_opt_clr(flags, name) do { \
27969 +       ((flags) &= ~AuOpt_##name); \
27970 +} while (0)
27971 +
27972 +static inline unsigned int au_opts_plink(unsigned int mntflags)
27973 +{
27974 +#ifdef CONFIG_PROC_FS
27975 +       return mntflags;
27976 +#else
27977 +       return mntflags & ~AuOpt_PLINK;
27978 +#endif
27979 +}
27980 +
27981 +/* ---------------------------------------------------------------------- */
27982 +
27983 +/* policies to select one among multiple writable branches */
27984 +enum {
27985 +       AuWbrCreate_TDP,        /* top down parent */
27986 +       AuWbrCreate_RR,         /* round robin */
27987 +       AuWbrCreate_MFS,        /* most free space */
27988 +       AuWbrCreate_MFSV,       /* mfs with seconds */
27989 +       AuWbrCreate_MFSRR,      /* mfs then rr */
27990 +       AuWbrCreate_MFSRRV,     /* mfs then rr with seconds */
27991 +       AuWbrCreate_TDMFS,      /* top down regardless parent and mfs */
27992 +       AuWbrCreate_TDMFSV,     /* top down regardless parent and mfs */
27993 +       AuWbrCreate_PMFS,       /* parent and mfs */
27994 +       AuWbrCreate_PMFSV,      /* parent and mfs with seconds */
27995 +       AuWbrCreate_PMFSRR,     /* parent, mfs and round-robin */
27996 +       AuWbrCreate_PMFSRRV,    /* plus seconds */
27997 +
27998 +       AuWbrCreate_Def = AuWbrCreate_TDP
27999 +};
28000 +
28001 +enum {
28002 +       AuWbrCopyup_TDP,        /* top down parent */
28003 +       AuWbrCopyup_BUP,        /* bottom up parent */
28004 +       AuWbrCopyup_BU,         /* bottom up */
28005 +
28006 +       AuWbrCopyup_Def = AuWbrCopyup_TDP
28007 +};
28008 +
28009 +/* ---------------------------------------------------------------------- */
28010 +
28011 +struct file;
28012 +
28013 +struct au_opt_add {
28014 +       aufs_bindex_t   bindex;
28015 +       char            *pathname;
28016 +       int             perm;
28017 +       struct path     path;
28018 +};
28019 +
28020 +struct au_opt_del {
28021 +       char            *pathname;
28022 +       struct path     h_path;
28023 +};
28024 +
28025 +struct au_opt_mod {
28026 +       char            *path;
28027 +       int             perm;
28028 +       struct dentry   *h_root;
28029 +};
28030 +
28031 +struct au_opt_xino {
28032 +       char            *path;
28033 +       struct file     *file;
28034 +};
28035 +
28036 +struct au_opt_xino_itrunc {
28037 +       aufs_bindex_t   bindex;
28038 +};
28039 +
28040 +struct au_opt_wbr_create {
28041 +       int                     wbr_create;
28042 +       int                     mfs_second;
28043 +       unsigned long long      mfsrr_watermark;
28044 +};
28045 +
28046 +struct au_opt {
28047 +       int type;
28048 +       union {
28049 +               struct au_opt_xino      xino;
28050 +               struct au_opt_xino_itrunc xino_itrunc;
28051 +               struct au_opt_add       add;
28052 +               struct au_opt_del       del;
28053 +               struct au_opt_mod       mod;
28054 +               int                     dirwh;
28055 +               int                     rdcache;
28056 +               unsigned int            rdblk;
28057 +               unsigned int            rdhash;
28058 +               int                     udba;
28059 +               struct au_opt_wbr_create wbr_create;
28060 +               int                     wbr_copyup;
28061 +               unsigned int            fhsm_second;
28062 +               bool                    tf; /* generic flag, true or false */
28063 +       };
28064 +};
28065 +
28066 +/* opts flags */
28067 +#define AuOpts_REMOUNT         1
28068 +#define AuOpts_REFRESH         (1 << 1)
28069 +#define AuOpts_TRUNC_XIB       (1 << 2)
28070 +#define AuOpts_REFRESH_DYAOP   (1 << 3)
28071 +#define AuOpts_REFRESH_IDOP    (1 << 4)
28072 +#define AuOpts_DR_FLUSHED      (1 << 5)
28073 +#define au_ftest_opts(flags, name)     ((flags) & AuOpts_##name)
28074 +#define au_fset_opts(flags, name) \
28075 +       do { (flags) |= AuOpts_##name; } while (0)
28076 +#define au_fclr_opts(flags, name) \
28077 +       do { (flags) &= ~AuOpts_##name; } while (0)
28078 +
28079 +#ifndef CONFIG_AUFS_DIRREN
28080 +#undef AuOpts_DR_FLUSHED
28081 +#define AuOpts_DR_FLUSHED      0
28082 +#endif
28083 +
28084 +struct au_opts {
28085 +       struct au_opt   *opt;
28086 +       int             max_opt;
28087 +
28088 +       unsigned int    given_udba;
28089 +       unsigned int    flags;
28090 +       unsigned long   sb_flags;
28091 +};
28092 +
28093 +/* ---------------------------------------------------------------------- */
28094 +
28095 +/* opts.c */
28096 +int au_br_perm_val(char *perm);
28097 +void au_optstr_br_perm(au_br_perm_str_t *str, int perm);
28098 +int au_udba_val(char *str);
28099 +const char *au_optstr_udba(int udba);
28100 +int au_wbr_create_val(char *str, struct au_opt_wbr_create *create);
28101 +const char *au_optstr_wbr_create(int wbr_create);
28102 +int au_wbr_copyup_val(char *str);
28103 +const char *au_optstr_wbr_copyup(int wbr_copyup);
28104 +
28105 +int au_opt_add(struct au_opt *opt, char *opt_str, unsigned long sb_flags,
28106 +              aufs_bindex_t bindex);
28107 +struct super_block;
28108 +int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
28109 +                  unsigned int pending);
28110 +int au_opts_mount(struct super_block *sb, struct au_opts *opts);
28111 +int au_opts_remount(struct super_block *sb, struct au_opts *opts);
28112 +
28113 +unsigned int au_opt_udba(struct super_block *sb);
28114 +
28115 +/* fsctx.c */
28116 +int aufs_fsctx_init(struct fs_context *fc);
28117 +extern const struct fs_parameter_spec aufs_fsctx_paramspec[];
28118 +
28119 +#endif /* __KERNEL__ */
28120 +#endif /* __AUFS_OPTS_H__ */
28121 diff -urN /usr/share/empty/fs/aufs/plink.c linux/fs/aufs/plink.c
28122 --- /usr/share/empty/fs/aufs/plink.c    1970-01-01 01:00:00.000000000 +0100
28123 +++ linux/fs/aufs/plink.c       2022-03-21 14:49:05.729966343 +0100
28124 @@ -0,0 +1,516 @@
28125 +// SPDX-License-Identifier: GPL-2.0
28126 +/*
28127 + * Copyright (C) 2005-2021 Junjiro R. Okajima
28128 + *
28129 + * This program, aufs is free software; you can redistribute it and/or modify
28130 + * it under the terms of the GNU General Public License as published by
28131 + * the Free Software Foundation; either version 2 of the License, or
28132 + * (at your option) any later version.
28133 + *
28134 + * This program is distributed in the hope that it will be useful,
28135 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28136 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28137 + * GNU General Public License for more details.
28138 + *
28139 + * You should have received a copy of the GNU General Public License
28140 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28141 + */
28142 +
28143 +/*
28144 + * pseudo-link
28145 + */
28146 +
28147 +#include "aufs.h"
28148 +
28149 +/*
28150 + * the pseudo-link maintenance mode.
28151 + * during a user process maintains the pseudo-links,
28152 + * prohibit adding a new plink and branch manipulation.
28153 + *
28154 + * Flags
28155 + * NOPLM:
28156 + *     For entry functions which will handle plink, and i_mutex is already held
28157 + *     in VFS.
28158 + *     They cannot wait and should return an error at once.
28159 + *     Callers has to check the error.
28160 + * NOPLMW:
28161 + *     For entry functions which will handle plink, but i_mutex is not held
28162 + *     in VFS.
28163 + *     They can wait the plink maintenance mode to finish.
28164 + *
28165 + * They behave like F_SETLK and F_SETLKW.
28166 + * If the caller never handle plink, then both flags are unnecessary.
28167 + */
28168 +
28169 +int au_plink_maint(struct super_block *sb, int flags)
28170 +{
28171 +       int err;
28172 +       pid_t pid, ppid;
28173 +       struct task_struct *parent, *prev;
28174 +       struct au_sbinfo *sbi;
28175 +
28176 +       SiMustAnyLock(sb);
28177 +
28178 +       err = 0;
28179 +       if (!au_opt_test(au_mntflags(sb), PLINK))
28180 +               goto out;
28181 +
28182 +       sbi = au_sbi(sb);
28183 +       pid = sbi->si_plink_maint_pid;
28184 +       if (!pid || pid == current->pid)
28185 +               goto out;
28186 +
28187 +       /* todo: it highly depends upon /sbin/mount.aufs */
28188 +       prev = NULL;
28189 +       parent = current;
28190 +       ppid = 0;
28191 +       rcu_read_lock();
28192 +       while (1) {
28193 +               parent = rcu_dereference(parent->real_parent);
28194 +               if (parent == prev)
28195 +                       break;
28196 +               ppid = task_pid_vnr(parent);
28197 +               if (pid == ppid) {
28198 +                       rcu_read_unlock();
28199 +                       goto out;
28200 +               }
28201 +               prev = parent;
28202 +       }
28203 +       rcu_read_unlock();
28204 +
28205 +       if (au_ftest_lock(flags, NOPLMW)) {
28206 +               /* if there is no i_mutex lock in VFS, we don't need to wait */
28207 +               /* AuDebugOn(!lockdep_depth(current)); */
28208 +               while (sbi->si_plink_maint_pid) {
28209 +                       si_read_unlock(sb);
28210 +                       /* gave up wake_up_bit() */
28211 +                       wait_event(sbi->si_plink_wq, !sbi->si_plink_maint_pid);
28212 +
28213 +                       if (au_ftest_lock(flags, FLUSH))
28214 +                               au_nwt_flush(&sbi->si_nowait);
28215 +                       si_noflush_read_lock(sb);
28216 +               }
28217 +       } else if (au_ftest_lock(flags, NOPLM)) {
28218 +               AuDbg("ppid %d, pid %d\n", ppid, pid);
28219 +               err = -EAGAIN;
28220 +       }
28221 +
28222 +out:
28223 +       return err;
28224 +}
28225 +
28226 +void au_plink_maint_leave(struct au_sbinfo *sbinfo)
28227 +{
28228 +       spin_lock(&sbinfo->si_plink_maint_lock);
28229 +       sbinfo->si_plink_maint_pid = 0;
28230 +       spin_unlock(&sbinfo->si_plink_maint_lock);
28231 +       wake_up_all(&sbinfo->si_plink_wq);
28232 +}
28233 +
28234 +int au_plink_maint_enter(struct super_block *sb)
28235 +{
28236 +       int err;
28237 +       struct au_sbinfo *sbinfo;
28238 +
28239 +       err = 0;
28240 +       sbinfo = au_sbi(sb);
28241 +       /* make sure i am the only one in this fs */
28242 +       si_write_lock(sb, AuLock_FLUSH);
28243 +       if (au_opt_test(au_mntflags(sb), PLINK)) {
28244 +               spin_lock(&sbinfo->si_plink_maint_lock);
28245 +               if (!sbinfo->si_plink_maint_pid)
28246 +                       sbinfo->si_plink_maint_pid = current->pid;
28247 +               else
28248 +                       err = -EBUSY;
28249 +               spin_unlock(&sbinfo->si_plink_maint_lock);
28250 +       }
28251 +       si_write_unlock(sb);
28252 +
28253 +       return err;
28254 +}
28255 +
28256 +/* ---------------------------------------------------------------------- */
28257 +
28258 +#ifdef CONFIG_AUFS_DEBUG
28259 +void au_plink_list(struct super_block *sb)
28260 +{
28261 +       int i;
28262 +       struct au_sbinfo *sbinfo;
28263 +       struct hlist_bl_head *hbl;
28264 +       struct hlist_bl_node *pos;
28265 +       struct au_icntnr *icntnr;
28266 +
28267 +       SiMustAnyLock(sb);
28268 +
28269 +       sbinfo = au_sbi(sb);
28270 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
28271 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
28272 +
28273 +       for (i = 0; i < AuPlink_NHASH; i++) {
28274 +               hbl = sbinfo->si_plink + i;
28275 +               hlist_bl_lock(hbl);
28276 +               hlist_bl_for_each_entry(icntnr, pos, hbl, plink)
28277 +                       AuDbg("%lu\n", icntnr->vfs_inode.i_ino);
28278 +               hlist_bl_unlock(hbl);
28279 +       }
28280 +}
28281 +#endif
28282 +
28283 +/* is the inode pseudo-linked? */
28284 +int au_plink_test(struct inode *inode)
28285 +{
28286 +       int found, i;
28287 +       struct au_sbinfo *sbinfo;
28288 +       struct hlist_bl_head *hbl;
28289 +       struct hlist_bl_node *pos;
28290 +       struct au_icntnr *icntnr;
28291 +
28292 +       sbinfo = au_sbi(inode->i_sb);
28293 +       AuRwMustAnyLock(&sbinfo->si_rwsem);
28294 +       AuDebugOn(!au_opt_test(au_mntflags(inode->i_sb), PLINK));
28295 +       AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
28296 +
28297 +       found = 0;
28298 +       i = au_plink_hash(inode->i_ino);
28299 +       hbl =  sbinfo->si_plink + i;
28300 +       hlist_bl_lock(hbl);
28301 +       hlist_bl_for_each_entry(icntnr, pos, hbl, plink)
28302 +               if (&icntnr->vfs_inode == inode) {
28303 +                       found = 1;
28304 +                       break;
28305 +               }
28306 +       hlist_bl_unlock(hbl);
28307 +       return found;
28308 +}
28309 +
28310 +/* ---------------------------------------------------------------------- */
28311 +
28312 +/*
28313 + * generate a name for plink.
28314 + * the file will be stored under AUFS_WH_PLINKDIR.
28315 + */
28316 +/* 20 is max digits length of ulong 64 */
28317 +#define PLINK_NAME_LEN ((20 + 1) * 2)
28318 +
28319 +static int plink_name(char *name, int len, struct inode *inode,
28320 +                     aufs_bindex_t bindex)
28321 +{
28322 +       int rlen;
28323 +       struct inode *h_inode;
28324 +
28325 +       h_inode = au_h_iptr(inode, bindex);
28326 +       rlen = snprintf(name, len, "%lu.%lu", inode->i_ino, h_inode->i_ino);
28327 +       return rlen;
28328 +}
28329 +
28330 +struct au_do_plink_lkup_args {
28331 +       struct dentry **errp;
28332 +       struct qstr *tgtname;
28333 +       struct path *h_ppath;
28334 +};
28335 +
28336 +static struct dentry *au_do_plink_lkup(struct qstr *tgtname,
28337 +                                      struct path *h_ppath)
28338 +{
28339 +       struct dentry *h_dentry;
28340 +       struct inode *h_inode;
28341 +
28342 +       h_inode = d_inode(h_ppath->dentry);
28343 +       inode_lock_shared_nested(h_inode, AuLsc_I_CHILD2);
28344 +       h_dentry = vfsub_lkup_one(tgtname, h_ppath);
28345 +       inode_unlock_shared(h_inode);
28346 +
28347 +       return h_dentry;
28348 +}
28349 +
28350 +static void au_call_do_plink_lkup(void *args)
28351 +{
28352 +       struct au_do_plink_lkup_args *a = args;
28353 +       *a->errp = au_do_plink_lkup(a->tgtname, a->h_ppath);
28354 +}
28355 +
28356 +/* lookup the plink-ed @inode under the branch at @bindex */
28357 +struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex)
28358 +{
28359 +       struct dentry *h_dentry;
28360 +       struct au_branch *br;
28361 +       struct path h_ppath;
28362 +       int wkq_err;
28363 +       char a[PLINK_NAME_LEN];
28364 +       struct qstr tgtname = QSTR_INIT(a, 0);
28365 +
28366 +       AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
28367 +
28368 +       br = au_sbr(inode->i_sb, bindex);
28369 +       h_ppath.dentry = br->br_wbr->wbr_plink;
28370 +       h_ppath.mnt = au_br_mnt(br);
28371 +       tgtname.len = plink_name(a, sizeof(a), inode, bindex);
28372 +
28373 +       if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
28374 +               struct au_do_plink_lkup_args args = {
28375 +                       .errp           = &h_dentry,
28376 +                       .tgtname        = &tgtname,
28377 +                       .h_ppath        = &h_ppath
28378 +               };
28379 +
28380 +               wkq_err = au_wkq_wait(au_call_do_plink_lkup, &args);
28381 +               if (unlikely(wkq_err))
28382 +                       h_dentry = ERR_PTR(wkq_err);
28383 +       } else
28384 +               h_dentry = au_do_plink_lkup(&tgtname, &h_ppath);
28385 +
28386 +       return h_dentry;
28387 +}
28388 +
28389 +/* create a pseudo-link */
28390 +static int do_whplink(struct qstr *tgt, struct path *h_ppath,
28391 +                     struct dentry *h_dentry)
28392 +{
28393 +       int err;
28394 +       struct path h_path;
28395 +       struct inode *h_dir, *delegated;
28396 +
28397 +       h_dir = d_inode(h_ppath->dentry);
28398 +       inode_lock_nested(h_dir, AuLsc_I_CHILD2);
28399 +       h_path.mnt = h_ppath->mnt;
28400 +again:
28401 +       h_path.dentry = vfsub_lkup_one(tgt, h_ppath);
28402 +       err = PTR_ERR(h_path.dentry);
28403 +       if (IS_ERR(h_path.dentry))
28404 +               goto out;
28405 +
28406 +       err = 0;
28407 +       /* wh.plink dir is not monitored */
28408 +       /* todo: is it really safe? */
28409 +       if (d_is_positive(h_path.dentry)
28410 +           && d_inode(h_path.dentry) != d_inode(h_dentry)) {
28411 +               delegated = NULL;
28412 +               err = vfsub_unlink(h_dir, &h_path, &delegated, /*force*/0);
28413 +               if (unlikely(err == -EWOULDBLOCK)) {
28414 +                       pr_warn("cannot retry for NFSv4 delegation"
28415 +                               " for an internal unlink\n");
28416 +                       iput(delegated);
28417 +               }
28418 +               dput(h_path.dentry);
28419 +               h_path.dentry = NULL;
28420 +               if (!err)
28421 +                       goto again;
28422 +       }
28423 +       if (!err && d_is_negative(h_path.dentry)) {
28424 +               delegated = NULL;
28425 +               err = vfsub_link(h_dentry, h_dir, &h_path, &delegated);
28426 +               if (unlikely(err == -EWOULDBLOCK)) {
28427 +                       pr_warn("cannot retry for NFSv4 delegation"
28428 +                               " for an internal link\n");
28429 +                       iput(delegated);
28430 +               }
28431 +       }
28432 +       dput(h_path.dentry);
28433 +
28434 +out:
28435 +       inode_unlock(h_dir);
28436 +       return err;
28437 +}
28438 +
28439 +struct do_whplink_args {
28440 +       int *errp;
28441 +       struct qstr *tgt;
28442 +       struct path *h_ppath;
28443 +       struct dentry *h_dentry;
28444 +};
28445 +
28446 +static void call_do_whplink(void *args)
28447 +{
28448 +       struct do_whplink_args *a = args;
28449 +       *a->errp = do_whplink(a->tgt, a->h_ppath, a->h_dentry);
28450 +}
28451 +
28452 +static int whplink(struct dentry *h_dentry, struct inode *inode,
28453 +                  aufs_bindex_t bindex)
28454 +{
28455 +       int err, wkq_err;
28456 +       struct au_branch *br;
28457 +       struct au_wbr *wbr;
28458 +       struct path h_ppath;
28459 +       char a[PLINK_NAME_LEN];
28460 +       struct qstr tgtname = QSTR_INIT(a, 0);
28461 +
28462 +       br = au_sbr(inode->i_sb, bindex);
28463 +       wbr = br->br_wbr;
28464 +       h_ppath.dentry = wbr->wbr_plink;
28465 +       h_ppath.mnt = au_br_mnt(br);
28466 +       tgtname.len = plink_name(a, sizeof(a), inode, bindex);
28467 +
28468 +       /* always superio. */
28469 +       if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
28470 +               struct do_whplink_args args = {
28471 +                       .errp           = &err,
28472 +                       .tgt            = &tgtname,
28473 +                       .h_ppath        = &h_ppath,
28474 +                       .h_dentry       = h_dentry
28475 +               };
28476 +               wkq_err = au_wkq_wait(call_do_whplink, &args);
28477 +               if (unlikely(wkq_err))
28478 +                       err = wkq_err;
28479 +       } else
28480 +               err = do_whplink(&tgtname, &h_ppath, h_dentry);
28481 +
28482 +       return err;
28483 +}
28484 +
28485 +/*
28486 + * create a new pseudo-link for @h_dentry on @bindex.
28487 + * the linked inode is held in aufs @inode.
28488 + */
28489 +void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
28490 +                    struct dentry *h_dentry)
28491 +{
28492 +       struct super_block *sb;
28493 +       struct au_sbinfo *sbinfo;
28494 +       struct hlist_bl_head *hbl;
28495 +       struct hlist_bl_node *pos;
28496 +       struct au_icntnr *icntnr;
28497 +       int found, err, cnt, i;
28498 +
28499 +       sb = inode->i_sb;
28500 +       sbinfo = au_sbi(sb);
28501 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
28502 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
28503 +
28504 +       found = au_plink_test(inode);
28505 +       if (found)
28506 +               return;
28507 +
28508 +       i = au_plink_hash(inode->i_ino);
28509 +       hbl = sbinfo->si_plink + i;
28510 +       au_igrab(inode);
28511 +
28512 +       hlist_bl_lock(hbl);
28513 +       hlist_bl_for_each_entry(icntnr, pos, hbl, plink) {
28514 +               if (&icntnr->vfs_inode == inode) {
28515 +                       found = 1;
28516 +                       break;
28517 +               }
28518 +       }
28519 +       if (!found) {
28520 +               icntnr = container_of(inode, struct au_icntnr, vfs_inode);
28521 +               hlist_bl_add_head(&icntnr->plink, hbl);
28522 +       }
28523 +       hlist_bl_unlock(hbl);
28524 +       if (!found) {
28525 +               cnt = au_hbl_count(hbl);
28526 +#define msg "unexpectedly unbalanced or too many pseudo-links"
28527 +               if (cnt > AUFS_PLINK_WARN)
28528 +                       AuWarn1(msg ", %d\n", cnt);
28529 +#undef msg
28530 +               err = whplink(h_dentry, inode, bindex);
28531 +               if (unlikely(err)) {
28532 +                       pr_warn("err %d, damaged pseudo link.\n", err);
28533 +                       au_hbl_del(&icntnr->plink, hbl);
28534 +                       iput(&icntnr->vfs_inode);
28535 +               }
28536 +       } else
28537 +               iput(&icntnr->vfs_inode);
28538 +}
28539 +
28540 +/* free all plinks */
28541 +void au_plink_put(struct super_block *sb, int verbose)
28542 +{
28543 +       int i, warned;
28544 +       struct au_sbinfo *sbinfo;
28545 +       struct hlist_bl_head *hbl;
28546 +       struct hlist_bl_node *pos, *tmp;
28547 +       struct au_icntnr *icntnr;
28548 +
28549 +       SiMustWriteLock(sb);
28550 +
28551 +       sbinfo = au_sbi(sb);
28552 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
28553 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
28554 +
28555 +       /* no spin_lock since sbinfo is write-locked */
28556 +       warned = 0;
28557 +       for (i = 0; i < AuPlink_NHASH; i++) {
28558 +               hbl = sbinfo->si_plink + i;
28559 +               if (!warned && verbose && !hlist_bl_empty(hbl)) {
28560 +                       pr_warn("pseudo-link is not flushed");
28561 +                       warned = 1;
28562 +               }
28563 +               hlist_bl_for_each_entry_safe(icntnr, pos, tmp, hbl, plink)
28564 +                       iput(&icntnr->vfs_inode);
28565 +               INIT_HLIST_BL_HEAD(hbl);
28566 +       }
28567 +}
28568 +
28569 +void au_plink_clean(struct super_block *sb, int verbose)
28570 +{
28571 +       struct dentry *root;
28572 +
28573 +       root = sb->s_root;
28574 +       aufs_write_lock(root);
28575 +       if (au_opt_test(au_mntflags(sb), PLINK))
28576 +               au_plink_put(sb, verbose);
28577 +       aufs_write_unlock(root);
28578 +}
28579 +
28580 +static int au_plink_do_half_refresh(struct inode *inode, aufs_bindex_t br_id)
28581 +{
28582 +       int do_put;
28583 +       aufs_bindex_t btop, bbot, bindex;
28584 +
28585 +       do_put = 0;
28586 +       btop = au_ibtop(inode);
28587 +       bbot = au_ibbot(inode);
28588 +       if (btop >= 0) {
28589 +               for (bindex = btop; bindex <= bbot; bindex++) {
28590 +                       if (!au_h_iptr(inode, bindex)
28591 +                           || au_ii_br_id(inode, bindex) != br_id)
28592 +                               continue;
28593 +                       au_set_h_iptr(inode, bindex, NULL, 0);
28594 +                       do_put = 1;
28595 +                       break;
28596 +               }
28597 +               if (do_put)
28598 +                       for (bindex = btop; bindex <= bbot; bindex++)
28599 +                               if (au_h_iptr(inode, bindex)) {
28600 +                                       do_put = 0;
28601 +                                       break;
28602 +                               }
28603 +       } else
28604 +               do_put = 1;
28605 +
28606 +       return do_put;
28607 +}
28608 +
28609 +/* free the plinks on a branch specified by @br_id */
28610 +void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id)
28611 +{
28612 +       struct au_sbinfo *sbinfo;
28613 +       struct hlist_bl_head *hbl;
28614 +       struct hlist_bl_node *pos, *tmp;
28615 +       struct au_icntnr *icntnr;
28616 +       struct inode *inode;
28617 +       int i, do_put;
28618 +
28619 +       SiMustWriteLock(sb);
28620 +
28621 +       sbinfo = au_sbi(sb);
28622 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
28623 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
28624 +
28625 +       /* no bit_lock since sbinfo is write-locked */
28626 +       for (i = 0; i < AuPlink_NHASH; i++) {
28627 +               hbl = sbinfo->si_plink + i;
28628 +               hlist_bl_for_each_entry_safe(icntnr, pos, tmp, hbl, plink) {
28629 +                       inode = au_igrab(&icntnr->vfs_inode);
28630 +                       ii_write_lock_child(inode);
28631 +                       do_put = au_plink_do_half_refresh(inode, br_id);
28632 +                       if (do_put) {
28633 +                               hlist_bl_del(&icntnr->plink);
28634 +                               iput(inode);
28635 +                       }
28636 +                       ii_write_unlock(inode);
28637 +                       iput(inode);
28638 +               }
28639 +       }
28640 +}
28641 diff -urN /usr/share/empty/fs/aufs/poll.c linux/fs/aufs/poll.c
28642 --- /usr/share/empty/fs/aufs/poll.c     1970-01-01 01:00:00.000000000 +0100
28643 +++ linux/fs/aufs/poll.c        2022-03-21 14:49:05.729966343 +0100
28644 @@ -0,0 +1,51 @@
28645 +// SPDX-License-Identifier: GPL-2.0
28646 +/*
28647 + * Copyright (C) 2005-2021 Junjiro R. Okajima
28648 + *
28649 + * This program, aufs is free software; you can redistribute it and/or modify
28650 + * it under the terms of the GNU General Public License as published by
28651 + * the Free Software Foundation; either version 2 of the License, or
28652 + * (at your option) any later version.
28653 + *
28654 + * This program is distributed in the hope that it will be useful,
28655 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28656 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28657 + * GNU General Public License for more details.
28658 + *
28659 + * You should have received a copy of the GNU General Public License
28660 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28661 + */
28662 +
28663 +/*
28664 + * poll operation
28665 + * There is only one filesystem which implements ->poll operation, currently.
28666 + */
28667 +
28668 +#include "aufs.h"
28669 +
28670 +__poll_t aufs_poll(struct file *file, struct poll_table_struct *pt)
28671 +{
28672 +       __poll_t mask;
28673 +       struct file *h_file;
28674 +       struct super_block *sb;
28675 +
28676 +       /* We should pretend an error happened. */
28677 +       mask = EPOLLERR /* | EPOLLIN | EPOLLOUT */;
28678 +       sb = file->f_path.dentry->d_sb;
28679 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
28680 +
28681 +       h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
28682 +       if (IS_ERR(h_file)) {
28683 +               AuDbg("h_file %ld\n", PTR_ERR(h_file));
28684 +               goto out;
28685 +       }
28686 +
28687 +       mask = vfs_poll(h_file, pt);
28688 +       fput(h_file); /* instead of au_read_post() */
28689 +
28690 +out:
28691 +       si_read_unlock(sb);
28692 +       if (mask & EPOLLERR)
28693 +               AuDbg("mask 0x%x\n", mask);
28694 +       return mask;
28695 +}
28696 diff -urN /usr/share/empty/fs/aufs/posix_acl.c linux/fs/aufs/posix_acl.c
28697 --- /usr/share/empty/fs/aufs/posix_acl.c        1970-01-01 01:00:00.000000000 +0100
28698 +++ linux/fs/aufs/posix_acl.c   2022-03-21 14:49:05.729966343 +0100
28699 @@ -0,0 +1,111 @@
28700 +// SPDX-License-Identifier: GPL-2.0
28701 +/*
28702 + * Copyright (C) 2014-2021 Junjiro R. Okajima
28703 + *
28704 + * This program, aufs is free software; you can redistribute it and/or modify
28705 + * it under the terms of the GNU General Public License as published by
28706 + * the Free Software Foundation; either version 2 of the License, or
28707 + * (at your option) any later version.
28708 + *
28709 + * This program is distributed in the hope that it will be useful,
28710 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28711 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28712 + * GNU General Public License for more details.
28713 + *
28714 + * You should have received a copy of the GNU General Public License
28715 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28716 + */
28717 +
28718 +/*
28719 + * posix acl operations
28720 + */
28721 +
28722 +#include <linux/fs.h>
28723 +#include "aufs.h"
28724 +
28725 +struct posix_acl *aufs_get_acl(struct inode *inode, int type, bool rcu)
28726 +{
28727 +       struct posix_acl *acl;
28728 +       int err;
28729 +       aufs_bindex_t bindex;
28730 +       struct inode *h_inode;
28731 +       struct super_block *sb;
28732 +
28733 +       acl = ERR_PTR(-ECHILD);
28734 +       if (rcu)
28735 +               goto out;
28736 +
28737 +       acl = NULL;
28738 +       sb = inode->i_sb;
28739 +       si_read_lock(sb, AuLock_FLUSH);
28740 +       ii_read_lock_child(inode);
28741 +       if (!(sb->s_flags & SB_POSIXACL))
28742 +               goto unlock;
28743 +
28744 +       bindex = au_ibtop(inode);
28745 +       h_inode = au_h_iptr(inode, bindex);
28746 +       if (unlikely(!h_inode
28747 +                    || ((h_inode->i_mode & S_IFMT)
28748 +                        != (inode->i_mode & S_IFMT)))) {
28749 +               err = au_busy_or_stale();
28750 +               acl = ERR_PTR(err);
28751 +               goto unlock;
28752 +       }
28753 +
28754 +       /* always topmost only */
28755 +       acl = get_acl(h_inode, type);
28756 +       if (IS_ERR(acl))
28757 +               forget_cached_acl(inode, type);
28758 +       else
28759 +               set_cached_acl(inode, type, acl);
28760 +
28761 +unlock:
28762 +       ii_read_unlock(inode);
28763 +       si_read_unlock(sb);
28764 +
28765 +out:
28766 +       AuTraceErrPtr(acl);
28767 +       return acl;
28768 +}
28769 +
28770 +int aufs_set_acl(struct user_namespace *userns, struct inode *inode,
28771 +                struct posix_acl *acl, int type)
28772 +{
28773 +       int err;
28774 +       ssize_t ssz;
28775 +       struct dentry *dentry;
28776 +       struct au_sxattr arg = {
28777 +               .type = AU_ACL_SET,
28778 +               .u.acl_set = {
28779 +                       .acl    = acl,
28780 +                       .type   = type
28781 +               },
28782 +       };
28783 +
28784 +       IMustLock(inode);
28785 +
28786 +       if (inode->i_ino == AUFS_ROOT_INO)
28787 +               dentry = dget(inode->i_sb->s_root);
28788 +       else {
28789 +               dentry = d_find_alias(inode);
28790 +               if (!dentry)
28791 +                       dentry = d_find_any_alias(inode);
28792 +               if (!dentry) {
28793 +                       pr_warn("cannot handle this inode, "
28794 +                               "please report to aufs-users ML\n");
28795 +                       err = -ENOENT;
28796 +                       goto out;
28797 +               }
28798 +       }
28799 +
28800 +       ssz = au_sxattr(dentry, inode, &arg);
28801 +       /* forget even it if succeeds since the branch might set differently */
28802 +       forget_cached_acl(inode, type);
28803 +       dput(dentry);
28804 +       err = ssz;
28805 +       if (ssz >= 0)
28806 +               err = 0;
28807 +
28808 +out:
28809 +       return err;
28810 +}
28811 diff -urN /usr/share/empty/fs/aufs/procfs.c linux/fs/aufs/procfs.c
28812 --- /usr/share/empty/fs/aufs/procfs.c   1970-01-01 01:00:00.000000000 +0100
28813 +++ linux/fs/aufs/procfs.c      2022-03-21 14:49:05.729966343 +0100
28814 @@ -0,0 +1,170 @@
28815 +// SPDX-License-Identifier: GPL-2.0
28816 +/*
28817 + * Copyright (C) 2010-2021 Junjiro R. Okajima
28818 + *
28819 + * This program, aufs is free software; you can redistribute it and/or modify
28820 + * it under the terms of the GNU General Public License as published by
28821 + * the Free Software Foundation; either version 2 of the License, or
28822 + * (at your option) any later version.
28823 + *
28824 + * This program is distributed in the hope that it will be useful,
28825 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28826 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28827 + * GNU General Public License for more details.
28828 + *
28829 + * You should have received a copy of the GNU General Public License
28830 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28831 + */
28832 +
28833 +/*
28834 + * procfs interfaces
28835 + */
28836 +
28837 +#include <linux/proc_fs.h>
28838 +#include "aufs.h"
28839 +
28840 +static int au_procfs_plm_release(struct inode *inode, struct file *file)
28841 +{
28842 +       struct au_sbinfo *sbinfo;
28843 +
28844 +       sbinfo = file->private_data;
28845 +       if (sbinfo) {
28846 +               au_plink_maint_leave(sbinfo);
28847 +               kobject_put(&sbinfo->si_kobj);
28848 +       }
28849 +
28850 +       return 0;
28851 +}
28852 +
28853 +static void au_procfs_plm_write_clean(struct file *file)
28854 +{
28855 +       struct au_sbinfo *sbinfo;
28856 +
28857 +       sbinfo = file->private_data;
28858 +       if (sbinfo)
28859 +               au_plink_clean(sbinfo->si_sb, /*verbose*/0);
28860 +}
28861 +
28862 +static int au_procfs_plm_write_si(struct file *file, unsigned long id)
28863 +{
28864 +       int err;
28865 +       struct super_block *sb;
28866 +       struct au_sbinfo *sbinfo;
28867 +       struct hlist_bl_node *pos;
28868 +
28869 +       err = -EBUSY;
28870 +       if (unlikely(file->private_data))
28871 +               goto out;
28872 +
28873 +       sb = NULL;
28874 +       /* don't use au_sbilist_lock() here */
28875 +       hlist_bl_lock(&au_sbilist);
28876 +       hlist_bl_for_each_entry(sbinfo, pos, &au_sbilist, si_list)
28877 +               if (id == sysaufs_si_id(sbinfo)) {
28878 +                       if (kobject_get_unless_zero(&sbinfo->si_kobj))
28879 +                               sb = sbinfo->si_sb;
28880 +                       break;
28881 +               }
28882 +       hlist_bl_unlock(&au_sbilist);
28883 +
28884 +       err = -EINVAL;
28885 +       if (unlikely(!sb))
28886 +               goto out;
28887 +
28888 +       err = au_plink_maint_enter(sb);
28889 +       if (!err)
28890 +               /* keep kobject_get() */
28891 +               file->private_data = sbinfo;
28892 +       else
28893 +               kobject_put(&sbinfo->si_kobj);
28894 +out:
28895 +       return err;
28896 +}
28897 +
28898 +/*
28899 + * Accept a valid "si=xxxx" only.
28900 + * Once it is accepted successfully, accept "clean" too.
28901 + */
28902 +static ssize_t au_procfs_plm_write(struct file *file, const char __user *ubuf,
28903 +                                  size_t count, loff_t *ppos)
28904 +{
28905 +       ssize_t err;
28906 +       unsigned long id;
28907 +       /* last newline is allowed */
28908 +       char buf[3 + sizeof(unsigned long) * 2 + 1];
28909 +
28910 +       err = -EACCES;
28911 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
28912 +               goto out;
28913 +
28914 +       err = -EINVAL;
28915 +       if (unlikely(count > sizeof(buf)))
28916 +               goto out;
28917 +
28918 +       err = copy_from_user(buf, ubuf, count);
28919 +       if (unlikely(err)) {
28920 +               err = -EFAULT;
28921 +               goto out;
28922 +       }
28923 +       buf[count] = 0;
28924 +
28925 +       err = -EINVAL;
28926 +       if (!strcmp("clean", buf)) {
28927 +               au_procfs_plm_write_clean(file);
28928 +               goto out_success;
28929 +       } else if (unlikely(strncmp("si=", buf, 3)))
28930 +               goto out;
28931 +
28932 +       err = kstrtoul(buf + 3, 16, &id);
28933 +       if (unlikely(err))
28934 +               goto out;
28935 +
28936 +       err = au_procfs_plm_write_si(file, id);
28937 +       if (unlikely(err))
28938 +               goto out;
28939 +
28940 +out_success:
28941 +       err = count; /* success */
28942 +out:
28943 +       return err;
28944 +}
28945 +
28946 +static const struct proc_ops au_procfs_plm_op = {
28947 +       .proc_write     = au_procfs_plm_write,
28948 +       .proc_release   = au_procfs_plm_release
28949 +};
28950 +
28951 +/* ---------------------------------------------------------------------- */
28952 +
28953 +static struct proc_dir_entry *au_procfs_dir;
28954 +
28955 +void au_procfs_fin(void)
28956 +{
28957 +       remove_proc_entry(AUFS_PLINK_MAINT_NAME, au_procfs_dir);
28958 +       remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
28959 +}
28960 +
28961 +int __init au_procfs_init(void)
28962 +{
28963 +       int err;
28964 +       struct proc_dir_entry *entry;
28965 +
28966 +       err = -ENOMEM;
28967 +       au_procfs_dir = proc_mkdir(AUFS_PLINK_MAINT_DIR, NULL);
28968 +       if (unlikely(!au_procfs_dir))
28969 +               goto out;
28970 +
28971 +       entry = proc_create(AUFS_PLINK_MAINT_NAME, S_IFREG | 0200,
28972 +                           au_procfs_dir, &au_procfs_plm_op);
28973 +       if (unlikely(!entry))
28974 +               goto out_dir;
28975 +
28976 +       err = 0;
28977 +       goto out; /* success */
28978 +
28979 +
28980 +out_dir:
28981 +       remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
28982 +out:
28983 +       return err;
28984 +}
28985 diff -urN /usr/share/empty/fs/aufs/rdu.c linux/fs/aufs/rdu.c
28986 --- /usr/share/empty/fs/aufs/rdu.c      1970-01-01 01:00:00.000000000 +0100
28987 +++ linux/fs/aufs/rdu.c 2022-03-21 14:49:05.729966343 +0100
28988 @@ -0,0 +1,384 @@
28989 +// SPDX-License-Identifier: GPL-2.0
28990 +/*
28991 + * Copyright (C) 2005-2021 Junjiro R. Okajima
28992 + *
28993 + * This program, aufs is free software; you can redistribute it and/or modify
28994 + * it under the terms of the GNU General Public License as published by
28995 + * the Free Software Foundation; either version 2 of the License, or
28996 + * (at your option) any later version.
28997 + *
28998 + * This program is distributed in the hope that it will be useful,
28999 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
29000 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29001 + * GNU General Public License for more details.
29002 + *
29003 + * You should have received a copy of the GNU General Public License
29004 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
29005 + */
29006 +
29007 +/*
29008 + * readdir in userspace.
29009 + */
29010 +
29011 +#include <linux/compat.h>
29012 +#include <linux/fs_stack.h>
29013 +#include <linux/security.h>
29014 +#include "aufs.h"
29015 +
29016 +/* bits for struct aufs_rdu.flags */
29017 +#define        AuRdu_CALLED    1
29018 +#define        AuRdu_CONT      (1 << 1)
29019 +#define        AuRdu_FULL      (1 << 2)
29020 +#define au_ftest_rdu(flags, name)      ((flags) & AuRdu_##name)
29021 +#define au_fset_rdu(flags, name) \
29022 +       do { (flags) |= AuRdu_##name; } while (0)
29023 +#define au_fclr_rdu(flags, name) \
29024 +       do { (flags) &= ~AuRdu_##name; } while (0)
29025 +
29026 +struct au_rdu_arg {
29027 +       struct dir_context              ctx;
29028 +       struct aufs_rdu                 *rdu;
29029 +       union au_rdu_ent_ul             ent;
29030 +       unsigned long                   end;
29031 +
29032 +       struct super_block              *sb;
29033 +       int                             err;
29034 +};
29035 +
29036 +static int au_rdu_fill(struct dir_context *ctx, const char *name, int nlen,
29037 +                      loff_t offset, u64 h_ino, unsigned int d_type)
29038 +{
29039 +       int err, len;
29040 +       struct au_rdu_arg *arg = container_of(ctx, struct au_rdu_arg, ctx);
29041 +       struct aufs_rdu *rdu = arg->rdu;
29042 +       struct au_rdu_ent ent;
29043 +
29044 +       err = 0;
29045 +       arg->err = 0;
29046 +       au_fset_rdu(rdu->cookie.flags, CALLED);
29047 +       len = au_rdu_len(nlen);
29048 +       if (arg->ent.ul + len  < arg->end) {
29049 +               ent.ino = h_ino;
29050 +               ent.bindex = rdu->cookie.bindex;
29051 +               ent.type = d_type;
29052 +               ent.nlen = nlen;
29053 +               if (unlikely(nlen > AUFS_MAX_NAMELEN))
29054 +                       ent.type = DT_UNKNOWN;
29055 +
29056 +               /* unnecessary to support mmap_sem since this is a dir */
29057 +               err = -EFAULT;
29058 +               if (copy_to_user(arg->ent.e, &ent, sizeof(ent)))
29059 +                       goto out;
29060 +               if (copy_to_user(arg->ent.e->name, name, nlen))
29061 +                       goto out;
29062 +               /* the terminating NULL */
29063 +               if (__put_user(0, arg->ent.e->name + nlen))
29064 +                       goto out;
29065 +               err = 0;
29066 +               /* AuDbg("%p, %.*s\n", arg->ent.p, nlen, name); */
29067 +               arg->ent.ul += len;
29068 +               rdu->rent++;
29069 +       } else {
29070 +               err = -EFAULT;
29071 +               au_fset_rdu(rdu->cookie.flags, FULL);
29072 +               rdu->full = 1;
29073 +               rdu->tail = arg->ent;
29074 +       }
29075 +
29076 +out:
29077 +       /* AuTraceErr(err); */
29078 +       return err;
29079 +}
29080 +
29081 +static int au_rdu_do(struct file *h_file, struct au_rdu_arg *arg)
29082 +{
29083 +       int err;
29084 +       loff_t offset;
29085 +       struct au_rdu_cookie *cookie = &arg->rdu->cookie;
29086 +
29087 +       /* we don't have to care (FMODE_32BITHASH | FMODE_64BITHASH) for ext4 */
29088 +       offset = vfsub_llseek(h_file, cookie->h_pos, SEEK_SET);
29089 +       err = offset;
29090 +       if (unlikely(offset != cookie->h_pos))
29091 +               goto out;
29092 +
29093 +       err = 0;
29094 +       do {
29095 +               arg->err = 0;
29096 +               au_fclr_rdu(cookie->flags, CALLED);
29097 +               /* smp_mb(); */
29098 +               err = vfsub_iterate_dir(h_file, &arg->ctx);
29099 +               if (err >= 0)
29100 +                       err = arg->err;
29101 +       } while (!err
29102 +                && au_ftest_rdu(cookie->flags, CALLED)
29103 +                && !au_ftest_rdu(cookie->flags, FULL));
29104 +       cookie->h_pos = h_file->f_pos;
29105 +
29106 +out:
29107 +       AuTraceErr(err);
29108 +       return err;
29109 +}
29110 +
29111 +static int au_rdu(struct file *file, struct aufs_rdu *rdu)
29112 +{
29113 +       int err;
29114 +       aufs_bindex_t bbot;
29115 +       struct au_rdu_arg arg = {
29116 +               .ctx = {
29117 +                       .actor = au_rdu_fill
29118 +               }
29119 +       };
29120 +       struct dentry *dentry;
29121 +       struct inode *inode;
29122 +       struct file *h_file;
29123 +       struct au_rdu_cookie *cookie = &rdu->cookie;
29124 +
29125 +       /* VERIFY_WRITE */
29126 +       err = !access_ok(rdu->ent.e, rdu->sz);
29127 +       if (unlikely(err)) {
29128 +               err = -EFAULT;
29129 +               AuTraceErr(err);
29130 +               goto out;
29131 +       }
29132 +       rdu->rent = 0;
29133 +       rdu->tail = rdu->ent;
29134 +       rdu->full = 0;
29135 +       arg.rdu = rdu;
29136 +       arg.ent = rdu->ent;
29137 +       arg.end = arg.ent.ul;
29138 +       arg.end += rdu->sz;
29139 +
29140 +       err = -ENOTDIR;
29141 +       if (unlikely(!file->f_op->iterate && !file->f_op->iterate_shared))
29142 +               goto out;
29143 +
29144 +       err = security_file_permission(file, MAY_READ);
29145 +       AuTraceErr(err);
29146 +       if (unlikely(err))
29147 +               goto out;
29148 +
29149 +       dentry = file->f_path.dentry;
29150 +       inode = d_inode(dentry);
29151 +       inode_lock_shared(inode);
29152 +
29153 +       arg.sb = inode->i_sb;
29154 +       err = si_read_lock(arg.sb, AuLock_FLUSH | AuLock_NOPLM);
29155 +       if (unlikely(err))
29156 +               goto out_mtx;
29157 +       err = au_alive_dir(dentry);
29158 +       if (unlikely(err))
29159 +               goto out_si;
29160 +       /* todo: reval? */
29161 +       fi_read_lock(file);
29162 +
29163 +       err = -EAGAIN;
29164 +       if (unlikely(au_ftest_rdu(cookie->flags, CONT)
29165 +                    && cookie->generation != au_figen(file)))
29166 +               goto out_unlock;
29167 +
29168 +       err = 0;
29169 +       if (!rdu->blk) {
29170 +               rdu->blk = au_sbi(arg.sb)->si_rdblk;
29171 +               if (!rdu->blk)
29172 +                       rdu->blk = au_dir_size(file, /*dentry*/NULL);
29173 +       }
29174 +       bbot = au_fbtop(file);
29175 +       if (cookie->bindex < bbot)
29176 +               cookie->bindex = bbot;
29177 +       bbot = au_fbbot_dir(file);
29178 +       /* AuDbg("b%d, b%d\n", cookie->bindex, bbot); */
29179 +       for (; !err && cookie->bindex <= bbot;
29180 +            cookie->bindex++, cookie->h_pos = 0) {
29181 +               h_file = au_hf_dir(file, cookie->bindex);
29182 +               if (!h_file)
29183 +                       continue;
29184 +
29185 +               au_fclr_rdu(cookie->flags, FULL);
29186 +               err = au_rdu_do(h_file, &arg);
29187 +               AuTraceErr(err);
29188 +               if (unlikely(au_ftest_rdu(cookie->flags, FULL) || err))
29189 +                       break;
29190 +       }
29191 +       AuDbg("rent %llu\n", rdu->rent);
29192 +
29193 +       if (!err && !au_ftest_rdu(cookie->flags, CONT)) {
29194 +               rdu->shwh = !!au_opt_test(au_sbi(arg.sb)->si_mntflags, SHWH);
29195 +               au_fset_rdu(cookie->flags, CONT);
29196 +               cookie->generation = au_figen(file);
29197 +       }
29198 +
29199 +       ii_read_lock_child(inode);
29200 +       fsstack_copy_attr_atime(inode, au_h_iptr(inode, au_ibtop(inode)));
29201 +       ii_read_unlock(inode);
29202 +
29203 +out_unlock:
29204 +       fi_read_unlock(file);
29205 +out_si:
29206 +       si_read_unlock(arg.sb);
29207 +out_mtx:
29208 +       inode_unlock_shared(inode);
29209 +out:
29210 +       AuTraceErr(err);
29211 +       return err;
29212 +}
29213 +
29214 +static int au_rdu_ino(struct file *file, struct aufs_rdu *rdu)
29215 +{
29216 +       int err;
29217 +       ino_t ino;
29218 +       unsigned long long nent;
29219 +       union au_rdu_ent_ul *u;
29220 +       struct au_rdu_ent ent;
29221 +       struct super_block *sb;
29222 +
29223 +       err = 0;
29224 +       nent = rdu->nent;
29225 +       u = &rdu->ent;
29226 +       sb = file->f_path.dentry->d_sb;
29227 +       si_read_lock(sb, AuLock_FLUSH);
29228 +       while (nent-- > 0) {
29229 +               /* unnecessary to support mmap_sem since this is a dir */
29230 +               err = copy_from_user(&ent, u->e, sizeof(ent));
29231 +               if (!err)
29232 +                       /* VERIFY_WRITE */
29233 +                       err = !access_ok(&u->e->ino, sizeof(ino));
29234 +               if (unlikely(err)) {
29235 +                       err = -EFAULT;
29236 +                       AuTraceErr(err);
29237 +                       break;
29238 +               }
29239 +
29240 +               /* AuDbg("b%d, i%llu\n", ent.bindex, ent.ino); */
29241 +               if (!ent.wh)
29242 +                       err = au_ino(sb, ent.bindex, ent.ino, ent.type, &ino);
29243 +               else
29244 +                       err = au_wh_ino(sb, ent.bindex, ent.ino, ent.type,
29245 +                                       &ino);
29246 +               if (unlikely(err)) {
29247 +                       AuTraceErr(err);
29248 +                       break;
29249 +               }
29250 +
29251 +               err = __put_user(ino, &u->e->ino);
29252 +               if (unlikely(err)) {
29253 +                       err = -EFAULT;
29254 +                       AuTraceErr(err);
29255 +                       break;
29256 +               }
29257 +               u->ul += au_rdu_len(ent.nlen);
29258 +       }
29259 +       si_read_unlock(sb);
29260 +
29261 +       return err;
29262 +}
29263 +
29264 +/* ---------------------------------------------------------------------- */
29265 +
29266 +static int au_rdu_verify(struct aufs_rdu *rdu)
29267 +{
29268 +       AuDbg("rdu{%llu, %p, %u | %u | %llu, %u, %u | "
29269 +             "%llu, b%d, 0x%x, g%u}\n",
29270 +             rdu->sz, rdu->ent.e, rdu->verify[AufsCtlRduV_SZ],
29271 +             rdu->blk,
29272 +             rdu->rent, rdu->shwh, rdu->full,
29273 +             rdu->cookie.h_pos, rdu->cookie.bindex, rdu->cookie.flags,
29274 +             rdu->cookie.generation);
29275 +
29276 +       if (rdu->verify[AufsCtlRduV_SZ] == sizeof(*rdu))
29277 +               return 0;
29278 +
29279 +       AuDbg("%u:%u\n",
29280 +             rdu->verify[AufsCtlRduV_SZ], (unsigned int)sizeof(*rdu));
29281 +       return -EINVAL;
29282 +}
29283 +
29284 +long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
29285 +{
29286 +       long err, e;
29287 +       struct aufs_rdu rdu;
29288 +       void __user *p = (void __user *)arg;
29289 +
29290 +       err = copy_from_user(&rdu, p, sizeof(rdu));
29291 +       if (unlikely(err)) {
29292 +               err = -EFAULT;
29293 +               AuTraceErr(err);
29294 +               goto out;
29295 +       }
29296 +       err = au_rdu_verify(&rdu);
29297 +       if (unlikely(err))
29298 +               goto out;
29299 +
29300 +       switch (cmd) {
29301 +       case AUFS_CTL_RDU:
29302 +               err = au_rdu(file, &rdu);
29303 +               if (unlikely(err))
29304 +                       break;
29305 +
29306 +               e = copy_to_user(p, &rdu, sizeof(rdu));
29307 +               if (unlikely(e)) {
29308 +                       err = -EFAULT;
29309 +                       AuTraceErr(err);
29310 +               }
29311 +               break;
29312 +       case AUFS_CTL_RDU_INO:
29313 +               err = au_rdu_ino(file, &rdu);
29314 +               break;
29315 +
29316 +       default:
29317 +               /* err = -ENOTTY; */
29318 +               err = -EINVAL;
29319 +       }
29320 +
29321 +out:
29322 +       AuTraceErr(err);
29323 +       return err;
29324 +}
29325 +
29326 +#ifdef CONFIG_COMPAT
29327 +long au_rdu_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
29328 +{
29329 +       long err, e;
29330 +       struct aufs_rdu rdu;
29331 +       void __user *p = compat_ptr(arg);
29332 +
29333 +       /* todo: get_user()? */
29334 +       err = copy_from_user(&rdu, p, sizeof(rdu));
29335 +       if (unlikely(err)) {
29336 +               err = -EFAULT;
29337 +               AuTraceErr(err);
29338 +               goto out;
29339 +       }
29340 +       rdu.ent.e = compat_ptr(rdu.ent.ul);
29341 +       err = au_rdu_verify(&rdu);
29342 +       if (unlikely(err))
29343 +               goto out;
29344 +
29345 +       switch (cmd) {
29346 +       case AUFS_CTL_RDU:
29347 +               err = au_rdu(file, &rdu);
29348 +               if (unlikely(err))
29349 +                       break;
29350 +
29351 +               rdu.ent.ul = ptr_to_compat(rdu.ent.e);
29352 +               rdu.tail.ul = ptr_to_compat(rdu.tail.e);
29353 +               e = copy_to_user(p, &rdu, sizeof(rdu));
29354 +               if (unlikely(e)) {
29355 +                       err = -EFAULT;
29356 +                       AuTraceErr(err);
29357 +               }
29358 +               break;
29359 +       case AUFS_CTL_RDU_INO:
29360 +               err = au_rdu_ino(file, &rdu);
29361 +               break;
29362 +
29363 +       default:
29364 +               /* err = -ENOTTY; */
29365 +               err = -EINVAL;
29366 +       }
29367 +
29368 +out:
29369 +       AuTraceErr(err);
29370 +       return err;
29371 +}
29372 +#endif
29373 diff -urN /usr/share/empty/fs/aufs/rwsem.h linux/fs/aufs/rwsem.h
29374 --- /usr/share/empty/fs/aufs/rwsem.h    1970-01-01 01:00:00.000000000 +0100
29375 +++ linux/fs/aufs/rwsem.h       2022-03-21 14:49:05.729966343 +0100
29376 @@ -0,0 +1,85 @@
29377 +/* SPDX-License-Identifier: GPL-2.0 */
29378 +/*
29379 + * Copyright (C) 2005-2021 Junjiro R. Okajima
29380 + *
29381 + * This program, aufs is free software; you can redistribute it and/or modify
29382 + * it under the terms of the GNU General Public License as published by
29383 + * the Free Software Foundation; either version 2 of the License, or
29384 + * (at your option) any later version.
29385 + *
29386 + * This program is distributed in the hope that it will be useful,
29387 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
29388 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29389 + * GNU General Public License for more details.
29390 + *
29391 + * You should have received a copy of the GNU General Public License
29392 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
29393 + */
29394 +
29395 +/*
29396 + * simple read-write semaphore wrappers
29397 + */
29398 +
29399 +#ifndef __AUFS_RWSEM_H__
29400 +#define __AUFS_RWSEM_H__
29401 +
29402 +#ifdef __KERNEL__
29403 +
29404 +#include "debug.h"
29405 +
29406 +/* in the future, the name 'au_rwsem' will be totally gone */
29407 +#define au_rwsem       rw_semaphore
29408 +
29409 +/* to debug easier, do not make them inlined functions */
29410 +#define AuRwMustNoWaiters(rw)  AuDebugOn(rwsem_is_contended(rw))
29411 +
29412 +#ifdef CONFIG_LOCKDEP
29413 +/* rwsem_is_locked() is unusable */
29414 +#define AuRwMustReadLock(rw)   AuDebugOn(IS_ENABLED(CONFIG_LOCKDEP)    \
29415 +                                         && !lockdep_recursing(current) \
29416 +                                         && debug_locks                \
29417 +                                         && !lockdep_is_held_type(rw, 1))
29418 +#define AuRwMustWriteLock(rw)  AuDebugOn(IS_ENABLED(CONFIG_LOCKDEP)    \
29419 +                                         && !lockdep_recursing(current) \
29420 +                                         && debug_locks                \
29421 +                                         && !lockdep_is_held_type(rw, 0))
29422 +#define AuRwMustAnyLock(rw)    AuDebugOn(IS_ENABLED(CONFIG_LOCKDEP)    \
29423 +                                         && !lockdep_recursing(current) \
29424 +                                         && debug_locks                \
29425 +                                         && !lockdep_is_held(rw))
29426 +#define AuRwDestroy(rw)                AuDebugOn(IS_ENABLED(CONFIG_LOCKDEP)    \
29427 +                                         && !lockdep_recursing(current) \
29428 +                                         && debug_locks                \
29429 +                                         && lockdep_is_held(rw))
29430 +#else
29431 +#define AuRwMustReadLock(rw)   do {} while (0)
29432 +#define AuRwMustWriteLock(rw)  do {} while (0)
29433 +#define AuRwMustAnyLock(rw)    do {} while (0)
29434 +#define AuRwDestroy(rw)                do {} while (0)
29435 +#endif
29436 +
29437 +#define au_rw_init(rw) init_rwsem(rw)
29438 +
29439 +#define au_rw_init_wlock(rw) do {              \
29440 +               au_rw_init(rw);                 \
29441 +               down_write(rw);                 \
29442 +       } while (0)
29443 +
29444 +#define au_rw_init_wlock_nested(rw, lsc) do {  \
29445 +               au_rw_init(rw);                 \
29446 +               down_write_nested(rw, lsc);     \
29447 +       } while (0)
29448 +
29449 +#define au_rw_read_lock(rw)            down_read(rw)
29450 +#define au_rw_read_lock_nested(rw, lsc)        down_read_nested(rw, lsc)
29451 +#define au_rw_read_unlock(rw)          up_read(rw)
29452 +#define au_rw_dgrade_lock(rw)          downgrade_write(rw)
29453 +#define au_rw_write_lock(rw)           down_write(rw)
29454 +#define au_rw_write_lock_nested(rw, lsc) down_write_nested(rw, lsc)
29455 +#define au_rw_write_unlock(rw)         up_write(rw)
29456 +/* why is not _nested version defined? */
29457 +#define au_rw_read_trylock(rw)         down_read_trylock(rw)
29458 +#define au_rw_write_trylock(rw)                down_write_trylock(rw)
29459 +
29460 +#endif /* __KERNEL__ */
29461 +#endif /* __AUFS_RWSEM_H__ */
29462 diff -urN /usr/share/empty/fs/aufs/sbinfo.c linux/fs/aufs/sbinfo.c
29463 --- /usr/share/empty/fs/aufs/sbinfo.c   1970-01-01 01:00:00.000000000 +0100
29464 +++ linux/fs/aufs/sbinfo.c      2022-03-21 14:49:05.729966343 +0100
29465 @@ -0,0 +1,316 @@
29466 +// SPDX-License-Identifier: GPL-2.0
29467 +/*
29468 + * Copyright (C) 2005-2021 Junjiro R. Okajima
29469 + *
29470 + * This program, aufs is free software; you can redistribute it and/or modify
29471 + * it under the terms of the GNU General Public License as published by
29472 + * the Free Software Foundation; either version 2 of the License, or
29473 + * (at your option) any later version.
29474 + *
29475 + * This program is distributed in the hope that it will be useful,
29476 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
29477 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29478 + * GNU General Public License for more details.
29479 + *
29480 + * You should have received a copy of the GNU General Public License
29481 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
29482 + */
29483 +
29484 +/*
29485 + * superblock private data
29486 + */
29487 +
29488 +#include <linux/iversion.h>
29489 +#include "aufs.h"
29490 +
29491 +/*
29492 + * they are necessary regardless sysfs is disabled.
29493 + */
29494 +void au_si_free(struct kobject *kobj)
29495 +{
29496 +       int i;
29497 +       struct au_sbinfo *sbinfo;
29498 +       char *locked __maybe_unused; /* debug only */
29499 +
29500 +       sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
29501 +       for (i = 0; i < AuPlink_NHASH; i++)
29502 +               AuDebugOn(!hlist_bl_empty(sbinfo->si_plink + i));
29503 +       AuDebugOn(atomic_read(&sbinfo->si_nowait.nw_len));
29504 +
29505 +       AuLCntZero(au_lcnt_read(&sbinfo->si_ninodes, /*do_rev*/0));
29506 +       au_lcnt_fin(&sbinfo->si_ninodes, /*do_sync*/0);
29507 +       AuLCntZero(au_lcnt_read(&sbinfo->si_nfiles, /*do_rev*/0));
29508 +       au_lcnt_fin(&sbinfo->si_nfiles, /*do_sync*/0);
29509 +
29510 +       dbgaufs_si_fin(sbinfo);
29511 +       au_rw_write_lock(&sbinfo->si_rwsem);
29512 +       au_br_free(sbinfo);
29513 +       au_rw_write_unlock(&sbinfo->si_rwsem);
29514 +
29515 +       au_kfree_try_rcu(sbinfo->si_branch);
29516 +       mutex_destroy(&sbinfo->si_xib_mtx);
29517 +       AuRwDestroy(&sbinfo->si_rwsem);
29518 +
29519 +       au_lcnt_wait_for_fin(&sbinfo->si_ninodes);
29520 +       /* si_nfiles is waited too */
29521 +       au_kfree_rcu(sbinfo);
29522 +}
29523 +
29524 +struct au_sbinfo *au_si_alloc(struct super_block *sb)
29525 +{
29526 +       struct au_sbinfo *sbinfo;
29527 +       int err, i;
29528 +
29529 +       err = -ENOMEM;
29530 +       sbinfo = kzalloc(sizeof(*sbinfo), GFP_NOFS);
29531 +       if (unlikely(!sbinfo))
29532 +               goto out;
29533 +
29534 +       /* will be reallocated separately */
29535 +       sbinfo->si_branch = kzalloc(sizeof(*sbinfo->si_branch), GFP_NOFS);
29536 +       if (unlikely(!sbinfo->si_branch))
29537 +               goto out_sbinfo;
29538 +
29539 +       err = sysaufs_si_init(sbinfo);
29540 +       if (!err) {
29541 +               dbgaufs_si_null(sbinfo);
29542 +               err = dbgaufs_si_init(sbinfo);
29543 +               if (unlikely(err))
29544 +                       kobject_put(&sbinfo->si_kobj);
29545 +       }
29546 +       if (unlikely(err))
29547 +               goto out_br;
29548 +
29549 +       au_nwt_init(&sbinfo->si_nowait);
29550 +       au_rw_init_wlock(&sbinfo->si_rwsem);
29551 +
29552 +       au_lcnt_init(&sbinfo->si_ninodes, /*release*/NULL);
29553 +       au_lcnt_init(&sbinfo->si_nfiles, /*release*/NULL);
29554 +
29555 +       sbinfo->si_bbot = -1;
29556 +       sbinfo->si_last_br_id = AUFS_BRANCH_MAX / 2;
29557 +
29558 +       sbinfo->si_wbr_copyup = AuWbrCopyup_Def;
29559 +       sbinfo->si_wbr_create = AuWbrCreate_Def;
29560 +       sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + sbinfo->si_wbr_copyup;
29561 +       sbinfo->si_wbr_create_ops = au_wbr_create_ops + sbinfo->si_wbr_create;
29562 +
29563 +       au_fhsm_init(sbinfo);
29564 +
29565 +       sbinfo->si_mntflags = au_opts_plink(AuOpt_Def);
29566 +
29567 +       sbinfo->si_xino_jiffy = jiffies;
29568 +       sbinfo->si_xino_expire
29569 +               = msecs_to_jiffies(AUFS_XINO_DEF_SEC * MSEC_PER_SEC);
29570 +       mutex_init(&sbinfo->si_xib_mtx);
29571 +       /* leave si_xib_last_pindex and si_xib_next_bit */
29572 +
29573 +       INIT_HLIST_BL_HEAD(&sbinfo->si_aopen);
29574 +
29575 +       sbinfo->si_rdcache = msecs_to_jiffies(AUFS_RDCACHE_DEF * MSEC_PER_SEC);
29576 +       sbinfo->si_rdblk = AUFS_RDBLK_DEF;
29577 +       sbinfo->si_rdhash = AUFS_RDHASH_DEF;
29578 +       sbinfo->si_dirwh = AUFS_DIRWH_DEF;
29579 +
29580 +       for (i = 0; i < AuPlink_NHASH; i++)
29581 +               INIT_HLIST_BL_HEAD(sbinfo->si_plink + i);
29582 +       init_waitqueue_head(&sbinfo->si_plink_wq);
29583 +       spin_lock_init(&sbinfo->si_plink_maint_lock);
29584 +
29585 +       INIT_HLIST_BL_HEAD(&sbinfo->si_files);
29586 +
29587 +       /* with getattr by default */
29588 +       sbinfo->si_iop_array = aufs_iop;
29589 +
29590 +       /* leave other members for sysaufs and si_mnt. */
29591 +       sbinfo->si_sb = sb;
29592 +       if (sb) {
29593 +               sb->s_fs_info = sbinfo;
29594 +               si_pid_set(sb);
29595 +       }
29596 +       return sbinfo; /* success */
29597 +
29598 +out_br:
29599 +       au_kfree_try_rcu(sbinfo->si_branch);
29600 +out_sbinfo:
29601 +       au_kfree_rcu(sbinfo);
29602 +out:
29603 +       return ERR_PTR(err);
29604 +}
29605 +
29606 +int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr, int may_shrink)
29607 +{
29608 +       int err, sz;
29609 +       struct au_branch **brp;
29610 +
29611 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
29612 +
29613 +       err = -ENOMEM;
29614 +       sz = sizeof(*brp) * (sbinfo->si_bbot + 1);
29615 +       if (unlikely(!sz))
29616 +               sz = sizeof(*brp);
29617 +       brp = au_kzrealloc(sbinfo->si_branch, sz, sizeof(*brp) * nbr, GFP_NOFS,
29618 +                          may_shrink);
29619 +       if (brp) {
29620 +               sbinfo->si_branch = brp;
29621 +               err = 0;
29622 +       }
29623 +
29624 +       return err;
29625 +}
29626 +
29627 +/* ---------------------------------------------------------------------- */
29628 +
29629 +unsigned int au_sigen_inc(struct super_block *sb)
29630 +{
29631 +       unsigned int gen;
29632 +       struct inode *inode;
29633 +
29634 +       SiMustWriteLock(sb);
29635 +
29636 +       gen = ++au_sbi(sb)->si_generation;
29637 +       au_update_digen(sb->s_root);
29638 +       inode = d_inode(sb->s_root);
29639 +       au_update_iigen(inode, /*half*/0);
29640 +       inode_inc_iversion(inode);
29641 +       return gen;
29642 +}
29643 +
29644 +aufs_bindex_t au_new_br_id(struct super_block *sb)
29645 +{
29646 +       aufs_bindex_t br_id;
29647 +       int i;
29648 +       struct au_sbinfo *sbinfo;
29649 +
29650 +       SiMustWriteLock(sb);
29651 +
29652 +       sbinfo = au_sbi(sb);
29653 +       for (i = 0; i <= AUFS_BRANCH_MAX; i++) {
29654 +               br_id = ++sbinfo->si_last_br_id;
29655 +               AuDebugOn(br_id < 0);
29656 +               if (br_id && au_br_index(sb, br_id) < 0)
29657 +                       return br_id;
29658 +       }
29659 +
29660 +       return -1;
29661 +}
29662 +
29663 +/* ---------------------------------------------------------------------- */
29664 +
29665 +/* it is ok that new 'nwt' tasks are appended while we are sleeping */
29666 +int si_read_lock(struct super_block *sb, int flags)
29667 +{
29668 +       int err;
29669 +
29670 +       err = 0;
29671 +       if (au_ftest_lock(flags, FLUSH))
29672 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
29673 +
29674 +       si_noflush_read_lock(sb);
29675 +       err = au_plink_maint(sb, flags);
29676 +       if (unlikely(err))
29677 +               si_read_unlock(sb);
29678 +
29679 +       return err;
29680 +}
29681 +
29682 +int si_write_lock(struct super_block *sb, int flags)
29683 +{
29684 +       int err;
29685 +
29686 +       if (au_ftest_lock(flags, FLUSH))
29687 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
29688 +
29689 +       si_noflush_write_lock(sb);
29690 +       err = au_plink_maint(sb, flags);
29691 +       if (unlikely(err))
29692 +               si_write_unlock(sb);
29693 +
29694 +       return err;
29695 +}
29696 +
29697 +/* dentry and super_block lock. call at entry point */
29698 +int aufs_read_lock(struct dentry *dentry, int flags)
29699 +{
29700 +       int err;
29701 +       struct super_block *sb;
29702 +
29703 +       sb = dentry->d_sb;
29704 +       err = si_read_lock(sb, flags);
29705 +       if (unlikely(err))
29706 +               goto out;
29707 +
29708 +       if (au_ftest_lock(flags, DW))
29709 +               di_write_lock_child(dentry);
29710 +       else
29711 +               di_read_lock_child(dentry, flags);
29712 +
29713 +       if (au_ftest_lock(flags, GEN)) {
29714 +               err = au_digen_test(dentry, au_sigen(sb));
29715 +               if (!au_opt_test(au_mntflags(sb), UDBA_NONE))
29716 +                       AuDebugOn(!err && au_dbrange_test(dentry));
29717 +               else if (!err)
29718 +                       err = au_dbrange_test(dentry);
29719 +               if (unlikely(err))
29720 +                       aufs_read_unlock(dentry, flags);
29721 +       }
29722 +
29723 +out:
29724 +       return err;
29725 +}
29726 +
29727 +void aufs_read_unlock(struct dentry *dentry, int flags)
29728 +{
29729 +       if (au_ftest_lock(flags, DW))
29730 +               di_write_unlock(dentry);
29731 +       else
29732 +               di_read_unlock(dentry, flags);
29733 +       si_read_unlock(dentry->d_sb);
29734 +}
29735 +
29736 +void aufs_write_lock(struct dentry *dentry)
29737 +{
29738 +       si_write_lock(dentry->d_sb, AuLock_FLUSH | AuLock_NOPLMW);
29739 +       di_write_lock_child(dentry);
29740 +}
29741 +
29742 +void aufs_write_unlock(struct dentry *dentry)
29743 +{
29744 +       di_write_unlock(dentry);
29745 +       si_write_unlock(dentry->d_sb);
29746 +}
29747 +
29748 +int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags)
29749 +{
29750 +       int err;
29751 +       unsigned int sigen;
29752 +       struct super_block *sb;
29753 +
29754 +       sb = d1->d_sb;
29755 +       err = si_read_lock(sb, flags);
29756 +       if (unlikely(err))
29757 +               goto out;
29758 +
29759 +       di_write_lock2_child(d1, d2, au_ftest_lock(flags, DIRS));
29760 +
29761 +       if (au_ftest_lock(flags, GEN)) {
29762 +               sigen = au_sigen(sb);
29763 +               err = au_digen_test(d1, sigen);
29764 +               AuDebugOn(!err && au_dbrange_test(d1));
29765 +               if (!err) {
29766 +                       err = au_digen_test(d2, sigen);
29767 +                       AuDebugOn(!err && au_dbrange_test(d2));
29768 +               }
29769 +               if (unlikely(err))
29770 +                       aufs_read_and_write_unlock2(d1, d2);
29771 +       }
29772 +
29773 +out:
29774 +       return err;
29775 +}
29776 +
29777 +void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2)
29778 +{
29779 +       di_write_unlock2(d1, d2);
29780 +       si_read_unlock(d1->d_sb);
29781 +}
29782 diff -urN /usr/share/empty/fs/aufs/super.c linux/fs/aufs/super.c
29783 --- /usr/share/empty/fs/aufs/super.c    1970-01-01 01:00:00.000000000 +0100
29784 +++ linux/fs/aufs/super.c       2022-03-21 14:49:05.729966343 +0100
29785 @@ -0,0 +1,871 @@
29786 +// SPDX-License-Identifier: GPL-2.0
29787 +/*
29788 + * Copyright (C) 2005-2021 Junjiro R. Okajima
29789 + *
29790 + * This program, aufs is free software; you can redistribute it and/or modify
29791 + * it under the terms of the GNU General Public License as published by
29792 + * the Free Software Foundation; either version 2 of the License, or
29793 + * (at your option) any later version.
29794 + *
29795 + * This program is distributed in the hope that it will be useful,
29796 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
29797 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29798 + * GNU General Public License for more details.
29799 + *
29800 + * You should have received a copy of the GNU General Public License
29801 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
29802 + */
29803 +
29804 +/*
29805 + * mount and super_block operations
29806 + */
29807 +
29808 +#include <linux/iversion.h>
29809 +#include <linux/mm.h>
29810 +#include <linux/seq_file.h>
29811 +#include <linux/statfs.h>
29812 +#include <linux/vmalloc.h>
29813 +#include "aufs.h"
29814 +
29815 +/*
29816 + * super_operations
29817 + */
29818 +static struct inode *aufs_alloc_inode(struct super_block *sb __maybe_unused)
29819 +{
29820 +       struct au_icntnr *c;
29821 +
29822 +       c = au_cache_alloc_icntnr();
29823 +       if (c) {
29824 +               au_icntnr_init(c);
29825 +               inode_set_iversion(&c->vfs_inode, 1); /* sigen(sb); */
29826 +               c->iinfo.ii_hinode = NULL;
29827 +               return &c->vfs_inode;
29828 +       }
29829 +       return NULL;
29830 +}
29831 +
29832 +static void aufs_destroy_inode(struct inode *inode)
29833 +{
29834 +       if (!au_is_bad_inode(inode))
29835 +               au_iinfo_fin(inode);
29836 +}
29837 +
29838 +static void aufs_free_inode(struct inode *inode)
29839 +{
29840 +       au_cache_free_icntnr(container_of(inode, struct au_icntnr, vfs_inode));
29841 +}
29842 +
29843 +struct inode *au_iget_locked(struct super_block *sb, ino_t ino)
29844 +{
29845 +       struct inode *inode;
29846 +       int err;
29847 +
29848 +       inode = iget_locked(sb, ino);
29849 +       if (unlikely(!inode)) {
29850 +               inode = ERR_PTR(-ENOMEM);
29851 +               goto out;
29852 +       }
29853 +       if (!(inode->i_state & I_NEW))
29854 +               goto out;
29855 +
29856 +       err = au_xigen_new(inode);
29857 +       if (!err)
29858 +               err = au_iinfo_init(inode);
29859 +       if (!err)
29860 +               inode_inc_iversion(inode);
29861 +       else {
29862 +               iget_failed(inode);
29863 +               inode = ERR_PTR(err);
29864 +       }
29865 +
29866 +out:
29867 +       /* never return NULL */
29868 +       AuDebugOn(!inode);
29869 +       AuTraceErrPtr(inode);
29870 +       return inode;
29871 +}
29872 +
29873 +/* lock free root dinfo */
29874 +static int au_show_brs(struct seq_file *seq, struct super_block *sb)
29875 +{
29876 +       int err;
29877 +       aufs_bindex_t bindex, bbot;
29878 +       struct path path;
29879 +       struct au_hdentry *hdp;
29880 +       struct au_branch *br;
29881 +       au_br_perm_str_t perm;
29882 +
29883 +       err = 0;
29884 +       bbot = au_sbbot(sb);
29885 +       bindex = 0;
29886 +       hdp = au_hdentry(au_di(sb->s_root), bindex);
29887 +       for (; !err && bindex <= bbot; bindex++, hdp++) {
29888 +               br = au_sbr(sb, bindex);
29889 +               path.mnt = au_br_mnt(br);
29890 +               path.dentry = hdp->hd_dentry;
29891 +               err = au_seq_path(seq, &path);
29892 +               if (!err) {
29893 +                       au_optstr_br_perm(&perm, br->br_perm);
29894 +                       seq_printf(seq, "=%s", perm.a);
29895 +                       if (bindex != bbot)
29896 +                               seq_putc(seq, ':');
29897 +               }
29898 +       }
29899 +       if (unlikely(err || seq_has_overflowed(seq)))
29900 +               err = -E2BIG;
29901 +
29902 +       return err;
29903 +}
29904 +
29905 +static void au_gen_fmt(char *fmt, int len __maybe_unused, const char *pat,
29906 +                      const char *append)
29907 +{
29908 +       char *p;
29909 +
29910 +       p = fmt;
29911 +       while (*pat != ':')
29912 +               *p++ = *pat++;
29913 +       *p++ = *pat++;
29914 +       strcpy(p, append);
29915 +       AuDebugOn(strlen(fmt) >= len);
29916 +}
29917 +
29918 +static void au_show_wbr_create(struct seq_file *m, int v,
29919 +                              struct au_sbinfo *sbinfo)
29920 +{
29921 +       const char *pat;
29922 +       char fmt[32];
29923 +       struct au_wbr_mfs *mfs;
29924 +
29925 +       AuRwMustAnyLock(&sbinfo->si_rwsem);
29926 +
29927 +       seq_puts(m, ",create=");
29928 +       pat = au_optstr_wbr_create(v);
29929 +       mfs = &sbinfo->si_wbr_mfs;
29930 +       switch (v) {
29931 +       case AuWbrCreate_TDP:
29932 +       case AuWbrCreate_RR:
29933 +       case AuWbrCreate_MFS:
29934 +       case AuWbrCreate_PMFS:
29935 +               seq_puts(m, pat);
29936 +               break;
29937 +       case AuWbrCreate_MFSRR:
29938 +       case AuWbrCreate_TDMFS:
29939 +       case AuWbrCreate_PMFSRR:
29940 +               au_gen_fmt(fmt, sizeof(fmt), pat, "%llu");
29941 +               seq_printf(m, fmt, mfs->mfsrr_watermark);
29942 +               break;
29943 +       case AuWbrCreate_MFSV:
29944 +       case AuWbrCreate_PMFSV:
29945 +               au_gen_fmt(fmt, sizeof(fmt), pat, "%lu");
29946 +               seq_printf(m, fmt,
29947 +                          jiffies_to_msecs(mfs->mfs_expire)
29948 +                          / MSEC_PER_SEC);
29949 +               break;
29950 +       case AuWbrCreate_MFSRRV:
29951 +       case AuWbrCreate_TDMFSV:
29952 +       case AuWbrCreate_PMFSRRV:
29953 +               au_gen_fmt(fmt, sizeof(fmt), pat, "%llu:%lu");
29954 +               seq_printf(m, fmt, mfs->mfsrr_watermark,
29955 +                          jiffies_to_msecs(mfs->mfs_expire) / MSEC_PER_SEC);
29956 +               break;
29957 +       default:
29958 +               BUG();
29959 +       }
29960 +}
29961 +
29962 +static int au_show_xino(struct seq_file *seq, struct super_block *sb)
29963 +{
29964 +#ifdef CONFIG_SYSFS
29965 +       return 0;
29966 +#else
29967 +       int err;
29968 +       const int len = sizeof(AUFS_XINO_FNAME) - 1;
29969 +       aufs_bindex_t bindex, brid;
29970 +       struct qstr *name;
29971 +       struct file *f;
29972 +       struct dentry *d, *h_root;
29973 +       struct au_branch *br;
29974 +
29975 +       AuRwMustAnyLock(&sbinfo->si_rwsem);
29976 +
29977 +       err = 0;
29978 +       f = au_sbi(sb)->si_xib;
29979 +       if (!f)
29980 +               goto out;
29981 +
29982 +       /* stop printing the default xino path on the first writable branch */
29983 +       h_root = NULL;
29984 +       bindex = au_xi_root(sb, f->f_path.dentry);
29985 +       if (bindex >= 0) {
29986 +               br = au_sbr_sb(sb, bindex);
29987 +               h_root = au_br_dentry(br);
29988 +       }
29989 +
29990 +       d = f->f_path.dentry;
29991 +       name = &d->d_name;
29992 +       /* safe ->d_parent because the file is unlinked */
29993 +       if (d->d_parent == h_root
29994 +           && name->len == len
29995 +           && !memcmp(name->name, AUFS_XINO_FNAME, len))
29996 +               goto out;
29997 +
29998 +       seq_puts(seq, ",xino=");
29999 +       err = au_xino_path(seq, f);
30000 +
30001 +out:
30002 +       return err;
30003 +#endif
30004 +}
30005 +
30006 +/* seq_file will re-call me in case of too long string */
30007 +static int aufs_show_options(struct seq_file *m, struct dentry *dentry)
30008 +{
30009 +       int err;
30010 +       unsigned int mnt_flags, v;
30011 +       struct super_block *sb;
30012 +       struct au_sbinfo *sbinfo;
30013 +
30014 +#define AuBool(name, str) do { \
30015 +       v = au_opt_test(mnt_flags, name); \
30016 +       if (v != au_opt_test(AuOpt_Def, name)) \
30017 +               seq_printf(m, ",%s" #str, v ? "" : "no"); \
30018 +} while (0)
30019 +
30020 +#define AuStr(name, str) do { \
30021 +       v = mnt_flags & AuOptMask_##name; \
30022 +       if (v != (AuOpt_Def & AuOptMask_##name)) \
30023 +               seq_printf(m, "," #str "=%s", au_optstr_##str(v)); \
30024 +} while (0)
30025 +
30026 +#define AuUInt(name, str, val) do { \
30027 +       if (val != AUFS_##name##_DEF) \
30028 +               seq_printf(m, "," #str "=%u", val); \
30029 +} while (0)
30030 +
30031 +       sb = dentry->d_sb;
30032 +       if (sb->s_flags & SB_POSIXACL)
30033 +               seq_puts(m, ",acl");
30034 +#if 0 /* reserved for future use */
30035 +       if (sb->s_flags & SB_I_VERSION)
30036 +               seq_puts(m, ",i_version");
30037 +#endif
30038 +
30039 +       /* lock free root dinfo */
30040 +       si_noflush_read_lock(sb);
30041 +       sbinfo = au_sbi(sb);
30042 +       seq_printf(m, ",si=%lx", sysaufs_si_id(sbinfo));
30043 +
30044 +       mnt_flags = au_mntflags(sb);
30045 +       if (au_opt_test(mnt_flags, XINO)) {
30046 +               err = au_show_xino(m, sb);
30047 +               if (unlikely(err))
30048 +                       goto out;
30049 +       } else
30050 +               seq_puts(m, ",noxino");
30051 +
30052 +       AuBool(TRUNC_XINO, trunc_xino);
30053 +       AuStr(UDBA, udba);
30054 +       AuBool(SHWH, shwh);
30055 +       AuBool(PLINK, plink);
30056 +       AuBool(DIO, dio);
30057 +       AuBool(DIRPERM1, dirperm1);
30058 +
30059 +       v = sbinfo->si_wbr_create;
30060 +       if (v != AuWbrCreate_Def)
30061 +               au_show_wbr_create(m, v, sbinfo);
30062 +
30063 +       v = sbinfo->si_wbr_copyup;
30064 +       if (v != AuWbrCopyup_Def)
30065 +               seq_printf(m, ",cpup=%s", au_optstr_wbr_copyup(v));
30066 +
30067 +       v = au_opt_test(mnt_flags, ALWAYS_DIROPQ);
30068 +       if (v != au_opt_test(AuOpt_Def, ALWAYS_DIROPQ))
30069 +               seq_printf(m, ",diropq=%c", v ? 'a' : 'w');
30070 +
30071 +       AuUInt(DIRWH, dirwh, sbinfo->si_dirwh);
30072 +
30073 +       v = jiffies_to_msecs(sbinfo->si_rdcache) / MSEC_PER_SEC;
30074 +       AuUInt(RDCACHE, rdcache, v);
30075 +
30076 +       AuUInt(RDBLK, rdblk, sbinfo->si_rdblk);
30077 +       AuUInt(RDHASH, rdhash, sbinfo->si_rdhash);
30078 +
30079 +       au_fhsm_show(m, sbinfo);
30080 +
30081 +       AuBool(DIRREN, dirren);
30082 +       AuBool(SUM, sum);
30083 +       /* AuBool(SUM_W, wsum); */
30084 +       AuBool(WARN_PERM, warn_perm);
30085 +       AuBool(VERBOSE, verbose);
30086 +
30087 +out:
30088 +       /* be sure to print "br:" last */
30089 +       if (!sysaufs_brs) {
30090 +               seq_puts(m, ",br:");
30091 +               au_show_brs(m, sb);
30092 +       }
30093 +       si_read_unlock(sb);
30094 +       return 0;
30095 +
30096 +#undef AuBool
30097 +#undef AuStr
30098 +#undef AuUInt
30099 +}
30100 +
30101 +/* ---------------------------------------------------------------------- */
30102 +
30103 +/* sum mode which returns the summation for statfs(2) */
30104 +
30105 +static u64 au_add_till_max(u64 a, u64 b)
30106 +{
30107 +       u64 old;
30108 +
30109 +       old = a;
30110 +       a += b;
30111 +       if (old <= a)
30112 +               return a;
30113 +       return ULLONG_MAX;
30114 +}
30115 +
30116 +static u64 au_mul_till_max(u64 a, long mul)
30117 +{
30118 +       u64 old;
30119 +
30120 +       old = a;
30121 +       a *= mul;
30122 +       if (old <= a)
30123 +               return a;
30124 +       return ULLONG_MAX;
30125 +}
30126 +
30127 +static int au_statfs_sum(struct super_block *sb, struct kstatfs *buf)
30128 +{
30129 +       int err;
30130 +       long bsize, factor;
30131 +       u64 blocks, bfree, bavail, files, ffree;
30132 +       aufs_bindex_t bbot, bindex, i;
30133 +       unsigned char shared;
30134 +       struct path h_path;
30135 +       struct super_block *h_sb;
30136 +
30137 +       err = 0;
30138 +       bsize = LONG_MAX;
30139 +       files = 0;
30140 +       ffree = 0;
30141 +       blocks = 0;
30142 +       bfree = 0;
30143 +       bavail = 0;
30144 +       bbot = au_sbbot(sb);
30145 +       for (bindex = 0; bindex <= bbot; bindex++) {
30146 +               h_path.mnt = au_sbr_mnt(sb, bindex);
30147 +               h_sb = h_path.mnt->mnt_sb;
30148 +               shared = 0;
30149 +               for (i = 0; !shared && i < bindex; i++)
30150 +                       shared = (au_sbr_sb(sb, i) == h_sb);
30151 +               if (shared)
30152 +                       continue;
30153 +
30154 +               /* sb->s_root for NFS is unreliable */
30155 +               h_path.dentry = h_path.mnt->mnt_root;
30156 +               err = vfs_statfs(&h_path, buf);
30157 +               if (unlikely(err))
30158 +                       goto out;
30159 +
30160 +               if (bsize > buf->f_bsize) {
30161 +                       /*
30162 +                        * we will reduce bsize, so we have to expand blocks
30163 +                        * etc. to match them again
30164 +                        */
30165 +                       factor = (bsize / buf->f_bsize);
30166 +                       blocks = au_mul_till_max(blocks, factor);
30167 +                       bfree = au_mul_till_max(bfree, factor);
30168 +                       bavail = au_mul_till_max(bavail, factor);
30169 +                       bsize = buf->f_bsize;
30170 +               }
30171 +
30172 +               factor = (buf->f_bsize / bsize);
30173 +               blocks = au_add_till_max(blocks,
30174 +                               au_mul_till_max(buf->f_blocks, factor));
30175 +               bfree = au_add_till_max(bfree,
30176 +                               au_mul_till_max(buf->f_bfree, factor));
30177 +               bavail = au_add_till_max(bavail,
30178 +                               au_mul_till_max(buf->f_bavail, factor));
30179 +               files = au_add_till_max(files, buf->f_files);
30180 +               ffree = au_add_till_max(ffree, buf->f_ffree);
30181 +       }
30182 +
30183 +       buf->f_bsize = bsize;
30184 +       buf->f_blocks = blocks;
30185 +       buf->f_bfree = bfree;
30186 +       buf->f_bavail = bavail;
30187 +       buf->f_files = files;
30188 +       buf->f_ffree = ffree;
30189 +       buf->f_frsize = 0;
30190 +
30191 +out:
30192 +       return err;
30193 +}
30194 +
30195 +static int aufs_statfs(struct dentry *dentry, struct kstatfs *buf)
30196 +{
30197 +       int err;
30198 +       struct path h_path;
30199 +       struct super_block *sb;
30200 +
30201 +       /* lock free root dinfo */
30202 +       sb = dentry->d_sb;
30203 +       si_noflush_read_lock(sb);
30204 +       if (!au_opt_test(au_mntflags(sb), SUM)) {
30205 +               /* sb->s_root for NFS is unreliable */
30206 +               h_path.mnt = au_sbr_mnt(sb, 0);
30207 +               h_path.dentry = h_path.mnt->mnt_root;
30208 +               err = vfs_statfs(&h_path, buf);
30209 +       } else
30210 +               err = au_statfs_sum(sb, buf);
30211 +       si_read_unlock(sb);
30212 +
30213 +       if (!err) {
30214 +               buf->f_type = AUFS_SUPER_MAGIC;
30215 +               buf->f_namelen = AUFS_MAX_NAMELEN;
30216 +               memset(&buf->f_fsid, 0, sizeof(buf->f_fsid));
30217 +       }
30218 +       /* buf->f_bsize = buf->f_blocks = buf->f_bfree = buf->f_bavail = -1; */
30219 +
30220 +       return err;
30221 +}
30222 +
30223 +/* ---------------------------------------------------------------------- */
30224 +
30225 +static int aufs_sync_fs(struct super_block *sb, int wait)
30226 +{
30227 +       int err, e;
30228 +       aufs_bindex_t bbot, bindex;
30229 +       struct au_branch *br;
30230 +       struct super_block *h_sb;
30231 +
30232 +       err = 0;
30233 +       si_noflush_read_lock(sb);
30234 +       bbot = au_sbbot(sb);
30235 +       for (bindex = 0; bindex <= bbot; bindex++) {
30236 +               br = au_sbr(sb, bindex);
30237 +               if (!au_br_writable(br->br_perm))
30238 +                       continue;
30239 +
30240 +               h_sb = au_sbr_sb(sb, bindex);
30241 +               e = vfsub_sync_filesystem(h_sb);
30242 +               if (unlikely(e && !err))
30243 +                       err = e;
30244 +               /* go on even if an error happens */
30245 +       }
30246 +       si_read_unlock(sb);
30247 +
30248 +       return err;
30249 +}
30250 +
30251 +/* ---------------------------------------------------------------------- */
30252 +
30253 +/* final actions when unmounting a file system */
30254 +static void aufs_put_super(struct super_block *sb)
30255 +{
30256 +       struct au_sbinfo *sbinfo;
30257 +
30258 +       sbinfo = au_sbi(sb);
30259 +       if (sbinfo)
30260 +               kobject_put(&sbinfo->si_kobj);
30261 +}
30262 +
30263 +/* ---------------------------------------------------------------------- */
30264 +
30265 +void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb,
30266 +                    struct super_block *sb, void *arg)
30267 +{
30268 +       void *array;
30269 +       unsigned long long n, sz;
30270 +
30271 +       array = NULL;
30272 +       n = 0;
30273 +       if (!*hint)
30274 +               goto out;
30275 +
30276 +       if (*hint > ULLONG_MAX / sizeof(array)) {
30277 +               array = ERR_PTR(-EMFILE);
30278 +               pr_err("hint %llu\n", *hint);
30279 +               goto out;
30280 +       }
30281 +
30282 +       sz = sizeof(array) * *hint;
30283 +       array = kzalloc(sz, GFP_NOFS);
30284 +       if (unlikely(!array))
30285 +               array = vzalloc(sz);
30286 +       if (unlikely(!array)) {
30287 +               array = ERR_PTR(-ENOMEM);
30288 +               goto out;
30289 +       }
30290 +
30291 +       n = cb(sb, array, *hint, arg);
30292 +       AuDebugOn(n > *hint);
30293 +
30294 +out:
30295 +       *hint = n;
30296 +       return array;
30297 +}
30298 +
30299 +static unsigned long long au_iarray_cb(struct super_block *sb, void *a,
30300 +                                      unsigned long long max __maybe_unused,
30301 +                                      void *arg)
30302 +{
30303 +       unsigned long long n;
30304 +       struct inode **p, *inode;
30305 +       struct list_head *head;
30306 +
30307 +       n = 0;
30308 +       p = a;
30309 +       head = arg;
30310 +       spin_lock(&sb->s_inode_list_lock);
30311 +       list_for_each_entry(inode, head, i_sb_list) {
30312 +               if (!au_is_bad_inode(inode)
30313 +                   && au_ii(inode)->ii_btop >= 0) {
30314 +                       spin_lock(&inode->i_lock);
30315 +                       if (atomic_read(&inode->i_count)) {
30316 +                               au_igrab(inode);
30317 +                               *p++ = inode;
30318 +                               n++;
30319 +                               AuDebugOn(n > max);
30320 +                       }
30321 +                       spin_unlock(&inode->i_lock);
30322 +               }
30323 +       }
30324 +       spin_unlock(&sb->s_inode_list_lock);
30325 +
30326 +       return n;
30327 +}
30328 +
30329 +struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max)
30330 +{
30331 +       struct au_sbinfo *sbi;
30332 +
30333 +       sbi = au_sbi(sb);
30334 +       *max = au_lcnt_read(&sbi->si_ninodes, /*do_rev*/1);
30335 +       return au_array_alloc(max, au_iarray_cb, sb, &sb->s_inodes);
30336 +}
30337 +
30338 +void au_iarray_free(struct inode **a, unsigned long long max)
30339 +{
30340 +       unsigned long long ull;
30341 +
30342 +       for (ull = 0; ull < max; ull++)
30343 +               iput(a[ull]);
30344 +       kvfree(a);
30345 +}
30346 +
30347 +/* ---------------------------------------------------------------------- */
30348 +
30349 +/*
30350 + * refresh dentry and inode at remount time.
30351 + */
30352 +/* todo: consolidate with simple_reval_dpath() and au_reval_for_attr() */
30353 +static int au_do_refresh(struct dentry *dentry, unsigned int dir_flags,
30354 +                     struct dentry *parent)
30355 +{
30356 +       int err;
30357 +
30358 +       di_write_lock_child(dentry);
30359 +       di_read_lock_parent(parent, AuLock_IR);
30360 +       err = au_refresh_dentry(dentry, parent);
30361 +       if (!err && dir_flags)
30362 +               au_hn_reset(d_inode(dentry), dir_flags);
30363 +       di_read_unlock(parent, AuLock_IR);
30364 +       di_write_unlock(dentry);
30365 +
30366 +       return err;
30367 +}
30368 +
30369 +static int au_do_refresh_d(struct dentry *dentry, unsigned int sigen,
30370 +                          struct au_sbinfo *sbinfo,
30371 +                          const unsigned int dir_flags, unsigned int do_idop)
30372 +{
30373 +       int err;
30374 +       struct dentry *parent;
30375 +
30376 +       err = 0;
30377 +       parent = dget_parent(dentry);
30378 +       if (!au_digen_test(parent, sigen) && au_digen_test(dentry, sigen)) {
30379 +               if (d_really_is_positive(dentry)) {
30380 +                       if (!d_is_dir(dentry))
30381 +                               err = au_do_refresh(dentry, /*dir_flags*/0,
30382 +                                                parent);
30383 +                       else {
30384 +                               err = au_do_refresh(dentry, dir_flags, parent);
30385 +                               if (unlikely(err))
30386 +                                       au_fset_si(sbinfo, FAILED_REFRESH_DIR);
30387 +                       }
30388 +               } else
30389 +                       err = au_do_refresh(dentry, /*dir_flags*/0, parent);
30390 +               AuDbgDentry(dentry);
30391 +       }
30392 +       dput(parent);
30393 +
30394 +       if (!err) {
30395 +               if (do_idop)
30396 +                       au_refresh_dop(dentry, /*force_reval*/0);
30397 +       } else
30398 +               au_refresh_dop(dentry, /*force_reval*/1);
30399 +
30400 +       AuTraceErr(err);
30401 +       return err;
30402 +}
30403 +
30404 +static int au_refresh_d(struct super_block *sb, unsigned int do_idop)
30405 +{
30406 +       int err, i, j, ndentry, e;
30407 +       unsigned int sigen;
30408 +       struct au_dcsub_pages dpages;
30409 +       struct au_dpage *dpage;
30410 +       struct dentry **dentries, *d;
30411 +       struct au_sbinfo *sbinfo;
30412 +       struct dentry *root = sb->s_root;
30413 +       const unsigned int dir_flags = au_hi_flags(d_inode(root), /*isdir*/1);
30414 +
30415 +       if (do_idop)
30416 +               au_refresh_dop(root, /*force_reval*/0);
30417 +
30418 +       err = au_dpages_init(&dpages, GFP_NOFS);
30419 +       if (unlikely(err))
30420 +               goto out;
30421 +       err = au_dcsub_pages(&dpages, root, NULL, NULL);
30422 +       if (unlikely(err))
30423 +               goto out_dpages;
30424 +
30425 +       sigen = au_sigen(sb);
30426 +       sbinfo = au_sbi(sb);
30427 +       for (i = 0; i < dpages.ndpage; i++) {
30428 +               dpage = dpages.dpages + i;
30429 +               dentries = dpage->dentries;
30430 +               ndentry = dpage->ndentry;
30431 +               for (j = 0; j < ndentry; j++) {
30432 +                       d = dentries[j];
30433 +                       e = au_do_refresh_d(d, sigen, sbinfo, dir_flags,
30434 +                                           do_idop);
30435 +                       if (unlikely(e && !err))
30436 +                               err = e;
30437 +                       /* go on even err */
30438 +               }
30439 +       }
30440 +
30441 +out_dpages:
30442 +       au_dpages_free(&dpages);
30443 +out:
30444 +       return err;
30445 +}
30446 +
30447 +static int au_refresh_i(struct super_block *sb, unsigned int do_idop)
30448 +{
30449 +       int err, e;
30450 +       unsigned int sigen;
30451 +       unsigned long long max, ull;
30452 +       struct inode *inode, **array;
30453 +
30454 +       array = au_iarray_alloc(sb, &max);
30455 +       err = PTR_ERR(array);
30456 +       if (IS_ERR(array))
30457 +               goto out;
30458 +
30459 +       err = 0;
30460 +       sigen = au_sigen(sb);
30461 +       for (ull = 0; ull < max; ull++) {
30462 +               inode = array[ull];
30463 +               if (unlikely(!inode))
30464 +                       break;
30465 +
30466 +               e = 0;
30467 +               ii_write_lock_child(inode);
30468 +               if (au_iigen(inode, NULL) != sigen) {
30469 +                       e = au_refresh_hinode_self(inode);
30470 +                       if (unlikely(e)) {
30471 +                               au_refresh_iop(inode, /*force_getattr*/1);
30472 +                               pr_err("error %d, i%lu\n", e, inode->i_ino);
30473 +                               if (!err)
30474 +                                       err = e;
30475 +                               /* go on even if err */
30476 +                       }
30477 +               }
30478 +               if (!e && do_idop)
30479 +                       au_refresh_iop(inode, /*force_getattr*/0);
30480 +               ii_write_unlock(inode);
30481 +       }
30482 +
30483 +       au_iarray_free(array, max);
30484 +
30485 +out:
30486 +       return err;
30487 +}
30488 +
30489 +void au_remount_refresh(struct super_block *sb, unsigned int do_idop)
30490 +{
30491 +       int err, e;
30492 +       unsigned int udba;
30493 +       aufs_bindex_t bindex, bbot;
30494 +       struct dentry *root;
30495 +       struct inode *inode;
30496 +       struct au_branch *br;
30497 +       struct au_sbinfo *sbi;
30498 +
30499 +       au_sigen_inc(sb);
30500 +       sbi = au_sbi(sb);
30501 +       au_fclr_si(sbi, FAILED_REFRESH_DIR);
30502 +
30503 +       root = sb->s_root;
30504 +       DiMustNoWaiters(root);
30505 +       inode = d_inode(root);
30506 +       IiMustNoWaiters(inode);
30507 +
30508 +       udba = au_opt_udba(sb);
30509 +       bbot = au_sbbot(sb);
30510 +       for (bindex = 0; bindex <= bbot; bindex++) {
30511 +               br = au_sbr(sb, bindex);
30512 +               err = au_hnotify_reset_br(udba, br, br->br_perm);
30513 +               if (unlikely(err))
30514 +                       AuIOErr("hnotify failed on br %d, %d, ignored\n",
30515 +                               bindex, err);
30516 +               /* go on even if err */
30517 +       }
30518 +       au_hn_reset(inode, au_hi_flags(inode, /*isdir*/1));
30519 +
30520 +       if (do_idop) {
30521 +               if (au_ftest_si(sbi, NO_DREVAL)) {
30522 +                       AuDebugOn(sb->s_d_op == &aufs_dop_noreval);
30523 +                       sb->s_d_op = &aufs_dop_noreval;
30524 +                       AuDebugOn(sbi->si_iop_array == aufs_iop_nogetattr);
30525 +                       sbi->si_iop_array = aufs_iop_nogetattr;
30526 +               } else {
30527 +                       AuDebugOn(sb->s_d_op == &aufs_dop);
30528 +                       sb->s_d_op = &aufs_dop;
30529 +                       AuDebugOn(sbi->si_iop_array == aufs_iop);
30530 +                       sbi->si_iop_array = aufs_iop;
30531 +               }
30532 +               pr_info("reset to %ps and %ps\n",
30533 +                       sb->s_d_op, sbi->si_iop_array);
30534 +       }
30535 +
30536 +       di_write_unlock(root);
30537 +       err = au_refresh_d(sb, do_idop);
30538 +       e = au_refresh_i(sb, do_idop);
30539 +       if (unlikely(e && !err))
30540 +               err = e;
30541 +       /* aufs_write_lock() calls ..._child() */
30542 +       di_write_lock_child(root);
30543 +
30544 +       au_cpup_attr_all(inode, /*force*/1);
30545 +
30546 +       if (unlikely(err))
30547 +               AuIOErr("refresh failed, ignored, %d\n", err);
30548 +}
30549 +
30550 +const struct super_operations aufs_sop = {
30551 +       .alloc_inode    = aufs_alloc_inode,
30552 +       .destroy_inode  = aufs_destroy_inode,
30553 +       .free_inode     = aufs_free_inode,
30554 +       /* always deleting, no clearing */
30555 +       .drop_inode     = generic_delete_inode,
30556 +       .show_options   = aufs_show_options,
30557 +       .statfs         = aufs_statfs,
30558 +       .put_super      = aufs_put_super,
30559 +       .sync_fs        = aufs_sync_fs
30560 +};
30561 +
30562 +/* ---------------------------------------------------------------------- */
30563 +
30564 +int au_alloc_root(struct super_block *sb)
30565 +{
30566 +       int err;
30567 +       struct inode *inode;
30568 +       struct dentry *root;
30569 +
30570 +       err = -ENOMEM;
30571 +       inode = au_iget_locked(sb, AUFS_ROOT_INO);
30572 +       err = PTR_ERR(inode);
30573 +       if (IS_ERR(inode))
30574 +               goto out;
30575 +
30576 +       inode->i_op = aufs_iop + AuIop_DIR; /* with getattr by default */
30577 +       inode->i_fop = &aufs_dir_fop;
30578 +       inode->i_mode = S_IFDIR;
30579 +       set_nlink(inode, 2);
30580 +       unlock_new_inode(inode);
30581 +
30582 +       root = d_make_root(inode);
30583 +       if (unlikely(!root))
30584 +               goto out;
30585 +       err = PTR_ERR(root);
30586 +       if (IS_ERR(root))
30587 +               goto out;
30588 +
30589 +       err = au_di_init(root);
30590 +       if (!err) {
30591 +               sb->s_root = root;
30592 +               return 0; /* success */
30593 +       }
30594 +       dput(root);
30595 +
30596 +out:
30597 +       return err;
30598 +}
30599 +
30600 +/* ---------------------------------------------------------------------- */
30601 +
30602 +static void aufs_kill_sb(struct super_block *sb)
30603 +{
30604 +       struct au_sbinfo *sbinfo;
30605 +       struct dentry *root;
30606 +
30607 +       sbinfo = au_sbi(sb);
30608 +       if (!sbinfo)
30609 +               goto out;
30610 +
30611 +       au_sbilist_del(sb);
30612 +
30613 +       root = sb->s_root;
30614 +       if (root)
30615 +               aufs_write_lock(root);
30616 +       else
30617 +               __si_write_lock(sb);
30618 +
30619 +       au_fhsm_fin(sb);
30620 +       if (sbinfo->si_wbr_create_ops->fin)
30621 +               sbinfo->si_wbr_create_ops->fin(sb);
30622 +       if (au_opt_test(sbinfo->si_mntflags, UDBA_HNOTIFY)) {
30623 +               au_opt_set_udba(sbinfo->si_mntflags, UDBA_NONE);
30624 +               au_remount_refresh(sb, /*do_idop*/0);
30625 +       }
30626 +       if (au_opt_test(sbinfo->si_mntflags, PLINK))
30627 +               au_plink_put(sb, /*verbose*/1);
30628 +       au_xino_clr(sb);
30629 +       if (root)
30630 +               au_dr_opt_flush(sb);
30631 +
30632 +       if (root)
30633 +               aufs_write_unlock(root);
30634 +       else
30635 +               __si_write_unlock(sb);
30636 +
30637 +       sbinfo->si_sb = NULL;
30638 +       au_nwt_flush(&sbinfo->si_nowait);
30639 +
30640 +out:
30641 +       kill_anon_super(sb);
30642 +}
30643 +
30644 +struct file_system_type aufs_fs_type = {
30645 +       .name           = AUFS_FSTYPE,
30646 +       /* a race between rename and others */
30647 +       .fs_flags       = FS_RENAME_DOES_D_MOVE
30648 +                               /* untested */
30649 +                               /*| FS_ALLOW_IDMAP*/
30650 +                               ,
30651 +       .init_fs_context = aufs_fsctx_init,
30652 +       .parameters     = aufs_fsctx_paramspec,
30653 +       .kill_sb        = aufs_kill_sb,
30654 +       /* no need to __module_get() and module_put(). */
30655 +       .owner          = THIS_MODULE,
30656 +};
30657 diff -urN /usr/share/empty/fs/aufs/super.h linux/fs/aufs/super.h
30658 --- /usr/share/empty/fs/aufs/super.h    1970-01-01 01:00:00.000000000 +0100
30659 +++ linux/fs/aufs/super.h       2022-03-21 14:49:05.729966343 +0100
30660 @@ -0,0 +1,592 @@
30661 +/* SPDX-License-Identifier: GPL-2.0 */
30662 +/*
30663 + * Copyright (C) 2005-2021 Junjiro R. Okajima
30664 + *
30665 + * This program, aufs is free software; you can redistribute it and/or modify
30666 + * it under the terms of the GNU General Public License as published by
30667 + * the Free Software Foundation; either version 2 of the License, or
30668 + * (at your option) any later version.
30669 + *
30670 + * This program is distributed in the hope that it will be useful,
30671 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
30672 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30673 + * GNU General Public License for more details.
30674 + *
30675 + * You should have received a copy of the GNU General Public License
30676 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
30677 + */
30678 +
30679 +/*
30680 + * super_block operations
30681 + */
30682 +
30683 +#ifndef __AUFS_SUPER_H__
30684 +#define __AUFS_SUPER_H__
30685 +
30686 +#ifdef __KERNEL__
30687 +
30688 +#include <linux/fs.h>
30689 +#include <linux/kobject.h>
30690 +#include "hbl.h"
30691 +#include "lcnt.h"
30692 +#include "rwsem.h"
30693 +#include "wkq.h"
30694 +
30695 +/* policies to select one among multiple writable branches */
30696 +struct au_wbr_copyup_operations {
30697 +       int (*copyup)(struct dentry *dentry);
30698 +};
30699 +
30700 +#define AuWbr_DIR      1               /* target is a dir */
30701 +#define AuWbr_PARENT   (1 << 1)        /* always require a parent */
30702 +
30703 +#define au_ftest_wbr(flags, name)      ((flags) & AuWbr_##name)
30704 +#define au_fset_wbr(flags, name)       { (flags) |= AuWbr_##name; }
30705 +#define au_fclr_wbr(flags, name)       { (flags) &= ~AuWbr_##name; }
30706 +
30707 +struct au_wbr_create_operations {
30708 +       int (*create)(struct dentry *dentry, unsigned int flags);
30709 +       int (*init)(struct super_block *sb);
30710 +       int (*fin)(struct super_block *sb);
30711 +};
30712 +
30713 +struct au_wbr_mfs {
30714 +       struct mutex    mfs_lock; /* protect this structure */
30715 +       unsigned long   mfs_jiffy;
30716 +       unsigned long   mfs_expire;
30717 +       aufs_bindex_t   mfs_bindex;
30718 +
30719 +       unsigned long long      mfsrr_bytes;
30720 +       unsigned long long      mfsrr_watermark;
30721 +};
30722 +
30723 +#define AuPlink_NHASH 100
30724 +static inline int au_plink_hash(ino_t ino)
30725 +{
30726 +       return ino % AuPlink_NHASH;
30727 +}
30728 +
30729 +/* File-based Hierarchical Storage Management */
30730 +struct au_fhsm {
30731 +#ifdef CONFIG_AUFS_FHSM
30732 +       /* allow only one process who can receive the notification */
30733 +       spinlock_t              fhsm_spin;
30734 +       pid_t                   fhsm_pid;
30735 +       wait_queue_head_t       fhsm_wqh;
30736 +       atomic_t                fhsm_readable;
30737 +
30738 +       /* these are protected by si_rwsem */
30739 +       unsigned long           fhsm_expire;
30740 +       aufs_bindex_t           fhsm_bottom;
30741 +#endif
30742 +};
30743 +
30744 +struct au_branch;
30745 +struct au_sbinfo {
30746 +       /* nowait tasks in the system-wide workqueue */
30747 +       struct au_nowait_tasks  si_nowait;
30748 +
30749 +       /*
30750 +        * tried sb->s_umount, but failed due to the dependency between i_mutex.
30751 +        * rwsem for au_sbinfo is necessary.
30752 +        */
30753 +       struct au_rwsem         si_rwsem;
30754 +
30755 +       /*
30756 +        * dirty approach to protect sb->sb_inodes and ->s_files (gone) from
30757 +        * remount.
30758 +        */
30759 +       au_lcnt_t               si_ninodes, si_nfiles;
30760 +
30761 +       /* branch management */
30762 +       unsigned int            si_generation;
30763 +
30764 +       /* see AuSi_ flags */
30765 +       unsigned char           au_si_status;
30766 +
30767 +       aufs_bindex_t           si_bbot;
30768 +
30769 +       /* dirty trick to keep br_id plus */
30770 +       unsigned int            si_last_br_id :
30771 +                               sizeof(aufs_bindex_t) * BITS_PER_BYTE - 1;
30772 +       struct au_branch        **si_branch;
30773 +
30774 +       /* policy to select a writable branch */
30775 +       unsigned char           si_wbr_copyup;
30776 +       unsigned char           si_wbr_create;
30777 +       struct au_wbr_copyup_operations *si_wbr_copyup_ops;
30778 +       struct au_wbr_create_operations *si_wbr_create_ops;
30779 +
30780 +       /* round robin */
30781 +       atomic_t                si_wbr_rr_next;
30782 +
30783 +       /* most free space */
30784 +       struct au_wbr_mfs       si_wbr_mfs;
30785 +
30786 +       /* File-based Hierarchical Storage Management */
30787 +       struct au_fhsm          si_fhsm;
30788 +
30789 +       /* mount flags */
30790 +       /* include/asm-ia64/siginfo.h defines a macro named si_flags */
30791 +       unsigned int            si_mntflags;
30792 +
30793 +       /* external inode number (bitmap and translation table) */
30794 +       loff_t                  si_ximaxent;    /* max entries in a xino */
30795 +
30796 +       struct file             *si_xib;
30797 +       struct mutex            si_xib_mtx; /* protect xib members */
30798 +       unsigned long           *si_xib_buf;
30799 +       unsigned long           si_xib_last_pindex;
30800 +       int                     si_xib_next_bit;
30801 +
30802 +       unsigned long           si_xino_jiffy;
30803 +       unsigned long           si_xino_expire;
30804 +       /* reserved for future use */
30805 +       /* unsigned long long   si_xib_limit; */        /* Max xib file size */
30806 +
30807 +#ifdef CONFIG_AUFS_EXPORT
30808 +       /* i_generation */
30809 +       /* todo: make xigen file an array to support many inode numbers */
30810 +       struct file             *si_xigen;
30811 +       atomic_t                si_xigen_next;
30812 +#endif
30813 +
30814 +       /* dirty trick to support atomic_open */
30815 +       struct hlist_bl_head    si_aopen;
30816 +
30817 +       /* vdir parameters */
30818 +       unsigned long           si_rdcache;     /* max cache time in jiffies */
30819 +       unsigned int            si_rdblk;       /* deblk size */
30820 +       unsigned int            si_rdhash;      /* hash size */
30821 +
30822 +       /*
30823 +        * If the number of whiteouts are larger than si_dirwh, leave all of
30824 +        * them after au_whtmp_ren to reduce the cost of rmdir(2).
30825 +        * future fsck.aufs or kernel thread will remove them later.
30826 +        * Otherwise, remove all whiteouts and the dir in rmdir(2).
30827 +        */
30828 +       unsigned int            si_dirwh;
30829 +
30830 +       /* pseudo_link list */
30831 +       struct hlist_bl_head    si_plink[AuPlink_NHASH];
30832 +       wait_queue_head_t       si_plink_wq;
30833 +       spinlock_t              si_plink_maint_lock;
30834 +       pid_t                   si_plink_maint_pid;
30835 +
30836 +       /* file list */
30837 +       struct hlist_bl_head    si_files;
30838 +
30839 +       /* with/without getattr, brother of sb->s_d_op */
30840 +       const struct inode_operations *si_iop_array;
30841 +
30842 +       /*
30843 +        * sysfs and lifetime management.
30844 +        * this is not a small structure and it may be a waste of memory in case
30845 +        * of sysfs is disabled, particularly when many aufs-es are mounted.
30846 +        * but using sysfs is majority.
30847 +        */
30848 +       struct kobject          si_kobj;
30849 +#ifdef CONFIG_DEBUG_FS
30850 +       struct dentry            *si_dbgaufs;
30851 +       struct dentry            *si_dbgaufs_plink;
30852 +       struct dentry            *si_dbgaufs_xib;
30853 +#ifdef CONFIG_AUFS_EXPORT
30854 +       struct dentry            *si_dbgaufs_xigen;
30855 +#endif
30856 +#endif
30857 +
30858 +#ifdef CONFIG_AUFS_SBILIST
30859 +       struct hlist_bl_node    si_list;
30860 +#endif
30861 +
30862 +       /* dirty, necessary for unmounting, sysfs and sysrq */
30863 +       struct super_block      *si_sb;
30864 +};
30865 +
30866 +/* sbinfo status flags */
30867 +/*
30868 + * set true when refresh_dirs() failed at remount time.
30869 + * then try refreshing dirs at access time again.
30870 + * if it is false, refreshing dirs at access time is unnecessary
30871 + */
30872 +#define AuSi_FAILED_REFRESH_DIR        1
30873 +#define AuSi_FHSM              (1 << 1)        /* fhsm is active now */
30874 +#define AuSi_NO_DREVAL         (1 << 2)        /* disable all d_revalidate */
30875 +
30876 +#ifndef CONFIG_AUFS_FHSM
30877 +#undef AuSi_FHSM
30878 +#define AuSi_FHSM              0
30879 +#endif
30880 +
30881 +static inline unsigned char au_do_ftest_si(struct au_sbinfo *sbi,
30882 +                                          unsigned int flag)
30883 +{
30884 +       AuRwMustAnyLock(&sbi->si_rwsem);
30885 +       return sbi->au_si_status & flag;
30886 +}
30887 +#define au_ftest_si(sbinfo, name)      au_do_ftest_si(sbinfo, AuSi_##name)
30888 +#define au_fset_si(sbinfo, name) do { \
30889 +       AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
30890 +       (sbinfo)->au_si_status |= AuSi_##name; \
30891 +} while (0)
30892 +#define au_fclr_si(sbinfo, name) do { \
30893 +       AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
30894 +       (sbinfo)->au_si_status &= ~AuSi_##name; \
30895 +} while (0)
30896 +
30897 +/* ---------------------------------------------------------------------- */
30898 +
30899 +/* policy to select one among writable branches */
30900 +#define AuWbrCopyup(sbinfo, ...) \
30901 +       ((sbinfo)->si_wbr_copyup_ops->copyup(__VA_ARGS__))
30902 +#define AuWbrCreate(sbinfo, ...) \
30903 +       ((sbinfo)->si_wbr_create_ops->create(__VA_ARGS__))
30904 +
30905 +/* flags for si_read_lock()/aufs_read_lock()/di_read_lock() */
30906 +#define AuLock_DW              1               /* write-lock dentry */
30907 +#define AuLock_IR              (1 << 1)        /* read-lock inode */
30908 +#define AuLock_IW              (1 << 2)        /* write-lock inode */
30909 +#define AuLock_FLUSH           (1 << 3)        /* wait for 'nowait' tasks */
30910 +#define AuLock_DIRS            (1 << 4)        /* target is a pair of dirs */
30911 +                                               /* except RENAME_EXCHANGE */
30912 +#define AuLock_NOPLM           (1 << 5)        /* return err in plm mode */
30913 +#define AuLock_NOPLMW          (1 << 6)        /* wait for plm mode ends */
30914 +#define AuLock_GEN             (1 << 7)        /* test digen/iigen */
30915 +#define au_ftest_lock(flags, name)     ((flags) & AuLock_##name)
30916 +#define au_fset_lock(flags, name) \
30917 +       do { (flags) |= AuLock_##name; } while (0)
30918 +#define au_fclr_lock(flags, name) \
30919 +       do { (flags) &= ~AuLock_##name; } while (0)
30920 +
30921 +/* ---------------------------------------------------------------------- */
30922 +
30923 +/* super.c */
30924 +struct inode *au_iget_locked(struct super_block *sb, ino_t ino);
30925 +
30926 +typedef unsigned long long (*au_arraycb_t)(struct super_block *sb, void *array,
30927 +                                          unsigned long long max, void *arg);
30928 +void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb,
30929 +                    struct super_block *sb, void *arg);
30930 +struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max);
30931 +void au_iarray_free(struct inode **a, unsigned long long max);
30932 +
30933 +void au_remount_refresh(struct super_block *sb, unsigned int do_idop);
30934 +extern const struct super_operations aufs_sop;
30935 +int au_alloc_root(struct super_block *sb);
30936 +extern struct file_system_type aufs_fs_type;
30937 +
30938 +/* sbinfo.c */
30939 +void au_si_free(struct kobject *kobj);
30940 +struct au_sbinfo *au_si_alloc(struct super_block *sb);
30941 +int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr, int may_shrink);
30942 +
30943 +unsigned int au_sigen_inc(struct super_block *sb);
30944 +aufs_bindex_t au_new_br_id(struct super_block *sb);
30945 +
30946 +int si_read_lock(struct super_block *sb, int flags);
30947 +int si_write_lock(struct super_block *sb, int flags);
30948 +int aufs_read_lock(struct dentry *dentry, int flags);
30949 +void aufs_read_unlock(struct dentry *dentry, int flags);
30950 +void aufs_write_lock(struct dentry *dentry);
30951 +void aufs_write_unlock(struct dentry *dentry);
30952 +int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags);
30953 +void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2);
30954 +
30955 +/* wbr_policy.c */
30956 +extern struct au_wbr_copyup_operations au_wbr_copyup_ops[];
30957 +extern struct au_wbr_create_operations au_wbr_create_ops[];
30958 +int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst);
30959 +int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex);
30960 +int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t btop);
30961 +
30962 +/* mvdown.c */
30963 +int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *arg);
30964 +
30965 +#ifdef CONFIG_AUFS_FHSM
30966 +/* fhsm.c */
30967 +
30968 +static inline pid_t au_fhsm_pid(struct au_fhsm *fhsm)
30969 +{
30970 +       pid_t pid;
30971 +
30972 +       spin_lock(&fhsm->fhsm_spin);
30973 +       pid = fhsm->fhsm_pid;
30974 +       spin_unlock(&fhsm->fhsm_spin);
30975 +
30976 +       return pid;
30977 +}
30978 +
30979 +void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force);
30980 +void au_fhsm_wrote_all(struct super_block *sb, int force);
30981 +int au_fhsm_fd(struct super_block *sb, int oflags);
30982 +int au_fhsm_br_alloc(struct au_branch *br);
30983 +void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex);
30984 +void au_fhsm_fin(struct super_block *sb);
30985 +void au_fhsm_init(struct au_sbinfo *sbinfo);
30986 +void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec);
30987 +void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo);
30988 +#else
30989 +AuStubVoid(au_fhsm_wrote, struct super_block *sb, aufs_bindex_t bindex,
30990 +          int force)
30991 +AuStubVoid(au_fhsm_wrote_all, struct super_block *sb, int force)
30992 +AuStub(int, au_fhsm_fd, return -EOPNOTSUPP, struct super_block *sb, int oflags)
30993 +AuStub(pid_t, au_fhsm_pid, return 0, struct au_fhsm *fhsm)
30994 +AuStubInt0(au_fhsm_br_alloc, struct au_branch *br)
30995 +AuStubVoid(au_fhsm_set_bottom, struct super_block *sb, aufs_bindex_t bindex)
30996 +AuStubVoid(au_fhsm_fin, struct super_block *sb)
30997 +AuStubVoid(au_fhsm_init, struct au_sbinfo *sbinfo)
30998 +AuStubVoid(au_fhsm_set, struct au_sbinfo *sbinfo, unsigned int sec)
30999 +AuStubVoid(au_fhsm_show, struct seq_file *seq, struct au_sbinfo *sbinfo)
31000 +#endif
31001 +
31002 +/* ---------------------------------------------------------------------- */
31003 +
31004 +static inline struct au_sbinfo *au_sbi(struct super_block *sb)
31005 +{
31006 +       return sb->s_fs_info;
31007 +}
31008 +
31009 +/* ---------------------------------------------------------------------- */
31010 +
31011 +#ifdef CONFIG_AUFS_EXPORT
31012 +int au_test_nfsd(void);
31013 +void au_export_init(struct super_block *sb);
31014 +void au_xigen_inc(struct inode *inode);
31015 +int au_xigen_new(struct inode *inode);
31016 +int au_xigen_set(struct super_block *sb, struct path *path);
31017 +void au_xigen_clr(struct super_block *sb);
31018 +
31019 +static inline int au_busy_or_stale(void)
31020 +{
31021 +       if (!au_test_nfsd())
31022 +               return -EBUSY;
31023 +       return -ESTALE;
31024 +}
31025 +#else
31026 +AuStubInt0(au_test_nfsd, void)
31027 +AuStubVoid(au_export_init, struct super_block *sb)
31028 +AuStubVoid(au_xigen_inc, struct inode *inode)
31029 +AuStubInt0(au_xigen_new, struct inode *inode)
31030 +AuStubInt0(au_xigen_set, struct super_block *sb, struct path *path)
31031 +AuStubVoid(au_xigen_clr, struct super_block *sb)
31032 +AuStub(int, au_busy_or_stale, return -EBUSY, void)
31033 +#endif /* CONFIG_AUFS_EXPORT */
31034 +
31035 +/* ---------------------------------------------------------------------- */
31036 +
31037 +#ifdef CONFIG_AUFS_SBILIST
31038 +/* module.c */
31039 +extern struct hlist_bl_head au_sbilist;
31040 +
31041 +static inline void au_sbilist_init(void)
31042 +{
31043 +       INIT_HLIST_BL_HEAD(&au_sbilist);
31044 +}
31045 +
31046 +static inline void au_sbilist_add(struct super_block *sb)
31047 +{
31048 +       au_hbl_add(&au_sbi(sb)->si_list, &au_sbilist);
31049 +}
31050 +
31051 +static inline void au_sbilist_del(struct super_block *sb)
31052 +{
31053 +       au_hbl_del(&au_sbi(sb)->si_list, &au_sbilist);
31054 +}
31055 +
31056 +#ifdef CONFIG_AUFS_MAGIC_SYSRQ
31057 +static inline void au_sbilist_lock(void)
31058 +{
31059 +       hlist_bl_lock(&au_sbilist);
31060 +}
31061 +
31062 +static inline void au_sbilist_unlock(void)
31063 +{
31064 +       hlist_bl_unlock(&au_sbilist);
31065 +}
31066 +#define AuGFP_SBILIST  GFP_ATOMIC
31067 +#else
31068 +AuStubVoid(au_sbilist_lock, void)
31069 +AuStubVoid(au_sbilist_unlock, void)
31070 +#define AuGFP_SBILIST  GFP_NOFS
31071 +#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
31072 +#else
31073 +AuStubVoid(au_sbilist_init, void)
31074 +AuStubVoid(au_sbilist_add, struct super_block *sb)
31075 +AuStubVoid(au_sbilist_del, struct super_block *sb)
31076 +AuStubVoid(au_sbilist_lock, void)
31077 +AuStubVoid(au_sbilist_unlock, void)
31078 +#define AuGFP_SBILIST  GFP_NOFS
31079 +#endif
31080 +
31081 +/* ---------------------------------------------------------------------- */
31082 +
31083 +static inline void dbgaufs_si_null(struct au_sbinfo *sbinfo)
31084 +{
31085 +       /*
31086 +        * This function is a dynamic '__init' function actually,
31087 +        * so the tiny check for si_rwsem is unnecessary.
31088 +        */
31089 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
31090 +#ifdef CONFIG_DEBUG_FS
31091 +       sbinfo->si_dbgaufs = NULL;
31092 +       sbinfo->si_dbgaufs_plink = NULL;
31093 +       sbinfo->si_dbgaufs_xib = NULL;
31094 +#ifdef CONFIG_AUFS_EXPORT
31095 +       sbinfo->si_dbgaufs_xigen = NULL;
31096 +#endif
31097 +#endif
31098 +}
31099 +
31100 +/* ---------------------------------------------------------------------- */
31101 +
31102 +/* current->atomic_flags */
31103 +/* this value should never corrupt the ones defined in linux/sched.h */
31104 +#define PFA_AUFS       0x10
31105 +
31106 +TASK_PFA_TEST(AUFS, test_aufs) /* task_test_aufs */
31107 +TASK_PFA_SET(AUFS, aufs)       /* task_set_aufs */
31108 +TASK_PFA_CLEAR(AUFS, aufs)     /* task_clear_aufs */
31109 +
31110 +static inline int si_pid_test(struct super_block *sb)
31111 +{
31112 +       return !!task_test_aufs(current);
31113 +}
31114 +
31115 +static inline void si_pid_clr(struct super_block *sb)
31116 +{
31117 +       AuDebugOn(!task_test_aufs(current));
31118 +       task_clear_aufs(current);
31119 +}
31120 +
31121 +static inline void si_pid_set(struct super_block *sb)
31122 +{
31123 +       AuDebugOn(task_test_aufs(current));
31124 +       task_set_aufs(current);
31125 +}
31126 +
31127 +/* ---------------------------------------------------------------------- */
31128 +
31129 +/* lock superblock. mainly for entry point functions */
31130 +#define __si_read_lock(sb)     au_rw_read_lock(&au_sbi(sb)->si_rwsem)
31131 +#define __si_write_lock(sb)    au_rw_write_lock(&au_sbi(sb)->si_rwsem)
31132 +#define __si_read_trylock(sb)  au_rw_read_trylock(&au_sbi(sb)->si_rwsem)
31133 +#define __si_write_trylock(sb) au_rw_write_trylock(&au_sbi(sb)->si_rwsem)
31134 +/*
31135 +#define __si_read_trylock_nested(sb) \
31136 +       au_rw_read_trylock_nested(&au_sbi(sb)->si_rwsem)
31137 +#define __si_write_trylock_nested(sb) \
31138 +       au_rw_write_trylock_nested(&au_sbi(sb)->si_rwsem)
31139 +*/
31140 +
31141 +#define __si_read_unlock(sb)   au_rw_read_unlock(&au_sbi(sb)->si_rwsem)
31142 +#define __si_write_unlock(sb)  au_rw_write_unlock(&au_sbi(sb)->si_rwsem)
31143 +#define __si_downgrade_lock(sb)        au_rw_dgrade_lock(&au_sbi(sb)->si_rwsem)
31144 +
31145 +#define SiMustNoWaiters(sb)    AuRwMustNoWaiters(&au_sbi(sb)->si_rwsem)
31146 +#define SiMustAnyLock(sb)      AuRwMustAnyLock(&au_sbi(sb)->si_rwsem)
31147 +#define SiMustWriteLock(sb)    AuRwMustWriteLock(&au_sbi(sb)->si_rwsem)
31148 +
31149 +static inline void si_noflush_read_lock(struct super_block *sb)
31150 +{
31151 +       __si_read_lock(sb);
31152 +       si_pid_set(sb);
31153 +}
31154 +
31155 +static inline int si_noflush_read_trylock(struct super_block *sb)
31156 +{
31157 +       int locked;
31158 +
31159 +       locked = __si_read_trylock(sb);
31160 +       if (locked)
31161 +               si_pid_set(sb);
31162 +       return locked;
31163 +}
31164 +
31165 +static inline void si_noflush_write_lock(struct super_block *sb)
31166 +{
31167 +       __si_write_lock(sb);
31168 +       si_pid_set(sb);
31169 +}
31170 +
31171 +static inline int si_noflush_write_trylock(struct super_block *sb)
31172 +{
31173 +       int locked;
31174 +
31175 +       locked = __si_write_trylock(sb);
31176 +       if (locked)
31177 +               si_pid_set(sb);
31178 +       return locked;
31179 +}
31180 +
31181 +#if 0 /* reserved */
31182 +static inline int si_read_trylock(struct super_block *sb, int flags)
31183 +{
31184 +       if (au_ftest_lock(flags, FLUSH))
31185 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
31186 +       return si_noflush_read_trylock(sb);
31187 +}
31188 +#endif
31189 +
31190 +static inline void si_read_unlock(struct super_block *sb)
31191 +{
31192 +       si_pid_clr(sb);
31193 +       __si_read_unlock(sb);
31194 +}
31195 +
31196 +#if 0 /* reserved */
31197 +static inline int si_write_trylock(struct super_block *sb, int flags)
31198 +{
31199 +       if (au_ftest_lock(flags, FLUSH))
31200 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
31201 +       return si_noflush_write_trylock(sb);
31202 +}
31203 +#endif
31204 +
31205 +static inline void si_write_unlock(struct super_block *sb)
31206 +{
31207 +       si_pid_clr(sb);
31208 +       __si_write_unlock(sb);
31209 +}
31210 +
31211 +#if 0 /* reserved */
31212 +static inline void si_downgrade_lock(struct super_block *sb)
31213 +{
31214 +       __si_downgrade_lock(sb);
31215 +}
31216 +#endif
31217 +
31218 +/* ---------------------------------------------------------------------- */
31219 +
31220 +static inline aufs_bindex_t au_sbbot(struct super_block *sb)
31221 +{
31222 +       SiMustAnyLock(sb);
31223 +       return au_sbi(sb)->si_bbot;
31224 +}
31225 +
31226 +static inline unsigned int au_mntflags(struct super_block *sb)
31227 +{
31228 +       SiMustAnyLock(sb);
31229 +       return au_sbi(sb)->si_mntflags;
31230 +}
31231 +
31232 +static inline unsigned int au_sigen(struct super_block *sb)
31233 +{
31234 +       SiMustAnyLock(sb);
31235 +       return au_sbi(sb)->si_generation;
31236 +}
31237 +
31238 +static inline struct au_branch *au_sbr(struct super_block *sb,
31239 +                                      aufs_bindex_t bindex)
31240 +{
31241 +       SiMustAnyLock(sb);
31242 +       return au_sbi(sb)->si_branch[0 + bindex];
31243 +}
31244 +
31245 +static inline loff_t au_xi_maxent(struct super_block *sb)
31246 +{
31247 +       SiMustAnyLock(sb);
31248 +       return au_sbi(sb)->si_ximaxent;
31249 +}
31250 +
31251 +#endif /* __KERNEL__ */
31252 +#endif /* __AUFS_SUPER_H__ */
31253 diff -urN /usr/share/empty/fs/aufs/sysaufs.c linux/fs/aufs/sysaufs.c
31254 --- /usr/share/empty/fs/aufs/sysaufs.c  1970-01-01 01:00:00.000000000 +0100
31255 +++ linux/fs/aufs/sysaufs.c     2022-03-21 14:49:05.729966343 +0100
31256 @@ -0,0 +1,93 @@
31257 +// SPDX-License-Identifier: GPL-2.0
31258 +/*
31259 + * Copyright (C) 2005-2021 Junjiro R. Okajima
31260 + *
31261 + * This program, aufs is free software; you can redistribute it and/or modify
31262 + * it under the terms of the GNU General Public License as published by
31263 + * the Free Software Foundation; either version 2 of the License, or
31264 + * (at your option) any later version.
31265 + *
31266 + * This program is distributed in the hope that it will be useful,
31267 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31268 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31269 + * GNU General Public License for more details.
31270 + *
31271 + * You should have received a copy of the GNU General Public License
31272 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31273 + */
31274 +
31275 +/*
31276 + * sysfs interface and lifetime management
31277 + * they are necessary regardless sysfs is disabled.
31278 + */
31279 +
31280 +#include <linux/random.h>
31281 +#include "aufs.h"
31282 +
31283 +unsigned long sysaufs_si_mask;
31284 +struct kset *sysaufs_kset;
31285 +
31286 +#define AuSiAttr(_name) { \
31287 +       .attr   = { .name = __stringify(_name), .mode = 0444 }, \
31288 +       .show   = sysaufs_si_##_name,                           \
31289 +}
31290 +
31291 +static struct sysaufs_si_attr sysaufs_si_attr_xi_path = AuSiAttr(xi_path);
31292 +struct attribute *sysaufs_si_attrs[] = {
31293 +       &sysaufs_si_attr_xi_path.attr,
31294 +       NULL,
31295 +};
31296 +
31297 +static const struct sysfs_ops au_sbi_ops = {
31298 +       .show   = sysaufs_si_show
31299 +};
31300 +
31301 +static struct kobj_type au_sbi_ktype = {
31302 +       .release        = au_si_free,
31303 +       .sysfs_ops      = &au_sbi_ops,
31304 +       .default_attrs  = sysaufs_si_attrs
31305 +};
31306 +
31307 +/* ---------------------------------------------------------------------- */
31308 +
31309 +int sysaufs_si_init(struct au_sbinfo *sbinfo)
31310 +{
31311 +       int err;
31312 +
31313 +       sbinfo->si_kobj.kset = sysaufs_kset;
31314 +       /* cf. sysaufs_name() */
31315 +       err = kobject_init_and_add
31316 +               (&sbinfo->si_kobj, &au_sbi_ktype, /*&sysaufs_kset->kobj*/NULL,
31317 +                SysaufsSiNamePrefix "%lx", sysaufs_si_id(sbinfo));
31318 +
31319 +       return err;
31320 +}
31321 +
31322 +void sysaufs_fin(void)
31323 +{
31324 +       sysfs_remove_group(&sysaufs_kset->kobj, sysaufs_attr_group);
31325 +       kset_unregister(sysaufs_kset);
31326 +}
31327 +
31328 +int __init sysaufs_init(void)
31329 +{
31330 +       int err;
31331 +
31332 +       do {
31333 +               get_random_bytes(&sysaufs_si_mask, sizeof(sysaufs_si_mask));
31334 +       } while (!sysaufs_si_mask);
31335 +
31336 +       err = -EINVAL;
31337 +       sysaufs_kset = kset_create_and_add(AUFS_NAME, NULL, fs_kobj);
31338 +       if (unlikely(!sysaufs_kset))
31339 +               goto out;
31340 +       err = PTR_ERR(sysaufs_kset);
31341 +       if (IS_ERR(sysaufs_kset))
31342 +               goto out;
31343 +       err = sysfs_create_group(&sysaufs_kset->kobj, sysaufs_attr_group);
31344 +       if (unlikely(err))
31345 +               kset_unregister(sysaufs_kset);
31346 +
31347 +out:
31348 +       return err;
31349 +}
31350 diff -urN /usr/share/empty/fs/aufs/sysaufs.h linux/fs/aufs/sysaufs.h
31351 --- /usr/share/empty/fs/aufs/sysaufs.h  1970-01-01 01:00:00.000000000 +0100
31352 +++ linux/fs/aufs/sysaufs.h     2022-03-21 14:49:05.729966343 +0100
31353 @@ -0,0 +1,102 @@
31354 +/* SPDX-License-Identifier: GPL-2.0 */
31355 +/*
31356 + * Copyright (C) 2005-2021 Junjiro R. Okajima
31357 + *
31358 + * This program, aufs is free software; you can redistribute it and/or modify
31359 + * it under the terms of the GNU General Public License as published by
31360 + * the Free Software Foundation; either version 2 of the License, or
31361 + * (at your option) any later version.
31362 + *
31363 + * This program is distributed in the hope that it will be useful,
31364 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31365 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31366 + * GNU General Public License for more details.
31367 + *
31368 + * You should have received a copy of the GNU General Public License
31369 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31370 + */
31371 +
31372 +/*
31373 + * sysfs interface and mount lifetime management
31374 + */
31375 +
31376 +#ifndef __SYSAUFS_H__
31377 +#define __SYSAUFS_H__
31378 +
31379 +#ifdef __KERNEL__
31380 +
31381 +#include <linux/sysfs.h>
31382 +#include "module.h"
31383 +
31384 +struct super_block;
31385 +struct au_sbinfo;
31386 +
31387 +struct sysaufs_si_attr {
31388 +       struct attribute attr;
31389 +       int (*show)(struct seq_file *seq, struct super_block *sb);
31390 +};
31391 +
31392 +/* ---------------------------------------------------------------------- */
31393 +
31394 +/* sysaufs.c */
31395 +extern unsigned long sysaufs_si_mask;
31396 +extern struct kset *sysaufs_kset;
31397 +extern struct attribute *sysaufs_si_attrs[];
31398 +int sysaufs_si_init(struct au_sbinfo *sbinfo);
31399 +int __init sysaufs_init(void);
31400 +void sysaufs_fin(void);
31401 +
31402 +/* ---------------------------------------------------------------------- */
31403 +
31404 +/* some people doesn't like to show a pointer in kernel */
31405 +static inline unsigned long sysaufs_si_id(struct au_sbinfo *sbinfo)
31406 +{
31407 +       return sysaufs_si_mask ^ (unsigned long)sbinfo;
31408 +}
31409 +
31410 +#define SysaufsSiNamePrefix    "si_"
31411 +#define SysaufsSiNameLen       (sizeof(SysaufsSiNamePrefix) + 16)
31412 +static inline void sysaufs_name(struct au_sbinfo *sbinfo, char *name)
31413 +{
31414 +       snprintf(name, SysaufsSiNameLen, SysaufsSiNamePrefix "%lx",
31415 +                sysaufs_si_id(sbinfo));
31416 +}
31417 +
31418 +struct au_branch;
31419 +#ifdef CONFIG_SYSFS
31420 +/* sysfs.c */
31421 +extern struct attribute_group *sysaufs_attr_group;
31422 +
31423 +int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb);
31424 +ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
31425 +                        char *buf);
31426 +long au_brinfo_ioctl(struct file *file, unsigned long arg);
31427 +#ifdef CONFIG_COMPAT
31428 +long au_brinfo_compat_ioctl(struct file *file, unsigned long arg);
31429 +#endif
31430 +
31431 +void sysaufs_br_init(struct au_branch *br);
31432 +void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex);
31433 +void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
31434 +
31435 +#define sysaufs_brs_init()     do {} while (0)
31436 +
31437 +#else
31438 +#define sysaufs_attr_group     NULL
31439 +
31440 +AuStubInt0(sysaufs_si_xi_path, struct seq_file *seq, struct super_block *sb)
31441 +AuStub(ssize_t, sysaufs_si_show, return 0, struct kobject *kobj,
31442 +       struct attribute *attr, char *buf)
31443 +AuStubVoid(sysaufs_br_init, struct au_branch *br)
31444 +AuStubVoid(sysaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex)
31445 +AuStubVoid(sysaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
31446 +
31447 +static inline void sysaufs_brs_init(void)
31448 +{
31449 +       sysaufs_brs = 0;
31450 +}
31451 +
31452 +#endif /* CONFIG_SYSFS */
31453 +
31454 +#endif /* __KERNEL__ */
31455 +#endif /* __SYSAUFS_H__ */
31456 diff -urN /usr/share/empty/fs/aufs/sysfs.c linux/fs/aufs/sysfs.c
31457 --- /usr/share/empty/fs/aufs/sysfs.c    1970-01-01 01:00:00.000000000 +0100
31458 +++ linux/fs/aufs/sysfs.c       2022-03-21 14:49:05.729966343 +0100
31459 @@ -0,0 +1,374 @@
31460 +// SPDX-License-Identifier: GPL-2.0
31461 +/*
31462 + * Copyright (C) 2005-2021 Junjiro R. Okajima
31463 + *
31464 + * This program, aufs is free software; you can redistribute it and/or modify
31465 + * it under the terms of the GNU General Public License as published by
31466 + * the Free Software Foundation; either version 2 of the License, or
31467 + * (at your option) any later version.
31468 + *
31469 + * This program is distributed in the hope that it will be useful,
31470 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31471 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31472 + * GNU General Public License for more details.
31473 + *
31474 + * You should have received a copy of the GNU General Public License
31475 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31476 + */
31477 +
31478 +/*
31479 + * sysfs interface
31480 + */
31481 +
31482 +#include <linux/compat.h>
31483 +#include <linux/seq_file.h>
31484 +#include "aufs.h"
31485 +
31486 +#ifdef CONFIG_AUFS_FS_MODULE
31487 +/* this entry violates the "one line per file" policy of sysfs */
31488 +static ssize_t config_show(struct kobject *kobj, struct kobj_attribute *attr,
31489 +                          char *buf)
31490 +{
31491 +       ssize_t err;
31492 +       static char *conf =
31493 +/* this file is generated at compiling */
31494 +#include "conf.str"
31495 +               ;
31496 +
31497 +       err = snprintf(buf, PAGE_SIZE, conf);
31498 +       if (unlikely(err >= PAGE_SIZE))
31499 +               err = -EFBIG;
31500 +       return err;
31501 +}
31502 +
31503 +static struct kobj_attribute au_config_attr = __ATTR_RO(config);
31504 +#endif
31505 +
31506 +static struct attribute *au_attr[] = {
31507 +#ifdef CONFIG_AUFS_FS_MODULE
31508 +       &au_config_attr.attr,
31509 +#endif
31510 +       NULL,   /* need to NULL terminate the list of attributes */
31511 +};
31512 +
31513 +static struct attribute_group sysaufs_attr_group_body = {
31514 +       .attrs = au_attr
31515 +};
31516 +
31517 +struct attribute_group *sysaufs_attr_group = &sysaufs_attr_group_body;
31518 +
31519 +/* ---------------------------------------------------------------------- */
31520 +
31521 +int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb)
31522 +{
31523 +       int err;
31524 +
31525 +       SiMustAnyLock(sb);
31526 +
31527 +       err = 0;
31528 +       if (au_opt_test(au_mntflags(sb), XINO)) {
31529 +               err = au_xino_path(seq, au_sbi(sb)->si_xib);
31530 +               seq_putc(seq, '\n');
31531 +       }
31532 +       return err;
31533 +}
31534 +
31535 +/*
31536 + * the lifetime of branch is independent from the entry under sysfs.
31537 + * sysfs handles the lifetime of the entry, and never call ->show() after it is
31538 + * unlinked.
31539 + */
31540 +static int sysaufs_si_br(struct seq_file *seq, struct super_block *sb,
31541 +                        aufs_bindex_t bindex, int idx)
31542 +{
31543 +       int err;
31544 +       struct path path;
31545 +       struct dentry *root;
31546 +       struct au_branch *br;
31547 +       au_br_perm_str_t perm;
31548 +
31549 +       AuDbg("b%d\n", bindex);
31550 +
31551 +       err = 0;
31552 +       root = sb->s_root;
31553 +       di_read_lock_parent(root, !AuLock_IR);
31554 +       br = au_sbr(sb, bindex);
31555 +
31556 +       switch (idx) {
31557 +       case AuBrSysfs_BR:
31558 +               path.mnt = au_br_mnt(br);
31559 +               path.dentry = au_h_dptr(root, bindex);
31560 +               err = au_seq_path(seq, &path);
31561 +               if (!err) {
31562 +                       au_optstr_br_perm(&perm, br->br_perm);
31563 +                       seq_printf(seq, "=%s\n", perm.a);
31564 +               }
31565 +               break;
31566 +       case AuBrSysfs_BRID:
31567 +               seq_printf(seq, "%d\n", br->br_id);
31568 +               break;
31569 +       }
31570 +       di_read_unlock(root, !AuLock_IR);
31571 +       if (unlikely(err || seq_has_overflowed(seq)))
31572 +               err = -E2BIG;
31573 +
31574 +       return err;
31575 +}
31576 +
31577 +/* ---------------------------------------------------------------------- */
31578 +
31579 +static struct seq_file *au_seq(char *p, ssize_t len)
31580 +{
31581 +       struct seq_file *seq;
31582 +
31583 +       seq = kzalloc(sizeof(*seq), GFP_NOFS);
31584 +       if (seq) {
31585 +               /* mutex_init(&seq.lock); */
31586 +               seq->buf = p;
31587 +               seq->size = len;
31588 +               return seq; /* success */
31589 +       }
31590 +
31591 +       seq = ERR_PTR(-ENOMEM);
31592 +       return seq;
31593 +}
31594 +
31595 +#define SysaufsBr_PREFIX       "br"
31596 +#define SysaufsBrid_PREFIX     "brid"
31597 +
31598 +/* todo: file size may exceed PAGE_SIZE */
31599 +ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
31600 +                       char *buf)
31601 +{
31602 +       ssize_t err;
31603 +       int idx;
31604 +       long l;
31605 +       aufs_bindex_t bbot;
31606 +       struct au_sbinfo *sbinfo;
31607 +       struct super_block *sb;
31608 +       struct seq_file *seq;
31609 +       char *name;
31610 +       struct attribute **cattr;
31611 +
31612 +       sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
31613 +       sb = sbinfo->si_sb;
31614 +
31615 +       /*
31616 +        * prevent a race condition between sysfs and aufs.
31617 +        * for instance, sysfs_file_read() calls sysfs_get_active_two() which
31618 +        * prohibits maintaining the sysfs entries.
31619 +        * hew we acquire read lock after sysfs_get_active_two().
31620 +        * on the other hand, the remount process may maintain the sysfs/aufs
31621 +        * entries after acquiring write lock.
31622 +        * it can cause a deadlock.
31623 +        * simply we gave up processing read here.
31624 +        */
31625 +       err = -EBUSY;
31626 +       if (unlikely(!si_noflush_read_trylock(sb)))
31627 +               goto out;
31628 +
31629 +       seq = au_seq(buf, PAGE_SIZE);
31630 +       err = PTR_ERR(seq);
31631 +       if (IS_ERR(seq))
31632 +               goto out_unlock;
31633 +
31634 +       name = (void *)attr->name;
31635 +       cattr = sysaufs_si_attrs;
31636 +       while (*cattr) {
31637 +               if (!strcmp(name, (*cattr)->name)) {
31638 +                       err = container_of(*cattr, struct sysaufs_si_attr, attr)
31639 +                               ->show(seq, sb);
31640 +                       goto out_seq;
31641 +               }
31642 +               cattr++;
31643 +       }
31644 +
31645 +       if (!strncmp(name, SysaufsBrid_PREFIX,
31646 +                    sizeof(SysaufsBrid_PREFIX) - 1)) {
31647 +               idx = AuBrSysfs_BRID;
31648 +               name += sizeof(SysaufsBrid_PREFIX) - 1;
31649 +       } else if (!strncmp(name, SysaufsBr_PREFIX,
31650 +                           sizeof(SysaufsBr_PREFIX) - 1)) {
31651 +               idx = AuBrSysfs_BR;
31652 +               name += sizeof(SysaufsBr_PREFIX) - 1;
31653 +       } else
31654 +                 BUG();
31655 +
31656 +       err = kstrtol(name, 10, &l);
31657 +       if (!err) {
31658 +               bbot = au_sbbot(sb);
31659 +               if (l <= bbot)
31660 +                       err = sysaufs_si_br(seq, sb, (aufs_bindex_t)l, idx);
31661 +               else
31662 +                       err = -ENOENT;
31663 +       }
31664 +
31665 +out_seq:
31666 +       if (!err) {
31667 +               err = seq->count;
31668 +               /* sysfs limit */
31669 +               if (unlikely(err == PAGE_SIZE))
31670 +                       err = -EFBIG;
31671 +       }
31672 +       au_kfree_rcu(seq);
31673 +out_unlock:
31674 +       si_read_unlock(sb);
31675 +out:
31676 +       return err;
31677 +}
31678 +
31679 +/* ---------------------------------------------------------------------- */
31680 +
31681 +static int au_brinfo(struct super_block *sb, union aufs_brinfo __user *arg)
31682 +{
31683 +       int err;
31684 +       int16_t brid;
31685 +       aufs_bindex_t bindex, bbot;
31686 +       size_t sz;
31687 +       char *buf;
31688 +       struct seq_file *seq;
31689 +       struct au_branch *br;
31690 +
31691 +       si_read_lock(sb, AuLock_FLUSH);
31692 +       bbot = au_sbbot(sb);
31693 +       err = bbot + 1;
31694 +       if (!arg)
31695 +               goto out;
31696 +
31697 +       err = -ENOMEM;
31698 +       buf = (void *)__get_free_page(GFP_NOFS);
31699 +       if (unlikely(!buf))
31700 +               goto out;
31701 +
31702 +       seq = au_seq(buf, PAGE_SIZE);
31703 +       err = PTR_ERR(seq);
31704 +       if (IS_ERR(seq))
31705 +               goto out_buf;
31706 +
31707 +       sz = sizeof(*arg) - offsetof(union aufs_brinfo, path);
31708 +       for (bindex = 0; bindex <= bbot; bindex++, arg++) {
31709 +               /* VERIFY_WRITE */
31710 +               err = !access_ok(arg, sizeof(*arg));
31711 +               if (unlikely(err))
31712 +                       break;
31713 +
31714 +               br = au_sbr(sb, bindex);
31715 +               brid = br->br_id;
31716 +               BUILD_BUG_ON(sizeof(brid) != sizeof(arg->id));
31717 +               err = __put_user(brid, &arg->id);
31718 +               if (unlikely(err))
31719 +                       break;
31720 +
31721 +               BUILD_BUG_ON(sizeof(br->br_perm) != sizeof(arg->perm));
31722 +               err = __put_user(br->br_perm, &arg->perm);
31723 +               if (unlikely(err))
31724 +                       break;
31725 +
31726 +               err = au_seq_path(seq, &br->br_path);
31727 +               if (unlikely(err))
31728 +                       break;
31729 +               seq_putc(seq, '\0');
31730 +               if (!seq_has_overflowed(seq)) {
31731 +                       err = copy_to_user(arg->path, seq->buf, seq->count);
31732 +                       seq->count = 0;
31733 +                       if (unlikely(err))
31734 +                               break;
31735 +               } else {
31736 +                       err = -E2BIG;
31737 +                       goto out_seq;
31738 +               }
31739 +       }
31740 +       if (unlikely(err))
31741 +               err = -EFAULT;
31742 +
31743 +out_seq:
31744 +       au_kfree_rcu(seq);
31745 +out_buf:
31746 +       free_page((unsigned long)buf);
31747 +out:
31748 +       si_read_unlock(sb);
31749 +       return err;
31750 +}
31751 +
31752 +long au_brinfo_ioctl(struct file *file, unsigned long arg)
31753 +{
31754 +       return au_brinfo(file->f_path.dentry->d_sb, (void __user *)arg);
31755 +}
31756 +
31757 +#ifdef CONFIG_COMPAT
31758 +long au_brinfo_compat_ioctl(struct file *file, unsigned long arg)
31759 +{
31760 +       return au_brinfo(file->f_path.dentry->d_sb, compat_ptr(arg));
31761 +}
31762 +#endif
31763 +
31764 +/* ---------------------------------------------------------------------- */
31765 +
31766 +void sysaufs_br_init(struct au_branch *br)
31767 +{
31768 +       int i;
31769 +       struct au_brsysfs *br_sysfs;
31770 +       struct attribute *attr;
31771 +
31772 +       br_sysfs = br->br_sysfs;
31773 +       for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31774 +               attr = &br_sysfs->attr;
31775 +               sysfs_attr_init(attr);
31776 +               attr->name = br_sysfs->name;
31777 +               attr->mode = 0444;
31778 +               br_sysfs++;
31779 +       }
31780 +}
31781 +
31782 +void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
31783 +{
31784 +       struct au_branch *br;
31785 +       struct kobject *kobj;
31786 +       struct au_brsysfs *br_sysfs;
31787 +       int i;
31788 +       aufs_bindex_t bbot;
31789 +
31790 +       if (!sysaufs_brs)
31791 +               return;
31792 +
31793 +       kobj = &au_sbi(sb)->si_kobj;
31794 +       bbot = au_sbbot(sb);
31795 +       for (; bindex <= bbot; bindex++) {
31796 +               br = au_sbr(sb, bindex);
31797 +               br_sysfs = br->br_sysfs;
31798 +               for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31799 +                       sysfs_remove_file(kobj, &br_sysfs->attr);
31800 +                       br_sysfs++;
31801 +               }
31802 +       }
31803 +}
31804 +
31805 +void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex)
31806 +{
31807 +       int err, i;
31808 +       aufs_bindex_t bbot;
31809 +       struct kobject *kobj;
31810 +       struct au_branch *br;
31811 +       struct au_brsysfs *br_sysfs;
31812 +
31813 +       if (!sysaufs_brs)
31814 +               return;
31815 +
31816 +       kobj = &au_sbi(sb)->si_kobj;
31817 +       bbot = au_sbbot(sb);
31818 +       for (; bindex <= bbot; bindex++) {
31819 +               br = au_sbr(sb, bindex);
31820 +               br_sysfs = br->br_sysfs;
31821 +               snprintf(br_sysfs[AuBrSysfs_BR].name, sizeof(br_sysfs->name),
31822 +                        SysaufsBr_PREFIX "%d", bindex);
31823 +               snprintf(br_sysfs[AuBrSysfs_BRID].name, sizeof(br_sysfs->name),
31824 +                        SysaufsBrid_PREFIX "%d", bindex);
31825 +               for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31826 +                       err = sysfs_create_file(kobj, &br_sysfs->attr);
31827 +                       if (unlikely(err))
31828 +                               pr_warn("failed %s under sysfs(%d)\n",
31829 +                                       br_sysfs->name, err);
31830 +                       br_sysfs++;
31831 +               }
31832 +       }
31833 +}
31834 diff -urN /usr/share/empty/fs/aufs/sysrq.c linux/fs/aufs/sysrq.c
31835 --- /usr/share/empty/fs/aufs/sysrq.c    1970-01-01 01:00:00.000000000 +0100
31836 +++ linux/fs/aufs/sysrq.c       2022-03-21 14:49:05.729966343 +0100
31837 @@ -0,0 +1,149 @@
31838 +// SPDX-License-Identifier: GPL-2.0
31839 +/*
31840 + * Copyright (C) 2005-2021 Junjiro R. Okajima
31841 + *
31842 + * This program, aufs is free software; you can redistribute it and/or modify
31843 + * it under the terms of the GNU General Public License as published by
31844 + * the Free Software Foundation; either version 2 of the License, or
31845 + * (at your option) any later version.
31846 + *
31847 + * This program is distributed in the hope that it will be useful,
31848 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31849 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31850 + * GNU General Public License for more details.
31851 + *
31852 + * You should have received a copy of the GNU General Public License
31853 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31854 + */
31855 +
31856 +/*
31857 + * magic sysrq handler
31858 + */
31859 +
31860 +/* #include <linux/sysrq.h> */
31861 +#include <linux/writeback.h>
31862 +#include "aufs.h"
31863 +
31864 +/* ---------------------------------------------------------------------- */
31865 +
31866 +static void sysrq_sb(struct super_block *sb)
31867 +{
31868 +       char *plevel;
31869 +       struct au_sbinfo *sbinfo;
31870 +       struct file *file;
31871 +       struct hlist_bl_head *files;
31872 +       struct hlist_bl_node *pos;
31873 +       struct au_finfo *finfo;
31874 +       struct inode *i;
31875 +
31876 +       plevel = au_plevel;
31877 +       au_plevel = KERN_WARNING;
31878 +
31879 +       /* since we define pr_fmt, call printk directly */
31880 +#define pr(str) printk(KERN_WARNING AUFS_NAME ": " str)
31881 +
31882 +       sbinfo = au_sbi(sb);
31883 +       printk(KERN_WARNING "si=%lx\n", sysaufs_si_id(sbinfo));
31884 +       pr("superblock\n");
31885 +       au_dpri_sb(sb);
31886 +
31887 +#if 0 /* reserved */
31888 +       do {
31889 +               int err, i, j, ndentry;
31890 +               struct au_dcsub_pages dpages;
31891 +               struct au_dpage *dpage;
31892 +
31893 +               err = au_dpages_init(&dpages, GFP_ATOMIC);
31894 +               if (unlikely(err))
31895 +                       break;
31896 +               err = au_dcsub_pages(&dpages, sb->s_root, NULL, NULL);
31897 +               if (!err)
31898 +                       for (i = 0; i < dpages.ndpage; i++) {
31899 +                               dpage = dpages.dpages + i;
31900 +                               ndentry = dpage->ndentry;
31901 +                               for (j = 0; j < ndentry; j++)
31902 +                                       au_dpri_dentry(dpage->dentries[j]);
31903 +                       }
31904 +               au_dpages_free(&dpages);
31905 +       } while (0);
31906 +#endif
31907 +
31908 +       pr("isolated inode\n");
31909 +       spin_lock(&sb->s_inode_list_lock);
31910 +       list_for_each_entry(i, &sb->s_inodes, i_sb_list) {
31911 +               spin_lock(&i->i_lock);
31912 +               if (hlist_empty(&i->i_dentry))
31913 +                       au_dpri_inode(i);
31914 +               spin_unlock(&i->i_lock);
31915 +       }
31916 +       spin_unlock(&sb->s_inode_list_lock);
31917 +
31918 +       pr("files\n");
31919 +       files = &au_sbi(sb)->si_files;
31920 +       hlist_bl_lock(files);
31921 +       hlist_bl_for_each_entry(finfo, pos, files, fi_hlist) {
31922 +               umode_t mode;
31923 +
31924 +               file = finfo->fi_file;
31925 +               mode = file_inode(file)->i_mode;
31926 +               if (!special_file(mode))
31927 +                       au_dpri_file(file);
31928 +       }
31929 +       hlist_bl_unlock(files);
31930 +       pr("done\n");
31931 +
31932 +#undef pr
31933 +       au_plevel = plevel;
31934 +}
31935 +
31936 +/* ---------------------------------------------------------------------- */
31937 +
31938 +/* module parameter */
31939 +static char *aufs_sysrq_key = "a";
31940 +module_param_named(sysrq, aufs_sysrq_key, charp, 0444);
31941 +MODULE_PARM_DESC(sysrq, "MagicSysRq key for " AUFS_NAME);
31942 +
31943 +static void au_sysrq(int key __maybe_unused)
31944 +{
31945 +       struct au_sbinfo *sbinfo;
31946 +       struct hlist_bl_node *pos;
31947 +
31948 +       lockdep_off();
31949 +       au_sbilist_lock();
31950 +       hlist_bl_for_each_entry(sbinfo, pos, &au_sbilist, si_list)
31951 +               sysrq_sb(sbinfo->si_sb);
31952 +       au_sbilist_unlock();
31953 +       lockdep_on();
31954 +}
31955 +
31956 +static struct sysrq_key_op au_sysrq_op = {
31957 +       .handler        = au_sysrq,
31958 +       .help_msg       = "Aufs",
31959 +       .action_msg     = "Aufs",
31960 +       .enable_mask    = SYSRQ_ENABLE_DUMP
31961 +};
31962 +
31963 +/* ---------------------------------------------------------------------- */
31964 +
31965 +int __init au_sysrq_init(void)
31966 +{
31967 +       int err;
31968 +       char key;
31969 +
31970 +       err = -1;
31971 +       key = *aufs_sysrq_key;
31972 +       if ('a' <= key && key <= 'z')
31973 +               err = register_sysrq_key(key, &au_sysrq_op);
31974 +       if (unlikely(err))
31975 +               pr_err("err %d, sysrq=%c\n", err, key);
31976 +       return err;
31977 +}
31978 +
31979 +void au_sysrq_fin(void)
31980 +{
31981 +       int err;
31982 +
31983 +       err = unregister_sysrq_key(*aufs_sysrq_key, &au_sysrq_op);
31984 +       if (unlikely(err))
31985 +               pr_err("err %d (ignored)\n", err);
31986 +}
31987 diff -urN /usr/share/empty/fs/aufs/vdir.c linux/fs/aufs/vdir.c
31988 --- /usr/share/empty/fs/aufs/vdir.c     1970-01-01 01:00:00.000000000 +0100
31989 +++ linux/fs/aufs/vdir.c        2022-03-21 14:49:05.733299677 +0100
31990 @@ -0,0 +1,896 @@
31991 +// SPDX-License-Identifier: GPL-2.0
31992 +/*
31993 + * Copyright (C) 2005-2021 Junjiro R. Okajima
31994 + *
31995 + * This program, aufs is free software; you can redistribute it and/or modify
31996 + * it under the terms of the GNU General Public License as published by
31997 + * the Free Software Foundation; either version 2 of the License, or
31998 + * (at your option) any later version.
31999 + *
32000 + * This program is distributed in the hope that it will be useful,
32001 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
32002 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
32003 + * GNU General Public License for more details.
32004 + *
32005 + * You should have received a copy of the GNU General Public License
32006 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
32007 + */
32008 +
32009 +/*
32010 + * virtual or vertical directory
32011 + */
32012 +
32013 +#include <linux/iversion.h>
32014 +#include "aufs.h"
32015 +
32016 +static unsigned int calc_size(int nlen)
32017 +{
32018 +       return ALIGN(sizeof(struct au_vdir_de) + nlen, sizeof(ino_t));
32019 +}
32020 +
32021 +static int set_deblk_end(union au_vdir_deblk_p *p,
32022 +                        union au_vdir_deblk_p *deblk_end)
32023 +{
32024 +       if (calc_size(0) <= deblk_end->deblk - p->deblk) {
32025 +               p->de->de_str.len = 0;
32026 +               /* smp_mb(); */
32027 +               return 0;
32028 +       }
32029 +       return -1; /* error */
32030 +}
32031 +
32032 +/* returns true or false */
32033 +static int is_deblk_end(union au_vdir_deblk_p *p,
32034 +                       union au_vdir_deblk_p *deblk_end)
32035 +{
32036 +       if (calc_size(0) <= deblk_end->deblk - p->deblk)
32037 +               return !p->de->de_str.len;
32038 +       return 1;
32039 +}
32040 +
32041 +static unsigned char *last_deblk(struct au_vdir *vdir)
32042 +{
32043 +       return vdir->vd_deblk[vdir->vd_nblk - 1];
32044 +}
32045 +
32046 +/* ---------------------------------------------------------------------- */
32047 +
32048 +/* estimate the appropriate size for name hash table */
32049 +unsigned int au_rdhash_est(loff_t sz)
32050 +{
32051 +       unsigned int n;
32052 +
32053 +       n = UINT_MAX;
32054 +       sz >>= 10;
32055 +       if (sz < n)
32056 +               n = sz;
32057 +       if (sz < AUFS_RDHASH_DEF)
32058 +               n = AUFS_RDHASH_DEF;
32059 +       /* pr_info("n %u\n", n); */
32060 +       return n;
32061 +}
32062 +
32063 +/*
32064 + * the allocated memory has to be freed by
32065 + * au_nhash_wh_free() or au_nhash_de_free().
32066 + */
32067 +int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp)
32068 +{
32069 +       struct hlist_head *head;
32070 +       unsigned int u;
32071 +       size_t sz;
32072 +
32073 +       sz = sizeof(*nhash->nh_head) * num_hash;
32074 +       head = kmalloc(sz, gfp);
32075 +       if (head) {
32076 +               nhash->nh_num = num_hash;
32077 +               nhash->nh_head = head;
32078 +               for (u = 0; u < num_hash; u++)
32079 +                       INIT_HLIST_HEAD(head++);
32080 +               return 0; /* success */
32081 +       }
32082 +
32083 +       return -ENOMEM;
32084 +}
32085 +
32086 +static void nhash_count(struct hlist_head *head)
32087 +{
32088 +#if 0 /* debugging */
32089 +       unsigned long n;
32090 +       struct hlist_node *pos;
32091 +
32092 +       n = 0;
32093 +       hlist_for_each(pos, head)
32094 +               n++;
32095 +       pr_info("%lu\n", n);
32096 +#endif
32097 +}
32098 +
32099 +static void au_nhash_wh_do_free(struct hlist_head *head)
32100 +{
32101 +       struct au_vdir_wh *pos;
32102 +       struct hlist_node *node;
32103 +
32104 +       hlist_for_each_entry_safe(pos, node, head, wh_hash)
32105 +               au_kfree_rcu(pos);
32106 +}
32107 +
32108 +static void au_nhash_de_do_free(struct hlist_head *head)
32109 +{
32110 +       struct au_vdir_dehstr *pos;
32111 +       struct hlist_node *node;
32112 +
32113 +       hlist_for_each_entry_safe(pos, node, head, hash)
32114 +               au_cache_free_vdir_dehstr(pos);
32115 +}
32116 +
32117 +static void au_nhash_do_free(struct au_nhash *nhash,
32118 +                            void (*free)(struct hlist_head *head))
32119 +{
32120 +       unsigned int n;
32121 +       struct hlist_head *head;
32122 +
32123 +       n = nhash->nh_num;
32124 +       if (!n)
32125 +               return;
32126 +
32127 +       head = nhash->nh_head;
32128 +       while (n-- > 0) {
32129 +               nhash_count(head);
32130 +               free(head++);
32131 +       }
32132 +       au_kfree_try_rcu(nhash->nh_head);
32133 +}
32134 +
32135 +void au_nhash_wh_free(struct au_nhash *whlist)
32136 +{
32137 +       au_nhash_do_free(whlist, au_nhash_wh_do_free);
32138 +}
32139 +
32140 +static void au_nhash_de_free(struct au_nhash *delist)
32141 +{
32142 +       au_nhash_do_free(delist, au_nhash_de_do_free);
32143 +}
32144 +
32145 +/* ---------------------------------------------------------------------- */
32146 +
32147 +int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
32148 +                           int limit)
32149 +{
32150 +       int num;
32151 +       unsigned int u, n;
32152 +       struct hlist_head *head;
32153 +       struct au_vdir_wh *pos;
32154 +
32155 +       num = 0;
32156 +       n = whlist->nh_num;
32157 +       head = whlist->nh_head;
32158 +       for (u = 0; u < n; u++, head++)
32159 +               hlist_for_each_entry(pos, head, wh_hash)
32160 +                       if (pos->wh_bindex == btgt && ++num > limit)
32161 +                               return 1;
32162 +       return 0;
32163 +}
32164 +
32165 +static struct hlist_head *au_name_hash(struct au_nhash *nhash,
32166 +                                      unsigned char *name,
32167 +                                      unsigned int len)
32168 +{
32169 +       unsigned int v;
32170 +       /* const unsigned int magic_bit = 12; */
32171 +
32172 +       AuDebugOn(!nhash->nh_num || !nhash->nh_head);
32173 +
32174 +       v = 0;
32175 +       if (len > 8)
32176 +               len = 8;
32177 +       while (len--)
32178 +               v += *name++;
32179 +       /* v = hash_long(v, magic_bit); */
32180 +       v %= nhash->nh_num;
32181 +       return nhash->nh_head + v;
32182 +}
32183 +
32184 +static int au_nhash_test_name(struct au_vdir_destr *str, const char *name,
32185 +                             int nlen)
32186 +{
32187 +       return str->len == nlen && !memcmp(str->name, name, nlen);
32188 +}
32189 +
32190 +/* returns found or not */
32191 +int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen)
32192 +{
32193 +       struct hlist_head *head;
32194 +       struct au_vdir_wh *pos;
32195 +       struct au_vdir_destr *str;
32196 +
32197 +       head = au_name_hash(whlist, name, nlen);
32198 +       hlist_for_each_entry(pos, head, wh_hash) {
32199 +               str = &pos->wh_str;
32200 +               AuDbg("%.*s\n", str->len, str->name);
32201 +               if (au_nhash_test_name(str, name, nlen))
32202 +                       return 1;
32203 +       }
32204 +       return 0;
32205 +}
32206 +
32207 +/* returns found(true) or not */
32208 +static int test_known(struct au_nhash *delist, char *name, int nlen)
32209 +{
32210 +       struct hlist_head *head;
32211 +       struct au_vdir_dehstr *pos;
32212 +       struct au_vdir_destr *str;
32213 +
32214 +       head = au_name_hash(delist, name, nlen);
32215 +       hlist_for_each_entry(pos, head, hash) {
32216 +               str = pos->str;
32217 +               AuDbg("%.*s\n", str->len, str->name);
32218 +               if (au_nhash_test_name(str, name, nlen))
32219 +                       return 1;
32220 +       }
32221 +       return 0;
32222 +}
32223 +
32224 +static void au_shwh_init_wh(struct au_vdir_wh *wh, ino_t ino,
32225 +                           unsigned char d_type)
32226 +{
32227 +#ifdef CONFIG_AUFS_SHWH
32228 +       wh->wh_ino = ino;
32229 +       wh->wh_type = d_type;
32230 +#endif
32231 +}
32232 +
32233 +/* ---------------------------------------------------------------------- */
32234 +
32235 +int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
32236 +                      unsigned int d_type, aufs_bindex_t bindex,
32237 +                      unsigned char shwh)
32238 +{
32239 +       int err;
32240 +       struct au_vdir_destr *str;
32241 +       struct au_vdir_wh *wh;
32242 +
32243 +       AuDbg("%.*s\n", nlen, name);
32244 +       AuDebugOn(!whlist->nh_num || !whlist->nh_head);
32245 +
32246 +       err = -ENOMEM;
32247 +       wh = kmalloc(sizeof(*wh) + nlen, GFP_NOFS);
32248 +       if (unlikely(!wh))
32249 +               goto out;
32250 +
32251 +       err = 0;
32252 +       wh->wh_bindex = bindex;
32253 +       if (shwh)
32254 +               au_shwh_init_wh(wh, ino, d_type);
32255 +       str = &wh->wh_str;
32256 +       str->len = nlen;
32257 +       memcpy(str->name, name, nlen);
32258 +       hlist_add_head(&wh->wh_hash, au_name_hash(whlist, name, nlen));
32259 +       /* smp_mb(); */
32260 +
32261 +out:
32262 +       return err;
32263 +}
32264 +
32265 +static int append_deblk(struct au_vdir *vdir)
32266 +{
32267 +       int err;
32268 +       unsigned long ul;
32269 +       const unsigned int deblk_sz = vdir->vd_deblk_sz;
32270 +       union au_vdir_deblk_p p, deblk_end;
32271 +       unsigned char **o;
32272 +
32273 +       err = -ENOMEM;
32274 +       o = au_krealloc(vdir->vd_deblk, sizeof(*o) * (vdir->vd_nblk + 1),
32275 +                       GFP_NOFS, /*may_shrink*/0);
32276 +       if (unlikely(!o))
32277 +               goto out;
32278 +
32279 +       vdir->vd_deblk = o;
32280 +       p.deblk = kmalloc(deblk_sz, GFP_NOFS);
32281 +       if (p.deblk) {
32282 +               ul = vdir->vd_nblk++;
32283 +               vdir->vd_deblk[ul] = p.deblk;
32284 +               vdir->vd_last.ul = ul;
32285 +               vdir->vd_last.p.deblk = p.deblk;
32286 +               deblk_end.deblk = p.deblk + deblk_sz;
32287 +               err = set_deblk_end(&p, &deblk_end);
32288 +       }
32289 +
32290 +out:
32291 +       return err;
32292 +}
32293 +
32294 +static int append_de(struct au_vdir *vdir, char *name, int nlen, ino_t ino,
32295 +                    unsigned int d_type, struct au_nhash *delist)
32296 +{
32297 +       int err;
32298 +       unsigned int sz;
32299 +       const unsigned int deblk_sz = vdir->vd_deblk_sz;
32300 +       union au_vdir_deblk_p p, *room, deblk_end;
32301 +       struct au_vdir_dehstr *dehstr;
32302 +
32303 +       p.deblk = last_deblk(vdir);
32304 +       deblk_end.deblk = p.deblk + deblk_sz;
32305 +       room = &vdir->vd_last.p;
32306 +       AuDebugOn(room->deblk < p.deblk || deblk_end.deblk <= room->deblk
32307 +                 || !is_deblk_end(room, &deblk_end));
32308 +
32309 +       sz = calc_size(nlen);
32310 +       if (unlikely(sz > deblk_end.deblk - room->deblk)) {
32311 +               err = append_deblk(vdir);
32312 +               if (unlikely(err))
32313 +                       goto out;
32314 +
32315 +               p.deblk = last_deblk(vdir);
32316 +               deblk_end.deblk = p.deblk + deblk_sz;
32317 +               /* smp_mb(); */
32318 +               AuDebugOn(room->deblk != p.deblk);
32319 +       }
32320 +
32321 +       err = -ENOMEM;
32322 +       dehstr = au_cache_alloc_vdir_dehstr();
32323 +       if (unlikely(!dehstr))
32324 +               goto out;
32325 +
32326 +       dehstr->str = &room->de->de_str;
32327 +       hlist_add_head(&dehstr->hash, au_name_hash(delist, name, nlen));
32328 +       room->de->de_ino = ino;
32329 +       room->de->de_type = d_type;
32330 +       room->de->de_str.len = nlen;
32331 +       memcpy(room->de->de_str.name, name, nlen);
32332 +
32333 +       err = 0;
32334 +       room->deblk += sz;
32335 +       if (unlikely(set_deblk_end(room, &deblk_end)))
32336 +               err = append_deblk(vdir);
32337 +       /* smp_mb(); */
32338 +
32339 +out:
32340 +       return err;
32341 +}
32342 +
32343 +/* ---------------------------------------------------------------------- */
32344 +
32345 +void au_vdir_free(struct au_vdir *vdir)
32346 +{
32347 +       unsigned char **deblk;
32348 +
32349 +       deblk = vdir->vd_deblk;
32350 +       while (vdir->vd_nblk--)
32351 +               au_kfree_try_rcu(*deblk++);
32352 +       au_kfree_try_rcu(vdir->vd_deblk);
32353 +       au_cache_free_vdir(vdir);
32354 +}
32355 +
32356 +static struct au_vdir *alloc_vdir(struct file *file)
32357 +{
32358 +       struct au_vdir *vdir;
32359 +       struct super_block *sb;
32360 +       int err;
32361 +
32362 +       sb = file->f_path.dentry->d_sb;
32363 +       SiMustAnyLock(sb);
32364 +
32365 +       err = -ENOMEM;
32366 +       vdir = au_cache_alloc_vdir();
32367 +       if (unlikely(!vdir))
32368 +               goto out;
32369 +
32370 +       vdir->vd_deblk = kzalloc(sizeof(*vdir->vd_deblk), GFP_NOFS);
32371 +       if (unlikely(!vdir->vd_deblk))
32372 +               goto out_free;
32373 +
32374 +       vdir->vd_deblk_sz = au_sbi(sb)->si_rdblk;
32375 +       if (!vdir->vd_deblk_sz) {
32376 +               /* estimate the appropriate size for deblk */
32377 +               vdir->vd_deblk_sz = au_dir_size(file, /*dentry*/NULL);
32378 +               /* pr_info("vd_deblk_sz %u\n", vdir->vd_deblk_sz); */
32379 +       }
32380 +       vdir->vd_nblk = 0;
32381 +       vdir->vd_version = 0;
32382 +       vdir->vd_jiffy = 0;
32383 +       err = append_deblk(vdir);
32384 +       if (!err)
32385 +               return vdir; /* success */
32386 +
32387 +       au_kfree_try_rcu(vdir->vd_deblk);
32388 +
32389 +out_free:
32390 +       au_cache_free_vdir(vdir);
32391 +out:
32392 +       vdir = ERR_PTR(err);
32393 +       return vdir;
32394 +}
32395 +
32396 +static int reinit_vdir(struct au_vdir *vdir)
32397 +{
32398 +       int err;
32399 +       union au_vdir_deblk_p p, deblk_end;
32400 +
32401 +       while (vdir->vd_nblk > 1) {
32402 +               au_kfree_try_rcu(vdir->vd_deblk[vdir->vd_nblk - 1]);
32403 +               /* vdir->vd_deblk[vdir->vd_nblk - 1] = NULL; */
32404 +               vdir->vd_nblk--;
32405 +       }
32406 +       p.deblk = vdir->vd_deblk[0];
32407 +       deblk_end.deblk = p.deblk + vdir->vd_deblk_sz;
32408 +       err = set_deblk_end(&p, &deblk_end);
32409 +       /* keep vd_dblk_sz */
32410 +       vdir->vd_last.ul = 0;
32411 +       vdir->vd_last.p.deblk = vdir->vd_deblk[0];
32412 +       vdir->vd_version = 0;
32413 +       vdir->vd_jiffy = 0;
32414 +       /* smp_mb(); */
32415 +       return err;
32416 +}
32417 +
32418 +/* ---------------------------------------------------------------------- */
32419 +
32420 +#define AuFillVdir_CALLED      1
32421 +#define AuFillVdir_WHABLE      (1 << 1)
32422 +#define AuFillVdir_SHWH                (1 << 2)
32423 +#define au_ftest_fillvdir(flags, name) ((flags) & AuFillVdir_##name)
32424 +#define au_fset_fillvdir(flags, name) \
32425 +       do { (flags) |= AuFillVdir_##name; } while (0)
32426 +#define au_fclr_fillvdir(flags, name) \
32427 +       do { (flags) &= ~AuFillVdir_##name; } while (0)
32428 +
32429 +#ifndef CONFIG_AUFS_SHWH
32430 +#undef AuFillVdir_SHWH
32431 +#define AuFillVdir_SHWH                0
32432 +#endif
32433 +
32434 +struct fillvdir_arg {
32435 +       struct dir_context      ctx;
32436 +       struct file             *file;
32437 +       struct au_vdir          *vdir;
32438 +       struct au_nhash         delist;
32439 +       struct au_nhash         whlist;
32440 +       aufs_bindex_t           bindex;
32441 +       unsigned int            flags;
32442 +       int                     err;
32443 +};
32444 +
32445 +static int fillvdir(struct dir_context *ctx, const char *__name, int nlen,
32446 +                   loff_t offset __maybe_unused, u64 h_ino,
32447 +                   unsigned int d_type)
32448 +{
32449 +       struct fillvdir_arg *arg = container_of(ctx, struct fillvdir_arg, ctx);
32450 +       char *name = (void *)__name;
32451 +       struct super_block *sb;
32452 +       ino_t ino;
32453 +       const unsigned char shwh = !!au_ftest_fillvdir(arg->flags, SHWH);
32454 +
32455 +       arg->err = 0;
32456 +       sb = arg->file->f_path.dentry->d_sb;
32457 +       au_fset_fillvdir(arg->flags, CALLED);
32458 +       /* smp_mb(); */
32459 +       if (nlen <= AUFS_WH_PFX_LEN
32460 +           || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
32461 +               if (test_known(&arg->delist, name, nlen)
32462 +                   || au_nhash_test_known_wh(&arg->whlist, name, nlen))
32463 +                       goto out; /* already exists or whiteouted */
32464 +
32465 +               arg->err = au_ino(sb, arg->bindex, h_ino, d_type, &ino);
32466 +               if (!arg->err) {
32467 +                       if (unlikely(nlen > AUFS_MAX_NAMELEN))
32468 +                               d_type = DT_UNKNOWN;
32469 +                       arg->err = append_de(arg->vdir, name, nlen, ino,
32470 +                                            d_type, &arg->delist);
32471 +               }
32472 +       } else if (au_ftest_fillvdir(arg->flags, WHABLE)) {
32473 +               name += AUFS_WH_PFX_LEN;
32474 +               nlen -= AUFS_WH_PFX_LEN;
32475 +               if (au_nhash_test_known_wh(&arg->whlist, name, nlen))
32476 +                       goto out; /* already whiteouted */
32477 +
32478 +               ino = 0; /* just to suppress a warning */
32479 +               if (shwh)
32480 +                       arg->err = au_wh_ino(sb, arg->bindex, h_ino, d_type,
32481 +                                            &ino);
32482 +               if (!arg->err) {
32483 +                       if (nlen <= AUFS_MAX_NAMELEN + AUFS_WH_PFX_LEN)
32484 +                               d_type = DT_UNKNOWN;
32485 +                       arg->err = au_nhash_append_wh
32486 +                               (&arg->whlist, name, nlen, ino, d_type,
32487 +                                arg->bindex, shwh);
32488 +               }
32489 +       }
32490 +
32491 +out:
32492 +       if (!arg->err)
32493 +               arg->vdir->vd_jiffy = jiffies;
32494 +       /* smp_mb(); */
32495 +       AuTraceErr(arg->err);
32496 +       return arg->err;
32497 +}
32498 +
32499 +static int au_handle_shwh(struct super_block *sb, struct au_vdir *vdir,
32500 +                         struct au_nhash *whlist, struct au_nhash *delist)
32501 +{
32502 +#ifdef CONFIG_AUFS_SHWH
32503 +       int err;
32504 +       unsigned int nh, u;
32505 +       struct hlist_head *head;
32506 +       struct au_vdir_wh *pos;
32507 +       struct hlist_node *n;
32508 +       char *p, *o;
32509 +       struct au_vdir_destr *destr;
32510 +
32511 +       AuDebugOn(!au_opt_test(au_mntflags(sb), SHWH));
32512 +
32513 +       err = -ENOMEM;
32514 +       o = p = (void *)__get_free_page(GFP_NOFS);
32515 +       if (unlikely(!p))
32516 +               goto out;
32517 +
32518 +       err = 0;
32519 +       nh = whlist->nh_num;
32520 +       memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
32521 +       p += AUFS_WH_PFX_LEN;
32522 +       for (u = 0; u < nh; u++) {
32523 +               head = whlist->nh_head + u;
32524 +               hlist_for_each_entry_safe(pos, n, head, wh_hash) {
32525 +                       destr = &pos->wh_str;
32526 +                       memcpy(p, destr->name, destr->len);
32527 +                       err = append_de(vdir, o, destr->len + AUFS_WH_PFX_LEN,
32528 +                                       pos->wh_ino, pos->wh_type, delist);
32529 +                       if (unlikely(err))
32530 +                               break;
32531 +               }
32532 +       }
32533 +
32534 +       free_page((unsigned long)o);
32535 +
32536 +out:
32537 +       AuTraceErr(err);
32538 +       return err;
32539 +#else
32540 +       return 0;
32541 +#endif
32542 +}
32543 +
32544 +static int au_do_read_vdir(struct fillvdir_arg *arg)
32545 +{
32546 +       int err;
32547 +       unsigned int rdhash;
32548 +       loff_t offset;
32549 +       aufs_bindex_t bbot, bindex, btop;
32550 +       unsigned char shwh;
32551 +       struct file *hf, *file;
32552 +       struct super_block *sb;
32553 +
32554 +       file = arg->file;
32555 +       sb = file->f_path.dentry->d_sb;
32556 +       SiMustAnyLock(sb);
32557 +
32558 +       rdhash = au_sbi(sb)->si_rdhash;
32559 +       if (!rdhash)
32560 +               rdhash = au_rdhash_est(au_dir_size(file, /*dentry*/NULL));
32561 +       err = au_nhash_alloc(&arg->delist, rdhash, GFP_NOFS);
32562 +       if (unlikely(err))
32563 +               goto out;
32564 +       err = au_nhash_alloc(&arg->whlist, rdhash, GFP_NOFS);
32565 +       if (unlikely(err))
32566 +               goto out_delist;
32567 +
32568 +       err = 0;
32569 +       arg->flags = 0;
32570 +       shwh = 0;
32571 +       if (au_opt_test(au_mntflags(sb), SHWH)) {
32572 +               shwh = 1;
32573 +               au_fset_fillvdir(arg->flags, SHWH);
32574 +       }
32575 +       btop = au_fbtop(file);
32576 +       bbot = au_fbbot_dir(file);
32577 +       for (bindex = btop; !err && bindex <= bbot; bindex++) {
32578 +               hf = au_hf_dir(file, bindex);
32579 +               if (!hf)
32580 +                       continue;
32581 +
32582 +               offset = vfsub_llseek(hf, 0, SEEK_SET);
32583 +               err = offset;
32584 +               if (unlikely(offset))
32585 +                       break;
32586 +
32587 +               arg->bindex = bindex;
32588 +               au_fclr_fillvdir(arg->flags, WHABLE);
32589 +               if (shwh
32590 +                   || (bindex != bbot
32591 +                       && au_br_whable(au_sbr_perm(sb, bindex))))
32592 +                       au_fset_fillvdir(arg->flags, WHABLE);
32593 +               do {
32594 +                       arg->err = 0;
32595 +                       au_fclr_fillvdir(arg->flags, CALLED);
32596 +                       /* smp_mb(); */
32597 +                       err = vfsub_iterate_dir(hf, &arg->ctx);
32598 +                       if (err >= 0)
32599 +                               err = arg->err;
32600 +               } while (!err && au_ftest_fillvdir(arg->flags, CALLED));
32601 +
32602 +               /*
32603 +                * dir_relax() may be good for concurrency, but aufs should not
32604 +                * use it since it will cause a lockdep problem.
32605 +                */
32606 +       }
32607 +
32608 +       if (!err && shwh)
32609 +               err = au_handle_shwh(sb, arg->vdir, &arg->whlist, &arg->delist);
32610 +
32611 +       au_nhash_wh_free(&arg->whlist);
32612 +
32613 +out_delist:
32614 +       au_nhash_de_free(&arg->delist);
32615 +out:
32616 +       return err;
32617 +}
32618 +
32619 +static int read_vdir(struct file *file, int may_read)
32620 +{
32621 +       int err;
32622 +       unsigned long expire;
32623 +       unsigned char do_read;
32624 +       struct fillvdir_arg arg = {
32625 +               .ctx = {
32626 +                       .actor = fillvdir
32627 +               }
32628 +       };
32629 +       struct inode *inode;
32630 +       struct au_vdir *vdir, *allocated;
32631 +
32632 +       err = 0;
32633 +       inode = file_inode(file);
32634 +       IMustLock(inode);
32635 +       IiMustWriteLock(inode);
32636 +       SiMustAnyLock(inode->i_sb);
32637 +
32638 +       allocated = NULL;
32639 +       do_read = 0;
32640 +       expire = au_sbi(inode->i_sb)->si_rdcache;
32641 +       vdir = au_ivdir(inode);
32642 +       if (!vdir) {
32643 +               do_read = 1;
32644 +               vdir = alloc_vdir(file);
32645 +               err = PTR_ERR(vdir);
32646 +               if (IS_ERR(vdir))
32647 +                       goto out;
32648 +               err = 0;
32649 +               allocated = vdir;
32650 +       } else if (may_read
32651 +                  && (!inode_eq_iversion(inode, vdir->vd_version)
32652 +                      || time_after(jiffies, vdir->vd_jiffy + expire))) {
32653 +               do_read = 1;
32654 +               err = reinit_vdir(vdir);
32655 +               if (unlikely(err))
32656 +                       goto out;
32657 +       }
32658 +
32659 +       if (!do_read)
32660 +               return 0; /* success */
32661 +
32662 +       arg.file = file;
32663 +       arg.vdir = vdir;
32664 +       err = au_do_read_vdir(&arg);
32665 +       if (!err) {
32666 +               /* file->f_pos = 0; */ /* todo: ctx->pos? */
32667 +               vdir->vd_version = inode_query_iversion(inode);
32668 +               vdir->vd_last.ul = 0;
32669 +               vdir->vd_last.p.deblk = vdir->vd_deblk[0];
32670 +               if (allocated)
32671 +                       au_set_ivdir(inode, allocated);
32672 +       } else if (allocated)
32673 +               au_vdir_free(allocated);
32674 +
32675 +out:
32676 +       return err;
32677 +}
32678 +
32679 +static int copy_vdir(struct au_vdir *tgt, struct au_vdir *src)
32680 +{
32681 +       int err, rerr;
32682 +       unsigned long ul, n;
32683 +       const unsigned int deblk_sz = src->vd_deblk_sz;
32684 +
32685 +       AuDebugOn(tgt->vd_nblk != 1);
32686 +
32687 +       err = -ENOMEM;
32688 +       if (tgt->vd_nblk < src->vd_nblk) {
32689 +               unsigned char **p;
32690 +
32691 +               p = au_krealloc(tgt->vd_deblk, sizeof(*p) * src->vd_nblk,
32692 +                               GFP_NOFS, /*may_shrink*/0);
32693 +               if (unlikely(!p))
32694 +                       goto out;
32695 +               tgt->vd_deblk = p;
32696 +       }
32697 +
32698 +       if (tgt->vd_deblk_sz != deblk_sz) {
32699 +               unsigned char *p;
32700 +
32701 +               tgt->vd_deblk_sz = deblk_sz;
32702 +               p = au_krealloc(tgt->vd_deblk[0], deblk_sz, GFP_NOFS,
32703 +                               /*may_shrink*/1);
32704 +               if (unlikely(!p))
32705 +                       goto out;
32706 +               tgt->vd_deblk[0] = p;
32707 +       }
32708 +       memcpy(tgt->vd_deblk[0], src->vd_deblk[0], deblk_sz);
32709 +       tgt->vd_version = src->vd_version;
32710 +       tgt->vd_jiffy = src->vd_jiffy;
32711 +
32712 +       n = src->vd_nblk;
32713 +       for (ul = 1; ul < n; ul++) {
32714 +               tgt->vd_deblk[ul] = kmemdup(src->vd_deblk[ul], deblk_sz,
32715 +                                           GFP_NOFS);
32716 +               if (unlikely(!tgt->vd_deblk[ul]))
32717 +                       goto out;
32718 +               tgt->vd_nblk++;
32719 +       }
32720 +       tgt->vd_nblk = n;
32721 +       tgt->vd_last.ul = tgt->vd_last.ul;
32722 +       tgt->vd_last.p.deblk = tgt->vd_deblk[tgt->vd_last.ul];
32723 +       tgt->vd_last.p.deblk += src->vd_last.p.deblk
32724 +               - src->vd_deblk[src->vd_last.ul];
32725 +       /* smp_mb(); */
32726 +       return 0; /* success */
32727 +
32728 +out:
32729 +       rerr = reinit_vdir(tgt);
32730 +       BUG_ON(rerr);
32731 +       return err;
32732 +}
32733 +
32734 +int au_vdir_init(struct file *file)
32735 +{
32736 +       int err;
32737 +       struct inode *inode;
32738 +       struct au_vdir *vdir_cache, *allocated;
32739 +
32740 +       /* test file->f_pos here instead of ctx->pos */
32741 +       err = read_vdir(file, !file->f_pos);
32742 +       if (unlikely(err))
32743 +               goto out;
32744 +
32745 +       allocated = NULL;
32746 +       vdir_cache = au_fvdir_cache(file);
32747 +       if (!vdir_cache) {
32748 +               vdir_cache = alloc_vdir(file);
32749 +               err = PTR_ERR(vdir_cache);
32750 +               if (IS_ERR(vdir_cache))
32751 +                       goto out;
32752 +               allocated = vdir_cache;
32753 +       } else if (!file->f_pos && vdir_cache->vd_version != file->f_version) {
32754 +               /* test file->f_pos here instead of ctx->pos */
32755 +               err = reinit_vdir(vdir_cache);
32756 +               if (unlikely(err))
32757 +                       goto out;
32758 +       } else
32759 +               return 0; /* success */
32760 +
32761 +       inode = file_inode(file);
32762 +       err = copy_vdir(vdir_cache, au_ivdir(inode));
32763 +       if (!err) {
32764 +               file->f_version = inode_query_iversion(inode);
32765 +               if (allocated)
32766 +                       au_set_fvdir_cache(file, allocated);
32767 +       } else if (allocated)
32768 +               au_vdir_free(allocated);
32769 +
32770 +out:
32771 +       return err;
32772 +}
32773 +
32774 +static loff_t calc_offset(struct au_vdir *vdir)
32775 +{
32776 +       loff_t offset;
32777 +       union au_vdir_deblk_p p;
32778 +
32779 +       p.deblk = vdir->vd_deblk[vdir->vd_last.ul];
32780 +       offset = vdir->vd_last.p.deblk - p.deblk;
32781 +       offset += vdir->vd_deblk_sz * vdir->vd_last.ul;
32782 +       return offset;
32783 +}
32784 +
32785 +/* returns true or false */
32786 +static int seek_vdir(struct file *file, struct dir_context *ctx)
32787 +{
32788 +       int valid;
32789 +       unsigned int deblk_sz;
32790 +       unsigned long ul, n;
32791 +       loff_t offset;
32792 +       union au_vdir_deblk_p p, deblk_end;
32793 +       struct au_vdir *vdir_cache;
32794 +
32795 +       valid = 1;
32796 +       vdir_cache = au_fvdir_cache(file);
32797 +       offset = calc_offset(vdir_cache);
32798 +       AuDbg("offset %lld\n", offset);
32799 +       if (ctx->pos == offset)
32800 +               goto out;
32801 +
32802 +       vdir_cache->vd_last.ul = 0;
32803 +       vdir_cache->vd_last.p.deblk = vdir_cache->vd_deblk[0];
32804 +       if (!ctx->pos)
32805 +               goto out;
32806 +
32807 +       valid = 0;
32808 +       deblk_sz = vdir_cache->vd_deblk_sz;
32809 +       ul = div64_u64(ctx->pos, deblk_sz);
32810 +       AuDbg("ul %lu\n", ul);
32811 +       if (ul >= vdir_cache->vd_nblk)
32812 +               goto out;
32813 +
32814 +       n = vdir_cache->vd_nblk;
32815 +       for (; ul < n; ul++) {
32816 +               p.deblk = vdir_cache->vd_deblk[ul];
32817 +               deblk_end.deblk = p.deblk + deblk_sz;
32818 +               offset = ul;
32819 +               offset *= deblk_sz;
32820 +               while (!is_deblk_end(&p, &deblk_end) && offset < ctx->pos) {
32821 +                       unsigned int l;
32822 +
32823 +                       l = calc_size(p.de->de_str.len);
32824 +                       offset += l;
32825 +                       p.deblk += l;
32826 +               }
32827 +               if (!is_deblk_end(&p, &deblk_end)) {
32828 +                       valid = 1;
32829 +                       vdir_cache->vd_last.ul = ul;
32830 +                       vdir_cache->vd_last.p = p;
32831 +                       break;
32832 +               }
32833 +       }
32834 +
32835 +out:
32836 +       /* smp_mb(); */
32837 +       if (!valid)
32838 +               AuDbg("valid %d\n", !valid);
32839 +       return valid;
32840 +}
32841 +
32842 +int au_vdir_fill_de(struct file *file, struct dir_context *ctx)
32843 +{
32844 +       unsigned int l, deblk_sz;
32845 +       union au_vdir_deblk_p deblk_end;
32846 +       struct au_vdir *vdir_cache;
32847 +       struct au_vdir_de *de;
32848 +
32849 +       if (!seek_vdir(file, ctx))
32850 +               return 0;
32851 +
32852 +       vdir_cache = au_fvdir_cache(file);
32853 +       deblk_sz = vdir_cache->vd_deblk_sz;
32854 +       while (1) {
32855 +               deblk_end.deblk = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
32856 +               deblk_end.deblk += deblk_sz;
32857 +               while (!is_deblk_end(&vdir_cache->vd_last.p, &deblk_end)) {
32858 +                       de = vdir_cache->vd_last.p.de;
32859 +                       AuDbg("%.*s, off%lld, i%lu, dt%d\n",
32860 +                             de->de_str.len, de->de_str.name, ctx->pos,
32861 +                             (unsigned long)de->de_ino, de->de_type);
32862 +                       if (unlikely(!dir_emit(ctx, de->de_str.name,
32863 +                                              de->de_str.len, de->de_ino,
32864 +                                              de->de_type))) {
32865 +                               /* todo: ignore the error caused by udba? */
32866 +                               /* return err; */
32867 +                               return 0;
32868 +                       }
32869 +
32870 +                       l = calc_size(de->de_str.len);
32871 +                       vdir_cache->vd_last.p.deblk += l;
32872 +                       ctx->pos += l;
32873 +               }
32874 +               if (vdir_cache->vd_last.ul < vdir_cache->vd_nblk - 1) {
32875 +                       vdir_cache->vd_last.ul++;
32876 +                       vdir_cache->vd_last.p.deblk
32877 +                               = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
32878 +                       ctx->pos = deblk_sz * vdir_cache->vd_last.ul;
32879 +                       continue;
32880 +               }
32881 +               break;
32882 +       }
32883 +
32884 +       /* smp_mb(); */
32885 +       return 0;
32886 +}
32887 diff -urN /usr/share/empty/fs/aufs/vfsub.c linux/fs/aufs/vfsub.c
32888 --- /usr/share/empty/fs/aufs/vfsub.c    1970-01-01 01:00:00.000000000 +0100
32889 +++ linux/fs/aufs/vfsub.c       2022-03-21 14:49:05.733299677 +0100
32890 @@ -0,0 +1,918 @@
32891 +// SPDX-License-Identifier: GPL-2.0
32892 +/*
32893 + * Copyright (C) 2005-2021 Junjiro R. Okajima
32894 + *
32895 + * This program, aufs is free software; you can redistribute it and/or modify
32896 + * it under the terms of the GNU General Public License as published by
32897 + * the Free Software Foundation; either version 2 of the License, or
32898 + * (at your option) any later version.
32899 + *
32900 + * This program is distributed in the hope that it will be useful,
32901 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
32902 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
32903 + * GNU General Public License for more details.
32904 + *
32905 + * You should have received a copy of the GNU General Public License
32906 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
32907 + */
32908 +
32909 +/*
32910 + * sub-routines for VFS
32911 + */
32912 +
32913 +#include <linux/mnt_namespace.h>
32914 +#include <linux/nsproxy.h>
32915 +#include <linux/security.h>
32916 +#include <linux/splice.h>
32917 +#include "aufs.h"
32918 +
32919 +#ifdef CONFIG_AUFS_BR_FUSE
32920 +int vfsub_test_mntns(struct vfsmount *mnt, struct super_block *h_sb)
32921 +{
32922 +       if (!au_test_fuse(h_sb) || !au_userns)
32923 +               return 0;
32924 +
32925 +       return is_current_mnt_ns(mnt) ? 0 : -EACCES;
32926 +}
32927 +#endif
32928 +
32929 +int vfsub_sync_filesystem(struct super_block *h_sb)
32930 +{
32931 +       int err;
32932 +
32933 +       lockdep_off();
32934 +       down_read(&h_sb->s_umount);
32935 +       err = sync_filesystem(h_sb);
32936 +       up_read(&h_sb->s_umount);
32937 +       lockdep_on();
32938 +
32939 +       return err;
32940 +}
32941 +
32942 +/* ---------------------------------------------------------------------- */
32943 +
32944 +int vfsub_update_h_iattr(struct path *h_path, int *did)
32945 +{
32946 +       int err;
32947 +       struct kstat st;
32948 +       struct super_block *h_sb;
32949 +
32950 +       /*
32951 +        * Always needs h_path->mnt for LSM or FUSE branch.
32952 +        */
32953 +       AuDebugOn(!h_path->mnt);
32954 +
32955 +       /* for remote fs, leave work for its getattr or d_revalidate */
32956 +       /* for bad i_attr fs, handle them in aufs_getattr() */
32957 +       /* still some fs may acquire i_mutex. we need to skip them */
32958 +       err = 0;
32959 +       if (!did)
32960 +               did = &err;
32961 +       h_sb = h_path->dentry->d_sb;
32962 +       *did = (!au_test_fs_remote(h_sb) && au_test_fs_refresh_iattr(h_sb));
32963 +       if (*did)
32964 +               err = vfsub_getattr(h_path, &st);
32965 +
32966 +       return err;
32967 +}
32968 +
32969 +/* ---------------------------------------------------------------------- */
32970 +
32971 +struct file *vfsub_dentry_open(struct path *path, int flags)
32972 +{
32973 +       return dentry_open(path, flags /* | __FMODE_NONOTIFY */,
32974 +                          current_cred());
32975 +}
32976 +
32977 +struct file *vfsub_filp_open(const char *path, int oflags, int mode)
32978 +{
32979 +       struct file *file;
32980 +
32981 +       lockdep_off();
32982 +       file = filp_open(path,
32983 +                        oflags /* | __FMODE_NONOTIFY */,
32984 +                        mode);
32985 +       lockdep_on();
32986 +       if (IS_ERR(file))
32987 +               goto out;
32988 +       vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
32989 +
32990 +out:
32991 +       return file;
32992 +}
32993 +
32994 +/*
32995 + * Ideally this function should call VFS:do_last() in order to keep all its
32996 + * checkings. But it is very hard for aufs to regenerate several VFS internal
32997 + * structure such as nameidata. This is a second (or third) best approach.
32998 + * cf. linux/fs/namei.c:do_last(), lookup_open() and atomic_open().
32999 + */
33000 +int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
33001 +                     struct vfsub_aopen_args *args)
33002 +{
33003 +       int err;
33004 +       struct au_branch *br = args->br;
33005 +       struct file *file = args->file;
33006 +       /* copied from linux/fs/namei.c:atomic_open() */
33007 +       struct dentry *const DENTRY_NOT_SET = (void *)-1UL;
33008 +
33009 +       IMustLock(dir);
33010 +       AuDebugOn(!dir->i_op->atomic_open);
33011 +
33012 +       err = au_br_test_oflag(args->open_flag, br);
33013 +       if (unlikely(err))
33014 +               goto out;
33015 +
33016 +       au_lcnt_inc(&br->br_nfiles);
33017 +       file->f_path.dentry = DENTRY_NOT_SET;
33018 +       file->f_path.mnt = au_br_mnt(br);
33019 +       AuDbg("%ps\n", dir->i_op->atomic_open);
33020 +       err = dir->i_op->atomic_open(dir, dentry, file, args->open_flag,
33021 +                                    args->create_mode);
33022 +       if (unlikely(err < 0)) {
33023 +               au_lcnt_dec(&br->br_nfiles);
33024 +               goto out;
33025 +       }
33026 +
33027 +       /* temporary workaround for nfsv4 branch */
33028 +       if (au_test_nfs(dir->i_sb))
33029 +               nfs_mark_for_revalidate(dir);
33030 +
33031 +       if (file->f_mode & FMODE_CREATED)
33032 +               fsnotify_create(dir, dentry);
33033 +       if (!(file->f_mode & FMODE_OPENED)) {
33034 +               au_lcnt_dec(&br->br_nfiles);
33035 +               goto out;
33036 +       }
33037 +
33038 +       /* todo: call VFS:may_open() here */
33039 +       /* todo: ima_file_check() too? */
33040 +       if (!err && (args->open_flag & __FMODE_EXEC))
33041 +               err = deny_write_access(file);
33042 +       if (!err)
33043 +               fsnotify_open(file);
33044 +       else
33045 +               au_lcnt_dec(&br->br_nfiles);
33046 +       /* note that the file is created and still opened */
33047 +
33048 +out:
33049 +       return err;
33050 +}
33051 +
33052 +int vfsub_kern_path(const char *name, unsigned int flags, struct path *path)
33053 +{
33054 +       int err;
33055 +
33056 +       err = kern_path(name, flags, path);
33057 +       if (!err && d_is_positive(path->dentry))
33058 +               vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
33059 +       return err;
33060 +}
33061 +
33062 +struct dentry *vfsub_lookup_one_len_unlocked(const char *name,
33063 +                                            struct path *ppath, int len)
33064 +{
33065 +       struct path path;
33066 +
33067 +       path.dentry = lookup_one_len_unlocked(name, ppath->dentry, len);
33068 +       if (IS_ERR(path.dentry))
33069 +               goto out;
33070 +       if (d_is_positive(path.dentry)) {
33071 +               path.mnt = ppath->mnt;
33072 +               vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
33073 +       }
33074 +
33075 +out:
33076 +       AuTraceErrPtr(path.dentry);
33077 +       return path.dentry;
33078 +}
33079 +
33080 +struct dentry *vfsub_lookup_one_len(const char *name, struct path *ppath,
33081 +                                   int len)
33082 +{
33083 +       struct path path;
33084 +
33085 +       /* VFS checks it too, but by WARN_ON_ONCE() */
33086 +       IMustLock(d_inode(ppath->dentry));
33087 +
33088 +       path.dentry = lookup_one_len(name, ppath->dentry, len);
33089 +       if (IS_ERR(path.dentry))
33090 +               goto out;
33091 +       if (d_is_positive(path.dentry)) {
33092 +               path.mnt = ppath->mnt;
33093 +               vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
33094 +       }
33095 +
33096 +out:
33097 +       AuTraceErrPtr(path.dentry);
33098 +       return path.dentry;
33099 +}
33100 +
33101 +void vfsub_call_lkup_one(void *args)
33102 +{
33103 +       struct vfsub_lkup_one_args *a = args;
33104 +       *a->errp = vfsub_lkup_one(a->name, a->ppath);
33105 +}
33106 +
33107 +/* ---------------------------------------------------------------------- */
33108 +
33109 +struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
33110 +                                struct dentry *d2, struct au_hinode *hdir2)
33111 +{
33112 +       struct dentry *d;
33113 +
33114 +       lockdep_off();
33115 +       d = lock_rename(d1, d2);
33116 +       lockdep_on();
33117 +       au_hn_suspend(hdir1);
33118 +       if (hdir1 != hdir2)
33119 +               au_hn_suspend(hdir2);
33120 +
33121 +       return d;
33122 +}
33123 +
33124 +void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
33125 +                        struct dentry *d2, struct au_hinode *hdir2)
33126 +{
33127 +       au_hn_resume(hdir1);
33128 +       if (hdir1 != hdir2)
33129 +               au_hn_resume(hdir2);
33130 +       lockdep_off();
33131 +       unlock_rename(d1, d2);
33132 +       lockdep_on();
33133 +}
33134 +
33135 +/* ---------------------------------------------------------------------- */
33136 +
33137 +int vfsub_create(struct inode *dir, struct path *path, int mode, bool want_excl)
33138 +{
33139 +       int err;
33140 +       struct dentry *d;
33141 +       struct user_namespace *userns;
33142 +
33143 +       IMustLock(dir);
33144 +
33145 +       d = path->dentry;
33146 +       path->dentry = d->d_parent;
33147 +       err = security_path_mknod(path, d, mode, 0);
33148 +       path->dentry = d;
33149 +       if (unlikely(err))
33150 +               goto out;
33151 +       userns = mnt_user_ns(path->mnt);
33152 +
33153 +       lockdep_off();
33154 +       err = vfs_create(userns, dir, path->dentry, mode, want_excl);
33155 +       lockdep_on();
33156 +       if (!err) {
33157 +               struct path tmp = *path;
33158 +               int did;
33159 +
33160 +               vfsub_update_h_iattr(&tmp, &did);
33161 +               if (did) {
33162 +                       tmp.dentry = path->dentry->d_parent;
33163 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33164 +               }
33165 +               /*ignore*/
33166 +       }
33167 +
33168 +out:
33169 +       return err;
33170 +}
33171 +
33172 +int vfsub_symlink(struct inode *dir, struct path *path, const char *symname)
33173 +{
33174 +       int err;
33175 +       struct dentry *d;
33176 +       struct user_namespace *userns;
33177 +
33178 +       IMustLock(dir);
33179 +
33180 +       d = path->dentry;
33181 +       path->dentry = d->d_parent;
33182 +       err = security_path_symlink(path, d, symname);
33183 +       path->dentry = d;
33184 +       if (unlikely(err))
33185 +               goto out;
33186 +       userns = mnt_user_ns(path->mnt);
33187 +
33188 +       lockdep_off();
33189 +       err = vfs_symlink(userns, dir, path->dentry, symname);
33190 +       lockdep_on();
33191 +       if (!err) {
33192 +               struct path tmp = *path;
33193 +               int did;
33194 +
33195 +               vfsub_update_h_iattr(&tmp, &did);
33196 +               if (did) {
33197 +                       tmp.dentry = path->dentry->d_parent;
33198 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33199 +               }
33200 +               /*ignore*/
33201 +       }
33202 +
33203 +out:
33204 +       return err;
33205 +}
33206 +
33207 +int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev)
33208 +{
33209 +       int err;
33210 +       struct dentry *d;
33211 +       struct user_namespace *userns;
33212 +
33213 +       IMustLock(dir);
33214 +
33215 +       d = path->dentry;
33216 +       path->dentry = d->d_parent;
33217 +       err = security_path_mknod(path, d, mode, new_encode_dev(dev));
33218 +       path->dentry = d;
33219 +       if (unlikely(err))
33220 +               goto out;
33221 +       userns = mnt_user_ns(path->mnt);
33222 +
33223 +       lockdep_off();
33224 +       err = vfs_mknod(userns, dir, path->dentry, mode, dev);
33225 +       lockdep_on();
33226 +       if (!err) {
33227 +               struct path tmp = *path;
33228 +               int did;
33229 +
33230 +               vfsub_update_h_iattr(&tmp, &did);
33231 +               if (did) {
33232 +                       tmp.dentry = path->dentry->d_parent;
33233 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33234 +               }
33235 +               /*ignore*/
33236 +       }
33237 +
33238 +out:
33239 +       return err;
33240 +}
33241 +
33242 +static int au_test_nlink(struct inode *inode)
33243 +{
33244 +       const unsigned int link_max = UINT_MAX >> 1; /* rough margin */
33245 +
33246 +       if (!au_test_fs_no_limit_nlink(inode->i_sb)
33247 +           || inode->i_nlink < link_max)
33248 +               return 0;
33249 +       return -EMLINK;
33250 +}
33251 +
33252 +int vfsub_link(struct dentry *src_dentry, struct inode *dir, struct path *path,
33253 +              struct inode **delegated_inode)
33254 +{
33255 +       int err;
33256 +       struct dentry *d;
33257 +       struct user_namespace *userns;
33258 +
33259 +       IMustLock(dir);
33260 +
33261 +       err = au_test_nlink(d_inode(src_dentry));
33262 +       if (unlikely(err))
33263 +               return err;
33264 +
33265 +       /* we don't call may_linkat() */
33266 +       d = path->dentry;
33267 +       path->dentry = d->d_parent;
33268 +       err = security_path_link(src_dentry, path, d);
33269 +       path->dentry = d;
33270 +       if (unlikely(err))
33271 +               goto out;
33272 +       userns = mnt_user_ns(path->mnt);
33273 +
33274 +       lockdep_off();
33275 +       err = vfs_link(src_dentry, userns, dir, path->dentry, delegated_inode);
33276 +       lockdep_on();
33277 +       if (!err) {
33278 +               struct path tmp = *path;
33279 +               int did;
33280 +
33281 +               /* fuse has different memory inode for the same inumber */
33282 +               vfsub_update_h_iattr(&tmp, &did);
33283 +               if (did) {
33284 +                       tmp.dentry = path->dentry->d_parent;
33285 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33286 +                       tmp.dentry = src_dentry;
33287 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33288 +               }
33289 +               /*ignore*/
33290 +       }
33291 +
33292 +out:
33293 +       return err;
33294 +}
33295 +
33296 +int vfsub_rename(struct inode *src_dir, struct dentry *src_dentry,
33297 +                struct inode *dir, struct path *path,
33298 +                struct inode **delegated_inode, unsigned int flags)
33299 +{
33300 +       int err;
33301 +       struct renamedata rd;
33302 +       struct path tmp = {
33303 +               .mnt    = path->mnt
33304 +       };
33305 +       struct dentry *d;
33306 +
33307 +       IMustLock(dir);
33308 +       IMustLock(src_dir);
33309 +
33310 +       d = path->dentry;
33311 +       path->dentry = d->d_parent;
33312 +       tmp.dentry = src_dentry->d_parent;
33313 +       err = security_path_rename(&tmp, src_dentry, path, d, /*flags*/0);
33314 +       path->dentry = d;
33315 +       if (unlikely(err))
33316 +               goto out;
33317 +
33318 +       rd.old_mnt_userns = mnt_user_ns(path->mnt);
33319 +       rd.old_dir = src_dir;
33320 +       rd.old_dentry = src_dentry;
33321 +       rd.new_mnt_userns = rd.old_mnt_userns;
33322 +       rd.new_dir = dir;
33323 +       rd.new_dentry = path->dentry;
33324 +       rd.delegated_inode = delegated_inode;
33325 +       rd.flags = flags;
33326 +       lockdep_off();
33327 +       err = vfs_rename(&rd);
33328 +       lockdep_on();
33329 +       if (!err) {
33330 +               int did;
33331 +
33332 +               tmp.dentry = d->d_parent;
33333 +               vfsub_update_h_iattr(&tmp, &did);
33334 +               if (did) {
33335 +                       tmp.dentry = src_dentry;
33336 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33337 +                       tmp.dentry = src_dentry->d_parent;
33338 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33339 +               }
33340 +               /*ignore*/
33341 +       }
33342 +
33343 +out:
33344 +       return err;
33345 +}
33346 +
33347 +int vfsub_mkdir(struct inode *dir, struct path *path, int mode)
33348 +{
33349 +       int err;
33350 +       struct dentry *d;
33351 +       struct user_namespace *userns;
33352 +
33353 +       IMustLock(dir);
33354 +
33355 +       d = path->dentry;
33356 +       path->dentry = d->d_parent;
33357 +       err = security_path_mkdir(path, d, mode);
33358 +       path->dentry = d;
33359 +       if (unlikely(err))
33360 +               goto out;
33361 +       userns = mnt_user_ns(path->mnt);
33362 +
33363 +       lockdep_off();
33364 +       err = vfs_mkdir(userns, dir, path->dentry, mode);
33365 +       lockdep_on();
33366 +       if (!err) {
33367 +               struct path tmp = *path;
33368 +               int did;
33369 +
33370 +               vfsub_update_h_iattr(&tmp, &did);
33371 +               if (did) {
33372 +                       tmp.dentry = path->dentry->d_parent;
33373 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33374 +               }
33375 +               /*ignore*/
33376 +       }
33377 +
33378 +out:
33379 +       return err;
33380 +}
33381 +
33382 +int vfsub_rmdir(struct inode *dir, struct path *path)
33383 +{
33384 +       int err;
33385 +       struct dentry *d;
33386 +       struct user_namespace *userns;
33387 +
33388 +       IMustLock(dir);
33389 +
33390 +       d = path->dentry;
33391 +       path->dentry = d->d_parent;
33392 +       err = security_path_rmdir(path, d);
33393 +       path->dentry = d;
33394 +       if (unlikely(err))
33395 +               goto out;
33396 +       userns = mnt_user_ns(path->mnt);
33397 +
33398 +       lockdep_off();
33399 +       err = vfs_rmdir(userns, dir, path->dentry);
33400 +       lockdep_on();
33401 +       if (!err) {
33402 +               struct path tmp = {
33403 +                       .dentry = path->dentry->d_parent,
33404 +                       .mnt    = path->mnt
33405 +               };
33406 +
33407 +               vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
33408 +       }
33409 +
33410 +out:
33411 +       return err;
33412 +}
33413 +
33414 +/* ---------------------------------------------------------------------- */
33415 +
33416 +/* todo: support mmap_sem? */
33417 +ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
33418 +                    loff_t *ppos)
33419 +{
33420 +       ssize_t err;
33421 +
33422 +       lockdep_off();
33423 +       err = vfs_read(file, ubuf, count, ppos);
33424 +       lockdep_on();
33425 +       if (err >= 0)
33426 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33427 +       return err;
33428 +}
33429 +
33430 +ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
33431 +                    loff_t *ppos)
33432 +{
33433 +       ssize_t err;
33434 +
33435 +       lockdep_off();
33436 +       err = kernel_read(file, kbuf, count, ppos);
33437 +       lockdep_on();
33438 +       AuTraceErr(err);
33439 +       if (err >= 0)
33440 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33441 +       return err;
33442 +}
33443 +
33444 +ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
33445 +                     loff_t *ppos)
33446 +{
33447 +       ssize_t err;
33448 +
33449 +       lockdep_off();
33450 +       err = vfs_write(file, ubuf, count, ppos);
33451 +       lockdep_on();
33452 +       if (err >= 0)
33453 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33454 +       return err;
33455 +}
33456 +
33457 +ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count, loff_t *ppos)
33458 +{
33459 +       ssize_t err;
33460 +
33461 +       lockdep_off();
33462 +       err = kernel_write(file, kbuf, count, ppos);
33463 +       lockdep_on();
33464 +       if (err >= 0)
33465 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33466 +       return err;
33467 +}
33468 +
33469 +int vfsub_flush(struct file *file, fl_owner_t id)
33470 +{
33471 +       int err;
33472 +
33473 +       err = 0;
33474 +       if (file->f_op->flush) {
33475 +               if (!au_test_nfs(file->f_path.dentry->d_sb))
33476 +                       err = file->f_op->flush(file, id);
33477 +               else {
33478 +                       lockdep_off();
33479 +                       err = file->f_op->flush(file, id);
33480 +                       lockdep_on();
33481 +               }
33482 +               if (!err)
33483 +                       vfsub_update_h_iattr(&file->f_path, /*did*/NULL);
33484 +               /*ignore*/
33485 +       }
33486 +       return err;
33487 +}
33488 +
33489 +int vfsub_iterate_dir(struct file *file, struct dir_context *ctx)
33490 +{
33491 +       int err;
33492 +
33493 +       AuDbg("%pD, ctx{%ps, %llu}\n", file, ctx->actor, ctx->pos);
33494 +
33495 +       lockdep_off();
33496 +       err = iterate_dir(file, ctx);
33497 +       lockdep_on();
33498 +       if (err >= 0)
33499 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33500 +
33501 +       return err;
33502 +}
33503 +
33504 +long vfsub_splice_to(struct file *in, loff_t *ppos,
33505 +                    struct pipe_inode_info *pipe, size_t len,
33506 +                    unsigned int flags)
33507 +{
33508 +       long err;
33509 +
33510 +       lockdep_off();
33511 +       err = do_splice_to(in, ppos, pipe, len, flags);
33512 +       lockdep_on();
33513 +       file_accessed(in);
33514 +       if (err >= 0)
33515 +               vfsub_update_h_iattr(&in->f_path, /*did*/NULL); /*ignore*/
33516 +       return err;
33517 +}
33518 +
33519 +long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
33520 +                      loff_t *ppos, size_t len, unsigned int flags)
33521 +{
33522 +       long err;
33523 +
33524 +       lockdep_off();
33525 +       err = do_splice_from(pipe, out, ppos, len, flags);
33526 +       lockdep_on();
33527 +       if (err >= 0)
33528 +               vfsub_update_h_iattr(&out->f_path, /*did*/NULL); /*ignore*/
33529 +       return err;
33530 +}
33531 +
33532 +int vfsub_fsync(struct file *file, struct path *path, int datasync)
33533 +{
33534 +       int err;
33535 +
33536 +       /* file can be NULL */
33537 +       lockdep_off();
33538 +       err = vfs_fsync(file, datasync);
33539 +       lockdep_on();
33540 +       if (!err) {
33541 +               if (!path) {
33542 +                       AuDebugOn(!file);
33543 +                       path = &file->f_path;
33544 +               }
33545 +               vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
33546 +       }
33547 +       return err;
33548 +}
33549 +
33550 +/* cf. open.c:do_sys_truncate() and do_sys_ftruncate() */
33551 +int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
33552 +               struct file *h_file)
33553 +{
33554 +       int err;
33555 +       struct inode *h_inode;
33556 +       struct super_block *h_sb;
33557 +       struct user_namespace *h_userns;
33558 +
33559 +       if (!h_file) {
33560 +               err = vfsub_truncate(h_path, length);
33561 +               goto out;
33562 +       }
33563 +
33564 +       h_inode = d_inode(h_path->dentry);
33565 +       h_sb = h_inode->i_sb;
33566 +       lockdep_off();
33567 +       sb_start_write(h_sb);
33568 +       lockdep_on();
33569 +       err = security_path_truncate(h_path);
33570 +       if (!err) {
33571 +               h_userns = mnt_user_ns(h_path->mnt);
33572 +               lockdep_off();
33573 +               err = do_truncate(h_userns, h_path->dentry, length, attr,
33574 +                                 h_file);
33575 +               lockdep_on();
33576 +       }
33577 +       lockdep_off();
33578 +       sb_end_write(h_sb);
33579 +       lockdep_on();
33580 +
33581 +out:
33582 +       return err;
33583 +}
33584 +
33585 +/* ---------------------------------------------------------------------- */
33586 +
33587 +struct au_vfsub_mkdir_args {
33588 +       int *errp;
33589 +       struct inode *dir;
33590 +       struct path *path;
33591 +       int mode;
33592 +};
33593 +
33594 +static void au_call_vfsub_mkdir(void *args)
33595 +{
33596 +       struct au_vfsub_mkdir_args *a = args;
33597 +       *a->errp = vfsub_mkdir(a->dir, a->path, a->mode);
33598 +}
33599 +
33600 +int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode)
33601 +{
33602 +       int err, do_sio, wkq_err;
33603 +       struct user_namespace *userns;
33604 +
33605 +       userns = mnt_user_ns(path->mnt);
33606 +       do_sio = au_test_h_perm_sio(userns, dir, MAY_EXEC | MAY_WRITE);
33607 +       if (!do_sio) {
33608 +               lockdep_off();
33609 +               err = vfsub_mkdir(dir, path, mode);
33610 +               lockdep_on();
33611 +       } else {
33612 +               struct au_vfsub_mkdir_args args = {
33613 +                       .errp   = &err,
33614 +                       .dir    = dir,
33615 +                       .path   = path,
33616 +                       .mode   = mode
33617 +               };
33618 +               wkq_err = au_wkq_wait(au_call_vfsub_mkdir, &args);
33619 +               if (unlikely(wkq_err))
33620 +                       err = wkq_err;
33621 +       }
33622 +
33623 +       return err;
33624 +}
33625 +
33626 +struct au_vfsub_rmdir_args {
33627 +       int *errp;
33628 +       struct inode *dir;
33629 +       struct path *path;
33630 +};
33631 +
33632 +static void au_call_vfsub_rmdir(void *args)
33633 +{
33634 +       struct au_vfsub_rmdir_args *a = args;
33635 +       *a->errp = vfsub_rmdir(a->dir, a->path);
33636 +}
33637 +
33638 +int vfsub_sio_rmdir(struct inode *dir, struct path *path)
33639 +{
33640 +       int err, do_sio, wkq_err;
33641 +       struct user_namespace *userns;
33642 +
33643 +       userns = mnt_user_ns(path->mnt);
33644 +       do_sio = au_test_h_perm_sio(userns, dir, MAY_EXEC | MAY_WRITE);
33645 +       if (!do_sio) {
33646 +               lockdep_off();
33647 +               err = vfsub_rmdir(dir, path);
33648 +               lockdep_on();
33649 +       } else {
33650 +               struct au_vfsub_rmdir_args args = {
33651 +                       .errp   = &err,
33652 +                       .dir    = dir,
33653 +                       .path   = path
33654 +               };
33655 +               wkq_err = au_wkq_wait(au_call_vfsub_rmdir, &args);
33656 +               if (unlikely(wkq_err))
33657 +                       err = wkq_err;
33658 +       }
33659 +
33660 +       return err;
33661 +}
33662 +
33663 +/* ---------------------------------------------------------------------- */
33664 +
33665 +struct notify_change_args {
33666 +       int *errp;
33667 +       struct path *path;
33668 +       struct iattr *ia;
33669 +       struct inode **delegated_inode;
33670 +};
33671 +
33672 +static void call_notify_change(void *args)
33673 +{
33674 +       struct notify_change_args *a = args;
33675 +       struct inode *h_inode;
33676 +       struct user_namespace *userns;
33677 +
33678 +       h_inode = d_inode(a->path->dentry);
33679 +       IMustLock(h_inode);
33680 +
33681 +       *a->errp = -EPERM;
33682 +       if (!IS_IMMUTABLE(h_inode) && !IS_APPEND(h_inode)) {
33683 +               userns = mnt_user_ns(a->path->mnt);
33684 +               lockdep_off();
33685 +               *a->errp = notify_change(userns, a->path->dentry, a->ia,
33686 +                                        a->delegated_inode);
33687 +               lockdep_on();
33688 +               if (!*a->errp)
33689 +                       vfsub_update_h_iattr(a->path, /*did*/NULL); /*ignore*/
33690 +       }
33691 +       AuTraceErr(*a->errp);
33692 +}
33693 +
33694 +int vfsub_notify_change(struct path *path, struct iattr *ia,
33695 +                       struct inode **delegated_inode)
33696 +{
33697 +       int err;
33698 +       struct notify_change_args args = {
33699 +               .errp                   = &err,
33700 +               .path                   = path,
33701 +               .ia                     = ia,
33702 +               .delegated_inode        = delegated_inode
33703 +       };
33704 +
33705 +       call_notify_change(&args);
33706 +
33707 +       return err;
33708 +}
33709 +
33710 +int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
33711 +                           struct inode **delegated_inode)
33712 +{
33713 +       int err, wkq_err;
33714 +       struct notify_change_args args = {
33715 +               .errp                   = &err,
33716 +               .path                   = path,
33717 +               .ia                     = ia,
33718 +               .delegated_inode        = delegated_inode
33719 +       };
33720 +
33721 +       wkq_err = au_wkq_wait(call_notify_change, &args);
33722 +       if (unlikely(wkq_err))
33723 +               err = wkq_err;
33724 +
33725 +       return err;
33726 +}
33727 +
33728 +/* ---------------------------------------------------------------------- */
33729 +
33730 +struct unlink_args {
33731 +       int *errp;
33732 +       struct inode *dir;
33733 +       struct path *path;
33734 +       struct inode **delegated_inode;
33735 +};
33736 +
33737 +static void call_unlink(void *args)
33738 +{
33739 +       struct unlink_args *a = args;
33740 +       struct dentry *d = a->path->dentry;
33741 +       struct inode *h_inode;
33742 +       struct user_namespace *userns;
33743 +       const int stop_sillyrename = (au_test_nfs(d->d_sb)
33744 +                                     && au_dcount(d) == 1);
33745 +
33746 +       IMustLock(a->dir);
33747 +
33748 +       a->path->dentry = d->d_parent;
33749 +       *a->errp = security_path_unlink(a->path, d);
33750 +       a->path->dentry = d;
33751 +       if (unlikely(*a->errp))
33752 +               return;
33753 +
33754 +       if (!stop_sillyrename)
33755 +               dget(d);
33756 +       h_inode = NULL;
33757 +       if (d_is_positive(d)) {
33758 +               h_inode = d_inode(d);
33759 +               ihold(h_inode);
33760 +       }
33761 +
33762 +       userns = mnt_user_ns(a->path->mnt);
33763 +       lockdep_off();
33764 +       *a->errp = vfs_unlink(userns, a->dir, d, a->delegated_inode);
33765 +       lockdep_on();
33766 +       if (!*a->errp) {
33767 +               struct path tmp = {
33768 +                       .dentry = d->d_parent,
33769 +                       .mnt    = a->path->mnt
33770 +               };
33771 +               vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
33772 +       }
33773 +
33774 +       if (!stop_sillyrename)
33775 +               dput(d);
33776 +       if (h_inode)
33777 +               iput(h_inode);
33778 +
33779 +       AuTraceErr(*a->errp);
33780 +}
33781 +
33782 +/*
33783 + * @dir: must be locked.
33784 + * @dentry: target dentry.
33785 + */
33786 +int vfsub_unlink(struct inode *dir, struct path *path,
33787 +                struct inode **delegated_inode, int force)
33788 +{
33789 +       int err;
33790 +       struct unlink_args args = {
33791 +               .errp                   = &err,
33792 +               .dir                    = dir,
33793 +               .path                   = path,
33794 +               .delegated_inode        = delegated_inode
33795 +       };
33796 +
33797 +       if (!force)
33798 +               call_unlink(&args);
33799 +       else {
33800 +               int wkq_err;
33801 +
33802 +               wkq_err = au_wkq_wait(call_unlink, &args);
33803 +               if (unlikely(wkq_err))
33804 +                       err = wkq_err;
33805 +       }
33806 +
33807 +       return err;
33808 +}
33809 diff -urN /usr/share/empty/fs/aufs/vfsub.h linux/fs/aufs/vfsub.h
33810 --- /usr/share/empty/fs/aufs/vfsub.h    1970-01-01 01:00:00.000000000 +0100
33811 +++ linux/fs/aufs/vfsub.h       2022-03-21 14:49:05.733299677 +0100
33812 @@ -0,0 +1,358 @@
33813 +/* SPDX-License-Identifier: GPL-2.0 */
33814 +/*
33815 + * Copyright (C) 2005-2021 Junjiro R. Okajima
33816 + *
33817 + * This program, aufs is free software; you can redistribute it and/or modify
33818 + * it under the terms of the GNU General Public License as published by
33819 + * the Free Software Foundation; either version 2 of the License, or
33820 + * (at your option) any later version.
33821 + *
33822 + * This program is distributed in the hope that it will be useful,
33823 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
33824 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33825 + * GNU General Public License for more details.
33826 + *
33827 + * You should have received a copy of the GNU General Public License
33828 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
33829 + */
33830 +
33831 +/*
33832 + * sub-routines for VFS
33833 + */
33834 +
33835 +#ifndef __AUFS_VFSUB_H__
33836 +#define __AUFS_VFSUB_H__
33837 +
33838 +#ifdef __KERNEL__
33839 +
33840 +#include <linux/fs.h>
33841 +#include <linux/mount.h>
33842 +#include <linux/posix_acl.h>
33843 +#include <linux/xattr.h>
33844 +#include "debug.h"
33845 +
33846 +/* copied from linux/fs/internal.h */
33847 +/* todo: BAD approach!! */
33848 +extern void __mnt_drop_write(struct vfsmount *);
33849 +extern struct file *alloc_empty_file(int, const struct cred *);
33850 +
33851 +/* ---------------------------------------------------------------------- */
33852 +
33853 +/* lock subclass for lower inode */
33854 +/* default MAX_LOCKDEP_SUBCLASSES(8) is not enough */
33855 +/* reduce? gave up. */
33856 +enum {
33857 +       AuLsc_I_Begin = I_MUTEX_PARENT2, /* 5 */
33858 +       AuLsc_I_PARENT,         /* lower inode, parent first */
33859 +       AuLsc_I_PARENT2,        /* copyup dirs */
33860 +       AuLsc_I_PARENT3,        /* copyup wh */
33861 +       AuLsc_I_CHILD,
33862 +       AuLsc_I_CHILD2,
33863 +       AuLsc_I_End
33864 +};
33865 +
33866 +/* to debug easier, do not make them inlined functions */
33867 +#define MtxMustLock(mtx)       AuDebugOn(!mutex_is_locked(mtx))
33868 +#define IMustLock(i)           AuDebugOn(!inode_is_locked(i))
33869 +
33870 +/* ---------------------------------------------------------------------- */
33871 +
33872 +static inline void vfsub_drop_nlink(struct inode *inode)
33873 +{
33874 +       AuDebugOn(!inode->i_nlink);
33875 +       drop_nlink(inode);
33876 +}
33877 +
33878 +static inline void vfsub_dead_dir(struct inode *inode)
33879 +{
33880 +       AuDebugOn(!S_ISDIR(inode->i_mode));
33881 +       inode->i_flags |= S_DEAD;
33882 +       clear_nlink(inode);
33883 +}
33884 +
33885 +static inline int vfsub_native_ro(struct inode *inode)
33886 +{
33887 +       return sb_rdonly(inode->i_sb)
33888 +               || IS_RDONLY(inode)
33889 +               /* || IS_APPEND(inode) */
33890 +               || IS_IMMUTABLE(inode);
33891 +}
33892 +
33893 +#ifdef CONFIG_AUFS_BR_FUSE
33894 +int vfsub_test_mntns(struct vfsmount *mnt, struct super_block *h_sb);
33895 +#else
33896 +AuStubInt0(vfsub_test_mntns, struct vfsmount *mnt, struct super_block *h_sb);
33897 +#endif
33898 +
33899 +int vfsub_sync_filesystem(struct super_block *h_sb);
33900 +
33901 +/* ---------------------------------------------------------------------- */
33902 +
33903 +int vfsub_update_h_iattr(struct path *h_path, int *did);
33904 +struct file *vfsub_dentry_open(struct path *path, int flags);
33905 +struct file *vfsub_filp_open(const char *path, int oflags, int mode);
33906 +struct au_branch;
33907 +struct vfsub_aopen_args {
33908 +       struct file             *file;
33909 +       unsigned int            open_flag;
33910 +       umode_t                 create_mode;
33911 +       struct au_branch        *br;
33912 +};
33913 +int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
33914 +                     struct vfsub_aopen_args *args);
33915 +int vfsub_kern_path(const char *name, unsigned int flags, struct path *path);
33916 +
33917 +struct dentry *vfsub_lookup_one_len_unlocked(const char *name,
33918 +                                            struct path *ppath, int len);
33919 +struct dentry *vfsub_lookup_one_len(const char *name, struct path *ppath,
33920 +                                   int len);
33921 +
33922 +struct vfsub_lkup_one_args {
33923 +       struct dentry **errp;
33924 +       struct qstr *name;
33925 +       struct path *ppath;
33926 +};
33927 +
33928 +static inline struct dentry *vfsub_lkup_one(struct qstr *name,
33929 +                                           struct path *ppath)
33930 +{
33931 +       return vfsub_lookup_one_len(name->name, ppath, name->len);
33932 +}
33933 +
33934 +void vfsub_call_lkup_one(void *args);
33935 +
33936 +/* ---------------------------------------------------------------------- */
33937 +
33938 +static inline int vfsub_mnt_want_write(struct vfsmount *mnt)
33939 +{
33940 +       int err;
33941 +
33942 +       lockdep_off();
33943 +       err = mnt_want_write(mnt);
33944 +       lockdep_on();
33945 +       return err;
33946 +}
33947 +
33948 +static inline void vfsub_mnt_drop_write(struct vfsmount *mnt)
33949 +{
33950 +       lockdep_off();
33951 +       mnt_drop_write(mnt);
33952 +       lockdep_on();
33953 +}
33954 +
33955 +#if 0 /* reserved */
33956 +static inline void vfsub_mnt_drop_write_file(struct file *file)
33957 +{
33958 +       lockdep_off();
33959 +       mnt_drop_write_file(file);
33960 +       lockdep_on();
33961 +}
33962 +#endif
33963 +
33964 +/* ---------------------------------------------------------------------- */
33965 +
33966 +struct au_hinode;
33967 +struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
33968 +                                struct dentry *d2, struct au_hinode *hdir2);
33969 +void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
33970 +                        struct dentry *d2, struct au_hinode *hdir2);
33971 +
33972 +int vfsub_create(struct inode *dir, struct path *path, int mode,
33973 +                bool want_excl);
33974 +int vfsub_symlink(struct inode *dir, struct path *path,
33975 +                 const char *symname);
33976 +int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev);
33977 +int vfsub_link(struct dentry *src_dentry, struct inode *dir,
33978 +              struct path *path, struct inode **delegated_inode);
33979 +int vfsub_rename(struct inode *src_hdir, struct dentry *src_dentry,
33980 +                struct inode *hdir, struct path *path,
33981 +                struct inode **delegated_inode, unsigned int flags);
33982 +int vfsub_mkdir(struct inode *dir, struct path *path, int mode);
33983 +int vfsub_rmdir(struct inode *dir, struct path *path);
33984 +
33985 +/* ---------------------------------------------------------------------- */
33986 +
33987 +ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
33988 +                    loff_t *ppos);
33989 +ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
33990 +                       loff_t *ppos);
33991 +ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
33992 +                     loff_t *ppos);
33993 +ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count,
33994 +                     loff_t *ppos);
33995 +int vfsub_flush(struct file *file, fl_owner_t id);
33996 +int vfsub_iterate_dir(struct file *file, struct dir_context *ctx);
33997 +
33998 +static inline loff_t vfsub_f_size_read(struct file *file)
33999 +{
34000 +       return i_size_read(file_inode(file));
34001 +}
34002 +
34003 +static inline unsigned int vfsub_file_flags(struct file *file)
34004 +{
34005 +       unsigned int flags;
34006 +
34007 +       spin_lock(&file->f_lock);
34008 +       flags = file->f_flags;
34009 +       spin_unlock(&file->f_lock);
34010 +
34011 +       return flags;
34012 +}
34013 +
34014 +static inline int vfsub_file_execed(struct file *file)
34015 +{
34016 +       /* todo: direct access f_flags */
34017 +       return !!(vfsub_file_flags(file) & __FMODE_EXEC);
34018 +}
34019 +
34020 +#if 0 /* reserved */
34021 +static inline void vfsub_file_accessed(struct file *h_file)
34022 +{
34023 +       file_accessed(h_file);
34024 +       vfsub_update_h_iattr(&h_file->f_path, /*did*/NULL); /*ignore*/
34025 +}
34026 +#endif
34027 +
34028 +#if 0 /* reserved */
34029 +static inline void vfsub_touch_atime(struct vfsmount *h_mnt,
34030 +                                    struct dentry *h_dentry)
34031 +{
34032 +       struct path h_path = {
34033 +               .dentry = h_dentry,
34034 +               .mnt    = h_mnt
34035 +       };
34036 +       touch_atime(&h_path);
34037 +       vfsub_update_h_iattr(&h_path, /*did*/NULL); /*ignore*/
34038 +}
34039 +#endif
34040 +
34041 +static inline int vfsub_update_time(struct inode *h_inode,
34042 +                                   struct timespec64 *ts, int flags)
34043 +{
34044 +       return inode_update_time(h_inode, ts, flags);
34045 +       /* no vfsub_update_h_iattr() since we don't have struct path */
34046 +}
34047 +
34048 +#ifdef CONFIG_FS_POSIX_ACL
34049 +static inline int vfsub_acl_chmod(struct user_namespace *h_userns,
34050 +                                 struct inode *h_inode, umode_t h_mode)
34051 +{
34052 +       int err;
34053 +
34054 +       err = posix_acl_chmod(h_userns, h_inode, h_mode);
34055 +       if (err == -EOPNOTSUPP)
34056 +               err = 0;
34057 +       return err;
34058 +}
34059 +#else
34060 +AuStubInt0(vfsub_acl_chmod, struct user_namespace *h_userns,
34061 +          struct inode *h_inode, umode_t h_mode);
34062 +#endif
34063 +
34064 +long vfsub_splice_to(struct file *in, loff_t *ppos,
34065 +                    struct pipe_inode_info *pipe, size_t len,
34066 +                    unsigned int flags);
34067 +long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
34068 +                      loff_t *ppos, size_t len, unsigned int flags);
34069 +
34070 +static inline long vfsub_truncate(struct path *path, loff_t length)
34071 +{
34072 +       long err;
34073 +
34074 +       lockdep_off();
34075 +       err = vfs_truncate(path, length);
34076 +       lockdep_on();
34077 +       return err;
34078 +}
34079 +
34080 +int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
34081 +               struct file *h_file);
34082 +int vfsub_fsync(struct file *file, struct path *path, int datasync);
34083 +
34084 +/*
34085 + * re-use branch fs's ioctl(FICLONE) while aufs itself doesn't support such
34086 + * ioctl.
34087 + */
34088 +static inline loff_t vfsub_clone_file_range(struct file *src, struct file *dst,
34089 +                                           loff_t len)
34090 +{
34091 +       loff_t err;
34092 +
34093 +       lockdep_off();
34094 +       err = vfs_clone_file_range(src, 0, dst, 0, len, /*remap_flags*/0);
34095 +       lockdep_on();
34096 +
34097 +       return err;
34098 +}
34099 +
34100 +/* copy_file_range(2) is a systemcall */
34101 +static inline ssize_t vfsub_copy_file_range(struct file *src, loff_t src_pos,
34102 +                                           struct file *dst, loff_t dst_pos,
34103 +                                           size_t len, unsigned int flags)
34104 +{
34105 +       ssize_t ssz;
34106 +
34107 +       lockdep_off();
34108 +       ssz = vfs_copy_file_range(src, src_pos, dst, dst_pos, len, flags);
34109 +       lockdep_on();
34110 +
34111 +       return ssz;
34112 +}
34113 +
34114 +/* ---------------------------------------------------------------------- */
34115 +
34116 +static inline loff_t vfsub_llseek(struct file *file, loff_t offset, int origin)
34117 +{
34118 +       loff_t err;
34119 +
34120 +       lockdep_off();
34121 +       err = vfs_llseek(file, offset, origin);
34122 +       lockdep_on();
34123 +       return err;
34124 +}
34125 +
34126 +/* ---------------------------------------------------------------------- */
34127 +
34128 +int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode);
34129 +int vfsub_sio_rmdir(struct inode *dir, struct path *path);
34130 +int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
34131 +                           struct inode **delegated_inode);
34132 +int vfsub_notify_change(struct path *path, struct iattr *ia,
34133 +                       struct inode **delegated_inode);
34134 +int vfsub_unlink(struct inode *dir, struct path *path,
34135 +                struct inode **delegated_inode, int force);
34136 +
34137 +static inline int vfsub_getattr(const struct path *path, struct kstat *st)
34138 +{
34139 +       return vfs_getattr(path, st, STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
34140 +}
34141 +
34142 +/* ---------------------------------------------------------------------- */
34143 +
34144 +static inline int vfsub_setxattr(struct user_namespace *userns,
34145 +                                struct dentry *dentry, const char *name,
34146 +                                const void *value, size_t size, int flags)
34147 +{
34148 +       int err;
34149 +
34150 +       lockdep_off();
34151 +       err = vfs_setxattr(userns, dentry, name, value, size, flags);
34152 +       lockdep_on();
34153 +
34154 +       return err;
34155 +}
34156 +
34157 +static inline int vfsub_removexattr(struct user_namespace *userns,
34158 +                                   struct dentry *dentry, const char *name)
34159 +{
34160 +       int err;
34161 +
34162 +       lockdep_off();
34163 +       err = vfs_removexattr(userns, dentry, name);
34164 +       lockdep_on();
34165 +
34166 +       return err;
34167 +}
34168 +
34169 +#endif /* __KERNEL__ */
34170 +#endif /* __AUFS_VFSUB_H__ */
34171 diff -urN /usr/share/empty/fs/aufs/wbr_policy.c linux/fs/aufs/wbr_policy.c
34172 --- /usr/share/empty/fs/aufs/wbr_policy.c       1970-01-01 01:00:00.000000000 +0100
34173 +++ linux/fs/aufs/wbr_policy.c  2022-03-21 14:49:05.733299677 +0100
34174 @@ -0,0 +1,830 @@
34175 +// SPDX-License-Identifier: GPL-2.0
34176 +/*
34177 + * Copyright (C) 2005-2021 Junjiro R. Okajima
34178 + *
34179 + * This program, aufs is free software; you can redistribute it and/or modify
34180 + * it under the terms of the GNU General Public License as published by
34181 + * the Free Software Foundation; either version 2 of the License, or
34182 + * (at your option) any later version.
34183 + *
34184 + * This program is distributed in the hope that it will be useful,
34185 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
34186 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
34187 + * GNU General Public License for more details.
34188 + *
34189 + * You should have received a copy of the GNU General Public License
34190 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
34191 + */
34192 +
34193 +/*
34194 + * policies for selecting one among multiple writable branches
34195 + */
34196 +
34197 +#include <linux/statfs.h>
34198 +#include "aufs.h"
34199 +
34200 +/* subset of cpup_attr() */
34201 +static noinline_for_stack
34202 +int au_cpdown_attr(struct path *h_path, struct dentry *h_src)
34203 +{
34204 +       int err, sbits;
34205 +       struct iattr ia;
34206 +       struct inode *h_isrc;
34207 +
34208 +       h_isrc = d_inode(h_src);
34209 +       ia.ia_valid = ATTR_FORCE | ATTR_MODE | ATTR_UID | ATTR_GID;
34210 +       ia.ia_mode = h_isrc->i_mode;
34211 +       ia.ia_uid = h_isrc->i_uid;
34212 +       ia.ia_gid = h_isrc->i_gid;
34213 +       sbits = !!(ia.ia_mode & (S_ISUID | S_ISGID));
34214 +       au_cpup_attr_flags(d_inode(h_path->dentry), h_isrc->i_flags);
34215 +       /* no delegation since it is just created */
34216 +       err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
34217 +
34218 +       /* is this nfs only? */
34219 +       if (!err && sbits && au_test_nfs(h_path->dentry->d_sb)) {
34220 +               ia.ia_valid = ATTR_FORCE | ATTR_MODE;
34221 +               ia.ia_mode = h_isrc->i_mode;
34222 +               err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
34223 +       }
34224 +
34225 +       return err;
34226 +}
34227 +
34228 +#define AuCpdown_PARENT_OPQ    1
34229 +#define AuCpdown_WHED          (1 << 1)
34230 +#define AuCpdown_MADE_DIR      (1 << 2)
34231 +#define AuCpdown_DIROPQ                (1 << 3)
34232 +#define au_ftest_cpdown(flags, name)   ((flags) & AuCpdown_##name)
34233 +#define au_fset_cpdown(flags, name) \
34234 +       do { (flags) |= AuCpdown_##name; } while (0)
34235 +#define au_fclr_cpdown(flags, name) \
34236 +       do { (flags) &= ~AuCpdown_##name; } while (0)
34237 +
34238 +static int au_cpdown_dir_opq(struct dentry *dentry, aufs_bindex_t bdst,
34239 +                            unsigned int *flags)
34240 +{
34241 +       int err;
34242 +       struct dentry *opq_dentry;
34243 +
34244 +       opq_dentry = au_diropq_create(dentry, bdst);
34245 +       err = PTR_ERR(opq_dentry);
34246 +       if (IS_ERR(opq_dentry))
34247 +               goto out;
34248 +       dput(opq_dentry);
34249 +       au_fset_cpdown(*flags, DIROPQ);
34250 +
34251 +out:
34252 +       return err;
34253 +}
34254 +
34255 +static int au_cpdown_dir_wh(struct dentry *dentry, struct dentry *h_parent,
34256 +                           struct inode *dir, aufs_bindex_t bdst)
34257 +{
34258 +       int err;
34259 +       struct path h_path;
34260 +       struct au_branch *br;
34261 +
34262 +       br = au_sbr(dentry->d_sb, bdst);
34263 +       h_path.dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
34264 +       err = PTR_ERR(h_path.dentry);
34265 +       if (IS_ERR(h_path.dentry))
34266 +               goto out;
34267 +
34268 +       err = 0;
34269 +       if (d_is_positive(h_path.dentry)) {
34270 +               h_path.mnt = au_br_mnt(br);
34271 +               err = au_wh_unlink_dentry(au_h_iptr(dir, bdst), &h_path,
34272 +                                         dentry);
34273 +       }
34274 +       dput(h_path.dentry);
34275 +
34276 +out:
34277 +       return err;
34278 +}
34279 +
34280 +static int au_cpdown_dir(struct dentry *dentry, aufs_bindex_t bdst,
34281 +                        struct au_pin *pin,
34282 +                        struct dentry *h_parent, void *arg)
34283 +{
34284 +       int err, rerr;
34285 +       aufs_bindex_t bopq, btop;
34286 +       struct path h_path;
34287 +       struct dentry *parent;
34288 +       struct inode *h_dir, *h_inode, *inode, *dir;
34289 +       unsigned int *flags = arg;
34290 +
34291 +       btop = au_dbtop(dentry);
34292 +       /* dentry is di-locked */
34293 +       parent = dget_parent(dentry);
34294 +       dir = d_inode(parent);
34295 +       h_dir = d_inode(h_parent);
34296 +       AuDebugOn(h_dir != au_h_iptr(dir, bdst));
34297 +       IMustLock(h_dir);
34298 +
34299 +       err = au_lkup_neg(dentry, bdst, /*wh*/0);
34300 +       if (unlikely(err < 0))
34301 +               goto out;
34302 +       h_path.dentry = au_h_dptr(dentry, bdst);
34303 +       h_path.mnt = au_sbr_mnt(dentry->d_sb, bdst);
34304 +       err = vfsub_sio_mkdir(au_h_iptr(dir, bdst), &h_path, 0755);
34305 +       if (unlikely(err))
34306 +               goto out_put;
34307 +       au_fset_cpdown(*flags, MADE_DIR);
34308 +
34309 +       bopq = au_dbdiropq(dentry);
34310 +       au_fclr_cpdown(*flags, WHED);
34311 +       au_fclr_cpdown(*flags, DIROPQ);
34312 +       if (au_dbwh(dentry) == bdst)
34313 +               au_fset_cpdown(*flags, WHED);
34314 +       if (!au_ftest_cpdown(*flags, PARENT_OPQ) && bopq <= bdst)
34315 +               au_fset_cpdown(*flags, PARENT_OPQ);
34316 +       h_inode = d_inode(h_path.dentry);
34317 +       inode_lock_nested(h_inode, AuLsc_I_CHILD);
34318 +       if (au_ftest_cpdown(*flags, WHED)) {
34319 +               err = au_cpdown_dir_opq(dentry, bdst, flags);
34320 +               if (unlikely(err)) {
34321 +                       inode_unlock(h_inode);
34322 +                       goto out_dir;
34323 +               }
34324 +       }
34325 +
34326 +       err = au_cpdown_attr(&h_path, au_h_dptr(dentry, btop));
34327 +       inode_unlock(h_inode);
34328 +       if (unlikely(err))
34329 +               goto out_opq;
34330 +
34331 +       if (au_ftest_cpdown(*flags, WHED)) {
34332 +               err = au_cpdown_dir_wh(dentry, h_parent, dir, bdst);
34333 +               if (unlikely(err))
34334 +                       goto out_opq;
34335 +       }
34336 +
34337 +       inode = d_inode(dentry);
34338 +       if (au_ibbot(inode) < bdst)
34339 +               au_set_ibbot(inode, bdst);
34340 +       au_set_h_iptr(inode, bdst, au_igrab(h_inode),
34341 +                     au_hi_flags(inode, /*isdir*/1));
34342 +       au_fhsm_wrote(dentry->d_sb, bdst, /*force*/0);
34343 +       goto out; /* success */
34344 +
34345 +       /* revert */
34346 +out_opq:
34347 +       if (au_ftest_cpdown(*flags, DIROPQ)) {
34348 +               inode_lock_nested(h_inode, AuLsc_I_CHILD);
34349 +               rerr = au_diropq_remove(dentry, bdst);
34350 +               inode_unlock(h_inode);
34351 +               if (unlikely(rerr)) {
34352 +                       AuIOErr("failed removing diropq for %pd b%d (%d)\n",
34353 +                               dentry, bdst, rerr);
34354 +                       err = -EIO;
34355 +                       goto out;
34356 +               }
34357 +       }
34358 +out_dir:
34359 +       if (au_ftest_cpdown(*flags, MADE_DIR)) {
34360 +               rerr = vfsub_sio_rmdir(au_h_iptr(dir, bdst), &h_path);
34361 +               if (unlikely(rerr)) {
34362 +                       AuIOErr("failed removing %pd b%d (%d)\n",
34363 +                               dentry, bdst, rerr);
34364 +                       err = -EIO;
34365 +               }
34366 +       }
34367 +out_put:
34368 +       au_set_h_dptr(dentry, bdst, NULL);
34369 +       if (au_dbbot(dentry) == bdst)
34370 +               au_update_dbbot(dentry);
34371 +out:
34372 +       dput(parent);
34373 +       return err;
34374 +}
34375 +
34376 +int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst)
34377 +{
34378 +       int err;
34379 +       unsigned int flags;
34380 +
34381 +       flags = 0;
34382 +       err = au_cp_dirs(dentry, bdst, au_cpdown_dir, &flags);
34383 +
34384 +       return err;
34385 +}
34386 +
34387 +/* ---------------------------------------------------------------------- */
34388 +
34389 +/* policies for create */
34390 +
34391 +int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex)
34392 +{
34393 +       int err, i, j, ndentry;
34394 +       aufs_bindex_t bopq;
34395 +       struct au_dcsub_pages dpages;
34396 +       struct au_dpage *dpage;
34397 +       struct dentry **dentries, *parent, *d;
34398 +
34399 +       err = au_dpages_init(&dpages, GFP_NOFS);
34400 +       if (unlikely(err))
34401 +               goto out;
34402 +       parent = dget_parent(dentry);
34403 +       err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/0);
34404 +       if (unlikely(err))
34405 +               goto out_free;
34406 +
34407 +       err = bindex;
34408 +       for (i = 0; i < dpages.ndpage; i++) {
34409 +               dpage = dpages.dpages + i;
34410 +               dentries = dpage->dentries;
34411 +               ndentry = dpage->ndentry;
34412 +               for (j = 0; j < ndentry; j++) {
34413 +                       d = dentries[j];
34414 +                       di_read_lock_parent2(d, !AuLock_IR);
34415 +                       bopq = au_dbdiropq(d);
34416 +                       di_read_unlock(d, !AuLock_IR);
34417 +                       if (bopq >= 0 && bopq < err)
34418 +                               err = bopq;
34419 +               }
34420 +       }
34421 +
34422 +out_free:
34423 +       dput(parent);
34424 +       au_dpages_free(&dpages);
34425 +out:
34426 +       return err;
34427 +}
34428 +
34429 +static int au_wbr_bu(struct super_block *sb, aufs_bindex_t bindex)
34430 +{
34431 +       for (; bindex >= 0; bindex--)
34432 +               if (!au_br_rdonly(au_sbr(sb, bindex)))
34433 +                       return bindex;
34434 +       return -EROFS;
34435 +}
34436 +
34437 +/* top down parent */
34438 +static int au_wbr_create_tdp(struct dentry *dentry,
34439 +                            unsigned int flags __maybe_unused)
34440 +{
34441 +       int err;
34442 +       aufs_bindex_t btop, bindex;
34443 +       struct super_block *sb;
34444 +       struct dentry *parent, *h_parent;
34445 +
34446 +       sb = dentry->d_sb;
34447 +       btop = au_dbtop(dentry);
34448 +       err = btop;
34449 +       if (!au_br_rdonly(au_sbr(sb, btop)))
34450 +               goto out;
34451 +
34452 +       err = -EROFS;
34453 +       parent = dget_parent(dentry);
34454 +       for (bindex = au_dbtop(parent); bindex < btop; bindex++) {
34455 +               h_parent = au_h_dptr(parent, bindex);
34456 +               if (!h_parent || d_is_negative(h_parent))
34457 +                       continue;
34458 +
34459 +               if (!au_br_rdonly(au_sbr(sb, bindex))) {
34460 +                       err = bindex;
34461 +                       break;
34462 +               }
34463 +       }
34464 +       dput(parent);
34465 +
34466 +       /* bottom up here */
34467 +       if (unlikely(err < 0)) {
34468 +               err = au_wbr_bu(sb, btop - 1);
34469 +               if (err >= 0)
34470 +                       err = au_wbr_nonopq(dentry, err);
34471 +       }
34472 +
34473 +out:
34474 +       AuDbg("b%d\n", err);
34475 +       return err;
34476 +}
34477 +
34478 +/* ---------------------------------------------------------------------- */
34479 +
34480 +/* an exception for the policy other than tdp */
34481 +static int au_wbr_create_exp(struct dentry *dentry)
34482 +{
34483 +       int err;
34484 +       aufs_bindex_t bwh, bdiropq;
34485 +       struct dentry *parent;
34486 +
34487 +       err = -1;
34488 +       bwh = au_dbwh(dentry);
34489 +       parent = dget_parent(dentry);
34490 +       bdiropq = au_dbdiropq(parent);
34491 +       if (bwh >= 0) {
34492 +               if (bdiropq >= 0)
34493 +                       err = min(bdiropq, bwh);
34494 +               else
34495 +                       err = bwh;
34496 +               AuDbg("%d\n", err);
34497 +       } else if (bdiropq >= 0) {
34498 +               err = bdiropq;
34499 +               AuDbg("%d\n", err);
34500 +       }
34501 +       dput(parent);
34502 +
34503 +       if (err >= 0)
34504 +               err = au_wbr_nonopq(dentry, err);
34505 +
34506 +       if (err >= 0 && au_br_rdonly(au_sbr(dentry->d_sb, err)))
34507 +               err = -1;
34508 +
34509 +       AuDbg("%d\n", err);
34510 +       return err;
34511 +}
34512 +
34513 +/* ---------------------------------------------------------------------- */
34514 +
34515 +/* round robin */
34516 +static int au_wbr_create_init_rr(struct super_block *sb)
34517 +{
34518 +       int err;
34519 +
34520 +       err = au_wbr_bu(sb, au_sbbot(sb));
34521 +       atomic_set(&au_sbi(sb)->si_wbr_rr_next, -err); /* less important */
34522 +       /* smp_mb(); */
34523 +
34524 +       AuDbg("b%d\n", err);
34525 +       return err;
34526 +}
34527 +
34528 +static int au_wbr_create_rr(struct dentry *dentry, unsigned int flags)
34529 +{
34530 +       int err, nbr;
34531 +       unsigned int u;
34532 +       aufs_bindex_t bindex, bbot;
34533 +       struct super_block *sb;
34534 +       atomic_t *next;
34535 +
34536 +       err = au_wbr_create_exp(dentry);
34537 +       if (err >= 0)
34538 +               goto out;
34539 +
34540 +       sb = dentry->d_sb;
34541 +       next = &au_sbi(sb)->si_wbr_rr_next;
34542 +       bbot = au_sbbot(sb);
34543 +       nbr = bbot + 1;
34544 +       for (bindex = 0; bindex <= bbot; bindex++) {
34545 +               if (!au_ftest_wbr(flags, DIR)) {
34546 +                       err = atomic_dec_return(next) + 1;
34547 +                       /* modulo for 0 is meaningless */
34548 +                       if (unlikely(!err))
34549 +                               err = atomic_dec_return(next) + 1;
34550 +               } else
34551 +                       err = atomic_read(next);
34552 +               AuDbg("%d\n", err);
34553 +               u = err;
34554 +               err = u % nbr;
34555 +               AuDbg("%d\n", err);
34556 +               if (!au_br_rdonly(au_sbr(sb, err)))
34557 +                       break;
34558 +               err = -EROFS;
34559 +       }
34560 +
34561 +       if (err >= 0)
34562 +               err = au_wbr_nonopq(dentry, err);
34563 +
34564 +out:
34565 +       AuDbg("%d\n", err);
34566 +       return err;
34567 +}
34568 +
34569 +/* ---------------------------------------------------------------------- */
34570 +
34571 +/* most free space */
34572 +static void au_mfs(struct dentry *dentry, struct dentry *parent)
34573 +{
34574 +       struct super_block *sb;
34575 +       struct au_branch *br;
34576 +       struct au_wbr_mfs *mfs;
34577 +       struct dentry *h_parent;
34578 +       aufs_bindex_t bindex, bbot;
34579 +       int err;
34580 +       unsigned long long b, bavail;
34581 +       struct path h_path;
34582 +       /* reduce the stack usage */
34583 +       struct kstatfs *st;
34584 +
34585 +       st = kmalloc(sizeof(*st), GFP_NOFS);
34586 +       if (unlikely(!st)) {
34587 +               AuWarn1("failed updating mfs(%d), ignored\n", -ENOMEM);
34588 +               return;
34589 +       }
34590 +
34591 +       bavail = 0;
34592 +       sb = dentry->d_sb;
34593 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34594 +       MtxMustLock(&mfs->mfs_lock);
34595 +       mfs->mfs_bindex = -EROFS;
34596 +       mfs->mfsrr_bytes = 0;
34597 +       if (!parent) {
34598 +               bindex = 0;
34599 +               bbot = au_sbbot(sb);
34600 +       } else {
34601 +               bindex = au_dbtop(parent);
34602 +               bbot = au_dbtaildir(parent);
34603 +       }
34604 +
34605 +       for (; bindex <= bbot; bindex++) {
34606 +               if (parent) {
34607 +                       h_parent = au_h_dptr(parent, bindex);
34608 +                       if (!h_parent || d_is_negative(h_parent))
34609 +                               continue;
34610 +               }
34611 +               br = au_sbr(sb, bindex);
34612 +               if (au_br_rdonly(br))
34613 +                       continue;
34614 +
34615 +               /* sb->s_root for NFS is unreliable */
34616 +               h_path.mnt = au_br_mnt(br);
34617 +               h_path.dentry = h_path.mnt->mnt_root;
34618 +               err = vfs_statfs(&h_path, st);
34619 +               if (unlikely(err)) {
34620 +                       AuWarn1("failed statfs, b%d, %d\n", bindex, err);
34621 +                       continue;
34622 +               }
34623 +
34624 +               /* when the available size is equal, select the lower one */
34625 +               BUILD_BUG_ON(sizeof(b) < sizeof(st->f_bavail)
34626 +                            || sizeof(b) < sizeof(st->f_bsize));
34627 +               b = st->f_bavail * st->f_bsize;
34628 +               br->br_wbr->wbr_bytes = b;
34629 +               if (b >= bavail) {
34630 +                       bavail = b;
34631 +                       mfs->mfs_bindex = bindex;
34632 +                       mfs->mfs_jiffy = jiffies;
34633 +               }
34634 +       }
34635 +
34636 +       mfs->mfsrr_bytes = bavail;
34637 +       AuDbg("b%d\n", mfs->mfs_bindex);
34638 +       au_kfree_rcu(st);
34639 +}
34640 +
34641 +static int au_wbr_create_mfs(struct dentry *dentry, unsigned int flags)
34642 +{
34643 +       int err;
34644 +       struct dentry *parent;
34645 +       struct super_block *sb;
34646 +       struct au_wbr_mfs *mfs;
34647 +
34648 +       err = au_wbr_create_exp(dentry);
34649 +       if (err >= 0)
34650 +               goto out;
34651 +
34652 +       sb = dentry->d_sb;
34653 +       parent = NULL;
34654 +       if (au_ftest_wbr(flags, PARENT))
34655 +               parent = dget_parent(dentry);
34656 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34657 +       mutex_lock(&mfs->mfs_lock);
34658 +       if (time_after(jiffies, mfs->mfs_jiffy + mfs->mfs_expire)
34659 +           || mfs->mfs_bindex < 0
34660 +           || au_br_rdonly(au_sbr(sb, mfs->mfs_bindex)))
34661 +               au_mfs(dentry, parent);
34662 +       mutex_unlock(&mfs->mfs_lock);
34663 +       err = mfs->mfs_bindex;
34664 +       dput(parent);
34665 +
34666 +       if (err >= 0)
34667 +               err = au_wbr_nonopq(dentry, err);
34668 +
34669 +out:
34670 +       AuDbg("b%d\n", err);
34671 +       return err;
34672 +}
34673 +
34674 +static int au_wbr_create_init_mfs(struct super_block *sb)
34675 +{
34676 +       struct au_wbr_mfs *mfs;
34677 +
34678 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34679 +       mutex_init(&mfs->mfs_lock);
34680 +       mfs->mfs_jiffy = 0;
34681 +       mfs->mfs_bindex = -EROFS;
34682 +
34683 +       return 0;
34684 +}
34685 +
34686 +static int au_wbr_create_fin_mfs(struct super_block *sb __maybe_unused)
34687 +{
34688 +       mutex_destroy(&au_sbi(sb)->si_wbr_mfs.mfs_lock);
34689 +       return 0;
34690 +}
34691 +
34692 +/* ---------------------------------------------------------------------- */
34693 +
34694 +/* top down regardless parent, and then mfs */
34695 +static int au_wbr_create_tdmfs(struct dentry *dentry,
34696 +                              unsigned int flags __maybe_unused)
34697 +{
34698 +       int err;
34699 +       aufs_bindex_t bwh, btail, bindex, bfound, bmfs;
34700 +       unsigned long long watermark;
34701 +       struct super_block *sb;
34702 +       struct au_wbr_mfs *mfs;
34703 +       struct au_branch *br;
34704 +       struct dentry *parent;
34705 +
34706 +       sb = dentry->d_sb;
34707 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34708 +       mutex_lock(&mfs->mfs_lock);
34709 +       if (time_after(jiffies, mfs->mfs_jiffy + mfs->mfs_expire)
34710 +           || mfs->mfs_bindex < 0)
34711 +               au_mfs(dentry, /*parent*/NULL);
34712 +       watermark = mfs->mfsrr_watermark;
34713 +       bmfs = mfs->mfs_bindex;
34714 +       mutex_unlock(&mfs->mfs_lock);
34715 +
34716 +       /* another style of au_wbr_create_exp() */
34717 +       bwh = au_dbwh(dentry);
34718 +       parent = dget_parent(dentry);
34719 +       btail = au_dbtaildir(parent);
34720 +       if (bwh >= 0 && bwh < btail)
34721 +               btail = bwh;
34722 +
34723 +       err = au_wbr_nonopq(dentry, btail);
34724 +       if (unlikely(err < 0))
34725 +               goto out;
34726 +       btail = err;
34727 +       bfound = -1;
34728 +       for (bindex = 0; bindex <= btail; bindex++) {
34729 +               br = au_sbr(sb, bindex);
34730 +               if (au_br_rdonly(br))
34731 +                       continue;
34732 +               if (br->br_wbr->wbr_bytes > watermark) {
34733 +                       bfound = bindex;
34734 +                       break;
34735 +               }
34736 +       }
34737 +       err = bfound;
34738 +       if (err < 0)
34739 +               err = bmfs;
34740 +
34741 +out:
34742 +       dput(parent);
34743 +       AuDbg("b%d\n", err);
34744 +       return err;
34745 +}
34746 +
34747 +/* ---------------------------------------------------------------------- */
34748 +
34749 +/* most free space and then round robin */
34750 +static int au_wbr_create_mfsrr(struct dentry *dentry, unsigned int flags)
34751 +{
34752 +       int err;
34753 +       struct au_wbr_mfs *mfs;
34754 +
34755 +       err = au_wbr_create_mfs(dentry, flags);
34756 +       if (err >= 0) {
34757 +               mfs = &au_sbi(dentry->d_sb)->si_wbr_mfs;
34758 +               mutex_lock(&mfs->mfs_lock);
34759 +               if (mfs->mfsrr_bytes < mfs->mfsrr_watermark)
34760 +                       err = au_wbr_create_rr(dentry, flags);
34761 +               mutex_unlock(&mfs->mfs_lock);
34762 +       }
34763 +
34764 +       AuDbg("b%d\n", err);
34765 +       return err;
34766 +}
34767 +
34768 +static int au_wbr_create_init_mfsrr(struct super_block *sb)
34769 +{
34770 +       int err;
34771 +
34772 +       au_wbr_create_init_mfs(sb); /* ignore */
34773 +       err = au_wbr_create_init_rr(sb);
34774 +
34775 +       return err;
34776 +}
34777 +
34778 +/* ---------------------------------------------------------------------- */
34779 +
34780 +/* top down parent and most free space */
34781 +static int au_wbr_create_pmfs(struct dentry *dentry, unsigned int flags)
34782 +{
34783 +       int err, e2;
34784 +       unsigned long long b;
34785 +       aufs_bindex_t bindex, btop, bbot;
34786 +       struct super_block *sb;
34787 +       struct dentry *parent, *h_parent;
34788 +       struct au_branch *br;
34789 +
34790 +       err = au_wbr_create_tdp(dentry, flags);
34791 +       if (unlikely(err < 0))
34792 +               goto out;
34793 +       parent = dget_parent(dentry);
34794 +       btop = au_dbtop(parent);
34795 +       bbot = au_dbtaildir(parent);
34796 +       if (btop == bbot)
34797 +               goto out_parent; /* success */
34798 +
34799 +       e2 = au_wbr_create_mfs(dentry, flags);
34800 +       if (e2 < 0)
34801 +               goto out_parent; /* success */
34802 +
34803 +       /* when the available size is equal, select upper one */
34804 +       sb = dentry->d_sb;
34805 +       br = au_sbr(sb, err);
34806 +       b = br->br_wbr->wbr_bytes;
34807 +       AuDbg("b%d, %llu\n", err, b);
34808 +
34809 +       for (bindex = btop; bindex <= bbot; bindex++) {
34810 +               h_parent = au_h_dptr(parent, bindex);
34811 +               if (!h_parent || d_is_negative(h_parent))
34812 +                       continue;
34813 +
34814 +               br = au_sbr(sb, bindex);
34815 +               if (!au_br_rdonly(br) && br->br_wbr->wbr_bytes > b) {
34816 +                       b = br->br_wbr->wbr_bytes;
34817 +                       err = bindex;
34818 +                       AuDbg("b%d, %llu\n", err, b);
34819 +               }
34820 +       }
34821 +
34822 +       if (err >= 0)
34823 +               err = au_wbr_nonopq(dentry, err);
34824 +
34825 +out_parent:
34826 +       dput(parent);
34827 +out:
34828 +       AuDbg("b%d\n", err);
34829 +       return err;
34830 +}
34831 +
34832 +/* ---------------------------------------------------------------------- */
34833 +
34834 +/*
34835 + * - top down parent
34836 + * - most free space with parent
34837 + * - most free space round-robin regardless parent
34838 + */
34839 +static int au_wbr_create_pmfsrr(struct dentry *dentry, unsigned int flags)
34840 +{
34841 +       int err;
34842 +       unsigned long long watermark;
34843 +       struct super_block *sb;
34844 +       struct au_branch *br;
34845 +       struct au_wbr_mfs *mfs;
34846 +
34847 +       err = au_wbr_create_pmfs(dentry, flags | AuWbr_PARENT);
34848 +       if (unlikely(err < 0))
34849 +               goto out;
34850 +
34851 +       sb = dentry->d_sb;
34852 +       br = au_sbr(sb, err);
34853 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34854 +       mutex_lock(&mfs->mfs_lock);
34855 +       watermark = mfs->mfsrr_watermark;
34856 +       mutex_unlock(&mfs->mfs_lock);
34857 +       if (br->br_wbr->wbr_bytes < watermark)
34858 +               /* regardless the parent dir */
34859 +               err = au_wbr_create_mfsrr(dentry, flags);
34860 +
34861 +out:
34862 +       AuDbg("b%d\n", err);
34863 +       return err;
34864 +}
34865 +
34866 +/* ---------------------------------------------------------------------- */
34867 +
34868 +/* policies for copyup */
34869 +
34870 +/* top down parent */
34871 +static int au_wbr_copyup_tdp(struct dentry *dentry)
34872 +{
34873 +       return au_wbr_create_tdp(dentry, /*flags, anything is ok*/0);
34874 +}
34875 +
34876 +/* bottom up parent */
34877 +static int au_wbr_copyup_bup(struct dentry *dentry)
34878 +{
34879 +       int err;
34880 +       aufs_bindex_t bindex, btop;
34881 +       struct dentry *parent, *h_parent;
34882 +       struct super_block *sb;
34883 +
34884 +       err = -EROFS;
34885 +       sb = dentry->d_sb;
34886 +       parent = dget_parent(dentry);
34887 +       btop = au_dbtop(parent);
34888 +       for (bindex = au_dbtop(dentry); bindex >= btop; bindex--) {
34889 +               h_parent = au_h_dptr(parent, bindex);
34890 +               if (!h_parent || d_is_negative(h_parent))
34891 +                       continue;
34892 +
34893 +               if (!au_br_rdonly(au_sbr(sb, bindex))) {
34894 +                       err = bindex;
34895 +                       break;
34896 +               }
34897 +       }
34898 +       dput(parent);
34899 +
34900 +       /* bottom up here */
34901 +       if (unlikely(err < 0))
34902 +               err = au_wbr_bu(sb, btop - 1);
34903 +
34904 +       AuDbg("b%d\n", err);
34905 +       return err;
34906 +}
34907 +
34908 +/* bottom up */
34909 +int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t btop)
34910 +{
34911 +       int err;
34912 +
34913 +       err = au_wbr_bu(dentry->d_sb, btop);
34914 +       AuDbg("b%d\n", err);
34915 +       if (err > btop)
34916 +               err = au_wbr_nonopq(dentry, err);
34917 +
34918 +       AuDbg("b%d\n", err);
34919 +       return err;
34920 +}
34921 +
34922 +static int au_wbr_copyup_bu(struct dentry *dentry)
34923 +{
34924 +       int err;
34925 +       aufs_bindex_t btop;
34926 +
34927 +       btop = au_dbtop(dentry);
34928 +       err = au_wbr_do_copyup_bu(dentry, btop);
34929 +       return err;
34930 +}
34931 +
34932 +/* ---------------------------------------------------------------------- */
34933 +
34934 +struct au_wbr_copyup_operations au_wbr_copyup_ops[] = {
34935 +       [AuWbrCopyup_TDP] = {
34936 +               .copyup = au_wbr_copyup_tdp
34937 +       },
34938 +       [AuWbrCopyup_BUP] = {
34939 +               .copyup = au_wbr_copyup_bup
34940 +       },
34941 +       [AuWbrCopyup_BU] = {
34942 +               .copyup = au_wbr_copyup_bu
34943 +       }
34944 +};
34945 +
34946 +struct au_wbr_create_operations au_wbr_create_ops[] = {
34947 +       [AuWbrCreate_TDP] = {
34948 +               .create = au_wbr_create_tdp
34949 +       },
34950 +       [AuWbrCreate_RR] = {
34951 +               .create = au_wbr_create_rr,
34952 +               .init   = au_wbr_create_init_rr
34953 +       },
34954 +       [AuWbrCreate_MFS] = {
34955 +               .create = au_wbr_create_mfs,
34956 +               .init   = au_wbr_create_init_mfs,
34957 +               .fin    = au_wbr_create_fin_mfs
34958 +       },
34959 +       [AuWbrCreate_MFSV] = {
34960 +               .create = au_wbr_create_mfs,
34961 +               .init   = au_wbr_create_init_mfs,
34962 +               .fin    = au_wbr_create_fin_mfs
34963 +       },
34964 +       [AuWbrCreate_MFSRR] = {
34965 +               .create = au_wbr_create_mfsrr,
34966 +               .init   = au_wbr_create_init_mfsrr,
34967 +               .fin    = au_wbr_create_fin_mfs
34968 +       },
34969 +       [AuWbrCreate_MFSRRV] = {
34970 +               .create = au_wbr_create_mfsrr,
34971 +               .init   = au_wbr_create_init_mfsrr,
34972 +               .fin    = au_wbr_create_fin_mfs
34973 +       },
34974 +       [AuWbrCreate_TDMFS] = {
34975 +               .create = au_wbr_create_tdmfs,
34976 +               .init   = au_wbr_create_init_mfs,
34977 +               .fin    = au_wbr_create_fin_mfs
34978 +       },
34979 +       [AuWbrCreate_TDMFSV] = {
34980 +               .create = au_wbr_create_tdmfs,
34981 +               .init   = au_wbr_create_init_mfs,
34982 +               .fin    = au_wbr_create_fin_mfs
34983 +       },
34984 +       [AuWbrCreate_PMFS] = {
34985 +               .create = au_wbr_create_pmfs,
34986 +               .init   = au_wbr_create_init_mfs,
34987 +               .fin    = au_wbr_create_fin_mfs
34988 +       },
34989 +       [AuWbrCreate_PMFSV] = {
34990 +               .create = au_wbr_create_pmfs,
34991 +               .init   = au_wbr_create_init_mfs,
34992 +               .fin    = au_wbr_create_fin_mfs
34993 +       },
34994 +       [AuWbrCreate_PMFSRR] = {
34995 +               .create = au_wbr_create_pmfsrr,
34996 +               .init   = au_wbr_create_init_mfsrr,
34997 +               .fin    = au_wbr_create_fin_mfs
34998 +       },
34999 +       [AuWbrCreate_PMFSRRV] = {
35000 +               .create = au_wbr_create_pmfsrr,
35001 +               .init   = au_wbr_create_init_mfsrr,
35002 +               .fin    = au_wbr_create_fin_mfs
35003 +       }
35004 +};
35005 diff -urN /usr/share/empty/fs/aufs/whout.c linux/fs/aufs/whout.c
35006 --- /usr/share/empty/fs/aufs/whout.c    1970-01-01 01:00:00.000000000 +0100
35007 +++ linux/fs/aufs/whout.c       2022-03-21 14:49:05.733299677 +0100
35008 @@ -0,0 +1,1072 @@
35009 +// SPDX-License-Identifier: GPL-2.0
35010 +/*
35011 + * Copyright (C) 2005-2021 Junjiro R. Okajima
35012 + *
35013 + * This program, aufs is free software; you can redistribute it and/or modify
35014 + * it under the terms of the GNU General Public License as published by
35015 + * the Free Software Foundation; either version 2 of the License, or
35016 + * (at your option) any later version.
35017 + *
35018 + * This program is distributed in the hope that it will be useful,
35019 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
35020 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
35021 + * GNU General Public License for more details.
35022 + *
35023 + * You should have received a copy of the GNU General Public License
35024 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
35025 + */
35026 +
35027 +/*
35028 + * whiteout for logical deletion and opaque directory
35029 + */
35030 +
35031 +#include "aufs.h"
35032 +
35033 +#define WH_MASK                        0444
35034 +
35035 +/*
35036 + * If a directory contains this file, then it is opaque.  We start with the
35037 + * .wh. flag so that it is blocked by lookup.
35038 + */
35039 +static struct qstr diropq_name = QSTR_INIT(AUFS_WH_DIROPQ,
35040 +                                          sizeof(AUFS_WH_DIROPQ) - 1);
35041 +
35042 +/*
35043 + * generate whiteout name, which is NOT terminated by NULL.
35044 + * @name: original d_name.name
35045 + * @len: original d_name.len
35046 + * @wh: whiteout qstr
35047 + * returns zero when succeeds, otherwise error.
35048 + * succeeded value as wh->name should be freed by kfree().
35049 + */
35050 +int au_wh_name_alloc(struct qstr *wh, const struct qstr *name)
35051 +{
35052 +       char *p;
35053 +
35054 +       if (unlikely(name->len > PATH_MAX - AUFS_WH_PFX_LEN))
35055 +               return -ENAMETOOLONG;
35056 +
35057 +       wh->len = name->len + AUFS_WH_PFX_LEN;
35058 +       p = kmalloc(wh->len, GFP_NOFS);
35059 +       wh->name = p;
35060 +       if (p) {
35061 +               memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
35062 +               memcpy(p + AUFS_WH_PFX_LEN, name->name, name->len);
35063 +               /* smp_mb(); */
35064 +               return 0;
35065 +       }
35066 +       return -ENOMEM;
35067 +}
35068 +
35069 +/* ---------------------------------------------------------------------- */
35070 +
35071 +/*
35072 + * test if the @wh_name exists under @h_ppath.
35073 + * @try_sio specifies the necessary of super-io.
35074 + */
35075 +int au_wh_test(struct user_namespace *h_userns, struct path *h_ppath,
35076 +              struct qstr *wh_name, int try_sio)
35077 +{
35078 +       int err;
35079 +       struct dentry *wh_dentry;
35080 +
35081 +       if (!try_sio)
35082 +               wh_dentry = vfsub_lkup_one(wh_name, h_ppath);
35083 +       else
35084 +               wh_dentry = au_sio_lkup_one(h_userns, wh_name, h_ppath);
35085 +       err = PTR_ERR(wh_dentry);
35086 +       if (IS_ERR(wh_dentry)) {
35087 +               if (err == -ENAMETOOLONG)
35088 +                       err = 0;
35089 +               goto out;
35090 +       }
35091 +
35092 +       err = 0;
35093 +       if (d_is_negative(wh_dentry))
35094 +               goto out_wh; /* success */
35095 +
35096 +       err = 1;
35097 +       if (d_is_reg(wh_dentry))
35098 +               goto out_wh; /* success */
35099 +
35100 +       err = -EIO;
35101 +       AuIOErr("%pd Invalid whiteout entry type 0%o.\n",
35102 +               wh_dentry, d_inode(wh_dentry)->i_mode);
35103 +
35104 +out_wh:
35105 +       dput(wh_dentry);
35106 +out:
35107 +       return err;
35108 +}
35109 +
35110 +/*
35111 + * test if the @h_path->dentry sets opaque or not.
35112 + */
35113 +int au_diropq_test(struct user_namespace *h_userns, struct path *h_path)
35114 +{
35115 +       int err;
35116 +       struct inode *h_dir;
35117 +
35118 +       h_dir = d_inode(h_path->dentry);
35119 +       err = au_wh_test(h_userns, h_path, &diropq_name,
35120 +                        au_test_h_perm_sio(h_userns, h_dir, MAY_EXEC));
35121 +       return err;
35122 +}
35123 +
35124 +/*
35125 + * returns a negative dentry whose name is unique and temporary.
35126 + */
35127 +struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
35128 +                            struct qstr *prefix)
35129 +{
35130 +       struct dentry *dentry;
35131 +       int i;
35132 +       char defname[NAME_MAX - AUFS_MAX_NAMELEN + DNAME_INLINE_LEN + 1],
35133 +               *name, *p;
35134 +       /* strict atomic_t is unnecessary here */
35135 +       static unsigned short cnt;
35136 +       struct qstr qs;
35137 +       struct path h_ppath;
35138 +       struct user_namespace *h_userns;
35139 +
35140 +       BUILD_BUG_ON(sizeof(cnt) * 2 > AUFS_WH_TMP_LEN);
35141 +
35142 +       name = defname;
35143 +       qs.len = sizeof(defname) - DNAME_INLINE_LEN + prefix->len - 1;
35144 +       if (unlikely(prefix->len > DNAME_INLINE_LEN)) {
35145 +               dentry = ERR_PTR(-ENAMETOOLONG);
35146 +               if (unlikely(qs.len > NAME_MAX))
35147 +                       goto out;
35148 +               dentry = ERR_PTR(-ENOMEM);
35149 +               name = kmalloc(qs.len + 1, GFP_NOFS);
35150 +               if (unlikely(!name))
35151 +                       goto out;
35152 +       }
35153 +
35154 +       /* doubly whiteout-ed */
35155 +       memcpy(name, AUFS_WH_PFX AUFS_WH_PFX, AUFS_WH_PFX_LEN * 2);
35156 +       p = name + AUFS_WH_PFX_LEN * 2;
35157 +       memcpy(p, prefix->name, prefix->len);
35158 +       p += prefix->len;
35159 +       *p++ = '.';
35160 +       AuDebugOn(name + qs.len + 1 - p <= AUFS_WH_TMP_LEN);
35161 +
35162 +       h_ppath.dentry = h_parent;
35163 +       h_ppath.mnt = au_br_mnt(br);
35164 +       h_userns = au_br_userns(br);
35165 +       qs.name = name;
35166 +       for (i = 0; i < 3; i++) {
35167 +               sprintf(p, "%.*x", AUFS_WH_TMP_LEN, cnt++);
35168 +               dentry = au_sio_lkup_one(h_userns, &qs, &h_ppath);
35169 +               if (IS_ERR(dentry) || d_is_negative(dentry))
35170 +                       goto out_name;
35171 +               dput(dentry);
35172 +       }
35173 +       /* pr_warn("could not get random name\n"); */
35174 +       dentry = ERR_PTR(-EEXIST);
35175 +       AuDbg("%.*s\n", AuLNPair(&qs));
35176 +       BUG();
35177 +
35178 +out_name:
35179 +       if (name != defname)
35180 +               au_kfree_try_rcu(name);
35181 +out:
35182 +       AuTraceErrPtr(dentry);
35183 +       return dentry;
35184 +}
35185 +
35186 +/*
35187 + * rename the @h_dentry on @br to the whiteouted temporary name.
35188 + */
35189 +int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br)
35190 +{
35191 +       int err;
35192 +       struct path h_path = {
35193 +               .mnt = au_br_mnt(br)
35194 +       };
35195 +       struct inode *h_dir, *delegated;
35196 +       struct dentry *h_parent;
35197 +
35198 +       h_parent = h_dentry->d_parent; /* dir inode is locked */
35199 +       h_dir = d_inode(h_parent);
35200 +       IMustLock(h_dir);
35201 +
35202 +       h_path.dentry = au_whtmp_lkup(h_parent, br, &h_dentry->d_name);
35203 +       err = PTR_ERR(h_path.dentry);
35204 +       if (IS_ERR(h_path.dentry))
35205 +               goto out;
35206 +
35207 +       /* under the same dir, no need to lock_rename() */
35208 +       delegated = NULL;
35209 +       err = vfsub_rename(h_dir, h_dentry, h_dir, &h_path, &delegated,
35210 +                          /*flags*/0);
35211 +       AuTraceErr(err);
35212 +       if (unlikely(err == -EWOULDBLOCK)) {
35213 +               pr_warn("cannot retry for NFSv4 delegation"
35214 +                       " for an internal rename\n");
35215 +               iput(delegated);
35216 +       }
35217 +       dput(h_path.dentry);
35218 +
35219 +out:
35220 +       AuTraceErr(err);
35221 +       return err;
35222 +}
35223 +
35224 +/* ---------------------------------------------------------------------- */
35225 +/*
35226 + * functions for removing a whiteout
35227 + */
35228 +
35229 +static int do_unlink_wh(struct inode *h_dir, struct path *h_path)
35230 +{
35231 +       int err, force;
35232 +       struct inode *delegated;
35233 +
35234 +       /*
35235 +        * forces superio when the dir has a sticky bit.
35236 +        * this may be a violation of unix fs semantics.
35237 +        */
35238 +       force = (h_dir->i_mode & S_ISVTX)
35239 +               && !uid_eq(current_fsuid(), d_inode(h_path->dentry)->i_uid);
35240 +       delegated = NULL;
35241 +       err = vfsub_unlink(h_dir, h_path, &delegated, force);
35242 +       if (unlikely(err == -EWOULDBLOCK)) {
35243 +               pr_warn("cannot retry for NFSv4 delegation"
35244 +                       " for an internal unlink\n");
35245 +               iput(delegated);
35246 +       }
35247 +       return err;
35248 +}
35249 +
35250 +int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
35251 +                       struct dentry *dentry)
35252 +{
35253 +       int err;
35254 +
35255 +       err = do_unlink_wh(h_dir, h_path);
35256 +       if (!err && dentry)
35257 +               au_set_dbwh(dentry, -1);
35258 +
35259 +       return err;
35260 +}
35261 +
35262 +static int unlink_wh_name(struct path *h_ppath, struct qstr *wh)
35263 +{
35264 +       int err;
35265 +       struct path h_path;
35266 +
35267 +       err = 0;
35268 +       h_path.dentry = vfsub_lkup_one(wh, h_ppath);
35269 +       if (IS_ERR(h_path.dentry))
35270 +               err = PTR_ERR(h_path.dentry);
35271 +       else {
35272 +               if (d_is_reg(h_path.dentry)) {
35273 +                       h_path.mnt = h_ppath->mnt;
35274 +                       err = do_unlink_wh(d_inode(h_ppath->dentry), &h_path);
35275 +               }
35276 +               dput(h_path.dentry);
35277 +       }
35278 +
35279 +       return err;
35280 +}
35281 +
35282 +/* ---------------------------------------------------------------------- */
35283 +/*
35284 + * initialize/clean whiteout for a branch
35285 + */
35286 +
35287 +static void au_wh_clean(struct inode *h_dir, struct path *whpath,
35288 +                       const int isdir)
35289 +{
35290 +       int err;
35291 +       struct inode *delegated;
35292 +
35293 +       if (d_is_negative(whpath->dentry))
35294 +               return;
35295 +
35296 +       if (isdir)
35297 +               err = vfsub_rmdir(h_dir, whpath);
35298 +       else {
35299 +               delegated = NULL;
35300 +               err = vfsub_unlink(h_dir, whpath, &delegated, /*force*/0);
35301 +               if (unlikely(err == -EWOULDBLOCK)) {
35302 +                       pr_warn("cannot retry for NFSv4 delegation"
35303 +                               " for an internal unlink\n");
35304 +                       iput(delegated);
35305 +               }
35306 +       }
35307 +       if (unlikely(err))
35308 +               pr_warn("failed removing %pd (%d), ignored.\n",
35309 +                       whpath->dentry, err);
35310 +}
35311 +
35312 +static int test_linkable(struct dentry *h_root)
35313 +{
35314 +       struct inode *h_dir = d_inode(h_root);
35315 +
35316 +       if (h_dir->i_op->link)
35317 +               return 0;
35318 +
35319 +       pr_err("%pd (%s) doesn't support link(2), use noplink and rw+nolwh\n",
35320 +              h_root, au_sbtype(h_root->d_sb));
35321 +       return -ENOSYS; /* the branch doesn't have its ->link() */
35322 +}
35323 +
35324 +/* todo: should this mkdir be done in /sbin/mount.aufs helper? */
35325 +static int au_whdir(struct inode *h_dir, struct path *path)
35326 +{
35327 +       int err;
35328 +
35329 +       err = -EEXIST;
35330 +       if (d_is_negative(path->dentry)) {
35331 +               int mode = 0700;
35332 +
35333 +               if (au_test_nfs(path->dentry->d_sb))
35334 +                       mode |= 0111;
35335 +               err = vfsub_mkdir(h_dir, path, mode);
35336 +       } else if (d_is_dir(path->dentry))
35337 +               err = 0;
35338 +       else
35339 +               pr_err("unknown %pd exists\n", path->dentry);
35340 +
35341 +       return err;
35342 +}
35343 +
35344 +struct au_wh_base {
35345 +       const struct qstr *name;
35346 +       struct dentry *dentry;
35347 +};
35348 +
35349 +static void au_wh_init_ro(struct inode *h_dir, struct au_wh_base base[],
35350 +                         struct path *h_path)
35351 +{
35352 +       h_path->dentry = base[AuBrWh_BASE].dentry;
35353 +       au_wh_clean(h_dir, h_path, /*isdir*/0);
35354 +       h_path->dentry = base[AuBrWh_PLINK].dentry;
35355 +       au_wh_clean(h_dir, h_path, /*isdir*/1);
35356 +       h_path->dentry = base[AuBrWh_ORPH].dentry;
35357 +       au_wh_clean(h_dir, h_path, /*isdir*/1);
35358 +}
35359 +
35360 +/*
35361 + * returns tri-state,
35362 + * minus: error, caller should print the message
35363 + * zero: success
35364 + * plus: error, caller should NOT print the message
35365 + */
35366 +static int au_wh_init_rw_nolink(struct dentry *h_root, struct au_wbr *wbr,
35367 +                               int do_plink, struct au_wh_base base[],
35368 +                               struct path *h_path)
35369 +{
35370 +       int err;
35371 +       struct inode *h_dir;
35372 +
35373 +       h_dir = d_inode(h_root);
35374 +       h_path->dentry = base[AuBrWh_BASE].dentry;
35375 +       au_wh_clean(h_dir, h_path, /*isdir*/0);
35376 +       h_path->dentry = base[AuBrWh_PLINK].dentry;
35377 +       if (do_plink) {
35378 +               err = test_linkable(h_root);
35379 +               if (unlikely(err)) {
35380 +                       err = 1;
35381 +                       goto out;
35382 +               }
35383 +
35384 +               err = au_whdir(h_dir, h_path);
35385 +               if (unlikely(err))
35386 +                       goto out;
35387 +               wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
35388 +       } else
35389 +               au_wh_clean(h_dir, h_path, /*isdir*/1);
35390 +       h_path->dentry = base[AuBrWh_ORPH].dentry;
35391 +       err = au_whdir(h_dir, h_path);
35392 +       if (unlikely(err))
35393 +               goto out;
35394 +       wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
35395 +
35396 +out:
35397 +       return err;
35398 +}
35399 +
35400 +/*
35401 + * for the moment, aufs supports the branch filesystem which does not support
35402 + * link(2). testing on FAT which does not support i_op->setattr() fully either,
35403 + * copyup failed. finally, such filesystem will not be used as the writable
35404 + * branch.
35405 + *
35406 + * returns tri-state, see above.
35407 + */
35408 +static int au_wh_init_rw(struct dentry *h_root, struct au_wbr *wbr,
35409 +                        int do_plink, struct au_wh_base base[],
35410 +                        struct path *h_path)
35411 +{
35412 +       int err;
35413 +       struct inode *h_dir;
35414 +
35415 +       WbrWhMustWriteLock(wbr);
35416 +
35417 +       err = test_linkable(h_root);
35418 +       if (unlikely(err)) {
35419 +               err = 1;
35420 +               goto out;
35421 +       }
35422 +
35423 +       /*
35424 +        * todo: should this create be done in /sbin/mount.aufs helper?
35425 +        */
35426 +       err = -EEXIST;
35427 +       h_dir = d_inode(h_root);
35428 +       if (d_is_negative(base[AuBrWh_BASE].dentry)) {
35429 +               h_path->dentry = base[AuBrWh_BASE].dentry;
35430 +               err = vfsub_create(h_dir, h_path, WH_MASK, /*want_excl*/true);
35431 +       } else if (d_is_reg(base[AuBrWh_BASE].dentry))
35432 +               err = 0;
35433 +       else
35434 +               pr_err("unknown %pd2 exists\n", base[AuBrWh_BASE].dentry);
35435 +       if (unlikely(err))
35436 +               goto out;
35437 +
35438 +       h_path->dentry = base[AuBrWh_PLINK].dentry;
35439 +       if (do_plink) {
35440 +               err = au_whdir(h_dir, h_path);
35441 +               if (unlikely(err))
35442 +                       goto out;
35443 +               wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
35444 +       } else
35445 +               au_wh_clean(h_dir, h_path, /*isdir*/1);
35446 +       wbr->wbr_whbase = dget(base[AuBrWh_BASE].dentry);
35447 +
35448 +       h_path->dentry = base[AuBrWh_ORPH].dentry;
35449 +       err = au_whdir(h_dir, h_path);
35450 +       if (unlikely(err))
35451 +               goto out;
35452 +       wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
35453 +
35454 +out:
35455 +       return err;
35456 +}
35457 +
35458 +/*
35459 + * initialize the whiteout base file/dir for @br.
35460 + */
35461 +int au_wh_init(struct au_branch *br, struct super_block *sb)
35462 +{
35463 +       int err, i;
35464 +       const unsigned char do_plink
35465 +               = !!au_opt_test(au_mntflags(sb), PLINK);
35466 +       struct inode *h_dir;
35467 +       struct path path = br->br_path;
35468 +       struct dentry *h_root = path.dentry;
35469 +       struct au_wbr *wbr = br->br_wbr;
35470 +       static const struct qstr base_name[] = {
35471 +               [AuBrWh_BASE] = QSTR_INIT(AUFS_BASE_NAME,
35472 +                                         sizeof(AUFS_BASE_NAME) - 1),
35473 +               [AuBrWh_PLINK] = QSTR_INIT(AUFS_PLINKDIR_NAME,
35474 +                                          sizeof(AUFS_PLINKDIR_NAME) - 1),
35475 +               [AuBrWh_ORPH] = QSTR_INIT(AUFS_ORPHDIR_NAME,
35476 +                                         sizeof(AUFS_ORPHDIR_NAME) - 1)
35477 +       };
35478 +       struct au_wh_base base[] = {
35479 +               [AuBrWh_BASE] = {
35480 +                       .name   = base_name + AuBrWh_BASE,
35481 +                       .dentry = NULL
35482 +               },
35483 +               [AuBrWh_PLINK] = {
35484 +                       .name   = base_name + AuBrWh_PLINK,
35485 +                       .dentry = NULL
35486 +               },
35487 +               [AuBrWh_ORPH] = {
35488 +                       .name   = base_name + AuBrWh_ORPH,
35489 +                       .dentry = NULL
35490 +               }
35491 +       };
35492 +
35493 +       if (wbr)
35494 +               WbrWhMustWriteLock(wbr);
35495 +
35496 +       for (i = 0; i < AuBrWh_Last; i++) {
35497 +               /* doubly whiteouted */
35498 +               struct dentry *d;
35499 +
35500 +               d = au_wh_lkup(h_root, (void *)base[i].name, br);
35501 +               err = PTR_ERR(d);
35502 +               if (IS_ERR(d))
35503 +                       goto out;
35504 +
35505 +               base[i].dentry = d;
35506 +               AuDebugOn(wbr
35507 +                         && wbr->wbr_wh[i]
35508 +                         && wbr->wbr_wh[i] != base[i].dentry);
35509 +       }
35510 +
35511 +       if (wbr)
35512 +               for (i = 0; i < AuBrWh_Last; i++) {
35513 +                       dput(wbr->wbr_wh[i]);
35514 +                       wbr->wbr_wh[i] = NULL;
35515 +               }
35516 +
35517 +       err = 0;
35518 +       if (!au_br_writable(br->br_perm)) {
35519 +               h_dir = d_inode(h_root);
35520 +               au_wh_init_ro(h_dir, base, &path);
35521 +       } else if (!au_br_wh_linkable(br->br_perm)) {
35522 +               err = au_wh_init_rw_nolink(h_root, wbr, do_plink, base, &path);
35523 +               if (err > 0)
35524 +                       goto out;
35525 +               else if (err)
35526 +                       goto out_err;
35527 +       } else {
35528 +               err = au_wh_init_rw(h_root, wbr, do_plink, base, &path);
35529 +               if (err > 0)
35530 +                       goto out;
35531 +               else if (err)
35532 +                       goto out_err;
35533 +       }
35534 +       goto out; /* success */
35535 +
35536 +out_err:
35537 +       pr_err("an error(%d) on the writable branch %pd(%s)\n",
35538 +              err, h_root, au_sbtype(h_root->d_sb));
35539 +out:
35540 +       for (i = 0; i < AuBrWh_Last; i++)
35541 +               dput(base[i].dentry);
35542 +       return err;
35543 +}
35544 +
35545 +/* ---------------------------------------------------------------------- */
35546 +/*
35547 + * whiteouts are all hard-linked usually.
35548 + * when its link count reaches a ceiling, we create a new whiteout base
35549 + * asynchronously.
35550 + */
35551 +
35552 +struct reinit_br_wh {
35553 +       struct super_block *sb;
35554 +       struct au_branch *br;
35555 +};
35556 +
35557 +static void reinit_br_wh(void *arg)
35558 +{
35559 +       int err;
35560 +       aufs_bindex_t bindex;
35561 +       struct path h_path;
35562 +       struct reinit_br_wh *a = arg;
35563 +       struct au_wbr *wbr;
35564 +       struct inode *dir, *delegated;
35565 +       struct dentry *h_root;
35566 +       struct au_hinode *hdir;
35567 +
35568 +       err = 0;
35569 +       wbr = a->br->br_wbr;
35570 +       /* big aufs lock */
35571 +       si_noflush_write_lock(a->sb);
35572 +       if (!au_br_writable(a->br->br_perm))
35573 +               goto out;
35574 +       bindex = au_br_index(a->sb, a->br->br_id);
35575 +       if (unlikely(bindex < 0))
35576 +               goto out;
35577 +
35578 +       di_read_lock_parent(a->sb->s_root, AuLock_IR);
35579 +       dir = d_inode(a->sb->s_root);
35580 +       hdir = au_hi(dir, bindex);
35581 +       h_root = au_h_dptr(a->sb->s_root, bindex);
35582 +       AuDebugOn(h_root != au_br_dentry(a->br));
35583 +
35584 +       au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
35585 +       wbr_wh_write_lock(wbr);
35586 +       err = au_h_verify(wbr->wbr_whbase, au_opt_udba(a->sb), hdir->hi_inode,
35587 +                         h_root, a->br);
35588 +       if (!err) {
35589 +               h_path.dentry = wbr->wbr_whbase;
35590 +               h_path.mnt = au_br_mnt(a->br);
35591 +               delegated = NULL;
35592 +               err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated,
35593 +                                  /*force*/0);
35594 +               if (unlikely(err == -EWOULDBLOCK)) {
35595 +                       pr_warn("cannot retry for NFSv4 delegation"
35596 +                               " for an internal unlink\n");
35597 +                       iput(delegated);
35598 +               }
35599 +       } else {
35600 +               pr_warn("%pd is moved, ignored\n", wbr->wbr_whbase);
35601 +               err = 0;
35602 +       }
35603 +       dput(wbr->wbr_whbase);
35604 +       wbr->wbr_whbase = NULL;
35605 +       if (!err)
35606 +               err = au_wh_init(a->br, a->sb);
35607 +       wbr_wh_write_unlock(wbr);
35608 +       au_hn_inode_unlock(hdir);
35609 +       di_read_unlock(a->sb->s_root, AuLock_IR);
35610 +       if (!err)
35611 +               au_fhsm_wrote(a->sb, bindex, /*force*/0);
35612 +
35613 +out:
35614 +       if (wbr)
35615 +               atomic_dec(&wbr->wbr_wh_running);
35616 +       au_lcnt_dec(&a->br->br_count);
35617 +       si_write_unlock(a->sb);
35618 +       au_nwt_done(&au_sbi(a->sb)->si_nowait);
35619 +       au_kfree_rcu(a);
35620 +       if (unlikely(err))
35621 +               AuIOErr("err %d\n", err);
35622 +}
35623 +
35624 +static void kick_reinit_br_wh(struct super_block *sb, struct au_branch *br)
35625 +{
35626 +       int do_dec, wkq_err;
35627 +       struct reinit_br_wh *arg;
35628 +
35629 +       do_dec = 1;
35630 +       if (atomic_inc_return(&br->br_wbr->wbr_wh_running) != 1)
35631 +               goto out;
35632 +
35633 +       /* ignore ENOMEM */
35634 +       arg = kmalloc(sizeof(*arg), GFP_NOFS);
35635 +       if (arg) {
35636 +               /*
35637 +                * dec(wh_running), kfree(arg) and dec(br_count)
35638 +                * in reinit function
35639 +                */
35640 +               arg->sb = sb;
35641 +               arg->br = br;
35642 +               au_lcnt_inc(&br->br_count);
35643 +               wkq_err = au_wkq_nowait(reinit_br_wh, arg, sb, /*flags*/0);
35644 +               if (unlikely(wkq_err)) {
35645 +                       atomic_dec(&br->br_wbr->wbr_wh_running);
35646 +                       au_lcnt_dec(&br->br_count);
35647 +                       au_kfree_rcu(arg);
35648 +               }
35649 +               do_dec = 0;
35650 +       }
35651 +
35652 +out:
35653 +       if (do_dec)
35654 +               atomic_dec(&br->br_wbr->wbr_wh_running);
35655 +}
35656 +
35657 +/* ---------------------------------------------------------------------- */
35658 +
35659 +/*
35660 + * create the whiteout @wh.
35661 + */
35662 +static int link_or_create_wh(struct super_block *sb, aufs_bindex_t bindex,
35663 +                            struct dentry *wh)
35664 +{
35665 +       int err;
35666 +       struct path h_path = {
35667 +               .dentry = wh
35668 +       };
35669 +       struct au_branch *br;
35670 +       struct au_wbr *wbr;
35671 +       struct dentry *h_parent;
35672 +       struct inode *h_dir, *delegated;
35673 +
35674 +       h_parent = wh->d_parent; /* dir inode is locked */
35675 +       h_dir = d_inode(h_parent);
35676 +       IMustLock(h_dir);
35677 +
35678 +       br = au_sbr(sb, bindex);
35679 +       h_path.mnt = au_br_mnt(br);
35680 +       wbr = br->br_wbr;
35681 +       wbr_wh_read_lock(wbr);
35682 +       if (wbr->wbr_whbase) {
35683 +               delegated = NULL;
35684 +               err = vfsub_link(wbr->wbr_whbase, h_dir, &h_path, &delegated);
35685 +               if (unlikely(err == -EWOULDBLOCK)) {
35686 +                       pr_warn("cannot retry for NFSv4 delegation"
35687 +                               " for an internal link\n");
35688 +                       iput(delegated);
35689 +               }
35690 +               if (!err || err != -EMLINK)
35691 +                       goto out;
35692 +
35693 +               /* link count full. re-initialize br_whbase. */
35694 +               kick_reinit_br_wh(sb, br);
35695 +       }
35696 +
35697 +       /* return this error in this context */
35698 +       err = vfsub_create(h_dir, &h_path, WH_MASK, /*want_excl*/true);
35699 +       if (!err)
35700 +               au_fhsm_wrote(sb, bindex, /*force*/0);
35701 +
35702 +out:
35703 +       wbr_wh_read_unlock(wbr);
35704 +       return err;
35705 +}
35706 +
35707 +/* ---------------------------------------------------------------------- */
35708 +
35709 +/*
35710 + * create or remove the diropq.
35711 + */
35712 +static struct dentry *do_diropq(struct dentry *dentry, aufs_bindex_t bindex,
35713 +                               unsigned int flags)
35714 +{
35715 +       struct dentry *opq_dentry;
35716 +       struct super_block *sb;
35717 +       struct au_branch *br;
35718 +       struct path h_path;
35719 +       int err;
35720 +
35721 +       sb = dentry->d_sb;
35722 +       br = au_sbr(sb, bindex);
35723 +       h_path.dentry = au_h_dptr(dentry, bindex);
35724 +       h_path.mnt = au_br_mnt(br);
35725 +       opq_dentry = vfsub_lkup_one(&diropq_name, &h_path);
35726 +       if (IS_ERR(opq_dentry))
35727 +               goto out;
35728 +
35729 +       if (au_ftest_diropq(flags, CREATE)) {
35730 +               err = link_or_create_wh(sb, bindex, opq_dentry);
35731 +               if (!err) {
35732 +                       au_set_dbdiropq(dentry, bindex);
35733 +                       goto out; /* success */
35734 +               }
35735 +       } else {
35736 +               h_path.dentry = opq_dentry;
35737 +               err = do_unlink_wh(au_h_iptr(d_inode(dentry), bindex), &h_path);
35738 +               if (!err)
35739 +                       au_set_dbdiropq(dentry, -1);
35740 +       }
35741 +       dput(opq_dentry);
35742 +       opq_dentry = ERR_PTR(err);
35743 +
35744 +out:
35745 +       return opq_dentry;
35746 +}
35747 +
35748 +struct do_diropq_args {
35749 +       struct dentry **errp;
35750 +       struct dentry *dentry;
35751 +       aufs_bindex_t bindex;
35752 +       unsigned int flags;
35753 +};
35754 +
35755 +static void call_do_diropq(void *args)
35756 +{
35757 +       struct do_diropq_args *a = args;
35758 +       *a->errp = do_diropq(a->dentry, a->bindex, a->flags);
35759 +}
35760 +
35761 +struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
35762 +                            unsigned int flags)
35763 +{
35764 +       struct dentry *diropq, *h_dentry;
35765 +       struct user_namespace *h_userns;
35766 +
35767 +       h_userns = au_sbr_userns(dentry->d_sb, bindex);
35768 +       h_dentry = au_h_dptr(dentry, bindex);
35769 +       if (!au_test_h_perm_sio(h_userns, d_inode(h_dentry),
35770 +                               MAY_EXEC | MAY_WRITE))
35771 +               diropq = do_diropq(dentry, bindex, flags);
35772 +       else {
35773 +               int wkq_err;
35774 +               struct do_diropq_args args = {
35775 +                       .errp           = &diropq,
35776 +                       .dentry         = dentry,
35777 +                       .bindex         = bindex,
35778 +                       .flags          = flags
35779 +               };
35780 +
35781 +               wkq_err = au_wkq_wait(call_do_diropq, &args);
35782 +               if (unlikely(wkq_err))
35783 +                       diropq = ERR_PTR(wkq_err);
35784 +       }
35785 +
35786 +       return diropq;
35787 +}
35788 +
35789 +/* ---------------------------------------------------------------------- */
35790 +
35791 +/*
35792 + * lookup whiteout dentry.
35793 + * @h_parent: lower parent dentry which must exist and be locked
35794 + * @base_name: name of dentry which will be whiteouted
35795 + * returns dentry for whiteout.
35796 + */
35797 +struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
35798 +                         struct au_branch *br)
35799 +{
35800 +       int err;
35801 +       struct qstr wh_name;
35802 +       struct dentry *wh_dentry;
35803 +       struct path h_path;
35804 +
35805 +       err = au_wh_name_alloc(&wh_name, base_name);
35806 +       wh_dentry = ERR_PTR(err);
35807 +       if (!err) {
35808 +               h_path.dentry = h_parent;
35809 +               h_path.mnt = au_br_mnt(br);
35810 +               wh_dentry = vfsub_lkup_one(&wh_name, &h_path);
35811 +               au_kfree_try_rcu(wh_name.name);
35812 +       }
35813 +       return wh_dentry;
35814 +}
35815 +
35816 +/*
35817 + * link/create a whiteout for @dentry on @bindex.
35818 + */
35819 +struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
35820 +                           struct dentry *h_parent)
35821 +{
35822 +       struct dentry *wh_dentry;
35823 +       struct super_block *sb;
35824 +       int err;
35825 +
35826 +       sb = dentry->d_sb;
35827 +       wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, au_sbr(sb, bindex));
35828 +       if (!IS_ERR(wh_dentry) && d_is_negative(wh_dentry)) {
35829 +               err = link_or_create_wh(sb, bindex, wh_dentry);
35830 +               if (!err) {
35831 +                       au_set_dbwh(dentry, bindex);
35832 +                       au_fhsm_wrote(sb, bindex, /*force*/0);
35833 +               } else {
35834 +                       dput(wh_dentry);
35835 +                       wh_dentry = ERR_PTR(err);
35836 +               }
35837 +       }
35838 +
35839 +       return wh_dentry;
35840 +}
35841 +
35842 +/* ---------------------------------------------------------------------- */
35843 +
35844 +/* Delete all whiteouts in this directory on branch bindex. */
35845 +static int del_wh_children(struct path *h_path, struct au_nhash *whlist,
35846 +                          aufs_bindex_t bindex)
35847 +{
35848 +       int err;
35849 +       unsigned long ul, n;
35850 +       struct qstr wh_name;
35851 +       char *p;
35852 +       struct hlist_head *head;
35853 +       struct au_vdir_wh *pos;
35854 +       struct au_vdir_destr *str;
35855 +
35856 +       err = -ENOMEM;
35857 +       p = (void *)__get_free_page(GFP_NOFS);
35858 +       wh_name.name = p;
35859 +       if (unlikely(!wh_name.name))
35860 +               goto out;
35861 +
35862 +       err = 0;
35863 +       memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
35864 +       p += AUFS_WH_PFX_LEN;
35865 +       n = whlist->nh_num;
35866 +       head = whlist->nh_head;
35867 +       for (ul = 0; !err && ul < n; ul++, head++) {
35868 +               hlist_for_each_entry(pos, head, wh_hash) {
35869 +                       if (pos->wh_bindex != bindex)
35870 +                               continue;
35871 +
35872 +                       str = &pos->wh_str;
35873 +                       if (str->len + AUFS_WH_PFX_LEN <= PATH_MAX) {
35874 +                               memcpy(p, str->name, str->len);
35875 +                               wh_name.len = AUFS_WH_PFX_LEN + str->len;
35876 +                               err = unlink_wh_name(h_path, &wh_name);
35877 +                               if (!err)
35878 +                                       continue;
35879 +                               break;
35880 +                       }
35881 +                       AuIOErr("whiteout name too long %.*s\n",
35882 +                               str->len, str->name);
35883 +                       err = -EIO;
35884 +                       break;
35885 +               }
35886 +       }
35887 +       free_page((unsigned long)wh_name.name);
35888 +
35889 +out:
35890 +       return err;
35891 +}
35892 +
35893 +struct del_wh_children_args {
35894 +       int *errp;
35895 +       struct path *h_path;
35896 +       struct au_nhash *whlist;
35897 +       aufs_bindex_t bindex;
35898 +};
35899 +
35900 +static void call_del_wh_children(void *args)
35901 +{
35902 +       struct del_wh_children_args *a = args;
35903 +       *a->errp = del_wh_children(a->h_path, a->whlist, a->bindex);
35904 +}
35905 +
35906 +/* ---------------------------------------------------------------------- */
35907 +
35908 +struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp)
35909 +{
35910 +       struct au_whtmp_rmdir *whtmp;
35911 +       int err;
35912 +       unsigned int rdhash;
35913 +
35914 +       SiMustAnyLock(sb);
35915 +
35916 +       whtmp = kzalloc(sizeof(*whtmp), gfp);
35917 +       if (unlikely(!whtmp)) {
35918 +               whtmp = ERR_PTR(-ENOMEM);
35919 +               goto out;
35920 +       }
35921 +
35922 +       /* no estimation for dir size */
35923 +       rdhash = au_sbi(sb)->si_rdhash;
35924 +       if (!rdhash)
35925 +               rdhash = AUFS_RDHASH_DEF;
35926 +       err = au_nhash_alloc(&whtmp->whlist, rdhash, gfp);
35927 +       if (unlikely(err)) {
35928 +               au_kfree_rcu(whtmp);
35929 +               whtmp = ERR_PTR(err);
35930 +       }
35931 +
35932 +out:
35933 +       return whtmp;
35934 +}
35935 +
35936 +void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp)
35937 +{
35938 +       if (whtmp->br)
35939 +               au_lcnt_dec(&whtmp->br->br_count);
35940 +       dput(whtmp->wh_dentry);
35941 +       iput(whtmp->dir);
35942 +       au_nhash_wh_free(&whtmp->whlist);
35943 +       au_kfree_rcu(whtmp);
35944 +}
35945 +
35946 +/*
35947 + * rmdir the whiteouted temporary named dir @h_dentry.
35948 + * @whlist: whiteouted children.
35949 + */
35950 +int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
35951 +                  struct dentry *wh_dentry, struct au_nhash *whlist)
35952 +{
35953 +       int err;
35954 +       unsigned int h_nlink;
35955 +       struct path wh_path;
35956 +       struct inode *wh_inode, *h_dir;
35957 +       struct au_branch *br;
35958 +       struct user_namespace *h_userns;
35959 +
35960 +       h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */
35961 +       IMustLock(h_dir);
35962 +
35963 +       br = au_sbr(dir->i_sb, bindex);
35964 +       wh_path.dentry = wh_dentry;
35965 +       wh_path.mnt = au_br_mnt(br);
35966 +       h_userns = au_br_userns(br);
35967 +       wh_inode = d_inode(wh_dentry);
35968 +       inode_lock_nested(wh_inode, AuLsc_I_CHILD);
35969 +
35970 +       /*
35971 +        * someone else might change some whiteouts while we were sleeping.
35972 +        * it means this whlist may have an obsoleted entry.
35973 +        */
35974 +       if (!au_test_h_perm_sio(h_userns, wh_inode, MAY_EXEC | MAY_WRITE))
35975 +               err = del_wh_children(&wh_path, whlist, bindex);
35976 +       else {
35977 +               int wkq_err;
35978 +               struct del_wh_children_args args = {
35979 +                       .errp           = &err,
35980 +                       .h_path         = &wh_path,
35981 +                       .whlist         = whlist,
35982 +                       .bindex         = bindex
35983 +               };
35984 +
35985 +               wkq_err = au_wkq_wait(call_del_wh_children, &args);
35986 +               if (unlikely(wkq_err))
35987 +                       err = wkq_err;
35988 +       }
35989 +       inode_unlock(wh_inode);
35990 +
35991 +       if (!err) {
35992 +               h_nlink = h_dir->i_nlink;
35993 +               err = vfsub_rmdir(h_dir, &wh_path);
35994 +               /* some fs doesn't change the parent nlink in some cases */
35995 +               h_nlink -= h_dir->i_nlink;
35996 +       }
35997 +
35998 +       if (!err) {
35999 +               if (au_ibtop(dir) == bindex) {
36000 +                       /* todo: dir->i_mutex is necessary */
36001 +                       au_cpup_attr_timesizes(dir);
36002 +                       if (h_nlink)
36003 +                               vfsub_drop_nlink(dir);
36004 +               }
36005 +               return 0; /* success */
36006 +       }
36007 +
36008 +       pr_warn("failed removing %pd(%d), ignored\n", wh_dentry, err);
36009 +       return err;
36010 +}
36011 +
36012 +static void call_rmdir_whtmp(void *args)
36013 +{
36014 +       int err;
36015 +       aufs_bindex_t bindex;
36016 +       struct au_whtmp_rmdir *a = args;
36017 +       struct super_block *sb;
36018 +       struct dentry *h_parent;
36019 +       struct inode *h_dir;
36020 +       struct au_hinode *hdir;
36021 +
36022 +       /* rmdir by nfsd may cause deadlock with this i_mutex */
36023 +       /* inode_lock(a->dir); */
36024 +       err = -EROFS;
36025 +       sb = a->dir->i_sb;
36026 +       si_read_lock(sb, !AuLock_FLUSH);
36027 +       if (!au_br_writable(a->br->br_perm))
36028 +               goto out;
36029 +       bindex = au_br_index(sb, a->br->br_id);
36030 +       if (unlikely(bindex < 0))
36031 +               goto out;
36032 +
36033 +       err = -EIO;
36034 +       ii_write_lock_parent(a->dir);
36035 +       h_parent = dget_parent(a->wh_dentry);
36036 +       h_dir = d_inode(h_parent);
36037 +       hdir = au_hi(a->dir, bindex);
36038 +       err = vfsub_mnt_want_write(au_br_mnt(a->br));
36039 +       if (unlikely(err))
36040 +               goto out_mnt;
36041 +       au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
36042 +       err = au_h_verify(a->wh_dentry, au_opt_udba(sb), h_dir, h_parent,
36043 +                         a->br);
36044 +       if (!err)
36045 +               err = au_whtmp_rmdir(a->dir, bindex, a->wh_dentry, &a->whlist);
36046 +       au_hn_inode_unlock(hdir);
36047 +       vfsub_mnt_drop_write(au_br_mnt(a->br));
36048 +
36049 +out_mnt:
36050 +       dput(h_parent);
36051 +       ii_write_unlock(a->dir);
36052 +out:
36053 +       /* inode_unlock(a->dir); */
36054 +       au_whtmp_rmdir_free(a);
36055 +       si_read_unlock(sb);
36056 +       au_nwt_done(&au_sbi(sb)->si_nowait);
36057 +       if (unlikely(err))
36058 +               AuIOErr("err %d\n", err);
36059 +}
36060 +
36061 +void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
36062 +                        struct dentry *wh_dentry, struct au_whtmp_rmdir *args)
36063 +{
36064 +       int wkq_err;
36065 +       struct super_block *sb;
36066 +
36067 +       IMustLock(dir);
36068 +
36069 +       /* all post-process will be done in do_rmdir_whtmp(). */
36070 +       sb = dir->i_sb;
36071 +       args->dir = au_igrab(dir);
36072 +       args->br = au_sbr(sb, bindex);
36073 +       au_lcnt_inc(&args->br->br_count);
36074 +       args->wh_dentry = dget(wh_dentry);
36075 +       wkq_err = au_wkq_nowait(call_rmdir_whtmp, args, sb, /*flags*/0);
36076 +       if (unlikely(wkq_err)) {
36077 +               pr_warn("rmdir error %pd (%d), ignored\n", wh_dentry, wkq_err);
36078 +               au_whtmp_rmdir_free(args);
36079 +       }
36080 +}
36081 diff -urN /usr/share/empty/fs/aufs/whout.h linux/fs/aufs/whout.h
36082 --- /usr/share/empty/fs/aufs/whout.h    1970-01-01 01:00:00.000000000 +0100
36083 +++ linux/fs/aufs/whout.h       2022-03-21 14:49:05.733299677 +0100
36084 @@ -0,0 +1,87 @@
36085 +/* SPDX-License-Identifier: GPL-2.0 */
36086 +/*
36087 + * Copyright (C) 2005-2021 Junjiro R. Okajima
36088 + *
36089 + * This program, aufs is free software; you can redistribute it and/or modify
36090 + * it under the terms of the GNU General Public License as published by
36091 + * the Free Software Foundation; either version 2 of the License, or
36092 + * (at your option) any later version.
36093 + *
36094 + * This program is distributed in the hope that it will be useful,
36095 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
36096 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36097 + * GNU General Public License for more details.
36098 + *
36099 + * You should have received a copy of the GNU General Public License
36100 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
36101 + */
36102 +
36103 +/*
36104 + * whiteout for logical deletion and opaque directory
36105 + */
36106 +
36107 +#ifndef __AUFS_WHOUT_H__
36108 +#define __AUFS_WHOUT_H__
36109 +
36110 +#ifdef __KERNEL__
36111 +
36112 +#include "dir.h"
36113 +
36114 +/* whout.c */
36115 +int au_wh_name_alloc(struct qstr *wh, const struct qstr *name);
36116 +int au_wh_test(struct user_namespace *h_userns, struct path *h_ppath,
36117 +              struct qstr *wh_name, int try_sio);
36118 +int au_diropq_test(struct user_namespace *h_userns, struct path *h_path);
36119 +struct au_branch;
36120 +struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
36121 +                            struct qstr *prefix);
36122 +int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br);
36123 +int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
36124 +                       struct dentry *dentry);
36125 +int au_wh_init(struct au_branch *br, struct super_block *sb);
36126 +
36127 +/* diropq flags */
36128 +#define AuDiropq_CREATE        1
36129 +#define au_ftest_diropq(flags, name)   ((flags) & AuDiropq_##name)
36130 +#define au_fset_diropq(flags, name) \
36131 +       do { (flags) |= AuDiropq_##name; } while (0)
36132 +#define au_fclr_diropq(flags, name) \
36133 +       do { (flags) &= ~AuDiropq_##name; } while (0)
36134 +
36135 +struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
36136 +                            unsigned int flags);
36137 +struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
36138 +                         struct au_branch *br);
36139 +struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
36140 +                           struct dentry *h_parent);
36141 +
36142 +/* real rmdir for the whiteout-ed dir */
36143 +struct au_whtmp_rmdir {
36144 +       struct inode *dir;
36145 +       struct au_branch *br;
36146 +       struct dentry *wh_dentry;
36147 +       struct au_nhash whlist;
36148 +};
36149 +
36150 +struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp);
36151 +void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp);
36152 +int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
36153 +                  struct dentry *wh_dentry, struct au_nhash *whlist);
36154 +void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
36155 +                        struct dentry *wh_dentry, struct au_whtmp_rmdir *args);
36156 +
36157 +/* ---------------------------------------------------------------------- */
36158 +
36159 +static inline struct dentry *au_diropq_create(struct dentry *dentry,
36160 +                                             aufs_bindex_t bindex)
36161 +{
36162 +       return au_diropq_sio(dentry, bindex, AuDiropq_CREATE);
36163 +}
36164 +
36165 +static inline int au_diropq_remove(struct dentry *dentry, aufs_bindex_t bindex)
36166 +{
36167 +       return PTR_ERR(au_diropq_sio(dentry, bindex, !AuDiropq_CREATE));
36168 +}
36169 +
36170 +#endif /* __KERNEL__ */
36171 +#endif /* __AUFS_WHOUT_H__ */
36172 diff -urN /usr/share/empty/fs/aufs/wkq.c linux/fs/aufs/wkq.c
36173 --- /usr/share/empty/fs/aufs/wkq.c      1970-01-01 01:00:00.000000000 +0100
36174 +++ linux/fs/aufs/wkq.c 2022-03-21 14:49:05.733299677 +0100
36175 @@ -0,0 +1,372 @@
36176 +// SPDX-License-Identifier: GPL-2.0
36177 +/*
36178 + * Copyright (C) 2005-2021 Junjiro R. Okajima
36179 + *
36180 + * This program, aufs is free software; you can redistribute it and/or modify
36181 + * it under the terms of the GNU General Public License as published by
36182 + * the Free Software Foundation; either version 2 of the License, or
36183 + * (at your option) any later version.
36184 + *
36185 + * This program is distributed in the hope that it will be useful,
36186 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
36187 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36188 + * GNU General Public License for more details.
36189 + *
36190 + * You should have received a copy of the GNU General Public License
36191 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
36192 + */
36193 +
36194 +/*
36195 + * workqueue for asynchronous/super-io operations
36196 + * todo: try new credential scheme
36197 + */
36198 +
36199 +#include <linux/module.h>
36200 +#include "aufs.h"
36201 +
36202 +/* internal workqueue named AUFS_WKQ_NAME */
36203 +
36204 +static struct workqueue_struct *au_wkq;
36205 +
36206 +struct au_wkinfo {
36207 +       struct work_struct wk;
36208 +       struct kobject *kobj;
36209 +
36210 +       unsigned int flags; /* see wkq.h */
36211 +
36212 +       au_wkq_func_t func;
36213 +       void *args;
36214 +
36215 +#ifdef CONFIG_LOCKDEP
36216 +       int dont_check;
36217 +       struct held_lock **hlock;
36218 +#endif
36219 +
36220 +       struct completion *comp;
36221 +};
36222 +
36223 +/* ---------------------------------------------------------------------- */
36224 +/*
36225 + * Aufs passes some operations to the workqueue such as the internal copyup.
36226 + * This scheme looks rather unnatural for LOCKDEP debugging feature, since the
36227 + * job run by workqueue depends upon the locks acquired in the other task.
36228 + * Delegating a small operation to the workqueue, aufs passes its lockdep
36229 + * information too. And the job in the workqueue restores the info in order to
36230 + * pretend as if it acquired those locks. This is just to make LOCKDEP work
36231 + * correctly and expectedly.
36232 + */
36233 +
36234 +#ifndef CONFIG_LOCKDEP
36235 +AuStubInt0(au_wkq_lockdep_alloc, struct au_wkinfo *wkinfo);
36236 +AuStubVoid(au_wkq_lockdep_free, struct au_wkinfo *wkinfo);
36237 +AuStubVoid(au_wkq_lockdep_pre, struct au_wkinfo *wkinfo);
36238 +AuStubVoid(au_wkq_lockdep_post, struct au_wkinfo *wkinfo);
36239 +AuStubVoid(au_wkq_lockdep_init, struct au_wkinfo *wkinfo);
36240 +#else
36241 +static void au_wkq_lockdep_init(struct au_wkinfo *wkinfo)
36242 +{
36243 +       wkinfo->hlock = NULL;
36244 +       wkinfo->dont_check = 0;
36245 +}
36246 +
36247 +/*
36248 + * 1: matched
36249 + * 0: unmatched
36250 + */
36251 +static int au_wkq_lockdep_test(struct lock_class_key *key, const char *name)
36252 +{
36253 +       static DEFINE_SPINLOCK(spin);
36254 +       static struct {
36255 +               char *name;
36256 +               struct lock_class_key *key;
36257 +       } a[] = {
36258 +               { .name = "&sbinfo->si_rwsem" },
36259 +               { .name = "&finfo->fi_rwsem" },
36260 +               { .name = "&dinfo->di_rwsem" },
36261 +               { .name = "&iinfo->ii_rwsem" }
36262 +       };
36263 +       static int set;
36264 +       int i;
36265 +
36266 +       /* lockless read from 'set.' see below */
36267 +       if (set == ARRAY_SIZE(a)) {
36268 +               for (i = 0; i < ARRAY_SIZE(a); i++)
36269 +                       if (a[i].key == key)
36270 +                               goto match;
36271 +               goto unmatch;
36272 +       }
36273 +
36274 +       spin_lock(&spin);
36275 +       if (set)
36276 +               for (i = 0; i < ARRAY_SIZE(a); i++)
36277 +                       if (a[i].key == key) {
36278 +                               spin_unlock(&spin);
36279 +                               goto match;
36280 +                       }
36281 +       for (i = 0; i < ARRAY_SIZE(a); i++) {
36282 +               if (a[i].key) {
36283 +                       if (unlikely(a[i].key == key)) { /* rare but possible */
36284 +                               spin_unlock(&spin);
36285 +                               goto match;
36286 +                       } else
36287 +                               continue;
36288 +               }
36289 +               if (strstr(a[i].name, name)) {
36290 +                       /*
36291 +                        * the order of these three lines is important for the
36292 +                        * lockless read above.
36293 +                        */
36294 +                       a[i].key = key;
36295 +                       spin_unlock(&spin);
36296 +                       set++;
36297 +                       /* AuDbg("%d, %s\n", set, name); */
36298 +                       goto match;
36299 +               }
36300 +       }
36301 +       spin_unlock(&spin);
36302 +       goto unmatch;
36303 +
36304 +match:
36305 +       return 1;
36306 +unmatch:
36307 +       return 0;
36308 +}
36309 +
36310 +static int au_wkq_lockdep_alloc(struct au_wkinfo *wkinfo)
36311 +{
36312 +       int err, n;
36313 +       struct task_struct *curr;
36314 +       struct held_lock **hl, *held_locks, *p;
36315 +
36316 +       err = 0;
36317 +       curr = current;
36318 +       wkinfo->dont_check = lockdep_recursing(curr);
36319 +       if (wkinfo->dont_check)
36320 +               goto out;
36321 +       n = curr->lockdep_depth;
36322 +       if (!n)
36323 +               goto out;
36324 +
36325 +       err = -ENOMEM;
36326 +       wkinfo->hlock = kmalloc_array(n + 1, sizeof(*wkinfo->hlock), GFP_NOFS);
36327 +       if (unlikely(!wkinfo->hlock))
36328 +               goto out;
36329 +
36330 +       err = 0;
36331 +#if 0 /* left for debugging */
36332 +       if (0 && au_debug_test())
36333 +               lockdep_print_held_locks(curr);
36334 +#endif
36335 +       held_locks = curr->held_locks;
36336 +       hl = wkinfo->hlock;
36337 +       while (n--) {
36338 +               p = held_locks++;
36339 +               if (au_wkq_lockdep_test(p->instance->key, p->instance->name))
36340 +                       *hl++ = p;
36341 +       }
36342 +       *hl = NULL;
36343 +
36344 +out:
36345 +       return err;
36346 +}
36347 +
36348 +static void au_wkq_lockdep_free(struct au_wkinfo *wkinfo)
36349 +{
36350 +       au_kfree_try_rcu(wkinfo->hlock);
36351 +}
36352 +
36353 +static void au_wkq_lockdep_pre(struct au_wkinfo *wkinfo)
36354 +{
36355 +       struct held_lock *p, **hl = wkinfo->hlock;
36356 +       int subclass;
36357 +
36358 +       if (wkinfo->dont_check)
36359 +               lockdep_off();
36360 +       if (!hl)
36361 +               return;
36362 +       while ((p = *hl++)) { /* assignment */
36363 +               subclass = lockdep_hlock_class(p)->subclass;
36364 +               /* AuDbg("%s, %d\n", p->instance->name, subclass); */
36365 +               if (p->read)
36366 +                       rwsem_acquire_read(p->instance, subclass, 0,
36367 +                                          /*p->acquire_ip*/_RET_IP_);
36368 +               else
36369 +                       rwsem_acquire(p->instance, subclass, 0,
36370 +                                     /*p->acquire_ip*/_RET_IP_);
36371 +       }
36372 +}
36373 +
36374 +static void au_wkq_lockdep_post(struct au_wkinfo *wkinfo)
36375 +{
36376 +       struct held_lock *p, **hl = wkinfo->hlock;
36377 +
36378 +       if (wkinfo->dont_check)
36379 +               lockdep_on();
36380 +       if (!hl)
36381 +               return;
36382 +       while ((p = *hl++)) /* assignment */
36383 +               rwsem_release(p->instance, /*p->acquire_ip*/_RET_IP_);
36384 +}
36385 +#endif
36386 +
36387 +static void wkq_func(struct work_struct *wk)
36388 +{
36389 +       struct au_wkinfo *wkinfo = container_of(wk, struct au_wkinfo, wk);
36390 +
36391 +       AuDebugOn(!uid_eq(current_fsuid(), GLOBAL_ROOT_UID));
36392 +       AuDebugOn(rlimit(RLIMIT_FSIZE) != RLIM_INFINITY);
36393 +
36394 +       au_wkq_lockdep_pre(wkinfo);
36395 +       wkinfo->func(wkinfo->args);
36396 +       au_wkq_lockdep_post(wkinfo);
36397 +       if (au_ftest_wkq(wkinfo->flags, WAIT))
36398 +               complete(wkinfo->comp);
36399 +       else {
36400 +               kobject_put(wkinfo->kobj);
36401 +               module_put(THIS_MODULE); /* todo: ?? */
36402 +               au_kfree_rcu(wkinfo);
36403 +       }
36404 +}
36405 +
36406 +/*
36407 + * Since struct completion is large, try allocating it dynamically.
36408 + */
36409 +#define AuWkqCompDeclare(name) struct completion *comp = NULL
36410 +
36411 +static int au_wkq_comp_alloc(struct au_wkinfo *wkinfo, struct completion **comp)
36412 +{
36413 +       *comp = kmalloc(sizeof(**comp), GFP_NOFS);
36414 +       if (*comp) {
36415 +               init_completion(*comp);
36416 +               wkinfo->comp = *comp;
36417 +               return 0;
36418 +       }
36419 +       return -ENOMEM;
36420 +}
36421 +
36422 +static void au_wkq_comp_free(struct completion *comp)
36423 +{
36424 +       au_kfree_rcu(comp);
36425 +}
36426 +
36427 +static void au_wkq_run(struct au_wkinfo *wkinfo)
36428 +{
36429 +       if (au_ftest_wkq(wkinfo->flags, NEST)) {
36430 +               if (au_wkq_test()) {
36431 +                       AuWarn1("wkq from wkq, unless silly-rename on NFS,"
36432 +                               " due to a dead dir by UDBA,"
36433 +                               " or async xino write?\n");
36434 +                       AuDebugOn(au_ftest_wkq(wkinfo->flags, WAIT));
36435 +               }
36436 +       } else
36437 +               au_dbg_verify_kthread();
36438 +
36439 +       if (au_ftest_wkq(wkinfo->flags, WAIT)) {
36440 +               INIT_WORK_ONSTACK(&wkinfo->wk, wkq_func);
36441 +               queue_work(au_wkq, &wkinfo->wk);
36442 +       } else {
36443 +               INIT_WORK(&wkinfo->wk, wkq_func);
36444 +               schedule_work(&wkinfo->wk);
36445 +       }
36446 +}
36447 +
36448 +/*
36449 + * Be careful. It is easy to make deadlock happen.
36450 + * processA: lock, wkq and wait
36451 + * processB: wkq and wait, lock in wkq
36452 + * --> deadlock
36453 + */
36454 +int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args)
36455 +{
36456 +       int err;
36457 +       AuWkqCompDeclare(comp);
36458 +       struct au_wkinfo wkinfo = {
36459 +               .flags  = flags,
36460 +               .func   = func,
36461 +               .args   = args
36462 +       };
36463 +
36464 +       err = au_wkq_comp_alloc(&wkinfo, &comp);
36465 +       if (unlikely(err))
36466 +               goto out;
36467 +       err = au_wkq_lockdep_alloc(&wkinfo);
36468 +       if (unlikely(err))
36469 +               goto out_comp;
36470 +       if (!err) {
36471 +               au_wkq_run(&wkinfo);
36472 +               /* no timeout, no interrupt */
36473 +               wait_for_completion(wkinfo.comp);
36474 +       }
36475 +       au_wkq_lockdep_free(&wkinfo);
36476 +
36477 +out_comp:
36478 +       au_wkq_comp_free(comp);
36479 +out:
36480 +       destroy_work_on_stack(&wkinfo.wk);
36481 +       return err;
36482 +}
36483 +
36484 +/*
36485 + * Note: dget/dput() in func for aufs dentries are not supported. It will be a
36486 + * problem in a concurrent umounting.
36487 + */
36488 +int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
36489 +                 unsigned int flags)
36490 +{
36491 +       int err;
36492 +       struct au_wkinfo *wkinfo;
36493 +
36494 +       atomic_inc(&au_sbi(sb)->si_nowait.nw_len);
36495 +
36496 +       /*
36497 +        * wkq_func() must free this wkinfo.
36498 +        * it highly depends upon the implementation of workqueue.
36499 +        */
36500 +       err = 0;
36501 +       wkinfo = kmalloc(sizeof(*wkinfo), GFP_NOFS);
36502 +       if (wkinfo) {
36503 +               wkinfo->kobj = &au_sbi(sb)->si_kobj;
36504 +               wkinfo->flags = flags & ~AuWkq_WAIT;
36505 +               wkinfo->func = func;
36506 +               wkinfo->args = args;
36507 +               wkinfo->comp = NULL;
36508 +               au_wkq_lockdep_init(wkinfo);
36509 +               kobject_get(wkinfo->kobj);
36510 +               __module_get(THIS_MODULE); /* todo: ?? */
36511 +
36512 +               au_wkq_run(wkinfo);
36513 +       } else {
36514 +               err = -ENOMEM;
36515 +               au_nwt_done(&au_sbi(sb)->si_nowait);
36516 +       }
36517 +
36518 +       return err;
36519 +}
36520 +
36521 +/* ---------------------------------------------------------------------- */
36522 +
36523 +void au_nwt_init(struct au_nowait_tasks *nwt)
36524 +{
36525 +       atomic_set(&nwt->nw_len, 0);
36526 +       /* smp_mb(); */ /* atomic_set */
36527 +       init_waitqueue_head(&nwt->nw_wq);
36528 +}
36529 +
36530 +void au_wkq_fin(void)
36531 +{
36532 +       destroy_workqueue(au_wkq);
36533 +}
36534 +
36535 +int __init au_wkq_init(void)
36536 +{
36537 +       int err;
36538 +
36539 +       err = 0;
36540 +       au_wkq = alloc_workqueue(AUFS_WKQ_NAME, 0, WQ_DFL_ACTIVE);
36541 +       if (IS_ERR(au_wkq))
36542 +               err = PTR_ERR(au_wkq);
36543 +       else if (!au_wkq)
36544 +               err = -ENOMEM;
36545 +
36546 +       return err;
36547 +}
36548 diff -urN /usr/share/empty/fs/aufs/wkq.h linux/fs/aufs/wkq.h
36549 --- /usr/share/empty/fs/aufs/wkq.h      1970-01-01 01:00:00.000000000 +0100
36550 +++ linux/fs/aufs/wkq.h 2022-03-21 14:49:05.733299677 +0100
36551 @@ -0,0 +1,89 @@
36552 +/* SPDX-License-Identifier: GPL-2.0 */
36553 +/*
36554 + * Copyright (C) 2005-2021 Junjiro R. Okajima
36555 + *
36556 + * This program, aufs is free software; you can redistribute it and/or modify
36557 + * it under the terms of the GNU General Public License as published by
36558 + * the Free Software Foundation; either version 2 of the License, or
36559 + * (at your option) any later version.
36560 + *
36561 + * This program is distributed in the hope that it will be useful,
36562 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
36563 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36564 + * GNU General Public License for more details.
36565 + *
36566 + * You should have received a copy of the GNU General Public License
36567 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
36568 + */
36569 +
36570 +/*
36571 + * workqueue for asynchronous/super-io operations
36572 + * todo: try new credentials management scheme
36573 + */
36574 +
36575 +#ifndef __AUFS_WKQ_H__
36576 +#define __AUFS_WKQ_H__
36577 +
36578 +#ifdef __KERNEL__
36579 +
36580 +#include <linux/wait.h>
36581 +
36582 +struct super_block;
36583 +
36584 +/* ---------------------------------------------------------------------- */
36585 +
36586 +/*
36587 + * in the next operation, wait for the 'nowait' tasks in system-wide workqueue
36588 + */
36589 +struct au_nowait_tasks {
36590 +       atomic_t                nw_len;
36591 +       wait_queue_head_t       nw_wq;
36592 +};
36593 +
36594 +/* ---------------------------------------------------------------------- */
36595 +
36596 +typedef void (*au_wkq_func_t)(void *args);
36597 +
36598 +/* wkq flags */
36599 +#define AuWkq_WAIT     1
36600 +#define AuWkq_NEST     (1 << 1)
36601 +#define au_ftest_wkq(flags, name)      ((flags) & AuWkq_##name)
36602 +#define au_fset_wkq(flags, name) \
36603 +       do { (flags) |= AuWkq_##name; } while (0)
36604 +#define au_fclr_wkq(flags, name) \
36605 +       do { (flags) &= ~AuWkq_##name; } while (0)
36606 +
36607 +/* wkq.c */
36608 +int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args);
36609 +int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
36610 +                 unsigned int flags);
36611 +void au_nwt_init(struct au_nowait_tasks *nwt);
36612 +int __init au_wkq_init(void);
36613 +void au_wkq_fin(void);
36614 +
36615 +/* ---------------------------------------------------------------------- */
36616 +
36617 +static inline int au_wkq_test(void)
36618 +{
36619 +       return current->flags & PF_WQ_WORKER;
36620 +}
36621 +
36622 +static inline int au_wkq_wait(au_wkq_func_t func, void *args)
36623 +{
36624 +       return au_wkq_do_wait(AuWkq_WAIT, func, args);
36625 +}
36626 +
36627 +static inline void au_nwt_done(struct au_nowait_tasks *nwt)
36628 +{
36629 +       if (atomic_dec_and_test(&nwt->nw_len))
36630 +               wake_up_all(&nwt->nw_wq);
36631 +}
36632 +
36633 +static inline int au_nwt_flush(struct au_nowait_tasks *nwt)
36634 +{
36635 +       wait_event(nwt->nw_wq, !atomic_read(&nwt->nw_len));
36636 +       return 0;
36637 +}
36638 +
36639 +#endif /* __KERNEL__ */
36640 +#endif /* __AUFS_WKQ_H__ */
36641 diff -urN /usr/share/empty/fs/aufs/xattr.c linux/fs/aufs/xattr.c
36642 --- /usr/share/empty/fs/aufs/xattr.c    1970-01-01 01:00:00.000000000 +0100
36643 +++ linux/fs/aufs/xattr.c       2022-03-21 14:49:05.733299677 +0100
36644 @@ -0,0 +1,368 @@
36645 +// SPDX-License-Identifier: GPL-2.0
36646 +/*
36647 + * Copyright (C) 2014-2021 Junjiro R. Okajima
36648 + *
36649 + * This program, aufs is free software; you can redistribute it and/or modify
36650 + * it under the terms of the GNU General Public License as published by
36651 + * the Free Software Foundation; either version 2 of the License, or
36652 + * (at your option) any later version.
36653 + *
36654 + * This program is distributed in the hope that it will be useful,
36655 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
36656 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36657 + * GNU General Public License for more details.
36658 + *
36659 + * You should have received a copy of the GNU General Public License
36660 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
36661 + */
36662 +
36663 +/*
36664 + * handling xattr functions
36665 + */
36666 +
36667 +#include <linux/fs.h>
36668 +#include <linux/posix_acl_xattr.h>
36669 +#include <linux/xattr.h>
36670 +#include "aufs.h"
36671 +
36672 +static int au_xattr_ignore(int err, char *name, unsigned int ignore_flags)
36673 +{
36674 +       if (!ignore_flags)
36675 +               goto out;
36676 +       switch (err) {
36677 +       case -ENOMEM:
36678 +       case -EDQUOT:
36679 +               goto out;
36680 +       }
36681 +
36682 +       if ((ignore_flags & AuBrAttr_ICEX) == AuBrAttr_ICEX) {
36683 +               err = 0;
36684 +               goto out;
36685 +       }
36686 +
36687 +#define cmp(brattr, prefix) do {                                       \
36688 +               if (!strncmp(name, XATTR_##prefix##_PREFIX,             \
36689 +                            XATTR_##prefix##_PREFIX_LEN)) {            \
36690 +                       if (ignore_flags & AuBrAttr_ICEX_##brattr)      \
36691 +                               err = 0;                                \
36692 +                       goto out;                                       \
36693 +               }                                                       \
36694 +       } while (0)
36695 +
36696 +       cmp(SEC, SECURITY);
36697 +       cmp(SYS, SYSTEM);
36698 +       cmp(TR, TRUSTED);
36699 +       cmp(USR, USER);
36700 +#undef cmp
36701 +
36702 +       if (ignore_flags & AuBrAttr_ICEX_OTH)
36703 +               err = 0;
36704 +
36705 +out:
36706 +       return err;
36707 +}
36708 +
36709 +static const int au_xattr_out_of_list = AuBrAttr_ICEX_OTH << 1;
36710 +
36711 +static int au_do_cpup_xattr(struct path *h_dst, struct path *h_src,
36712 +                           char *name, char **buf, unsigned int ignore_flags,
36713 +                           unsigned int verbose)
36714 +{
36715 +       int err;
36716 +       ssize_t ssz;
36717 +       struct inode *h_idst;
36718 +       struct dentry *h_dst_dentry, *h_src_dentry;
36719 +       struct user_namespace *h_dst_userns, *h_src_userns;
36720 +
36721 +       h_src_userns = mnt_user_ns(h_src->mnt);
36722 +       h_src_dentry = h_src->dentry;
36723 +       ssz = vfs_getxattr_alloc(h_src_userns, h_src_dentry, name, buf, 0,
36724 +                                GFP_NOFS);
36725 +       err = ssz;
36726 +       if (unlikely(err <= 0)) {
36727 +               if (err == -ENODATA
36728 +                   || (err == -EOPNOTSUPP
36729 +                       && ((ignore_flags & au_xattr_out_of_list)
36730 +                           || (au_test_nfs_noacl(d_inode(h_src_dentry))
36731 +                               && (!strcmp(name, XATTR_NAME_POSIX_ACL_ACCESS)
36732 +                                   || !strcmp(name,
36733 +                                              XATTR_NAME_POSIX_ACL_DEFAULT))))
36734 +                           ))
36735 +                       err = 0;
36736 +               if (err && (verbose || au_debug_test()))
36737 +                       pr_err("%s, err %d\n", name, err);
36738 +               goto out;
36739 +       }
36740 +
36741 +       /* unlock it temporary */
36742 +       h_dst_userns = mnt_user_ns(h_dst->mnt);
36743 +       h_dst_dentry = h_dst->dentry;
36744 +       h_idst = d_inode(h_dst_dentry);
36745 +       inode_unlock(h_idst);
36746 +       err = vfsub_setxattr(h_dst_userns, h_dst_dentry, name, *buf, ssz,
36747 +                            /*flags*/0);
36748 +       inode_lock_nested(h_idst, AuLsc_I_CHILD2);
36749 +       if (unlikely(err)) {
36750 +               if (verbose || au_debug_test())
36751 +                       pr_err("%s, err %d\n", name, err);
36752 +               err = au_xattr_ignore(err, name, ignore_flags);
36753 +       }
36754 +
36755 +out:
36756 +       return err;
36757 +}
36758 +
36759 +int au_cpup_xattr(struct path *h_dst, struct path *h_src, int ignore_flags,
36760 +                 unsigned int verbose)
36761 +{
36762 +       int err, unlocked, acl_access, acl_default;
36763 +       ssize_t ssz;
36764 +       struct dentry *h_dst_dentry, *h_src_dentry;
36765 +       struct inode *h_isrc, *h_idst;
36766 +       char *value, *p, *o, *e;
36767 +
36768 +       /* try stopping to update the source inode while we are referencing */
36769 +       /* there should not be the parent-child relationship between them */
36770 +       h_dst_dentry = h_dst->dentry;
36771 +       h_idst = d_inode(h_dst_dentry);
36772 +       h_src_dentry = h_src->dentry;
36773 +       h_isrc = d_inode(h_src_dentry);
36774 +       inode_unlock(h_idst);
36775 +       inode_lock_shared_nested(h_isrc, AuLsc_I_CHILD);
36776 +       inode_lock_nested(h_idst, AuLsc_I_CHILD2);
36777 +       unlocked = 0;
36778 +
36779 +       /* some filesystems don't list POSIX ACL, for example tmpfs */
36780 +       ssz = vfs_listxattr(h_src_dentry, NULL, 0);
36781 +       err = ssz;
36782 +       if (unlikely(err < 0)) {
36783 +               AuTraceErr(err);
36784 +               if (err == -ENODATA
36785 +                   || err == -EOPNOTSUPP)
36786 +                       err = 0;        /* ignore */
36787 +               goto out;
36788 +       }
36789 +
36790 +       err = 0;
36791 +       p = NULL;
36792 +       o = NULL;
36793 +       if (ssz) {
36794 +               err = -ENOMEM;
36795 +               p = kmalloc(ssz, GFP_NOFS);
36796 +               o = p;
36797 +               if (unlikely(!p))
36798 +                       goto out;
36799 +               err = vfs_listxattr(h_src_dentry, p, ssz);
36800 +       }
36801 +       inode_unlock_shared(h_isrc);
36802 +       unlocked = 1;
36803 +       AuDbg("err %d, ssz %zd\n", err, ssz);
36804 +       if (unlikely(err < 0))
36805 +               goto out_free;
36806 +
36807 +       err = 0;
36808 +       e = p + ssz;
36809 +       value = NULL;
36810 +       acl_access = 0;
36811 +       acl_default = 0;
36812 +       while (!err && p < e) {
36813 +               acl_access |= !strncmp(p, XATTR_NAME_POSIX_ACL_ACCESS,
36814 +                                      sizeof(XATTR_NAME_POSIX_ACL_ACCESS) - 1);
36815 +               acl_default |= !strncmp(p, XATTR_NAME_POSIX_ACL_DEFAULT,
36816 +                                       sizeof(XATTR_NAME_POSIX_ACL_DEFAULT)
36817 +                                       - 1);
36818 +               err = au_do_cpup_xattr(h_dst, h_src, p, &value, ignore_flags,
36819 +                                      verbose);
36820 +               p += strlen(p) + 1;
36821 +       }
36822 +       AuTraceErr(err);
36823 +       ignore_flags |= au_xattr_out_of_list;
36824 +       if (!err && !acl_access) {
36825 +               err = au_do_cpup_xattr(h_dst, h_src,
36826 +                                      XATTR_NAME_POSIX_ACL_ACCESS, &value,
36827 +                                      ignore_flags, verbose);
36828 +               AuTraceErr(err);
36829 +       }
36830 +       if (!err && !acl_default) {
36831 +               err = au_do_cpup_xattr(h_dst, h_src,
36832 +                                      XATTR_NAME_POSIX_ACL_DEFAULT, &value,
36833 +                                      ignore_flags, verbose);
36834 +               AuTraceErr(err);
36835 +       }
36836 +
36837 +       au_kfree_try_rcu(value);
36838 +
36839 +out_free:
36840 +       au_kfree_try_rcu(o);
36841 +out:
36842 +       if (!unlocked)
36843 +               inode_unlock_shared(h_isrc);
36844 +       AuTraceErr(err);
36845 +       return err;
36846 +}
36847 +
36848 +/* ---------------------------------------------------------------------- */
36849 +
36850 +static int au_smack_reentering(struct super_block *sb)
36851 +{
36852 +#if IS_ENABLED(CONFIG_SECURITY_SMACK) || IS_ENABLED(CONFIG_SECURITY_SELINUX)
36853 +       /*
36854 +        * as a part of lookup, smack_d_instantiate() is called, and it calls
36855 +        * i_op->getxattr(). ouch.
36856 +        */
36857 +       return si_pid_test(sb);
36858 +#else
36859 +       return 0;
36860 +#endif
36861 +}
36862 +
36863 +enum {
36864 +       AU_XATTR_LIST,
36865 +       AU_XATTR_GET
36866 +};
36867 +
36868 +struct au_lgxattr {
36869 +       int type;
36870 +       union {
36871 +               struct {
36872 +                       char    *list;
36873 +                       size_t  size;
36874 +               } list;
36875 +               struct {
36876 +                       const char      *name;
36877 +                       void            *value;
36878 +                       size_t          size;
36879 +               } get;
36880 +       } u;
36881 +};
36882 +
36883 +static ssize_t au_lgxattr(struct dentry *dentry, struct inode *inode,
36884 +                         struct au_lgxattr *arg)
36885 +{
36886 +       ssize_t err;
36887 +       int reenter;
36888 +       struct path h_path;
36889 +       struct super_block *sb;
36890 +
36891 +       sb = dentry->d_sb;
36892 +       reenter = au_smack_reentering(sb);
36893 +       if (!reenter) {
36894 +               err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
36895 +               if (unlikely(err))
36896 +                       goto out;
36897 +       }
36898 +       err = au_h_path_getattr(dentry, inode, /*force*/1, &h_path, reenter);
36899 +       if (unlikely(err))
36900 +               goto out_si;
36901 +       if (unlikely(!h_path.dentry))
36902 +               /* illegally overlapped or something */
36903 +               goto out_di; /* pretending success */
36904 +
36905 +       /* always topmost entry only */
36906 +       switch (arg->type) {
36907 +       case AU_XATTR_LIST:
36908 +               err = vfs_listxattr(h_path.dentry,
36909 +                                   arg->u.list.list, arg->u.list.size);
36910 +               break;
36911 +       case AU_XATTR_GET:
36912 +               AuDebugOn(d_is_negative(h_path.dentry));
36913 +               err = vfs_getxattr(mnt_user_ns(h_path.mnt), h_path.dentry,
36914 +                                  arg->u.get.name, arg->u.get.value,
36915 +                                  arg->u.get.size);
36916 +               break;
36917 +       }
36918 +
36919 +out_di:
36920 +       if (!reenter)
36921 +               di_read_unlock(dentry, AuLock_IR);
36922 +out_si:
36923 +       if (!reenter)
36924 +               si_read_unlock(sb);
36925 +out:
36926 +       AuTraceErr(err);
36927 +       return err;
36928 +}
36929 +
36930 +ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size)
36931 +{
36932 +       struct au_lgxattr arg = {
36933 +               .type = AU_XATTR_LIST,
36934 +               .u.list = {
36935 +                       .list   = list,
36936 +                       .size   = size
36937 +               },
36938 +       };
36939 +
36940 +       return au_lgxattr(dentry, /*inode*/NULL, &arg);
36941 +}
36942 +
36943 +static ssize_t au_getxattr(struct dentry *dentry, struct inode *inode,
36944 +                          const char *name, void *value, size_t size)
36945 +{
36946 +       struct au_lgxattr arg = {
36947 +               .type = AU_XATTR_GET,
36948 +               .u.get = {
36949 +                       .name   = name,
36950 +                       .value  = value,
36951 +                       .size   = size
36952 +               },
36953 +       };
36954 +
36955 +       return au_lgxattr(dentry, inode, &arg);
36956 +}
36957 +
36958 +static int au_setxattr(struct dentry *dentry, struct inode *inode,
36959 +                      const char *name, const void *value, size_t size,
36960 +                      int flags)
36961 +{
36962 +       struct au_sxattr arg = {
36963 +               .type = AU_XATTR_SET,
36964 +               .u.set = {
36965 +                       .name   = name,
36966 +                       .value  = value,
36967 +                       .size   = size,
36968 +                       .flags  = flags
36969 +               },
36970 +       };
36971 +
36972 +       return au_sxattr(dentry, inode, &arg);
36973 +}
36974 +
36975 +/* ---------------------------------------------------------------------- */
36976 +
36977 +static int au_xattr_get(const struct xattr_handler *handler,
36978 +                       struct dentry *dentry, struct inode *inode,
36979 +                       const char *name, void *buffer, size_t size)
36980 +{
36981 +       return au_getxattr(dentry, inode, name, buffer, size);
36982 +}
36983 +
36984 +static int au_xattr_set(const struct xattr_handler *handler,
36985 +                       struct user_namespace *userns,
36986 +                       struct dentry *dentry, struct inode *inode,
36987 +                       const char *name, const void *value, size_t size,
36988 +                       int flags)
36989 +{
36990 +       return au_setxattr(dentry, inode, name, value, size, flags);
36991 +}
36992 +
36993 +static const struct xattr_handler au_xattr_handler = {
36994 +       .name   = "",
36995 +       .prefix = "",
36996 +       .get    = au_xattr_get,
36997 +       .set    = au_xattr_set
36998 +};
36999 +
37000 +static const struct xattr_handler *au_xattr_handlers[] = {
37001 +#ifdef CONFIG_FS_POSIX_ACL
37002 +       &posix_acl_access_xattr_handler,
37003 +       &posix_acl_default_xattr_handler,
37004 +#endif
37005 +       &au_xattr_handler, /* must be last */
37006 +       NULL
37007 +};
37008 +
37009 +void au_xattr_init(struct super_block *sb)
37010 +{
37011 +       sb->s_xattr = au_xattr_handlers;
37012 +}
37013 diff -urN /usr/share/empty/fs/aufs/xino.c linux/fs/aufs/xino.c
37014 --- /usr/share/empty/fs/aufs/xino.c     1970-01-01 01:00:00.000000000 +0100
37015 +++ linux/fs/aufs/xino.c        2022-03-21 14:49:05.733299677 +0100
37016 @@ -0,0 +1,1926 @@
37017 +// SPDX-License-Identifier: GPL-2.0
37018 +/*
37019 + * Copyright (C) 2005-2021 Junjiro R. Okajima
37020 + *
37021 + * This program, aufs is free software; you can redistribute it and/or modify
37022 + * it under the terms of the GNU General Public License as published by
37023 + * the Free Software Foundation; either version 2 of the License, or
37024 + * (at your option) any later version.
37025 + *
37026 + * This program is distributed in the hope that it will be useful,
37027 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
37028 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
37029 + * GNU General Public License for more details.
37030 + *
37031 + * You should have received a copy of the GNU General Public License
37032 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
37033 + */
37034 +
37035 +/*
37036 + * external inode number translation table and bitmap
37037 + *
37038 + * things to consider
37039 + * - the lifetime
37040 + *   + au_xino object
37041 + *   + XINO files (xino, xib, xigen)
37042 + *   + dynamic debugfs entries (xiN)
37043 + *   + static debugfs entries (xib, xigen)
37044 + *   + static sysfs entry (xi_path)
37045 + * - several entry points to handle them.
37046 + *   + mount(2) without xino option (default)
37047 + *   + mount(2) with xino option
37048 + *   + mount(2) with noxino option
37049 + *   + umount(2)
37050 + *   + remount with add/del branches
37051 + *   + remount with xino/noxino options
37052 + */
37053 +
37054 +#include <linux/seq_file.h>
37055 +#include <linux/statfs.h>
37056 +#include "aufs.h"
37057 +
37058 +static aufs_bindex_t sbr_find_shared(struct super_block *sb, aufs_bindex_t btop,
37059 +                                    aufs_bindex_t bbot,
37060 +                                    struct super_block *h_sb)
37061 +{
37062 +       /* todo: try binary-search if the branches are many */
37063 +       for (; btop <= bbot; btop++)
37064 +               if (h_sb == au_sbr_sb(sb, btop))
37065 +                       return btop;
37066 +       return -1;
37067 +}
37068 +
37069 +/*
37070 + * find another branch who is on the same filesystem of the specified
37071 + * branch{@btgt}. search until @bbot.
37072 + */
37073 +static aufs_bindex_t is_sb_shared(struct super_block *sb, aufs_bindex_t btgt,
37074 +                                 aufs_bindex_t bbot)
37075 +{
37076 +       aufs_bindex_t bindex;
37077 +       struct super_block *tgt_sb;
37078 +
37079 +       tgt_sb = au_sbr_sb(sb, btgt);
37080 +       bindex = sbr_find_shared(sb, /*btop*/0, btgt - 1, tgt_sb);
37081 +       if (bindex < 0)
37082 +               bindex = sbr_find_shared(sb, btgt + 1, bbot, tgt_sb);
37083 +
37084 +       return bindex;
37085 +}
37086 +
37087 +/* ---------------------------------------------------------------------- */
37088 +
37089 +/*
37090 + * stop unnecessary notify events at creating xino files
37091 + */
37092 +
37093 +aufs_bindex_t au_xi_root(struct super_block *sb, struct dentry *dentry)
37094 +{
37095 +       aufs_bindex_t bfound, bindex, bbot;
37096 +       struct dentry *parent;
37097 +       struct au_branch *br;
37098 +
37099 +       bfound = -1;
37100 +       parent = dentry->d_parent; /* safe d_parent access */
37101 +       bbot = au_sbbot(sb);
37102 +       for (bindex = 0; bindex <= bbot; bindex++) {
37103 +               br = au_sbr(sb, bindex);
37104 +               if (au_br_dentry(br) == parent) {
37105 +                       bfound = bindex;
37106 +                       break;
37107 +               }
37108 +       }
37109 +
37110 +       AuDbg("bfound b%d\n", bfound);
37111 +       return bfound;
37112 +}
37113 +
37114 +struct au_xino_lock_dir {
37115 +       struct au_hinode *hdir;
37116 +       struct dentry *parent;
37117 +       struct inode *dir;
37118 +};
37119 +
37120 +static struct dentry *au_dget_parent_lock(struct dentry *dentry,
37121 +                                         unsigned int lsc)
37122 +{
37123 +       struct dentry *parent;
37124 +       struct inode *dir;
37125 +
37126 +       parent = dget_parent(dentry);
37127 +       dir = d_inode(parent);
37128 +       inode_lock_nested(dir, lsc);
37129 +#if 0 /* it should not happen */
37130 +       spin_lock(&dentry->d_lock);
37131 +       if (unlikely(dentry->d_parent != parent)) {
37132 +               spin_unlock(&dentry->d_lock);
37133 +               inode_unlock(dir);
37134 +               dput(parent);
37135 +               parent = NULL;
37136 +               goto out;
37137 +       }
37138 +       spin_unlock(&dentry->d_lock);
37139 +
37140 +out:
37141 +#endif
37142 +       return parent;
37143 +}
37144 +
37145 +static void au_xino_lock_dir(struct super_block *sb, struct path *xipath,
37146 +                            struct au_xino_lock_dir *ldir)
37147 +{
37148 +       aufs_bindex_t bindex;
37149 +
37150 +       ldir->hdir = NULL;
37151 +       bindex = au_xi_root(sb, xipath->dentry);
37152 +       if (bindex >= 0) {
37153 +               /* rw branch root */
37154 +               ldir->hdir = au_hi(d_inode(sb->s_root), bindex);
37155 +               au_hn_inode_lock_nested(ldir->hdir, AuLsc_I_PARENT);
37156 +       } else {
37157 +               /* other */
37158 +               ldir->parent = au_dget_parent_lock(xipath->dentry,
37159 +                                                  AuLsc_I_PARENT);
37160 +               ldir->dir = d_inode(ldir->parent);
37161 +       }
37162 +}
37163 +
37164 +static void au_xino_unlock_dir(struct au_xino_lock_dir *ldir)
37165 +{
37166 +       if (ldir->hdir)
37167 +               au_hn_inode_unlock(ldir->hdir);
37168 +       else {
37169 +               inode_unlock(ldir->dir);
37170 +               dput(ldir->parent);
37171 +       }
37172 +}
37173 +
37174 +/* ---------------------------------------------------------------------- */
37175 +
37176 +/*
37177 + * create and set a new xino file
37178 + */
37179 +struct file *au_xino_create(struct super_block *sb, char *fpath, int silent,
37180 +                           int wbrtop)
37181 +{
37182 +       struct file *file;
37183 +       struct dentry *h_parent, *d;
37184 +       struct inode *h_dir, *inode;
37185 +       int err;
37186 +       static DEFINE_MUTEX(mtx);
37187 +
37188 +       /*
37189 +        * at mount-time, and the xino file is the default path,
37190 +        * hnotify is disabled so we have no notify events to ignore.
37191 +        * when a user specified the xino, we cannot get au_hdir to be ignored.
37192 +        */
37193 +       if (!wbrtop)
37194 +               mutex_lock(&mtx);
37195 +       file = vfsub_filp_open(fpath, O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
37196 +                              /* | __FMODE_NONOTIFY */,
37197 +                              0666);
37198 +       if (IS_ERR(file)) {
37199 +               if (!wbrtop)
37200 +                       mutex_unlock(&mtx);
37201 +               if (!silent)
37202 +                       pr_err("open %s(%ld)\n", fpath, PTR_ERR(file));
37203 +               return file;
37204 +       }
37205 +
37206 +       /* keep file count */
37207 +       err = 0;
37208 +       d = file->f_path.dentry;
37209 +       h_parent = au_dget_parent_lock(d, AuLsc_I_PARENT);
37210 +       if (!wbrtop)
37211 +               mutex_unlock(&mtx);
37212 +       /* mnt_want_write() is unnecessary here */
37213 +       h_dir = d_inode(h_parent);
37214 +       inode = file_inode(file);
37215 +       /* no delegation since it is just created */
37216 +       if (inode->i_nlink)
37217 +               err = vfsub_unlink(h_dir, &file->f_path, /*delegated*/NULL,
37218 +                                  /*force*/0);
37219 +       inode_unlock(h_dir);
37220 +       dput(h_parent);
37221 +       if (unlikely(err)) {
37222 +               if (!silent)
37223 +                       pr_err("unlink %s(%d)\n", fpath, err);
37224 +               goto out;
37225 +       }
37226 +
37227 +       err = -EINVAL;
37228 +       if (unlikely(sb && sb == d->d_sb)) {
37229 +               if (!silent)
37230 +                       pr_err("%s must be outside\n", fpath);
37231 +               goto out;
37232 +       }
37233 +       if (unlikely(au_test_fs_bad_xino(d->d_sb))) {
37234 +               if (!silent)
37235 +                       pr_err("xino doesn't support %s(%s)\n",
37236 +                              fpath, au_sbtype(d->d_sb));
37237 +               goto out;
37238 +       }
37239 +       return file; /* success */
37240 +
37241 +out:
37242 +       fput(file);
37243 +       file = ERR_PTR(err);
37244 +       return file;
37245 +}
37246 +
37247 +/*
37248 + * create a new xinofile at the same place/path as @base.
37249 + */
37250 +struct file *au_xino_create2(struct super_block *sb, struct path *base,
37251 +                            struct file *copy_src)
37252 +{
37253 +       struct file *file;
37254 +       struct dentry *dentry;
37255 +       struct inode *dir, *delegated;
37256 +       struct qstr *name;
37257 +       struct path ppath, path;
37258 +       int err, do_unlock;
37259 +       struct au_xino_lock_dir ldir;
37260 +
37261 +       do_unlock = 1;
37262 +       au_xino_lock_dir(sb, base, &ldir);
37263 +       dentry = base->dentry;
37264 +       ppath.dentry = dentry->d_parent; /* dir inode is locked */
37265 +       ppath.mnt = base->mnt;
37266 +       dir = d_inode(ppath.dentry);
37267 +       IMustLock(dir);
37268 +
37269 +       name = &dentry->d_name;
37270 +       path.dentry = vfsub_lookup_one_len(name->name, &ppath, name->len);
37271 +       if (IS_ERR(path.dentry)) {
37272 +               file = (void *)path.dentry;
37273 +               pr_err("%pd lookup err %ld\n", dentry, PTR_ERR(path.dentry));
37274 +               goto out;
37275 +       }
37276 +
37277 +       /* no need to mnt_want_write() since we call dentry_open() later */
37278 +       err = vfs_create(mnt_user_ns(base->mnt), dir, path.dentry, 0666, NULL);
37279 +       if (unlikely(err)) {
37280 +               file = ERR_PTR(err);
37281 +               pr_err("%pd create err %d\n", dentry, err);
37282 +               goto out_dput;
37283 +       }
37284 +
37285 +       path.mnt = base->mnt;
37286 +       file = vfsub_dentry_open(&path,
37287 +                                O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
37288 +                                /* | __FMODE_NONOTIFY */);
37289 +       if (IS_ERR(file)) {
37290 +               pr_err("%pd open err %ld\n", dentry, PTR_ERR(file));
37291 +               goto out_dput;
37292 +       }
37293 +
37294 +       delegated = NULL;
37295 +       err = vfsub_unlink(dir, &file->f_path, &delegated, /*force*/0);
37296 +       au_xino_unlock_dir(&ldir);
37297 +       do_unlock = 0;
37298 +       if (unlikely(err == -EWOULDBLOCK)) {
37299 +               pr_warn("cannot retry for NFSv4 delegation"
37300 +                       " for an internal unlink\n");
37301 +               iput(delegated);
37302 +       }
37303 +       if (unlikely(err)) {
37304 +               pr_err("%pd unlink err %d\n", dentry, err);
37305 +               goto out_fput;
37306 +       }
37307 +
37308 +       if (copy_src) {
37309 +               /* no one can touch copy_src xino */
37310 +               err = au_copy_file(file, copy_src, vfsub_f_size_read(copy_src));
37311 +               if (unlikely(err)) {
37312 +                       pr_err("%pd copy err %d\n", dentry, err);
37313 +                       goto out_fput;
37314 +               }
37315 +       }
37316 +       goto out_dput; /* success */
37317 +
37318 +out_fput:
37319 +       fput(file);
37320 +       file = ERR_PTR(err);
37321 +out_dput:
37322 +       dput(path.dentry);
37323 +out:
37324 +       if (do_unlock)
37325 +               au_xino_unlock_dir(&ldir);
37326 +       return file;
37327 +}
37328 +
37329 +struct file *au_xino_file1(struct au_xino *xi)
37330 +{
37331 +       struct file *file;
37332 +       unsigned int u, nfile;
37333 +
37334 +       file = NULL;
37335 +       nfile = xi->xi_nfile;
37336 +       for (u = 0; u < nfile; u++) {
37337 +               file = xi->xi_file[u];
37338 +               if (file)
37339 +                       break;
37340 +       }
37341 +
37342 +       return file;
37343 +}
37344 +
37345 +static int au_xino_file_set(struct au_xino *xi, int idx, struct file *file)
37346 +{
37347 +       int err;
37348 +       struct file *f;
37349 +       void *p;
37350 +
37351 +       if (file)
37352 +               get_file(file);
37353 +
37354 +       err = 0;
37355 +       f = NULL;
37356 +       if (idx < xi->xi_nfile) {
37357 +               f = xi->xi_file[idx];
37358 +               if (f)
37359 +                       fput(f);
37360 +       } else {
37361 +               p = au_kzrealloc(xi->xi_file,
37362 +                                sizeof(*xi->xi_file) * xi->xi_nfile,
37363 +                                sizeof(*xi->xi_file) * (idx + 1),
37364 +                                GFP_NOFS, /*may_shrink*/0);
37365 +               if (p) {
37366 +                       MtxMustLock(&xi->xi_mtx);
37367 +                       xi->xi_file = p;
37368 +                       xi->xi_nfile = idx + 1;
37369 +               } else {
37370 +                       err = -ENOMEM;
37371 +                       if (file)
37372 +                               fput(file);
37373 +                       goto out;
37374 +               }
37375 +       }
37376 +       xi->xi_file[idx] = file;
37377 +
37378 +out:
37379 +       return err;
37380 +}
37381 +
37382 +/*
37383 + * if @xinew->xi is not set, then create new xigen file.
37384 + */
37385 +struct file *au_xi_new(struct super_block *sb, struct au_xi_new *xinew)
37386 +{
37387 +       struct file *file;
37388 +       int err;
37389 +
37390 +       SiMustAnyLock(sb);
37391 +
37392 +       file = au_xino_create2(sb, xinew->base, xinew->copy_src);
37393 +       if (IS_ERR(file)) {
37394 +               err = PTR_ERR(file);
37395 +               pr_err("%s[%d], err %d\n",
37396 +                      xinew->xi ? "xino" : "xigen",
37397 +                      xinew->idx, err);
37398 +               goto out;
37399 +       }
37400 +
37401 +       if (xinew->xi)
37402 +               err = au_xino_file_set(xinew->xi, xinew->idx, file);
37403 +       else {
37404 +               BUG();
37405 +               /* todo: make xigen file an array */
37406 +               /* err = au_xigen_file_set(sb, xinew->idx, file); */
37407 +       }
37408 +       fput(file);
37409 +       if (unlikely(err))
37410 +               file = ERR_PTR(err);
37411 +
37412 +out:
37413 +       return file;
37414 +}
37415 +
37416 +/* ---------------------------------------------------------------------- */
37417 +
37418 +/*
37419 + * truncate xino files
37420 + */
37421 +static int au_xino_do_trunc(struct super_block *sb, aufs_bindex_t bindex,
37422 +                           int idx, struct kstatfs *st)
37423 +{
37424 +       int err;
37425 +       blkcnt_t blocks;
37426 +       struct file *file, *new_xino;
37427 +       struct au_xi_new xinew = {
37428 +               .idx = idx
37429 +       };
37430 +
37431 +       err = 0;
37432 +       xinew.xi = au_sbr(sb, bindex)->br_xino;
37433 +       file = au_xino_file(xinew.xi, idx);
37434 +       if (!file)
37435 +               goto out;
37436 +
37437 +       xinew.base = &file->f_path;
37438 +       err = vfs_statfs(xinew.base, st);
37439 +       if (unlikely(err)) {
37440 +               AuErr1("statfs err %d, ignored\n", err);
37441 +               err = 0;
37442 +               goto out;
37443 +       }
37444 +
37445 +       blocks = file_inode(file)->i_blocks;
37446 +       pr_info("begin truncating xino(b%d-%d), ib%llu, %llu/%llu free blks\n",
37447 +               bindex, idx, (u64)blocks, st->f_bfree, st->f_blocks);
37448 +
37449 +       xinew.copy_src = file;
37450 +       new_xino = au_xi_new(sb, &xinew);
37451 +       if (IS_ERR(new_xino)) {
37452 +               err = PTR_ERR(new_xino);
37453 +               pr_err("xino(b%d-%d), err %d, ignored\n", bindex, idx, err);
37454 +               goto out;
37455 +       }
37456 +
37457 +       err = vfs_statfs(&new_xino->f_path, st);
37458 +       if (!err)
37459 +               pr_info("end truncating xino(b%d-%d), ib%llu, %llu/%llu free blks\n",
37460 +                       bindex, idx, (u64)file_inode(new_xino)->i_blocks,
37461 +                       st->f_bfree, st->f_blocks);
37462 +       else {
37463 +               AuErr1("statfs err %d, ignored\n", err);
37464 +               err = 0;
37465 +       }
37466 +
37467 +out:
37468 +       return err;
37469 +}
37470 +
37471 +int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex, int idx_begin)
37472 +{
37473 +       int err, i;
37474 +       unsigned long jiffy;
37475 +       aufs_bindex_t bbot;
37476 +       struct kstatfs *st;
37477 +       struct au_branch *br;
37478 +       struct au_xino *xi;
37479 +
37480 +       err = -ENOMEM;
37481 +       st = kmalloc(sizeof(*st), GFP_NOFS);
37482 +       if (unlikely(!st))
37483 +               goto out;
37484 +
37485 +       err = -EINVAL;
37486 +       bbot = au_sbbot(sb);
37487 +       if (unlikely(bindex < 0 || bbot < bindex))
37488 +               goto out_st;
37489 +
37490 +       err = 0;
37491 +       jiffy = jiffies;
37492 +       br = au_sbr(sb, bindex);
37493 +       xi = br->br_xino;
37494 +       for (i = idx_begin; !err && i < xi->xi_nfile; i++)
37495 +               err = au_xino_do_trunc(sb, bindex, i, st);
37496 +       if (!err)
37497 +               au_sbi(sb)->si_xino_jiffy = jiffy;
37498 +
37499 +out_st:
37500 +       au_kfree_rcu(st);
37501 +out:
37502 +       return err;
37503 +}
37504 +
37505 +struct xino_do_trunc_args {
37506 +       struct super_block *sb;
37507 +       struct au_branch *br;
37508 +       int idx;
37509 +};
37510 +
37511 +static void xino_do_trunc(void *_args)
37512 +{
37513 +       struct xino_do_trunc_args *args = _args;
37514 +       struct super_block *sb;
37515 +       struct au_branch *br;
37516 +       struct inode *dir;
37517 +       int err, idx;
37518 +       aufs_bindex_t bindex;
37519 +
37520 +       err = 0;
37521 +       sb = args->sb;
37522 +       dir = d_inode(sb->s_root);
37523 +       br = args->br;
37524 +       idx = args->idx;
37525 +
37526 +       si_noflush_write_lock(sb);
37527 +       ii_read_lock_parent(dir);
37528 +       bindex = au_br_index(sb, br->br_id);
37529 +       err = au_xino_trunc(sb, bindex, idx);
37530 +       ii_read_unlock(dir);
37531 +       if (unlikely(err))
37532 +               pr_warn("err b%d, (%d)\n", bindex, err);
37533 +       atomic_dec(&br->br_xino->xi_truncating);
37534 +       au_lcnt_dec(&br->br_count);
37535 +       si_write_unlock(sb);
37536 +       au_nwt_done(&au_sbi(sb)->si_nowait);
37537 +       au_kfree_rcu(args);
37538 +}
37539 +
37540 +/*
37541 + * returns the index in the xi_file array whose corresponding file is necessary
37542 + * to truncate, or -1 which means no need to truncate.
37543 + */
37544 +static int xino_trunc_test(struct super_block *sb, struct au_branch *br)
37545 +{
37546 +       int err;
37547 +       unsigned int u;
37548 +       struct kstatfs st;
37549 +       struct au_sbinfo *sbinfo;
37550 +       struct au_xino *xi;
37551 +       struct file *file;
37552 +
37553 +       /* todo: si_xino_expire and the ratio should be customizable */
37554 +       sbinfo = au_sbi(sb);
37555 +       if (time_before(jiffies,
37556 +                       sbinfo->si_xino_jiffy + sbinfo->si_xino_expire))
37557 +               return -1;
37558 +
37559 +       /* truncation border */
37560 +       xi = br->br_xino;
37561 +       for (u = 0; u < xi->xi_nfile; u++) {
37562 +               file = au_xino_file(xi, u);
37563 +               if (!file)
37564 +                       continue;
37565 +
37566 +               err = vfs_statfs(&file->f_path, &st);
37567 +               if (unlikely(err)) {
37568 +                       AuErr1("statfs err %d, ignored\n", err);
37569 +                       return -1;
37570 +               }
37571 +               if (div64_u64(st.f_bfree * 100, st.f_blocks)
37572 +                   >= AUFS_XINO_DEF_TRUNC)
37573 +                       return u;
37574 +       }
37575 +
37576 +       return -1;
37577 +}
37578 +
37579 +static void xino_try_trunc(struct super_block *sb, struct au_branch *br)
37580 +{
37581 +       int idx;
37582 +       struct xino_do_trunc_args *args;
37583 +       int wkq_err;
37584 +
37585 +       idx = xino_trunc_test(sb, br);
37586 +       if (idx < 0)
37587 +               return;
37588 +
37589 +       if (atomic_inc_return(&br->br_xino->xi_truncating) > 1)
37590 +               goto out;
37591 +
37592 +       /* lock and kfree() will be called in trunc_xino() */
37593 +       args = kmalloc(sizeof(*args), GFP_NOFS);
37594 +       if (unlikely(!args)) {
37595 +               AuErr1("no memory\n");
37596 +               goto out;
37597 +       }
37598 +
37599 +       au_lcnt_inc(&br->br_count);
37600 +       args->sb = sb;
37601 +       args->br = br;
37602 +       args->idx = idx;
37603 +       wkq_err = au_wkq_nowait(xino_do_trunc, args, sb, /*flags*/0);
37604 +       if (!wkq_err)
37605 +               return; /* success */
37606 +
37607 +       pr_err("wkq %d\n", wkq_err);
37608 +       au_lcnt_dec(&br->br_count);
37609 +       au_kfree_rcu(args);
37610 +
37611 +out:
37612 +       atomic_dec(&br->br_xino->xi_truncating);
37613 +}
37614 +
37615 +/* ---------------------------------------------------------------------- */
37616 +
37617 +struct au_xi_calc {
37618 +       int idx;
37619 +       loff_t pos;
37620 +};
37621 +
37622 +static void au_xi_calc(struct super_block *sb, ino_t h_ino,
37623 +                      struct au_xi_calc *calc)
37624 +{
37625 +       loff_t maxent;
37626 +
37627 +       maxent = au_xi_maxent(sb);
37628 +       calc->idx = div64_u64_rem(h_ino, maxent, &calc->pos);
37629 +       calc->pos *= sizeof(ino_t);
37630 +}
37631 +
37632 +static int au_xino_do_new_async(struct super_block *sb, struct au_branch *br,
37633 +                               struct au_xi_calc *calc)
37634 +{
37635 +       int err;
37636 +       struct file *file;
37637 +       struct au_xino *xi = br->br_xino;
37638 +       struct au_xi_new xinew = {
37639 +               .xi = xi
37640 +       };
37641 +
37642 +       SiMustAnyLock(sb);
37643 +
37644 +       err = 0;
37645 +       if (!xi)
37646 +               goto out;
37647 +
37648 +       mutex_lock(&xi->xi_mtx);
37649 +       file = au_xino_file(xi, calc->idx);
37650 +       if (file)
37651 +               goto out_mtx;
37652 +
37653 +       file = au_xino_file(xi, /*idx*/-1);
37654 +       AuDebugOn(!file);
37655 +       xinew.idx = calc->idx;
37656 +       xinew.base = &file->f_path;
37657 +       /* xinew.copy_src = NULL; */
37658 +       file = au_xi_new(sb, &xinew);
37659 +       if (IS_ERR(file))
37660 +               err = PTR_ERR(file);
37661 +
37662 +out_mtx:
37663 +       mutex_unlock(&xi->xi_mtx);
37664 +out:
37665 +       return err;
37666 +}
37667 +
37668 +struct au_xino_do_new_async_args {
37669 +       struct super_block *sb;
37670 +       struct au_branch *br;
37671 +       struct au_xi_calc calc;
37672 +       ino_t ino;
37673 +};
37674 +
37675 +struct au_xi_writing {
37676 +       struct hlist_bl_node node;
37677 +       ino_t h_ino, ino;
37678 +};
37679 +
37680 +static int au_xino_do_write(struct file *file, struct au_xi_calc *calc,
37681 +                           ino_t ino);
37682 +
37683 +static void au_xino_call_do_new_async(void *args)
37684 +{
37685 +       struct au_xino_do_new_async_args *a = args;
37686 +       struct au_branch *br;
37687 +       struct super_block *sb;
37688 +       struct au_sbinfo *sbi;
37689 +       struct inode *root;
37690 +       struct file *file;
37691 +       struct au_xi_writing *del, *p;
37692 +       struct hlist_bl_head *hbl;
37693 +       struct hlist_bl_node *pos;
37694 +       int err;
37695 +
37696 +       br = a->br;
37697 +       sb = a->sb;
37698 +       sbi = au_sbi(sb);
37699 +       si_noflush_read_lock(sb);
37700 +       root = d_inode(sb->s_root);
37701 +       ii_read_lock_child(root);
37702 +       err = au_xino_do_new_async(sb, br, &a->calc);
37703 +       if (unlikely(err)) {
37704 +               AuIOErr("err %d\n", err);
37705 +               goto out;
37706 +       }
37707 +
37708 +       file = au_xino_file(br->br_xino, a->calc.idx);
37709 +       AuDebugOn(!file);
37710 +       err = au_xino_do_write(file, &a->calc, a->ino);
37711 +       if (unlikely(err)) {
37712 +               AuIOErr("err %d\n", err);
37713 +               goto out;
37714 +       }
37715 +
37716 +       del = NULL;
37717 +       hbl = &br->br_xino->xi_writing;
37718 +       hlist_bl_lock(hbl);
37719 +       au_hbl_for_each(pos, hbl) {
37720 +               p = container_of(pos, struct au_xi_writing, node);
37721 +               if (p->ino == a->ino) {
37722 +                       del = p;
37723 +                       hlist_bl_del(&p->node);
37724 +                       break;
37725 +               }
37726 +       }
37727 +       hlist_bl_unlock(hbl);
37728 +       au_kfree_rcu(del);
37729 +
37730 +out:
37731 +       au_lcnt_dec(&br->br_count);
37732 +       ii_read_unlock(root);
37733 +       si_read_unlock(sb);
37734 +       au_nwt_done(&sbi->si_nowait);
37735 +       au_kfree_rcu(a);
37736 +}
37737 +
37738 +/*
37739 + * create a new xino file asynchronously
37740 + */
37741 +static int au_xino_new_async(struct super_block *sb, struct au_branch *br,
37742 +                            struct au_xi_calc *calc, ino_t ino)
37743 +{
37744 +       int err;
37745 +       struct au_xino_do_new_async_args *arg;
37746 +
37747 +       err = -ENOMEM;
37748 +       arg = kmalloc(sizeof(*arg), GFP_NOFS);
37749 +       if (unlikely(!arg))
37750 +               goto out;
37751 +
37752 +       arg->sb = sb;
37753 +       arg->br = br;
37754 +       arg->calc = *calc;
37755 +       arg->ino = ino;
37756 +       au_lcnt_inc(&br->br_count);
37757 +       err = au_wkq_nowait(au_xino_call_do_new_async, arg, sb, AuWkq_NEST);
37758 +       if (unlikely(err)) {
37759 +               pr_err("wkq %d\n", err);
37760 +               au_lcnt_dec(&br->br_count);
37761 +               au_kfree_rcu(arg);
37762 +       }
37763 +
37764 +out:
37765 +       return err;
37766 +}
37767 +
37768 +/*
37769 + * read @ino from xinofile for the specified branch{@sb, @bindex}
37770 + * at the position of @h_ino.
37771 + */
37772 +int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
37773 +                ino_t *ino)
37774 +{
37775 +       int err;
37776 +       ssize_t sz;
37777 +       struct au_xi_calc calc;
37778 +       struct au_sbinfo *sbinfo;
37779 +       struct file *file;
37780 +       struct au_xino *xi;
37781 +       struct hlist_bl_head *hbl;
37782 +       struct hlist_bl_node *pos;
37783 +       struct au_xi_writing *p;
37784 +
37785 +       *ino = 0;
37786 +       if (!au_opt_test(au_mntflags(sb), XINO))
37787 +               return 0; /* no xino */
37788 +
37789 +       err = 0;
37790 +       au_xi_calc(sb, h_ino, &calc);
37791 +       xi = au_sbr(sb, bindex)->br_xino;
37792 +       file = au_xino_file(xi, calc.idx);
37793 +       if (!file) {
37794 +               hbl = &xi->xi_writing;
37795 +               hlist_bl_lock(hbl);
37796 +               au_hbl_for_each(pos, hbl) {
37797 +                       p = container_of(pos, struct au_xi_writing, node);
37798 +                       if (p->h_ino == h_ino) {
37799 +                               AuDbg("hi%llu, i%llu, found\n",
37800 +                                     (u64)p->h_ino, (u64)p->ino);
37801 +                               *ino = p->ino;
37802 +                               break;
37803 +                       }
37804 +               }
37805 +               hlist_bl_unlock(hbl);
37806 +               return 0;
37807 +       } else if (vfsub_f_size_read(file) < calc.pos + sizeof(*ino))
37808 +               return 0; /* no xino */
37809 +
37810 +       sbinfo = au_sbi(sb);
37811 +       sz = xino_fread(file, ino, sizeof(*ino), &calc.pos);
37812 +       if (sz == sizeof(*ino))
37813 +               return 0; /* success */
37814 +
37815 +       err = sz;
37816 +       if (unlikely(sz >= 0)) {
37817 +               err = -EIO;
37818 +               AuIOErr("xino read error (%zd)\n", sz);
37819 +       }
37820 +       return err;
37821 +}
37822 +
37823 +static int au_xino_do_write(struct file *file, struct au_xi_calc *calc,
37824 +                           ino_t ino)
37825 +{
37826 +       ssize_t sz;
37827 +
37828 +       sz = xino_fwrite(file, &ino, sizeof(ino), &calc->pos);
37829 +       if (sz == sizeof(ino))
37830 +               return 0; /* success */
37831 +
37832 +       AuIOErr("write failed (%zd)\n", sz);
37833 +       return -EIO;
37834 +}
37835 +
37836 +/*
37837 + * write @ino to the xinofile for the specified branch{@sb, @bindex}
37838 + * at the position of @h_ino.
37839 + * even if @ino is zero, it is written to the xinofile and means no entry.
37840 + * if the size of the xino file on a specific filesystem exceeds the watermark,
37841 + * try truncating it.
37842 + */
37843 +int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
37844 +                 ino_t ino)
37845 +{
37846 +       int err;
37847 +       unsigned int mnt_flags;
37848 +       struct au_xi_calc calc;
37849 +       struct file *file;
37850 +       struct au_branch *br;
37851 +       struct au_xino *xi;
37852 +       struct au_xi_writing *p;
37853 +
37854 +       SiMustAnyLock(sb);
37855 +
37856 +       mnt_flags = au_mntflags(sb);
37857 +       if (!au_opt_test(mnt_flags, XINO))
37858 +               return 0;
37859 +
37860 +       au_xi_calc(sb, h_ino, &calc);
37861 +       br = au_sbr(sb, bindex);
37862 +       xi = br->br_xino;
37863 +       file = au_xino_file(xi, calc.idx);
37864 +       if (!file) {
37865 +               /* store the inum pair into the list */
37866 +               p = kmalloc(sizeof(*p), GFP_NOFS | __GFP_NOFAIL);
37867 +               p->h_ino = h_ino;
37868 +               p->ino = ino;
37869 +               au_hbl_add(&p->node, &xi->xi_writing);
37870 +
37871 +               /* create and write a new xino file asynchronously */
37872 +               err = au_xino_new_async(sb, br, &calc, ino);
37873 +               if (!err)
37874 +                       return 0; /* success */
37875 +               goto out;
37876 +       }
37877 +
37878 +       err = au_xino_do_write(file, &calc, ino);
37879 +       if (!err) {
37880 +               br = au_sbr(sb, bindex);
37881 +               if (au_opt_test(mnt_flags, TRUNC_XINO)
37882 +                   && au_test_fs_trunc_xino(au_br_sb(br)))
37883 +                       xino_try_trunc(sb, br);
37884 +               return 0; /* success */
37885 +       }
37886 +
37887 +out:
37888 +       AuIOErr("write failed (%d)\n", err);
37889 +       return -EIO;
37890 +}
37891 +
37892 +static ssize_t xino_fread_wkq(struct file *file, void *buf, size_t size,
37893 +                             loff_t *pos);
37894 +
37895 +/* todo: unnecessary to support mmap_sem since kernel-space? */
37896 +ssize_t xino_fread(struct file *file, void *kbuf, size_t size, loff_t *pos)
37897 +{
37898 +       ssize_t err;
37899 +       int i;
37900 +       const int prevent_endless = 10;
37901 +
37902 +       i = 0;
37903 +       do {
37904 +               err = vfsub_read_k(file, kbuf, size, pos);
37905 +               if (err == -EINTR
37906 +                   && !au_wkq_test()
37907 +                   && fatal_signal_pending(current)) {
37908 +                       err = xino_fread_wkq(file, kbuf, size, pos);
37909 +                       BUG_ON(err == -EINTR);
37910 +               }
37911 +       } while (i++ < prevent_endless
37912 +                && (err == -EAGAIN || err == -EINTR));
37913 +
37914 +#if 0 /* reserved for future use */
37915 +       if (err > 0)
37916 +               fsnotify_access(file->f_path.dentry);
37917 +#endif
37918 +
37919 +       return err;
37920 +}
37921 +
37922 +struct xino_fread_args {
37923 +       ssize_t *errp;
37924 +       struct file *file;
37925 +       void *buf;
37926 +       size_t size;
37927 +       loff_t *pos;
37928 +};
37929 +
37930 +static void call_xino_fread(void *args)
37931 +{
37932 +       struct xino_fread_args *a = args;
37933 +       *a->errp = xino_fread(a->file, a->buf, a->size, a->pos);
37934 +}
37935 +
37936 +static ssize_t xino_fread_wkq(struct file *file, void *buf, size_t size,
37937 +                             loff_t *pos)
37938 +{
37939 +       ssize_t err;
37940 +       int wkq_err;
37941 +       struct xino_fread_args args = {
37942 +               .errp   = &err,
37943 +               .file   = file,
37944 +               .buf    = buf,
37945 +               .size   = size,
37946 +               .pos    = pos
37947 +       };
37948 +
37949 +       wkq_err = au_wkq_wait(call_xino_fread, &args);
37950 +       if (unlikely(wkq_err))
37951 +               err = wkq_err;
37952 +
37953 +       return err;
37954 +}
37955 +
37956 +static ssize_t xino_fwrite_wkq(struct file *file, void *buf, size_t size,
37957 +                              loff_t *pos);
37958 +
37959 +static ssize_t do_xino_fwrite(struct file *file, void *kbuf, size_t size,
37960 +                             loff_t *pos)
37961 +{
37962 +       ssize_t err;
37963 +       int i;
37964 +       const int prevent_endless = 10;
37965 +
37966 +       i = 0;
37967 +       do {
37968 +               err = vfsub_write_k(file, kbuf, size, pos);
37969 +               if (err == -EINTR
37970 +                   && !au_wkq_test()
37971 +                   && fatal_signal_pending(current)) {
37972 +                       err = xino_fwrite_wkq(file, kbuf, size, pos);
37973 +                       BUG_ON(err == -EINTR);
37974 +               }
37975 +       } while (i++ < prevent_endless
37976 +                && (err == -EAGAIN || err == -EINTR));
37977 +
37978 +#if 0 /* reserved for future use */
37979 +       if (err > 0)
37980 +               fsnotify_modify(file->f_path.dentry);
37981 +#endif
37982 +
37983 +       return err;
37984 +}
37985 +
37986 +struct do_xino_fwrite_args {
37987 +       ssize_t *errp;
37988 +       struct file *file;
37989 +       void *buf;
37990 +       size_t size;
37991 +       loff_t *pos;
37992 +};
37993 +
37994 +static void call_do_xino_fwrite(void *args)
37995 +{
37996 +       struct do_xino_fwrite_args *a = args;
37997 +       *a->errp = do_xino_fwrite(a->file, a->buf, a->size, a->pos);
37998 +}
37999 +
38000 +static ssize_t xino_fwrite_wkq(struct file *file, void *buf, size_t size,
38001 +                              loff_t *pos)
38002 +{
38003 +       ssize_t err;
38004 +       int wkq_err;
38005 +       struct do_xino_fwrite_args args = {
38006 +               .errp   = &err,
38007 +               .file   = file,
38008 +               .buf    = buf,
38009 +               .size   = size,
38010 +               .pos    = pos
38011 +       };
38012 +
38013 +       /*
38014 +        * it breaks RLIMIT_FSIZE and normal user's limit,
38015 +        * users should care about quota and real 'filesystem full.'
38016 +        */
38017 +       wkq_err = au_wkq_wait(call_do_xino_fwrite, &args);
38018 +       if (unlikely(wkq_err))
38019 +               err = wkq_err;
38020 +
38021 +       return err;
38022 +}
38023 +
38024 +ssize_t xino_fwrite(struct file *file, void *buf, size_t size, loff_t *pos)
38025 +{
38026 +       ssize_t err;
38027 +
38028 +       if (rlimit(RLIMIT_FSIZE) == RLIM_INFINITY) {
38029 +               lockdep_off();
38030 +               err = do_xino_fwrite(file, buf, size, pos);
38031 +               lockdep_on();
38032 +       } else {
38033 +               lockdep_off();
38034 +               err = xino_fwrite_wkq(file, buf, size, pos);
38035 +               lockdep_on();
38036 +       }
38037 +
38038 +       return err;
38039 +}
38040 +
38041 +/* ---------------------------------------------------------------------- */
38042 +
38043 +/*
38044 + * inode number bitmap
38045 + */
38046 +static const int page_bits = (int)PAGE_SIZE * BITS_PER_BYTE;
38047 +static ino_t xib_calc_ino(unsigned long pindex, int bit)
38048 +{
38049 +       ino_t ino;
38050 +
38051 +       AuDebugOn(bit < 0 || page_bits <= bit);
38052 +       ino = AUFS_FIRST_INO + pindex * page_bits + bit;
38053 +       return ino;
38054 +}
38055 +
38056 +static void xib_calc_bit(ino_t ino, unsigned long *pindex, int *bit)
38057 +{
38058 +       AuDebugOn(ino < AUFS_FIRST_INO);
38059 +       ino -= AUFS_FIRST_INO;
38060 +       *pindex = ino / page_bits;
38061 +       *bit = ino % page_bits;
38062 +}
38063 +
38064 +static int xib_pindex(struct super_block *sb, unsigned long pindex)
38065 +{
38066 +       int err;
38067 +       loff_t pos;
38068 +       ssize_t sz;
38069 +       struct au_sbinfo *sbinfo;
38070 +       struct file *xib;
38071 +       unsigned long *p;
38072 +
38073 +       sbinfo = au_sbi(sb);
38074 +       MtxMustLock(&sbinfo->si_xib_mtx);
38075 +       AuDebugOn(pindex > ULONG_MAX / PAGE_SIZE
38076 +                 || !au_opt_test(sbinfo->si_mntflags, XINO));
38077 +
38078 +       if (pindex == sbinfo->si_xib_last_pindex)
38079 +               return 0;
38080 +
38081 +       xib = sbinfo->si_xib;
38082 +       p = sbinfo->si_xib_buf;
38083 +       pos = sbinfo->si_xib_last_pindex;
38084 +       pos *= PAGE_SIZE;
38085 +       sz = xino_fwrite(xib, p, PAGE_SIZE, &pos);
38086 +       if (unlikely(sz != PAGE_SIZE))
38087 +               goto out;
38088 +
38089 +       pos = pindex;
38090 +       pos *= PAGE_SIZE;
38091 +       if (vfsub_f_size_read(xib) >= pos + PAGE_SIZE)
38092 +               sz = xino_fread(xib, p, PAGE_SIZE, &pos);
38093 +       else {
38094 +               memset(p, 0, PAGE_SIZE);
38095 +               sz = xino_fwrite(xib, p, PAGE_SIZE, &pos);
38096 +       }
38097 +       if (sz == PAGE_SIZE) {
38098 +               sbinfo->si_xib_last_pindex = pindex;
38099 +               return 0; /* success */
38100 +       }
38101 +
38102 +out:
38103 +       AuIOErr1("write failed (%zd)\n", sz);
38104 +       err = sz;
38105 +       if (sz >= 0)
38106 +               err = -EIO;
38107 +       return err;
38108 +}
38109 +
38110 +static void au_xib_clear_bit(struct inode *inode)
38111 +{
38112 +       int err, bit;
38113 +       unsigned long pindex;
38114 +       struct super_block *sb;
38115 +       struct au_sbinfo *sbinfo;
38116 +
38117 +       AuDebugOn(inode->i_nlink);
38118 +
38119 +       sb = inode->i_sb;
38120 +       xib_calc_bit(inode->i_ino, &pindex, &bit);
38121 +       AuDebugOn(page_bits <= bit);
38122 +       sbinfo = au_sbi(sb);
38123 +       mutex_lock(&sbinfo->si_xib_mtx);
38124 +       err = xib_pindex(sb, pindex);
38125 +       if (!err) {
38126 +               clear_bit(bit, sbinfo->si_xib_buf);
38127 +               sbinfo->si_xib_next_bit = bit;
38128 +       }
38129 +       mutex_unlock(&sbinfo->si_xib_mtx);
38130 +}
38131 +
38132 +/* ---------------------------------------------------------------------- */
38133 +
38134 +/*
38135 + * truncate a xino bitmap file
38136 + */
38137 +
38138 +/* todo: slow */
38139 +static int do_xib_restore(struct super_block *sb, struct file *file, void *page)
38140 +{
38141 +       int err, bit;
38142 +       ssize_t sz;
38143 +       unsigned long pindex;
38144 +       loff_t pos, pend;
38145 +       struct au_sbinfo *sbinfo;
38146 +       ino_t *ino;
38147 +       unsigned long *p;
38148 +
38149 +       err = 0;
38150 +       sbinfo = au_sbi(sb);
38151 +       MtxMustLock(&sbinfo->si_xib_mtx);
38152 +       p = sbinfo->si_xib_buf;
38153 +       pend = vfsub_f_size_read(file);
38154 +       pos = 0;
38155 +       while (pos < pend) {
38156 +               sz = xino_fread(file, page, PAGE_SIZE, &pos);
38157 +               err = sz;
38158 +               if (unlikely(sz <= 0))
38159 +                       goto out;
38160 +
38161 +               err = 0;
38162 +               for (ino = page; sz > 0; ino++, sz -= sizeof(ino)) {
38163 +                       if (unlikely(*ino < AUFS_FIRST_INO))
38164 +                               continue;
38165 +
38166 +                       xib_calc_bit(*ino, &pindex, &bit);
38167 +                       AuDebugOn(page_bits <= bit);
38168 +                       err = xib_pindex(sb, pindex);
38169 +                       if (!err)
38170 +                               set_bit(bit, p);
38171 +                       else
38172 +                               goto out;
38173 +               }
38174 +       }
38175 +
38176 +out:
38177 +       return err;
38178 +}
38179 +
38180 +static int xib_restore(struct super_block *sb)
38181 +{
38182 +       int err, i;
38183 +       unsigned int nfile;
38184 +       aufs_bindex_t bindex, bbot;
38185 +       void *page;
38186 +       struct au_branch *br;
38187 +       struct au_xino *xi;
38188 +       struct file *file;
38189 +
38190 +       err = -ENOMEM;
38191 +       page = (void *)__get_free_page(GFP_NOFS);
38192 +       if (unlikely(!page))
38193 +               goto out;
38194 +
38195 +       err = 0;
38196 +       bbot = au_sbbot(sb);
38197 +       for (bindex = 0; !err && bindex <= bbot; bindex++)
38198 +               if (!bindex || is_sb_shared(sb, bindex, bindex - 1) < 0) {
38199 +                       br = au_sbr(sb, bindex);
38200 +                       xi = br->br_xino;
38201 +                       nfile = xi->xi_nfile;
38202 +                       for (i = 0; i < nfile; i++) {
38203 +                               file = au_xino_file(xi, i);
38204 +                               if (file)
38205 +                                       err = do_xib_restore(sb, file, page);
38206 +                       }
38207 +               } else
38208 +                       AuDbg("skip shared b%d\n", bindex);
38209 +       free_page((unsigned long)page);
38210 +
38211 +out:
38212 +       return err;
38213 +}
38214 +
38215 +int au_xib_trunc(struct super_block *sb)
38216 +{
38217 +       int err;
38218 +       ssize_t sz;
38219 +       loff_t pos;
38220 +       struct au_sbinfo *sbinfo;
38221 +       unsigned long *p;
38222 +       struct file *file;
38223 +
38224 +       SiMustWriteLock(sb);
38225 +
38226 +       err = 0;
38227 +       sbinfo = au_sbi(sb);
38228 +       if (!au_opt_test(sbinfo->si_mntflags, XINO))
38229 +               goto out;
38230 +
38231 +       file = sbinfo->si_xib;
38232 +       if (vfsub_f_size_read(file) <= PAGE_SIZE)
38233 +               goto out;
38234 +
38235 +       file = au_xino_create2(sb, &sbinfo->si_xib->f_path, NULL);
38236 +       err = PTR_ERR(file);
38237 +       if (IS_ERR(file))
38238 +               goto out;
38239 +       fput(sbinfo->si_xib);
38240 +       sbinfo->si_xib = file;
38241 +
38242 +       p = sbinfo->si_xib_buf;
38243 +       memset(p, 0, PAGE_SIZE);
38244 +       pos = 0;
38245 +       sz = xino_fwrite(sbinfo->si_xib, p, PAGE_SIZE, &pos);
38246 +       if (unlikely(sz != PAGE_SIZE)) {
38247 +               err = sz;
38248 +               AuIOErr("err %d\n", err);
38249 +               if (sz >= 0)
38250 +                       err = -EIO;
38251 +               goto out;
38252 +       }
38253 +
38254 +       mutex_lock(&sbinfo->si_xib_mtx);
38255 +       /* mnt_want_write() is unnecessary here */
38256 +       err = xib_restore(sb);
38257 +       mutex_unlock(&sbinfo->si_xib_mtx);
38258 +
38259 +out:
38260 +       return err;
38261 +}
38262 +
38263 +/* ---------------------------------------------------------------------- */
38264 +
38265 +struct au_xino *au_xino_alloc(unsigned int nfile)
38266 +{
38267 +       struct au_xino *xi;
38268 +
38269 +       xi = kzalloc(sizeof(*xi), GFP_NOFS);
38270 +       if (unlikely(!xi))
38271 +               goto out;
38272 +       xi->xi_nfile = nfile;
38273 +       xi->xi_file = kcalloc(nfile, sizeof(*xi->xi_file), GFP_NOFS);
38274 +       if (unlikely(!xi->xi_file))
38275 +               goto out_free;
38276 +
38277 +       xi->xi_nondir.total = 8; /* initial size */
38278 +       xi->xi_nondir.array = kcalloc(xi->xi_nondir.total, sizeof(ino_t),
38279 +                                     GFP_NOFS);
38280 +       if (unlikely(!xi->xi_nondir.array))
38281 +               goto out_file;
38282 +
38283 +       spin_lock_init(&xi->xi_nondir.spin);
38284 +       init_waitqueue_head(&xi->xi_nondir.wqh);
38285 +       mutex_init(&xi->xi_mtx);
38286 +       INIT_HLIST_BL_HEAD(&xi->xi_writing);
38287 +       atomic_set(&xi->xi_truncating, 0);
38288 +       kref_init(&xi->xi_kref);
38289 +       goto out; /* success */
38290 +
38291 +out_file:
38292 +       au_kfree_try_rcu(xi->xi_file);
38293 +out_free:
38294 +       au_kfree_rcu(xi);
38295 +       xi = NULL;
38296 +out:
38297 +       return xi;
38298 +}
38299 +
38300 +static int au_xino_init(struct au_branch *br, int idx, struct file *file)
38301 +{
38302 +       int err;
38303 +       struct au_xino *xi;
38304 +
38305 +       err = 0;
38306 +       xi = au_xino_alloc(idx + 1);
38307 +       if (unlikely(!xi)) {
38308 +               err = -ENOMEM;
38309 +               goto out;
38310 +       }
38311 +
38312 +       if (file)
38313 +               get_file(file);
38314 +       xi->xi_file[idx] = file;
38315 +       AuDebugOn(br->br_xino);
38316 +       br->br_xino = xi;
38317 +
38318 +out:
38319 +       return err;
38320 +}
38321 +
38322 +static void au_xino_release(struct kref *kref)
38323 +{
38324 +       struct au_xino *xi;
38325 +       int i;
38326 +       unsigned long ul;
38327 +       struct hlist_bl_head *hbl;
38328 +       struct hlist_bl_node *pos, *n;
38329 +       struct au_xi_writing *p;
38330 +
38331 +       xi = container_of(kref, struct au_xino, xi_kref);
38332 +       for (i = 0; i < xi->xi_nfile; i++)
38333 +               if (xi->xi_file[i])
38334 +                       fput(xi->xi_file[i]);
38335 +       for (i = xi->xi_nondir.total - 1; i >= 0; i--)
38336 +               AuDebugOn(xi->xi_nondir.array[i]);
38337 +       mutex_destroy(&xi->xi_mtx);
38338 +       hbl = &xi->xi_writing;
38339 +       ul = au_hbl_count(hbl);
38340 +       if (unlikely(ul)) {
38341 +               pr_warn("xi_writing %lu\n", ul);
38342 +               hlist_bl_lock(hbl);
38343 +               hlist_bl_for_each_entry_safe(p, pos, n, hbl, node) {
38344 +                       hlist_bl_del(&p->node);
38345 +                       /* kmemleak reported au_kfree_rcu() doesn't free it */
38346 +                       kfree(p);
38347 +               }
38348 +               hlist_bl_unlock(hbl);
38349 +       }
38350 +       au_kfree_try_rcu(xi->xi_file);
38351 +       au_kfree_try_rcu(xi->xi_nondir.array);
38352 +       au_kfree_rcu(xi);
38353 +}
38354 +
38355 +int au_xino_put(struct au_branch *br)
38356 +{
38357 +       int ret;
38358 +       struct au_xino *xi;
38359 +
38360 +       ret = 0;
38361 +       xi = br->br_xino;
38362 +       if (xi) {
38363 +               br->br_xino = NULL;
38364 +               ret = kref_put(&xi->xi_kref, au_xino_release);
38365 +       }
38366 +
38367 +       return ret;
38368 +}
38369 +
38370 +/* ---------------------------------------------------------------------- */
38371 +
38372 +/*
38373 + * xino mount option handlers
38374 + */
38375 +
38376 +/* xino bitmap */
38377 +static void xino_clear_xib(struct super_block *sb)
38378 +{
38379 +       struct au_sbinfo *sbinfo;
38380 +
38381 +       SiMustWriteLock(sb);
38382 +
38383 +       sbinfo = au_sbi(sb);
38384 +       if (sbinfo->si_xib)
38385 +               fput(sbinfo->si_xib);
38386 +       sbinfo->si_xib = NULL;
38387 +       if (sbinfo->si_xib_buf)
38388 +               free_page((unsigned long)sbinfo->si_xib_buf);
38389 +       sbinfo->si_xib_buf = NULL;
38390 +}
38391 +
38392 +static int au_xino_set_xib(struct super_block *sb, struct path *path)
38393 +{
38394 +       int err;
38395 +       loff_t pos;
38396 +       struct au_sbinfo *sbinfo;
38397 +       struct file *file;
38398 +       struct super_block *xi_sb;
38399 +
38400 +       SiMustWriteLock(sb);
38401 +
38402 +       sbinfo = au_sbi(sb);
38403 +       file = au_xino_create2(sb, path, sbinfo->si_xib);
38404 +       err = PTR_ERR(file);
38405 +       if (IS_ERR(file))
38406 +               goto out;
38407 +       if (sbinfo->si_xib)
38408 +               fput(sbinfo->si_xib);
38409 +       sbinfo->si_xib = file;
38410 +       xi_sb = file_inode(file)->i_sb;
38411 +       sbinfo->si_ximaxent = xi_sb->s_maxbytes;
38412 +       if (unlikely(sbinfo->si_ximaxent < PAGE_SIZE)) {
38413 +               err = -EIO;
38414 +               pr_err("s_maxbytes(%llu) on %s is too small\n",
38415 +                      (u64)sbinfo->si_ximaxent, au_sbtype(xi_sb));
38416 +               goto out_unset;
38417 +       }
38418 +       sbinfo->si_ximaxent /= sizeof(ino_t);
38419 +
38420 +       err = -ENOMEM;
38421 +       if (!sbinfo->si_xib_buf)
38422 +               sbinfo->si_xib_buf = (void *)get_zeroed_page(GFP_NOFS);
38423 +       if (unlikely(!sbinfo->si_xib_buf))
38424 +               goto out_unset;
38425 +
38426 +       sbinfo->si_xib_last_pindex = 0;
38427 +       sbinfo->si_xib_next_bit = 0;
38428 +       if (vfsub_f_size_read(file) < PAGE_SIZE) {
38429 +               pos = 0;
38430 +               err = xino_fwrite(file, sbinfo->si_xib_buf, PAGE_SIZE, &pos);
38431 +               if (unlikely(err != PAGE_SIZE))
38432 +                       goto out_free;
38433 +       }
38434 +       err = 0;
38435 +       goto out; /* success */
38436 +
38437 +out_free:
38438 +       if (sbinfo->si_xib_buf)
38439 +               free_page((unsigned long)sbinfo->si_xib_buf);
38440 +       sbinfo->si_xib_buf = NULL;
38441 +       if (err >= 0)
38442 +               err = -EIO;
38443 +out_unset:
38444 +       fput(sbinfo->si_xib);
38445 +       sbinfo->si_xib = NULL;
38446 +out:
38447 +       AuTraceErr(err);
38448 +       return err;
38449 +}
38450 +
38451 +/* xino for each branch */
38452 +static void xino_clear_br(struct super_block *sb)
38453 +{
38454 +       aufs_bindex_t bindex, bbot;
38455 +       struct au_branch *br;
38456 +
38457 +       bbot = au_sbbot(sb);
38458 +       for (bindex = 0; bindex <= bbot; bindex++) {
38459 +               br = au_sbr(sb, bindex);
38460 +               AuDebugOn(!br);
38461 +               au_xino_put(br);
38462 +       }
38463 +}
38464 +
38465 +static void au_xino_set_br_shared(struct super_block *sb, struct au_branch *br,
38466 +                                 aufs_bindex_t bshared)
38467 +{
38468 +       struct au_branch *brshared;
38469 +
38470 +       brshared = au_sbr(sb, bshared);
38471 +       AuDebugOn(!brshared->br_xino);
38472 +       AuDebugOn(!brshared->br_xino->xi_file);
38473 +       if (br->br_xino != brshared->br_xino) {
38474 +               au_xino_get(brshared);
38475 +               au_xino_put(br);
38476 +               br->br_xino = brshared->br_xino;
38477 +       }
38478 +}
38479 +
38480 +struct au_xino_do_set_br {
38481 +       struct au_branch *br;
38482 +       ino_t h_ino;
38483 +       aufs_bindex_t bshared;
38484 +};
38485 +
38486 +static int au_xino_do_set_br(struct super_block *sb, struct path *path,
38487 +                            struct au_xino_do_set_br *args)
38488 +{
38489 +       int err;
38490 +       struct au_xi_calc calc;
38491 +       struct file *file;
38492 +       struct au_branch *br;
38493 +       struct au_xi_new xinew = {
38494 +               .base = path
38495 +       };
38496 +
38497 +       br = args->br;
38498 +       xinew.xi = br->br_xino;
38499 +       au_xi_calc(sb, args->h_ino, &calc);
38500 +       xinew.copy_src = au_xino_file(xinew.xi, calc.idx);
38501 +       if (args->bshared >= 0)
38502 +               /* shared xino */
38503 +               au_xino_set_br_shared(sb, br, args->bshared);
38504 +       else if (!xinew.xi) {
38505 +               /* new xino */
38506 +               err = au_xino_init(br, calc.idx, xinew.copy_src);
38507 +               if (unlikely(err))
38508 +                       goto out;
38509 +       }
38510 +
38511 +       /* force re-creating */
38512 +       xinew.xi = br->br_xino;
38513 +       xinew.idx = calc.idx;
38514 +       mutex_lock(&xinew.xi->xi_mtx);
38515 +       file = au_xi_new(sb, &xinew);
38516 +       mutex_unlock(&xinew.xi->xi_mtx);
38517 +       err = PTR_ERR(file);
38518 +       if (IS_ERR(file))
38519 +               goto out;
38520 +       AuDebugOn(!file);
38521 +
38522 +       err = au_xino_do_write(file, &calc, AUFS_ROOT_INO);
38523 +       if (unlikely(err))
38524 +               au_xino_put(br);
38525 +
38526 +out:
38527 +       AuTraceErr(err);
38528 +       return err;
38529 +}
38530 +
38531 +static int au_xino_set_br(struct super_block *sb, struct path *path)
38532 +{
38533 +       int err;
38534 +       aufs_bindex_t bindex, bbot;
38535 +       struct au_xino_do_set_br args;
38536 +       struct inode *inode;
38537 +
38538 +       SiMustWriteLock(sb);
38539 +
38540 +       bbot = au_sbbot(sb);
38541 +       inode = d_inode(sb->s_root);
38542 +       for (bindex = 0; bindex <= bbot; bindex++) {
38543 +               args.h_ino = au_h_iptr(inode, bindex)->i_ino;
38544 +               args.br = au_sbr(sb, bindex);
38545 +               args.bshared = is_sb_shared(sb, bindex, bindex - 1);
38546 +               err = au_xino_do_set_br(sb, path, &args);
38547 +               if (unlikely(err))
38548 +                       break;
38549 +       }
38550 +
38551 +       AuTraceErr(err);
38552 +       return err;
38553 +}
38554 +
38555 +void au_xino_clr(struct super_block *sb)
38556 +{
38557 +       struct au_sbinfo *sbinfo;
38558 +
38559 +       au_xigen_clr(sb);
38560 +       xino_clear_xib(sb);
38561 +       xino_clear_br(sb);
38562 +       dbgaufs_brs_del(sb, 0);
38563 +       sbinfo = au_sbi(sb);
38564 +       /* lvalue, do not call au_mntflags() */
38565 +       au_opt_clr(sbinfo->si_mntflags, XINO);
38566 +}
38567 +
38568 +int au_xino_set(struct super_block *sb, struct au_opt_xino *xiopt, int remount)
38569 +{
38570 +       int err, skip;
38571 +       struct dentry *dentry, *parent, *cur_dentry, *cur_parent;
38572 +       struct qstr *dname, *cur_name;
38573 +       struct file *cur_xino;
38574 +       struct au_sbinfo *sbinfo;
38575 +       struct path *path, *cur_path;
38576 +
38577 +       SiMustWriteLock(sb);
38578 +
38579 +       err = 0;
38580 +       sbinfo = au_sbi(sb);
38581 +       path = &xiopt->file->f_path;
38582 +       dentry = path->dentry;
38583 +       parent = dget_parent(dentry);
38584 +       if (remount) {
38585 +               skip = 0;
38586 +               cur_xino = sbinfo->si_xib;
38587 +               if (cur_xino) {
38588 +                       cur_path = &cur_xino->f_path;
38589 +                       cur_dentry = cur_path->dentry;
38590 +                       cur_parent = dget_parent(cur_dentry);
38591 +                       cur_name = &cur_dentry->d_name;
38592 +                       dname = &dentry->d_name;
38593 +                       skip = (cur_parent == parent
38594 +                               && au_qstreq(dname, cur_name));
38595 +                       dput(cur_parent);
38596 +               }
38597 +               if (skip)
38598 +                       goto out;
38599 +       }
38600 +
38601 +       au_opt_set(sbinfo->si_mntflags, XINO);
38602 +       err = au_xino_set_xib(sb, path);
38603 +       /* si_x{read,write} are set */
38604 +       if (!err)
38605 +               err = au_xigen_set(sb, path);
38606 +       if (!err)
38607 +               err = au_xino_set_br(sb, path);
38608 +       if (!err) {
38609 +               dbgaufs_brs_add(sb, 0, /*topdown*/1);
38610 +               goto out; /* success */
38611 +       }
38612 +
38613 +       /* reset all */
38614 +       AuIOErr("failed setting xino(%d).\n", err);
38615 +       au_xino_clr(sb);
38616 +
38617 +out:
38618 +       dput(parent);
38619 +       return err;
38620 +}
38621 +
38622 +/*
38623 + * create a xinofile at the default place/path.
38624 + */
38625 +struct file *au_xino_def(struct super_block *sb)
38626 +{
38627 +       struct file *file;
38628 +       char *page, *p;
38629 +       struct au_branch *br;
38630 +       struct super_block *h_sb;
38631 +       struct path path;
38632 +       aufs_bindex_t bbot, bindex, bwr;
38633 +
38634 +       br = NULL;
38635 +       bbot = au_sbbot(sb);
38636 +       bwr = -1;
38637 +       for (bindex = 0; bindex <= bbot; bindex++) {
38638 +               br = au_sbr(sb, bindex);
38639 +               if (au_br_writable(br->br_perm)
38640 +                   && !au_test_fs_bad_xino(au_br_sb(br))) {
38641 +                       bwr = bindex;
38642 +                       break;
38643 +               }
38644 +       }
38645 +
38646 +       if (bwr >= 0) {
38647 +               file = ERR_PTR(-ENOMEM);
38648 +               page = (void *)__get_free_page(GFP_NOFS);
38649 +               if (unlikely(!page))
38650 +                       goto out;
38651 +               path.mnt = au_br_mnt(br);
38652 +               path.dentry = au_h_dptr(sb->s_root, bwr);
38653 +               p = d_path(&path, page, PATH_MAX - sizeof(AUFS_XINO_FNAME));
38654 +               file = (void *)p;
38655 +               if (!IS_ERR(p)) {
38656 +                       strcat(p, "/" AUFS_XINO_FNAME);
38657 +                       AuDbg("%s\n", p);
38658 +                       file = au_xino_create(sb, p, /*silent*/0, /*wbrtop*/1);
38659 +               }
38660 +               free_page((unsigned long)page);
38661 +       } else {
38662 +               file = au_xino_create(sb, AUFS_XINO_DEFPATH, /*silent*/0,
38663 +                                     /*wbrtop*/0);
38664 +               if (IS_ERR(file))
38665 +                       goto out;
38666 +               h_sb = file->f_path.dentry->d_sb;
38667 +               if (unlikely(au_test_fs_bad_xino(h_sb))) {
38668 +                       pr_err("xino doesn't support %s(%s)\n",
38669 +                              AUFS_XINO_DEFPATH, au_sbtype(h_sb));
38670 +                       fput(file);
38671 +                       file = ERR_PTR(-EINVAL);
38672 +               }
38673 +       }
38674 +
38675 +out:
38676 +       return file;
38677 +}
38678 +
38679 +/* ---------------------------------------------------------------------- */
38680 +
38681 +/*
38682 + * initialize the xinofile for the specified branch @br
38683 + * at the place/path where @base_file indicates.
38684 + * test whether another branch is on the same filesystem or not,
38685 + * if found then share the xinofile with another branch.
38686 + */
38687 +int au_xino_init_br(struct super_block *sb, struct au_branch *br, ino_t h_ino,
38688 +                   struct path *base)
38689 +{
38690 +       int err;
38691 +       struct au_xino_do_set_br args = {
38692 +               .h_ino  = h_ino,
38693 +               .br     = br
38694 +       };
38695 +
38696 +       args.bshared = sbr_find_shared(sb, /*btop*/0, au_sbbot(sb),
38697 +                                      au_br_sb(br));
38698 +       err = au_xino_do_set_br(sb, base, &args);
38699 +       if (unlikely(err))
38700 +               au_xino_put(br);
38701 +
38702 +       return err;
38703 +}
38704 +
38705 +/* ---------------------------------------------------------------------- */
38706 +
38707 +/*
38708 + * get an unused inode number from bitmap
38709 + */
38710 +ino_t au_xino_new_ino(struct super_block *sb)
38711 +{
38712 +       ino_t ino;
38713 +       unsigned long *p, pindex, ul, pend;
38714 +       struct au_sbinfo *sbinfo;
38715 +       struct file *file;
38716 +       int free_bit, err;
38717 +
38718 +       if (!au_opt_test(au_mntflags(sb), XINO))
38719 +               return iunique(sb, AUFS_FIRST_INO);
38720 +
38721 +       sbinfo = au_sbi(sb);
38722 +       mutex_lock(&sbinfo->si_xib_mtx);
38723 +       p = sbinfo->si_xib_buf;
38724 +       free_bit = sbinfo->si_xib_next_bit;
38725 +       if (free_bit < page_bits && !test_bit(free_bit, p))
38726 +               goto out; /* success */
38727 +       free_bit = find_first_zero_bit(p, page_bits);
38728 +       if (free_bit < page_bits)
38729 +               goto out; /* success */
38730 +
38731 +       pindex = sbinfo->si_xib_last_pindex;
38732 +       for (ul = pindex - 1; ul < ULONG_MAX; ul--) {
38733 +               err = xib_pindex(sb, ul);
38734 +               if (unlikely(err))
38735 +                       goto out_err;
38736 +               free_bit = find_first_zero_bit(p, page_bits);
38737 +               if (free_bit < page_bits)
38738 +                       goto out; /* success */
38739 +       }
38740 +
38741 +       file = sbinfo->si_xib;
38742 +       pend = vfsub_f_size_read(file) / PAGE_SIZE;
38743 +       for (ul = pindex + 1; ul <= pend; ul++) {
38744 +               err = xib_pindex(sb, ul);
38745 +               if (unlikely(err))
38746 +                       goto out_err;
38747 +               free_bit = find_first_zero_bit(p, page_bits);
38748 +               if (free_bit < page_bits)
38749 +                       goto out; /* success */
38750 +       }
38751 +       BUG();
38752 +
38753 +out:
38754 +       set_bit(free_bit, p);
38755 +       sbinfo->si_xib_next_bit = free_bit + 1;
38756 +       pindex = sbinfo->si_xib_last_pindex;
38757 +       mutex_unlock(&sbinfo->si_xib_mtx);
38758 +       ino = xib_calc_ino(pindex, free_bit);
38759 +       AuDbg("i%lu\n", (unsigned long)ino);
38760 +       return ino;
38761 +out_err:
38762 +       mutex_unlock(&sbinfo->si_xib_mtx);
38763 +       AuDbg("i0\n");
38764 +       return 0;
38765 +}
38766 +
38767 +/* for s_op->delete_inode() */
38768 +void au_xino_delete_inode(struct inode *inode, const int unlinked)
38769 +{
38770 +       int err;
38771 +       unsigned int mnt_flags;
38772 +       aufs_bindex_t bindex, bbot, bi;
38773 +       unsigned char try_trunc;
38774 +       struct au_iinfo *iinfo;
38775 +       struct super_block *sb;
38776 +       struct au_hinode *hi;
38777 +       struct inode *h_inode;
38778 +       struct au_branch *br;
38779 +       struct au_xi_calc calc;
38780 +       struct file *file;
38781 +
38782 +       AuDebugOn(au_is_bad_inode(inode));
38783 +
38784 +       sb = inode->i_sb;
38785 +       mnt_flags = au_mntflags(sb);
38786 +       if (!au_opt_test(mnt_flags, XINO)
38787 +           || inode->i_ino == AUFS_ROOT_INO)
38788 +               return;
38789 +
38790 +       if (unlinked) {
38791 +               au_xigen_inc(inode);
38792 +               au_xib_clear_bit(inode);
38793 +       }
38794 +
38795 +       iinfo = au_ii(inode);
38796 +       bindex = iinfo->ii_btop;
38797 +       if (bindex < 0)
38798 +               return;
38799 +
38800 +       try_trunc = !!au_opt_test(mnt_flags, TRUNC_XINO);
38801 +       hi = au_hinode(iinfo, bindex);
38802 +       bbot = iinfo->ii_bbot;
38803 +       for (; bindex <= bbot; bindex++, hi++) {
38804 +               h_inode = hi->hi_inode;
38805 +               if (!h_inode
38806 +                   || (!unlinked && h_inode->i_nlink))
38807 +                       continue;
38808 +
38809 +               /* inode may not be revalidated */
38810 +               bi = au_br_index(sb, hi->hi_id);
38811 +               if (bi < 0)
38812 +                       continue;
38813 +
38814 +               br = au_sbr(sb, bi);
38815 +               au_xi_calc(sb, h_inode->i_ino, &calc);
38816 +               file = au_xino_file(br->br_xino, calc.idx);
38817 +               if (IS_ERR_OR_NULL(file))
38818 +                       continue;
38819 +
38820 +               err = au_xino_do_write(file, &calc, /*ino*/0);
38821 +               if (!err && try_trunc
38822 +                   && au_test_fs_trunc_xino(au_br_sb(br)))
38823 +                       xino_try_trunc(sb, br);
38824 +       }
38825 +}
38826 +
38827 +/* ---------------------------------------------------------------------- */
38828 +
38829 +static int au_xinondir_find(struct au_xino *xi, ino_t h_ino)
38830 +{
38831 +       int found, total, i;
38832 +
38833 +       found = -1;
38834 +       total = xi->xi_nondir.total;
38835 +       for (i = 0; i < total; i++) {
38836 +               if (xi->xi_nondir.array[i] != h_ino)
38837 +                       continue;
38838 +               found = i;
38839 +               break;
38840 +       }
38841 +
38842 +       return found;
38843 +}
38844 +
38845 +static int au_xinondir_expand(struct au_xino *xi)
38846 +{
38847 +       int err, sz;
38848 +       ino_t *p;
38849 +
38850 +       BUILD_BUG_ON(KMALLOC_MAX_SIZE > INT_MAX);
38851 +
38852 +       err = -ENOMEM;
38853 +       sz = xi->xi_nondir.total * sizeof(ino_t);
38854 +       if (unlikely(sz > KMALLOC_MAX_SIZE / 2))
38855 +               goto out;
38856 +       p = au_kzrealloc(xi->xi_nondir.array, sz, sz << 1, GFP_ATOMIC,
38857 +                        /*may_shrink*/0);
38858 +       if (p) {
38859 +               xi->xi_nondir.array = p;
38860 +               xi->xi_nondir.total <<= 1;
38861 +               AuDbg("xi_nondir.total %d\n", xi->xi_nondir.total);
38862 +               err = 0;
38863 +       }
38864 +
38865 +out:
38866 +       return err;
38867 +}
38868 +
38869 +void au_xinondir_leave(struct super_block *sb, aufs_bindex_t bindex,
38870 +                      ino_t h_ino, int idx)
38871 +{
38872 +       struct au_xino *xi;
38873 +
38874 +       AuDebugOn(!au_opt_test(au_mntflags(sb), XINO));
38875 +       xi = au_sbr(sb, bindex)->br_xino;
38876 +       AuDebugOn(idx < 0 || xi->xi_nondir.total <= idx);
38877 +
38878 +       spin_lock(&xi->xi_nondir.spin);
38879 +       AuDebugOn(xi->xi_nondir.array[idx] != h_ino);
38880 +       xi->xi_nondir.array[idx] = 0;
38881 +       spin_unlock(&xi->xi_nondir.spin);
38882 +       wake_up_all(&xi->xi_nondir.wqh);
38883 +}
38884 +
38885 +int au_xinondir_enter(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
38886 +                     int *idx)
38887 +{
38888 +       int err, found, empty;
38889 +       struct au_xino *xi;
38890 +
38891 +       err = 0;
38892 +       *idx = -1;
38893 +       if (!au_opt_test(au_mntflags(sb), XINO))
38894 +               goto out; /* no xino */
38895 +
38896 +       xi = au_sbr(sb, bindex)->br_xino;
38897 +
38898 +again:
38899 +       spin_lock(&xi->xi_nondir.spin);
38900 +       found = au_xinondir_find(xi, h_ino);
38901 +       if (found == -1) {
38902 +               empty = au_xinondir_find(xi, /*h_ino*/0);
38903 +               if (empty == -1) {
38904 +                       empty = xi->xi_nondir.total;
38905 +                       err = au_xinondir_expand(xi);
38906 +                       if (unlikely(err))
38907 +                               goto out_unlock;
38908 +               }
38909 +               xi->xi_nondir.array[empty] = h_ino;
38910 +               *idx = empty;
38911 +       } else {
38912 +               spin_unlock(&xi->xi_nondir.spin);
38913 +               wait_event(xi->xi_nondir.wqh,
38914 +                          xi->xi_nondir.array[found] != h_ino);
38915 +               goto again;
38916 +       }
38917 +
38918 +out_unlock:
38919 +       spin_unlock(&xi->xi_nondir.spin);
38920 +out:
38921 +       return err;
38922 +}
38923 +
38924 +/* ---------------------------------------------------------------------- */
38925 +
38926 +int au_xino_path(struct seq_file *seq, struct file *file)
38927 +{
38928 +       int err;
38929 +
38930 +       err = au_seq_path(seq, &file->f_path);
38931 +       if (unlikely(err))
38932 +               goto out;
38933 +
38934 +#define Deleted "\\040(deleted)"
38935 +       seq->count -= sizeof(Deleted) - 1;
38936 +       AuDebugOn(memcmp(seq->buf + seq->count, Deleted,
38937 +                        sizeof(Deleted) - 1));
38938 +#undef Deleted
38939 +
38940 +out:
38941 +       return err;
38942 +}
38943 diff -urN /usr/share/empty/include/uapi/linux/aufs_type.h linux/include/uapi/linux/aufs_type.h
38944 --- /usr/share/empty/include/uapi/linux/aufs_type.h     1970-01-01 01:00:00.000000000 +0100
38945 +++ linux/include/uapi/linux/aufs_type.h        2022-03-21 14:49:05.733299677 +0100
38946 @@ -0,0 +1,452 @@
38947 +/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
38948 +/*
38949 + * Copyright (C) 2005-2021 Junjiro R. Okajima
38950 + *
38951 + * This program, aufs is free software; you can redistribute it and/or modify
38952 + * it under the terms of the GNU General Public License as published by
38953 + * the Free Software Foundation; either version 2 of the License, or
38954 + * (at your option) any later version.
38955 + *
38956 + * This program is distributed in the hope that it will be useful,
38957 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
38958 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
38959 + * GNU General Public License for more details.
38960 + *
38961 + * You should have received a copy of the GNU General Public License
38962 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
38963 + */
38964 +
38965 +#ifndef __AUFS_TYPE_H__
38966 +#define __AUFS_TYPE_H__
38967 +
38968 +#define AUFS_NAME      "aufs"
38969 +
38970 +#ifdef __KERNEL__
38971 +/*
38972 + * define it before including all other headers.
38973 + * sched.h may use pr_* macros before defining "current", so define the
38974 + * no-current version first, and re-define later.
38975 + */
38976 +#define pr_fmt(fmt)    AUFS_NAME " %s:%d: " fmt, __func__, __LINE__
38977 +#include <linux/sched.h>
38978 +#undef pr_fmt
38979 +#define pr_fmt(fmt) \
38980 +               AUFS_NAME " %s:%d:%.*s[%d]: " fmt, __func__, __LINE__, \
38981 +               (int)sizeof(current->comm), current->comm, current->pid
38982 +#include <linux/limits.h>
38983 +#else
38984 +#include <stdint.h>
38985 +#include <sys/types.h>
38986 +#include <limits.h>
38987 +#endif /* __KERNEL__ */
38988 +
38989 +#define AUFS_VERSION   "5.x-rcN-20220314"
38990 +
38991 +/* todo? move this to linux-2.6.19/include/magic.h */
38992 +#define AUFS_SUPER_MAGIC       ('a' << 24 | 'u' << 16 | 'f' << 8 | 's')
38993 +
38994 +/* ---------------------------------------------------------------------- */
38995 +
38996 +#ifdef __KERNEL__
38997 +#ifdef CONFIG_AUFS_BRANCH_MAX_127
38998 +typedef int8_t aufs_bindex_t;
38999 +#define AUFS_BRANCH_MAX 127
39000 +#else
39001 +typedef int16_t aufs_bindex_t;
39002 +#ifdef CONFIG_AUFS_BRANCH_MAX_511
39003 +#define AUFS_BRANCH_MAX 511
39004 +#elif defined(CONFIG_AUFS_BRANCH_MAX_1023)
39005 +#define AUFS_BRANCH_MAX 1023
39006 +#elif defined(CONFIG_AUFS_BRANCH_MAX_32767)
39007 +#define AUFS_BRANCH_MAX 32767
39008 +#endif
39009 +#endif
39010 +
39011 +#ifndef AUFS_BRANCH_MAX
39012 +#error unknown CONFIG_AUFS_BRANCH_MAX value
39013 +#endif
39014 +#endif /* __KERNEL__ */
39015 +
39016 +/* ---------------------------------------------------------------------- */
39017 +
39018 +#define AUFS_FSTYPE            AUFS_NAME
39019 +
39020 +#define AUFS_ROOT_INO          2
39021 +#define AUFS_FIRST_INO         11
39022 +
39023 +#define AUFS_WH_PFX            ".wh."
39024 +#define AUFS_WH_PFX_LEN                ((int)sizeof(AUFS_WH_PFX) - 1)
39025 +#define AUFS_WH_TMP_LEN                4
39026 +/* a limit for rmdir/rename a dir and copyup */
39027 +#define AUFS_MAX_NAMELEN       (NAME_MAX \
39028 +                               - AUFS_WH_PFX_LEN * 2   /* doubly whiteouted */\
39029 +                               - 1                     /* dot */\
39030 +                               - AUFS_WH_TMP_LEN)      /* hex */
39031 +#define AUFS_XINO_FNAME                "." AUFS_NAME ".xino"
39032 +#define AUFS_XINO_DEFPATH      "/tmp/" AUFS_XINO_FNAME
39033 +#define AUFS_XINO_DEF_SEC      30 /* seconds */
39034 +#define AUFS_XINO_DEF_TRUNC    45 /* percentage */
39035 +#define AUFS_DIRWH_DEF         3
39036 +#define AUFS_RDCACHE_DEF       10 /* seconds */
39037 +#define AUFS_RDCACHE_MAX       3600 /* seconds */
39038 +#define AUFS_RDBLK_DEF         512 /* bytes */
39039 +#define AUFS_RDHASH_DEF                32
39040 +#define AUFS_WKQ_NAME          AUFS_NAME "d"
39041 +#define AUFS_MFS_DEF_SEC       30 /* seconds */
39042 +#define AUFS_MFS_MAX_SEC       3600 /* seconds */
39043 +#define AUFS_FHSM_CACHE_DEF_SEC        30 /* seconds */
39044 +#define AUFS_PLINK_WARN                50 /* number of plinks in a single bucket */
39045 +
39046 +/* pseudo-link maintenace under /proc */
39047 +#define AUFS_PLINK_MAINT_NAME  "plink_maint"
39048 +#define AUFS_PLINK_MAINT_DIR   "fs/" AUFS_NAME
39049 +#define AUFS_PLINK_MAINT_PATH  AUFS_PLINK_MAINT_DIR "/" AUFS_PLINK_MAINT_NAME
39050 +
39051 +/* dirren, renamed dir */
39052 +#define AUFS_DR_INFO_PFX       AUFS_WH_PFX ".dr."
39053 +#define AUFS_DR_BRHINO_NAME    AUFS_WH_PFX "hino"
39054 +/* whiteouted doubly */
39055 +#define AUFS_WH_DR_INFO_PFX    AUFS_WH_PFX AUFS_DR_INFO_PFX
39056 +#define AUFS_WH_DR_BRHINO      AUFS_WH_PFX AUFS_DR_BRHINO_NAME
39057 +
39058 +#define AUFS_DIROPQ_NAME       AUFS_WH_PFX ".opq" /* whiteouted doubly */
39059 +#define AUFS_WH_DIROPQ         AUFS_WH_PFX AUFS_DIROPQ_NAME
39060 +
39061 +#define AUFS_BASE_NAME         AUFS_WH_PFX AUFS_NAME
39062 +#define AUFS_PLINKDIR_NAME     AUFS_WH_PFX "plnk"
39063 +#define AUFS_ORPHDIR_NAME      AUFS_WH_PFX "orph"
39064 +
39065 +/* doubly whiteouted */
39066 +#define AUFS_WH_BASE           AUFS_WH_PFX AUFS_BASE_NAME
39067 +#define AUFS_WH_PLINKDIR       AUFS_WH_PFX AUFS_PLINKDIR_NAME
39068 +#define AUFS_WH_ORPHDIR                AUFS_WH_PFX AUFS_ORPHDIR_NAME
39069 +
39070 +/* branch permissions and attributes */
39071 +#define AUFS_BRPERM_RW         "rw"
39072 +#define AUFS_BRPERM_RO         "ro"
39073 +#define AUFS_BRPERM_RR         "rr"
39074 +#define AUFS_BRATTR_COO_REG    "coo_reg"
39075 +#define AUFS_BRATTR_COO_ALL    "coo_all"
39076 +#define AUFS_BRATTR_FHSM       "fhsm"
39077 +#define AUFS_BRATTR_UNPIN      "unpin"
39078 +#define AUFS_BRATTR_ICEX       "icex"
39079 +#define AUFS_BRATTR_ICEX_SEC   "icexsec"
39080 +#define AUFS_BRATTR_ICEX_SYS   "icexsys"
39081 +#define AUFS_BRATTR_ICEX_TR    "icextr"
39082 +#define AUFS_BRATTR_ICEX_USR   "icexusr"
39083 +#define AUFS_BRATTR_ICEX_OTH   "icexoth"
39084 +#define AUFS_BRRATTR_WH                "wh"
39085 +#define AUFS_BRWATTR_NLWH      "nolwh"
39086 +#define AUFS_BRWATTR_MOO       "moo"
39087 +
39088 +#define AuBrPerm_RW            1               /* writable, hardlinkable wh */
39089 +#define AuBrPerm_RO            (1 << 1)        /* readonly */
39090 +#define AuBrPerm_RR            (1 << 2)        /* natively readonly */
39091 +#define AuBrPerm_Mask          (AuBrPerm_RW | AuBrPerm_RO | AuBrPerm_RR)
39092 +
39093 +#define AuBrAttr_COO_REG       (1 << 3)        /* copy-up on open */
39094 +#define AuBrAttr_COO_ALL       (1 << 4)
39095 +#define AuBrAttr_COO_Mask      (AuBrAttr_COO_REG | AuBrAttr_COO_ALL)
39096 +
39097 +#define AuBrAttr_FHSM          (1 << 5)        /* file-based hsm */
39098 +#define AuBrAttr_UNPIN         (1 << 6)        /* rename-able top dir of
39099 +                                                  branch. meaningless since
39100 +                                                  linux-3.18-rc1 */
39101 +
39102 +/* ignore error in copying XATTR */
39103 +#define AuBrAttr_ICEX_SEC      (1 << 7)
39104 +#define AuBrAttr_ICEX_SYS      (1 << 8)
39105 +#define AuBrAttr_ICEX_TR       (1 << 9)
39106 +#define AuBrAttr_ICEX_USR      (1 << 10)
39107 +#define AuBrAttr_ICEX_OTH      (1 << 11)
39108 +#define AuBrAttr_ICEX          (AuBrAttr_ICEX_SEC      \
39109 +                                | AuBrAttr_ICEX_SYS    \
39110 +                                | AuBrAttr_ICEX_TR     \
39111 +                                | AuBrAttr_ICEX_USR    \
39112 +                                | AuBrAttr_ICEX_OTH)
39113 +
39114 +#define AuBrRAttr_WH           (1 << 12)       /* whiteout-able */
39115 +#define AuBrRAttr_Mask         AuBrRAttr_WH
39116 +
39117 +#define AuBrWAttr_NoLinkWH     (1 << 13)       /* un-hardlinkable whiteouts */
39118 +#define AuBrWAttr_MOO          (1 << 14)       /* move-up on open */
39119 +#define AuBrWAttr_Mask         (AuBrWAttr_NoLinkWH | AuBrWAttr_MOO)
39120 +
39121 +#define AuBrAttr_CMOO_Mask     (AuBrAttr_COO_Mask | AuBrWAttr_MOO)
39122 +
39123 +/* #warning test userspace */
39124 +#ifdef __KERNEL__
39125 +#ifndef CONFIG_AUFS_FHSM
39126 +#undef AuBrAttr_FHSM
39127 +#define AuBrAttr_FHSM          0
39128 +#endif
39129 +#ifndef CONFIG_AUFS_XATTR
39130 +#undef AuBrAttr_ICEX
39131 +#define AuBrAttr_ICEX          0
39132 +#undef AuBrAttr_ICEX_SEC
39133 +#define AuBrAttr_ICEX_SEC      0
39134 +#undef AuBrAttr_ICEX_SYS
39135 +#define AuBrAttr_ICEX_SYS      0
39136 +#undef AuBrAttr_ICEX_TR
39137 +#define AuBrAttr_ICEX_TR       0
39138 +#undef AuBrAttr_ICEX_USR
39139 +#define AuBrAttr_ICEX_USR      0
39140 +#undef AuBrAttr_ICEX_OTH
39141 +#define AuBrAttr_ICEX_OTH      0
39142 +#endif
39143 +#endif
39144 +
39145 +/* the longest combination */
39146 +/* AUFS_BRATTR_ICEX and AUFS_BRATTR_ICEX_TR don't affect here */
39147 +#define AuBrPermStrSz  sizeof(AUFS_BRPERM_RW                   \
39148 +                              "+" AUFS_BRATTR_COO_REG          \
39149 +                              "+" AUFS_BRATTR_FHSM             \
39150 +                              "+" AUFS_BRATTR_UNPIN            \
39151 +                              "+" AUFS_BRATTR_ICEX_SEC         \
39152 +                              "+" AUFS_BRATTR_ICEX_SYS         \
39153 +                              "+" AUFS_BRATTR_ICEX_USR         \
39154 +                              "+" AUFS_BRATTR_ICEX_OTH         \
39155 +                              "+" AUFS_BRWATTR_NLWH)
39156 +
39157 +typedef struct {
39158 +       char a[AuBrPermStrSz];
39159 +} au_br_perm_str_t;
39160 +
39161 +static inline int au_br_writable(int brperm)
39162 +{
39163 +       return brperm & AuBrPerm_RW;
39164 +}
39165 +
39166 +static inline int au_br_whable(int brperm)
39167 +{
39168 +       return brperm & (AuBrPerm_RW | AuBrRAttr_WH);
39169 +}
39170 +
39171 +static inline int au_br_wh_linkable(int brperm)
39172 +{
39173 +       return !(brperm & AuBrWAttr_NoLinkWH);
39174 +}
39175 +
39176 +static inline int au_br_cmoo(int brperm)
39177 +{
39178 +       return brperm & AuBrAttr_CMOO_Mask;
39179 +}
39180 +
39181 +static inline int au_br_fhsm(int brperm)
39182 +{
39183 +       return brperm & AuBrAttr_FHSM;
39184 +}
39185 +
39186 +/* ---------------------------------------------------------------------- */
39187 +
39188 +/* ioctl */
39189 +enum {
39190 +       /* readdir in userspace */
39191 +       AuCtl_RDU,
39192 +       AuCtl_RDU_INO,
39193 +
39194 +       AuCtl_WBR_FD,   /* pathconf wrapper */
39195 +       AuCtl_IBUSY,    /* busy inode */
39196 +       AuCtl_MVDOWN,   /* move-down */
39197 +       AuCtl_BR,       /* info about branches */
39198 +       AuCtl_FHSM_FD   /* connection for fhsm */
39199 +};
39200 +
39201 +/* borrowed from linux/include/linux/kernel.h */
39202 +#ifndef ALIGN
39203 +#ifdef _GNU_SOURCE
39204 +#define ALIGN(x, a)            __ALIGN_MASK(x, (typeof(x))(a)-1)
39205 +#else
39206 +#define ALIGN(x, a)            (((x) + (a) - 1) & ~((a) - 1))
39207 +#endif
39208 +#define __ALIGN_MASK(x, mask)  (((x)+(mask))&~(mask))
39209 +#endif
39210 +
39211 +/* borrowed from linux/include/linux/compiler-gcc3.h */
39212 +#ifndef __aligned
39213 +#define __aligned(x)                   __attribute__((aligned(x)))
39214 +#endif
39215 +
39216 +#ifdef __KERNEL__
39217 +#ifndef __packed
39218 +#define __packed                       __attribute__((packed))
39219 +#endif
39220 +#endif
39221 +
39222 +struct au_rdu_cookie {
39223 +       uint64_t        h_pos;
39224 +       int16_t         bindex;
39225 +       uint8_t         flags;
39226 +       uint8_t         pad;
39227 +       uint32_t        generation;
39228 +} __aligned(8);
39229 +
39230 +struct au_rdu_ent {
39231 +       uint64_t        ino;
39232 +       int16_t         bindex;
39233 +       uint8_t         type;
39234 +       uint8_t         nlen;
39235 +       uint8_t         wh;
39236 +       char            name[];
39237 +} __aligned(8);
39238 +
39239 +static inline int au_rdu_len(int nlen)
39240 +{
39241 +       /* include the terminating NULL */
39242 +       return ALIGN(sizeof(struct au_rdu_ent) + nlen + 1,
39243 +                    sizeof(uint64_t));
39244 +}
39245 +
39246 +union au_rdu_ent_ul {
39247 +       struct au_rdu_ent __user        *e;
39248 +       uint64_t                        ul;
39249 +};
39250 +
39251 +enum {
39252 +       AufsCtlRduV_SZ,
39253 +       AufsCtlRduV_End
39254 +};
39255 +
39256 +struct aufs_rdu {
39257 +       /* input */
39258 +       union {
39259 +               uint64_t        sz;     /* AuCtl_RDU */
39260 +               uint64_t        nent;   /* AuCtl_RDU_INO */
39261 +       };
39262 +       union au_rdu_ent_ul     ent;
39263 +       uint16_t                verify[AufsCtlRduV_End];
39264 +
39265 +       /* input/output */
39266 +       uint32_t                blk;
39267 +
39268 +       /* output */
39269 +       union au_rdu_ent_ul     tail;
39270 +       /* number of entries which were added in a single call */
39271 +       uint64_t                rent;
39272 +       uint8_t                 full;
39273 +       uint8_t                 shwh;
39274 +
39275 +       struct au_rdu_cookie    cookie;
39276 +} __aligned(8);
39277 +
39278 +/* ---------------------------------------------------------------------- */
39279 +
39280 +/* dirren. the branch is identified by the filename who contains this */
39281 +struct au_drinfo {
39282 +       uint64_t ino;
39283 +       union {
39284 +               uint8_t oldnamelen;
39285 +               uint64_t _padding;
39286 +       };
39287 +       uint8_t oldname[];
39288 +} __aligned(8);
39289 +
39290 +struct au_drinfo_fdata {
39291 +       uint32_t magic;
39292 +       struct au_drinfo drinfo;
39293 +} __aligned(8);
39294 +
39295 +#define AUFS_DRINFO_MAGIC_V1   ('a' << 24 | 'd' << 16 | 'r' << 8 | 0x01)
39296 +/* future */
39297 +#define AUFS_DRINFO_MAGIC_V2   ('a' << 24 | 'd' << 16 | 'r' << 8 | 0x02)
39298 +
39299 +/* ---------------------------------------------------------------------- */
39300 +
39301 +struct aufs_wbr_fd {
39302 +       uint32_t        oflags;
39303 +       int16_t         brid;
39304 +} __aligned(8);
39305 +
39306 +/* ---------------------------------------------------------------------- */
39307 +
39308 +struct aufs_ibusy {
39309 +       uint64_t        ino, h_ino;
39310 +       int16_t         bindex;
39311 +} __aligned(8);
39312 +
39313 +/* ---------------------------------------------------------------------- */
39314 +
39315 +/* error code for move-down */
39316 +/* the actual message strings are implemented in aufs-util.git */
39317 +enum {
39318 +       EAU_MVDOWN_OPAQUE = 1,
39319 +       EAU_MVDOWN_WHITEOUT,
39320 +       EAU_MVDOWN_UPPER,
39321 +       EAU_MVDOWN_BOTTOM,
39322 +       EAU_MVDOWN_NOUPPER,
39323 +       EAU_MVDOWN_NOLOWERBR,
39324 +       EAU_Last
39325 +};
39326 +
39327 +/* flags for move-down */
39328 +#define AUFS_MVDOWN_DMSG       1
39329 +#define AUFS_MVDOWN_OWLOWER    (1 << 1)        /* overwrite lower */
39330 +#define AUFS_MVDOWN_KUPPER     (1 << 2)        /* keep upper */
39331 +#define AUFS_MVDOWN_ROLOWER    (1 << 3)        /* do even if lower is RO */
39332 +#define AUFS_MVDOWN_ROLOWER_R  (1 << 4)        /* did on lower RO */
39333 +#define AUFS_MVDOWN_ROUPPER    (1 << 5)        /* do even if upper is RO */
39334 +#define AUFS_MVDOWN_ROUPPER_R  (1 << 6)        /* did on upper RO */
39335 +#define AUFS_MVDOWN_BRID_UPPER (1 << 7)        /* upper brid */
39336 +#define AUFS_MVDOWN_BRID_LOWER (1 << 8)        /* lower brid */
39337 +#define AUFS_MVDOWN_FHSM_LOWER (1 << 9)        /* find fhsm attr for lower */
39338 +#define AUFS_MVDOWN_STFS       (1 << 10)       /* req. stfs */
39339 +#define AUFS_MVDOWN_STFS_FAILED        (1 << 11)       /* output: stfs is unusable */
39340 +#define AUFS_MVDOWN_BOTTOM     (1 << 12)       /* output: no more lowers */
39341 +
39342 +/* index for move-down */
39343 +enum {
39344 +       AUFS_MVDOWN_UPPER,
39345 +       AUFS_MVDOWN_LOWER,
39346 +       AUFS_MVDOWN_NARRAY
39347 +};
39348 +
39349 +/*
39350 + * additional info of move-down
39351 + * number of free blocks and inodes.
39352 + * subset of struct kstatfs, but smaller and always 64bit.
39353 + */
39354 +struct aufs_stfs {
39355 +       uint64_t        f_blocks;
39356 +       uint64_t        f_bavail;
39357 +       uint64_t        f_files;
39358 +       uint64_t        f_ffree;
39359 +};
39360 +
39361 +struct aufs_stbr {
39362 +       int16_t                 brid;   /* optional input */
39363 +       int16_t                 bindex; /* output */
39364 +       struct aufs_stfs        stfs;   /* output when AUFS_MVDOWN_STFS set */
39365 +} __aligned(8);
39366 +
39367 +struct aufs_mvdown {
39368 +       uint32_t                flags;                  /* input/output */
39369 +       struct aufs_stbr        stbr[AUFS_MVDOWN_NARRAY]; /* input/output */
39370 +       int8_t                  au_errno;               /* output */
39371 +} __aligned(8);
39372 +
39373 +/* ---------------------------------------------------------------------- */
39374 +
39375 +union aufs_brinfo {
39376 +       /* PATH_MAX may differ between kernel-space and user-space */
39377 +       char    _spacer[4096];
39378 +       struct {
39379 +               int16_t id;
39380 +               int     perm;
39381 +               char    path[];
39382 +       };
39383 +} __aligned(8);
39384 +
39385 +/* ---------------------------------------------------------------------- */
39386 +
39387 +#define AuCtlType              'A'
39388 +#define AUFS_CTL_RDU           _IOWR(AuCtlType, AuCtl_RDU, struct aufs_rdu)
39389 +#define AUFS_CTL_RDU_INO       _IOWR(AuCtlType, AuCtl_RDU_INO, struct aufs_rdu)
39390 +#define AUFS_CTL_WBR_FD                _IOW(AuCtlType, AuCtl_WBR_FD, \
39391 +                                    struct aufs_wbr_fd)
39392 +#define AUFS_CTL_IBUSY         _IOWR(AuCtlType, AuCtl_IBUSY, struct aufs_ibusy)
39393 +#define AUFS_CTL_MVDOWN                _IOWR(AuCtlType, AuCtl_MVDOWN, \
39394 +                                     struct aufs_mvdown)
39395 +#define AUFS_CTL_BRINFO                _IOW(AuCtlType, AuCtl_BR, union aufs_brinfo)
39396 +#define AUFS_CTL_FHSM_FD       _IOW(AuCtlType, AuCtl_FHSM_FD, int)
39397 +
39398 +#endif /* __AUFS_TYPE_H__ */
39399 SPDX-License-Identifier: GPL-2.0
39400 aufs5.x-rcN loopback patch
39401
39402 diff --git a/drivers/block/loop.c b/drivers/block/loop.c
39403 index 9009a0efc4b28..60307b90fbead 100644
39404 --- a/drivers/block/loop.c
39405 +++ b/drivers/block/loop.c
39406 @@ -510,6 +510,15 @@ static inline void loop_update_dio(struct loop_device *lo)
39407                                 lo->use_dio);
39408  }
39409  
39410 +static struct file *loop_real_file(struct file *file)
39411 +{
39412 +       struct file *f = NULL;
39413 +
39414 +       if (file->f_path.dentry->d_sb->s_op->real_loop)
39415 +               f = file->f_path.dentry->d_sb->s_op->real_loop(file);
39416 +       return f;
39417 +}
39418 +
39419  static void loop_reread_partitions(struct loop_device *lo)
39420  {
39421         int rc;
39422 @@ -567,6 +576,7 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39423  {
39424         struct file *file = fget(arg);
39425         struct file *old_file;
39426 +       struct file *f, *virt_file = NULL, *old_virt_file;
39427         int error;
39428         bool partscan;
39429         bool is_loop;
39430 @@ -586,11 +596,19 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39431         if (!(lo->lo_flags & LO_FLAGS_READ_ONLY))
39432                 goto out_err;
39433  
39434 +       f = loop_real_file(file);
39435 +       if (f) {
39436 +               virt_file = file;
39437 +               file = f;
39438 +               get_file(file);
39439 +       }
39440 +
39441         error = loop_validate_file(file, bdev);
39442         if (error)
39443                 goto out_err;
39444  
39445         old_file = lo->lo_backing_file;
39446 +       old_virt_file = lo->lo_backing_virt_file;
39447  
39448         error = -EINVAL;
39449  
39450 @@ -603,6 +621,7 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39451         blk_mq_freeze_queue(lo->lo_queue);
39452         mapping_set_gfp_mask(old_file->f_mapping, lo->old_gfp_mask);
39453         lo->lo_backing_file = file;
39454 +       lo->lo_backing_virt_file = virt_file;
39455         lo->old_gfp_mask = mapping_gfp_mask(file->f_mapping);
39456         mapping_set_gfp_mask(file->f_mapping,
39457                              lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
39458 @@ -625,6 +644,8 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39459          * dependency.
39460          */
39461         fput(old_file);
39462 +       if (old_virt_file)
39463 +               fput(old_virt_file);
39464         if (partscan)
39465                 loop_reread_partitions(lo);
39466         return 0;
39467 @@ -633,6 +654,8 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39468         loop_global_unlock(lo, is_loop);
39469  out_putf:
39470         fput(file);
39471 +       if (virt_file)
39472 +               fput(virt_file);
39473         return error;
39474  }
39475  
39476 @@ -971,6 +994,7 @@ static int loop_configure(struct loop_device *lo, fmode_t mode,
39477                           const struct loop_config *config)
39478  {
39479         struct file *file = fget(config->fd);
39480 +       struct file *f, *virt_file = NULL;
39481         struct inode *inode;
39482         struct address_space *mapping;
39483         int error;
39484 @@ -986,6 +1010,13 @@ static int loop_configure(struct loop_device *lo, fmode_t mode,
39485         /* This is safe, since we have a reference from open(). */
39486         __module_get(THIS_MODULE);
39487  
39488 +       f = loop_real_file(file);
39489 +       if (f) {
39490 +               virt_file = file;
39491 +               file = f;
39492 +               get_file(file);
39493 +       }
39494 +
39495         /*
39496          * If we don't hold exclusive handle for the device, upgrade to it
39497          * here to avoid changing device under exclusive owner.
39498 @@ -1051,6 +1082,7 @@ static int loop_configure(struct loop_device *lo, fmode_t mode,
39499         lo->use_dio = lo->lo_flags & LO_FLAGS_DIRECT_IO;
39500         lo->lo_device = bdev;
39501         lo->lo_backing_file = file;
39502 +       lo->lo_backing_virt_file = virt_file;
39503         lo->old_gfp_mask = mapping_gfp_mask(mapping);
39504         mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
39505  
39506 @@ -1101,6 +1133,8 @@ static int loop_configure(struct loop_device *lo, fmode_t mode,
39507                 bd_abort_claiming(bdev, loop_configure);
39508  out_putf:
39509         fput(file);
39510 +       if (virt_file)
39511 +               fput(virt_file);
39512         /* This is safe: open() is still holding a reference. */
39513         module_put(THIS_MODULE);
39514         return error;
39515 @@ -1109,6 +1143,7 @@ static int loop_configure(struct loop_device *lo, fmode_t mode,
39516  static void __loop_clr_fd(struct loop_device *lo, bool release)
39517  {
39518         struct file *filp;
39519 +       struct file *virt_filp = lo->lo_backing_virt_file;
39520         gfp_t gfp = lo->old_gfp_mask;
39521         struct loop_worker *pos, *worker;
39522  
39523 @@ -1154,6 +1189,7 @@ static void __loop_clr_fd(struct loop_device *lo, bool release)
39524         spin_lock_irq(&lo->lo_lock);
39525         filp = lo->lo_backing_file;
39526         lo->lo_backing_file = NULL;
39527 +       lo->lo_backing_virt_file = NULL;
39528         spin_unlock_irq(&lo->lo_lock);
39529  
39530         lo->lo_device = NULL;
39531 @@ -1215,6 +1251,8 @@ static void __loop_clr_fd(struct loop_device *lo, bool release)
39532          * fput can take open_mutex which is usually taken before lo_mutex.
39533          */
39534         fput(filp);
39535 +       if (virt_filp)
39536 +               fput(virt_filp);
39537  }
39538  
39539  static int loop_clr_fd(struct loop_device *lo)
39540 diff --git a/drivers/block/loop.h b/drivers/block/loop.h
39541 index 082d4b6bfc6a6..61feca64688df 100644
39542 --- a/drivers/block/loop.h
39543 +++ b/drivers/block/loop.h
39544 @@ -34,7 +34,7 @@ struct loop_device {
39545         int             lo_flags;
39546         char            lo_file_name[LO_NAME_SIZE];
39547  
39548 -       struct file *   lo_backing_file;
39549 +       struct file     *lo_backing_file, *lo_backing_virt_file;
39550         struct block_device *lo_device;
39551  
39552         gfp_t           old_gfp_mask;
39553 diff --git a/fs/aufs/f_op.c b/fs/aufs/f_op.c
39554 index 99ceca1440449..e49dfe8550329 100644
39555 --- a/fs/aufs/f_op.c
39556 +++ b/fs/aufs/f_op.c
39557 @@ -304,7 +304,7 @@ static ssize_t aufs_read_iter(struct kiocb *kio, struct iov_iter *iov_iter)
39558         if (IS_ERR(h_file))
39559                 goto out;
39560  
39561 -       if (au_test_loopback_kthread()) {
39562 +       if (0 && au_test_loopback_kthread()) {
39563                 au_warn_loopback(h_file->f_path.dentry->d_sb);
39564                 if (file->f_mapping != h_file->f_mapping) {
39565                         file->f_mapping = h_file->f_mapping;
39566 diff --git a/fs/aufs/loop.c b/fs/aufs/loop.c
39567 index 74347bd75b380..5ef888a1d53f4 100644
39568 --- a/fs/aufs/loop.c
39569 +++ b/fs/aufs/loop.c
39570 @@ -133,3 +133,19 @@ void au_loopback_fin(void)
39571                 symbol_put(loop_backing_file);
39572         au_kfree_try_rcu(au_warn_loopback_array);
39573  }
39574 +
39575 +/* ---------------------------------------------------------------------- */
39576 +
39577 +/* support the loopback block device insude aufs */
39578 +
39579 +struct file *aufs_real_loop(struct file *file)
39580 +{
39581 +       struct file *f;
39582 +
39583 +       BUG_ON(!au_test_aufs(file->f_path.dentry->d_sb));
39584 +       fi_read_lock(file);
39585 +       f = au_hf_top(file);
39586 +       fi_read_unlock(file);
39587 +       AuDebugOn(!f);
39588 +       return f;
39589 +}
39590 diff --git a/fs/aufs/loop.h b/fs/aufs/loop.h
39591 index 7293bee427f96..3345c098d0d47 100644
39592 --- a/fs/aufs/loop.h
39593 +++ b/fs/aufs/loop.h
39594 @@ -26,6 +26,8 @@ void au_warn_loopback(struct super_block *h_sb);
39595  
39596  int au_loopback_init(void);
39597  void au_loopback_fin(void);
39598 +
39599 +struct file *aufs_real_loop(struct file *file);
39600  #else
39601  AuStub(struct file *, loop_backing_file, return NULL, struct super_block *sb)
39602  
39603 @@ -36,6 +38,8 @@ AuStubVoid(au_warn_loopback, struct super_block *h_sb)
39604  
39605  AuStubInt0(au_loopback_init, void)
39606  AuStubVoid(au_loopback_fin, void)
39607 +
39608 +AuStub(struct file *, aufs_real_loop, return NULL, struct file *file)
39609  #endif /* BLK_DEV_LOOP */
39610  
39611  #endif /* __KERNEL__ */
39612 diff --git a/fs/aufs/super.c b/fs/aufs/super.c
39613 index 90043afec51c1..0835f6da42d97 100644
39614 --- a/fs/aufs/super.c
39615 +++ b/fs/aufs/super.c
39616 @@ -758,7 +758,10 @@ const struct super_operations aufs_sop = {
39617         .show_options   = aufs_show_options,
39618         .statfs         = aufs_statfs,
39619         .put_super      = aufs_put_super,
39620 -       .sync_fs        = aufs_sync_fs
39621 +       .sync_fs        = aufs_sync_fs,
39622 +#ifdef CONFIG_AUFS_BDEV_LOOP
39623 +       .real_loop      = aufs_real_loop
39624 +#endif
39625  };
39626  
39627  /* ---------------------------------------------------------------------- */
39628 diff --git a/include/linux/fs.h b/include/linux/fs.h
39629 index 5479e6dc18a23..05fe92d076c19 100644
39630 --- a/include/linux/fs.h
39631 +++ b/include/linux/fs.h
39632 @@ -2137,6 +2137,10 @@ struct super_operations {
39633                                   struct shrink_control *);
39634         long (*free_cached_objects)(struct super_block *,
39635                                     struct shrink_control *);
39636 +#if IS_ENABLED(CONFIG_BLK_DEV_LOOP) || IS_ENABLED(CONFIG_BLK_DEV_LOOP_MODULE)
39637 +       /* and aufs */
39638 +       struct file *(*real_loop)(struct file *);
39639 +#endif
39640  };
39641  
39642  /*
This page took 3.273431 seconds and 2 git commands to generate.