]> git.pld-linux.org Git - packages/kernel.git/blob - kernel-aufs5.patch
Up to 5.18.0.
[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 30b751c7f11a..fb8214374858 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 208a74e0b00e..57d47a0762e0 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 61d9f114c37f..95cb37e9c555 100644
30 --- a/MAINTAINERS
31 +++ b/MAINTAINERS
32 @@ -3323,6 +3323,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 a58595f5ee2c..ba35d0d6847a 100644
54 --- a/drivers/block/loop.c
55 +++ b/drivers/block/loop.c
56 @@ -635,6 +635,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 93f4f5ee07bf..cddcf2e44930 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 f15d885b9796..d91d1bf393cc 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 a0a36bfa3aa0..7a54c6b514a2 100644
118 --- a/fs/namespace.c
119 +++ b/fs/namespace.c
120 @@ -852,6 +852,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 047b79db8eb5..a21caa5c54c8 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 bbde95387a23..06c9242932a2 100644
163 --- a/include/linux/fs.h
164 +++ b/include/linux/fs.h
165 @@ -1347,6 +1347,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 @@ -1979,6 +1980,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 @@ -2486,6 +2488,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 @@ -2664,6 +2667,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 467b94257105..dab20282c82e 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 8f882f5881e8..6b9808f09843 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 a55179fd60fc..8e21c53cf883 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 c06cab6546ed..9d7c317899e1 100644
247 --- a/kernel/locking/lockdep.c
248 +++ b/kernel/locking/lockdep.c
249 @@ -187,7 +187,7 @@ unsigned long max_lock_class_idx;
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 @@ -208,6 +208,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 c1031843cc6a..6597f53969bf 100644
271 --- a/fs/proc/base.c
272 +++ b/fs/proc/base.c
273 @@ -2190,7 +2190,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 13452b32e2bd..38acccfef9d4 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 f46060eb91b5..2a7c8709acad 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 a6d21fc0033c..02c2de31196e 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 e34edb775334..08ac00a002a7 100644
341 --- a/include/linux/mm.h
342 +++ b/include/linux/mm.h
343 @@ -1902,6 +1902,43 @@ static inline void unmap_shared_mapping_range(struct address_space *mapping,
344         unmap_mapping_range(mapping, holebegin, holelen, 0);
345  }
346  
347 +#if IS_ENABLED(CONFIG_AUFS_FS)
348 +extern void vma_do_file_update_time(struct vm_area_struct *, const char[], int);
349 +extern struct file *vma_do_pr_or_file(struct vm_area_struct *, const char[],
350 +                                     int);
351 +extern void vma_do_get_file(struct vm_area_struct *, const char[], int);
352 +extern void vma_do_fput(struct vm_area_struct *, const char[], int);
353 +
354 +#define vma_file_update_time(vma)      vma_do_file_update_time(vma, __func__, \
355 +                                                               __LINE__)
356 +#define vma_pr_or_file(vma)            vma_do_pr_or_file(vma, __func__, \
357 +                                                         __LINE__)
358 +#define vma_get_file(vma)              vma_do_get_file(vma, __func__, __LINE__)
359 +#define vma_fput(vma)                  vma_do_fput(vma, __func__, __LINE__)
360 +
361 +#ifndef CONFIG_MMU
362 +extern struct file *vmr_do_pr_or_file(struct vm_region *, const char[], int);
363 +extern void vmr_do_fput(struct vm_region *, const char[], int);
364 +
365 +#define vmr_pr_or_file(region)         vmr_do_pr_or_file(region, __func__, \
366 +                                                         __LINE__)
367 +#define vmr_fput(region)               vmr_do_fput(region, __func__, __LINE__)
368 +#endif /* !CONFIG_MMU */
369 +
370 +#else
371 +
372 +#define vma_file_update_time(vma)      file_update_time((vma)->vm_file)
373 +#define vma_pr_or_file(vma)            (vma)->vm_file
374 +#define vma_get_file(vma)              get_file((vma)->vm_file)
375 +#define vma_fput(vma)                  fput((vma)->vm_file)
376 +
377 +#ifndef CONFIG_MMU
378 +#define vmr_pr_or_file(region)         (region)->vm_file
379 +#define vmr_fput(region)               fput((region)->vm_file)
380 +#endif /* !CONFIG_MMU */
381 +
382 +#endif /* CONFIG_AUFS_FS */
383 +
384  extern int access_process_vm(struct task_struct *tsk, unsigned long addr,
385                 void *buf, int len, unsigned int gup_flags);
386  extern int access_remote_vm(struct mm_struct *mm, unsigned long addr,
387 diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h
388 index 8834e38c06a4..628b7d1eedbd 100644
389 --- a/include/linux/mm_types.h
390 +++ b/include/linux/mm_types.h
391 @@ -362,6 +362,9 @@ struct vm_region {
392         unsigned long   vm_top;         /* region allocated to here */
393         unsigned long   vm_pgoff;       /* the offset in vm_file corresponding to vm_start */
394         struct file     *vm_file;       /* the backing file or NULL */
395 +#if IS_ENABLED(CONFIG_AUFS_FS)
396 +       struct file     *vm_prfile;     /* the virtual backing file or NULL */
397 +#endif
398  
399         int             vm_usage;       /* region usage count (access under nommu_region_sem) */
400         bool            vm_icache_flushed : 1; /* true if the icache has been flushed for
401 @@ -458,6 +461,9 @@ struct vm_area_struct {
402         unsigned long vm_pgoff;         /* Offset (within vm_file) in PAGE_SIZE
403                                            units */
404         struct file * vm_file;          /* File we map to (can be NULL). */
405 +#if IS_ENABLED(CONFIG_AUFS_FS)
406 +       struct file *vm_prfile;         /* shadow of vm_file */
407 +#endif
408         void * vm_private_data;         /* was vm_pte (shared mem) */
409  
410  #ifdef CONFIG_SWAP
411 diff --git a/kernel/fork.c b/kernel/fork.c
412 index 9796897560ab..67c5046f47b8 100644
413 --- a/kernel/fork.c
414 +++ b/kernel/fork.c
415 @@ -664,7 +664,7 @@ static __latent_entropy int dup_mmap(struct mm_struct *mm,
416                 if (file) {
417                         struct address_space *mapping = file->f_mapping;
418  
419 -                       get_file(file);
420 +                       vma_get_file(tmp);
421                         i_mmap_lock_write(mapping);
422                         if (tmp->vm_flags & VM_SHARED)
423                                 mapping_allow_writable(mapping);
424 diff --git a/mm/Makefile b/mm/Makefile
425 index 4cc13f3179a5..a1bbb3af3fc2 100644
426 --- a/mm/Makefile
427 +++ b/mm/Makefile
428 @@ -133,3 +133,4 @@ obj-$(CONFIG_PAGE_REPORTING) += page_reporting.o
429  obj-$(CONFIG_IO_MAPPING) += io-mapping.o
430  obj-$(CONFIG_HAVE_BOOTMEM_INFO_NODE) += bootmem_info.o
431  obj-$(CONFIG_GENERIC_IOREMAP) += ioremap.o
432 +obj-$(CONFIG_AUFS_FS:m=y) += prfile.o
433 diff --git a/mm/filemap.c b/mm/filemap.c
434 index 3a5ffb5587cd..6d12a6337a60 100644
435 --- a/mm/filemap.c
436 +++ b/mm/filemap.c
437 @@ -3422,7 +3422,7 @@ vm_fault_t filemap_page_mkwrite(struct vm_fault *vmf)
438         vm_fault_t ret = VM_FAULT_LOCKED;
439  
440         sb_start_pagefault(mapping->host->i_sb);
441 -       file_update_time(vmf->vma->vm_file);
442 +       vma_file_update_time(vmf->vma);
443         folio_lock(folio);
444         if (folio->mapping != mapping) {
445                 folio_unlock(folio);
446 diff --git a/mm/mmap.c b/mm/mmap.c
447 index 3aa839f81e63..9dfe979e8c2b 100644
448 --- a/mm/mmap.c
449 +++ b/mm/mmap.c
450 @@ -184,7 +184,7 @@ static struct vm_area_struct *remove_vma(struct vm_area_struct *vma)
451         if (vma->vm_ops && vma->vm_ops->close)
452                 vma->vm_ops->close(vma);
453         if (vma->vm_file)
454 -               fput(vma->vm_file);
455 +               vma_fput(vma);
456         mpol_put(vma_policy(vma));
457         vm_area_free(vma);
458         return next;
459 @@ -953,7 +953,7 @@ int __vma_adjust(struct vm_area_struct *vma, unsigned long start,
460         if (remove_next) {
461                 if (file) {
462                         uprobe_munmap(next, next->vm_start, next->vm_end);
463 -                       fput(file);
464 +                       vma_fput(vma);
465                 }
466                 if (next->anon_vma)
467                         anon_vma_merge(vma, next);
468 @@ -1877,7 +1877,7 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
469         return addr;
470  
471  unmap_and_free_vma:
472 -       fput(vma->vm_file);
473 +       vma_fput(vma);
474         vma->vm_file = NULL;
475  
476         /* Undo any partial mapping done by a device driver. */
477 @@ -2737,7 +2737,7 @@ int __split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
478                 goto out_free_mpol;
479  
480         if (new->vm_file)
481 -               get_file(new->vm_file);
482 +               vma_get_file(new);
483  
484         if (new->vm_ops && new->vm_ops->open)
485                 new->vm_ops->open(new);
486 @@ -2756,7 +2756,7 @@ int __split_vma(struct mm_struct *mm, struct vm_area_struct *vma,
487         if (new->vm_ops && new->vm_ops->close)
488                 new->vm_ops->close(new);
489         if (new->vm_file)
490 -               fput(new->vm_file);
491 +               vma_fput(new);
492         unlink_anon_vmas(new);
493   out_free_mpol:
494         mpol_put(vma_policy(new));
495 @@ -2929,6 +2929,9 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
496         unsigned long populate = 0;
497         unsigned long ret = -EINVAL;
498         struct file *file;
499 +#if IS_ENABLED(CONFIG_AUFS_FS)
500 +       struct file *prfile;
501 +#endif
502  
503         pr_warn_once("%s (%d) uses deprecated remap_file_pages() syscall. See Documentation/vm/remap_file_pages.rst.\n",
504                      current->comm, current->pid);
505 @@ -2984,10 +2987,34 @@ SYSCALL_DEFINE5(remap_file_pages, unsigned long, start, unsigned long, size,
506         if (vma->vm_flags & VM_LOCKED)
507                 flags |= MAP_LOCKED;
508  
509 +#if IS_ENABLED(CONFIG_AUFS_FS)
510 +       vma_get_file(vma);
511 +       file = vma->vm_file;
512 +       prfile = vma->vm_prfile;
513 +       ret = do_mmap(vma->vm_file, start, size,
514 +                       prot, flags, pgoff, &populate, NULL);
515 +       if (!IS_ERR_VALUE(ret) && file && prfile) {
516 +               struct vm_area_struct *new_vma;
517 +
518 +               new_vma = find_vma(mm, ret);
519 +               if (!new_vma->vm_prfile)
520 +                       new_vma->vm_prfile = prfile;
521 +               if (new_vma != vma)
522 +                       get_file(prfile);
523 +       }
524 +       /*
525 +        * two fput()s instead of vma_fput(vma),
526 +        * coz vma may not be available anymore.
527 +        */
528 +       fput(file);
529 +       if (prfile)
530 +               fput(prfile);
531 +#else
532         file = get_file(vma->vm_file);
533         ret = do_mmap(vma->vm_file, start, size,
534                         prot, flags, pgoff, &populate, NULL);
535         fput(file);
536 +#endif /* CONFIG_AUFS_FS */
537  out:
538         mmap_write_unlock(mm);
539         if (populate)
540 @@ -3261,7 +3288,7 @@ struct vm_area_struct *copy_vma(struct vm_area_struct **vmap,
541                 if (anon_vma_clone(new_vma, vma))
542                         goto out_free_mempol;
543                 if (new_vma->vm_file)
544 -                       get_file(new_vma->vm_file);
545 +                       vma_get_file(new_vma);
546                 if (new_vma->vm_ops && new_vma->vm_ops->open)
547                         new_vma->vm_ops->open(new_vma);
548                 vma_link(mm, new_vma, prev, rb_link, rb_parent);
549 diff --git a/mm/nommu.c b/mm/nommu.c
550 index 55a9e48a7a02..8dc77ce96335 100644
551 --- a/mm/nommu.c
552 +++ b/mm/nommu.c
553 @@ -522,7 +522,7 @@ static void __put_nommu_region(struct vm_region *region)
554                 up_write(&nommu_region_sem);
555  
556                 if (region->vm_file)
557 -                       fput(region->vm_file);
558 +                       vmr_fput(region);
559  
560                 /* IO memory and memory shared directly out of the pagecache
561                  * from ramfs/tmpfs mustn't be released here */
562 @@ -654,7 +654,7 @@ static void delete_vma(struct mm_struct *mm, struct vm_area_struct *vma)
563         if (vma->vm_ops && vma->vm_ops->close)
564                 vma->vm_ops->close(vma);
565         if (vma->vm_file)
566 -               fput(vma->vm_file);
567 +               vma_fput(vma);
568         put_nommu_region(vma->vm_region);
569         vm_area_free(vma);
570  }
571 @@ -1174,7 +1174,7 @@ unsigned long do_mmap(struct file *file,
572                                         goto error_just_free;
573                                 }
574                         }
575 -                       fput(region->vm_file);
576 +                       vmr_fput(region);
577                         kmem_cache_free(vm_region_jar, region);
578                         region = pregion;
579                         result = start;
580 @@ -1251,10 +1251,10 @@ unsigned long do_mmap(struct file *file,
581         up_write(&nommu_region_sem);
582  error:
583         if (region->vm_file)
584 -               fput(region->vm_file);
585 +               vmr_fput(region);
586         kmem_cache_free(vm_region_jar, region);
587         if (vma->vm_file)
588 -               fput(vma->vm_file);
589 +               vma_fput(vma);
590         vm_area_free(vma);
591         return ret;
592  
593 diff --git a/mm/prfile.c b/mm/prfile.c
594 new file mode 100644
595 index 000000000000..511543ab1b41
596 --- /dev/null
597 +++ b/mm/prfile.c
598 @@ -0,0 +1,86 @@
599 +// SPDX-License-Identifier: GPL-2.0
600 +/*
601 + * Mainly for aufs which mmap(2) different file and wants to print different
602 + * path in /proc/PID/maps.
603 + * Call these functions via macros defined in linux/mm.h.
604 + *
605 + * See Documentation/filesystems/aufs/design/06mmap.txt
606 + *
607 + * Copyright (c) 2014-2021 Junjro R. Okajima
608 + * Copyright (c) 2014 Ian Campbell
609 + */
610 +
611 +#include <linux/mm.h>
612 +#include <linux/file.h>
613 +#include <linux/fs.h>
614 +
615 +/* #define PRFILE_TRACE */
616 +static inline void prfile_trace(struct file *f, struct file *pr,
617 +                             const char func[], int line, const char func2[])
618 +{
619 +#ifdef PRFILE_TRACE
620 +       if (pr)
621 +               pr_info("%s:%d: %s, %pD2\n", func, line, func2, f);
622 +#endif
623 +}
624 +
625 +void vma_do_file_update_time(struct vm_area_struct *vma, const char func[],
626 +                            int line)
627 +{
628 +       struct file *f = vma->vm_file, *pr = vma->vm_prfile;
629 +
630 +       prfile_trace(f, pr, func, line, __func__);
631 +       file_update_time(f);
632 +       if (f && pr)
633 +               file_update_time(pr);
634 +}
635 +
636 +struct file *vma_do_pr_or_file(struct vm_area_struct *vma, const char func[],
637 +                              int line)
638 +{
639 +       struct file *f = vma->vm_file, *pr = vma->vm_prfile;
640 +
641 +       prfile_trace(f, pr, func, line, __func__);
642 +       return (f && pr) ? pr : f;
643 +}
644 +
645 +void vma_do_get_file(struct vm_area_struct *vma, const char func[], int line)
646 +{
647 +       struct file *f = vma->vm_file, *pr = vma->vm_prfile;
648 +
649 +       prfile_trace(f, pr, func, line, __func__);
650 +       get_file(f);
651 +       if (f && pr)
652 +               get_file(pr);
653 +}
654 +
655 +void vma_do_fput(struct vm_area_struct *vma, const char func[], int line)
656 +{
657 +       struct file *f = vma->vm_file, *pr = vma->vm_prfile;
658 +
659 +       prfile_trace(f, pr, func, line, __func__);
660 +       fput(f);
661 +       if (f && pr)
662 +               fput(pr);
663 +}
664 +
665 +#ifndef CONFIG_MMU
666 +struct file *vmr_do_pr_or_file(struct vm_region *region, const char func[],
667 +                              int line)
668 +{
669 +       struct file *f = region->vm_file, *pr = region->vm_prfile;
670 +
671 +       prfile_trace(f, pr, func, line, __func__);
672 +       return (f && pr) ? pr : f;
673 +}
674 +
675 +void vmr_do_fput(struct vm_region *region, const char func[], int line)
676 +{
677 +       struct file *f = region->vm_file, *pr = region->vm_prfile;
678 +
679 +       prfile_trace(f, pr, func, line, __func__);
680 +       fput(f);
681 +       if (f && pr)
682 +               fput(pr);
683 +}
684 +#endif /* !CONFIG_MMU */
685 SPDX-License-Identifier: GPL-2.0
686 aufs5.x-rcN standalone patch
687
688 diff --git a/fs/dcache.c b/fs/dcache.c
689 index cddcf2e44930..8e24a158ef4f 100644
690 --- a/fs/dcache.c
691 +++ b/fs/dcache.c
692 @@ -1450,6 +1450,7 @@ void d_walk(struct dentry *parent, void *data,
693         seq = 1;
694         goto again;
695  }
696 +EXPORT_SYMBOL_GPL(d_walk);
697  
698  struct check_mount {
699         struct vfsmount *mnt;
700 @@ -2996,6 +2997,7 @@ void d_exchange(struct dentry *dentry1, struct dentry *dentry2)
701  
702         write_sequnlock(&rename_lock);
703  }
704 +EXPORT_SYMBOL_GPL(d_exchange);
705  
706  /**
707   * d_ancestor - search for an ancestor
708 diff --git a/fs/exec.c b/fs/exec.c
709 index e3e55d5e0be1..145d95db8bc9 100644
710 --- a/fs/exec.c
711 +++ b/fs/exec.c
712 @@ -111,6 +111,7 @@ bool path_noexec(const struct path *path)
713         return (path->mnt->mnt_flags & MNT_NOEXEC) ||
714                (path->mnt->mnt_sb->s_iflags & SB_I_NOEXEC);
715  }
716 +EXPORT_SYMBOL_GPL(path_noexec);
717  
718  #ifdef CONFIG_USELIB
719  /*
720 diff --git a/fs/fcntl.c b/fs/fcntl.c
721 index d91d1bf393cc..3417421651d5 100644
722 --- a/fs/fcntl.c
723 +++ b/fs/fcntl.c
724 @@ -86,6 +86,7 @@ int setfl(int fd, struct file *filp, unsigned long arg)
725   out:
726         return error;
727  }
728 +EXPORT_SYMBOL_GPL(setfl);
729  
730  static void f_modown(struct file *filp, struct pid *pid, enum pid_type type,
731                       int force)
732 diff --git a/fs/file_table.c b/fs/file_table.c
733 index ada8fe814db9..4ebcd56ea4f0 100644
734 --- a/fs/file_table.c
735 +++ b/fs/file_table.c
736 @@ -198,6 +198,7 @@ struct file *alloc_empty_file(int flags, const struct cred *cred)
737         }
738         return ERR_PTR(-ENFILE);
739  }
740 +EXPORT_SYMBOL_GPL(alloc_empty_file);
741  
742  /*
743   * Variant of alloc_empty_file() that doesn't check and modify nr_files.
744 diff --git a/fs/namespace.c b/fs/namespace.c
745 index 7a54c6b514a2..ddae5f68ac19 100644
746 --- a/fs/namespace.c
747 +++ b/fs/namespace.c
748 @@ -456,6 +456,7 @@ void __mnt_drop_write(struct vfsmount *mnt)
749         mnt_dec_writers(real_mount(mnt));
750         preempt_enable();
751  }
752 +EXPORT_SYMBOL_GPL(__mnt_drop_write);
753  
754  /**
755   * mnt_drop_write - give up write access to a mount
756 @@ -857,6 +858,7 @@ int is_current_mnt_ns(struct vfsmount *mnt)
757  {
758         return check_mnt(real_mount(mnt));
759  }
760 +EXPORT_SYMBOL_GPL(is_current_mnt_ns);
761  
762  /*
763   * vfsmount lock must be held for write
764 @@ -2060,6 +2062,7 @@ int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg,
765         }
766         return 0;
767  }
768 +EXPORT_SYMBOL_GPL(iterate_mounts);
769  
770  static void lock_mnt_tree(struct mount *mnt)
771  {
772 diff --git a/fs/notify/group.c b/fs/notify/group.c
773 index b7d4d64f87c2..2d2d2c6d33e4 100644
774 --- a/fs/notify/group.c
775 +++ b/fs/notify/group.c
776 @@ -100,6 +100,7 @@ void fsnotify_get_group(struct fsnotify_group *group)
777  {
778         refcount_inc(&group->refcnt);
779  }
780 +EXPORT_SYMBOL_GPL(fsnotify_get_group);
781  
782  /*
783   * Drop a reference to a group.  Free it if it's through.
784 diff --git a/fs/open.c b/fs/open.c
785 index 1315253e0247..46d9235f3a85 100644
786 --- a/fs/open.c
787 +++ b/fs/open.c
788 @@ -66,6 +66,7 @@ int do_truncate(struct user_namespace *mnt_userns, struct dentry *dentry,
789         inode_unlock(dentry->d_inode);
790         return ret;
791  }
792 +EXPORT_SYMBOL_GPL(do_truncate);
793  
794  long vfs_truncate(const struct path *path, loff_t length)
795  {
796 diff --git a/fs/read_write.c b/fs/read_write.c
797 index e643aec2b0ef..87acd61a0180 100644
798 --- a/fs/read_write.c
799 +++ b/fs/read_write.c
800 @@ -489,6 +489,7 @@ ssize_t vfs_read(struct file *file, char __user *buf, size_t count, loff_t *pos)
801         inc_syscr(current);
802         return ret;
803  }
804 +EXPORT_SYMBOL_GPL(vfs_read);
805  
806  static ssize_t new_sync_write(struct file *filp, const char __user *buf, size_t len, loff_t *ppos)
807  {
808 @@ -599,6 +600,7 @@ ssize_t vfs_write(struct file *file, const char __user *buf, size_t count, loff_
809         file_end_write(file);
810         return ret;
811  }
812 +EXPORT_SYMBOL_GPL(vfs_write);
813  
814  /* file_ppos returns &file->f_pos or NULL if file is stream */
815  static inline loff_t *file_ppos(struct file *file)
816 diff --git a/fs/splice.c b/fs/splice.c
817 index a21caa5c54c8..ccf9e1681eb7 100644
818 --- a/fs/splice.c
819 +++ b/fs/splice.c
820 @@ -766,6 +766,7 @@ long do_splice_from(struct pipe_inode_info *pipe, struct file *out,
821                 return warn_unsupported(out, "write");
822         return out->f_op->splice_write(pipe, out, ppos, len, flags);
823  }
824 +EXPORT_SYMBOL_GPL(do_splice_from);
825  
826  /*
827   * Attempt to initiate a splice from a file to a pipe.
828 @@ -795,6 +796,7 @@ long do_splice_to(struct file *in, loff_t *ppos,
829                 return warn_unsupported(in, "read");
830         return in->f_op->splice_read(in, ppos, pipe, len, flags);
831  }
832 +EXPORT_SYMBOL_GPL(do_splice_to);
833  
834  /**
835   * splice_direct_to_actor - splices data directly between two non-pipes
836 diff --git a/fs/xattr.c b/fs/xattr.c
837 index 5c8c5175b385..ff7e9ff774b7 100644
838 --- a/fs/xattr.c
839 +++ b/fs/xattr.c
840 @@ -384,6 +384,7 @@ vfs_getxattr_alloc(struct user_namespace *mnt_userns, struct dentry *dentry,
841         *xattr_value = value;
842         return error;
843  }
844 +EXPORT_SYMBOL_GPL(vfs_getxattr_alloc);
845  
846  ssize_t
847  __vfs_getxattr(struct dentry *dentry, struct inode *inode, const char *name,
848 diff --git a/kernel/locking/lockdep.c b/kernel/locking/lockdep.c
849 index 9d7c317899e1..ddfdeeec39d6 100644
850 --- a/kernel/locking/lockdep.c
851 +++ b/kernel/locking/lockdep.c
852 @@ -208,6 +208,7 @@ inline struct lock_class *lockdep_hlock_class(struct held_lock *hlock)
853          */
854         return lock_classes + class_idx;
855  }
856 +EXPORT_SYMBOL_GPL(lockdep_hlock_class);
857  #define hlock_class(hlock) lockdep_hlock_class(hlock)
858  
859  #ifdef CONFIG_LOCK_STAT
860 diff --git a/kernel/task_work.c b/kernel/task_work.c
861 index c59e1a49bc40..8af926a7276d 100644
862 --- a/kernel/task_work.c
863 +++ b/kernel/task_work.c
864 @@ -167,3 +167,4 @@ void task_work_run(void)
865                 } while (work);
866         }
867  }
868 +EXPORT_SYMBOL_GPL(task_work_run);
869 diff --git a/security/security.c b/security/security.c
870 index b7cf5cbfdc67..37f2d9d3d08c 100644
871 --- a/security/security.c
872 +++ b/security/security.c
873 @@ -1164,6 +1164,7 @@ int security_path_rmdir(const struct path *dir, struct dentry *dentry)
874                 return 0;
875         return call_int_hook(path_rmdir, 0, dir, dentry);
876  }
877 +EXPORT_SYMBOL_GPL(security_path_rmdir);
878  
879  int security_path_unlink(const struct path *dir, struct dentry *dentry)
880  {
881 @@ -1180,6 +1181,7 @@ int security_path_symlink(const struct path *dir, struct dentry *dentry,
882                 return 0;
883         return call_int_hook(path_symlink, 0, dir, dentry, old_name);
884  }
885 +EXPORT_SYMBOL_GPL(security_path_symlink);
886  
887  int security_path_link(struct dentry *old_dentry, const struct path *new_dir,
888                        struct dentry *new_dentry)
889 @@ -1188,6 +1190,7 @@ int security_path_link(struct dentry *old_dentry, const struct path *new_dir,
890                 return 0;
891         return call_int_hook(path_link, 0, old_dentry, new_dir, new_dentry);
892  }
893 +EXPORT_SYMBOL_GPL(security_path_link);
894  
895  int security_path_rename(const struct path *old_dir, struct dentry *old_dentry,
896                          const struct path *new_dir, struct dentry *new_dentry,
897 @@ -1215,6 +1218,7 @@ int security_path_truncate(const struct path *path)
898                 return 0;
899         return call_int_hook(path_truncate, 0, path);
900  }
901 +EXPORT_SYMBOL_GPL(security_path_truncate);
902  
903  int security_path_chmod(const struct path *path, umode_t mode)
904  {
905 @@ -1222,6 +1226,7 @@ int security_path_chmod(const struct path *path, umode_t mode)
906                 return 0;
907         return call_int_hook(path_chmod, 0, path, mode);
908  }
909 +EXPORT_SYMBOL_GPL(security_path_chmod);
910  
911  int security_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
912  {
913 @@ -1229,6 +1234,7 @@ int security_path_chown(const struct path *path, kuid_t uid, kgid_t gid)
914                 return 0;
915         return call_int_hook(path_chown, 0, path, uid, gid);
916  }
917 +EXPORT_SYMBOL_GPL(security_path_chown);
918  
919  int security_path_chroot(const struct path *path)
920  {
921 @@ -1329,6 +1335,7 @@ int security_inode_permission(struct inode *inode, int mask)
922                 return 0;
923         return call_int_hook(inode_permission, 0, inode, mask);
924  }
925 +EXPORT_SYMBOL_GPL(security_inode_permission);
926  
927  int security_inode_setattr(struct dentry *dentry, struct iattr *attr)
928  {
929 @@ -1526,6 +1533,7 @@ int security_file_permission(struct file *file, int mask)
930  
931         return fsnotify_perm(file, mask);
932  }
933 +EXPORT_SYMBOL_GPL(security_file_permission);
934  
935  int security_file_alloc(struct file *file)
936  {
937 diff -urN /usr/share/empty/Documentation/ABI/testing/debugfs-aufs linux/Documentation/ABI/testing/debugfs-aufs
938 --- /usr/share/empty/Documentation/ABI/testing/debugfs-aufs     1970-01-01 01:00:00.000000000 +0100
939 +++ linux/Documentation/ABI/testing/debugfs-aufs        2022-03-21 14:49:05.713299677 +0100
940 @@ -0,0 +1,55 @@
941 +What:          /debug/aufs/si_<id>/
942 +Date:          March 2009
943 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
944 +Description:
945 +               Under /debug/aufs, a directory named si_<id> is created
946 +               per aufs mount, where <id> is a unique id generated
947 +               internally.
948 +
949 +What:          /debug/aufs/si_<id>/plink
950 +Date:          Apr 2013
951 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
952 +Description:
953 +               It has three lines and shows the information about the
954 +               pseudo-link. The first line is a single number
955 +               representing a number of buckets. The second line is a
956 +               number of pseudo-links per buckets (separated by a
957 +               blank). The last line is a single number representing a
958 +               total number of psedo-links.
959 +               When the aufs mount option 'noplink' is specified, it
960 +               will show "1\n0\n0\n".
961 +
962 +What:          /debug/aufs/si_<id>/xib
963 +Date:          March 2009
964 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
965 +Description:
966 +               It shows the consumed blocks by xib (External Inode Number
967 +               Bitmap), its block size and file size.
968 +               When the aufs mount option 'noxino' is specified, it
969 +               will be empty. About XINO files, see the aufs manual.
970 +
971 +What:          /debug/aufs/si_<id>/xi<branch-index>
972 +Date:          March 2009
973 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
974 +Description:
975 +               It shows the consumed blocks by xino (External Inode Number
976 +               Translation Table), its link count, block size and file
977 +               size.
978 +               Due to the file size limit, there may exist multiple
979 +               xino files per branch.  In this case, "-N" is added to
980 +               the filename and it corresponds to the index of the
981 +               internal xino array.  "-0" is omitted.
982 +               When the aufs mount option 'noxino' is specified, Those
983 +               entries won't exist.  About XINO files, see the aufs
984 +               manual.
985 +
986 +What:          /debug/aufs/si_<id>/xigen
987 +Date:          March 2009
988 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
989 +Description:
990 +               It shows the consumed blocks by xigen (External Inode
991 +               Generation Table), its block size and file size.
992 +               If CONFIG_AUFS_EXPORT is disabled, this entry will not
993 +               be created.
994 +               When the aufs mount option 'noxino' is specified, it
995 +               will be empty. About XINO files, see the aufs manual.
996 diff -urN /usr/share/empty/Documentation/ABI/testing/sysfs-aufs linux/Documentation/ABI/testing/sysfs-aufs
997 --- /usr/share/empty/Documentation/ABI/testing/sysfs-aufs       1970-01-01 01:00:00.000000000 +0100
998 +++ linux/Documentation/ABI/testing/sysfs-aufs  2022-03-21 14:49:05.713299677 +0100
999 @@ -0,0 +1,31 @@
1000 +What:          /sys/fs/aufs/si_<id>/
1001 +Date:          March 2009
1002 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1003 +Description:
1004 +               Under /sys/fs/aufs, a directory named si_<id> is created
1005 +               per aufs mount, where <id> is a unique id generated
1006 +               internally.
1007 +
1008 +What:          /sys/fs/aufs/si_<id>/br<idx>
1009 +Date:          March 2009
1010 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1011 +Description:
1012 +               It shows the abolute path of a member directory (which
1013 +               is called branch) in aufs, and its permission.
1014 +
1015 +What:          /sys/fs/aufs/si_<id>/brid<idx>
1016 +Date:          July 2013
1017 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1018 +Description:
1019 +               It shows the id of a member directory (which is called
1020 +               branch) in aufs.
1021 +
1022 +What:          /sys/fs/aufs/si_<id>/xi_path
1023 +Date:          March 2009
1024 +Contact:       J. R. Okajima <hooanon05g@gmail.com>
1025 +Description:
1026 +               It shows the abolute path of XINO (External Inode Number
1027 +               Bitmap, Translation Table and Generation Table) file
1028 +               even if it is the default path.
1029 +               When the aufs mount option 'noxino' is specified, it
1030 +               will be empty. About XINO files, see the aufs manual.
1031 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/01intro.txt linux/Documentation/filesystems/aufs/design/01intro.txt
1032 --- /usr/share/empty/Documentation/filesystems/aufs/design/01intro.txt  1970-01-01 01:00:00.000000000 +0100
1033 +++ linux/Documentation/filesystems/aufs/design/01intro.txt     2022-03-21 14:49:05.713299677 +0100
1034 @@ -0,0 +1,171 @@
1035 +
1036 +# Copyright (C) 2005-2021 Junjiro R. Okajima
1037 +# 
1038 +# This program is free software; you can redistribute it and/or modify
1039 +# it under the terms of the GNU General Public License as published by
1040 +# the Free Software Foundation; either version 2 of the License, or
1041 +# (at your option) any later version.
1042 +# 
1043 +# This program is distributed in the hope that it will be useful,
1044 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1045 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1046 +# GNU General Public License for more details.
1047 +# 
1048 +# You should have received a copy of the GNU General Public License
1049 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1050 +
1051 +Introduction
1052 +----------------------------------------
1053 +
1054 +aufs [ei ju: ef es] | /ey-yoo-ef-es/ | [a u f s]
1055 +1. abbrev. for "advanced multi-layered unification filesystem".
1056 +2. abbrev. for "another unionfs".
1057 +3. abbrev. for "auf das" in German which means "on the" in English.
1058 +   Ex. "Butter aufs Brot"(G) means "butter onto bread"(E).
1059 +   But "Filesystem aufs Filesystem" is hard to understand.
1060 +4. abbrev. for "African Urban Fashion Show".
1061 +
1062 +AUFS is a filesystem with features:
1063 +- multi layered stackable unification filesystem, the member directory
1064 +  is called as a branch.
1065 +- branch permission and attribute, 'readonly', 'real-readonly',
1066 +  'readwrite', 'whiteout-able', 'link-able whiteout', etc. and their
1067 +  combination.
1068 +- internal "file copy-on-write".
1069 +- logical deletion, whiteout.
1070 +- dynamic branch manipulation, adding, deleting and changing permission.
1071 +- allow bypassing aufs, user's direct branch access.
1072 +- external inode number translation table and bitmap which maintains the
1073 +  persistent aufs inode number.
1074 +- seekable directory, including NFS readdir.
1075 +- file mapping, mmap and sharing pages.
1076 +- pseudo-link, hardlink over branches.
1077 +- loopback mounted filesystem as a branch.
1078 +- several policies to select one among multiple writable branches.
1079 +- revert a single systemcall when an error occurs in aufs.
1080 +- and more...
1081 +
1082 +
1083 +Multi Layered Stackable Unification Filesystem
1084 +----------------------------------------------------------------------
1085 +Most people already knows what it is.
1086 +It is a filesystem which unifies several directories and provides a
1087 +merged single directory. When users access a file, the access will be
1088 +passed/re-directed/converted (sorry, I am not sure which English word is
1089 +correct) to the real file on the member filesystem. The member
1090 +filesystem is called 'lower filesystem' or 'branch' and has a mode
1091 +'readonly' and 'readwrite.' And the deletion for a file on the lower
1092 +readonly branch is handled by creating 'whiteout' on the upper writable
1093 +branch.
1094 +
1095 +On LKML, there have been discussions about UnionMount (Jan Blunck,
1096 +Bharata B Rao and Valerie Aurora) and Unionfs (Erez Zadok). They took
1097 +different approaches to implement the merged-view.
1098 +The former tries putting it into VFS, and the latter implements as a
1099 +separate filesystem.
1100 +(If I misunderstand about these implementations, please let me know and
1101 +I shall correct it. Because it is a long time ago when I read their
1102 +source files last time).
1103 +
1104 +UnionMount's approach will be able to small, but may be hard to share
1105 +branches between several UnionMount since the whiteout in it is
1106 +implemented in the inode on branch filesystem and always
1107 +shared. According to Bharata's post, readdir does not seems to be
1108 +finished yet.
1109 +There are several missing features known in this implementations such as
1110 +- for users, the inode number may change silently. eg. copy-up.
1111 +- link(2) may break by copy-up.
1112 +- read(2) may get an obsoleted filedata (fstat(2) too).
1113 +- fcntl(F_SETLK) may be broken by copy-up.
1114 +- unnecessary copy-up may happen, for example mmap(MAP_PRIVATE) after
1115 +  open(O_RDWR).
1116 +
1117 +In linux-3.18, "overlay" filesystem (formerly known as "overlayfs") was
1118 +merged into mainline. This is another implementation of UnionMount as a
1119 +separated filesystem. All the limitations and known problems which
1120 +UnionMount are equally inherited to "overlay" filesystem.
1121 +
1122 +Unionfs has a longer history. When I started implementing a stackable
1123 +filesystem (Aug 2005), it already existed. It has virtual super_block,
1124 +inode, dentry and file objects and they have an array pointing lower
1125 +same kind objects. After contributing many patches for Unionfs, I
1126 +re-started my project AUFS (Jun 2006).
1127 +
1128 +In AUFS, the structure of filesystem resembles to Unionfs, but I
1129 +implemented my own ideas, approaches and enhancements and it became
1130 +totally different one.
1131 +
1132 +Comparing DM snapshot and fs based implementation
1133 +- the number of bytes to be copied between devices is much smaller.
1134 +- the type of filesystem must be one and only.
1135 +- the fs must be writable, no readonly fs, even for the lower original
1136 +  device. so the compression fs will not be usable. but if we use
1137 +  loopback mount, we may address this issue.
1138 +  for instance,
1139 +       mount /cdrom/squashfs.img /sq
1140 +       losetup /sq/ext2.img
1141 +       losetup /somewhere/cow
1142 +       dmsetup "snapshot /dev/loop0 /dev/loop1 ..."
1143 +- it will be difficult (or needs more operations) to extract the
1144 +  difference between the original device and COW.
1145 +- DM snapshot-merge may help a lot when users try merging. in the
1146 +  fs-layer union, users will use rsync(1).
1147 +
1148 +You may want to read my old paper "Filesystems in LiveCD"
1149 +(http://aufs.sourceforge.net/aufs2/report/sq/sq.pdf).
1150 +
1151 +
1152 +Several characters/aspects/persona of aufs
1153 +----------------------------------------------------------------------
1154 +
1155 +Aufs has several characters, aspects or persona.
1156 +1. a filesystem, callee of VFS helper
1157 +2. sub-VFS, caller of VFS helper for branches
1158 +3. a virtual filesystem which maintains persistent inode number
1159 +4. reader/writer of files on branches such like an application
1160 +
1161 +1. Callee of VFS Helper
1162 +As an ordinary linux filesystem, aufs is a callee of VFS. For instance,
1163 +unlink(2) from an application reaches sys_unlink() kernel function and
1164 +then vfs_unlink() is called. vfs_unlink() is one of VFS helper and it
1165 +calls filesystem specific unlink operation. Actually aufs implements the
1166 +unlink operation but it behaves like a redirector.
1167 +
1168 +2. Caller of VFS Helper for Branches
1169 +aufs_unlink() passes the unlink request to the branch filesystem as if
1170 +it were called from VFS. So the called unlink operation of the branch
1171 +filesystem acts as usual. As a caller of VFS helper, aufs should handle
1172 +every necessary pre/post operation for the branch filesystem.
1173 +- acquire the lock for the parent dir on a branch
1174 +- lookup in a branch
1175 +- revalidate dentry on a branch
1176 +- mnt_want_write() for a branch
1177 +- vfs_unlink() for a branch
1178 +- mnt_drop_write() for a branch
1179 +- release the lock on a branch
1180 +
1181 +3. Persistent Inode Number
1182 +One of the most important issue for a filesystem is to maintain inode
1183 +numbers. This is particularly important to support exporting a
1184 +filesystem via NFS. Aufs is a virtual filesystem which doesn't have a
1185 +backend block device for its own. But some storage is necessary to
1186 +keep and maintain the inode numbers. It may be a large space and may not
1187 +suit to keep in memory. Aufs rents some space from its first writable
1188 +branch filesystem (by default) and creates file(s) on it. These files
1189 +are created by aufs internally and removed soon (currently) keeping
1190 +opened.
1191 +Note: Because these files are removed, they are totally gone after
1192 +      unmounting aufs. It means the inode numbers are not persistent
1193 +      across unmount or reboot. I have a plan to make them really
1194 +      persistent which will be important for aufs on NFS server.
1195 +
1196 +4. Read/Write Files Internally (copy-on-write)
1197 +Because a branch can be readonly, when you write a file on it, aufs will
1198 +"copy-up" it to the upper writable branch internally. And then write the
1199 +originally requested thing to the file. Generally kernel doesn't
1200 +open/read/write file actively. In aufs, even a single write may cause a
1201 +internal "file copy". This behaviour is very similar to cp(1) command.
1202 +
1203 +Some people may think it is better to pass such work to user space
1204 +helper, instead of doing in kernel space. Actually I am still thinking
1205 +about it. But currently I have implemented it in kernel space.
1206 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/02struct.txt linux/Documentation/filesystems/aufs/design/02struct.txt
1207 --- /usr/share/empty/Documentation/filesystems/aufs/design/02struct.txt 1970-01-01 01:00:00.000000000 +0100
1208 +++ linux/Documentation/filesystems/aufs/design/02struct.txt    2022-03-21 14:49:05.713299677 +0100
1209 @@ -0,0 +1,258 @@
1210 +
1211 +# Copyright (C) 2005-2021 Junjiro R. Okajima
1212 +# 
1213 +# This program is free software; you can redistribute it and/or modify
1214 +# it under the terms of the GNU General Public License as published by
1215 +# the Free Software Foundation; either version 2 of the License, or
1216 +# (at your option) any later version.
1217 +# 
1218 +# This program is distributed in the hope that it will be useful,
1219 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1220 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1221 +# GNU General Public License for more details.
1222 +# 
1223 +# You should have received a copy of the GNU General Public License
1224 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1225 +
1226 +Basic Aufs Internal Structure
1227 +
1228 +Superblock/Inode/Dentry/File Objects
1229 +----------------------------------------------------------------------
1230 +As like an ordinary filesystem, aufs has its own
1231 +superblock/inode/dentry/file objects. All these objects have a
1232 +dynamically allocated array and store the same kind of pointers to the
1233 +lower filesystem, branch.
1234 +For example, when you build a union with one readwrite branch and one
1235 +readonly, mounted /au, /rw and /ro respectively.
1236 +- /au = /rw + /ro
1237 +- /ro/fileA exists but /rw/fileA
1238 +
1239 +Aufs lookup operation finds /ro/fileA and gets dentry for that. These
1240 +pointers are stored in a aufs dentry. The array in aufs dentry will be,
1241 +- [0] = NULL (because /rw/fileA doesn't exist)
1242 +- [1] = /ro/fileA
1243 +
1244 +This style of an array is essentially same to the aufs
1245 +superblock/inode/dentry/file objects.
1246 +
1247 +Because aufs supports manipulating branches, ie. add/delete/change
1248 +branches dynamically, these objects has its own generation. When
1249 +branches are changed, the generation in aufs superblock is
1250 +incremented. And a generation in other object are compared when it is
1251 +accessed. When a generation in other objects are obsoleted, aufs
1252 +refreshes the internal array.
1253 +
1254 +
1255 +Superblock
1256 +----------------------------------------------------------------------
1257 +Additionally aufs superblock has some data for policies to select one
1258 +among multiple writable branches, XIB files, pseudo-links and kobject.
1259 +See below in detail.
1260 +About the policies which supports copy-down a directory, see
1261 +wbr_policy.txt too.
1262 +
1263 +
1264 +Branch and XINO(External Inode Number Translation Table)
1265 +----------------------------------------------------------------------
1266 +Every branch has its own xino (external inode number translation table)
1267 +file. The xino file is created and unlinked by aufs internally. When two
1268 +members of a union exist on the same filesystem, they share the single
1269 +xino file.
1270 +The struct of a xino file is simple, just a sequence of aufs inode
1271 +numbers which is indexed by the lower inode number.
1272 +In the above sample, assume the inode number of /ro/fileA is i111 and
1273 +aufs assigns the inode number i999 for fileA. Then aufs writes 999 as
1274 +4(8) bytes at 111 * 4(8) bytes offset in the xino file.
1275 +
1276 +When the inode numbers are not contiguous, the xino file will be sparse
1277 +which has a hole in it and doesn't consume as much disk space as it
1278 +might appear. If your branch filesystem consumes disk space for such
1279 +holes, then you should specify 'xino=' option at mounting aufs.
1280 +
1281 +Aufs has a mount option to free the disk blocks for such holes in XINO
1282 +files on tmpfs or ramdisk. But it is not so effective actually. If you
1283 +meet a problem of disk shortage due to XINO files, then you should try
1284 +"tmpfs-ino.patch" (and "vfs-ino.patch" too) in aufs4-standalone.git.
1285 +The patch localizes the assignment inumbers per tmpfs-mount and avoid
1286 +the holes in XINO files.
1287 +
1288 +Also a writable branch has three kinds of "whiteout bases". All these
1289 +are existed when the branch is joined to aufs, and their names are
1290 +whiteout-ed doubly, so that users will never see their names in aufs
1291 +hierarchy.
1292 +1. a regular file which will be hardlinked to all whiteouts.
1293 +2. a directory to store a pseudo-link.
1294 +3. a directory to store an "orphan"-ed file temporary.
1295 +
1296 +1. Whiteout Base
1297 +   When you remove a file on a readonly branch, aufs handles it as a
1298 +   logical deletion and creates a whiteout on the upper writable branch
1299 +   as a hardlink of this file in order not to consume inode on the
1300 +   writable branch.
1301 +2. Pseudo-link Dir
1302 +   See below, Pseudo-link.
1303 +3. Step-Parent Dir
1304 +   When "fileC" exists on the lower readonly branch only and it is
1305 +   opened and removed with its parent dir, and then user writes
1306 +   something into it, then aufs copies-up fileC to this
1307 +   directory. Because there is no other dir to store fileC. After
1308 +   creating a file under this dir, the file is unlinked.
1309 +
1310 +Because aufs supports manipulating branches, ie. add/delete/change
1311 +dynamically, a branch has its own id. When the branch order changes,
1312 +aufs finds the new index by searching the branch id.
1313 +
1314 +
1315 +Pseudo-link
1316 +----------------------------------------------------------------------
1317 +Assume "fileA" exists on the lower readonly branch only and it is
1318 +hardlinked to "fileB" on the branch. When you write something to fileA,
1319 +aufs copies-up it to the upper writable branch. Additionally aufs
1320 +creates a hardlink under the Pseudo-link Directory of the writable
1321 +branch. The inode of a pseudo-link is kept in aufs super_block as a
1322 +simple list. If fileB is read after unlinking fileA, aufs returns
1323 +filedata from the pseudo-link instead of the lower readonly
1324 +branch. Because the pseudo-link is based upon the inode, to keep the
1325 +inode number by xino (see above) is essentially necessary.
1326 +
1327 +All the hardlinks under the Pseudo-link Directory of the writable branch
1328 +should be restored in a proper location later. Aufs provides a utility
1329 +to do this. The userspace helpers executed at remounting and unmounting
1330 +aufs by default.
1331 +During this utility is running, it puts aufs into the pseudo-link
1332 +maintenance mode. In this mode, only the process which began the
1333 +maintenance mode (and its child processes) is allowed to operate in
1334 +aufs. Some other processes which are not related to the pseudo-link will
1335 +be allowed to run too, but the rest have to return an error or wait
1336 +until the maintenance mode ends. If a process already acquires an inode
1337 +mutex (in VFS), it has to return an error.
1338 +
1339 +
1340 +XIB(external inode number bitmap)
1341 +----------------------------------------------------------------------
1342 +Addition to the xino file per a branch, aufs has an external inode number
1343 +bitmap in a superblock object. It is also an internal file such like a
1344 +xino file.
1345 +It is a simple bitmap to mark whether the aufs inode number is in-use or
1346 +not.
1347 +To reduce the file I/O, aufs prepares a single memory page to cache xib.
1348 +
1349 +As well as XINO files, aufs has a feature to truncate/refresh XIB to
1350 +reduce the number of consumed disk blocks for these files.
1351 +
1352 +
1353 +Virtual or Vertical Dir, and Readdir in Userspace
1354 +----------------------------------------------------------------------
1355 +In order to support multiple layers (branches), aufs readdir operation
1356 +constructs a virtual dir block on memory. For readdir, aufs calls
1357 +vfs_readdir() internally for each dir on branches, merges their entries
1358 +with eliminating the whiteout-ed ones, and sets it to file (dir)
1359 +object. So the file object has its entry list until it is closed. The
1360 +entry list will be updated when the file position is zero and becomes
1361 +obsoleted. This decision is made in aufs automatically.
1362 +
1363 +The dynamically allocated memory block for the name of entries has a
1364 +unit of 512 bytes (by default) and stores the names contiguously (no
1365 +padding). Another block for each entry is handled by kmem_cache too.
1366 +During building dir blocks, aufs creates hash list and judging whether
1367 +the entry is whiteouted by its upper branch or already listed.
1368 +The merged result is cached in the corresponding inode object and
1369 +maintained by a customizable life-time option.
1370 +
1371 +Some people may call it can be a security hole or invite DoS attack
1372 +since the opened and once readdir-ed dir (file object) holds its entry
1373 +list and becomes a pressure for system memory. But I'd say it is similar
1374 +to files under /proc or /sys. The virtual files in them also holds a
1375 +memory page (generally) while they are opened. When an idea to reduce
1376 +memory for them is introduced, it will be applied to aufs too.
1377 +For those who really hate this situation, I've developed readdir(3)
1378 +library which operates this merging in userspace. You just need to set
1379 +LD_PRELOAD environment variable, and aufs will not consume no memory in
1380 +kernel space for readdir(3).
1381 +
1382 +
1383 +Workqueue
1384 +----------------------------------------------------------------------
1385 +Aufs sometimes requires privilege access to a branch. For instance,
1386 +in copy-up/down operation. When a user process is going to make changes
1387 +to a file which exists in the lower readonly branch only, and the mode
1388 +of one of ancestor directories may not be writable by a user
1389 +process. Here aufs copy-up the file with its ancestors and they may
1390 +require privilege to set its owner/group/mode/etc.
1391 +This is a typical case of a application character of aufs (see
1392 +Introduction).
1393 +
1394 +Aufs uses workqueue synchronously for this case. It creates its own
1395 +workqueue. The workqueue is a kernel thread and has privilege. Aufs
1396 +passes the request to call mkdir or write (for example), and wait for
1397 +its completion. This approach solves a problem of a signal handler
1398 +simply.
1399 +If aufs didn't adopt the workqueue and changed the privilege of the
1400 +process, then the process may receive the unexpected SIGXFSZ or other
1401 +signals.
1402 +
1403 +Also aufs uses the system global workqueue ("events" kernel thread) too
1404 +for asynchronous tasks, such like handling inotify/fsnotify, re-creating a
1405 +whiteout base and etc. This is unrelated to a privilege.
1406 +Most of aufs operation tries acquiring a rw_semaphore for aufs
1407 +superblock at the beginning, at the same time waits for the completion
1408 +of all queued asynchronous tasks.
1409 +
1410 +
1411 +Whiteout
1412 +----------------------------------------------------------------------
1413 +The whiteout in aufs is very similar to Unionfs's. That is represented
1414 +by its filename. UnionMount takes an approach of a file mode, but I am
1415 +afraid several utilities (find(1) or something) will have to support it.
1416 +
1417 +Basically the whiteout represents "logical deletion" which stops aufs to
1418 +lookup further, but also it represents "dir is opaque" which also stop
1419 +further lookup.
1420 +
1421 +In aufs, rmdir(2) and rename(2) for dir uses whiteout alternatively.
1422 +In order to make several functions in a single systemcall to be
1423 +revertible, aufs adopts an approach to rename a directory to a temporary
1424 +unique whiteouted name.
1425 +For example, in rename(2) dir where the target dir already existed, aufs
1426 +renames the target dir to a temporary unique whiteouted name before the
1427 +actual rename on a branch, and then handles other actions (make it opaque,
1428 +update the attributes, etc). If an error happens in these actions, aufs
1429 +simply renames the whiteouted name back and returns an error. If all are
1430 +succeeded, aufs registers a function to remove the whiteouted unique
1431 +temporary name completely and asynchronously to the system global
1432 +workqueue.
1433 +
1434 +
1435 +Copy-up
1436 +----------------------------------------------------------------------
1437 +It is a well-known feature or concept.
1438 +When user modifies a file on a readonly branch, aufs operate "copy-up"
1439 +internally and makes change to the new file on the upper writable branch.
1440 +When the trigger systemcall does not update the timestamps of the parent
1441 +dir, aufs reverts it after copy-up.
1442 +
1443 +
1444 +Move-down (aufs3.9 and later)
1445 +----------------------------------------------------------------------
1446 +"Copy-up" is one of the essential feature in aufs. It copies a file from
1447 +the lower readonly branch to the upper writable branch when a user
1448 +changes something about the file.
1449 +"Move-down" is an opposite action of copy-up. Basically this action is
1450 +ran manually instead of automatically and internally.
1451 +For desgin and implementation, aufs has to consider these issues.
1452 +- whiteout for the file may exist on the lower branch.
1453 +- ancestor directories may not exist on the lower branch.
1454 +- diropq for the ancestor directories may exist on the upper branch.
1455 +- free space on the lower branch will reduce.
1456 +- another access to the file may happen during moving-down, including
1457 +  UDBA (see "Revalidate Dentry and UDBA").
1458 +- the file should not be hard-linked nor pseudo-linked. they should be
1459 +  handled by auplink utility later.
1460 +
1461 +Sometimes users want to move-down a file from the upper writable branch
1462 +to the lower readonly or writable branch. For instance,
1463 +- the free space of the upper writable branch is going to run out.
1464 +- create a new intermediate branch between the upper and lower branch.
1465 +- etc.
1466 +
1467 +For this purpose, use "aumvdown" command in aufs-util.git.
1468 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/03atomic_open.txt linux/Documentation/filesystems/aufs/design/03atomic_open.txt
1469 --- /usr/share/empty/Documentation/filesystems/aufs/design/03atomic_open.txt    1970-01-01 01:00:00.000000000 +0100
1470 +++ linux/Documentation/filesystems/aufs/design/03atomic_open.txt       2022-03-21 14:49:05.713299677 +0100
1471 @@ -0,0 +1,85 @@
1472 +
1473 +# Copyright (C) 2015-2021 Junjiro R. Okajima
1474 +# 
1475 +# This program is free software; you can redistribute it and/or modify
1476 +# it under the terms of the GNU General Public License as published by
1477 +# the Free Software Foundation; either version 2 of the License, or
1478 +# (at your option) any later version.
1479 +# 
1480 +# This program is distributed in the hope that it will be useful,
1481 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1482 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1483 +# GNU General Public License for more details.
1484 +# 
1485 +# You should have received a copy of the GNU General Public License
1486 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1487 +
1488 +Support for a branch who has its ->atomic_open()
1489 +----------------------------------------------------------------------
1490 +The filesystems who implement its ->atomic_open() are not majority. For
1491 +example NFSv4 does, and aufs should call NFSv4 ->atomic_open,
1492 +particularly for open(O_CREAT|O_EXCL, 0400) case. Other than
1493 +->atomic_open(), NFSv4 returns an error for this open(2). While I am not
1494 +sure whether all filesystems who have ->atomic_open() behave like this,
1495 +but NFSv4 surely returns the error.
1496 +
1497 +In order to support ->atomic_open() for aufs, there are a few
1498 +approaches.
1499 +
1500 +A. Introduce aufs_atomic_open()
1501 +   - calls one of VFS:do_last(), lookup_open() or atomic_open() for
1502 +     branch fs.
1503 +B. Introduce aufs_atomic_open() calling create, open and chmod. this is
1504 +   an aufs user Pip Cet's approach
1505 +   - calls aufs_create(), VFS finish_open() and notify_change().
1506 +   - pass fake-mode to finish_open(), and then correct the mode by
1507 +     notify_change().
1508 +C. Extend aufs_open() to call branch fs's ->atomic_open()
1509 +   - no aufs_atomic_open().
1510 +   - aufs_lookup() registers the TID to an aufs internal object.
1511 +   - aufs_create() does nothing when the matching TID is registered, but
1512 +     registers the mode.
1513 +   - aufs_open() calls branch fs's ->atomic_open() when the matching
1514 +     TID is registered.
1515 +D. Extend aufs_open() to re-try branch fs's ->open() with superuser's
1516 +   credential
1517 +   - no aufs_atomic_open().
1518 +   - aufs_create() registers the TID to an internal object. this info
1519 +     represents "this process created this file just now."
1520 +   - when aufs gets EACCES from branch fs's ->open(), then confirm the
1521 +     registered TID and re-try open() with superuser's credential.
1522 +
1523 +Pros and cons for each approach.
1524 +
1525 +A.
1526 +   - straightforward but highly depends upon VFS internal.
1527 +   - the atomic behavaiour is kept.
1528 +   - some of parameters such as nameidata are hard to reproduce for
1529 +     branch fs.
1530 +   - large overhead.
1531 +B.
1532 +   - easy to implement.
1533 +   - the atomic behavaiour is lost.
1534 +C.
1535 +   - the atomic behavaiour is kept.
1536 +   - dirty and tricky.
1537 +   - VFS checks whether the file is created correctly after calling
1538 +     ->create(), which means this approach doesn't work.
1539 +D.
1540 +   - easy to implement.
1541 +   - the atomic behavaiour is lost.
1542 +   - to open a file with superuser's credential and give it to a user
1543 +     process is a bad idea, since the file object keeps the credential
1544 +     in it. It may affect LSM or something. This approach doesn't work
1545 +     either.
1546 +
1547 +The approach A is ideal, but it hard to implement. So here is a
1548 +variation of A, which is to be implemented.
1549 +
1550 +A-1. Introduce aufs_atomic_open()
1551 +     - calls branch fs ->atomic_open() if exists. otherwise calls
1552 +       vfs_create() and finish_open().
1553 +     - the demerit is that the several checks after branch fs
1554 +       ->atomic_open() are lost. in the ordinary case, the checks are
1555 +       done by VFS:do_last(), lookup_open() and atomic_open(). some can
1556 +       be implemented in aufs, but not all I am afraid.
1557 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/03lookup.txt linux/Documentation/filesystems/aufs/design/03lookup.txt
1558 --- /usr/share/empty/Documentation/filesystems/aufs/design/03lookup.txt 1970-01-01 01:00:00.000000000 +0100
1559 +++ linux/Documentation/filesystems/aufs/design/03lookup.txt    2022-03-21 14:49:05.713299677 +0100
1560 @@ -0,0 +1,113 @@
1561 +
1562 +# Copyright (C) 2005-2021 Junjiro R. Okajima
1563 +# 
1564 +# This program is free software; you can redistribute it and/or modify
1565 +# it under the terms of the GNU General Public License as published by
1566 +# the Free Software Foundation; either version 2 of the License, or
1567 +# (at your option) any later version.
1568 +# 
1569 +# This program is distributed in the hope that it will be useful,
1570 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1571 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1572 +# GNU General Public License for more details.
1573 +# 
1574 +# You should have received a copy of the GNU General Public License
1575 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1576 +
1577 +Lookup in a Branch
1578 +----------------------------------------------------------------------
1579 +Since aufs has a character of sub-VFS (see Introduction), it operates
1580 +lookup for branches as VFS does. It may be a heavy work. But almost all
1581 +lookup operation in aufs is the simplest case, ie. lookup only an entry
1582 +directly connected to its parent. Digging down the directory hierarchy
1583 +is unnecessary. VFS has a function lookup_one_len() for that use, and
1584 +aufs calls it.
1585 +
1586 +When a branch is a remote filesystem, aufs basically relies upon its
1587 +->d_revalidate(), also aufs forces the hardest revalidate tests for
1588 +them.
1589 +For d_revalidate, aufs implements three levels of revalidate tests. See
1590 +"Revalidate Dentry and UDBA" in detail.
1591 +
1592 +
1593 +Test Only the Highest One for the Directory Permission (dirperm1 option)
1594 +----------------------------------------------------------------------
1595 +Let's try case study.
1596 +- aufs has two branches, upper readwrite and lower readonly.
1597 +  /au = /rw + /ro
1598 +- "dirA" exists under /ro, but /rw. and its mode is 0700.
1599 +- user invoked "chmod a+rx /au/dirA"
1600 +- the internal copy-up is activated and "/rw/dirA" is created and its
1601 +  permission bits are set to world readable.
1602 +- then "/au/dirA" becomes world readable?
1603 +
1604 +In this case, /ro/dirA is still 0700 since it exists in readonly branch,
1605 +or it may be a natively readonly filesystem. If aufs respects the lower
1606 +branch, it should not respond readdir request from other users. But user
1607 +allowed it by chmod. Should really aufs rejects showing the entries
1608 +under /ro/dirA?
1609 +
1610 +To be honest, I don't have a good solution for this case. So aufs
1611 +implements 'dirperm1' and 'nodirperm1' mount options, and leave it to
1612 +users.
1613 +When dirperm1 is specified, aufs checks only the highest one for the
1614 +directory permission, and shows the entries. Otherwise, as usual, checks
1615 +every dir existing on all branches and rejects the request.
1616 +
1617 +As a side effect, dirperm1 option improves the performance of aufs
1618 +because the number of permission check is reduced when the number of
1619 +branch is many.
1620 +
1621 +
1622 +Revalidate Dentry and UDBA (User's Direct Branch Access)
1623 +----------------------------------------------------------------------
1624 +Generally VFS helpers re-validate a dentry as a part of lookup.
1625 +0. digging down the directory hierarchy.
1626 +1. lock the parent dir by its i_mutex.
1627 +2. lookup the final (child) entry.
1628 +3. revalidate it.
1629 +4. call the actual operation (create, unlink, etc.)
1630 +5. unlock the parent dir
1631 +
1632 +If the filesystem implements its ->d_revalidate() (step 3), then it is
1633 +called. Actually aufs implements it and checks the dentry on a branch is
1634 +still valid.
1635 +But it is not enough. Because aufs has to release the lock for the
1636 +parent dir on a branch at the end of ->lookup() (step 2) and
1637 +->d_revalidate() (step 3) while the i_mutex of the aufs dir is still
1638 +held by VFS.
1639 +If the file on a branch is changed directly, eg. bypassing aufs, after
1640 +aufs released the lock, then the subsequent operation may cause
1641 +something unpleasant result.
1642 +
1643 +This situation is a result of VFS architecture, ->lookup() and
1644 +->d_revalidate() is separated. But I never say it is wrong. It is a good
1645 +design from VFS's point of view. It is just not suitable for sub-VFS
1646 +character in aufs.
1647 +
1648 +Aufs supports such case by three level of revalidation which is
1649 +selectable by user.
1650 +1. Simple Revalidate
1651 +   Addition to the native flow in VFS's, confirm the child-parent
1652 +   relationship on the branch just after locking the parent dir on the
1653 +   branch in the "actual operation" (step 4). When this validation
1654 +   fails, aufs returns EBUSY. ->d_revalidate() (step 3) in aufs still
1655 +   checks the validation of the dentry on branches.
1656 +2. Monitor Changes Internally by Inotify/Fsnotify
1657 +   Addition to above, in the "actual operation" (step 4) aufs re-lookup
1658 +   the dentry on the branch, and returns EBUSY if it finds different
1659 +   dentry.
1660 +   Additionally, aufs sets the inotify/fsnotify watch for every dir on branches
1661 +   during it is in cache. When the event is notified, aufs registers a
1662 +   function to kernel 'events' thread by schedule_work(). And the
1663 +   function sets some special status to the cached aufs dentry and inode
1664 +   private data. If they are not cached, then aufs has nothing to
1665 +   do. When the same file is accessed through aufs (step 0-3) later,
1666 +   aufs will detect the status and refresh all necessary data.
1667 +   In this mode, aufs has to ignore the event which is fired by aufs
1668 +   itself.
1669 +3. No Extra Validation
1670 +   This is the simplest test and doesn't add any additional revalidation
1671 +   test, and skip the revalidation in step 4. It is useful and improves
1672 +   aufs performance when system surely hide the aufs branches from user,
1673 +   by over-mounting something (or another method).
1674 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/04branch.txt linux/Documentation/filesystems/aufs/design/04branch.txt
1675 --- /usr/share/empty/Documentation/filesystems/aufs/design/04branch.txt 1970-01-01 01:00:00.000000000 +0100
1676 +++ linux/Documentation/filesystems/aufs/design/04branch.txt    2022-03-21 14:49:05.713299677 +0100
1677 @@ -0,0 +1,74 @@
1678 +
1679 +# Copyright (C) 2005-2021 Junjiro R. Okajima
1680 +# 
1681 +# This program is free software; you can redistribute it and/or modify
1682 +# it under the terms of the GNU General Public License as published by
1683 +# the Free Software Foundation; either version 2 of the License, or
1684 +# (at your option) any later version.
1685 +# 
1686 +# This program is distributed in the hope that it will be useful,
1687 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1688 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1689 +# GNU General Public License for more details.
1690 +# 
1691 +# You should have received a copy of the GNU General Public License
1692 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1693 +
1694 +Branch Manipulation
1695 +
1696 +Since aufs supports dynamic branch manipulation, ie. add/remove a branch
1697 +and changing its permission/attribute, there are a lot of works to do.
1698 +
1699 +
1700 +Add a Branch
1701 +----------------------------------------------------------------------
1702 +o Confirm the adding dir exists outside of aufs, including loopback
1703 +  mount, and its various attributes.
1704 +o Initialize the xino file and whiteout bases if necessary.
1705 +  See struct.txt.
1706 +
1707 +o Check the owner/group/mode of the directory
1708 +  When the owner/group/mode of the adding directory differs from the
1709 +  existing branch, aufs issues a warning because it may impose a
1710 +  security risk.
1711 +  For example, when a upper writable branch has a world writable empty
1712 +  top directory, a malicious user can create any files on the writable
1713 +  branch directly, like copy-up and modify manually. If something like
1714 +  /etc/{passwd,shadow} exists on the lower readonly branch but the upper
1715 +  writable branch, and the writable branch is world-writable, then a
1716 +  malicious guy may create /etc/passwd on the writable branch directly
1717 +  and the infected file will be valid in aufs.
1718 +  I am afraid it can be a security issue, but aufs can do nothing except
1719 +  producing a warning.
1720 +
1721 +
1722 +Delete a Branch
1723 +----------------------------------------------------------------------
1724 +o Confirm the deleting branch is not busy
1725 +  To be general, there is one merit to adopt "remount" interface to
1726 +  manipulate branches. It is to discard caches. At deleting a branch,
1727 +  aufs checks the still cached (and connected) dentries and inodes. If
1728 +  there are any, then they are all in-use. An inode without its
1729 +  corresponding dentry can be alive alone (for example, inotify/fsnotify case).
1730 +
1731 +  For the cached one, aufs checks whether the same named entry exists on
1732 +  other branches.
1733 +  If the cached one is a directory, because aufs provides a merged view
1734 +  to users, as long as one dir is left on any branch aufs can show the
1735 +  dir to users. In this case, the branch can be removed from aufs.
1736 +  Otherwise aufs rejects deleting the branch.
1737 +
1738 +  If any file on the deleting branch is opened by aufs, then aufs
1739 +  rejects deleting.
1740 +
1741 +
1742 +Modify the Permission of a Branch
1743 +----------------------------------------------------------------------
1744 +o Re-initialize or remove the xino file and whiteout bases if necessary.
1745 +  See struct.txt.
1746 +
1747 +o rw --> ro: Confirm the modifying branch is not busy
1748 +  Aufs rejects the request if any of these conditions are true.
1749 +  - a file on the branch is mmap-ed.
1750 +  - a regular file on the branch is opened for write and there is no
1751 +    same named entry on the upper branch.
1752 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/05wbr_policy.txt linux/Documentation/filesystems/aufs/design/05wbr_policy.txt
1753 --- /usr/share/empty/Documentation/filesystems/aufs/design/05wbr_policy.txt     1970-01-01 01:00:00.000000000 +0100
1754 +++ linux/Documentation/filesystems/aufs/design/05wbr_policy.txt        2022-03-21 14:49:05.713299677 +0100
1755 @@ -0,0 +1,64 @@
1756 +
1757 +# Copyright (C) 2005-2021 Junjiro R. Okajima
1758 +# 
1759 +# This program is free software; you can redistribute it and/or modify
1760 +# it under the terms of the GNU General Public License as published by
1761 +# the Free Software Foundation; either version 2 of the License, or
1762 +# (at your option) any later version.
1763 +# 
1764 +# This program is distributed in the hope that it will be useful,
1765 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1766 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1767 +# GNU General Public License for more details.
1768 +# 
1769 +# You should have received a copy of the GNU General Public License
1770 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1771 +
1772 +Policies to Select One among Multiple Writable Branches
1773 +----------------------------------------------------------------------
1774 +When the number of writable branch is more than one, aufs has to decide
1775 +the target branch for file creation or copy-up. By default, the highest
1776 +writable branch which has the parent (or ancestor) dir of the target
1777 +file is chosen (top-down-parent policy).
1778 +By user's request, aufs implements some other policies to select the
1779 +writable branch, for file creation several policies, round-robin,
1780 +most-free-space, and other policies. For copy-up, top-down-parent,
1781 +bottom-up-parent, bottom-up and others.
1782 +
1783 +As expected, the round-robin policy selects the branch in circular. When
1784 +you have two writable branches and creates 10 new files, 5 files will be
1785 +created for each branch. mkdir(2) systemcall is an exception. When you
1786 +create 10 new directories, all will be created on the same branch.
1787 +And the most-free-space policy selects the one which has most free
1788 +space among the writable branches. The amount of free space will be
1789 +checked by aufs internally, and users can specify its time interval.
1790 +
1791 +The policies for copy-up is more simple,
1792 +top-down-parent is equivalent to the same named on in create policy,
1793 +bottom-up-parent selects the writable branch where the parent dir
1794 +exists and the nearest upper one from the copyup-source,
1795 +bottom-up selects the nearest upper writable branch from the
1796 +copyup-source, regardless the existence of the parent dir.
1797 +
1798 +There are some rules or exceptions to apply these policies.
1799 +- If there is a readonly branch above the policy-selected branch and
1800 +  the parent dir is marked as opaque (a variation of whiteout), or the
1801 +  target (creating) file is whiteout-ed on the upper readonly branch,
1802 +  then the result of the policy is ignored and the target file will be
1803 +  created on the nearest upper writable branch than the readonly branch.
1804 +- If there is a writable branch above the policy-selected branch and
1805 +  the parent dir is marked as opaque or the target file is whiteouted
1806 +  on the branch, then the result of the policy is ignored and the target
1807 +  file will be created on the highest one among the upper writable
1808 +  branches who has diropq or whiteout. In case of whiteout, aufs removes
1809 +  it as usual.
1810 +- link(2) and rename(2) systemcalls are exceptions in every policy.
1811 +  They try selecting the branch where the source exists as possible
1812 +  since copyup a large file will take long time. If it can't be,
1813 +  ie. the branch where the source exists is readonly, then they will
1814 +  follow the copyup policy.
1815 +- There is an exception for rename(2) when the target exists.
1816 +  If the rename target exists, aufs compares the index of the branches
1817 +  where the source and the target exists and selects the higher
1818 +  one. If the selected branch is readonly, then aufs follows the
1819 +  copyup policy.
1820 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.dot linux/Documentation/filesystems/aufs/design/06dirren.dot
1821 --- /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.dot 1970-01-01 01:00:00.000000000 +0100
1822 +++ linux/Documentation/filesystems/aufs/design/06dirren.dot    2022-03-21 14:49:05.713299677 +0100
1823 @@ -0,0 +1,31 @@
1824 +
1825 +// to view this graph, run dot(1) command in GRAPHVIZ.
1826 +
1827 +digraph G {
1828 +node [shape=box];
1829 +whinfo [label="detailed info file\n(lower_brid_root-hinum, h_inum, namelen, old name)"];
1830 +
1831 +node [shape=oval];
1832 +
1833 +aufs_rename -> whinfo [label="store/remove"];
1834 +
1835 +node [shape=oval];
1836 +inode_list [label="h_inum list in branch\ncache"];
1837 +
1838 +node [shape=box];
1839 +whinode [label="h_inum list file"];
1840 +
1841 +node [shape=oval];
1842 +brmgmt [label="br_add/del/mod/umount"];
1843 +
1844 +brmgmt -> inode_list [label="create/remove"];
1845 +brmgmt -> whinode [label="load/store"];
1846 +
1847 +inode_list -> whinode [style=dashed,dir=both];
1848 +
1849 +aufs_rename -> inode_list [label="add/del"];
1850 +
1851 +aufs_lookup -> inode_list [label="search"];
1852 +
1853 +aufs_lookup -> whinfo [label="load/remove"];
1854 +}
1855 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.txt linux/Documentation/filesystems/aufs/design/06dirren.txt
1856 --- /usr/share/empty/Documentation/filesystems/aufs/design/06dirren.txt 1970-01-01 01:00:00.000000000 +0100
1857 +++ linux/Documentation/filesystems/aufs/design/06dirren.txt    2022-03-21 14:49:05.713299677 +0100
1858 @@ -0,0 +1,102 @@
1859 +
1860 +# Copyright (C) 2017-2021 Junjiro R. Okajima
1861 +#
1862 +# This program is free software; you can redistribute it and/or modify
1863 +# it under the terms of the GNU General Public License as published by
1864 +# the Free Software Foundation; either version 2 of the License, or
1865 +# (at your option) any later version.
1866 +#
1867 +# This program is distributed in the hope that it will be useful,
1868 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1869 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1870 +# GNU General Public License for more details.
1871 +#
1872 +# You should have received a copy of the GNU General Public License
1873 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
1874 +
1875 +Special handling for renaming a directory (DIRREN)
1876 +----------------------------------------------------------------------
1877 +First, let's assume we have a simple usecase.
1878 +
1879 +- /u = /rw + /ro
1880 +- /rw/dirA exists
1881 +- /ro/dirA and /ro/dirA/file exist too
1882 +- there is no dirB on both branches
1883 +- a user issues rename("dirA", "dirB")
1884 +
1885 +Now, what should aufs behave against this rename(2)?
1886 +There are a few possible cases.
1887 +
1888 +A. returns EROFS.
1889 +   since dirA exists on a readonly branch which cannot be renamed.
1890 +B. returns EXDEV.
1891 +   it is possible to copy-up dirA (only the dir itself), but the child
1892 +   entries ("file" in this case) should not be. it must be a bad
1893 +   approach to copy-up recursively.
1894 +C. returns a success.
1895 +   even the branch /ro is readonly, aufs tries renaming it. Obviously it
1896 +   is a violation of aufs' policy.
1897 +D. construct an extra information which indicates that /ro/dirA should
1898 +   be handled as the name of dirB.
1899 +   overlayfs has a similar feature called REDIRECT.
1900 +
1901 +Until now, aufs implements the case B only which returns EXDEV, and
1902 +expects the userspace application behaves like mv(1) which tries
1903 +issueing rename(2) recursively.
1904 +
1905 +A new aufs feature called DIRREN is introduced which implements the case
1906 +D. There are several "extra information" added.
1907 +
1908 +1. detailed info per renamed directory
1909 +   path: /rw/dirB/$AUFS_WH_DR_INFO_PFX.<lower branch-id>
1910 +2. the inode-number list of directories on a branch
1911 +   path: /rw/dirB/$AUFS_WH_DR_BRHINO
1912 +
1913 +The filename of "detailed info per directory" represents the lower
1914 +branch, and its format is
1915 +- a type of the branch id
1916 +  one of these.
1917 +  + uuid (not implemented yet)
1918 +  + fsid
1919 +  + dev
1920 +- the inode-number of the branch root dir
1921 +
1922 +And it contains these info in a single regular file.
1923 +- magic number
1924 +- branch's inode-number of the logically renamed dir
1925 +- the name of the before-renamed dir
1926 +
1927 +The "detailed info per directory" file is created in aufs rename(2), and
1928 +loaded in any lookup.
1929 +The info is considered in lookup for the matching case only. Here
1930 +"matching" means that the root of branch (in the info filename) is same
1931 +to the current looking-up branch. After looking-up the before-renamed
1932 +name, the inode-number is compared. And the matched dentry is used.
1933 +
1934 +The "inode-number list of directories" is a regular file which contains
1935 +simply the inode-numbers on the branch. The file is created or updated
1936 +in removing the branch, and loaded in adding the branch. Its lifetime is
1937 +equal to the branch.
1938 +The list is refered in lookup, and when the current target inode is
1939 +found in the list, the aufs tries loading the "detailed info per
1940 +directory" and get the changed and valid name of the dir.
1941 +
1942 +Theoretically these "extra informaiton" may be able to be put into XATTR
1943 +in the dir inode. But aufs doesn't choose this way because
1944 +1. XATTR may not be supported by the branch (or its configuration)
1945 +2. XATTR may have its size limit.
1946 +3. XATTR may be less easy to convert than a regular file, when the
1947 +   format of the info is changed in the future.
1948 +At the same time, I agree that the regular file approach is much slower
1949 +than XATTR approach. So, in the future, aufs may take the XATTR or other
1950 +better approach.
1951 +
1952 +This DIRREN feature is enabled by aufs configuration, and is activated
1953 +by a new mount option.
1954 +
1955 +For the more complicated case, there is a work with UDBA option, which
1956 +is to dected the direct access to the branches (by-passing aufs) and to
1957 +maintain the cashes in aufs. Since a single cached aufs dentry may
1958 +contains two names, before- and after-rename, the name comparision in
1959 +UDBA handler may not work correctly. In this case, the behaviour will be
1960 +equivalen to udba=reval case.
1961 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06fhsm.txt linux/Documentation/filesystems/aufs/design/06fhsm.txt
1962 --- /usr/share/empty/Documentation/filesystems/aufs/design/06fhsm.txt   1970-01-01 01:00:00.000000000 +0100
1963 +++ linux/Documentation/filesystems/aufs/design/06fhsm.txt      2022-03-21 14:49:05.716633010 +0100
1964 @@ -0,0 +1,120 @@
1965 +
1966 +# Copyright (C) 2011-2021 Junjiro R. Okajima
1967 +# 
1968 +# This program is free software; you can redistribute it and/or modify
1969 +# it under the terms of the GNU General Public License as published by
1970 +# the Free Software Foundation; either version 2 of the License, or
1971 +# (at your option) any later version.
1972 +# 
1973 +# This program is distributed in the hope that it will be useful,
1974 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
1975 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1976 +# GNU General Public License for more details.
1977 +# 
1978 +# You should have received a copy of the GNU General Public License
1979 +# along with this program; if not, write to the Free Software
1980 +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
1981 +
1982 +
1983 +File-based Hierarchical Storage Management (FHSM)
1984 +----------------------------------------------------------------------
1985 +Hierarchical Storage Management (or HSM) is a well-known feature in the
1986 +storage world. Aufs provides this feature as file-based with multiple
1987 +writable branches, based upon the principle of "Colder, the Lower".
1988 +Here the word "colder" means that the less used files, and "lower" means
1989 +that the position in the order of the stacked branches vertically.
1990 +These multiple writable branches are prioritized, ie. the topmost one
1991 +should be the fastest drive and be used heavily.
1992 +
1993 +o Characters in aufs FHSM story
1994 +- aufs itself and a new branch attribute.
1995 +- a new ioctl interface to move-down and to establish a connection with
1996 +  the daemon ("move-down" is a converse of "copy-up").
1997 +- userspace tool and daemon.
1998 +
1999 +The userspace daemon establishes a connection with aufs and waits for
2000 +the notification. The notified information is very similar to struct
2001 +statfs containing the number of consumed blocks and inodes.
2002 +When the consumed blocks/inodes of a branch exceeds the user-specified
2003 +upper watermark, the daemon activates its move-down process until the
2004 +consumed blocks/inodes reaches the user-specified lower watermark.
2005 +
2006 +The actual move-down is done by aufs based upon the request from
2007 +user-space since we need to maintain the inode number and the internal
2008 +pointer arrays in aufs.
2009 +
2010 +Currently aufs FHSM handles the regular files only. Additionally they
2011 +must not be hard-linked nor pseudo-linked.
2012 +
2013 +
2014 +o Cowork of aufs and the user-space daemon
2015 +  During the userspace daemon established the connection, aufs sends a
2016 +  small notification to it whenever aufs writes something into the
2017 +  writable branch. But it may cost high since aufs issues statfs(2)
2018 +  internally. So user can specify a new option to cache the
2019 +  info. Actually the notification is controlled by these factors.
2020 +  + the specified cache time.
2021 +  + classified as "force" by aufs internally.
2022 +  Until the specified time expires, aufs doesn't send the info
2023 +  except the forced cases. When aufs decide forcing, the info is always
2024 +  notified to userspace.
2025 +  For example, the number of free inodes is generally large enough and
2026 +  the shortage of it happens rarely. So aufs doesn't force the
2027 +  notification when creating a new file, directory and others. This is
2028 +  the typical case which aufs doesn't force.
2029 +  When aufs writes the actual filedata and the files consumes any of new
2030 +  blocks, the aufs forces notifying.
2031 +
2032 +
2033 +o Interfaces in aufs
2034 +- New branch attribute.
2035 +  + fhsm
2036 +    Specifies that the branch is managed by FHSM feature. In other word,
2037 +    participant in the FHSM.
2038 +    When nofhsm is set to the branch, it will not be the source/target
2039 +    branch of the move-down operation. This attribute is set
2040 +    independently from coo and moo attributes, and if you want full
2041 +    FHSM, you should specify them as well.
2042 +- New mount option.
2043 +  + fhsm_sec
2044 +    Specifies a second to suppress many less important info to be
2045 +    notified.
2046 +- New ioctl.
2047 +  + AUFS_CTL_FHSM_FD
2048 +    create a new file descriptor which userspace can read the notification
2049 +    (a subset of struct statfs) from aufs.
2050 +- Module parameter 'brs'
2051 +  It has to be set to 1. Otherwise the new mount option 'fhsm' will not
2052 +  be set.
2053 +- mount helpers /sbin/mount.aufs and /sbin/umount.aufs
2054 +  When there are two or more branches with fhsm attributes,
2055 +  /sbin/mount.aufs invokes the user-space daemon and /sbin/umount.aufs
2056 +  terminates it. As a result of remounting and branch-manipulation, the
2057 +  number of branches with fhsm attribute can be one. In this case,
2058 +  /sbin/mount.aufs will terminate the user-space daemon.
2059 +
2060 +
2061 +Finally the operation is done as these steps in kernel-space.
2062 +- make sure that,
2063 +  + no one else is using the file.
2064 +  + the file is not hard-linked.
2065 +  + the file is not pseudo-linked.
2066 +  + the file is a regular file.
2067 +  + the parent dir is not opaqued.
2068 +- find the target writable branch.
2069 +- make sure the file is not whiteout-ed by the upper (than the target)
2070 +  branch.
2071 +- make the parent dir on the target branch.
2072 +- mutex lock the inode on the branch.
2073 +- unlink the whiteout on the target branch (if exists).
2074 +- lookup and create the whiteout-ed temporary name on the target branch.
2075 +- copy the file as the whiteout-ed temporary name on the target branch.
2076 +- rename the whiteout-ed temporary name to the original name.
2077 +- unlink the file on the source branch.
2078 +- maintain the internal pointer array and the external inode number
2079 +  table (XINO).
2080 +- maintain the timestamps and other attributes of the parent dir and the
2081 +  file.
2082 +
2083 +And of course, in every step, an error may happen. So the operation
2084 +should restore the original file state after an error happens.
2085 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06mmap.txt linux/Documentation/filesystems/aufs/design/06mmap.txt
2086 --- /usr/share/empty/Documentation/filesystems/aufs/design/06mmap.txt   1970-01-01 01:00:00.000000000 +0100
2087 +++ linux/Documentation/filesystems/aufs/design/06mmap.txt      2022-03-21 14:49:05.716633010 +0100
2088 @@ -0,0 +1,72 @@
2089 +
2090 +# Copyright (C) 2005-2021 Junjiro R. Okajima
2091 +# 
2092 +# This program is free software; you can redistribute it and/or modify
2093 +# it under the terms of the GNU General Public License as published by
2094 +# the Free Software Foundation; either version 2 of the License, or
2095 +# (at your option) any later version.
2096 +# 
2097 +# This program is distributed in the hope that it will be useful,
2098 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2099 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2100 +# GNU General Public License for more details.
2101 +# 
2102 +# You should have received a copy of the GNU General Public License
2103 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2104 +
2105 +mmap(2) -- File Memory Mapping
2106 +----------------------------------------------------------------------
2107 +In aufs, the file-mapped pages are handled by a branch fs directly, no
2108 +interaction with aufs. It means aufs_mmap() calls the branch fs's
2109 +->mmap().
2110 +This approach is simple and good, but there is one problem.
2111 +Under /proc, several entries show the mmapped files by its path (with
2112 +device and inode number), and the printed path will be the path on the
2113 +branch fs's instead of virtual aufs's.
2114 +This is not a problem in most cases, but some utilities lsof(1) (and its
2115 +user) may expect the path on aufs.
2116 +
2117 +To address this issue, aufs adds a new member called vm_prfile in struct
2118 +vm_area_struct (and struct vm_region). The original vm_file points to
2119 +the file on the branch fs in order to handle everything correctly as
2120 +usual. The new vm_prfile points to a virtual file in aufs, and the
2121 +show-functions in procfs refers to vm_prfile if it is set.
2122 +Also we need to maintain several other places where touching vm_file
2123 +such like
2124 +- fork()/clone() copies vma and the reference count of vm_file is
2125 +  incremented.
2126 +- merging vma maintains the ref count too.
2127 +
2128 +This is not a good approach. It just fakes the printed path. But it
2129 +leaves all behaviour around f_mapping unchanged. This is surely an
2130 +advantage.
2131 +Actually aufs had adopted another complicated approach which calls
2132 +generic_file_mmap() and handles struct vm_operations_struct. In this
2133 +approach, aufs met a hard problem and I could not solve it without
2134 +switching the approach.
2135 +
2136 +There may be one more another approach which is
2137 +- bind-mount the branch-root onto the aufs-root internally
2138 +- grab the new vfsmount (ie. struct mount)
2139 +- lazy-umount the branch-root internally
2140 +- in open(2) the aufs-file, open the branch-file with the hidden
2141 +  vfsmount (instead of the original branch's vfsmount)
2142 +- ideally this "bind-mount and lazy-umount" should be done atomically,
2143 +  but it may be possible from userspace by the mount helper.
2144 +
2145 +Adding the internal hidden vfsmount and using it in opening a file, the
2146 +file path under /proc will be printed correctly. This approach looks
2147 +smarter, but is not possible I am afraid.
2148 +- aufs-root may be bind-mount later. when it happens, another hidden
2149 +  vfsmount will be required.
2150 +- it is hard to get the chance to bind-mount and lazy-umount
2151 +  + in kernel-space, FS can have vfsmount in open(2) via
2152 +    file->f_path, and aufs can know its vfsmount. But several locks are
2153 +    already acquired, and if aufs tries to bind-mount and lazy-umount
2154 +    here, then it may cause a deadlock.
2155 +  + in user-space, bind-mount doesn't invoke the mount helper.
2156 +- since /proc shows dev and ino, aufs has to give vma these info. it
2157 +  means a new member vm_prinode will be necessary. this is essentially
2158 +  equivalent to vm_prfile described above.
2159 +
2160 +I have to give up this "looks-smater" approach.
2161 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/06xattr.txt linux/Documentation/filesystems/aufs/design/06xattr.txt
2162 --- /usr/share/empty/Documentation/filesystems/aufs/design/06xattr.txt  1970-01-01 01:00:00.000000000 +0100
2163 +++ linux/Documentation/filesystems/aufs/design/06xattr.txt     2022-03-21 14:49:05.716633010 +0100
2164 @@ -0,0 +1,96 @@
2165 +
2166 +# Copyright (C) 2014-2021 Junjiro R. Okajima
2167 +#
2168 +# This program is free software; you can redistribute it and/or modify
2169 +# it under the terms of the GNU General Public License as published by
2170 +# the Free Software Foundation; either version 2 of the License, or
2171 +# (at your option) any later version.
2172 +#
2173 +# This program is distributed in the hope that it will be useful,
2174 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2175 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2176 +# GNU General Public License for more details.
2177 +#
2178 +# You should have received a copy of the GNU General Public License
2179 +# along with this program; if not, write to the Free Software
2180 +# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
2181 +
2182 +
2183 +Listing XATTR/EA and getting the value
2184 +----------------------------------------------------------------------
2185 +For the inode standard attributes (owner, group, timestamps, etc.), aufs
2186 +shows the values from the topmost existing file. This behaviour is good
2187 +for the non-dir entries since the bahaviour exactly matches the shown
2188 +information. But for the directories, aufs considers all the same named
2189 +entries on the lower branches. Which means, if one of the lower entry
2190 +rejects readdir call, then aufs returns an error even if the topmost
2191 +entry allows it. This behaviour is necessary to respect the branch fs's
2192 +security, but can make users confused since the user-visible standard
2193 +attributes don't match the behaviour.
2194 +To address this issue, aufs has a mount option called dirperm1 which
2195 +checks the permission for the topmost entry only, and ignores the lower
2196 +entry's permission.
2197 +
2198 +A similar issue can happen around XATTR.
2199 +getxattr(2) and listxattr(2) families behave as if dirperm1 option is
2200 +always set. Otherwise these very unpleasant situation would happen.
2201 +- listxattr(2) may return the duplicated entries.
2202 +- users may not be able to remove or reset the XATTR forever,
2203 +
2204 +
2205 +XATTR/EA support in the internal (copy,move)-(up,down)
2206 +----------------------------------------------------------------------
2207 +Generally the extended attributes of inode are categorized as these.
2208 +- "security" for LSM and capability.
2209 +- "system" for posix ACL, 'acl' mount option is required for the branch
2210 +  fs generally.
2211 +- "trusted" for userspace, CAP_SYS_ADMIN is required.
2212 +- "user" for userspace, 'user_xattr' mount option is required for the
2213 +  branch fs generally.
2214 +
2215 +Moreover there are some other categories. Aufs handles these rather
2216 +unpopular categories as the ordinary ones, ie. there is no special
2217 +condition nor exception.
2218 +
2219 +In copy-up, the support for XATTR on the dst branch may differ from the
2220 +src branch. In this case, the copy-up operation will get an error and
2221 +the original user operation which triggered the copy-up will fail. It
2222 +can happen that even all copy-up will fail.
2223 +When both of src and dst branches support XATTR and if an error occurs
2224 +during copying XATTR, then the copy-up should fail obviously. That is a
2225 +good reason and aufs should return an error to userspace. But when only
2226 +the src branch support that XATTR, aufs should not return an error.
2227 +For example, the src branch supports ACL but the dst branch doesn't
2228 +because the dst branch may natively un-support it or temporary
2229 +un-support it due to "noacl" mount option. Of course, the dst branch fs
2230 +may NOT return an error even if the XATTR is not supported. It is
2231 +totally up to the branch fs.
2232 +
2233 +Anyway when the aufs internal copy-up gets an error from the dst branch
2234 +fs, then aufs tries removing the just copied entry and returns the error
2235 +to the userspace. The worst case of this situation will be all copy-up
2236 +will fail.
2237 +
2238 +For the copy-up operation, there two basic approaches.
2239 +- copy the specified XATTR only (by category above), and return the
2240 +  error unconditionally if it happens.
2241 +- copy all XATTR, and ignore the error on the specified category only.
2242 +
2243 +In order to support XATTR and to implement the correct behaviour, aufs
2244 +chooses the latter approach and introduces some new branch attributes,
2245 +"icexsec", "icexsys", "icextr", "icexusr", and "icexoth".
2246 +They correspond to the XATTR namespaces (see above). Additionally, to be
2247 +convenient, "icex" is also provided which means all "icex*" attributes
2248 +are set (here the word "icex" stands for "ignore copy-error on XATTR").
2249 +
2250 +The meaning of these attributes is to ignore the error from setting
2251 +XATTR on that branch.
2252 +Note that aufs tries copying all XATTR unconditionally, and ignores the
2253 +error from the dst branch according to the specified attributes.
2254 +
2255 +Some XATTR may have its default value. The default value may come from
2256 +the parent dir or the environment. If the default value is set at the
2257 +file creating-time, it will be overwritten by copy-up.
2258 +Some contradiction may happen I am afraid.
2259 +Do we need another attribute to stop copying XATTR? I am unsure. For
2260 +now, aufs implements the branch attributes to ignore the error.
2261 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/07export.txt linux/Documentation/filesystems/aufs/design/07export.txt
2262 --- /usr/share/empty/Documentation/filesystems/aufs/design/07export.txt 1970-01-01 01:00:00.000000000 +0100
2263 +++ linux/Documentation/filesystems/aufs/design/07export.txt    2022-03-21 14:49:05.716633010 +0100
2264 @@ -0,0 +1,58 @@
2265 +
2266 +# Copyright (C) 2005-2021 Junjiro R. Okajima
2267 +# 
2268 +# This program is free software; you can redistribute it and/or modify
2269 +# it under the terms of the GNU General Public License as published by
2270 +# the Free Software Foundation; either version 2 of the License, or
2271 +# (at your option) any later version.
2272 +# 
2273 +# This program is distributed in the hope that it will be useful,
2274 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2275 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2276 +# GNU General Public License for more details.
2277 +# 
2278 +# You should have received a copy of the GNU General Public License
2279 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2280 +
2281 +Export Aufs via NFS
2282 +----------------------------------------------------------------------
2283 +Here is an approach.
2284 +- like xino/xib, add a new file 'xigen' which stores aufs inode
2285 +  generation.
2286 +- iget_locked(): initialize aufs inode generation for a new inode, and
2287 +  store it in xigen file.
2288 +- destroy_inode(): increment aufs inode generation and store it in xigen
2289 +  file. it is necessary even if it is not unlinked, because any data of
2290 +  inode may be changed by UDBA.
2291 +- encode_fh(): for a root dir, simply return FILEID_ROOT. otherwise
2292 +  build file handle by
2293 +  + branch id (4 bytes)
2294 +  + superblock generation (4 bytes)
2295 +  + inode number (4 or 8 bytes)
2296 +  + parent dir inode number (4 or 8 bytes)
2297 +  + inode generation (4 bytes))
2298 +  + return value of exportfs_encode_fh() for the parent on a branch (4
2299 +    bytes)
2300 +  + file handle for a branch (by exportfs_encode_fh())
2301 +- fh_to_dentry():
2302 +  + find the index of a branch from its id in handle, and check it is
2303 +    still exist in aufs.
2304 +  + 1st level: get the inode number from handle and search it in cache.
2305 +  + 2nd level: if not found in cache, get the parent inode number from
2306 +    the handle and search it in cache. and then open the found parent
2307 +    dir, find the matching inode number by vfs_readdir() and get its
2308 +    name, and call lookup_one_len() for the target dentry.
2309 +  + 3rd level: if the parent dir is not cached, call
2310 +    exportfs_decode_fh() for a branch and get the parent on a branch,
2311 +    build a pathname of it, convert it a pathname in aufs, call
2312 +    path_lookup(). now aufs gets a parent dir dentry, then handle it as
2313 +    the 2nd level.
2314 +  + to open the dir, aufs needs struct vfsmount. aufs keeps vfsmount
2315 +    for every branch, but not itself. to get this, (currently) aufs
2316 +    searches in current->nsproxy->mnt_ns list. it may not be a good
2317 +    idea, but I didn't get other approach.
2318 +  + test the generation of the gotten inode.
2319 +- every inode operation: they may get EBUSY due to UDBA. in this case,
2320 +  convert it into ESTALE for NFSD.
2321 +- readdir(): call lockdep_on/off() because filldir in NFSD calls
2322 +  lookup_one_len(), vfs_getattr(), encode_fh() and others.
2323 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/08shwh.txt linux/Documentation/filesystems/aufs/design/08shwh.txt
2324 --- /usr/share/empty/Documentation/filesystems/aufs/design/08shwh.txt   1970-01-01 01:00:00.000000000 +0100
2325 +++ linux/Documentation/filesystems/aufs/design/08shwh.txt      2022-03-21 14:49:05.716633010 +0100
2326 @@ -0,0 +1,52 @@
2327 +
2328 +# Copyright (C) 2005-2021 Junjiro R. Okajima
2329 +# 
2330 +# This program is free software; you can redistribute it and/or modify
2331 +# it under the terms of the GNU General Public License as published by
2332 +# the Free Software Foundation; either version 2 of the License, or
2333 +# (at your option) any later version.
2334 +# 
2335 +# This program is distributed in the hope that it will be useful,
2336 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2337 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2338 +# GNU General Public License for more details.
2339 +# 
2340 +# You should have received a copy of the GNU General Public License
2341 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2342 +
2343 +Show Whiteout Mode (shwh)
2344 +----------------------------------------------------------------------
2345 +Generally aufs hides the name of whiteouts. But in some cases, to show
2346 +them is very useful for users. For instance, creating a new middle layer
2347 +(branch) by merging existing layers.
2348 +
2349 +(borrowing aufs1 HOW-TO from a user, Michael Towers)
2350 +When you have three branches,
2351 +- Bottom: 'system', squashfs (underlying base system), read-only
2352 +- Middle: 'mods', squashfs, read-only
2353 +- Top: 'overlay', ram (tmpfs), read-write
2354 +
2355 +The top layer is loaded at boot time and saved at shutdown, to preserve
2356 +the changes made to the system during the session.
2357 +When larger changes have been made, or smaller changes have accumulated,
2358 +the size of the saved top layer data grows. At this point, it would be
2359 +nice to be able to merge the two overlay branches ('mods' and 'overlay')
2360 +and rewrite the 'mods' squashfs, clearing the top layer and thus
2361 +restoring save and load speed.
2362 +
2363 +This merging is simplified by the use of another aufs mount, of just the
2364 +two overlay branches using the 'shwh' option.
2365 +# mount -t aufs -o ro,shwh,br:/livesys/overlay=ro+wh:/livesys/mods=rr+wh \
2366 +       aufs /livesys/merge_union
2367 +
2368 +A merged view of these two branches is then available at
2369 +/livesys/merge_union, and the new feature is that the whiteouts are
2370 +visible!
2371 +Note that in 'shwh' mode the aufs mount must be 'ro', which will disable
2372 +writing to all branches. Also the default mode for all branches is 'ro'.
2373 +It is now possible to save the combined contents of the two overlay
2374 +branches to a new squashfs, e.g.:
2375 +# mksquashfs /livesys/merge_union /path/to/newmods.squash
2376 +
2377 +This new squashfs archive can be stored on the boot device and the
2378 +initramfs will use it to replace the old one at the next boot.
2379 diff -urN /usr/share/empty/Documentation/filesystems/aufs/design/10dynop.txt linux/Documentation/filesystems/aufs/design/10dynop.txt
2380 --- /usr/share/empty/Documentation/filesystems/aufs/design/10dynop.txt  1970-01-01 01:00:00.000000000 +0100
2381 +++ linux/Documentation/filesystems/aufs/design/10dynop.txt     2022-03-21 14:49:05.716633010 +0100
2382 @@ -0,0 +1,47 @@
2383 +
2384 +# Copyright (C) 2010-2021 Junjiro R. Okajima
2385 +#
2386 +# This program is free software; you can redistribute it and/or modify
2387 +# it under the terms of the GNU General Public License as published by
2388 +# the Free Software Foundation; either version 2 of the License, or
2389 +# (at your option) any later version.
2390 +#
2391 +# This program is distributed in the hope that it will be useful,
2392 +# but WITHOUT ANY WARRANTY; without even the implied warranty of
2393 +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2394 +# GNU General Public License for more details.
2395 +#
2396 +# You should have received a copy of the GNU General Public License
2397 +# along with this program.  If not, see <http://www.gnu.org/licenses/>.
2398 +
2399 +Dynamically customizable FS operations
2400 +----------------------------------------------------------------------
2401 +Generally FS operations (struct inode_operations, struct
2402 +address_space_operations, struct file_operations, etc.) are defined as
2403 +"static const", but it never means that FS have only one set of
2404 +operation. Some FS have multiple sets of them. For instance, ext2 has
2405 +three sets, one for XIP, for NOBH, and for normal.
2406 +Since aufs overrides and redirects these operations, sometimes aufs has
2407 +to change its behaviour according to the branch FS type. More importantly
2408 +VFS acts differently if a function (member in the struct) is set or
2409 +not. It means aufs should have several sets of operations and select one
2410 +among them according to the branch FS definition.
2411 +
2412 +In order to solve this problem and not to affect the behaviour of VFS,
2413 +aufs defines these operations dynamically. For instance, aufs defines
2414 +dummy direct_IO function for struct address_space_operations, but it may
2415 +not be set to the address_space_operations actually. When the branch FS
2416 +doesn't have it, aufs doesn't set it to its address_space_operations
2417 +while the function definition itself is still alive. So the behaviour
2418 +itself will not change, and it will return an error when direct_IO is
2419 +not set.
2420 +
2421 +The lifetime of these dynamically generated operation object is
2422 +maintained by aufs branch object. When the branch is removed from aufs,
2423 +the reference counter of the object is decremented. When it reaches
2424 +zero, the dynamically generated operation object will be freed.
2425 +
2426 +This approach is designed to support AIO (io_submit), Direct I/O and
2427 +XIP (DAX) mainly.
2428 +Currently this approach is applied to address_space_operations for
2429 +regular files only.
2430 diff -urN /usr/share/empty/Documentation/filesystems/aufs/README linux/Documentation/filesystems/aufs/README
2431 --- /usr/share/empty/Documentation/filesystems/aufs/README      1970-01-01 01:00:00.000000000 +0100
2432 +++ linux/Documentation/filesystems/aufs/README 2022-03-21 14:49:05.713299677 +0100
2433 @@ -0,0 +1,396 @@
2434 +
2435 +Aufs5 -- advanced multi layered unification filesystem version 5.x
2436 +http://aufs.sf.net
2437 +Junjiro R. Okajima
2438 +
2439 +
2440 +0. Introduction
2441 +----------------------------------------
2442 +In the early days, aufs was entirely re-designed and re-implemented
2443 +Unionfs Version 1.x series. Adding many original ideas, approaches,
2444 +improvements and implementations, it became totally different from
2445 +Unionfs while keeping the basic features.
2446 +Later, Unionfs Version 2.x series began taking some of the same
2447 +approaches to aufs1's.
2448 +Unionfs was being developed by Professor Erez Zadok at Stony Brook
2449 +University and his team.
2450 +
2451 +Aufs5 supports linux-v5.0 and later, If you want older kernel version
2452 +support,
2453 +- for linux-v4.x series, try aufs4-linux.git or aufs4-standalone.git
2454 +- for linux-v3.x series, try aufs3-linux.git or aufs3-standalone.git
2455 +- for linux-v2.6.16 and later, try aufs2-2.6.git, aufs2-standalone.git
2456 +  or aufs1 from CVS on SourceForge.
2457 +
2458 +Note: it becomes clear that "Aufs was rejected. Let's give it up."
2459 +      According to Christoph Hellwig, linux rejects all union-type
2460 +      filesystems but UnionMount.
2461 +<http://marc.info/?l=linux-kernel&m=123938533724484&w=2>
2462 +
2463 +PS. Al Viro seems have a plan to merge aufs as well as overlayfs and
2464 +    UnionMount, and he pointed out an issue around a directory mutex
2465 +    lock and aufs addressed it. But it is still unsure whether aufs will
2466 +    be merged (or any other union solution).
2467 +<http://marc.info/?l=linux-kernel&m=136312705029295&w=1>
2468 +
2469 +
2470 +1. Features
2471 +----------------------------------------
2472 +- unite several directories into a single virtual filesystem. The member
2473 +  directory is called as a branch.
2474 +- you can specify the permission flags to the branch, which are 'readonly',
2475 +  'readwrite' and 'whiteout-able.'
2476 +- by upper writable branch, internal copyup and whiteout, files/dirs on
2477 +  readonly branch are modifiable logically.
2478 +- dynamic branch manipulation, add, del.
2479 +- etc...
2480 +
2481 +Also there are many enhancements in aufs, such as:
2482 +- test only the highest one for the directory permission (dirperm1)
2483 +- copyup on open (coo=)
2484 +- 'move' policy for copy-up between two writable branches, after
2485 +  checking free space.
2486 +- xattr, acl
2487 +- readdir(3) in userspace.
2488 +- keep inode number by external inode number table
2489 +- keep the timestamps of file/dir in internal copyup operation
2490 +- seekable directory, supporting NFS readdir.
2491 +- whiteout is hardlinked in order to reduce the consumption of inodes
2492 +  on branch
2493 +- do not copyup, nor create a whiteout when it is unnecessary
2494 +- revert a single systemcall when an error occurs in aufs
2495 +- remount interface instead of ioctl
2496 +- maintain /etc/mtab by an external command, /sbin/mount.aufs.
2497 +- loopback mounted filesystem as a branch
2498 +- kernel thread for removing the dir who has a plenty of whiteouts
2499 +- support copyup sparse file (a file which has a 'hole' in it)
2500 +- default permission flags for branches
2501 +- selectable permission flags for ro branch, whether whiteout can
2502 +  exist or not
2503 +- export via NFS.
2504 +- support <sysfs>/fs/aufs and <debugfs>/aufs.
2505 +- support multiple writable branches, some policies to select one
2506 +  among multiple writable branches.
2507 +- a new semantics for link(2) and rename(2) to support multiple
2508 +  writable branches.
2509 +- no glibc changes are required.
2510 +- pseudo hardlink (hardlink over branches)
2511 +- allow a direct access manually to a file on branch, e.g. bypassing aufs.
2512 +  including NFS or remote filesystem branch.
2513 +- userspace wrapper for pathconf(3)/fpathconf(3) with _PC_LINK_MAX.
2514 +- and more...
2515 +
2516 +Currently these features are dropped temporary from aufs5.
2517 +See design/08plan.txt in detail.
2518 +- nested mount, i.e. aufs as readonly no-whiteout branch of another aufs
2519 +  (robr)
2520 +- statistics of aufs thread (/sys/fs/aufs/stat)
2521 +
2522 +Features or just an idea in the future (see also design/*.txt),
2523 +- reorder the branch index without del/re-add.
2524 +- permanent xino files for NFSD
2525 +- an option for refreshing the opened files after add/del branches
2526 +- light version, without branch manipulation. (unnecessary?)
2527 +- copyup in userspace
2528 +- inotify in userspace
2529 +- readv/writev
2530 +
2531 +
2532 +2. Download
2533 +----------------------------------------
2534 +There are three GIT trees for aufs5, aufs5-linux.git,
2535 +aufs5-standalone.git, and aufs-util.git. Note that there is no "5" in
2536 +"aufs-util.git."
2537 +While the aufs-util is always necessary, you need either of aufs5-linux
2538 +or aufs5-standalone.
2539 +
2540 +The aufs5-linux tree includes the whole linux mainline GIT tree,
2541 +git://git.kernel.org/.../torvalds/linux.git.
2542 +And you cannot select CONFIG_AUFS_FS=m for this version, eg. you cannot
2543 +build aufs5 as an external kernel module.
2544 +Several extra patches are not included in this tree. Only
2545 +aufs5-standalone tree contains them. They are described in the later
2546 +section "Configuration and Compilation."
2547 +
2548 +On the other hand, the aufs5-standalone tree has only aufs source files
2549 +and necessary patches, and you can select CONFIG_AUFS_FS=m.
2550 +But you need to apply all aufs patches manually.
2551 +
2552 +You will find GIT branches whose name is in form of "aufs5.x" where "x"
2553 +represents the linux kernel version, "linux-5.x". For instance,
2554 +"aufs5.0" is for linux-5.0. For latest "linux-5.x-rcN", use
2555 +"aufs5.x-rcN" branch.
2556 +
2557 +o aufs5-linux tree
2558 +$ git clone --reference /your/linux/git/tree \
2559 +       git://github.com/sfjro/aufs5-linux.git aufs5-linux.git
2560 +- if you don't have linux GIT tree, then remove "--reference ..."
2561 +$ cd aufs5-linux.git
2562 +$ git checkout origin/aufs5.0
2563 +
2564 +Or You may want to directly git-pull aufs into your linux GIT tree, and
2565 +leave the patch-work to GIT.
2566 +$ cd /your/linux/git/tree
2567 +$ git remote add aufs5 git://github.com/sfjro/aufs5-linux.git
2568 +$ git fetch aufs5
2569 +$ git checkout -b my5.0 v5.0
2570 +$ (add your local change...)
2571 +$ git pull aufs5 aufs5.0
2572 +- now you have v5.0 + your_changes + aufs5.0 in you my5.0 branch.
2573 +- you may need to solve some conflicts between your_changes and
2574 +  aufs5.0. in this case, git-rerere is recommended so that you can
2575 +  solve the similar conflicts automatically when you upgrade to 5.1 or
2576 +  later in the future.
2577 +
2578 +o aufs5-standalone tree
2579 +$ git clone git://github.com/sfjro/aufs5-standalone.git aufs5-standalone.git
2580 +$ cd aufs5-standalone.git
2581 +$ git checkout origin/aufs5.0
2582 +
2583 +o aufs-util tree
2584 +$ git clone git://git.code.sf.net/p/aufs/aufs-util aufs-util.git
2585 +- note that the public aufs-util.git is on SourceForge instead of
2586 +  GitHUB.
2587 +$ cd aufs-util.git
2588 +$ git checkout origin/aufs5.0
2589 +
2590 +Note: The 5.x-rcN branch is to be used with `rc' kernel versions ONLY.
2591 +The minor version number, 'x' in '5.x', of aufs may not always
2592 +follow the minor version number of the kernel.
2593 +Because changes in the kernel that cause the use of a new
2594 +minor version number do not always require changes to aufs-util.
2595 +
2596 +Since aufs-util has its own minor version number, you may not be
2597 +able to find a GIT branch in aufs-util for your kernel's
2598 +exact minor version number.
2599 +In this case, you should git-checkout the branch for the
2600 +nearest lower number.
2601 +
2602 +For (an unreleased) example:
2603 +If you are using "linux-5.10" and the "aufs5.10" branch
2604 +does not exist in aufs-util repository, then "aufs5.9", "aufs5.8"
2605 +or something numerically smaller is the branch for your kernel.
2606 +
2607 +Also you can view all branches by
2608 +       $ git branch -a
2609 +
2610 +
2611 +3. Configuration and Compilation
2612 +----------------------------------------
2613 +Make sure you have git-checkout'ed the correct branch.
2614 +
2615 +For aufs5-linux tree,
2616 +- enable CONFIG_AUFS_FS.
2617 +- set other aufs configurations if necessary.
2618 +- for aufs5.13 and later
2619 +  Because aufs is not only an ordinary filesystem (callee of VFS), but
2620 +  also a caller of VFS functions for branch filesystems, subclassing of
2621 +  the internal locks for LOCKDEP is necessary. LOCKDEP is a debugging
2622 +  feature of linux kernel. If you enable CONFIG_LOCKDEP, then you will
2623 +  need to customize some LOCKDEP numbers. Here are what I use on my
2624 +  test environment.
2625 +       CONFIG_LOCKDEP_BITS=21
2626 +       CONFIG_LOCKDEP_CHAINS_BITS=21
2627 +       CONFIG_LOCKDEP_STACK_TRACE_BITS=24
2628 +
2629 +For aufs5-standalone tree,
2630 +There are several ways to build.
2631 +
2632 +1.
2633 +- apply ./aufs5-kbuild.patch to your kernel source files.
2634 +- apply ./aufs5-base.patch too.
2635 +- apply ./aufs5-mmap.patch too.
2636 +- apply ./aufs5-standalone.patch too, if you have a plan to set
2637 +  CONFIG_AUFS_FS=m. otherwise you don't need ./aufs5-standalone.patch.
2638 +- copy ./{Documentation,fs,include/uapi/linux/aufs_type.h} files to your
2639 +  kernel source tree. Never copy $PWD/include/uapi/linux/Kbuild.
2640 +- enable CONFIG_AUFS_FS, you can select either
2641 +  =m or =y.
2642 +- and build your kernel as usual.
2643 +- install the built kernel.
2644 +- install the header files too by "make headers_install" to the
2645 +  directory where you specify. By default, it is $PWD/usr.
2646 +  "make help" shows a brief note for headers_install.
2647 +- and reboot your system.
2648 +
2649 +2.
2650 +- module only (CONFIG_AUFS_FS=m).
2651 +- apply ./aufs5-base.patch to your kernel source files.
2652 +- apply ./aufs5-mmap.patch too.
2653 +- apply ./aufs5-standalone.patch too.
2654 +- build your kernel, don't forget "make headers_install", and reboot.
2655 +- edit ./config.mk and set other aufs configurations if necessary.
2656 +  Note: You should read $PWD/fs/aufs/Kconfig carefully which describes
2657 +  every aufs configurations.
2658 +- build the module by simple "make".
2659 +- you can specify ${KDIR} make variable which points to your kernel
2660 +  source tree.
2661 +- install the files
2662 +  + run "make install" to install the aufs module, or copy the built
2663 +    $PWD/aufs.ko to /lib/modules/... and run depmod -a (or reboot simply).
2664 +  + run "make install_headers" (instead of headers_install) to install
2665 +    the modified aufs header file (you can specify DESTDIR which is
2666 +    available in aufs standalone version's Makefile only), or copy
2667 +    $PWD/usr/include/linux/aufs_type.h to /usr/include/linux or wherever
2668 +    you like manually. By default, the target directory is $PWD/usr.
2669 +- no need to apply aufs5-kbuild.patch, nor copying source files to your
2670 +  kernel source tree.
2671 +
2672 +Note: The header file aufs_type.h is necessary to build aufs-util
2673 +      as well as "make headers_install" in the kernel source tree.
2674 +      headers_install is subject to be forgotten, but it is essentially
2675 +      necessary, not only for building aufs-util.
2676 +      You may not meet problems without headers_install in some older
2677 +      version though.
2678 +
2679 +And then,
2680 +- read README in aufs-util, build and install it
2681 +- note that your distribution may contain an obsoleted version of
2682 +  aufs_type.h in /usr/include/linux or something. When you build aufs
2683 +  utilities, make sure that your compiler refers the correct aufs header
2684 +  file which is built by "make headers_install."
2685 +- if you want to use readdir(3) in userspace or pathconf(3) wrapper,
2686 +  then run "make install_ulib" too. And refer to the aufs manual in
2687 +  detail.
2688 +
2689 +There several other patches in aufs5-standalone.git. They are all
2690 +optional. When you meet some problems, they will help you.
2691 +- aufs5-loopback.patch
2692 +  Supports a nested loopback mount in a branch-fs. This patch is
2693 +  unnecessary until aufs produces a message like "you may want to try
2694 +  another patch for loopback file".
2695 +- vfs-ino.patch
2696 +  Modifies a system global kernel internal function get_next_ino() in
2697 +  order to stop assigning 0 for an inode-number. Not directly related to
2698 +  aufs, but recommended generally.
2699 +- tmpfs-idr.patch
2700 +  Keeps the tmpfs inode number as the lowest value. Effective to reduce
2701 +  the size of aufs XINO files for tmpfs branch. Also it prevents the
2702 +  duplication of inode number, which is important for backup tools and
2703 +  other utilities. When you find aufs XINO files for tmpfs branch
2704 +  growing too much, try this patch.
2705 +
2706 +
2707 +4. Usage
2708 +----------------------------------------
2709 +At first, make sure aufs-util are installed, and please read the aufs
2710 +manual, aufs.5 in aufs-util.git tree.
2711 +$ man -l aufs.5
2712 +
2713 +And then,
2714 +$ mkdir /tmp/rw /tmp/aufs
2715 +# mount -t aufs -o br=/tmp/rw:${HOME} none /tmp/aufs
2716 +
2717 +Here is another example. The result is equivalent.
2718 +# mount -t aufs -o br=/tmp/rw=rw:${HOME}=ro none /tmp/aufs
2719 +  Or
2720 +# mount -t aufs -o br:/tmp/rw none /tmp/aufs
2721 +# mount -o remount,append:${HOME} /tmp/aufs
2722 +
2723 +Then, you can see whole tree of your home dir through /tmp/aufs. If
2724 +you modify a file under /tmp/aufs, the one on your home directory is
2725 +not affected, instead the same named file will be newly created under
2726 +/tmp/rw. And all of your modification to a file will be applied to
2727 +the one under /tmp/rw. This is called the file based Copy on Write
2728 +(COW) method.
2729 +Aufs mount options are described in aufs.5.
2730 +If you run chroot or something and make your aufs as a root directory,
2731 +then you need to customize the shutdown script. See the aufs manual in
2732 +detail.
2733 +
2734 +Additionally, there are some sample usages of aufs which are a
2735 +diskless system with network booting, and LiveCD over NFS.
2736 +See sample dir in CVS tree on SourceForge.
2737 +
2738 +
2739 +5. Contact
2740 +----------------------------------------
2741 +When you have any problems or strange behaviour in aufs, please let me
2742 +know with:
2743 +- /proc/mounts (instead of the output of mount(8))
2744 +- /sys/module/aufs/*
2745 +- /sys/fs/aufs/* (if you have them)
2746 +- /debug/aufs/* (if you have them)
2747 +- linux kernel version
2748 +  if your kernel is not plain, for example modified by distributor,
2749 +  the url where i can download its source is necessary too.
2750 +- aufs version which was printed at loading the module or booting the
2751 +  system, instead of the date you downloaded.
2752 +- configuration (define/undefine CONFIG_AUFS_xxx)
2753 +- kernel configuration or /proc/config.gz (if you have it)
2754 +- LSM (linux security module, if you are using)
2755 +- behaviour which you think to be incorrect
2756 +- actual operation, reproducible one is better
2757 +- mailto: aufs-users at lists.sourceforge.net
2758 +
2759 +Usually, I don't watch the Public Areas(Bugs, Support Requests, Patches,
2760 +and Feature Requests) on SourceForge. Please join and write to
2761 +aufs-users ML.
2762 +
2763 +
2764 +6. Acknowledgements
2765 +----------------------------------------
2766 +Thanks to everyone who have tried and are using aufs, whoever
2767 +have reported a bug or any feedback.
2768 +
2769 +Especially donators:
2770 +Tomas Matejicek(slax.org) made a donation (much more than once).
2771 +       Since Apr 2010, Tomas M (the author of Slax and Linux Live
2772 +       scripts) is making "doubling" donations.
2773 +       Unfortunately I cannot list all of the donators, but I really
2774 +       appreciate.
2775 +       It ends Aug 2010, but the ordinary donation URL is still available.
2776 +       <http://sourceforge.net/donate/index.php?group_id=167503>
2777 +Dai Itasaka made a donation (2007/8).
2778 +Chuck Smith made a donation (2008/4, 10 and 12).
2779 +Henk Schoneveld made a donation (2008/9).
2780 +Chih-Wei Huang, ASUS, CTC donated Eee PC 4G (2008/10).
2781 +Francois Dupoux made a donation (2008/11).
2782 +Bruno Cesar Ribas and Luis Carlos Erpen de Bona, C3SL serves public
2783 +       aufs2 GIT tree (2009/2).
2784 +William Grant made a donation (2009/3).
2785 +Patrick Lane made a donation (2009/4).
2786 +The Mail Archive (mail-archive.com) made donations (2009/5).
2787 +Nippy Networks (Ed Wildgoose) made a donation (2009/7).
2788 +New Dream Network, LLC (www.dreamhost.com) made a donation (2009/11).
2789 +Pavel Pronskiy made a donation (2011/2).
2790 +Iridium and Inmarsat satellite phone retailer (www.mailasail.com), Nippy
2791 +       Networks (Ed Wildgoose) made a donation for hardware (2011/3).
2792 +Max Lekomcev (DOM-TV project) made a donation (2011/7, 12, 2012/3, 6 and
2793 +11).
2794 +Sam Liddicott made a donation (2011/9).
2795 +Era Scarecrow made a donation (2013/4).
2796 +Bor Ratajc made a donation (2013/4).
2797 +Alessandro Gorreta made a donation (2013/4).
2798 +POIRETTE Marc made a donation (2013/4).
2799 +Alessandro Gorreta made a donation (2013/4).
2800 +lauri kasvandik made a donation (2013/5).
2801 +"pemasu from Finland" made a donation (2013/7).
2802 +The Parted Magic Project made a donation (2013/9 and 11).
2803 +Pavel Barta made a donation (2013/10).
2804 +Nikolay Pertsev made a donation (2014/5).
2805 +James B made a donation (2014/7, 2015/7, and 2021/12).
2806 +Stefano Di Biase made a donation (2014/8).
2807 +Daniel Epellei made a donation (2015/1).
2808 +OmegaPhil made a donation (2016/1, 2018/4).
2809 +Tomasz Szewczyk made a donation (2016/4).
2810 +James Burry made a donation (2016/12).
2811 +Carsten Rose made a donation (2018/9).
2812 +Porteus Kiosk made a donation (2018/10).
2813 +
2814 +Thank you very much.
2815 +Donations are always, including future donations, very important and
2816 +helpful for me to keep on developing aufs.
2817 +
2818 +
2819 +7.
2820 +----------------------------------------
2821 +If you are an experienced user, no explanation is needed. Aufs is
2822 +just a linux filesystem.
2823 +
2824 +
2825 +Enjoy!
2826 +
2827 +# Local variables: ;
2828 +# mode: text;
2829 +# End: ;
2830 diff -urN /usr/share/empty/fs/aufs/aufs.h linux/fs/aufs/aufs.h
2831 --- /usr/share/empty/fs/aufs/aufs.h     1970-01-01 01:00:00.000000000 +0100
2832 +++ linux/fs/aufs/aufs.h        2022-03-21 14:49:05.716633010 +0100
2833 @@ -0,0 +1,62 @@
2834 +/* SPDX-License-Identifier: GPL-2.0 */
2835 +/*
2836 + * Copyright (C) 2005-2021 Junjiro R. Okajima
2837 + *
2838 + * This program, aufs is free software; you can redistribute it and/or modify
2839 + * it under the terms of the GNU General Public License as published by
2840 + * the Free Software Foundation; either version 2 of the License, or
2841 + * (at your option) any later version.
2842 + *
2843 + * This program is distributed in the hope that it will be useful,
2844 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2845 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2846 + * GNU General Public License for more details.
2847 + *
2848 + * You should have received a copy of the GNU General Public License
2849 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
2850 + */
2851 +
2852 +/*
2853 + * all header files
2854 + */
2855 +
2856 +#ifndef __AUFS_H__
2857 +#define __AUFS_H__
2858 +
2859 +#ifdef __KERNEL__
2860 +
2861 +#define AuStub(type, name, body, ...) \
2862 +       static inline type name(__VA_ARGS__) { body; }
2863 +
2864 +#define AuStubVoid(name, ...) \
2865 +       AuStub(void, name, , __VA_ARGS__)
2866 +#define AuStubInt0(name, ...) \
2867 +       AuStub(int, name, return 0, __VA_ARGS__)
2868 +
2869 +#include "debug.h"
2870 +
2871 +#include "branch.h"
2872 +#include "cpup.h"
2873 +#include "dcsub.h"
2874 +#include "dbgaufs.h"
2875 +#include "dentry.h"
2876 +#include "dir.h"
2877 +#include "dirren.h"
2878 +#include "dynop.h"
2879 +#include "file.h"
2880 +#include "fstype.h"
2881 +#include "hbl.h"
2882 +#include "inode.h"
2883 +#include "lcnt.h"
2884 +#include "loop.h"
2885 +#include "module.h"
2886 +#include "opts.h"
2887 +#include "rwsem.h"
2888 +#include "super.h"
2889 +#include "sysaufs.h"
2890 +#include "vfsub.h"
2891 +#include "whout.h"
2892 +#include "wkq.h"
2893 +
2894 +#endif /* __KERNEL__ */
2895 +#endif /* __AUFS_H__ */
2896 diff -urN /usr/share/empty/fs/aufs/branch.c linux/fs/aufs/branch.c
2897 --- /usr/share/empty/fs/aufs/branch.c   1970-01-01 01:00:00.000000000 +0100
2898 +++ linux/fs/aufs/branch.c      2022-03-21 14:49:05.719966344 +0100
2899 @@ -0,0 +1,1427 @@
2900 +// SPDX-License-Identifier: GPL-2.0
2901 +/*
2902 + * Copyright (C) 2005-2021 Junjiro R. Okajima
2903 + *
2904 + * This program, aufs is free software; you can redistribute it and/or modify
2905 + * it under the terms of the GNU General Public License as published by
2906 + * the Free Software Foundation; either version 2 of the License, or
2907 + * (at your option) any later version.
2908 + *
2909 + * This program is distributed in the hope that it will be useful,
2910 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2911 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2912 + * GNU General Public License for more details.
2913 + *
2914 + * You should have received a copy of the GNU General Public License
2915 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
2916 + */
2917 +
2918 +/*
2919 + * branch management
2920 + */
2921 +
2922 +#include <linux/compat.h>
2923 +#include <linux/statfs.h>
2924 +#include "aufs.h"
2925 +
2926 +/*
2927 + * free a single branch
2928 + */
2929 +static void au_br_do_free(struct au_branch *br)
2930 +{
2931 +       int i;
2932 +       struct au_wbr *wbr;
2933 +       struct au_dykey **key;
2934 +
2935 +       au_hnotify_fin_br(br);
2936 +       /* always, regardless the mount option */
2937 +       au_dr_hino_free(&br->br_dirren);
2938 +       au_xino_put(br);
2939 +
2940 +       AuLCntZero(au_lcnt_read(&br->br_nfiles, /*do_rev*/0));
2941 +       au_lcnt_fin(&br->br_nfiles, /*do_sync*/0);
2942 +       AuLCntZero(au_lcnt_read(&br->br_count, /*do_rev*/0));
2943 +       au_lcnt_fin(&br->br_count, /*do_sync*/0);
2944 +
2945 +       wbr = br->br_wbr;
2946 +       if (wbr) {
2947 +               for (i = 0; i < AuBrWh_Last; i++)
2948 +                       dput(wbr->wbr_wh[i]);
2949 +               AuDebugOn(atomic_read(&wbr->wbr_wh_running));
2950 +               AuRwDestroy(&wbr->wbr_wh_rwsem);
2951 +       }
2952 +
2953 +       if (br->br_fhsm) {
2954 +               au_br_fhsm_fin(br->br_fhsm);
2955 +               au_kfree_try_rcu(br->br_fhsm);
2956 +       }
2957 +
2958 +       key = br->br_dykey;
2959 +       for (i = 0; i < AuBrDynOp; i++, key++)
2960 +               if (*key)
2961 +                       au_dy_put(*key);
2962 +               else
2963 +                       break;
2964 +
2965 +       /* recursive lock, s_umount of branch's */
2966 +       /* synchronize_rcu(); */ /* why? */
2967 +       lockdep_off();
2968 +       path_put(&br->br_path);
2969 +       lockdep_on();
2970 +       au_kfree_rcu(wbr);
2971 +       au_lcnt_wait_for_fin(&br->br_nfiles);
2972 +       au_lcnt_wait_for_fin(&br->br_count);
2973 +       /* I don't know why, but percpu_refcount requires this */
2974 +       /* synchronize_rcu(); */
2975 +       au_kfree_rcu(br);
2976 +}
2977 +
2978 +/*
2979 + * frees all branches
2980 + */
2981 +void au_br_free(struct au_sbinfo *sbinfo)
2982 +{
2983 +       aufs_bindex_t bmax;
2984 +       struct au_branch **br;
2985 +
2986 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
2987 +
2988 +       bmax = sbinfo->si_bbot + 1;
2989 +       br = sbinfo->si_branch;
2990 +       while (bmax--)
2991 +               au_br_do_free(*br++);
2992 +}
2993 +
2994 +/*
2995 + * find the index of a branch which is specified by @br_id.
2996 + */
2997 +int au_br_index(struct super_block *sb, aufs_bindex_t br_id)
2998 +{
2999 +       aufs_bindex_t bindex, bbot;
3000 +
3001 +       bbot = au_sbbot(sb);
3002 +       for (bindex = 0; bindex <= bbot; bindex++)
3003 +               if (au_sbr_id(sb, bindex) == br_id)
3004 +                       return bindex;
3005 +       return -1;
3006 +}
3007 +
3008 +/* ---------------------------------------------------------------------- */
3009 +
3010 +/*
3011 + * add a branch
3012 + */
3013 +
3014 +static int test_overlap(struct super_block *sb, struct dentry *h_adding,
3015 +                       struct dentry *h_root)
3016 +{
3017 +       if (unlikely(h_adding == h_root
3018 +                    || au_test_loopback_overlap(sb, h_adding)))
3019 +               return 1;
3020 +       if (h_adding->d_sb != h_root->d_sb)
3021 +               return 0;
3022 +       return au_test_subdir(h_adding, h_root)
3023 +               || au_test_subdir(h_root, h_adding);
3024 +}
3025 +
3026 +/*
3027 + * returns a newly allocated branch. @new_nbranch is a number of branches
3028 + * after adding a branch.
3029 + */
3030 +static struct au_branch *au_br_alloc(struct super_block *sb, int new_nbranch,
3031 +                                    int perm)
3032 +{
3033 +       struct au_branch *add_branch;
3034 +       struct dentry *root;
3035 +       struct inode *inode;
3036 +       int err;
3037 +
3038 +       err = -ENOMEM;
3039 +       add_branch = kzalloc(sizeof(*add_branch), GFP_NOFS);
3040 +       if (unlikely(!add_branch))
3041 +               goto out;
3042 +       add_branch->br_xino = au_xino_alloc(/*nfile*/1);
3043 +       if (unlikely(!add_branch->br_xino))
3044 +               goto out_br;
3045 +       err = au_hnotify_init_br(add_branch, perm);
3046 +       if (unlikely(err))
3047 +               goto out_xino;
3048 +
3049 +       if (au_br_writable(perm)) {
3050 +               /* may be freed separately at changing the branch permission */
3051 +               add_branch->br_wbr = kzalloc(sizeof(*add_branch->br_wbr),
3052 +                                            GFP_NOFS);
3053 +               if (unlikely(!add_branch->br_wbr))
3054 +                       goto out_hnotify;
3055 +       }
3056 +
3057 +       if (au_br_fhsm(perm)) {
3058 +               err = au_fhsm_br_alloc(add_branch);
3059 +               if (unlikely(err))
3060 +                       goto out_wbr;
3061 +       }
3062 +
3063 +       root = sb->s_root;
3064 +       err = au_sbr_realloc(au_sbi(sb), new_nbranch, /*may_shrink*/0);
3065 +       if (!err)
3066 +               err = au_di_realloc(au_di(root), new_nbranch, /*may_shrink*/0);
3067 +       if (!err) {
3068 +               inode = d_inode(root);
3069 +               err = au_hinode_realloc(au_ii(inode), new_nbranch,
3070 +                                       /*may_shrink*/0);
3071 +       }
3072 +       if (!err)
3073 +               return add_branch; /* success */
3074 +
3075 +out_wbr:
3076 +       au_kfree_rcu(add_branch->br_wbr);
3077 +out_hnotify:
3078 +       au_hnotify_fin_br(add_branch);
3079 +out_xino:
3080 +       au_xino_put(add_branch);
3081 +out_br:
3082 +       au_kfree_rcu(add_branch);
3083 +out:
3084 +       return ERR_PTR(err);
3085 +}
3086 +
3087 +/*
3088 + * test if the branch permission is legal or not.
3089 + */
3090 +static int test_br(struct inode *inode, int brperm, char *path)
3091 +{
3092 +       int err;
3093 +
3094 +       err = (au_br_writable(brperm) && IS_RDONLY(inode));
3095 +       if (!err)
3096 +               goto out;
3097 +
3098 +       err = -EINVAL;
3099 +       pr_err("write permission for readonly mount or inode, %s\n", path);
3100 +
3101 +out:
3102 +       return err;
3103 +}
3104 +
3105 +/*
3106 + * returns:
3107 + * 0: success, the caller will add it
3108 + * plus: success, it is already unified, the caller should ignore it
3109 + * minus: error
3110 + */
3111 +static int test_add(struct super_block *sb, struct au_opt_add *add, int remount)
3112 +{
3113 +       int err;
3114 +       aufs_bindex_t bbot, bindex;
3115 +       struct dentry *root, *h_dentry;
3116 +       struct inode *inode, *h_inode;
3117 +
3118 +       root = sb->s_root;
3119 +       bbot = au_sbbot(sb);
3120 +       if (unlikely(bbot >= 0
3121 +                    && au_find_dbindex(root, add->path.dentry) >= 0)) {
3122 +               err = 1;
3123 +               if (!remount) {
3124 +                       err = -EINVAL;
3125 +                       pr_err("%s duplicated\n", add->pathname);
3126 +               }
3127 +               goto out;
3128 +       }
3129 +
3130 +       err = -ENOSPC; /* -E2BIG; */
3131 +       if (unlikely(AUFS_BRANCH_MAX <= add->bindex
3132 +                    || AUFS_BRANCH_MAX - 1 <= bbot)) {
3133 +               pr_err("number of branches exceeded %s\n", add->pathname);
3134 +               goto out;
3135 +       }
3136 +
3137 +       err = -EDOM;
3138 +       if (unlikely(add->bindex < 0 || bbot + 1 < add->bindex)) {
3139 +               pr_err("bad index %d\n", add->bindex);
3140 +               goto out;
3141 +       }
3142 +
3143 +       inode = d_inode(add->path.dentry);
3144 +       err = -ENOENT;
3145 +       if (unlikely(!inode->i_nlink)) {
3146 +               pr_err("no existence %s\n", add->pathname);
3147 +               goto out;
3148 +       }
3149 +
3150 +       err = -EINVAL;
3151 +       if (unlikely(inode->i_sb == sb)) {
3152 +               pr_err("%s must be outside\n", add->pathname);
3153 +               goto out;
3154 +       }
3155 +
3156 +       if (unlikely(au_test_fs_unsuppoted(inode->i_sb))) {
3157 +               pr_err("unsupported filesystem, %s (%s)\n",
3158 +                      add->pathname, au_sbtype(inode->i_sb));
3159 +               goto out;
3160 +       }
3161 +
3162 +       if (unlikely(inode->i_sb->s_stack_depth)) {
3163 +               pr_err("already stacked, %s (%s)\n",
3164 +                      add->pathname, au_sbtype(inode->i_sb));
3165 +               goto out;
3166 +       }
3167 +
3168 +       err = test_br(d_inode(add->path.dentry), add->perm, add->pathname);
3169 +       if (unlikely(err))
3170 +               goto out;
3171 +
3172 +       if (bbot < 0)
3173 +               return 0; /* success */
3174 +
3175 +       err = -EINVAL;
3176 +       for (bindex = 0; bindex <= bbot; bindex++)
3177 +               if (unlikely(test_overlap(sb, add->path.dentry,
3178 +                                         au_h_dptr(root, bindex)))) {
3179 +                       pr_err("%s is overlapped\n", add->pathname);
3180 +                       goto out;
3181 +               }
3182 +
3183 +       err = 0;
3184 +       if (au_opt_test(au_mntflags(sb), WARN_PERM)) {
3185 +               h_dentry = au_h_dptr(root, 0);
3186 +               h_inode = d_inode(h_dentry);
3187 +               if ((h_inode->i_mode & S_IALLUGO) != (inode->i_mode & S_IALLUGO)
3188 +                   || !uid_eq(h_inode->i_uid, inode->i_uid)
3189 +                   || !gid_eq(h_inode->i_gid, inode->i_gid))
3190 +                       pr_warn("uid/gid/perm %s %u/%u/0%o, %u/%u/0%o\n",
3191 +                               add->pathname,
3192 +                               i_uid_read(inode), i_gid_read(inode),
3193 +                               (inode->i_mode & S_IALLUGO),
3194 +                               i_uid_read(h_inode), i_gid_read(h_inode),
3195 +                               (h_inode->i_mode & S_IALLUGO));
3196 +       }
3197 +
3198 +out:
3199 +       return err;
3200 +}
3201 +
3202 +/*
3203 + * initialize or clean the whiteouts for an adding branch
3204 + */
3205 +static int au_br_init_wh(struct super_block *sb, struct au_branch *br,
3206 +                        int new_perm)
3207 +{
3208 +       int err, old_perm;
3209 +       aufs_bindex_t bindex;
3210 +       struct inode *h_inode;
3211 +       struct au_wbr *wbr;
3212 +       struct au_hinode *hdir;
3213 +       struct dentry *h_dentry;
3214 +
3215 +       err = vfsub_mnt_want_write(au_br_mnt(br));
3216 +       if (unlikely(err))
3217 +               goto out;
3218 +
3219 +       wbr = br->br_wbr;
3220 +       old_perm = br->br_perm;
3221 +       br->br_perm = new_perm;
3222 +       hdir = NULL;
3223 +       h_inode = NULL;
3224 +       bindex = au_br_index(sb, br->br_id);
3225 +       if (0 <= bindex) {
3226 +               hdir = au_hi(d_inode(sb->s_root), bindex);
3227 +               au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
3228 +       } else {
3229 +               h_dentry = au_br_dentry(br);
3230 +               h_inode = d_inode(h_dentry);
3231 +               inode_lock_nested(h_inode, AuLsc_I_PARENT);
3232 +       }
3233 +       if (!wbr)
3234 +               err = au_wh_init(br, sb);
3235 +       else {
3236 +               wbr_wh_write_lock(wbr);
3237 +               err = au_wh_init(br, sb);
3238 +               wbr_wh_write_unlock(wbr);
3239 +       }
3240 +       if (hdir)
3241 +               au_hn_inode_unlock(hdir);
3242 +       else
3243 +               inode_unlock(h_inode);
3244 +       vfsub_mnt_drop_write(au_br_mnt(br));
3245 +       br->br_perm = old_perm;
3246 +
3247 +       if (!err && wbr && !au_br_writable(new_perm)) {
3248 +               au_kfree_rcu(wbr);
3249 +               br->br_wbr = NULL;
3250 +       }
3251 +
3252 +out:
3253 +       return err;
3254 +}
3255 +
3256 +static int au_wbr_init(struct au_branch *br, struct super_block *sb,
3257 +                      int perm)
3258 +{
3259 +       int err;
3260 +       struct kstatfs kst;
3261 +       struct au_wbr *wbr;
3262 +
3263 +       wbr = br->br_wbr;
3264 +       au_rw_init(&wbr->wbr_wh_rwsem);
3265 +       atomic_set(&wbr->wbr_wh_running, 0);
3266 +
3267 +       /*
3268 +        * a limit for rmdir/rename a dir
3269 +        * cf. AUFS_MAX_NAMELEN in include/uapi/linux/aufs_type.h
3270 +        */
3271 +       err = vfs_statfs(&br->br_path, &kst);
3272 +       if (unlikely(err))
3273 +               goto out;
3274 +       err = -EINVAL;
3275 +       if (kst.f_namelen >= NAME_MAX)
3276 +               err = au_br_init_wh(sb, br, perm);
3277 +       else
3278 +               pr_err("%pd(%s), unsupported namelen %ld\n",
3279 +                      au_br_dentry(br),
3280 +                      au_sbtype(au_br_dentry(br)->d_sb), kst.f_namelen);
3281 +
3282 +out:
3283 +       return err;
3284 +}
3285 +
3286 +/* initialize a new branch */
3287 +static int au_br_init(struct au_branch *br, struct super_block *sb,
3288 +                     struct au_opt_add *add)
3289 +{
3290 +       int err;
3291 +       struct au_branch *brbase;
3292 +       struct file *xf;
3293 +       struct inode *h_inode;
3294 +
3295 +       err = 0;
3296 +       br->br_perm = add->perm;
3297 +       br->br_path = add->path; /* set first, path_get() later */
3298 +       spin_lock_init(&br->br_dykey_lock);
3299 +       au_lcnt_init(&br->br_nfiles, /*release*/NULL);
3300 +       au_lcnt_init(&br->br_count, /*release*/NULL);
3301 +       br->br_id = au_new_br_id(sb);
3302 +       AuDebugOn(br->br_id < 0);
3303 +
3304 +       /* always, regardless the given option */
3305 +       err = au_dr_br_init(sb, br, &add->path);
3306 +       if (unlikely(err))
3307 +               goto out_err;
3308 +
3309 +       if (au_br_writable(add->perm)) {
3310 +               err = au_wbr_init(br, sb, add->perm);
3311 +               if (unlikely(err))
3312 +                       goto out_err;
3313 +       }
3314 +
3315 +       if (au_opt_test(au_mntflags(sb), XINO)) {
3316 +               brbase = au_sbr(sb, 0);
3317 +               xf = au_xino_file(brbase->br_xino, /*idx*/-1);
3318 +               AuDebugOn(!xf);
3319 +               h_inode = d_inode(add->path.dentry);
3320 +               err = au_xino_init_br(sb, br, h_inode->i_ino, &xf->f_path);
3321 +               if (unlikely(err)) {
3322 +                       AuDebugOn(au_xino_file(br->br_xino, /*idx*/-1));
3323 +                       goto out_err;
3324 +               }
3325 +       }
3326 +
3327 +       sysaufs_br_init(br);
3328 +       path_get(&br->br_path);
3329 +       goto out; /* success */
3330 +
3331 +out_err:
3332 +       memset(&br->br_path, 0, sizeof(br->br_path));
3333 +out:
3334 +       return err;
3335 +}
3336 +
3337 +static void au_br_do_add_brp(struct au_sbinfo *sbinfo, aufs_bindex_t bindex,
3338 +                            struct au_branch *br, aufs_bindex_t bbot,
3339 +                            aufs_bindex_t amount)
3340 +{
3341 +       struct au_branch **brp;
3342 +
3343 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
3344 +
3345 +       brp = sbinfo->si_branch + bindex;
3346 +       memmove(brp + 1, brp, sizeof(*brp) * amount);
3347 +       *brp = br;
3348 +       sbinfo->si_bbot++;
3349 +       if (unlikely(bbot < 0))
3350 +               sbinfo->si_bbot = 0;
3351 +}
3352 +
3353 +static void au_br_do_add_hdp(struct au_dinfo *dinfo, aufs_bindex_t bindex,
3354 +                            aufs_bindex_t bbot, aufs_bindex_t amount)
3355 +{
3356 +       struct au_hdentry *hdp;
3357 +
3358 +       AuRwMustWriteLock(&dinfo->di_rwsem);
3359 +
3360 +       hdp = au_hdentry(dinfo, bindex);
3361 +       memmove(hdp + 1, hdp, sizeof(*hdp) * amount);
3362 +       au_h_dentry_init(hdp);
3363 +       dinfo->di_bbot++;
3364 +       if (unlikely(bbot < 0))
3365 +               dinfo->di_btop = 0;
3366 +}
3367 +
3368 +static void au_br_do_add_hip(struct au_iinfo *iinfo, aufs_bindex_t bindex,
3369 +                            aufs_bindex_t bbot, aufs_bindex_t amount)
3370 +{
3371 +       struct au_hinode *hip;
3372 +
3373 +       AuRwMustWriteLock(&iinfo->ii_rwsem);
3374 +
3375 +       hip = au_hinode(iinfo, bindex);
3376 +       memmove(hip + 1, hip, sizeof(*hip) * amount);
3377 +       au_hinode_init(hip);
3378 +       iinfo->ii_bbot++;
3379 +       if (unlikely(bbot < 0))
3380 +               iinfo->ii_btop = 0;
3381 +}
3382 +
3383 +static void au_br_do_add(struct super_block *sb, struct au_branch *br,
3384 +                        aufs_bindex_t bindex)
3385 +{
3386 +       struct dentry *root, *h_dentry;
3387 +       struct inode *root_inode, *h_inode;
3388 +       aufs_bindex_t bbot, amount;
3389 +
3390 +       root = sb->s_root;
3391 +       root_inode = d_inode(root);
3392 +       bbot = au_sbbot(sb);
3393 +       amount = bbot + 1 - bindex;
3394 +       h_dentry = au_br_dentry(br);
3395 +       au_sbilist_lock();
3396 +       au_br_do_add_brp(au_sbi(sb), bindex, br, bbot, amount);
3397 +       au_br_do_add_hdp(au_di(root), bindex, bbot, amount);
3398 +       au_br_do_add_hip(au_ii(root_inode), bindex, bbot, amount);
3399 +       au_set_h_dptr(root, bindex, dget(h_dentry));
3400 +       h_inode = d_inode(h_dentry);
3401 +       au_set_h_iptr(root_inode, bindex, au_igrab(h_inode), /*flags*/0);
3402 +       au_sbilist_unlock();
3403 +}
3404 +
3405 +int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount)
3406 +{
3407 +       int err;
3408 +       aufs_bindex_t bbot, add_bindex;
3409 +       struct dentry *root, *h_dentry;
3410 +       struct inode *root_inode;
3411 +       struct au_branch *add_branch;
3412 +
3413 +       root = sb->s_root;
3414 +       root_inode = d_inode(root);
3415 +       IMustLock(root_inode);
3416 +       IiMustWriteLock(root_inode);
3417 +       err = test_add(sb, add, remount);
3418 +       if (unlikely(err < 0))
3419 +               goto out;
3420 +       if (err) {
3421 +               err = 0;
3422 +               goto out; /* success */
3423 +       }
3424 +
3425 +       bbot = au_sbbot(sb);
3426 +       add_branch = au_br_alloc(sb, bbot + 2, add->perm);
3427 +       err = PTR_ERR(add_branch);
3428 +       if (IS_ERR(add_branch))
3429 +               goto out;
3430 +
3431 +       err = au_br_init(add_branch, sb, add);
3432 +       if (unlikely(err)) {
3433 +               au_br_do_free(add_branch);
3434 +               goto out;
3435 +       }
3436 +
3437 +       add_bindex = add->bindex;
3438 +       sysaufs_brs_del(sb, add_bindex);        /* remove successors */
3439 +       au_br_do_add(sb, add_branch, add_bindex);
3440 +       sysaufs_brs_add(sb, add_bindex);        /* append successors */
3441 +       dbgaufs_brs_add(sb, add_bindex, /*topdown*/0);  /* rename successors */
3442 +
3443 +       h_dentry = add->path.dentry;
3444 +       if (!add_bindex) {
3445 +               au_cpup_attr_all(root_inode, /*force*/1);
3446 +               sb->s_maxbytes = h_dentry->d_sb->s_maxbytes;
3447 +       } else
3448 +               au_add_nlink(root_inode, d_inode(h_dentry));
3449 +
3450 +out:
3451 +       return err;
3452 +}
3453 +
3454 +/* ---------------------------------------------------------------------- */
3455 +
3456 +static unsigned long long au_farray_cb(struct super_block *sb, void *a,
3457 +                                      unsigned long long max __maybe_unused,
3458 +                                      void *arg)
3459 +{
3460 +       unsigned long long n;
3461 +       struct file **p, *f;
3462 +       struct hlist_bl_head *files;
3463 +       struct hlist_bl_node *pos;
3464 +       struct au_finfo *finfo;
3465 +
3466 +       n = 0;
3467 +       p = a;
3468 +       files = &au_sbi(sb)->si_files;
3469 +       hlist_bl_lock(files);
3470 +       hlist_bl_for_each_entry(finfo, pos, files, fi_hlist) {
3471 +               f = finfo->fi_file;
3472 +               if (file_count(f)
3473 +                   && !special_file(file_inode(f)->i_mode)) {
3474 +                       get_file(f);
3475 +                       *p++ = f;
3476 +                       n++;
3477 +                       AuDebugOn(n > max);
3478 +               }
3479 +       }
3480 +       hlist_bl_unlock(files);
3481 +
3482 +       return n;
3483 +}
3484 +
3485 +static struct file **au_farray_alloc(struct super_block *sb,
3486 +                                    unsigned long long *max)
3487 +{
3488 +       struct au_sbinfo *sbi;
3489 +
3490 +       sbi = au_sbi(sb);
3491 +       *max = au_lcnt_read(&sbi->si_nfiles, /*do_rev*/1);
3492 +       return au_array_alloc(max, au_farray_cb, sb, /*arg*/NULL);
3493 +}
3494 +
3495 +static void au_farray_free(struct file **a, unsigned long long max)
3496 +{
3497 +       unsigned long long ull;
3498 +
3499 +       for (ull = 0; ull < max; ull++)
3500 +               if (a[ull])
3501 +                       fput(a[ull]);
3502 +       kvfree(a);
3503 +}
3504 +
3505 +/* ---------------------------------------------------------------------- */
3506 +
3507 +/*
3508 + * delete a branch
3509 + */
3510 +
3511 +/* to show the line number, do not make it inlined function */
3512 +#define AuVerbose(do_info, fmt, ...) do { \
3513 +       if (do_info) \
3514 +               pr_info(fmt, ##__VA_ARGS__); \
3515 +} while (0)
3516 +
3517 +static int au_test_ibusy(struct inode *inode, aufs_bindex_t btop,
3518 +                        aufs_bindex_t bbot)
3519 +{
3520 +       return (inode && !S_ISDIR(inode->i_mode)) || btop == bbot;
3521 +}
3522 +
3523 +static int au_test_dbusy(struct dentry *dentry, aufs_bindex_t btop,
3524 +                        aufs_bindex_t bbot)
3525 +{
3526 +       return au_test_ibusy(d_inode(dentry), btop, bbot);
3527 +}
3528 +
3529 +/*
3530 + * test if the branch is deletable or not.
3531 + */
3532 +static int test_dentry_busy(struct dentry *root, aufs_bindex_t bindex,
3533 +                           unsigned int sigen, const unsigned int verbose)
3534 +{
3535 +       int err, i, j, ndentry;
3536 +       aufs_bindex_t btop, bbot;
3537 +       struct au_dcsub_pages dpages;
3538 +       struct au_dpage *dpage;
3539 +       struct dentry *d;
3540 +
3541 +       err = au_dpages_init(&dpages, GFP_NOFS);
3542 +       if (unlikely(err))
3543 +               goto out;
3544 +       err = au_dcsub_pages(&dpages, root, NULL, NULL);
3545 +       if (unlikely(err))
3546 +               goto out_dpages;
3547 +
3548 +       for (i = 0; !err && i < dpages.ndpage; i++) {
3549 +               dpage = dpages.dpages + i;
3550 +               ndentry = dpage->ndentry;
3551 +               for (j = 0; !err && j < ndentry; j++) {
3552 +                       d = dpage->dentries[j];
3553 +                       AuDebugOn(au_dcount(d) <= 0);
3554 +                       if (!au_digen_test(d, sigen)) {
3555 +                               di_read_lock_child(d, AuLock_IR);
3556 +                               if (unlikely(au_dbrange_test(d))) {
3557 +                                       di_read_unlock(d, AuLock_IR);
3558 +                                       continue;
3559 +                               }
3560 +                       } else {
3561 +                               di_write_lock_child(d);
3562 +                               if (unlikely(au_dbrange_test(d))) {
3563 +                                       di_write_unlock(d);
3564 +                                       continue;
3565 +                               }
3566 +                               err = au_reval_dpath(d, sigen);
3567 +                               if (!err)
3568 +                                       di_downgrade_lock(d, AuLock_IR);
3569 +                               else {
3570 +                                       di_write_unlock(d);
3571 +                                       break;
3572 +                               }
3573 +                       }
3574 +
3575 +                       /* AuDbgDentry(d); */
3576 +                       btop = au_dbtop(d);
3577 +                       bbot = au_dbbot(d);
3578 +                       if (btop <= bindex
3579 +                           && bindex <= bbot
3580 +                           && au_h_dptr(d, bindex)
3581 +                           && au_test_dbusy(d, btop, bbot)) {
3582 +                               err = -EBUSY;
3583 +                               AuVerbose(verbose, "busy %pd\n", d);
3584 +                               AuDbgDentry(d);
3585 +                       }
3586 +                       di_read_unlock(d, AuLock_IR);
3587 +               }
3588 +       }
3589 +
3590 +out_dpages:
3591 +       au_dpages_free(&dpages);
3592 +out:
3593 +       return err;
3594 +}
3595 +
3596 +static int test_inode_busy(struct super_block *sb, aufs_bindex_t bindex,
3597 +                          unsigned int sigen, const unsigned int verbose)
3598 +{
3599 +       int err;
3600 +       unsigned long long max, ull;
3601 +       struct inode *i, **array;
3602 +       aufs_bindex_t btop, bbot;
3603 +
3604 +       array = au_iarray_alloc(sb, &max);
3605 +       err = PTR_ERR(array);
3606 +       if (IS_ERR(array))
3607 +               goto out;
3608 +
3609 +       err = 0;
3610 +       AuDbg("b%d\n", bindex);
3611 +       for (ull = 0; !err && ull < max; ull++) {
3612 +               i = array[ull];
3613 +               if (unlikely(!i))
3614 +                       break;
3615 +               if (i->i_ino == AUFS_ROOT_INO)
3616 +                       continue;
3617 +
3618 +               /* AuDbgInode(i); */
3619 +               if (au_iigen(i, NULL) == sigen)
3620 +                       ii_read_lock_child(i);
3621 +               else {
3622 +                       ii_write_lock_child(i);
3623 +                       err = au_refresh_hinode_self(i);
3624 +                       au_iigen_dec(i);
3625 +                       if (!err)
3626 +                               ii_downgrade_lock(i);
3627 +                       else {
3628 +                               ii_write_unlock(i);
3629 +                               break;
3630 +                       }
3631 +               }
3632 +
3633 +               btop = au_ibtop(i);
3634 +               bbot = au_ibbot(i);
3635 +               if (btop <= bindex
3636 +                   && bindex <= bbot
3637 +                   && au_h_iptr(i, bindex)
3638 +                   && au_test_ibusy(i, btop, bbot)) {
3639 +                       err = -EBUSY;
3640 +                       AuVerbose(verbose, "busy i%lu\n", i->i_ino);
3641 +                       AuDbgInode(i);
3642 +               }
3643 +               ii_read_unlock(i);
3644 +       }
3645 +       au_iarray_free(array, max);
3646 +
3647 +out:
3648 +       return err;
3649 +}
3650 +
3651 +static int test_children_busy(struct dentry *root, aufs_bindex_t bindex,
3652 +                             const unsigned int verbose)
3653 +{
3654 +       int err;
3655 +       unsigned int sigen;
3656 +
3657 +       sigen = au_sigen(root->d_sb);
3658 +       DiMustNoWaiters(root);
3659 +       IiMustNoWaiters(d_inode(root));
3660 +       di_write_unlock(root);
3661 +       err = test_dentry_busy(root, bindex, sigen, verbose);
3662 +       if (!err)
3663 +               err = test_inode_busy(root->d_sb, bindex, sigen, verbose);
3664 +       di_write_lock_child(root); /* aufs_write_lock() calls ..._child() */
3665 +
3666 +       return err;
3667 +}
3668 +
3669 +static int test_dir_busy(struct file *file, aufs_bindex_t br_id,
3670 +                        struct file **to_free, int *idx)
3671 +{
3672 +       int err;
3673 +       unsigned char matched, root;
3674 +       aufs_bindex_t bindex, bbot;
3675 +       struct au_fidir *fidir;
3676 +       struct au_hfile *hfile;
3677 +
3678 +       err = 0;
3679 +       root = IS_ROOT(file->f_path.dentry);
3680 +       if (root) {
3681 +               get_file(file);
3682 +               to_free[*idx] = file;
3683 +               (*idx)++;
3684 +               goto out;
3685 +       }
3686 +
3687 +       matched = 0;
3688 +       fidir = au_fi(file)->fi_hdir;
3689 +       AuDebugOn(!fidir);
3690 +       bbot = au_fbbot_dir(file);
3691 +       for (bindex = au_fbtop(file); bindex <= bbot; bindex++) {
3692 +               hfile = fidir->fd_hfile + bindex;
3693 +               if (!hfile->hf_file)
3694 +                       continue;
3695 +
3696 +               if (hfile->hf_br->br_id == br_id) {
3697 +                       matched = 1;
3698 +                       break;
3699 +               }
3700 +       }
3701 +       if (matched)
3702 +               err = -EBUSY;
3703 +
3704 +out:
3705 +       return err;
3706 +}
3707 +
3708 +static int test_file_busy(struct super_block *sb, aufs_bindex_t br_id,
3709 +                         struct file **to_free, int opened)
3710 +{
3711 +       int err, idx;
3712 +       unsigned long long ull, max;
3713 +       aufs_bindex_t btop;
3714 +       struct file *file, **array;
3715 +       struct dentry *root;
3716 +       struct au_hfile *hfile;
3717 +
3718 +       array = au_farray_alloc(sb, &max);
3719 +       err = PTR_ERR(array);
3720 +       if (IS_ERR(array))
3721 +               goto out;
3722 +
3723 +       err = 0;
3724 +       idx = 0;
3725 +       root = sb->s_root;
3726 +       di_write_unlock(root);
3727 +       for (ull = 0; ull < max; ull++) {
3728 +               file = array[ull];
3729 +               if (unlikely(!file))
3730 +                       break;
3731 +
3732 +               /* AuDbg("%pD\n", file); */
3733 +               fi_read_lock(file);
3734 +               btop = au_fbtop(file);
3735 +               if (!d_is_dir(file->f_path.dentry)) {
3736 +                       hfile = &au_fi(file)->fi_htop;
3737 +                       if (hfile->hf_br->br_id == br_id)
3738 +                               err = -EBUSY;
3739 +               } else
3740 +                       err = test_dir_busy(file, br_id, to_free, &idx);
3741 +               fi_read_unlock(file);
3742 +               if (unlikely(err))
3743 +                       break;
3744 +       }
3745 +       di_write_lock_child(root);
3746 +       au_farray_free(array, max);
3747 +       AuDebugOn(idx > opened);
3748 +
3749 +out:
3750 +       return err;
3751 +}
3752 +
3753 +static void br_del_file(struct file **to_free, unsigned long long opened,
3754 +                       aufs_bindex_t br_id)
3755 +{
3756 +       unsigned long long ull;
3757 +       aufs_bindex_t bindex, btop, bbot, bfound;
3758 +       struct file *file;
3759 +       struct au_fidir *fidir;
3760 +       struct au_hfile *hfile;
3761 +
3762 +       for (ull = 0; ull < opened; ull++) {
3763 +               file = to_free[ull];
3764 +               if (unlikely(!file))
3765 +                       break;
3766 +
3767 +               /* AuDbg("%pD\n", file); */
3768 +               AuDebugOn(!d_is_dir(file->f_path.dentry));
3769 +               bfound = -1;
3770 +               fidir = au_fi(file)->fi_hdir;
3771 +               AuDebugOn(!fidir);
3772 +               fi_write_lock(file);
3773 +               btop = au_fbtop(file);
3774 +               bbot = au_fbbot_dir(file);
3775 +               for (bindex = btop; bindex <= bbot; bindex++) {
3776 +                       hfile = fidir->fd_hfile + bindex;
3777 +                       if (!hfile->hf_file)
3778 +                               continue;
3779 +
3780 +                       if (hfile->hf_br->br_id == br_id) {
3781 +                               bfound = bindex;
3782 +                               break;
3783 +                       }
3784 +               }
3785 +               AuDebugOn(bfound < 0);
3786 +               au_set_h_fptr(file, bfound, NULL);
3787 +               if (bfound == btop) {
3788 +                       for (btop++; btop <= bbot; btop++)
3789 +                               if (au_hf_dir(file, btop)) {
3790 +                                       au_set_fbtop(file, btop);
3791 +                                       break;
3792 +                               }
3793 +               }
3794 +               fi_write_unlock(file);
3795 +       }
3796 +}
3797 +
3798 +static void au_br_do_del_brp(struct au_sbinfo *sbinfo,
3799 +                            const aufs_bindex_t bindex,
3800 +                            const aufs_bindex_t bbot)
3801 +{
3802 +       struct au_branch **brp, **p;
3803 +
3804 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
3805 +
3806 +       brp = sbinfo->si_branch + bindex;
3807 +       if (bindex < bbot)
3808 +               memmove(brp, brp + 1, sizeof(*brp) * (bbot - bindex));
3809 +       sbinfo->si_branch[0 + bbot] = NULL;
3810 +       sbinfo->si_bbot--;
3811 +
3812 +       p = au_krealloc(sbinfo->si_branch, sizeof(*p) * bbot, AuGFP_SBILIST,
3813 +                       /*may_shrink*/1);
3814 +       if (p)
3815 +               sbinfo->si_branch = p;
3816 +       /* harmless error */
3817 +}
3818 +
3819 +static void au_br_do_del_hdp(struct au_dinfo *dinfo, const aufs_bindex_t bindex,
3820 +                            const aufs_bindex_t bbot)
3821 +{
3822 +       struct au_hdentry *hdp, *p;
3823 +
3824 +       AuRwMustWriteLock(&dinfo->di_rwsem);
3825 +
3826 +       hdp = au_hdentry(dinfo, bindex);
3827 +       if (bindex < bbot)
3828 +               memmove(hdp, hdp + 1, sizeof(*hdp) * (bbot - bindex));
3829 +       /* au_h_dentry_init(au_hdentry(dinfo, bbot); */
3830 +       dinfo->di_bbot--;
3831 +
3832 +       p = au_krealloc(dinfo->di_hdentry, sizeof(*p) * bbot, AuGFP_SBILIST,
3833 +                       /*may_shrink*/1);
3834 +       if (p)
3835 +               dinfo->di_hdentry = p;
3836 +       /* harmless error */
3837 +}
3838 +
3839 +static void au_br_do_del_hip(struct au_iinfo *iinfo, const aufs_bindex_t bindex,
3840 +                            const aufs_bindex_t bbot)
3841 +{
3842 +       struct au_hinode *hip, *p;
3843 +
3844 +       AuRwMustWriteLock(&iinfo->ii_rwsem);
3845 +
3846 +       hip = au_hinode(iinfo, bindex);
3847 +       if (bindex < bbot)
3848 +               memmove(hip, hip + 1, sizeof(*hip) * (bbot - bindex));
3849 +       /* au_hinode_init(au_hinode(iinfo, bbot)); */
3850 +       iinfo->ii_bbot--;
3851 +
3852 +       p = au_krealloc(iinfo->ii_hinode, sizeof(*p) * bbot, AuGFP_SBILIST,
3853 +                       /*may_shrink*/1);
3854 +       if (p)
3855 +               iinfo->ii_hinode = p;
3856 +       /* harmless error */
3857 +}
3858 +
3859 +static void au_br_do_del(struct super_block *sb, aufs_bindex_t bindex,
3860 +                        struct au_branch *br)
3861 +{
3862 +       aufs_bindex_t bbot;
3863 +       struct au_sbinfo *sbinfo;
3864 +       struct dentry *root, *h_root;
3865 +       struct inode *inode, *h_inode;
3866 +       struct au_hinode *hinode;
3867 +
3868 +       SiMustWriteLock(sb);
3869 +
3870 +       root = sb->s_root;
3871 +       inode = d_inode(root);
3872 +       sbinfo = au_sbi(sb);
3873 +       bbot = sbinfo->si_bbot;
3874 +
3875 +       h_root = au_h_dptr(root, bindex);
3876 +       hinode = au_hi(inode, bindex);
3877 +       h_inode = au_igrab(hinode->hi_inode);
3878 +       au_hiput(hinode);
3879 +
3880 +       au_sbilist_lock();
3881 +       au_br_do_del_brp(sbinfo, bindex, bbot);
3882 +       au_br_do_del_hdp(au_di(root), bindex, bbot);
3883 +       au_br_do_del_hip(au_ii(inode), bindex, bbot);
3884 +       au_sbilist_unlock();
3885 +
3886 +       /* ignore an error */
3887 +       au_dr_br_fin(sb, br); /* always, regardless the mount option */
3888 +
3889 +       dput(h_root);
3890 +       iput(h_inode);
3891 +       au_br_do_free(br);
3892 +}
3893 +
3894 +static unsigned long long empty_cb(struct super_block *sb, void *array,
3895 +                                  unsigned long long max, void *arg)
3896 +{
3897 +       return max;
3898 +}
3899 +
3900 +int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount)
3901 +{
3902 +       int err, rerr, i;
3903 +       unsigned long long opened;
3904 +       unsigned int mnt_flags;
3905 +       aufs_bindex_t bindex, bbot, br_id;
3906 +       unsigned char do_wh, verbose;
3907 +       struct au_branch *br;
3908 +       struct au_wbr *wbr;
3909 +       struct dentry *root;
3910 +       struct file **to_free;
3911 +
3912 +       err = 0;
3913 +       opened = 0;
3914 +       to_free = NULL;
3915 +       root = sb->s_root;
3916 +       bindex = au_find_dbindex(root, del->h_path.dentry);
3917 +       if (bindex < 0) {
3918 +               if (remount)
3919 +                       goto out; /* success */
3920 +               err = -ENOENT;
3921 +               pr_err("%s no such branch\n", del->pathname);
3922 +               goto out;
3923 +       }
3924 +       AuDbg("bindex b%d\n", bindex);
3925 +
3926 +       err = -EBUSY;
3927 +       mnt_flags = au_mntflags(sb);
3928 +       verbose = !!au_opt_test(mnt_flags, VERBOSE);
3929 +       bbot = au_sbbot(sb);
3930 +       if (unlikely(!bbot)) {
3931 +               AuVerbose(verbose, "no more branches left\n");
3932 +               goto out;
3933 +       }
3934 +
3935 +       br = au_sbr(sb, bindex);
3936 +       AuDebugOn(!path_equal(&br->br_path, &del->h_path));
3937 +       if (unlikely(au_lcnt_read(&br->br_count, /*do_rev*/1))) {
3938 +               AuVerbose(verbose, "br %pd2 is busy now\n", del->h_path.dentry);
3939 +               goto out;
3940 +       }
3941 +
3942 +       br_id = br->br_id;
3943 +       opened = au_lcnt_read(&br->br_nfiles, /*do_rev*/1);
3944 +       if (unlikely(opened)) {
3945 +               to_free = au_array_alloc(&opened, empty_cb, sb, NULL);
3946 +               err = PTR_ERR(to_free);
3947 +               if (IS_ERR(to_free))
3948 +                       goto out;
3949 +
3950 +               err = test_file_busy(sb, br_id, to_free, opened);
3951 +               if (unlikely(err)) {
3952 +                       AuVerbose(verbose, "%llu file(s) opened\n", opened);
3953 +                       goto out;
3954 +               }
3955 +       }
3956 +
3957 +       wbr = br->br_wbr;
3958 +       do_wh = wbr && (wbr->wbr_whbase || wbr->wbr_plink || wbr->wbr_orph);
3959 +       if (do_wh) {
3960 +               /* instead of WbrWhMustWriteLock(wbr) */
3961 +               SiMustWriteLock(sb);
3962 +               for (i = 0; i < AuBrWh_Last; i++) {
3963 +                       dput(wbr->wbr_wh[i]);
3964 +                       wbr->wbr_wh[i] = NULL;
3965 +               }
3966 +       }
3967 +
3968 +       err = test_children_busy(root, bindex, verbose);
3969 +       if (unlikely(err)) {
3970 +               if (do_wh)
3971 +                       goto out_wh;
3972 +               goto out;
3973 +       }
3974 +
3975 +       err = 0;
3976 +       if (to_free) {
3977 +               /*
3978 +                * now we confirmed the branch is deletable.
3979 +                * let's free the remaining opened dirs on the branch.
3980 +                */
3981 +               di_write_unlock(root);
3982 +               br_del_file(to_free, opened, br_id);
3983 +               di_write_lock_child(root);
3984 +       }
3985 +
3986 +       sysaufs_brs_del(sb, bindex);    /* remove successors */
3987 +       dbgaufs_xino_del(br);           /* remove one */
3988 +       au_br_do_del(sb, bindex, br);
3989 +       sysaufs_brs_add(sb, bindex);    /* append successors */
3990 +       dbgaufs_brs_add(sb, bindex, /*topdown*/1);      /* rename successors */
3991 +
3992 +       if (!bindex) {
3993 +               au_cpup_attr_all(d_inode(root), /*force*/1);
3994 +               sb->s_maxbytes = au_sbr_sb(sb, 0)->s_maxbytes;
3995 +       } else
3996 +               au_sub_nlink(d_inode(root), d_inode(del->h_path.dentry));
3997 +       if (au_opt_test(mnt_flags, PLINK))
3998 +               au_plink_half_refresh(sb, br_id);
3999 +
4000 +       goto out; /* success */
4001 +
4002 +out_wh:
4003 +       /* revert */
4004 +       rerr = au_br_init_wh(sb, br, br->br_perm);
4005 +       if (rerr)
4006 +               pr_warn("failed re-creating base whiteout, %s. (%d)\n",
4007 +                       del->pathname, rerr);
4008 +out:
4009 +       if (to_free)
4010 +               au_farray_free(to_free, opened);
4011 +       return err;
4012 +}
4013 +
4014 +/* ---------------------------------------------------------------------- */
4015 +
4016 +static int au_ibusy(struct super_block *sb, struct aufs_ibusy __user *arg)
4017 +{
4018 +       int err;
4019 +       aufs_bindex_t btop, bbot;
4020 +       struct aufs_ibusy ibusy;
4021 +       struct inode *inode, *h_inode;
4022 +
4023 +       err = -EPERM;
4024 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
4025 +               goto out;
4026 +
4027 +       err = copy_from_user(&ibusy, arg, sizeof(ibusy));
4028 +       if (!err)
4029 +               /* VERIFY_WRITE */
4030 +               err = !access_ok(&arg->h_ino, sizeof(arg->h_ino));
4031 +       if (unlikely(err)) {
4032 +               err = -EFAULT;
4033 +               AuTraceErr(err);
4034 +               goto out;
4035 +       }
4036 +
4037 +       err = -EINVAL;
4038 +       si_read_lock(sb, AuLock_FLUSH);
4039 +       if (unlikely(ibusy.bindex < 0 || ibusy.bindex > au_sbbot(sb)))
4040 +               goto out_unlock;
4041 +
4042 +       err = 0;
4043 +       ibusy.h_ino = 0; /* invalid */
4044 +       inode = ilookup(sb, ibusy.ino);
4045 +       if (!inode
4046 +           || inode->i_ino == AUFS_ROOT_INO
4047 +           || au_is_bad_inode(inode))
4048 +               goto out_unlock;
4049 +
4050 +       ii_read_lock_child(inode);
4051 +       btop = au_ibtop(inode);
4052 +       bbot = au_ibbot(inode);
4053 +       if (btop <= ibusy.bindex && ibusy.bindex <= bbot) {
4054 +               h_inode = au_h_iptr(inode, ibusy.bindex);
4055 +               if (h_inode && au_test_ibusy(inode, btop, bbot))
4056 +                       ibusy.h_ino = h_inode->i_ino;
4057 +       }
4058 +       ii_read_unlock(inode);
4059 +       iput(inode);
4060 +
4061 +out_unlock:
4062 +       si_read_unlock(sb);
4063 +       if (!err) {
4064 +               err = __put_user(ibusy.h_ino, &arg->h_ino);
4065 +               if (unlikely(err)) {
4066 +                       err = -EFAULT;
4067 +                       AuTraceErr(err);
4068 +               }
4069 +       }
4070 +out:
4071 +       return err;
4072 +}
4073 +
4074 +long au_ibusy_ioctl(struct file *file, unsigned long arg)
4075 +{
4076 +       return au_ibusy(file->f_path.dentry->d_sb, (void __user *)arg);
4077 +}
4078 +
4079 +#ifdef CONFIG_COMPAT
4080 +long au_ibusy_compat_ioctl(struct file *file, unsigned long arg)
4081 +{
4082 +       return au_ibusy(file->f_path.dentry->d_sb, compat_ptr(arg));
4083 +}
4084 +#endif
4085 +
4086 +/* ---------------------------------------------------------------------- */
4087 +
4088 +/*
4089 + * change a branch permission
4090 + */
4091 +
4092 +static void au_warn_ima(void)
4093 +{
4094 +#ifdef CONFIG_IMA
4095 +       /* since it doesn't support mark_files_ro() */
4096 +       AuWarn1("RW -> RO makes IMA to produce wrong message\n");
4097 +#endif
4098 +}
4099 +
4100 +static int do_need_sigen_inc(int a, int b)
4101 +{
4102 +       return au_br_whable(a) && !au_br_whable(b);
4103 +}
4104 +
4105 +static int need_sigen_inc(int old, int new)
4106 +{
4107 +       return do_need_sigen_inc(old, new)
4108 +               || do_need_sigen_inc(new, old);
4109 +}
4110 +
4111 +static int au_br_mod_files_ro(struct super_block *sb, aufs_bindex_t bindex)
4112 +{
4113 +       int err, do_warn;
4114 +       unsigned int mnt_flags;
4115 +       unsigned long long ull, max;
4116 +       aufs_bindex_t br_id;
4117 +       unsigned char verbose, writer;
4118 +       struct file *file, *hf, **array;
4119 +       struct au_hfile *hfile;
4120 +       struct inode *h_inode;
4121 +
4122 +       mnt_flags = au_mntflags(sb);
4123 +       verbose = !!au_opt_test(mnt_flags, VERBOSE);
4124 +
4125 +       array = au_farray_alloc(sb, &max);
4126 +       err = PTR_ERR(array);
4127 +       if (IS_ERR(array))
4128 +               goto out;
4129 +
4130 +       do_warn = 0;
4131 +       br_id = au_sbr_id(sb, bindex);
4132 +       for (ull = 0; ull < max; ull++) {
4133 +               file = array[ull];
4134 +               if (unlikely(!file))
4135 +                       break;
4136 +
4137 +               /* AuDbg("%pD\n", file); */
4138 +               fi_read_lock(file);
4139 +               if (unlikely(au_test_mmapped(file))) {
4140 +                       err = -EBUSY;
4141 +                       AuVerbose(verbose, "mmapped %pD\n", file);
4142 +                       AuDbgFile(file);
4143 +                       FiMustNoWaiters(file);
4144 +                       fi_read_unlock(file);
4145 +                       goto out_array;
4146 +               }
4147 +
4148 +               hfile = &au_fi(file)->fi_htop;
4149 +               hf = hfile->hf_file;
4150 +               if (!d_is_reg(file->f_path.dentry)
4151 +                   || !(file->f_mode & FMODE_WRITE)
4152 +                   || hfile->hf_br->br_id != br_id
4153 +                   || !(hf->f_mode & FMODE_WRITE))
4154 +                       array[ull] = NULL;
4155 +               else {
4156 +                       do_warn = 1;
4157 +                       get_file(file);
4158 +               }
4159 +
4160 +               FiMustNoWaiters(file);
4161 +               fi_read_unlock(file);
4162 +               fput(file);
4163 +       }
4164 +
4165 +       err = 0;
4166 +       if (do_warn)
4167 +               au_warn_ima();
4168 +
4169 +       for (ull = 0; ull < max; ull++) {
4170 +               file = array[ull];
4171 +               if (!file)
4172 +                       continue;
4173 +
4174 +               /* todo: already flushed? */
4175 +               /*
4176 +                * fs/super.c:mark_files_ro() is gone, but aufs keeps its
4177 +                * approach which resets f_mode and calls mnt_drop_write() and
4178 +                * file_release_write() for each file, because the branch
4179 +                * attribute in aufs world is totally different from the native
4180 +                * fs rw/ro mode.
4181 +               */
4182 +               /* fi_read_lock(file); */
4183 +               hfile = &au_fi(file)->fi_htop;
4184 +               hf = hfile->hf_file;
4185 +               /* fi_read_unlock(file); */
4186 +               spin_lock(&hf->f_lock);
4187 +               writer = !!(hf->f_mode & FMODE_WRITER);
4188 +               hf->f_mode &= ~(FMODE_WRITE | FMODE_WRITER);
4189 +               spin_unlock(&hf->f_lock);
4190 +               if (writer) {
4191 +                       h_inode = file_inode(hf);
4192 +                       if (hf->f_mode & FMODE_READ)
4193 +                               i_readcount_inc(h_inode);
4194 +                       put_write_access(h_inode);
4195 +                       __mnt_drop_write(hf->f_path.mnt);
4196 +               }
4197 +       }
4198 +
4199 +out_array:
4200 +       au_farray_free(array, max);
4201 +out:
4202 +       AuTraceErr(err);
4203 +       return err;
4204 +}
4205 +
4206 +int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
4207 +             int *do_refresh)
4208 +{
4209 +       int err, rerr;
4210 +       aufs_bindex_t bindex;
4211 +       struct dentry *root;
4212 +       struct au_branch *br;
4213 +       struct au_br_fhsm *bf;
4214 +
4215 +       root = sb->s_root;
4216 +       bindex = au_find_dbindex(root, mod->h_root);
4217 +       if (bindex < 0) {
4218 +               if (remount)
4219 +                       return 0; /* success */
4220 +               err = -ENOENT;
4221 +               pr_err("%s no such branch\n", mod->path);
4222 +               goto out;
4223 +       }
4224 +       AuDbg("bindex b%d\n", bindex);
4225 +
4226 +       err = test_br(d_inode(mod->h_root), mod->perm, mod->path);
4227 +       if (unlikely(err))
4228 +               goto out;
4229 +
4230 +       br = au_sbr(sb, bindex);
4231 +       AuDebugOn(mod->h_root != au_br_dentry(br));
4232 +       if (br->br_perm == mod->perm)
4233 +               return 0; /* success */
4234 +
4235 +       /* pre-allocate for non-fhsm --> fhsm */
4236 +       bf = NULL;
4237 +       if (!au_br_fhsm(br->br_perm) && au_br_fhsm(mod->perm)) {
4238 +               err = au_fhsm_br_alloc(br);
4239 +               if (unlikely(err))
4240 +                       goto out;
4241 +               bf = br->br_fhsm;
4242 +               br->br_fhsm = NULL;
4243 +       }
4244 +
4245 +       if (au_br_writable(br->br_perm)) {
4246 +               /* remove whiteout base */
4247 +               err = au_br_init_wh(sb, br, mod->perm);
4248 +               if (unlikely(err))
4249 +                       goto out_bf;
4250 +
4251 +               if (!au_br_writable(mod->perm)) {
4252 +                       /* rw --> ro, file might be mmapped */
4253 +                       DiMustNoWaiters(root);
4254 +                       IiMustNoWaiters(d_inode(root));
4255 +                       di_write_unlock(root);
4256 +                       err = au_br_mod_files_ro(sb, bindex);
4257 +                       /* aufs_write_lock() calls ..._child() */
4258 +                       di_write_lock_child(root);
4259 +
4260 +                       if (unlikely(err)) {
4261 +                               rerr = -ENOMEM;
4262 +                               br->br_wbr = kzalloc(sizeof(*br->br_wbr),
4263 +                                                    GFP_NOFS);
4264 +                               if (br->br_wbr)
4265 +                                       rerr = au_wbr_init(br, sb, br->br_perm);
4266 +                               if (unlikely(rerr)) {
4267 +                                       AuIOErr("nested error %d (%d)\n",
4268 +                                               rerr, err);
4269 +                                       br->br_perm = mod->perm;
4270 +                               }
4271 +                       }
4272 +               }
4273 +       } else if (au_br_writable(mod->perm)) {
4274 +               /* ro --> rw */
4275 +               err = -ENOMEM;
4276 +               br->br_wbr = kzalloc(sizeof(*br->br_wbr), GFP_NOFS);
4277 +               if (br->br_wbr) {
4278 +                       err = au_wbr_init(br, sb, mod->perm);
4279 +                       if (unlikely(err)) {
4280 +                               au_kfree_rcu(br->br_wbr);
4281 +                               br->br_wbr = NULL;
4282 +                       }
4283 +               }
4284 +       }
4285 +       if (unlikely(err))
4286 +               goto out_bf;
4287 +
4288 +       if (au_br_fhsm(br->br_perm)) {
4289 +               if (!au_br_fhsm(mod->perm)) {
4290 +                       /* fhsm --> non-fhsm */
4291 +                       au_br_fhsm_fin(br->br_fhsm);
4292 +                       au_kfree_rcu(br->br_fhsm);
4293 +                       br->br_fhsm = NULL;
4294 +               }
4295 +       } else if (au_br_fhsm(mod->perm))
4296 +               /* non-fhsm --> fhsm */
4297 +               br->br_fhsm = bf;
4298 +
4299 +       *do_refresh |= need_sigen_inc(br->br_perm, mod->perm);
4300 +       br->br_perm = mod->perm;
4301 +       goto out; /* success */
4302 +
4303 +out_bf:
4304 +       au_kfree_try_rcu(bf);
4305 +out:
4306 +       AuTraceErr(err);
4307 +       return err;
4308 +}
4309 +
4310 +/* ---------------------------------------------------------------------- */
4311 +
4312 +int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs)
4313 +{
4314 +       int err;
4315 +       struct kstatfs kstfs;
4316 +
4317 +       err = vfs_statfs(&br->br_path, &kstfs);
4318 +       if (!err) {
4319 +               stfs->f_blocks = kstfs.f_blocks;
4320 +               stfs->f_bavail = kstfs.f_bavail;
4321 +               stfs->f_files = kstfs.f_files;
4322 +               stfs->f_ffree = kstfs.f_ffree;
4323 +       }
4324 +
4325 +       return err;
4326 +}
4327 diff -urN /usr/share/empty/fs/aufs/branch.h linux/fs/aufs/branch.h
4328 --- /usr/share/empty/fs/aufs/branch.h   1970-01-01 01:00:00.000000000 +0100
4329 +++ linux/fs/aufs/branch.h      2022-03-21 14:49:05.719966344 +0100
4330 @@ -0,0 +1,375 @@
4331 +/* SPDX-License-Identifier: GPL-2.0 */
4332 +/*
4333 + * Copyright (C) 2005-2021 Junjiro R. Okajima
4334 + *
4335 + * This program, aufs is free software; you can redistribute it and/or modify
4336 + * it under the terms of the GNU General Public License as published by
4337 + * the Free Software Foundation; either version 2 of the License, or
4338 + * (at your option) any later version.
4339 + *
4340 + * This program is distributed in the hope that it will be useful,
4341 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
4342 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
4343 + * GNU General Public License for more details.
4344 + *
4345 + * You should have received a copy of the GNU General Public License
4346 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
4347 + */
4348 +
4349 +/*
4350 + * branch filesystems and xino for them
4351 + */
4352 +
4353 +#ifndef __AUFS_BRANCH_H__
4354 +#define __AUFS_BRANCH_H__
4355 +
4356 +#ifdef __KERNEL__
4357 +
4358 +#include <linux/mount.h>
4359 +#include "dirren.h"
4360 +#include "dynop.h"
4361 +#include "lcnt.h"
4362 +#include "rwsem.h"
4363 +#include "super.h"
4364 +
4365 +/* ---------------------------------------------------------------------- */
4366 +
4367 +/* a xino file */
4368 +struct au_xino {
4369 +       struct file             **xi_file;
4370 +       unsigned int            xi_nfile;
4371 +
4372 +       struct {
4373 +               spinlock_t              spin;
4374 +               ino_t                   *array;
4375 +               int                     total;
4376 +               /* reserved for future use */
4377 +               /* unsigned long        *bitmap; */
4378 +               wait_queue_head_t       wqh;
4379 +       } xi_nondir;
4380 +
4381 +       struct mutex            xi_mtx; /* protects xi_file array */
4382 +       struct hlist_bl_head    xi_writing;
4383 +
4384 +       atomic_t                xi_truncating;
4385 +
4386 +       struct kref             xi_kref;
4387 +};
4388 +
4389 +/* File-based Hierarchical Storage Management */
4390 +struct au_br_fhsm {
4391 +#ifdef CONFIG_AUFS_FHSM
4392 +       struct mutex            bf_lock;
4393 +       unsigned long           bf_jiffy;
4394 +       struct aufs_stfs        bf_stfs;
4395 +       int                     bf_readable;
4396 +#endif
4397 +};
4398 +
4399 +/* members for writable branch only */
4400 +enum {AuBrWh_BASE, AuBrWh_PLINK, AuBrWh_ORPH, AuBrWh_Last};
4401 +struct au_wbr {
4402 +       struct au_rwsem         wbr_wh_rwsem;
4403 +       struct dentry           *wbr_wh[AuBrWh_Last];
4404 +       atomic_t                wbr_wh_running;
4405 +#define wbr_whbase             wbr_wh[AuBrWh_BASE]     /* whiteout base */
4406 +#define wbr_plink              wbr_wh[AuBrWh_PLINK]    /* pseudo-link dir */
4407 +#define wbr_orph               wbr_wh[AuBrWh_ORPH]     /* dir for orphans */
4408 +
4409 +       /* mfs mode */
4410 +       unsigned long long      wbr_bytes;
4411 +};
4412 +
4413 +/* ext2 has 3 types of operations at least, ext3 has 4 */
4414 +#define AuBrDynOp (AuDyLast * 4)
4415 +
4416 +#ifdef CONFIG_AUFS_HFSNOTIFY
4417 +/* support for asynchronous destruction */
4418 +struct au_br_hfsnotify {
4419 +       struct fsnotify_group   *hfsn_group;
4420 +};
4421 +#endif
4422 +
4423 +/* sysfs entries */
4424 +struct au_brsysfs {
4425 +       char                    name[16];
4426 +       struct attribute        attr;
4427 +};
4428 +
4429 +enum {
4430 +       AuBrSysfs_BR,
4431 +       AuBrSysfs_BRID,
4432 +       AuBrSysfs_Last
4433 +};
4434 +
4435 +/* protected by superblock rwsem */
4436 +struct au_branch {
4437 +       struct au_xino          *br_xino;
4438 +
4439 +       aufs_bindex_t           br_id;
4440 +
4441 +       int                     br_perm;
4442 +       struct path             br_path;
4443 +       spinlock_t              br_dykey_lock;
4444 +       struct au_dykey         *br_dykey[AuBrDynOp];
4445 +       au_lcnt_t               br_nfiles;      /* opened files */
4446 +       au_lcnt_t               br_count;       /* in-use for other */
4447 +
4448 +       struct au_wbr           *br_wbr;
4449 +       struct au_br_fhsm       *br_fhsm;
4450 +
4451 +#ifdef CONFIG_AUFS_HFSNOTIFY
4452 +       struct au_br_hfsnotify  *br_hfsn;
4453 +#endif
4454 +
4455 +#ifdef CONFIG_SYSFS
4456 +       /* entries under sysfs per mount-point */
4457 +       struct au_brsysfs       br_sysfs[AuBrSysfs_Last];
4458 +#endif
4459 +
4460 +#ifdef CONFIG_DEBUG_FS
4461 +       struct dentry            *br_dbgaufs; /* xino */
4462 +#endif
4463 +
4464 +       struct au_dr_br         br_dirren;
4465 +};
4466 +
4467 +/* ---------------------------------------------------------------------- */
4468 +
4469 +static inline struct vfsmount *au_br_mnt(struct au_branch *br)
4470 +{
4471 +       return br->br_path.mnt;
4472 +}
4473 +
4474 +static inline struct dentry *au_br_dentry(struct au_branch *br)
4475 +{
4476 +       return br->br_path.dentry;
4477 +}
4478 +
4479 +static inline struct user_namespace *au_br_userns(struct au_branch *br)
4480 +{
4481 +       return mnt_user_ns(br->br_path.mnt);
4482 +}
4483 +
4484 +static inline struct super_block *au_br_sb(struct au_branch *br)
4485 +{
4486 +       return au_br_mnt(br)->mnt_sb;
4487 +}
4488 +
4489 +static inline int au_br_rdonly(struct au_branch *br)
4490 +{
4491 +       return (sb_rdonly(au_br_sb(br))
4492 +               || !au_br_writable(br->br_perm))
4493 +               ? -EROFS : 0;
4494 +}
4495 +
4496 +static inline int au_br_hnotifyable(int brperm __maybe_unused)
4497 +{
4498 +#ifdef CONFIG_AUFS_HNOTIFY
4499 +       return !(brperm & AuBrPerm_RR);
4500 +#else
4501 +       return 0;
4502 +#endif
4503 +}
4504 +
4505 +static inline int au_br_test_oflag(int oflag, struct au_branch *br)
4506 +{
4507 +       int err, exec_flag;
4508 +
4509 +       err = 0;
4510 +       exec_flag = oflag & __FMODE_EXEC;
4511 +       if (unlikely(exec_flag && path_noexec(&br->br_path)))
4512 +               err = -EACCES;
4513 +
4514 +       return err;
4515 +}
4516 +
4517 +static inline void au_xino_get(struct au_branch *br)
4518 +{
4519 +       struct au_xino *xi;
4520 +
4521 +       xi = br->br_xino;
4522 +       if (xi)
4523 +               kref_get(&xi->xi_kref);
4524 +}
4525 +
4526 +static inline int au_xino_count(struct au_branch *br)
4527 +{
4528 +       int v;
4529 +       struct au_xino *xi;
4530 +
4531 +       v = 0;
4532 +       xi = br->br_xino;
4533 +       if (xi)
4534 +               v = kref_read(&xi->xi_kref);
4535 +
4536 +       return v;
4537 +}
4538 +
4539 +/* ---------------------------------------------------------------------- */
4540 +
4541 +/* branch.c */
4542 +struct au_sbinfo;
4543 +void au_br_free(struct au_sbinfo *sinfo);
4544 +int au_br_index(struct super_block *sb, aufs_bindex_t br_id);
4545 +struct au_opt_add;
4546 +int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount);
4547 +struct au_opt_del;
4548 +int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount);
4549 +long au_ibusy_ioctl(struct file *file, unsigned long arg);
4550 +#ifdef CONFIG_COMPAT
4551 +long au_ibusy_compat_ioctl(struct file *file, unsigned long arg);
4552 +#endif
4553 +struct au_opt_mod;
4554 +int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
4555 +             int *do_refresh);
4556 +struct aufs_stfs;
4557 +int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs);
4558 +
4559 +/* xino.c */
4560 +static const loff_t au_loff_max = LLONG_MAX;
4561 +
4562 +aufs_bindex_t au_xi_root(struct super_block *sb, struct dentry *dentry);
4563 +struct file *au_xino_create(struct super_block *sb, char *fpath, int silent,
4564 +                           int wbrtop);
4565 +struct file *au_xino_create2(struct super_block *sb, struct path *base,
4566 +                            struct file *copy_src);
4567 +struct au_xi_new {
4568 +       struct au_xino *xi;     /* switch between xino and xigen */
4569 +       int idx;
4570 +       struct path *base;
4571 +       struct file *copy_src;
4572 +};
4573 +struct file *au_xi_new(struct super_block *sb, struct au_xi_new *xinew);
4574 +
4575 +int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4576 +                ino_t *ino);
4577 +int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4578 +                 ino_t ino);
4579 +ssize_t xino_fread(struct file *file, void *buf, size_t size, loff_t *pos);
4580 +ssize_t xino_fwrite(struct file *file, void *buf, size_t size, loff_t *pos);
4581 +
4582 +int au_xib_trunc(struct super_block *sb);
4583 +int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex, int idx_begin);
4584 +
4585 +struct au_xino *au_xino_alloc(unsigned int nfile);
4586 +int au_xino_put(struct au_branch *br);
4587 +struct file *au_xino_file1(struct au_xino *xi);
4588 +
4589 +struct au_opt_xino;
4590 +void au_xino_clr(struct super_block *sb);
4591 +int au_xino_set(struct super_block *sb, struct au_opt_xino *xiopt, int remount);
4592 +struct file *au_xino_def(struct super_block *sb);
4593 +int au_xino_init_br(struct super_block *sb, struct au_branch *br, ino_t hino,
4594 +                   struct path *base);
4595 +
4596 +ino_t au_xino_new_ino(struct super_block *sb);
4597 +void au_xino_delete_inode(struct inode *inode, const int unlinked);
4598 +
4599 +void au_xinondir_leave(struct super_block *sb, aufs_bindex_t bindex,
4600 +                      ino_t h_ino, int idx);
4601 +int au_xinondir_enter(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
4602 +                     int *idx);
4603 +
4604 +int au_xino_path(struct seq_file *seq, struct file *file);
4605 +
4606 +/* ---------------------------------------------------------------------- */
4607 +
4608 +/* @idx is signed to accept -1 meaning the first file */
4609 +static inline struct file *au_xino_file(struct au_xino *xi, int idx)
4610 +{
4611 +       struct file *file;
4612 +
4613 +       file = NULL;
4614 +       if (!xi)
4615 +               goto out;
4616 +
4617 +       if (idx >= 0) {
4618 +               if (idx < xi->xi_nfile)
4619 +                       file = xi->xi_file[idx];
4620 +       } else
4621 +               file = au_xino_file1(xi);
4622 +
4623 +out:
4624 +       return file;
4625 +}
4626 +
4627 +/* ---------------------------------------------------------------------- */
4628 +
4629 +/* Superblock to branch */
4630 +static inline
4631 +aufs_bindex_t au_sbr_id(struct super_block *sb, aufs_bindex_t bindex)
4632 +{
4633 +       return au_sbr(sb, bindex)->br_id;
4634 +}
4635 +
4636 +static inline
4637 +struct vfsmount *au_sbr_mnt(struct super_block *sb, aufs_bindex_t bindex)
4638 +{
4639 +       return au_br_mnt(au_sbr(sb, bindex));
4640 +}
4641 +
4642 +static inline
4643 +struct user_namespace *au_sbr_userns(struct super_block *sb, aufs_bindex_t bindex)
4644 +{
4645 +       return au_br_userns(au_sbr(sb, bindex));
4646 +}
4647 +
4648 +static inline
4649 +struct super_block *au_sbr_sb(struct super_block *sb, aufs_bindex_t bindex)
4650 +{
4651 +       return au_br_sb(au_sbr(sb, bindex));
4652 +}
4653 +
4654 +static inline int au_sbr_perm(struct super_block *sb, aufs_bindex_t bindex)
4655 +{
4656 +       return au_sbr(sb, bindex)->br_perm;
4657 +}
4658 +
4659 +static inline int au_sbr_whable(struct super_block *sb, aufs_bindex_t bindex)
4660 +{
4661 +       return au_br_whable(au_sbr_perm(sb, bindex));
4662 +}
4663 +
4664 +/* ---------------------------------------------------------------------- */
4665 +
4666 +#define wbr_wh_read_lock(wbr)  au_rw_read_lock(&(wbr)->wbr_wh_rwsem)
4667 +#define wbr_wh_write_lock(wbr) au_rw_write_lock(&(wbr)->wbr_wh_rwsem)
4668 +#define wbr_wh_read_trylock(wbr)       au_rw_read_trylock(&(wbr)->wbr_wh_rwsem)
4669 +#define wbr_wh_write_trylock(wbr) au_rw_write_trylock(&(wbr)->wbr_wh_rwsem)
4670 +/*
4671 +#define wbr_wh_read_trylock_nested(wbr) \
4672 +       au_rw_read_trylock_nested(&(wbr)->wbr_wh_rwsem)
4673 +#define wbr_wh_write_trylock_nested(wbr) \
4674 +       au_rw_write_trylock_nested(&(wbr)->wbr_wh_rwsem)
4675 +*/
4676 +
4677 +#define wbr_wh_read_unlock(wbr)        au_rw_read_unlock(&(wbr)->wbr_wh_rwsem)
4678 +#define wbr_wh_write_unlock(wbr)       au_rw_write_unlock(&(wbr)->wbr_wh_rwsem)
4679 +#define wbr_wh_downgrade_lock(wbr)     au_rw_dgrade_lock(&(wbr)->wbr_wh_rwsem)
4680 +
4681 +#define WbrWhMustNoWaiters(wbr)        AuRwMustNoWaiters(&(wbr)->wbr_wh_rwsem)
4682 +#define WbrWhMustAnyLock(wbr)  AuRwMustAnyLock(&(wbr)->wbr_wh_rwsem)
4683 +#define WbrWhMustWriteLock(wbr)        AuRwMustWriteLock(&(wbr)->wbr_wh_rwsem)
4684 +
4685 +/* ---------------------------------------------------------------------- */
4686 +
4687 +#ifdef CONFIG_AUFS_FHSM
4688 +static inline void au_br_fhsm_init(struct au_br_fhsm *brfhsm)
4689 +{
4690 +       mutex_init(&brfhsm->bf_lock);
4691 +       brfhsm->bf_jiffy = 0;
4692 +       brfhsm->bf_readable = 0;
4693 +}
4694 +
4695 +static inline void au_br_fhsm_fin(struct au_br_fhsm *brfhsm)
4696 +{
4697 +       mutex_destroy(&brfhsm->bf_lock);
4698 +}
4699 +#else
4700 +AuStubVoid(au_br_fhsm_init, struct au_br_fhsm *brfhsm)
4701 +AuStubVoid(au_br_fhsm_fin, struct au_br_fhsm *brfhsm)
4702 +#endif
4703 +
4704 +#endif /* __KERNEL__ */
4705 +#endif /* __AUFS_BRANCH_H__ */
4706 diff -urN /usr/share/empty/fs/aufs/conf.mk linux/fs/aufs/conf.mk
4707 --- /usr/share/empty/fs/aufs/conf.mk    1970-01-01 01:00:00.000000000 +0100
4708 +++ linux/fs/aufs/conf.mk       2022-03-21 14:49:05.719966344 +0100
4709 @@ -0,0 +1,40 @@
4710 +# SPDX-License-Identifier: GPL-2.0
4711 +
4712 +AuConfStr = CONFIG_AUFS_FS=${CONFIG_AUFS_FS}
4713 +
4714 +define AuConf
4715 +ifdef ${1}
4716 +AuConfStr += ${1}=${${1}}
4717 +endif
4718 +endef
4719 +
4720 +AuConfAll = BRANCH_MAX_127 BRANCH_MAX_511 BRANCH_MAX_1023 BRANCH_MAX_32767 \
4721 +       SBILIST \
4722 +       HNOTIFY HFSNOTIFY \
4723 +       EXPORT INO_T_64 \
4724 +       XATTR \
4725 +       FHSM \
4726 +       RDU \
4727 +       DIRREN \
4728 +       SHWH \
4729 +       BR_RAMFS \
4730 +       BR_FUSE POLL \
4731 +       BR_HFSPLUS \
4732 +       BDEV_LOOP \
4733 +       DEBUG MAGIC_SYSRQ
4734 +$(foreach i, ${AuConfAll}, \
4735 +       $(eval $(call AuConf,CONFIG_AUFS_${i})))
4736 +
4737 +AuConfName = ${obj}/conf.str
4738 +${AuConfName}.tmp: FORCE
4739 +       @echo ${AuConfStr} | tr ' ' '\n' | sed -e 's/^/"/' -e 's/$$/\\n"/' > $@
4740 +${AuConfName}: ${AuConfName}.tmp
4741 +       @diff -q $< $@ > /dev/null 2>&1 || { \
4742 +       echo '  GEN    ' $@; \
4743 +       cp -p $< $@; \
4744 +       }
4745 +FORCE:
4746 +clean-files += ${AuConfName} ${AuConfName}.tmp
4747 +${obj}/sysfs.o: ${AuConfName}
4748 +
4749 +-include ${srctree}/${src}/conf_priv.mk
4750 diff -urN /usr/share/empty/fs/aufs/cpup.c linux/fs/aufs/cpup.c
4751 --- /usr/share/empty/fs/aufs/cpup.c     1970-01-01 01:00:00.000000000 +0100
4752 +++ linux/fs/aufs/cpup.c        2022-03-21 14:49:05.719966344 +0100
4753 @@ -0,0 +1,1459 @@
4754 +// SPDX-License-Identifier: GPL-2.0
4755 +/*
4756 + * Copyright (C) 2005-2021 Junjiro R. Okajima
4757 + *
4758 + * This program, aufs is free software; you can redistribute it and/or modify
4759 + * it under the terms of the GNU General Public License as published by
4760 + * the Free Software Foundation; either version 2 of the License, or
4761 + * (at your option) any later version.
4762 + *
4763 + * This program is distributed in the hope that it will be useful,
4764 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
4765 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
4766 + * GNU General Public License for more details.
4767 + *
4768 + * You should have received a copy of the GNU General Public License
4769 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
4770 + */
4771 +
4772 +/*
4773 + * copy-up functions, see wbr_policy.c for copy-down
4774 + */
4775 +
4776 +#include <linux/fs_stack.h>
4777 +#include <linux/mm.h>
4778 +#include <linux/task_work.h>
4779 +#include "aufs.h"
4780 +
4781 +void au_cpup_attr_flags(struct inode *dst, unsigned int iflags)
4782 +{
4783 +       const unsigned int mask = S_DEAD | S_SWAPFILE | S_PRIVATE
4784 +               | S_NOATIME | S_NOCMTIME | S_AUTOMOUNT;
4785 +
4786 +       BUILD_BUG_ON(sizeof(iflags) != sizeof(dst->i_flags));
4787 +
4788 +       dst->i_flags |= iflags & ~mask;
4789 +       if (au_test_fs_notime(dst->i_sb))
4790 +               dst->i_flags |= S_NOATIME | S_NOCMTIME;
4791 +}
4792 +
4793 +void au_cpup_attr_timesizes(struct inode *inode)
4794 +{
4795 +       struct inode *h_inode;
4796 +
4797 +       h_inode = au_h_iptr(inode, au_ibtop(inode));
4798 +       fsstack_copy_attr_times(inode, h_inode);
4799 +       fsstack_copy_inode_size(inode, h_inode);
4800 +}
4801 +
4802 +void au_cpup_attr_nlink(struct inode *inode, int force)
4803 +{
4804 +       struct inode *h_inode;
4805 +       struct super_block *sb;
4806 +       aufs_bindex_t bindex, bbot;
4807 +
4808 +       sb = inode->i_sb;
4809 +       bindex = au_ibtop(inode);
4810 +       h_inode = au_h_iptr(inode, bindex);
4811 +       if (!force
4812 +           && !S_ISDIR(h_inode->i_mode)
4813 +           && au_opt_test(au_mntflags(sb), PLINK)
4814 +           && au_plink_test(inode))
4815 +               return;
4816 +
4817 +       /*
4818 +        * 0 can happen in revalidating.
4819 +        * h_inode->i_mutex may not be held here, but it is harmless since once
4820 +        * i_nlink reaches 0, it will never become positive except O_TMPFILE
4821 +        * case.
4822 +        * todo: O_TMPFILE+linkat(AT_SYMLINK_FOLLOW) bypassing aufs may cause
4823 +        *       the incorrect link count.
4824 +        */
4825 +       set_nlink(inode, h_inode->i_nlink);
4826 +
4827 +       /*
4828 +        * fewer nlink makes find(1) noisy, but larger nlink doesn't.
4829 +        * it may includes whplink directory.
4830 +        */
4831 +       if (S_ISDIR(h_inode->i_mode)) {
4832 +               bbot = au_ibbot(inode);
4833 +               for (bindex++; bindex <= bbot; bindex++) {
4834 +                       h_inode = au_h_iptr(inode, bindex);
4835 +                       if (h_inode)
4836 +                               au_add_nlink(inode, h_inode);
4837 +               }
4838 +       }
4839 +}
4840 +
4841 +void au_cpup_attr_changeable(struct inode *inode)
4842 +{
4843 +       struct inode *h_inode;
4844 +
4845 +       h_inode = au_h_iptr(inode, au_ibtop(inode));
4846 +       inode->i_mode = h_inode->i_mode;
4847 +       inode->i_uid = h_inode->i_uid;
4848 +       inode->i_gid = h_inode->i_gid;
4849 +       au_cpup_attr_timesizes(inode);
4850 +       au_cpup_attr_flags(inode, h_inode->i_flags);
4851 +}
4852 +
4853 +void au_cpup_igen(struct inode *inode, struct inode *h_inode)
4854 +{
4855 +       struct au_iinfo *iinfo = au_ii(inode);
4856 +
4857 +       IiMustWriteLock(inode);
4858 +
4859 +       iinfo->ii_higen = h_inode->i_generation;
4860 +       iinfo->ii_hsb1 = h_inode->i_sb;
4861 +}
4862 +
4863 +void au_cpup_attr_all(struct inode *inode, int force)
4864 +{
4865 +       struct inode *h_inode;
4866 +
4867 +       h_inode = au_h_iptr(inode, au_ibtop(inode));
4868 +       au_cpup_attr_changeable(inode);
4869 +       if (inode->i_nlink > 0)
4870 +               au_cpup_attr_nlink(inode, force);
4871 +       inode->i_rdev = h_inode->i_rdev;
4872 +       inode->i_blkbits = h_inode->i_blkbits;
4873 +       au_cpup_igen(inode, h_inode);
4874 +}
4875 +
4876 +/* ---------------------------------------------------------------------- */
4877 +
4878 +/* Note: dt_dentry and dt_h_dentry are not dget/dput-ed */
4879 +
4880 +/* keep the timestamps of the parent dir when cpup */
4881 +void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
4882 +                   struct path *h_path)
4883 +{
4884 +       struct inode *h_inode;
4885 +
4886 +       dt->dt_dentry = dentry;
4887 +       dt->dt_h_path = *h_path;
4888 +       h_inode = d_inode(h_path->dentry);
4889 +       dt->dt_atime = h_inode->i_atime;
4890 +       dt->dt_mtime = h_inode->i_mtime;
4891 +       /* smp_mb(); */
4892 +}
4893 +
4894 +void au_dtime_revert(struct au_dtime *dt)
4895 +{
4896 +       struct iattr attr;
4897 +       int err;
4898 +
4899 +       attr.ia_atime = dt->dt_atime;
4900 +       attr.ia_mtime = dt->dt_mtime;
4901 +       attr.ia_valid = ATTR_FORCE | ATTR_MTIME | ATTR_MTIME_SET
4902 +               | ATTR_ATIME | ATTR_ATIME_SET;
4903 +
4904 +       /* no delegation since this is a directory */
4905 +       err = vfsub_notify_change(&dt->dt_h_path, &attr, /*delegated*/NULL);
4906 +       if (unlikely(err))
4907 +               pr_warn("restoring timestamps failed(%d). ignored\n", err);
4908 +}
4909 +
4910 +/* ---------------------------------------------------------------------- */
4911 +
4912 +/* internal use only */
4913 +struct au_cpup_reg_attr {
4914 +       int             valid;
4915 +       struct kstat    st;
4916 +       unsigned int    iflags; /* inode->i_flags */
4917 +};
4918 +
4919 +static noinline_for_stack
4920 +int cpup_iattr(struct dentry *dst, aufs_bindex_t bindex, struct path *h_src,
4921 +              struct au_cpup_reg_attr *h_src_attr)
4922 +{
4923 +       int err, sbits, icex;
4924 +       unsigned int mnt_flags;
4925 +       unsigned char verbose;
4926 +       struct iattr ia;
4927 +       struct path h_path;
4928 +       struct inode *h_isrc, *h_idst;
4929 +       struct kstat *h_st;
4930 +       struct au_branch *br;
4931 +
4932 +       br = au_sbr(dst->d_sb, bindex);
4933 +       h_path.mnt = au_br_mnt(br);
4934 +       h_path.dentry = au_h_dptr(dst, bindex);
4935 +       h_idst = d_inode(h_path.dentry);
4936 +       h_isrc = d_inode(h_src->dentry);
4937 +       ia.ia_valid = ATTR_FORCE | ATTR_UID | ATTR_GID
4938 +               | ATTR_ATIME | ATTR_MTIME
4939 +               | ATTR_ATIME_SET | ATTR_MTIME_SET;
4940 +       if (h_src_attr && h_src_attr->valid) {
4941 +               h_st = &h_src_attr->st;
4942 +               ia.ia_uid = h_st->uid;
4943 +               ia.ia_gid = h_st->gid;
4944 +               ia.ia_atime = h_st->atime;
4945 +               ia.ia_mtime = h_st->mtime;
4946 +               if (h_idst->i_mode != h_st->mode
4947 +                   && !S_ISLNK(h_idst->i_mode)) {
4948 +                       ia.ia_valid |= ATTR_MODE;
4949 +                       ia.ia_mode = h_st->mode;
4950 +               }
4951 +               sbits = !!(h_st->mode & (S_ISUID | S_ISGID));
4952 +               au_cpup_attr_flags(h_idst, h_src_attr->iflags);
4953 +       } else {
4954 +               ia.ia_uid = h_isrc->i_uid;
4955 +               ia.ia_gid = h_isrc->i_gid;
4956 +               ia.ia_atime = h_isrc->i_atime;
4957 +               ia.ia_mtime = h_isrc->i_mtime;
4958 +               if (h_idst->i_mode != h_isrc->i_mode
4959 +                   && !S_ISLNK(h_idst->i_mode)) {
4960 +                       ia.ia_valid |= ATTR_MODE;
4961 +                       ia.ia_mode = h_isrc->i_mode;
4962 +               }
4963 +               sbits = !!(h_isrc->i_mode & (S_ISUID | S_ISGID));
4964 +               au_cpup_attr_flags(h_idst, h_isrc->i_flags);
4965 +       }
4966 +       /* no delegation since it is just created */
4967 +       err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
4968 +
4969 +       /* is this nfs only? */
4970 +       if (!err && sbits && au_test_nfs(h_path.dentry->d_sb)) {
4971 +               ia.ia_valid = ATTR_FORCE | ATTR_MODE;
4972 +               ia.ia_mode = h_isrc->i_mode;
4973 +               err = vfsub_notify_change(&h_path, &ia, /*delegated*/NULL);
4974 +       }
4975 +
4976 +       icex = br->br_perm & AuBrAttr_ICEX;
4977 +       if (!err) {
4978 +               mnt_flags = au_mntflags(dst->d_sb);
4979 +               verbose = !!au_opt_test(mnt_flags, VERBOSE);
4980 +               err = au_cpup_xattr(&h_path, h_src, icex, verbose);
4981 +       }
4982 +
4983 +       return err;
4984 +}
4985 +
4986 +/* ---------------------------------------------------------------------- */
4987 +
4988 +static int au_do_copy_file(struct file *dst, struct file *src, loff_t len,
4989 +                          char *buf, unsigned long blksize)
4990 +{
4991 +       int err;
4992 +       size_t sz, rbytes, wbytes;
4993 +       unsigned char all_zero;
4994 +       char *p, *zp;
4995 +       struct inode *h_inode;
4996 +       /* reduce stack usage */
4997 +       struct iattr *ia;
4998 +
4999 +       zp = page_address(ZERO_PAGE(0));
5000 +       if (unlikely(!zp))
5001 +               return -ENOMEM; /* possible? */
5002 +
5003 +       err = 0;
5004 +       all_zero = 0;
5005 +       while (len) {
5006 +               AuDbg("len %lld\n", len);
5007 +               sz = blksize;
5008 +               if (len < blksize)
5009 +                       sz = len;
5010 +
5011 +               rbytes = 0;
5012 +               /* todo: signal_pending? */
5013 +               while (!rbytes || err == -EAGAIN || err == -EINTR) {
5014 +                       rbytes = vfsub_read_k(src, buf, sz, &src->f_pos);
5015 +                       err = rbytes;
5016 +               }
5017 +               if (unlikely(err < 0))
5018 +                       break;
5019 +
5020 +               all_zero = 0;
5021 +               if (len >= rbytes && rbytes == blksize)
5022 +                       all_zero = !memcmp(buf, zp, rbytes);
5023 +               if (!all_zero) {
5024 +                       wbytes = rbytes;
5025 +                       p = buf;
5026 +                       while (wbytes) {
5027 +                               size_t b;
5028 +
5029 +                               b = vfsub_write_k(dst, p, wbytes, &dst->f_pos);
5030 +                               err = b;
5031 +                               /* todo: signal_pending? */
5032 +                               if (unlikely(err == -EAGAIN || err == -EINTR))
5033 +                                       continue;
5034 +                               if (unlikely(err < 0))
5035 +                                       break;
5036 +                               wbytes -= b;
5037 +                               p += b;
5038 +                       }
5039 +                       if (unlikely(err < 0))
5040 +                               break;
5041 +               } else {
5042 +                       loff_t res;
5043 +
5044 +                       AuLabel(hole);
5045 +                       res = vfsub_llseek(dst, rbytes, SEEK_CUR);
5046 +                       err = res;
5047 +                       if (unlikely(res < 0))
5048 +                               break;
5049 +               }
5050 +               len -= rbytes;
5051 +               err = 0;
5052 +       }
5053 +
5054 +       /* the last block may be a hole */
5055 +       if (!err && all_zero) {
5056 +               AuLabel(last hole);
5057 +
5058 +               err = 1;
5059 +               if (au_test_nfs(dst->f_path.dentry->d_sb)) {
5060 +                       /* nfs requires this step to make last hole */
5061 +                       /* is this only nfs? */
5062 +                       do {
5063 +                               /* todo: signal_pending? */
5064 +                               err = vfsub_write_k(dst, "\0", 1, &dst->f_pos);
5065 +                       } while (err == -EAGAIN || err == -EINTR);
5066 +                       if (err == 1)
5067 +                               dst->f_pos--;
5068 +               }
5069 +
5070 +               if (err == 1) {
5071 +                       ia = (void *)buf;
5072 +                       ia->ia_size = dst->f_pos;
5073 +                       ia->ia_valid = ATTR_SIZE | ATTR_FILE;
5074 +                       ia->ia_file = dst;
5075 +                       h_inode = file_inode(dst);
5076 +                       inode_lock_nested(h_inode, AuLsc_I_CHILD2);
5077 +                       /* no delegation since it is just created */
5078 +                       err = vfsub_notify_change(&dst->f_path, ia,
5079 +                                                 /*delegated*/NULL);
5080 +                       inode_unlock(h_inode);
5081 +               }
5082 +       }
5083 +
5084 +       return err;
5085 +}
5086 +
5087 +int au_copy_file(struct file *dst, struct file *src, loff_t len)
5088 +{
5089 +       int err;
5090 +       unsigned long blksize;
5091 +       unsigned char do_kfree;
5092 +       char *buf;
5093 +       struct super_block *h_sb;
5094 +
5095 +       err = -ENOMEM;
5096 +       h_sb = file_inode(dst)->i_sb;
5097 +       blksize = h_sb->s_blocksize;
5098 +       if (!blksize || PAGE_SIZE < blksize)
5099 +               blksize = PAGE_SIZE;
5100 +       AuDbg("blksize %lu\n", blksize);
5101 +       do_kfree = (blksize != PAGE_SIZE && blksize >= sizeof(struct iattr *));
5102 +       if (do_kfree)
5103 +               buf = kmalloc(blksize, GFP_NOFS);
5104 +       else
5105 +               buf = (void *)__get_free_page(GFP_NOFS);
5106 +       if (unlikely(!buf))
5107 +               goto out;
5108 +
5109 +       if (len > (1 << 22))
5110 +               AuDbg("copying a large file %lld\n", (long long)len);
5111 +
5112 +       src->f_pos = 0;
5113 +       dst->f_pos = 0;
5114 +       err = au_do_copy_file(dst, src, len, buf, blksize);
5115 +       if (do_kfree) {
5116 +               AuDebugOn(!au_kfree_do_sz_test(blksize));
5117 +               au_kfree_do_rcu(buf);
5118 +       } else
5119 +               free_page((unsigned long)buf);
5120 +
5121 +out:
5122 +       return err;
5123 +}
5124 +
5125 +static int au_do_copy(struct file *dst, struct file *src, loff_t len)
5126 +{
5127 +       int err;
5128 +       struct super_block *h_src_sb;
5129 +       struct inode *h_src_inode;
5130 +
5131 +       h_src_inode = file_inode(src);
5132 +       h_src_sb = h_src_inode->i_sb;
5133 +
5134 +       /* XFS acquires inode_lock */
5135 +       if (!au_test_xfs(h_src_sb))
5136 +               err = au_copy_file(dst, src, len);
5137 +       else {
5138 +               inode_unlock_shared(h_src_inode);
5139 +               err = au_copy_file(dst, src, len);
5140 +               inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5141 +       }
5142 +
5143 +       return err;
5144 +}
5145 +
5146 +static int au_clone_or_copy(struct file *dst, struct file *src, loff_t len)
5147 +{
5148 +       int err;
5149 +       loff_t lo;
5150 +       struct super_block *h_src_sb;
5151 +       struct inode *h_src_inode;
5152 +
5153 +       h_src_inode = file_inode(src);
5154 +       h_src_sb = h_src_inode->i_sb;
5155 +       if (h_src_sb != file_inode(dst)->i_sb
5156 +           || !dst->f_op->remap_file_range) {
5157 +               err = au_do_copy(dst, src, len);
5158 +               goto out;
5159 +       }
5160 +
5161 +       if (!au_test_nfs(h_src_sb)) {
5162 +               inode_unlock_shared(h_src_inode);
5163 +               lo = vfsub_clone_file_range(src, dst, len);
5164 +               inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5165 +       } else
5166 +               lo = vfsub_clone_file_range(src, dst, len);
5167 +       if (lo == len) {
5168 +               err = 0;
5169 +               goto out; /* success */
5170 +       } else if (lo >= 0)
5171 +               /* todo: possible? */
5172 +               /* paritially succeeded */
5173 +               AuDbg("lo %lld, len %lld. Retrying.\n", lo, len);
5174 +       else if (lo != -EOPNOTSUPP) {
5175 +               /* older XFS has a condition in cloning */
5176 +               err = lo;
5177 +               goto out;
5178 +       }
5179 +
5180 +       /* the backend fs on NFS may not support cloning */
5181 +       err = au_do_copy(dst, src, len);
5182 +
5183 +out:
5184 +       AuTraceErr(err);
5185 +       return err;
5186 +}
5187 +
5188 +/*
5189 + * to support a sparse file which is opened with O_APPEND,
5190 + * we need to close the file.
5191 + */
5192 +static int au_cp_regular(struct au_cp_generic *cpg)
5193 +{
5194 +       int err, i;
5195 +       enum { SRC, DST };
5196 +       struct {
5197 +               aufs_bindex_t bindex;
5198 +               unsigned int flags;
5199 +               struct dentry *dentry;
5200 +               int force_wr;
5201 +               struct file *file;
5202 +       } *f, file[] = {
5203 +               {
5204 +                       .bindex = cpg->bsrc,
5205 +                       .flags = O_RDONLY | O_NOATIME | O_LARGEFILE,
5206 +               },
5207 +               {
5208 +                       .bindex = cpg->bdst,
5209 +                       .flags = O_WRONLY | O_NOATIME | O_LARGEFILE,
5210 +                       .force_wr = !!au_ftest_cpup(cpg->flags, RWDST),
5211 +               }
5212 +       };
5213 +       struct au_branch *br;
5214 +       struct super_block *sb, *h_src_sb;
5215 +       struct inode *h_src_inode;
5216 +       struct task_struct *tsk = current;
5217 +
5218 +       /* bsrc branch can be ro/rw. */
5219 +       sb = cpg->dentry->d_sb;
5220 +       f = file;
5221 +       for (i = 0; i < 2; i++, f++) {
5222 +               f->dentry = au_h_dptr(cpg->dentry, f->bindex);
5223 +               f->file = au_h_open(cpg->dentry, f->bindex, f->flags,
5224 +                                   /*file*/NULL, f->force_wr);
5225 +               if (IS_ERR(f->file)) {
5226 +                       err = PTR_ERR(f->file);
5227 +                       if (i == SRC)
5228 +                               goto out;
5229 +                       else
5230 +                               goto out_src;
5231 +               }
5232 +       }
5233 +
5234 +       /* try stopping to update while we copyup */
5235 +       h_src_inode = d_inode(file[SRC].dentry);
5236 +       h_src_sb = h_src_inode->i_sb;
5237 +       if (!au_test_nfs(h_src_sb))
5238 +               IMustLock(h_src_inode);
5239 +       err = au_clone_or_copy(file[DST].file, file[SRC].file, cpg->len);
5240 +
5241 +       /* i wonder if we had O_NO_DELAY_FPUT flag */
5242 +       if (tsk->flags & PF_KTHREAD)
5243 +               __fput_sync(file[DST].file);
5244 +       else {
5245 +               /* it happened actually */
5246 +               fput(file[DST].file);
5247 +               /*
5248 +                * too bad.
5249 +                * we have to call both since we don't know which place the file
5250 +                * was added to.
5251 +                */
5252 +               task_work_run();
5253 +               flush_delayed_fput();
5254 +       }
5255 +       br = au_sbr(sb, file[DST].bindex);
5256 +       au_lcnt_dec(&br->br_nfiles);
5257 +
5258 +out_src:
5259 +       fput(file[SRC].file);
5260 +       br = au_sbr(sb, file[SRC].bindex);
5261 +       au_lcnt_dec(&br->br_nfiles);
5262 +out:
5263 +       return err;
5264 +}
5265 +
5266 +static int au_do_cpup_regular(struct au_cp_generic *cpg,
5267 +                             struct au_cpup_reg_attr *h_src_attr)
5268 +{
5269 +       int err, rerr;
5270 +       loff_t l;
5271 +       struct path h_path;
5272 +       struct inode *h_src_inode, *h_dst_inode;
5273 +
5274 +       err = 0;
5275 +       h_src_inode = au_h_iptr(d_inode(cpg->dentry), cpg->bsrc);
5276 +       l = i_size_read(h_src_inode);
5277 +       if (cpg->len == -1 || l < cpg->len)
5278 +               cpg->len = l;
5279 +       if (cpg->len) {
5280 +               /* try stopping to update while we are referencing */
5281 +               inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5282 +               au_pin_hdir_unlock(cpg->pin);
5283 +
5284 +               h_path.dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
5285 +               h_path.mnt = au_sbr_mnt(cpg->dentry->d_sb, cpg->bsrc);
5286 +               h_src_attr->iflags = h_src_inode->i_flags;
5287 +               if (!au_test_nfs(h_src_inode->i_sb))
5288 +                       err = vfsub_getattr(&h_path, &h_src_attr->st);
5289 +               else {
5290 +                       inode_unlock_shared(h_src_inode);
5291 +                       err = vfsub_getattr(&h_path, &h_src_attr->st);
5292 +                       inode_lock_shared_nested(h_src_inode, AuLsc_I_CHILD);
5293 +               }
5294 +               if (unlikely(err)) {
5295 +                       inode_unlock_shared(h_src_inode);
5296 +                       goto out;
5297 +               }
5298 +               h_src_attr->valid = 1;
5299 +               if (!au_test_nfs(h_src_inode->i_sb)) {
5300 +                       err = au_cp_regular(cpg);
5301 +                       inode_unlock_shared(h_src_inode);
5302 +               } else {
5303 +                       inode_unlock_shared(h_src_inode);
5304 +                       err = au_cp_regular(cpg);
5305 +               }
5306 +               rerr = au_pin_hdir_relock(cpg->pin);
5307 +               if (!err && rerr)
5308 +                       err = rerr;
5309 +       }
5310 +       if (!err && (h_src_inode->i_state & I_LINKABLE)) {
5311 +               h_path.dentry = au_h_dptr(cpg->dentry, cpg->bdst);
5312 +               h_dst_inode = d_inode(h_path.dentry);
5313 +               spin_lock(&h_dst_inode->i_lock);
5314 +               h_dst_inode->i_state |= I_LINKABLE;
5315 +               spin_unlock(&h_dst_inode->i_lock);
5316 +       }
5317 +
5318 +out:
5319 +       return err;
5320 +}
5321 +
5322 +static int au_do_cpup_symlink(struct path *h_path, struct dentry *h_src,
5323 +                             struct inode *h_dir)
5324 +{
5325 +       int err;
5326 +       DEFINE_DELAYED_CALL(done);
5327 +       const char *sym;
5328 +
5329 +       sym = vfs_get_link(h_src, &done);
5330 +       err = PTR_ERR(sym);
5331 +       if (IS_ERR(sym))
5332 +               goto out;
5333 +
5334 +       err = vfsub_symlink(h_dir, h_path, sym);
5335 +
5336 +out:
5337 +       do_delayed_call(&done);
5338 +       return err;
5339 +}
5340 +
5341 +/*
5342 + * regardless 'acl' option, reset all ACL.
5343 + * All ACL will be copied up later from the original entry on the lower branch.
5344 + */
5345 +static int au_reset_acl(struct inode *h_dir, struct path *h_path, umode_t mode)
5346 +{
5347 +       int err;
5348 +       struct dentry *h_dentry;
5349 +       struct inode *h_inode;
5350 +       struct user_namespace *h_userns;
5351 +
5352 +       h_userns = mnt_user_ns(h_path->mnt);
5353 +       h_dentry = h_path->dentry;
5354 +       h_inode = d_inode(h_dentry);
5355 +       /* forget_all_cached_acls(h_inode)); */
5356 +       err = vfsub_removexattr(h_userns, h_dentry, XATTR_NAME_POSIX_ACL_ACCESS);
5357 +       AuTraceErr(err);
5358 +       if (err == -EOPNOTSUPP)
5359 +               err = 0;
5360 +       if (!err)
5361 +               err = vfsub_acl_chmod(h_userns, h_inode, mode);
5362 +
5363 +       AuTraceErr(err);
5364 +       return err;
5365 +}
5366 +
5367 +static int au_do_cpup_dir(struct au_cp_generic *cpg, struct dentry *dst_parent,
5368 +                         struct inode *h_dir, struct path *h_path)
5369 +{
5370 +       int err;
5371 +       struct inode *dir, *inode;
5372 +       struct user_namespace *h_userns;
5373 +
5374 +       h_userns = mnt_user_ns(h_path->mnt);
5375 +       err = vfsub_removexattr(h_userns, h_path->dentry,
5376 +                               XATTR_NAME_POSIX_ACL_DEFAULT);
5377 +       AuTraceErr(err);
5378 +       if (err == -EOPNOTSUPP)
5379 +               err = 0;
5380 +       if (unlikely(err))
5381 +               goto out;
5382 +
5383 +       /*
5384 +        * strange behaviour from the users view,
5385 +        * particularly setattr case
5386 +        */
5387 +       dir = d_inode(dst_parent);
5388 +       if (au_ibtop(dir) == cpg->bdst)
5389 +               au_cpup_attr_nlink(dir, /*force*/1);
5390 +       inode = d_inode(cpg->dentry);
5391 +       au_cpup_attr_nlink(inode, /*force*/1);
5392 +
5393 +out:
5394 +       return err;
5395 +}
5396 +
5397 +static noinline_for_stack
5398 +int cpup_entry(struct au_cp_generic *cpg, struct dentry *dst_parent,
5399 +              struct au_cpup_reg_attr *h_src_attr)
5400 +{
5401 +       int err;
5402 +       umode_t mode;
5403 +       unsigned int mnt_flags;
5404 +       unsigned char isdir, isreg, force;
5405 +       const unsigned char do_dt = !!au_ftest_cpup(cpg->flags, DTIME);
5406 +       struct au_dtime dt;
5407 +       struct path h_path;
5408 +       struct dentry *h_src, *h_dst, *h_parent;
5409 +       struct inode *h_inode, *h_dir;
5410 +       struct super_block *sb;
5411 +
5412 +       /* bsrc branch can be ro/rw. */
5413 +       h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
5414 +       h_inode = d_inode(h_src);
5415 +       AuDebugOn(h_inode != au_h_iptr(d_inode(cpg->dentry), cpg->bsrc));
5416 +
5417 +       /* try stopping to be referenced while we are creating */
5418 +       h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
5419 +       if (au_ftest_cpup(cpg->flags, RENAME))
5420 +               AuDebugOn(strncmp(h_dst->d_name.name, AUFS_WH_PFX,
5421 +                                 AUFS_WH_PFX_LEN));
5422 +       h_parent = h_dst->d_parent; /* dir inode is locked */
5423 +       h_dir = d_inode(h_parent);
5424 +       IMustLock(h_dir);
5425 +       AuDebugOn(h_parent != h_dst->d_parent);
5426 +
5427 +       sb = cpg->dentry->d_sb;
5428 +       h_path.mnt = au_sbr_mnt(sb, cpg->bdst);
5429 +       if (do_dt) {
5430 +               h_path.dentry = h_parent;
5431 +               au_dtime_store(&dt, dst_parent, &h_path);
5432 +       }
5433 +       h_path.dentry = h_dst;
5434 +
5435 +       isreg = 0;
5436 +       isdir = 0;
5437 +       mode = h_inode->i_mode;
5438 +       switch (mode & S_IFMT) {
5439 +       case S_IFREG:
5440 +               isreg = 1;
5441 +               err = vfsub_create(h_dir, &h_path, 0600, /*want_excl*/true);
5442 +               if (!err)
5443 +                       err = au_do_cpup_regular(cpg, h_src_attr);
5444 +               break;
5445 +       case S_IFDIR:
5446 +               isdir = 1;
5447 +               err = vfsub_mkdir(h_dir, &h_path, mode);
5448 +               if (!err)
5449 +                       err = au_do_cpup_dir(cpg, dst_parent, h_dir, &h_path);
5450 +               break;
5451 +       case S_IFLNK:
5452 +               err = au_do_cpup_symlink(&h_path, h_src, h_dir);
5453 +               break;
5454 +       case S_IFCHR:
5455 +       case S_IFBLK:
5456 +               AuDebugOn(!capable(CAP_MKNOD));
5457 +               fallthrough;
5458 +       case S_IFIFO:
5459 +       case S_IFSOCK:
5460 +               err = vfsub_mknod(h_dir, &h_path, mode, h_inode->i_rdev);
5461 +               break;
5462 +       default:
5463 +               AuIOErr("Unknown inode type 0%o\n", mode);
5464 +               err = -EIO;
5465 +       }
5466 +       if (!err)
5467 +               err = au_reset_acl(h_dir, &h_path, mode);
5468 +
5469 +       mnt_flags = au_mntflags(sb);
5470 +       if (!au_opt_test(mnt_flags, UDBA_NONE)
5471 +           && !isdir
5472 +           && au_opt_test(mnt_flags, XINO)
5473 +           && (h_inode->i_nlink == 1
5474 +               || (h_inode->i_state & I_LINKABLE))
5475 +           /* todo: unnecessary? */
5476 +           /* && d_inode(cpg->dentry)->i_nlink == 1 */
5477 +           && cpg->bdst < cpg->bsrc
5478 +           && !au_ftest_cpup(cpg->flags, KEEPLINO))
5479 +               au_xino_write(sb, cpg->bsrc, h_inode->i_ino, /*ino*/0);
5480 +               /* ignore this error */
5481 +
5482 +       if (!err) {
5483 +               force = 0;
5484 +               if (isreg) {
5485 +                       force = !!cpg->len;
5486 +                       if (cpg->len == -1)
5487 +                               force = !!i_size_read(h_inode);
5488 +               }
5489 +               au_fhsm_wrote(sb, cpg->bdst, force);
5490 +       }
5491 +
5492 +       if (do_dt)
5493 +               au_dtime_revert(&dt);
5494 +       return err;
5495 +}
5496 +
5497 +static int au_do_ren_after_cpup(struct au_cp_generic *cpg, struct path *h_path)
5498 +{
5499 +       int err;
5500 +       struct dentry *dentry, *h_dentry, *h_parent, *parent;
5501 +       struct path h_ppath;
5502 +       struct inode *h_dir;
5503 +       aufs_bindex_t bdst;
5504 +
5505 +       dentry = cpg->dentry;
5506 +       bdst = cpg->bdst;
5507 +       h_ppath.mnt = au_sbr_mnt(dentry->d_sb, bdst);
5508 +       h_dentry = au_h_dptr(dentry, bdst);
5509 +       if (!au_ftest_cpup(cpg->flags, OVERWRITE)) {
5510 +               dget(h_dentry);
5511 +               au_set_h_dptr(dentry, bdst, NULL);
5512 +               err = au_lkup_neg(dentry, bdst, /*wh*/0);
5513 +               if (!err)
5514 +                       h_path->dentry = dget(au_h_dptr(dentry, bdst));
5515 +               au_set_h_dptr(dentry, bdst, h_dentry);
5516 +       } else {
5517 +               err = 0;
5518 +               parent = dget_parent(dentry);
5519 +               h_ppath.dentry = au_h_dptr(parent, bdst);
5520 +               dput(parent);
5521 +               h_path->dentry = vfsub_lkup_one(&dentry->d_name, &h_ppath);
5522 +               if (IS_ERR(h_path->dentry))
5523 +                       err = PTR_ERR(h_path->dentry);
5524 +       }
5525 +       if (unlikely(err))
5526 +               goto out;
5527 +
5528 +       h_parent = h_dentry->d_parent; /* dir inode is locked */
5529 +       h_dir = d_inode(h_parent);
5530 +       IMustLock(h_dir);
5531 +       AuDbg("%pd %pd\n", h_dentry, h_path->dentry);
5532 +       /* no delegation since it is just created */
5533 +       err = vfsub_rename(h_dir, h_dentry, h_dir, h_path, /*delegated*/NULL,
5534 +                          /*flags*/0);
5535 +       dput(h_path->dentry);
5536 +
5537 +out:
5538 +       return err;
5539 +}
5540 +
5541 +/*
5542 + * copyup the @dentry from @bsrc to @bdst.
5543 + * the caller must set the both of lower dentries.
5544 + * @len is for truncating when it is -1 copyup the entire file.
5545 + * in link/rename cases, @dst_parent may be different from the real one.
5546 + * basic->bsrc can be larger than basic->bdst.
5547 + * aufs doesn't touch the credential so
5548 + * security_inode_copy_up{,_xattr}() are unnecessary.
5549 + */
5550 +static int au_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
5551 +{
5552 +       int err, rerr;
5553 +       aufs_bindex_t old_ibtop;
5554 +       unsigned char isdir, plink;
5555 +       struct dentry *h_src, *h_dst, *h_parent;
5556 +       struct inode *dst_inode, *h_dir, *inode, *delegated, *src_inode;
5557 +       struct super_block *sb;
5558 +       struct au_branch *br;
5559 +       struct path h_src_path;
5560 +       /* to reduce stack size */
5561 +       struct {
5562 +               struct au_dtime dt;
5563 +               struct path h_path;
5564 +               struct au_cpup_reg_attr h_src_attr;
5565 +       } *a;
5566 +
5567 +       err = -ENOMEM;
5568 +       a = kmalloc(sizeof(*a), GFP_NOFS);
5569 +       if (unlikely(!a))
5570 +               goto out;
5571 +       a->h_src_attr.valid = 0;
5572 +
5573 +       sb = cpg->dentry->d_sb;
5574 +       br = au_sbr(sb, cpg->bdst);
5575 +       a->h_path.mnt = au_br_mnt(br);
5576 +       h_dst = au_h_dptr(cpg->dentry, cpg->bdst);
5577 +       h_parent = h_dst->d_parent; /* dir inode is locked */
5578 +       h_dir = d_inode(h_parent);
5579 +       IMustLock(h_dir);
5580 +
5581 +       h_src = au_h_dptr(cpg->dentry, cpg->bsrc);
5582 +       inode = d_inode(cpg->dentry);
5583 +
5584 +       if (!dst_parent)
5585 +               dst_parent = dget_parent(cpg->dentry);
5586 +       else
5587 +               dget(dst_parent);
5588 +
5589 +       plink = !!au_opt_test(au_mntflags(sb), PLINK);
5590 +       dst_inode = au_h_iptr(inode, cpg->bdst);
5591 +       if (dst_inode) {
5592 +               if (unlikely(!plink)) {
5593 +                       err = -EIO;
5594 +                       AuIOErr("hi%lu(i%lu) exists on b%d "
5595 +                               "but plink is disabled\n",
5596 +                               dst_inode->i_ino, inode->i_ino, cpg->bdst);
5597 +                       goto out_parent;
5598 +               }
5599 +
5600 +               if (dst_inode->i_nlink) {
5601 +                       const int do_dt = au_ftest_cpup(cpg->flags, DTIME);
5602 +
5603 +                       h_src = au_plink_lkup(inode, cpg->bdst);
5604 +                       err = PTR_ERR(h_src);
5605 +                       if (IS_ERR(h_src))
5606 +                               goto out_parent;
5607 +                       if (unlikely(d_is_negative(h_src))) {
5608 +                               err = -EIO;
5609 +                               AuIOErr("i%lu exists on b%d "
5610 +                                       "but not pseudo-linked\n",
5611 +                                       inode->i_ino, cpg->bdst);
5612 +                               dput(h_src);
5613 +                               goto out_parent;
5614 +                       }
5615 +
5616 +                       if (do_dt) {
5617 +                               a->h_path.dentry = h_parent;
5618 +                               au_dtime_store(&a->dt, dst_parent, &a->h_path);
5619 +                       }
5620 +
5621 +                       a->h_path.dentry = h_dst;
5622 +                       delegated = NULL;
5623 +                       err = vfsub_link(h_src, h_dir, &a->h_path, &delegated);
5624 +                       if (!err && au_ftest_cpup(cpg->flags, RENAME))
5625 +                               err = au_do_ren_after_cpup(cpg, &a->h_path);
5626 +                       if (do_dt)
5627 +                               au_dtime_revert(&a->dt);
5628 +                       if (unlikely(err == -EWOULDBLOCK)) {
5629 +                               pr_warn("cannot retry for NFSv4 delegation"
5630 +                                       " for an internal link\n");
5631 +                               iput(delegated);
5632 +                       }
5633 +                       dput(h_src);
5634 +                       goto out_parent;
5635 +               } else
5636 +                       /* todo: cpup_wh_file? */
5637 +                       /* udba work */
5638 +                       au_update_ibrange(inode, /*do_put_zero*/1);
5639 +       }
5640 +
5641 +       isdir = S_ISDIR(inode->i_mode);
5642 +       old_ibtop = au_ibtop(inode);
5643 +       err = cpup_entry(cpg, dst_parent, &a->h_src_attr);
5644 +       if (unlikely(err))
5645 +               goto out_rev;
5646 +       dst_inode = d_inode(h_dst);
5647 +       inode_lock_nested(dst_inode, AuLsc_I_CHILD2);
5648 +       /* todo: necessary? */
5649 +       /* au_pin_hdir_unlock(cpg->pin); */
5650 +
5651 +       h_src_path.dentry = h_src;
5652 +       h_src_path.mnt = au_sbr_mnt(sb, cpg->bsrc);
5653 +       err = cpup_iattr(cpg->dentry, cpg->bdst, &h_src_path, &a->h_src_attr);
5654 +       if (unlikely(err)) {
5655 +               /* todo: necessary? */
5656 +               /* au_pin_hdir_relock(cpg->pin); */ /* ignore an error */
5657 +               inode_unlock(dst_inode);
5658 +               goto out_rev;
5659 +       }
5660 +
5661 +       if (cpg->bdst < old_ibtop) {
5662 +               if (S_ISREG(inode->i_mode)) {
5663 +                       err = au_dy_iaop(inode, cpg->bdst, dst_inode);
5664 +                       if (unlikely(err)) {
5665 +                               /* ignore an error */
5666 +                               /* au_pin_hdir_relock(cpg->pin); */
5667 +                               inode_unlock(dst_inode);
5668 +                               goto out_rev;
5669 +                       }
5670 +               }
5671 +               au_set_ibtop(inode, cpg->bdst);
5672 +       } else
5673 +               au_set_ibbot(inode, cpg->bdst);
5674 +       au_set_h_iptr(inode, cpg->bdst, au_igrab(dst_inode),
5675 +                     au_hi_flags(inode, isdir));
5676 +
5677 +       /* todo: necessary? */
5678 +       /* err = au_pin_hdir_relock(cpg->pin); */
5679 +       inode_unlock(dst_inode);
5680 +       if (unlikely(err))
5681 +               goto out_rev;
5682 +
5683 +       src_inode = d_inode(h_src);
5684 +       if (!isdir
5685 +           && (src_inode->i_nlink > 1
5686 +               || src_inode->i_state & I_LINKABLE)
5687 +           && plink)
5688 +               au_plink_append(inode, cpg->bdst, h_dst);
5689 +
5690 +       if (au_ftest_cpup(cpg->flags, RENAME)) {
5691 +               a->h_path.dentry = h_dst;
5692 +               err = au_do_ren_after_cpup(cpg, &a->h_path);
5693 +       }
5694 +       if (!err)
5695 +               goto out_parent; /* success */
5696 +
5697 +       /* revert */
5698 +out_rev:
5699 +       a->h_path.dentry = h_parent;
5700 +       au_dtime_store(&a->dt, dst_parent, &a->h_path);
5701 +       a->h_path.dentry = h_dst;
5702 +       rerr = 0;
5703 +       if (d_is_positive(h_dst)) {
5704 +               if (!isdir) {
5705 +                       /* no delegation since it is just created */
5706 +                       rerr = vfsub_unlink(h_dir, &a->h_path,
5707 +                                           /*delegated*/NULL, /*force*/0);
5708 +               } else
5709 +                       rerr = vfsub_rmdir(h_dir, &a->h_path);
5710 +       }
5711 +       au_dtime_revert(&a->dt);
5712 +       if (rerr) {
5713 +               AuIOErr("failed removing broken entry(%d, %d)\n", err, rerr);
5714 +               err = -EIO;
5715 +       }
5716 +out_parent:
5717 +       dput(dst_parent);
5718 +       au_kfree_rcu(a);
5719 +out:
5720 +       return err;
5721 +}
5722 +
5723 +#if 0 /* reserved */
5724 +struct au_cpup_single_args {
5725 +       int *errp;
5726 +       struct au_cp_generic *cpg;
5727 +       struct dentry *dst_parent;
5728 +};
5729 +
5730 +static void au_call_cpup_single(void *args)
5731 +{
5732 +       struct au_cpup_single_args *a = args;
5733 +
5734 +       au_pin_hdir_acquire_nest(a->cpg->pin);
5735 +       *a->errp = au_cpup_single(a->cpg, a->dst_parent);
5736 +       au_pin_hdir_release(a->cpg->pin);
5737 +}
5738 +#endif
5739 +
5740 +/*
5741 + * prevent SIGXFSZ in copy-up.
5742 + * testing CAP_MKNOD is for generic fs,
5743 + * but CAP_FSETID is for xfs only, currently.
5744 + */
5745 +static int au_cpup_sio_test(struct au_pin *pin, umode_t mode)
5746 +{
5747 +       int do_sio;
5748 +       struct super_block *sb;
5749 +       struct inode *h_dir;
5750 +
5751 +       do_sio = 0;
5752 +       sb = au_pinned_parent(pin)->d_sb;
5753 +       if (!au_wkq_test()
5754 +           && (!au_sbi(sb)->si_plink_maint_pid
5755 +               || au_plink_maint(sb, AuLock_NOPLM))) {
5756 +               switch (mode & S_IFMT) {
5757 +               case S_IFREG:
5758 +                       /* no condition about RLIMIT_FSIZE and the file size */
5759 +                       do_sio = 1;
5760 +                       break;
5761 +               case S_IFCHR:
5762 +               case S_IFBLK:
5763 +                       do_sio = !capable(CAP_MKNOD);
5764 +                       break;
5765 +               }
5766 +               if (!do_sio)
5767 +                       do_sio = ((mode & (S_ISUID | S_ISGID))
5768 +                                 && !capable(CAP_FSETID));
5769 +               /* this workaround may be removed in the future */
5770 +               if (!do_sio) {
5771 +                       h_dir = au_pinned_h_dir(pin);
5772 +                       do_sio = h_dir->i_mode & S_ISVTX;
5773 +               }
5774 +       }
5775 +
5776 +       return do_sio;
5777 +}
5778 +
5779 +#if 0 /* reserved */
5780 +int au_sio_cpup_single(struct au_cp_generic *cpg, struct dentry *dst_parent)
5781 +{
5782 +       int err, wkq_err;
5783 +       struct dentry *h_dentry;
5784 +
5785 +       h_dentry = au_h_dptr(cpg->dentry, cpg->bsrc);
5786 +       if (!au_cpup_sio_test(pin, d_inode(h_dentry)->i_mode))
5787 +               err = au_cpup_single(cpg, dst_parent);
5788 +       else {
5789 +               struct au_cpup_single_args args = {
5790 +                       .errp           = &err,
5791 +                       .cpg            = cpg,
5792 +                       .dst_parent     = dst_parent
5793 +               };
5794 +               wkq_err = au_wkq_wait(au_call_cpup_single, &args);
5795 +               if (unlikely(wkq_err))
5796 +                       err = wkq_err;
5797 +       }
5798 +
5799 +       return err;
5800 +}
5801 +#endif
5802 +
5803 +/*
5804 + * copyup the @dentry from the first active lower branch to @bdst,
5805 + * using au_cpup_single().
5806 + */
5807 +static int au_cpup_simple(struct au_cp_generic *cpg)
5808 +{
5809 +       int err;
5810 +       unsigned int flags_orig;
5811 +       struct dentry *dentry;
5812 +
5813 +       AuDebugOn(cpg->bsrc < 0);
5814 +
5815 +       dentry = cpg->dentry;
5816 +       DiMustWriteLock(dentry);
5817 +
5818 +       err = au_lkup_neg(dentry, cpg->bdst, /*wh*/1);
5819 +       if (!err) {
5820 +               flags_orig = cpg->flags;
5821 +               au_fset_cpup(cpg->flags, RENAME);
5822 +               err = au_cpup_single(cpg, NULL);
5823 +               cpg->flags = flags_orig;
5824 +               if (!err)
5825 +                       return 0; /* success */
5826 +
5827 +               /* revert */
5828 +               au_set_h_dptr(dentry, cpg->bdst, NULL);
5829 +               au_set_dbtop(dentry, cpg->bsrc);
5830 +       }
5831 +
5832 +       return err;
5833 +}
5834 +
5835 +struct au_cpup_simple_args {
5836 +       int *errp;
5837 +       struct au_cp_generic *cpg;
5838 +};
5839 +
5840 +static void au_call_cpup_simple(void *args)
5841 +{
5842 +       struct au_cpup_simple_args *a = args;
5843 +
5844 +       au_pin_hdir_acquire_nest(a->cpg->pin);
5845 +       *a->errp = au_cpup_simple(a->cpg);
5846 +       au_pin_hdir_release(a->cpg->pin);
5847 +}
5848 +
5849 +static int au_do_sio_cpup_simple(struct au_cp_generic *cpg)
5850 +{
5851 +       int err, wkq_err;
5852 +       struct dentry *dentry, *parent;
5853 +       struct file *h_file;
5854 +       struct inode *h_dir;
5855 +       struct user_namespace *h_userns;
5856 +
5857 +       dentry = cpg->dentry;
5858 +       h_file = NULL;
5859 +       if (au_ftest_cpup(cpg->flags, HOPEN)) {
5860 +               AuDebugOn(cpg->bsrc < 0);
5861 +               h_file = au_h_open_pre(dentry, cpg->bsrc, /*force_wr*/0);
5862 +               err = PTR_ERR(h_file);
5863 +               if (IS_ERR(h_file))
5864 +                       goto out;
5865 +       }
5866 +
5867 +       parent = dget_parent(dentry);
5868 +       h_dir = au_h_iptr(d_inode(parent), cpg->bdst);
5869 +       h_userns = au_sbr_userns(dentry->d_sb, cpg->bdst);
5870 +       if (!au_test_h_perm_sio(h_userns, h_dir, MAY_EXEC | MAY_WRITE)
5871 +           && !au_cpup_sio_test(cpg->pin, d_inode(dentry)->i_mode))
5872 +               err = au_cpup_simple(cpg);
5873 +       else {
5874 +               struct au_cpup_simple_args args = {
5875 +                       .errp           = &err,
5876 +                       .cpg            = cpg
5877 +               };
5878 +               wkq_err = au_wkq_wait(au_call_cpup_simple, &args);
5879 +               if (unlikely(wkq_err))
5880 +                       err = wkq_err;
5881 +       }
5882 +
5883 +       dput(parent);
5884 +       if (h_file)
5885 +               au_h_open_post(dentry, cpg->bsrc, h_file);
5886 +
5887 +out:
5888 +       return err;
5889 +}
5890 +
5891 +int au_sio_cpup_simple(struct au_cp_generic *cpg)
5892 +{
5893 +       aufs_bindex_t bsrc, bbot;
5894 +       struct dentry *dentry, *h_dentry;
5895 +
5896 +       if (cpg->bsrc < 0) {
5897 +               dentry = cpg->dentry;
5898 +               bbot = au_dbbot(dentry);
5899 +               for (bsrc = cpg->bdst + 1; bsrc <= bbot; bsrc++) {
5900 +                       h_dentry = au_h_dptr(dentry, bsrc);
5901 +                       if (h_dentry) {
5902 +                               AuDebugOn(d_is_negative(h_dentry));
5903 +                               break;
5904 +                       }
5905 +               }
5906 +               AuDebugOn(bsrc > bbot);
5907 +               cpg->bsrc = bsrc;
5908 +       }
5909 +       AuDebugOn(cpg->bsrc <= cpg->bdst);
5910 +       return au_do_sio_cpup_simple(cpg);
5911 +}
5912 +
5913 +int au_sio_cpdown_simple(struct au_cp_generic *cpg)
5914 +{
5915 +       AuDebugOn(cpg->bdst <= cpg->bsrc);
5916 +       return au_do_sio_cpup_simple(cpg);
5917 +}
5918 +
5919 +/* ---------------------------------------------------------------------- */
5920 +
5921 +/*
5922 + * copyup the deleted file for writing.
5923 + */
5924 +static int au_do_cpup_wh(struct au_cp_generic *cpg, struct dentry *wh_dentry,
5925 +                        struct file *file)
5926 +{
5927 +       int err;
5928 +       unsigned int flags_orig;
5929 +       aufs_bindex_t bsrc_orig;
5930 +       struct au_dinfo *dinfo;
5931 +       struct {
5932 +               struct au_hdentry *hd;
5933 +               struct dentry *h_dentry;
5934 +       } hdst, hsrc;
5935 +
5936 +       dinfo = au_di(cpg->dentry);
5937 +       AuRwMustWriteLock(&dinfo->di_rwsem);
5938 +
5939 +       bsrc_orig = cpg->bsrc;
5940 +       cpg->bsrc = dinfo->di_btop;
5941 +       hdst.hd = au_hdentry(dinfo, cpg->bdst);
5942 +       hdst.h_dentry = hdst.hd->hd_dentry;
5943 +       hdst.hd->hd_dentry = wh_dentry;
5944 +       dinfo->di_btop = cpg->bdst;
5945 +
5946 +       hsrc.h_dentry = NULL;
5947 +       if (file) {
5948 +               hsrc.hd = au_hdentry(dinfo, cpg->bsrc);
5949 +               hsrc.h_dentry = hsrc.hd->hd_dentry;
5950 +               hsrc.hd->hd_dentry = au_hf_top(file)->f_path.dentry;
5951 +       }
5952 +       flags_orig = cpg->flags;
5953 +       cpg->flags = !AuCpup_DTIME;
5954 +       err = au_cpup_single(cpg, /*h_parent*/NULL);
5955 +       cpg->flags = flags_orig;
5956 +       if (file) {
5957 +               if (!err)
5958 +                       err = au_reopen_nondir(file);
5959 +               hsrc.hd->hd_dentry = hsrc.h_dentry;
5960 +       }
5961 +       hdst.hd->hd_dentry = hdst.h_dentry;
5962 +       dinfo->di_btop = cpg->bsrc;
5963 +       cpg->bsrc = bsrc_orig;
5964 +
5965 +       return err;
5966 +}
5967 +
5968 +static int au_cpup_wh(struct au_cp_generic *cpg, struct file *file)
5969 +{
5970 +       int err;
5971 +       aufs_bindex_t bdst;
5972 +       struct au_dtime dt;
5973 +       struct dentry *dentry, *parent, *h_parent, *wh_dentry;
5974 +       struct au_branch *br;
5975 +       struct path h_path;
5976 +
5977 +       dentry = cpg->dentry;
5978 +       bdst = cpg->bdst;
5979 +       br = au_sbr(dentry->d_sb, bdst);
5980 +       parent = dget_parent(dentry);
5981 +       h_parent = au_h_dptr(parent, bdst);
5982 +       wh_dentry = au_whtmp_lkup(h_parent, br, &dentry->d_name);
5983 +       err = PTR_ERR(wh_dentry);
5984 +       if (IS_ERR(wh_dentry))
5985 +               goto out;
5986 +
5987 +       h_path.dentry = h_parent;
5988 +       h_path.mnt = au_br_mnt(br);
5989 +       au_dtime_store(&dt, parent, &h_path);
5990 +       err = au_do_cpup_wh(cpg, wh_dentry, file);
5991 +       if (unlikely(err))
5992 +               goto out_wh;
5993 +
5994 +       dget(wh_dentry);
5995 +       h_path.dentry = wh_dentry;
5996 +       if (!d_is_dir(wh_dentry)) {
5997 +               /* no delegation since it is just created */
5998 +               err = vfsub_unlink(d_inode(h_parent), &h_path,
5999 +                                  /*delegated*/NULL, /*force*/0);
6000 +       } else
6001 +               err = vfsub_rmdir(d_inode(h_parent), &h_path);
6002 +       if (unlikely(err)) {
6003 +               AuIOErr("failed remove copied-up tmp file %pd(%d)\n",
6004 +                       wh_dentry, err);
6005 +               err = -EIO;
6006 +       }
6007 +       au_dtime_revert(&dt);
6008 +       au_set_hi_wh(d_inode(dentry), bdst, wh_dentry);
6009 +
6010 +out_wh:
6011 +       dput(wh_dentry);
6012 +out:
6013 +       dput(parent);
6014 +       return err;
6015 +}
6016 +
6017 +struct au_cpup_wh_args {
6018 +       int *errp;
6019 +       struct au_cp_generic *cpg;
6020 +       struct file *file;
6021 +};
6022 +
6023 +static void au_call_cpup_wh(void *args)
6024 +{
6025 +       struct au_cpup_wh_args *a = args;
6026 +
6027 +       au_pin_hdir_acquire_nest(a->cpg->pin);
6028 +       *a->errp = au_cpup_wh(a->cpg, a->file);
6029 +       au_pin_hdir_release(a->cpg->pin);
6030 +}
6031 +
6032 +int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file)
6033 +{
6034 +       int err, wkq_err;
6035 +       aufs_bindex_t bdst;
6036 +       struct dentry *dentry, *parent, *h_orph, *h_parent;
6037 +       struct inode *dir, *h_dir, *h_tmpdir;
6038 +       struct au_wbr *wbr;
6039 +       struct au_pin wh_pin, *pin_orig;
6040 +       struct user_namespace *h_userns;
6041 +
6042 +       dentry = cpg->dentry;
6043 +       bdst = cpg->bdst;
6044 +       parent = dget_parent(dentry);
6045 +       dir = d_inode(parent);
6046 +       h_orph = NULL;
6047 +       h_parent = NULL;
6048 +       h_dir = au_igrab(au_h_iptr(dir, bdst));
6049 +       h_tmpdir = h_dir;
6050 +       pin_orig = NULL;
6051 +       if (!h_dir->i_nlink) {
6052 +               wbr = au_sbr(dentry->d_sb, bdst)->br_wbr;
6053 +               h_orph = wbr->wbr_orph;
6054 +
6055 +               h_parent = dget(au_h_dptr(parent, bdst));
6056 +               au_set_h_dptr(parent, bdst, dget(h_orph));
6057 +               h_tmpdir = d_inode(h_orph);
6058 +               au_set_h_iptr(dir, bdst, au_igrab(h_tmpdir), /*flags*/0);
6059 +
6060 +               inode_lock_nested(h_tmpdir, AuLsc_I_PARENT3);
6061 +               /* todo: au_h_open_pre()? */
6062 +
6063 +               pin_orig = cpg->pin;
6064 +               au_pin_init(&wh_pin, dentry, bdst, AuLsc_DI_PARENT,
6065 +                           AuLsc_I_PARENT3, cpg->pin->udba, AuPin_DI_LOCKED);
6066 +               cpg->pin = &wh_pin;
6067 +       }
6068 +
6069 +       h_userns = au_sbr_userns(dentry->d_sb, bdst);
6070 +       if (!au_test_h_perm_sio(h_userns, h_tmpdir, MAY_EXEC | MAY_WRITE)
6071 +           && !au_cpup_sio_test(cpg->pin, d_inode(dentry)->i_mode))
6072 +               err = au_cpup_wh(cpg, file);
6073 +       else {
6074 +               struct au_cpup_wh_args args = {
6075 +                       .errp   = &err,
6076 +                       .cpg    = cpg,
6077 +                       .file   = file
6078 +               };
6079 +               wkq_err = au_wkq_wait(au_call_cpup_wh, &args);
6080 +               if (unlikely(wkq_err))
6081 +                       err = wkq_err;
6082 +       }
6083 +
6084 +       if (h_orph) {
6085 +               inode_unlock(h_tmpdir);
6086 +               /* todo: au_h_open_post()? */
6087 +               au_set_h_iptr(dir, bdst, au_igrab(h_dir), /*flags*/0);
6088 +               au_set_h_dptr(parent, bdst, h_parent);
6089 +               AuDebugOn(!pin_orig);
6090 +               cpg->pin = pin_orig;
6091 +       }
6092 +       iput(h_dir);
6093 +       dput(parent);
6094 +
6095 +       return err;
6096 +}
6097 +
6098 +/* ---------------------------------------------------------------------- */
6099 +
6100 +/*
6101 + * generic routine for both of copy-up and copy-down.
6102 + */
6103 +/* cf. revalidate function in file.c */
6104 +int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
6105 +              int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
6106 +                        struct au_pin *pin,
6107 +                        struct dentry *h_parent, void *arg),
6108 +              void *arg)
6109 +{
6110 +       int err;
6111 +       struct au_pin pin;
6112 +       struct dentry *d, *parent, *h_parent, *real_parent, *h_dentry;
6113 +
6114 +       err = 0;
6115 +       parent = dget_parent(dentry);
6116 +       if (IS_ROOT(parent))
6117 +               goto out;
6118 +
6119 +       au_pin_init(&pin, dentry, bdst, AuLsc_DI_PARENT2, AuLsc_I_PARENT2,
6120 +                   au_opt_udba(dentry->d_sb), AuPin_MNT_WRITE);
6121 +
6122 +       /* do not use au_dpage */
6123 +       real_parent = parent;
6124 +       while (1) {
6125 +               dput(parent);
6126 +               parent = dget_parent(dentry);
6127 +               h_parent = au_h_dptr(parent, bdst);
6128 +               if (h_parent)
6129 +                       goto out; /* success */
6130 +
6131 +               /* find top dir which is necessary to cpup */
6132 +               do {
6133 +                       d = parent;
6134 +                       dput(parent);
6135 +                       parent = dget_parent(d);
6136 +                       di_read_lock_parent3(parent, !AuLock_IR);
6137 +                       h_parent = au_h_dptr(parent, bdst);
6138 +                       di_read_unlock(parent, !AuLock_IR);
6139 +               } while (!h_parent);
6140 +
6141 +               if (d != real_parent)
6142 +                       di_write_lock_child3(d);
6143 +
6144 +               /* somebody else might create while we were sleeping */
6145 +               h_dentry = au_h_dptr(d, bdst);
6146 +               if (!h_dentry || d_is_negative(h_dentry)) {
6147 +                       if (h_dentry)
6148 +                               au_update_dbtop(d);
6149 +
6150 +                       au_pin_set_dentry(&pin, d);
6151 +                       err = au_do_pin(&pin);
6152 +                       if (!err) {
6153 +                               err = cp(d, bdst, &pin, h_parent, arg);
6154 +                               au_unpin(&pin);
6155 +                       }
6156 +               }
6157 +
6158 +               if (d != real_parent)
6159 +                       di_write_unlock(d);
6160 +               if (unlikely(err))
6161 +                       break;
6162 +       }
6163 +
6164 +out:
6165 +       dput(parent);
6166 +       return err;
6167 +}
6168 +
6169 +static int au_cpup_dir(struct dentry *dentry, aufs_bindex_t bdst,
6170 +                      struct au_pin *pin,
6171 +                      struct dentry *h_parent __maybe_unused,
6172 +                      void *arg __maybe_unused)
6173 +{
6174 +       struct au_cp_generic cpg = {
6175 +               .dentry = dentry,
6176 +               .bdst   = bdst,
6177 +               .bsrc   = -1,
6178 +               .len    = 0,
6179 +               .pin    = pin,
6180 +               .flags  = AuCpup_DTIME
6181 +       };
6182 +       return au_sio_cpup_simple(&cpg);
6183 +}
6184 +
6185 +int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
6186 +{
6187 +       return au_cp_dirs(dentry, bdst, au_cpup_dir, NULL);
6188 +}
6189 +
6190 +int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst)
6191 +{
6192 +       int err;
6193 +       struct dentry *parent;
6194 +       struct inode *dir;
6195 +
6196 +       parent = dget_parent(dentry);
6197 +       dir = d_inode(parent);
6198 +       err = 0;
6199 +       if (au_h_iptr(dir, bdst))
6200 +               goto out;
6201 +
6202 +       di_read_unlock(parent, AuLock_IR);
6203 +       di_write_lock_parent(parent);
6204 +       /* someone else might change our inode while we were sleeping */
6205 +       if (!au_h_iptr(dir, bdst))
6206 +               err = au_cpup_dirs(dentry, bdst);
6207 +       di_downgrade_lock(parent, AuLock_IR);
6208 +
6209 +out:
6210 +       dput(parent);
6211 +       return err;
6212 +}
6213 diff -urN /usr/share/empty/fs/aufs/cpup.h linux/fs/aufs/cpup.h
6214 --- /usr/share/empty/fs/aufs/cpup.h     1970-01-01 01:00:00.000000000 +0100
6215 +++ linux/fs/aufs/cpup.h        2022-03-21 14:49:05.719966344 +0100
6216 @@ -0,0 +1,100 @@
6217 +/* SPDX-License-Identifier: GPL-2.0 */
6218 +/*
6219 + * Copyright (C) 2005-2021 Junjiro R. Okajima
6220 + *
6221 + * This program, aufs is free software; you can redistribute it and/or modify
6222 + * it under the terms of the GNU General Public License as published by
6223 + * the Free Software Foundation; either version 2 of the License, or
6224 + * (at your option) any later version.
6225 + *
6226 + * This program is distributed in the hope that it will be useful,
6227 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6228 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6229 + * GNU General Public License for more details.
6230 + *
6231 + * You should have received a copy of the GNU General Public License
6232 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6233 + */
6234 +
6235 +/*
6236 + * copy-up/down functions
6237 + */
6238 +
6239 +#ifndef __AUFS_CPUP_H__
6240 +#define __AUFS_CPUP_H__
6241 +
6242 +#ifdef __KERNEL__
6243 +
6244 +#include <linux/path.h>
6245 +
6246 +struct inode;
6247 +struct file;
6248 +struct au_pin;
6249 +
6250 +void au_cpup_attr_flags(struct inode *dst, unsigned int iflags);
6251 +void au_cpup_attr_timesizes(struct inode *inode);
6252 +void au_cpup_attr_nlink(struct inode *inode, int force);
6253 +void au_cpup_attr_changeable(struct inode *inode);
6254 +void au_cpup_igen(struct inode *inode, struct inode *h_inode);
6255 +void au_cpup_attr_all(struct inode *inode, int force);
6256 +
6257 +/* ---------------------------------------------------------------------- */
6258 +
6259 +struct au_cp_generic {
6260 +       struct dentry   *dentry;
6261 +       aufs_bindex_t   bdst, bsrc;
6262 +       loff_t          len;
6263 +       struct au_pin   *pin;
6264 +       unsigned int    flags;
6265 +};
6266 +
6267 +/* cpup flags */
6268 +#define AuCpup_DTIME           1               /* do dtime_store/revert */
6269 +#define AuCpup_KEEPLINO                (1 << 1)        /* do not clear the lower xino,
6270 +                                                  for link(2) */
6271 +#define AuCpup_RENAME          (1 << 2)        /* rename after cpup */
6272 +#define AuCpup_HOPEN           (1 << 3)        /* call h_open_pre/post() in
6273 +                                                  cpup */
6274 +#define AuCpup_OVERWRITE       (1 << 4)        /* allow overwriting the
6275 +                                                  existing entry */
6276 +#define AuCpup_RWDST           (1 << 5)        /* force write target even if
6277 +                                                  the branch is marked as RO */
6278 +
6279 +#ifndef CONFIG_AUFS_BR_HFSPLUS
6280 +#undef AuCpup_HOPEN
6281 +#define AuCpup_HOPEN           0
6282 +#endif
6283 +
6284 +#define au_ftest_cpup(flags, name)     ((flags) & AuCpup_##name)
6285 +#define au_fset_cpup(flags, name) \
6286 +       do { (flags) |= AuCpup_##name; } while (0)
6287 +#define au_fclr_cpup(flags, name) \
6288 +       do { (flags) &= ~AuCpup_##name; } while (0)
6289 +
6290 +int au_copy_file(struct file *dst, struct file *src, loff_t len);
6291 +int au_sio_cpup_simple(struct au_cp_generic *cpg);
6292 +int au_sio_cpdown_simple(struct au_cp_generic *cpg);
6293 +int au_sio_cpup_wh(struct au_cp_generic *cpg, struct file *file);
6294 +
6295 +int au_cp_dirs(struct dentry *dentry, aufs_bindex_t bdst,
6296 +              int (*cp)(struct dentry *dentry, aufs_bindex_t bdst,
6297 +                        struct au_pin *pin,
6298 +                        struct dentry *h_parent, void *arg),
6299 +              void *arg);
6300 +int au_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
6301 +int au_test_and_cpup_dirs(struct dentry *dentry, aufs_bindex_t bdst);
6302 +
6303 +/* ---------------------------------------------------------------------- */
6304 +
6305 +/* keep timestamps when copyup */
6306 +struct au_dtime {
6307 +       struct dentry *dt_dentry;
6308 +       struct path dt_h_path;
6309 +       struct timespec64 dt_atime, dt_mtime;
6310 +};
6311 +void au_dtime_store(struct au_dtime *dt, struct dentry *dentry,
6312 +                   struct path *h_path);
6313 +void au_dtime_revert(struct au_dtime *dt);
6314 +
6315 +#endif /* __KERNEL__ */
6316 +#endif /* __AUFS_CPUP_H__ */
6317 diff -urN /usr/share/empty/fs/aufs/dbgaufs.c linux/fs/aufs/dbgaufs.c
6318 --- /usr/share/empty/fs/aufs/dbgaufs.c  1970-01-01 01:00:00.000000000 +0100
6319 +++ linux/fs/aufs/dbgaufs.c     2022-03-21 14:49:05.719966344 +0100
6320 @@ -0,0 +1,526 @@
6321 +// SPDX-License-Identifier: GPL-2.0
6322 +/*
6323 + * Copyright (C) 2005-2021 Junjiro R. Okajima
6324 + *
6325 + * This program, aufs is free software; you can redistribute it and/or modify
6326 + * it under the terms of the GNU General Public License as published by
6327 + * the Free Software Foundation; either version 2 of the License, or
6328 + * (at your option) any later version.
6329 + *
6330 + * This program is distributed in the hope that it will be useful,
6331 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6332 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6333 + * GNU General Public License for more details.
6334 + *
6335 + * You should have received a copy of the GNU General Public License
6336 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6337 + */
6338 +
6339 +/*
6340 + * debugfs interface
6341 + */
6342 +
6343 +#include <linux/debugfs.h>
6344 +#include "aufs.h"
6345 +
6346 +#ifndef CONFIG_SYSFS
6347 +#error DEBUG_FS depends upon SYSFS
6348 +#endif
6349 +
6350 +static struct dentry *dbgaufs;
6351 +static const mode_t dbgaufs_mode = 0444;
6352 +
6353 +/* 20 is max digits length of ulong 64 */
6354 +struct dbgaufs_arg {
6355 +       int n;
6356 +       char a[20 * 4];
6357 +};
6358 +
6359 +/*
6360 + * common function for all XINO files
6361 + */
6362 +static int dbgaufs_xi_release(struct inode *inode __maybe_unused,
6363 +                             struct file *file)
6364 +{
6365 +       void *p;
6366 +
6367 +       p = file->private_data;
6368 +       if (p) {
6369 +               /* this is struct dbgaufs_arg */
6370 +               AuDebugOn(!au_kfree_sz_test(p));
6371 +               au_kfree_do_rcu(p);
6372 +       }
6373 +       return 0;
6374 +}
6375 +
6376 +static int dbgaufs_xi_open(struct file *xf, struct file *file, int do_fcnt,
6377 +                          int cnt)
6378 +{
6379 +       int err;
6380 +       struct kstat st;
6381 +       struct dbgaufs_arg *p;
6382 +
6383 +       err = -ENOMEM;
6384 +       p = kmalloc(sizeof(*p), GFP_NOFS);
6385 +       if (unlikely(!p))
6386 +               goto out;
6387 +
6388 +       err = 0;
6389 +       p->n = 0;
6390 +       file->private_data = p;
6391 +       if (!xf)
6392 +               goto out;
6393 +
6394 +       err = vfsub_getattr(&xf->f_path, &st);
6395 +       if (!err) {
6396 +               if (do_fcnt)
6397 +                       p->n = snprintf
6398 +                               (p->a, sizeof(p->a), "%d, %llux%u %lld\n",
6399 +                                cnt, st.blocks, st.blksize,
6400 +                                (long long)st.size);
6401 +               else
6402 +                       p->n = snprintf(p->a, sizeof(p->a), "%llux%u %lld\n",
6403 +                                       st.blocks, st.blksize,
6404 +                                       (long long)st.size);
6405 +               AuDebugOn(p->n >= sizeof(p->a));
6406 +       } else {
6407 +               p->n = snprintf(p->a, sizeof(p->a), "err %d\n", err);
6408 +               err = 0;
6409 +       }
6410 +
6411 +out:
6412 +       return err;
6413 +}
6414 +
6415 +static ssize_t dbgaufs_xi_read(struct file *file, char __user *buf,
6416 +                              size_t count, loff_t *ppos)
6417 +{
6418 +       struct dbgaufs_arg *p;
6419 +
6420 +       p = file->private_data;
6421 +       return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
6422 +}
6423 +
6424 +/* ---------------------------------------------------------------------- */
6425 +
6426 +struct dbgaufs_plink_arg {
6427 +       int n;
6428 +       char a[];
6429 +};
6430 +
6431 +static int dbgaufs_plink_release(struct inode *inode __maybe_unused,
6432 +                                struct file *file)
6433 +{
6434 +       free_page((unsigned long)file->private_data);
6435 +       return 0;
6436 +}
6437 +
6438 +static int dbgaufs_plink_open(struct inode *inode, struct file *file)
6439 +{
6440 +       int err, i, limit;
6441 +       unsigned long n, sum;
6442 +       struct dbgaufs_plink_arg *p;
6443 +       struct au_sbinfo *sbinfo;
6444 +       struct super_block *sb;
6445 +       struct hlist_bl_head *hbl;
6446 +
6447 +       err = -ENOMEM;
6448 +       p = (void *)get_zeroed_page(GFP_NOFS);
6449 +       if (unlikely(!p))
6450 +               goto out;
6451 +
6452 +       err = -EFBIG;
6453 +       sbinfo = inode->i_private;
6454 +       sb = sbinfo->si_sb;
6455 +       si_noflush_read_lock(sb);
6456 +       if (au_opt_test(au_mntflags(sb), PLINK)) {
6457 +               limit = PAGE_SIZE - sizeof(p->n);
6458 +
6459 +               /* the number of buckets */
6460 +               n = snprintf(p->a + p->n, limit, "%d\n", AuPlink_NHASH);
6461 +               p->n += n;
6462 +               limit -= n;
6463 +
6464 +               sum = 0;
6465 +               for (i = 0, hbl = sbinfo->si_plink; i < AuPlink_NHASH;
6466 +                    i++, hbl++) {
6467 +                       n = au_hbl_count(hbl);
6468 +                       sum += n;
6469 +
6470 +                       n = snprintf(p->a + p->n, limit, "%lu ", n);
6471 +                       p->n += n;
6472 +                       limit -= n;
6473 +                       if (unlikely(limit <= 0))
6474 +                               goto out_free;
6475 +               }
6476 +               p->a[p->n - 1] = '\n';
6477 +
6478 +               /* the sum of plinks */
6479 +               n = snprintf(p->a + p->n, limit, "%lu\n", sum);
6480 +               p->n += n;
6481 +               limit -= n;
6482 +               if (unlikely(limit <= 0))
6483 +                       goto out_free;
6484 +       } else {
6485 +#define str "1\n0\n0\n"
6486 +               p->n = sizeof(str) - 1;
6487 +               strcpy(p->a, str);
6488 +#undef str
6489 +       }
6490 +       si_read_unlock(sb);
6491 +
6492 +       err = 0;
6493 +       file->private_data = p;
6494 +       goto out; /* success */
6495 +
6496 +out_free:
6497 +       free_page((unsigned long)p);
6498 +out:
6499 +       return err;
6500 +}
6501 +
6502 +static ssize_t dbgaufs_plink_read(struct file *file, char __user *buf,
6503 +                                 size_t count, loff_t *ppos)
6504 +{
6505 +       struct dbgaufs_plink_arg *p;
6506 +
6507 +       p = file->private_data;
6508 +       return simple_read_from_buffer(buf, count, ppos, p->a, p->n);
6509 +}
6510 +
6511 +static const struct file_operations dbgaufs_plink_fop = {
6512 +       .owner          = THIS_MODULE,
6513 +       .open           = dbgaufs_plink_open,
6514 +       .release        = dbgaufs_plink_release,
6515 +       .read           = dbgaufs_plink_read
6516 +};
6517 +
6518 +/* ---------------------------------------------------------------------- */
6519 +
6520 +static int dbgaufs_xib_open(struct inode *inode, struct file *file)
6521 +{
6522 +       int err;
6523 +       struct au_sbinfo *sbinfo;
6524 +       struct super_block *sb;
6525 +
6526 +       sbinfo = inode->i_private;
6527 +       sb = sbinfo->si_sb;
6528 +       si_noflush_read_lock(sb);
6529 +       err = dbgaufs_xi_open(sbinfo->si_xib, file, /*do_fcnt*/0, /*cnt*/0);
6530 +       si_read_unlock(sb);
6531 +       return err;
6532 +}
6533 +
6534 +static const struct file_operations dbgaufs_xib_fop = {
6535 +       .owner          = THIS_MODULE,
6536 +       .open           = dbgaufs_xib_open,
6537 +       .release        = dbgaufs_xi_release,
6538 +       .read           = dbgaufs_xi_read
6539 +};
6540 +
6541 +/* ---------------------------------------------------------------------- */
6542 +
6543 +#define DbgaufsXi_PREFIX "xi"
6544 +
6545 +static int dbgaufs_xino_open(struct inode *inode, struct file *file)
6546 +{
6547 +       int err, idx;
6548 +       long l;
6549 +       aufs_bindex_t bindex;
6550 +       char *p, a[sizeof(DbgaufsXi_PREFIX) + 8];
6551 +       struct au_sbinfo *sbinfo;
6552 +       struct super_block *sb;
6553 +       struct au_xino *xi;
6554 +       struct file *xf;
6555 +       struct qstr *name;
6556 +       struct au_branch *br;
6557 +
6558 +       err = -ENOENT;
6559 +       name = &file->f_path.dentry->d_name;
6560 +       if (unlikely(name->len < sizeof(DbgaufsXi_PREFIX)
6561 +                    || memcmp(name->name, DbgaufsXi_PREFIX,
6562 +                              sizeof(DbgaufsXi_PREFIX) - 1)))
6563 +               goto out;
6564 +
6565 +       AuDebugOn(name->len >= sizeof(a));
6566 +       memcpy(a, name->name, name->len);
6567 +       a[name->len] = '\0';
6568 +       p = strchr(a, '-');
6569 +       if (p)
6570 +               *p = '\0';
6571 +       err = kstrtol(a + sizeof(DbgaufsXi_PREFIX) - 1, 10, &l);
6572 +       if (unlikely(err))
6573 +               goto out;
6574 +       bindex = l;
6575 +       idx = 0;
6576 +       if (p) {
6577 +               err = kstrtol(p + 1, 10, &l);
6578 +               if (unlikely(err))
6579 +                       goto out;
6580 +               idx = l;
6581 +       }
6582 +
6583 +       err = -ENOENT;
6584 +       sbinfo = inode->i_private;
6585 +       sb = sbinfo->si_sb;
6586 +       si_noflush_read_lock(sb);
6587 +       if (unlikely(bindex < 0 || bindex > au_sbbot(sb)))
6588 +               goto out_si;
6589 +       br = au_sbr(sb, bindex);
6590 +       xi = br->br_xino;
6591 +       if (unlikely(idx >= xi->xi_nfile))
6592 +               goto out_si;
6593 +       xf = au_xino_file(xi, idx);
6594 +       if (xf)
6595 +               err = dbgaufs_xi_open(xf, file, /*do_fcnt*/1,
6596 +                                     au_xino_count(br));
6597 +
6598 +out_si:
6599 +       si_read_unlock(sb);
6600 +out:
6601 +       AuTraceErr(err);
6602 +       return err;
6603 +}
6604 +
6605 +static const struct file_operations dbgaufs_xino_fop = {
6606 +       .owner          = THIS_MODULE,
6607 +       .open           = dbgaufs_xino_open,
6608 +       .release        = dbgaufs_xi_release,
6609 +       .read           = dbgaufs_xi_read
6610 +};
6611 +
6612 +void dbgaufs_xino_del(struct au_branch *br)
6613 +{
6614 +       struct dentry *dbgaufs;
6615 +
6616 +       dbgaufs = br->br_dbgaufs;
6617 +       if (!dbgaufs)
6618 +               return;
6619 +
6620 +       br->br_dbgaufs = NULL;
6621 +       /* debugfs acquires the parent i_mutex */
6622 +       lockdep_off();
6623 +       debugfs_remove(dbgaufs);
6624 +       lockdep_on();
6625 +}
6626 +
6627 +void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
6628 +{
6629 +       aufs_bindex_t bbot;
6630 +       struct au_branch *br;
6631 +
6632 +       if (!au_sbi(sb)->si_dbgaufs)
6633 +               return;
6634 +
6635 +       bbot = au_sbbot(sb);
6636 +       for (; bindex <= bbot; bindex++) {
6637 +               br = au_sbr(sb, bindex);
6638 +               dbgaufs_xino_del(br);
6639 +       }
6640 +}
6641 +
6642 +static void dbgaufs_br_do_add(struct super_block *sb, aufs_bindex_t bindex,
6643 +                             unsigned int idx, struct dentry *parent,
6644 +                             struct au_sbinfo *sbinfo)
6645 +{
6646 +       struct au_branch *br;
6647 +       struct dentry *d;
6648 +       /* "xi" bindex(5) "-" idx(2) NULL */
6649 +       char name[sizeof(DbgaufsXi_PREFIX) + 8];
6650 +
6651 +       if (!idx)
6652 +               snprintf(name, sizeof(name), DbgaufsXi_PREFIX "%d", bindex);
6653 +       else
6654 +               snprintf(name, sizeof(name), DbgaufsXi_PREFIX "%d-%u",
6655 +                        bindex, idx);
6656 +       br = au_sbr(sb, bindex);
6657 +       if (br->br_dbgaufs) {
6658 +               struct qstr qstr = QSTR_INIT(name, strlen(name));
6659 +
6660 +               if (!au_qstreq(&br->br_dbgaufs->d_name, &qstr)) {
6661 +                       /* debugfs acquires the parent i_mutex */
6662 +                       lockdep_off();
6663 +                       d = debugfs_rename(parent, br->br_dbgaufs, parent,
6664 +                                          name);
6665 +                       lockdep_on();
6666 +                       if (unlikely(!d))
6667 +                               pr_warn("failed renaming %pd/%s, ignored.\n",
6668 +                                       parent, name);
6669 +               }
6670 +       } else {
6671 +               lockdep_off();
6672 +               br->br_dbgaufs = debugfs_create_file(name, dbgaufs_mode, parent,
6673 +                                                    sbinfo, &dbgaufs_xino_fop);
6674 +               lockdep_on();
6675 +               if (unlikely(!br->br_dbgaufs))
6676 +                       pr_warn("failed creating %pd/%s, ignored.\n",
6677 +                               parent, name);
6678 +       }
6679 +}
6680 +
6681 +static void dbgaufs_br_add(struct super_block *sb, aufs_bindex_t bindex,
6682 +                          struct dentry *parent, struct au_sbinfo *sbinfo)
6683 +{
6684 +       struct au_branch *br;
6685 +       struct au_xino *xi;
6686 +       unsigned int u;
6687 +
6688 +       br = au_sbr(sb, bindex);
6689 +       xi = br->br_xino;
6690 +       for (u = 0; u < xi->xi_nfile; u++)
6691 +               dbgaufs_br_do_add(sb, bindex, u, parent, sbinfo);
6692 +}
6693 +
6694 +void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex, int topdown)
6695 +{
6696 +       struct au_sbinfo *sbinfo;
6697 +       struct dentry *parent;
6698 +       aufs_bindex_t bbot;
6699 +
6700 +       if (!au_opt_test(au_mntflags(sb), XINO))
6701 +               return;
6702 +
6703 +       sbinfo = au_sbi(sb);
6704 +       parent = sbinfo->si_dbgaufs;
6705 +       if (!parent)
6706 +               return;
6707 +
6708 +       bbot = au_sbbot(sb);
6709 +       if (topdown)
6710 +               for (; bindex <= bbot; bindex++)
6711 +                       dbgaufs_br_add(sb, bindex, parent, sbinfo);
6712 +       else
6713 +               for (; bbot >= bindex; bbot--)
6714 +                       dbgaufs_br_add(sb, bbot, parent, sbinfo);
6715 +}
6716 +
6717 +/* ---------------------------------------------------------------------- */
6718 +
6719 +#ifdef CONFIG_AUFS_EXPORT
6720 +static int dbgaufs_xigen_open(struct inode *inode, struct file *file)
6721 +{
6722 +       int err;
6723 +       struct au_sbinfo *sbinfo;
6724 +       struct super_block *sb;
6725 +
6726 +       sbinfo = inode->i_private;
6727 +       sb = sbinfo->si_sb;
6728 +       si_noflush_read_lock(sb);
6729 +       err = dbgaufs_xi_open(sbinfo->si_xigen, file, /*do_fcnt*/0, /*cnt*/0);
6730 +       si_read_unlock(sb);
6731 +       return err;
6732 +}
6733 +
6734 +static const struct file_operations dbgaufs_xigen_fop = {
6735 +       .owner          = THIS_MODULE,
6736 +       .open           = dbgaufs_xigen_open,
6737 +       .release        = dbgaufs_xi_release,
6738 +       .read           = dbgaufs_xi_read
6739 +};
6740 +
6741 +static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
6742 +{
6743 +       int err;
6744 +
6745 +       /*
6746 +        * This function is a dynamic '__init' function actually,
6747 +        * so the tiny check for si_rwsem is unnecessary.
6748 +        */
6749 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6750 +
6751 +       err = -EIO;
6752 +       sbinfo->si_dbgaufs_xigen = debugfs_create_file
6753 +               ("xigen", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6754 +                &dbgaufs_xigen_fop);
6755 +       if (sbinfo->si_dbgaufs_xigen)
6756 +               err = 0;
6757 +
6758 +       return err;
6759 +}
6760 +#else
6761 +static int dbgaufs_xigen_init(struct au_sbinfo *sbinfo)
6762 +{
6763 +       return 0;
6764 +}
6765 +#endif /* CONFIG_AUFS_EXPORT */
6766 +
6767 +/* ---------------------------------------------------------------------- */
6768 +
6769 +void dbgaufs_si_fin(struct au_sbinfo *sbinfo)
6770 +{
6771 +       /*
6772 +        * This function is a dynamic '__fin' function actually,
6773 +        * so the tiny check for si_rwsem is unnecessary.
6774 +        */
6775 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6776 +
6777 +       debugfs_remove_recursive(sbinfo->si_dbgaufs);
6778 +       sbinfo->si_dbgaufs = NULL;
6779 +}
6780 +
6781 +int dbgaufs_si_init(struct au_sbinfo *sbinfo)
6782 +{
6783 +       int err;
6784 +       char name[SysaufsSiNameLen];
6785 +
6786 +       /*
6787 +        * This function is a dynamic '__init' function actually,
6788 +        * so the tiny check for si_rwsem is unnecessary.
6789 +        */
6790 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
6791 +
6792 +       err = -ENOENT;
6793 +       if (!dbgaufs) {
6794 +               AuErr1("/debug/aufs is uninitialized\n");
6795 +               goto out;
6796 +       }
6797 +
6798 +       err = -EIO;
6799 +       sysaufs_name(sbinfo, name);
6800 +       sbinfo->si_dbgaufs = debugfs_create_dir(name, dbgaufs);
6801 +       if (unlikely(!sbinfo->si_dbgaufs))
6802 +               goto out;
6803 +
6804 +       /* regardless plink/noplink option */
6805 +       sbinfo->si_dbgaufs_plink = debugfs_create_file
6806 +               ("plink", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6807 +                &dbgaufs_plink_fop);
6808 +       if (unlikely(!sbinfo->si_dbgaufs_plink))
6809 +               goto out_dir;
6810 +
6811 +       /* regardless xino/noxino option */
6812 +       sbinfo->si_dbgaufs_xib = debugfs_create_file
6813 +               ("xib", dbgaufs_mode, sbinfo->si_dbgaufs, sbinfo,
6814 +                &dbgaufs_xib_fop);
6815 +       if (unlikely(!sbinfo->si_dbgaufs_xib))
6816 +               goto out_dir;
6817 +
6818 +       err = dbgaufs_xigen_init(sbinfo);
6819 +       if (!err)
6820 +               goto out; /* success */
6821 +
6822 +out_dir:
6823 +       dbgaufs_si_fin(sbinfo);
6824 +out:
6825 +       if (unlikely(err))
6826 +               pr_err("debugfs/aufs failed\n");
6827 +       return err;
6828 +}
6829 +
6830 +/* ---------------------------------------------------------------------- */
6831 +
6832 +void dbgaufs_fin(void)
6833 +{
6834 +       debugfs_remove(dbgaufs);
6835 +}
6836 +
6837 +int __init dbgaufs_init(void)
6838 +{
6839 +       int err;
6840 +
6841 +       err = -EIO;
6842 +       dbgaufs = debugfs_create_dir(AUFS_NAME, NULL);
6843 +       if (dbgaufs)
6844 +               err = 0;
6845 +       return err;
6846 +}
6847 diff -urN /usr/share/empty/fs/aufs/dbgaufs.h linux/fs/aufs/dbgaufs.h
6848 --- /usr/share/empty/fs/aufs/dbgaufs.h  1970-01-01 01:00:00.000000000 +0100
6849 +++ linux/fs/aufs/dbgaufs.h     2022-03-21 14:49:05.719966344 +0100
6850 @@ -0,0 +1,53 @@
6851 +/* SPDX-License-Identifier: GPL-2.0 */
6852 +/*
6853 + * Copyright (C) 2005-2021 Junjiro R. Okajima
6854 + *
6855 + * This program, aufs is free software; you can redistribute it and/or modify
6856 + * it under the terms of the GNU General Public License as published by
6857 + * the Free Software Foundation; either version 2 of the License, or
6858 + * (at your option) any later version.
6859 + *
6860 + * This program is distributed in the hope that it will be useful,
6861 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6862 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6863 + * GNU General Public License for more details.
6864 + *
6865 + * You should have received a copy of the GNU General Public License
6866 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6867 + */
6868 +
6869 +/*
6870 + * debugfs interface
6871 + */
6872 +
6873 +#ifndef __DBGAUFS_H__
6874 +#define __DBGAUFS_H__
6875 +
6876 +#ifdef __KERNEL__
6877 +
6878 +struct super_block;
6879 +struct au_sbinfo;
6880 +struct au_branch;
6881 +
6882 +#ifdef CONFIG_DEBUG_FS
6883 +/* dbgaufs.c */
6884 +void dbgaufs_xino_del(struct au_branch *br);
6885 +void dbgaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
6886 +void dbgaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex, int topdown);
6887 +void dbgaufs_si_fin(struct au_sbinfo *sbinfo);
6888 +int dbgaufs_si_init(struct au_sbinfo *sbinfo);
6889 +void dbgaufs_fin(void);
6890 +int __init dbgaufs_init(void);
6891 +#else
6892 +AuStubVoid(dbgaufs_xino_del, struct au_branch *br)
6893 +AuStubVoid(dbgaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
6894 +AuStubVoid(dbgaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex,
6895 +          int topdown)
6896 +AuStubVoid(dbgaufs_si_fin, struct au_sbinfo *sbinfo)
6897 +AuStubInt0(dbgaufs_si_init, struct au_sbinfo *sbinfo)
6898 +AuStubVoid(dbgaufs_fin, void)
6899 +AuStubInt0(__init dbgaufs_init, void)
6900 +#endif /* CONFIG_DEBUG_FS */
6901 +
6902 +#endif /* __KERNEL__ */
6903 +#endif /* __DBGAUFS_H__ */
6904 diff -urN /usr/share/empty/fs/aufs/dcsub.c linux/fs/aufs/dcsub.c
6905 --- /usr/share/empty/fs/aufs/dcsub.c    1970-01-01 01:00:00.000000000 +0100
6906 +++ linux/fs/aufs/dcsub.c       2022-03-21 14:49:05.719966344 +0100
6907 @@ -0,0 +1,225 @@
6908 +// SPDX-License-Identifier: GPL-2.0
6909 +/*
6910 + * Copyright (C) 2005-2021 Junjiro R. Okajima
6911 + *
6912 + * This program, aufs is free software; you can redistribute it and/or modify
6913 + * it under the terms of the GNU General Public License as published by
6914 + * the Free Software Foundation; either version 2 of the License, or
6915 + * (at your option) any later version.
6916 + *
6917 + * This program is distributed in the hope that it will be useful,
6918 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
6919 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
6920 + * GNU General Public License for more details.
6921 + *
6922 + * You should have received a copy of the GNU General Public License
6923 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
6924 + */
6925 +
6926 +/*
6927 + * sub-routines for dentry cache
6928 + */
6929 +
6930 +#include "aufs.h"
6931 +
6932 +static void au_dpage_free(struct au_dpage *dpage)
6933 +{
6934 +       int i;
6935 +       struct dentry **p;
6936 +
6937 +       p = dpage->dentries;
6938 +       for (i = 0; i < dpage->ndentry; i++)
6939 +               dput(*p++);
6940 +       free_page((unsigned long)dpage->dentries);
6941 +}
6942 +
6943 +int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp)
6944 +{
6945 +       int err;
6946 +       void *p;
6947 +
6948 +       err = -ENOMEM;
6949 +       dpages->dpages = kmalloc(sizeof(*dpages->dpages), gfp);
6950 +       if (unlikely(!dpages->dpages))
6951 +               goto out;
6952 +
6953 +       p = (void *)__get_free_page(gfp);
6954 +       if (unlikely(!p))
6955 +               goto out_dpages;
6956 +
6957 +       dpages->dpages[0].ndentry = 0;
6958 +       dpages->dpages[0].dentries = p;
6959 +       dpages->ndpage = 1;
6960 +       return 0; /* success */
6961 +
6962 +out_dpages:
6963 +       au_kfree_try_rcu(dpages->dpages);
6964 +out:
6965 +       return err;
6966 +}
6967 +
6968 +void au_dpages_free(struct au_dcsub_pages *dpages)
6969 +{
6970 +       int i;
6971 +       struct au_dpage *p;
6972 +
6973 +       p = dpages->dpages;
6974 +       for (i = 0; i < dpages->ndpage; i++)
6975 +               au_dpage_free(p++);
6976 +       au_kfree_try_rcu(dpages->dpages);
6977 +}
6978 +
6979 +static int au_dpages_append(struct au_dcsub_pages *dpages,
6980 +                           struct dentry *dentry, gfp_t gfp)
6981 +{
6982 +       int err, sz;
6983 +       struct au_dpage *dpage;
6984 +       void *p;
6985 +
6986 +       dpage = dpages->dpages + dpages->ndpage - 1;
6987 +       sz = PAGE_SIZE / sizeof(dentry);
6988 +       if (unlikely(dpage->ndentry >= sz)) {
6989 +               AuLabel(new dpage);
6990 +               err = -ENOMEM;
6991 +               sz = dpages->ndpage * sizeof(*dpages->dpages);
6992 +               p = au_kzrealloc(dpages->dpages, sz,
6993 +                                sz + sizeof(*dpages->dpages), gfp,
6994 +                                /*may_shrink*/0);
6995 +               if (unlikely(!p))
6996 +                       goto out;
6997 +
6998 +               dpages->dpages = p;
6999 +               dpage = dpages->dpages + dpages->ndpage;
7000 +               p = (void *)__get_free_page(gfp);
7001 +               if (unlikely(!p))
7002 +                       goto out;
7003 +
7004 +               dpage->ndentry = 0;
7005 +               dpage->dentries = p;
7006 +               dpages->ndpage++;
7007 +       }
7008 +
7009 +       AuDebugOn(au_dcount(dentry) <= 0);
7010 +       dpage->dentries[dpage->ndentry++] = dget_dlock(dentry);
7011 +       return 0; /* success */
7012 +
7013 +out:
7014 +       return err;
7015 +}
7016 +
7017 +/* todo: BAD approach */
7018 +/* copied from linux/fs/dcache.c */
7019 +enum d_walk_ret {
7020 +       D_WALK_CONTINUE,
7021 +       D_WALK_QUIT,
7022 +       D_WALK_NORETRY,
7023 +       D_WALK_SKIP,
7024 +};
7025 +
7026 +extern void d_walk(struct dentry *parent, void *data,
7027 +                  enum d_walk_ret (*enter)(void *, struct dentry *));
7028 +
7029 +struct ac_dpages_arg {
7030 +       int err;
7031 +       struct au_dcsub_pages *dpages;
7032 +       struct super_block *sb;
7033 +       au_dpages_test test;
7034 +       void *arg;
7035 +};
7036 +
7037 +static enum d_walk_ret au_call_dpages_append(void *_arg, struct dentry *dentry)
7038 +{
7039 +       enum d_walk_ret ret;
7040 +       struct ac_dpages_arg *arg = _arg;
7041 +
7042 +       ret = D_WALK_CONTINUE;
7043 +       if (dentry->d_sb == arg->sb
7044 +           && !IS_ROOT(dentry)
7045 +           && au_dcount(dentry) > 0
7046 +           && au_di(dentry)
7047 +           && (!arg->test || arg->test(dentry, arg->arg))) {
7048 +               arg->err = au_dpages_append(arg->dpages, dentry, GFP_ATOMIC);
7049 +               if (unlikely(arg->err))
7050 +                       ret = D_WALK_QUIT;
7051 +       }
7052 +
7053 +       return ret;
7054 +}
7055 +
7056 +int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
7057 +                  au_dpages_test test, void *arg)
7058 +{
7059 +       struct ac_dpages_arg args = {
7060 +               .err    = 0,
7061 +               .dpages = dpages,
7062 +               .sb     = root->d_sb,
7063 +               .test   = test,
7064 +               .arg    = arg
7065 +       };
7066 +
7067 +       d_walk(root, &args, au_call_dpages_append);
7068 +
7069 +       return args.err;
7070 +}
7071 +
7072 +int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
7073 +                      int do_include, au_dpages_test test, void *arg)
7074 +{
7075 +       int err;
7076 +
7077 +       err = 0;
7078 +       write_seqlock(&rename_lock);
7079 +       spin_lock(&dentry->d_lock);
7080 +       if (do_include
7081 +           && 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 +               goto out;
7087 +
7088 +       /*
7089 +        * RCU for vfsmount is unnecessary since this is a traverse in a single
7090 +        * mount
7091 +        */
7092 +       while (!IS_ROOT(dentry)) {
7093 +               dentry = dentry->d_parent; /* rename_lock is locked */
7094 +               spin_lock(&dentry->d_lock);
7095 +               if (au_dcount(dentry) > 0
7096 +                   && (!test || test(dentry, arg)))
7097 +                       err = au_dpages_append(dpages, dentry, GFP_ATOMIC);
7098 +               spin_unlock(&dentry->d_lock);
7099 +               if (unlikely(err))
7100 +                       break;
7101 +       }
7102 +
7103 +out:
7104 +       write_sequnlock(&rename_lock);
7105 +       return err;
7106 +}
7107 +
7108 +static inline int au_dcsub_dpages_aufs(struct dentry *dentry, void *arg)
7109 +{
7110 +       return au_di(dentry) && dentry->d_sb == arg;
7111 +}
7112 +
7113 +int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
7114 +                           struct dentry *dentry, int do_include)
7115 +{
7116 +       return au_dcsub_pages_rev(dpages, dentry, do_include,
7117 +                                 au_dcsub_dpages_aufs, dentry->d_sb);
7118 +}
7119 +
7120 +int au_test_subdir(struct dentry *d1, struct dentry *d2)
7121 +{
7122 +       struct path path[2] = {
7123 +               {
7124 +                       .dentry = d1
7125 +               },
7126 +               {
7127 +                       .dentry = d2
7128 +               }
7129 +       };
7130 +
7131 +       return path_is_under(path + 0, path + 1);
7132 +}
7133 diff -urN /usr/share/empty/fs/aufs/dcsub.h linux/fs/aufs/dcsub.h
7134 --- /usr/share/empty/fs/aufs/dcsub.h    1970-01-01 01:00:00.000000000 +0100
7135 +++ linux/fs/aufs/dcsub.h       2022-03-21 14:49:05.719966344 +0100
7136 @@ -0,0 +1,137 @@
7137 +/* SPDX-License-Identifier: GPL-2.0 */
7138 +/*
7139 + * Copyright (C) 2005-2021 Junjiro R. Okajima
7140 + *
7141 + * This program, aufs is free software; you can redistribute it and/or modify
7142 + * it under the terms of the GNU General Public License as published by
7143 + * the Free Software Foundation; either version 2 of the License, or
7144 + * (at your option) any later version.
7145 + *
7146 + * This program is distributed in the hope that it will be useful,
7147 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7148 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7149 + * GNU General Public License for more details.
7150 + *
7151 + * You should have received a copy of the GNU General Public License
7152 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
7153 + */
7154 +
7155 +/*
7156 + * sub-routines for dentry cache
7157 + */
7158 +
7159 +#ifndef __AUFS_DCSUB_H__
7160 +#define __AUFS_DCSUB_H__
7161 +
7162 +#ifdef __KERNEL__
7163 +
7164 +#include <linux/dcache.h>
7165 +#include <linux/fs.h>
7166 +
7167 +struct au_dpage {
7168 +       int ndentry;
7169 +       struct dentry **dentries;
7170 +};
7171 +
7172 +struct au_dcsub_pages {
7173 +       int ndpage;
7174 +       struct au_dpage *dpages;
7175 +};
7176 +
7177 +/* ---------------------------------------------------------------------- */
7178 +
7179 +/* dcsub.c */
7180 +int au_dpages_init(struct au_dcsub_pages *dpages, gfp_t gfp);
7181 +void au_dpages_free(struct au_dcsub_pages *dpages);
7182 +typedef int (*au_dpages_test)(struct dentry *dentry, void *arg);
7183 +int au_dcsub_pages(struct au_dcsub_pages *dpages, struct dentry *root,
7184 +                  au_dpages_test test, void *arg);
7185 +int au_dcsub_pages_rev(struct au_dcsub_pages *dpages, struct dentry *dentry,
7186 +                      int do_include, au_dpages_test test, void *arg);
7187 +int au_dcsub_pages_rev_aufs(struct au_dcsub_pages *dpages,
7188 +                           struct dentry *dentry, int do_include);
7189 +int au_test_subdir(struct dentry *d1, struct dentry *d2);
7190 +
7191 +/* ---------------------------------------------------------------------- */
7192 +
7193 +/*
7194 + * todo: in linux-3.13, several similar (but faster) helpers are added to
7195 + * include/linux/dcache.h. Try them (in the future).
7196 + */
7197 +
7198 +static inline int au_d_hashed_positive(struct dentry *d)
7199 +{
7200 +       int err;
7201 +       struct inode *inode = d_inode(d);
7202 +
7203 +       err = 0;
7204 +       if (unlikely(d_unhashed(d)
7205 +                    || d_is_negative(d)
7206 +                    || !inode->i_nlink))
7207 +               err = -ENOENT;
7208 +       return err;
7209 +}
7210 +
7211 +static inline int au_d_linkable(struct dentry *d)
7212 +{
7213 +       int err;
7214 +       struct inode *inode = d_inode(d);
7215 +
7216 +       err = au_d_hashed_positive(d);
7217 +       if (err
7218 +           && d_is_positive(d)
7219 +           && (inode->i_state & I_LINKABLE))
7220 +               err = 0;
7221 +       return err;
7222 +}
7223 +
7224 +static inline int au_d_alive(struct dentry *d)
7225 +{
7226 +       int err;
7227 +       struct inode *inode;
7228 +
7229 +       err = 0;
7230 +       if (!IS_ROOT(d))
7231 +               err = au_d_hashed_positive(d);
7232 +       else {
7233 +               inode = d_inode(d);
7234 +               if (unlikely(d_unlinked(d)
7235 +                            || d_is_negative(d)
7236 +                            || !inode->i_nlink))
7237 +                       err = -ENOENT;
7238 +       }
7239 +       return err;
7240 +}
7241 +
7242 +static inline int au_alive_dir(struct dentry *d)
7243 +{
7244 +       int err;
7245 +
7246 +       err = au_d_alive(d);
7247 +       if (unlikely(err || IS_DEADDIR(d_inode(d))))
7248 +               err = -ENOENT;
7249 +       return err;
7250 +}
7251 +
7252 +static inline int au_qstreq(struct qstr *a, struct qstr *b)
7253 +{
7254 +       return a->len == b->len
7255 +               && !memcmp(a->name, b->name, a->len);
7256 +}
7257 +
7258 +/*
7259 + * by the commit
7260 + * 360f547 2015-01-25 dcache: let the dentry count go down to zero without
7261 + *                     taking d_lock
7262 + * the type of d_lockref.count became int, but the inlined function d_count()
7263 + * still returns unsigned int.
7264 + * I don't know why. Maybe it is for every d_count() users?
7265 + * Anyway au_dcount() lives on.
7266 + */
7267 +static inline int au_dcount(struct dentry *d)
7268 +{
7269 +       return (int)d_count(d);
7270 +}
7271 +
7272 +#endif /* __KERNEL__ */
7273 +#endif /* __AUFS_DCSUB_H__ */
7274 diff -urN /usr/share/empty/fs/aufs/debug.c linux/fs/aufs/debug.c
7275 --- /usr/share/empty/fs/aufs/debug.c    1970-01-01 01:00:00.000000000 +0100
7276 +++ linux/fs/aufs/debug.c       2022-03-21 14:49:05.719966344 +0100
7277 @@ -0,0 +1,444 @@
7278 +// SPDX-License-Identifier: GPL-2.0
7279 +/*
7280 + * Copyright (C) 2005-2021 Junjiro R. Okajima
7281 + *
7282 + * This program, aufs is free software; you can redistribute it and/or modify
7283 + * it under the terms of the GNU General Public License as published by
7284 + * the Free Software Foundation; either version 2 of the License, or
7285 + * (at your option) any later version.
7286 + *
7287 + * This program is distributed in the hope that it will be useful,
7288 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7289 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7290 + * GNU General Public License for more details.
7291 + *
7292 + * You should have received a copy of the GNU General Public License
7293 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
7294 + */
7295 +
7296 +/*
7297 + * debug print functions
7298 + */
7299 +
7300 +#include <linux/iversion.h>
7301 +#include "aufs.h"
7302 +
7303 +/* Returns 0, or -errno.  arg is in kp->arg. */
7304 +static int param_atomic_t_set(const char *val, const struct kernel_param *kp)
7305 +{
7306 +       int err, n;
7307 +
7308 +       err = kstrtoint(val, 0, &n);
7309 +       if (!err) {
7310 +               if (n > 0)
7311 +                       au_debug_on();
7312 +               else
7313 +                       au_debug_off();
7314 +       }
7315 +       return err;
7316 +}
7317 +
7318 +/* Returns length written or -errno.  Buffer is 4k (ie. be short!) */
7319 +static int param_atomic_t_get(char *buffer, const struct kernel_param *kp)
7320 +{
7321 +       atomic_t *a;
7322 +
7323 +       a = kp->arg;
7324 +       return sprintf(buffer, "%d", atomic_read(a));
7325 +}
7326 +
7327 +static struct kernel_param_ops param_ops_atomic_t = {
7328 +       .set = param_atomic_t_set,
7329 +       .get = param_atomic_t_get
7330 +       /* void (*free)(void *arg) */
7331 +};
7332 +
7333 +atomic_t aufs_debug = ATOMIC_INIT(0);
7334 +MODULE_PARM_DESC(debug, "debug print");
7335 +module_param_named(debug, aufs_debug, atomic_t, 0664);
7336 +
7337 +DEFINE_MUTEX(au_dbg_mtx);      /* just to serialize the dbg msgs */
7338 +char *au_plevel = KERN_DEBUG;
7339 +#define dpri(fmt, ...) do {                                    \
7340 +       if ((au_plevel                                          \
7341 +            && strcmp(au_plevel, KERN_DEBUG))                  \
7342 +           || au_debug_test())                                 \
7343 +               printk("%s" fmt, au_plevel, ##__VA_ARGS__);     \
7344 +} while (0)
7345 +
7346 +/* ---------------------------------------------------------------------- */
7347 +
7348 +void au_dpri_whlist(struct au_nhash *whlist)
7349 +{
7350 +       unsigned long ul, n;
7351 +       struct hlist_head *head;
7352 +       struct au_vdir_wh *pos;
7353 +
7354 +       n = whlist->nh_num;
7355 +       head = whlist->nh_head;
7356 +       for (ul = 0; ul < n; ul++) {
7357 +               hlist_for_each_entry(pos, head, wh_hash)
7358 +                       dpri("b%d, %.*s, %d\n",
7359 +                            pos->wh_bindex,
7360 +                            pos->wh_str.len, pos->wh_str.name,
7361 +                            pos->wh_str.len);
7362 +               head++;
7363 +       }
7364 +}
7365 +
7366 +void au_dpri_vdir(struct au_vdir *vdir)
7367 +{
7368 +       unsigned long ul;
7369 +       union au_vdir_deblk_p p;
7370 +       unsigned char *o;
7371 +
7372 +       if (!vdir || IS_ERR(vdir)) {
7373 +               dpri("err %ld\n", PTR_ERR(vdir));
7374 +               return;
7375 +       }
7376 +
7377 +       dpri("deblk %u, nblk %lu, deblk %p, last{%lu, %p}, ver %llu\n",
7378 +            vdir->vd_deblk_sz, vdir->vd_nblk, vdir->vd_deblk,
7379 +            vdir->vd_last.ul, vdir->vd_last.p.deblk, vdir->vd_version);
7380 +       for (ul = 0; ul < vdir->vd_nblk; ul++) {
7381 +               p.deblk = vdir->vd_deblk[ul];
7382 +               o = p.deblk;
7383 +               dpri("[%lu]: %p\n", ul, o);
7384 +       }
7385 +}
7386 +
7387 +static int do_pri_inode(aufs_bindex_t bindex, struct inode *inode, int hn,
7388 +                       struct dentry *wh)
7389 +{
7390 +       char *n = NULL;
7391 +       int l = 0;
7392 +
7393 +       if (!inode || IS_ERR(inode)) {
7394 +               dpri("i%d: err %ld\n", bindex, PTR_ERR(inode));
7395 +               return -1;
7396 +       }
7397 +
7398 +       /* the type of i_blocks depends upon CONFIG_LBDAF */
7399 +       BUILD_BUG_ON(sizeof(inode->i_blocks) != sizeof(unsigned long)
7400 +                    && sizeof(inode->i_blocks) != sizeof(u64));
7401 +       if (wh) {
7402 +               n = (void *)wh->d_name.name;
7403 +               l = wh->d_name.len;
7404 +       }
7405 +
7406 +       dpri("i%d: %p, i%lu, %s, cnt %d, nl %u, 0%o, sz %llu, blk %llu,"
7407 +            " hn %d, ct %lld, np %lu, st 0x%lx, f 0x%x, v %llu, g %x%s%.*s\n",
7408 +            bindex, inode,
7409 +            inode->i_ino, inode->i_sb ? au_sbtype(inode->i_sb) : "??",
7410 +            atomic_read(&inode->i_count), inode->i_nlink, inode->i_mode,
7411 +            i_size_read(inode), (unsigned long long)inode->i_blocks,
7412 +            hn, (long long)timespec64_to_ns(&inode->i_ctime) & 0x0ffff,
7413 +            inode->i_mapping ? inode->i_mapping->nrpages : 0,
7414 +            inode->i_state, inode->i_flags, inode_peek_iversion(inode),
7415 +            inode->i_generation,
7416 +            l ? ", wh " : "", l, n);
7417 +       return 0;
7418 +}
7419 +
7420 +void au_dpri_inode(struct inode *inode)
7421 +{
7422 +       struct au_iinfo *iinfo;
7423 +       struct au_hinode *hi;
7424 +       aufs_bindex_t bindex;
7425 +       int err, hn;
7426 +
7427 +       err = do_pri_inode(-1, inode, -1, NULL);
7428 +       if (err || !au_test_aufs(inode->i_sb) || au_is_bad_inode(inode))
7429 +               return;
7430 +
7431 +       iinfo = au_ii(inode);
7432 +       dpri("i-1: btop %d, bbot %d, gen %d\n",
7433 +            iinfo->ii_btop, iinfo->ii_bbot, au_iigen(inode, NULL));
7434 +       if (iinfo->ii_btop < 0)
7435 +               return;
7436 +       hn = 0;
7437 +       for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot; bindex++) {
7438 +               hi = au_hinode(iinfo, bindex);
7439 +               hn = !!au_hn(hi);
7440 +               do_pri_inode(bindex, hi->hi_inode, hn, hi->hi_whdentry);
7441 +       }
7442 +}
7443 +
7444 +void au_dpri_dalias(struct inode *inode)
7445 +{
7446 +       struct dentry *d;
7447 +
7448 +       spin_lock(&inode->i_lock);
7449 +       hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias)
7450 +               au_dpri_dentry(d);
7451 +       spin_unlock(&inode->i_lock);
7452 +}
7453 +
7454 +static int do_pri_dentry(aufs_bindex_t bindex, struct dentry *dentry)
7455 +{
7456 +       struct dentry *wh = NULL;
7457 +       int hn;
7458 +       struct inode *inode;
7459 +       struct au_iinfo *iinfo;
7460 +       struct au_hinode *hi;
7461 +
7462 +       if (!dentry || IS_ERR(dentry)) {
7463 +               dpri("d%d: err %ld\n", bindex, PTR_ERR(dentry));
7464 +               return -1;
7465 +       }
7466 +       /* do not call dget_parent() here */
7467 +       /* note: access d_xxx without d_lock */
7468 +       dpri("d%d: %p, %pd2?, %s, cnt %d, flags 0x%x, %shashed\n",
7469 +            bindex, dentry, dentry,
7470 +            dentry->d_sb ? au_sbtype(dentry->d_sb) : "??",
7471 +            au_dcount(dentry), dentry->d_flags,
7472 +            d_unhashed(dentry) ? "un" : "");
7473 +       hn = -1;
7474 +       inode = NULL;
7475 +       if (d_is_positive(dentry))
7476 +               inode = d_inode(dentry);
7477 +       if (inode
7478 +           && au_test_aufs(dentry->d_sb)
7479 +           && bindex >= 0
7480 +           && !au_is_bad_inode(inode)) {
7481 +               iinfo = au_ii(inode);
7482 +               hi = au_hinode(iinfo, bindex);
7483 +               hn = !!au_hn(hi);
7484 +               wh = hi->hi_whdentry;
7485 +       }
7486 +       do_pri_inode(bindex, inode, hn, wh);
7487 +       return 0;
7488 +}
7489 +
7490 +void au_dpri_dentry(struct dentry *dentry)
7491 +{
7492 +       struct au_dinfo *dinfo;
7493 +       aufs_bindex_t bindex;
7494 +       int err;
7495 +
7496 +       err = do_pri_dentry(-1, dentry);
7497 +       if (err || !au_test_aufs(dentry->d_sb))
7498 +               return;
7499 +
7500 +       dinfo = au_di(dentry);
7501 +       if (!dinfo)
7502 +               return;
7503 +       dpri("d-1: btop %d, bbot %d, bwh %d, bdiropq %d, gen %d, tmp %d\n",
7504 +            dinfo->di_btop, dinfo->di_bbot,
7505 +            dinfo->di_bwh, dinfo->di_bdiropq, au_digen(dentry),
7506 +            dinfo->di_tmpfile);
7507 +       if (dinfo->di_btop < 0)
7508 +               return;
7509 +       for (bindex = dinfo->di_btop; bindex <= dinfo->di_bbot; bindex++)
7510 +               do_pri_dentry(bindex, au_hdentry(dinfo, bindex)->hd_dentry);
7511 +}
7512 +
7513 +static int do_pri_file(aufs_bindex_t bindex, struct file *file)
7514 +{
7515 +       char a[32];
7516 +
7517 +       if (!file || IS_ERR(file)) {
7518 +               dpri("f%d: err %ld\n", bindex, PTR_ERR(file));
7519 +               return -1;
7520 +       }
7521 +       a[0] = 0;
7522 +       if (bindex < 0
7523 +           && !IS_ERR_OR_NULL(file->f_path.dentry)
7524 +           && au_test_aufs(file->f_path.dentry->d_sb)
7525 +           && au_fi(file))
7526 +               snprintf(a, sizeof(a), ", gen %d, mmapped %d",
7527 +                        au_figen(file), atomic_read(&au_fi(file)->fi_mmapped));
7528 +       dpri("f%d: mode 0x%x, flags 0%o, cnt %ld, v %llu, pos %llu%s\n",
7529 +            bindex, file->f_mode, file->f_flags, (long)file_count(file),
7530 +            file->f_version, file->f_pos, a);
7531 +       if (!IS_ERR_OR_NULL(file->f_path.dentry))
7532 +               do_pri_dentry(bindex, file->f_path.dentry);
7533 +       return 0;
7534 +}
7535 +
7536 +void au_dpri_file(struct file *file)
7537 +{
7538 +       struct au_finfo *finfo;
7539 +       struct au_fidir *fidir;
7540 +       struct au_hfile *hfile;
7541 +       aufs_bindex_t bindex;
7542 +       int err;
7543 +
7544 +       err = do_pri_file(-1, file);
7545 +       if (err
7546 +           || IS_ERR_OR_NULL(file->f_path.dentry)
7547 +           || !au_test_aufs(file->f_path.dentry->d_sb))
7548 +               return;
7549 +
7550 +       finfo = au_fi(file);
7551 +       if (!finfo)
7552 +               return;
7553 +       if (finfo->fi_btop < 0)
7554 +               return;
7555 +       fidir = finfo->fi_hdir;
7556 +       if (!fidir)
7557 +               do_pri_file(finfo->fi_btop, finfo->fi_htop.hf_file);
7558 +       else
7559 +               for (bindex = finfo->fi_btop;
7560 +                    bindex >= 0 && bindex <= fidir->fd_bbot;
7561 +                    bindex++) {
7562 +                       hfile = fidir->fd_hfile + bindex;
7563 +                       do_pri_file(bindex, hfile ? hfile->hf_file : NULL);
7564 +               }
7565 +}
7566 +
7567 +static int do_pri_br(aufs_bindex_t bindex, struct au_branch *br)
7568 +{
7569 +       struct vfsmount *mnt;
7570 +       struct super_block *sb;
7571 +
7572 +       if (!br || IS_ERR(br))
7573 +               goto out;
7574 +       mnt = au_br_mnt(br);
7575 +       if (!mnt || IS_ERR(mnt))
7576 +               goto out;
7577 +       sb = mnt->mnt_sb;
7578 +       if (!sb || IS_ERR(sb))
7579 +               goto out;
7580 +
7581 +       dpri("s%d: {perm 0x%x, id %d, wbr %p}, "
7582 +            "%s, dev 0x%02x%02x, flags 0x%lx, cnt %d, active %d, "
7583 +            "xino %d\n",
7584 +            bindex, br->br_perm, br->br_id, br->br_wbr,
7585 +            au_sbtype(sb), MAJOR(sb->s_dev), MINOR(sb->s_dev),
7586 +            sb->s_flags, sb->s_count,
7587 +            atomic_read(&sb->s_active),
7588 +            !!au_xino_file(br->br_xino, /*idx*/-1));
7589 +       return 0;
7590 +
7591 +out:
7592 +       dpri("s%d: err %ld\n", bindex, PTR_ERR(br));
7593 +       return -1;
7594 +}
7595 +
7596 +void au_dpri_sb(struct super_block *sb)
7597 +{
7598 +       struct au_sbinfo *sbinfo;
7599 +       aufs_bindex_t bindex;
7600 +       int err;
7601 +       /* to reduce stack size */
7602 +       struct {
7603 +               struct vfsmount mnt;
7604 +               struct au_branch fake;
7605 +       } *a;
7606 +
7607 +       /* this function can be called from magic sysrq */
7608 +       a = kzalloc(sizeof(*a), GFP_ATOMIC);
7609 +       if (unlikely(!a)) {
7610 +               dpri("no memory\n");
7611 +               return;
7612 +       }
7613 +
7614 +       a->mnt.mnt_sb = sb;
7615 +       a->fake.br_path.mnt = &a->mnt;
7616 +       err = do_pri_br(-1, &a->fake);
7617 +       au_kfree_rcu(a);
7618 +       dpri("dev 0x%x\n", sb->s_dev);
7619 +       if (err || !au_test_aufs(sb))
7620 +               return;
7621 +
7622 +       sbinfo = au_sbi(sb);
7623 +       if (!sbinfo)
7624 +               return;
7625 +       dpri("nw %d, gen %u, kobj %d\n",
7626 +            atomic_read(&sbinfo->si_nowait.nw_len), sbinfo->si_generation,
7627 +            kref_read(&sbinfo->si_kobj.kref));
7628 +       for (bindex = 0; bindex <= sbinfo->si_bbot; bindex++)
7629 +               do_pri_br(bindex, sbinfo->si_branch[0 + bindex]);
7630 +}
7631 +
7632 +/* ---------------------------------------------------------------------- */
7633 +
7634 +void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line)
7635 +{
7636 +       struct inode *h_inode, *inode = d_inode(dentry);
7637 +       struct dentry *h_dentry;
7638 +       aufs_bindex_t bindex, bbot, bi;
7639 +
7640 +       if (!inode /* || au_di(dentry)->di_lsc == AuLsc_DI_TMP */)
7641 +               return;
7642 +
7643 +       bbot = au_dbbot(dentry);
7644 +       bi = au_ibbot(inode);
7645 +       if (bi < bbot)
7646 +               bbot = bi;
7647 +       bindex = au_dbtop(dentry);
7648 +       bi = au_ibtop(inode);
7649 +       if (bi > bindex)
7650 +               bindex = bi;
7651 +
7652 +       for (; bindex <= bbot; bindex++) {
7653 +               h_dentry = au_h_dptr(dentry, bindex);
7654 +               if (!h_dentry)
7655 +                       continue;
7656 +               h_inode = au_h_iptr(inode, bindex);
7657 +               if (unlikely(h_inode != d_inode(h_dentry))) {
7658 +                       au_debug_on();
7659 +                       AuDbg("b%d, %s:%d\n", bindex, func, line);
7660 +                       AuDbgDentry(dentry);
7661 +                       AuDbgInode(inode);
7662 +                       au_debug_off();
7663 +                       if (au_test_fuse(h_inode->i_sb))
7664 +                               WARN_ON_ONCE(1);
7665 +                       else
7666 +                               BUG();
7667 +               }
7668 +       }
7669 +}
7670 +
7671 +void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen)
7672 +{
7673 +       int err, i, j;
7674 +       struct au_dcsub_pages dpages;
7675 +       struct au_dpage *dpage;
7676 +       struct dentry **dentries;
7677 +
7678 +       err = au_dpages_init(&dpages, GFP_NOFS);
7679 +       AuDebugOn(err);
7680 +       err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/1);
7681 +       AuDebugOn(err);
7682 +       for (i = dpages.ndpage - 1; !err && i >= 0; i--) {
7683 +               dpage = dpages.dpages + i;
7684 +               dentries = dpage->dentries;
7685 +               for (j = dpage->ndentry - 1; !err && j >= 0; j--)
7686 +                       AuDebugOn(au_digen_test(dentries[j], sigen));
7687 +       }
7688 +       au_dpages_free(&dpages);
7689 +}
7690 +
7691 +void au_dbg_verify_kthread(void)
7692 +{
7693 +       if (au_wkq_test()) {
7694 +               au_dbg_blocked();
7695 +               /*
7696 +                * It may be recursive, but udba=notify between two aufs mounts,
7697 +                * where a single ro branch is shared, is not a problem.
7698 +                */
7699 +               /* WARN_ON(1); */
7700 +       }
7701 +}
7702 +
7703 +/* ---------------------------------------------------------------------- */
7704 +
7705 +int __init au_debug_init(void)
7706 +{
7707 +       aufs_bindex_t bindex;
7708 +       struct au_vdir_destr destr;
7709 +
7710 +       bindex = -1;
7711 +       AuDebugOn(bindex >= 0);
7712 +
7713 +       destr.len = -1;
7714 +       AuDebugOn(destr.len < NAME_MAX);
7715 +
7716 +#ifdef CONFIG_4KSTACKS
7717 +       pr_warn("CONFIG_4KSTACKS is defined.\n");
7718 +#endif
7719 +
7720 +       return 0;
7721 +}
7722 diff -urN /usr/share/empty/fs/aufs/debug.h linux/fs/aufs/debug.h
7723 --- /usr/share/empty/fs/aufs/debug.h    1970-01-01 01:00:00.000000000 +0100
7724 +++ linux/fs/aufs/debug.h       2022-03-21 14:49:05.719966344 +0100
7725 @@ -0,0 +1,226 @@
7726 +/* SPDX-License-Identifier: GPL-2.0 */
7727 +/*
7728 + * Copyright (C) 2005-2021 Junjiro R. Okajima
7729 + *
7730 + * This program, aufs is free software; you can redistribute it and/or modify
7731 + * it under the terms of the GNU General Public License as published by
7732 + * the Free Software Foundation; either version 2 of the License, or
7733 + * (at your option) any later version.
7734 + *
7735 + * This program is distributed in the hope that it will be useful,
7736 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7737 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7738 + * GNU General Public License for more details.
7739 + *
7740 + * You should have received a copy of the GNU General Public License
7741 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
7742 + */
7743 +
7744 +/*
7745 + * debug print functions
7746 + */
7747 +
7748 +#ifndef __AUFS_DEBUG_H__
7749 +#define __AUFS_DEBUG_H__
7750 +
7751 +#ifdef __KERNEL__
7752 +
7753 +#include <linux/atomic.h>
7754 +#include <linux/module.h>
7755 +#include <linux/kallsyms.h>
7756 +#include <linux/sysrq.h>
7757 +
7758 +#ifdef CONFIG_AUFS_DEBUG
7759 +#define AuDebugOn(a)           BUG_ON(a)
7760 +
7761 +/* module parameter */
7762 +extern atomic_t aufs_debug;
7763 +static inline void au_debug_on(void)
7764 +{
7765 +       atomic_inc(&aufs_debug);
7766 +}
7767 +static inline void au_debug_off(void)
7768 +{
7769 +       atomic_dec_if_positive(&aufs_debug);
7770 +}
7771 +
7772 +static inline int au_debug_test(void)
7773 +{
7774 +       return atomic_read(&aufs_debug) > 0;
7775 +}
7776 +#else
7777 +#define AuDebugOn(a)           do {} while (0)
7778 +AuStubVoid(au_debug_on, void)
7779 +AuStubVoid(au_debug_off, void)
7780 +AuStubInt0(au_debug_test, void)
7781 +#endif /* CONFIG_AUFS_DEBUG */
7782 +
7783 +#define param_check_atomic_t(name, p) __param_check(name, p, atomic_t)
7784 +
7785 +/* ---------------------------------------------------------------------- */
7786 +
7787 +/* debug print */
7788 +
7789 +#define AuDbg(fmt, ...) do { \
7790 +       if (au_debug_test()) \
7791 +               pr_debug("DEBUG: " fmt, ##__VA_ARGS__); \
7792 +} while (0)
7793 +#define AuLabel(l)             AuDbg(#l "\n")
7794 +#define AuIOErr(fmt, ...)      pr_err("I/O Error, " fmt, ##__VA_ARGS__)
7795 +#define AuWarn1(fmt, ...) do { \
7796 +       static unsigned char _c; \
7797 +       if (!_c++) \
7798 +               pr_warn(fmt, ##__VA_ARGS__); \
7799 +} while (0)
7800 +
7801 +#define AuErr1(fmt, ...) do { \
7802 +       static unsigned char _c; \
7803 +       if (!_c++) \
7804 +               pr_err(fmt, ##__VA_ARGS__); \
7805 +} while (0)
7806 +
7807 +#define AuIOErr1(fmt, ...) do { \
7808 +       static unsigned char _c; \
7809 +       if (!_c++) \
7810 +               AuIOErr(fmt, ##__VA_ARGS__); \
7811 +} while (0)
7812 +
7813 +#define AuUnsupportMsg "This operation is not supported." \
7814 +                       " Please report this application to aufs-users ML."
7815 +#define AuUnsupport(fmt, ...) do { \
7816 +       pr_err(AuUnsupportMsg "\n" fmt, ##__VA_ARGS__); \
7817 +       dump_stack(); \
7818 +} while (0)
7819 +
7820 +#define AuTraceErr(e) do { \
7821 +       if (unlikely((e) < 0)) \
7822 +               AuDbg("err %d\n", (int)(e)); \
7823 +} while (0)
7824 +
7825 +#define AuTraceErrPtr(p) do { \
7826 +       if (IS_ERR(p)) \
7827 +               AuDbg("err %ld\n", PTR_ERR(p)); \
7828 +} while (0)
7829 +
7830 +/* dirty macros for debug print, use with "%.*s" and caution */
7831 +#define AuLNPair(qstr)         (qstr)->len, (qstr)->name
7832 +
7833 +/* ---------------------------------------------------------------------- */
7834 +
7835 +struct dentry;
7836 +#ifdef CONFIG_AUFS_DEBUG
7837 +extern struct mutex au_dbg_mtx;
7838 +extern char *au_plevel;
7839 +struct au_nhash;
7840 +void au_dpri_whlist(struct au_nhash *whlist);
7841 +struct au_vdir;
7842 +void au_dpri_vdir(struct au_vdir *vdir);
7843 +struct inode;
7844 +void au_dpri_inode(struct inode *inode);
7845 +void au_dpri_dalias(struct inode *inode);
7846 +void au_dpri_dentry(struct dentry *dentry);
7847 +struct file;
7848 +void au_dpri_file(struct file *filp);
7849 +struct super_block;
7850 +void au_dpri_sb(struct super_block *sb);
7851 +
7852 +#define au_dbg_verify_dinode(d) __au_dbg_verify_dinode(d, __func__, __LINE__)
7853 +void __au_dbg_verify_dinode(struct dentry *dentry, const char *func, int line);
7854 +void au_dbg_verify_gen(struct dentry *parent, unsigned int sigen);
7855 +void au_dbg_verify_kthread(void);
7856 +
7857 +int __init au_debug_init(void);
7858 +
7859 +#define AuDbgWhlist(w) do { \
7860 +       mutex_lock(&au_dbg_mtx); \
7861 +       AuDbg(#w "\n"); \
7862 +       au_dpri_whlist(w); \
7863 +       mutex_unlock(&au_dbg_mtx); \
7864 +} while (0)
7865 +
7866 +#define AuDbgVdir(v) do { \
7867 +       mutex_lock(&au_dbg_mtx); \
7868 +       AuDbg(#v "\n"); \
7869 +       au_dpri_vdir(v); \
7870 +       mutex_unlock(&au_dbg_mtx); \
7871 +} while (0)
7872 +
7873 +#define AuDbgInode(i) do { \
7874 +       mutex_lock(&au_dbg_mtx); \
7875 +       AuDbg(#i "\n"); \
7876 +       au_dpri_inode(i); \
7877 +       mutex_unlock(&au_dbg_mtx); \
7878 +} while (0)
7879 +
7880 +#define AuDbgDAlias(i) do { \
7881 +       mutex_lock(&au_dbg_mtx); \
7882 +       AuDbg(#i "\n"); \
7883 +       au_dpri_dalias(i); \
7884 +       mutex_unlock(&au_dbg_mtx); \
7885 +} while (0)
7886 +
7887 +#define AuDbgDentry(d) do { \
7888 +       mutex_lock(&au_dbg_mtx); \
7889 +       AuDbg(#d "\n"); \
7890 +       au_dpri_dentry(d); \
7891 +       mutex_unlock(&au_dbg_mtx); \
7892 +} while (0)
7893 +
7894 +#define AuDbgFile(f) do { \
7895 +       mutex_lock(&au_dbg_mtx); \
7896 +       AuDbg(#f "\n"); \
7897 +       au_dpri_file(f); \
7898 +       mutex_unlock(&au_dbg_mtx); \
7899 +} while (0)
7900 +
7901 +#define AuDbgSb(sb) do { \
7902 +       mutex_lock(&au_dbg_mtx); \
7903 +       AuDbg(#sb "\n"); \
7904 +       au_dpri_sb(sb); \
7905 +       mutex_unlock(&au_dbg_mtx); \
7906 +} while (0)
7907 +
7908 +#define AuDbgSym(addr) do {                            \
7909 +       char sym[KSYM_SYMBOL_LEN];                      \
7910 +       sprint_symbol(sym, (unsigned long)addr);        \
7911 +       AuDbg("%s\n", sym);                             \
7912 +} while (0)
7913 +#else
7914 +AuStubVoid(au_dbg_verify_dinode, struct dentry *dentry)
7915 +AuStubVoid(au_dbg_verify_gen, struct dentry *parent, unsigned int sigen)
7916 +AuStubVoid(au_dbg_verify_kthread, void)
7917 +AuStubInt0(__init au_debug_init, void)
7918 +
7919 +#define AuDbgWhlist(w)         do {} while (0)
7920 +#define AuDbgVdir(v)           do {} while (0)
7921 +#define AuDbgInode(i)          do {} while (0)
7922 +#define AuDbgDAlias(i)         do {} while (0)
7923 +#define AuDbgDentry(d)         do {} while (0)
7924 +#define AuDbgFile(f)           do {} while (0)
7925 +#define AuDbgSb(sb)            do {} while (0)
7926 +#define AuDbgSym(addr)         do {} while (0)
7927 +#endif /* CONFIG_AUFS_DEBUG */
7928 +
7929 +/* ---------------------------------------------------------------------- */
7930 +
7931 +#ifdef CONFIG_AUFS_MAGIC_SYSRQ
7932 +int __init au_sysrq_init(void);
7933 +void au_sysrq_fin(void);
7934 +
7935 +#ifdef CONFIG_HW_CONSOLE
7936 +#define au_dbg_blocked() do { \
7937 +       WARN_ON(1); \
7938 +       handle_sysrq('w'); \
7939 +} while (0)
7940 +#else
7941 +AuStubVoid(au_dbg_blocked, void)
7942 +#endif
7943 +
7944 +#else
7945 +AuStubInt0(__init au_sysrq_init, void)
7946 +AuStubVoid(au_sysrq_fin, void)
7947 +AuStubVoid(au_dbg_blocked, void)
7948 +#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
7949 +
7950 +#endif /* __KERNEL__ */
7951 +#endif /* __AUFS_DEBUG_H__ */
7952 diff -urN /usr/share/empty/fs/aufs/dentry.c linux/fs/aufs/dentry.c
7953 --- /usr/share/empty/fs/aufs/dentry.c   1970-01-01 01:00:00.000000000 +0100
7954 +++ linux/fs/aufs/dentry.c      2022-03-21 14:49:05.719966344 +0100
7955 @@ -0,0 +1,1168 @@
7956 +// SPDX-License-Identifier: GPL-2.0
7957 +/*
7958 + * Copyright (C) 2005-2021 Junjiro R. Okajima
7959 + *
7960 + * This program, aufs is free software; you can redistribute it and/or modify
7961 + * it under the terms of the GNU General Public License as published by
7962 + * the Free Software Foundation; either version 2 of the License, or
7963 + * (at your option) any later version.
7964 + *
7965 + * This program is distributed in the hope that it will be useful,
7966 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
7967 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
7968 + * GNU General Public License for more details.
7969 + *
7970 + * You should have received a copy of the GNU General Public License
7971 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
7972 + */
7973 +
7974 +/*
7975 + * lookup and dentry operations
7976 + */
7977 +
7978 +#include <linux/iversion.h>
7979 +#include "aufs.h"
7980 +
7981 +/*
7982 + * returns positive/negative dentry, NULL or an error.
7983 + * NULL means whiteout-ed or not-found.
7984 + */
7985 +static struct dentry*
7986 +au_do_lookup(struct dentry *h_parent, struct dentry *dentry,
7987 +            aufs_bindex_t bindex, struct au_do_lookup_args *args)
7988 +{
7989 +       struct dentry *h_dentry;
7990 +       struct inode *h_inode;
7991 +       struct au_branch *br;
7992 +       struct user_namespace *h_userns;
7993 +       struct path h_path;
7994 +       int wh_found, opq;
7995 +       unsigned char wh_able;
7996 +       const unsigned char allow_neg = !!au_ftest_lkup(args->flags, ALLOW_NEG);
7997 +       const unsigned char ignore_perm = !!au_ftest_lkup(args->flags,
7998 +                                                         IGNORE_PERM);
7999 +
8000 +       wh_found = 0;
8001 +       br = au_sbr(dentry->d_sb, bindex);
8002 +       h_path.dentry = h_parent;
8003 +       h_path.mnt = au_br_mnt(br);
8004 +       h_userns = au_br_userns(br);
8005 +       wh_able = !!au_br_whable(br->br_perm);
8006 +       if (wh_able)
8007 +               wh_found = au_wh_test(h_userns, &h_path, &args->whname,
8008 +                                     ignore_perm);
8009 +       h_dentry = ERR_PTR(wh_found);
8010 +       if (!wh_found)
8011 +               goto real_lookup;
8012 +       if (unlikely(wh_found < 0))
8013 +               goto out;
8014 +
8015 +       /* We found a whiteout */
8016 +       /* au_set_dbbot(dentry, bindex); */
8017 +       au_set_dbwh(dentry, bindex);
8018 +       if (!allow_neg)
8019 +               return NULL; /* success */
8020 +
8021 +real_lookup:
8022 +       if (!ignore_perm)
8023 +               h_dentry = vfsub_lkup_one(args->name, &h_path);
8024 +       else
8025 +               h_dentry = au_sio_lkup_one(h_userns, args->name, &h_path);
8026 +       if (IS_ERR(h_dentry)) {
8027 +               if (PTR_ERR(h_dentry) == -ENAMETOOLONG
8028 +                   && !allow_neg)
8029 +                       h_dentry = NULL;
8030 +               goto out;
8031 +       }
8032 +
8033 +       h_inode = d_inode(h_dentry);
8034 +       if (d_is_negative(h_dentry)) {
8035 +               if (!allow_neg)
8036 +                       goto out_neg;
8037 +       } else if (wh_found
8038 +                  || (args->type && args->type != (h_inode->i_mode & S_IFMT)))
8039 +               goto out_neg;
8040 +       else if (au_ftest_lkup(args->flags, DIRREN)
8041 +                /* && h_inode */
8042 +                && !au_dr_lkup_h_ino(args, bindex, h_inode->i_ino)) {
8043 +               AuDbg("b%d %pd ignored hi%llu\n", bindex, h_dentry,
8044 +                     (unsigned long long)h_inode->i_ino);
8045 +               goto out_neg;
8046 +       }
8047 +
8048 +       if (au_dbbot(dentry) <= bindex)
8049 +               au_set_dbbot(dentry, bindex);
8050 +       if (au_dbtop(dentry) < 0 || bindex < au_dbtop(dentry))
8051 +               au_set_dbtop(dentry, bindex);
8052 +       au_set_h_dptr(dentry, bindex, h_dentry);
8053 +
8054 +       if (!d_is_dir(h_dentry)
8055 +           || !wh_able
8056 +           || (d_really_is_positive(dentry) && !d_is_dir(dentry)))
8057 +               goto out; /* success */
8058 +
8059 +       h_path.dentry = h_dentry;
8060 +       inode_lock_shared_nested(h_inode, AuLsc_I_CHILD);
8061 +       opq = au_diropq_test(h_userns, &h_path);
8062 +       inode_unlock_shared(h_inode);
8063 +       if (opq > 0)
8064 +               au_set_dbdiropq(dentry, bindex);
8065 +       else if (unlikely(opq < 0)) {
8066 +               au_set_h_dptr(dentry, bindex, NULL);
8067 +               h_dentry = ERR_PTR(opq);
8068 +       }
8069 +       goto out;
8070 +
8071 +out_neg:
8072 +       dput(h_dentry);
8073 +       h_dentry = NULL;
8074 +out:
8075 +       return h_dentry;
8076 +}
8077 +
8078 +static int au_test_shwh(struct super_block *sb, const struct qstr *name)
8079 +{
8080 +       if (unlikely(!au_opt_test(au_mntflags(sb), SHWH)
8081 +                    && !strncmp(name->name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)))
8082 +               return -EPERM;
8083 +       return 0;
8084 +}
8085 +
8086 +/*
8087 + * returns the number of lower positive dentries,
8088 + * otherwise an error.
8089 + * can be called at unlinking with @type is zero.
8090 + */
8091 +int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t btop,
8092 +                  unsigned int flags)
8093 +{
8094 +       int npositive, err;
8095 +       aufs_bindex_t bindex, btail, bdiropq;
8096 +       unsigned char isdir, dirperm1, dirren;
8097 +       struct au_do_lookup_args args = {
8098 +               .flags          = flags,
8099 +               .name           = &dentry->d_name
8100 +       };
8101 +       struct dentry *parent;
8102 +       struct super_block *sb;
8103 +
8104 +       sb = dentry->d_sb;
8105 +       err = au_test_shwh(sb, args.name);
8106 +       if (unlikely(err))
8107 +               goto out;
8108 +
8109 +       err = au_wh_name_alloc(&args.whname, args.name);
8110 +       if (unlikely(err))
8111 +               goto out;
8112 +
8113 +       isdir = !!d_is_dir(dentry);
8114 +       dirperm1 = !!au_opt_test(au_mntflags(sb), DIRPERM1);
8115 +       dirren = !!au_opt_test(au_mntflags(sb), DIRREN);
8116 +       if (dirren)
8117 +               au_fset_lkup(args.flags, DIRREN);
8118 +
8119 +       npositive = 0;
8120 +       parent = dget_parent(dentry);
8121 +       btail = au_dbtaildir(parent);
8122 +       for (bindex = btop; bindex <= btail; bindex++) {
8123 +               struct dentry *h_parent, *h_dentry;
8124 +               struct inode *h_inode, *h_dir;
8125 +               struct au_branch *br;
8126 +
8127 +               h_dentry = au_h_dptr(dentry, bindex);
8128 +               if (h_dentry) {
8129 +                       if (d_is_positive(h_dentry))
8130 +                               npositive++;
8131 +                       break;
8132 +               }
8133 +               h_parent = au_h_dptr(parent, bindex);
8134 +               if (!h_parent || !d_is_dir(h_parent))
8135 +                       continue;
8136 +
8137 +               if (dirren) {
8138 +                       /* if the inum matches, then use the prepared name */
8139 +                       err = au_dr_lkup_name(&args, bindex);
8140 +                       if (unlikely(err))
8141 +                               goto out_parent;
8142 +               }
8143 +
8144 +               h_dir = d_inode(h_parent);
8145 +               inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
8146 +               h_dentry = au_do_lookup(h_parent, dentry, bindex, &args);
8147 +               inode_unlock_shared(h_dir);
8148 +               err = PTR_ERR(h_dentry);
8149 +               if (IS_ERR(h_dentry))
8150 +                       goto out_parent;
8151 +               if (h_dentry)
8152 +                       au_fclr_lkup(args.flags, ALLOW_NEG);
8153 +               if (dirperm1)
8154 +                       au_fset_lkup(args.flags, IGNORE_PERM);
8155 +
8156 +               if (au_dbwh(dentry) == bindex)
8157 +                       break;
8158 +               if (!h_dentry)
8159 +                       continue;
8160 +               if (d_is_negative(h_dentry))
8161 +                       continue;
8162 +               h_inode = d_inode(h_dentry);
8163 +               npositive++;
8164 +               if (!args.type)
8165 +                       args.type = h_inode->i_mode & S_IFMT;
8166 +               if (args.type != S_IFDIR)
8167 +                       break;
8168 +               else if (isdir) {
8169 +                       /* the type of lower may be different */
8170 +                       bdiropq = au_dbdiropq(dentry);
8171 +                       if (bdiropq >= 0 && bdiropq <= bindex)
8172 +                               break;
8173 +               }
8174 +               br = au_sbr(sb, bindex);
8175 +               if (dirren
8176 +                   && au_dr_hino_test_add(&br->br_dirren, h_inode->i_ino,
8177 +                                          /*add_ent*/NULL)) {
8178 +                       /* prepare next name to lookup */
8179 +                       err = au_dr_lkup(&args, dentry, bindex);
8180 +                       if (unlikely(err))
8181 +                               goto out_parent;
8182 +               }
8183 +       }
8184 +
8185 +       if (npositive) {
8186 +               AuLabel(positive);
8187 +               au_update_dbtop(dentry);
8188 +       }
8189 +       err = npositive;
8190 +       if (unlikely(!au_opt_test(au_mntflags(sb), UDBA_NONE)
8191 +                    && au_dbtop(dentry) < 0)) {
8192 +               err = -EIO;
8193 +               AuIOErr("both of real entry and whiteout found, %pd, err %d\n",
8194 +                       dentry, err);
8195 +       }
8196 +
8197 +out_parent:
8198 +       dput(parent);
8199 +       au_kfree_try_rcu(args.whname.name);
8200 +       if (dirren)
8201 +               au_dr_lkup_fin(&args);
8202 +out:
8203 +       return err;
8204 +}
8205 +
8206 +struct dentry *au_sio_lkup_one(struct user_namespace *userns, struct qstr *name,
8207 +                              struct path *ppath)
8208 +{
8209 +       struct dentry *dentry;
8210 +       int wkq_err;
8211 +
8212 +       if (!au_test_h_perm_sio(userns, d_inode(ppath->dentry), MAY_EXEC))
8213 +               dentry = vfsub_lkup_one(name, ppath);
8214 +       else {
8215 +               struct vfsub_lkup_one_args args = {
8216 +                       .errp   = &dentry,
8217 +                       .name   = name,
8218 +                       .ppath  = ppath
8219 +               };
8220 +
8221 +               wkq_err = au_wkq_wait(vfsub_call_lkup_one, &args);
8222 +               if (unlikely(wkq_err))
8223 +                       dentry = ERR_PTR(wkq_err);
8224 +       }
8225 +
8226 +       return dentry;
8227 +}
8228 +
8229 +/*
8230 + * lookup @dentry on @bindex which should be negative.
8231 + */
8232 +int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh)
8233 +{
8234 +       int err;
8235 +       struct dentry *parent, *h_dentry;
8236 +       struct au_branch *br;
8237 +       struct user_namespace *h_userns;
8238 +       struct path h_ppath;
8239 +
8240 +       parent = dget_parent(dentry);
8241 +       br = au_sbr(dentry->d_sb, bindex);
8242 +       h_ppath.dentry = au_h_dptr(parent, bindex);
8243 +       h_ppath.mnt = au_br_mnt(br);
8244 +       h_userns = au_br_userns(br);
8245 +       if (wh)
8246 +               h_dentry = au_whtmp_lkup(h_ppath.dentry, br, &dentry->d_name);
8247 +       else
8248 +               h_dentry = au_sio_lkup_one(h_userns, &dentry->d_name, &h_ppath);
8249 +       err = PTR_ERR(h_dentry);
8250 +       if (IS_ERR(h_dentry))
8251 +               goto out;
8252 +       if (unlikely(d_is_positive(h_dentry))) {
8253 +               err = -EIO;
8254 +               AuIOErr("%pd should be negative on b%d.\n", h_dentry, bindex);
8255 +               dput(h_dentry);
8256 +               goto out;
8257 +       }
8258 +
8259 +       err = 0;
8260 +       if (bindex < au_dbtop(dentry))
8261 +               au_set_dbtop(dentry, bindex);
8262 +       if (au_dbbot(dentry) < bindex)
8263 +               au_set_dbbot(dentry, bindex);
8264 +       au_set_h_dptr(dentry, bindex, h_dentry);
8265 +
8266 +out:
8267 +       dput(parent);
8268 +       return err;
8269 +}
8270 +
8271 +/* ---------------------------------------------------------------------- */
8272 +
8273 +/* subset of struct inode */
8274 +struct au_iattr {
8275 +       unsigned long           i_ino;
8276 +       /* unsigned int         i_nlink; */
8277 +       kuid_t                  i_uid;
8278 +       kgid_t                  i_gid;
8279 +       u64                     i_version;
8280 +/*
8281 +       loff_t                  i_size;
8282 +       blkcnt_t                i_blocks;
8283 +*/
8284 +       umode_t                 i_mode;
8285 +};
8286 +
8287 +static void au_iattr_save(struct au_iattr *ia, struct inode *h_inode)
8288 +{
8289 +       ia->i_ino = h_inode->i_ino;
8290 +       /* ia->i_nlink = h_inode->i_nlink; */
8291 +       ia->i_uid = h_inode->i_uid;
8292 +       ia->i_gid = h_inode->i_gid;
8293 +       ia->i_version = inode_query_iversion(h_inode);
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_iattr_test(struct au_iattr *ia, struct inode *h_inode)
8302 +{
8303 +       return ia->i_ino != h_inode->i_ino
8304 +               /* || ia->i_nlink != h_inode->i_nlink */
8305 +               || !uid_eq(ia->i_uid, h_inode->i_uid)
8306 +               || !gid_eq(ia->i_gid, h_inode->i_gid)
8307 +               || !inode_eq_iversion(h_inode, ia->i_version)
8308 +/*
8309 +               || ia->i_size != h_inode->i_size
8310 +               || ia->i_blocks != h_inode->i_blocks
8311 +*/
8312 +               || ia->i_mode != (h_inode->i_mode & S_IFMT);
8313 +}
8314 +
8315 +static int au_h_verify_dentry(struct dentry *h_dentry, struct dentry *h_parent,
8316 +                             struct au_branch *br)
8317 +{
8318 +       int err;
8319 +       struct au_iattr ia;
8320 +       struct inode *h_inode;
8321 +       struct dentry *h_d;
8322 +       struct super_block *h_sb;
8323 +       struct path h_ppath;
8324 +
8325 +       err = 0;
8326 +       memset(&ia, -1, sizeof(ia));
8327 +       h_sb = h_dentry->d_sb;
8328 +       h_inode = NULL;
8329 +       if (d_is_positive(h_dentry)) {
8330 +               h_inode = d_inode(h_dentry);
8331 +               au_iattr_save(&ia, h_inode);
8332 +       } else if (au_test_nfs(h_sb) || au_test_fuse(h_sb))
8333 +               /* nfs d_revalidate may return 0 for negative dentry */
8334 +               /* fuse d_revalidate always return 0 for negative dentry */
8335 +               goto out;
8336 +
8337 +       /* main purpose is namei.c:cached_lookup() and d_revalidate */
8338 +       h_ppath.dentry = h_parent;
8339 +       h_ppath.mnt = au_br_mnt(br);
8340 +       h_d = vfsub_lkup_one(&h_dentry->d_name, &h_ppath);
8341 +       err = PTR_ERR(h_d);
8342 +       if (IS_ERR(h_d))
8343 +               goto out;
8344 +
8345 +       err = 0;
8346 +       if (unlikely(h_d != h_dentry
8347 +                    || d_inode(h_d) != h_inode
8348 +                    || (h_inode && au_iattr_test(&ia, h_inode))))
8349 +               err = au_busy_or_stale();
8350 +       dput(h_d);
8351 +
8352 +out:
8353 +       AuTraceErr(err);
8354 +       return err;
8355 +}
8356 +
8357 +int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
8358 +               struct dentry *h_parent, struct au_branch *br)
8359 +{
8360 +       int err;
8361 +
8362 +       err = 0;
8363 +       if (udba == AuOpt_UDBA_REVAL
8364 +           && !au_test_fs_remote(h_dentry->d_sb)) {
8365 +               IMustLock(h_dir);
8366 +               err = (d_inode(h_dentry->d_parent) != h_dir);
8367 +       } else if (udba != AuOpt_UDBA_NONE)
8368 +               err = au_h_verify_dentry(h_dentry, h_parent, br);
8369 +
8370 +       return err;
8371 +}
8372 +
8373 +/* ---------------------------------------------------------------------- */
8374 +
8375 +static int au_do_refresh_hdentry(struct dentry *dentry, struct dentry *parent)
8376 +{
8377 +       int err;
8378 +       aufs_bindex_t new_bindex, bindex, bbot, bwh, bdiropq;
8379 +       struct au_hdentry tmp, *p, *q;
8380 +       struct au_dinfo *dinfo;
8381 +       struct super_block *sb;
8382 +
8383 +       DiMustWriteLock(dentry);
8384 +
8385 +       sb = dentry->d_sb;
8386 +       dinfo = au_di(dentry);
8387 +       bbot = dinfo->di_bbot;
8388 +       bwh = dinfo->di_bwh;
8389 +       bdiropq = dinfo->di_bdiropq;
8390 +       bindex = dinfo->di_btop;
8391 +       p = au_hdentry(dinfo, bindex);
8392 +       for (; bindex <= bbot; bindex++, p++) {
8393 +               if (!p->hd_dentry)
8394 +                       continue;
8395 +
8396 +               new_bindex = au_br_index(sb, p->hd_id);
8397 +               if (new_bindex == bindex)
8398 +                       continue;
8399 +
8400 +               if (dinfo->di_bwh == bindex)
8401 +                       bwh = new_bindex;
8402 +               if (dinfo->di_bdiropq == bindex)
8403 +                       bdiropq = new_bindex;
8404 +               if (new_bindex < 0) {
8405 +                       au_hdput(p);
8406 +                       p->hd_dentry = NULL;
8407 +                       continue;
8408 +               }
8409 +
8410 +               /* swap two lower dentries, and loop again */
8411 +               q = au_hdentry(dinfo, new_bindex);
8412 +               tmp = *q;
8413 +               *q = *p;
8414 +               *p = tmp;
8415 +               if (tmp.hd_dentry) {
8416 +                       bindex--;
8417 +                       p--;
8418 +               }
8419 +       }
8420 +
8421 +       dinfo->di_bwh = -1;
8422 +       if (bwh >= 0 && bwh <= au_sbbot(sb) && au_sbr_whable(sb, bwh))
8423 +               dinfo->di_bwh = bwh;
8424 +
8425 +       dinfo->di_bdiropq = -1;
8426 +       if (bdiropq >= 0
8427 +           && bdiropq <= au_sbbot(sb)
8428 +           && au_sbr_whable(sb, bdiropq))
8429 +               dinfo->di_bdiropq = bdiropq;
8430 +
8431 +       err = -EIO;
8432 +       dinfo->di_btop = -1;
8433 +       dinfo->di_bbot = -1;
8434 +       bbot = au_dbbot(parent);
8435 +       bindex = 0;
8436 +       p = au_hdentry(dinfo, bindex);
8437 +       for (; bindex <= bbot; bindex++, p++)
8438 +               if (p->hd_dentry) {
8439 +                       dinfo->di_btop = bindex;
8440 +                       break;
8441 +               }
8442 +
8443 +       if (dinfo->di_btop >= 0) {
8444 +               bindex = bbot;
8445 +               p = au_hdentry(dinfo, bindex);
8446 +               for (; bindex >= 0; bindex--, p--)
8447 +                       if (p->hd_dentry) {
8448 +                               dinfo->di_bbot = bindex;
8449 +                               err = 0;
8450 +                               break;
8451 +                       }
8452 +       }
8453 +
8454 +       return err;
8455 +}
8456 +
8457 +static void au_do_hide(struct dentry *dentry)
8458 +{
8459 +       struct inode *inode;
8460 +
8461 +       if (d_really_is_positive(dentry)) {
8462 +               inode = d_inode(dentry);
8463 +               if (!d_is_dir(dentry)) {
8464 +                       if (inode->i_nlink && !d_unhashed(dentry))
8465 +                               drop_nlink(inode);
8466 +               } else {
8467 +                       clear_nlink(inode);
8468 +                       /* stop next lookup */
8469 +                       inode->i_flags |= S_DEAD;
8470 +               }
8471 +               smp_mb(); /* necessary? */
8472 +       }
8473 +       d_drop(dentry);
8474 +}
8475 +
8476 +static int au_hide_children(struct dentry *parent)
8477 +{
8478 +       int err, i, j, ndentry;
8479 +       struct au_dcsub_pages dpages;
8480 +       struct au_dpage *dpage;
8481 +       struct dentry *dentry;
8482 +
8483 +       err = au_dpages_init(&dpages, GFP_NOFS);
8484 +       if (unlikely(err))
8485 +               goto out;
8486 +       err = au_dcsub_pages(&dpages, parent, NULL, NULL);
8487 +       if (unlikely(err))
8488 +               goto out_dpages;
8489 +
8490 +       /* in reverse order */
8491 +       for (i = dpages.ndpage - 1; i >= 0; i--) {
8492 +               dpage = dpages.dpages + i;
8493 +               ndentry = dpage->ndentry;
8494 +               for (j = ndentry - 1; j >= 0; j--) {
8495 +                       dentry = dpage->dentries[j];
8496 +                       if (dentry != parent)
8497 +                               au_do_hide(dentry);
8498 +               }
8499 +       }
8500 +
8501 +out_dpages:
8502 +       au_dpages_free(&dpages);
8503 +out:
8504 +       return err;
8505 +}
8506 +
8507 +static void au_hide(struct dentry *dentry)
8508 +{
8509 +       int err;
8510 +
8511 +       AuDbgDentry(dentry);
8512 +       if (d_is_dir(dentry)) {
8513 +               /* shrink_dcache_parent(dentry); */
8514 +               err = au_hide_children(dentry);
8515 +               if (unlikely(err))
8516 +                       AuIOErr("%pd, failed hiding children, ignored %d\n",
8517 +                               dentry, err);
8518 +       }
8519 +       au_do_hide(dentry);
8520 +}
8521 +
8522 +/*
8523 + * By adding a dirty branch, a cached dentry may be affected in various ways.
8524 + *
8525 + * a dirty branch is added
8526 + * - on the top of layers
8527 + * - in the middle of layers
8528 + * - to the bottom of layers
8529 + *
8530 + * on the added branch there exists
8531 + * - a whiteout
8532 + * - a diropq
8533 + * - a same named entry
8534 + *   + exist
8535 + *     * negative --> positive
8536 + *     * positive --> positive
8537 + *      - type is unchanged
8538 + *      - type is changed
8539 + *   + doesn't exist
8540 + *     * negative --> negative
8541 + *     * positive --> negative (rejected by au_br_del() for non-dir case)
8542 + * - none
8543 + */
8544 +static int au_refresh_by_dinfo(struct dentry *dentry, struct au_dinfo *dinfo,
8545 +                              struct au_dinfo *tmp)
8546 +{
8547 +       int err;
8548 +       aufs_bindex_t bindex, bbot;
8549 +       struct {
8550 +               struct dentry *dentry;
8551 +               struct inode *inode;
8552 +               mode_t mode;
8553 +       } orig_h, tmp_h = {
8554 +               .dentry = NULL
8555 +       };
8556 +       struct au_hdentry *hd;
8557 +       struct inode *inode, *h_inode;
8558 +       struct dentry *h_dentry;
8559 +
8560 +       err = 0;
8561 +       AuDebugOn(dinfo->di_btop < 0);
8562 +       orig_h.mode = 0;
8563 +       orig_h.dentry = au_hdentry(dinfo, dinfo->di_btop)->hd_dentry;
8564 +       orig_h.inode = NULL;
8565 +       if (d_is_positive(orig_h.dentry)) {
8566 +               orig_h.inode = d_inode(orig_h.dentry);
8567 +               orig_h.mode = orig_h.inode->i_mode & S_IFMT;
8568 +       }
8569 +       if (tmp->di_btop >= 0) {
8570 +               tmp_h.dentry = au_hdentry(tmp, tmp->di_btop)->hd_dentry;
8571 +               if (d_is_positive(tmp_h.dentry)) {
8572 +                       tmp_h.inode = d_inode(tmp_h.dentry);
8573 +                       tmp_h.mode = tmp_h.inode->i_mode & S_IFMT;
8574 +               }
8575 +       }
8576 +
8577 +       inode = NULL;
8578 +       if (d_really_is_positive(dentry))
8579 +               inode = d_inode(dentry);
8580 +       if (!orig_h.inode) {
8581 +               AuDbg("negative originally\n");
8582 +               if (inode) {
8583 +                       au_hide(dentry);
8584 +                       goto out;
8585 +               }
8586 +               AuDebugOn(inode);
8587 +               AuDebugOn(dinfo->di_btop != dinfo->di_bbot);
8588 +               AuDebugOn(dinfo->di_bdiropq != -1);
8589 +
8590 +               if (!tmp_h.inode) {
8591 +                       AuDbg("negative --> negative\n");
8592 +                       /* should have only one negative lower */
8593 +                       if (tmp->di_btop >= 0
8594 +                           && tmp->di_btop < dinfo->di_btop) {
8595 +                               AuDebugOn(tmp->di_btop != tmp->di_bbot);
8596 +                               AuDebugOn(dinfo->di_btop != dinfo->di_bbot);
8597 +                               au_set_h_dptr(dentry, dinfo->di_btop, NULL);
8598 +                               au_di_cp(dinfo, tmp);
8599 +                               hd = au_hdentry(tmp, tmp->di_btop);
8600 +                               au_set_h_dptr(dentry, tmp->di_btop,
8601 +                                             dget(hd->hd_dentry));
8602 +                       }
8603 +                       au_dbg_verify_dinode(dentry);
8604 +               } else {
8605 +                       AuDbg("negative --> positive\n");
8606 +                       /*
8607 +                        * similar to the behaviour of creating with bypassing
8608 +                        * aufs.
8609 +                        * unhash it in order to force an error in the
8610 +                        * succeeding create operation.
8611 +                        * we should not set S_DEAD here.
8612 +                        */
8613 +                       d_drop(dentry);
8614 +                       /* au_di_swap(tmp, dinfo); */
8615 +                       au_dbg_verify_dinode(dentry);
8616 +               }
8617 +       } else {
8618 +               AuDbg("positive originally\n");
8619 +               /* inode may be NULL */
8620 +               AuDebugOn(inode && (inode->i_mode & S_IFMT) != orig_h.mode);
8621 +               if (!tmp_h.inode) {
8622 +                       AuDbg("positive --> negative\n");
8623 +                       /* or bypassing aufs */
8624 +                       au_hide(dentry);
8625 +                       if (tmp->di_bwh >= 0 && tmp->di_bwh <= dinfo->di_btop)
8626 +                               dinfo->di_bwh = tmp->di_bwh;
8627 +                       if (inode)
8628 +                               err = au_refresh_hinode_self(inode);
8629 +                       au_dbg_verify_dinode(dentry);
8630 +               } else if (orig_h.mode == tmp_h.mode) {
8631 +                       AuDbg("positive --> positive, same type\n");
8632 +                       if (!S_ISDIR(orig_h.mode)
8633 +                           && dinfo->di_btop > tmp->di_btop) {
8634 +                               /*
8635 +                                * similar to the behaviour of removing and
8636 +                                * creating.
8637 +                                */
8638 +                               au_hide(dentry);
8639 +                               if (inode)
8640 +                                       err = au_refresh_hinode_self(inode);
8641 +                               au_dbg_verify_dinode(dentry);
8642 +                       } else {
8643 +                               /* fill empty slots */
8644 +                               if (dinfo->di_btop > tmp->di_btop)
8645 +                                       dinfo->di_btop = tmp->di_btop;
8646 +                               if (dinfo->di_bbot < tmp->di_bbot)
8647 +                                       dinfo->di_bbot = tmp->di_bbot;
8648 +                               dinfo->di_bwh = tmp->di_bwh;
8649 +                               dinfo->di_bdiropq = tmp->di_bdiropq;
8650 +                               bbot = dinfo->di_bbot;
8651 +                               bindex = tmp->di_btop;
8652 +                               hd = au_hdentry(tmp, bindex);
8653 +                               for (; bindex <= bbot; bindex++, hd++) {
8654 +                                       if (au_h_dptr(dentry, bindex))
8655 +                                               continue;
8656 +                                       h_dentry = hd->hd_dentry;
8657 +                                       if (!h_dentry)
8658 +                                               continue;
8659 +                                       AuDebugOn(d_is_negative(h_dentry));
8660 +                                       h_inode = d_inode(h_dentry);
8661 +                                       AuDebugOn(orig_h.mode
8662 +                                                 != (h_inode->i_mode
8663 +                                                     & S_IFMT));
8664 +                                       au_set_h_dptr(dentry, bindex,
8665 +                                                     dget(h_dentry));
8666 +                               }
8667 +                               if (inode)
8668 +                                       err = au_refresh_hinode(inode, dentry);
8669 +                               au_dbg_verify_dinode(dentry);
8670 +                       }
8671 +               } else {
8672 +                       AuDbg("positive --> positive, different type\n");
8673 +                       /* similar to the behaviour of removing and creating */
8674 +                       au_hide(dentry);
8675 +                       if (inode)
8676 +                               err = au_refresh_hinode_self(inode);
8677 +                       au_dbg_verify_dinode(dentry);
8678 +               }
8679 +       }
8680 +
8681 +out:
8682 +       return err;
8683 +}
8684 +
8685 +void au_refresh_dop(struct dentry *dentry, int force_reval)
8686 +{
8687 +       const struct dentry_operations *dop
8688 +               = force_reval ? &aufs_dop : dentry->d_sb->s_d_op;
8689 +       static const unsigned int mask
8690 +               = DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE;
8691 +
8692 +       BUILD_BUG_ON(sizeof(mask) != sizeof(dentry->d_flags));
8693 +
8694 +       if (dentry->d_op == dop)
8695 +               return;
8696 +
8697 +       AuDbg("%pd\n", dentry);
8698 +       spin_lock(&dentry->d_lock);
8699 +       if (dop == &aufs_dop)
8700 +               dentry->d_flags |= mask;
8701 +       else
8702 +               dentry->d_flags &= ~mask;
8703 +       dentry->d_op = dop;
8704 +       spin_unlock(&dentry->d_lock);
8705 +}
8706 +
8707 +int au_refresh_dentry(struct dentry *dentry, struct dentry *parent)
8708 +{
8709 +       int err, ebrange, nbr;
8710 +       unsigned int sigen;
8711 +       struct au_dinfo *dinfo, *tmp;
8712 +       struct super_block *sb;
8713 +       struct inode *inode;
8714 +
8715 +       DiMustWriteLock(dentry);
8716 +       AuDebugOn(IS_ROOT(dentry));
8717 +       AuDebugOn(d_really_is_negative(parent));
8718 +
8719 +       sb = dentry->d_sb;
8720 +       sigen = au_sigen(sb);
8721 +       err = au_digen_test(parent, sigen);
8722 +       if (unlikely(err))
8723 +               goto out;
8724 +
8725 +       nbr = au_sbbot(sb) + 1;
8726 +       dinfo = au_di(dentry);
8727 +       err = au_di_realloc(dinfo, nbr, /*may_shrink*/0);
8728 +       if (unlikely(err))
8729 +               goto out;
8730 +       ebrange = au_dbrange_test(dentry);
8731 +       if (!ebrange)
8732 +               ebrange = au_do_refresh_hdentry(dentry, parent);
8733 +
8734 +       if (d_unhashed(dentry) || ebrange /* || dinfo->di_tmpfile */) {
8735 +               AuDebugOn(au_dbtop(dentry) < 0 && au_dbbot(dentry) >= 0);
8736 +               if (d_really_is_positive(dentry)) {
8737 +                       inode = d_inode(dentry);
8738 +                       err = au_refresh_hinode_self(inode);
8739 +               }
8740 +               au_dbg_verify_dinode(dentry);
8741 +               if (!err)
8742 +                       goto out_dgen; /* success */
8743 +               goto out;
8744 +       }
8745 +
8746 +       /* temporary dinfo */
8747 +       AuDbgDentry(dentry);
8748 +       err = -ENOMEM;
8749 +       tmp = au_di_alloc(sb, AuLsc_DI_TMP);
8750 +       if (unlikely(!tmp))
8751 +               goto out;
8752 +       au_di_swap(tmp, dinfo);
8753 +       /* returns the number of positive dentries */
8754 +       /*
8755 +        * if current working dir is removed, it returns an error.
8756 +        * but the dentry is legal.
8757 +        */
8758 +       err = au_lkup_dentry(dentry, /*btop*/0, AuLkup_ALLOW_NEG);
8759 +       AuDbgDentry(dentry);
8760 +       au_di_swap(tmp, dinfo);
8761 +       if (err == -ENOENT)
8762 +               err = 0;
8763 +       if (err >= 0) {
8764 +               /* compare/refresh by dinfo */
8765 +               AuDbgDentry(dentry);
8766 +               err = au_refresh_by_dinfo(dentry, dinfo, tmp);
8767 +               au_dbg_verify_dinode(dentry);
8768 +               AuTraceErr(err);
8769 +       }
8770 +       au_di_realloc(dinfo, nbr, /*may_shrink*/1); /* harmless if err */
8771 +       au_rw_write_unlock(&tmp->di_rwsem);
8772 +       au_di_free(tmp);
8773 +       if (unlikely(err))
8774 +               goto out;
8775 +
8776 +out_dgen:
8777 +       au_update_digen(dentry);
8778 +out:
8779 +       if (unlikely(err && !(dentry->d_flags & DCACHE_NFSFS_RENAMED))) {
8780 +               AuIOErr("failed refreshing %pd, %d\n", dentry, err);
8781 +               AuDbgDentry(dentry);
8782 +       }
8783 +       AuTraceErr(err);
8784 +       return err;
8785 +}
8786 +
8787 +static int au_do_h_d_reval(struct dentry *h_dentry, unsigned int flags,
8788 +                          struct dentry *dentry, aufs_bindex_t bindex)
8789 +{
8790 +       int err, valid;
8791 +
8792 +       err = 0;
8793 +       if (!(h_dentry->d_flags & DCACHE_OP_REVALIDATE))
8794 +               goto out;
8795 +
8796 +       AuDbg("b%d\n", bindex);
8797 +       /*
8798 +        * gave up supporting LOOKUP_CREATE/OPEN for lower fs,
8799 +        * due to whiteout and branch permission.
8800 +        */
8801 +       flags &= ~(/*LOOKUP_PARENT |*/ LOOKUP_OPEN | LOOKUP_CREATE
8802 +                  | LOOKUP_FOLLOW | LOOKUP_EXCL);
8803 +       /* it may return tri-state */
8804 +       valid = h_dentry->d_op->d_revalidate(h_dentry, flags);
8805 +
8806 +       if (unlikely(valid < 0))
8807 +               err = valid;
8808 +       else if (!valid)
8809 +               err = -EINVAL;
8810 +
8811 +out:
8812 +       AuTraceErr(err);
8813 +       return err;
8814 +}
8815 +
8816 +/* todo: remove this */
8817 +static int h_d_revalidate(struct dentry *dentry, struct inode *inode,
8818 +                         unsigned int flags, int do_udba, int dirren)
8819 +{
8820 +       int err;
8821 +       umode_t mode, h_mode;
8822 +       aufs_bindex_t bindex, btail, btop, ibs, ibe;
8823 +       unsigned char plus, unhashed, is_root, h_plus, h_nfs, tmpfile;
8824 +       struct inode *h_inode, *h_cached_inode;
8825 +       struct dentry *h_dentry;
8826 +       struct qstr *name, *h_name;
8827 +
8828 +       err = 0;
8829 +       plus = 0;
8830 +       mode = 0;
8831 +       ibs = -1;
8832 +       ibe = -1;
8833 +       unhashed = !!d_unhashed(dentry);
8834 +       is_root = !!IS_ROOT(dentry);
8835 +       name = &dentry->d_name;
8836 +       tmpfile = au_di(dentry)->di_tmpfile;
8837 +
8838 +       /*
8839 +        * Theoretically, REVAL test should be unnecessary in case of
8840 +        * {FS,I}NOTIFY.
8841 +        * But {fs,i}notify doesn't fire some necessary events,
8842 +        *      IN_ATTRIB for atime/nlink/pageio
8843 +        * Let's do REVAL test too.
8844 +        */
8845 +       if (do_udba && inode) {
8846 +               mode = (inode->i_mode & S_IFMT);
8847 +               plus = (inode->i_nlink > 0);
8848 +               ibs = au_ibtop(inode);
8849 +               ibe = au_ibbot(inode);
8850 +       }
8851 +
8852 +       btop = au_dbtop(dentry);
8853 +       btail = btop;
8854 +       if (inode && S_ISDIR(inode->i_mode))
8855 +               btail = au_dbtaildir(dentry);
8856 +       for (bindex = btop; bindex <= btail; bindex++) {
8857 +               h_dentry = au_h_dptr(dentry, bindex);
8858 +               if (!h_dentry)
8859 +                       continue;
8860 +
8861 +               AuDbg("b%d, %pd\n", bindex, h_dentry);
8862 +               h_nfs = !!au_test_nfs(h_dentry->d_sb);
8863 +               spin_lock(&h_dentry->d_lock);
8864 +               h_name = &h_dentry->d_name;
8865 +               if (unlikely(do_udba
8866 +                            && !is_root
8867 +                            && ((!h_nfs
8868 +                                 && (unhashed != !!d_unhashed(h_dentry)
8869 +                                     || (!tmpfile && !dirren
8870 +                                         && !au_qstreq(name, h_name))
8871 +                                         ))
8872 +                                || (h_nfs
8873 +                                    && !(flags & LOOKUP_OPEN)
8874 +                                    && (h_dentry->d_flags
8875 +                                        & DCACHE_NFSFS_RENAMED)))
8876 +                           )) {
8877 +                       int h_unhashed;
8878 +
8879 +                       h_unhashed = d_unhashed(h_dentry);
8880 +                       spin_unlock(&h_dentry->d_lock);
8881 +                       AuDbg("unhash 0x%x 0x%x, %pd %pd\n",
8882 +                             unhashed, h_unhashed, dentry, h_dentry);
8883 +                       goto err;
8884 +               }
8885 +               spin_unlock(&h_dentry->d_lock);
8886 +
8887 +               err = au_do_h_d_reval(h_dentry, flags, dentry, bindex);
8888 +               if (unlikely(err))
8889 +                       /* do not goto err, to keep the errno */
8890 +                       break;
8891 +
8892 +               /* todo: plink too? */
8893 +               if (!do_udba)
8894 +                       continue;
8895 +
8896 +               /* UDBA tests */
8897 +               if (unlikely(!!inode != d_is_positive(h_dentry)))
8898 +                       goto err;
8899 +
8900 +               h_inode = NULL;
8901 +               if (d_is_positive(h_dentry))
8902 +                       h_inode = d_inode(h_dentry);
8903 +               h_plus = plus;
8904 +               h_mode = mode;
8905 +               h_cached_inode = h_inode;
8906 +               if (h_inode) {
8907 +                       h_mode = (h_inode->i_mode & S_IFMT);
8908 +                       h_plus = (h_inode->i_nlink > 0);
8909 +               }
8910 +               if (inode && ibs <= bindex && bindex <= ibe)
8911 +                       h_cached_inode = au_h_iptr(inode, bindex);
8912 +
8913 +               if (!h_nfs) {
8914 +                       if (unlikely(plus != h_plus && !tmpfile))
8915 +                               goto err;
8916 +               } else {
8917 +                       if (unlikely(!(h_dentry->d_flags & DCACHE_NFSFS_RENAMED)
8918 +                                    && !is_root
8919 +                                    && !IS_ROOT(h_dentry)
8920 +                                    && unhashed != d_unhashed(h_dentry)))
8921 +                               goto err;
8922 +               }
8923 +               if (unlikely(mode != h_mode
8924 +                            || h_cached_inode != h_inode))
8925 +                       goto err;
8926 +               continue;
8927 +
8928 +err:
8929 +               err = -EINVAL;
8930 +               break;
8931 +       }
8932 +
8933 +       AuTraceErr(err);
8934 +       return err;
8935 +}
8936 +
8937 +/* todo: consolidate with do_refresh() and au_reval_for_attr() */
8938 +static int simple_reval_dpath(struct dentry *dentry, unsigned int sigen)
8939 +{
8940 +       int err;
8941 +       struct dentry *parent;
8942 +
8943 +       if (!au_digen_test(dentry, sigen))
8944 +               return 0;
8945 +
8946 +       parent = dget_parent(dentry);
8947 +       di_read_lock_parent(parent, AuLock_IR);
8948 +       AuDebugOn(au_digen_test(parent, sigen));
8949 +       au_dbg_verify_gen(parent, sigen);
8950 +       err = au_refresh_dentry(dentry, parent);
8951 +       di_read_unlock(parent, AuLock_IR);
8952 +       dput(parent);
8953 +       AuTraceErr(err);
8954 +       return err;
8955 +}
8956 +
8957 +int au_reval_dpath(struct dentry *dentry, unsigned int sigen)
8958 +{
8959 +       int err;
8960 +       struct dentry *d, *parent;
8961 +
8962 +       if (!au_ftest_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR))
8963 +               return simple_reval_dpath(dentry, sigen);
8964 +
8965 +       /* slow loop, keep it simple and stupid */
8966 +       /* cf: au_cpup_dirs() */
8967 +       err = 0;
8968 +       parent = NULL;
8969 +       while (au_digen_test(dentry, sigen)) {
8970 +               d = dentry;
8971 +               while (1) {
8972 +                       dput(parent);
8973 +                       parent = dget_parent(d);
8974 +                       if (!au_digen_test(parent, sigen))
8975 +                               break;
8976 +                       d = parent;
8977 +               }
8978 +
8979 +               if (d != dentry)
8980 +                       di_write_lock_child2(d);
8981 +
8982 +               /* someone might update our dentry while we were sleeping */
8983 +               if (au_digen_test(d, sigen)) {
8984 +                       /*
8985 +                        * todo: consolidate with simple_reval_dpath(),
8986 +                        * do_refresh() and au_reval_for_attr().
8987 +                        */
8988 +                       di_read_lock_parent(parent, AuLock_IR);
8989 +                       err = au_refresh_dentry(d, parent);
8990 +                       di_read_unlock(parent, AuLock_IR);
8991 +               }
8992 +
8993 +               if (d != dentry)
8994 +                       di_write_unlock(d);
8995 +               dput(parent);
8996 +               if (unlikely(err))
8997 +                       break;
8998 +       }
8999 +
9000 +       return err;
9001 +}
9002 +
9003 +/*
9004 + * if valid returns 1, otherwise 0.
9005 + */
9006 +static int aufs_d_revalidate(struct dentry *dentry, unsigned int flags)
9007 +{
9008 +       int valid, err;
9009 +       unsigned int sigen;
9010 +       unsigned char do_udba, dirren;
9011 +       struct super_block *sb;
9012 +       struct inode *inode;
9013 +
9014 +       /* todo: support rcu-walk? */
9015 +       if (flags & LOOKUP_RCU)
9016 +               return -ECHILD;
9017 +
9018 +       valid = 0;
9019 +       if (unlikely(!au_di(dentry)))
9020 +               goto out;
9021 +
9022 +       valid = 1;
9023 +       sb = dentry->d_sb;
9024 +       /*
9025 +        * todo: very ugly
9026 +        * i_mutex of parent dir may be held,
9027 +        * but we should not return 'invalid' due to busy.
9028 +        */
9029 +       err = aufs_read_lock(dentry, AuLock_FLUSH | AuLock_DW | AuLock_NOPLM);
9030 +       if (unlikely(err)) {
9031 +               valid = err;
9032 +               AuTraceErr(err);
9033 +               goto out;
9034 +       }
9035 +       inode = NULL;
9036 +       if (d_really_is_positive(dentry))
9037 +               inode = d_inode(dentry);
9038 +       if (unlikely(inode && au_is_bad_inode(inode))) {
9039 +               err = -EINVAL;
9040 +               AuTraceErr(err);
9041 +               goto out_dgrade;
9042 +       }
9043 +       if (unlikely(au_dbrange_test(dentry))) {
9044 +               err = -EINVAL;
9045 +               AuTraceErr(err);
9046 +               goto out_dgrade;
9047 +       }
9048 +
9049 +       sigen = au_sigen(sb);
9050 +       if (au_digen_test(dentry, sigen)) {
9051 +               AuDebugOn(IS_ROOT(dentry));
9052 +               err = au_reval_dpath(dentry, sigen);
9053 +               if (unlikely(err)) {
9054 +                       AuTraceErr(err);
9055 +                       goto out_dgrade;
9056 +               }
9057 +       }
9058 +       di_downgrade_lock(dentry, AuLock_IR);
9059 +
9060 +       err = -EINVAL;
9061 +       if (!(flags & (LOOKUP_OPEN | LOOKUP_EMPTY))
9062 +           && inode
9063 +           && !(inode->i_state && I_LINKABLE)
9064 +           && (IS_DEADDIR(inode) || !inode->i_nlink)) {
9065 +               AuTraceErr(err);
9066 +               goto out_inval;
9067 +       }
9068 +
9069 +       do_udba = !au_opt_test(au_mntflags(sb), UDBA_NONE);
9070 +       if (do_udba && inode) {
9071 +               aufs_bindex_t btop = au_ibtop(inode);
9072 +               struct inode *h_inode;
9073 +
9074 +               if (btop >= 0) {
9075 +                       h_inode = au_h_iptr(inode, btop);
9076 +                       if (h_inode && au_test_higen(inode, h_inode)) {
9077 +                               AuTraceErr(err);
9078 +                               goto out_inval;
9079 +                       }
9080 +               }
9081 +       }
9082 +
9083 +       dirren = !!au_opt_test(au_mntflags(sb), DIRREN);
9084 +       err = h_d_revalidate(dentry, inode, flags, do_udba, dirren);
9085 +       if (unlikely(!err && do_udba && au_dbtop(dentry) < 0)) {
9086 +               err = -EIO;
9087 +               AuDbg("both of real entry and whiteout found, %p, err %d\n",
9088 +                     dentry, err);
9089 +       }
9090 +       goto out_inval;
9091 +
9092 +out_dgrade:
9093 +       di_downgrade_lock(dentry, AuLock_IR);
9094 +out_inval:
9095 +       aufs_read_unlock(dentry, AuLock_IR);
9096 +       AuTraceErr(err);
9097 +       valid = !err;
9098 +out:
9099 +       if (!valid) {
9100 +               AuDbg("%pd invalid, %d\n", dentry, valid);
9101 +               d_drop(dentry);
9102 +       }
9103 +       return valid;
9104 +}
9105 +
9106 +static void aufs_d_release(struct dentry *dentry)
9107 +{
9108 +       if (au_di(dentry)) {
9109 +               au_di_fin(dentry);
9110 +               au_hn_di_reinit(dentry);
9111 +       }
9112 +}
9113 +
9114 +const struct dentry_operations aufs_dop = {
9115 +       .d_revalidate           = aufs_d_revalidate,
9116 +       .d_weak_revalidate      = aufs_d_revalidate,
9117 +       .d_release              = aufs_d_release
9118 +};
9119 +
9120 +/* aufs_dop without d_revalidate */
9121 +const struct dentry_operations aufs_dop_noreval = {
9122 +       .d_release              = aufs_d_release
9123 +};
9124 diff -urN /usr/share/empty/fs/aufs/dentry.h linux/fs/aufs/dentry.h
9125 --- /usr/share/empty/fs/aufs/dentry.h   1970-01-01 01:00:00.000000000 +0100
9126 +++ linux/fs/aufs/dentry.h      2022-03-21 14:49:05.719966344 +0100
9127 @@ -0,0 +1,269 @@
9128 +/* SPDX-License-Identifier: GPL-2.0 */
9129 +/*
9130 + * Copyright (C) 2005-2021 Junjiro R. Okajima
9131 + *
9132 + * This program, aufs is free software; you can redistribute it and/or modify
9133 + * it under the terms of the GNU General Public License as published by
9134 + * the Free Software Foundation; either version 2 of the License, or
9135 + * (at your option) any later version.
9136 + *
9137 + * This program is distributed in the hope that it will be useful,
9138 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
9139 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9140 + * GNU General Public License for more details.
9141 + *
9142 + * You should have received a copy of the GNU General Public License
9143 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
9144 + */
9145 +
9146 +/*
9147 + * lookup and dentry operations
9148 + */
9149 +
9150 +#ifndef __AUFS_DENTRY_H__
9151 +#define __AUFS_DENTRY_H__
9152 +
9153 +#ifdef __KERNEL__
9154 +
9155 +#include <linux/dcache.h>
9156 +#include "dirren.h"
9157 +#include "rwsem.h"
9158 +
9159 +struct au_hdentry {
9160 +       struct dentry           *hd_dentry;
9161 +       aufs_bindex_t           hd_id;
9162 +};
9163 +
9164 +struct au_dinfo {
9165 +       atomic_t                di_generation;
9166 +
9167 +       struct au_rwsem         di_rwsem;
9168 +       aufs_bindex_t           di_btop, di_bbot, di_bwh, di_bdiropq;
9169 +       unsigned char           di_tmpfile; /* to allow the different name */
9170 +       struct au_hdentry       *di_hdentry;
9171 +       struct rcu_head         rcu;
9172 +} ____cacheline_aligned_in_smp;
9173 +
9174 +/* ---------------------------------------------------------------------- */
9175 +
9176 +/* flags for au_lkup_dentry() */
9177 +#define AuLkup_ALLOW_NEG       1
9178 +#define AuLkup_IGNORE_PERM     (1 << 1)
9179 +#define AuLkup_DIRREN          (1 << 2)
9180 +#define au_ftest_lkup(flags, name)     ((flags) & AuLkup_##name)
9181 +#define au_fset_lkup(flags, name) \
9182 +       do { (flags) |= AuLkup_##name; } while (0)
9183 +#define au_fclr_lkup(flags, name) \
9184 +       do { (flags) &= ~AuLkup_##name; } while (0)
9185 +
9186 +#ifndef CONFIG_AUFS_DIRREN
9187 +#undef AuLkup_DIRREN
9188 +#define AuLkup_DIRREN 0
9189 +#endif
9190 +
9191 +struct au_do_lookup_args {
9192 +       unsigned int            flags;
9193 +       mode_t                  type;
9194 +       struct qstr             whname, *name;
9195 +       struct au_dr_lookup     dirren;
9196 +};
9197 +
9198 +/* ---------------------------------------------------------------------- */
9199 +
9200 +/* dentry.c */
9201 +extern const struct dentry_operations aufs_dop, aufs_dop_noreval;
9202 +struct au_branch;
9203 +struct dentry *au_sio_lkup_one(struct user_namespace *userns, struct qstr *name,
9204 +                              struct path *ppath);
9205 +int au_h_verify(struct dentry *h_dentry, unsigned int udba, struct inode *h_dir,
9206 +               struct dentry *h_parent, struct au_branch *br);
9207 +
9208 +int au_lkup_dentry(struct dentry *dentry, aufs_bindex_t btop,
9209 +                  unsigned int flags);
9210 +int au_lkup_neg(struct dentry *dentry, aufs_bindex_t bindex, int wh);
9211 +int au_refresh_dentry(struct dentry *dentry, struct dentry *parent);
9212 +int au_reval_dpath(struct dentry *dentry, unsigned int sigen);
9213 +void au_refresh_dop(struct dentry *dentry, int force_reval);
9214 +
9215 +/* dinfo.c */
9216 +void au_di_init_once(void *_di);
9217 +struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc);
9218 +void au_di_free(struct au_dinfo *dinfo);
9219 +void au_di_swap(struct au_dinfo *a, struct au_dinfo *b);
9220 +void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src);
9221 +int au_di_init(struct dentry *dentry);
9222 +void au_di_fin(struct dentry *dentry);
9223 +int au_di_realloc(struct au_dinfo *dinfo, int nbr, int may_shrink);
9224 +
9225 +void di_read_lock(struct dentry *d, int flags, unsigned int lsc);
9226 +void di_read_unlock(struct dentry *d, int flags);
9227 +void di_downgrade_lock(struct dentry *d, int flags);
9228 +void di_write_lock(struct dentry *d, unsigned int lsc);
9229 +void di_write_unlock(struct dentry *d);
9230 +void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir);
9231 +void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir);
9232 +void di_write_unlock2(struct dentry *d1, struct dentry *d2);
9233 +
9234 +struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex);
9235 +struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex);
9236 +aufs_bindex_t au_dbtail(struct dentry *dentry);
9237 +aufs_bindex_t au_dbtaildir(struct dentry *dentry);
9238 +
9239 +void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
9240 +                  struct dentry *h_dentry);
9241 +int au_digen_test(struct dentry *dentry, unsigned int sigen);
9242 +int au_dbrange_test(struct dentry *dentry);
9243 +void au_update_digen(struct dentry *dentry);
9244 +void au_update_dbrange(struct dentry *dentry, int do_put_zero);
9245 +void au_update_dbtop(struct dentry *dentry);
9246 +void au_update_dbbot(struct dentry *dentry);
9247 +int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry);
9248 +
9249 +/* ---------------------------------------------------------------------- */
9250 +
9251 +static inline struct au_dinfo *au_di(struct dentry *dentry)
9252 +{
9253 +       return dentry->d_fsdata;
9254 +}
9255 +
9256 +/* ---------------------------------------------------------------------- */
9257 +
9258 +/* lock subclass for dinfo */
9259 +enum {
9260 +       AuLsc_DI_CHILD,         /* child first */
9261 +       AuLsc_DI_CHILD2,        /* rename(2), link(2), and cpup at hnotify */
9262 +       AuLsc_DI_CHILD3,        /* copyup dirs */
9263 +       AuLsc_DI_PARENT,
9264 +       AuLsc_DI_PARENT2,
9265 +       AuLsc_DI_PARENT3,
9266 +       AuLsc_DI_TMP            /* temp for replacing dinfo */
9267 +};
9268 +
9269 +/*
9270 + * di_read_lock_child, di_write_lock_child,
9271 + * di_read_lock_child2, di_write_lock_child2,
9272 + * di_read_lock_child3, di_write_lock_child3,
9273 + * di_read_lock_parent, di_write_lock_parent,
9274 + * di_read_lock_parent2, di_write_lock_parent2,
9275 + * di_read_lock_parent3, di_write_lock_parent3,
9276 + */
9277 +#define AuReadLockFunc(name, lsc) \
9278 +static inline void di_read_lock_##name(struct dentry *d, int flags) \
9279 +{ di_read_lock(d, flags, AuLsc_DI_##lsc); }
9280 +
9281 +#define AuWriteLockFunc(name, lsc) \
9282 +static inline void di_write_lock_##name(struct dentry *d) \
9283 +{ di_write_lock(d, AuLsc_DI_##lsc); }
9284 +
9285 +#define AuRWLockFuncs(name, lsc) \
9286 +       AuReadLockFunc(name, lsc) \
9287 +       AuWriteLockFunc(name, lsc)
9288 +
9289 +AuRWLockFuncs(child, CHILD);
9290 +AuRWLockFuncs(child2, CHILD2);
9291 +AuRWLockFuncs(child3, CHILD3);
9292 +AuRWLockFuncs(parent, PARENT);
9293 +AuRWLockFuncs(parent2, PARENT2);
9294 +AuRWLockFuncs(parent3, PARENT3);
9295 +
9296 +#undef AuReadLockFunc
9297 +#undef AuWriteLockFunc
9298 +#undef AuRWLockFuncs
9299 +
9300 +#define DiMustNoWaiters(d)     AuRwMustNoWaiters(&au_di(d)->di_rwsem)
9301 +#define DiMustAnyLock(d)       AuRwMustAnyLock(&au_di(d)->di_rwsem)
9302 +#define DiMustWriteLock(d)     AuRwMustWriteLock(&au_di(d)->di_rwsem)
9303 +
9304 +/* ---------------------------------------------------------------------- */
9305 +
9306 +/* todo: memory barrier? */
9307 +static inline unsigned int au_digen(struct dentry *d)
9308 +{
9309 +       return atomic_read(&au_di(d)->di_generation);
9310 +}
9311 +
9312 +static inline void au_h_dentry_init(struct au_hdentry *hdentry)
9313 +{
9314 +       hdentry->hd_dentry = NULL;
9315 +}
9316 +
9317 +static inline struct au_hdentry *au_hdentry(struct au_dinfo *di,
9318 +                                           aufs_bindex_t bindex)
9319 +{
9320 +       return di->di_hdentry + bindex;
9321 +}
9322 +
9323 +static inline void au_hdput(struct au_hdentry *hd)
9324 +{
9325 +       if (hd)
9326 +               dput(hd->hd_dentry);
9327 +}
9328 +
9329 +static inline aufs_bindex_t au_dbtop(struct dentry *dentry)
9330 +{
9331 +       DiMustAnyLock(dentry);
9332 +       return au_di(dentry)->di_btop;
9333 +}
9334 +
9335 +static inline aufs_bindex_t au_dbbot(struct dentry *dentry)
9336 +{
9337 +       DiMustAnyLock(dentry);
9338 +       return au_di(dentry)->di_bbot;
9339 +}
9340 +
9341 +static inline aufs_bindex_t au_dbwh(struct dentry *dentry)
9342 +{
9343 +       DiMustAnyLock(dentry);
9344 +       return au_di(dentry)->di_bwh;
9345 +}
9346 +
9347 +static inline aufs_bindex_t au_dbdiropq(struct dentry *dentry)
9348 +{
9349 +       DiMustAnyLock(dentry);
9350 +       return au_di(dentry)->di_bdiropq;
9351 +}
9352 +
9353 +/* todo: hard/soft set? */
9354 +static inline void au_set_dbtop(struct dentry *dentry, aufs_bindex_t bindex)
9355 +{
9356 +       DiMustWriteLock(dentry);
9357 +       au_di(dentry)->di_btop = bindex;
9358 +}
9359 +
9360 +static inline void au_set_dbbot(struct dentry *dentry, aufs_bindex_t bindex)
9361 +{
9362 +       DiMustWriteLock(dentry);
9363 +       au_di(dentry)->di_bbot = bindex;
9364 +}
9365 +
9366 +static inline void au_set_dbwh(struct dentry *dentry, aufs_bindex_t bindex)
9367 +{
9368 +       DiMustWriteLock(dentry);
9369 +       /* dbwh can be outside of btop - bbot range */
9370 +       au_di(dentry)->di_bwh = bindex;
9371 +}
9372 +
9373 +static inline void au_set_dbdiropq(struct dentry *dentry, aufs_bindex_t bindex)
9374 +{
9375 +       DiMustWriteLock(dentry);
9376 +       au_di(dentry)->di_bdiropq = bindex;
9377 +}
9378 +
9379 +/* ---------------------------------------------------------------------- */
9380 +
9381 +#ifdef CONFIG_AUFS_HNOTIFY
9382 +static inline void au_digen_dec(struct dentry *d)
9383 +{
9384 +       atomic_dec(&au_di(d)->di_generation);
9385 +}
9386 +
9387 +static inline void au_hn_di_reinit(struct dentry *dentry)
9388 +{
9389 +       dentry->d_fsdata = NULL;
9390 +}
9391 +#else
9392 +AuStubVoid(au_hn_di_reinit, struct dentry *dentry __maybe_unused)
9393 +#endif /* CONFIG_AUFS_HNOTIFY */
9394 +
9395 +#endif /* __KERNEL__ */
9396 +#endif /* __AUFS_DENTRY_H__ */
9397 diff -urN /usr/share/empty/fs/aufs/dinfo.c linux/fs/aufs/dinfo.c
9398 --- /usr/share/empty/fs/aufs/dinfo.c    1970-01-01 01:00:00.000000000 +0100
9399 +++ linux/fs/aufs/dinfo.c       2022-03-21 14:49:05.719966344 +0100
9400 @@ -0,0 +1,554 @@
9401 +// SPDX-License-Identifier: GPL-2.0
9402 +/*
9403 + * Copyright (C) 2005-2021 Junjiro R. Okajima
9404 + *
9405 + * This program, aufs is free software; you can redistribute it and/or modify
9406 + * it under the terms of the GNU General Public License as published by
9407 + * the Free Software Foundation; either version 2 of the License, or
9408 + * (at your option) any later version.
9409 + *
9410 + * This program is distributed in the hope that it will be useful,
9411 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
9412 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9413 + * GNU General Public License for more details.
9414 + *
9415 + * You should have received a copy of the GNU General Public License
9416 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
9417 + */
9418 +
9419 +/*
9420 + * dentry private data
9421 + */
9422 +
9423 +#include "aufs.h"
9424 +
9425 +void au_di_init_once(void *_dinfo)
9426 +{
9427 +       struct au_dinfo *dinfo = _dinfo;
9428 +
9429 +       au_rw_init(&dinfo->di_rwsem);
9430 +}
9431 +
9432 +struct au_dinfo *au_di_alloc(struct super_block *sb, unsigned int lsc)
9433 +{
9434 +       struct au_dinfo *dinfo;
9435 +       int nbr, i;
9436 +
9437 +       dinfo = au_cache_alloc_dinfo();
9438 +       if (unlikely(!dinfo))
9439 +               goto out;
9440 +
9441 +       nbr = au_sbbot(sb) + 1;
9442 +       if (nbr <= 0)
9443 +               nbr = 1;
9444 +       dinfo->di_hdentry = kcalloc(nbr, sizeof(*dinfo->di_hdentry), GFP_NOFS);
9445 +       if (dinfo->di_hdentry) {
9446 +               au_rw_write_lock_nested(&dinfo->di_rwsem, lsc);
9447 +               dinfo->di_btop = -1;
9448 +               dinfo->di_bbot = -1;
9449 +               dinfo->di_bwh = -1;
9450 +               dinfo->di_bdiropq = -1;
9451 +               dinfo->di_tmpfile = 0;
9452 +               for (i = 0; i < nbr; i++)
9453 +                       dinfo->di_hdentry[i].hd_id = -1;
9454 +               goto out;
9455 +       }
9456 +
9457 +       au_cache_free_dinfo(dinfo);
9458 +       dinfo = NULL;
9459 +
9460 +out:
9461 +       return dinfo;
9462 +}
9463 +
9464 +void au_di_free(struct au_dinfo *dinfo)
9465 +{
9466 +       struct au_hdentry *p;
9467 +       aufs_bindex_t bbot, bindex;
9468 +
9469 +       /* dentry may not be revalidated */
9470 +       bindex = dinfo->di_btop;
9471 +       if (bindex >= 0) {
9472 +               bbot = dinfo->di_bbot;
9473 +               p = au_hdentry(dinfo, bindex);
9474 +               while (bindex++ <= bbot)
9475 +                       au_hdput(p++);
9476 +       }
9477 +       au_kfree_try_rcu(dinfo->di_hdentry);
9478 +       au_cache_free_dinfo(dinfo);
9479 +}
9480 +
9481 +void au_di_swap(struct au_dinfo *a, struct au_dinfo *b)
9482 +{
9483 +       struct au_hdentry *p;
9484 +       aufs_bindex_t bi;
9485 +
9486 +       AuRwMustWriteLock(&a->di_rwsem);
9487 +       AuRwMustWriteLock(&b->di_rwsem);
9488 +
9489 +#define DiSwap(v, name)                                \
9490 +       do {                                    \
9491 +               v = a->di_##name;               \
9492 +               a->di_##name = b->di_##name;    \
9493 +               b->di_##name = v;               \
9494 +       } while (0)
9495 +
9496 +       DiSwap(p, hdentry);
9497 +       DiSwap(bi, btop);
9498 +       DiSwap(bi, bbot);
9499 +       DiSwap(bi, bwh);
9500 +       DiSwap(bi, bdiropq);
9501 +       /* smp_mb(); */
9502 +
9503 +#undef DiSwap
9504 +}
9505 +
9506 +void au_di_cp(struct au_dinfo *dst, struct au_dinfo *src)
9507 +{
9508 +       AuRwMustWriteLock(&dst->di_rwsem);
9509 +       AuRwMustWriteLock(&src->di_rwsem);
9510 +
9511 +       dst->di_btop = src->di_btop;
9512 +       dst->di_bbot = src->di_bbot;
9513 +       dst->di_bwh = src->di_bwh;
9514 +       dst->di_bdiropq = src->di_bdiropq;
9515 +       /* smp_mb(); */
9516 +}
9517 +
9518 +int au_di_init(struct dentry *dentry)
9519 +{
9520 +       int err;
9521 +       struct super_block *sb;
9522 +       struct au_dinfo *dinfo;
9523 +
9524 +       err = 0;
9525 +       sb = dentry->d_sb;
9526 +       dinfo = au_di_alloc(sb, AuLsc_DI_CHILD);
9527 +       if (dinfo) {
9528 +               atomic_set(&dinfo->di_generation, au_sigen(sb));
9529 +               /* smp_mb(); */ /* atomic_set */
9530 +               dentry->d_fsdata = dinfo;
9531 +       } else
9532 +               err = -ENOMEM;
9533 +
9534 +       return err;
9535 +}
9536 +
9537 +void au_di_fin(struct dentry *dentry)
9538 +{
9539 +       struct au_dinfo *dinfo;
9540 +
9541 +       dinfo = au_di(dentry);
9542 +       AuRwDestroy(&dinfo->di_rwsem);
9543 +       au_di_free(dinfo);
9544 +}
9545 +
9546 +int au_di_realloc(struct au_dinfo *dinfo, int nbr, int may_shrink)
9547 +{
9548 +       int err, sz;
9549 +       struct au_hdentry *hdp;
9550 +
9551 +       AuRwMustWriteLock(&dinfo->di_rwsem);
9552 +
9553 +       err = -ENOMEM;
9554 +       sz = sizeof(*hdp) * (dinfo->di_bbot + 1);
9555 +       if (!sz)
9556 +               sz = sizeof(*hdp);
9557 +       hdp = au_kzrealloc(dinfo->di_hdentry, sz, sizeof(*hdp) * nbr, GFP_NOFS,
9558 +                          may_shrink);
9559 +       if (hdp) {
9560 +               dinfo->di_hdentry = hdp;
9561 +               err = 0;
9562 +       }
9563 +
9564 +       return err;
9565 +}
9566 +
9567 +/* ---------------------------------------------------------------------- */
9568 +
9569 +static void do_ii_write_lock(struct inode *inode, unsigned int lsc)
9570 +{
9571 +       switch (lsc) {
9572 +       case AuLsc_DI_CHILD:
9573 +               ii_write_lock_child(inode);
9574 +               break;
9575 +       case AuLsc_DI_CHILD2:
9576 +               ii_write_lock_child2(inode);
9577 +               break;
9578 +       case AuLsc_DI_CHILD3:
9579 +               ii_write_lock_child3(inode);
9580 +               break;
9581 +       case AuLsc_DI_PARENT:
9582 +               ii_write_lock_parent(inode);
9583 +               break;
9584 +       case AuLsc_DI_PARENT2:
9585 +               ii_write_lock_parent2(inode);
9586 +               break;
9587 +       case AuLsc_DI_PARENT3:
9588 +               ii_write_lock_parent3(inode);
9589 +               break;
9590 +       default:
9591 +               BUG();
9592 +       }
9593 +}
9594 +
9595 +static void do_ii_read_lock(struct inode *inode, unsigned int lsc)
9596 +{
9597 +       switch (lsc) {
9598 +       case AuLsc_DI_CHILD:
9599 +               ii_read_lock_child(inode);
9600 +               break;
9601 +       case AuLsc_DI_CHILD2:
9602 +               ii_read_lock_child2(inode);
9603 +               break;
9604 +       case AuLsc_DI_CHILD3:
9605 +               ii_read_lock_child3(inode);
9606 +               break;
9607 +       case AuLsc_DI_PARENT:
9608 +               ii_read_lock_parent(inode);
9609 +               break;
9610 +       case AuLsc_DI_PARENT2:
9611 +               ii_read_lock_parent2(inode);
9612 +               break;
9613 +       case AuLsc_DI_PARENT3:
9614 +               ii_read_lock_parent3(inode);
9615 +               break;
9616 +       default:
9617 +               BUG();
9618 +       }
9619 +}
9620 +
9621 +void di_read_lock(struct dentry *d, int flags, unsigned int lsc)
9622 +{
9623 +       struct inode *inode;
9624 +
9625 +       au_rw_read_lock_nested(&au_di(d)->di_rwsem, lsc);
9626 +       if (d_really_is_positive(d)) {
9627 +               inode = d_inode(d);
9628 +               if (au_ftest_lock(flags, IW))
9629 +                       do_ii_write_lock(inode, lsc);
9630 +               else if (au_ftest_lock(flags, IR))
9631 +                       do_ii_read_lock(inode, lsc);
9632 +       }
9633 +}
9634 +
9635 +void di_read_unlock(struct dentry *d, int flags)
9636 +{
9637 +       struct inode *inode;
9638 +
9639 +       if (d_really_is_positive(d)) {
9640 +               inode = d_inode(d);
9641 +               if (au_ftest_lock(flags, IW)) {
9642 +                       au_dbg_verify_dinode(d);
9643 +                       ii_write_unlock(inode);
9644 +               } else if (au_ftest_lock(flags, IR)) {
9645 +                       au_dbg_verify_dinode(d);
9646 +                       ii_read_unlock(inode);
9647 +               }
9648 +       }
9649 +       au_rw_read_unlock(&au_di(d)->di_rwsem);
9650 +}
9651 +
9652 +void di_downgrade_lock(struct dentry *d, int flags)
9653 +{
9654 +       if (d_really_is_positive(d) && au_ftest_lock(flags, IR))
9655 +               ii_downgrade_lock(d_inode(d));
9656 +       au_rw_dgrade_lock(&au_di(d)->di_rwsem);
9657 +}
9658 +
9659 +void di_write_lock(struct dentry *d, unsigned int lsc)
9660 +{
9661 +       au_rw_write_lock_nested(&au_di(d)->di_rwsem, lsc);
9662 +       if (d_really_is_positive(d))
9663 +               do_ii_write_lock(d_inode(d), lsc);
9664 +}
9665 +
9666 +void di_write_unlock(struct dentry *d)
9667 +{
9668 +       au_dbg_verify_dinode(d);
9669 +       if (d_really_is_positive(d))
9670 +               ii_write_unlock(d_inode(d));
9671 +       au_rw_write_unlock(&au_di(d)->di_rwsem);
9672 +}
9673 +
9674 +void di_write_lock2_child(struct dentry *d1, struct dentry *d2, int isdir)
9675 +{
9676 +       AuDebugOn(d1 == d2
9677 +                 || d_inode(d1) == d_inode(d2)
9678 +                 || d1->d_sb != d2->d_sb);
9679 +
9680 +       if ((isdir && au_test_subdir(d1, d2))
9681 +           || d1 < d2) {
9682 +               di_write_lock_child(d1);
9683 +               di_write_lock_child2(d2);
9684 +       } else {
9685 +               di_write_lock_child(d2);
9686 +               di_write_lock_child2(d1);
9687 +       }
9688 +}
9689 +
9690 +void di_write_lock2_parent(struct dentry *d1, struct dentry *d2, int isdir)
9691 +{
9692 +       AuDebugOn(d1 == d2
9693 +                 || d_inode(d1) == d_inode(d2)
9694 +                 || d1->d_sb != d2->d_sb);
9695 +
9696 +       if ((isdir && au_test_subdir(d1, d2))
9697 +           || d1 < d2) {
9698 +               di_write_lock_parent(d1);
9699 +               di_write_lock_parent2(d2);
9700 +       } else {
9701 +               di_write_lock_parent(d2);
9702 +               di_write_lock_parent2(d1);
9703 +       }
9704 +}
9705 +
9706 +void di_write_unlock2(struct dentry *d1, struct dentry *d2)
9707 +{
9708 +       di_write_unlock(d1);
9709 +       if (d_inode(d1) == d_inode(d2))
9710 +               au_rw_write_unlock(&au_di(d2)->di_rwsem);
9711 +       else
9712 +               di_write_unlock(d2);
9713 +}
9714 +
9715 +/* ---------------------------------------------------------------------- */
9716 +
9717 +struct dentry *au_h_dptr(struct dentry *dentry, aufs_bindex_t bindex)
9718 +{
9719 +       struct dentry *d;
9720 +
9721 +       DiMustAnyLock(dentry);
9722 +
9723 +       if (au_dbtop(dentry) < 0 || bindex < au_dbtop(dentry))
9724 +               return NULL;
9725 +       AuDebugOn(bindex < 0);
9726 +       d = au_hdentry(au_di(dentry), bindex)->hd_dentry;
9727 +       AuDebugOn(d && au_dcount(d) <= 0);
9728 +       return d;
9729 +}
9730 +
9731 +/*
9732 + * extended version of au_h_dptr().
9733 + * returns a hashed and positive (or linkable) h_dentry in bindex, NULL, or
9734 + * error.
9735 + */
9736 +struct dentry *au_h_d_alias(struct dentry *dentry, aufs_bindex_t bindex)
9737 +{
9738 +       struct dentry *h_dentry;
9739 +       struct inode *inode, *h_inode;
9740 +
9741 +       AuDebugOn(d_really_is_negative(dentry));
9742 +
9743 +       h_dentry = NULL;
9744 +       if (au_dbtop(dentry) <= bindex
9745 +           && bindex <= au_dbbot(dentry))
9746 +               h_dentry = au_h_dptr(dentry, bindex);
9747 +       if (h_dentry && !au_d_linkable(h_dentry)) {
9748 +               dget(h_dentry);
9749 +               goto out; /* success */
9750 +       }
9751 +
9752 +       inode = d_inode(dentry);
9753 +       AuDebugOn(bindex < au_ibtop(inode));
9754 +       AuDebugOn(au_ibbot(inode) < bindex);
9755 +       h_inode = au_h_iptr(inode, bindex);
9756 +       h_dentry = d_find_alias(h_inode);
9757 +       if (h_dentry) {
9758 +               if (!IS_ERR(h_dentry)) {
9759 +                       if (!au_d_linkable(h_dentry))
9760 +                               goto out; /* success */
9761 +                       dput(h_dentry);
9762 +               } else
9763 +                       goto out;
9764 +       }
9765 +
9766 +       if (au_opt_test(au_mntflags(dentry->d_sb), PLINK)) {
9767 +               h_dentry = au_plink_lkup(inode, bindex);
9768 +               AuDebugOn(!h_dentry);
9769 +               if (!IS_ERR(h_dentry)) {
9770 +                       if (!au_d_hashed_positive(h_dentry))
9771 +                               goto out; /* success */
9772 +                       dput(h_dentry);
9773 +                       h_dentry = NULL;
9774 +               }
9775 +       }
9776 +
9777 +out:
9778 +       AuDbgDentry(h_dentry);
9779 +       return h_dentry;
9780 +}
9781 +
9782 +aufs_bindex_t au_dbtail(struct dentry *dentry)
9783 +{
9784 +       aufs_bindex_t bbot, bwh;
9785 +
9786 +       bbot = au_dbbot(dentry);
9787 +       if (0 <= bbot) {
9788 +               bwh = au_dbwh(dentry);
9789 +               if (!bwh)
9790 +                       return bwh;
9791 +               if (0 < bwh && bwh < bbot)
9792 +                       return bwh - 1;
9793 +       }
9794 +       return bbot;
9795 +}
9796 +
9797 +aufs_bindex_t au_dbtaildir(struct dentry *dentry)
9798 +{
9799 +       aufs_bindex_t bbot, bopq;
9800 +
9801 +       bbot = au_dbtail(dentry);
9802 +       if (0 <= bbot) {
9803 +               bopq = au_dbdiropq(dentry);
9804 +               if (0 <= bopq && bopq < bbot)
9805 +                       bbot = bopq;
9806 +       }
9807 +       return bbot;
9808 +}
9809 +
9810 +/* ---------------------------------------------------------------------- */
9811 +
9812 +void au_set_h_dptr(struct dentry *dentry, aufs_bindex_t bindex,
9813 +                  struct dentry *h_dentry)
9814 +{
9815 +       struct au_dinfo *dinfo;
9816 +       struct au_hdentry *hd;
9817 +       struct au_branch *br;
9818 +
9819 +       DiMustWriteLock(dentry);
9820 +
9821 +       dinfo = au_di(dentry);
9822 +       hd = au_hdentry(dinfo, bindex);
9823 +       au_hdput(hd);
9824 +       hd->hd_dentry = h_dentry;
9825 +       if (h_dentry) {
9826 +               br = au_sbr(dentry->d_sb, bindex);
9827 +               hd->hd_id = br->br_id;
9828 +       }
9829 +}
9830 +
9831 +int au_dbrange_test(struct dentry *dentry)
9832 +{
9833 +       int err;
9834 +       aufs_bindex_t btop, bbot;
9835 +
9836 +       err = 0;
9837 +       btop = au_dbtop(dentry);
9838 +       bbot = au_dbbot(dentry);
9839 +       if (btop >= 0)
9840 +               AuDebugOn(bbot < 0 && btop > bbot);
9841 +       else {
9842 +               err = -EIO;
9843 +               AuDebugOn(bbot >= 0);
9844 +       }
9845 +
9846 +       return err;
9847 +}
9848 +
9849 +int au_digen_test(struct dentry *dentry, unsigned int sigen)
9850 +{
9851 +       int err;
9852 +
9853 +       err = 0;
9854 +       if (unlikely(au_digen(dentry) != sigen
9855 +                    || au_iigen_test(d_inode(dentry), sigen)))
9856 +               err = -EIO;
9857 +
9858 +       return err;
9859 +}
9860 +
9861 +void au_update_digen(struct dentry *dentry)
9862 +{
9863 +       atomic_set(&au_di(dentry)->di_generation, au_sigen(dentry->d_sb));
9864 +       /* smp_mb(); */ /* atomic_set */
9865 +}
9866 +
9867 +void au_update_dbrange(struct dentry *dentry, int do_put_zero)
9868 +{
9869 +       struct au_dinfo *dinfo;
9870 +       struct dentry *h_d;
9871 +       struct au_hdentry *hdp;
9872 +       aufs_bindex_t bindex, bbot;
9873 +
9874 +       DiMustWriteLock(dentry);
9875 +
9876 +       dinfo = au_di(dentry);
9877 +       if (!dinfo || dinfo->di_btop < 0)
9878 +               return;
9879 +
9880 +       if (do_put_zero) {
9881 +               bbot = dinfo->di_bbot;
9882 +               bindex = dinfo->di_btop;
9883 +               hdp = au_hdentry(dinfo, bindex);
9884 +               for (; bindex <= bbot; bindex++, hdp++) {
9885 +                       h_d = hdp->hd_dentry;
9886 +                       if (h_d && d_is_negative(h_d))
9887 +                               au_set_h_dptr(dentry, bindex, NULL);
9888 +               }
9889 +       }
9890 +
9891 +       dinfo->di_btop = 0;
9892 +       hdp = au_hdentry(dinfo, dinfo->di_btop);
9893 +       for (; dinfo->di_btop <= dinfo->di_bbot; dinfo->di_btop++, hdp++)
9894 +               if (hdp->hd_dentry)
9895 +                       break;
9896 +       if (dinfo->di_btop > dinfo->di_bbot) {
9897 +               dinfo->di_btop = -1;
9898 +               dinfo->di_bbot = -1;
9899 +               return;
9900 +       }
9901 +
9902 +       hdp = au_hdentry(dinfo, dinfo->di_bbot);
9903 +       for (; dinfo->di_bbot >= 0; dinfo->di_bbot--, hdp--)
9904 +               if (hdp->hd_dentry)
9905 +                       break;
9906 +       AuDebugOn(dinfo->di_btop > dinfo->di_bbot || dinfo->di_bbot < 0);
9907 +}
9908 +
9909 +void au_update_dbtop(struct dentry *dentry)
9910 +{
9911 +       aufs_bindex_t bindex, bbot;
9912 +       struct dentry *h_dentry;
9913 +
9914 +       bbot = au_dbbot(dentry);
9915 +       for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++) {
9916 +               h_dentry = au_h_dptr(dentry, bindex);
9917 +               if (!h_dentry)
9918 +                       continue;
9919 +               if (d_is_positive(h_dentry)) {
9920 +                       au_set_dbtop(dentry, bindex);
9921 +                       return;
9922 +               }
9923 +               au_set_h_dptr(dentry, bindex, NULL);
9924 +       }
9925 +}
9926 +
9927 +void au_update_dbbot(struct dentry *dentry)
9928 +{
9929 +       aufs_bindex_t bindex, btop;
9930 +       struct dentry *h_dentry;
9931 +
9932 +       btop = au_dbtop(dentry);
9933 +       for (bindex = au_dbbot(dentry); bindex >= btop; bindex--) {
9934 +               h_dentry = au_h_dptr(dentry, bindex);
9935 +               if (!h_dentry)
9936 +                       continue;
9937 +               if (d_is_positive(h_dentry)) {
9938 +                       au_set_dbbot(dentry, bindex);
9939 +                       return;
9940 +               }
9941 +               au_set_h_dptr(dentry, bindex, NULL);
9942 +       }
9943 +}
9944 +
9945 +int au_find_dbindex(struct dentry *dentry, struct dentry *h_dentry)
9946 +{
9947 +       aufs_bindex_t bindex, bbot;
9948 +
9949 +       bbot = au_dbbot(dentry);
9950 +       for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++)
9951 +               if (au_h_dptr(dentry, bindex) == h_dentry)
9952 +                       return bindex;
9953 +       return -1;
9954 +}
9955 diff -urN /usr/share/empty/fs/aufs/dir.c linux/fs/aufs/dir.c
9956 --- /usr/share/empty/fs/aufs/dir.c      1970-01-01 01:00:00.000000000 +0100
9957 +++ linux/fs/aufs/dir.c 2022-03-21 14:49:05.719966344 +0100
9958 @@ -0,0 +1,765 @@
9959 +// SPDX-License-Identifier: GPL-2.0
9960 +/*
9961 + * Copyright (C) 2005-2021 Junjiro R. Okajima
9962 + *
9963 + * This program, aufs is free software; you can redistribute it and/or modify
9964 + * it under the terms of the GNU General Public License as published by
9965 + * the Free Software Foundation; either version 2 of the License, or
9966 + * (at your option) any later version.
9967 + *
9968 + * This program is distributed in the hope that it will be useful,
9969 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
9970 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
9971 + * GNU General Public License for more details.
9972 + *
9973 + * You should have received a copy of the GNU General Public License
9974 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
9975 + */
9976 +
9977 +/*
9978 + * directory operations
9979 + */
9980 +
9981 +#include <linux/fs_stack.h>
9982 +#include <linux/iversion.h>
9983 +#include "aufs.h"
9984 +
9985 +void au_add_nlink(struct inode *dir, struct inode *h_dir)
9986 +{
9987 +       unsigned int nlink;
9988 +
9989 +       AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
9990 +
9991 +       nlink = dir->i_nlink;
9992 +       nlink += h_dir->i_nlink - 2;
9993 +       if (h_dir->i_nlink < 2)
9994 +               nlink += 2;
9995 +       smp_mb(); /* for i_nlink */
9996 +       /* 0 can happen in revaliding */
9997 +       set_nlink(dir, nlink);
9998 +}
9999 +
10000 +void au_sub_nlink(struct inode *dir, struct inode *h_dir)
10001 +{
10002 +       unsigned int nlink;
10003 +
10004 +       AuDebugOn(!S_ISDIR(dir->i_mode) || !S_ISDIR(h_dir->i_mode));
10005 +
10006 +       nlink = dir->i_nlink;
10007 +       nlink -= h_dir->i_nlink - 2;
10008 +       if (h_dir->i_nlink < 2)
10009 +               nlink -= 2;
10010 +       smp_mb(); /* for i_nlink */
10011 +       /* nlink == 0 means the branch-fs is broken */
10012 +       set_nlink(dir, nlink);
10013 +}
10014 +
10015 +loff_t au_dir_size(struct file *file, struct dentry *dentry)
10016 +{
10017 +       loff_t sz;
10018 +       aufs_bindex_t bindex, bbot;
10019 +       struct file *h_file;
10020 +       struct dentry *h_dentry;
10021 +
10022 +       sz = 0;
10023 +       if (file) {
10024 +               AuDebugOn(!d_is_dir(file->f_path.dentry));
10025 +
10026 +               bbot = au_fbbot_dir(file);
10027 +               for (bindex = au_fbtop(file);
10028 +                    bindex <= bbot && sz < KMALLOC_MAX_SIZE;
10029 +                    bindex++) {
10030 +                       h_file = au_hf_dir(file, bindex);
10031 +                       if (h_file && file_inode(h_file))
10032 +                               sz += vfsub_f_size_read(h_file);
10033 +               }
10034 +       } else {
10035 +               AuDebugOn(!dentry);
10036 +               AuDebugOn(!d_is_dir(dentry));
10037 +
10038 +               bbot = au_dbtaildir(dentry);
10039 +               for (bindex = au_dbtop(dentry);
10040 +                    bindex <= bbot && sz < KMALLOC_MAX_SIZE;
10041 +                    bindex++) {
10042 +                       h_dentry = au_h_dptr(dentry, bindex);
10043 +                       if (h_dentry && d_is_positive(h_dentry))
10044 +                               sz += i_size_read(d_inode(h_dentry));
10045 +               }
10046 +       }
10047 +       if (sz < KMALLOC_MAX_SIZE)
10048 +               sz = roundup_pow_of_two(sz);
10049 +       if (sz > KMALLOC_MAX_SIZE)
10050 +               sz = KMALLOC_MAX_SIZE;
10051 +       else if (sz < NAME_MAX) {
10052 +               BUILD_BUG_ON(AUFS_RDBLK_DEF < NAME_MAX);
10053 +               sz = AUFS_RDBLK_DEF;
10054 +       }
10055 +       return sz;
10056 +}
10057 +
10058 +struct au_dir_ts_arg {
10059 +       struct dentry *dentry;
10060 +       aufs_bindex_t brid;
10061 +};
10062 +
10063 +static void au_do_dir_ts(void *arg)
10064 +{
10065 +       struct au_dir_ts_arg *a = arg;
10066 +       struct au_dtime dt;
10067 +       struct path h_path;
10068 +       struct inode *dir, *h_dir;
10069 +       struct super_block *sb;
10070 +       struct au_branch *br;
10071 +       struct au_hinode *hdir;
10072 +       int err;
10073 +       aufs_bindex_t btop, bindex;
10074 +
10075 +       sb = a->dentry->d_sb;
10076 +       if (d_really_is_negative(a->dentry))
10077 +               goto out;
10078 +       /* no dir->i_mutex lock */
10079 +       aufs_read_lock(a->dentry, AuLock_DW); /* noflush */
10080 +
10081 +       dir = d_inode(a->dentry);
10082 +       btop = au_ibtop(dir);
10083 +       bindex = au_br_index(sb, a->brid);
10084 +       if (bindex < btop)
10085 +               goto out_unlock;
10086 +
10087 +       br = au_sbr(sb, bindex);
10088 +       h_path.dentry = au_h_dptr(a->dentry, bindex);
10089 +       if (!h_path.dentry)
10090 +               goto out_unlock;
10091 +       h_path.mnt = au_br_mnt(br);
10092 +       au_dtime_store(&dt, a->dentry, &h_path);
10093 +
10094 +       br = au_sbr(sb, btop);
10095 +       if (!au_br_writable(br->br_perm))
10096 +               goto out_unlock;
10097 +       h_path.dentry = au_h_dptr(a->dentry, btop);
10098 +       h_path.mnt = au_br_mnt(br);
10099 +       err = vfsub_mnt_want_write(h_path.mnt);
10100 +       if (err)
10101 +               goto out_unlock;
10102 +       hdir = au_hi(dir, btop);
10103 +       au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
10104 +       h_dir = au_h_iptr(dir, btop);
10105 +       if (h_dir->i_nlink
10106 +           && timespec64_compare(&h_dir->i_mtime, &dt.dt_mtime) < 0) {
10107 +               dt.dt_h_path = h_path;
10108 +               au_dtime_revert(&dt);
10109 +       }
10110 +       au_hn_inode_unlock(hdir);
10111 +       vfsub_mnt_drop_write(h_path.mnt);
10112 +       au_cpup_attr_timesizes(dir);
10113 +
10114 +out_unlock:
10115 +       aufs_read_unlock(a->dentry, AuLock_DW);
10116 +out:
10117 +       dput(a->dentry);
10118 +       au_nwt_done(&au_sbi(sb)->si_nowait);
10119 +       au_kfree_try_rcu(arg);
10120 +}
10121 +
10122 +void au_dir_ts(struct inode *dir, aufs_bindex_t bindex)
10123 +{
10124 +       int perm, wkq_err;
10125 +       aufs_bindex_t btop;
10126 +       struct au_dir_ts_arg *arg;
10127 +       struct dentry *dentry;
10128 +       struct super_block *sb;
10129 +
10130 +       IMustLock(dir);
10131 +
10132 +       dentry = d_find_any_alias(dir);
10133 +       AuDebugOn(!dentry);
10134 +       sb = dentry->d_sb;
10135 +       btop = au_ibtop(dir);
10136 +       if (btop == bindex) {
10137 +               au_cpup_attr_timesizes(dir);
10138 +               goto out;
10139 +       }
10140 +
10141 +       perm = au_sbr_perm(sb, btop);
10142 +       if (!au_br_writable(perm))
10143 +               goto out;
10144 +
10145 +       arg = kmalloc(sizeof(*arg), GFP_NOFS);
10146 +       if (!arg)
10147 +               goto out;
10148 +
10149 +       arg->dentry = dget(dentry); /* will be dput-ted by au_do_dir_ts() */
10150 +       arg->brid = au_sbr_id(sb, bindex);
10151 +       wkq_err = au_wkq_nowait(au_do_dir_ts, arg, sb, /*flags*/0);
10152 +       if (unlikely(wkq_err)) {
10153 +               pr_err("wkq %d\n", wkq_err);
10154 +               dput(dentry);
10155 +               au_kfree_try_rcu(arg);
10156 +       }
10157 +
10158 +out:
10159 +       dput(dentry);
10160 +}
10161 +
10162 +/* ---------------------------------------------------------------------- */
10163 +
10164 +static int reopen_dir(struct file *file)
10165 +{
10166 +       int err;
10167 +       unsigned int flags;
10168 +       aufs_bindex_t bindex, btail, btop;
10169 +       struct dentry *dentry, *h_dentry;
10170 +       struct file *h_file;
10171 +
10172 +       /* open all lower dirs */
10173 +       dentry = file->f_path.dentry;
10174 +       btop = au_dbtop(dentry);
10175 +       for (bindex = au_fbtop(file); bindex < btop; bindex++)
10176 +               au_set_h_fptr(file, bindex, NULL);
10177 +       au_set_fbtop(file, btop);
10178 +
10179 +       btail = au_dbtaildir(dentry);
10180 +       for (bindex = au_fbbot_dir(file); btail < bindex; bindex--)
10181 +               au_set_h_fptr(file, bindex, NULL);
10182 +       au_set_fbbot_dir(file, btail);
10183 +
10184 +       flags = vfsub_file_flags(file);
10185 +       for (bindex = btop; bindex <= btail; bindex++) {
10186 +               h_dentry = au_h_dptr(dentry, bindex);
10187 +               if (!h_dentry)
10188 +                       continue;
10189 +               h_file = au_hf_dir(file, bindex);
10190 +               if (h_file)
10191 +                       continue;
10192 +
10193 +               h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
10194 +               err = PTR_ERR(h_file);
10195 +               if (IS_ERR(h_file))
10196 +                       goto out; /* close all? */
10197 +               au_set_h_fptr(file, bindex, h_file);
10198 +       }
10199 +       au_update_figen(file);
10200 +       /* todo: necessary? */
10201 +       /* file->f_ra = h_file->f_ra; */
10202 +       err = 0;
10203 +
10204 +out:
10205 +       return err;
10206 +}
10207 +
10208 +static int do_open_dir(struct file *file, int flags, struct file *h_file)
10209 +{
10210 +       int err;
10211 +       aufs_bindex_t bindex, btail;
10212 +       struct dentry *dentry, *h_dentry;
10213 +       struct vfsmount *mnt;
10214 +
10215 +       FiMustWriteLock(file);
10216 +       AuDebugOn(h_file);
10217 +
10218 +       err = 0;
10219 +       mnt = file->f_path.mnt;
10220 +       dentry = file->f_path.dentry;
10221 +       file->f_version = inode_query_iversion(d_inode(dentry));
10222 +       bindex = au_dbtop(dentry);
10223 +       au_set_fbtop(file, bindex);
10224 +       btail = au_dbtaildir(dentry);
10225 +       au_set_fbbot_dir(file, btail);
10226 +       for (; !err && bindex <= btail; bindex++) {
10227 +               h_dentry = au_h_dptr(dentry, bindex);
10228 +               if (!h_dentry)
10229 +                       continue;
10230 +
10231 +               err = vfsub_test_mntns(mnt, h_dentry->d_sb);
10232 +               if (unlikely(err))
10233 +                       break;
10234 +               h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
10235 +               if (IS_ERR(h_file)) {
10236 +                       err = PTR_ERR(h_file);
10237 +                       break;
10238 +               }
10239 +               au_set_h_fptr(file, bindex, h_file);
10240 +       }
10241 +       au_update_figen(file);
10242 +       /* todo: necessary? */
10243 +       /* file->f_ra = h_file->f_ra; */
10244 +       if (!err)
10245 +               return 0; /* success */
10246 +
10247 +       /* close all */
10248 +       for (bindex = au_fbtop(file); bindex <= btail; bindex++)
10249 +               au_set_h_fptr(file, bindex, NULL);
10250 +       au_set_fbtop(file, -1);
10251 +       au_set_fbbot_dir(file, -1);
10252 +
10253 +       return err;
10254 +}
10255 +
10256 +static int aufs_open_dir(struct inode *inode __maybe_unused,
10257 +                        struct file *file)
10258 +{
10259 +       int err;
10260 +       struct super_block *sb;
10261 +       struct au_fidir *fidir;
10262 +
10263 +       err = -ENOMEM;
10264 +       sb = file->f_path.dentry->d_sb;
10265 +       si_read_lock(sb, AuLock_FLUSH);
10266 +       fidir = au_fidir_alloc(sb);
10267 +       if (fidir) {
10268 +               struct au_do_open_args args = {
10269 +                       .open   = do_open_dir,
10270 +                       .fidir  = fidir
10271 +               };
10272 +               err = au_do_open(file, &args);
10273 +               if (unlikely(err))
10274 +                       au_kfree_rcu(fidir);
10275 +       }
10276 +       si_read_unlock(sb);
10277 +       return err;
10278 +}
10279 +
10280 +static int aufs_release_dir(struct inode *inode __maybe_unused,
10281 +                           struct file *file)
10282 +{
10283 +       struct au_vdir *vdir_cache;
10284 +       struct au_finfo *finfo;
10285 +       struct au_fidir *fidir;
10286 +       struct au_hfile *hf;
10287 +       aufs_bindex_t bindex, bbot;
10288 +
10289 +       finfo = au_fi(file);
10290 +       fidir = finfo->fi_hdir;
10291 +       if (fidir) {
10292 +               au_hbl_del(&finfo->fi_hlist,
10293 +                          &au_sbi(file->f_path.dentry->d_sb)->si_files);
10294 +               vdir_cache = fidir->fd_vdir_cache; /* lock-free */
10295 +               if (vdir_cache)
10296 +                       au_vdir_free(vdir_cache);
10297 +
10298 +               bindex = finfo->fi_btop;
10299 +               if (bindex >= 0) {
10300 +                       hf = fidir->fd_hfile + bindex;
10301 +                       /*
10302 +                        * calls fput() instead of filp_close(),
10303 +                        * since no dnotify or lock for the lower file.
10304 +                        */
10305 +                       bbot = fidir->fd_bbot;
10306 +                       for (; bindex <= bbot; bindex++, hf++)
10307 +                               if (hf->hf_file)
10308 +                                       au_hfput(hf, /*execed*/0);
10309 +               }
10310 +               au_kfree_rcu(fidir);
10311 +               finfo->fi_hdir = NULL;
10312 +       }
10313 +       au_finfo_fin(file);
10314 +       return 0;
10315 +}
10316 +
10317 +/* ---------------------------------------------------------------------- */
10318 +
10319 +static int au_do_flush_dir(struct file *file, fl_owner_t id)
10320 +{
10321 +       int err;
10322 +       aufs_bindex_t bindex, bbot;
10323 +       struct file *h_file;
10324 +
10325 +       err = 0;
10326 +       bbot = au_fbbot_dir(file);
10327 +       for (bindex = au_fbtop(file); !err && bindex <= bbot; bindex++) {
10328 +               h_file = au_hf_dir(file, bindex);
10329 +               if (h_file)
10330 +                       err = vfsub_flush(h_file, id);
10331 +       }
10332 +       return err;
10333 +}
10334 +
10335 +static int aufs_flush_dir(struct file *file, fl_owner_t id)
10336 +{
10337 +       return au_do_flush(file, id, au_do_flush_dir);
10338 +}
10339 +
10340 +/* ---------------------------------------------------------------------- */
10341 +
10342 +static int au_do_fsync_dir_no_file(struct dentry *dentry, int datasync)
10343 +{
10344 +       int err;
10345 +       aufs_bindex_t bbot, bindex;
10346 +       struct inode *inode;
10347 +       struct super_block *sb;
10348 +
10349 +       err = 0;
10350 +       sb = dentry->d_sb;
10351 +       inode = d_inode(dentry);
10352 +       IMustLock(inode);
10353 +       bbot = au_dbbot(dentry);
10354 +       for (bindex = au_dbtop(dentry); !err && bindex <= bbot; bindex++) {
10355 +               struct path h_path;
10356 +
10357 +               if (au_test_ro(sb, bindex, inode))
10358 +                       continue;
10359 +               h_path.dentry = au_h_dptr(dentry, bindex);
10360 +               if (!h_path.dentry)
10361 +                       continue;
10362 +
10363 +               h_path.mnt = au_sbr_mnt(sb, bindex);
10364 +               err = vfsub_fsync(NULL, &h_path, datasync);
10365 +       }
10366 +
10367 +       return err;
10368 +}
10369 +
10370 +static int au_do_fsync_dir(struct file *file, int datasync)
10371 +{
10372 +       int err;
10373 +       aufs_bindex_t bbot, bindex;
10374 +       struct file *h_file;
10375 +       struct super_block *sb;
10376 +       struct inode *inode;
10377 +
10378 +       err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1, /*fi_lsc*/0);
10379 +       if (unlikely(err))
10380 +               goto out;
10381 +
10382 +       inode = file_inode(file);
10383 +       sb = inode->i_sb;
10384 +       bbot = au_fbbot_dir(file);
10385 +       for (bindex = au_fbtop(file); !err && bindex <= bbot; bindex++) {
10386 +               h_file = au_hf_dir(file, bindex);
10387 +               if (!h_file || au_test_ro(sb, bindex, inode))
10388 +                       continue;
10389 +
10390 +               err = vfsub_fsync(h_file, &h_file->f_path, datasync);
10391 +       }
10392 +
10393 +out:
10394 +       return err;
10395 +}
10396 +
10397 +/*
10398 + * @file may be NULL
10399 + */
10400 +static int aufs_fsync_dir(struct file *file, loff_t start, loff_t end,
10401 +                         int datasync)
10402 +{
10403 +       int err;
10404 +       struct dentry *dentry;
10405 +       struct inode *inode;
10406 +       struct super_block *sb;
10407 +
10408 +       err = 0;
10409 +       dentry = file->f_path.dentry;
10410 +       inode = d_inode(dentry);
10411 +       inode_lock(inode);
10412 +       sb = dentry->d_sb;
10413 +       si_noflush_read_lock(sb);
10414 +       if (file)
10415 +               err = au_do_fsync_dir(file, datasync);
10416 +       else {
10417 +               di_write_lock_child(dentry);
10418 +               err = au_do_fsync_dir_no_file(dentry, datasync);
10419 +       }
10420 +       au_cpup_attr_timesizes(inode);
10421 +       di_write_unlock(dentry);
10422 +       if (file)
10423 +               fi_write_unlock(file);
10424 +
10425 +       si_read_unlock(sb);
10426 +       inode_unlock(inode);
10427 +       return err;
10428 +}
10429 +
10430 +/* ---------------------------------------------------------------------- */
10431 +
10432 +static int aufs_iterate_shared(struct file *file, struct dir_context *ctx)
10433 +{
10434 +       int err;
10435 +       struct dentry *dentry;
10436 +       struct inode *inode, *h_inode;
10437 +       struct super_block *sb;
10438 +
10439 +       AuDbg("%pD, ctx{%ps, %llu}\n", file, ctx->actor, ctx->pos);
10440 +
10441 +       dentry = file->f_path.dentry;
10442 +       inode = d_inode(dentry);
10443 +       IMustLock(inode);
10444 +
10445 +       sb = dentry->d_sb;
10446 +       si_read_lock(sb, AuLock_FLUSH);
10447 +       err = au_reval_and_lock_fdi(file, reopen_dir, /*wlock*/1, /*fi_lsc*/0);
10448 +       if (unlikely(err))
10449 +               goto out;
10450 +       err = au_alive_dir(dentry);
10451 +       if (!err)
10452 +               err = au_vdir_init(file);
10453 +       di_downgrade_lock(dentry, AuLock_IR);
10454 +       if (unlikely(err))
10455 +               goto out_unlock;
10456 +
10457 +       h_inode = au_h_iptr(inode, au_ibtop(inode));
10458 +       if (!au_test_nfsd()) {
10459 +               err = au_vdir_fill_de(file, ctx);
10460 +               fsstack_copy_attr_atime(inode, h_inode);
10461 +       } else {
10462 +               /*
10463 +                * nfsd filldir may call lookup_one_len(), vfs_getattr(),
10464 +                * encode_fh() and others.
10465 +                */
10466 +               atomic_inc(&h_inode->i_count);
10467 +               di_read_unlock(dentry, AuLock_IR);
10468 +               si_read_unlock(sb);
10469 +               err = au_vdir_fill_de(file, ctx);
10470 +               fsstack_copy_attr_atime(inode, h_inode);
10471 +               fi_write_unlock(file);
10472 +               iput(h_inode);
10473 +
10474 +               AuTraceErr(err);
10475 +               return err;
10476 +       }
10477 +
10478 +out_unlock:
10479 +       di_read_unlock(dentry, AuLock_IR);
10480 +       fi_write_unlock(file);
10481 +out:
10482 +       si_read_unlock(sb);
10483 +       return err;
10484 +}
10485 +
10486 +/* ---------------------------------------------------------------------- */
10487 +
10488 +#define AuTestEmpty_WHONLY     1
10489 +#define AuTestEmpty_CALLED     (1 << 1)
10490 +#define AuTestEmpty_SHWH       (1 << 2)
10491 +#define au_ftest_testempty(flags, name)        ((flags) & AuTestEmpty_##name)
10492 +#define au_fset_testempty(flags, name) \
10493 +       do { (flags) |= AuTestEmpty_##name; } while (0)
10494 +#define au_fclr_testempty(flags, name) \
10495 +       do { (flags) &= ~AuTestEmpty_##name; } while (0)
10496 +
10497 +#ifndef CONFIG_AUFS_SHWH
10498 +#undef AuTestEmpty_SHWH
10499 +#define AuTestEmpty_SHWH       0
10500 +#endif
10501 +
10502 +struct test_empty_arg {
10503 +       struct dir_context ctx;
10504 +       struct au_nhash *whlist;
10505 +       unsigned int flags;
10506 +       int err;
10507 +       aufs_bindex_t bindex;
10508 +};
10509 +
10510 +static int test_empty_cb(struct dir_context *ctx, const char *__name,
10511 +                        int namelen, loff_t offset __maybe_unused, u64 ino,
10512 +                        unsigned int d_type)
10513 +{
10514 +       struct test_empty_arg *arg = container_of(ctx, struct test_empty_arg,
10515 +                                                 ctx);
10516 +       char *name = (void *)__name;
10517 +
10518 +       arg->err = 0;
10519 +       au_fset_testempty(arg->flags, CALLED);
10520 +       /* smp_mb(); */
10521 +       if (name[0] == '.'
10522 +           && (namelen == 1 || (name[1] == '.' && namelen == 2)))
10523 +               goto out; /* success */
10524 +
10525 +       if (namelen <= AUFS_WH_PFX_LEN
10526 +           || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
10527 +               if (au_ftest_testempty(arg->flags, WHONLY)
10528 +                   && !au_nhash_test_known_wh(arg->whlist, name, namelen))
10529 +                       arg->err = -ENOTEMPTY;
10530 +               goto out;
10531 +       }
10532 +
10533 +       name += AUFS_WH_PFX_LEN;
10534 +       namelen -= AUFS_WH_PFX_LEN;
10535 +       if (!au_nhash_test_known_wh(arg->whlist, name, namelen))
10536 +               arg->err = au_nhash_append_wh
10537 +                       (arg->whlist, name, namelen, ino, d_type, arg->bindex,
10538 +                        au_ftest_testempty(arg->flags, SHWH));
10539 +
10540 +out:
10541 +       /* smp_mb(); */
10542 +       AuTraceErr(arg->err);
10543 +       return arg->err;
10544 +}
10545 +
10546 +static int do_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
10547 +{
10548 +       int err;
10549 +       struct file *h_file;
10550 +       struct au_branch *br;
10551 +
10552 +       h_file = au_h_open(dentry, arg->bindex,
10553 +                          O_RDONLY | O_NONBLOCK | O_DIRECTORY | O_LARGEFILE,
10554 +                          /*file*/NULL, /*force_wr*/0);
10555 +       err = PTR_ERR(h_file);
10556 +       if (IS_ERR(h_file))
10557 +               goto out;
10558 +
10559 +       err = 0;
10560 +       if (!au_opt_test(au_mntflags(dentry->d_sb), UDBA_NONE)
10561 +           && !file_inode(h_file)->i_nlink)
10562 +               goto out_put;
10563 +
10564 +       do {
10565 +               arg->err = 0;
10566 +               au_fclr_testempty(arg->flags, CALLED);
10567 +               /* smp_mb(); */
10568 +               err = vfsub_iterate_dir(h_file, &arg->ctx);
10569 +               if (err >= 0)
10570 +                       err = arg->err;
10571 +       } while (!err && au_ftest_testempty(arg->flags, CALLED));
10572 +
10573 +out_put:
10574 +       fput(h_file);
10575 +       br = au_sbr(dentry->d_sb, arg->bindex);
10576 +       au_lcnt_dec(&br->br_nfiles);
10577 +out:
10578 +       return err;
10579 +}
10580 +
10581 +struct do_test_empty_args {
10582 +       int *errp;
10583 +       struct dentry *dentry;
10584 +       struct test_empty_arg *arg;
10585 +};
10586 +
10587 +static void call_do_test_empty(void *args)
10588 +{
10589 +       struct do_test_empty_args *a = args;
10590 +       *a->errp = do_test_empty(a->dentry, a->arg);
10591 +}
10592 +
10593 +static int sio_test_empty(struct dentry *dentry, struct test_empty_arg *arg)
10594 +{
10595 +       int err, wkq_err;
10596 +       struct dentry *h_dentry;
10597 +       struct inode *h_inode;
10598 +       struct user_namespace *h_userns;
10599 +
10600 +       h_userns = au_sbr_userns(dentry->d_sb, arg->bindex);
10601 +       h_dentry = au_h_dptr(dentry, arg->bindex);
10602 +       h_inode = d_inode(h_dentry);
10603 +       /* todo: i_mode changes anytime? */
10604 +       inode_lock_shared_nested(h_inode, AuLsc_I_CHILD);
10605 +       err = au_test_h_perm_sio(h_userns, h_inode, MAY_EXEC | MAY_READ);
10606 +       inode_unlock_shared(h_inode);
10607 +       if (!err)
10608 +               err = do_test_empty(dentry, arg);
10609 +       else {
10610 +               struct do_test_empty_args args = {
10611 +                       .errp   = &err,
10612 +                       .dentry = dentry,
10613 +                       .arg    = arg
10614 +               };
10615 +               unsigned int flags = arg->flags;
10616 +
10617 +               wkq_err = au_wkq_wait(call_do_test_empty, &args);
10618 +               if (unlikely(wkq_err))
10619 +                       err = wkq_err;
10620 +               arg->flags = flags;
10621 +       }
10622 +
10623 +       return err;
10624 +}
10625 +
10626 +int au_test_empty_lower(struct dentry *dentry)
10627 +{
10628 +       int err;
10629 +       unsigned int rdhash;
10630 +       aufs_bindex_t bindex, btop, btail;
10631 +       struct au_nhash whlist;
10632 +       struct test_empty_arg arg = {
10633 +               .ctx = {
10634 +                       .actor = test_empty_cb
10635 +               }
10636 +       };
10637 +       int (*test_empty)(struct dentry *dentry, struct test_empty_arg *arg);
10638 +
10639 +       SiMustAnyLock(dentry->d_sb);
10640 +
10641 +       rdhash = au_sbi(dentry->d_sb)->si_rdhash;
10642 +       if (!rdhash)
10643 +               rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, dentry));
10644 +       err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
10645 +       if (unlikely(err))
10646 +               goto out;
10647 +
10648 +       arg.flags = 0;
10649 +       arg.whlist = &whlist;
10650 +       btop = au_dbtop(dentry);
10651 +       if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
10652 +               au_fset_testempty(arg.flags, SHWH);
10653 +       test_empty = do_test_empty;
10654 +       if (au_opt_test(au_mntflags(dentry->d_sb), DIRPERM1))
10655 +               test_empty = sio_test_empty;
10656 +       arg.bindex = btop;
10657 +       err = test_empty(dentry, &arg);
10658 +       if (unlikely(err))
10659 +               goto out_whlist;
10660 +
10661 +       au_fset_testempty(arg.flags, WHONLY);
10662 +       btail = au_dbtaildir(dentry);
10663 +       for (bindex = btop + 1; !err && bindex <= btail; bindex++) {
10664 +               struct dentry *h_dentry;
10665 +
10666 +               h_dentry = au_h_dptr(dentry, bindex);
10667 +               if (h_dentry && d_is_positive(h_dentry)) {
10668 +                       arg.bindex = bindex;
10669 +                       err = test_empty(dentry, &arg);
10670 +               }
10671 +       }
10672 +
10673 +out_whlist:
10674 +       au_nhash_wh_free(&whlist);
10675 +out:
10676 +       return err;
10677 +}
10678 +
10679 +int au_test_empty(struct dentry *dentry, struct au_nhash *whlist)
10680 +{
10681 +       int err;
10682 +       struct test_empty_arg arg = {
10683 +               .ctx = {
10684 +                       .actor = test_empty_cb
10685 +               }
10686 +       };
10687 +       aufs_bindex_t bindex, btail;
10688 +
10689 +       err = 0;
10690 +       arg.whlist = whlist;
10691 +       arg.flags = AuTestEmpty_WHONLY;
10692 +       if (au_opt_test(au_mntflags(dentry->d_sb), SHWH))
10693 +               au_fset_testempty(arg.flags, SHWH);
10694 +       btail = au_dbtaildir(dentry);
10695 +       for (bindex = au_dbtop(dentry); !err && bindex <= btail; bindex++) {
10696 +               struct dentry *h_dentry;
10697 +
10698 +               h_dentry = au_h_dptr(dentry, bindex);
10699 +               if (h_dentry && d_is_positive(h_dentry)) {
10700 +                       arg.bindex = bindex;
10701 +                       err = sio_test_empty(dentry, &arg);
10702 +               }
10703 +       }
10704 +
10705 +       return err;
10706 +}
10707 +
10708 +/* ---------------------------------------------------------------------- */
10709 +
10710 +const struct file_operations aufs_dir_fop = {
10711 +       .owner          = THIS_MODULE,
10712 +       .llseek         = default_llseek,
10713 +       .read           = generic_read_dir,
10714 +       .iterate_shared = aufs_iterate_shared,
10715 +       .unlocked_ioctl = aufs_ioctl_dir,
10716 +#ifdef CONFIG_COMPAT
10717 +       .compat_ioctl   = aufs_compat_ioctl_dir,
10718 +#endif
10719 +       .open           = aufs_open_dir,
10720 +       .release        = aufs_release_dir,
10721 +       .flush          = aufs_flush_dir,
10722 +       .fsync          = aufs_fsync_dir
10723 +};
10724 diff -urN /usr/share/empty/fs/aufs/dir.h linux/fs/aufs/dir.h
10725 --- /usr/share/empty/fs/aufs/dir.h      1970-01-01 01:00:00.000000000 +0100
10726 +++ linux/fs/aufs/dir.h 2022-03-21 14:49:05.719966344 +0100
10727 @@ -0,0 +1,134 @@
10728 +/* SPDX-License-Identifier: GPL-2.0 */
10729 +/*
10730 + * Copyright (C) 2005-2021 Junjiro R. Okajima
10731 + *
10732 + * This program, aufs is free software; you can redistribute it and/or modify
10733 + * it under the terms of the GNU General Public License as published by
10734 + * the Free Software Foundation; either version 2 of the License, or
10735 + * (at your option) any later version.
10736 + *
10737 + * This program is distributed in the hope that it will be useful,
10738 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
10739 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10740 + * GNU General Public License for more details.
10741 + *
10742 + * You should have received a copy of the GNU General Public License
10743 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
10744 + */
10745 +
10746 +/*
10747 + * directory operations
10748 + */
10749 +
10750 +#ifndef __AUFS_DIR_H__
10751 +#define __AUFS_DIR_H__
10752 +
10753 +#ifdef __KERNEL__
10754 +
10755 +#include <linux/fs.h>
10756 +
10757 +/* ---------------------------------------------------------------------- */
10758 +
10759 +/* need to be faster and smaller */
10760 +
10761 +struct au_nhash {
10762 +       unsigned int            nh_num;
10763 +       struct hlist_head       *nh_head;
10764 +};
10765 +
10766 +struct au_vdir_destr {
10767 +       unsigned char   len;
10768 +       unsigned char   name[];
10769 +} __packed;
10770 +
10771 +struct au_vdir_dehstr {
10772 +       struct hlist_node       hash;
10773 +       struct au_vdir_destr    *str;
10774 +       struct rcu_head         rcu;
10775 +} ____cacheline_aligned_in_smp;
10776 +
10777 +struct au_vdir_de {
10778 +       ino_t                   de_ino;
10779 +       unsigned char           de_type;
10780 +       /* caution: packed */
10781 +       struct au_vdir_destr    de_str;
10782 +} __packed;
10783 +
10784 +struct au_vdir_wh {
10785 +       struct hlist_node       wh_hash;
10786 +#ifdef CONFIG_AUFS_SHWH
10787 +       ino_t                   wh_ino;
10788 +       aufs_bindex_t           wh_bindex;
10789 +       unsigned char           wh_type;
10790 +#else
10791 +       aufs_bindex_t           wh_bindex;
10792 +#endif
10793 +       /* caution: packed */
10794 +       struct au_vdir_destr    wh_str;
10795 +} __packed;
10796 +
10797 +union au_vdir_deblk_p {
10798 +       unsigned char           *deblk;
10799 +       struct au_vdir_de       *de;
10800 +};
10801 +
10802 +struct au_vdir {
10803 +       unsigned char   **vd_deblk;
10804 +       unsigned long   vd_nblk;
10805 +       struct {
10806 +               unsigned long           ul;
10807 +               union au_vdir_deblk_p   p;
10808 +       } vd_last;
10809 +
10810 +       u64             vd_version;
10811 +       unsigned int    vd_deblk_sz;
10812 +       unsigned long   vd_jiffy;
10813 +       struct rcu_head rcu;
10814 +} ____cacheline_aligned_in_smp;
10815 +
10816 +/* ---------------------------------------------------------------------- */
10817 +
10818 +/* dir.c */
10819 +extern const struct file_operations aufs_dir_fop;
10820 +void au_add_nlink(struct inode *dir, struct inode *h_dir);
10821 +void au_sub_nlink(struct inode *dir, struct inode *h_dir);
10822 +loff_t au_dir_size(struct file *file, struct dentry *dentry);
10823 +void au_dir_ts(struct inode *dir, aufs_bindex_t bsrc);
10824 +int au_test_empty_lower(struct dentry *dentry);
10825 +int au_test_empty(struct dentry *dentry, struct au_nhash *whlist);
10826 +
10827 +/* vdir.c */
10828 +unsigned int au_rdhash_est(loff_t sz);
10829 +int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp);
10830 +void au_nhash_wh_free(struct au_nhash *whlist);
10831 +int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
10832 +                           int limit);
10833 +int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen);
10834 +int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
10835 +                      unsigned int d_type, aufs_bindex_t bindex,
10836 +                      unsigned char shwh);
10837 +void au_vdir_free(struct au_vdir *vdir);
10838 +int au_vdir_init(struct file *file);
10839 +int au_vdir_fill_de(struct file *file, struct dir_context *ctx);
10840 +
10841 +/* ioctl.c */
10842 +long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg);
10843 +
10844 +#ifdef CONFIG_AUFS_RDU
10845 +/* rdu.c */
10846 +long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
10847 +#ifdef CONFIG_COMPAT
10848 +long au_rdu_compat_ioctl(struct file *file, unsigned int cmd,
10849 +                        unsigned long arg);
10850 +#endif
10851 +#else
10852 +AuStub(long, au_rdu_ioctl, return -EINVAL, struct file *file,
10853 +       unsigned int cmd, unsigned long arg)
10854 +#ifdef CONFIG_COMPAT
10855 +AuStub(long, au_rdu_compat_ioctl, return -EINVAL, struct file *file,
10856 +       unsigned int cmd, unsigned long arg)
10857 +#endif
10858 +#endif
10859 +
10860 +#endif /* __KERNEL__ */
10861 +#endif /* __AUFS_DIR_H__ */
10862 diff -urN /usr/share/empty/fs/aufs/dirren.c linux/fs/aufs/dirren.c
10863 --- /usr/share/empty/fs/aufs/dirren.c   1970-01-01 01:00:00.000000000 +0100
10864 +++ linux/fs/aufs/dirren.c      2022-03-21 14:49:05.723299677 +0100
10865 @@ -0,0 +1,1315 @@
10866 +// SPDX-License-Identifier: GPL-2.0
10867 +/*
10868 + * Copyright (C) 2017-2021 Junjiro R. Okajima
10869 + *
10870 + * This program, aufs is free software; you can redistribute it and/or modify
10871 + * it under the terms of the GNU General Public License as published by
10872 + * the Free Software Foundation; either version 2 of the License, or
10873 + * (at your option) any later version.
10874 + *
10875 + * This program is distributed in the hope that it will be useful,
10876 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
10877 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10878 + * GNU General Public License for more details.
10879 + *
10880 + * You should have received a copy of the GNU General Public License
10881 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
10882 + */
10883 +
10884 +/*
10885 + * special handling in renaming a directory
10886 + * in order to support looking-up the before-renamed name on the lower readonly
10887 + * branches
10888 + */
10889 +
10890 +#include <linux/byteorder/generic.h>
10891 +#include "aufs.h"
10892 +
10893 +static void au_dr_hino_del(struct au_dr_br *dr, struct au_dr_hino *ent)
10894 +{
10895 +       int idx;
10896 +
10897 +       idx = au_dr_ihash(ent->dr_h_ino);
10898 +       au_hbl_del(&ent->dr_hnode, dr->dr_h_ino + idx);
10899 +}
10900 +
10901 +static int au_dr_hino_test_empty(struct au_dr_br *dr)
10902 +{
10903 +       int ret, i;
10904 +       struct hlist_bl_head *hbl;
10905 +
10906 +       ret = 1;
10907 +       for (i = 0; ret && i < AuDirren_NHASH; i++) {
10908 +               hbl = dr->dr_h_ino + i;
10909 +               hlist_bl_lock(hbl);
10910 +               ret &= hlist_bl_empty(hbl);
10911 +               hlist_bl_unlock(hbl);
10912 +       }
10913 +
10914 +       return ret;
10915 +}
10916 +
10917 +static struct au_dr_hino *au_dr_hino_find(struct au_dr_br *dr, ino_t ino)
10918 +{
10919 +       struct au_dr_hino *found, *ent;
10920 +       struct hlist_bl_head *hbl;
10921 +       struct hlist_bl_node *pos;
10922 +       int idx;
10923 +
10924 +       found = NULL;
10925 +       idx = au_dr_ihash(ino);
10926 +       hbl = dr->dr_h_ino + idx;
10927 +       hlist_bl_lock(hbl);
10928 +       hlist_bl_for_each_entry(ent, pos, hbl, dr_hnode)
10929 +               if (ent->dr_h_ino == ino) {
10930 +                       found = ent;
10931 +                       break;
10932 +               }
10933 +       hlist_bl_unlock(hbl);
10934 +
10935 +       return found;
10936 +}
10937 +
10938 +int au_dr_hino_test_add(struct au_dr_br *dr, ino_t ino,
10939 +                       struct au_dr_hino *add_ent)
10940 +{
10941 +       int found, idx;
10942 +       struct hlist_bl_head *hbl;
10943 +       struct hlist_bl_node *pos;
10944 +       struct au_dr_hino *ent;
10945 +
10946 +       found = 0;
10947 +       idx = au_dr_ihash(ino);
10948 +       hbl = dr->dr_h_ino + idx;
10949 +#if 0 /* debug print */
10950 +       {
10951 +               struct hlist_bl_node *tmp;
10952 +
10953 +               hlist_bl_for_each_entry_safe(ent, pos, tmp, hbl, dr_hnode)
10954 +                       AuDbg("hi%llu\n", (unsigned long long)ent->dr_h_ino);
10955 +       }
10956 +#endif
10957 +       hlist_bl_lock(hbl);
10958 +       hlist_bl_for_each_entry(ent, pos, hbl, dr_hnode)
10959 +               if (ent->dr_h_ino == ino) {
10960 +                       found = 1;
10961 +                       break;
10962 +               }
10963 +       if (!found && add_ent)
10964 +               hlist_bl_add_head(&add_ent->dr_hnode, hbl);
10965 +       hlist_bl_unlock(hbl);
10966 +
10967 +       if (!found && add_ent)
10968 +               AuDbg("i%llu added\n", (unsigned long long)add_ent->dr_h_ino);
10969 +
10970 +       return found;
10971 +}
10972 +
10973 +void au_dr_hino_free(struct au_dr_br *dr)
10974 +{
10975 +       int i;
10976 +       struct hlist_bl_head *hbl;
10977 +       struct hlist_bl_node *pos, *tmp;
10978 +       struct au_dr_hino *ent;
10979 +
10980 +       /* SiMustWriteLock(sb); */
10981 +
10982 +       for (i = 0; i < AuDirren_NHASH; i++) {
10983 +               hbl = dr->dr_h_ino + i;
10984 +               /* no spinlock since sbinfo must be write-locked */
10985 +               hlist_bl_for_each_entry_safe(ent, pos, tmp, hbl, dr_hnode)
10986 +                       au_kfree_rcu(ent);
10987 +               INIT_HLIST_BL_HEAD(hbl);
10988 +       }
10989 +}
10990 +
10991 +/* returns the number of inodes or an error */
10992 +static int au_dr_hino_store(struct super_block *sb, struct au_branch *br,
10993 +                           struct file *hinofile)
10994 +{
10995 +       int err, i;
10996 +       ssize_t ssz;
10997 +       loff_t pos, oldsize;
10998 +       __be64 u64;
10999 +       struct inode *hinoinode;
11000 +       struct hlist_bl_head *hbl;
11001 +       struct hlist_bl_node *n1, *n2;
11002 +       struct au_dr_hino *ent;
11003 +
11004 +       SiMustWriteLock(sb);
11005 +       AuDebugOn(!au_br_writable(br->br_perm));
11006 +
11007 +       hinoinode = file_inode(hinofile);
11008 +       oldsize = i_size_read(hinoinode);
11009 +
11010 +       err = 0;
11011 +       pos = 0;
11012 +       hbl = br->br_dirren.dr_h_ino;
11013 +       for (i = 0; !err && i < AuDirren_NHASH; i++, hbl++) {
11014 +               /* no bit-lock since sbinfo must be write-locked */
11015 +               hlist_bl_for_each_entry_safe(ent, n1, n2, hbl, dr_hnode) {
11016 +                       AuDbg("hi%llu, %pD2\n",
11017 +                             (unsigned long long)ent->dr_h_ino, hinofile);
11018 +                       u64 = cpu_to_be64(ent->dr_h_ino);
11019 +                       ssz = vfsub_write_k(hinofile, &u64, sizeof(u64), &pos);
11020 +                       if (ssz == sizeof(u64))
11021 +                               continue;
11022 +
11023 +                       /* write error */
11024 +                       pr_err("ssz %zd, %pD2\n", ssz, hinofile);
11025 +                       err = -ENOSPC;
11026 +                       if (ssz < 0)
11027 +                               err = ssz;
11028 +                       break;
11029 +               }
11030 +       }
11031 +       /* regardless the error */
11032 +       if (pos < oldsize) {
11033 +               err = vfsub_trunc(&hinofile->f_path, pos, /*attr*/0, hinofile);
11034 +               AuTraceErr(err);
11035 +       }
11036 +
11037 +       AuTraceErr(err);
11038 +       return err;
11039 +}
11040 +
11041 +static int au_dr_hino_load(struct au_dr_br *dr, struct file *hinofile)
11042 +{
11043 +       int err, hidx;
11044 +       ssize_t ssz;
11045 +       size_t sz, n;
11046 +       loff_t pos;
11047 +       uint64_t u64;
11048 +       struct au_dr_hino *ent;
11049 +       struct inode *hinoinode;
11050 +       struct hlist_bl_head *hbl;
11051 +
11052 +       err = 0;
11053 +       pos = 0;
11054 +       hbl = dr->dr_h_ino;
11055 +       hinoinode = file_inode(hinofile);
11056 +       sz = i_size_read(hinoinode);
11057 +       AuDebugOn(sz % sizeof(u64));
11058 +       n = sz / sizeof(u64);
11059 +       while (n--) {
11060 +               ssz = vfsub_read_k(hinofile, &u64, sizeof(u64), &pos);
11061 +               if (unlikely(ssz != sizeof(u64))) {
11062 +                       pr_err("ssz %zd, %pD2\n", ssz, hinofile);
11063 +                       err = -EINVAL;
11064 +                       if (ssz < 0)
11065 +                               err = ssz;
11066 +                       goto out_free;
11067 +               }
11068 +
11069 +               ent = kmalloc(sizeof(*ent), GFP_NOFS);
11070 +               if (!ent) {
11071 +                       err = -ENOMEM;
11072 +                       AuTraceErr(err);
11073 +                       goto out_free;
11074 +               }
11075 +               ent->dr_h_ino = be64_to_cpu((__force __be64)u64);
11076 +               AuDbg("hi%llu, %pD2\n",
11077 +                     (unsigned long long)ent->dr_h_ino, hinofile);
11078 +               hidx = au_dr_ihash(ent->dr_h_ino);
11079 +               au_hbl_add(&ent->dr_hnode, hbl + hidx);
11080 +       }
11081 +       goto out; /* success */
11082 +
11083 +out_free:
11084 +       au_dr_hino_free(dr);
11085 +out:
11086 +       AuTraceErr(err);
11087 +       return err;
11088 +}
11089 +
11090 +/*
11091 + * @bindex/@br is a switch to distinguish whether suspending hnotify or not.
11092 + * @path is a switch to distinguish load and store.
11093 + */
11094 +static int au_dr_hino(struct super_block *sb, aufs_bindex_t bindex,
11095 +                     struct au_branch *br, const struct path *path)
11096 +{
11097 +       int err, flags;
11098 +       unsigned char load, suspend;
11099 +       struct file *hinofile;
11100 +       struct au_hinode *hdir;
11101 +       struct inode *dir, *delegated;
11102 +       struct path hinopath;
11103 +       struct qstr hinoname = QSTR_INIT(AUFS_WH_DR_BRHINO,
11104 +                                        sizeof(AUFS_WH_DR_BRHINO) - 1);
11105 +
11106 +       AuDebugOn(bindex < 0 && !br);
11107 +       AuDebugOn(bindex >= 0 && br);
11108 +
11109 +       err = -EINVAL;
11110 +       suspend = !br;
11111 +       if (suspend)
11112 +               br = au_sbr(sb, bindex);
11113 +       load = !!path;
11114 +       if (!load) {
11115 +               path = &br->br_path;
11116 +               AuDebugOn(!au_br_writable(br->br_perm));
11117 +               if (unlikely(!au_br_writable(br->br_perm)))
11118 +                       goto out;
11119 +       }
11120 +
11121 +       hdir = NULL;
11122 +       if (suspend) {
11123 +               dir = d_inode(sb->s_root);
11124 +               hdir = au_hinode(au_ii(dir), bindex);
11125 +               dir = hdir->hi_inode;
11126 +               au_hn_inode_lock_nested(hdir, AuLsc_I_CHILD);
11127 +       } else {
11128 +               dir = d_inode(path->dentry);
11129 +               inode_lock_nested(dir, AuLsc_I_CHILD);
11130 +       }
11131 +       hinopath.mnt = path->mnt;
11132 +       hinopath.dentry = vfsub_lkup_one(&hinoname, (struct path *)path);
11133 +       err = PTR_ERR(hinopath.dentry);
11134 +       if (IS_ERR(hinopath.dentry))
11135 +               goto out_unlock;
11136 +
11137 +       err = 0;
11138 +       flags = O_RDONLY;
11139 +       if (load) {
11140 +               if (d_is_negative(hinopath.dentry))
11141 +                       goto out_dput; /* success */
11142 +       } else {
11143 +               if (au_dr_hino_test_empty(&br->br_dirren)) {
11144 +                       if (d_is_positive(hinopath.dentry)) {
11145 +                               delegated = NULL;
11146 +                               err = vfsub_unlink(dir, &hinopath, &delegated,
11147 +                                                  /*force*/0);
11148 +                               AuTraceErr(err);
11149 +                               if (unlikely(err))
11150 +                                       pr_err("ignored err %d, %pd2\n",
11151 +                                              err, hinopath.dentry);
11152 +                               if (unlikely(err == -EWOULDBLOCK))
11153 +                                       iput(delegated);
11154 +                               err = 0;
11155 +                       }
11156 +                       goto out_dput;
11157 +               } else if (!d_is_positive(hinopath.dentry)) {
11158 +                       err = vfsub_create(dir, &hinopath, 0600,
11159 +                                          /*want_excl*/false);
11160 +                       AuTraceErr(err);
11161 +                       if (unlikely(err))
11162 +                               goto out_dput;
11163 +               }
11164 +               flags = O_WRONLY;
11165 +       }
11166 +       hinofile = vfsub_dentry_open(&hinopath, flags);
11167 +       if (suspend)
11168 +               au_hn_inode_unlock(hdir);
11169 +       else
11170 +               inode_unlock(dir);
11171 +       dput(hinopath.dentry);
11172 +       AuTraceErrPtr(hinofile);
11173 +       if (IS_ERR(hinofile)) {
11174 +               err = PTR_ERR(hinofile);
11175 +               goto out;
11176 +       }
11177 +
11178 +       if (load)
11179 +               err = au_dr_hino_load(&br->br_dirren, hinofile);
11180 +       else
11181 +               err = au_dr_hino_store(sb, br, hinofile);
11182 +       fput(hinofile);
11183 +       goto out;
11184 +
11185 +out_dput:
11186 +       dput(hinopath.dentry);
11187 +out_unlock:
11188 +       if (suspend)
11189 +               au_hn_inode_unlock(hdir);
11190 +       else
11191 +               inode_unlock(dir);
11192 +out:
11193 +       AuTraceErr(err);
11194 +       return err;
11195 +}
11196 +
11197 +/* ---------------------------------------------------------------------- */
11198 +
11199 +static int au_dr_brid_init(struct au_dr_brid *brid, const struct path *path)
11200 +{
11201 +       int err;
11202 +       struct kstatfs kstfs;
11203 +       dev_t dev;
11204 +       struct dentry *dentry;
11205 +       struct super_block *sb;
11206 +
11207 +       err = vfs_statfs((void *)path, &kstfs);
11208 +       AuTraceErr(err);
11209 +       if (unlikely(err))
11210 +               goto out;
11211 +
11212 +       /* todo: support for UUID */
11213 +
11214 +       if (kstfs.f_fsid.val[0] || kstfs.f_fsid.val[1]) {
11215 +               brid->type = AuBrid_FSID;
11216 +               brid->fsid = kstfs.f_fsid;
11217 +       } else {
11218 +               dentry = path->dentry;
11219 +               sb = dentry->d_sb;
11220 +               dev = sb->s_dev;
11221 +               if (dev) {
11222 +                       brid->type = AuBrid_DEV;
11223 +                       brid->dev = dev;
11224 +               }
11225 +       }
11226 +
11227 +out:
11228 +       return err;
11229 +}
11230 +
11231 +int au_dr_br_init(struct super_block *sb, struct au_branch *br,
11232 +                 const struct path *path)
11233 +{
11234 +       int err, i;
11235 +       struct au_dr_br *dr;
11236 +       struct hlist_bl_head *hbl;
11237 +
11238 +       dr = &br->br_dirren;
11239 +       hbl = dr->dr_h_ino;
11240 +       for (i = 0; i < AuDirren_NHASH; i++, hbl++)
11241 +               INIT_HLIST_BL_HEAD(hbl);
11242 +
11243 +       err = au_dr_brid_init(&dr->dr_brid, path);
11244 +       if (unlikely(err))
11245 +               goto out;
11246 +
11247 +       if (au_opt_test(au_mntflags(sb), DIRREN))
11248 +               err = au_dr_hino(sb, /*bindex*/-1, br, path);
11249 +
11250 +out:
11251 +       AuTraceErr(err);
11252 +       return err;
11253 +}
11254 +
11255 +int au_dr_br_fin(struct super_block *sb, struct au_branch *br)
11256 +{
11257 +       int err;
11258 +
11259 +       err = 0;
11260 +       if (au_br_writable(br->br_perm))
11261 +               err = au_dr_hino(sb, /*bindex*/-1, br, /*path*/NULL);
11262 +       if (!err)
11263 +               au_dr_hino_free(&br->br_dirren);
11264 +
11265 +       return err;
11266 +}
11267 +
11268 +/* ---------------------------------------------------------------------- */
11269 +
11270 +static int au_brid_str(struct au_dr_brid *brid, struct inode *h_inode,
11271 +                      char *buf, size_t sz)
11272 +{
11273 +       int err;
11274 +       unsigned int major, minor;
11275 +       char *p;
11276 +
11277 +       p = buf;
11278 +       err = snprintf(p, sz, "%d_", brid->type);
11279 +       AuDebugOn(err > sz);
11280 +       p += err;
11281 +       sz -= err;
11282 +       switch (brid->type) {
11283 +       case AuBrid_Unset:
11284 +               return -EINVAL;
11285 +       case AuBrid_UUID:
11286 +               err = snprintf(p, sz, "%pU", brid->uuid.b);
11287 +               break;
11288 +       case AuBrid_FSID:
11289 +               err = snprintf(p, sz, "%08x-%08x",
11290 +                              brid->fsid.val[0], brid->fsid.val[1]);
11291 +               break;
11292 +       case AuBrid_DEV:
11293 +               major = MAJOR(brid->dev);
11294 +               minor = MINOR(brid->dev);
11295 +               if (major <= 0xff && minor <= 0xff)
11296 +                       err = snprintf(p, sz, "%02x%02x", major, minor);
11297 +               else
11298 +                       err = snprintf(p, sz, "%03x:%05x", major, minor);
11299 +               break;
11300 +       }
11301 +       AuDebugOn(err > sz);
11302 +       p += err;
11303 +       sz -= err;
11304 +       err = snprintf(p, sz, "_%llu", (unsigned long long)h_inode->i_ino);
11305 +       AuDebugOn(err > sz);
11306 +       p += err;
11307 +       sz -= err;
11308 +
11309 +       return p - buf;
11310 +}
11311 +
11312 +static int au_drinfo_name(struct au_branch *br, char *name, int len)
11313 +{
11314 +       int rlen;
11315 +       struct dentry *br_dentry;
11316 +       struct inode *br_inode;
11317 +
11318 +       br_dentry = au_br_dentry(br);
11319 +       br_inode = d_inode(br_dentry);
11320 +       rlen = au_brid_str(&br->br_dirren.dr_brid, br_inode, name, len);
11321 +       AuDebugOn(rlen >= AUFS_DIRREN_ENV_VAL_SZ);
11322 +       AuDebugOn(rlen > len);
11323 +
11324 +       return rlen;
11325 +}
11326 +
11327 +/* ---------------------------------------------------------------------- */
11328 +
11329 +/*
11330 + * from the given @h_dentry, construct drinfo at @*fdata.
11331 + * when the size of @*fdata is not enough, reallocate and return new @fdata and
11332 + * @allocated.
11333 + */
11334 +static int au_drinfo_construct(struct au_drinfo_fdata **fdata,
11335 +                              struct dentry *h_dentry,
11336 +                              unsigned char *allocated)
11337 +{
11338 +       int err, v;
11339 +       struct au_drinfo_fdata *f, *p;
11340 +       struct au_drinfo *drinfo;
11341 +       struct inode *h_inode;
11342 +       struct qstr *qname;
11343 +
11344 +       err = 0;
11345 +       f = *fdata;
11346 +       h_inode = d_inode(h_dentry);
11347 +       qname = &h_dentry->d_name;
11348 +       drinfo = &f->drinfo;
11349 +       drinfo->ino = (__force uint64_t)cpu_to_be64(h_inode->i_ino);
11350 +       drinfo->oldnamelen = qname->len;
11351 +       if (*allocated < sizeof(*f) + qname->len) {
11352 +               v = roundup_pow_of_two(*allocated + qname->len);
11353 +               p = au_krealloc(f, v, GFP_NOFS, /*may_shrink*/0);
11354 +               if (unlikely(!p)) {
11355 +                       err = -ENOMEM;
11356 +                       AuTraceErr(err);
11357 +                       goto out;
11358 +               }
11359 +               f = p;
11360 +               *fdata = f;
11361 +               *allocated = v;
11362 +               drinfo = &f->drinfo;
11363 +       }
11364 +       memcpy(drinfo->oldname, qname->name, qname->len);
11365 +       AuDbg("i%llu, %.*s\n",
11366 +             be64_to_cpu((__force __be64)drinfo->ino), drinfo->oldnamelen,
11367 +             drinfo->oldname);
11368 +
11369 +out:
11370 +       AuTraceErr(err);
11371 +       return err;
11372 +}
11373 +
11374 +/* callers have to free the return value */
11375 +static struct au_drinfo *au_drinfo_read_k(struct file *file, ino_t h_ino)
11376 +{
11377 +       struct au_drinfo *ret, *drinfo;
11378 +       struct au_drinfo_fdata fdata;
11379 +       int len;
11380 +       loff_t pos;
11381 +       ssize_t ssz;
11382 +
11383 +       ret = ERR_PTR(-EIO);
11384 +       pos = 0;
11385 +       ssz = vfsub_read_k(file, &fdata, sizeof(fdata), &pos);
11386 +       if (unlikely(ssz != sizeof(fdata))) {
11387 +               AuIOErr("ssz %zd, %u, %pD2\n",
11388 +                       ssz, (unsigned int)sizeof(fdata), file);
11389 +               goto out;
11390 +       }
11391 +
11392 +       fdata.magic = ntohl((__force __be32)fdata.magic);
11393 +       switch (fdata.magic) {
11394 +       case AUFS_DRINFO_MAGIC_V1:
11395 +               break;
11396 +       default:
11397 +               AuIOErr("magic-num 0x%x, 0x%x, %pD2\n",
11398 +                       fdata.magic, AUFS_DRINFO_MAGIC_V1, file);
11399 +               goto out;
11400 +       }
11401 +
11402 +       drinfo = &fdata.drinfo;
11403 +       len = drinfo->oldnamelen;
11404 +       if (!len) {
11405 +               AuIOErr("broken drinfo %pD2\n", file);
11406 +               goto out;
11407 +       }
11408 +
11409 +       ret = NULL;
11410 +       drinfo->ino = be64_to_cpu((__force __be64)drinfo->ino);
11411 +       if (unlikely(h_ino && drinfo->ino != h_ino)) {
11412 +               AuDbg("ignored i%llu, i%llu, %pD2\n",
11413 +                     (unsigned long long)drinfo->ino,
11414 +                     (unsigned long long)h_ino, file);
11415 +               goto out; /* success */
11416 +       }
11417 +
11418 +       ret = kmalloc(sizeof(*ret) + len, GFP_NOFS);
11419 +       if (unlikely(!ret)) {
11420 +               ret = ERR_PTR(-ENOMEM);
11421 +               AuTraceErrPtr(ret);
11422 +               goto out;
11423 +       }
11424 +
11425 +       *ret = *drinfo;
11426 +       ssz = vfsub_read_k(file, (void *)ret->oldname, len, &pos);
11427 +       if (unlikely(ssz != len)) {
11428 +               au_kfree_rcu(ret);
11429 +               ret = ERR_PTR(-EIO);
11430 +               AuIOErr("ssz %zd, %u, %pD2\n", ssz, len, file);
11431 +               goto out;
11432 +       }
11433 +
11434 +       AuDbg("oldname %.*s\n", ret->oldnamelen, ret->oldname);
11435 +
11436 +out:
11437 +       return ret;
11438 +}
11439 +
11440 +/* ---------------------------------------------------------------------- */
11441 +
11442 +/* in order to be revertible */
11443 +struct au_drinfo_rev_elm {
11444 +       int                     created;
11445 +       struct dentry           *info_dentry;
11446 +       struct au_drinfo        *info_last;
11447 +};
11448 +
11449 +struct au_drinfo_rev {
11450 +       unsigned char                   already;
11451 +       aufs_bindex_t                   nelm;
11452 +       struct au_drinfo_rev_elm        elm[];
11453 +};
11454 +
11455 +/* todo: isn't it too large? */
11456 +struct au_drinfo_store {
11457 +       struct path h_ppath;
11458 +       struct dentry *h_dentry;
11459 +       struct au_drinfo_fdata *fdata;
11460 +       char *infoname;                 /* inside of whname, just after PFX */
11461 +       char whname[sizeof(AUFS_WH_DR_INFO_PFX) + AUFS_DIRREN_ENV_VAL_SZ];
11462 +       aufs_bindex_t btgt, btail;
11463 +       unsigned char no_sio,
11464 +               allocated,              /* current size of *fdata */
11465 +               infonamelen,            /* room size for p */
11466 +               whnamelen,              /* length of the generated name */
11467 +               renameback;             /* renamed back */
11468 +};
11469 +
11470 +/* on rename(2) error, the caller should revert it using @elm */
11471 +static int au_drinfo_do_store(struct au_drinfo_store *w,
11472 +                             struct au_drinfo_rev_elm *elm)
11473 +{
11474 +       int err, len;
11475 +       ssize_t ssz;
11476 +       loff_t pos;
11477 +       struct path infopath = {
11478 +               .mnt = w->h_ppath.mnt
11479 +       };
11480 +       struct inode *h_dir, *h_inode, *delegated;
11481 +       struct file *infofile;
11482 +       struct qstr *qname;
11483 +
11484 +       AuDebugOn(elm
11485 +                 && memcmp(elm, page_address(ZERO_PAGE(0)), sizeof(*elm)));
11486 +
11487 +       infopath.dentry = vfsub_lookup_one_len(w->whname, &w->h_ppath,
11488 +                                              w->whnamelen);
11489 +       AuTraceErrPtr(infopath.dentry);
11490 +       if (IS_ERR(infopath.dentry)) {
11491 +               err = PTR_ERR(infopath.dentry);
11492 +               goto out;
11493 +       }
11494 +
11495 +       err = 0;
11496 +       h_dir = d_inode(w->h_ppath.dentry);
11497 +       if (elm && d_is_negative(infopath.dentry)) {
11498 +               err = vfsub_create(h_dir, &infopath, 0600, /*want_excl*/true);
11499 +               AuTraceErr(err);
11500 +               if (unlikely(err))
11501 +                       goto out_dput;
11502 +               elm->created = 1;
11503 +               elm->info_dentry = dget(infopath.dentry);
11504 +       }
11505 +
11506 +       infofile = vfsub_dentry_open(&infopath, O_RDWR);
11507 +       AuTraceErrPtr(infofile);
11508 +       if (IS_ERR(infofile)) {
11509 +               err = PTR_ERR(infofile);
11510 +               goto out_dput;
11511 +       }
11512 +
11513 +       h_inode = d_inode(infopath.dentry);
11514 +       if (elm && i_size_read(h_inode)) {
11515 +               h_inode = d_inode(w->h_dentry);
11516 +               elm->info_last = au_drinfo_read_k(infofile, h_inode->i_ino);
11517 +               AuTraceErrPtr(elm->info_last);
11518 +               if (IS_ERR(elm->info_last)) {
11519 +                       err = PTR_ERR(elm->info_last);
11520 +                       elm->info_last = NULL;
11521 +                       AuDebugOn(elm->info_dentry);
11522 +                       goto out_fput;
11523 +               }
11524 +       }
11525 +
11526 +       if (elm && w->renameback) {
11527 +               delegated = NULL;
11528 +               err = vfsub_unlink(h_dir, &infopath, &delegated, /*force*/0);
11529 +               AuTraceErr(err);
11530 +               if (unlikely(err == -EWOULDBLOCK))
11531 +                       iput(delegated);
11532 +               goto out_fput;
11533 +       }
11534 +
11535 +       pos = 0;
11536 +       qname = &w->h_dentry->d_name;
11537 +       len = sizeof(*w->fdata) + qname->len;
11538 +       if (!elm)
11539 +               len = sizeof(*w->fdata) + w->fdata->drinfo.oldnamelen;
11540 +       ssz = vfsub_write_k(infofile, w->fdata, len, &pos);
11541 +       if (ssz == len) {
11542 +               AuDbg("hi%llu, %.*s\n", w->fdata->drinfo.ino,
11543 +                     w->fdata->drinfo.oldnamelen, w->fdata->drinfo.oldname);
11544 +               goto out_fput; /* success */
11545 +       } else {
11546 +               err = -EIO;
11547 +               if (ssz < 0)
11548 +                       err = ssz;
11549 +               /* the caller should revert it using @elm */
11550 +       }
11551 +
11552 +out_fput:
11553 +       fput(infofile);
11554 +out_dput:
11555 +       dput(infopath.dentry);
11556 +out:
11557 +       AuTraceErr(err);
11558 +       return err;
11559 +}
11560 +
11561 +struct au_call_drinfo_do_store_args {
11562 +       int *errp;
11563 +       struct au_drinfo_store *w;
11564 +       struct au_drinfo_rev_elm *elm;
11565 +};
11566 +
11567 +static void au_call_drinfo_do_store(void *args)
11568 +{
11569 +       struct au_call_drinfo_do_store_args *a = args;
11570 +
11571 +       *a->errp = au_drinfo_do_store(a->w, a->elm);
11572 +}
11573 +
11574 +static int au_drinfo_store_sio(struct au_drinfo_store *w,
11575 +                              struct au_drinfo_rev_elm *elm)
11576 +{
11577 +       int err, wkq_err;
11578 +
11579 +       if (w->no_sio)
11580 +               err = au_drinfo_do_store(w, elm);
11581 +       else {
11582 +               struct au_call_drinfo_do_store_args a = {
11583 +                       .errp   = &err,
11584 +                       .w      = w,
11585 +                       .elm    = elm
11586 +               };
11587 +               wkq_err = au_wkq_wait(au_call_drinfo_do_store, &a);
11588 +               if (unlikely(wkq_err))
11589 +                       err = wkq_err;
11590 +       }
11591 +       AuTraceErr(err);
11592 +
11593 +       return err;
11594 +}
11595 +
11596 +static int au_drinfo_store_work_init(struct au_drinfo_store *w,
11597 +                                    aufs_bindex_t btgt)
11598 +{
11599 +       int err;
11600 +
11601 +       memset(w, 0, sizeof(*w));
11602 +       w->allocated = roundup_pow_of_two(sizeof(*w->fdata) + 40);
11603 +       strcpy(w->whname, AUFS_WH_DR_INFO_PFX);
11604 +       w->infoname = w->whname + sizeof(AUFS_WH_DR_INFO_PFX) - 1;
11605 +       w->infonamelen = sizeof(w->whname) - sizeof(AUFS_WH_DR_INFO_PFX);
11606 +       w->btgt = btgt;
11607 +       w->no_sio = !!uid_eq(current_fsuid(), GLOBAL_ROOT_UID);
11608 +
11609 +       err = -ENOMEM;
11610 +       w->fdata = kcalloc(1, w->allocated, GFP_NOFS);
11611 +       if (unlikely(!w->fdata)) {
11612 +               AuTraceErr(err);
11613 +               goto out;
11614 +       }
11615 +       w->fdata->magic = (__force uint32_t)htonl(AUFS_DRINFO_MAGIC_V1);
11616 +       err = 0;
11617 +
11618 +out:
11619 +       return err;
11620 +}
11621 +
11622 +static void au_drinfo_store_work_fin(struct au_drinfo_store *w)
11623 +{
11624 +       au_kfree_rcu(w->fdata);
11625 +}
11626 +
11627 +static void au_drinfo_store_rev(struct au_drinfo_rev *rev,
11628 +                               struct au_drinfo_store *w)
11629 +{
11630 +       struct au_drinfo_rev_elm *elm;
11631 +       struct inode *h_dir, *delegated;
11632 +       int err, nelm;
11633 +       struct path infopath = {
11634 +               .mnt = w->h_ppath.mnt
11635 +       };
11636 +
11637 +       h_dir = d_inode(w->h_ppath.dentry);
11638 +       IMustLock(h_dir);
11639 +
11640 +       err = 0;
11641 +       elm = rev->elm;
11642 +       for (nelm = rev->nelm; nelm > 0; nelm--, elm++) {
11643 +               AuDebugOn(elm->created && elm->info_last);
11644 +               if (elm->created) {
11645 +                       AuDbg("here\n");
11646 +                       delegated = NULL;
11647 +                       infopath.dentry = elm->info_dentry;
11648 +                       err = vfsub_unlink(h_dir, &infopath, &delegated,
11649 +                                          !w->no_sio);
11650 +                       AuTraceErr(err);
11651 +                       if (unlikely(err == -EWOULDBLOCK))
11652 +                               iput(delegated);
11653 +                       dput(elm->info_dentry);
11654 +               } else if (elm->info_last) {
11655 +                       AuDbg("here\n");
11656 +                       w->fdata->drinfo = *elm->info_last;
11657 +                       memcpy(w->fdata->drinfo.oldname,
11658 +                              elm->info_last->oldname,
11659 +                              elm->info_last->oldnamelen);
11660 +                       err = au_drinfo_store_sio(w, /*elm*/NULL);
11661 +                       au_kfree_rcu(elm->info_last);
11662 +               }
11663 +               if (unlikely(err))
11664 +                       AuIOErr("%d, %s\n", err, w->whname);
11665 +               /* go on even if err */
11666 +       }
11667 +}
11668 +
11669 +/* caller has to call au_dr_rename_fin() later */
11670 +static int au_drinfo_store(struct dentry *dentry, aufs_bindex_t btgt,
11671 +                          struct qstr *dst_name, void *_rev)
11672 +{
11673 +       int err, sz, nelm;
11674 +       aufs_bindex_t bindex, btail;
11675 +       struct au_drinfo_store work;
11676 +       struct au_drinfo_rev *rev, **p;
11677 +       struct au_drinfo_rev_elm *elm;
11678 +       struct super_block *sb;
11679 +       struct au_branch *br;
11680 +       struct au_hinode *hdir;
11681 +
11682 +       err = au_drinfo_store_work_init(&work, btgt);
11683 +       AuTraceErr(err);
11684 +       if (unlikely(err))
11685 +               goto out;
11686 +
11687 +       err = -ENOMEM;
11688 +       btail = au_dbtaildir(dentry);
11689 +       nelm = btail - btgt;
11690 +       sz = sizeof(*rev) + sizeof(*elm) * nelm;
11691 +       rev = kcalloc(1, sz, GFP_NOFS);
11692 +       if (unlikely(!rev)) {
11693 +               AuTraceErr(err);
11694 +               goto out_args;
11695 +       }
11696 +       rev->nelm = nelm;
11697 +       elm = rev->elm;
11698 +       p = _rev;
11699 +       *p = rev;
11700 +
11701 +       err = 0;
11702 +       sb = dentry->d_sb;
11703 +       work.h_ppath.dentry = au_h_dptr(dentry, btgt);
11704 +       work.h_ppath.mnt = au_sbr_mnt(sb, btgt);
11705 +       hdir = au_hi(d_inode(dentry), btgt);
11706 +       au_hn_inode_lock_nested(hdir, AuLsc_I_CHILD);
11707 +       for (bindex = btgt + 1; bindex <= btail; bindex++, elm++) {
11708 +               work.h_dentry = au_h_dptr(dentry, bindex);
11709 +               if (!work.h_dentry)
11710 +                       continue;
11711 +
11712 +               err = au_drinfo_construct(&work.fdata, work.h_dentry,
11713 +                                         &work.allocated);
11714 +               AuTraceErr(err);
11715 +               if (unlikely(err))
11716 +                       break;
11717 +
11718 +               work.renameback = au_qstreq(&work.h_dentry->d_name, dst_name);
11719 +               br = au_sbr(sb, bindex);
11720 +               work.whnamelen = sizeof(AUFS_WH_DR_INFO_PFX) - 1;
11721 +               work.whnamelen += au_drinfo_name(br, work.infoname,
11722 +                                                work.infonamelen);
11723 +               AuDbg("whname %.*s, i%llu, %.*s\n",
11724 +                     work.whnamelen, work.whname,
11725 +                     be64_to_cpu((__force __be64)work.fdata->drinfo.ino),
11726 +                     work.fdata->drinfo.oldnamelen,
11727 +                     work.fdata->drinfo.oldname);
11728 +
11729 +               err = au_drinfo_store_sio(&work, elm);
11730 +               AuTraceErr(err);
11731 +               if (unlikely(err))
11732 +                       break;
11733 +       }
11734 +       if (unlikely(err)) {
11735 +               /* revert all drinfo */
11736 +               au_drinfo_store_rev(rev, &work);
11737 +               au_kfree_try_rcu(rev);
11738 +               *p = NULL;
11739 +       }
11740 +       au_hn_inode_unlock(hdir);
11741 +
11742 +out_args:
11743 +       au_drinfo_store_work_fin(&work);
11744 +out:
11745 +       return err;
11746 +}
11747 +
11748 +/* ---------------------------------------------------------------------- */
11749 +
11750 +int au_dr_rename(struct dentry *src, aufs_bindex_t bindex,
11751 +                struct qstr *dst_name, void *_rev)
11752 +{
11753 +       int err, already;
11754 +       ino_t ino;
11755 +       struct super_block *sb;
11756 +       struct au_branch *br;
11757 +       struct au_dr_br *dr;
11758 +       struct dentry *h_dentry;
11759 +       struct inode *h_inode;
11760 +       struct au_dr_hino *ent;
11761 +       struct au_drinfo_rev *rev, **p;
11762 +
11763 +       AuDbg("bindex %d\n", bindex);
11764 +
11765 +       err = -ENOMEM;
11766 +       ent = kmalloc(sizeof(*ent), GFP_NOFS);
11767 +       if (unlikely(!ent))
11768 +               goto out;
11769 +
11770 +       sb = src->d_sb;
11771 +       br = au_sbr(sb, bindex);
11772 +       dr = &br->br_dirren;
11773 +       h_dentry = au_h_dptr(src, bindex);
11774 +       h_inode = d_inode(h_dentry);
11775 +       ino = h_inode->i_ino;
11776 +       ent->dr_h_ino = ino;
11777 +       already = au_dr_hino_test_add(dr, ino, ent);
11778 +       AuDbg("b%d, hi%llu, already %d\n",
11779 +             bindex, (unsigned long long)ino, already);
11780 +
11781 +       err = au_drinfo_store(src, bindex, dst_name, _rev);
11782 +       AuTraceErr(err);
11783 +       if (!err) {
11784 +               p = _rev;
11785 +               rev = *p;
11786 +               rev->already = already;
11787 +               goto out; /* success */
11788 +       }
11789 +
11790 +       /* revert */
11791 +       if (!already)
11792 +               au_dr_hino_del(dr, ent);
11793 +       au_kfree_rcu(ent);
11794 +
11795 +out:
11796 +       AuTraceErr(err);
11797 +       return err;
11798 +}
11799 +
11800 +void au_dr_rename_fin(struct dentry *src, aufs_bindex_t btgt, void *_rev)
11801 +{
11802 +       struct au_drinfo_rev *rev;
11803 +       struct au_drinfo_rev_elm *elm;
11804 +       int nelm;
11805 +
11806 +       rev = _rev;
11807 +       elm = rev->elm;
11808 +       for (nelm = rev->nelm; nelm > 0; nelm--, elm++) {
11809 +               dput(elm->info_dentry);
11810 +               au_kfree_rcu(elm->info_last);
11811 +       }
11812 +       au_kfree_try_rcu(rev);
11813 +}
11814 +
11815 +void au_dr_rename_rev(struct dentry *src, aufs_bindex_t btgt, void *_rev)
11816 +{
11817 +       int err;
11818 +       struct au_drinfo_store work;
11819 +       struct au_drinfo_rev *rev = _rev;
11820 +       struct super_block *sb;
11821 +       struct au_branch *br;
11822 +       struct inode *h_inode;
11823 +       struct au_dr_br *dr;
11824 +       struct au_dr_hino *ent;
11825 +
11826 +       err = au_drinfo_store_work_init(&work, btgt);
11827 +       if (unlikely(err))
11828 +               goto out;
11829 +
11830 +       sb = src->d_sb;
11831 +       br = au_sbr(sb, btgt);
11832 +       work.h_ppath.dentry = au_h_dptr(src, btgt);
11833 +       work.h_ppath.mnt = au_br_mnt(br);
11834 +       au_drinfo_store_rev(rev, &work);
11835 +       au_drinfo_store_work_fin(&work);
11836 +       if (rev->already)
11837 +               goto out;
11838 +
11839 +       dr = &br->br_dirren;
11840 +       h_inode = d_inode(work.h_ppath.dentry);
11841 +       ent = au_dr_hino_find(dr, h_inode->i_ino);
11842 +       BUG_ON(!ent);
11843 +       au_dr_hino_del(dr, ent);
11844 +       au_kfree_rcu(ent);
11845 +
11846 +out:
11847 +       au_kfree_try_rcu(rev);
11848 +       if (unlikely(err))
11849 +               pr_err("failed to remove dirren info\n");
11850 +}
11851 +
11852 +/* ---------------------------------------------------------------------- */
11853 +
11854 +static struct au_drinfo *au_drinfo_do_load(struct path *h_ppath,
11855 +                                          char *whname, int whnamelen,
11856 +                                          struct dentry **info_dentry)
11857 +{
11858 +       struct au_drinfo *drinfo;
11859 +       struct file *f;
11860 +       struct inode *h_dir;
11861 +       struct path infopath;
11862 +       int unlocked;
11863 +
11864 +       AuDbg("%pd/%.*s\n", h_ppath->dentry, whnamelen, whname);
11865 +
11866 +       *info_dentry = NULL;
11867 +       drinfo = NULL;
11868 +       unlocked = 0;
11869 +       h_dir = d_inode(h_ppath->dentry);
11870 +       inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
11871 +       infopath.dentry = vfsub_lookup_one_len(whname, h_ppath, whnamelen);
11872 +       if (IS_ERR(infopath.dentry)) {
11873 +               drinfo = (void *)infopath.dentry;
11874 +               goto out;
11875 +       }
11876 +
11877 +       if (d_is_negative(infopath.dentry))
11878 +               goto out_dput; /* success */
11879 +
11880 +       infopath.mnt = h_ppath->mnt;
11881 +       f = vfsub_dentry_open(&infopath, O_RDONLY);
11882 +       inode_unlock_shared(h_dir);
11883 +       unlocked = 1;
11884 +       if (IS_ERR(f)) {
11885 +               drinfo = (void *)f;
11886 +               goto out_dput;
11887 +       }
11888 +
11889 +       drinfo = au_drinfo_read_k(f, /*h_ino*/0);
11890 +       if (IS_ERR_OR_NULL(drinfo))
11891 +               goto out_fput;
11892 +
11893 +       AuDbg("oldname %.*s\n", drinfo->oldnamelen, drinfo->oldname);
11894 +       *info_dentry = dget(infopath.dentry); /* keep it alive */
11895 +
11896 +out_fput:
11897 +       fput(f);
11898 +out_dput:
11899 +       dput(infopath.dentry);
11900 +out:
11901 +       if (!unlocked)
11902 +               inode_unlock_shared(h_dir);
11903 +       AuTraceErrPtr(drinfo);
11904 +       return drinfo;
11905 +}
11906 +
11907 +struct au_drinfo_do_load_args {
11908 +       struct au_drinfo **drinfop;
11909 +       struct path *h_ppath;
11910 +       char *whname;
11911 +       int whnamelen;
11912 +       struct dentry **info_dentry;
11913 +};
11914 +
11915 +static void au_call_drinfo_do_load(void *args)
11916 +{
11917 +       struct au_drinfo_do_load_args *a = args;
11918 +
11919 +       *a->drinfop = au_drinfo_do_load(a->h_ppath, a->whname, a->whnamelen,
11920 +                                       a->info_dentry);
11921 +}
11922 +
11923 +struct au_drinfo_load {
11924 +       struct path h_ppath;
11925 +       struct qstr *qname;
11926 +       unsigned char no_sio;
11927 +
11928 +       aufs_bindex_t ninfo;
11929 +       struct au_drinfo **drinfo;
11930 +};
11931 +
11932 +static int au_drinfo_load(struct au_drinfo_load *w, aufs_bindex_t bindex,
11933 +                         struct au_branch *br)
11934 +{
11935 +       int err, wkq_err, whnamelen, e;
11936 +       char whname[sizeof(AUFS_WH_DR_INFO_PFX) + AUFS_DIRREN_ENV_VAL_SZ]
11937 +               = AUFS_WH_DR_INFO_PFX;
11938 +       struct au_drinfo *drinfo;
11939 +       struct qstr oldname;
11940 +       struct inode *h_dir, *delegated;
11941 +       struct dentry *info_dentry;
11942 +       struct path infopath;
11943 +
11944 +       whnamelen = sizeof(AUFS_WH_DR_INFO_PFX) - 1;
11945 +       whnamelen += au_drinfo_name(br, whname + whnamelen,
11946 +                                   sizeof(whname) - whnamelen);
11947 +       if (w->no_sio)
11948 +               drinfo = au_drinfo_do_load(&w->h_ppath, whname, whnamelen,
11949 +                                          &info_dentry);
11950 +       else {
11951 +               struct au_drinfo_do_load_args args = {
11952 +                       .drinfop        = &drinfo,
11953 +                       .h_ppath        = &w->h_ppath,
11954 +                       .whname         = whname,
11955 +                       .whnamelen      = whnamelen,
11956 +                       .info_dentry    = &info_dentry
11957 +               };
11958 +               wkq_err = au_wkq_wait(au_call_drinfo_do_load, &args);
11959 +               if (unlikely(wkq_err))
11960 +                       drinfo = ERR_PTR(wkq_err);
11961 +       }
11962 +       err = PTR_ERR(drinfo);
11963 +       if (IS_ERR_OR_NULL(drinfo))
11964 +               goto out;
11965 +
11966 +       err = 0;
11967 +       oldname.len = drinfo->oldnamelen;
11968 +       oldname.name = drinfo->oldname;
11969 +       if (au_qstreq(w->qname, &oldname)) {
11970 +               /* the name is renamed back */
11971 +               au_kfree_rcu(drinfo);
11972 +               drinfo = NULL;
11973 +
11974 +               infopath.dentry = info_dentry;
11975 +               infopath.mnt = w->h_ppath.mnt;
11976 +               h_dir = d_inode(w->h_ppath.dentry);
11977 +               delegated = NULL;
11978 +               inode_lock_nested(h_dir, AuLsc_I_PARENT);
11979 +               e = vfsub_unlink(h_dir, &infopath, &delegated, !w->no_sio);
11980 +               inode_unlock(h_dir);
11981 +               if (unlikely(e))
11982 +                       AuIOErr("ignored %d, %pd2\n", e, &infopath.dentry);
11983 +               if (unlikely(e == -EWOULDBLOCK))
11984 +                       iput(delegated);
11985 +       }
11986 +       au_kfree_rcu(w->drinfo[bindex]);
11987 +       w->drinfo[bindex] = drinfo;
11988 +       dput(info_dentry);
11989 +
11990 +out:
11991 +       AuTraceErr(err);
11992 +       return err;
11993 +}
11994 +
11995 +/* ---------------------------------------------------------------------- */
11996 +
11997 +static void au_dr_lkup_free(struct au_drinfo **drinfo, int n)
11998 +{
11999 +       struct au_drinfo **p = drinfo;
12000 +
12001 +       while (n-- > 0)
12002 +               au_kfree_rcu(*drinfo++);
12003 +       au_kfree_try_rcu(p);
12004 +}
12005 +
12006 +int au_dr_lkup(struct au_do_lookup_args *lkup, struct dentry *dentry,
12007 +              aufs_bindex_t btgt)
12008 +{
12009 +       int err, ninfo;
12010 +       struct au_drinfo_load w;
12011 +       aufs_bindex_t bindex, bbot;
12012 +       struct au_branch *br;
12013 +       struct inode *h_dir;
12014 +       struct au_dr_hino *ent;
12015 +       struct super_block *sb;
12016 +
12017 +       AuDbg("%.*s, name %.*s, whname %.*s, b%d\n",
12018 +             AuLNPair(&dentry->d_name), AuLNPair(&lkup->dirren.dr_name),
12019 +             AuLNPair(&lkup->whname), btgt);
12020 +
12021 +       sb = dentry->d_sb;
12022 +       bbot = au_sbbot(sb);
12023 +       w.ninfo = bbot + 1;
12024 +       if (!lkup->dirren.drinfo) {
12025 +               lkup->dirren.drinfo = kcalloc(w.ninfo,
12026 +                                             sizeof(*lkup->dirren.drinfo),
12027 +                                             GFP_NOFS);
12028 +               if (unlikely(!lkup->dirren.drinfo)) {
12029 +                       err = -ENOMEM;
12030 +                       goto out;
12031 +               }
12032 +               lkup->dirren.ninfo = w.ninfo;
12033 +       }
12034 +       w.drinfo = lkup->dirren.drinfo;
12035 +       w.no_sio = !!uid_eq(current_fsuid(), GLOBAL_ROOT_UID);
12036 +       w.h_ppath.dentry = au_h_dptr(dentry, btgt);
12037 +       AuDebugOn(!w.h_ppath.dentry);
12038 +       w.h_ppath.mnt = au_sbr_mnt(sb, btgt);
12039 +       w.qname = &dentry->d_name;
12040 +
12041 +       ninfo = 0;
12042 +       for (bindex = btgt + 1; bindex <= bbot; bindex++) {
12043 +               br = au_sbr(sb, bindex);
12044 +               err = au_drinfo_load(&w, bindex, br);
12045 +               if (unlikely(err))
12046 +                       goto out_free;
12047 +               if (w.drinfo[bindex])
12048 +                       ninfo++;
12049 +       }
12050 +       if (!ninfo) {
12051 +               br = au_sbr(sb, btgt);
12052 +               h_dir = d_inode(w.h_ppath.dentry);
12053 +               ent = au_dr_hino_find(&br->br_dirren, h_dir->i_ino);
12054 +               AuDebugOn(!ent);
12055 +               au_dr_hino_del(&br->br_dirren, ent);
12056 +               au_kfree_rcu(ent);
12057 +       }
12058 +       goto out; /* success */
12059 +
12060 +out_free:
12061 +       au_dr_lkup_free(lkup->dirren.drinfo, lkup->dirren.ninfo);
12062 +       lkup->dirren.ninfo = 0;
12063 +       lkup->dirren.drinfo = NULL;
12064 +out:
12065 +       AuTraceErr(err);
12066 +       return err;
12067 +}
12068 +
12069 +void au_dr_lkup_fin(struct au_do_lookup_args *lkup)
12070 +{
12071 +       au_dr_lkup_free(lkup->dirren.drinfo, lkup->dirren.ninfo);
12072 +}
12073 +
12074 +int au_dr_lkup_name(struct au_do_lookup_args *lkup, aufs_bindex_t btgt)
12075 +{
12076 +       int err;
12077 +       struct au_drinfo *drinfo;
12078 +
12079 +       err = 0;
12080 +       if (!lkup->dirren.drinfo)
12081 +               goto out;
12082 +       AuDebugOn(lkup->dirren.ninfo <= btgt);
12083 +       drinfo = lkup->dirren.drinfo[btgt];
12084 +       if (!drinfo)
12085 +               goto out;
12086 +
12087 +       au_kfree_try_rcu(lkup->whname.name);
12088 +       lkup->whname.name = NULL;
12089 +       lkup->dirren.dr_name.len = drinfo->oldnamelen;
12090 +       lkup->dirren.dr_name.name = drinfo->oldname;
12091 +       lkup->name = &lkup->dirren.dr_name;
12092 +       err = au_wh_name_alloc(&lkup->whname, lkup->name);
12093 +       if (!err)
12094 +               AuDbg("name %.*s, whname %.*s, b%d\n",
12095 +                     AuLNPair(lkup->name), AuLNPair(&lkup->whname),
12096 +                     btgt);
12097 +
12098 +out:
12099 +       AuTraceErr(err);
12100 +       return err;
12101 +}
12102 +
12103 +int au_dr_lkup_h_ino(struct au_do_lookup_args *lkup, aufs_bindex_t bindex,
12104 +                    ino_t h_ino)
12105 +{
12106 +       int match;
12107 +       struct au_drinfo *drinfo;
12108 +
12109 +       match = 1;
12110 +       if (!lkup->dirren.drinfo)
12111 +               goto out;
12112 +       AuDebugOn(lkup->dirren.ninfo <= bindex);
12113 +       drinfo = lkup->dirren.drinfo[bindex];
12114 +       if (!drinfo)
12115 +               goto out;
12116 +
12117 +       match = (drinfo->ino == h_ino);
12118 +       AuDbg("match %d\n", match);
12119 +
12120 +out:
12121 +       return match;
12122 +}
12123 +
12124 +/* ---------------------------------------------------------------------- */
12125 +
12126 +int au_dr_opt_set(struct super_block *sb)
12127 +{
12128 +       int err;
12129 +       aufs_bindex_t bindex, bbot;
12130 +       struct au_branch *br;
12131 +
12132 +       err = 0;
12133 +       bbot = au_sbbot(sb);
12134 +       for (bindex = 0; !err && bindex <= bbot; bindex++) {
12135 +               br = au_sbr(sb, bindex);
12136 +               err = au_dr_hino(sb, bindex, /*br*/NULL, &br->br_path);
12137 +       }
12138 +
12139 +       return err;
12140 +}
12141 +
12142 +int au_dr_opt_flush(struct super_block *sb)
12143 +{
12144 +       int err;
12145 +       aufs_bindex_t bindex, bbot;
12146 +       struct au_branch *br;
12147 +
12148 +       err = 0;
12149 +       bbot = au_sbbot(sb);
12150 +       for (bindex = 0; !err && bindex <= bbot; bindex++) {
12151 +               br = au_sbr(sb, bindex);
12152 +               if (au_br_writable(br->br_perm))
12153 +                       err = au_dr_hino(sb, bindex, /*br*/NULL, /*path*/NULL);
12154 +       }
12155 +
12156 +       return err;
12157 +}
12158 +
12159 +int au_dr_opt_clr(struct super_block *sb, int no_flush)
12160 +{
12161 +       int err;
12162 +       aufs_bindex_t bindex, bbot;
12163 +       struct au_branch *br;
12164 +
12165 +       err = 0;
12166 +       if (!no_flush) {
12167 +               err = au_dr_opt_flush(sb);
12168 +               if (unlikely(err))
12169 +                       goto out;
12170 +       }
12171 +
12172 +       bbot = au_sbbot(sb);
12173 +       for (bindex = 0; bindex <= bbot; bindex++) {
12174 +               br = au_sbr(sb, bindex);
12175 +               au_dr_hino_free(&br->br_dirren);
12176 +       }
12177 +
12178 +out:
12179 +       return err;
12180 +}
12181 diff -urN /usr/share/empty/fs/aufs/dirren.h linux/fs/aufs/dirren.h
12182 --- /usr/share/empty/fs/aufs/dirren.h   1970-01-01 01:00:00.000000000 +0100
12183 +++ linux/fs/aufs/dirren.h      2022-03-21 14:49:05.723299677 +0100
12184 @@ -0,0 +1,140 @@
12185 +/* SPDX-License-Identifier: GPL-2.0 */
12186 +/*
12187 + * Copyright (C) 2017-2021 Junjiro R. Okajima
12188 + *
12189 + * This program, aufs is free software; you can redistribute it and/or modify
12190 + * it under the terms of the GNU General Public License as published by
12191 + * the Free Software Foundation; either version 2 of the License, or
12192 + * (at your option) any later version.
12193 + *
12194 + * This program is distributed in the hope that it will be useful,
12195 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12196 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12197 + * GNU General Public License for more details.
12198 + *
12199 + * You should have received a copy of the GNU General Public License
12200 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
12201 + */
12202 +
12203 +/*
12204 + * renamed dir info
12205 + */
12206 +
12207 +#ifndef __AUFS_DIRREN_H__
12208 +#define __AUFS_DIRREN_H__
12209 +
12210 +#ifdef __KERNEL__
12211 +
12212 +#include <linux/dcache.h>
12213 +#include <linux/statfs.h>
12214 +#include <linux/uuid.h>
12215 +#include "hbl.h"
12216 +
12217 +#define AuDirren_NHASH 100
12218 +
12219 +#ifdef CONFIG_AUFS_DIRREN
12220 +enum au_brid_type {
12221 +       AuBrid_Unset,
12222 +       AuBrid_UUID,
12223 +       AuBrid_FSID,
12224 +       AuBrid_DEV
12225 +};
12226 +
12227 +struct au_dr_brid {
12228 +       enum au_brid_type       type;
12229 +       union {
12230 +               uuid_t  uuid;   /* unimplemented yet */
12231 +               fsid_t  fsid;
12232 +               dev_t   dev;
12233 +       };
12234 +};
12235 +
12236 +/* 20 is the max digits length of ulong 64 */
12237 +/* brid-type "_" uuid "_" inum */
12238 +#define AUFS_DIRREN_FNAME_SZ   (1 + 1 + UUID_STRING_LEN + 20)
12239 +#define AUFS_DIRREN_ENV_VAL_SZ (AUFS_DIRREN_FNAME_SZ + 1 + 20)
12240 +
12241 +struct au_dr_hino {
12242 +       struct hlist_bl_node    dr_hnode;
12243 +       ino_t                   dr_h_ino;
12244 +};
12245 +
12246 +struct au_dr_br {
12247 +       struct hlist_bl_head    dr_h_ino[AuDirren_NHASH];
12248 +       struct au_dr_brid       dr_brid;
12249 +};
12250 +
12251 +struct au_dr_lookup {
12252 +       /* dr_name is pointed by struct au_do_lookup_args.name */
12253 +       struct qstr             dr_name; /* subset of dr_info */
12254 +       aufs_bindex_t           ninfo;
12255 +       struct au_drinfo        **drinfo;
12256 +};
12257 +#else
12258 +struct au_dr_hino;
12259 +/* empty */
12260 +struct au_dr_br { };
12261 +struct au_dr_lookup { };
12262 +#endif
12263 +
12264 +/* ---------------------------------------------------------------------- */
12265 +
12266 +struct au_branch;
12267 +struct au_do_lookup_args;
12268 +struct au_hinode;
12269 +#ifdef CONFIG_AUFS_DIRREN
12270 +int au_dr_hino_test_add(struct au_dr_br *dr, ino_t h_ino,
12271 +                       struct au_dr_hino *add_ent);
12272 +void au_dr_hino_free(struct au_dr_br *dr);
12273 +int au_dr_br_init(struct super_block *sb, struct au_branch *br,
12274 +                 const struct path *path);
12275 +int au_dr_br_fin(struct super_block *sb, struct au_branch *br);
12276 +int au_dr_rename(struct dentry *src, aufs_bindex_t bindex,
12277 +                struct qstr *dst_name, void *_rev);
12278 +void au_dr_rename_fin(struct dentry *src, aufs_bindex_t btgt, void *rev);
12279 +void au_dr_rename_rev(struct dentry *src, aufs_bindex_t bindex, void *rev);
12280 +int au_dr_lkup(struct au_do_lookup_args *lkup, struct dentry *dentry,
12281 +              aufs_bindex_t bindex);
12282 +int au_dr_lkup_name(struct au_do_lookup_args *lkup, aufs_bindex_t btgt);
12283 +int au_dr_lkup_h_ino(struct au_do_lookup_args *lkup, aufs_bindex_t bindex,
12284 +                    ino_t h_ino);
12285 +void au_dr_lkup_fin(struct au_do_lookup_args *lkup);
12286 +int au_dr_opt_set(struct super_block *sb);
12287 +int au_dr_opt_flush(struct super_block *sb);
12288 +int au_dr_opt_clr(struct super_block *sb, int no_flush);
12289 +#else
12290 +AuStubInt0(au_dr_hino_test_add, struct au_dr_br *dr, ino_t h_ino,
12291 +          struct au_dr_hino *add_ent);
12292 +AuStubVoid(au_dr_hino_free, struct au_dr_br *dr);
12293 +AuStubInt0(au_dr_br_init, struct super_block *sb, struct au_branch *br,
12294 +          const struct path *path);
12295 +AuStubInt0(au_dr_br_fin, struct super_block *sb, struct au_branch *br);
12296 +AuStubInt0(au_dr_rename, struct dentry *src, aufs_bindex_t bindex,
12297 +          struct qstr *dst_name, void *_rev);
12298 +AuStubVoid(au_dr_rename_fin, struct dentry *src, aufs_bindex_t btgt, void *rev);
12299 +AuStubVoid(au_dr_rename_rev, struct dentry *src, aufs_bindex_t bindex,
12300 +          void *rev);
12301 +AuStubInt0(au_dr_lkup, struct au_do_lookup_args *lkup, struct dentry *dentry,
12302 +          aufs_bindex_t bindex);
12303 +AuStubInt0(au_dr_lkup_name, struct au_do_lookup_args *lkup, aufs_bindex_t btgt);
12304 +AuStubInt0(au_dr_lkup_h_ino, struct au_do_lookup_args *lkup,
12305 +          aufs_bindex_t bindex, ino_t h_ino);
12306 +AuStubVoid(au_dr_lkup_fin, struct au_do_lookup_args *lkup);
12307 +AuStubInt0(au_dr_opt_set, struct super_block *sb);
12308 +AuStubInt0(au_dr_opt_flush, struct super_block *sb);
12309 +AuStubInt0(au_dr_opt_clr, struct super_block *sb, int no_flush);
12310 +#endif
12311 +
12312 +/* ---------------------------------------------------------------------- */
12313 +
12314 +#ifdef CONFIG_AUFS_DIRREN
12315 +static inline int au_dr_ihash(ino_t h_ino)
12316 +{
12317 +       return h_ino % AuDirren_NHASH;
12318 +}
12319 +#else
12320 +AuStubInt0(au_dr_ihash, ino_t h_ino);
12321 +#endif
12322 +
12323 +#endif /* __KERNEL__ */
12324 +#endif /* __AUFS_DIRREN_H__ */
12325 diff -urN /usr/share/empty/fs/aufs/dynop.c linux/fs/aufs/dynop.c
12326 --- /usr/share/empty/fs/aufs/dynop.c    1970-01-01 01:00:00.000000000 +0100
12327 +++ linux/fs/aufs/dynop.c       2022-05-23 09:44:37.726505832 +0200
12328 @@ -0,0 +1,367 @@
12329 +// SPDX-License-Identifier: GPL-2.0
12330 +/*
12331 + * Copyright (C) 2010-2021 Junjiro R. Okajima
12332 + *
12333 + * This program, aufs is free software; you can redistribute it and/or modify
12334 + * it under the terms of the GNU General Public License as published by
12335 + * the Free Software Foundation; either version 2 of the License, or
12336 + * (at your option) any later version.
12337 + *
12338 + * This program is distributed in the hope that it will be useful,
12339 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12340 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12341 + * GNU General Public License for more details.
12342 + *
12343 + * You should have received a copy of the GNU General Public License
12344 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
12345 + */
12346 +
12347 +/*
12348 + * dynamically customizable operations for regular files
12349 + */
12350 +
12351 +#include "aufs.h"
12352 +
12353 +#define DyPrSym(key)   AuDbgSym(key->dk_op.dy_hop)
12354 +
12355 +/*
12356 + * How large will these lists be?
12357 + * Usually just a few elements, 20-30 at most for each, I guess.
12358 + */
12359 +static struct hlist_bl_head dynop[AuDyLast];
12360 +
12361 +static struct au_dykey *dy_gfind_get(struct hlist_bl_head *hbl,
12362 +                                    const void *h_op)
12363 +{
12364 +       struct au_dykey *key, *tmp;
12365 +       struct hlist_bl_node *pos;
12366 +
12367 +       key = NULL;
12368 +       hlist_bl_lock(hbl);
12369 +       hlist_bl_for_each_entry(tmp, pos, hbl, dk_hnode)
12370 +               if (tmp->dk_op.dy_hop == h_op) {
12371 +                       if (kref_get_unless_zero(&tmp->dk_kref))
12372 +                               key = tmp;
12373 +                       break;
12374 +               }
12375 +       hlist_bl_unlock(hbl);
12376 +
12377 +       return key;
12378 +}
12379 +
12380 +static struct au_dykey *dy_bradd(struct au_branch *br, struct au_dykey *key)
12381 +{
12382 +       struct au_dykey **k, *found;
12383 +       const void *h_op = key->dk_op.dy_hop;
12384 +       int i;
12385 +
12386 +       found = NULL;
12387 +       k = br->br_dykey;
12388 +       for (i = 0; i < AuBrDynOp; i++)
12389 +               if (k[i]) {
12390 +                       if (k[i]->dk_op.dy_hop == h_op) {
12391 +                               found = k[i];
12392 +                               break;
12393 +                       }
12394 +               } else
12395 +                       break;
12396 +       if (!found) {
12397 +               spin_lock(&br->br_dykey_lock);
12398 +               for (; i < AuBrDynOp; i++)
12399 +                       if (k[i]) {
12400 +                               if (k[i]->dk_op.dy_hop == h_op) {
12401 +                                       found = k[i];
12402 +                                       break;
12403 +                               }
12404 +                       } else {
12405 +                               k[i] = key;
12406 +                               break;
12407 +                       }
12408 +               spin_unlock(&br->br_dykey_lock);
12409 +               BUG_ON(i == AuBrDynOp); /* expand the array */
12410 +       }
12411 +
12412 +       return found;
12413 +}
12414 +
12415 +/* kref_get() if @key is already added */
12416 +static struct au_dykey *dy_gadd(struct hlist_bl_head *hbl, struct au_dykey *key)
12417 +{
12418 +       struct au_dykey *tmp, *found;
12419 +       struct hlist_bl_node *pos;
12420 +       const void *h_op = key->dk_op.dy_hop;
12421 +
12422 +       found = NULL;
12423 +       hlist_bl_lock(hbl);
12424 +       hlist_bl_for_each_entry(tmp, pos, hbl, dk_hnode)
12425 +               if (tmp->dk_op.dy_hop == h_op) {
12426 +                       if (kref_get_unless_zero(&tmp->dk_kref))
12427 +                               found = tmp;
12428 +                       break;
12429 +               }
12430 +       if (!found)
12431 +               hlist_bl_add_head(&key->dk_hnode, hbl);
12432 +       hlist_bl_unlock(hbl);
12433 +
12434 +       if (!found)
12435 +               DyPrSym(key);
12436 +       return found;
12437 +}
12438 +
12439 +static void dy_free_rcu(struct rcu_head *rcu)
12440 +{
12441 +       struct au_dykey *key;
12442 +
12443 +       key = container_of(rcu, struct au_dykey, dk_rcu);
12444 +       DyPrSym(key);
12445 +       kfree(key);
12446 +}
12447 +
12448 +static void dy_free(struct kref *kref)
12449 +{
12450 +       struct au_dykey *key;
12451 +       struct hlist_bl_head *hbl;
12452 +
12453 +       key = container_of(kref, struct au_dykey, dk_kref);
12454 +       hbl = dynop + key->dk_op.dy_type;
12455 +       au_hbl_del(&key->dk_hnode, hbl);
12456 +       call_rcu(&key->dk_rcu, dy_free_rcu);
12457 +}
12458 +
12459 +void au_dy_put(struct au_dykey *key)
12460 +{
12461 +       kref_put(&key->dk_kref, dy_free);
12462 +}
12463 +
12464 +/* ---------------------------------------------------------------------- */
12465 +
12466 +#define DyDbgSize(cnt, op)     AuDebugOn(cnt != sizeof(op)/sizeof(void *))
12467 +
12468 +#ifdef CONFIG_AUFS_DEBUG
12469 +#define DyDbgDeclare(cnt)      unsigned int cnt = 0
12470 +#define DyDbgInc(cnt)          do { cnt++; } while (0)
12471 +#else
12472 +#define DyDbgDeclare(cnt)      do {} while (0)
12473 +#define DyDbgInc(cnt)          do {} while (0)
12474 +#endif
12475 +
12476 +#define DySet(func, dst, src, h_op, h_sb) do {                         \
12477 +       DyDbgInc(cnt);                                                  \
12478 +       if (h_op->func) {                                               \
12479 +               if (src.func)                                           \
12480 +                       dst.func = src.func;                            \
12481 +               else                                                    \
12482 +                       AuDbg("%s %s\n", au_sbtype(h_sb), #func);       \
12483 +       }                                                               \
12484 +} while (0)
12485 +
12486 +#define DySetForce(func, dst, src) do {                \
12487 +       AuDebugOn(!src.func);                   \
12488 +       DyDbgInc(cnt);                          \
12489 +       dst.func = src.func;                    \
12490 +} while (0)
12491 +
12492 +#define DySetAop(func) \
12493 +       DySet(func, dyaop->da_op, aufs_aop, h_aop, h_sb)
12494 +#define DySetAopForce(func) \
12495 +       DySetForce(func, dyaop->da_op, aufs_aop)
12496 +
12497 +static void dy_aop(struct au_dykey *key, const void *h_op,
12498 +                  struct super_block *h_sb __maybe_unused)
12499 +{
12500 +       struct au_dyaop *dyaop = (void *)key;
12501 +       const struct address_space_operations *h_aop = h_op;
12502 +       DyDbgDeclare(cnt);
12503 +
12504 +       AuDbg("%s\n", au_sbtype(h_sb));
12505 +
12506 +       DySetAop(writepage);
12507 +       DySetAopForce(readpage);        /* force */
12508 +       DySetAop(writepages);
12509 +       DySetAop(dirty_folio);
12510 +       DySetAop(invalidate_folio);
12511 +       DySetAop(readahead);
12512 +       DySetAop(write_begin);
12513 +       DySetAop(write_end);
12514 +       DySetAop(bmap);
12515 +       DySetAop(releasepage);
12516 +       DySetAop(freepage);
12517 +       /* this one will be changed according to an aufs mount option */
12518 +       DySetAop(direct_IO);
12519 +       DySetAop(migratepage);
12520 +       DySetAop(isolate_page);
12521 +       DySetAop(putback_page);
12522 +       DySetAop(launder_folio);
12523 +       DySetAop(is_partially_uptodate);
12524 +       DySetAop(is_dirty_writeback);
12525 +       DySetAop(error_remove_page);
12526 +       DySetAop(swap_activate);
12527 +       DySetAop(swap_deactivate);
12528 +
12529 +       DyDbgSize(cnt, *h_aop);
12530 +}
12531 +
12532 +/* ---------------------------------------------------------------------- */
12533 +
12534 +static void dy_bug(struct kref *kref)
12535 +{
12536 +       BUG();
12537 +}
12538 +
12539 +static struct au_dykey *dy_get(struct au_dynop *op, struct au_branch *br)
12540 +{
12541 +       struct au_dykey *key, *old;
12542 +       struct hlist_bl_head *hbl;
12543 +       struct op {
12544 +               unsigned int sz;
12545 +               void (*set)(struct au_dykey *key, const void *h_op,
12546 +                           struct super_block *h_sb __maybe_unused);
12547 +       };
12548 +       static const struct op a[] = {
12549 +               [AuDy_AOP] = {
12550 +                       .sz     = sizeof(struct au_dyaop),
12551 +                       .set    = dy_aop
12552 +               }
12553 +       };
12554 +       const struct op *p;
12555 +
12556 +       hbl = dynop + op->dy_type;
12557 +       key = dy_gfind_get(hbl, op->dy_hop);
12558 +       if (key)
12559 +               goto out_add; /* success */
12560 +
12561 +       p = a + op->dy_type;
12562 +       key = kzalloc(p->sz, GFP_NOFS);
12563 +       if (unlikely(!key)) {
12564 +               key = ERR_PTR(-ENOMEM);
12565 +               goto out;
12566 +       }
12567 +
12568 +       key->dk_op.dy_hop = op->dy_hop;
12569 +       kref_init(&key->dk_kref);
12570 +       p->set(key, op->dy_hop, au_br_sb(br));
12571 +       old = dy_gadd(hbl, key);
12572 +       if (old) {
12573 +               au_kfree_rcu(key);
12574 +               key = old;
12575 +       }
12576 +
12577 +out_add:
12578 +       old = dy_bradd(br, key);
12579 +       if (old)
12580 +               /* its ref-count should never be zero here */
12581 +               kref_put(&key->dk_kref, dy_bug);
12582 +out:
12583 +       return key;
12584 +}
12585 +
12586 +/* ---------------------------------------------------------------------- */
12587 +/*
12588 + * Aufs prohibits O_DIRECT by default even if the branch supports it.
12589 + * This behaviour is necessary to return an error from open(O_DIRECT) instead
12590 + * of the succeeding I/O. The dio mount option enables O_DIRECT and makes
12591 + * open(O_DIRECT) always succeed, but the succeeding I/O may return an error.
12592 + * See the aufs manual in detail.
12593 + */
12594 +static void dy_adx(struct au_dyaop *dyaop, int do_dx)
12595 +{
12596 +       if (!do_dx)
12597 +               dyaop->da_op.direct_IO = NULL;
12598 +       else
12599 +               dyaop->da_op.direct_IO = aufs_aop.direct_IO;
12600 +}
12601 +
12602 +static struct au_dyaop *dy_aget(struct au_branch *br,
12603 +                               const struct address_space_operations *h_aop,
12604 +                               int do_dx)
12605 +{
12606 +       struct au_dyaop *dyaop;
12607 +       struct au_dynop op;
12608 +
12609 +       op.dy_type = AuDy_AOP;
12610 +       op.dy_haop = h_aop;
12611 +       dyaop = (void *)dy_get(&op, br);
12612 +       if (IS_ERR(dyaop))
12613 +               goto out;
12614 +       dy_adx(dyaop, do_dx);
12615 +
12616 +out:
12617 +       return dyaop;
12618 +}
12619 +
12620 +int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
12621 +               struct inode *h_inode)
12622 +{
12623 +       int err, do_dx;
12624 +       struct super_block *sb;
12625 +       struct au_branch *br;
12626 +       struct au_dyaop *dyaop;
12627 +
12628 +       AuDebugOn(!S_ISREG(h_inode->i_mode));
12629 +       IiMustWriteLock(inode);
12630 +
12631 +       sb = inode->i_sb;
12632 +       br = au_sbr(sb, bindex);
12633 +       do_dx = !!au_opt_test(au_mntflags(sb), DIO);
12634 +       dyaop = dy_aget(br, h_inode->i_mapping->a_ops, do_dx);
12635 +       err = PTR_ERR(dyaop);
12636 +       if (IS_ERR(dyaop))
12637 +               /* unnecessary to call dy_fput() */
12638 +               goto out;
12639 +
12640 +       err = 0;
12641 +       inode->i_mapping->a_ops = &dyaop->da_op;
12642 +
12643 +out:
12644 +       return err;
12645 +}
12646 +
12647 +/*
12648 + * Is it safe to replace a_ops during the inode/file is in operation?
12649 + * Yes, I hope so.
12650 + */
12651 +int au_dy_irefresh(struct inode *inode)
12652 +{
12653 +       int err;
12654 +       aufs_bindex_t btop;
12655 +       struct inode *h_inode;
12656 +
12657 +       err = 0;
12658 +       if (S_ISREG(inode->i_mode)) {
12659 +               btop = au_ibtop(inode);
12660 +               h_inode = au_h_iptr(inode, btop);
12661 +               err = au_dy_iaop(inode, btop, h_inode);
12662 +       }
12663 +       return err;
12664 +}
12665 +
12666 +void au_dy_arefresh(int do_dx)
12667 +{
12668 +       struct hlist_bl_head *hbl;
12669 +       struct hlist_bl_node *pos;
12670 +       struct au_dykey *key;
12671 +
12672 +       hbl = dynop + AuDy_AOP;
12673 +       hlist_bl_lock(hbl);
12674 +       hlist_bl_for_each_entry(key, pos, hbl, dk_hnode)
12675 +               dy_adx((void *)key, do_dx);
12676 +       hlist_bl_unlock(hbl);
12677 +}
12678 +
12679 +/* ---------------------------------------------------------------------- */
12680 +
12681 +void __init au_dy_init(void)
12682 +{
12683 +       int i;
12684 +
12685 +       for (i = 0; i < AuDyLast; i++)
12686 +               INIT_HLIST_BL_HEAD(dynop + i);
12687 +}
12688 +
12689 +void au_dy_fin(void)
12690 +{
12691 +       int i;
12692 +
12693 +       for (i = 0; i < AuDyLast; i++)
12694 +               WARN_ON(!hlist_bl_empty(dynop + i));
12695 +}
12696 diff -urN /usr/share/empty/fs/aufs/dynop.h linux/fs/aufs/dynop.h
12697 --- /usr/share/empty/fs/aufs/dynop.h    1970-01-01 01:00:00.000000000 +0100
12698 +++ linux/fs/aufs/dynop.h       2022-03-21 14:49:05.723299677 +0100
12699 @@ -0,0 +1,77 @@
12700 +/* SPDX-License-Identifier: GPL-2.0 */
12701 +/*
12702 + * Copyright (C) 2010-2021 Junjiro R. Okajima
12703 + *
12704 + * This program, aufs is free software; you can redistribute it and/or modify
12705 + * it under the terms of the GNU General Public License as published by
12706 + * the Free Software Foundation; either version 2 of the License, or
12707 + * (at your option) any later version.
12708 + *
12709 + * This program is distributed in the hope that it will be useful,
12710 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12711 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12712 + * GNU General Public License for more details.
12713 + *
12714 + * You should have received a copy of the GNU General Public License
12715 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
12716 + */
12717 +
12718 +/*
12719 + * dynamically customizable operations (for regular files only)
12720 + */
12721 +
12722 +#ifndef __AUFS_DYNOP_H__
12723 +#define __AUFS_DYNOP_H__
12724 +
12725 +#ifdef __KERNEL__
12726 +
12727 +#include <linux/fs.h>
12728 +#include <linux/kref.h>
12729 +
12730 +enum {AuDy_AOP, AuDyLast};
12731 +
12732 +struct au_dynop {
12733 +       int                                             dy_type;
12734 +       union {
12735 +               const void                              *dy_hop;
12736 +               const struct address_space_operations   *dy_haop;
12737 +       };
12738 +};
12739 +
12740 +struct au_dykey {
12741 +       union {
12742 +               struct hlist_bl_node    dk_hnode;
12743 +               struct rcu_head         dk_rcu;
12744 +       };
12745 +       struct au_dynop         dk_op;
12746 +
12747 +       /*
12748 +        * during I am in the branch local array, kref is gotten. when the
12749 +        * branch is removed, kref is put.
12750 +        */
12751 +       struct kref             dk_kref;
12752 +};
12753 +
12754 +/* stop unioning since their sizes are very different from each other */
12755 +struct au_dyaop {
12756 +       struct au_dykey                 da_key;
12757 +       struct address_space_operations da_op; /* not const */
12758 +};
12759 +/* make sure that 'struct au_dykey *' can be any type */
12760 +static_assert(!offsetof(struct au_dyaop, da_key));
12761 +
12762 +/* ---------------------------------------------------------------------- */
12763 +
12764 +/* dynop.c */
12765 +struct au_branch;
12766 +void au_dy_put(struct au_dykey *key);
12767 +int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
12768 +               struct inode *h_inode);
12769 +int au_dy_irefresh(struct inode *inode);
12770 +void au_dy_arefresh(int do_dio);
12771 +
12772 +void __init au_dy_init(void);
12773 +void au_dy_fin(void);
12774 +
12775 +#endif /* __KERNEL__ */
12776 +#endif /* __AUFS_DYNOP_H__ */
12777 diff -urN /usr/share/empty/fs/aufs/export.c linux/fs/aufs/export.c
12778 --- /usr/share/empty/fs/aufs/export.c   1970-01-01 01:00:00.000000000 +0100
12779 +++ linux/fs/aufs/export.c      2022-03-21 14:49:05.723299677 +0100
12780 @@ -0,0 +1,830 @@
12781 +// SPDX-License-Identifier: GPL-2.0
12782 +/*
12783 + * Copyright (C) 2005-2021 Junjiro R. Okajima
12784 + *
12785 + * This program, aufs is free software; you can redistribute it and/or modify
12786 + * it under the terms of the GNU General Public License as published by
12787 + * the Free Software Foundation; either version 2 of the License, or
12788 + * (at your option) any later version.
12789 + *
12790 + * This program is distributed in the hope that it will be useful,
12791 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
12792 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12793 + * GNU General Public License for more details.
12794 + *
12795 + * You should have received a copy of the GNU General Public License
12796 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
12797 + */
12798 +
12799 +/*
12800 + * export via nfs
12801 + */
12802 +
12803 +#include <linux/exportfs.h>
12804 +#include <linux/fs_struct.h>
12805 +#include <linux/nsproxy.h>
12806 +#include <linux/random.h>
12807 +#include <linux/writeback.h>
12808 +#include "aufs.h"
12809 +
12810 +union conv {
12811 +#ifdef CONFIG_AUFS_INO_T_64
12812 +       __u32 a[2];
12813 +#else
12814 +       __u32 a[1];
12815 +#endif
12816 +       ino_t ino;
12817 +};
12818 +
12819 +static ino_t decode_ino(__u32 *a)
12820 +{
12821 +       union conv u;
12822 +
12823 +       BUILD_BUG_ON(sizeof(u.ino) != sizeof(u.a));
12824 +       u.a[0] = a[0];
12825 +#ifdef CONFIG_AUFS_INO_T_64
12826 +       u.a[1] = a[1];
12827 +#endif
12828 +       return u.ino;
12829 +}
12830 +
12831 +static void encode_ino(__u32 *a, ino_t ino)
12832 +{
12833 +       union conv u;
12834 +
12835 +       u.ino = ino;
12836 +       a[0] = u.a[0];
12837 +#ifdef CONFIG_AUFS_INO_T_64
12838 +       a[1] = u.a[1];
12839 +#endif
12840 +}
12841 +
12842 +/* NFS file handle */
12843 +enum {
12844 +       Fh_br_id,
12845 +       Fh_sigen,
12846 +#ifdef CONFIG_AUFS_INO_T_64
12847 +       /* support 64bit inode number */
12848 +       Fh_ino1,
12849 +       Fh_ino2,
12850 +       Fh_dir_ino1,
12851 +       Fh_dir_ino2,
12852 +#else
12853 +       Fh_ino1,
12854 +       Fh_dir_ino1,
12855 +#endif
12856 +       Fh_igen,
12857 +       Fh_h_type,
12858 +       Fh_tail,
12859 +
12860 +       Fh_ino = Fh_ino1,
12861 +       Fh_dir_ino = Fh_dir_ino1
12862 +};
12863 +
12864 +static int au_test_anon(struct dentry *dentry)
12865 +{
12866 +       /* note: read d_flags without d_lock */
12867 +       return !!(dentry->d_flags & DCACHE_DISCONNECTED);
12868 +}
12869 +
12870 +int au_test_nfsd(void)
12871 +{
12872 +       int ret;
12873 +       struct task_struct *tsk = current;
12874 +       char comm[sizeof(tsk->comm)];
12875 +
12876 +       ret = 0;
12877 +       if (tsk->flags & PF_KTHREAD) {
12878 +               get_task_comm(comm, tsk);
12879 +               ret = !strcmp(comm, "nfsd");
12880 +       }
12881 +
12882 +       return ret;
12883 +}
12884 +
12885 +/* ---------------------------------------------------------------------- */
12886 +/* inode generation external table */
12887 +
12888 +void au_xigen_inc(struct inode *inode)
12889 +{
12890 +       loff_t pos;
12891 +       ssize_t sz;
12892 +       __u32 igen;
12893 +       struct super_block *sb;
12894 +       struct au_sbinfo *sbinfo;
12895 +
12896 +       sb = inode->i_sb;
12897 +       AuDebugOn(!au_opt_test(au_mntflags(sb), XINO));
12898 +
12899 +       sbinfo = au_sbi(sb);
12900 +       pos = inode->i_ino;
12901 +       pos *= sizeof(igen);
12902 +       igen = inode->i_generation + 1;
12903 +       sz = xino_fwrite(sbinfo->si_xigen, &igen, sizeof(igen), &pos);
12904 +       if (sz == sizeof(igen))
12905 +               return; /* success */
12906 +
12907 +       if (unlikely(sz >= 0))
12908 +               AuIOErr("xigen error (%zd)\n", sz);
12909 +}
12910 +
12911 +int au_xigen_new(struct inode *inode)
12912 +{
12913 +       int err;
12914 +       loff_t pos;
12915 +       ssize_t sz;
12916 +       struct super_block *sb;
12917 +       struct au_sbinfo *sbinfo;
12918 +       struct file *file;
12919 +
12920 +       err = 0;
12921 +       /* todo: dirty, at mount time */
12922 +       if (inode->i_ino == AUFS_ROOT_INO)
12923 +               goto out;
12924 +       sb = inode->i_sb;
12925 +       SiMustAnyLock(sb);
12926 +       if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
12927 +               goto out;
12928 +
12929 +       err = -EFBIG;
12930 +       pos = inode->i_ino;
12931 +       if (unlikely(au_loff_max / sizeof(inode->i_generation) - 1 < pos)) {
12932 +               AuIOErr1("too large i%lld\n", pos);
12933 +               goto out;
12934 +       }
12935 +       pos *= sizeof(inode->i_generation);
12936 +
12937 +       err = 0;
12938 +       sbinfo = au_sbi(sb);
12939 +       file = sbinfo->si_xigen;
12940 +       BUG_ON(!file);
12941 +
12942 +       if (vfsub_f_size_read(file)
12943 +           < pos + sizeof(inode->i_generation)) {
12944 +               inode->i_generation = atomic_inc_return(&sbinfo->si_xigen_next);
12945 +               sz = xino_fwrite(file, &inode->i_generation,
12946 +                                sizeof(inode->i_generation), &pos);
12947 +       } else
12948 +               sz = xino_fread(file, &inode->i_generation,
12949 +                               sizeof(inode->i_generation), &pos);
12950 +       if (sz == sizeof(inode->i_generation))
12951 +               goto out; /* success */
12952 +
12953 +       err = sz;
12954 +       if (unlikely(sz >= 0)) {
12955 +               err = -EIO;
12956 +               AuIOErr("xigen error (%zd)\n", sz);
12957 +       }
12958 +
12959 +out:
12960 +       return err;
12961 +}
12962 +
12963 +int au_xigen_set(struct super_block *sb, struct path *path)
12964 +{
12965 +       int err;
12966 +       struct au_sbinfo *sbinfo;
12967 +       struct file *file;
12968 +
12969 +       SiMustWriteLock(sb);
12970 +
12971 +       sbinfo = au_sbi(sb);
12972 +       file = au_xino_create2(sb, path, sbinfo->si_xigen);
12973 +       err = PTR_ERR(file);
12974 +       if (IS_ERR(file))
12975 +               goto out;
12976 +       err = 0;
12977 +       if (sbinfo->si_xigen)
12978 +               fput(sbinfo->si_xigen);
12979 +       sbinfo->si_xigen = file;
12980 +
12981 +out:
12982 +       AuTraceErr(err);
12983 +       return err;
12984 +}
12985 +
12986 +void au_xigen_clr(struct super_block *sb)
12987 +{
12988 +       struct au_sbinfo *sbinfo;
12989 +
12990 +       SiMustWriteLock(sb);
12991 +
12992 +       sbinfo = au_sbi(sb);
12993 +       if (sbinfo->si_xigen) {
12994 +               fput(sbinfo->si_xigen);
12995 +               sbinfo->si_xigen = NULL;
12996 +       }
12997 +}
12998 +
12999 +/* ---------------------------------------------------------------------- */
13000 +
13001 +static struct dentry *decode_by_ino(struct super_block *sb, ino_t ino,
13002 +                                   ino_t dir_ino)
13003 +{
13004 +       struct dentry *dentry, *d;
13005 +       struct inode *inode;
13006 +       unsigned int sigen;
13007 +
13008 +       dentry = NULL;
13009 +       inode = ilookup(sb, ino);
13010 +       if (!inode)
13011 +               goto out;
13012 +
13013 +       dentry = ERR_PTR(-ESTALE);
13014 +       sigen = au_sigen(sb);
13015 +       if (unlikely(au_is_bad_inode(inode)
13016 +                    || IS_DEADDIR(inode)
13017 +                    || sigen != au_iigen(inode, NULL)))
13018 +               goto out_iput;
13019 +
13020 +       dentry = NULL;
13021 +       if (!dir_ino || S_ISDIR(inode->i_mode))
13022 +               dentry = d_find_alias(inode);
13023 +       else {
13024 +               spin_lock(&inode->i_lock);
13025 +               hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
13026 +                       spin_lock(&d->d_lock);
13027 +                       if (!au_test_anon(d)
13028 +                           && d_inode(d->d_parent)->i_ino == dir_ino) {
13029 +                               dentry = dget_dlock(d);
13030 +                               spin_unlock(&d->d_lock);
13031 +                               break;
13032 +                       }
13033 +                       spin_unlock(&d->d_lock);
13034 +               }
13035 +               spin_unlock(&inode->i_lock);
13036 +       }
13037 +       if (unlikely(dentry && au_digen_test(dentry, sigen))) {
13038 +               /* need to refresh */
13039 +               dput(dentry);
13040 +               dentry = NULL;
13041 +       }
13042 +
13043 +out_iput:
13044 +       iput(inode);
13045 +out:
13046 +       AuTraceErrPtr(dentry);
13047 +       return dentry;
13048 +}
13049 +
13050 +/* ---------------------------------------------------------------------- */
13051 +
13052 +/* todo: dirty? */
13053 +/* if exportfs_decode_fh() passed vfsmount*, we could be happy */
13054 +
13055 +struct au_compare_mnt_args {
13056 +       /* input */
13057 +       struct super_block *sb;
13058 +
13059 +       /* output */
13060 +       struct vfsmount *mnt;
13061 +};
13062 +
13063 +static int au_compare_mnt(struct vfsmount *mnt, void *arg)
13064 +{
13065 +       struct au_compare_mnt_args *a = arg;
13066 +
13067 +       if (mnt->mnt_sb != a->sb)
13068 +               return 0;
13069 +       a->mnt = mntget(mnt);
13070 +       return 1;
13071 +}
13072 +
13073 +static struct vfsmount *au_mnt_get(struct super_block *sb)
13074 +{
13075 +       int err;
13076 +       struct path root;
13077 +       struct au_compare_mnt_args args = {
13078 +               .sb = sb
13079 +       };
13080 +
13081 +       get_fs_root(current->fs, &root);
13082 +       rcu_read_lock();
13083 +       err = iterate_mounts(au_compare_mnt, &args, root.mnt);
13084 +       rcu_read_unlock();
13085 +       path_put(&root);
13086 +       AuDebugOn(!err);
13087 +       AuDebugOn(!args.mnt);
13088 +       return args.mnt;
13089 +}
13090 +
13091 +struct au_nfsd_si_lock {
13092 +       unsigned int sigen;
13093 +       aufs_bindex_t bindex, br_id;
13094 +       unsigned char force_lock;
13095 +};
13096 +
13097 +static int si_nfsd_read_lock(struct super_block *sb,
13098 +                            struct au_nfsd_si_lock *nsi_lock)
13099 +{
13100 +       int err;
13101 +       aufs_bindex_t bindex;
13102 +
13103 +       si_read_lock(sb, AuLock_FLUSH);
13104 +
13105 +       /* branch id may be wrapped around */
13106 +       err = 0;
13107 +       bindex = au_br_index(sb, nsi_lock->br_id);
13108 +       if (bindex >= 0 && nsi_lock->sigen + AUFS_BRANCH_MAX > au_sigen(sb))
13109 +               goto out; /* success */
13110 +
13111 +       err = -ESTALE;
13112 +       bindex = -1;
13113 +       if (!nsi_lock->force_lock)
13114 +               si_read_unlock(sb);
13115 +
13116 +out:
13117 +       nsi_lock->bindex = bindex;
13118 +       return err;
13119 +}
13120 +
13121 +struct find_name_by_ino {
13122 +       struct dir_context ctx;
13123 +       int called, found;
13124 +       ino_t ino;
13125 +       char *name;
13126 +       int namelen;
13127 +};
13128 +
13129 +static int
13130 +find_name_by_ino(struct dir_context *ctx, const char *name, int namelen,
13131 +                loff_t offset, u64 ino, unsigned int d_type)
13132 +{
13133 +       struct find_name_by_ino *a = container_of(ctx, struct find_name_by_ino,
13134 +                                                 ctx);
13135 +
13136 +       a->called++;
13137 +       if (a->ino != ino)
13138 +               return 0;
13139 +
13140 +       memcpy(a->name, name, namelen);
13141 +       a->namelen = namelen;
13142 +       a->found = 1;
13143 +       return 1;
13144 +}
13145 +
13146 +static struct dentry *au_lkup_by_ino(struct path *path, ino_t ino,
13147 +                                    struct au_nfsd_si_lock *nsi_lock)
13148 +{
13149 +       struct dentry *dentry, *parent;
13150 +       struct file *file;
13151 +       struct inode *dir;
13152 +       struct find_name_by_ino arg = {
13153 +               .ctx = {
13154 +                       .actor = find_name_by_ino
13155 +               }
13156 +       };
13157 +       int err;
13158 +
13159 +       parent = path->dentry;
13160 +       if (nsi_lock)
13161 +               si_read_unlock(parent->d_sb);
13162 +       file = vfsub_dentry_open(path, au_dir_roflags);
13163 +       dentry = (void *)file;
13164 +       if (IS_ERR(file))
13165 +               goto out;
13166 +
13167 +       dentry = ERR_PTR(-ENOMEM);
13168 +       arg.name = (void *)__get_free_page(GFP_NOFS);
13169 +       if (unlikely(!arg.name))
13170 +               goto out_file;
13171 +       arg.ino = ino;
13172 +       arg.found = 0;
13173 +       do {
13174 +               arg.called = 0;
13175 +               /* smp_mb(); */
13176 +               err = vfsub_iterate_dir(file, &arg.ctx);
13177 +       } while (!err && !arg.found && arg.called);
13178 +       dentry = ERR_PTR(err);
13179 +       if (unlikely(err))
13180 +               goto out_name;
13181 +       /* instead of ENOENT */
13182 +       dentry = ERR_PTR(-ESTALE);
13183 +       if (!arg.found)
13184 +               goto out_name;
13185 +
13186 +       /* do not call vfsub_lkup_one() */
13187 +       dir = d_inode(parent);
13188 +       dentry = vfsub_lookup_one_len_unlocked(arg.name, path, arg.namelen);
13189 +       AuTraceErrPtr(dentry);
13190 +       if (IS_ERR(dentry))
13191 +               goto out_name;
13192 +       AuDebugOn(au_test_anon(dentry));
13193 +       if (unlikely(d_really_is_negative(dentry))) {
13194 +               dput(dentry);
13195 +               dentry = ERR_PTR(-ENOENT);
13196 +       }
13197 +
13198 +out_name:
13199 +       free_page((unsigned long)arg.name);
13200 +out_file:
13201 +       fput(file);
13202 +out:
13203 +       if (unlikely(nsi_lock
13204 +                    && si_nfsd_read_lock(parent->d_sb, nsi_lock) < 0))
13205 +               if (!IS_ERR(dentry)) {
13206 +                       dput(dentry);
13207 +                       dentry = ERR_PTR(-ESTALE);
13208 +               }
13209 +       AuTraceErrPtr(dentry);
13210 +       return dentry;
13211 +}
13212 +
13213 +static struct dentry *decode_by_dir_ino(struct super_block *sb, ino_t ino,
13214 +                                       ino_t dir_ino,
13215 +                                       struct au_nfsd_si_lock *nsi_lock)
13216 +{
13217 +       struct dentry *dentry;
13218 +       struct path path;
13219 +
13220 +       if (dir_ino != AUFS_ROOT_INO) {
13221 +               path.dentry = decode_by_ino(sb, dir_ino, 0);
13222 +               dentry = path.dentry;
13223 +               if (!path.dentry || IS_ERR(path.dentry))
13224 +                       goto out;
13225 +               AuDebugOn(au_test_anon(path.dentry));
13226 +       } else
13227 +               path.dentry = dget(sb->s_root);
13228 +
13229 +       path.mnt = au_mnt_get(sb);
13230 +       dentry = au_lkup_by_ino(&path, ino, nsi_lock);
13231 +       path_put(&path);
13232 +
13233 +out:
13234 +       AuTraceErrPtr(dentry);
13235 +       return dentry;
13236 +}
13237 +
13238 +/* ---------------------------------------------------------------------- */
13239 +
13240 +static int h_acceptable(void *expv, struct dentry *dentry)
13241 +{
13242 +       return 1;
13243 +}
13244 +
13245 +static char *au_build_path(struct dentry *h_parent, struct path *h_rootpath,
13246 +                          char *buf, int len, struct super_block *sb)
13247 +{
13248 +       char *p;
13249 +       int n;
13250 +       struct path path;
13251 +
13252 +       p = d_path(h_rootpath, buf, len);
13253 +       if (IS_ERR(p))
13254 +               goto out;
13255 +       n = strlen(p);
13256 +
13257 +       path.mnt = h_rootpath->mnt;
13258 +       path.dentry = h_parent;
13259 +       p = d_path(&path, buf, len);
13260 +       if (IS_ERR(p))
13261 +               goto out;
13262 +       if (n != 1)
13263 +               p += n;
13264 +
13265 +       path.mnt = au_mnt_get(sb);
13266 +       path.dentry = sb->s_root;
13267 +       p = d_path(&path, buf, len - strlen(p));
13268 +       mntput(path.mnt);
13269 +       if (IS_ERR(p))
13270 +               goto out;
13271 +       if (n != 1)
13272 +               p[strlen(p)] = '/';
13273 +
13274 +out:
13275 +       AuTraceErrPtr(p);
13276 +       return p;
13277 +}
13278 +
13279 +static
13280 +struct dentry *decode_by_path(struct super_block *sb, ino_t ino, __u32 *fh,
13281 +                             int fh_len, struct au_nfsd_si_lock *nsi_lock)
13282 +{
13283 +       struct dentry *dentry, *h_parent, *root;
13284 +       struct super_block *h_sb;
13285 +       char *pathname, *p;
13286 +       struct vfsmount *h_mnt;
13287 +       struct au_branch *br;
13288 +       int err;
13289 +       struct path path;
13290 +
13291 +       br = au_sbr(sb, nsi_lock->bindex);
13292 +       h_mnt = au_br_mnt(br);
13293 +       h_sb = h_mnt->mnt_sb;
13294 +       /* todo: call lower fh_to_dentry()? fh_to_parent()? */
13295 +       lockdep_off();
13296 +       h_parent = exportfs_decode_fh(h_mnt, (void *)(fh + Fh_tail),
13297 +                                     fh_len - Fh_tail, fh[Fh_h_type],
13298 +                                     h_acceptable, /*context*/NULL);
13299 +       lockdep_on();
13300 +       dentry = h_parent;
13301 +       if (unlikely(!h_parent || IS_ERR(h_parent))) {
13302 +               AuWarn1("%s decode_fh failed, %ld\n",
13303 +                       au_sbtype(h_sb), PTR_ERR(h_parent));
13304 +               goto out;
13305 +       }
13306 +       dentry = NULL;
13307 +       if (unlikely(au_test_anon(h_parent))) {
13308 +               AuWarn1("%s decode_fh returned a disconnected dentry\n",
13309 +                       au_sbtype(h_sb));
13310 +               goto out_h_parent;
13311 +       }
13312 +
13313 +       dentry = ERR_PTR(-ENOMEM);
13314 +       pathname = (void *)__get_free_page(GFP_NOFS);
13315 +       if (unlikely(!pathname))
13316 +               goto out_h_parent;
13317 +
13318 +       root = sb->s_root;
13319 +       path.mnt = h_mnt;
13320 +       di_read_lock_parent(root, !AuLock_IR);
13321 +       path.dentry = au_h_dptr(root, nsi_lock->bindex);
13322 +       di_read_unlock(root, !AuLock_IR);
13323 +       p = au_build_path(h_parent, &path, pathname, PAGE_SIZE, sb);
13324 +       dentry = (void *)p;
13325 +       if (IS_ERR(p))
13326 +               goto out_pathname;
13327 +
13328 +       si_read_unlock(sb);
13329 +       err = vfsub_kern_path(p, LOOKUP_FOLLOW | LOOKUP_DIRECTORY, &path);
13330 +       dentry = ERR_PTR(err);
13331 +       if (unlikely(err))
13332 +               goto out_relock;
13333 +
13334 +       dentry = ERR_PTR(-ENOENT);
13335 +       AuDebugOn(au_test_anon(path.dentry));
13336 +       if (unlikely(d_really_is_negative(path.dentry)))
13337 +               goto out_path;
13338 +
13339 +       if (ino != d_inode(path.dentry)->i_ino)
13340 +               dentry = au_lkup_by_ino(&path, ino, /*nsi_lock*/NULL);
13341 +       else
13342 +               dentry = dget(path.dentry);
13343 +
13344 +out_path:
13345 +       path_put(&path);
13346 +out_relock:
13347 +       if (unlikely(si_nfsd_read_lock(sb, nsi_lock) < 0))
13348 +               if (!IS_ERR(dentry)) {
13349 +                       dput(dentry);
13350 +                       dentry = ERR_PTR(-ESTALE);
13351 +               }
13352 +out_pathname:
13353 +       free_page((unsigned long)pathname);
13354 +out_h_parent:
13355 +       dput(h_parent);
13356 +out:
13357 +       AuTraceErrPtr(dentry);
13358 +       return dentry;
13359 +}
13360 +
13361 +/* ---------------------------------------------------------------------- */
13362 +
13363 +static struct dentry *
13364 +aufs_fh_to_dentry(struct super_block *sb, struct fid *fid, int fh_len,
13365 +                 int fh_type)
13366 +{
13367 +       struct dentry *dentry;
13368 +       __u32 *fh = fid->raw;
13369 +       struct au_branch *br;
13370 +       ino_t ino, dir_ino;
13371 +       struct au_nfsd_si_lock nsi_lock = {
13372 +               .force_lock     = 0
13373 +       };
13374 +
13375 +       dentry = ERR_PTR(-ESTALE);
13376 +       /* it should never happen, but the file handle is unreliable */
13377 +       if (unlikely(fh_len < Fh_tail))
13378 +               goto out;
13379 +       nsi_lock.sigen = fh[Fh_sigen];
13380 +       nsi_lock.br_id = fh[Fh_br_id];
13381 +
13382 +       /* branch id may be wrapped around */
13383 +       br = NULL;
13384 +       if (unlikely(si_nfsd_read_lock(sb, &nsi_lock)))
13385 +               goto out;
13386 +       nsi_lock.force_lock = 1;
13387 +
13388 +       /* is this inode still cached? */
13389 +       ino = decode_ino(fh + Fh_ino);
13390 +       /* it should never happen */
13391 +       if (unlikely(ino == AUFS_ROOT_INO))
13392 +               goto out_unlock;
13393 +
13394 +       dir_ino = decode_ino(fh + Fh_dir_ino);
13395 +       dentry = decode_by_ino(sb, ino, dir_ino);
13396 +       if (IS_ERR(dentry))
13397 +               goto out_unlock;
13398 +       if (dentry)
13399 +               goto accept;
13400 +
13401 +       /* is the parent dir cached? */
13402 +       br = au_sbr(sb, nsi_lock.bindex);
13403 +       au_lcnt_inc(&br->br_nfiles);
13404 +       dentry = decode_by_dir_ino(sb, ino, dir_ino, &nsi_lock);
13405 +       if (IS_ERR(dentry))
13406 +               goto out_unlock;
13407 +       if (dentry)
13408 +               goto accept;
13409 +
13410 +       /* lookup path */
13411 +       dentry = decode_by_path(sb, ino, fh, fh_len, &nsi_lock);
13412 +       if (IS_ERR(dentry))
13413 +               goto out_unlock;
13414 +       if (unlikely(!dentry))
13415 +               /* todo?: make it ESTALE */
13416 +               goto out_unlock;
13417 +
13418 +accept:
13419 +       if (!au_digen_test(dentry, au_sigen(sb))
13420 +           && d_inode(dentry)->i_generation == fh[Fh_igen])
13421 +               goto out_unlock; /* success */
13422 +
13423 +       dput(dentry);
13424 +       dentry = ERR_PTR(-ESTALE);
13425 +out_unlock:
13426 +       if (br)
13427 +               au_lcnt_dec(&br->br_nfiles);
13428 +       si_read_unlock(sb);
13429 +out:
13430 +       AuTraceErrPtr(dentry);
13431 +       return dentry;
13432 +}
13433 +
13434 +#if 0 /* reserved for future use */
13435 +/* support subtreecheck option */
13436 +static struct dentry *aufs_fh_to_parent(struct super_block *sb, struct fid *fid,
13437 +                                       int fh_len, int fh_type)
13438 +{
13439 +       struct dentry *parent;
13440 +       __u32 *fh = fid->raw;
13441 +       ino_t dir_ino;
13442 +
13443 +       dir_ino = decode_ino(fh + Fh_dir_ino);
13444 +       parent = decode_by_ino(sb, dir_ino, 0);
13445 +       if (IS_ERR(parent))
13446 +               goto out;
13447 +       if (!parent)
13448 +               parent = decode_by_path(sb, au_br_index(sb, fh[Fh_br_id]),
13449 +                                       dir_ino, fh, fh_len);
13450 +
13451 +out:
13452 +       AuTraceErrPtr(parent);
13453 +       return parent;
13454 +}
13455 +#endif
13456 +
13457 +/* ---------------------------------------------------------------------- */
13458 +
13459 +static int aufs_encode_fh(struct inode *inode, __u32 *fh, int *max_len,
13460 +                         struct inode *dir)
13461 +{
13462 +       int err;
13463 +       aufs_bindex_t bindex;
13464 +       struct super_block *sb, *h_sb;
13465 +       struct dentry *dentry, *parent, *h_parent;
13466 +       struct inode *h_dir;
13467 +       struct au_branch *br;
13468 +
13469 +       err = -ENOSPC;
13470 +       if (unlikely(*max_len <= Fh_tail)) {
13471 +               AuWarn1("NFSv2 client (max_len %d)?\n", *max_len);
13472 +               goto out;
13473 +       }
13474 +
13475 +       err = FILEID_ROOT;
13476 +       if (inode->i_ino == AUFS_ROOT_INO) {
13477 +               AuDebugOn(inode->i_ino != AUFS_ROOT_INO);
13478 +               goto out;
13479 +       }
13480 +
13481 +       h_parent = NULL;
13482 +       sb = inode->i_sb;
13483 +       err = si_read_lock(sb, AuLock_FLUSH);
13484 +       if (unlikely(err))
13485 +               goto out;
13486 +
13487 +#ifdef CONFIG_AUFS_DEBUG
13488 +       if (unlikely(!au_opt_test(au_mntflags(sb), XINO)))
13489 +               AuWarn1("NFS-exporting requires xino\n");
13490 +#endif
13491 +       err = -EIO;
13492 +       parent = NULL;
13493 +       ii_read_lock_child(inode);
13494 +       bindex = au_ibtop(inode);
13495 +       if (!dir) {
13496 +               dentry = d_find_any_alias(inode);
13497 +               if (unlikely(!dentry))
13498 +                       goto out_unlock;
13499 +               AuDebugOn(au_test_anon(dentry));
13500 +               parent = dget_parent(dentry);
13501 +               dput(dentry);
13502 +               if (unlikely(!parent))
13503 +                       goto out_unlock;
13504 +               if (d_really_is_positive(parent))
13505 +                       dir = d_inode(parent);
13506 +       }
13507 +
13508 +       ii_read_lock_parent(dir);
13509 +       h_dir = au_h_iptr(dir, bindex);
13510 +       ii_read_unlock(dir);
13511 +       if (unlikely(!h_dir))
13512 +               goto out_parent;
13513 +       h_parent = d_find_any_alias(h_dir);
13514 +       if (unlikely(!h_parent))
13515 +               goto out_hparent;
13516 +
13517 +       err = -EPERM;
13518 +       br = au_sbr(sb, bindex);
13519 +       h_sb = au_br_sb(br);
13520 +       if (unlikely(!h_sb->s_export_op)) {
13521 +               AuErr1("%s branch is not exportable\n", au_sbtype(h_sb));
13522 +               goto out_hparent;
13523 +       }
13524 +
13525 +       fh[Fh_br_id] = br->br_id;
13526 +       fh[Fh_sigen] = au_sigen(sb);
13527 +       encode_ino(fh + Fh_ino, inode->i_ino);
13528 +       encode_ino(fh + Fh_dir_ino, dir->i_ino);
13529 +       fh[Fh_igen] = inode->i_generation;
13530 +
13531 +       *max_len -= Fh_tail;
13532 +       fh[Fh_h_type] = exportfs_encode_fh(h_parent, (void *)(fh + Fh_tail),
13533 +                                          max_len,
13534 +                                          /*connectable or subtreecheck*/0);
13535 +       err = fh[Fh_h_type];
13536 +       *max_len += Fh_tail;
13537 +       /* todo: macros? */
13538 +       if (err != FILEID_INVALID)
13539 +               err = 99;
13540 +       else
13541 +               AuWarn1("%s encode_fh failed\n", au_sbtype(h_sb));
13542 +
13543 +out_hparent:
13544 +       dput(h_parent);
13545 +out_parent:
13546 +       dput(parent);
13547 +out_unlock:
13548 +       ii_read_unlock(inode);
13549 +       si_read_unlock(sb);
13550 +out:
13551 +       if (unlikely(err < 0))
13552 +               err = FILEID_INVALID;
13553 +       return err;
13554 +}
13555 +
13556 +/* ---------------------------------------------------------------------- */
13557 +
13558 +static int aufs_commit_metadata(struct inode *inode)
13559 +{
13560 +       int err;
13561 +       aufs_bindex_t bindex;
13562 +       struct super_block *sb;
13563 +       struct inode *h_inode;
13564 +       int (*f)(struct inode *inode);
13565 +
13566 +       sb = inode->i_sb;
13567 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
13568 +       ii_write_lock_child(inode);
13569 +       bindex = au_ibtop(inode);
13570 +       AuDebugOn(bindex < 0);
13571 +       h_inode = au_h_iptr(inode, bindex);
13572 +
13573 +       f = h_inode->i_sb->s_export_op->commit_metadata;
13574 +       if (f)
13575 +               err = f(h_inode);
13576 +       else
13577 +               err = sync_inode_metadata(h_inode, /*wait*/1);
13578 +
13579 +       au_cpup_attr_timesizes(inode);
13580 +       ii_write_unlock(inode);
13581 +       si_read_unlock(sb);
13582 +       return err;
13583 +}
13584 +
13585 +/* ---------------------------------------------------------------------- */
13586 +
13587 +static struct export_operations aufs_export_op = {
13588 +       .fh_to_dentry           = aufs_fh_to_dentry,
13589 +       /* .fh_to_parent        = aufs_fh_to_parent, */
13590 +       .encode_fh              = aufs_encode_fh,
13591 +       .commit_metadata        = aufs_commit_metadata
13592 +};
13593 +
13594 +void au_export_init(struct super_block *sb)
13595 +{
13596 +       struct au_sbinfo *sbinfo;
13597 +       __u32 u;
13598 +
13599 +       BUILD_BUG_ON_MSG(IS_BUILTIN(CONFIG_AUFS_FS)
13600 +                        && IS_MODULE(CONFIG_EXPORTFS),
13601 +                        AUFS_NAME ": unsupported configuration "
13602 +                        "CONFIG_EXPORTFS=m and CONFIG_AUFS_FS=y");
13603 +
13604 +       sb->s_export_op = &aufs_export_op;
13605 +       sbinfo = au_sbi(sb);
13606 +       sbinfo->si_xigen = NULL;
13607 +       get_random_bytes(&u, sizeof(u));
13608 +       BUILD_BUG_ON(sizeof(u) != sizeof(int));
13609 +       atomic_set(&sbinfo->si_xigen_next, u);
13610 +}
13611 diff -urN /usr/share/empty/fs/aufs/fhsm.c linux/fs/aufs/fhsm.c
13612 --- /usr/share/empty/fs/aufs/fhsm.c     1970-01-01 01:00:00.000000000 +0100
13613 +++ linux/fs/aufs/fhsm.c        2022-03-21 14:49:05.723299677 +0100
13614 @@ -0,0 +1,427 @@
13615 +// SPDX-License-Identifier: GPL-2.0
13616 +/*
13617 + * Copyright (C) 2011-2021 Junjiro R. Okajima
13618 + *
13619 + * This program, aufs is free software; you can redistribute it and/or modify
13620 + * it under the terms of the GNU General Public License as published by
13621 + * the Free Software Foundation; either version 2 of the License, or
13622 + * (at your option) any later version.
13623 + *
13624 + * This program is distributed in the hope that it will be useful,
13625 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
13626 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13627 + * GNU General Public License for more details.
13628 + *
13629 + * You should have received a copy of the GNU General Public License
13630 + * along with this program; if not, write to the Free Software
13631 + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
13632 + */
13633 +
13634 +/*
13635 + * File-based Hierarchy Storage Management
13636 + */
13637 +
13638 +#include <linux/anon_inodes.h>
13639 +#include <linux/poll.h>
13640 +#include <linux/seq_file.h>
13641 +#include <linux/statfs.h>
13642 +#include "aufs.h"
13643 +
13644 +static aufs_bindex_t au_fhsm_bottom(struct super_block *sb)
13645 +{
13646 +       struct au_sbinfo *sbinfo;
13647 +       struct au_fhsm *fhsm;
13648 +
13649 +       SiMustAnyLock(sb);
13650 +
13651 +       sbinfo = au_sbi(sb);
13652 +       fhsm = &sbinfo->si_fhsm;
13653 +       AuDebugOn(!fhsm);
13654 +       return fhsm->fhsm_bottom;
13655 +}
13656 +
13657 +void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex)
13658 +{
13659 +       struct au_sbinfo *sbinfo;
13660 +       struct au_fhsm *fhsm;
13661 +
13662 +       SiMustWriteLock(sb);
13663 +
13664 +       sbinfo = au_sbi(sb);
13665 +       fhsm = &sbinfo->si_fhsm;
13666 +       AuDebugOn(!fhsm);
13667 +       fhsm->fhsm_bottom = bindex;
13668 +}
13669 +
13670 +/* ---------------------------------------------------------------------- */
13671 +
13672 +static int au_fhsm_test_jiffy(struct au_sbinfo *sbinfo, struct au_branch *br)
13673 +{
13674 +       struct au_br_fhsm *bf;
13675 +
13676 +       bf = br->br_fhsm;
13677 +       MtxMustLock(&bf->bf_lock);
13678 +
13679 +       return !bf->bf_readable
13680 +               || time_after(jiffies,
13681 +                             bf->bf_jiffy + sbinfo->si_fhsm.fhsm_expire);
13682 +}
13683 +
13684 +/* ---------------------------------------------------------------------- */
13685 +
13686 +static void au_fhsm_notify(struct super_block *sb, int val)
13687 +{
13688 +       struct au_sbinfo *sbinfo;
13689 +       struct au_fhsm *fhsm;
13690 +
13691 +       SiMustAnyLock(sb);
13692 +
13693 +       sbinfo = au_sbi(sb);
13694 +       fhsm = &sbinfo->si_fhsm;
13695 +       if (au_fhsm_pid(fhsm)
13696 +           && atomic_read(&fhsm->fhsm_readable) != -1) {
13697 +               atomic_set(&fhsm->fhsm_readable, val);
13698 +               if (val)
13699 +                       wake_up(&fhsm->fhsm_wqh);
13700 +       }
13701 +}
13702 +
13703 +static int au_fhsm_stfs(struct super_block *sb, aufs_bindex_t bindex,
13704 +                       struct aufs_stfs *rstfs, int do_lock, int do_notify)
13705 +{
13706 +       int err;
13707 +       struct au_branch *br;
13708 +       struct au_br_fhsm *bf;
13709 +
13710 +       br = au_sbr(sb, bindex);
13711 +       AuDebugOn(au_br_rdonly(br));
13712 +       bf = br->br_fhsm;
13713 +       AuDebugOn(!bf);
13714 +
13715 +       if (do_lock)
13716 +               mutex_lock(&bf->bf_lock);
13717 +       else
13718 +               MtxMustLock(&bf->bf_lock);
13719 +
13720 +       /* sb->s_root for NFS is unreliable */
13721 +       err = au_br_stfs(br, &bf->bf_stfs);
13722 +       if (unlikely(err)) {
13723 +               AuErr1("FHSM failed (%d), b%d, ignored.\n", bindex, err);
13724 +               goto out;
13725 +       }
13726 +
13727 +       bf->bf_jiffy = jiffies;
13728 +       bf->bf_readable = 1;
13729 +       if (do_notify)
13730 +               au_fhsm_notify(sb, /*val*/1);
13731 +       if (rstfs)
13732 +               *rstfs = bf->bf_stfs;
13733 +
13734 +out:
13735 +       if (do_lock)
13736 +               mutex_unlock(&bf->bf_lock);
13737 +       au_fhsm_notify(sb, /*val*/1);
13738 +
13739 +       return err;
13740 +}
13741 +
13742 +void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force)
13743 +{
13744 +       int err;
13745 +       struct au_sbinfo *sbinfo;
13746 +       struct au_fhsm *fhsm;
13747 +       struct au_branch *br;
13748 +       struct au_br_fhsm *bf;
13749 +
13750 +       AuDbg("b%d, force %d\n", bindex, force);
13751 +       SiMustAnyLock(sb);
13752 +
13753 +       sbinfo = au_sbi(sb);
13754 +       fhsm = &sbinfo->si_fhsm;
13755 +       if (!au_ftest_si(sbinfo, FHSM)
13756 +           || fhsm->fhsm_bottom == bindex)
13757 +               return;
13758 +
13759 +       br = au_sbr(sb, bindex);
13760 +       bf = br->br_fhsm;
13761 +       AuDebugOn(!bf);
13762 +       mutex_lock(&bf->bf_lock);
13763 +       if (force
13764 +           || au_fhsm_pid(fhsm)
13765 +           || au_fhsm_test_jiffy(sbinfo, br))
13766 +               err = au_fhsm_stfs(sb, bindex, /*rstfs*/NULL, /*do_lock*/0,
13767 +                                 /*do_notify*/1);
13768 +       mutex_unlock(&bf->bf_lock);
13769 +}
13770 +
13771 +void au_fhsm_wrote_all(struct super_block *sb, int force)
13772 +{
13773 +       aufs_bindex_t bindex, bbot;
13774 +       struct au_branch *br;
13775 +
13776 +       /* exclude the bottom */
13777 +       bbot = au_fhsm_bottom(sb);
13778 +       for (bindex = 0; bindex < bbot; bindex++) {
13779 +               br = au_sbr(sb, bindex);
13780 +               if (au_br_fhsm(br->br_perm))
13781 +                       au_fhsm_wrote(sb, bindex, force);
13782 +       }
13783 +}
13784 +
13785 +/* ---------------------------------------------------------------------- */
13786 +
13787 +static __poll_t au_fhsm_poll(struct file *file, struct poll_table_struct *wait)
13788 +{
13789 +       __poll_t mask;
13790 +       struct au_sbinfo *sbinfo;
13791 +       struct au_fhsm *fhsm;
13792 +
13793 +       mask = 0;
13794 +       sbinfo = file->private_data;
13795 +       fhsm = &sbinfo->si_fhsm;
13796 +       poll_wait(file, &fhsm->fhsm_wqh, wait);
13797 +       if (atomic_read(&fhsm->fhsm_readable))
13798 +               mask = EPOLLIN /* | EPOLLRDNORM */;
13799 +
13800 +       if (!mask)
13801 +               AuDbg("mask 0x%x\n", mask);
13802 +       return mask;
13803 +}
13804 +
13805 +static int au_fhsm_do_read_one(struct aufs_stbr __user *stbr,
13806 +                             struct aufs_stfs *stfs, __s16 brid)
13807 +{
13808 +       int err;
13809 +
13810 +       err = copy_to_user(&stbr->stfs, stfs, sizeof(*stfs));
13811 +       if (!err)
13812 +               err = __put_user(brid, &stbr->brid);
13813 +       if (unlikely(err))
13814 +               err = -EFAULT;
13815 +
13816 +       return err;
13817 +}
13818 +
13819 +static ssize_t au_fhsm_do_read(struct super_block *sb,
13820 +                              struct aufs_stbr __user *stbr, size_t count)
13821 +{
13822 +       ssize_t err;
13823 +       int nstbr;
13824 +       aufs_bindex_t bindex, bbot;
13825 +       struct au_branch *br;
13826 +       struct au_br_fhsm *bf;
13827 +
13828 +       /* except the bottom branch */
13829 +       err = 0;
13830 +       nstbr = 0;
13831 +       bbot = au_fhsm_bottom(sb);
13832 +       for (bindex = 0; !err && bindex < bbot; bindex++) {
13833 +               br = au_sbr(sb, bindex);
13834 +               if (!au_br_fhsm(br->br_perm))
13835 +                       continue;
13836 +
13837 +               bf = br->br_fhsm;
13838 +               mutex_lock(&bf->bf_lock);
13839 +               if (bf->bf_readable) {
13840 +                       err = -EFAULT;
13841 +                       if (count >= sizeof(*stbr))
13842 +                               err = au_fhsm_do_read_one(stbr++, &bf->bf_stfs,
13843 +                                                         br->br_id);
13844 +                       if (!err) {
13845 +                               bf->bf_readable = 0;
13846 +                               count -= sizeof(*stbr);
13847 +                               nstbr++;
13848 +                       }
13849 +               }
13850 +               mutex_unlock(&bf->bf_lock);
13851 +       }
13852 +       if (!err)
13853 +               err = sizeof(*stbr) * nstbr;
13854 +
13855 +       return err;
13856 +}
13857 +
13858 +static ssize_t au_fhsm_read(struct file *file, char __user *buf, size_t count,
13859 +                          loff_t *pos)
13860 +{
13861 +       ssize_t err;
13862 +       int readable;
13863 +       aufs_bindex_t nfhsm, bindex, bbot;
13864 +       struct au_sbinfo *sbinfo;
13865 +       struct au_fhsm *fhsm;
13866 +       struct au_branch *br;
13867 +       struct super_block *sb;
13868 +
13869 +       err = 0;
13870 +       sbinfo = file->private_data;
13871 +       fhsm = &sbinfo->si_fhsm;
13872 +need_data:
13873 +       spin_lock_irq(&fhsm->fhsm_wqh.lock);
13874 +       if (!atomic_read(&fhsm->fhsm_readable)) {
13875 +               if (vfsub_file_flags(file) & O_NONBLOCK)
13876 +                       err = -EAGAIN;
13877 +               else
13878 +                       err = wait_event_interruptible_locked_irq
13879 +                               (fhsm->fhsm_wqh,
13880 +                                atomic_read(&fhsm->fhsm_readable));
13881 +       }
13882 +       spin_unlock_irq(&fhsm->fhsm_wqh.lock);
13883 +       if (unlikely(err))
13884 +               goto out;
13885 +
13886 +       /* sb may already be dead */
13887 +       au_rw_read_lock(&sbinfo->si_rwsem);
13888 +       readable = atomic_read(&fhsm->fhsm_readable);
13889 +       if (readable > 0) {
13890 +               sb = sbinfo->si_sb;
13891 +               AuDebugOn(!sb);
13892 +               /* exclude the bottom branch */
13893 +               nfhsm = 0;
13894 +               bbot = au_fhsm_bottom(sb);
13895 +               for (bindex = 0; bindex < bbot; bindex++) {
13896 +                       br = au_sbr(sb, bindex);
13897 +                       if (au_br_fhsm(br->br_perm))
13898 +                               nfhsm++;
13899 +               }
13900 +               err = -EMSGSIZE;
13901 +               if (nfhsm * sizeof(struct aufs_stbr) <= count) {
13902 +                       atomic_set(&fhsm->fhsm_readable, 0);
13903 +                       err = au_fhsm_do_read(sbinfo->si_sb, (void __user *)buf,
13904 +                                            count);
13905 +               }
13906 +       }
13907 +       au_rw_read_unlock(&sbinfo->si_rwsem);
13908 +       if (!readable)
13909 +               goto need_data;
13910 +
13911 +out:
13912 +       return err;
13913 +}
13914 +
13915 +static int au_fhsm_release(struct inode *inode, struct file *file)
13916 +{
13917 +       struct au_sbinfo *sbinfo;
13918 +       struct au_fhsm *fhsm;
13919 +
13920 +       /* sb may already be dead */
13921 +       sbinfo = file->private_data;
13922 +       fhsm = &sbinfo->si_fhsm;
13923 +       spin_lock(&fhsm->fhsm_spin);
13924 +       fhsm->fhsm_pid = 0;
13925 +       spin_unlock(&fhsm->fhsm_spin);
13926 +       kobject_put(&sbinfo->si_kobj);
13927 +
13928 +       return 0;
13929 +}
13930 +
13931 +static const struct file_operations au_fhsm_fops = {
13932 +       .owner          = THIS_MODULE,
13933 +       .llseek         = noop_llseek,
13934 +       .read           = au_fhsm_read,
13935 +       .poll           = au_fhsm_poll,
13936 +       .release        = au_fhsm_release
13937 +};
13938 +
13939 +int au_fhsm_fd(struct super_block *sb, int oflags)
13940 +{
13941 +       int err, fd;
13942 +       struct au_sbinfo *sbinfo;
13943 +       struct au_fhsm *fhsm;
13944 +
13945 +       err = -EPERM;
13946 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
13947 +               goto out;
13948 +
13949 +       err = -EINVAL;
13950 +       if (unlikely(oflags & ~(O_CLOEXEC | O_NONBLOCK)))
13951 +               goto out;
13952 +
13953 +       err = 0;
13954 +       sbinfo = au_sbi(sb);
13955 +       fhsm = &sbinfo->si_fhsm;
13956 +       spin_lock(&fhsm->fhsm_spin);
13957 +       if (!fhsm->fhsm_pid)
13958 +               fhsm->fhsm_pid = current->pid;
13959 +       else
13960 +               err = -EBUSY;
13961 +       spin_unlock(&fhsm->fhsm_spin);
13962 +       if (unlikely(err))
13963 +               goto out;
13964 +
13965 +       oflags |= O_RDONLY;
13966 +       /* oflags |= FMODE_NONOTIFY; */
13967 +       fd = anon_inode_getfd("[aufs_fhsm]", &au_fhsm_fops, sbinfo, oflags);
13968 +       err = fd;
13969 +       if (unlikely(fd < 0))
13970 +               goto out_pid;
13971 +
13972 +       /* succeed regardless 'fhsm' status */
13973 +       kobject_get(&sbinfo->si_kobj);
13974 +       si_noflush_read_lock(sb);
13975 +       if (au_ftest_si(sbinfo, FHSM))
13976 +               au_fhsm_wrote_all(sb, /*force*/0);
13977 +       si_read_unlock(sb);
13978 +       goto out; /* success */
13979 +
13980 +out_pid:
13981 +       spin_lock(&fhsm->fhsm_spin);
13982 +       fhsm->fhsm_pid = 0;
13983 +       spin_unlock(&fhsm->fhsm_spin);
13984 +out:
13985 +       AuTraceErr(err);
13986 +       return err;
13987 +}
13988 +
13989 +/* ---------------------------------------------------------------------- */
13990 +
13991 +int au_fhsm_br_alloc(struct au_branch *br)
13992 +{
13993 +       int err;
13994 +
13995 +       err = 0;
13996 +       br->br_fhsm = kmalloc(sizeof(*br->br_fhsm), GFP_NOFS);
13997 +       if (br->br_fhsm)
13998 +               au_br_fhsm_init(br->br_fhsm);
13999 +       else
14000 +               err = -ENOMEM;
14001 +
14002 +       return err;
14003 +}
14004 +
14005 +/* ---------------------------------------------------------------------- */
14006 +
14007 +void au_fhsm_fin(struct super_block *sb)
14008 +{
14009 +       au_fhsm_notify(sb, /*val*/-1);
14010 +}
14011 +
14012 +void au_fhsm_init(struct au_sbinfo *sbinfo)
14013 +{
14014 +       struct au_fhsm *fhsm;
14015 +
14016 +       fhsm = &sbinfo->si_fhsm;
14017 +       spin_lock_init(&fhsm->fhsm_spin);
14018 +       init_waitqueue_head(&fhsm->fhsm_wqh);
14019 +       atomic_set(&fhsm->fhsm_readable, 0);
14020 +       fhsm->fhsm_expire
14021 +               = msecs_to_jiffies(AUFS_FHSM_CACHE_DEF_SEC * MSEC_PER_SEC);
14022 +       fhsm->fhsm_bottom = -1;
14023 +}
14024 +
14025 +void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec)
14026 +{
14027 +       sbinfo->si_fhsm.fhsm_expire
14028 +               = msecs_to_jiffies(sec * MSEC_PER_SEC);
14029 +}
14030 +
14031 +void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo)
14032 +{
14033 +       unsigned int u;
14034 +
14035 +       if (!au_ftest_si(sbinfo, FHSM))
14036 +               return;
14037 +
14038 +       u = jiffies_to_msecs(sbinfo->si_fhsm.fhsm_expire) / MSEC_PER_SEC;
14039 +       if (u != AUFS_FHSM_CACHE_DEF_SEC)
14040 +               seq_printf(seq, ",fhsm_sec=%u", u);
14041 +}
14042 diff -urN /usr/share/empty/fs/aufs/file.c linux/fs/aufs/file.c
14043 --- /usr/share/empty/fs/aufs/file.c     1970-01-01 01:00:00.000000000 +0100
14044 +++ linux/fs/aufs/file.c        2022-05-23 09:44:37.729839166 +0200
14045 @@ -0,0 +1,861 @@
14046 +// SPDX-License-Identifier: GPL-2.0
14047 +/*
14048 + * Copyright (C) 2005-2021 Junjiro R. Okajima
14049 + *
14050 + * This program, aufs is free software; you can redistribute it and/or modify
14051 + * it under the terms of the GNU General Public License as published by
14052 + * the Free Software Foundation; either version 2 of the License, or
14053 + * (at your option) any later version.
14054 + *
14055 + * This program is distributed in the hope that it will be useful,
14056 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
14057 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14058 + * GNU General Public License for more details.
14059 + *
14060 + * You should have received a copy of the GNU General Public License
14061 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
14062 + */
14063 +
14064 +/*
14065 + * handling file/dir, and address_space operation
14066 + */
14067 +
14068 +#ifdef CONFIG_AUFS_DEBUG
14069 +#include <linux/migrate.h>
14070 +#endif
14071 +#include <linux/pagemap.h>
14072 +#include "aufs.h"
14073 +
14074 +/* drop flags for writing */
14075 +unsigned int au_file_roflags(unsigned int flags)
14076 +{
14077 +       flags &= ~(O_WRONLY | O_RDWR | O_APPEND | O_CREAT | O_TRUNC);
14078 +       flags |= O_RDONLY | O_NOATIME;
14079 +       return flags;
14080 +}
14081 +
14082 +/* common functions to regular file and dir */
14083 +struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
14084 +                      struct file *file, int force_wr)
14085 +{
14086 +       struct file *h_file;
14087 +       struct dentry *h_dentry;
14088 +       struct inode *h_inode;
14089 +       struct super_block *sb;
14090 +       struct au_branch *br;
14091 +       struct path h_path;
14092 +       int err;
14093 +
14094 +       /* a race condition can happen between open and unlink/rmdir */
14095 +       h_file = ERR_PTR(-ENOENT);
14096 +       h_dentry = au_h_dptr(dentry, bindex);
14097 +       if (au_test_nfsd() && (!h_dentry || d_is_negative(h_dentry)))
14098 +               goto out;
14099 +       h_inode = d_inode(h_dentry);
14100 +       spin_lock(&h_dentry->d_lock);
14101 +       err = (!d_unhashed(dentry) && d_unlinked(h_dentry))
14102 +               /* || !d_inode(dentry)->i_nlink */
14103 +               ;
14104 +       spin_unlock(&h_dentry->d_lock);
14105 +       if (unlikely(err))
14106 +               goto out;
14107 +
14108 +       sb = dentry->d_sb;
14109 +       br = au_sbr(sb, bindex);
14110 +       err = au_br_test_oflag(flags, br);
14111 +       h_file = ERR_PTR(err);
14112 +       if (unlikely(err))
14113 +               goto out;
14114 +
14115 +       /* drop flags for writing */
14116 +       if (au_test_ro(sb, bindex, d_inode(dentry))) {
14117 +               if (force_wr && !(flags & O_WRONLY))
14118 +                       force_wr = 0;
14119 +               flags = au_file_roflags(flags);
14120 +               if (force_wr) {
14121 +                       h_file = ERR_PTR(-EROFS);
14122 +                       flags = au_file_roflags(flags);
14123 +                       if (unlikely(vfsub_native_ro(h_inode)
14124 +                                    || IS_APPEND(h_inode)))
14125 +                               goto out;
14126 +                       flags &= ~O_ACCMODE;
14127 +                       flags |= O_WRONLY;
14128 +               }
14129 +       }
14130 +       flags &= ~O_CREAT;
14131 +       au_lcnt_inc(&br->br_nfiles);
14132 +       h_path.dentry = h_dentry;
14133 +       h_path.mnt = au_br_mnt(br);
14134 +       h_file = vfsub_dentry_open(&h_path, flags);
14135 +       if (IS_ERR(h_file))
14136 +               goto out_br;
14137 +
14138 +       if (flags & __FMODE_EXEC) {
14139 +               err = deny_write_access(h_file);
14140 +               if (unlikely(err)) {
14141 +                       fput(h_file);
14142 +                       h_file = ERR_PTR(err);
14143 +                       goto out_br;
14144 +               }
14145 +       }
14146 +       fsnotify_open(h_file);
14147 +       goto out; /* success */
14148 +
14149 +out_br:
14150 +       au_lcnt_dec(&br->br_nfiles);
14151 +out:
14152 +       return h_file;
14153 +}
14154 +
14155 +static int au_cmoo(struct dentry *dentry)
14156 +{
14157 +       int err, cmoo, matched;
14158 +       unsigned int udba;
14159 +       struct path h_path;
14160 +       struct au_pin pin;
14161 +       struct au_cp_generic cpg = {
14162 +               .dentry = dentry,
14163 +               .bdst   = -1,
14164 +               .bsrc   = -1,
14165 +               .len    = -1,
14166 +               .pin    = &pin,
14167 +               .flags  = AuCpup_DTIME | AuCpup_HOPEN
14168 +       };
14169 +       struct inode *delegated;
14170 +       struct super_block *sb;
14171 +       struct au_sbinfo *sbinfo;
14172 +       struct au_fhsm *fhsm;
14173 +       pid_t pid;
14174 +       struct au_branch *br;
14175 +       struct dentry *parent;
14176 +       struct au_hinode *hdir;
14177 +
14178 +       DiMustWriteLock(dentry);
14179 +       IiMustWriteLock(d_inode(dentry));
14180 +
14181 +       err = 0;
14182 +       if (IS_ROOT(dentry))
14183 +               goto out;
14184 +       cpg.bsrc = au_dbtop(dentry);
14185 +       if (!cpg.bsrc)
14186 +               goto out;
14187 +
14188 +       sb = dentry->d_sb;
14189 +       sbinfo = au_sbi(sb);
14190 +       fhsm = &sbinfo->si_fhsm;
14191 +       pid = au_fhsm_pid(fhsm);
14192 +       rcu_read_lock();
14193 +       matched = (pid
14194 +                  && (current->pid == pid
14195 +                      || rcu_dereference(current->real_parent)->pid == pid));
14196 +       rcu_read_unlock();
14197 +       if (matched)
14198 +               goto out;
14199 +
14200 +       br = au_sbr(sb, cpg.bsrc);
14201 +       cmoo = au_br_cmoo(br->br_perm);
14202 +       if (!cmoo)
14203 +               goto out;
14204 +       if (!d_is_reg(dentry))
14205 +               cmoo &= AuBrAttr_COO_ALL;
14206 +       if (!cmoo)
14207 +               goto out;
14208 +
14209 +       parent = dget_parent(dentry);
14210 +       di_write_lock_parent(parent);
14211 +       err = au_wbr_do_copyup_bu(dentry, cpg.bsrc - 1);
14212 +       cpg.bdst = err;
14213 +       if (unlikely(err < 0)) {
14214 +               err = 0;        /* there is no upper writable branch */
14215 +               goto out_dgrade;
14216 +       }
14217 +       AuDbg("bsrc %d, bdst %d\n", cpg.bsrc, cpg.bdst);
14218 +
14219 +       /* do not respect the coo attrib for the target branch */
14220 +       err = au_cpup_dirs(dentry, cpg.bdst);
14221 +       if (unlikely(err))
14222 +               goto out_dgrade;
14223 +
14224 +       di_downgrade_lock(parent, AuLock_IR);
14225 +       udba = au_opt_udba(sb);
14226 +       err = au_pin(&pin, dentry, cpg.bdst, udba,
14227 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14228 +       if (unlikely(err))
14229 +               goto out_parent;
14230 +
14231 +       err = au_sio_cpup_simple(&cpg);
14232 +       au_unpin(&pin);
14233 +       if (unlikely(err))
14234 +               goto out_parent;
14235 +       if (!(cmoo & AuBrWAttr_MOO))
14236 +               goto out_parent; /* success */
14237 +
14238 +       err = au_pin(&pin, dentry, cpg.bsrc, udba,
14239 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14240 +       if (unlikely(err))
14241 +               goto out_parent;
14242 +
14243 +       h_path.mnt = au_br_mnt(br);
14244 +       h_path.dentry = au_h_dptr(dentry, cpg.bsrc);
14245 +       hdir = au_hi(d_inode(parent), cpg.bsrc);
14246 +       delegated = NULL;
14247 +       err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated, /*force*/1);
14248 +       au_unpin(&pin);
14249 +       /* todo: keep h_dentry or not? */
14250 +       if (unlikely(err == -EWOULDBLOCK)) {
14251 +               pr_warn("cannot retry for NFSv4 delegation"
14252 +                       " for an internal unlink\n");
14253 +               iput(delegated);
14254 +       }
14255 +       if (unlikely(err)) {
14256 +               pr_err("unlink %pd after coo failed (%d), ignored\n",
14257 +                      dentry, err);
14258 +               err = 0;
14259 +       }
14260 +       goto out_parent; /* success */
14261 +
14262 +out_dgrade:
14263 +       di_downgrade_lock(parent, AuLock_IR);
14264 +out_parent:
14265 +       di_read_unlock(parent, AuLock_IR);
14266 +       dput(parent);
14267 +out:
14268 +       AuTraceErr(err);
14269 +       return err;
14270 +}
14271 +
14272 +int au_do_open(struct file *file, struct au_do_open_args *args)
14273 +{
14274 +       int err, aopen = args->aopen;
14275 +       struct dentry *dentry;
14276 +       struct au_finfo *finfo;
14277 +
14278 +       if (!aopen)
14279 +               err = au_finfo_init(file, args->fidir);
14280 +       else {
14281 +               lockdep_off();
14282 +               err = au_finfo_init(file, args->fidir);
14283 +               lockdep_on();
14284 +       }
14285 +       if (unlikely(err))
14286 +               goto out;
14287 +
14288 +       dentry = file->f_path.dentry;
14289 +       AuDebugOn(IS_ERR_OR_NULL(dentry));
14290 +       di_write_lock_child(dentry);
14291 +       err = au_cmoo(dentry);
14292 +       di_downgrade_lock(dentry, AuLock_IR);
14293 +       if (!err) {
14294 +               if (!aopen)
14295 +                       err = args->open(file, vfsub_file_flags(file), NULL);
14296 +               else {
14297 +                       lockdep_off();
14298 +                       err = args->open(file, vfsub_file_flags(file),
14299 +                                        args->h_file);
14300 +                       lockdep_on();
14301 +               }
14302 +       }
14303 +       di_read_unlock(dentry, AuLock_IR);
14304 +
14305 +       finfo = au_fi(file);
14306 +       if (!err) {
14307 +               finfo->fi_file = file;
14308 +               au_hbl_add(&finfo->fi_hlist,
14309 +                          &au_sbi(file->f_path.dentry->d_sb)->si_files);
14310 +       }
14311 +       if (!aopen)
14312 +               fi_write_unlock(file);
14313 +       else {
14314 +               lockdep_off();
14315 +               fi_write_unlock(file);
14316 +               lockdep_on();
14317 +       }
14318 +       if (unlikely(err)) {
14319 +               finfo->fi_hdir = NULL;
14320 +               au_finfo_fin(file);
14321 +       }
14322 +
14323 +out:
14324 +       AuTraceErr(err);
14325 +       return err;
14326 +}
14327 +
14328 +int au_reopen_nondir(struct file *file)
14329 +{
14330 +       int err;
14331 +       aufs_bindex_t btop;
14332 +       struct dentry *dentry;
14333 +       struct au_branch *br;
14334 +       struct file *h_file, *h_file_tmp;
14335 +
14336 +       dentry = file->f_path.dentry;
14337 +       btop = au_dbtop(dentry);
14338 +       br = au_sbr(dentry->d_sb, btop);
14339 +       h_file_tmp = NULL;
14340 +       if (au_fbtop(file) == btop) {
14341 +               h_file = au_hf_top(file);
14342 +               if (file->f_mode == h_file->f_mode)
14343 +                       return 0; /* success */
14344 +               h_file_tmp = h_file;
14345 +               get_file(h_file_tmp);
14346 +               au_lcnt_inc(&br->br_nfiles);
14347 +               au_set_h_fptr(file, btop, NULL);
14348 +       }
14349 +       AuDebugOn(au_fi(file)->fi_hdir);
14350 +       /*
14351 +        * it can happen
14352 +        * file exists on both of rw and ro
14353 +        * open --> dbtop and fbtop are both 0
14354 +        * prepend a branch as rw, "rw" become ro
14355 +        * remove rw/file
14356 +        * delete the top branch, "rw" becomes rw again
14357 +        *      --> dbtop is 1, fbtop is still 0
14358 +        * write --> fbtop is 0 but dbtop is 1
14359 +        */
14360 +       /* AuDebugOn(au_fbtop(file) < btop); */
14361 +
14362 +       h_file = au_h_open(dentry, btop, vfsub_file_flags(file) & ~O_TRUNC,
14363 +                          file, /*force_wr*/0);
14364 +       err = PTR_ERR(h_file);
14365 +       if (IS_ERR(h_file)) {
14366 +               if (h_file_tmp) {
14367 +                       /* revert */
14368 +                       au_set_h_fptr(file, btop, h_file_tmp);
14369 +                       h_file_tmp = NULL;
14370 +               }
14371 +               goto out; /* todo: close all? */
14372 +       }
14373 +
14374 +       err = 0;
14375 +       au_set_fbtop(file, btop);
14376 +       au_set_h_fptr(file, btop, h_file);
14377 +       au_update_figen(file);
14378 +       /* todo: necessary? */
14379 +       /* file->f_ra = h_file->f_ra; */
14380 +
14381 +out:
14382 +       if (h_file_tmp) {
14383 +               fput(h_file_tmp);
14384 +               au_lcnt_dec(&br->br_nfiles);
14385 +       }
14386 +       return err;
14387 +}
14388 +
14389 +/* ---------------------------------------------------------------------- */
14390 +
14391 +static int au_reopen_wh(struct file *file, aufs_bindex_t btgt,
14392 +                       struct dentry *hi_wh)
14393 +{
14394 +       int err;
14395 +       aufs_bindex_t btop;
14396 +       struct au_dinfo *dinfo;
14397 +       struct dentry *h_dentry;
14398 +       struct au_hdentry *hdp;
14399 +
14400 +       dinfo = au_di(file->f_path.dentry);
14401 +       AuRwMustWriteLock(&dinfo->di_rwsem);
14402 +
14403 +       btop = dinfo->di_btop;
14404 +       dinfo->di_btop = btgt;
14405 +       hdp = au_hdentry(dinfo, btgt);
14406 +       h_dentry = hdp->hd_dentry;
14407 +       hdp->hd_dentry = hi_wh;
14408 +       err = au_reopen_nondir(file);
14409 +       hdp->hd_dentry = h_dentry;
14410 +       dinfo->di_btop = btop;
14411 +
14412 +       return err;
14413 +}
14414 +
14415 +static int au_ready_to_write_wh(struct file *file, loff_t len,
14416 +                               aufs_bindex_t bcpup, struct au_pin *pin)
14417 +{
14418 +       int err;
14419 +       struct inode *inode, *h_inode;
14420 +       struct dentry *h_dentry, *hi_wh;
14421 +       struct au_cp_generic cpg = {
14422 +               .dentry = file->f_path.dentry,
14423 +               .bdst   = bcpup,
14424 +               .bsrc   = -1,
14425 +               .len    = len,
14426 +               .pin    = pin
14427 +       };
14428 +
14429 +       au_update_dbtop(cpg.dentry);
14430 +       inode = d_inode(cpg.dentry);
14431 +       h_inode = NULL;
14432 +       if (au_dbtop(cpg.dentry) <= bcpup
14433 +           && au_dbbot(cpg.dentry) >= bcpup) {
14434 +               h_dentry = au_h_dptr(cpg.dentry, bcpup);
14435 +               if (h_dentry && d_is_positive(h_dentry))
14436 +                       h_inode = d_inode(h_dentry);
14437 +       }
14438 +       hi_wh = au_hi_wh(inode, bcpup);
14439 +       if (!hi_wh && !h_inode)
14440 +               err = au_sio_cpup_wh(&cpg, file);
14441 +       else
14442 +               /* already copied-up after unlink */
14443 +               err = au_reopen_wh(file, bcpup, hi_wh);
14444 +
14445 +       if (!err
14446 +           && (inode->i_nlink > 1
14447 +               || (inode->i_state & I_LINKABLE))
14448 +           && au_opt_test(au_mntflags(cpg.dentry->d_sb), PLINK))
14449 +               au_plink_append(inode, bcpup, au_h_dptr(cpg.dentry, bcpup));
14450 +
14451 +       return err;
14452 +}
14453 +
14454 +/*
14455 + * prepare the @file for writing.
14456 + */
14457 +int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin)
14458 +{
14459 +       int err;
14460 +       aufs_bindex_t dbtop;
14461 +       struct dentry *parent;
14462 +       struct inode *inode;
14463 +       struct super_block *sb;
14464 +       struct file *h_file;
14465 +       struct au_cp_generic cpg = {
14466 +               .dentry = file->f_path.dentry,
14467 +               .bdst   = -1,
14468 +               .bsrc   = -1,
14469 +               .len    = len,
14470 +               .pin    = pin,
14471 +               .flags  = AuCpup_DTIME
14472 +       };
14473 +
14474 +       sb = cpg.dentry->d_sb;
14475 +       inode = d_inode(cpg.dentry);
14476 +       cpg.bsrc = au_fbtop(file);
14477 +       err = au_test_ro(sb, cpg.bsrc, inode);
14478 +       if (!err && (au_hf_top(file)->f_mode & FMODE_WRITE)) {
14479 +               err = au_pin(pin, cpg.dentry, cpg.bsrc, AuOpt_UDBA_NONE,
14480 +                            /*flags*/0);
14481 +               goto out;
14482 +       }
14483 +
14484 +       /* need to cpup or reopen */
14485 +       parent = dget_parent(cpg.dentry);
14486 +       di_write_lock_parent(parent);
14487 +       err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
14488 +       cpg.bdst = err;
14489 +       if (unlikely(err < 0))
14490 +               goto out_dgrade;
14491 +       err = 0;
14492 +
14493 +       if (!d_unhashed(cpg.dentry) && !au_h_dptr(parent, cpg.bdst)) {
14494 +               err = au_cpup_dirs(cpg.dentry, cpg.bdst);
14495 +               if (unlikely(err))
14496 +                       goto out_dgrade;
14497 +       }
14498 +
14499 +       err = au_pin(pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
14500 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14501 +       if (unlikely(err))
14502 +               goto out_dgrade;
14503 +
14504 +       dbtop = au_dbtop(cpg.dentry);
14505 +       if (dbtop <= cpg.bdst)
14506 +               cpg.bsrc = cpg.bdst;
14507 +
14508 +       if (dbtop <= cpg.bdst           /* just reopen */
14509 +           || !d_unhashed(cpg.dentry)  /* copyup and reopen */
14510 +               ) {
14511 +               h_file = au_h_open_pre(cpg.dentry, cpg.bsrc, /*force_wr*/0);
14512 +               if (IS_ERR(h_file))
14513 +                       err = PTR_ERR(h_file);
14514 +               else {
14515 +                       di_downgrade_lock(parent, AuLock_IR);
14516 +                       if (dbtop > cpg.bdst)
14517 +                               err = au_sio_cpup_simple(&cpg);
14518 +                       if (!err)
14519 +                               err = au_reopen_nondir(file);
14520 +                       au_h_open_post(cpg.dentry, cpg.bsrc, h_file);
14521 +               }
14522 +       } else {                        /* copyup as wh and reopen */
14523 +               /*
14524 +                * since writable hfsplus branch is not supported,
14525 +                * h_open_pre/post() are unnecessary.
14526 +                */
14527 +               err = au_ready_to_write_wh(file, len, cpg.bdst, pin);
14528 +               di_downgrade_lock(parent, AuLock_IR);
14529 +       }
14530 +
14531 +       if (!err) {
14532 +               au_pin_set_parent_lflag(pin, /*lflag*/0);
14533 +               goto out_dput; /* success */
14534 +       }
14535 +       au_unpin(pin);
14536 +       goto out_unlock;
14537 +
14538 +out_dgrade:
14539 +       di_downgrade_lock(parent, AuLock_IR);
14540 +out_unlock:
14541 +       di_read_unlock(parent, AuLock_IR);
14542 +out_dput:
14543 +       dput(parent);
14544 +out:
14545 +       return err;
14546 +}
14547 +
14548 +/* ---------------------------------------------------------------------- */
14549 +
14550 +int au_do_flush(struct file *file, fl_owner_t id,
14551 +               int (*flush)(struct file *file, fl_owner_t id))
14552 +{
14553 +       int err;
14554 +       struct super_block *sb;
14555 +       struct inode *inode;
14556 +
14557 +       inode = file_inode(file);
14558 +       sb = inode->i_sb;
14559 +       si_noflush_read_lock(sb);
14560 +       fi_read_lock(file);
14561 +       ii_read_lock_child(inode);
14562 +
14563 +       err = flush(file, id);
14564 +       au_cpup_attr_timesizes(inode);
14565 +
14566 +       ii_read_unlock(inode);
14567 +       fi_read_unlock(file);
14568 +       si_read_unlock(sb);
14569 +       return err;
14570 +}
14571 +
14572 +/* ---------------------------------------------------------------------- */
14573 +
14574 +static int au_file_refresh_by_inode(struct file *file, int *need_reopen)
14575 +{
14576 +       int err;
14577 +       struct au_pin pin;
14578 +       struct au_finfo *finfo;
14579 +       struct dentry *parent, *hi_wh;
14580 +       struct inode *inode;
14581 +       struct super_block *sb;
14582 +       struct au_cp_generic cpg = {
14583 +               .dentry = file->f_path.dentry,
14584 +               .bdst   = -1,
14585 +               .bsrc   = -1,
14586 +               .len    = -1,
14587 +               .pin    = &pin,
14588 +               .flags  = AuCpup_DTIME
14589 +       };
14590 +
14591 +       FiMustWriteLock(file);
14592 +
14593 +       err = 0;
14594 +       finfo = au_fi(file);
14595 +       sb = cpg.dentry->d_sb;
14596 +       inode = d_inode(cpg.dentry);
14597 +       cpg.bdst = au_ibtop(inode);
14598 +       if (cpg.bdst == finfo->fi_btop || IS_ROOT(cpg.dentry))
14599 +               goto out;
14600 +
14601 +       parent = dget_parent(cpg.dentry);
14602 +       if (au_test_ro(sb, cpg.bdst, inode)) {
14603 +               di_read_lock_parent(parent, !AuLock_IR);
14604 +               err = AuWbrCopyup(au_sbi(sb), cpg.dentry);
14605 +               cpg.bdst = err;
14606 +               di_read_unlock(parent, !AuLock_IR);
14607 +               if (unlikely(err < 0))
14608 +                       goto out_parent;
14609 +               err = 0;
14610 +       }
14611 +
14612 +       di_read_lock_parent(parent, AuLock_IR);
14613 +       hi_wh = au_hi_wh(inode, cpg.bdst);
14614 +       if (!S_ISDIR(inode->i_mode)
14615 +           && au_opt_test(au_mntflags(sb), PLINK)
14616 +           && au_plink_test(inode)
14617 +           && !d_unhashed(cpg.dentry)
14618 +           && cpg.bdst < au_dbtop(cpg.dentry)) {
14619 +               err = au_test_and_cpup_dirs(cpg.dentry, cpg.bdst);
14620 +               if (unlikely(err))
14621 +                       goto out_unlock;
14622 +
14623 +               /* always superio. */
14624 +               err = au_pin(&pin, cpg.dentry, cpg.bdst, AuOpt_UDBA_NONE,
14625 +                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
14626 +               if (!err) {
14627 +                       err = au_sio_cpup_simple(&cpg);
14628 +                       au_unpin(&pin);
14629 +               }
14630 +       } else if (hi_wh) {
14631 +               /* already copied-up after unlink */
14632 +               err = au_reopen_wh(file, cpg.bdst, hi_wh);
14633 +               *need_reopen = 0;
14634 +       }
14635 +
14636 +out_unlock:
14637 +       di_read_unlock(parent, AuLock_IR);
14638 +out_parent:
14639 +       dput(parent);
14640 +out:
14641 +       return err;
14642 +}
14643 +
14644 +static void au_do_refresh_dir(struct file *file)
14645 +{
14646 +       aufs_bindex_t bindex, bbot, new_bindex, brid;
14647 +       struct au_hfile *p, tmp, *q;
14648 +       struct au_finfo *finfo;
14649 +       struct super_block *sb;
14650 +       struct au_fidir *fidir;
14651 +
14652 +       FiMustWriteLock(file);
14653 +
14654 +       sb = file->f_path.dentry->d_sb;
14655 +       finfo = au_fi(file);
14656 +       fidir = finfo->fi_hdir;
14657 +       AuDebugOn(!fidir);
14658 +       p = fidir->fd_hfile + finfo->fi_btop;
14659 +       brid = p->hf_br->br_id;
14660 +       bbot = fidir->fd_bbot;
14661 +       for (bindex = finfo->fi_btop; bindex <= bbot; bindex++, p++) {
14662 +               if (!p->hf_file)
14663 +                       continue;
14664 +
14665 +               new_bindex = au_br_index(sb, p->hf_br->br_id);
14666 +               if (new_bindex == bindex)
14667 +                       continue;
14668 +               if (new_bindex < 0) {
14669 +                       au_set_h_fptr(file, bindex, NULL);
14670 +                       continue;
14671 +               }
14672 +
14673 +               /* swap two lower inode, and loop again */
14674 +               q = fidir->fd_hfile + new_bindex;
14675 +               tmp = *q;
14676 +               *q = *p;
14677 +               *p = tmp;
14678 +               if (tmp.hf_file) {
14679 +                       bindex--;
14680 +                       p--;
14681 +               }
14682 +       }
14683 +
14684 +       p = fidir->fd_hfile;
14685 +       if (!au_test_mmapped(file) && !d_unlinked(file->f_path.dentry)) {
14686 +               bbot = au_sbbot(sb);
14687 +               for (finfo->fi_btop = 0; finfo->fi_btop <= bbot;
14688 +                    finfo->fi_btop++, p++)
14689 +                       if (p->hf_file) {
14690 +                               if (file_inode(p->hf_file))
14691 +                                       break;
14692 +                               au_hfput(p, /*execed*/0);
14693 +                       }
14694 +       } else {
14695 +               bbot = au_br_index(sb, brid);
14696 +               for (finfo->fi_btop = 0; finfo->fi_btop < bbot;
14697 +                    finfo->fi_btop++, p++)
14698 +                       if (p->hf_file)
14699 +                               au_hfput(p, /*execed*/0);
14700 +               bbot = au_sbbot(sb);
14701 +       }
14702 +
14703 +       p = fidir->fd_hfile + bbot;
14704 +       for (fidir->fd_bbot = bbot; fidir->fd_bbot >= finfo->fi_btop;
14705 +            fidir->fd_bbot--, p--)
14706 +               if (p->hf_file) {
14707 +                       if (file_inode(p->hf_file))
14708 +                               break;
14709 +                       au_hfput(p, /*execed*/0);
14710 +               }
14711 +       AuDebugOn(fidir->fd_bbot < finfo->fi_btop);
14712 +}
14713 +
14714 +/*
14715 + * after branch manipulating, refresh the file.
14716 + */
14717 +static int refresh_file(struct file *file, int (*reopen)(struct file *file))
14718 +{
14719 +       int err, need_reopen, nbr;
14720 +       aufs_bindex_t bbot, bindex;
14721 +       struct dentry *dentry;
14722 +       struct super_block *sb;
14723 +       struct au_finfo *finfo;
14724 +       struct au_hfile *hfile;
14725 +
14726 +       dentry = file->f_path.dentry;
14727 +       sb = dentry->d_sb;
14728 +       nbr = au_sbbot(sb) + 1;
14729 +       finfo = au_fi(file);
14730 +       if (!finfo->fi_hdir) {
14731 +               hfile = &finfo->fi_htop;
14732 +               AuDebugOn(!hfile->hf_file);
14733 +               bindex = au_br_index(sb, hfile->hf_br->br_id);
14734 +               AuDebugOn(bindex < 0);
14735 +               if (bindex != finfo->fi_btop)
14736 +                       au_set_fbtop(file, bindex);
14737 +       } else {
14738 +               err = au_fidir_realloc(finfo, nbr, /*may_shrink*/0);
14739 +               if (unlikely(err))
14740 +                       goto out;
14741 +               au_do_refresh_dir(file);
14742 +       }
14743 +
14744 +       err = 0;
14745 +       need_reopen = 1;
14746 +       if (!au_test_mmapped(file))
14747 +               err = au_file_refresh_by_inode(file, &need_reopen);
14748 +       if (finfo->fi_hdir)
14749 +               /* harmless if err */
14750 +               au_fidir_realloc(finfo, nbr, /*may_shrink*/1);
14751 +       if (!err && need_reopen && !d_unlinked(dentry))
14752 +               err = reopen(file);
14753 +       if (!err) {
14754 +               au_update_figen(file);
14755 +               goto out; /* success */
14756 +       }
14757 +
14758 +       /* error, close all lower files */
14759 +       if (finfo->fi_hdir) {
14760 +               bbot = au_fbbot_dir(file);
14761 +               for (bindex = au_fbtop(file); bindex <= bbot; bindex++)
14762 +                       au_set_h_fptr(file, bindex, NULL);
14763 +       }
14764 +
14765 +out:
14766 +       return err;
14767 +}
14768 +
14769 +/* common function to regular file and dir */
14770 +int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
14771 +                         int wlock, unsigned int fi_lsc)
14772 +{
14773 +       int err;
14774 +       unsigned int sigen, figen;
14775 +       aufs_bindex_t btop;
14776 +       unsigned char pseudo_link;
14777 +       struct dentry *dentry;
14778 +       struct inode *inode;
14779 +
14780 +       err = 0;
14781 +       dentry = file->f_path.dentry;
14782 +       inode = d_inode(dentry);
14783 +       sigen = au_sigen(dentry->d_sb);
14784 +       fi_write_lock_nested(file, fi_lsc);
14785 +       figen = au_figen(file);
14786 +       if (!fi_lsc)
14787 +               di_write_lock_child(dentry);
14788 +       else
14789 +               di_write_lock_child2(dentry);
14790 +       btop = au_dbtop(dentry);
14791 +       pseudo_link = (btop != au_ibtop(inode));
14792 +       if (sigen == figen && !pseudo_link && au_fbtop(file) == btop) {
14793 +               if (!wlock) {
14794 +                       di_downgrade_lock(dentry, AuLock_IR);
14795 +                       fi_downgrade_lock(file);
14796 +               }
14797 +               goto out; /* success */
14798 +       }
14799 +
14800 +       AuDbg("sigen %d, figen %d\n", sigen, figen);
14801 +       if (au_digen_test(dentry, sigen)) {
14802 +               err = au_reval_dpath(dentry, sigen);
14803 +               AuDebugOn(!err && au_digen_test(dentry, sigen));
14804 +       }
14805 +
14806 +       if (!err)
14807 +               err = refresh_file(file, reopen);
14808 +       if (!err) {
14809 +               if (!wlock) {
14810 +                       di_downgrade_lock(dentry, AuLock_IR);
14811 +                       fi_downgrade_lock(file);
14812 +               }
14813 +       } else {
14814 +               di_write_unlock(dentry);
14815 +               fi_write_unlock(file);
14816 +       }
14817 +
14818 +out:
14819 +       return err;
14820 +}
14821 +
14822 +/* ---------------------------------------------------------------------- */
14823 +
14824 +/* cf. aufs_nopage() */
14825 +/* for madvise(2) */
14826 +static int aufs_readpage(struct file *file __maybe_unused, struct page *page)
14827 +{
14828 +       unlock_page(page);
14829 +       return 0;
14830 +}
14831 +
14832 +/* it will never be called, but necessary to support O_DIRECT */
14833 +static ssize_t aufs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
14834 +{ BUG(); return 0; }
14835 +
14836 +/* they will never be called. */
14837 +#ifdef CONFIG_AUFS_DEBUG
14838 +static int aufs_write_begin(struct file *file, struct address_space *mapping,
14839 +                           loff_t pos, unsigned len, unsigned flags,
14840 +                           struct page **pagep, void **fsdata)
14841 +{ AuUnsupport(); return 0; }
14842 +static int aufs_write_end(struct file *file, struct address_space *mapping,
14843 +                         loff_t pos, unsigned len, unsigned copied,
14844 +                         struct page *page, void *fsdata)
14845 +{ AuUnsupport(); return 0; }
14846 +static int aufs_writepage(struct page *page, struct writeback_control *wbc)
14847 +{ AuUnsupport(); return 0; }
14848 +
14849 +static bool aufs_dirty_folio(struct address_space *mapping, struct folio *folio)
14850 +{ AuUnsupport(); return true; }
14851 +static void aufs_invalidate_folio(struct folio *folio, size_t offset, size_t len)
14852 +{ AuUnsupport(); }
14853 +static int aufs_releasepage(struct page *page, gfp_t gfp)
14854 +{ AuUnsupport(); return 0; }
14855 +#if 0 /* called by memory compaction regardless file */
14856 +static int aufs_migratepage(struct address_space *mapping, struct page *newpage,
14857 +                           struct page *page, enum migrate_mode mode)
14858 +{ AuUnsupport(); return 0; }
14859 +#endif
14860 +static bool aufs_isolate_page(struct page *page, isolate_mode_t mode)
14861 +{ AuUnsupport(); return true; }
14862 +static void aufs_putback_page(struct page *page)
14863 +{ AuUnsupport(); }
14864 +static int aufs_launder_folio(struct folio *folio)
14865 +{ AuUnsupport(); return 0; }
14866 +static bool aufs_is_partially_uptodate(struct folio *folio, size_t from,
14867 +                                     size_t count)
14868 +{ AuUnsupport(); return true; }
14869 +static void aufs_is_dirty_writeback(struct page *page, bool *dirty,
14870 +                                   bool *writeback)
14871 +{ AuUnsupport(); }
14872 +static int aufs_error_remove_page(struct address_space *mapping,
14873 +                                 struct page *page)
14874 +{ AuUnsupport(); return 0; }
14875 +static int aufs_swap_activate(struct swap_info_struct *sis, struct file *file,
14876 +                             sector_t *span)
14877 +{ AuUnsupport(); return 0; }
14878 +static void aufs_swap_deactivate(struct file *file)
14879 +{ AuUnsupport(); }
14880 +#endif /* CONFIG_AUFS_DEBUG */
14881 +
14882 +const struct address_space_operations aufs_aop = {
14883 +       .readpage               = aufs_readpage,
14884 +       .direct_IO              = aufs_direct_IO,
14885 +#ifdef CONFIG_AUFS_DEBUG
14886 +       .writepage              = aufs_writepage,
14887 +       /* no writepages, because of writepage */
14888 +       .dirty_folio            = aufs_dirty_folio,
14889 +       /* no readpages, because of readpage */
14890 +       .write_begin            = aufs_write_begin,
14891 +       .write_end              = aufs_write_end,
14892 +       /* no bmap, no block device */
14893 +       .invalidate_folio       = aufs_invalidate_folio,
14894 +       .releasepage            = aufs_releasepage,
14895 +       /* is fallback_migrate_page ok? */
14896 +       /* .migratepage         = aufs_migratepage, */
14897 +       .isolate_page           = aufs_isolate_page,
14898 +       .putback_page           = aufs_putback_page,
14899 +       .launder_folio          = aufs_launder_folio,
14900 +       .is_partially_uptodate  = aufs_is_partially_uptodate,
14901 +       .is_dirty_writeback     = aufs_is_dirty_writeback,
14902 +       .error_remove_page      = aufs_error_remove_page,
14903 +       .swap_activate          = aufs_swap_activate,
14904 +       .swap_deactivate        = aufs_swap_deactivate
14905 +#endif /* CONFIG_AUFS_DEBUG */
14906 +};
14907 diff -urN /usr/share/empty/fs/aufs/file.h linux/fs/aufs/file.h
14908 --- /usr/share/empty/fs/aufs/file.h     1970-01-01 01:00:00.000000000 +0100
14909 +++ linux/fs/aufs/file.h        2022-03-21 14:49:05.723299677 +0100
14910 @@ -0,0 +1,342 @@
14911 +/* SPDX-License-Identifier: GPL-2.0 */
14912 +/*
14913 + * Copyright (C) 2005-2021 Junjiro R. Okajima
14914 + *
14915 + * This program, aufs is free software; you can redistribute it and/or modify
14916 + * it under the terms of the GNU General Public License as published by
14917 + * the Free Software Foundation; either version 2 of the License, or
14918 + * (at your option) any later version.
14919 + *
14920 + * This program is distributed in the hope that it will be useful,
14921 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
14922 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14923 + * GNU General Public License for more details.
14924 + *
14925 + * You should have received a copy of the GNU General Public License
14926 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
14927 + */
14928 +
14929 +/*
14930 + * file operations
14931 + */
14932 +
14933 +#ifndef __AUFS_FILE_H__
14934 +#define __AUFS_FILE_H__
14935 +
14936 +#ifdef __KERNEL__
14937 +
14938 +#include <linux/file.h>
14939 +#include <linux/fs.h>
14940 +#include <linux/mm_types.h>
14941 +#include <linux/poll.h>
14942 +#include "rwsem.h"
14943 +
14944 +struct au_branch;
14945 +struct au_hfile {
14946 +       struct file             *hf_file;
14947 +       struct au_branch        *hf_br;
14948 +};
14949 +
14950 +struct au_vdir;
14951 +struct au_fidir {
14952 +       aufs_bindex_t           fd_bbot;
14953 +       aufs_bindex_t           fd_nent;
14954 +       struct au_vdir          *fd_vdir_cache;
14955 +       struct au_hfile         fd_hfile[];
14956 +};
14957 +
14958 +static inline int au_fidir_sz(int nent)
14959 +{
14960 +       AuDebugOn(nent < 0);
14961 +       return sizeof(struct au_fidir) + sizeof(struct au_hfile) * nent;
14962 +}
14963 +
14964 +struct au_finfo {
14965 +       atomic_t                fi_generation;
14966 +
14967 +       struct au_rwsem         fi_rwsem;
14968 +       aufs_bindex_t           fi_btop;
14969 +
14970 +       /* do not union them */
14971 +       struct {                                /* for non-dir */
14972 +               struct au_hfile                 fi_htop;
14973 +               atomic_t                        fi_mmapped;
14974 +       };
14975 +       struct au_fidir         *fi_hdir;       /* for dir only */
14976 +
14977 +       struct hlist_bl_node    fi_hlist;
14978 +       struct file             *fi_file;       /* very ugly */
14979 +       struct rcu_head         rcu;
14980 +} ____cacheline_aligned_in_smp;
14981 +
14982 +/* ---------------------------------------------------------------------- */
14983 +
14984 +/* file.c */
14985 +extern const struct address_space_operations aufs_aop;
14986 +unsigned int au_file_roflags(unsigned int flags);
14987 +struct file *au_h_open(struct dentry *dentry, aufs_bindex_t bindex, int flags,
14988 +                      struct file *file, int force_wr);
14989 +struct au_do_open_args {
14990 +       int             aopen;
14991 +       int             (*open)(struct file *file, int flags,
14992 +                               struct file *h_file);
14993 +       struct au_fidir *fidir;
14994 +       struct file     *h_file;
14995 +};
14996 +int au_do_open(struct file *file, struct au_do_open_args *args);
14997 +int au_reopen_nondir(struct file *file);
14998 +struct au_pin;
14999 +int au_ready_to_write(struct file *file, loff_t len, struct au_pin *pin);
15000 +int au_reval_and_lock_fdi(struct file *file, int (*reopen)(struct file *file),
15001 +                         int wlock, unsigned int fi_lsc);
15002 +int au_do_flush(struct file *file, fl_owner_t id,
15003 +               int (*flush)(struct file *file, fl_owner_t id));
15004 +
15005 +/* poll.c */
15006 +#ifdef CONFIG_AUFS_POLL
15007 +__poll_t aufs_poll(struct file *file, struct poll_table_struct *pt);
15008 +#endif
15009 +
15010 +#ifdef CONFIG_AUFS_BR_HFSPLUS
15011 +/* hfsplus.c */
15012 +struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
15013 +                          int force_wr);
15014 +void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
15015 +                   struct file *h_file);
15016 +#else
15017 +AuStub(struct file *, au_h_open_pre, return NULL, struct dentry *dentry,
15018 +       aufs_bindex_t bindex, int force_wr)
15019 +AuStubVoid(au_h_open_post, struct dentry *dentry, aufs_bindex_t bindex,
15020 +          struct file *h_file);
15021 +#endif
15022 +
15023 +/* f_op.c */
15024 +extern const struct file_operations aufs_file_fop;
15025 +int au_do_open_nondir(struct file *file, int flags, struct file *h_file);
15026 +int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file);
15027 +struct file *au_read_pre(struct file *file, int keep_fi, unsigned int lsc);
15028 +
15029 +/* finfo.c */
15030 +void au_hfput(struct au_hfile *hf, int execed);
15031 +void au_set_h_fptr(struct file *file, aufs_bindex_t bindex,
15032 +                  struct file *h_file);
15033 +
15034 +void au_update_figen(struct file *file);
15035 +struct au_fidir *au_fidir_alloc(struct super_block *sb);
15036 +int au_fidir_realloc(struct au_finfo *finfo, int nbr, int may_shrink);
15037 +
15038 +void au_fi_init_once(void *_fi);
15039 +void au_finfo_fin(struct file *file);
15040 +int au_finfo_init(struct file *file, struct au_fidir *fidir);
15041 +
15042 +/* ioctl.c */
15043 +long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg);
15044 +#ifdef CONFIG_COMPAT
15045 +long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
15046 +                          unsigned long arg);
15047 +long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
15048 +                             unsigned long arg);
15049 +#endif
15050 +
15051 +/* ---------------------------------------------------------------------- */
15052 +
15053 +static inline struct au_finfo *au_fi(struct file *file)
15054 +{
15055 +       return file->private_data;
15056 +}
15057 +
15058 +/* ---------------------------------------------------------------------- */
15059 +
15060 +#define fi_read_lock(f)        au_rw_read_lock(&au_fi(f)->fi_rwsem)
15061 +#define fi_write_lock(f)       au_rw_write_lock(&au_fi(f)->fi_rwsem)
15062 +#define fi_read_trylock(f)     au_rw_read_trylock(&au_fi(f)->fi_rwsem)
15063 +#define fi_write_trylock(f)    au_rw_write_trylock(&au_fi(f)->fi_rwsem)
15064 +/*
15065 +#define fi_read_trylock_nested(f) \
15066 +       au_rw_read_trylock_nested(&au_fi(f)->fi_rwsem)
15067 +#define fi_write_trylock_nested(f) \
15068 +       au_rw_write_trylock_nested(&au_fi(f)->fi_rwsem)
15069 +*/
15070 +
15071 +#define fi_read_unlock(f)      au_rw_read_unlock(&au_fi(f)->fi_rwsem)
15072 +#define fi_write_unlock(f)     au_rw_write_unlock(&au_fi(f)->fi_rwsem)
15073 +#define fi_downgrade_lock(f)   au_rw_dgrade_lock(&au_fi(f)->fi_rwsem)
15074 +
15075 +/* lock subclass for finfo */
15076 +enum {
15077 +       AuLsc_FI_1,
15078 +       AuLsc_FI_2
15079 +};
15080 +
15081 +static inline void fi_read_lock_nested(struct file *f, unsigned int lsc)
15082 +{
15083 +       au_rw_read_lock_nested(&au_fi(f)->fi_rwsem, lsc);
15084 +}
15085 +
15086 +static inline void fi_write_lock_nested(struct file *f, unsigned int lsc)
15087 +{
15088 +       au_rw_write_lock_nested(&au_fi(f)->fi_rwsem, lsc);
15089 +}
15090 +
15091 +/*
15092 + * fi_read_lock_1, fi_write_lock_1,
15093 + * fi_read_lock_2, fi_write_lock_2
15094 + */
15095 +#define AuReadLockFunc(name) \
15096 +static inline void fi_read_lock_##name(struct file *f) \
15097 +{ fi_read_lock_nested(f, AuLsc_FI_##name); }
15098 +
15099 +#define AuWriteLockFunc(name) \
15100 +static inline void fi_write_lock_##name(struct file *f) \
15101 +{ fi_write_lock_nested(f, AuLsc_FI_##name); }
15102 +
15103 +#define AuRWLockFuncs(name) \
15104 +       AuReadLockFunc(name) \
15105 +       AuWriteLockFunc(name)
15106 +
15107 +AuRWLockFuncs(1);
15108 +AuRWLockFuncs(2);
15109 +
15110 +#undef AuReadLockFunc
15111 +#undef AuWriteLockFunc
15112 +#undef AuRWLockFuncs
15113 +
15114 +#define FiMustNoWaiters(f)     AuRwMustNoWaiters(&au_fi(f)->fi_rwsem)
15115 +#define FiMustAnyLock(f)       AuRwMustAnyLock(&au_fi(f)->fi_rwsem)
15116 +#define FiMustWriteLock(f)     AuRwMustWriteLock(&au_fi(f)->fi_rwsem)
15117 +
15118 +/* ---------------------------------------------------------------------- */
15119 +
15120 +/* todo: hard/soft set? */
15121 +static inline aufs_bindex_t au_fbtop(struct file *file)
15122 +{
15123 +       FiMustAnyLock(file);
15124 +       return au_fi(file)->fi_btop;
15125 +}
15126 +
15127 +static inline aufs_bindex_t au_fbbot_dir(struct file *file)
15128 +{
15129 +       FiMustAnyLock(file);
15130 +       AuDebugOn(!au_fi(file)->fi_hdir);
15131 +       return au_fi(file)->fi_hdir->fd_bbot;
15132 +}
15133 +
15134 +static inline struct au_vdir *au_fvdir_cache(struct file *file)
15135 +{
15136 +       FiMustAnyLock(file);
15137 +       AuDebugOn(!au_fi(file)->fi_hdir);
15138 +       return au_fi(file)->fi_hdir->fd_vdir_cache;
15139 +}
15140 +
15141 +static inline void au_set_fbtop(struct file *file, aufs_bindex_t bindex)
15142 +{
15143 +       FiMustWriteLock(file);
15144 +       au_fi(file)->fi_btop = bindex;
15145 +}
15146 +
15147 +static inline void au_set_fbbot_dir(struct file *file, aufs_bindex_t bindex)
15148 +{
15149 +       FiMustWriteLock(file);
15150 +       AuDebugOn(!au_fi(file)->fi_hdir);
15151 +       au_fi(file)->fi_hdir->fd_bbot = bindex;
15152 +}
15153 +
15154 +static inline void au_set_fvdir_cache(struct file *file,
15155 +                                     struct au_vdir *vdir_cache)
15156 +{
15157 +       FiMustWriteLock(file);
15158 +       AuDebugOn(!au_fi(file)->fi_hdir);
15159 +       au_fi(file)->fi_hdir->fd_vdir_cache = vdir_cache;
15160 +}
15161 +
15162 +static inline struct file *au_hf_top(struct file *file)
15163 +{
15164 +       FiMustAnyLock(file);
15165 +       AuDebugOn(au_fi(file)->fi_hdir);
15166 +       return au_fi(file)->fi_htop.hf_file;
15167 +}
15168 +
15169 +static inline struct file *au_hf_dir(struct file *file, aufs_bindex_t bindex)
15170 +{
15171 +       FiMustAnyLock(file);
15172 +       AuDebugOn(!au_fi(file)->fi_hdir);
15173 +       return au_fi(file)->fi_hdir->fd_hfile[0 + bindex].hf_file;
15174 +}
15175 +
15176 +/* todo: memory barrier? */
15177 +static inline unsigned int au_figen(struct file *f)
15178 +{
15179 +       return atomic_read(&au_fi(f)->fi_generation);
15180 +}
15181 +
15182 +static inline void au_set_mmapped(struct file *f)
15183 +{
15184 +       if (atomic_inc_return(&au_fi(f)->fi_mmapped))
15185 +               return;
15186 +       pr_warn("fi_mmapped wrapped around\n");
15187 +       while (!atomic_inc_return(&au_fi(f)->fi_mmapped))
15188 +               ;
15189 +}
15190 +
15191 +static inline void au_unset_mmapped(struct file *f)
15192 +{
15193 +       atomic_dec(&au_fi(f)->fi_mmapped);
15194 +}
15195 +
15196 +static inline int au_test_mmapped(struct file *f)
15197 +{
15198 +       return atomic_read(&au_fi(f)->fi_mmapped);
15199 +}
15200 +
15201 +/* customize vma->vm_file */
15202 +
15203 +static inline void au_do_vm_file_reset(struct vm_area_struct *vma,
15204 +                                      struct file *file)
15205 +{
15206 +       struct file *f;
15207 +
15208 +       f = vma->vm_file;
15209 +       get_file(file);
15210 +       vma->vm_file = file;
15211 +       fput(f);
15212 +}
15213 +
15214 +#ifdef CONFIG_MMU
15215 +#define AuDbgVmRegion(file, vma) do {} while (0)
15216 +
15217 +static inline void au_vm_file_reset(struct vm_area_struct *vma,
15218 +                                   struct file *file)
15219 +{
15220 +       au_do_vm_file_reset(vma, file);
15221 +}
15222 +#else
15223 +#define AuDbgVmRegion(file, vma) \
15224 +       AuDebugOn((vma)->vm_region && (vma)->vm_region->vm_file != (file))
15225 +
15226 +static inline void au_vm_file_reset(struct vm_area_struct *vma,
15227 +                                   struct file *file)
15228 +{
15229 +       struct file *f;
15230 +
15231 +       au_do_vm_file_reset(vma, file);
15232 +       f = vma->vm_region->vm_file;
15233 +       get_file(file);
15234 +       vma->vm_region->vm_file = file;
15235 +       fput(f);
15236 +}
15237 +#endif /* CONFIG_MMU */
15238 +
15239 +/* handle vma->vm_prfile */
15240 +static inline void au_vm_prfile_set(struct vm_area_struct *vma,
15241 +                                   struct file *file)
15242 +{
15243 +       get_file(file);
15244 +       vma->vm_prfile = file;
15245 +#ifndef CONFIG_MMU
15246 +       get_file(file);
15247 +       vma->vm_region->vm_prfile = file;
15248 +#endif
15249 +}
15250 +
15251 +#endif /* __KERNEL__ */
15252 +#endif /* __AUFS_FILE_H__ */
15253 diff -urN /usr/share/empty/fs/aufs/finfo.c linux/fs/aufs/finfo.c
15254 --- /usr/share/empty/fs/aufs/finfo.c    1970-01-01 01:00:00.000000000 +0100
15255 +++ linux/fs/aufs/finfo.c       2022-03-21 14:49:05.723299677 +0100
15256 @@ -0,0 +1,149 @@
15257 +// SPDX-License-Identifier: GPL-2.0
15258 +/*
15259 + * Copyright (C) 2005-2021 Junjiro R. Okajima
15260 + *
15261 + * This program, aufs is free software; you can redistribute it and/or modify
15262 + * it under the terms of the GNU General Public License as published by
15263 + * the Free Software Foundation; either version 2 of the License, or
15264 + * (at your option) any later version.
15265 + *
15266 + * This program is distributed in the hope that it will be useful,
15267 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
15268 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15269 + * GNU General Public License for more details.
15270 + *
15271 + * You should have received a copy of the GNU General Public License
15272 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15273 + */
15274 +
15275 +/*
15276 + * file private data
15277 + */
15278 +
15279 +#include "aufs.h"
15280 +
15281 +void au_hfput(struct au_hfile *hf, int execed)
15282 +{
15283 +       if (execed)
15284 +               allow_write_access(hf->hf_file);
15285 +       fput(hf->hf_file);
15286 +       hf->hf_file = NULL;
15287 +       au_lcnt_dec(&hf->hf_br->br_nfiles);
15288 +       hf->hf_br = NULL;
15289 +}
15290 +
15291 +void au_set_h_fptr(struct file *file, aufs_bindex_t bindex, struct file *val)
15292 +{
15293 +       struct au_finfo *finfo = au_fi(file);
15294 +       struct au_hfile *hf;
15295 +       struct au_fidir *fidir;
15296 +
15297 +       fidir = finfo->fi_hdir;
15298 +       if (!fidir) {
15299 +               AuDebugOn(finfo->fi_btop != bindex);
15300 +               hf = &finfo->fi_htop;
15301 +       } else
15302 +               hf = fidir->fd_hfile + bindex;
15303 +
15304 +       if (hf && hf->hf_file)
15305 +               au_hfput(hf, vfsub_file_execed(file));
15306 +       if (val) {
15307 +               FiMustWriteLock(file);
15308 +               AuDebugOn(IS_ERR_OR_NULL(file->f_path.dentry));
15309 +               hf->hf_file = val;
15310 +               hf->hf_br = au_sbr(file->f_path.dentry->d_sb, bindex);
15311 +       }
15312 +}
15313 +
15314 +void au_update_figen(struct file *file)
15315 +{
15316 +       atomic_set(&au_fi(file)->fi_generation, au_digen(file->f_path.dentry));
15317 +       /* smp_mb(); */ /* atomic_set */
15318 +}
15319 +
15320 +/* ---------------------------------------------------------------------- */
15321 +
15322 +struct au_fidir *au_fidir_alloc(struct super_block *sb)
15323 +{
15324 +       struct au_fidir *fidir;
15325 +       int nbr;
15326 +
15327 +       nbr = au_sbbot(sb) + 1;
15328 +       if (nbr < 2)
15329 +               nbr = 2; /* initial allocate for 2 branches */
15330 +       fidir = kzalloc(au_fidir_sz(nbr), GFP_NOFS);
15331 +       if (fidir) {
15332 +               fidir->fd_bbot = -1;
15333 +               fidir->fd_nent = nbr;
15334 +       }
15335 +
15336 +       return fidir;
15337 +}
15338 +
15339 +int au_fidir_realloc(struct au_finfo *finfo, int nbr, int may_shrink)
15340 +{
15341 +       int err;
15342 +       struct au_fidir *fidir, *p;
15343 +
15344 +       AuRwMustWriteLock(&finfo->fi_rwsem);
15345 +       fidir = finfo->fi_hdir;
15346 +       AuDebugOn(!fidir);
15347 +
15348 +       err = -ENOMEM;
15349 +       p = au_kzrealloc(fidir, au_fidir_sz(fidir->fd_nent), au_fidir_sz(nbr),
15350 +                        GFP_NOFS, may_shrink);
15351 +       if (p) {
15352 +               p->fd_nent = nbr;
15353 +               finfo->fi_hdir = p;
15354 +               err = 0;
15355 +       }
15356 +
15357 +       return err;
15358 +}
15359 +
15360 +/* ---------------------------------------------------------------------- */
15361 +
15362 +void au_finfo_fin(struct file *file)
15363 +{
15364 +       struct au_finfo *finfo;
15365 +
15366 +       au_lcnt_dec(&au_sbi(file->f_path.dentry->d_sb)->si_nfiles);
15367 +
15368 +       finfo = au_fi(file);
15369 +       AuDebugOn(finfo->fi_hdir);
15370 +       AuRwDestroy(&finfo->fi_rwsem);
15371 +       au_cache_free_finfo(finfo);
15372 +}
15373 +
15374 +void au_fi_init_once(void *_finfo)
15375 +{
15376 +       struct au_finfo *finfo = _finfo;
15377 +
15378 +       au_rw_init(&finfo->fi_rwsem);
15379 +}
15380 +
15381 +int au_finfo_init(struct file *file, struct au_fidir *fidir)
15382 +{
15383 +       int err;
15384 +       struct au_finfo *finfo;
15385 +       struct dentry *dentry;
15386 +
15387 +       err = -ENOMEM;
15388 +       dentry = file->f_path.dentry;
15389 +       finfo = au_cache_alloc_finfo();
15390 +       if (unlikely(!finfo))
15391 +               goto out;
15392 +
15393 +       err = 0;
15394 +       au_lcnt_inc(&au_sbi(dentry->d_sb)->si_nfiles);
15395 +       au_rw_write_lock(&finfo->fi_rwsem);
15396 +       finfo->fi_btop = -1;
15397 +       finfo->fi_hdir = fidir;
15398 +       atomic_set(&finfo->fi_generation, au_digen(dentry));
15399 +       /* smp_mb(); */ /* atomic_set */
15400 +
15401 +       file->private_data = finfo;
15402 +
15403 +out:
15404 +       return err;
15405 +}
15406 diff -urN /usr/share/empty/fs/aufs/f_op.c linux/fs/aufs/f_op.c
15407 --- /usr/share/empty/fs/aufs/f_op.c     1970-01-01 01:00:00.000000000 +0100
15408 +++ linux/fs/aufs/f_op.c        2022-03-21 14:49:05.723299677 +0100
15409 @@ -0,0 +1,771 @@
15410 +// SPDX-License-Identifier: GPL-2.0
15411 +/*
15412 + * Copyright (C) 2005-2021 Junjiro R. Okajima
15413 + *
15414 + * This program, aufs is free software; you can redistribute it and/or modify
15415 + * it under the terms of the GNU General Public License as published by
15416 + * the Free Software Foundation; either version 2 of the License, or
15417 + * (at your option) any later version.
15418 + *
15419 + * This program is distributed in the hope that it will be useful,
15420 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
15421 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15422 + * GNU General Public License for more details.
15423 + *
15424 + * You should have received a copy of the GNU General Public License
15425 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
15426 + */
15427 +
15428 +/*
15429 + * file and vm operations
15430 + */
15431 +
15432 +#include <linux/aio.h>
15433 +#include <linux/fs_stack.h>
15434 +#include <linux/mman.h>
15435 +#include <linux/security.h>
15436 +#include "aufs.h"
15437 +
15438 +int au_do_open_nondir(struct file *file, int flags, struct file *h_file)
15439 +{
15440 +       int err;
15441 +       aufs_bindex_t bindex;
15442 +       struct dentry *dentry, *h_dentry;
15443 +       struct au_finfo *finfo;
15444 +       struct inode *h_inode;
15445 +
15446 +       FiMustWriteLock(file);
15447 +
15448 +       err = 0;
15449 +       dentry = file->f_path.dentry;
15450 +       AuDebugOn(IS_ERR_OR_NULL(dentry));
15451 +       finfo = au_fi(file);
15452 +       memset(&finfo->fi_htop, 0, sizeof(finfo->fi_htop));
15453 +       atomic_set(&finfo->fi_mmapped, 0);
15454 +       bindex = au_dbtop(dentry);
15455 +       if (!h_file) {
15456 +               h_dentry = au_h_dptr(dentry, bindex);
15457 +               err = vfsub_test_mntns(file->f_path.mnt, h_dentry->d_sb);
15458 +               if (unlikely(err))
15459 +                       goto out;
15460 +               h_file = au_h_open(dentry, bindex, flags, file, /*force_wr*/0);
15461 +               if (IS_ERR(h_file)) {
15462 +                       err = PTR_ERR(h_file);
15463 +                       goto out;
15464 +               }
15465 +       } else {
15466 +               h_dentry = h_file->f_path.dentry;
15467 +               err = vfsub_test_mntns(file->f_path.mnt, h_dentry->d_sb);
15468 +               if (unlikely(err))
15469 +                       goto out;
15470 +               /* br ref is already inc-ed */
15471 +       }
15472 +
15473 +       if ((flags & __O_TMPFILE)
15474 +           && !(flags & O_EXCL)) {
15475 +               h_inode = file_inode(h_file);
15476 +               spin_lock(&h_inode->i_lock);
15477 +               h_inode->i_state |= I_LINKABLE;
15478 +               spin_unlock(&h_inode->i_lock);
15479 +       }
15480 +       au_set_fbtop(file, bindex);
15481 +       au_set_h_fptr(file, bindex, h_file);
15482 +       au_update_figen(file);
15483 +       /* todo: necessary? */
15484 +       /* file->f_ra = h_file->f_ra; */
15485 +
15486 +out:
15487 +       return err;
15488 +}
15489 +
15490 +static int aufs_open_nondir(struct inode *inode __maybe_unused,
15491 +                           struct file *file)
15492 +{
15493 +       int err;
15494 +       struct super_block *sb;
15495 +       struct au_do_open_args args = {
15496 +               .open   = au_do_open_nondir
15497 +       };
15498 +
15499 +       AuDbg("%pD, f_flags 0x%x, f_mode 0x%x\n",
15500 +             file, vfsub_file_flags(file), file->f_mode);
15501 +
15502 +       sb = file->f_path.dentry->d_sb;
15503 +       si_read_lock(sb, AuLock_FLUSH);
15504 +       err = au_do_open(file, &args);
15505 +       si_read_unlock(sb);
15506 +       return err;
15507 +}
15508 +
15509 +int aufs_release_nondir(struct inode *inode __maybe_unused, struct file *file)
15510 +{
15511 +       struct au_finfo *finfo;
15512 +       aufs_bindex_t bindex;
15513 +
15514 +       finfo = au_fi(file);
15515 +       au_hbl_del(&finfo->fi_hlist,
15516 +                  &au_sbi(file->f_path.dentry->d_sb)->si_files);
15517 +       bindex = finfo->fi_btop;
15518 +       if (bindex >= 0)
15519 +               au_set_h_fptr(file, bindex, NULL);
15520 +
15521 +       au_finfo_fin(file);
15522 +       return 0;
15523 +}
15524 +
15525 +/* ---------------------------------------------------------------------- */
15526 +
15527 +static int au_do_flush_nondir(struct file *file, fl_owner_t id)
15528 +{
15529 +       int err;
15530 +       struct file *h_file;
15531 +
15532 +       err = 0;
15533 +       h_file = au_hf_top(file);
15534 +       if (h_file)
15535 +               err = vfsub_flush(h_file, id);
15536 +       return err;
15537 +}
15538 +
15539 +static int aufs_flush_nondir(struct file *file, fl_owner_t id)
15540 +{
15541 +       return au_do_flush(file, id, au_do_flush_nondir);
15542 +}
15543 +
15544 +/* ---------------------------------------------------------------------- */
15545 +/*
15546 + * read and write functions acquire [fdi]_rwsem once, but release before
15547 + * mmap_sem. This is because to stop a race condition between mmap(2).
15548 + * Releasing these aufs-rwsem should be safe, no branch-management (by keeping
15549 + * si_rwsem), no harmful copy-up should happen. Actually copy-up may happen in
15550 + * read functions after [fdi]_rwsem are released, but it should be harmless.
15551 + */
15552 +
15553 +/* Callers should call au_read_post() or fput() in the end */
15554 +struct file *au_read_pre(struct file *file, int keep_fi, unsigned int lsc)
15555 +{
15556 +       struct file *h_file;
15557 +       int err;
15558 +
15559 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/0, lsc);
15560 +       if (!err) {
15561 +               di_read_unlock(file->f_path.dentry, AuLock_IR);
15562 +               h_file = au_hf_top(file);
15563 +               get_file(h_file);
15564 +               if (!keep_fi)
15565 +                       fi_read_unlock(file);
15566 +       } else
15567 +               h_file = ERR_PTR(err);
15568 +
15569 +       return h_file;
15570 +}
15571 +
15572 +static void au_read_post(struct inode *inode, struct file *h_file)
15573 +{
15574 +       /* update without lock, I don't think it a problem */
15575 +       fsstack_copy_attr_atime(inode, file_inode(h_file));
15576 +       fput(h_file);
15577 +}
15578 +
15579 +struct au_write_pre {
15580 +       /* input */
15581 +       unsigned int lsc;
15582 +
15583 +       /* output */
15584 +       blkcnt_t blks;
15585 +       aufs_bindex_t btop;
15586 +};
15587 +
15588 +/*
15589 + * return with iinfo is write-locked
15590 + * callers should call au_write_post() or iinfo_write_unlock() + fput() in the
15591 + * end
15592 + */
15593 +static struct file *au_write_pre(struct file *file, int do_ready,
15594 +                                struct au_write_pre *wpre)
15595 +{
15596 +       struct file *h_file;
15597 +       struct dentry *dentry;
15598 +       int err;
15599 +       unsigned int lsc;
15600 +       struct au_pin pin;
15601 +
15602 +       lsc = 0;
15603 +       if (wpre)
15604 +               lsc = wpre->lsc;
15605 +       err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1, lsc);
15606 +       h_file = ERR_PTR(err);
15607 +       if (unlikely(err))
15608 +               goto out;
15609 +
15610 +       dentry = file->f_path.dentry;
15611 +       if (do_ready) {
15612 +               err = au_ready_to_write(file, -1, &pin);
15613 +               if (unlikely(err)) {
15614 +                       h_file = ERR_PTR(err);
15615 +                       di_write_unlock(dentry);
15616 +                       goto out_fi;
15617 +               }
15618 +       }
15619 +
15620 +       di_downgrade_lock(dentry, /*flags*/0);
15621 +       if (wpre)
15622 +               wpre->btop = au_fbtop(file);
15623 +       h_file = au_hf_top(file);
15624 +       get_file(h_file);
15625 +       if (wpre)
15626 +               wpre->blks = file_inode(h_file)->i_blocks;
15627 +       if (do_ready)
15628 +               au_unpin(&pin);
15629 +       di_read_unlock(dentry, /*flags*/0);
15630 +
15631 +out_fi:
15632 +       fi_write_unlock(file);
15633 +out:
15634 +       return h_file;
15635 +}
15636 +
15637 +static void au_write_post(struct inode *inode, struct file *h_file,
15638 +                         struct au_write_pre *wpre, ssize_t written)
15639 +{
15640 +       struct inode *h_inode;
15641 +
15642 +       au_cpup_attr_timesizes(inode);
15643 +       AuDebugOn(au_ibtop(inode) != wpre->btop);
15644 +       h_inode = file_inode(h_file);
15645 +       inode->i_mode = h_inode->i_mode;
15646 +       ii_write_unlock(inode);
15647 +       /* AuDbg("blks %llu, %llu\n", (u64)blks, (u64)h_inode->i_blocks); */
15648 +       if (written > 0)
15649 +               au_fhsm_wrote(inode->i_sb, wpre->btop,
15650 +                             /*force*/h_inode->i_blocks > wpre->blks);
15651 +       fput(h_file);
15652 +}
15653 +
15654 +/*
15655 + * todo: very ugly
15656 + * it locks both of i_mutex and si_rwsem for read in safe.
15657 + * if the plink maintenance mode continues forever (that is the problem),
15658 + * may loop forever.
15659 + */
15660 +static void au_mtx_and_read_lock(struct inode *inode)
15661 +{
15662 +       int err;
15663 +       struct super_block *sb = inode->i_sb;
15664 +
15665 +       while (1) {
15666 +               inode_lock(inode);
15667 +               err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
15668 +               if (!err)
15669 +                       break;
15670 +               inode_unlock(inode);
15671 +               si_read_lock(sb, AuLock_NOPLMW);
15672 +               si_read_unlock(sb);
15673 +       }
15674 +}
15675 +
15676 +static ssize_t au_do_iter(struct file *h_file, int rw, struct kiocb *kio,
15677 +                         struct iov_iter *iov_iter)
15678 +{
15679 +       ssize_t err;
15680 +       struct file *file;
15681 +       ssize_t (*iter)(struct kiocb *, struct iov_iter *);
15682 +
15683 +       err = security_file_permission(h_file, rw);
15684 +       if (unlikely(err))
15685 +               goto out;
15686 +
15687 +       err = -ENOSYS;  /* the branch doesn't have its ->(read|write)_iter() */
15688 +       iter = NULL;
15689 +       if (rw == MAY_READ)
15690 +               iter = h_file->f_op->read_iter;
15691 +       else if (rw == MAY_WRITE)
15692 +               iter = h_file->f_op->write_iter;
15693 +
15694 +       file = kio->ki_filp;
15695 +       kio->ki_filp = h_file;
15696 +       if (iter) {
15697 +               lockdep_off();
15698 +               err = iter(kio, iov_iter);
15699 +               lockdep_on();
15700 +       } else
15701 +               /* currently there is no such fs */
15702 +               WARN_ON_ONCE(1);
15703 +       kio->ki_filp = file;
15704 +
15705 +out:
15706 +       return err;
15707 +}
15708 +
15709 +static ssize_t aufs_read_iter(struct kiocb *kio, struct iov_iter *iov_iter)
15710 +{
15711 +       ssize_t err;
15712 +       struct file *file, *h_file;
15713 +       struct inode *inode;
15714 +       struct super_block *sb;
15715 +
15716 +       file = kio->ki_filp;
15717 +       inode = file_inode(file);
15718 +       sb = inode->i_sb;
15719 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
15720 +
15721 +       h_file = au_read_pre(file, /*keep_fi*/1, /*lsc*/0);
15722 +       err = PTR_ERR(h_file);
15723 +       if (IS_ERR(h_file))
15724 +               goto out;
15725 +
15726 +       if (au_test_loopback_kthread()) {
15727 +               au_warn_loopback(h_file->f_path.dentry->d_sb);
15728 +               if (file->f_mapping != h_file->f_mapping) {
15729 +                       file->f_mapping = h_file->f_mapping;
15730 +                       smp_mb(); /* unnecessary? */
15731 +               }
15732 +       }
15733 +       fi_read_unlock(file);
15734 +
15735 +       err = au_do_iter(h_file, MAY_READ, kio, iov_iter);
15736 +       /* todo: necessary? */
15737 +       /* file->f_ra = h_file->f_ra; */
15738 +       au_read_post(inode, h_file);
15739 +
15740 +out:
15741 +       si_read_unlock(sb);
15742 +       return err;
15743 +}
15744 +
15745 +static ssize_t aufs_write_iter(struct kiocb *kio, struct iov_iter *iov_iter)
15746 +{
15747 +       ssize_t err;
15748 +       struct au_write_pre wpre;
15749 +       struct inode *inode;
15750 +       struct file *file, *h_file;
15751 +
15752 +       file = kio->ki_filp;
15753 +       inode = file_inode(file);
15754 +       au_mtx_and_read_lock(inode);
15755 +
15756 +       wpre.lsc = 0;
15757 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15758 +       err = PTR_ERR(h_file);
15759 +       if (IS_ERR(h_file))
15760 +               goto out;
15761 +
15762 +       err = au_do_iter(h_file, MAY_WRITE, kio, iov_iter);
15763 +       au_write_post(inode, h_file, &wpre, err);
15764 +
15765 +out:
15766 +       si_read_unlock(inode->i_sb);
15767 +       inode_unlock(inode);
15768 +       return err;
15769 +}
15770 +
15771 +/*
15772 + * We may be able to remove aufs_splice_{read,write}() since almost all FSes
15773 + * don't have their own .splice_{read,write} implimentations, and they use
15774 + * generic_file_splice_read() and iter_file_splice_write() who can act like the
15775 + * simple converters to f_op->iter_read() and ->iter_write().
15776 + * But we keep our own implementations because some non-mainlined FSes may have
15777 + * their own .splice_{read,write} implimentations and aufs doesn't want to take
15778 + * away an opportunity to co-work with aufs from them.
15779 + */
15780 +static ssize_t aufs_splice_read(struct file *file, loff_t *ppos,
15781 +                               struct pipe_inode_info *pipe, size_t len,
15782 +                               unsigned int flags)
15783 +{
15784 +       ssize_t err;
15785 +       struct file *h_file;
15786 +       struct inode *inode;
15787 +       struct super_block *sb;
15788 +
15789 +       inode = file_inode(file);
15790 +       sb = inode->i_sb;
15791 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
15792 +
15793 +       h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
15794 +       err = PTR_ERR(h_file);
15795 +       if (IS_ERR(h_file))
15796 +               goto out;
15797 +
15798 +       err = vfsub_splice_to(h_file, ppos, pipe, len, flags);
15799 +       /* todo: necessary? */
15800 +       /* file->f_ra = h_file->f_ra; */
15801 +       au_read_post(inode, h_file);
15802 +
15803 +out:
15804 +       si_read_unlock(sb);
15805 +       return err;
15806 +}
15807 +
15808 +static ssize_t
15809 +aufs_splice_write(struct pipe_inode_info *pipe, struct file *file, loff_t *ppos,
15810 +                 size_t len, unsigned int flags)
15811 +{
15812 +       ssize_t err;
15813 +       struct au_write_pre wpre;
15814 +       struct inode *inode;
15815 +       struct file *h_file;
15816 +
15817 +       inode = file_inode(file);
15818 +       au_mtx_and_read_lock(inode);
15819 +
15820 +       wpre.lsc = 0;
15821 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15822 +       err = PTR_ERR(h_file);
15823 +       if (IS_ERR(h_file))
15824 +               goto out;
15825 +
15826 +       err = vfsub_splice_from(pipe, h_file, ppos, len, flags);
15827 +       au_write_post(inode, h_file, &wpre, err);
15828 +
15829 +out:
15830 +       si_read_unlock(inode->i_sb);
15831 +       inode_unlock(inode);
15832 +       return err;
15833 +}
15834 +
15835 +static long aufs_fallocate(struct file *file, int mode, loff_t offset,
15836 +                          loff_t len)
15837 +{
15838 +       long err;
15839 +       struct au_write_pre wpre;
15840 +       struct inode *inode;
15841 +       struct file *h_file;
15842 +
15843 +       inode = file_inode(file);
15844 +       au_mtx_and_read_lock(inode);
15845 +
15846 +       wpre.lsc = 0;
15847 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
15848 +       err = PTR_ERR(h_file);
15849 +       if (IS_ERR(h_file))
15850 +               goto out;
15851 +
15852 +       lockdep_off();
15853 +       err = vfs_fallocate(h_file, mode, offset, len);
15854 +       lockdep_on();
15855 +       au_write_post(inode, h_file, &wpre, /*written*/1);
15856 +
15857 +out:
15858 +       si_read_unlock(inode->i_sb);
15859 +       inode_unlock(inode);
15860 +       return err;
15861 +}
15862 +
15863 +static ssize_t aufs_copy_file_range(struct file *src, loff_t src_pos,
15864 +                                   struct file *dst, loff_t dst_pos,
15865 +                                   size_t len, unsigned int flags)
15866 +{
15867 +       ssize_t err;
15868 +       struct au_write_pre wpre;
15869 +       enum { SRC, DST };
15870 +       struct {
15871 +               struct inode *inode;
15872 +               struct file *h_file;
15873 +               struct super_block *h_sb;
15874 +       } a[2];
15875 +#define a_src  a[SRC]
15876 +#define a_dst  a[DST]
15877 +
15878 +       err = -EINVAL;
15879 +       a_src.inode = file_inode(src);
15880 +       if (unlikely(!S_ISREG(a_src.inode->i_mode)))
15881 +               goto out;
15882 +       a_dst.inode = file_inode(dst);
15883 +       if (unlikely(!S_ISREG(a_dst.inode->i_mode)))
15884 +               goto out;
15885 +
15886 +       au_mtx_and_read_lock(a_dst.inode);
15887 +       /*
15888 +        * in order to match the order in di_write_lock2_{child,parent}(),
15889 +        * use f_path.dentry for this comparison.
15890 +        */
15891 +       if (src->f_path.dentry < dst->f_path.dentry) {
15892 +               a_src.h_file = au_read_pre(src, /*keep_fi*/1, AuLsc_FI_1);
15893 +               err = PTR_ERR(a_src.h_file);
15894 +               if (IS_ERR(a_src.h_file))
15895 +                       goto out_si;
15896 +
15897 +               wpre.lsc = AuLsc_FI_2;
15898 +               a_dst.h_file = au_write_pre(dst, /*do_ready*/1, &wpre);
15899 +               err = PTR_ERR(a_dst.h_file);
15900 +               if (IS_ERR(a_dst.h_file)) {
15901 +                       au_read_post(a_src.inode, a_src.h_file);
15902 +                       goto out_si;
15903 +               }
15904 +       } else {
15905 +               wpre.lsc = AuLsc_FI_1;
15906 +               a_dst.h_file = au_write_pre(dst, /*do_ready*/1, &wpre);
15907 +               err = PTR_ERR(a_dst.h_file);
15908 +               if (IS_ERR(a_dst.h_file))
15909 +                       goto out_si;
15910 +
15911 +               a_src.h_file = au_read_pre(src, /*keep_fi*/1, AuLsc_FI_2);
15912 +               err = PTR_ERR(a_src.h_file);
15913 +               if (IS_ERR(a_src.h_file)) {
15914 +                       au_write_post(a_dst.inode, a_dst.h_file, &wpre,
15915 +                                     /*written*/0);
15916 +                       goto out_si;
15917 +               }
15918 +       }
15919 +
15920 +       err = -EXDEV;
15921 +       a_src.h_sb = file_inode(a_src.h_file)->i_sb;
15922 +       a_dst.h_sb = file_inode(a_dst.h_file)->i_sb;
15923 +       if (unlikely(a_src.h_sb != a_dst.h_sb)) {
15924 +               AuDbgFile(src);
15925 +               AuDbgFile(dst);
15926 +               goto out_file;
15927 +       }
15928 +
15929 +       err = vfsub_copy_file_range(a_src.h_file, src_pos, a_dst.h_file,
15930 +                                   dst_pos, len, flags);
15931 +
15932 +out_file:
15933 +       au_write_post(a_dst.inode, a_dst.h_file, &wpre, err);
15934 +       fi_read_unlock(src);
15935 +       au_read_post(a_src.inode, a_src.h_file);
15936 +out_si:
15937 +       si_read_unlock(a_dst.inode->i_sb);
15938 +       inode_unlock(a_dst.inode);
15939 +out:
15940 +       return err;
15941 +#undef a_src
15942 +#undef a_dst
15943 +}
15944 +
15945 +/* ---------------------------------------------------------------------- */
15946 +
15947 +/*
15948 + * The locking order around current->mmap_sem.
15949 + * - in most and regular cases
15950 + *   file I/O syscall -- aufs_read() or something
15951 + *     -- si_rwsem for read -- mmap_sem
15952 + *     (Note that [fdi]i_rwsem are released before mmap_sem).
15953 + * - in mmap case
15954 + *   mmap(2) -- mmap_sem -- aufs_mmap() -- si_rwsem for read -- [fdi]i_rwsem
15955 + * This AB-BA order is definitely bad, but is not a problem since "si_rwsem for
15956 + * read" allows multiple processes to acquire it and [fdi]i_rwsem are not held
15957 + * in file I/O. Aufs needs to stop lockdep in aufs_mmap() though.
15958 + * It means that when aufs acquires si_rwsem for write, the process should never
15959 + * acquire mmap_sem.
15960 + *
15961 + * Actually aufs_iterate() holds [fdi]i_rwsem before mmap_sem, but this is not a
15962 + * problem either since any directory is not able to be mmap-ed.
15963 + * The similar scenario is applied to aufs_readlink() too.
15964 + */
15965 +
15966 +#if 0 /* stop calling security_file_mmap() */
15967 +/* cf. linux/include/linux/mman.h: calc_vm_prot_bits() */
15968 +#define AuConv_VM_PROT(f, b)   _calc_vm_trans(f, VM_##b, PROT_##b)
15969 +
15970 +static unsigned long au_arch_prot_conv(unsigned long flags)
15971 +{
15972 +       /* currently ppc64 only */
15973 +#ifdef CONFIG_PPC64
15974 +       /* cf. linux/arch/powerpc/include/asm/mman.h */
15975 +       AuDebugOn(arch_calc_vm_prot_bits(-1) != VM_SAO);
15976 +       return AuConv_VM_PROT(flags, SAO);
15977 +#else
15978 +       AuDebugOn(arch_calc_vm_prot_bits(-1));
15979 +       return 0;
15980 +#endif
15981 +}
15982 +
15983 +static unsigned long au_prot_conv(unsigned long flags)
15984 +{
15985 +       return AuConv_VM_PROT(flags, READ)
15986 +               | AuConv_VM_PROT(flags, WRITE)
15987 +               | AuConv_VM_PROT(flags, EXEC)
15988 +               | au_arch_prot_conv(flags);
15989 +}
15990 +
15991 +/* cf. linux/include/linux/mman.h: calc_vm_flag_bits() */
15992 +#define AuConv_VM_MAP(f, b)    _calc_vm_trans(f, VM_##b, MAP_##b)
15993 +
15994 +static unsigned long au_flag_conv(unsigned long flags)
15995 +{
15996 +       return AuConv_VM_MAP(flags, GROWSDOWN)
15997 +               | AuConv_VM_MAP(flags, DENYWRITE)
15998 +               | AuConv_VM_MAP(flags, LOCKED);
15999 +}
16000 +#endif
16001 +
16002 +static int aufs_mmap(struct file *file, struct vm_area_struct *vma)
16003 +{
16004 +       int err;
16005 +       const unsigned char wlock
16006 +               = (file->f_mode & FMODE_WRITE) && (vma->vm_flags & VM_SHARED);
16007 +       struct super_block *sb;
16008 +       struct file *h_file;
16009 +       struct inode *inode;
16010 +
16011 +       AuDbgVmRegion(file, vma);
16012 +
16013 +       inode = file_inode(file);
16014 +       sb = inode->i_sb;
16015 +       lockdep_off();
16016 +       si_read_lock(sb, AuLock_NOPLMW);
16017 +
16018 +       h_file = au_write_pre(file, wlock, /*wpre*/NULL);
16019 +       lockdep_on();
16020 +       err = PTR_ERR(h_file);
16021 +       if (IS_ERR(h_file))
16022 +               goto out;
16023 +
16024 +       err = 0;
16025 +       au_set_mmapped(file);
16026 +       au_vm_file_reset(vma, h_file);
16027 +       /*
16028 +        * we cannot call security_mmap_file() here since it may acquire
16029 +        * mmap_sem or i_mutex.
16030 +        *
16031 +        * err = security_mmap_file(h_file, au_prot_conv(vma->vm_flags),
16032 +        *                       au_flag_conv(vma->vm_flags));
16033 +        */
16034 +       if (!err)
16035 +               err = call_mmap(h_file, vma);
16036 +       if (!err) {
16037 +               au_vm_prfile_set(vma, file);
16038 +               fsstack_copy_attr_atime(inode, file_inode(h_file));
16039 +               goto out_fput; /* success */
16040 +       }
16041 +       au_unset_mmapped(file);
16042 +       au_vm_file_reset(vma, file);
16043 +
16044 +out_fput:
16045 +       lockdep_off();
16046 +       ii_write_unlock(inode);
16047 +       lockdep_on();
16048 +       fput(h_file);
16049 +out:
16050 +       lockdep_off();
16051 +       si_read_unlock(sb);
16052 +       lockdep_on();
16053 +       AuTraceErr(err);
16054 +       return err;
16055 +}
16056 +
16057 +/* ---------------------------------------------------------------------- */
16058 +
16059 +static int aufs_fsync_nondir(struct file *file, loff_t start, loff_t end,
16060 +                            int datasync)
16061 +{
16062 +       int err;
16063 +       struct au_write_pre wpre;
16064 +       struct inode *inode;
16065 +       struct file *h_file;
16066 +
16067 +       err = 0; /* -EBADF; */ /* posix? */
16068 +       if (unlikely(!(file->f_mode & FMODE_WRITE)))
16069 +               goto out;
16070 +
16071 +       inode = file_inode(file);
16072 +       au_mtx_and_read_lock(inode);
16073 +
16074 +       wpre.lsc = 0;
16075 +       h_file = au_write_pre(file, /*do_ready*/1, &wpre);
16076 +       err = PTR_ERR(h_file);
16077 +       if (IS_ERR(h_file))
16078 +               goto out_unlock;
16079 +
16080 +       err = vfsub_fsync(h_file, &h_file->f_path, datasync);
16081 +       au_write_post(inode, h_file, &wpre, /*written*/0);
16082 +
16083 +out_unlock:
16084 +       si_read_unlock(inode->i_sb);
16085 +       inode_unlock(inode);
16086 +out:
16087 +       return err;
16088 +}
16089 +
16090 +static int aufs_fasync(int fd, struct file *file, int flag)
16091 +{
16092 +       int err;
16093 +       struct file *h_file;
16094 +       struct super_block *sb;
16095 +
16096 +       sb = file->f_path.dentry->d_sb;
16097 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
16098 +
16099 +       h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
16100 +       err = PTR_ERR(h_file);
16101 +       if (IS_ERR(h_file))
16102 +               goto out;
16103 +
16104 +       if (h_file->f_op->fasync)
16105 +               err = h_file->f_op->fasync(fd, h_file, flag);
16106 +       fput(h_file); /* instead of au_read_post() */
16107 +
16108 +out:
16109 +       si_read_unlock(sb);
16110 +       return err;
16111 +}
16112 +
16113 +static int aufs_setfl(struct file *file, unsigned long arg)
16114 +{
16115 +       int err;
16116 +       struct file *h_file;
16117 +       struct super_block *sb;
16118 +
16119 +       sb = file->f_path.dentry->d_sb;
16120 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
16121 +
16122 +       h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
16123 +       err = PTR_ERR(h_file);
16124 +       if (IS_ERR(h_file))
16125 +               goto out;
16126 +
16127 +       /* stop calling h_file->fasync */
16128 +       arg |= vfsub_file_flags(file) & FASYNC;
16129 +       err = setfl(/*unused fd*/-1, h_file, arg);
16130 +       fput(h_file); /* instead of au_read_post() */
16131 +
16132 +out:
16133 +       si_read_unlock(sb);
16134 +       return err;
16135 +}
16136 +
16137 +/* ---------------------------------------------------------------------- */
16138 +
16139 +/* no one supports this operation, currently */
16140 +#if 0 /* reserved for future use */
16141 +static ssize_t aufs_sendpage(struct file *file, struct page *page, int offset,
16142 +                            size_t len, loff_t *pos, int more)
16143 +{
16144 +}
16145 +#endif
16146 +
16147 +/* ---------------------------------------------------------------------- */
16148 +
16149 +const struct file_operations aufs_file_fop = {
16150 +       .owner          = THIS_MODULE,
16151 +
16152 +       .llseek         = default_llseek,
16153 +
16154 +       .read_iter      = aufs_read_iter,
16155 +       .write_iter     = aufs_write_iter,
16156 +
16157 +#ifdef CONFIG_AUFS_POLL
16158 +       .poll           = aufs_poll,
16159 +#endif
16160 +       .unlocked_ioctl = aufs_ioctl_nondir,
16161 +#ifdef CONFIG_COMPAT
16162 +       .compat_ioctl   = aufs_compat_ioctl_nondir,
16163 +#endif
16164 +       .mmap           = aufs_mmap,
16165 +       .open           = aufs_open_nondir,
16166 +       .flush          = aufs_flush_nondir,
16167 +       .release        = aufs_release_nondir,
16168 +       .fsync          = aufs_fsync_nondir,
16169 +       .fasync         = aufs_fasync,
16170 +       /* .sendpage    = aufs_sendpage, */
16171 +       .setfl          = aufs_setfl,
16172 +       .splice_write   = aufs_splice_write,
16173 +       .splice_read    = aufs_splice_read,
16174 +#if 0 /* reserved for future use */
16175 +       .aio_splice_write = aufs_aio_splice_write,
16176 +       .aio_splice_read  = aufs_aio_splice_read,
16177 +#endif
16178 +       .fallocate      = aufs_fallocate,
16179 +       .copy_file_range = aufs_copy_file_range
16180 +};
16181 diff -urN /usr/share/empty/fs/aufs/fsctx.c linux/fs/aufs/fsctx.c
16182 --- /usr/share/empty/fs/aufs/fsctx.c    1970-01-01 01:00:00.000000000 +0100
16183 +++ linux/fs/aufs/fsctx.c       2022-03-21 14:49:05.723299677 +0100
16184 @@ -0,0 +1,1242 @@
16185 +// SPDX-License-Identifier: GPL-2.0
16186 +/*
16187 + * Copyright (C) 2022 Junjiro R. Okajima
16188 + *
16189 + * This program, aufs is free software; you can redistribute it and/or modify
16190 + * it under the terms of the GNU General Public License as published by
16191 + * the Free Software Foundation; either version 2 of the License, or
16192 + * (at your option) any later version.
16193 + *
16194 + * This program is distributed in the hope that it will be useful,
16195 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
16196 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16197 + * GNU General Public License for more details.
16198 + *
16199 + * You should have received a copy of the GNU General Public License
16200 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16201 + */
16202 +
16203 +/*
16204 + * fs context, aka new mount api
16205 + */
16206 +
16207 +#include <linux/fs_context.h>
16208 +#include "aufs.h"
16209 +
16210 +struct au_fsctx_opts {
16211 +       aufs_bindex_t bindex;
16212 +       unsigned char skipped;
16213 +       struct au_opt *opt, *opt_tail;
16214 +       struct super_block *sb;
16215 +       struct au_sbinfo *sbinfo;
16216 +       struct au_opts opts;
16217 +};
16218 +
16219 +/* stop extra interpretation of errno in mount(8), and strange error messages */
16220 +static int cvt_err(int err)
16221 +{
16222 +       AuTraceErr(err);
16223 +
16224 +       switch (err) {
16225 +       case -ENOENT:
16226 +       case -ENOTDIR:
16227 +       case -EEXIST:
16228 +       case -EIO:
16229 +               err = -EINVAL;
16230 +       }
16231 +       return err;
16232 +}
16233 +
16234 +static int au_fsctx_reconfigure(struct fs_context *fc)
16235 +{
16236 +       int err, do_dx;
16237 +       unsigned int mntflags;
16238 +       struct dentry *root;
16239 +       struct super_block *sb;
16240 +       struct inode *inode;
16241 +       struct au_fsctx_opts *a = fc->fs_private;
16242 +
16243 +       AuDbg("fc %p\n", fc);
16244 +
16245 +       root = fc->root;
16246 +       sb = root->d_sb;
16247 +       err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
16248 +       if (!err) {
16249 +               di_write_lock_child(root);
16250 +               err = au_opts_verify(sb, fc->sb_flags, /*pending*/0);
16251 +               aufs_write_unlock(root);
16252 +       }
16253 +
16254 +       inode = d_inode(root);
16255 +       inode_lock(inode);
16256 +       err = si_write_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
16257 +       if (unlikely(err))
16258 +               goto out;
16259 +       di_write_lock_child(root);
16260 +
16261 +       /* au_opts_remount() may return an error */
16262 +       err = au_opts_remount(sb, &a->opts);
16263 +
16264 +       if (au_ftest_opts(a->opts.flags, REFRESH))
16265 +               au_remount_refresh(sb, au_ftest_opts(a->opts.flags,
16266 +                                                    REFRESH_IDOP));
16267 +
16268 +       if (au_ftest_opts(a->opts.flags, REFRESH_DYAOP)) {
16269 +               mntflags = au_mntflags(sb);
16270 +               do_dx = !!au_opt_test(mntflags, DIO);
16271 +               au_dy_arefresh(do_dx);
16272 +       }
16273 +
16274 +       au_fhsm_wrote_all(sb, /*force*/1); /* ?? */
16275 +       aufs_write_unlock(root);
16276 +
16277 +out:
16278 +       inode_unlock(inode);
16279 +       err = cvt_err(err);
16280 +       AuTraceErr(err);
16281 +
16282 +       return err;
16283 +}
16284 +
16285 +/* ---------------------------------------------------------------------- */
16286 +
16287 +static int au_fsctx_fill_super(struct super_block *sb, struct fs_context *fc)
16288 +{
16289 +       int err;
16290 +       struct au_fsctx_opts *a = fc->fs_private;
16291 +       struct au_sbinfo *sbinfo = a->sbinfo;
16292 +       struct dentry *root;
16293 +       struct inode *inode;
16294 +
16295 +       sbinfo->si_sb = sb;
16296 +       sb->s_fs_info = sbinfo;
16297 +       kobject_get(&sbinfo->si_kobj);
16298 +
16299 +       __si_write_lock(sb);
16300 +       si_pid_set(sb);
16301 +       au_sbilist_add(sb);
16302 +
16303 +       /* all timestamps always follow the ones on the branch */
16304 +       sb->s_flags |= SB_NOATIME | SB_NODIRATIME;
16305 +       sb->s_flags |= SB_I_VERSION; /* do we really need this? */
16306 +       sb->s_op = &aufs_sop;
16307 +       sb->s_d_op = &aufs_dop;
16308 +       sb->s_magic = AUFS_SUPER_MAGIC;
16309 +       sb->s_maxbytes = 0;
16310 +       sb->s_stack_depth = 1;
16311 +       au_export_init(sb);
16312 +       au_xattr_init(sb);
16313 +
16314 +       err = au_alloc_root(sb);
16315 +       if (unlikely(err)) {
16316 +               si_write_unlock(sb);
16317 +               goto out;
16318 +       }
16319 +       root = sb->s_root;
16320 +       inode = d_inode(root);
16321 +       ii_write_lock_parent(inode);
16322 +       aufs_write_unlock(root);
16323 +
16324 +       /* lock vfs_inode first, then aufs. */
16325 +       inode_lock(inode);
16326 +       aufs_write_lock(root);
16327 +       err = au_opts_mount(sb, &a->opts);
16328 +       AuTraceErr(err);
16329 +       if (!err && au_ftest_si(sbinfo, NO_DREVAL)) {
16330 +               sb->s_d_op = &aufs_dop_noreval;
16331 +               /* infofc(fc, "%ps", sb->s_d_op); */
16332 +               pr_info("%ps\n", sb->s_d_op);
16333 +               au_refresh_dop(root, /*force_reval*/0);
16334 +               sbinfo->si_iop_array = aufs_iop_nogetattr;
16335 +               au_refresh_iop(inode, /*force_getattr*/0);
16336 +       }
16337 +       aufs_write_unlock(root);
16338 +       inode_unlock(inode);
16339 +       if (!err)
16340 +               goto out; /* success */
16341 +
16342 +       dput(root);
16343 +       sb->s_root = NULL;
16344 +
16345 +out:
16346 +       if (unlikely(err))
16347 +               kobject_put(&sbinfo->si_kobj);
16348 +       AuTraceErr(err);
16349 +       err = cvt_err(err);
16350 +       AuTraceErr(err);
16351 +       return err;
16352 +}
16353 +
16354 +static int au_fsctx_get_tree(struct fs_context *fc)
16355 +{
16356 +       int err;
16357 +
16358 +       AuDbg("fc %p\n", fc);
16359 +       err = get_tree_nodev(fc, au_fsctx_fill_super);
16360 +
16361 +       AuTraceErr(err);
16362 +       return err;
16363 +}
16364 +
16365 +/* ---------------------------------------------------------------------- */
16366 +
16367 +static void au_fsctx_dump(struct au_opts *opts)
16368 +{
16369 +#ifdef CONFIG_AUFS_DEBUG
16370 +       /* reduce stack space */
16371 +       union {
16372 +               struct au_opt_add *add;
16373 +               struct au_opt_del *del;
16374 +               struct au_opt_mod *mod;
16375 +               struct au_opt_xino *xino;
16376 +               struct au_opt_xino_itrunc *xino_itrunc;
16377 +               struct au_opt_wbr_create *create;
16378 +       } u;
16379 +       struct au_opt *opt;
16380 +
16381 +       opt = opts->opt;
16382 +       while (opt->type != Opt_tail) {
16383 +               switch (opt->type) {
16384 +               case Opt_add:
16385 +                       u.add = &opt->add;
16386 +                       AuDbg("add {b%d, %s, 0x%x, %p}\n",
16387 +                                 u.add->bindex, u.add->pathname, u.add->perm,
16388 +                                 u.add->path.dentry);
16389 +                       break;
16390 +               case Opt_del:
16391 +                       fallthrough;
16392 +               case Opt_idel:
16393 +                       u.del = &opt->del;
16394 +                       AuDbg("del {%s, %p}\n",
16395 +                             u.del->pathname, u.del->h_path.dentry);
16396 +                       break;
16397 +               case Opt_mod:
16398 +                       fallthrough;
16399 +               case Opt_imod:
16400 +                       u.mod = &opt->mod;
16401 +                       AuDbg("mod {%s, 0x%x, %p}\n",
16402 +                                 u.mod->path, u.mod->perm, u.mod->h_root);
16403 +                       break;
16404 +               case Opt_append:
16405 +                       u.add = &opt->add;
16406 +                       AuDbg("append {b%d, %s, 0x%x, %p}\n",
16407 +                                 u.add->bindex, u.add->pathname, u.add->perm,
16408 +                                 u.add->path.dentry);
16409 +                       break;
16410 +               case Opt_prepend:
16411 +                       u.add = &opt->add;
16412 +                       AuDbg("prepend {b%d, %s, 0x%x, %p}\n",
16413 +                                 u.add->bindex, u.add->pathname, u.add->perm,
16414 +                                 u.add->path.dentry);
16415 +                       break;
16416 +
16417 +               case Opt_dirwh:
16418 +                       AuDbg("dirwh %d\n", opt->dirwh);
16419 +                       break;
16420 +               case Opt_rdcache:
16421 +                       AuDbg("rdcache %d\n", opt->rdcache);
16422 +                       break;
16423 +               case Opt_rdblk:
16424 +                       AuDbg("rdblk %d\n", opt->rdblk);
16425 +                       break;
16426 +               case Opt_rdhash:
16427 +                       AuDbg("rdhash %u\n", opt->rdhash);
16428 +                       break;
16429 +
16430 +               case Opt_xino:
16431 +                       u.xino = &opt->xino;
16432 +                       AuDbg("xino {%s %pD}\n", u.xino->path, u.xino->file);
16433 +                       break;
16434 +
16435 +#define au_fsctx_TF(name)                                        \
16436 +                       case Opt_##name:                          \
16437 +                               if (opt->tf)                      \
16438 +                                       AuLabel(name);            \
16439 +                               else                              \
16440 +                                       AuLabel(no##name);        \
16441 +                               break;
16442 +
16443 +               /* simple true/false flag */
16444 +               au_fsctx_TF(trunc_xino);
16445 +               au_fsctx_TF(trunc_xib);
16446 +               au_fsctx_TF(dirperm1);
16447 +               au_fsctx_TF(plink);
16448 +               au_fsctx_TF(shwh);
16449 +               au_fsctx_TF(dio);
16450 +               au_fsctx_TF(warn_perm);
16451 +               au_fsctx_TF(verbose);
16452 +               au_fsctx_TF(sum);
16453 +               au_fsctx_TF(dirren);
16454 +               au_fsctx_TF(acl);
16455 +#undef au_fsctx_TF
16456 +
16457 +               case Opt_trunc_xino_path:
16458 +                       fallthrough;
16459 +               case Opt_itrunc_xino:
16460 +                       u.xino_itrunc = &opt->xino_itrunc;
16461 +                       AuDbg("trunc_xino %d\n", u.xino_itrunc->bindex);
16462 +                       break;
16463 +               case Opt_noxino:
16464 +                       AuLabel(noxino);
16465 +                       break;
16466 +
16467 +               case Opt_list_plink:
16468 +                       AuLabel(list_plink);
16469 +                       break;
16470 +               case Opt_udba:
16471 +                       AuDbg("udba %d, %s\n",
16472 +                                 opt->udba, au_optstr_udba(opt->udba));
16473 +                       break;
16474 +               case Opt_diropq_a:
16475 +                       AuLabel(diropq_a);
16476 +                       break;
16477 +               case Opt_diropq_w:
16478 +                       AuLabel(diropq_w);
16479 +                       break;
16480 +               case Opt_wsum:
16481 +                       AuLabel(wsum);
16482 +                       break;
16483 +               case Opt_wbr_create:
16484 +                       u.create = &opt->wbr_create;
16485 +                       AuDbg("create %d, %s\n", u.create->wbr_create,
16486 +                                 au_optstr_wbr_create(u.create->wbr_create));
16487 +                       switch (u.create->wbr_create) {
16488 +                       case AuWbrCreate_MFSV:
16489 +                               fallthrough;
16490 +                       case AuWbrCreate_PMFSV:
16491 +                               AuDbg("%d sec\n", u.create->mfs_second);
16492 +                               break;
16493 +                       case AuWbrCreate_MFSRR:
16494 +                               fallthrough;
16495 +                       case AuWbrCreate_TDMFS:
16496 +                               AuDbg("%llu watermark\n",
16497 +                                         u.create->mfsrr_watermark);
16498 +                               break;
16499 +                       case AuWbrCreate_MFSRRV:
16500 +                               fallthrough;
16501 +                       case AuWbrCreate_TDMFSV:
16502 +                               fallthrough;
16503 +                       case AuWbrCreate_PMFSRRV:
16504 +                               AuDbg("%llu watermark, %d sec\n",
16505 +                                         u.create->mfsrr_watermark,
16506 +                                         u.create->mfs_second);
16507 +                               break;
16508 +                       }
16509 +                       break;
16510 +               case Opt_wbr_copyup:
16511 +                       AuDbg("copyup %d, %s\n", opt->wbr_copyup,
16512 +                                 au_optstr_wbr_copyup(opt->wbr_copyup));
16513 +                       break;
16514 +               case Opt_fhsm_sec:
16515 +                       AuDbg("fhsm_sec %u\n", opt->fhsm_second);
16516 +                       break;
16517 +
16518 +               default:
16519 +                       AuDbg("type %d\n", opt->type);
16520 +                       BUG();
16521 +               }
16522 +               opt++;
16523 +       }
16524 +#endif
16525 +}
16526 +
16527 +/* ---------------------------------------------------------------------- */
16528 +
16529 +/*
16530 + * For conditionally compiled mount options.
16531 + * Instead of fsparam_flag_no(), use this macro to distinguish ignore_silent.
16532 + */
16533 +#define au_ignore_flag(name, action)           \
16534 +       fsparam_flag(name, action),             \
16535 +       fsparam_flag("no" name, Opt_ignore_silent)
16536 +
16537 +const struct fs_parameter_spec aufs_fsctx_paramspec[] = {
16538 +       fsparam_string("br", Opt_br),
16539 +
16540 +       /* "add=%d:%s" or "ins=%d:%s" */
16541 +       fsparam_string("add", Opt_add),
16542 +       fsparam_string("ins", Opt_add),
16543 +       fsparam_path("append", Opt_append),
16544 +       fsparam_path("prepend", Opt_prepend),
16545 +
16546 +       fsparam_path("del", Opt_del),
16547 +       /* fsparam_s32("idel", Opt_idel), */
16548 +       fsparam_path("mod", Opt_mod),
16549 +       /* fsparam_string("imod", Opt_imod), */
16550 +
16551 +       fsparam_s32("dirwh", Opt_dirwh),
16552 +
16553 +       fsparam_path("xino", Opt_xino),
16554 +       fsparam_flag("noxino", Opt_noxino),
16555 +       fsparam_flag_no("trunc_xino", Opt_trunc_xino),
16556 +       /* "trunc_xino_v=%d:%d" */
16557 +       /* fsparam_string("trunc_xino_v", Opt_trunc_xino_v), */
16558 +       fsparam_path("trunc_xino", Opt_trunc_xino_path),
16559 +       fsparam_s32("itrunc_xino", Opt_itrunc_xino),
16560 +       /* fsparam_path("zxino", Opt_zxino), */
16561 +       fsparam_flag_no("trunc_xib", Opt_trunc_xib),
16562 +
16563 +#ifdef CONFIG_PROC_FS
16564 +       fsparam_flag_no("plink", Opt_plink),
16565 +#else
16566 +       au_ignore_flag("plink", Opt_ignore),
16567 +#endif
16568 +
16569 +#ifdef CONFIG_AUFS_DEBUG
16570 +       fsparam_flag("list_plink", Opt_list_plink),
16571 +#endif
16572 +
16573 +       fsparam_string("udba", Opt_udba),
16574 +
16575 +       fsparam_flag_no("dio", Opt_dio),
16576 +
16577 +#ifdef CONFIG_AUFS_DIRREN
16578 +       fsparam_flag_no("dirren", Opt_dirren),
16579 +#else
16580 +       au_ignore_flag("dirren", Opt_ignore),
16581 +#endif
16582 +
16583 +#ifdef CONFIG_AUFS_FHSM
16584 +       fsparam_s32("fhsm_sec", Opt_fhsm_sec),
16585 +#else
16586 +       fsparam_s32("fhsm_sec", Opt_ignore),
16587 +#endif
16588 +
16589 +       /* always | a | whiteouted | w */
16590 +       fsparam_string("diropq", Opt_diropq),
16591 +
16592 +       fsparam_flag_no("warn_perm", Opt_warn_perm),
16593 +
16594 +#ifdef CONFIG_AUFS_SHWH
16595 +       fsparam_flag_no("shwh", Opt_shwh),
16596 +#else
16597 +       au_ignore_flag("shwh", Opt_err),
16598 +#endif
16599 +
16600 +       fsparam_flag_no("dirperm1", Opt_dirperm1),
16601 +
16602 +       fsparam_flag_no("verbose", Opt_verbose),
16603 +       fsparam_flag("v", Opt_verbose),
16604 +       fsparam_flag("quiet", Opt_noverbose),
16605 +       fsparam_flag("q", Opt_noverbose),
16606 +       /* user-space may handle this */
16607 +       fsparam_flag("silent", Opt_noverbose),
16608 +
16609 +       fsparam_flag_no("sum", Opt_sum),
16610 +       fsparam_flag("wsum", Opt_wsum),
16611 +
16612 +       fsparam_s32("rdcache", Opt_rdcache),
16613 +       /* "def" or s32 */
16614 +       fsparam_string("rdblk", Opt_rdblk),
16615 +       /* "def" or s32 */
16616 +       fsparam_string("rdhash", Opt_rdhash),
16617 +
16618 +       fsparam_string("create", Opt_wbr_create),
16619 +       fsparam_string("create_policy", Opt_wbr_create),
16620 +       fsparam_string("cpup", Opt_wbr_copyup),
16621 +       fsparam_string("copyup", Opt_wbr_copyup),
16622 +       fsparam_string("copyup_policy", Opt_wbr_copyup),
16623 +
16624 +       /* generic VFS flag */
16625 +#ifdef CONFIG_FS_POSIX_ACL
16626 +       fsparam_flag_no("acl", Opt_acl),
16627 +#else
16628 +       au_ignore_flag("acl"),
16629 +#endif
16630 +
16631 +       /* internal use for the scripts */
16632 +       fsparam_string("si", Opt_ignore_silent),
16633 +
16634 +       /* obsoleted, keep them temporary */
16635 +       fsparam_flag("nodlgt", Opt_ignore_silent),
16636 +       fsparam_flag("clean_plink", Opt_ignore),
16637 +       fsparam_string("dirs", Opt_br),
16638 +       fsparam_u32("debug", Opt_ignore),
16639 +       /* "whiteout" or "all" */
16640 +       fsparam_string("delete", Opt_ignore),
16641 +       fsparam_string("imap", Opt_ignore),
16642 +
16643 +       /* temporary workaround, due to old mount(8)? */
16644 +       fsparam_flag("relatime", Opt_ignore_silent),
16645 +
16646 +       {}
16647 +};
16648 +
16649 +static int au_fsctx_parse_do_add(struct fs_context *fc, struct au_opt *opt,
16650 +                                char *brspec, size_t speclen,
16651 +                                aufs_bindex_t bindex)
16652 +{
16653 +       int err;
16654 +       char *p;
16655 +
16656 +       AuDbg("brspec %s\n", brspec);
16657 +
16658 +       err = -ENOMEM;
16659 +       if (!speclen)
16660 +               speclen = strlen(brspec);
16661 +       /* will be freed by au_fsctx_free() */
16662 +       p = kmemdup_nul(brspec, speclen, GFP_NOFS);
16663 +       if (unlikely(!p)) {
16664 +               errorfc(fc, "failed in %s", brspec);
16665 +               goto out;
16666 +       }
16667 +       err = au_opt_add(opt, p, fc->sb_flags, bindex);
16668 +
16669 +out:
16670 +       AuTraceErr(err);
16671 +       return err;
16672 +}
16673 +
16674 +static int au_fsctx_parse_br(struct fs_context *fc, char *brspec)
16675 +{
16676 +       int err;
16677 +       char *p;
16678 +       struct au_fsctx_opts *a = fc->fs_private;
16679 +       struct au_opt *opt = a->opt;
16680 +       aufs_bindex_t bindex = a->bindex;
16681 +
16682 +       AuDbg("brspec %s\n", brspec);
16683 +
16684 +       err = -EINVAL;
16685 +       while ((p = strsep(&brspec, ":")) && *p) {
16686 +               err = au_fsctx_parse_do_add(fc, opt, p, /*len*/0, bindex);
16687 +               AuTraceErr(err);
16688 +               if (unlikely(err))
16689 +                       break;
16690 +               bindex++;
16691 +               opt++;
16692 +               if (unlikely(opt > a->opt_tail)) {
16693 +                       err = -E2BIG;
16694 +                       bindex--;
16695 +                       opt--;
16696 +                       break;
16697 +               }
16698 +               opt->type = Opt_tail;
16699 +               a->skipped = 1;
16700 +       }
16701 +       a->bindex = bindex;
16702 +       a->opt = opt;
16703 +
16704 +       AuTraceErr(err);
16705 +       return err;
16706 +}
16707 +
16708 +static int au_fsctx_parse_add(struct fs_context *fc, char *addspec)
16709 +{
16710 +       int err, n;
16711 +       char *p;
16712 +       struct au_fsctx_opts *a = fc->fs_private;
16713 +       struct au_opt *opt = a->opt;
16714 +
16715 +       err = -EINVAL;
16716 +       p = strchr(addspec, ':');
16717 +       if (unlikely(!p)) {
16718 +               errorfc(fc, "bad arg in %s", addspec);
16719 +               goto out;
16720 +       }
16721 +       *p++ = '\0';
16722 +       err = kstrtoint(addspec, 0, &n);
16723 +       if (unlikely(err)) {
16724 +               errorfc(fc, "bad integer in %s", addspec);
16725 +               goto out;
16726 +       }
16727 +       AuDbg("n %d\n", n);
16728 +       err = au_fsctx_parse_do_add(fc, opt, p, /*len*/0, n);
16729 +
16730 +out:
16731 +       AuTraceErr(err);
16732 +       return err;
16733 +}
16734 +
16735 +static int au_fsctx_parse_del(struct fs_context *fc, struct au_opt_del *del,
16736 +                             struct fs_parameter *param)
16737 +{
16738 +       int err;
16739 +
16740 +       err = -ENOMEM;
16741 +       /* will be freed by au_fsctx_free() */
16742 +       del->pathname = kmemdup_nul(param->string, param->size, GFP_NOFS);
16743 +       if (unlikely(!del->pathname))
16744 +               goto out;
16745 +       AuDbg("del %s\n", del->pathname);
16746 +       err = vfsub_kern_path(del->pathname, AuOpt_LkupDirFlags, &del->h_path);
16747 +       if (unlikely(err))
16748 +               errorfc(fc, "lookup failed %s (%d)", del->pathname, err);
16749 +
16750 +out:
16751 +       AuTraceErr(err);
16752 +       return err;
16753 +}
16754 +
16755 +#if 0 /* reserved for future use */
16756 +static int au_fsctx_parse_idel(struct fs_context *fc, struct au_opt_del *del,
16757 +                              aufs_bindex_t bindex)
16758 +{
16759 +       int err;
16760 +       struct super_block *sb;
16761 +       struct dentry *root;
16762 +       struct au_fsctx_opts *a = fc->fs_private;
16763 +
16764 +       sb = a->sb;
16765 +       AuDebugOn(!sb);
16766 +
16767 +       err = -EINVAL;
16768 +       root = sb->s_root;
16769 +       aufs_read_lock(root, AuLock_FLUSH);
16770 +       if (bindex < 0 || au_sbbot(sb) < bindex) {
16771 +               errorfc(fc, "out of bounds, %d", bindex);
16772 +               goto out;
16773 +       }
16774 +
16775 +       err = 0;
16776 +       del->h_path.dentry = dget(au_h_dptr(root, bindex));
16777 +       del->h_path.mnt = mntget(au_sbr_mnt(sb, bindex));
16778 +
16779 +out:
16780 +       aufs_read_unlock(root, !AuLock_IR);
16781 +       AuTraceErr(err);
16782 +       return err;
16783 +}
16784 +#endif
16785 +
16786 +static int au_fsctx_parse_mod(struct fs_context *fc, struct au_opt_mod *mod,
16787 +                             struct fs_parameter *param)
16788 +{
16789 +       int err;
16790 +       struct path path;
16791 +       char *p;
16792 +
16793 +       err = -ENOMEM;
16794 +       /* will be freed by au_fsctx_free() */
16795 +       mod->path = kmemdup_nul(param->string, param->size, GFP_NOFS);
16796 +       if (unlikely(!mod->path))
16797 +               goto out;
16798 +
16799 +       err = -EINVAL;
16800 +       p = strchr(mod->path, '=');
16801 +       if (unlikely(!p)) {
16802 +               errorfc(fc, "no permission %s", mod->path);
16803 +               goto out;
16804 +       }
16805 +
16806 +       *p++ = 0;
16807 +       err = vfsub_kern_path(mod->path, AuOpt_LkupDirFlags, &path);
16808 +       if (unlikely(err)) {
16809 +               errorfc(fc, "lookup failed %s (%d)", mod->path, err);
16810 +               goto out;
16811 +       }
16812 +
16813 +       mod->perm = au_br_perm_val(p);
16814 +       AuDbg("mod path %s, perm 0x%x, %s", mod->path, mod->perm, p);
16815 +       mod->h_root = dget(path.dentry);
16816 +       path_put(&path);
16817 +
16818 +out:
16819 +       AuTraceErr(err);
16820 +       return err;
16821 +}
16822 +
16823 +#if 0 /* reserved for future use */
16824 +static int au_fsctx_parse_imod(struct fs_context *fc, struct au_opt_mod *mod,
16825 +                              char *ibrspec)
16826 +{
16827 +       int err, n;
16828 +       char *p;
16829 +       struct super_block *sb;
16830 +       struct dentry *root;
16831 +       struct au_fsctx_opts *a = fc->fs_private;
16832 +
16833 +       sb = a->sb;
16834 +       AuDebugOn(!sb);
16835 +
16836 +       err = -EINVAL;
16837 +       p = strchr(ibrspec, ':');
16838 +       if (unlikely(!p)) {
16839 +               errorfc(fc, "no index, %s", ibrspec);
16840 +               goto out;
16841 +       }
16842 +       *p++ = '\0';
16843 +       err = kstrtoint(ibrspec, 0, &n);
16844 +       if (unlikely(err)) {
16845 +               errorfc(fc, "bad integer in %s", ibrspec);
16846 +               goto out;
16847 +       }
16848 +       AuDbg("n %d\n", n);
16849 +
16850 +       root = sb->s_root;
16851 +       aufs_read_lock(root, AuLock_FLUSH);
16852 +       if (n < 0 || au_sbbot(sb) < n) {
16853 +               errorfc(fc, "out of bounds, %d", bindex);
16854 +               goto out_root;
16855 +       }
16856 +
16857 +       err = 0;
16858 +       mod->perm = au_br_perm_val(p);
16859 +       AuDbg("mod path %s, perm 0x%x, %s\n",
16860 +             mod->path, mod->perm, p);
16861 +       mod->h_root = dget(au_h_dptr(root, bindex));
16862 +
16863 +out_root:
16864 +       aufs_read_unlock(root, !AuLock_IR);
16865 +out:
16866 +       AuTraceErr(err);
16867 +       return err;
16868 +}
16869 +#endif
16870 +
16871 +static int au_fsctx_parse_xino(struct fs_context *fc,
16872 +                              struct au_opt_xino *xino,
16873 +                              struct fs_parameter *param)
16874 +{
16875 +       int err;
16876 +       struct au_fsctx_opts *a = fc->fs_private;
16877 +
16878 +       err = -ENOMEM;
16879 +       /* will be freed by au_opts_free() */
16880 +       xino->path = kmemdup_nul(param->string, param->size, GFP_NOFS);
16881 +       if (unlikely(!xino->path))
16882 +               goto out;
16883 +       AuDbg("path %s\n", xino->path);
16884 +
16885 +       xino->file = au_xino_create(a->sb, xino->path, /*silent*/0,
16886 +                                   /*wbrtop*/0);
16887 +       err = PTR_ERR(xino->file);
16888 +       if (IS_ERR(xino->file)) {
16889 +               xino->file = NULL;
16890 +               goto out;
16891 +       }
16892 +
16893 +       err = 0;
16894 +       if (unlikely(a->sb && xino->file->f_path.dentry->d_sb == a->sb)) {
16895 +               err = -EINVAL;
16896 +               errorfc(fc, "%s must be outside", xino->path);
16897 +       }
16898 +
16899 +out:
16900 +       AuTraceErr(err);
16901 +       return err;
16902 +}
16903 +
16904 +static
16905 +int au_fsctx_parse_xino_itrunc_path(struct fs_context *fc,
16906 +                                   struct au_opt_xino_itrunc *xino_itrunc,
16907 +                                   char *pathname)
16908 +{
16909 +       int err;
16910 +       aufs_bindex_t bbot, bindex;
16911 +       struct path path;
16912 +       struct dentry *root;
16913 +       struct au_fsctx_opts *a = fc->fs_private;
16914 +
16915 +       AuDebugOn(!a->sb);
16916 +
16917 +       err = vfsub_kern_path(pathname, AuOpt_LkupDirFlags, &path);
16918 +       if (unlikely(err)) {
16919 +               errorfc(fc, "lookup failed %s (%d)", pathname, err);
16920 +               goto out;
16921 +       }
16922 +
16923 +       xino_itrunc->bindex = -1;
16924 +       root = a->sb->s_root;
16925 +       aufs_read_lock(root, AuLock_FLUSH);
16926 +       bbot = au_sbbot(a->sb);
16927 +       for (bindex = 0; bindex <= bbot; bindex++) {
16928 +               if (au_h_dptr(root, bindex) == path.dentry) {
16929 +                       xino_itrunc->bindex = bindex;
16930 +                       break;
16931 +               }
16932 +       }
16933 +       aufs_read_unlock(root, !AuLock_IR);
16934 +       path_put(&path);
16935 +
16936 +       if (unlikely(xino_itrunc->bindex < 0)) {
16937 +               err = -EINVAL;
16938 +               errorfc(fc, "no such branch %s", pathname);
16939 +       }
16940 +
16941 +out:
16942 +       AuTraceErr(err);
16943 +       return err;
16944 +}
16945 +
16946 +static int au_fsctx_parse_xino_itrunc(struct fs_context *fc,
16947 +                                     struct au_opt_xino_itrunc *xino_itrunc,
16948 +                                     unsigned int bindex)
16949 +{
16950 +       int err;
16951 +       aufs_bindex_t bbot;
16952 +       struct super_block *sb;
16953 +       struct au_fsctx_opts *a = fc->fs_private;
16954 +
16955 +       sb = a->sb;
16956 +       AuDebugOn(!sb);
16957 +
16958 +       err = 0;
16959 +       si_noflush_read_lock(sb);
16960 +       bbot = au_sbbot(sb);
16961 +       si_read_unlock(sb);
16962 +       if (bindex <= bbot)
16963 +               xino_itrunc->bindex = bindex;
16964 +       else {
16965 +               err = -EINVAL;
16966 +               errorfc(fc, "out of bounds, %u", bindex);
16967 +       }
16968 +
16969 +       AuTraceErr(err);
16970 +       return err;
16971 +}
16972 +
16973 +static int au_fsctx_parse_param(struct fs_context *fc, struct fs_parameter *param)
16974 +{
16975 +       int err, token;
16976 +       struct fs_parse_result result;
16977 +       struct au_fsctx_opts *a = fc->fs_private;
16978 +       struct au_opt *opt = a->opt;
16979 +
16980 +       AuDbg("fc %p, param {key %s, string %s}\n",
16981 +             fc, param->key, param->string);
16982 +       err = fs_parse(fc, aufs_fsctx_paramspec, param, &result);
16983 +       if (unlikely(err < 0))
16984 +               goto out;
16985 +       token = err;
16986 +       AuDbg("token %d, res{negated %d, uint64 %llu}\n",
16987 +             token, result.negated, result.uint_64);
16988 +
16989 +       err = -EINVAL;
16990 +       a->skipped = 0;
16991 +       switch (token) {
16992 +       case Opt_br:
16993 +               err = au_fsctx_parse_br(fc, param->string);
16994 +               break;
16995 +       case Opt_add:
16996 +               err = au_fsctx_parse_add(fc, param->string);
16997 +               break;
16998 +       case Opt_append:
16999 +               err = au_fsctx_parse_do_add(fc, opt, param->string, param->size,
17000 +                                           /*dummy bindex*/1);
17001 +               break;
17002 +       case Opt_prepend:
17003 +               err = au_fsctx_parse_do_add(fc, opt, param->string, param->size,
17004 +                                           /*bindex*/0);
17005 +               break;
17006 +
17007 +       case Opt_del:
17008 +               err = au_fsctx_parse_del(fc, &opt->del, param);
17009 +               break;
17010 +#if 0 /* reserved for future use */
17011 +       case Opt_idel:
17012 +               if (!a->sb) {
17013 +                       err = 0;
17014 +                       a->skipped = 1;
17015 +                       break;
17016 +               }
17017 +               del->pathname = "(indexed)";
17018 +               err = au_opts_parse_idel(fc, &opt->del, result.uint_32);
17019 +               break;
17020 +#endif
17021 +
17022 +       case Opt_mod:
17023 +               err = au_fsctx_parse_mod(fc, &opt->mod, param);
17024 +               break;
17025 +#ifdef IMOD /* reserved for future use */
17026 +       case Opt_imod:
17027 +               if (!a->sb) {
17028 +                       err = 0;
17029 +                       a->skipped = 1;
17030 +                       break;
17031 +               }
17032 +               u.mod->path = "(indexed)";
17033 +               err = au_opts_parse_imod(fc, &opt->mod, param->string);
17034 +               break;
17035 +#endif
17036 +
17037 +       case Opt_xino:
17038 +               err = au_fsctx_parse_xino(fc, &opt->xino, param);
17039 +               break;
17040 +       case Opt_trunc_xino_path:
17041 +               if (!a->sb) {
17042 +                       errorfc(fc, "no such branch %s", param->string);
17043 +                       break;
17044 +               }
17045 +               err = au_fsctx_parse_xino_itrunc_path(fc, &opt->xino_itrunc,
17046 +                                                     param->string);
17047 +               break;
17048 +#if 0
17049 +       case Opt_trunc_xino_v:
17050 +               if (!a->sb) {
17051 +                       err = 0;
17052 +                       a->skipped = 1;
17053 +                       break;
17054 +               }
17055 +               err = au_fsctx_parse_xino_itrunc_path(fc, &opt->xino_itrunc,
17056 +                                                     param->string);
17057 +               break;
17058 +#endif
17059 +       case Opt_itrunc_xino:
17060 +               if (!a->sb) {
17061 +                       errorfc(fc, "out of bounds %s", param->string);
17062 +                       break;
17063 +               }
17064 +               err = au_fsctx_parse_xino_itrunc(fc, &opt->xino_itrunc,
17065 +                                                result.int_32);
17066 +               break;
17067 +
17068 +       case Opt_dirwh:
17069 +               err = 0;
17070 +               opt->dirwh = result.int_32;
17071 +               break;
17072 +
17073 +       case Opt_rdcache:
17074 +               if (unlikely(result.int_32 > AUFS_RDCACHE_MAX)) {
17075 +                       errorfc(fc, "rdcache must be smaller than %d",
17076 +                               AUFS_RDCACHE_MAX);
17077 +                       break;
17078 +               }
17079 +               err = 0;
17080 +               opt->rdcache = result.int_32;
17081 +               break;
17082 +
17083 +       case Opt_rdblk:
17084 +               err = 0;
17085 +               opt->rdblk = AUFS_RDBLK_DEF;
17086 +               if (!strcmp(param->string, "def"))
17087 +                       break;
17088 +
17089 +               err = kstrtoint(param->string, 0, &result.int_32);
17090 +               if (unlikely(err)) {
17091 +                       errorfc(fc, "bad value in %s", param->key);
17092 +                       break;
17093 +               }
17094 +               err = -EINVAL;
17095 +               if (unlikely(result.int_32 < 0
17096 +                            || result.int_32 > KMALLOC_MAX_SIZE)) {
17097 +                       errorfc(fc, "bad value in %s", param->key);
17098 +                       break;
17099 +               }
17100 +               if (unlikely(result.int_32 && result.int_32 < NAME_MAX)) {
17101 +                       errorfc(fc, "rdblk must be larger than %d", NAME_MAX);
17102 +                       break;
17103 +               }
17104 +               err = 0;
17105 +               opt->rdblk = result.int_32;
17106 +               break;
17107 +
17108 +       case Opt_rdhash:
17109 +               err = 0;
17110 +               opt->rdhash = AUFS_RDHASH_DEF;
17111 +               if (!strcmp(param->string, "def"))
17112 +                       break;
17113 +
17114 +               err = kstrtoint(param->string, 0, &result.int_32);
17115 +               if (unlikely(err)) {
17116 +                       errorfc(fc, "bad value in %s", param->key);
17117 +                       break;
17118 +               }
17119 +               /* how about zero? */
17120 +               if (result.int_32 < 0
17121 +                   || result.int_32 * sizeof(struct hlist_head)
17122 +                   > KMALLOC_MAX_SIZE) {
17123 +                       err = -EINVAL;
17124 +                       errorfc(fc, "bad integer in %s", param->key);
17125 +                       break;
17126 +               }
17127 +               opt->rdhash = result.int_32;
17128 +               break;
17129 +
17130 +       case Opt_diropq:
17131 +               /*
17132 +                * As other options, fs/aufs/opts.c can handle these strings by
17133 +                * match_token().  But "diropq=" is deprecated now and will
17134 +                * never have other value.  So simple strcmp() is enough here.
17135 +                */
17136 +               if (!strcmp(param->string, "a") ||
17137 +                   !strcmp(param->string, "always")) {
17138 +                       err = 0;
17139 +                       opt->type = Opt_diropq_a;
17140 +               } else if (!strcmp(param->string, "w") ||
17141 +                          !strcmp(param->string, "whiteouted")) {
17142 +                       err = 0;
17143 +                       opt->type = Opt_diropq_w;
17144 +               } else
17145 +                       errorfc(fc, "unknown value %s", param->string);
17146 +               break;
17147 +
17148 +       case Opt_udba:
17149 +               opt->udba = au_udba_val(param->string);
17150 +               if (opt->udba >= 0)
17151 +                       err = 0;
17152 +               else
17153 +                       errorf(fc, "wrong value, %s", param->string);
17154 +               break;
17155 +
17156 +       case Opt_wbr_create:
17157 +               opt->wbr_create.wbr_create
17158 +                       = au_wbr_create_val(param->string, &opt->wbr_create);
17159 +               if (opt->wbr_create.wbr_create >= 0)
17160 +                       err = 0;
17161 +               else
17162 +                       errorf(fc, "wrong value, %s", param->key);
17163 +               break;
17164 +
17165 +       case Opt_wbr_copyup:
17166 +               opt->wbr_copyup = au_wbr_copyup_val(param->string);
17167 +               if (opt->wbr_copyup >= 0)
17168 +                       err = 0;
17169 +               else
17170 +                       errorfc(fc, "wrong value, %s", param->key);
17171 +               break;
17172 +
17173 +       case Opt_fhsm_sec:
17174 +               if (unlikely(result.int_32 < 0)) {
17175 +                       errorfc(fc, "bad integer in %s\n", param->key);
17176 +                       break;
17177 +               }
17178 +               err = 0;
17179 +               if (sysaufs_brs)
17180 +                       opt->fhsm_second = result.int_32;
17181 +               else
17182 +                       warnfc(fc, "ignored %s %s", param->key, param->string);
17183 +               break;
17184 +
17185 +       /* simple true/false flag */
17186 +#define au_fsctx_TF(name)                              \
17187 +               case Opt_##name:                        \
17188 +                       err = 0;                        \
17189 +                       opt->tf = !result.negated;      \
17190 +                       break;
17191 +       au_fsctx_TF(trunc_xino);
17192 +       au_fsctx_TF(trunc_xib);
17193 +       au_fsctx_TF(dirperm1);
17194 +       au_fsctx_TF(plink);
17195 +       au_fsctx_TF(shwh);
17196 +       au_fsctx_TF(dio);
17197 +       au_fsctx_TF(warn_perm);
17198 +       au_fsctx_TF(verbose);
17199 +       au_fsctx_TF(sum);
17200 +       au_fsctx_TF(dirren);
17201 +       au_fsctx_TF(acl);
17202 +#undef au_fsctx_TF
17203 +
17204 +       case Opt_noverbose:
17205 +               err = 0;
17206 +               opt->type = Opt_verbose;
17207 +               opt->tf = false;
17208 +               break;
17209 +
17210 +       case Opt_noxino:
17211 +               fallthrough;
17212 +       case Opt_list_plink:
17213 +               fallthrough;
17214 +       case Opt_wsum:
17215 +               err = 0;
17216 +               break;
17217 +
17218 +       case Opt_ignore:
17219 +               warnfc(fc, "ignored %s", param->key);
17220 +               fallthrough;
17221 +       case Opt_ignore_silent:
17222 +               a->skipped = 1;
17223 +               err = 0;
17224 +               break;
17225 +       default:
17226 +               a->skipped = 1;
17227 +               err = -ENOPARAM;
17228 +               break;
17229 +       }
17230 +       if (unlikely(err))
17231 +               goto out;
17232 +       if (a->skipped)
17233 +               goto out;
17234 +
17235 +       switch (token) {
17236 +       case Opt_br:
17237 +               fallthrough;
17238 +       case Opt_noverbose:
17239 +               fallthrough;
17240 +       case Opt_diropq:
17241 +               break;
17242 +       default:
17243 +               opt->type = token;
17244 +               break;
17245 +       }
17246 +       opt++;
17247 +       if (unlikely(opt > a->opt_tail)) {
17248 +               err = -E2BIG;
17249 +               opt--;
17250 +       }
17251 +       opt->type = Opt_tail;
17252 +       a->opt = opt;
17253 +
17254 +out:
17255 +       return err;
17256 +}
17257 +
17258 +/*
17259 + * these options accept both 'name=val' and 'name:val' form.
17260 + * some accept optional '=' in its value.
17261 + * eg. br:/br1=rw:/br2=ro and br=/br1=rw:/br2=ro
17262 + */
17263 +static inline unsigned int is_colonopt(char *str)
17264 +{
17265 +#define do_test(name)                                  \
17266 +       if (!strncmp(str, name ":", sizeof(name)))      \
17267 +               return sizeof(name) - 1;
17268 +       do_test("br");
17269 +       do_test("add");
17270 +       do_test("ins");
17271 +       do_test("append");
17272 +       do_test("prepend");
17273 +       do_test("del");
17274 +       /* do_test("idel"); */
17275 +       do_test("mod");
17276 +       /* do_test("imod"); */
17277 +#undef do_test
17278 +
17279 +       return 0;
17280 +}
17281 +
17282 +static int au_fsctx_parse_monolithic(struct fs_context *fc, void *data)
17283 +{
17284 +       int err;
17285 +       unsigned int u;
17286 +       char *str;
17287 +       struct au_fsctx_opts *a = fc->fs_private;
17288 +
17289 +       str = data;
17290 +       AuDbg("str %s\n", str);
17291 +       while (str) {
17292 +               u = is_colonopt(str);
17293 +               if (u)
17294 +                       str[u] = '=';
17295 +               str = strchr(str, ',');
17296 +               if (!str)
17297 +                       break;
17298 +               str++;
17299 +       }
17300 +       str = data;
17301 +       AuDbg("str %s\n", str);
17302 +
17303 +       err = generic_parse_monolithic(fc, str);
17304 +       AuTraceErr(err);
17305 +       au_fsctx_dump(&a->opts);
17306 +
17307 +       return err;
17308 +}
17309 +
17310 +/* ---------------------------------------------------------------------- */
17311 +
17312 +static void au_fsctx_opts_free(struct au_opts *opts)
17313 +{
17314 +       struct au_opt *opt;
17315 +
17316 +       opt = opts->opt;
17317 +       while (opt->type != Opt_tail) {
17318 +               switch (opt->type) {
17319 +               case Opt_add:
17320 +                       fallthrough;
17321 +               case Opt_append:
17322 +                       fallthrough;
17323 +               case Opt_prepend:
17324 +                       kfree(opt->add.pathname);
17325 +                       path_put(&opt->add.path);
17326 +                       break;
17327 +               case Opt_del:
17328 +                       kfree(opt->del.pathname);
17329 +                       fallthrough;
17330 +               case Opt_idel:
17331 +                       path_put(&opt->del.h_path);
17332 +                       break;
17333 +               case Opt_mod:
17334 +                       kfree(opt->mod.path);
17335 +                       fallthrough;
17336 +               case Opt_imod:
17337 +                       dput(opt->mod.h_root);
17338 +                       break;
17339 +               case Opt_xino:
17340 +                       kfree(opt->xino.path);
17341 +                       fput(opt->xino.file);
17342 +                       break;
17343 +               }
17344 +               opt++;
17345 +       }
17346 +}
17347 +
17348 +static void au_fsctx_free(struct fs_context *fc)
17349 +{
17350 +       struct au_fsctx_opts *a = fc->fs_private;
17351 +
17352 +       /* fs_type=%p, root=%pD */
17353 +       AuDbg("fc %p{sb_flags 0x%x, sb_flags_mask 0x%x, purpose %u\n",
17354 +             fc, fc->sb_flags, fc->sb_flags_mask, fc->purpose);
17355 +
17356 +       kobject_put(&a->sbinfo->si_kobj);
17357 +       au_fsctx_opts_free(&a->opts);
17358 +       free_page((unsigned long)a->opts.opt);
17359 +       au_kfree_rcu(a);
17360 +}
17361 +
17362 +static const struct fs_context_operations au_fsctx_ops = {
17363 +       .free                   = au_fsctx_free,
17364 +       .parse_param            = au_fsctx_parse_param,
17365 +       .parse_monolithic       = au_fsctx_parse_monolithic,
17366 +       .get_tree               = au_fsctx_get_tree,
17367 +       .reconfigure            = au_fsctx_reconfigure
17368 +       /*
17369 +        * nfs4 requires ->dup()? No.
17370 +        * I don't know what is this ->dup() for.
17371 +        */
17372 +};
17373 +
17374 +int aufs_fsctx_init(struct fs_context *fc)
17375 +{
17376 +       int err;
17377 +       struct au_fsctx_opts *a;
17378 +
17379 +       /* fs_type=%p, root=%pD */
17380 +       AuDbg("fc %p{sb_flags 0x%x, sb_flags_mask 0x%x, purpose %u\n",
17381 +             fc, fc->sb_flags, fc->sb_flags_mask, fc->purpose);
17382 +
17383 +       /* they will be freed by au_fsctx_free() */
17384 +       err = -ENOMEM;
17385 +       a = kzalloc(sizeof(*a), GFP_NOFS);
17386 +       if (unlikely(!a))
17387 +               goto out;
17388 +       a->bindex = 0;
17389 +       a->opts.opt = (void *)__get_free_page(GFP_NOFS);
17390 +       if (unlikely(!a->opts.opt))
17391 +               goto out_a;
17392 +       a->opt = a->opts.opt;
17393 +       a->opt->type = Opt_tail;
17394 +       a->opts.max_opt = PAGE_SIZE / sizeof(*a->opts.opt);
17395 +       a->opt_tail = a->opt + a->opts.max_opt - 1;
17396 +       a->opts.sb_flags = fc->sb_flags;
17397 +
17398 +       a->sb = NULL;
17399 +       if (fc->root) {
17400 +               AuDebugOn(fc->purpose != FS_CONTEXT_FOR_RECONFIGURE);
17401 +               a->opts.flags = AuOpts_REMOUNT;
17402 +               a->sb = fc->root->d_sb;
17403 +               a->sbinfo = au_sbi(a->sb);
17404 +               kobject_get(&a->sbinfo->si_kobj);
17405 +       } else {
17406 +               a->sbinfo = au_si_alloc(a->sb);
17407 +               AuDebugOn(!a->sbinfo);
17408 +               err = PTR_ERR(a->sbinfo);
17409 +               if (IS_ERR(a->sbinfo))
17410 +                       goto out_opt;
17411 +               au_rw_write_unlock(&a->sbinfo->si_rwsem);
17412 +       }
17413 +
17414 +       err = 0;
17415 +       fc->fs_private = a;
17416 +       fc->ops = &au_fsctx_ops;
17417 +       goto out; /* success */
17418 +
17419 +out_opt:
17420 +       free_page((unsigned long)a->opts.opt);
17421 +out_a:
17422 +       au_kfree_rcu(a);
17423 +out:
17424 +       AuTraceErr(err);
17425 +       return err;
17426 +}
17427 diff -urN /usr/share/empty/fs/aufs/fstype.h linux/fs/aufs/fstype.h
17428 --- /usr/share/empty/fs/aufs/fstype.h   1970-01-01 01:00:00.000000000 +0100
17429 +++ linux/fs/aufs/fstype.h      2022-03-21 14:49:05.723299677 +0100
17430 @@ -0,0 +1,401 @@
17431 +/* SPDX-License-Identifier: GPL-2.0 */
17432 +/*
17433 + * Copyright (C) 2005-2021 Junjiro R. Okajima
17434 + *
17435 + * This program, aufs is free software; you can redistribute it and/or modify
17436 + * it under the terms of the GNU General Public License as published by
17437 + * the Free Software Foundation; either version 2 of the License, or
17438 + * (at your option) any later version.
17439 + *
17440 + * This program is distributed in the hope that it will be useful,
17441 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
17442 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17443 + * GNU General Public License for more details.
17444 + *
17445 + * You should have received a copy of the GNU General Public License
17446 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17447 + */
17448 +
17449 +/*
17450 + * judging filesystem type
17451 + */
17452 +
17453 +#ifndef __AUFS_FSTYPE_H__
17454 +#define __AUFS_FSTYPE_H__
17455 +
17456 +#ifdef __KERNEL__
17457 +
17458 +#include <linux/fs.h>
17459 +#include <linux/magic.h>
17460 +#include <linux/nfs_fs.h>
17461 +#include <linux/romfs_fs.h>
17462 +
17463 +static inline int au_test_aufs(struct super_block *sb)
17464 +{
17465 +       return sb->s_magic == AUFS_SUPER_MAGIC;
17466 +}
17467 +
17468 +static inline const char *au_sbtype(struct super_block *sb)
17469 +{
17470 +       return sb->s_type->name;
17471 +}
17472 +
17473 +static inline int au_test_iso9660(struct super_block *sb __maybe_unused)
17474 +{
17475 +#if IS_ENABLED(CONFIG_ISO9660_FS)
17476 +       return sb->s_magic == ISOFS_SUPER_MAGIC;
17477 +#else
17478 +       return 0;
17479 +#endif
17480 +}
17481 +
17482 +static inline int au_test_romfs(struct super_block *sb __maybe_unused)
17483 +{
17484 +#if IS_ENABLED(CONFIG_ROMFS_FS)
17485 +       return sb->s_magic == ROMFS_MAGIC;
17486 +#else
17487 +       return 0;
17488 +#endif
17489 +}
17490 +
17491 +static inline int au_test_cramfs(struct super_block *sb __maybe_unused)
17492 +{
17493 +#if IS_ENABLED(CONFIG_CRAMFS)
17494 +       return sb->s_magic == CRAMFS_MAGIC;
17495 +#endif
17496 +       return 0;
17497 +}
17498 +
17499 +static inline int au_test_nfs(struct super_block *sb __maybe_unused)
17500 +{
17501 +#if IS_ENABLED(CONFIG_NFS_FS)
17502 +       return sb->s_magic == NFS_SUPER_MAGIC;
17503 +#else
17504 +       return 0;
17505 +#endif
17506 +}
17507 +
17508 +static inline int au_test_fuse(struct super_block *sb __maybe_unused)
17509 +{
17510 +#if IS_ENABLED(CONFIG_FUSE_FS)
17511 +       return sb->s_magic == FUSE_SUPER_MAGIC;
17512 +#else
17513 +       return 0;
17514 +#endif
17515 +}
17516 +
17517 +static inline int au_test_xfs(struct super_block *sb __maybe_unused)
17518 +{
17519 +#if IS_ENABLED(CONFIG_XFS_FS)
17520 +       return sb->s_magic == XFS_SB_MAGIC;
17521 +#else
17522 +       return 0;
17523 +#endif
17524 +}
17525 +
17526 +static inline int au_test_tmpfs(struct super_block *sb __maybe_unused)
17527 +{
17528 +#ifdef CONFIG_TMPFS
17529 +       return sb->s_magic == TMPFS_MAGIC;
17530 +#else
17531 +       return 0;
17532 +#endif
17533 +}
17534 +
17535 +static inline int au_test_ecryptfs(struct super_block *sb __maybe_unused)
17536 +{
17537 +#if IS_ENABLED(CONFIG_ECRYPT_FS)
17538 +       return !strcmp(au_sbtype(sb), "ecryptfs");
17539 +#else
17540 +       return 0;
17541 +#endif
17542 +}
17543 +
17544 +static inline int au_test_ramfs(struct super_block *sb)
17545 +{
17546 +       return sb->s_magic == RAMFS_MAGIC;
17547 +}
17548 +
17549 +static inline int au_test_ubifs(struct super_block *sb __maybe_unused)
17550 +{
17551 +#if IS_ENABLED(CONFIG_UBIFS_FS)
17552 +       return sb->s_magic == UBIFS_SUPER_MAGIC;
17553 +#else
17554 +       return 0;
17555 +#endif
17556 +}
17557 +
17558 +static inline int au_test_procfs(struct super_block *sb __maybe_unused)
17559 +{
17560 +#ifdef CONFIG_PROC_FS
17561 +       return sb->s_magic == PROC_SUPER_MAGIC;
17562 +#else
17563 +       return 0;
17564 +#endif
17565 +}
17566 +
17567 +static inline int au_test_sysfs(struct super_block *sb __maybe_unused)
17568 +{
17569 +#ifdef CONFIG_SYSFS
17570 +       return sb->s_magic == SYSFS_MAGIC;
17571 +#else
17572 +       return 0;
17573 +#endif
17574 +}
17575 +
17576 +static inline int au_test_configfs(struct super_block *sb __maybe_unused)
17577 +{
17578 +#if IS_ENABLED(CONFIG_CONFIGFS_FS)
17579 +       return sb->s_magic == CONFIGFS_MAGIC;
17580 +#else
17581 +       return 0;
17582 +#endif
17583 +}
17584 +
17585 +static inline int au_test_minix(struct super_block *sb __maybe_unused)
17586 +{
17587 +#if IS_ENABLED(CONFIG_MINIX_FS)
17588 +       return sb->s_magic == MINIX3_SUPER_MAGIC
17589 +               || sb->s_magic == MINIX2_SUPER_MAGIC
17590 +               || sb->s_magic == MINIX2_SUPER_MAGIC2
17591 +               || sb->s_magic == MINIX_SUPER_MAGIC
17592 +               || sb->s_magic == MINIX_SUPER_MAGIC2;
17593 +#else
17594 +       return 0;
17595 +#endif
17596 +}
17597 +
17598 +static inline int au_test_fat(struct super_block *sb __maybe_unused)
17599 +{
17600 +#if IS_ENABLED(CONFIG_FAT_FS)
17601 +       return sb->s_magic == MSDOS_SUPER_MAGIC;
17602 +#else
17603 +       return 0;
17604 +#endif
17605 +}
17606 +
17607 +static inline int au_test_msdos(struct super_block *sb)
17608 +{
17609 +       return au_test_fat(sb);
17610 +}
17611 +
17612 +static inline int au_test_vfat(struct super_block *sb)
17613 +{
17614 +       return au_test_fat(sb);
17615 +}
17616 +
17617 +static inline int au_test_securityfs(struct super_block *sb __maybe_unused)
17618 +{
17619 +#ifdef CONFIG_SECURITYFS
17620 +       return sb->s_magic == SECURITYFS_MAGIC;
17621 +#else
17622 +       return 0;
17623 +#endif
17624 +}
17625 +
17626 +static inline int au_test_squashfs(struct super_block *sb __maybe_unused)
17627 +{
17628 +#if IS_ENABLED(CONFIG_SQUASHFS)
17629 +       return sb->s_magic == SQUASHFS_MAGIC;
17630 +#else
17631 +       return 0;
17632 +#endif
17633 +}
17634 +
17635 +static inline int au_test_btrfs(struct super_block *sb __maybe_unused)
17636 +{
17637 +#if IS_ENABLED(CONFIG_BTRFS_FS)
17638 +       return sb->s_magic == BTRFS_SUPER_MAGIC;
17639 +#else
17640 +       return 0;
17641 +#endif
17642 +}
17643 +
17644 +static inline int au_test_xenfs(struct super_block *sb __maybe_unused)
17645 +{
17646 +#if IS_ENABLED(CONFIG_XENFS)
17647 +       return sb->s_magic == XENFS_SUPER_MAGIC;
17648 +#else
17649 +       return 0;
17650 +#endif
17651 +}
17652 +
17653 +static inline int au_test_debugfs(struct super_block *sb __maybe_unused)
17654 +{
17655 +#ifdef CONFIG_DEBUG_FS
17656 +       return sb->s_magic == DEBUGFS_MAGIC;
17657 +#else
17658 +       return 0;
17659 +#endif
17660 +}
17661 +
17662 +static inline int au_test_nilfs(struct super_block *sb __maybe_unused)
17663 +{
17664 +#if IS_ENABLED(CONFIG_NILFS)
17665 +       return sb->s_magic == NILFS_SUPER_MAGIC;
17666 +#else
17667 +       return 0;
17668 +#endif
17669 +}
17670 +
17671 +static inline int au_test_hfsplus(struct super_block *sb __maybe_unused)
17672 +{
17673 +#if IS_ENABLED(CONFIG_HFSPLUS_FS)
17674 +       return sb->s_magic == HFSPLUS_SUPER_MAGIC;
17675 +#else
17676 +       return 0;
17677 +#endif
17678 +}
17679 +
17680 +/* ---------------------------------------------------------------------- */
17681 +/*
17682 + * they can't be an aufs branch.
17683 + */
17684 +static inline int au_test_fs_unsuppoted(struct super_block *sb)
17685 +{
17686 +       return
17687 +#ifndef CONFIG_AUFS_BR_RAMFS
17688 +               au_test_ramfs(sb) ||
17689 +#endif
17690 +               au_test_procfs(sb)
17691 +               || au_test_sysfs(sb)
17692 +               || au_test_configfs(sb)
17693 +               || au_test_debugfs(sb)
17694 +               || au_test_securityfs(sb)
17695 +               || au_test_xenfs(sb)
17696 +               || au_test_ecryptfs(sb)
17697 +               /* || !strcmp(au_sbtype(sb), "unionfs") */
17698 +               || au_test_aufs(sb); /* will be supported in next version */
17699 +}
17700 +
17701 +static inline int au_test_fs_remote(struct super_block *sb)
17702 +{
17703 +       return !au_test_tmpfs(sb)
17704 +#ifdef CONFIG_AUFS_BR_RAMFS
17705 +               && !au_test_ramfs(sb)
17706 +#endif
17707 +               && !(sb->s_type->fs_flags & FS_REQUIRES_DEV);
17708 +}
17709 +
17710 +/* ---------------------------------------------------------------------- */
17711 +
17712 +/*
17713 + * Note: these functions (below) are created after reading ->getattr() in all
17714 + * filesystems under linux/fs. it means we have to do so in every update...
17715 + */
17716 +
17717 +/*
17718 + * some filesystems require getattr to refresh the inode attributes before
17719 + * referencing.
17720 + * in most cases, we can rely on the inode attribute in NFS (or every remote fs)
17721 + * and leave the work for d_revalidate()
17722 + */
17723 +static inline int au_test_fs_refresh_iattr(struct super_block *sb)
17724 +{
17725 +       return au_test_nfs(sb)
17726 +               || au_test_fuse(sb)
17727 +               /* || au_test_btrfs(sb) */      /* untested */
17728 +               ;
17729 +}
17730 +
17731 +/*
17732 + * filesystems which don't maintain i_size or i_blocks.
17733 + */
17734 +static inline int au_test_fs_bad_iattr_size(struct super_block *sb)
17735 +{
17736 +       return au_test_xfs(sb)
17737 +               || au_test_btrfs(sb)
17738 +               || au_test_ubifs(sb)
17739 +               || au_test_hfsplus(sb)  /* maintained, but incorrect */
17740 +               /* || au_test_minix(sb) */      /* untested */
17741 +               ;
17742 +}
17743 +
17744 +/*
17745 + * filesystems which don't store the correct value in some of their inode
17746 + * attributes.
17747 + */
17748 +static inline int au_test_fs_bad_iattr(struct super_block *sb)
17749 +{
17750 +       return au_test_fs_bad_iattr_size(sb)
17751 +               || au_test_fat(sb)
17752 +               || au_test_msdos(sb)
17753 +               || au_test_vfat(sb);
17754 +}
17755 +
17756 +/* they don't check i_nlink in link(2) */
17757 +static inline int au_test_fs_no_limit_nlink(struct super_block *sb)
17758 +{
17759 +       return au_test_tmpfs(sb)
17760 +#ifdef CONFIG_AUFS_BR_RAMFS
17761 +               || au_test_ramfs(sb)
17762 +#endif
17763 +               || au_test_ubifs(sb)
17764 +               || au_test_hfsplus(sb);
17765 +}
17766 +
17767 +/*
17768 + * filesystems which sets S_NOATIME and S_NOCMTIME.
17769 + */
17770 +static inline int au_test_fs_notime(struct super_block *sb)
17771 +{
17772 +       return au_test_nfs(sb)
17773 +               || au_test_fuse(sb)
17774 +               || au_test_ubifs(sb)
17775 +               ;
17776 +}
17777 +
17778 +/* temporary support for i#1 in cramfs */
17779 +static inline int au_test_fs_unique_ino(struct inode *inode)
17780 +{
17781 +       if (au_test_cramfs(inode->i_sb))
17782 +               return inode->i_ino != 1;
17783 +       return 1;
17784 +}
17785 +
17786 +/* ---------------------------------------------------------------------- */
17787 +
17788 +/*
17789 + * the filesystem where the xino files placed must support i/o after unlink and
17790 + * maintain i_size and i_blocks.
17791 + */
17792 +static inline int au_test_fs_bad_xino(struct super_block *sb)
17793 +{
17794 +       return au_test_fs_remote(sb)
17795 +               || au_test_fs_bad_iattr_size(sb)
17796 +               /* don't want unnecessary work for xino */
17797 +               || au_test_aufs(sb)
17798 +               || au_test_ecryptfs(sb)
17799 +               || au_test_nilfs(sb);
17800 +}
17801 +
17802 +static inline int au_test_fs_trunc_xino(struct super_block *sb)
17803 +{
17804 +       return au_test_tmpfs(sb)
17805 +               || au_test_ramfs(sb);
17806 +}
17807 +
17808 +/*
17809 + * test if the @sb is real-readonly.
17810 + */
17811 +static inline int au_test_fs_rr(struct super_block *sb)
17812 +{
17813 +       return au_test_squashfs(sb)
17814 +               || au_test_iso9660(sb)
17815 +               || au_test_cramfs(sb)
17816 +               || au_test_romfs(sb);
17817 +}
17818 +
17819 +/*
17820 + * test if the @inode is nfs with 'noacl' option
17821 + * NFS always sets SB_POSIXACL regardless its mount option 'noacl.'
17822 + */
17823 +static inline int au_test_nfs_noacl(struct inode *inode)
17824 +{
17825 +       return au_test_nfs(inode->i_sb)
17826 +               /* && IS_POSIXACL(inode) */
17827 +               && !nfs_server_capable(inode, NFS_CAP_ACLS);
17828 +}
17829 +
17830 +#endif /* __KERNEL__ */
17831 +#endif /* __AUFS_FSTYPE_H__ */
17832 diff -urN /usr/share/empty/fs/aufs/hbl.h linux/fs/aufs/hbl.h
17833 --- /usr/share/empty/fs/aufs/hbl.h      1970-01-01 01:00:00.000000000 +0100
17834 +++ linux/fs/aufs/hbl.h 2022-03-21 14:49:05.723299677 +0100
17835 @@ -0,0 +1,65 @@
17836 +/* SPDX-License-Identifier: GPL-2.0 */
17837 +/*
17838 + * Copyright (C) 2017-2021 Junjiro R. Okajima
17839 + *
17840 + * This program, aufs is free software; you can redistribute it and/or modify
17841 + * it under the terms of the GNU General Public License as published by
17842 + * the Free Software Foundation; either version 2 of the License, or
17843 + * (at your option) any later version.
17844 + *
17845 + * This program is distributed in the hope that it will be useful,
17846 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
17847 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17848 + * GNU General Public License for more details.
17849 + *
17850 + * You should have received a copy of the GNU General Public License
17851 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17852 + */
17853 +
17854 +/*
17855 + * helpers for hlist_bl.h
17856 + */
17857 +
17858 +#ifndef __AUFS_HBL_H__
17859 +#define __AUFS_HBL_H__
17860 +
17861 +#ifdef __KERNEL__
17862 +
17863 +#include <linux/list_bl.h>
17864 +
17865 +static inline void au_hbl_add(struct hlist_bl_node *node,
17866 +                             struct hlist_bl_head *hbl)
17867 +{
17868 +       hlist_bl_lock(hbl);
17869 +       hlist_bl_add_head(node, hbl);
17870 +       hlist_bl_unlock(hbl);
17871 +}
17872 +
17873 +static inline void au_hbl_del(struct hlist_bl_node *node,
17874 +                             struct hlist_bl_head *hbl)
17875 +{
17876 +       hlist_bl_lock(hbl);
17877 +       hlist_bl_del(node);
17878 +       hlist_bl_unlock(hbl);
17879 +}
17880 +
17881 +#define au_hbl_for_each(pos, head)                                     \
17882 +       for (pos = hlist_bl_first(head);                                \
17883 +            pos;                                                       \
17884 +            pos = pos->next)
17885 +
17886 +static inline unsigned long au_hbl_count(struct hlist_bl_head *hbl)
17887 +{
17888 +       unsigned long cnt;
17889 +       struct hlist_bl_node *pos;
17890 +
17891 +       cnt = 0;
17892 +       hlist_bl_lock(hbl);
17893 +       au_hbl_for_each(pos, hbl)
17894 +               cnt++;
17895 +       hlist_bl_unlock(hbl);
17896 +       return cnt;
17897 +}
17898 +
17899 +#endif /* __KERNEL__ */
17900 +#endif /* __AUFS_HBL_H__ */
17901 diff -urN /usr/share/empty/fs/aufs/hfsnotify.c linux/fs/aufs/hfsnotify.c
17902 --- /usr/share/empty/fs/aufs/hfsnotify.c        1970-01-01 01:00:00.000000000 +0100
17903 +++ linux/fs/aufs/hfsnotify.c   2022-03-21 14:49:05.723299677 +0100
17904 @@ -0,0 +1,289 @@
17905 +// SPDX-License-Identifier: GPL-2.0
17906 +/*
17907 + * Copyright (C) 2005-2021 Junjiro R. Okajima
17908 + *
17909 + * This program, aufs is free software; you can redistribute it and/or modify
17910 + * it under the terms of the GNU General Public License as published by
17911 + * the Free Software Foundation; either version 2 of the License, or
17912 + * (at your option) any later version.
17913 + *
17914 + * This program is distributed in the hope that it will be useful,
17915 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
17916 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17917 + * GNU General Public License for more details.
17918 + *
17919 + * You should have received a copy of the GNU General Public License
17920 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17921 + */
17922 +
17923 +/*
17924 + * fsnotify for the lower directories
17925 + */
17926 +
17927 +#include "aufs.h"
17928 +
17929 +/* FS_IN_IGNORED is unnecessary */
17930 +static const __u32 AuHfsnMask = (FS_MOVED_TO | FS_MOVED_FROM | FS_DELETE
17931 +                                | FS_CREATE | FS_EVENT_ON_CHILD);
17932 +static DECLARE_WAIT_QUEUE_HEAD(au_hfsn_wq);
17933 +static __cacheline_aligned_in_smp atomic64_t au_hfsn_ifree = ATOMIC64_INIT(0);
17934 +
17935 +static void au_hfsn_free_mark(struct fsnotify_mark *mark)
17936 +{
17937 +       struct au_hnotify *hn = container_of(mark, struct au_hnotify,
17938 +                                            hn_mark);
17939 +       /* AuDbg("here\n"); */
17940 +       au_cache_free_hnotify(hn);
17941 +       smp_mb__before_atomic(); /* for atomic64_dec */
17942 +       if (atomic64_dec_and_test(&au_hfsn_ifree))
17943 +               wake_up(&au_hfsn_wq);
17944 +}
17945 +
17946 +static int au_hfsn_alloc(struct au_hinode *hinode)
17947 +{
17948 +       int err;
17949 +       struct au_hnotify *hn;
17950 +       struct super_block *sb;
17951 +       struct au_branch *br;
17952 +       struct fsnotify_mark *mark;
17953 +       aufs_bindex_t bindex;
17954 +
17955 +       hn = hinode->hi_notify;
17956 +       sb = hn->hn_aufs_inode->i_sb;
17957 +       bindex = au_br_index(sb, hinode->hi_id);
17958 +       br = au_sbr(sb, bindex);
17959 +       AuDebugOn(!br->br_hfsn);
17960 +
17961 +       mark = &hn->hn_mark;
17962 +       fsnotify_init_mark(mark, br->br_hfsn->hfsn_group);
17963 +       mark->mask = AuHfsnMask;
17964 +       /*
17965 +        * by udba rename or rmdir, aufs assign a new inode to the known
17966 +        * h_inode, so specify 1 to allow dups.
17967 +        */
17968 +       lockdep_off();
17969 +       err = fsnotify_add_inode_mark(mark, hinode->hi_inode, /*allow_dups*/1);
17970 +       lockdep_on();
17971 +
17972 +       return err;
17973 +}
17974 +
17975 +static int au_hfsn_free(struct au_hinode *hinode, struct au_hnotify *hn)
17976 +{
17977 +       struct fsnotify_mark *mark;
17978 +       unsigned long long ull;
17979 +       struct fsnotify_group *group;
17980 +
17981 +       ull = atomic64_inc_return(&au_hfsn_ifree);
17982 +       BUG_ON(!ull);
17983 +
17984 +       mark = &hn->hn_mark;
17985 +       spin_lock(&mark->lock);
17986 +       group = mark->group;
17987 +       fsnotify_get_group(group);
17988 +       spin_unlock(&mark->lock);
17989 +       lockdep_off();
17990 +       fsnotify_destroy_mark(mark, group);
17991 +       fsnotify_put_mark(mark);
17992 +       fsnotify_put_group(group);
17993 +       lockdep_on();
17994 +
17995 +       /* free hn by myself */
17996 +       return 0;
17997 +}
17998 +
17999 +/* ---------------------------------------------------------------------- */
18000 +
18001 +static void au_hfsn_ctl(struct au_hinode *hinode, int do_set)
18002 +{
18003 +       struct fsnotify_mark *mark;
18004 +
18005 +       mark = &hinode->hi_notify->hn_mark;
18006 +       spin_lock(&mark->lock);
18007 +       if (do_set) {
18008 +               AuDebugOn(mark->mask & AuHfsnMask);
18009 +               mark->mask |= AuHfsnMask;
18010 +       } else {
18011 +               AuDebugOn(!(mark->mask & AuHfsnMask));
18012 +               mark->mask &= ~AuHfsnMask;
18013 +       }
18014 +       spin_unlock(&mark->lock);
18015 +       /* fsnotify_recalc_inode_mask(hinode->hi_inode); */
18016 +}
18017 +
18018 +/* ---------------------------------------------------------------------- */
18019 +
18020 +/* #define AuDbgHnotify */
18021 +#ifdef AuDbgHnotify
18022 +static char *au_hfsn_name(u32 mask)
18023 +{
18024 +#ifdef CONFIG_AUFS_DEBUG
18025 +#define test_ret(flag)                         \
18026 +       do {                                    \
18027 +               if (mask & flag)                \
18028 +                       return #flag;           \
18029 +       } while (0)
18030 +       test_ret(FS_ACCESS);
18031 +       test_ret(FS_MODIFY);
18032 +       test_ret(FS_ATTRIB);
18033 +       test_ret(FS_CLOSE_WRITE);
18034 +       test_ret(FS_CLOSE_NOWRITE);
18035 +       test_ret(FS_OPEN);
18036 +       test_ret(FS_MOVED_FROM);
18037 +       test_ret(FS_MOVED_TO);
18038 +       test_ret(FS_CREATE);
18039 +       test_ret(FS_DELETE);
18040 +       test_ret(FS_DELETE_SELF);
18041 +       test_ret(FS_MOVE_SELF);
18042 +       test_ret(FS_UNMOUNT);
18043 +       test_ret(FS_Q_OVERFLOW);
18044 +       test_ret(FS_IN_IGNORED);
18045 +       test_ret(FS_ISDIR);
18046 +       test_ret(FS_IN_ONESHOT);
18047 +       test_ret(FS_EVENT_ON_CHILD);
18048 +       return "";
18049 +#undef test_ret
18050 +#else
18051 +       return "??";
18052 +#endif
18053 +}
18054 +#endif
18055 +
18056 +/* ---------------------------------------------------------------------- */
18057 +
18058 +static void au_hfsn_free_group(struct fsnotify_group *group)
18059 +{
18060 +       struct au_br_hfsnotify *hfsn = group->private;
18061 +
18062 +       /* AuDbg("here\n"); */
18063 +       au_kfree_try_rcu(hfsn);
18064 +}
18065 +
18066 +static int au_hfsn_handle_event(struct fsnotify_group *group,
18067 +                               u32 mask, const void *data, int data_type,
18068 +                               struct inode *dir,
18069 +                               const struct qstr *file_name, u32 cookie,
18070 +                               struct fsnotify_iter_info *iter_info)
18071 +{
18072 +       int err;
18073 +       struct au_hnotify *hnotify;
18074 +       struct inode *h_dir, *h_inode;
18075 +       struct fsnotify_mark *inode_mark;
18076 +
18077 +       AuDebugOn(!(data_type == FSNOTIFY_EVENT_INODE
18078 +                   || data_type == FSNOTIFY_EVENT_DENTRY));
18079 +
18080 +       err = 0;
18081 +       /* if FS_UNMOUNT happens, there must be another bug */
18082 +       AuDebugOn(mask & FS_UNMOUNT);
18083 +       if (mask & (FS_IN_IGNORED | FS_UNMOUNT))
18084 +               goto out;
18085 +
18086 +       h_dir = dir;
18087 +       h_inode = NULL;
18088 +#ifdef AuDbgHnotify
18089 +       au_debug_on();
18090 +       if (1 || h_child_qstr.len != sizeof(AUFS_XINO_FNAME) - 1
18091 +           || strncmp(h_child_qstr.name, AUFS_XINO_FNAME, h_child_qstr.len)) {
18092 +               AuDbg("i%lu, mask 0x%x %s, hcname %.*s, hi%lu\n",
18093 +                     h_dir->i_ino, mask, au_hfsn_name(mask),
18094 +                     AuLNPair(&h_child_qstr), h_inode ? h_inode->i_ino : 0);
18095 +               /* WARN_ON(1); */
18096 +       }
18097 +       au_debug_off();
18098 +#endif
18099 +
18100 +       inode_mark = fsnotify_iter_inode_mark(iter_info);
18101 +       AuDebugOn(!inode_mark);
18102 +       hnotify = container_of(inode_mark, struct au_hnotify, hn_mark);
18103 +       err = au_hnotify(h_dir, hnotify, mask, file_name, h_inode);
18104 +
18105 +out:
18106 +       return err;
18107 +}
18108 +
18109 +static struct fsnotify_ops au_hfsn_ops = {
18110 +       .handle_event           = au_hfsn_handle_event,
18111 +       .free_group_priv        = au_hfsn_free_group,
18112 +       .free_mark              = au_hfsn_free_mark
18113 +};
18114 +
18115 +/* ---------------------------------------------------------------------- */
18116 +
18117 +static void au_hfsn_fin_br(struct au_branch *br)
18118 +{
18119 +       struct au_br_hfsnotify *hfsn;
18120 +
18121 +       hfsn = br->br_hfsn;
18122 +       if (hfsn) {
18123 +               lockdep_off();
18124 +               fsnotify_put_group(hfsn->hfsn_group);
18125 +               lockdep_on();
18126 +       }
18127 +}
18128 +
18129 +static int au_hfsn_init_br(struct au_branch *br, int perm)
18130 +{
18131 +       int err;
18132 +       struct fsnotify_group *group;
18133 +       struct au_br_hfsnotify *hfsn;
18134 +
18135 +       err = 0;
18136 +       br->br_hfsn = NULL;
18137 +       if (!au_br_hnotifyable(perm))
18138 +               goto out;
18139 +
18140 +       err = -ENOMEM;
18141 +       hfsn = kmalloc(sizeof(*hfsn), GFP_NOFS);
18142 +       if (unlikely(!hfsn))
18143 +               goto out;
18144 +
18145 +       err = 0;
18146 +       group = fsnotify_alloc_group(&au_hfsn_ops);
18147 +       if (IS_ERR(group)) {
18148 +               err = PTR_ERR(group);
18149 +               pr_err("fsnotify_alloc_group() failed, %d\n", err);
18150 +               goto out_hfsn;
18151 +       }
18152 +
18153 +       group->private = hfsn;
18154 +       hfsn->hfsn_group = group;
18155 +       br->br_hfsn = hfsn;
18156 +       goto out; /* success */
18157 +
18158 +out_hfsn:
18159 +       au_kfree_try_rcu(hfsn);
18160 +out:
18161 +       return err;
18162 +}
18163 +
18164 +static int au_hfsn_reset_br(unsigned int udba, struct au_branch *br, int perm)
18165 +{
18166 +       int err;
18167 +
18168 +       err = 0;
18169 +       if (!br->br_hfsn)
18170 +               err = au_hfsn_init_br(br, perm);
18171 +
18172 +       return err;
18173 +}
18174 +
18175 +/* ---------------------------------------------------------------------- */
18176 +
18177 +static void au_hfsn_fin(void)
18178 +{
18179 +       AuDbg("au_hfsn_ifree %lld\n", (long long)atomic64_read(&au_hfsn_ifree));
18180 +       wait_event(au_hfsn_wq, !atomic64_read(&au_hfsn_ifree));
18181 +}
18182 +
18183 +const struct au_hnotify_op au_hnotify_op = {
18184 +       .ctl            = au_hfsn_ctl,
18185 +       .alloc          = au_hfsn_alloc,
18186 +       .free           = au_hfsn_free,
18187 +
18188 +       .fin            = au_hfsn_fin,
18189 +
18190 +       .reset_br       = au_hfsn_reset_br,
18191 +       .fin_br         = au_hfsn_fin_br,
18192 +       .init_br        = au_hfsn_init_br
18193 +};
18194 diff -urN /usr/share/empty/fs/aufs/hfsplus.c linux/fs/aufs/hfsplus.c
18195 --- /usr/share/empty/fs/aufs/hfsplus.c  1970-01-01 01:00:00.000000000 +0100
18196 +++ linux/fs/aufs/hfsplus.c     2022-03-21 14:49:05.723299677 +0100
18197 @@ -0,0 +1,60 @@
18198 +// SPDX-License-Identifier: GPL-2.0
18199 +/*
18200 + * Copyright (C) 2010-2021 Junjiro R. Okajima
18201 + *
18202 + * This program, aufs is free software; you can redistribute it and/or modify
18203 + * it under the terms of the GNU General Public License as published by
18204 + * the Free Software Foundation; either version 2 of the License, or
18205 + * (at your option) any later version.
18206 + *
18207 + * This program is distributed in the hope that it will be useful,
18208 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
18209 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18210 + * GNU General Public License for more details.
18211 + *
18212 + * You should have received a copy of the GNU General Public License
18213 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18214 + */
18215 +
18216 +/*
18217 + * special support for filesystems which acquires an inode mutex
18218 + * at final closing a file, eg, hfsplus.
18219 + *
18220 + * This trick is very simple and stupid, just to open the file before really
18221 + * necessary open to tell hfsplus that this is not the final closing.
18222 + * The caller should call au_h_open_pre() after acquiring the inode mutex,
18223 + * and au_h_open_post() after releasing it.
18224 + */
18225 +
18226 +#include "aufs.h"
18227 +
18228 +struct file *au_h_open_pre(struct dentry *dentry, aufs_bindex_t bindex,
18229 +                          int force_wr)
18230 +{
18231 +       struct file *h_file;
18232 +       struct dentry *h_dentry;
18233 +
18234 +       h_dentry = au_h_dptr(dentry, bindex);
18235 +       AuDebugOn(!h_dentry);
18236 +       AuDebugOn(d_is_negative(h_dentry));
18237 +
18238 +       h_file = NULL;
18239 +       if (au_test_hfsplus(h_dentry->d_sb)
18240 +           && d_is_reg(h_dentry))
18241 +               h_file = au_h_open(dentry, bindex,
18242 +                                  O_RDONLY | O_NOATIME | O_LARGEFILE,
18243 +                                  /*file*/NULL, force_wr);
18244 +       return h_file;
18245 +}
18246 +
18247 +void au_h_open_post(struct dentry *dentry, aufs_bindex_t bindex,
18248 +                   struct file *h_file)
18249 +{
18250 +       struct au_branch *br;
18251 +
18252 +       if (h_file) {
18253 +               fput(h_file);
18254 +               br = au_sbr(dentry->d_sb, bindex);
18255 +               au_lcnt_dec(&br->br_nfiles);
18256 +       }
18257 +}
18258 diff -urN /usr/share/empty/fs/aufs/hnotify.c linux/fs/aufs/hnotify.c
18259 --- /usr/share/empty/fs/aufs/hnotify.c  1970-01-01 01:00:00.000000000 +0100
18260 +++ linux/fs/aufs/hnotify.c     2022-03-21 14:49:05.723299677 +0100
18261 @@ -0,0 +1,715 @@
18262 +// SPDX-License-Identifier: GPL-2.0
18263 +/*
18264 + * Copyright (C) 2005-2021 Junjiro R. Okajima
18265 + *
18266 + * This program, aufs is free software; you can redistribute it and/or modify
18267 + * it under the terms of the GNU General Public License as published by
18268 + * the Free Software Foundation; either version 2 of the License, or
18269 + * (at your option) any later version.
18270 + *
18271 + * This program is distributed in the hope that it will be useful,
18272 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
18273 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18274 + * GNU General Public License for more details.
18275 + *
18276 + * You should have received a copy of the GNU General Public License
18277 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18278 + */
18279 +
18280 +/*
18281 + * abstraction to notify the direct changes on lower directories
18282 + */
18283 +
18284 +/* #include <linux/iversion.h> */
18285 +#include "aufs.h"
18286 +
18287 +int au_hn_alloc(struct au_hinode *hinode, struct inode *inode)
18288 +{
18289 +       int err;
18290 +       struct au_hnotify *hn;
18291 +
18292 +       err = -ENOMEM;
18293 +       hn = au_cache_alloc_hnotify();
18294 +       if (hn) {
18295 +               hn->hn_aufs_inode = inode;
18296 +               hinode->hi_notify = hn;
18297 +               err = au_hnotify_op.alloc(hinode);
18298 +               AuTraceErr(err);
18299 +               if (unlikely(err)) {
18300 +                       hinode->hi_notify = NULL;
18301 +                       au_cache_free_hnotify(hn);
18302 +                       /*
18303 +                        * The upper dir was removed by udba, but the same named
18304 +                        * dir left. In this case, aufs assigns a new inode
18305 +                        * number and set the monitor again.
18306 +                        * For the lower dir, the old monitor is still left.
18307 +                        */
18308 +                       if (err == -EEXIST)
18309 +                               err = 0;
18310 +               }
18311 +       }
18312 +
18313 +       AuTraceErr(err);
18314 +       return err;
18315 +}
18316 +
18317 +void au_hn_free(struct au_hinode *hinode)
18318 +{
18319 +       struct au_hnotify *hn;
18320 +
18321 +       hn = hinode->hi_notify;
18322 +       if (hn) {
18323 +               hinode->hi_notify = NULL;
18324 +               if (au_hnotify_op.free(hinode, hn))
18325 +                       au_cache_free_hnotify(hn);
18326 +       }
18327 +}
18328 +
18329 +/* ---------------------------------------------------------------------- */
18330 +
18331 +void au_hn_ctl(struct au_hinode *hinode, int do_set)
18332 +{
18333 +       if (hinode->hi_notify)
18334 +               au_hnotify_op.ctl(hinode, do_set);
18335 +}
18336 +
18337 +void au_hn_reset(struct inode *inode, unsigned int flags)
18338 +{
18339 +       aufs_bindex_t bindex, bbot;
18340 +       struct inode *hi;
18341 +       struct dentry *iwhdentry;
18342 +
18343 +       bbot = au_ibbot(inode);
18344 +       for (bindex = au_ibtop(inode); bindex <= bbot; bindex++) {
18345 +               hi = au_h_iptr(inode, bindex);
18346 +               if (!hi)
18347 +                       continue;
18348 +
18349 +               /* inode_lock_nested(hi, AuLsc_I_CHILD); */
18350 +               iwhdentry = au_hi_wh(inode, bindex);
18351 +               if (iwhdentry)
18352 +                       dget(iwhdentry);
18353 +               au_igrab(hi);
18354 +               au_set_h_iptr(inode, bindex, NULL, 0);
18355 +               au_set_h_iptr(inode, bindex, au_igrab(hi),
18356 +                             flags & ~AuHi_XINO);
18357 +               iput(hi);
18358 +               dput(iwhdentry);
18359 +               /* inode_unlock(hi); */
18360 +       }
18361 +}
18362 +
18363 +/* ---------------------------------------------------------------------- */
18364 +
18365 +static int hn_xino(struct inode *inode, struct inode *h_inode)
18366 +{
18367 +       int err;
18368 +       aufs_bindex_t bindex, bbot, bfound, btop;
18369 +       struct inode *h_i;
18370 +
18371 +       err = 0;
18372 +       if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
18373 +               pr_warn("branch root dir was changed\n");
18374 +               goto out;
18375 +       }
18376 +
18377 +       bfound = -1;
18378 +       bbot = au_ibbot(inode);
18379 +       btop = au_ibtop(inode);
18380 +#if 0 /* reserved for future use */
18381 +       if (bindex == bbot) {
18382 +               /* keep this ino in rename case */
18383 +               goto out;
18384 +       }
18385 +#endif
18386 +       for (bindex = btop; bindex <= bbot; bindex++)
18387 +               if (au_h_iptr(inode, bindex) == h_inode) {
18388 +                       bfound = bindex;
18389 +                       break;
18390 +               }
18391 +       if (bfound < 0)
18392 +               goto out;
18393 +
18394 +       for (bindex = btop; bindex <= bbot; bindex++) {
18395 +               h_i = au_h_iptr(inode, bindex);
18396 +               if (!h_i)
18397 +                       continue;
18398 +
18399 +               err = au_xino_write(inode->i_sb, bindex, h_i->i_ino, /*ino*/0);
18400 +               /* ignore this error */
18401 +               /* bad action? */
18402 +       }
18403 +
18404 +       /* children inode number will be broken */
18405 +
18406 +out:
18407 +       AuTraceErr(err);
18408 +       return err;
18409 +}
18410 +
18411 +static int hn_gen_tree(struct dentry *dentry)
18412 +{
18413 +       int err, i, j, ndentry;
18414 +       struct au_dcsub_pages dpages;
18415 +       struct au_dpage *dpage;
18416 +       struct dentry **dentries;
18417 +
18418 +       err = au_dpages_init(&dpages, GFP_NOFS);
18419 +       if (unlikely(err))
18420 +               goto out;
18421 +       err = au_dcsub_pages(&dpages, dentry, NULL, NULL);
18422 +       if (unlikely(err))
18423 +               goto out_dpages;
18424 +
18425 +       for (i = 0; i < dpages.ndpage; i++) {
18426 +               dpage = dpages.dpages + i;
18427 +               dentries = dpage->dentries;
18428 +               ndentry = dpage->ndentry;
18429 +               for (j = 0; j < ndentry; j++) {
18430 +                       struct dentry *d;
18431 +
18432 +                       d = dentries[j];
18433 +                       if (IS_ROOT(d))
18434 +                               continue;
18435 +
18436 +                       au_digen_dec(d);
18437 +                       if (d_really_is_positive(d))
18438 +                               /* todo: reset children xino?
18439 +                                  cached children only? */
18440 +                               au_iigen_dec(d_inode(d));
18441 +               }
18442 +       }
18443 +
18444 +out_dpages:
18445 +       au_dpages_free(&dpages);
18446 +out:
18447 +       return err;
18448 +}
18449 +
18450 +/*
18451 + * return 0 if processed.
18452 + */
18453 +static int hn_gen_by_inode(char *name, unsigned int nlen, struct inode *inode,
18454 +                          const unsigned int isdir)
18455 +{
18456 +       int err;
18457 +       struct dentry *d;
18458 +       struct qstr *dname;
18459 +
18460 +       err = 1;
18461 +       if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
18462 +               pr_warn("branch root dir was changed\n");
18463 +               err = 0;
18464 +               goto out;
18465 +       }
18466 +
18467 +       if (!isdir) {
18468 +               AuDebugOn(!name);
18469 +               au_iigen_dec(inode);
18470 +               spin_lock(&inode->i_lock);
18471 +               hlist_for_each_entry(d, &inode->i_dentry, d_u.d_alias) {
18472 +                       spin_lock(&d->d_lock);
18473 +                       dname = &d->d_name;
18474 +                       if (dname->len != nlen
18475 +                           && memcmp(dname->name, name, nlen)) {
18476 +                               spin_unlock(&d->d_lock);
18477 +                               continue;
18478 +                       }
18479 +                       err = 0;
18480 +                       au_digen_dec(d);
18481 +                       spin_unlock(&d->d_lock);
18482 +                       break;
18483 +               }
18484 +               spin_unlock(&inode->i_lock);
18485 +       } else {
18486 +               au_fset_si(au_sbi(inode->i_sb), FAILED_REFRESH_DIR);
18487 +               d = d_find_any_alias(inode);
18488 +               if (!d) {
18489 +                       au_iigen_dec(inode);
18490 +                       goto out;
18491 +               }
18492 +
18493 +               spin_lock(&d->d_lock);
18494 +               dname = &d->d_name;
18495 +               if (dname->len == nlen && !memcmp(dname->name, name, nlen)) {
18496 +                       spin_unlock(&d->d_lock);
18497 +                       err = hn_gen_tree(d);
18498 +                       spin_lock(&d->d_lock);
18499 +               }
18500 +               spin_unlock(&d->d_lock);
18501 +               dput(d);
18502 +       }
18503 +
18504 +out:
18505 +       AuTraceErr(err);
18506 +       return err;
18507 +}
18508 +
18509 +static int hn_gen_by_name(struct dentry *dentry, const unsigned int isdir)
18510 +{
18511 +       int err;
18512 +
18513 +       if (IS_ROOT(dentry)) {
18514 +               pr_warn("branch root dir was changed\n");
18515 +               return 0;
18516 +       }
18517 +
18518 +       err = 0;
18519 +       if (!isdir) {
18520 +               au_digen_dec(dentry);
18521 +               if (d_really_is_positive(dentry))
18522 +                       au_iigen_dec(d_inode(dentry));
18523 +       } else {
18524 +               au_fset_si(au_sbi(dentry->d_sb), FAILED_REFRESH_DIR);
18525 +               if (d_really_is_positive(dentry))
18526 +                       err = hn_gen_tree(dentry);
18527 +       }
18528 +
18529 +       AuTraceErr(err);
18530 +       return err;
18531 +}
18532 +
18533 +/* ---------------------------------------------------------------------- */
18534 +
18535 +/* hnotify job flags */
18536 +#define AuHnJob_XINO0          1
18537 +#define AuHnJob_GEN            (1 << 1)
18538 +#define AuHnJob_DIRENT         (1 << 2)
18539 +#define AuHnJob_ISDIR          (1 << 3)
18540 +#define AuHnJob_TRYXINO0       (1 << 4)
18541 +#define AuHnJob_MNTPNT         (1 << 5)
18542 +#define au_ftest_hnjob(flags, name)    ((flags) & AuHnJob_##name)
18543 +#define au_fset_hnjob(flags, name) \
18544 +       do { (flags) |= AuHnJob_##name; } while (0)
18545 +#define au_fclr_hnjob(flags, name) \
18546 +       do { (flags) &= ~AuHnJob_##name; } while (0)
18547 +
18548 +enum {
18549 +       AuHn_CHILD,
18550 +       AuHn_PARENT,
18551 +       AuHnLast
18552 +};
18553 +
18554 +struct au_hnotify_args {
18555 +       struct inode *h_dir, *dir, *h_child_inode;
18556 +       u32 mask;
18557 +       unsigned int flags[AuHnLast];
18558 +       unsigned int h_child_nlen;
18559 +       char h_child_name[];
18560 +};
18561 +
18562 +struct hn_job_args {
18563 +       unsigned int flags;
18564 +       struct inode *inode, *h_inode, *dir, *h_dir;
18565 +       struct dentry *dentry;
18566 +       char *h_name;
18567 +       int h_nlen;
18568 +};
18569 +
18570 +static int hn_job(struct hn_job_args *a)
18571 +{
18572 +       const unsigned int isdir = au_ftest_hnjob(a->flags, ISDIR);
18573 +       int e;
18574 +
18575 +       /* reset xino */
18576 +       if (au_ftest_hnjob(a->flags, XINO0) && a->inode)
18577 +               hn_xino(a->inode, a->h_inode); /* ignore this error */
18578 +
18579 +       if (au_ftest_hnjob(a->flags, TRYXINO0)
18580 +           && a->inode
18581 +           && a->h_inode) {
18582 +               inode_lock_shared_nested(a->h_inode, AuLsc_I_CHILD);
18583 +               if (!a->h_inode->i_nlink
18584 +                   && !(a->h_inode->i_state & I_LINKABLE))
18585 +                       hn_xino(a->inode, a->h_inode); /* ignore this error */
18586 +               inode_unlock_shared(a->h_inode);
18587 +       }
18588 +
18589 +       /* make the generation obsolete */
18590 +       if (au_ftest_hnjob(a->flags, GEN)) {
18591 +               e = -1;
18592 +               if (a->inode)
18593 +                       e = hn_gen_by_inode(a->h_name, a->h_nlen, a->inode,
18594 +                                             isdir);
18595 +               if (e && a->dentry)
18596 +                       hn_gen_by_name(a->dentry, isdir);
18597 +               /* ignore this error */
18598 +       }
18599 +
18600 +       /* make dir entries obsolete */
18601 +       if (au_ftest_hnjob(a->flags, DIRENT) && a->inode) {
18602 +               struct au_vdir *vdir;
18603 +
18604 +               vdir = au_ivdir(a->inode);
18605 +               if (vdir)
18606 +                       vdir->vd_jiffy = 0;
18607 +               /* IMustLock(a->inode); */
18608 +               /* inode_inc_iversion(a->inode); */
18609 +       }
18610 +
18611 +       /* can do nothing but warn */
18612 +       if (au_ftest_hnjob(a->flags, MNTPNT)
18613 +           && a->dentry
18614 +           && d_mountpoint(a->dentry))
18615 +               pr_warn("mount-point %pd is removed or renamed\n", a->dentry);
18616 +
18617 +       return 0;
18618 +}
18619 +
18620 +/* ---------------------------------------------------------------------- */
18621 +
18622 +static struct dentry *lookup_wlock_by_name(char *name, unsigned int nlen,
18623 +                                          struct inode *dir)
18624 +{
18625 +       struct dentry *dentry, *d, *parent;
18626 +       struct qstr *dname;
18627 +
18628 +       parent = d_find_any_alias(dir);
18629 +       if (!parent)
18630 +               return NULL;
18631 +
18632 +       dentry = NULL;
18633 +       spin_lock(&parent->d_lock);
18634 +       list_for_each_entry(d, &parent->d_subdirs, d_child) {
18635 +               /* AuDbg("%pd\n", d); */
18636 +               spin_lock_nested(&d->d_lock, DENTRY_D_LOCK_NESTED);
18637 +               dname = &d->d_name;
18638 +               if (dname->len != nlen || memcmp(dname->name, name, nlen))
18639 +                       goto cont_unlock;
18640 +               if (au_di(d))
18641 +                       au_digen_dec(d);
18642 +               else
18643 +                       goto cont_unlock;
18644 +               if (au_dcount(d) > 0) {
18645 +                       dentry = dget_dlock(d);
18646 +                       spin_unlock(&d->d_lock);
18647 +                       break;
18648 +               }
18649 +
18650 +cont_unlock:
18651 +               spin_unlock(&d->d_lock);
18652 +       }
18653 +       spin_unlock(&parent->d_lock);
18654 +       dput(parent);
18655 +
18656 +       if (dentry)
18657 +               di_write_lock_child(dentry);
18658 +
18659 +       return dentry;
18660 +}
18661 +
18662 +static struct inode *lookup_wlock_by_ino(struct super_block *sb,
18663 +                                        aufs_bindex_t bindex, ino_t h_ino)
18664 +{
18665 +       struct inode *inode;
18666 +       ino_t ino;
18667 +       int err;
18668 +
18669 +       inode = NULL;
18670 +       err = au_xino_read(sb, bindex, h_ino, &ino);
18671 +       if (!err && ino)
18672 +               inode = ilookup(sb, ino);
18673 +       if (!inode)
18674 +               goto out;
18675 +
18676 +       if (unlikely(inode->i_ino == AUFS_ROOT_INO)) {
18677 +               pr_warn("wrong root branch\n");
18678 +               iput(inode);
18679 +               inode = NULL;
18680 +               goto out;
18681 +       }
18682 +
18683 +       ii_write_lock_child(inode);
18684 +
18685 +out:
18686 +       return inode;
18687 +}
18688 +
18689 +static void au_hn_bh(void *_args)
18690 +{
18691 +       struct au_hnotify_args *a = _args;
18692 +       struct super_block *sb;
18693 +       aufs_bindex_t bindex, bbot, bfound;
18694 +       unsigned char xino, try_iput;
18695 +       int err;
18696 +       struct inode *inode;
18697 +       ino_t h_ino;
18698 +       struct hn_job_args args;
18699 +       struct dentry *dentry;
18700 +       struct au_sbinfo *sbinfo;
18701 +
18702 +       AuDebugOn(!_args);
18703 +       AuDebugOn(!a->h_dir);
18704 +       AuDebugOn(!a->dir);
18705 +       AuDebugOn(!a->mask);
18706 +       AuDbg("mask 0x%x, i%lu, hi%lu, hci%lu\n",
18707 +             a->mask, a->dir->i_ino, a->h_dir->i_ino,
18708 +             a->h_child_inode ? a->h_child_inode->i_ino : 0);
18709 +
18710 +       inode = NULL;
18711 +       dentry = NULL;
18712 +       /*
18713 +        * do not lock a->dir->i_mutex here
18714 +        * because of d_revalidate() may cause a deadlock.
18715 +        */
18716 +       sb = a->dir->i_sb;
18717 +       AuDebugOn(!sb);
18718 +       sbinfo = au_sbi(sb);
18719 +       AuDebugOn(!sbinfo);
18720 +       si_write_lock(sb, AuLock_NOPLMW);
18721 +
18722 +       if (au_opt_test(sbinfo->si_mntflags, DIRREN))
18723 +               switch (a->mask & FS_EVENTS_POSS_ON_CHILD) {
18724 +               case FS_MOVED_FROM:
18725 +               case FS_MOVED_TO:
18726 +                       AuWarn1("DIRREN with UDBA may not work correctly "
18727 +                               "for the direct rename(2)\n");
18728 +               }
18729 +
18730 +       ii_read_lock_parent(a->dir);
18731 +       bfound = -1;
18732 +       bbot = au_ibbot(a->dir);
18733 +       for (bindex = au_ibtop(a->dir); bindex <= bbot; bindex++)
18734 +               if (au_h_iptr(a->dir, bindex) == a->h_dir) {
18735 +                       bfound = bindex;
18736 +                       break;
18737 +               }
18738 +       ii_read_unlock(a->dir);
18739 +       if (unlikely(bfound < 0))
18740 +               goto out;
18741 +
18742 +       xino = !!au_opt_test(au_mntflags(sb), XINO);
18743 +       h_ino = 0;
18744 +       if (a->h_child_inode)
18745 +               h_ino = a->h_child_inode->i_ino;
18746 +
18747 +       if (a->h_child_nlen
18748 +           && (au_ftest_hnjob(a->flags[AuHn_CHILD], GEN)
18749 +               || au_ftest_hnjob(a->flags[AuHn_CHILD], MNTPNT)))
18750 +               dentry = lookup_wlock_by_name(a->h_child_name, a->h_child_nlen,
18751 +                                             a->dir);
18752 +       try_iput = 0;
18753 +       if (dentry && d_really_is_positive(dentry))
18754 +               inode = d_inode(dentry);
18755 +       if (xino && !inode && h_ino
18756 +           && (au_ftest_hnjob(a->flags[AuHn_CHILD], XINO0)
18757 +               || au_ftest_hnjob(a->flags[AuHn_CHILD], TRYXINO0)
18758 +               || au_ftest_hnjob(a->flags[AuHn_CHILD], GEN))) {
18759 +               inode = lookup_wlock_by_ino(sb, bfound, h_ino);
18760 +               try_iput = 1;
18761 +       }
18762 +
18763 +       args.flags = a->flags[AuHn_CHILD];
18764 +       args.dentry = dentry;
18765 +       args.inode = inode;
18766 +       args.h_inode = a->h_child_inode;
18767 +       args.dir = a->dir;
18768 +       args.h_dir = a->h_dir;
18769 +       args.h_name = a->h_child_name;
18770 +       args.h_nlen = a->h_child_nlen;
18771 +       err = hn_job(&args);
18772 +       if (dentry) {
18773 +               if (au_di(dentry))
18774 +                       di_write_unlock(dentry);
18775 +               dput(dentry);
18776 +       }
18777 +       if (inode && try_iput) {
18778 +               ii_write_unlock(inode);
18779 +               iput(inode);
18780 +       }
18781 +
18782 +       ii_write_lock_parent(a->dir);
18783 +       args.flags = a->flags[AuHn_PARENT];
18784 +       args.dentry = NULL;
18785 +       args.inode = a->dir;
18786 +       args.h_inode = a->h_dir;
18787 +       args.dir = NULL;
18788 +       args.h_dir = NULL;
18789 +       args.h_name = NULL;
18790 +       args.h_nlen = 0;
18791 +       err = hn_job(&args);
18792 +       ii_write_unlock(a->dir);
18793 +
18794 +out:
18795 +       iput(a->h_child_inode);
18796 +       iput(a->h_dir);
18797 +       iput(a->dir);
18798 +       si_write_unlock(sb);
18799 +       au_nwt_done(&sbinfo->si_nowait);
18800 +       au_kfree_rcu(a);
18801 +}
18802 +
18803 +/* ---------------------------------------------------------------------- */
18804 +
18805 +int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
18806 +              const struct qstr *h_child_qstr, struct inode *h_child_inode)
18807 +{
18808 +       int err, len;
18809 +       unsigned int flags[AuHnLast], f;
18810 +       unsigned char isdir, isroot, wh;
18811 +       struct inode *dir;
18812 +       struct au_hnotify_args *args;
18813 +       char *p, *h_child_name;
18814 +
18815 +       err = 0;
18816 +       AuDebugOn(!hnotify || !hnotify->hn_aufs_inode);
18817 +       dir = igrab(hnotify->hn_aufs_inode);
18818 +       if (!dir)
18819 +               goto out;
18820 +
18821 +       isroot = (dir->i_ino == AUFS_ROOT_INO);
18822 +       wh = 0;
18823 +       h_child_name = (void *)h_child_qstr->name;
18824 +       len = h_child_qstr->len;
18825 +       if (h_child_name) {
18826 +               if (len > AUFS_WH_PFX_LEN
18827 +                   && !memcmp(h_child_name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
18828 +                       h_child_name += AUFS_WH_PFX_LEN;
18829 +                       len -= AUFS_WH_PFX_LEN;
18830 +                       wh = 1;
18831 +               }
18832 +       }
18833 +
18834 +       isdir = 0;
18835 +       if (h_child_inode)
18836 +               isdir = !!S_ISDIR(h_child_inode->i_mode);
18837 +       flags[AuHn_PARENT] = AuHnJob_ISDIR;
18838 +       flags[AuHn_CHILD] = 0;
18839 +       if (isdir)
18840 +               flags[AuHn_CHILD] = AuHnJob_ISDIR;
18841 +       au_fset_hnjob(flags[AuHn_PARENT], DIRENT);
18842 +       au_fset_hnjob(flags[AuHn_CHILD], GEN);
18843 +       switch (mask & ALL_FSNOTIFY_DIRENT_EVENTS) {
18844 +       case FS_MOVED_FROM:
18845 +       case FS_MOVED_TO:
18846 +               au_fset_hnjob(flags[AuHn_CHILD], XINO0);
18847 +               au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
18848 +               fallthrough;
18849 +       case FS_CREATE:
18850 +               AuDebugOn(!h_child_name);
18851 +               break;
18852 +
18853 +       case FS_DELETE:
18854 +               /*
18855 +                * aufs never be able to get this child inode.
18856 +                * revalidation should be in d_revalidate()
18857 +                * by checking i_nlink, i_generation or d_unhashed().
18858 +                */
18859 +               AuDebugOn(!h_child_name);
18860 +               au_fset_hnjob(flags[AuHn_CHILD], TRYXINO0);
18861 +               au_fset_hnjob(flags[AuHn_CHILD], MNTPNT);
18862 +               break;
18863 +
18864 +       default:
18865 +               AuDebugOn(1);
18866 +       }
18867 +
18868 +       if (wh)
18869 +               h_child_inode = NULL;
18870 +
18871 +       err = -ENOMEM;
18872 +       /* iput() and kfree() will be called in au_hnotify() */
18873 +       args = kmalloc(sizeof(*args) + len + 1, GFP_NOFS);
18874 +       if (unlikely(!args)) {
18875 +               AuErr1("no memory\n");
18876 +               iput(dir);
18877 +               goto out;
18878 +       }
18879 +       args->flags[AuHn_PARENT] = flags[AuHn_PARENT];
18880 +       args->flags[AuHn_CHILD] = flags[AuHn_CHILD];
18881 +       args->mask = mask;
18882 +       args->dir = dir;
18883 +       args->h_dir = igrab(h_dir);
18884 +       if (h_child_inode)
18885 +               h_child_inode = igrab(h_child_inode); /* can be NULL */
18886 +       args->h_child_inode = h_child_inode;
18887 +       args->h_child_nlen = len;
18888 +       if (len) {
18889 +               p = (void *)args;
18890 +               p += sizeof(*args);
18891 +               memcpy(p, h_child_name, len);
18892 +               p[len] = 0;
18893 +       }
18894 +
18895 +       /* NFS fires the event for silly-renamed one from kworker */
18896 +       f = 0;
18897 +       if (!dir->i_nlink
18898 +           || (au_test_nfs(h_dir->i_sb) && (mask & FS_DELETE)))
18899 +               f = AuWkq_NEST;
18900 +       err = au_wkq_nowait(au_hn_bh, args, dir->i_sb, f);
18901 +       if (unlikely(err)) {
18902 +               pr_err("wkq %d\n", err);
18903 +               iput(args->h_child_inode);
18904 +               iput(args->h_dir);
18905 +               iput(args->dir);
18906 +               au_kfree_rcu(args);
18907 +       }
18908 +
18909 +out:
18910 +       return err;
18911 +}
18912 +
18913 +/* ---------------------------------------------------------------------- */
18914 +
18915 +int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm)
18916 +{
18917 +       int err;
18918 +
18919 +       AuDebugOn(!(udba & AuOptMask_UDBA));
18920 +
18921 +       err = 0;
18922 +       if (au_hnotify_op.reset_br)
18923 +               err = au_hnotify_op.reset_br(udba, br, perm);
18924 +
18925 +       return err;
18926 +}
18927 +
18928 +int au_hnotify_init_br(struct au_branch *br, int perm)
18929 +{
18930 +       int err;
18931 +
18932 +       err = 0;
18933 +       if (au_hnotify_op.init_br)
18934 +               err = au_hnotify_op.init_br(br, perm);
18935 +
18936 +       return err;
18937 +}
18938 +
18939 +void au_hnotify_fin_br(struct au_branch *br)
18940 +{
18941 +       if (au_hnotify_op.fin_br)
18942 +               au_hnotify_op.fin_br(br);
18943 +}
18944 +
18945 +static void au_hn_destroy_cache(void)
18946 +{
18947 +       kmem_cache_destroy(au_cache[AuCache_HNOTIFY]);
18948 +       au_cache[AuCache_HNOTIFY] = NULL;
18949 +}
18950 +
18951 +int __init au_hnotify_init(void)
18952 +{
18953 +       int err;
18954 +
18955 +       err = -ENOMEM;
18956 +       au_cache[AuCache_HNOTIFY] = AuCache(au_hnotify);
18957 +       if (au_cache[AuCache_HNOTIFY]) {
18958 +               err = 0;
18959 +               if (au_hnotify_op.init)
18960 +                       err = au_hnotify_op.init();
18961 +               if (unlikely(err))
18962 +                       au_hn_destroy_cache();
18963 +       }
18964 +       AuTraceErr(err);
18965 +       return err;
18966 +}
18967 +
18968 +void au_hnotify_fin(void)
18969 +{
18970 +       if (au_hnotify_op.fin)
18971 +               au_hnotify_op.fin();
18972 +
18973 +       /* cf. au_cache_fin() */
18974 +       if (au_cache[AuCache_HNOTIFY])
18975 +               au_hn_destroy_cache();
18976 +}
18977 diff -urN /usr/share/empty/fs/aufs/iinfo.c linux/fs/aufs/iinfo.c
18978 --- /usr/share/empty/fs/aufs/iinfo.c    1970-01-01 01:00:00.000000000 +0100
18979 +++ linux/fs/aufs/iinfo.c       2022-03-21 14:49:05.726633010 +0100
18980 @@ -0,0 +1,286 @@
18981 +// SPDX-License-Identifier: GPL-2.0
18982 +/*
18983 + * Copyright (C) 2005-2021 Junjiro R. Okajima
18984 + *
18985 + * This program, aufs is free software; you can redistribute it and/or modify
18986 + * it under the terms of the GNU General Public License as published by
18987 + * the Free Software Foundation; either version 2 of the License, or
18988 + * (at your option) any later version.
18989 + *
18990 + * This program is distributed in the hope that it will be useful,
18991 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
18992 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18993 + * GNU General Public License for more details.
18994 + *
18995 + * You should have received a copy of the GNU General Public License
18996 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18997 + */
18998 +
18999 +/*
19000 + * inode private data
19001 + */
19002 +
19003 +#include "aufs.h"
19004 +
19005 +struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex)
19006 +{
19007 +       struct inode *h_inode;
19008 +       struct au_hinode *hinode;
19009 +
19010 +       IiMustAnyLock(inode);
19011 +
19012 +       hinode = au_hinode(au_ii(inode), bindex);
19013 +       h_inode = hinode->hi_inode;
19014 +       AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
19015 +       return h_inode;
19016 +}
19017 +
19018 +/* todo: hard/soft set? */
19019 +void au_hiput(struct au_hinode *hinode)
19020 +{
19021 +       au_hn_free(hinode);
19022 +       dput(hinode->hi_whdentry);
19023 +       iput(hinode->hi_inode);
19024 +}
19025 +
19026 +unsigned int au_hi_flags(struct inode *inode, int isdir)
19027 +{
19028 +       unsigned int flags;
19029 +       const unsigned int mnt_flags = au_mntflags(inode->i_sb);
19030 +
19031 +       flags = 0;
19032 +       if (au_opt_test(mnt_flags, XINO))
19033 +               au_fset_hi(flags, XINO);
19034 +       if (isdir && au_opt_test(mnt_flags, UDBA_HNOTIFY))
19035 +               au_fset_hi(flags, HNOTIFY);
19036 +       return flags;
19037 +}
19038 +
19039 +void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
19040 +                  struct inode *h_inode, unsigned int flags)
19041 +{
19042 +       struct au_hinode *hinode;
19043 +       struct inode *hi;
19044 +       struct au_iinfo *iinfo = au_ii(inode);
19045 +
19046 +       IiMustWriteLock(inode);
19047 +
19048 +       hinode = au_hinode(iinfo, bindex);
19049 +       hi = hinode->hi_inode;
19050 +       AuDebugOn(h_inode && atomic_read(&h_inode->i_count) <= 0);
19051 +
19052 +       if (hi)
19053 +               au_hiput(hinode);
19054 +       hinode->hi_inode = h_inode;
19055 +       if (h_inode) {
19056 +               int err;
19057 +               struct super_block *sb = inode->i_sb;
19058 +               struct au_branch *br;
19059 +
19060 +               AuDebugOn(inode->i_mode
19061 +                         && (h_inode->i_mode & S_IFMT)
19062 +                         != (inode->i_mode & S_IFMT));
19063 +               if (bindex == iinfo->ii_btop)
19064 +                       au_cpup_igen(inode, h_inode);
19065 +               br = au_sbr(sb, bindex);
19066 +               hinode->hi_id = br->br_id;
19067 +               if (au_ftest_hi(flags, XINO)) {
19068 +                       err = au_xino_write(sb, bindex, h_inode->i_ino,
19069 +                                           inode->i_ino);
19070 +                       if (unlikely(err))
19071 +                               AuIOErr1("failed au_xino_write() %d\n", err);
19072 +               }
19073 +
19074 +               if (au_ftest_hi(flags, HNOTIFY)
19075 +                   && au_br_hnotifyable(br->br_perm)) {
19076 +                       err = au_hn_alloc(hinode, inode);
19077 +                       if (unlikely(err))
19078 +                               AuIOErr1("au_hn_alloc() %d\n", err);
19079 +               }
19080 +       }
19081 +}
19082 +
19083 +void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
19084 +                 struct dentry *h_wh)
19085 +{
19086 +       struct au_hinode *hinode;
19087 +
19088 +       IiMustWriteLock(inode);
19089 +
19090 +       hinode = au_hinode(au_ii(inode), bindex);
19091 +       AuDebugOn(hinode->hi_whdentry);
19092 +       hinode->hi_whdentry = h_wh;
19093 +}
19094 +
19095 +void au_update_iigen(struct inode *inode, int half)
19096 +{
19097 +       struct au_iinfo *iinfo;
19098 +       struct au_iigen *iigen;
19099 +       unsigned int sigen;
19100 +
19101 +       sigen = au_sigen(inode->i_sb);
19102 +       iinfo = au_ii(inode);
19103 +       iigen = &iinfo->ii_generation;
19104 +       spin_lock(&iigen->ig_spin);
19105 +       iigen->ig_generation = sigen;
19106 +       if (half)
19107 +               au_ig_fset(iigen->ig_flags, HALF_REFRESHED);
19108 +       else
19109 +               au_ig_fclr(iigen->ig_flags, HALF_REFRESHED);
19110 +       spin_unlock(&iigen->ig_spin);
19111 +}
19112 +
19113 +/* it may be called at remount time, too */
19114 +void au_update_ibrange(struct inode *inode, int do_put_zero)
19115 +{
19116 +       struct au_iinfo *iinfo;
19117 +       aufs_bindex_t bindex, bbot;
19118 +
19119 +       AuDebugOn(au_is_bad_inode(inode));
19120 +       IiMustWriteLock(inode);
19121 +
19122 +       iinfo = au_ii(inode);
19123 +       if (do_put_zero && iinfo->ii_btop >= 0) {
19124 +               for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot;
19125 +                    bindex++) {
19126 +                       struct inode *h_i;
19127 +
19128 +                       h_i = au_hinode(iinfo, bindex)->hi_inode;
19129 +                       if (h_i
19130 +                           && !h_i->i_nlink
19131 +                           && !(h_i->i_state & I_LINKABLE))
19132 +                               au_set_h_iptr(inode, bindex, NULL, 0);
19133 +               }
19134 +       }
19135 +
19136 +       iinfo->ii_btop = -1;
19137 +       iinfo->ii_bbot = -1;
19138 +       bbot = au_sbbot(inode->i_sb);
19139 +       for (bindex = 0; bindex <= bbot; bindex++)
19140 +               if (au_hinode(iinfo, bindex)->hi_inode) {
19141 +                       iinfo->ii_btop = bindex;
19142 +                       break;
19143 +               }
19144 +       if (iinfo->ii_btop >= 0)
19145 +               for (bindex = bbot; bindex >= iinfo->ii_btop; bindex--)
19146 +                       if (au_hinode(iinfo, bindex)->hi_inode) {
19147 +                               iinfo->ii_bbot = bindex;
19148 +                               break;
19149 +                       }
19150 +       AuDebugOn(iinfo->ii_btop > iinfo->ii_bbot);
19151 +}
19152 +
19153 +/* ---------------------------------------------------------------------- */
19154 +
19155 +void au_icntnr_init_once(void *_c)
19156 +{
19157 +       struct au_icntnr *c = _c;
19158 +       struct au_iinfo *iinfo = &c->iinfo;
19159 +
19160 +       spin_lock_init(&iinfo->ii_generation.ig_spin);
19161 +       au_rw_init(&iinfo->ii_rwsem);
19162 +       inode_init_once(&c->vfs_inode);
19163 +}
19164 +
19165 +void au_hinode_init(struct au_hinode *hinode)
19166 +{
19167 +       hinode->hi_inode = NULL;
19168 +       hinode->hi_id = -1;
19169 +       au_hn_init(hinode);
19170 +       hinode->hi_whdentry = NULL;
19171 +}
19172 +
19173 +int au_iinfo_init(struct inode *inode)
19174 +{
19175 +       struct au_iinfo *iinfo;
19176 +       struct super_block *sb;
19177 +       struct au_hinode *hi;
19178 +       int nbr, i;
19179 +
19180 +       sb = inode->i_sb;
19181 +       iinfo = &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
19182 +       nbr = au_sbbot(sb) + 1;
19183 +       if (unlikely(nbr <= 0))
19184 +               nbr = 1;
19185 +       hi = kmalloc_array(nbr, sizeof(*iinfo->ii_hinode), GFP_NOFS);
19186 +       if (hi) {
19187 +               au_lcnt_inc(&au_sbi(sb)->si_ninodes);
19188 +
19189 +               iinfo->ii_hinode = hi;
19190 +               for (i = 0; i < nbr; i++, hi++)
19191 +                       au_hinode_init(hi);
19192 +
19193 +               iinfo->ii_generation.ig_generation = au_sigen(sb);
19194 +               iinfo->ii_btop = -1;
19195 +               iinfo->ii_bbot = -1;
19196 +               iinfo->ii_vdir = NULL;
19197 +               return 0;
19198 +       }
19199 +       return -ENOMEM;
19200 +}
19201 +
19202 +int au_hinode_realloc(struct au_iinfo *iinfo, int nbr, int may_shrink)
19203 +{
19204 +       int err, i;
19205 +       struct au_hinode *hip;
19206 +
19207 +       AuRwMustWriteLock(&iinfo->ii_rwsem);
19208 +
19209 +       err = -ENOMEM;
19210 +       hip = au_krealloc(iinfo->ii_hinode, sizeof(*hip) * nbr, GFP_NOFS,
19211 +                         may_shrink);
19212 +       if (hip) {
19213 +               iinfo->ii_hinode = hip;
19214 +               i = iinfo->ii_bbot + 1;
19215 +               hip += i;
19216 +               for (; i < nbr; i++, hip++)
19217 +                       au_hinode_init(hip);
19218 +               err = 0;
19219 +       }
19220 +
19221 +       return err;
19222 +}
19223 +
19224 +void au_iinfo_fin(struct inode *inode)
19225 +{
19226 +       struct au_iinfo *iinfo;
19227 +       struct au_hinode *hi;
19228 +       struct super_block *sb;
19229 +       aufs_bindex_t bindex, bbot;
19230 +       const unsigned char unlinked = !inode->i_nlink;
19231 +
19232 +       AuDebugOn(au_is_bad_inode(inode));
19233 +
19234 +       sb = inode->i_sb;
19235 +       au_lcnt_dec(&au_sbi(sb)->si_ninodes);
19236 +       if (si_pid_test(sb))
19237 +               au_xino_delete_inode(inode, unlinked);
19238 +       else {
19239 +               /*
19240 +                * it is safe to hide the dependency between sbinfo and
19241 +                * sb->s_umount.
19242 +                */
19243 +               lockdep_off();
19244 +               si_noflush_read_lock(sb);
19245 +               au_xino_delete_inode(inode, unlinked);
19246 +               si_read_unlock(sb);
19247 +               lockdep_on();
19248 +       }
19249 +
19250 +       iinfo = au_ii(inode);
19251 +       if (iinfo->ii_vdir)
19252 +               au_vdir_free(iinfo->ii_vdir);
19253 +
19254 +       bindex = iinfo->ii_btop;
19255 +       if (bindex >= 0) {
19256 +               hi = au_hinode(iinfo, bindex);
19257 +               bbot = iinfo->ii_bbot;
19258 +               while (bindex++ <= bbot) {
19259 +                       if (hi->hi_inode)
19260 +                               au_hiput(hi);
19261 +                       hi++;
19262 +               }
19263 +       }
19264 +       au_kfree_rcu(iinfo->ii_hinode);
19265 +       AuRwDestroy(&iinfo->ii_rwsem);
19266 +}
19267 diff -urN /usr/share/empty/fs/aufs/inode.c linux/fs/aufs/inode.c
19268 --- /usr/share/empty/fs/aufs/inode.c    1970-01-01 01:00:00.000000000 +0100
19269 +++ linux/fs/aufs/inode.c       2022-03-21 14:49:05.726633010 +0100
19270 @@ -0,0 +1,531 @@
19271 +// SPDX-License-Identifier: GPL-2.0
19272 +/*
19273 + * Copyright (C) 2005-2021 Junjiro R. Okajima
19274 + *
19275 + * This program, aufs is free software; you can redistribute it and/or modify
19276 + * it under the terms of the GNU General Public License as published by
19277 + * the Free Software Foundation; either version 2 of the License, or
19278 + * (at your option) any later version.
19279 + *
19280 + * This program is distributed in the hope that it will be useful,
19281 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
19282 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19283 + * GNU General Public License for more details.
19284 + *
19285 + * You should have received a copy of the GNU General Public License
19286 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19287 + */
19288 +
19289 +/*
19290 + * inode functions
19291 + */
19292 +
19293 +#include <linux/iversion.h>
19294 +#include "aufs.h"
19295 +
19296 +struct inode *au_igrab(struct inode *inode)
19297 +{
19298 +       if (inode) {
19299 +               AuDebugOn(!atomic_read(&inode->i_count));
19300 +               ihold(inode);
19301 +       }
19302 +       return inode;
19303 +}
19304 +
19305 +static void au_refresh_hinode_attr(struct inode *inode, int do_version)
19306 +{
19307 +       au_cpup_attr_all(inode, /*force*/0);
19308 +       au_update_iigen(inode, /*half*/1);
19309 +       if (do_version)
19310 +               inode_inc_iversion(inode);
19311 +}
19312 +
19313 +static int au_ii_refresh(struct inode *inode, int *update)
19314 +{
19315 +       int err, e, nbr;
19316 +       umode_t type;
19317 +       aufs_bindex_t bindex, new_bindex;
19318 +       struct super_block *sb;
19319 +       struct au_iinfo *iinfo;
19320 +       struct au_hinode *p, *q, tmp;
19321 +
19322 +       AuDebugOn(au_is_bad_inode(inode));
19323 +       IiMustWriteLock(inode);
19324 +
19325 +       *update = 0;
19326 +       sb = inode->i_sb;
19327 +       nbr = au_sbbot(sb) + 1;
19328 +       type = inode->i_mode & S_IFMT;
19329 +       iinfo = au_ii(inode);
19330 +       err = au_hinode_realloc(iinfo, nbr, /*may_shrink*/0);
19331 +       if (unlikely(err))
19332 +               goto out;
19333 +
19334 +       AuDebugOn(iinfo->ii_btop < 0);
19335 +       p = au_hinode(iinfo, iinfo->ii_btop);
19336 +       for (bindex = iinfo->ii_btop; bindex <= iinfo->ii_bbot;
19337 +            bindex++, p++) {
19338 +               if (!p->hi_inode)
19339 +                       continue;
19340 +
19341 +               AuDebugOn(type != (p->hi_inode->i_mode & S_IFMT));
19342 +               new_bindex = au_br_index(sb, p->hi_id);
19343 +               if (new_bindex == bindex)
19344 +                       continue;
19345 +
19346 +               if (new_bindex < 0) {
19347 +                       *update = 1;
19348 +                       au_hiput(p);
19349 +                       p->hi_inode = NULL;
19350 +                       continue;
19351 +               }
19352 +
19353 +               if (new_bindex < iinfo->ii_btop)
19354 +                       iinfo->ii_btop = new_bindex;
19355 +               if (iinfo->ii_bbot < new_bindex)
19356 +                       iinfo->ii_bbot = new_bindex;
19357 +               /* swap two lower inode, and loop again */
19358 +               q = au_hinode(iinfo, new_bindex);
19359 +               tmp = *q;
19360 +               *q = *p;
19361 +               *p = tmp;
19362 +               if (tmp.hi_inode) {
19363 +                       bindex--;
19364 +                       p--;
19365 +               }
19366 +       }
19367 +       au_update_ibrange(inode, /*do_put_zero*/0);
19368 +       au_hinode_realloc(iinfo, nbr, /*may_shrink*/1); /* harmless if err */
19369 +       e = au_dy_irefresh(inode);
19370 +       if (unlikely(e && !err))
19371 +               err = e;
19372 +
19373 +out:
19374 +       AuTraceErr(err);
19375 +       return err;
19376 +}
19377 +
19378 +void au_refresh_iop(struct inode *inode, int force_getattr)
19379 +{
19380 +       int type;
19381 +       struct au_sbinfo *sbi = au_sbi(inode->i_sb);
19382 +       const struct inode_operations *iop
19383 +               = force_getattr ? aufs_iop : sbi->si_iop_array;
19384 +
19385 +       if (inode->i_op == iop)
19386 +               return;
19387 +
19388 +       switch (inode->i_mode & S_IFMT) {
19389 +       case S_IFDIR:
19390 +               type = AuIop_DIR;
19391 +               break;
19392 +       case S_IFLNK:
19393 +               type = AuIop_SYMLINK;
19394 +               break;
19395 +       default:
19396 +               type = AuIop_OTHER;
19397 +               break;
19398 +       }
19399 +
19400 +       inode->i_op = iop + type;
19401 +       /* unnecessary smp_wmb() */
19402 +}
19403 +
19404 +int au_refresh_hinode_self(struct inode *inode)
19405 +{
19406 +       int err, update;
19407 +
19408 +       err = au_ii_refresh(inode, &update);
19409 +       if (!err)
19410 +               au_refresh_hinode_attr(inode, update && S_ISDIR(inode->i_mode));
19411 +
19412 +       AuTraceErr(err);
19413 +       return err;
19414 +}
19415 +
19416 +int au_refresh_hinode(struct inode *inode, struct dentry *dentry)
19417 +{
19418 +       int err, e, update;
19419 +       unsigned int flags;
19420 +       umode_t mode;
19421 +       aufs_bindex_t bindex, bbot;
19422 +       unsigned char isdir;
19423 +       struct au_hinode *p;
19424 +       struct au_iinfo *iinfo;
19425 +
19426 +       err = au_ii_refresh(inode, &update);
19427 +       if (unlikely(err))
19428 +               goto out;
19429 +
19430 +       update = 0;
19431 +       iinfo = au_ii(inode);
19432 +       p = au_hinode(iinfo, iinfo->ii_btop);
19433 +       mode = (inode->i_mode & S_IFMT);
19434 +       isdir = S_ISDIR(mode);
19435 +       flags = au_hi_flags(inode, isdir);
19436 +       bbot = au_dbbot(dentry);
19437 +       for (bindex = au_dbtop(dentry); bindex <= bbot; bindex++) {
19438 +               struct inode *h_i, *h_inode;
19439 +               struct dentry *h_d;
19440 +
19441 +               h_d = au_h_dptr(dentry, bindex);
19442 +               if (!h_d || d_is_negative(h_d))
19443 +                       continue;
19444 +
19445 +               h_inode = d_inode(h_d);
19446 +               AuDebugOn(mode != (h_inode->i_mode & S_IFMT));
19447 +               if (iinfo->ii_btop <= bindex && bindex <= iinfo->ii_bbot) {
19448 +                       h_i = au_h_iptr(inode, bindex);
19449 +                       if (h_i) {
19450 +                               if (h_i == h_inode)
19451 +                                       continue;
19452 +                               err = -EIO;
19453 +                               break;
19454 +                       }
19455 +               }
19456 +               if (bindex < iinfo->ii_btop)
19457 +                       iinfo->ii_btop = bindex;
19458 +               if (iinfo->ii_bbot < bindex)
19459 +                       iinfo->ii_bbot = bindex;
19460 +               au_set_h_iptr(inode, bindex, au_igrab(h_inode), flags);
19461 +               update = 1;
19462 +       }
19463 +       au_update_ibrange(inode, /*do_put_zero*/0);
19464 +       e = au_dy_irefresh(inode);
19465 +       if (unlikely(e && !err))
19466 +               err = e;
19467 +       if (!err)
19468 +               au_refresh_hinode_attr(inode, update && isdir);
19469 +
19470 +out:
19471 +       AuTraceErr(err);
19472 +       return err;
19473 +}
19474 +
19475 +static int set_inode(struct inode *inode, struct dentry *dentry)
19476 +{
19477 +       int err;
19478 +       unsigned int flags;
19479 +       umode_t mode;
19480 +       aufs_bindex_t bindex, btop, btail;
19481 +       unsigned char isdir;
19482 +       struct dentry *h_dentry;
19483 +       struct inode *h_inode;
19484 +       struct au_iinfo *iinfo;
19485 +       const struct inode_operations *iop;
19486 +
19487 +       IiMustWriteLock(inode);
19488 +
19489 +       err = 0;
19490 +       isdir = 0;
19491 +       iop = au_sbi(inode->i_sb)->si_iop_array;
19492 +       btop = au_dbtop(dentry);
19493 +       h_dentry = au_h_dptr(dentry, btop);
19494 +       h_inode = d_inode(h_dentry);
19495 +       mode = h_inode->i_mode;
19496 +       switch (mode & S_IFMT) {
19497 +       case S_IFREG:
19498 +               btail = au_dbtail(dentry);
19499 +               inode->i_op = iop + AuIop_OTHER;
19500 +               inode->i_fop = &aufs_file_fop;
19501 +               err = au_dy_iaop(inode, btop, h_inode);
19502 +               if (unlikely(err))
19503 +                       goto out;
19504 +               break;
19505 +       case S_IFDIR:
19506 +               isdir = 1;
19507 +               btail = au_dbtaildir(dentry);
19508 +               inode->i_op = iop + AuIop_DIR;
19509 +               inode->i_fop = &aufs_dir_fop;
19510 +               break;
19511 +       case S_IFLNK:
19512 +               btail = au_dbtail(dentry);
19513 +               inode->i_op = iop + AuIop_SYMLINK;
19514 +               break;
19515 +       case S_IFBLK:
19516 +       case S_IFCHR:
19517 +       case S_IFIFO:
19518 +       case S_IFSOCK:
19519 +               btail = au_dbtail(dentry);
19520 +               inode->i_op = iop + AuIop_OTHER;
19521 +               init_special_inode(inode, mode, h_inode->i_rdev);
19522 +               break;
19523 +       default:
19524 +               AuIOErr("Unknown file type 0%o\n", mode);
19525 +               err = -EIO;
19526 +               goto out;
19527 +       }
19528 +
19529 +       /* do not set hnotify for whiteouted dirs (SHWH mode) */
19530 +       flags = au_hi_flags(inode, isdir);
19531 +       if (au_opt_test(au_mntflags(dentry->d_sb), SHWH)
19532 +           && au_ftest_hi(flags, HNOTIFY)
19533 +           && dentry->d_name.len > AUFS_WH_PFX_LEN
19534 +           && !memcmp(dentry->d_name.name, AUFS_WH_PFX, AUFS_WH_PFX_LEN))
19535 +               au_fclr_hi(flags, HNOTIFY);
19536 +       iinfo = au_ii(inode);
19537 +       iinfo->ii_btop = btop;
19538 +       iinfo->ii_bbot = btail;
19539 +       for (bindex = btop; bindex <= btail; bindex++) {
19540 +               h_dentry = au_h_dptr(dentry, bindex);
19541 +               if (h_dentry)
19542 +                       au_set_h_iptr(inode, bindex,
19543 +                                     au_igrab(d_inode(h_dentry)), flags);
19544 +       }
19545 +       au_cpup_attr_all(inode, /*force*/1);
19546 +       /*
19547 +        * to force calling aufs_get_acl() every time,
19548 +        * do not call cache_no_acl() for aufs inode.
19549 +        */
19550 +
19551 +out:
19552 +       return err;
19553 +}
19554 +
19555 +/*
19556 + * successful returns with iinfo write_locked
19557 + * minus: errno
19558 + * zero: success, matched
19559 + * plus: no error, but unmatched
19560 + */
19561 +static int reval_inode(struct inode *inode, struct dentry *dentry)
19562 +{
19563 +       int err;
19564 +       unsigned int gen, igflags;
19565 +       aufs_bindex_t bindex, bbot;
19566 +       struct inode *h_inode, *h_dinode;
19567 +       struct dentry *h_dentry;
19568 +
19569 +       /*
19570 +        * before this function, if aufs got any iinfo lock, it must be only
19571 +        * one, the parent dir.
19572 +        * it can happen by UDBA and the obsoleted inode number.
19573 +        */
19574 +       err = -EIO;
19575 +       if (unlikely(inode->i_ino == parent_ino(dentry)))
19576 +               goto out;
19577 +
19578 +       err = 1;
19579 +       ii_write_lock_new_child(inode);
19580 +       h_dentry = au_h_dptr(dentry, au_dbtop(dentry));
19581 +       h_dinode = d_inode(h_dentry);
19582 +       bbot = au_ibbot(inode);
19583 +       for (bindex = au_ibtop(inode); bindex <= bbot; bindex++) {
19584 +               h_inode = au_h_iptr(inode, bindex);
19585 +               if (!h_inode || h_inode != h_dinode)
19586 +                       continue;
19587 +
19588 +               err = 0;
19589 +               gen = au_iigen(inode, &igflags);
19590 +               if (gen == au_digen(dentry)
19591 +                   && !au_ig_ftest(igflags, HALF_REFRESHED))
19592 +                       break;
19593 +
19594 +               /* fully refresh inode using dentry */
19595 +               err = au_refresh_hinode(inode, dentry);
19596 +               if (!err)
19597 +                       au_update_iigen(inode, /*half*/0);
19598 +               break;
19599 +       }
19600 +
19601 +       if (unlikely(err))
19602 +               ii_write_unlock(inode);
19603 +out:
19604 +       return err;
19605 +}
19606 +
19607 +int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
19608 +          unsigned int d_type, ino_t *ino)
19609 +{
19610 +       int err, idx;
19611 +       const int isnondir = d_type != DT_DIR;
19612 +
19613 +       /* prevent hardlinked inode number from race condition */
19614 +       if (isnondir) {
19615 +               err = au_xinondir_enter(sb, bindex, h_ino, &idx);
19616 +               if (unlikely(err))
19617 +                       goto out;
19618 +       }
19619 +
19620 +       err = au_xino_read(sb, bindex, h_ino, ino);
19621 +       if (unlikely(err))
19622 +               goto out_xinondir;
19623 +
19624 +       if (!*ino) {
19625 +               err = -EIO;
19626 +               *ino = au_xino_new_ino(sb);
19627 +               if (unlikely(!*ino))
19628 +                       goto out_xinondir;
19629 +               err = au_xino_write(sb, bindex, h_ino, *ino);
19630 +               if (unlikely(err))
19631 +                       goto out_xinondir;
19632 +       }
19633 +
19634 +out_xinondir:
19635 +       if (isnondir && idx >= 0)
19636 +               au_xinondir_leave(sb, bindex, h_ino, idx);
19637 +out:
19638 +       return err;
19639 +}
19640 +
19641 +/* successful returns with iinfo write_locked */
19642 +/* todo: return with unlocked? */
19643 +struct inode *au_new_inode(struct dentry *dentry, int must_new)
19644 +{
19645 +       struct inode *inode, *h_inode;
19646 +       struct dentry *h_dentry;
19647 +       struct super_block *sb;
19648 +       ino_t h_ino, ino;
19649 +       int err, idx, hlinked;
19650 +       aufs_bindex_t btop;
19651 +
19652 +       sb = dentry->d_sb;
19653 +       btop = au_dbtop(dentry);
19654 +       h_dentry = au_h_dptr(dentry, btop);
19655 +       h_inode = d_inode(h_dentry);
19656 +       h_ino = h_inode->i_ino;
19657 +       hlinked = !d_is_dir(h_dentry) && h_inode->i_nlink > 1;
19658 +
19659 +new_ino:
19660 +       /*
19661 +        * stop 'race'-ing between hardlinks under different
19662 +        * parents.
19663 +        */
19664 +       if (hlinked) {
19665 +               err = au_xinondir_enter(sb, btop, h_ino, &idx);
19666 +               inode = ERR_PTR(err);
19667 +               if (unlikely(err))
19668 +                       goto out;
19669 +       }
19670 +
19671 +       err = au_xino_read(sb, btop, h_ino, &ino);
19672 +       inode = ERR_PTR(err);
19673 +       if (unlikely(err))
19674 +               goto out_xinondir;
19675 +
19676 +       if (!ino) {
19677 +               ino = au_xino_new_ino(sb);
19678 +               if (unlikely(!ino)) {
19679 +                       inode = ERR_PTR(-EIO);
19680 +                       goto out_xinondir;
19681 +               }
19682 +       }
19683 +
19684 +       AuDbg("i%lu\n", (unsigned long)ino);
19685 +       inode = au_iget_locked(sb, ino);
19686 +       err = PTR_ERR(inode);
19687 +       if (IS_ERR(inode))
19688 +               goto out_xinondir;
19689 +
19690 +       AuDbg("%lx, new %d\n", inode->i_state, !!(inode->i_state & I_NEW));
19691 +       if (inode->i_state & I_NEW) {
19692 +               ii_write_lock_new_child(inode);
19693 +               err = set_inode(inode, dentry);
19694 +               if (!err) {
19695 +                       unlock_new_inode(inode);
19696 +                       goto out_xinondir; /* success */
19697 +               }
19698 +
19699 +               /*
19700 +                * iget_failed() calls iput(), but we need to call
19701 +                * ii_write_unlock() after iget_failed(). so dirty hack for
19702 +                * i_count.
19703 +                */
19704 +               atomic_inc(&inode->i_count);
19705 +               iget_failed(inode);
19706 +               ii_write_unlock(inode);
19707 +               au_xino_write(sb, btop, h_ino, /*ino*/0);
19708 +               /* ignore this error */
19709 +               goto out_iput;
19710 +       } else if (!must_new && !IS_DEADDIR(inode) && inode->i_nlink) {
19711 +               /*
19712 +                * horrible race condition between lookup, readdir and copyup
19713 +                * (or something).
19714 +                */
19715 +               if (hlinked && idx >= 0)
19716 +                       au_xinondir_leave(sb, btop, h_ino, idx);
19717 +               err = reval_inode(inode, dentry);
19718 +               if (unlikely(err < 0)) {
19719 +                       hlinked = 0;
19720 +                       goto out_iput;
19721 +               }
19722 +               if (!err)
19723 +                       goto out; /* success */
19724 +               else if (hlinked && idx >= 0) {
19725 +                       err = au_xinondir_enter(sb, btop, h_ino, &idx);
19726 +                       if (unlikely(err)) {
19727 +                               iput(inode);
19728 +                               inode = ERR_PTR(err);
19729 +                               goto out;
19730 +                       }
19731 +               }
19732 +       }
19733 +
19734 +       if (unlikely(au_test_fs_unique_ino(h_inode)))
19735 +               AuWarn1("Warning: Un-notified UDBA or repeatedly renamed dir,"
19736 +                       " b%d, %s, %pd, hi%lu, i%lu.\n",
19737 +                       btop, au_sbtype(h_dentry->d_sb), dentry,
19738 +                       (unsigned long)h_ino, (unsigned long)ino);
19739 +       ino = 0;
19740 +       err = au_xino_write(sb, btop, h_ino, /*ino*/0);
19741 +       if (!err) {
19742 +               iput(inode);
19743 +               if (hlinked && idx >= 0)
19744 +                       au_xinondir_leave(sb, btop, h_ino, idx);
19745 +               goto new_ino;
19746 +       }
19747 +
19748 +out_iput:
19749 +       iput(inode);
19750 +       inode = ERR_PTR(err);
19751 +out_xinondir:
19752 +       if (hlinked && idx >= 0)
19753 +               au_xinondir_leave(sb, btop, h_ino, idx);
19754 +out:
19755 +       return inode;
19756 +}
19757 +
19758 +/* ---------------------------------------------------------------------- */
19759 +
19760 +int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
19761 +              struct inode *inode)
19762 +{
19763 +       int err;
19764 +       struct inode *hi;
19765 +
19766 +       err = au_br_rdonly(au_sbr(sb, bindex));
19767 +
19768 +       /* pseudo-link after flushed may happen out of bounds */
19769 +       if (!err
19770 +           && inode
19771 +           && au_ibtop(inode) <= bindex
19772 +           && bindex <= au_ibbot(inode)) {
19773 +               /*
19774 +                * permission check is unnecessary since vfsub routine
19775 +                * will be called later
19776 +                */
19777 +               hi = au_h_iptr(inode, bindex);
19778 +               if (hi)
19779 +                       err = IS_IMMUTABLE(hi) ? -EROFS : 0;
19780 +       }
19781 +
19782 +       return err;
19783 +}
19784 +
19785 +int au_test_h_perm(struct user_namespace *h_userns, struct inode *h_inode,
19786 +                  int mask)
19787 +{
19788 +       if (uid_eq(current_fsuid(), GLOBAL_ROOT_UID))
19789 +               return 0;
19790 +       return inode_permission(h_userns, h_inode, mask);
19791 +}
19792 +
19793 +int au_test_h_perm_sio(struct user_namespace *h_userns, struct inode *h_inode,
19794 +                      int mask)
19795 +{
19796 +       if (au_test_nfs(h_inode->i_sb)
19797 +           && (mask & MAY_WRITE)
19798 +           && S_ISDIR(h_inode->i_mode))
19799 +               mask |= MAY_READ; /* force permission check */
19800 +       return au_test_h_perm(h_userns, h_inode, mask);
19801 +}
19802 diff -urN /usr/share/empty/fs/aufs/inode.h linux/fs/aufs/inode.h
19803 --- /usr/share/empty/fs/aufs/inode.h    1970-01-01 01:00:00.000000000 +0100
19804 +++ linux/fs/aufs/inode.h       2022-03-21 14:49:05.726633010 +0100
19805 @@ -0,0 +1,705 @@
19806 +/* SPDX-License-Identifier: GPL-2.0 */
19807 +/*
19808 + * Copyright (C) 2005-2021 Junjiro R. Okajima
19809 + *
19810 + * This program, aufs is free software; you can redistribute it and/or modify
19811 + * it under the terms of the GNU General Public License as published by
19812 + * the Free Software Foundation; either version 2 of the License, or
19813 + * (at your option) any later version.
19814 + *
19815 + * This program is distributed in the hope that it will be useful,
19816 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
19817 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19818 + * GNU General Public License for more details.
19819 + *
19820 + * You should have received a copy of the GNU General Public License
19821 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19822 + */
19823 +
19824 +/*
19825 + * inode operations
19826 + */
19827 +
19828 +#ifndef __AUFS_INODE_H__
19829 +#define __AUFS_INODE_H__
19830 +
19831 +#ifdef __KERNEL__
19832 +
19833 +#include <linux/fsnotify.h>
19834 +#include "rwsem.h"
19835 +
19836 +struct vfsmount;
19837 +
19838 +struct au_hnotify {
19839 +#ifdef CONFIG_AUFS_HNOTIFY
19840 +#ifdef CONFIG_AUFS_HFSNOTIFY
19841 +       /* never use fsnotify_add_vfsmount_mark() */
19842 +       struct fsnotify_mark            hn_mark;
19843 +#endif
19844 +       struct inode            *hn_aufs_inode; /* no get/put */
19845 +       struct rcu_head         rcu;
19846 +#endif
19847 +} ____cacheline_aligned_in_smp;
19848 +
19849 +struct au_hinode {
19850 +       struct inode            *hi_inode;
19851 +       aufs_bindex_t           hi_id;
19852 +#ifdef CONFIG_AUFS_HNOTIFY
19853 +       struct au_hnotify       *hi_notify;
19854 +#endif
19855 +
19856 +       /* reference to the copied-up whiteout with get/put */
19857 +       struct dentry           *hi_whdentry;
19858 +};
19859 +
19860 +/* ig_flags */
19861 +#define AuIG_HALF_REFRESHED            1
19862 +#define au_ig_ftest(flags, name)       ((flags) & AuIG_##name)
19863 +#define au_ig_fset(flags, name) \
19864 +       do { (flags) |= AuIG_##name; } while (0)
19865 +#define au_ig_fclr(flags, name) \
19866 +       do { (flags) &= ~AuIG_##name; } while (0)
19867 +
19868 +struct au_iigen {
19869 +       spinlock_t      ig_spin;
19870 +       __u32           ig_generation, ig_flags;
19871 +};
19872 +
19873 +struct au_vdir;
19874 +struct au_iinfo {
19875 +       struct au_iigen         ii_generation;
19876 +       struct super_block      *ii_hsb1;       /* no get/put */
19877 +
19878 +       struct au_rwsem         ii_rwsem;
19879 +       aufs_bindex_t           ii_btop, ii_bbot;
19880 +       __u32                   ii_higen;
19881 +       struct au_hinode        *ii_hinode;
19882 +       struct au_vdir          *ii_vdir;
19883 +};
19884 +
19885 +struct au_icntnr {
19886 +       struct au_iinfo         iinfo;
19887 +       struct inode            vfs_inode;
19888 +       struct hlist_bl_node    plink;
19889 +       struct rcu_head         rcu;
19890 +} ____cacheline_aligned_in_smp;
19891 +
19892 +/* au_pin flags */
19893 +#define AuPin_DI_LOCKED                1
19894 +#define AuPin_MNT_WRITE                (1 << 1)
19895 +#define au_ftest_pin(flags, name)      ((flags) & AuPin_##name)
19896 +#define au_fset_pin(flags, name) \
19897 +       do { (flags) |= AuPin_##name; } while (0)
19898 +#define au_fclr_pin(flags, name) \
19899 +       do { (flags) &= ~AuPin_##name; } while (0)
19900 +
19901 +struct au_pin {
19902 +       /* input */
19903 +       struct dentry *dentry;
19904 +       unsigned int udba;
19905 +       unsigned char lsc_di, lsc_hi, flags;
19906 +       aufs_bindex_t bindex;
19907 +
19908 +       /* output */
19909 +       struct dentry *parent;
19910 +       struct au_hinode *hdir;
19911 +       struct vfsmount *h_mnt;
19912 +
19913 +       /* temporary unlock/relock for copyup */
19914 +       struct dentry *h_dentry, *h_parent;
19915 +       struct au_branch *br;
19916 +       struct task_struct *task;
19917 +};
19918 +
19919 +void au_pin_hdir_unlock(struct au_pin *p);
19920 +int au_pin_hdir_lock(struct au_pin *p);
19921 +int au_pin_hdir_relock(struct au_pin *p);
19922 +void au_pin_hdir_acquire_nest(struct au_pin *p);
19923 +void au_pin_hdir_release(struct au_pin *p);
19924 +
19925 +/* ---------------------------------------------------------------------- */
19926 +
19927 +static inline struct au_iinfo *au_ii(struct inode *inode)
19928 +{
19929 +       BUG_ON(is_bad_inode(inode));
19930 +       return &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
19931 +}
19932 +
19933 +/* ---------------------------------------------------------------------- */
19934 +
19935 +/* inode.c */
19936 +struct inode *au_igrab(struct inode *inode);
19937 +void au_refresh_iop(struct inode *inode, int force_getattr);
19938 +int au_refresh_hinode_self(struct inode *inode);
19939 +int au_refresh_hinode(struct inode *inode, struct dentry *dentry);
19940 +int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
19941 +          unsigned int d_type, ino_t *ino);
19942 +struct inode *au_new_inode(struct dentry *dentry, int must_new);
19943 +int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
19944 +              struct inode *inode);
19945 +int au_test_h_perm(struct user_namespace *h_userns, struct inode *h_inode,
19946 +                  int mask);
19947 +int au_test_h_perm_sio(struct user_namespace *h_userns, struct inode *h_inode,
19948 +                      int mask);
19949 +
19950 +static inline int au_wh_ino(struct super_block *sb, aufs_bindex_t bindex,
19951 +                           ino_t h_ino, unsigned int d_type, ino_t *ino)
19952 +{
19953 +#ifdef CONFIG_AUFS_SHWH
19954 +       return au_ino(sb, bindex, h_ino, d_type, ino);
19955 +#else
19956 +       return 0;
19957 +#endif
19958 +}
19959 +
19960 +/* i_op.c */
19961 +enum {
19962 +       AuIop_SYMLINK,
19963 +       AuIop_DIR,
19964 +       AuIop_OTHER,
19965 +       AuIop_Last
19966 +};
19967 +extern struct inode_operations aufs_iop[AuIop_Last], /* not const */
19968 +       aufs_iop_nogetattr[AuIop_Last];
19969 +
19970 +/* au_wr_dir flags */
19971 +#define AuWrDir_ADD_ENTRY      1
19972 +#define AuWrDir_ISDIR          (1 << 1)
19973 +#define AuWrDir_TMPFILE                (1 << 2)
19974 +#define au_ftest_wrdir(flags, name)    ((flags) & AuWrDir_##name)
19975 +#define au_fset_wrdir(flags, name) \
19976 +       do { (flags) |= AuWrDir_##name; } while (0)
19977 +#define au_fclr_wrdir(flags, name) \
19978 +       do { (flags) &= ~AuWrDir_##name; } while (0)
19979 +
19980 +struct au_wr_dir_args {
19981 +       aufs_bindex_t force_btgt;
19982 +       unsigned char flags;
19983 +};
19984 +int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
19985 +             struct au_wr_dir_args *args);
19986 +
19987 +struct dentry *au_pinned_h_parent(struct au_pin *pin);
19988 +void au_pin_init(struct au_pin *pin, struct dentry *dentry,
19989 +                aufs_bindex_t bindex, int lsc_di, int lsc_hi,
19990 +                unsigned int udba, unsigned char flags);
19991 +int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
19992 +          unsigned int udba, unsigned char flags) __must_check;
19993 +int au_do_pin(struct au_pin *pin) __must_check;
19994 +void au_unpin(struct au_pin *pin);
19995 +int au_reval_for_attr(struct dentry *dentry, unsigned int sigen);
19996 +
19997 +#define AuIcpup_DID_CPUP       1
19998 +#define au_ftest_icpup(flags, name)    ((flags) & AuIcpup_##name)
19999 +#define au_fset_icpup(flags, name) \
20000 +       do { (flags) |= AuIcpup_##name; } while (0)
20001 +#define au_fclr_icpup(flags, name) \
20002 +       do { (flags) &= ~AuIcpup_##name; } while (0)
20003 +
20004 +struct au_icpup_args {
20005 +       unsigned char flags;
20006 +       unsigned char pin_flags;
20007 +       aufs_bindex_t btgt;
20008 +       unsigned int udba;
20009 +       struct au_pin pin;
20010 +       struct path h_path;
20011 +       struct inode *h_inode;
20012 +};
20013 +
20014 +int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
20015 +                    struct au_icpup_args *a);
20016 +
20017 +int au_h_path_getattr(struct dentry *dentry, struct inode *inode, int force,
20018 +                     struct path *h_path, int locked);
20019 +
20020 +/* i_op_add.c */
20021 +int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
20022 +              struct dentry *h_parent, int isdir);
20023 +int aufs_mknod(struct user_namespace *userns, struct inode *dir,
20024 +              struct dentry *dentry, umode_t mode, dev_t dev);
20025 +int aufs_symlink(struct user_namespace *userns, struct inode *dir,
20026 +                struct dentry *dentry, const char *symname);
20027 +int aufs_create(struct user_namespace *userns, struct inode *dir,
20028 +               struct dentry *dentry, umode_t mode, bool want_excl);
20029 +struct vfsub_aopen_args;
20030 +int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
20031 +                      struct vfsub_aopen_args *args);
20032 +int aufs_tmpfile(struct user_namespace *userns, struct inode *dir,
20033 +                struct dentry *dentry, umode_t mode);
20034 +int aufs_link(struct dentry *src_dentry, struct inode *dir,
20035 +             struct dentry *dentry);
20036 +int aufs_mkdir(struct user_namespace *userns, struct inode *dir,
20037 +              struct dentry *dentry, umode_t mode);
20038 +
20039 +/* i_op_del.c */
20040 +int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup);
20041 +int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
20042 +              struct dentry *h_parent, int isdir);
20043 +int aufs_unlink(struct inode *dir, struct dentry *dentry);
20044 +int aufs_rmdir(struct inode *dir, struct dentry *dentry);
20045 +
20046 +/* i_op_ren.c */
20047 +int au_wbr(struct dentry *dentry, aufs_bindex_t btgt);
20048 +int aufs_rename(struct user_namespace *userns,
20049 +               struct inode *_src_dir, struct dentry *_src_dentry,
20050 +               struct inode *_dst_dir, struct dentry *_dst_dentry,
20051 +               unsigned int _flags);
20052 +
20053 +/* iinfo.c */
20054 +struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex);
20055 +void au_hiput(struct au_hinode *hinode);
20056 +void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
20057 +                 struct dentry *h_wh);
20058 +unsigned int au_hi_flags(struct inode *inode, int isdir);
20059 +
20060 +/* hinode flags */
20061 +#define AuHi_XINO      1
20062 +#define AuHi_HNOTIFY   (1 << 1)
20063 +#define au_ftest_hi(flags, name)       ((flags) & AuHi_##name)
20064 +#define au_fset_hi(flags, name) \
20065 +       do { (flags) |= AuHi_##name; } while (0)
20066 +#define au_fclr_hi(flags, name) \
20067 +       do { (flags) &= ~AuHi_##name; } while (0)
20068 +
20069 +#ifndef CONFIG_AUFS_HNOTIFY
20070 +#undef AuHi_HNOTIFY
20071 +#define AuHi_HNOTIFY   0
20072 +#endif
20073 +
20074 +void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
20075 +                  struct inode *h_inode, unsigned int flags);
20076 +
20077 +void au_update_iigen(struct inode *inode, int half);
20078 +void au_update_ibrange(struct inode *inode, int do_put_zero);
20079 +
20080 +void au_icntnr_init_once(void *_c);
20081 +void au_hinode_init(struct au_hinode *hinode);
20082 +int au_iinfo_init(struct inode *inode);
20083 +void au_iinfo_fin(struct inode *inode);
20084 +int au_hinode_realloc(struct au_iinfo *iinfo, int nbr, int may_shrink);
20085 +
20086 +#ifdef CONFIG_PROC_FS
20087 +/* plink.c */
20088 +int au_plink_maint(struct super_block *sb, int flags);
20089 +struct au_sbinfo;
20090 +void au_plink_maint_leave(struct au_sbinfo *sbinfo);
20091 +int au_plink_maint_enter(struct super_block *sb);
20092 +#ifdef CONFIG_AUFS_DEBUG
20093 +void au_plink_list(struct super_block *sb);
20094 +#else
20095 +AuStubVoid(au_plink_list, struct super_block *sb)
20096 +#endif
20097 +int au_plink_test(struct inode *inode);
20098 +struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex);
20099 +void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
20100 +                    struct dentry *h_dentry);
20101 +void au_plink_put(struct super_block *sb, int verbose);
20102 +void au_plink_clean(struct super_block *sb, int verbose);
20103 +void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id);
20104 +#else
20105 +AuStubInt0(au_plink_maint, struct super_block *sb, int flags);
20106 +AuStubVoid(au_plink_maint_leave, struct au_sbinfo *sbinfo);
20107 +AuStubInt0(au_plink_maint_enter, struct super_block *sb);
20108 +AuStubVoid(au_plink_list, struct super_block *sb);
20109 +AuStubInt0(au_plink_test, struct inode *inode);
20110 +AuStub(struct dentry *, au_plink_lkup, return NULL,
20111 +       struct inode *inode, aufs_bindex_t bindex);
20112 +AuStubVoid(au_plink_append, struct inode *inode, aufs_bindex_t bindex,
20113 +          struct dentry *h_dentry);
20114 +AuStubVoid(au_plink_put, struct super_block *sb, int verbose);
20115 +AuStubVoid(au_plink_clean, struct super_block *sb, int verbose);
20116 +AuStubVoid(au_plink_half_refresh, struct super_block *sb, aufs_bindex_t br_id);
20117 +#endif /* CONFIG_PROC_FS */
20118 +
20119 +#ifdef CONFIG_AUFS_XATTR
20120 +/* xattr.c */
20121 +int au_cpup_xattr(struct path *h_dst, struct path *h_src, int ignore_flags,
20122 +                 unsigned int verbose);
20123 +ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size);
20124 +void au_xattr_init(struct super_block *sb);
20125 +#else
20126 +AuStubInt0(au_cpup_xattr, struct path *h_dst, struct path *h_src,
20127 +          int ignore_flags, unsigned int verbose);
20128 +AuStubVoid(au_xattr_init, struct super_block *sb);
20129 +#endif
20130 +
20131 +#ifdef CONFIG_FS_POSIX_ACL
20132 +struct posix_acl *aufs_get_acl(struct inode *inode, int type, bool rcu);
20133 +int aufs_set_acl(struct user_namespace *userns, struct inode *inode,
20134 +                struct posix_acl *acl, int type);
20135 +#endif
20136 +
20137 +#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
20138 +enum {
20139 +       AU_XATTR_SET,
20140 +       AU_ACL_SET
20141 +};
20142 +
20143 +struct au_sxattr {
20144 +       int type;
20145 +       union {
20146 +               struct {
20147 +                       const char      *name;
20148 +                       const void      *value;
20149 +                       size_t          size;
20150 +                       int             flags;
20151 +               } set;
20152 +               struct {
20153 +                       struct posix_acl *acl;
20154 +                       int             type;
20155 +               } acl_set;
20156 +       } u;
20157 +};
20158 +ssize_t au_sxattr(struct dentry *dentry, struct inode *inode,
20159 +                 struct au_sxattr *arg);
20160 +#endif
20161 +
20162 +/* ---------------------------------------------------------------------- */
20163 +
20164 +/* lock subclass for iinfo */
20165 +enum {
20166 +       AuLsc_II_CHILD,         /* child first */
20167 +       AuLsc_II_CHILD2,        /* rename(2), link(2), and cpup at hnotify */
20168 +       AuLsc_II_CHILD3,        /* copyup dirs */
20169 +       AuLsc_II_PARENT,        /* see AuLsc_I_PARENT in vfsub.h */
20170 +       AuLsc_II_PARENT2,
20171 +       AuLsc_II_PARENT3,       /* copyup dirs */
20172 +       AuLsc_II_NEW_CHILD
20173 +};
20174 +
20175 +/*
20176 + * ii_read_lock_child, ii_write_lock_child,
20177 + * ii_read_lock_child2, ii_write_lock_child2,
20178 + * ii_read_lock_child3, ii_write_lock_child3,
20179 + * ii_read_lock_parent, ii_write_lock_parent,
20180 + * ii_read_lock_parent2, ii_write_lock_parent2,
20181 + * ii_read_lock_parent3, ii_write_lock_parent3,
20182 + * ii_read_lock_new_child, ii_write_lock_new_child,
20183 + */
20184 +#define AuReadLockFunc(name, lsc) \
20185 +static inline void ii_read_lock_##name(struct inode *i) \
20186 +{ \
20187 +       au_rw_read_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
20188 +}
20189 +
20190 +#define AuWriteLockFunc(name, lsc) \
20191 +static inline void ii_write_lock_##name(struct inode *i) \
20192 +{ \
20193 +       au_rw_write_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
20194 +}
20195 +
20196 +#define AuRWLockFuncs(name, lsc) \
20197 +       AuReadLockFunc(name, lsc) \
20198 +       AuWriteLockFunc(name, lsc)
20199 +
20200 +AuRWLockFuncs(child, CHILD);
20201 +AuRWLockFuncs(child2, CHILD2);
20202 +AuRWLockFuncs(child3, CHILD3);
20203 +AuRWLockFuncs(parent, PARENT);
20204 +AuRWLockFuncs(parent2, PARENT2);
20205 +AuRWLockFuncs(parent3, PARENT3);
20206 +AuRWLockFuncs(new_child, NEW_CHILD);
20207 +
20208 +#undef AuReadLockFunc
20209 +#undef AuWriteLockFunc
20210 +#undef AuRWLockFuncs
20211 +
20212 +#define ii_read_unlock(i)      au_rw_read_unlock(&au_ii(i)->ii_rwsem)
20213 +#define ii_write_unlock(i)     au_rw_write_unlock(&au_ii(i)->ii_rwsem)
20214 +#define ii_downgrade_lock(i)   au_rw_dgrade_lock(&au_ii(i)->ii_rwsem)
20215 +
20216 +#define IiMustNoWaiters(i)     AuRwMustNoWaiters(&au_ii(i)->ii_rwsem)
20217 +#define IiMustAnyLock(i)       AuRwMustAnyLock(&au_ii(i)->ii_rwsem)
20218 +#define IiMustWriteLock(i)     AuRwMustWriteLock(&au_ii(i)->ii_rwsem)
20219 +
20220 +/* ---------------------------------------------------------------------- */
20221 +
20222 +static inline void au_icntnr_init(struct au_icntnr *c)
20223 +{
20224 +#ifdef CONFIG_AUFS_DEBUG
20225 +       c->vfs_inode.i_mode = 0;
20226 +#endif
20227 +}
20228 +
20229 +static inline unsigned int au_iigen(struct inode *inode, unsigned int *igflags)
20230 +{
20231 +       unsigned int gen;
20232 +       struct au_iinfo *iinfo;
20233 +       struct au_iigen *iigen;
20234 +
20235 +       iinfo = au_ii(inode);
20236 +       iigen = &iinfo->ii_generation;
20237 +       spin_lock(&iigen->ig_spin);
20238 +       if (igflags)
20239 +               *igflags = iigen->ig_flags;
20240 +       gen = iigen->ig_generation;
20241 +       spin_unlock(&iigen->ig_spin);
20242 +
20243 +       return gen;
20244 +}
20245 +
20246 +/* tiny test for inode number */
20247 +/* tmpfs generation is too rough */
20248 +static inline int au_test_higen(struct inode *inode, struct inode *h_inode)
20249 +{
20250 +       struct au_iinfo *iinfo;
20251 +
20252 +       iinfo = au_ii(inode);
20253 +       AuRwMustAnyLock(&iinfo->ii_rwsem);
20254 +       return !(iinfo->ii_hsb1 == h_inode->i_sb
20255 +                && iinfo->ii_higen == h_inode->i_generation);
20256 +}
20257 +
20258 +static inline void au_iigen_dec(struct inode *inode)
20259 +{
20260 +       struct au_iinfo *iinfo;
20261 +       struct au_iigen *iigen;
20262 +
20263 +       iinfo = au_ii(inode);
20264 +       iigen = &iinfo->ii_generation;
20265 +       spin_lock(&iigen->ig_spin);
20266 +       iigen->ig_generation--;
20267 +       spin_unlock(&iigen->ig_spin);
20268 +}
20269 +
20270 +static inline int au_iigen_test(struct inode *inode, unsigned int sigen)
20271 +{
20272 +       int err;
20273 +
20274 +       err = 0;
20275 +       if (unlikely(inode && au_iigen(inode, NULL) != sigen))
20276 +               err = -EIO;
20277 +
20278 +       return err;
20279 +}
20280 +
20281 +/* ---------------------------------------------------------------------- */
20282 +
20283 +static inline struct au_hinode *au_hinode(struct au_iinfo *iinfo,
20284 +                                         aufs_bindex_t bindex)
20285 +{
20286 +       return iinfo->ii_hinode + bindex;
20287 +}
20288 +
20289 +static inline int au_is_bad_inode(struct inode *inode)
20290 +{
20291 +       return !!(is_bad_inode(inode) || !au_hinode(au_ii(inode), 0));
20292 +}
20293 +
20294 +static inline aufs_bindex_t au_ii_br_id(struct inode *inode,
20295 +                                       aufs_bindex_t bindex)
20296 +{
20297 +       IiMustAnyLock(inode);
20298 +       return au_hinode(au_ii(inode), bindex)->hi_id;
20299 +}
20300 +
20301 +static inline aufs_bindex_t au_ibtop(struct inode *inode)
20302 +{
20303 +       IiMustAnyLock(inode);
20304 +       return au_ii(inode)->ii_btop;
20305 +}
20306 +
20307 +static inline aufs_bindex_t au_ibbot(struct inode *inode)
20308 +{
20309 +       IiMustAnyLock(inode);
20310 +       return au_ii(inode)->ii_bbot;
20311 +}
20312 +
20313 +static inline struct au_vdir *au_ivdir(struct inode *inode)
20314 +{
20315 +       IiMustAnyLock(inode);
20316 +       return au_ii(inode)->ii_vdir;
20317 +}
20318 +
20319 +static inline struct dentry *au_hi_wh(struct inode *inode, aufs_bindex_t bindex)
20320 +{
20321 +       IiMustAnyLock(inode);
20322 +       return au_hinode(au_ii(inode), bindex)->hi_whdentry;
20323 +}
20324 +
20325 +static inline void au_set_ibtop(struct inode *inode, aufs_bindex_t bindex)
20326 +{
20327 +       IiMustWriteLock(inode);
20328 +       au_ii(inode)->ii_btop = bindex;
20329 +}
20330 +
20331 +static inline void au_set_ibbot(struct inode *inode, aufs_bindex_t bindex)
20332 +{
20333 +       IiMustWriteLock(inode);
20334 +       au_ii(inode)->ii_bbot = bindex;
20335 +}
20336 +
20337 +static inline void au_set_ivdir(struct inode *inode, struct au_vdir *vdir)
20338 +{
20339 +       IiMustWriteLock(inode);
20340 +       au_ii(inode)->ii_vdir = vdir;
20341 +}
20342 +
20343 +static inline struct au_hinode *au_hi(struct inode *inode, aufs_bindex_t bindex)
20344 +{
20345 +       IiMustAnyLock(inode);
20346 +       return au_hinode(au_ii(inode), bindex);
20347 +}
20348 +
20349 +/* ---------------------------------------------------------------------- */
20350 +
20351 +static inline struct dentry *au_pinned_parent(struct au_pin *pin)
20352 +{
20353 +       if (pin)
20354 +               return pin->parent;
20355 +       return NULL;
20356 +}
20357 +
20358 +static inline struct inode *au_pinned_h_dir(struct au_pin *pin)
20359 +{
20360 +       if (pin && pin->hdir)
20361 +               return pin->hdir->hi_inode;
20362 +       return NULL;
20363 +}
20364 +
20365 +static inline struct au_hinode *au_pinned_hdir(struct au_pin *pin)
20366 +{
20367 +       if (pin)
20368 +               return pin->hdir;
20369 +       return NULL;
20370 +}
20371 +
20372 +static inline void au_pin_set_dentry(struct au_pin *pin, struct dentry *dentry)
20373 +{
20374 +       if (pin)
20375 +               pin->dentry = dentry;
20376 +}
20377 +
20378 +static inline void au_pin_set_parent_lflag(struct au_pin *pin,
20379 +                                          unsigned char lflag)
20380 +{
20381 +       if (pin) {
20382 +               if (lflag)
20383 +                       au_fset_pin(pin->flags, DI_LOCKED);
20384 +               else
20385 +                       au_fclr_pin(pin->flags, DI_LOCKED);
20386 +       }
20387 +}
20388 +
20389 +#if 0 /* reserved */
20390 +static inline void au_pin_set_parent(struct au_pin *pin, struct dentry *parent)
20391 +{
20392 +       if (pin) {
20393 +               dput(pin->parent);
20394 +               pin->parent = dget(parent);
20395 +       }
20396 +}
20397 +#endif
20398 +
20399 +/* ---------------------------------------------------------------------- */
20400 +
20401 +struct au_branch;
20402 +#ifdef CONFIG_AUFS_HNOTIFY
20403 +struct au_hnotify_op {
20404 +       void (*ctl)(struct au_hinode *hinode, int do_set);
20405 +       int (*alloc)(struct au_hinode *hinode);
20406 +
20407 +       /*
20408 +        * if it returns true, the caller should free hinode->hi_notify,
20409 +        * otherwise ->free() frees it.
20410 +        */
20411 +       int (*free)(struct au_hinode *hinode,
20412 +                   struct au_hnotify *hn) __must_check;
20413 +
20414 +       void (*fin)(void);
20415 +       int (*init)(void);
20416 +
20417 +       int (*reset_br)(unsigned int udba, struct au_branch *br, int perm);
20418 +       void (*fin_br)(struct au_branch *br);
20419 +       int (*init_br)(struct au_branch *br, int perm);
20420 +};
20421 +
20422 +/* hnotify.c */
20423 +int au_hn_alloc(struct au_hinode *hinode, struct inode *inode);
20424 +void au_hn_free(struct au_hinode *hinode);
20425 +void au_hn_ctl(struct au_hinode *hinode, int do_set);
20426 +void au_hn_reset(struct inode *inode, unsigned int flags);
20427 +int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
20428 +              const struct qstr *h_child_qstr, struct inode *h_child_inode);
20429 +int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm);
20430 +int au_hnotify_init_br(struct au_branch *br, int perm);
20431 +void au_hnotify_fin_br(struct au_branch *br);
20432 +int __init au_hnotify_init(void);
20433 +void au_hnotify_fin(void);
20434 +
20435 +/* hfsnotify.c */
20436 +extern const struct au_hnotify_op au_hnotify_op;
20437 +
20438 +static inline
20439 +void au_hn_init(struct au_hinode *hinode)
20440 +{
20441 +       hinode->hi_notify = NULL;
20442 +}
20443 +
20444 +static inline struct au_hnotify *au_hn(struct au_hinode *hinode)
20445 +{
20446 +       return hinode->hi_notify;
20447 +}
20448 +
20449 +#else
20450 +AuStub(int, au_hn_alloc, return -EOPNOTSUPP,
20451 +       struct au_hinode *hinode __maybe_unused,
20452 +       struct inode *inode __maybe_unused)
20453 +AuStub(struct au_hnotify *, au_hn, return NULL, struct au_hinode *hinode)
20454 +AuStubVoid(au_hn_free, struct au_hinode *hinode __maybe_unused)
20455 +AuStubVoid(au_hn_ctl, struct au_hinode *hinode __maybe_unused,
20456 +          int do_set __maybe_unused)
20457 +AuStubVoid(au_hn_reset, struct inode *inode __maybe_unused,
20458 +          unsigned int flags __maybe_unused)
20459 +AuStubInt0(au_hnotify_reset_br, unsigned int udba __maybe_unused,
20460 +          struct au_branch *br __maybe_unused,
20461 +          int perm __maybe_unused)
20462 +AuStubInt0(au_hnotify_init_br, struct au_branch *br __maybe_unused,
20463 +          int perm __maybe_unused)
20464 +AuStubVoid(au_hnotify_fin_br, struct au_branch *br __maybe_unused)
20465 +AuStubInt0(__init au_hnotify_init, void)
20466 +AuStubVoid(au_hnotify_fin, void)
20467 +AuStubVoid(au_hn_init, struct au_hinode *hinode __maybe_unused)
20468 +#endif /* CONFIG_AUFS_HNOTIFY */
20469 +
20470 +static inline void au_hn_suspend(struct au_hinode *hdir)
20471 +{
20472 +       au_hn_ctl(hdir, /*do_set*/0);
20473 +}
20474 +
20475 +static inline void au_hn_resume(struct au_hinode *hdir)
20476 +{
20477 +       au_hn_ctl(hdir, /*do_set*/1);
20478 +}
20479 +
20480 +static inline void au_hn_inode_lock(struct au_hinode *hdir)
20481 +{
20482 +       inode_lock(hdir->hi_inode);
20483 +       au_hn_suspend(hdir);
20484 +}
20485 +
20486 +static inline void au_hn_inode_lock_nested(struct au_hinode *hdir,
20487 +                                         unsigned int sc __maybe_unused)
20488 +{
20489 +       inode_lock_nested(hdir->hi_inode, sc);
20490 +       au_hn_suspend(hdir);
20491 +}
20492 +
20493 +#if 0 /* unused */
20494 +#include "vfsub.h"
20495 +static inline void au_hn_inode_lock_shared_nested(struct au_hinode *hdir,
20496 +                                                 unsigned int sc)
20497 +{
20498 +       inode_lock_shared_nested(hdir->hi_inode, sc);
20499 +       au_hn_suspend(hdir);
20500 +}
20501 +#endif
20502 +
20503 +static inline void au_hn_inode_unlock(struct au_hinode *hdir)
20504 +{
20505 +       au_hn_resume(hdir);
20506 +       inode_unlock(hdir->hi_inode);
20507 +}
20508 +
20509 +#endif /* __KERNEL__ */
20510 +#endif /* __AUFS_INODE_H__ */
20511 diff -urN /usr/share/empty/fs/aufs/ioctl.c linux/fs/aufs/ioctl.c
20512 --- /usr/share/empty/fs/aufs/ioctl.c    1970-01-01 01:00:00.000000000 +0100
20513 +++ linux/fs/aufs/ioctl.c       2022-03-21 14:49:05.726633010 +0100
20514 @@ -0,0 +1,220 @@
20515 +// SPDX-License-Identifier: GPL-2.0
20516 +/*
20517 + * Copyright (C) 2005-2021 Junjiro R. Okajima
20518 + *
20519 + * This program, aufs is free software; you can redistribute it and/or modify
20520 + * it under the terms of the GNU General Public License as published by
20521 + * the Free Software Foundation; either version 2 of the License, or
20522 + * (at your option) any later version.
20523 + *
20524 + * This program is distributed in the hope that it will be useful,
20525 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
20526 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20527 + * GNU General Public License for more details.
20528 + *
20529 + * You should have received a copy of the GNU General Public License
20530 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20531 + */
20532 +
20533 +/*
20534 + * ioctl
20535 + * plink-management and readdir in userspace.
20536 + * assist the pathconf(3) wrapper library.
20537 + * move-down
20538 + * File-based Hierarchical Storage Management.
20539 + */
20540 +
20541 +#include <linux/compat.h>
20542 +#include <linux/file.h>
20543 +#include "aufs.h"
20544 +
20545 +static int au_wbr_fd(struct path *path, struct aufs_wbr_fd __user *arg)
20546 +{
20547 +       int err, fd;
20548 +       aufs_bindex_t wbi, bindex, bbot;
20549 +       struct file *h_file;
20550 +       struct super_block *sb;
20551 +       struct dentry *root;
20552 +       struct au_branch *br;
20553 +       struct aufs_wbr_fd wbrfd = {
20554 +               .oflags = au_dir_roflags,
20555 +               .brid   = -1
20556 +       };
20557 +       const int valid = O_RDONLY | O_NONBLOCK | O_LARGEFILE | O_DIRECTORY
20558 +               | O_NOATIME | O_CLOEXEC;
20559 +
20560 +       AuDebugOn(wbrfd.oflags & ~valid);
20561 +
20562 +       if (arg) {
20563 +               err = copy_from_user(&wbrfd, arg, sizeof(wbrfd));
20564 +               if (unlikely(err)) {
20565 +                       err = -EFAULT;
20566 +                       goto out;
20567 +               }
20568 +
20569 +               err = -EINVAL;
20570 +               AuDbg("wbrfd{0%o, %d}\n", wbrfd.oflags, wbrfd.brid);
20571 +               wbrfd.oflags |= au_dir_roflags;
20572 +               AuDbg("0%o\n", wbrfd.oflags);
20573 +               if (unlikely(wbrfd.oflags & ~valid))
20574 +                       goto out;
20575 +       }
20576 +
20577 +       fd = get_unused_fd_flags(0);
20578 +       err = fd;
20579 +       if (unlikely(fd < 0))
20580 +               goto out;
20581 +
20582 +       h_file = ERR_PTR(-EINVAL);
20583 +       wbi = 0;
20584 +       br = NULL;
20585 +       sb = path->dentry->d_sb;
20586 +       root = sb->s_root;
20587 +       aufs_read_lock(root, AuLock_IR);
20588 +       bbot = au_sbbot(sb);
20589 +       if (wbrfd.brid >= 0) {
20590 +               wbi = au_br_index(sb, wbrfd.brid);
20591 +               if (unlikely(wbi < 0 || wbi > bbot))
20592 +                       goto out_unlock;
20593 +       }
20594 +
20595 +       h_file = ERR_PTR(-ENOENT);
20596 +       br = au_sbr(sb, wbi);
20597 +       if (!au_br_writable(br->br_perm)) {
20598 +               if (arg)
20599 +                       goto out_unlock;
20600 +
20601 +               bindex = wbi + 1;
20602 +               wbi = -1;
20603 +               for (; bindex <= bbot; bindex++) {
20604 +                       br = au_sbr(sb, bindex);
20605 +                       if (au_br_writable(br->br_perm)) {
20606 +                               wbi = bindex;
20607 +                               br = au_sbr(sb, wbi);
20608 +                               break;
20609 +                       }
20610 +               }
20611 +       }
20612 +       AuDbg("wbi %d\n", wbi);
20613 +       if (wbi >= 0)
20614 +               h_file = au_h_open(root, wbi, wbrfd.oflags, NULL,
20615 +                                  /*force_wr*/0);
20616 +
20617 +out_unlock:
20618 +       aufs_read_unlock(root, AuLock_IR);
20619 +       err = PTR_ERR(h_file);
20620 +       if (IS_ERR(h_file))
20621 +               goto out_fd;
20622 +
20623 +       au_lcnt_dec(&br->br_nfiles); /* cf. au_h_open() */
20624 +       fd_install(fd, h_file);
20625 +       err = fd;
20626 +       goto out; /* success */
20627 +
20628 +out_fd:
20629 +       put_unused_fd(fd);
20630 +out:
20631 +       AuTraceErr(err);
20632 +       return err;
20633 +}
20634 +
20635 +/* ---------------------------------------------------------------------- */
20636 +
20637 +long aufs_ioctl_dir(struct file *file, unsigned int cmd, unsigned long arg)
20638 +{
20639 +       long err;
20640 +       struct dentry *dentry;
20641 +
20642 +       switch (cmd) {
20643 +       case AUFS_CTL_RDU:
20644 +       case AUFS_CTL_RDU_INO:
20645 +               err = au_rdu_ioctl(file, cmd, arg);
20646 +               break;
20647 +
20648 +       case AUFS_CTL_WBR_FD:
20649 +               err = au_wbr_fd(&file->f_path, (void __user *)arg);
20650 +               break;
20651 +
20652 +       case AUFS_CTL_IBUSY:
20653 +               err = au_ibusy_ioctl(file, arg);
20654 +               break;
20655 +
20656 +       case AUFS_CTL_BRINFO:
20657 +               err = au_brinfo_ioctl(file, arg);
20658 +               break;
20659 +
20660 +       case AUFS_CTL_FHSM_FD:
20661 +               dentry = file->f_path.dentry;
20662 +               if (IS_ROOT(dentry))
20663 +                       err = au_fhsm_fd(dentry->d_sb, arg);
20664 +               else
20665 +                       err = -ENOTTY;
20666 +               break;
20667 +
20668 +       default:
20669 +               /* do not call the lower */
20670 +               AuDbg("0x%x\n", cmd);
20671 +               err = -ENOTTY;
20672 +       }
20673 +
20674 +       AuTraceErr(err);
20675 +       return err;
20676 +}
20677 +
20678 +long aufs_ioctl_nondir(struct file *file, unsigned int cmd, unsigned long arg)
20679 +{
20680 +       long err;
20681 +
20682 +       switch (cmd) {
20683 +       case AUFS_CTL_MVDOWN:
20684 +               err = au_mvdown(file->f_path.dentry, (void __user *)arg);
20685 +               break;
20686 +
20687 +       case AUFS_CTL_WBR_FD:
20688 +               err = au_wbr_fd(&file->f_path, (void __user *)arg);
20689 +               break;
20690 +
20691 +       default:
20692 +               /* do not call the lower */
20693 +               AuDbg("0x%x\n", cmd);
20694 +               err = -ENOTTY;
20695 +       }
20696 +
20697 +       AuTraceErr(err);
20698 +       return err;
20699 +}
20700 +
20701 +#ifdef CONFIG_COMPAT
20702 +long aufs_compat_ioctl_dir(struct file *file, unsigned int cmd,
20703 +                          unsigned long arg)
20704 +{
20705 +       long err;
20706 +
20707 +       switch (cmd) {
20708 +       case AUFS_CTL_RDU:
20709 +       case AUFS_CTL_RDU_INO:
20710 +               err = au_rdu_compat_ioctl(file, cmd, arg);
20711 +               break;
20712 +
20713 +       case AUFS_CTL_IBUSY:
20714 +               err = au_ibusy_compat_ioctl(file, arg);
20715 +               break;
20716 +
20717 +       case AUFS_CTL_BRINFO:
20718 +               err = au_brinfo_compat_ioctl(file, arg);
20719 +               break;
20720 +
20721 +       default:
20722 +               err = aufs_ioctl_dir(file, cmd, arg);
20723 +       }
20724 +
20725 +       AuTraceErr(err);
20726 +       return err;
20727 +}
20728 +
20729 +long aufs_compat_ioctl_nondir(struct file *file, unsigned int cmd,
20730 +                             unsigned long arg)
20731 +{
20732 +       return aufs_ioctl_nondir(file, cmd, (unsigned long)compat_ptr(arg));
20733 +}
20734 +#endif
20735 diff -urN /usr/share/empty/fs/aufs/i_op_add.c linux/fs/aufs/i_op_add.c
20736 --- /usr/share/empty/fs/aufs/i_op_add.c 1970-01-01 01:00:00.000000000 +0100
20737 +++ linux/fs/aufs/i_op_add.c    2022-03-21 14:49:05.726633010 +0100
20738 @@ -0,0 +1,941 @@
20739 +// SPDX-License-Identifier: GPL-2.0
20740 +/*
20741 + * Copyright (C) 2005-2021 Junjiro R. Okajima
20742 + *
20743 + * This program, aufs is free software; you can redistribute it and/or modify
20744 + * it under the terms of the GNU General Public License as published by
20745 + * the Free Software Foundation; either version 2 of the License, or
20746 + * (at your option) any later version.
20747 + *
20748 + * This program is distributed in the hope that it will be useful,
20749 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
20750 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20751 + * GNU General Public License for more details.
20752 + *
20753 + * You should have received a copy of the GNU General Public License
20754 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20755 + */
20756 +
20757 +/*
20758 + * inode operations (add entry)
20759 + */
20760 +
20761 +#include <linux/iversion.h>
20762 +#include "aufs.h"
20763 +
20764 +/*
20765 + * final procedure of adding a new entry, except link(2).
20766 + * remove whiteout, instantiate, copyup the parent dir's times and size
20767 + * and update version.
20768 + * if it failed, re-create the removed whiteout.
20769 + */
20770 +static int epilog(struct inode *dir, aufs_bindex_t bindex,
20771 +                 struct dentry *wh_dentry, struct dentry *dentry)
20772 +{
20773 +       int err, rerr;
20774 +       aufs_bindex_t bwh;
20775 +       struct path h_path;
20776 +       struct super_block *sb;
20777 +       struct inode *inode, *h_dir;
20778 +       struct dentry *wh;
20779 +
20780 +       bwh = -1;
20781 +       sb = dir->i_sb;
20782 +       if (wh_dentry) {
20783 +               h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */
20784 +               IMustLock(h_dir);
20785 +               AuDebugOn(au_h_iptr(dir, bindex) != h_dir);
20786 +               bwh = au_dbwh(dentry);
20787 +               h_path.dentry = wh_dentry;
20788 +               h_path.mnt = au_sbr_mnt(sb, bindex);
20789 +               err = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path,
20790 +                                         dentry);
20791 +               if (unlikely(err))
20792 +                       goto out;
20793 +       }
20794 +
20795 +       inode = au_new_inode(dentry, /*must_new*/1);
20796 +       if (!IS_ERR(inode)) {
20797 +               d_instantiate(dentry, inode);
20798 +               dir = d_inode(dentry->d_parent); /* dir inode is locked */
20799 +               IMustLock(dir);
20800 +               au_dir_ts(dir, bindex);
20801 +               inode_inc_iversion(dir);
20802 +               au_fhsm_wrote(sb, bindex, /*force*/0);
20803 +               return 0; /* success */
20804 +       }
20805 +
20806 +       err = PTR_ERR(inode);
20807 +       if (!wh_dentry)
20808 +               goto out;
20809 +
20810 +       /* revert */
20811 +       /* dir inode is locked */
20812 +       wh = au_wh_create(dentry, bwh, wh_dentry->d_parent);
20813 +       rerr = PTR_ERR(wh);
20814 +       if (IS_ERR(wh)) {
20815 +               AuIOErr("%pd reverting whiteout failed(%d, %d)\n",
20816 +                       dentry, err, rerr);
20817 +               err = -EIO;
20818 +       } else
20819 +               dput(wh);
20820 +
20821 +out:
20822 +       return err;
20823 +}
20824 +
20825 +static int au_d_may_add(struct dentry *dentry)
20826 +{
20827 +       int err;
20828 +
20829 +       err = 0;
20830 +       if (unlikely(d_unhashed(dentry)))
20831 +               err = -ENOENT;
20832 +       if (unlikely(d_really_is_positive(dentry)))
20833 +               err = -EEXIST;
20834 +       return err;
20835 +}
20836 +
20837 +/*
20838 + * simple tests for the adding inode operations.
20839 + * following the checks in vfs, plus the parent-child relationship.
20840 + */
20841 +int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
20842 +              struct dentry *h_parent, int isdir)
20843 +{
20844 +       int err;
20845 +       umode_t h_mode;
20846 +       struct dentry *h_dentry;
20847 +       struct inode *h_inode;
20848 +
20849 +       err = -ENAMETOOLONG;
20850 +       if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
20851 +               goto out;
20852 +
20853 +       h_dentry = au_h_dptr(dentry, bindex);
20854 +       if (d_really_is_negative(dentry)) {
20855 +               err = -EEXIST;
20856 +               if (unlikely(d_is_positive(h_dentry)))
20857 +                       goto out;
20858 +       } else {
20859 +               /* rename(2) case */
20860 +               err = -EIO;
20861 +               if (unlikely(d_is_negative(h_dentry)))
20862 +                       goto out;
20863 +               h_inode = d_inode(h_dentry);
20864 +               if (unlikely(!h_inode->i_nlink))
20865 +                       goto out;
20866 +
20867 +               h_mode = h_inode->i_mode;
20868 +               if (!isdir) {
20869 +                       err = -EISDIR;
20870 +                       if (unlikely(S_ISDIR(h_mode)))
20871 +                               goto out;
20872 +               } else if (unlikely(!S_ISDIR(h_mode))) {
20873 +                       err = -ENOTDIR;
20874 +                       goto out;
20875 +               }
20876 +       }
20877 +
20878 +       err = 0;
20879 +       /* expected parent dir is locked */
20880 +       if (unlikely(h_parent != h_dentry->d_parent))
20881 +               err = -EIO;
20882 +
20883 +out:
20884 +       AuTraceErr(err);
20885 +       return err;
20886 +}
20887 +
20888 +/*
20889 + * initial procedure of adding a new entry.
20890 + * prepare writable branch and the parent dir, lock it,
20891 + * and lookup whiteout for the new entry.
20892 + */
20893 +static struct dentry*
20894 +lock_hdir_lkup_wh(struct dentry *dentry, struct au_dtime *dt,
20895 +                 struct dentry *src_dentry, struct au_pin *pin,
20896 +                 struct au_wr_dir_args *wr_dir_args)
20897 +{
20898 +       struct dentry *wh_dentry, *h_parent;
20899 +       struct super_block *sb;
20900 +       struct au_branch *br;
20901 +       int err;
20902 +       unsigned int udba;
20903 +       aufs_bindex_t bcpup;
20904 +
20905 +       AuDbg("%pd\n", dentry);
20906 +
20907 +       err = au_wr_dir(dentry, src_dentry, wr_dir_args);
20908 +       bcpup = err;
20909 +       wh_dentry = ERR_PTR(err);
20910 +       if (unlikely(err < 0))
20911 +               goto out;
20912 +
20913 +       sb = dentry->d_sb;
20914 +       udba = au_opt_udba(sb);
20915 +       err = au_pin(pin, dentry, bcpup, udba,
20916 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
20917 +       wh_dentry = ERR_PTR(err);
20918 +       if (unlikely(err))
20919 +               goto out;
20920 +
20921 +       h_parent = au_pinned_h_parent(pin);
20922 +       if (udba != AuOpt_UDBA_NONE
20923 +           && au_dbtop(dentry) == bcpup)
20924 +               err = au_may_add(dentry, bcpup, h_parent,
20925 +                                au_ftest_wrdir(wr_dir_args->flags, ISDIR));
20926 +       else if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
20927 +               err = -ENAMETOOLONG;
20928 +       wh_dentry = ERR_PTR(err);
20929 +       if (unlikely(err))
20930 +               goto out_unpin;
20931 +
20932 +       br = au_sbr(sb, bcpup);
20933 +       if (dt) {
20934 +               struct path tmp = {
20935 +                       .dentry = h_parent,
20936 +                       .mnt    = au_br_mnt(br)
20937 +               };
20938 +               au_dtime_store(dt, au_pinned_parent(pin), &tmp);
20939 +       }
20940 +
20941 +       wh_dentry = NULL;
20942 +       if (bcpup != au_dbwh(dentry))
20943 +               goto out; /* success */
20944 +
20945 +       /*
20946 +        * ENAMETOOLONG here means that if we allowed create such name, then it
20947 +        * would not be able to removed in the future. So we don't allow such
20948 +        * name here and we don't handle ENAMETOOLONG differently here.
20949 +        */
20950 +       wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
20951 +
20952 +out_unpin:
20953 +       if (IS_ERR(wh_dentry))
20954 +               au_unpin(pin);
20955 +out:
20956 +       return wh_dentry;
20957 +}
20958 +
20959 +/* ---------------------------------------------------------------------- */
20960 +
20961 +enum { Mknod, Symlink, Creat };
20962 +struct simple_arg {
20963 +       int type;
20964 +       union {
20965 +               struct {
20966 +                       umode_t                 mode;
20967 +                       bool                    want_excl;
20968 +                       bool                    try_aopen;
20969 +                       struct vfsub_aopen_args *aopen;
20970 +               } c;
20971 +               struct {
20972 +                       const char *symname;
20973 +               } s;
20974 +               struct {
20975 +                       umode_t mode;
20976 +                       dev_t dev;
20977 +               } m;
20978 +       } u;
20979 +};
20980 +
20981 +static int add_simple(struct inode *dir, struct dentry *dentry,
20982 +                     struct simple_arg *arg)
20983 +{
20984 +       int err, rerr;
20985 +       aufs_bindex_t btop;
20986 +       unsigned char created;
20987 +       const unsigned char try_aopen
20988 +               = (arg->type == Creat && arg->u.c.try_aopen);
20989 +       struct vfsub_aopen_args *aopen = arg->u.c.aopen;
20990 +       struct dentry *wh_dentry, *parent;
20991 +       struct inode *h_dir;
20992 +       struct super_block *sb;
20993 +       struct au_branch *br;
20994 +       /* to reduce stack size */
20995 +       struct {
20996 +               struct au_dtime dt;
20997 +               struct au_pin pin;
20998 +               struct path h_path;
20999 +               struct au_wr_dir_args wr_dir_args;
21000 +       } *a;
21001 +
21002 +       AuDbg("%pd\n", dentry);
21003 +       IMustLock(dir);
21004 +
21005 +       err = -ENOMEM;
21006 +       a = kmalloc(sizeof(*a), GFP_NOFS);
21007 +       if (unlikely(!a))
21008 +               goto out;
21009 +       a->wr_dir_args.force_btgt = -1;
21010 +       a->wr_dir_args.flags = AuWrDir_ADD_ENTRY;
21011 +
21012 +       parent = dentry->d_parent; /* dir inode is locked */
21013 +       if (!try_aopen) {
21014 +               err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
21015 +               if (unlikely(err))
21016 +                       goto out_free;
21017 +       }
21018 +       err = au_d_may_add(dentry);
21019 +       if (unlikely(err))
21020 +               goto out_unlock;
21021 +       if (!try_aopen)
21022 +               di_write_lock_parent(parent);
21023 +       wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
21024 +                                     &a->pin, &a->wr_dir_args);
21025 +       err = PTR_ERR(wh_dentry);
21026 +       if (IS_ERR(wh_dentry))
21027 +               goto out_parent;
21028 +
21029 +       btop = au_dbtop(dentry);
21030 +       sb = dentry->d_sb;
21031 +       br = au_sbr(sb, btop);
21032 +       a->h_path.dentry = au_h_dptr(dentry, btop);
21033 +       a->h_path.mnt = au_br_mnt(br);
21034 +       h_dir = au_pinned_h_dir(&a->pin);
21035 +       switch (arg->type) {
21036 +       case Creat:
21037 +               if (!try_aopen || !h_dir->i_op->atomic_open) {
21038 +                       err = vfsub_create(h_dir, &a->h_path, arg->u.c.mode,
21039 +                                          arg->u.c.want_excl);
21040 +                       created = !err;
21041 +                       if (!err && try_aopen)
21042 +                               aopen->file->f_mode |= FMODE_CREATED;
21043 +               } else {
21044 +                       aopen->br = br;
21045 +                       err = vfsub_atomic_open(h_dir, a->h_path.dentry, aopen);
21046 +                       AuDbg("err %d\n", err);
21047 +                       AuDbgFile(aopen->file);
21048 +                       created = err >= 0
21049 +                               && !!(aopen->file->f_mode & FMODE_CREATED);
21050 +               }
21051 +               break;
21052 +       case Symlink:
21053 +               err = vfsub_symlink(h_dir, &a->h_path, arg->u.s.symname);
21054 +               created = !err;
21055 +               break;
21056 +       case Mknod:
21057 +               err = vfsub_mknod(h_dir, &a->h_path, arg->u.m.mode,
21058 +                                 arg->u.m.dev);
21059 +               created = !err;
21060 +               break;
21061 +       default:
21062 +               BUG();
21063 +       }
21064 +       if (unlikely(err < 0))
21065 +               goto out_unpin;
21066 +
21067 +       err = epilog(dir, btop, wh_dentry, dentry);
21068 +       if (!err)
21069 +               goto out_unpin; /* success */
21070 +
21071 +       /* revert */
21072 +       if (created /* && d_is_positive(a->h_path.dentry) */) {
21073 +               /* no delegation since it is just created */
21074 +               rerr = vfsub_unlink(h_dir, &a->h_path, /*delegated*/NULL,
21075 +                                   /*force*/0);
21076 +               if (rerr) {
21077 +                       AuIOErr("%pd revert failure(%d, %d)\n",
21078 +                               dentry, err, rerr);
21079 +                       err = -EIO;
21080 +               }
21081 +               au_dtime_revert(&a->dt);
21082 +       }
21083 +       if (try_aopen && h_dir->i_op->atomic_open
21084 +           && (aopen->file->f_mode & FMODE_OPENED))
21085 +               /* aopen->file is still opened */
21086 +               au_lcnt_dec(&aopen->br->br_nfiles);
21087 +
21088 +out_unpin:
21089 +       au_unpin(&a->pin);
21090 +       dput(wh_dentry);
21091 +out_parent:
21092 +       if (!try_aopen)
21093 +               di_write_unlock(parent);
21094 +out_unlock:
21095 +       if (unlikely(err)) {
21096 +               au_update_dbtop(dentry);
21097 +               d_drop(dentry);
21098 +       }
21099 +       if (!try_aopen)
21100 +               aufs_read_unlock(dentry, AuLock_DW);
21101 +out_free:
21102 +       au_kfree_rcu(a);
21103 +out:
21104 +       return err;
21105 +}
21106 +
21107 +int aufs_mknod(struct user_namespace *userns, struct inode *dir,
21108 +              struct dentry *dentry, umode_t mode, dev_t dev)
21109 +{
21110 +       struct simple_arg arg = {
21111 +               .type = Mknod,
21112 +               .u.m = {
21113 +                       .mode   = mode,
21114 +                       .dev    = dev
21115 +               }
21116 +       };
21117 +       return add_simple(dir, dentry, &arg);
21118 +}
21119 +
21120 +int aufs_symlink(struct user_namespace *userns, struct inode *dir,
21121 +                struct dentry *dentry, const char *symname)
21122 +{
21123 +       struct simple_arg arg = {
21124 +               .type = Symlink,
21125 +               .u.s.symname = symname
21126 +       };
21127 +       return add_simple(dir, dentry, &arg);
21128 +}
21129 +
21130 +int aufs_create(struct user_namespace *userns, struct inode *dir,
21131 +               struct dentry *dentry, umode_t mode, bool want_excl)
21132 +{
21133 +       struct simple_arg arg = {
21134 +               .type = Creat,
21135 +               .u.c = {
21136 +                       .mode           = mode,
21137 +                       .want_excl      = want_excl
21138 +               }
21139 +       };
21140 +       return add_simple(dir, dentry, &arg);
21141 +}
21142 +
21143 +int au_aopen_or_create(struct inode *dir, struct dentry *dentry,
21144 +                      struct vfsub_aopen_args *aopen_args)
21145 +{
21146 +       struct simple_arg arg = {
21147 +               .type = Creat,
21148 +               .u.c = {
21149 +                       .mode           = aopen_args->create_mode,
21150 +                       .want_excl      = aopen_args->open_flag & O_EXCL,
21151 +                       .try_aopen      = true,
21152 +                       .aopen          = aopen_args
21153 +               }
21154 +       };
21155 +       return add_simple(dir, dentry, &arg);
21156 +}
21157 +
21158 +int aufs_tmpfile(struct user_namespace *userns, struct inode *dir,
21159 +                struct dentry *dentry, umode_t mode)
21160 +{
21161 +       int err;
21162 +       aufs_bindex_t bindex;
21163 +       struct super_block *sb;
21164 +       struct dentry *parent, *h_parent, *h_dentry;
21165 +       struct inode *h_dir, *inode;
21166 +       struct vfsmount *h_mnt;
21167 +       struct user_namespace *h_userns;
21168 +       struct au_wr_dir_args wr_dir_args = {
21169 +               .force_btgt     = -1,
21170 +               .flags          = AuWrDir_TMPFILE
21171 +       };
21172 +
21173 +       /* copy-up may happen */
21174 +       inode_lock(dir);
21175 +
21176 +       sb = dir->i_sb;
21177 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
21178 +       if (unlikely(err))
21179 +               goto out;
21180 +
21181 +       err = au_di_init(dentry);
21182 +       if (unlikely(err))
21183 +               goto out_si;
21184 +
21185 +       err = -EBUSY;
21186 +       parent = d_find_any_alias(dir);
21187 +       AuDebugOn(!parent);
21188 +       di_write_lock_parent(parent);
21189 +       if (unlikely(d_inode(parent) != dir))
21190 +               goto out_parent;
21191 +
21192 +       err = au_digen_test(parent, au_sigen(sb));
21193 +       if (unlikely(err))
21194 +               goto out_parent;
21195 +
21196 +       bindex = au_dbtop(parent);
21197 +       au_set_dbtop(dentry, bindex);
21198 +       au_set_dbbot(dentry, bindex);
21199 +       err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
21200 +       bindex = err;
21201 +       if (unlikely(err < 0))
21202 +               goto out_parent;
21203 +
21204 +       err = -EOPNOTSUPP;
21205 +       h_dir = au_h_iptr(dir, bindex);
21206 +       if (unlikely(!h_dir->i_op->tmpfile))
21207 +               goto out_parent;
21208 +
21209 +       h_mnt = au_sbr_mnt(sb, bindex);
21210 +       err = vfsub_mnt_want_write(h_mnt);
21211 +       if (unlikely(err))
21212 +               goto out_parent;
21213 +
21214 +       h_userns = mnt_user_ns(h_mnt);
21215 +       h_parent = au_h_dptr(parent, bindex);
21216 +       h_dentry = vfs_tmpfile(h_userns, h_parent, mode, /*open_flag*/0);
21217 +       if (IS_ERR(h_dentry)) {
21218 +               err = PTR_ERR(h_dentry);
21219 +               goto out_mnt;
21220 +       }
21221 +
21222 +       au_set_dbtop(dentry, bindex);
21223 +       au_set_dbbot(dentry, bindex);
21224 +       au_set_h_dptr(dentry, bindex, dget(h_dentry));
21225 +       inode = au_new_inode(dentry, /*must_new*/1);
21226 +       if (IS_ERR(inode)) {
21227 +               err = PTR_ERR(inode);
21228 +               au_set_h_dptr(dentry, bindex, NULL);
21229 +               au_set_dbtop(dentry, -1);
21230 +               au_set_dbbot(dentry, -1);
21231 +       } else {
21232 +               if (!inode->i_nlink)
21233 +                       set_nlink(inode, 1);
21234 +               d_tmpfile(dentry, inode);
21235 +               au_di(dentry)->di_tmpfile = 1;
21236 +
21237 +               /* update without i_mutex */
21238 +               if (au_ibtop(dir) == au_dbtop(dentry))
21239 +                       au_cpup_attr_timesizes(dir);
21240 +       }
21241 +       dput(h_dentry);
21242 +
21243 +out_mnt:
21244 +       vfsub_mnt_drop_write(h_mnt);
21245 +out_parent:
21246 +       di_write_unlock(parent);
21247 +       dput(parent);
21248 +       di_write_unlock(dentry);
21249 +       if (unlikely(err)) {
21250 +               au_di_fin(dentry);
21251 +               dentry->d_fsdata = NULL;
21252 +       }
21253 +out_si:
21254 +       si_read_unlock(sb);
21255 +out:
21256 +       inode_unlock(dir);
21257 +       return err;
21258 +}
21259 +
21260 +/* ---------------------------------------------------------------------- */
21261 +
21262 +struct au_link_args {
21263 +       aufs_bindex_t bdst, bsrc;
21264 +       struct au_pin pin;
21265 +       struct path h_path;
21266 +       struct dentry *src_parent, *parent;
21267 +};
21268 +
21269 +static int au_cpup_before_link(struct dentry *src_dentry,
21270 +                              struct au_link_args *a)
21271 +{
21272 +       int err;
21273 +       struct dentry *h_src_dentry;
21274 +       struct au_cp_generic cpg = {
21275 +               .dentry = src_dentry,
21276 +               .bdst   = a->bdst,
21277 +               .bsrc   = a->bsrc,
21278 +               .len    = -1,
21279 +               .pin    = &a->pin,
21280 +               .flags  = AuCpup_DTIME | AuCpup_HOPEN /* | AuCpup_KEEPLINO */
21281 +       };
21282 +
21283 +       di_read_lock_parent(a->src_parent, AuLock_IR);
21284 +       err = au_test_and_cpup_dirs(src_dentry, a->bdst);
21285 +       if (unlikely(err))
21286 +               goto out;
21287 +
21288 +       h_src_dentry = au_h_dptr(src_dentry, a->bsrc);
21289 +       err = au_pin(&a->pin, src_dentry, a->bdst,
21290 +                    au_opt_udba(src_dentry->d_sb),
21291 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
21292 +       if (unlikely(err))
21293 +               goto out;
21294 +
21295 +       err = au_sio_cpup_simple(&cpg);
21296 +       au_unpin(&a->pin);
21297 +
21298 +out:
21299 +       di_read_unlock(a->src_parent, AuLock_IR);
21300 +       return err;
21301 +}
21302 +
21303 +static int au_cpup_or_link(struct dentry *src_dentry, struct dentry *dentry,
21304 +                          struct au_link_args *a)
21305 +{
21306 +       int err;
21307 +       unsigned char plink;
21308 +       aufs_bindex_t bbot;
21309 +       struct dentry *h_src_dentry;
21310 +       struct inode *h_inode, *inode, *delegated;
21311 +       struct super_block *sb;
21312 +       struct file *h_file;
21313 +
21314 +       plink = 0;
21315 +       h_inode = NULL;
21316 +       sb = src_dentry->d_sb;
21317 +       inode = d_inode(src_dentry);
21318 +       if (au_ibtop(inode) <= a->bdst)
21319 +               h_inode = au_h_iptr(inode, a->bdst);
21320 +       if (!h_inode || !h_inode->i_nlink) {
21321 +               /* copyup src_dentry as the name of dentry. */
21322 +               bbot = au_dbbot(dentry);
21323 +               if (bbot < a->bsrc)
21324 +                       au_set_dbbot(dentry, a->bsrc);
21325 +               au_set_h_dptr(dentry, a->bsrc,
21326 +                             dget(au_h_dptr(src_dentry, a->bsrc)));
21327 +               dget(a->h_path.dentry);
21328 +               au_set_h_dptr(dentry, a->bdst, NULL);
21329 +               AuDbg("temporary d_inode...\n");
21330 +               spin_lock(&dentry->d_lock);
21331 +               dentry->d_inode = d_inode(src_dentry); /* tmp */
21332 +               spin_unlock(&dentry->d_lock);
21333 +               h_file = au_h_open_pre(dentry, a->bsrc, /*force_wr*/0);
21334 +               if (IS_ERR(h_file))
21335 +                       err = PTR_ERR(h_file);
21336 +               else {
21337 +                       struct au_cp_generic cpg = {
21338 +                               .dentry = dentry,
21339 +                               .bdst   = a->bdst,
21340 +                               .bsrc   = -1,
21341 +                               .len    = -1,
21342 +                               .pin    = &a->pin,
21343 +                               .flags  = AuCpup_KEEPLINO
21344 +                       };
21345 +                       err = au_sio_cpup_simple(&cpg);
21346 +                       au_h_open_post(dentry, a->bsrc, h_file);
21347 +                       if (!err) {
21348 +                               dput(a->h_path.dentry);
21349 +                               a->h_path.dentry = au_h_dptr(dentry, a->bdst);
21350 +                       } else
21351 +                               au_set_h_dptr(dentry, a->bdst,
21352 +                                             a->h_path.dentry);
21353 +               }
21354 +               spin_lock(&dentry->d_lock);
21355 +               dentry->d_inode = NULL; /* restore */
21356 +               spin_unlock(&dentry->d_lock);
21357 +               AuDbg("temporary d_inode...done\n");
21358 +               au_set_h_dptr(dentry, a->bsrc, NULL);
21359 +               au_set_dbbot(dentry, bbot);
21360 +       } else {
21361 +               /* the inode of src_dentry already exists on a.bdst branch */
21362 +               h_src_dentry = d_find_alias(h_inode);
21363 +               if (!h_src_dentry && au_plink_test(inode)) {
21364 +                       plink = 1;
21365 +                       h_src_dentry = au_plink_lkup(inode, a->bdst);
21366 +                       err = PTR_ERR(h_src_dentry);
21367 +                       if (IS_ERR(h_src_dentry))
21368 +                               goto out;
21369 +
21370 +                       if (unlikely(d_is_negative(h_src_dentry))) {
21371 +                               dput(h_src_dentry);
21372 +                               h_src_dentry = NULL;
21373 +                       }
21374 +
21375 +               }
21376 +               if (h_src_dentry) {
21377 +                       delegated = NULL;
21378 +                       err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
21379 +                                        &a->h_path, &delegated);
21380 +                       if (unlikely(err == -EWOULDBLOCK)) {
21381 +                               pr_warn("cannot retry for NFSv4 delegation"
21382 +                                       " for an internal link\n");
21383 +                               iput(delegated);
21384 +                       }
21385 +                       dput(h_src_dentry);
21386 +               } else {
21387 +                       AuIOErr("no dentry found for hi%lu on b%d\n",
21388 +                               h_inode->i_ino, a->bdst);
21389 +                       err = -EIO;
21390 +               }
21391 +       }
21392 +
21393 +       if (!err && !plink)
21394 +               au_plink_append(inode, a->bdst, a->h_path.dentry);
21395 +
21396 +out:
21397 +       AuTraceErr(err);
21398 +       return err;
21399 +}
21400 +
21401 +int aufs_link(struct dentry *src_dentry, struct inode *dir,
21402 +             struct dentry *dentry)
21403 +{
21404 +       int err, rerr;
21405 +       struct au_dtime dt;
21406 +       struct au_link_args *a;
21407 +       struct dentry *wh_dentry, *h_src_dentry;
21408 +       struct inode *inode, *delegated;
21409 +       struct super_block *sb;
21410 +       struct au_wr_dir_args wr_dir_args = {
21411 +               /* .force_btgt  = -1, */
21412 +               .flags          = AuWrDir_ADD_ENTRY
21413 +       };
21414 +
21415 +       IMustLock(dir);
21416 +       inode = d_inode(src_dentry);
21417 +       IMustLock(inode);
21418 +
21419 +       err = -ENOMEM;
21420 +       a = kzalloc(sizeof(*a), GFP_NOFS);
21421 +       if (unlikely(!a))
21422 +               goto out;
21423 +
21424 +       a->parent = dentry->d_parent; /* dir inode is locked */
21425 +       err = aufs_read_and_write_lock2(dentry, src_dentry,
21426 +                                       AuLock_NOPLM | AuLock_GEN);
21427 +       if (unlikely(err))
21428 +               goto out_kfree;
21429 +       err = au_d_linkable(src_dentry);
21430 +       if (unlikely(err))
21431 +               goto out_unlock;
21432 +       err = au_d_may_add(dentry);
21433 +       if (unlikely(err))
21434 +               goto out_unlock;
21435 +
21436 +       a->src_parent = dget_parent(src_dentry);
21437 +       wr_dir_args.force_btgt = au_ibtop(inode);
21438 +
21439 +       di_write_lock_parent(a->parent);
21440 +       wr_dir_args.force_btgt = au_wbr(dentry, wr_dir_args.force_btgt);
21441 +       wh_dentry = lock_hdir_lkup_wh(dentry, &dt, src_dentry, &a->pin,
21442 +                                     &wr_dir_args);
21443 +       err = PTR_ERR(wh_dentry);
21444 +       if (IS_ERR(wh_dentry))
21445 +               goto out_parent;
21446 +
21447 +       err = 0;
21448 +       sb = dentry->d_sb;
21449 +       a->bdst = au_dbtop(dentry);
21450 +       a->h_path.dentry = au_h_dptr(dentry, a->bdst);
21451 +       a->h_path.mnt = au_sbr_mnt(sb, a->bdst);
21452 +       a->bsrc = au_ibtop(inode);
21453 +       h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
21454 +       if (!h_src_dentry && au_di(src_dentry)->di_tmpfile)
21455 +               h_src_dentry = dget(au_hi_wh(inode, a->bsrc));
21456 +       if (!h_src_dentry) {
21457 +               a->bsrc = au_dbtop(src_dentry);
21458 +               h_src_dentry = au_h_d_alias(src_dentry, a->bsrc);
21459 +               AuDebugOn(!h_src_dentry);
21460 +       } else if (IS_ERR(h_src_dentry)) {
21461 +               err = PTR_ERR(h_src_dentry);
21462 +               goto out_parent;
21463 +       }
21464 +
21465 +       /*
21466 +        * aufs doesn't touch the credential so
21467 +        * security_dentry_create_files_as() is unnecessary.
21468 +        */
21469 +       if (au_opt_test(au_mntflags(sb), PLINK)) {
21470 +               if (a->bdst < a->bsrc
21471 +                   /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */)
21472 +                       err = au_cpup_or_link(src_dentry, dentry, a);
21473 +               else {
21474 +                       delegated = NULL;
21475 +                       err = vfsub_link(h_src_dentry, au_pinned_h_dir(&a->pin),
21476 +                                        &a->h_path, &delegated);
21477 +                       if (unlikely(err == -EWOULDBLOCK)) {
21478 +                               pr_warn("cannot retry for NFSv4 delegation"
21479 +                                       " for an internal link\n");
21480 +                               iput(delegated);
21481 +                       }
21482 +               }
21483 +               dput(h_src_dentry);
21484 +       } else {
21485 +               /*
21486 +                * copyup src_dentry to the branch we process,
21487 +                * and then link(2) to it.
21488 +                */
21489 +               dput(h_src_dentry);
21490 +               if (a->bdst < a->bsrc
21491 +                   /* && h_src_dentry->d_sb != a->h_path.dentry->d_sb */) {
21492 +                       au_unpin(&a->pin);
21493 +                       di_write_unlock(a->parent);
21494 +                       err = au_cpup_before_link(src_dentry, a);
21495 +                       di_write_lock_parent(a->parent);
21496 +                       if (!err)
21497 +                               err = au_pin(&a->pin, dentry, a->bdst,
21498 +                                            au_opt_udba(sb),
21499 +                                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
21500 +                       if (unlikely(err))
21501 +                               goto out_wh;
21502 +               }
21503 +               if (!err) {
21504 +                       h_src_dentry = au_h_dptr(src_dentry, a->bdst);
21505 +                       err = -ENOENT;
21506 +                       if (h_src_dentry && d_is_positive(h_src_dentry)) {
21507 +                               delegated = NULL;
21508 +                               err = vfsub_link(h_src_dentry,
21509 +                                                au_pinned_h_dir(&a->pin),
21510 +                                                &a->h_path, &delegated);
21511 +                               if (unlikely(err == -EWOULDBLOCK)) {
21512 +                                       pr_warn("cannot retry"
21513 +                                               " for NFSv4 delegation"
21514 +                                               " for an internal link\n");
21515 +                                       iput(delegated);
21516 +                               }
21517 +                       }
21518 +               }
21519 +       }
21520 +       if (unlikely(err))
21521 +               goto out_unpin;
21522 +
21523 +       if (wh_dentry) {
21524 +               a->h_path.dentry = wh_dentry;
21525 +               err = au_wh_unlink_dentry(au_pinned_h_dir(&a->pin), &a->h_path,
21526 +                                         dentry);
21527 +               if (unlikely(err))
21528 +                       goto out_revert;
21529 +       }
21530 +
21531 +       au_dir_ts(dir, a->bdst);
21532 +       inode_inc_iversion(dir);
21533 +       inc_nlink(inode);
21534 +       inode->i_ctime = dir->i_ctime;
21535 +       d_instantiate(dentry, au_igrab(inode));
21536 +       if (d_unhashed(a->h_path.dentry))
21537 +               /* some filesystem calls d_drop() */
21538 +               d_drop(dentry);
21539 +       /* some filesystems consume an inode even hardlink */
21540 +       au_fhsm_wrote(sb, a->bdst, /*force*/0);
21541 +       goto out_unpin; /* success */
21542 +
21543 +out_revert:
21544 +       /* no delegation since it is just created */
21545 +       rerr = vfsub_unlink(au_pinned_h_dir(&a->pin), &a->h_path,
21546 +                           /*delegated*/NULL, /*force*/0);
21547 +       if (unlikely(rerr)) {
21548 +               AuIOErr("%pd reverting failed(%d, %d)\n", dentry, err, rerr);
21549 +               err = -EIO;
21550 +       }
21551 +       au_dtime_revert(&dt);
21552 +out_unpin:
21553 +       au_unpin(&a->pin);
21554 +out_wh:
21555 +       dput(wh_dentry);
21556 +out_parent:
21557 +       di_write_unlock(a->parent);
21558 +       dput(a->src_parent);
21559 +out_unlock:
21560 +       if (unlikely(err)) {
21561 +               au_update_dbtop(dentry);
21562 +               d_drop(dentry);
21563 +       }
21564 +       aufs_read_and_write_unlock2(dentry, src_dentry);
21565 +out_kfree:
21566 +       au_kfree_rcu(a);
21567 +out:
21568 +       AuTraceErr(err);
21569 +       return err;
21570 +}
21571 +
21572 +int aufs_mkdir(struct user_namespace *userns, struct inode *dir,
21573 +              struct dentry *dentry, umode_t mode)
21574 +{
21575 +       int err, rerr;
21576 +       aufs_bindex_t bindex;
21577 +       unsigned char diropq;
21578 +       struct path h_path;
21579 +       struct dentry *wh_dentry, *parent, *opq_dentry;
21580 +       struct inode *h_inode;
21581 +       struct super_block *sb;
21582 +       struct {
21583 +               struct au_pin pin;
21584 +               struct au_dtime dt;
21585 +       } *a; /* reduce the stack usage */
21586 +       struct au_wr_dir_args wr_dir_args = {
21587 +               .force_btgt     = -1,
21588 +               .flags          = AuWrDir_ADD_ENTRY | AuWrDir_ISDIR
21589 +       };
21590 +
21591 +       IMustLock(dir);
21592 +
21593 +       err = -ENOMEM;
21594 +       a = kmalloc(sizeof(*a), GFP_NOFS);
21595 +       if (unlikely(!a))
21596 +               goto out;
21597 +
21598 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
21599 +       if (unlikely(err))
21600 +               goto out_free;
21601 +       err = au_d_may_add(dentry);
21602 +       if (unlikely(err))
21603 +               goto out_unlock;
21604 +
21605 +       parent = dentry->d_parent; /* dir inode is locked */
21606 +       di_write_lock_parent(parent);
21607 +       wh_dentry = lock_hdir_lkup_wh(dentry, &a->dt, /*src_dentry*/NULL,
21608 +                                     &a->pin, &wr_dir_args);
21609 +       err = PTR_ERR(wh_dentry);
21610 +       if (IS_ERR(wh_dentry))
21611 +               goto out_parent;
21612 +
21613 +       sb = dentry->d_sb;
21614 +       bindex = au_dbtop(dentry);
21615 +       h_path.dentry = au_h_dptr(dentry, bindex);
21616 +       h_path.mnt = au_sbr_mnt(sb, bindex);
21617 +       err = vfsub_mkdir(au_pinned_h_dir(&a->pin), &h_path, mode);
21618 +       if (unlikely(err))
21619 +               goto out_unpin;
21620 +
21621 +       /* make the dir opaque */
21622 +       diropq = 0;
21623 +       h_inode = d_inode(h_path.dentry);
21624 +       if (wh_dentry
21625 +           || au_opt_test(au_mntflags(sb), ALWAYS_DIROPQ)) {
21626 +               inode_lock_nested(h_inode, AuLsc_I_CHILD);
21627 +               opq_dentry = au_diropq_create(dentry, bindex);
21628 +               inode_unlock(h_inode);
21629 +               err = PTR_ERR(opq_dentry);
21630 +               if (IS_ERR(opq_dentry))
21631 +                       goto out_dir;
21632 +               dput(opq_dentry);
21633 +               diropq = 1;
21634 +       }
21635 +
21636 +       err = epilog(dir, bindex, wh_dentry, dentry);
21637 +       if (!err) {
21638 +               inc_nlink(dir);
21639 +               goto out_unpin; /* success */
21640 +       }
21641 +
21642 +       /* revert */
21643 +       if (diropq) {
21644 +               AuLabel(revert opq);
21645 +               inode_lock_nested(h_inode, AuLsc_I_CHILD);
21646 +               rerr = au_diropq_remove(dentry, bindex);
21647 +               inode_unlock(h_inode);
21648 +               if (rerr) {
21649 +                       AuIOErr("%pd reverting diropq failed(%d, %d)\n",
21650 +                               dentry, err, rerr);
21651 +                       err = -EIO;
21652 +               }
21653 +       }
21654 +
21655 +out_dir:
21656 +       AuLabel(revert dir);
21657 +       rerr = vfsub_rmdir(au_pinned_h_dir(&a->pin), &h_path);
21658 +       if (rerr) {
21659 +               AuIOErr("%pd reverting dir failed(%d, %d)\n",
21660 +                       dentry, err, rerr);
21661 +               err = -EIO;
21662 +       }
21663 +       au_dtime_revert(&a->dt);
21664 +out_unpin:
21665 +       au_unpin(&a->pin);
21666 +       dput(wh_dentry);
21667 +out_parent:
21668 +       di_write_unlock(parent);
21669 +out_unlock:
21670 +       if (unlikely(err)) {
21671 +               au_update_dbtop(dentry);
21672 +               d_drop(dentry);
21673 +       }
21674 +       aufs_read_unlock(dentry, AuLock_DW);
21675 +out_free:
21676 +       au_kfree_rcu(a);
21677 +out:
21678 +       return err;
21679 +}
21680 diff -urN /usr/share/empty/fs/aufs/i_op.c linux/fs/aufs/i_op.c
21681 --- /usr/share/empty/fs/aufs/i_op.c     1970-01-01 01:00:00.000000000 +0100
21682 +++ linux/fs/aufs/i_op.c        2022-03-21 14:49:05.726633010 +0100
21683 @@ -0,0 +1,1512 @@
21684 +// SPDX-License-Identifier: GPL-2.0
21685 +/*
21686 + * Copyright (C) 2005-2021 Junjiro R. Okajima
21687 + *
21688 + * This program, aufs is free software; you can redistribute it and/or modify
21689 + * it under the terms of the GNU General Public License as published by
21690 + * the Free Software Foundation; either version 2 of the License, or
21691 + * (at your option) any later version.
21692 + *
21693 + * This program is distributed in the hope that it will be useful,
21694 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
21695 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21696 + * GNU General Public License for more details.
21697 + *
21698 + * You should have received a copy of the GNU General Public License
21699 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21700 + */
21701 +
21702 +/*
21703 + * inode operations (except add/del/rename)
21704 + */
21705 +
21706 +#include <linux/device_cgroup.h>
21707 +#include <linux/fs_stack.h>
21708 +#include <linux/iversion.h>
21709 +#include <linux/security.h>
21710 +#include "aufs.h"
21711 +
21712 +static int h_permission(struct inode *h_inode, int mask,
21713 +                       struct path *h_path, int brperm)
21714 +{
21715 +       int err;
21716 +       const unsigned char write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
21717 +       struct user_namespace *h_userns;
21718 +
21719 +       err = -EPERM;
21720 +       if (write_mask && IS_IMMUTABLE(h_inode))
21721 +               goto out;
21722 +
21723 +       err = -EACCES;
21724 +       if (((mask & MAY_EXEC)
21725 +            && S_ISREG(h_inode->i_mode)
21726 +            && (path_noexec(h_path)
21727 +                || !(h_inode->i_mode & 0111))))
21728 +               goto out;
21729 +
21730 +       /*
21731 +        * - skip the lower fs test in the case of write to ro branch.
21732 +        * - nfs dir permission write check is optimized, but a policy for
21733 +        *   link/rename requires a real check.
21734 +        * - nfs always sets SB_POSIXACL regardless its mount option 'noacl.'
21735 +        *   in this case, generic_permission() returns -EOPNOTSUPP.
21736 +        */
21737 +       h_userns = mnt_user_ns(h_path->mnt);
21738 +       if ((write_mask && !au_br_writable(brperm))
21739 +           || (au_test_nfs(h_inode->i_sb) && S_ISDIR(h_inode->i_mode)
21740 +               && write_mask && !(mask & MAY_READ))
21741 +           || !h_inode->i_op->permission) {
21742 +               /* AuLabel(generic_permission); */
21743 +               /* AuDbg("get_acl %ps\n", h_inode->i_op->get_acl); */
21744 +               err = generic_permission(h_userns, h_inode, mask);
21745 +               if (err == -EOPNOTSUPP && au_test_nfs_noacl(h_inode))
21746 +                       err = h_inode->i_op->permission(h_userns, h_inode,
21747 +                                                       mask);
21748 +               AuTraceErr(err);
21749 +       } else {
21750 +               /* AuLabel(h_inode->permission); */
21751 +               err = h_inode->i_op->permission(h_userns, h_inode, mask);
21752 +               AuTraceErr(err);
21753 +       }
21754 +
21755 +       if (!err)
21756 +               err = devcgroup_inode_permission(h_inode, mask);
21757 +       if (!err)
21758 +               err = security_inode_permission(h_inode, mask);
21759 +
21760 +out:
21761 +       return err;
21762 +}
21763 +
21764 +static int aufs_permission(struct user_namespace *userns, struct inode *inode,
21765 +                          int mask)
21766 +{
21767 +       int err;
21768 +       aufs_bindex_t bindex, bbot;
21769 +       const unsigned char isdir = !!S_ISDIR(inode->i_mode),
21770 +               write_mask = !!(mask & (MAY_WRITE | MAY_APPEND));
21771 +       struct inode *h_inode;
21772 +       struct super_block *sb;
21773 +       struct au_branch *br;
21774 +
21775 +       /* todo: support rcu-walk? */
21776 +       if (mask & MAY_NOT_BLOCK)
21777 +               return -ECHILD;
21778 +
21779 +       sb = inode->i_sb;
21780 +       si_read_lock(sb, AuLock_FLUSH);
21781 +       ii_read_lock_child(inode);
21782 +#if 0 /* reserved for future use */
21783 +       /*
21784 +        * This test may be rather 'too much' since the test is essentially done
21785 +        * in the aufs_lookup().  Theoretically it is possible that the inode
21786 +        * generation doesn't match to the superblock's here.  But it isn't a
21787 +        * big deal I suppose.
21788 +        */
21789 +       err = au_iigen_test(inode, au_sigen(sb));
21790 +       if (unlikely(err))
21791 +               goto out;
21792 +#endif
21793 +
21794 +       if (!isdir
21795 +           || write_mask
21796 +           || au_opt_test(au_mntflags(sb), DIRPERM1)) {
21797 +               err = au_busy_or_stale();
21798 +               h_inode = au_h_iptr(inode, au_ibtop(inode));
21799 +               if (unlikely(!h_inode
21800 +                            || (h_inode->i_mode & S_IFMT)
21801 +                            != (inode->i_mode & S_IFMT)))
21802 +                       goto out;
21803 +
21804 +               err = 0;
21805 +               bindex = au_ibtop(inode);
21806 +               br = au_sbr(sb, bindex);
21807 +               err = h_permission(h_inode, mask, &br->br_path, br->br_perm);
21808 +               if (write_mask
21809 +                   && !err
21810 +                   && !special_file(h_inode->i_mode)) {
21811 +                       /* test whether the upper writable branch exists */
21812 +                       err = -EROFS;
21813 +                       for (; bindex >= 0; bindex--)
21814 +                               if (!au_br_rdonly(au_sbr(sb, bindex))) {
21815 +                                       err = 0;
21816 +                                       break;
21817 +                               }
21818 +               }
21819 +               goto out;
21820 +       }
21821 +
21822 +       /* non-write to dir */
21823 +       err = 0;
21824 +       bbot = au_ibbot(inode);
21825 +       for (bindex = au_ibtop(inode); !err && bindex <= bbot; bindex++) {
21826 +               h_inode = au_h_iptr(inode, bindex);
21827 +               if (h_inode) {
21828 +                       err = au_busy_or_stale();
21829 +                       if (unlikely(!S_ISDIR(h_inode->i_mode)))
21830 +                               break;
21831 +
21832 +                       br = au_sbr(sb, bindex);
21833 +                       err = h_permission(h_inode, mask, &br->br_path,
21834 +                                          br->br_perm);
21835 +               }
21836 +       }
21837 +
21838 +out:
21839 +       ii_read_unlock(inode);
21840 +       si_read_unlock(sb);
21841 +       return err;
21842 +}
21843 +
21844 +/* ---------------------------------------------------------------------- */
21845 +
21846 +static struct dentry *aufs_lookup(struct inode *dir, struct dentry *dentry,
21847 +                                 unsigned int flags)
21848 +{
21849 +       struct dentry *ret, *parent;
21850 +       struct inode *inode;
21851 +       struct super_block *sb;
21852 +       int err, npositive;
21853 +
21854 +       IMustLock(dir);
21855 +
21856 +       /* todo: support rcu-walk? */
21857 +       ret = ERR_PTR(-ECHILD);
21858 +       if (flags & LOOKUP_RCU)
21859 +               goto out;
21860 +
21861 +       ret = ERR_PTR(-ENAMETOOLONG);
21862 +       if (unlikely(dentry->d_name.len > AUFS_MAX_NAMELEN))
21863 +               goto out;
21864 +
21865 +       sb = dir->i_sb;
21866 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
21867 +       ret = ERR_PTR(err);
21868 +       if (unlikely(err))
21869 +               goto out;
21870 +
21871 +       err = au_di_init(dentry);
21872 +       ret = ERR_PTR(err);
21873 +       if (unlikely(err))
21874 +               goto out_si;
21875 +
21876 +       inode = NULL;
21877 +       npositive = 0; /* suppress a warning */
21878 +       parent = dentry->d_parent; /* dir inode is locked */
21879 +       di_read_lock_parent(parent, AuLock_IR);
21880 +       err = au_alive_dir(parent);
21881 +       if (!err)
21882 +               err = au_digen_test(parent, au_sigen(sb));
21883 +       if (!err) {
21884 +               /* regardless LOOKUP_CREATE, always ALLOW_NEG */
21885 +               npositive = au_lkup_dentry(dentry, au_dbtop(parent),
21886 +                                          AuLkup_ALLOW_NEG);
21887 +               err = npositive;
21888 +       }
21889 +       di_read_unlock(parent, AuLock_IR);
21890 +       ret = ERR_PTR(err);
21891 +       if (unlikely(err < 0))
21892 +               goto out_unlock;
21893 +
21894 +       if (npositive) {
21895 +               inode = au_new_inode(dentry, /*must_new*/0);
21896 +               if (IS_ERR(inode)) {
21897 +                       ret = (void *)inode;
21898 +                       inode = NULL;
21899 +                       goto out_unlock;
21900 +               }
21901 +       }
21902 +
21903 +       if (inode)
21904 +               atomic_inc(&inode->i_count);
21905 +       ret = d_splice_alias(inode, dentry);
21906 +#if 0 /* reserved for future use */
21907 +       if (unlikely(d_need_lookup(dentry))) {
21908 +               spin_lock(&dentry->d_lock);
21909 +               dentry->d_flags &= ~DCACHE_NEED_LOOKUP;
21910 +               spin_unlock(&dentry->d_lock);
21911 +       } else
21912 +#endif
21913 +       if (inode) {
21914 +               if (!IS_ERR(ret)) {
21915 +                       iput(inode);
21916 +                       if (ret && ret != dentry)
21917 +                               ii_write_unlock(inode);
21918 +               } else {
21919 +                       ii_write_unlock(inode);
21920 +                       iput(inode);
21921 +                       inode = NULL;
21922 +               }
21923 +       }
21924 +
21925 +out_unlock:
21926 +       di_write_unlock(dentry);
21927 +out_si:
21928 +       si_read_unlock(sb);
21929 +out:
21930 +       return ret;
21931 +}
21932 +
21933 +/* ---------------------------------------------------------------------- */
21934 +
21935 +/*
21936 + * very dirty and complicated aufs ->atomic_open().
21937 + * aufs_atomic_open()
21938 + * + au_aopen_or_create()
21939 + *   + add_simple()
21940 + *     + vfsub_atomic_open()
21941 + *       + branch fs ->atomic_open()
21942 + *        may call the actual 'open' for h_file
21943 + *       + inc br_nfiles only if opened
21944 + * + au_aopen_no_open() or au_aopen_do_open()
21945 + *
21946 + * au_aopen_do_open()
21947 + * + finish_open()
21948 + *   + au_do_aopen()
21949 + *     + au_do_open() the body of all 'open'
21950 + *       + au_do_open_nondir()
21951 + *        set the passed h_file
21952 + *
21953 + * au_aopen_no_open()
21954 + * + finish_no_open()
21955 + */
21956 +
21957 +struct aopen_node {
21958 +       struct hlist_bl_node hblist;
21959 +       struct file *file, *h_file;
21960 +};
21961 +
21962 +static int au_do_aopen(struct inode *inode, struct file *file)
21963 +{
21964 +       struct hlist_bl_head *aopen;
21965 +       struct hlist_bl_node *pos;
21966 +       struct aopen_node *node;
21967 +       struct au_do_open_args args = {
21968 +               .aopen  = 1,
21969 +               .open   = au_do_open_nondir
21970 +       };
21971 +
21972 +       aopen = &au_sbi(inode->i_sb)->si_aopen;
21973 +       hlist_bl_lock(aopen);
21974 +       hlist_bl_for_each_entry(node, pos, aopen, hblist)
21975 +               if (node->file == file) {
21976 +                       args.h_file = node->h_file;
21977 +                       break;
21978 +               }
21979 +       hlist_bl_unlock(aopen);
21980 +       /* AuDebugOn(!args.h_file); */
21981 +
21982 +       return au_do_open(file, &args);
21983 +}
21984 +
21985 +static int au_aopen_do_open(struct file *file, struct dentry *dentry,
21986 +                           struct aopen_node *aopen_node)
21987 +{
21988 +       int err;
21989 +       struct hlist_bl_head *aopen;
21990 +
21991 +       AuLabel(here);
21992 +       aopen = &au_sbi(dentry->d_sb)->si_aopen;
21993 +       au_hbl_add(&aopen_node->hblist, aopen);
21994 +       err = finish_open(file, dentry, au_do_aopen);
21995 +       au_hbl_del(&aopen_node->hblist, aopen);
21996 +       /* AuDbgFile(file); */
21997 +       AuDbg("%pd%s%s\n", dentry,
21998 +             (file->f_mode & FMODE_CREATED) ? " created" : "",
21999 +             (file->f_mode & FMODE_OPENED) ? " opened" : "");
22000 +
22001 +       AuTraceErr(err);
22002 +       return err;
22003 +}
22004 +
22005 +static int au_aopen_no_open(struct file *file, struct dentry *dentry)
22006 +{
22007 +       int err;
22008 +
22009 +       AuLabel(here);
22010 +       dget(dentry);
22011 +       err = finish_no_open(file, dentry);
22012 +
22013 +       AuTraceErr(err);
22014 +       return err;
22015 +}
22016 +
22017 +static int aufs_atomic_open(struct inode *dir, struct dentry *dentry,
22018 +                           struct file *file, unsigned int open_flag,
22019 +                           umode_t create_mode)
22020 +{
22021 +       int err, did_open;
22022 +       unsigned int lkup_flags;
22023 +       aufs_bindex_t bindex;
22024 +       struct super_block *sb;
22025 +       struct dentry *parent, *d;
22026 +       struct vfsub_aopen_args args = {
22027 +               .open_flag      = open_flag,
22028 +               .create_mode    = create_mode
22029 +       };
22030 +       struct aopen_node aopen_node = {
22031 +               .file   = file
22032 +       };
22033 +
22034 +       IMustLock(dir);
22035 +       AuDbg("open_flag 0%o\n", open_flag);
22036 +       AuDbgDentry(dentry);
22037 +
22038 +       err = 0;
22039 +       if (!au_di(dentry)) {
22040 +               lkup_flags = LOOKUP_OPEN;
22041 +               if (open_flag & O_CREAT)
22042 +                       lkup_flags |= LOOKUP_CREATE;
22043 +               d = aufs_lookup(dir, dentry, lkup_flags);
22044 +               if (IS_ERR(d)) {
22045 +                       err = PTR_ERR(d);
22046 +                       AuTraceErr(err);
22047 +                       goto out;
22048 +               } else if (d) {
22049 +                       /*
22050 +                        * obsoleted dentry found.
22051 +                        * another error will be returned later.
22052 +                        */
22053 +                       d_drop(d);
22054 +                       AuDbgDentry(d);
22055 +                       dput(d);
22056 +               }
22057 +               AuDbgDentry(dentry);
22058 +       }
22059 +
22060 +       if (d_is_positive(dentry)
22061 +           || d_unhashed(dentry)
22062 +           || d_unlinked(dentry)
22063 +           || !(open_flag & O_CREAT)) {
22064 +               err = au_aopen_no_open(file, dentry);
22065 +               goto out; /* success */
22066 +       }
22067 +
22068 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
22069 +       if (unlikely(err))
22070 +               goto out;
22071 +
22072 +       sb = dentry->d_sb;
22073 +       parent = dentry->d_parent;      /* dir is locked */
22074 +       di_write_lock_parent(parent);
22075 +       err = au_lkup_dentry(dentry, /*btop*/0, AuLkup_ALLOW_NEG);
22076 +       if (unlikely(err < 0))
22077 +               goto out_parent;
22078 +
22079 +       AuDbgDentry(dentry);
22080 +       if (d_is_positive(dentry)) {
22081 +               err = au_aopen_no_open(file, dentry);
22082 +               goto out_parent; /* success */
22083 +       }
22084 +
22085 +       args.file = alloc_empty_file(file->f_flags, current_cred());
22086 +       err = PTR_ERR(args.file);
22087 +       if (IS_ERR(args.file))
22088 +               goto out_parent;
22089 +
22090 +       bindex = au_dbtop(dentry);
22091 +       err = au_aopen_or_create(dir, dentry, &args);
22092 +       AuTraceErr(err);
22093 +       AuDbgFile(args.file);
22094 +       file->f_mode = args.file->f_mode & ~FMODE_OPENED;
22095 +       did_open = !!(args.file->f_mode & FMODE_OPENED);
22096 +       if (!did_open) {
22097 +               fput(args.file);
22098 +               args.file = NULL;
22099 +       }
22100 +       di_write_unlock(parent);
22101 +       di_write_unlock(dentry);
22102 +       if (unlikely(err < 0)) {
22103 +               if (args.file)
22104 +                       fput(args.file);
22105 +               goto out_sb;
22106 +       }
22107 +
22108 +       if (!did_open)
22109 +               err = au_aopen_no_open(file, dentry);
22110 +       else {
22111 +               aopen_node.h_file = args.file;
22112 +               err = au_aopen_do_open(file, dentry, &aopen_node);
22113 +       }
22114 +       if (unlikely(err < 0)) {
22115 +               if (args.file)
22116 +                       fput(args.file);
22117 +               if (did_open)
22118 +                       au_lcnt_dec(&args.br->br_nfiles);
22119 +       }
22120 +       goto out_sb; /* success */
22121 +
22122 +out_parent:
22123 +       di_write_unlock(parent);
22124 +       di_write_unlock(dentry);
22125 +out_sb:
22126 +       si_read_unlock(sb);
22127 +out:
22128 +       AuTraceErr(err);
22129 +       AuDbgFile(file);
22130 +       return err;
22131 +}
22132 +
22133 +
22134 +/* ---------------------------------------------------------------------- */
22135 +
22136 +static int au_wr_dir_cpup(struct dentry *dentry, struct dentry *parent,
22137 +                         const unsigned char add_entry, aufs_bindex_t bcpup,
22138 +                         aufs_bindex_t btop)
22139 +{
22140 +       int err;
22141 +       struct dentry *h_parent;
22142 +       struct inode *h_dir;
22143 +
22144 +       if (add_entry)
22145 +               IMustLock(d_inode(parent));
22146 +       else
22147 +               di_write_lock_parent(parent);
22148 +
22149 +       err = 0;
22150 +       if (!au_h_dptr(parent, bcpup)) {
22151 +               if (btop > bcpup)
22152 +                       err = au_cpup_dirs(dentry, bcpup);
22153 +               else if (btop < bcpup)
22154 +                       err = au_cpdown_dirs(dentry, bcpup);
22155 +               else
22156 +                       BUG();
22157 +       }
22158 +       if (!err && add_entry && !au_ftest_wrdir(add_entry, TMPFILE)) {
22159 +               h_parent = au_h_dptr(parent, bcpup);
22160 +               h_dir = d_inode(h_parent);
22161 +               inode_lock_shared_nested(h_dir, AuLsc_I_PARENT);
22162 +               err = au_lkup_neg(dentry, bcpup, /*wh*/0);
22163 +               /* todo: no unlock here */
22164 +               inode_unlock_shared(h_dir);
22165 +
22166 +               AuDbg("bcpup %d\n", bcpup);
22167 +               if (!err) {
22168 +                       if (d_really_is_negative(dentry))
22169 +                               au_set_h_dptr(dentry, btop, NULL);
22170 +                       au_update_dbrange(dentry, /*do_put_zero*/0);
22171 +               }
22172 +       }
22173 +
22174 +       if (!add_entry)
22175 +               di_write_unlock(parent);
22176 +       if (!err)
22177 +               err = bcpup; /* success */
22178 +
22179 +       AuTraceErr(err);
22180 +       return err;
22181 +}
22182 +
22183 +/*
22184 + * decide the branch and the parent dir where we will create a new entry.
22185 + * returns new bindex or an error.
22186 + * copyup the parent dir if needed.
22187 + */
22188 +int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
22189 +             struct au_wr_dir_args *args)
22190 +{
22191 +       int err;
22192 +       unsigned int flags;
22193 +       aufs_bindex_t bcpup, btop, src_btop;
22194 +       const unsigned char add_entry
22195 +               = au_ftest_wrdir(args->flags, ADD_ENTRY)
22196 +               | au_ftest_wrdir(args->flags, TMPFILE);
22197 +       struct super_block *sb;
22198 +       struct dentry *parent;
22199 +       struct au_sbinfo *sbinfo;
22200 +
22201 +       sb = dentry->d_sb;
22202 +       sbinfo = au_sbi(sb);
22203 +       parent = dget_parent(dentry);
22204 +       btop = au_dbtop(dentry);
22205 +       bcpup = btop;
22206 +       if (args->force_btgt < 0) {
22207 +               if (src_dentry) {
22208 +                       src_btop = au_dbtop(src_dentry);
22209 +                       if (src_btop < btop)
22210 +                               bcpup = src_btop;
22211 +               } else if (add_entry) {
22212 +                       flags = 0;
22213 +                       if (au_ftest_wrdir(args->flags, ISDIR))
22214 +                               au_fset_wbr(flags, DIR);
22215 +                       err = AuWbrCreate(sbinfo, dentry, flags);
22216 +                       bcpup = err;
22217 +               }
22218 +
22219 +               if (bcpup < 0 || au_test_ro(sb, bcpup, d_inode(dentry))) {
22220 +                       if (add_entry)
22221 +                               err = AuWbrCopyup(sbinfo, dentry);
22222 +                       else {
22223 +                               if (!IS_ROOT(dentry)) {
22224 +                                       di_read_lock_parent(parent, !AuLock_IR);
22225 +                                       err = AuWbrCopyup(sbinfo, dentry);
22226 +                                       di_read_unlock(parent, !AuLock_IR);
22227 +                               } else
22228 +                                       err = AuWbrCopyup(sbinfo, dentry);
22229 +                       }
22230 +                       bcpup = err;
22231 +                       if (unlikely(err < 0))
22232 +                               goto out;
22233 +               }
22234 +       } else {
22235 +               bcpup = args->force_btgt;
22236 +               AuDebugOn(au_test_ro(sb, bcpup, d_inode(dentry)));
22237 +       }
22238 +
22239 +       AuDbg("btop %d, bcpup %d\n", btop, bcpup);
22240 +       err = bcpup;
22241 +       if (bcpup == btop)
22242 +               goto out; /* success */
22243 +
22244 +       /* copyup the new parent into the branch we process */
22245 +       err = au_wr_dir_cpup(dentry, parent, add_entry, bcpup, btop);
22246 +       if (err >= 0) {
22247 +               if (d_really_is_negative(dentry)) {
22248 +                       au_set_h_dptr(dentry, btop, NULL);
22249 +                       au_set_dbtop(dentry, bcpup);
22250 +                       au_set_dbbot(dentry, bcpup);
22251 +               }
22252 +               AuDebugOn(add_entry
22253 +                         && !au_ftest_wrdir(args->flags, TMPFILE)
22254 +                         && !au_h_dptr(dentry, bcpup));
22255 +       }
22256 +
22257 +out:
22258 +       dput(parent);
22259 +       return err;
22260 +}
22261 +
22262 +/* ---------------------------------------------------------------------- */
22263 +
22264 +void au_pin_hdir_unlock(struct au_pin *p)
22265 +{
22266 +       if (p->hdir)
22267 +               au_hn_inode_unlock(p->hdir);
22268 +}
22269 +
22270 +int au_pin_hdir_lock(struct au_pin *p)
22271 +{
22272 +       int err;
22273 +
22274 +       err = 0;
22275 +       if (!p->hdir)
22276 +               goto out;
22277 +
22278 +       /* even if an error happens later, keep this lock */
22279 +       au_hn_inode_lock_nested(p->hdir, p->lsc_hi);
22280 +
22281 +       err = -EBUSY;
22282 +       if (unlikely(p->hdir->hi_inode != d_inode(p->h_parent)))
22283 +               goto out;
22284 +
22285 +       err = 0;
22286 +       if (p->h_dentry)
22287 +               err = au_h_verify(p->h_dentry, p->udba, p->hdir->hi_inode,
22288 +                                 p->h_parent, p->br);
22289 +
22290 +out:
22291 +       return err;
22292 +}
22293 +
22294 +int au_pin_hdir_relock(struct au_pin *p)
22295 +{
22296 +       int err, i;
22297 +       struct inode *h_i;
22298 +       struct dentry *h_d[] = {
22299 +               p->h_dentry,
22300 +               p->h_parent
22301 +       };
22302 +
22303 +       err = au_pin_hdir_lock(p);
22304 +       if (unlikely(err))
22305 +               goto out;
22306 +
22307 +       for (i = 0; !err && i < sizeof(h_d)/sizeof(*h_d); i++) {
22308 +               if (!h_d[i])
22309 +                       continue;
22310 +               if (d_is_positive(h_d[i])) {
22311 +                       h_i = d_inode(h_d[i]);
22312 +                       err = !h_i->i_nlink;
22313 +               }
22314 +       }
22315 +
22316 +out:
22317 +       return err;
22318 +}
22319 +
22320 +static void au_pin_hdir_set_owner(struct au_pin *p, struct task_struct *task)
22321 +{
22322 +       atomic_long_set(&p->hdir->hi_inode->i_rwsem.owner, (long)task);
22323 +}
22324 +
22325 +void au_pin_hdir_acquire_nest(struct au_pin *p)
22326 +{
22327 +       if (p->hdir) {
22328 +               rwsem_acquire_nest(&p->hdir->hi_inode->i_rwsem.dep_map,
22329 +                                  p->lsc_hi, 0, NULL, _RET_IP_);
22330 +               au_pin_hdir_set_owner(p, current);
22331 +       }
22332 +}
22333 +
22334 +void au_pin_hdir_release(struct au_pin *p)
22335 +{
22336 +       if (p->hdir) {
22337 +               au_pin_hdir_set_owner(p, p->task);
22338 +               rwsem_release(&p->hdir->hi_inode->i_rwsem.dep_map, _RET_IP_);
22339 +       }
22340 +}
22341 +
22342 +struct dentry *au_pinned_h_parent(struct au_pin *pin)
22343 +{
22344 +       if (pin && pin->parent)
22345 +               return au_h_dptr(pin->parent, pin->bindex);
22346 +       return NULL;
22347 +}
22348 +
22349 +void au_unpin(struct au_pin *p)
22350 +{
22351 +       if (p->hdir)
22352 +               au_pin_hdir_unlock(p);
22353 +       if (p->h_mnt && au_ftest_pin(p->flags, MNT_WRITE))
22354 +               vfsub_mnt_drop_write(p->h_mnt);
22355 +       if (!p->hdir)
22356 +               return;
22357 +
22358 +       if (!au_ftest_pin(p->flags, DI_LOCKED))
22359 +               di_read_unlock(p->parent, AuLock_IR);
22360 +       iput(p->hdir->hi_inode);
22361 +       dput(p->parent);
22362 +       p->parent = NULL;
22363 +       p->hdir = NULL;
22364 +       p->h_mnt = NULL;
22365 +       /* do not clear p->task */
22366 +}
22367 +
22368 +int au_do_pin(struct au_pin *p)
22369 +{
22370 +       int err;
22371 +       struct super_block *sb;
22372 +       struct inode *h_dir;
22373 +
22374 +       err = 0;
22375 +       sb = p->dentry->d_sb;
22376 +       p->br = au_sbr(sb, p->bindex);
22377 +       if (IS_ROOT(p->dentry)) {
22378 +               if (au_ftest_pin(p->flags, MNT_WRITE)) {
22379 +                       p->h_mnt = au_br_mnt(p->br);
22380 +                       err = vfsub_mnt_want_write(p->h_mnt);
22381 +                       if (unlikely(err)) {
22382 +                               au_fclr_pin(p->flags, MNT_WRITE);
22383 +                               goto out_err;
22384 +                       }
22385 +               }
22386 +               goto out;
22387 +       }
22388 +
22389 +       p->h_dentry = NULL;
22390 +       if (p->bindex <= au_dbbot(p->dentry))
22391 +               p->h_dentry = au_h_dptr(p->dentry, p->bindex);
22392 +
22393 +       p->parent = dget_parent(p->dentry);
22394 +       if (!au_ftest_pin(p->flags, DI_LOCKED))
22395 +               di_read_lock(p->parent, AuLock_IR, p->lsc_di);
22396 +
22397 +       h_dir = NULL;
22398 +       p->h_parent = au_h_dptr(p->parent, p->bindex);
22399 +       p->hdir = au_hi(d_inode(p->parent), p->bindex);
22400 +       if (p->hdir)
22401 +               h_dir = p->hdir->hi_inode;
22402 +
22403 +       /*
22404 +        * udba case, or
22405 +        * if DI_LOCKED is not set, then p->parent may be different
22406 +        * and h_parent can be NULL.
22407 +        */
22408 +       if (unlikely(!p->hdir || !h_dir || !p->h_parent)) {
22409 +               err = -EBUSY;
22410 +               if (!au_ftest_pin(p->flags, DI_LOCKED))
22411 +                       di_read_unlock(p->parent, AuLock_IR);
22412 +               dput(p->parent);
22413 +               p->parent = NULL;
22414 +               goto out_err;
22415 +       }
22416 +
22417 +       if (au_ftest_pin(p->flags, MNT_WRITE)) {
22418 +               p->h_mnt = au_br_mnt(p->br);
22419 +               err = vfsub_mnt_want_write(p->h_mnt);
22420 +               if (unlikely(err)) {
22421 +                       au_fclr_pin(p->flags, MNT_WRITE);
22422 +                       if (!au_ftest_pin(p->flags, DI_LOCKED))
22423 +                               di_read_unlock(p->parent, AuLock_IR);
22424 +                       dput(p->parent);
22425 +                       p->parent = NULL;
22426 +                       goto out_err;
22427 +               }
22428 +       }
22429 +
22430 +       au_igrab(h_dir);
22431 +       err = au_pin_hdir_lock(p);
22432 +       if (!err)
22433 +               goto out; /* success */
22434 +
22435 +       au_unpin(p);
22436 +
22437 +out_err:
22438 +       pr_err("err %d\n", err);
22439 +       err = au_busy_or_stale();
22440 +out:
22441 +       return err;
22442 +}
22443 +
22444 +void au_pin_init(struct au_pin *p, struct dentry *dentry,
22445 +                aufs_bindex_t bindex, int lsc_di, int lsc_hi,
22446 +                unsigned int udba, unsigned char flags)
22447 +{
22448 +       p->dentry = dentry;
22449 +       p->udba = udba;
22450 +       p->lsc_di = lsc_di;
22451 +       p->lsc_hi = lsc_hi;
22452 +       p->flags = flags;
22453 +       p->bindex = bindex;
22454 +
22455 +       p->parent = NULL;
22456 +       p->hdir = NULL;
22457 +       p->h_mnt = NULL;
22458 +
22459 +       p->h_dentry = NULL;
22460 +       p->h_parent = NULL;
22461 +       p->br = NULL;
22462 +       p->task = current;
22463 +}
22464 +
22465 +int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
22466 +          unsigned int udba, unsigned char flags)
22467 +{
22468 +       au_pin_init(pin, dentry, bindex, AuLsc_DI_PARENT, AuLsc_I_PARENT2,
22469 +                   udba, flags);
22470 +       return au_do_pin(pin);
22471 +}
22472 +
22473 +/* ---------------------------------------------------------------------- */
22474 +
22475 +/*
22476 + * ->setattr() and ->getattr() are called in various cases.
22477 + * chmod, stat: dentry is revalidated.
22478 + * fchmod, fstat: file and dentry are not revalidated, additionally they may be
22479 + *               unhashed.
22480 + * for ->setattr(), ia->ia_file is passed from ftruncate only.
22481 + */
22482 +/* todo: consolidate with do_refresh() and simple_reval_dpath() */
22483 +int au_reval_for_attr(struct dentry *dentry, unsigned int sigen)
22484 +{
22485 +       int err;
22486 +       struct dentry *parent;
22487 +
22488 +       err = 0;
22489 +       if (au_digen_test(dentry, sigen)) {
22490 +               parent = dget_parent(dentry);
22491 +               di_read_lock_parent(parent, AuLock_IR);
22492 +               err = au_refresh_dentry(dentry, parent);
22493 +               di_read_unlock(parent, AuLock_IR);
22494 +               dput(parent);
22495 +       }
22496 +
22497 +       AuTraceErr(err);
22498 +       return err;
22499 +}
22500 +
22501 +int au_pin_and_icpup(struct dentry *dentry, struct iattr *ia,
22502 +                    struct au_icpup_args *a)
22503 +{
22504 +       int err;
22505 +       loff_t sz;
22506 +       aufs_bindex_t btop, ibtop;
22507 +       struct dentry *hi_wh, *parent;
22508 +       struct inode *inode;
22509 +       struct au_wr_dir_args wr_dir_args = {
22510 +               .force_btgt     = -1,
22511 +               .flags          = 0
22512 +       };
22513 +
22514 +       if (d_is_dir(dentry))
22515 +               au_fset_wrdir(wr_dir_args.flags, ISDIR);
22516 +       /* plink or hi_wh() case */
22517 +       btop = au_dbtop(dentry);
22518 +       inode = d_inode(dentry);
22519 +       ibtop = au_ibtop(inode);
22520 +       if (btop != ibtop && !au_test_ro(inode->i_sb, ibtop, inode))
22521 +               wr_dir_args.force_btgt = ibtop;
22522 +       err = au_wr_dir(dentry, /*src_dentry*/NULL, &wr_dir_args);
22523 +       if (unlikely(err < 0))
22524 +               goto out;
22525 +       a->btgt = err;
22526 +       if (err != btop)
22527 +               au_fset_icpup(a->flags, DID_CPUP);
22528 +
22529 +       err = 0;
22530 +       a->pin_flags = AuPin_MNT_WRITE;
22531 +       parent = NULL;
22532 +       if (!IS_ROOT(dentry)) {
22533 +               au_fset_pin(a->pin_flags, DI_LOCKED);
22534 +               parent = dget_parent(dentry);
22535 +               di_write_lock_parent(parent);
22536 +       }
22537 +
22538 +       err = au_pin(&a->pin, dentry, a->btgt, a->udba, a->pin_flags);
22539 +       if (unlikely(err))
22540 +               goto out_parent;
22541 +
22542 +       sz = -1;
22543 +       a->h_path.dentry = au_h_dptr(dentry, btop);
22544 +       a->h_inode = d_inode(a->h_path.dentry);
22545 +       if (ia && (ia->ia_valid & ATTR_SIZE)) {
22546 +               inode_lock_shared_nested(a->h_inode, AuLsc_I_CHILD);
22547 +               if (ia->ia_size < i_size_read(a->h_inode))
22548 +                       sz = ia->ia_size;
22549 +               inode_unlock_shared(a->h_inode);
22550 +       }
22551 +
22552 +       hi_wh = NULL;
22553 +       if (au_ftest_icpup(a->flags, DID_CPUP) && d_unlinked(dentry)) {
22554 +               hi_wh = au_hi_wh(inode, a->btgt);
22555 +               if (!hi_wh) {
22556 +                       struct au_cp_generic cpg = {
22557 +                               .dentry = dentry,
22558 +                               .bdst   = a->btgt,
22559 +                               .bsrc   = -1,
22560 +                               .len    = sz,
22561 +                               .pin    = &a->pin
22562 +                       };
22563 +                       err = au_sio_cpup_wh(&cpg, /*file*/NULL);
22564 +                       if (unlikely(err))
22565 +                               goto out_unlock;
22566 +                       hi_wh = au_hi_wh(inode, a->btgt);
22567 +                       /* todo: revalidate hi_wh? */
22568 +               }
22569 +       }
22570 +
22571 +       if (parent) {
22572 +               au_pin_set_parent_lflag(&a->pin, /*lflag*/0);
22573 +               di_downgrade_lock(parent, AuLock_IR);
22574 +               dput(parent);
22575 +               parent = NULL;
22576 +       }
22577 +       if (!au_ftest_icpup(a->flags, DID_CPUP))
22578 +               goto out; /* success */
22579 +
22580 +       if (!d_unhashed(dentry)) {
22581 +               struct au_cp_generic cpg = {
22582 +                       .dentry = dentry,
22583 +                       .bdst   = a->btgt,
22584 +                       .bsrc   = btop,
22585 +                       .len    = sz,
22586 +                       .pin    = &a->pin,
22587 +                       .flags  = AuCpup_DTIME | AuCpup_HOPEN
22588 +               };
22589 +               err = au_sio_cpup_simple(&cpg);
22590 +               if (!err)
22591 +                       a->h_path.dentry = au_h_dptr(dentry, a->btgt);
22592 +       } else if (!hi_wh)
22593 +               a->h_path.dentry = au_h_dptr(dentry, a->btgt);
22594 +       else
22595 +               a->h_path.dentry = hi_wh; /* do not dget here */
22596 +
22597 +out_unlock:
22598 +       a->h_inode = d_inode(a->h_path.dentry);
22599 +       if (!err)
22600 +               goto out; /* success */
22601 +       au_unpin(&a->pin);
22602 +out_parent:
22603 +       if (parent) {
22604 +               di_write_unlock(parent);
22605 +               dput(parent);
22606 +       }
22607 +out:
22608 +       if (!err)
22609 +               inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
22610 +       return err;
22611 +}
22612 +
22613 +static int aufs_setattr(struct user_namespace *userns, struct dentry *dentry,
22614 +                       struct iattr *ia)
22615 +{
22616 +       int err;
22617 +       struct inode *inode, *delegated;
22618 +       struct super_block *sb;
22619 +       struct file *file;
22620 +       struct au_icpup_args *a;
22621 +       struct user_namespace *h_userns;
22622 +
22623 +       inode = d_inode(dentry);
22624 +       IMustLock(inode);
22625 +
22626 +       err = setattr_prepare(userns, dentry, ia);
22627 +       if (unlikely(err))
22628 +               goto out;
22629 +
22630 +       err = -ENOMEM;
22631 +       a = kzalloc(sizeof(*a), GFP_NOFS);
22632 +       if (unlikely(!a))
22633 +               goto out;
22634 +
22635 +       if (ia->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID))
22636 +               ia->ia_valid &= ~ATTR_MODE;
22637 +
22638 +       file = NULL;
22639 +       sb = dentry->d_sb;
22640 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
22641 +       if (unlikely(err))
22642 +               goto out_kfree;
22643 +
22644 +       if (ia->ia_valid & ATTR_FILE) {
22645 +               /* currently ftruncate(2) only */
22646 +               AuDebugOn(!d_is_reg(dentry));
22647 +               file = ia->ia_file;
22648 +               err = au_reval_and_lock_fdi(file, au_reopen_nondir, /*wlock*/1,
22649 +                                           /*fi_lsc*/0);
22650 +               if (unlikely(err))
22651 +                       goto out_si;
22652 +               ia->ia_file = au_hf_top(file);
22653 +               a->udba = AuOpt_UDBA_NONE;
22654 +       } else {
22655 +               /* fchmod() doesn't pass ia_file */
22656 +               a->udba = au_opt_udba(sb);
22657 +               di_write_lock_child(dentry);
22658 +               /* no d_unlinked(), to set UDBA_NONE for root */
22659 +               if (d_unhashed(dentry))
22660 +                       a->udba = AuOpt_UDBA_NONE;
22661 +               if (a->udba != AuOpt_UDBA_NONE) {
22662 +                       AuDebugOn(IS_ROOT(dentry));
22663 +                       err = au_reval_for_attr(dentry, au_sigen(sb));
22664 +                       if (unlikely(err))
22665 +                               goto out_dentry;
22666 +               }
22667 +       }
22668 +
22669 +       err = au_pin_and_icpup(dentry, ia, a);
22670 +       if (unlikely(err < 0))
22671 +               goto out_dentry;
22672 +       if (au_ftest_icpup(a->flags, DID_CPUP)) {
22673 +               ia->ia_file = NULL;
22674 +               ia->ia_valid &= ~ATTR_FILE;
22675 +       }
22676 +
22677 +       a->h_path.mnt = au_sbr_mnt(sb, a->btgt);
22678 +       if ((ia->ia_valid & (ATTR_MODE | ATTR_CTIME))
22679 +           == (ATTR_MODE | ATTR_CTIME)) {
22680 +               err = security_path_chmod(&a->h_path, ia->ia_mode);
22681 +               if (unlikely(err))
22682 +                       goto out_unlock;
22683 +       } else if ((ia->ia_valid & (ATTR_UID | ATTR_GID))
22684 +                  && (ia->ia_valid & ATTR_CTIME)) {
22685 +               err = security_path_chown(&a->h_path, ia->ia_uid, ia->ia_gid);
22686 +               if (unlikely(err))
22687 +                       goto out_unlock;
22688 +       }
22689 +
22690 +       if (ia->ia_valid & ATTR_SIZE) {
22691 +               struct file *f;
22692 +
22693 +               if (ia->ia_size < i_size_read(inode))
22694 +                       /* unmap only */
22695 +                       truncate_setsize(inode, ia->ia_size);
22696 +
22697 +               f = NULL;
22698 +               if (ia->ia_valid & ATTR_FILE)
22699 +                       f = ia->ia_file;
22700 +               inode_unlock(a->h_inode);
22701 +               err = vfsub_trunc(&a->h_path, ia->ia_size, ia->ia_valid, f);
22702 +               inode_lock_nested(a->h_inode, AuLsc_I_CHILD);
22703 +       } else {
22704 +               delegated = NULL;
22705 +               while (1) {
22706 +                       err = vfsub_notify_change(&a->h_path, ia, &delegated);
22707 +                       if (delegated) {
22708 +                               err = break_deleg_wait(&delegated);
22709 +                               if (!err)
22710 +                                       continue;
22711 +                       }
22712 +                       break;
22713 +               }
22714 +       }
22715 +       /*
22716 +        * regardless aufs 'acl' option setting.
22717 +        * why don't all acl-aware fs call this func from their ->setattr()?
22718 +        */
22719 +       if (!err && (ia->ia_valid & ATTR_MODE)) {
22720 +               h_userns = mnt_user_ns(a->h_path.mnt);
22721 +               err = vfsub_acl_chmod(h_userns, a->h_inode, ia->ia_mode);
22722 +       }
22723 +       if (!err)
22724 +               au_cpup_attr_changeable(inode);
22725 +
22726 +out_unlock:
22727 +       inode_unlock(a->h_inode);
22728 +       au_unpin(&a->pin);
22729 +       if (unlikely(err))
22730 +               au_update_dbtop(dentry);
22731 +out_dentry:
22732 +       di_write_unlock(dentry);
22733 +       if (file) {
22734 +               fi_write_unlock(file);
22735 +               ia->ia_file = file;
22736 +               ia->ia_valid |= ATTR_FILE;
22737 +       }
22738 +out_si:
22739 +       si_read_unlock(sb);
22740 +out_kfree:
22741 +       au_kfree_rcu(a);
22742 +out:
22743 +       AuTraceErr(err);
22744 +       return err;
22745 +}
22746 +
22747 +#if IS_ENABLED(CONFIG_AUFS_XATTR) || IS_ENABLED(CONFIG_FS_POSIX_ACL)
22748 +static int au_h_path_to_set_attr(struct dentry *dentry,
22749 +                                struct au_icpup_args *a, struct path *h_path)
22750 +{
22751 +       int err;
22752 +       struct super_block *sb;
22753 +
22754 +       sb = dentry->d_sb;
22755 +       a->udba = au_opt_udba(sb);
22756 +       /* no d_unlinked(), to set UDBA_NONE for root */
22757 +       if (d_unhashed(dentry))
22758 +               a->udba = AuOpt_UDBA_NONE;
22759 +       if (a->udba != AuOpt_UDBA_NONE) {
22760 +               AuDebugOn(IS_ROOT(dentry));
22761 +               err = au_reval_for_attr(dentry, au_sigen(sb));
22762 +               if (unlikely(err))
22763 +                       goto out;
22764 +       }
22765 +       err = au_pin_and_icpup(dentry, /*ia*/NULL, a);
22766 +       if (unlikely(err < 0))
22767 +               goto out;
22768 +
22769 +       h_path->dentry = a->h_path.dentry;
22770 +       h_path->mnt = au_sbr_mnt(sb, a->btgt);
22771 +
22772 +out:
22773 +       return err;
22774 +}
22775 +
22776 +ssize_t au_sxattr(struct dentry *dentry, struct inode *inode,
22777 +                 struct au_sxattr *arg)
22778 +{
22779 +       int err;
22780 +       struct path h_path;
22781 +       struct super_block *sb;
22782 +       struct au_icpup_args *a;
22783 +       struct inode *h_inode;
22784 +       struct user_namespace *h_userns;
22785 +
22786 +       IMustLock(inode);
22787 +
22788 +       err = -ENOMEM;
22789 +       a = kzalloc(sizeof(*a), GFP_NOFS);
22790 +       if (unlikely(!a))
22791 +               goto out;
22792 +
22793 +       sb = dentry->d_sb;
22794 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
22795 +       if (unlikely(err))
22796 +               goto out_kfree;
22797 +
22798 +       h_path.dentry = NULL;   /* silence gcc */
22799 +       di_write_lock_child(dentry);
22800 +       err = au_h_path_to_set_attr(dentry, a, &h_path);
22801 +       if (unlikely(err))
22802 +               goto out_di;
22803 +       h_userns = mnt_user_ns(h_path.mnt);
22804 +
22805 +       inode_unlock(a->h_inode);
22806 +       switch (arg->type) {
22807 +       case AU_XATTR_SET:
22808 +               AuDebugOn(d_is_negative(h_path.dentry));
22809 +               err = vfsub_setxattr(h_userns, h_path.dentry,
22810 +                                    arg->u.set.name, arg->u.set.value,
22811 +                                    arg->u.set.size, arg->u.set.flags);
22812 +               break;
22813 +       case AU_ACL_SET:
22814 +               err = -EOPNOTSUPP;
22815 +               h_inode = d_inode(h_path.dentry);
22816 +               if (h_inode->i_op->set_acl) {
22817 +                       /* this will call posix_acl_update_mode */
22818 +                       err = h_inode->i_op->set_acl(h_userns, h_inode,
22819 +                                                    arg->u.acl_set.acl,
22820 +                                                    arg->u.acl_set.type);
22821 +               }
22822 +               break;
22823 +       }
22824 +       if (!err)
22825 +               au_cpup_attr_timesizes(inode);
22826 +
22827 +       au_unpin(&a->pin);
22828 +       if (unlikely(err))
22829 +               au_update_dbtop(dentry);
22830 +
22831 +out_di:
22832 +       di_write_unlock(dentry);
22833 +       si_read_unlock(sb);
22834 +out_kfree:
22835 +       au_kfree_rcu(a);
22836 +out:
22837 +       AuTraceErr(err);
22838 +       return err;
22839 +}
22840 +#endif
22841 +
22842 +static void au_refresh_iattr(struct inode *inode, struct kstat *st,
22843 +                            unsigned int nlink)
22844 +{
22845 +       unsigned int n;
22846 +
22847 +       inode->i_mode = st->mode;
22848 +       /* don't i_[ug]id_write() here */
22849 +       inode->i_uid = st->uid;
22850 +       inode->i_gid = st->gid;
22851 +       inode->i_atime = st->atime;
22852 +       inode->i_mtime = st->mtime;
22853 +       inode->i_ctime = st->ctime;
22854 +
22855 +       au_cpup_attr_nlink(inode, /*force*/0);
22856 +       if (S_ISDIR(inode->i_mode)) {
22857 +               n = inode->i_nlink;
22858 +               n -= nlink;
22859 +               n += st->nlink;
22860 +               smp_mb(); /* for i_nlink */
22861 +               /* 0 can happen */
22862 +               set_nlink(inode, n);
22863 +       }
22864 +
22865 +       spin_lock(&inode->i_lock);
22866 +       inode->i_blocks = st->blocks;
22867 +       i_size_write(inode, st->size);
22868 +       spin_unlock(&inode->i_lock);
22869 +}
22870 +
22871 +/*
22872 + * common routine for aufs_getattr() and au_getxattr().
22873 + * returns zero or negative (an error).
22874 + * @dentry will be read-locked in success.
22875 + */
22876 +int au_h_path_getattr(struct dentry *dentry, struct inode *inode, int force,
22877 +                     struct path *h_path, int locked)
22878 +{
22879 +       int err;
22880 +       unsigned int mnt_flags, sigen;
22881 +       unsigned char udba_none;
22882 +       aufs_bindex_t bindex;
22883 +       struct super_block *sb, *h_sb;
22884 +
22885 +       h_path->mnt = NULL;
22886 +       h_path->dentry = NULL;
22887 +
22888 +       err = 0;
22889 +       sb = dentry->d_sb;
22890 +       mnt_flags = au_mntflags(sb);
22891 +       udba_none = !!au_opt_test(mnt_flags, UDBA_NONE);
22892 +
22893 +       if (unlikely(locked))
22894 +               goto body; /* skip locking dinfo */
22895 +
22896 +       /* support fstat(2) */
22897 +       if (!d_unlinked(dentry) && !udba_none) {
22898 +               sigen = au_sigen(sb);
22899 +               err = au_digen_test(dentry, sigen);
22900 +               if (!err) {
22901 +                       di_read_lock_child(dentry, AuLock_IR);
22902 +                       err = au_dbrange_test(dentry);
22903 +                       if (unlikely(err)) {
22904 +                               di_read_unlock(dentry, AuLock_IR);
22905 +                               goto out;
22906 +                       }
22907 +               } else {
22908 +                       AuDebugOn(IS_ROOT(dentry));
22909 +                       di_write_lock_child(dentry);
22910 +                       err = au_dbrange_test(dentry);
22911 +                       if (!err)
22912 +                               err = au_reval_for_attr(dentry, sigen);
22913 +                       if (!err)
22914 +                               di_downgrade_lock(dentry, AuLock_IR);
22915 +                       else {
22916 +                               di_write_unlock(dentry);
22917 +                               goto out;
22918 +                       }
22919 +               }
22920 +       } else
22921 +               di_read_lock_child(dentry, AuLock_IR);
22922 +
22923 +body:
22924 +       if (!inode) {
22925 +               inode = d_inode(dentry);
22926 +               if (unlikely(!inode))
22927 +                       goto out;
22928 +       }
22929 +       bindex = au_ibtop(inode);
22930 +       h_path->mnt = au_sbr_mnt(sb, bindex);
22931 +       h_sb = h_path->mnt->mnt_sb;
22932 +       if (!force
22933 +           && !au_test_fs_bad_iattr(h_sb)
22934 +           && udba_none)
22935 +               goto out; /* success */
22936 +
22937 +       if (au_dbtop(dentry) == bindex)
22938 +               h_path->dentry = au_h_dptr(dentry, bindex);
22939 +       else if (au_opt_test(mnt_flags, PLINK) && au_plink_test(inode)) {
22940 +               h_path->dentry = au_plink_lkup(inode, bindex);
22941 +               if (IS_ERR(h_path->dentry))
22942 +                       /* pretending success */
22943 +                       h_path->dentry = NULL;
22944 +               else
22945 +                       dput(h_path->dentry);
22946 +       }
22947 +
22948 +out:
22949 +       return err;
22950 +}
22951 +
22952 +static int aufs_getattr(struct user_namespace *userns, const struct path *path,
22953 +                       struct kstat *st, u32 request, unsigned int query)
22954 +{
22955 +       int err;
22956 +       unsigned char positive;
22957 +       struct path h_path;
22958 +       struct dentry *dentry;
22959 +       struct inode *inode;
22960 +       struct super_block *sb;
22961 +
22962 +       dentry = path->dentry;
22963 +       inode = d_inode(dentry);
22964 +       sb = dentry->d_sb;
22965 +       err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
22966 +       if (unlikely(err))
22967 +               goto out;
22968 +       err = au_h_path_getattr(dentry, /*inode*/NULL, /*force*/0, &h_path,
22969 +                               /*locked*/0);
22970 +       if (unlikely(err))
22971 +               goto out_si;
22972 +       if (unlikely(!h_path.dentry))
22973 +               /* illegally overlapped or something */
22974 +               goto out_fill; /* pretending success */
22975 +
22976 +       positive = d_is_positive(h_path.dentry);
22977 +       if (positive)
22978 +               /* no vfsub version */
22979 +               err = vfs_getattr(&h_path, st, request, query);
22980 +       if (!err) {
22981 +               if (positive)
22982 +                       au_refresh_iattr(inode, st,
22983 +                                        d_inode(h_path.dentry)->i_nlink);
22984 +               goto out_fill; /* success */
22985 +       }
22986 +       AuTraceErr(err);
22987 +       goto out_di;
22988 +
22989 +out_fill:
22990 +       generic_fillattr(userns, inode, st);
22991 +out_di:
22992 +       di_read_unlock(dentry, AuLock_IR);
22993 +out_si:
22994 +       si_read_unlock(sb);
22995 +out:
22996 +       AuTraceErr(err);
22997 +       return err;
22998 +}
22999 +
23000 +/* ---------------------------------------------------------------------- */
23001 +
23002 +static const char *aufs_get_link(struct dentry *dentry, struct inode *inode,
23003 +                                struct delayed_call *done)
23004 +{
23005 +       const char *ret;
23006 +       struct dentry *h_dentry;
23007 +       struct inode *h_inode;
23008 +       int err;
23009 +       aufs_bindex_t bindex;
23010 +
23011 +       ret = NULL; /* suppress a warning */
23012 +       err = -ECHILD;
23013 +       if (!dentry)
23014 +               goto out;
23015 +
23016 +       err = aufs_read_lock(dentry, AuLock_IR | AuLock_GEN);
23017 +       if (unlikely(err))
23018 +               goto out;
23019 +
23020 +       err = au_d_hashed_positive(dentry);
23021 +       if (unlikely(err))
23022 +               goto out_unlock;
23023 +
23024 +       err = -EINVAL;
23025 +       inode = d_inode(dentry);
23026 +       bindex = au_ibtop(inode);
23027 +       h_inode = au_h_iptr(inode, bindex);
23028 +       if (unlikely(!h_inode->i_op->get_link))
23029 +               goto out_unlock;
23030 +
23031 +       err = -EBUSY;
23032 +       h_dentry = NULL;
23033 +       if (au_dbtop(dentry) <= bindex) {
23034 +               h_dentry = au_h_dptr(dentry, bindex);
23035 +               if (h_dentry)
23036 +                       dget(h_dentry);
23037 +       }
23038 +       if (!h_dentry) {
23039 +               h_dentry = d_find_any_alias(h_inode);
23040 +               if (IS_ERR(h_dentry)) {
23041 +                       err = PTR_ERR(h_dentry);
23042 +                       goto out_unlock;
23043 +               }
23044 +       }
23045 +       if (unlikely(!h_dentry))
23046 +               goto out_unlock;
23047 +
23048 +       err = 0;
23049 +       AuDbg("%ps\n", h_inode->i_op->get_link);
23050 +       AuDbgDentry(h_dentry);
23051 +       ret = vfs_get_link(h_dentry, done);
23052 +       dput(h_dentry);
23053 +       if (IS_ERR(ret))
23054 +               err = PTR_ERR(ret);
23055 +
23056 +out_unlock:
23057 +       aufs_read_unlock(dentry, AuLock_IR);
23058 +out:
23059 +       if (unlikely(err))
23060 +               ret = ERR_PTR(err);
23061 +       AuTraceErrPtr(ret);
23062 +       return ret;
23063 +}
23064 +
23065 +/* ---------------------------------------------------------------------- */
23066 +
23067 +static int au_is_special(struct inode *inode)
23068 +{
23069 +       return (inode->i_mode & (S_IFBLK | S_IFCHR | S_IFIFO | S_IFSOCK));
23070 +}
23071 +
23072 +static int aufs_update_time(struct inode *inode, struct timespec64 *ts,
23073 +                           int flags)
23074 +{
23075 +       int err;
23076 +       aufs_bindex_t bindex;
23077 +       struct super_block *sb;
23078 +       struct inode *h_inode;
23079 +       struct vfsmount *h_mnt;
23080 +
23081 +       sb = inode->i_sb;
23082 +       WARN_ONCE((flags & S_ATIME) && !IS_NOATIME(inode),
23083 +                 "unexpected s_flags 0x%lx", sb->s_flags);
23084 +
23085 +       /* mmap_sem might be acquired already, cf. aufs_mmap() */
23086 +       lockdep_off();
23087 +       si_read_lock(sb, AuLock_FLUSH);
23088 +       ii_write_lock_child(inode);
23089 +
23090 +       err = 0;
23091 +       bindex = au_ibtop(inode);
23092 +       h_inode = au_h_iptr(inode, bindex);
23093 +       if (!au_test_ro(sb, bindex, inode)) {
23094 +               h_mnt = au_sbr_mnt(sb, bindex);
23095 +               err = vfsub_mnt_want_write(h_mnt);
23096 +               if (!err) {
23097 +                       err = vfsub_update_time(h_inode, ts, flags);
23098 +                       vfsub_mnt_drop_write(h_mnt);
23099 +               }
23100 +       } else if (au_is_special(h_inode)) {
23101 +               /*
23102 +                * Never copy-up here.
23103 +                * These special files may already be opened and used for
23104 +                * communicating. If we copied it up, then the communication
23105 +                * would be corrupted.
23106 +                */
23107 +               AuWarn1("timestamps for i%lu are ignored "
23108 +                       "since it is on readonly branch (hi%lu).\n",
23109 +                       inode->i_ino, h_inode->i_ino);
23110 +       } else if (flags & ~S_ATIME) {
23111 +               err = -EIO;
23112 +               AuIOErr1("unexpected flags 0x%x\n", flags);
23113 +               AuDebugOn(1);
23114 +       }
23115 +
23116 +       if (!err)
23117 +               au_cpup_attr_timesizes(inode);
23118 +       ii_write_unlock(inode);
23119 +       si_read_unlock(sb);
23120 +       lockdep_on();
23121 +
23122 +       if (!err && (flags & S_VERSION))
23123 +               inode_inc_iversion(inode);
23124 +
23125 +       return err;
23126 +}
23127 +
23128 +/* ---------------------------------------------------------------------- */
23129 +
23130 +/* no getattr version will be set by module.c:aufs_init() */
23131 +struct inode_operations aufs_iop_nogetattr[AuIop_Last],
23132 +       aufs_iop[] = {
23133 +       [AuIop_SYMLINK] = {
23134 +               .permission     = aufs_permission,
23135 +#ifdef CONFIG_FS_POSIX_ACL
23136 +               .get_acl        = aufs_get_acl,
23137 +               .set_acl        = aufs_set_acl, /* unsupport for symlink? */
23138 +#endif
23139 +
23140 +               .setattr        = aufs_setattr,
23141 +               .getattr        = aufs_getattr,
23142 +
23143 +#ifdef CONFIG_AUFS_XATTR
23144 +               .listxattr      = aufs_listxattr,
23145 +#endif
23146 +
23147 +               .get_link       = aufs_get_link,
23148 +
23149 +               /* .update_time = aufs_update_time */
23150 +       },
23151 +       [AuIop_DIR] = {
23152 +               .create         = aufs_create,
23153 +               .lookup         = aufs_lookup,
23154 +               .link           = aufs_link,
23155 +               .unlink         = aufs_unlink,
23156 +               .symlink        = aufs_symlink,
23157 +               .mkdir          = aufs_mkdir,
23158 +               .rmdir          = aufs_rmdir,
23159 +               .mknod          = aufs_mknod,
23160 +               .rename         = aufs_rename,
23161 +
23162 +               .permission     = aufs_permission,
23163 +#ifdef CONFIG_FS_POSIX_ACL
23164 +               .get_acl        = aufs_get_acl,
23165 +               .set_acl        = aufs_set_acl,
23166 +#endif
23167 +
23168 +               .setattr        = aufs_setattr,
23169 +               .getattr        = aufs_getattr,
23170 +
23171 +#ifdef CONFIG_AUFS_XATTR
23172 +               .listxattr      = aufs_listxattr,
23173 +#endif
23174 +
23175 +               .update_time    = aufs_update_time,
23176 +               .atomic_open    = aufs_atomic_open,
23177 +               .tmpfile        = aufs_tmpfile
23178 +       },
23179 +       [AuIop_OTHER] = {
23180 +               .permission     = aufs_permission,
23181 +#ifdef CONFIG_FS_POSIX_ACL
23182 +               .get_acl        = aufs_get_acl,
23183 +               .set_acl        = aufs_set_acl,
23184 +#endif
23185 +
23186 +               .setattr        = aufs_setattr,
23187 +               .getattr        = aufs_getattr,
23188 +
23189 +#ifdef CONFIG_AUFS_XATTR
23190 +               .listxattr      = aufs_listxattr,
23191 +#endif
23192 +
23193 +               .update_time    = aufs_update_time
23194 +       }
23195 +};
23196 diff -urN /usr/share/empty/fs/aufs/i_op_del.c linux/fs/aufs/i_op_del.c
23197 --- /usr/share/empty/fs/aufs/i_op_del.c 1970-01-01 01:00:00.000000000 +0100
23198 +++ linux/fs/aufs/i_op_del.c    2022-03-21 14:49:05.726633010 +0100
23199 @@ -0,0 +1,522 @@
23200 +// SPDX-License-Identifier: GPL-2.0
23201 +/*
23202 + * Copyright (C) 2005-2021 Junjiro R. Okajima
23203 + *
23204 + * This program, aufs is free software; you can redistribute it and/or modify
23205 + * it under the terms of the GNU General Public License as published by
23206 + * the Free Software Foundation; either version 2 of the License, or
23207 + * (at your option) any later version.
23208 + *
23209 + * This program is distributed in the hope that it will be useful,
23210 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
23211 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23212 + * GNU General Public License for more details.
23213 + *
23214 + * You should have received a copy of the GNU General Public License
23215 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23216 + */
23217 +
23218 +/*
23219 + * inode operations (del entry)
23220 + */
23221 +
23222 +#include <linux/iversion.h>
23223 +#include "aufs.h"
23224 +
23225 +/*
23226 + * decide if a new whiteout for @dentry is necessary or not.
23227 + * when it is necessary, prepare the parent dir for the upper branch whose
23228 + * branch index is @bcpup for creation. the actual creation of the whiteout will
23229 + * be done by caller.
23230 + * return value:
23231 + * 0: wh is unnecessary
23232 + * plus: wh is necessary
23233 + * minus: error
23234 + */
23235 +int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup)
23236 +{
23237 +       int need_wh, err;
23238 +       aufs_bindex_t btop;
23239 +       struct super_block *sb;
23240 +
23241 +       sb = dentry->d_sb;
23242 +       btop = au_dbtop(dentry);
23243 +       if (*bcpup < 0) {
23244 +               *bcpup = btop;
23245 +               if (au_test_ro(sb, btop, d_inode(dentry))) {
23246 +                       err = AuWbrCopyup(au_sbi(sb), dentry);
23247 +                       *bcpup = err;
23248 +                       if (unlikely(err < 0))
23249 +                               goto out;
23250 +               }
23251 +       } else
23252 +               AuDebugOn(btop < *bcpup
23253 +                         || au_test_ro(sb, *bcpup, d_inode(dentry)));
23254 +       AuDbg("bcpup %d, btop %d\n", *bcpup, btop);
23255 +
23256 +       if (*bcpup != btop) {
23257 +               err = au_cpup_dirs(dentry, *bcpup);
23258 +               if (unlikely(err))
23259 +                       goto out;
23260 +               need_wh = 1;
23261 +       } else {
23262 +               struct au_dinfo *dinfo, *tmp;
23263 +
23264 +               need_wh = -ENOMEM;
23265 +               dinfo = au_di(dentry);
23266 +               tmp = au_di_alloc(sb, AuLsc_DI_TMP);
23267 +               if (tmp) {
23268 +                       au_di_cp(tmp, dinfo);
23269 +                       au_di_swap(tmp, dinfo);
23270 +                       /* returns the number of positive dentries */
23271 +                       need_wh = au_lkup_dentry(dentry, btop + 1,
23272 +                                                /* AuLkup_IGNORE_PERM */ 0);
23273 +                       au_di_swap(tmp, dinfo);
23274 +                       au_rw_write_unlock(&tmp->di_rwsem);
23275 +                       au_di_free(tmp);
23276 +               }
23277 +       }
23278 +       AuDbg("need_wh %d\n", need_wh);
23279 +       err = need_wh;
23280 +
23281 +out:
23282 +       return err;
23283 +}
23284 +
23285 +/*
23286 + * simple tests for the del-entry operations.
23287 + * following the checks in vfs, plus the parent-child relationship.
23288 + */
23289 +int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
23290 +              struct dentry *h_parent, int isdir)
23291 +{
23292 +       int err;
23293 +       umode_t h_mode;
23294 +       struct dentry *h_dentry, *h_latest;
23295 +       struct inode *h_inode;
23296 +       struct path h_ppath;
23297 +       struct super_block *sb;
23298 +       struct au_branch *br;
23299 +       struct user_namespace *h_userns;
23300 +
23301 +       h_dentry = au_h_dptr(dentry, bindex);
23302 +       if (d_really_is_positive(dentry)) {
23303 +               err = -ENOENT;
23304 +               if (unlikely(d_is_negative(h_dentry)))
23305 +                       goto out;
23306 +               h_inode = d_inode(h_dentry);
23307 +               if (unlikely(!h_inode->i_nlink))
23308 +                       goto out;
23309 +
23310 +               h_mode = h_inode->i_mode;
23311 +               if (!isdir) {
23312 +                       err = -EISDIR;
23313 +                       if (unlikely(S_ISDIR(h_mode)))
23314 +                               goto out;
23315 +               } else if (unlikely(!S_ISDIR(h_mode))) {
23316 +                       err = -ENOTDIR;
23317 +                       goto out;
23318 +               }
23319 +       } else {
23320 +               /* rename(2) case */
23321 +               err = -EIO;
23322 +               if (unlikely(d_is_positive(h_dentry)))
23323 +                       goto out;
23324 +       }
23325 +
23326 +       err = -ENOENT;
23327 +       /* expected parent dir is locked */
23328 +       if (unlikely(h_parent != h_dentry->d_parent))
23329 +               goto out;
23330 +       err = 0;
23331 +
23332 +       /*
23333 +        * rmdir a dir may break the consistency on some filesystem.
23334 +        * let's try heavy test.
23335 +        */
23336 +       err = -EACCES;
23337 +       sb = dentry->d_sb;
23338 +       br = au_sbr(sb, bindex);
23339 +       h_userns = au_br_userns(br);
23340 +       if (unlikely(!au_opt_test(au_mntflags(sb), DIRPERM1)
23341 +                    && au_test_h_perm(h_userns, d_inode(h_parent),
23342 +                                      MAY_EXEC | MAY_WRITE)))
23343 +               goto out;
23344 +
23345 +       h_ppath.dentry = h_parent;
23346 +       h_ppath.mnt = au_br_mnt(br);
23347 +       h_latest = au_sio_lkup_one(h_userns, &dentry->d_name, &h_ppath);
23348 +       err = -EIO;
23349 +       if (IS_ERR(h_latest))
23350 +               goto out;
23351 +       if (h_latest == h_dentry)
23352 +               err = 0;
23353 +       dput(h_latest);
23354 +
23355 +out:
23356 +       return err;
23357 +}
23358 +
23359 +/*
23360 + * decide the branch where we operate for @dentry. the branch index will be set
23361 + * @rbcpup. after deciding it, 'pin' it and store the timestamps of the parent
23362 + * dir for reverting.
23363 + * when a new whiteout is necessary, create it.
23364 + */
23365 +static struct dentry*
23366 +lock_hdir_create_wh(struct dentry *dentry, int isdir, aufs_bindex_t *rbcpup,
23367 +                   struct au_dtime *dt, struct au_pin *pin)
23368 +{
23369 +       struct dentry *wh_dentry;
23370 +       struct super_block *sb;
23371 +       struct path h_path;
23372 +       int err, need_wh;
23373 +       unsigned int udba;
23374 +       aufs_bindex_t bcpup;
23375 +
23376 +       need_wh = au_wr_dir_need_wh(dentry, isdir, rbcpup);
23377 +       wh_dentry = ERR_PTR(need_wh);
23378 +       if (unlikely(need_wh < 0))
23379 +               goto out;
23380 +
23381 +       sb = dentry->d_sb;
23382 +       udba = au_opt_udba(sb);
23383 +       bcpup = *rbcpup;
23384 +       err = au_pin(pin, dentry, bcpup, udba,
23385 +                    AuPin_DI_LOCKED | AuPin_MNT_WRITE);
23386 +       wh_dentry = ERR_PTR(err);
23387 +       if (unlikely(err))
23388 +               goto out;
23389 +
23390 +       h_path.dentry = au_pinned_h_parent(pin);
23391 +       if (udba != AuOpt_UDBA_NONE
23392 +           && au_dbtop(dentry) == bcpup) {
23393 +               err = au_may_del(dentry, bcpup, h_path.dentry, isdir);
23394 +               wh_dentry = ERR_PTR(err);
23395 +               if (unlikely(err))
23396 +                       goto out_unpin;
23397 +       }
23398 +
23399 +       h_path.mnt = au_sbr_mnt(sb, bcpup);
23400 +       au_dtime_store(dt, au_pinned_parent(pin), &h_path);
23401 +       wh_dentry = NULL;
23402 +       if (!need_wh)
23403 +               goto out; /* success, no need to create whiteout */
23404 +
23405 +       wh_dentry = au_wh_create(dentry, bcpup, h_path.dentry);
23406 +       if (IS_ERR(wh_dentry))
23407 +               goto out_unpin;
23408 +
23409 +       /* returns with the parent is locked and wh_dentry is dget-ed */
23410 +       goto out; /* success */
23411 +
23412 +out_unpin:
23413 +       au_unpin(pin);
23414 +out:
23415 +       return wh_dentry;
23416 +}
23417 +
23418 +/*
23419 + * when removing a dir, rename it to a unique temporary whiteout-ed name first
23420 + * in order to be revertible and save time for removing many child whiteouts
23421 + * under the dir.
23422 + * returns 1 when there are too many child whiteout and caller should remove
23423 + * them asynchronously. returns 0 when the number of children is enough small to
23424 + * remove now or the branch fs is a remote fs.
23425 + * otherwise return an error.
23426 + */
23427 +static int renwh_and_rmdir(struct dentry *dentry, aufs_bindex_t bindex,
23428 +                          struct au_nhash *whlist, struct inode *dir)
23429 +{
23430 +       int rmdir_later, err, dirwh;
23431 +       struct dentry *h_dentry;
23432 +       struct super_block *sb;
23433 +       struct inode *inode;
23434 +
23435 +       sb = dentry->d_sb;
23436 +       SiMustAnyLock(sb);
23437 +       h_dentry = au_h_dptr(dentry, bindex);
23438 +       err = au_whtmp_ren(h_dentry, au_sbr(sb, bindex));
23439 +       if (unlikely(err))
23440 +               goto out;
23441 +
23442 +       /* stop monitoring */
23443 +       inode = d_inode(dentry);
23444 +       au_hn_free(au_hi(inode, bindex));
23445 +
23446 +       if (!au_test_fs_remote(h_dentry->d_sb)) {
23447 +               dirwh = au_sbi(sb)->si_dirwh;
23448 +               rmdir_later = (dirwh <= 1);
23449 +               if (!rmdir_later)
23450 +                       rmdir_later = au_nhash_test_longer_wh(whlist, bindex,
23451 +                                                             dirwh);
23452 +               if (rmdir_later)
23453 +                       return rmdir_later;
23454 +       }
23455 +
23456 +       err = au_whtmp_rmdir(dir, bindex, h_dentry, whlist);
23457 +       if (unlikely(err)) {
23458 +               AuIOErr("rmdir %pd, b%d failed, %d. ignored\n",
23459 +                       h_dentry, bindex, err);
23460 +               err = 0;
23461 +       }
23462 +
23463 +out:
23464 +       AuTraceErr(err);
23465 +       return err;
23466 +}
23467 +
23468 +/*
23469 + * final procedure for deleting a entry.
23470 + * maintain dentry and iattr.
23471 + */
23472 +static void epilog(struct inode *dir, struct dentry *dentry,
23473 +                  aufs_bindex_t bindex)
23474 +{
23475 +       struct inode *inode;
23476 +
23477 +       inode = d_inode(dentry);
23478 +       d_drop(dentry);
23479 +       inode->i_ctime = dir->i_ctime;
23480 +
23481 +       au_dir_ts(dir, bindex);
23482 +       inode_inc_iversion(dir);
23483 +}
23484 +
23485 +/*
23486 + * when an error happened, remove the created whiteout and revert everything.
23487 + */
23488 +static int do_revert(int err, struct inode *dir, aufs_bindex_t bindex,
23489 +                    aufs_bindex_t bwh, struct dentry *wh_dentry,
23490 +                    struct dentry *dentry, struct au_dtime *dt)
23491 +{
23492 +       int rerr;
23493 +       struct path h_path = {
23494 +               .dentry = wh_dentry,
23495 +               .mnt    = au_sbr_mnt(dir->i_sb, bindex)
23496 +       };
23497 +
23498 +       rerr = au_wh_unlink_dentry(au_h_iptr(dir, bindex), &h_path, dentry);
23499 +       if (!rerr) {
23500 +               au_set_dbwh(dentry, bwh);
23501 +               au_dtime_revert(dt);
23502 +               return 0;
23503 +       }
23504 +
23505 +       AuIOErr("%pd reverting whiteout failed(%d, %d)\n", dentry, err, rerr);
23506 +       return -EIO;
23507 +}
23508 +
23509 +/* ---------------------------------------------------------------------- */
23510 +
23511 +int aufs_unlink(struct inode *dir, struct dentry *dentry)
23512 +{
23513 +       int err;
23514 +       aufs_bindex_t bwh, bindex, btop;
23515 +       struct inode *inode, *h_dir, *delegated;
23516 +       struct dentry *parent, *wh_dentry;
23517 +       /* to reduce stack size */
23518 +       struct {
23519 +               struct au_dtime dt;
23520 +               struct au_pin pin;
23521 +               struct path h_path;
23522 +       } *a;
23523 +
23524 +       IMustLock(dir);
23525 +
23526 +       err = -ENOMEM;
23527 +       a = kmalloc(sizeof(*a), GFP_NOFS);
23528 +       if (unlikely(!a))
23529 +               goto out;
23530 +
23531 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_GEN);
23532 +       if (unlikely(err))
23533 +               goto out_free;
23534 +       err = au_d_hashed_positive(dentry);
23535 +       if (unlikely(err))
23536 +               goto out_unlock;
23537 +       inode = d_inode(dentry);
23538 +       IMustLock(inode);
23539 +       err = -EISDIR;
23540 +       if (unlikely(d_is_dir(dentry)))
23541 +               goto out_unlock; /* possible? */
23542 +
23543 +       btop = au_dbtop(dentry);
23544 +       bwh = au_dbwh(dentry);
23545 +       bindex = -1;
23546 +       parent = dentry->d_parent; /* dir inode is locked */
23547 +       di_write_lock_parent(parent);
23548 +       wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/0, &bindex, &a->dt,
23549 +                                       &a->pin);
23550 +       err = PTR_ERR(wh_dentry);
23551 +       if (IS_ERR(wh_dentry))
23552 +               goto out_parent;
23553 +
23554 +       a->h_path.mnt = au_sbr_mnt(dentry->d_sb, btop);
23555 +       a->h_path.dentry = au_h_dptr(dentry, btop);
23556 +       dget(a->h_path.dentry);
23557 +       if (bindex == btop) {
23558 +               h_dir = au_pinned_h_dir(&a->pin);
23559 +               delegated = NULL;
23560 +               err = vfsub_unlink(h_dir, &a->h_path, &delegated, /*force*/0);
23561 +               if (unlikely(err == -EWOULDBLOCK)) {
23562 +                       pr_warn("cannot retry for NFSv4 delegation"
23563 +                               " for an internal unlink\n");
23564 +                       iput(delegated);
23565 +               }
23566 +       } else {
23567 +               /* dir inode is locked */
23568 +               h_dir = d_inode(wh_dentry->d_parent);
23569 +               IMustLock(h_dir);
23570 +               err = 0;
23571 +       }
23572 +
23573 +       if (!err) {
23574 +               vfsub_drop_nlink(inode);
23575 +               epilog(dir, dentry, bindex);
23576 +
23577 +               /* update target timestamps */
23578 +               if (bindex == btop) {
23579 +                       vfsub_update_h_iattr(&a->h_path, /*did*/NULL);
23580 +                       /*ignore*/
23581 +                       inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
23582 +               } else
23583 +                       /* todo: this timestamp may be reverted later */
23584 +                       inode->i_ctime = h_dir->i_ctime;
23585 +               goto out_unpin; /* success */
23586 +       }
23587 +
23588 +       /* revert */
23589 +       if (wh_dentry) {
23590 +               int rerr;
23591 +
23592 +               rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
23593 +                                &a->dt);
23594 +               if (rerr)
23595 +                       err = rerr;
23596 +       }
23597 +
23598 +out_unpin:
23599 +       au_unpin(&a->pin);
23600 +       dput(wh_dentry);
23601 +       dput(a->h_path.dentry);
23602 +out_parent:
23603 +       di_write_unlock(parent);
23604 +out_unlock:
23605 +       aufs_read_unlock(dentry, AuLock_DW);
23606 +out_free:
23607 +       au_kfree_rcu(a);
23608 +out:
23609 +       return err;
23610 +}
23611 +
23612 +int aufs_rmdir(struct inode *dir, struct dentry *dentry)
23613 +{
23614 +       int err, rmdir_later;
23615 +       aufs_bindex_t bwh, bindex, btop;
23616 +       struct inode *inode;
23617 +       struct dentry *parent, *wh_dentry, *h_dentry;
23618 +       struct au_whtmp_rmdir *args;
23619 +       /* to reduce stack size */
23620 +       struct {
23621 +               struct au_dtime dt;
23622 +               struct au_pin pin;
23623 +       } *a;
23624 +
23625 +       IMustLock(dir);
23626 +
23627 +       err = -ENOMEM;
23628 +       a = kmalloc(sizeof(*a), GFP_NOFS);
23629 +       if (unlikely(!a))
23630 +               goto out;
23631 +
23632 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_GEN);
23633 +       if (unlikely(err))
23634 +               goto out_free;
23635 +       err = au_alive_dir(dentry);
23636 +       if (unlikely(err))
23637 +               goto out_unlock;
23638 +       inode = d_inode(dentry);
23639 +       IMustLock(inode);
23640 +       err = -ENOTDIR;
23641 +       if (unlikely(!d_is_dir(dentry)))
23642 +               goto out_unlock; /* possible? */
23643 +
23644 +       err = -ENOMEM;
23645 +       args = au_whtmp_rmdir_alloc(dir->i_sb, GFP_NOFS);
23646 +       if (unlikely(!args))
23647 +               goto out_unlock;
23648 +
23649 +       parent = dentry->d_parent; /* dir inode is locked */
23650 +       di_write_lock_parent(parent);
23651 +       err = au_test_empty(dentry, &args->whlist);
23652 +       if (unlikely(err))
23653 +               goto out_parent;
23654 +
23655 +       btop = au_dbtop(dentry);
23656 +       bwh = au_dbwh(dentry);
23657 +       bindex = -1;
23658 +       wh_dentry = lock_hdir_create_wh(dentry, /*isdir*/1, &bindex, &a->dt,
23659 +                                       &a->pin);
23660 +       err = PTR_ERR(wh_dentry);
23661 +       if (IS_ERR(wh_dentry))
23662 +               goto out_parent;
23663 +
23664 +       h_dentry = au_h_dptr(dentry, btop);
23665 +       dget(h_dentry);
23666 +       rmdir_later = 0;
23667 +       if (bindex == btop) {
23668 +               err = renwh_and_rmdir(dentry, btop, &args->whlist, dir);
23669 +               if (err > 0) {
23670 +                       rmdir_later = err;
23671 +                       err = 0;
23672 +               }
23673 +       } else {
23674 +               /* stop monitoring */
23675 +               au_hn_free(au_hi(inode, btop));
23676 +
23677 +               /* dir inode is locked */
23678 +               IMustLock(d_inode(wh_dentry->d_parent));
23679 +               err = 0;
23680 +       }
23681 +
23682 +       if (!err) {
23683 +               vfsub_dead_dir(inode);
23684 +               au_set_dbdiropq(dentry, -1);
23685 +               epilog(dir, dentry, bindex);
23686 +
23687 +               if (rmdir_later) {
23688 +                       au_whtmp_kick_rmdir(dir, btop, h_dentry, args);
23689 +                       args = NULL;
23690 +               }
23691 +
23692 +               goto out_unpin; /* success */
23693 +       }
23694 +
23695 +       /* revert */
23696 +       AuLabel(revert);
23697 +       if (wh_dentry) {
23698 +               int rerr;
23699 +
23700 +               rerr = do_revert(err, dir, bindex, bwh, wh_dentry, dentry,
23701 +                                &a->dt);
23702 +               if (rerr)
23703 +                       err = rerr;
23704 +       }
23705 +
23706 +out_unpin:
23707 +       au_unpin(&a->pin);
23708 +       dput(wh_dentry);
23709 +       dput(h_dentry);
23710 +out_parent:
23711 +       di_write_unlock(parent);
23712 +       if (args)
23713 +               au_whtmp_rmdir_free(args);
23714 +out_unlock:
23715 +       aufs_read_unlock(dentry, AuLock_DW);
23716 +out_free:
23717 +       au_kfree_rcu(a);
23718 +out:
23719 +       AuTraceErr(err);
23720 +       return err;
23721 +}
23722 diff -urN /usr/share/empty/fs/aufs/i_op_ren.c linux/fs/aufs/i_op_ren.c
23723 --- /usr/share/empty/fs/aufs/i_op_ren.c 1970-01-01 01:00:00.000000000 +0100
23724 +++ linux/fs/aufs/i_op_ren.c    2022-03-21 14:49:05.726633010 +0100
23725 @@ -0,0 +1,1257 @@
23726 +// SPDX-License-Identifier: GPL-2.0
23727 +/*
23728 + * Copyright (C) 2005-2021 Junjiro R. Okajima
23729 + *
23730 + * This program, aufs is free software; you can redistribute it and/or modify
23731 + * it under the terms of the GNU General Public License as published by
23732 + * the Free Software Foundation; either version 2 of the License, or
23733 + * (at your option) any later version.
23734 + *
23735 + * This program is distributed in the hope that it will be useful,
23736 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
23737 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23738 + * GNU General Public License for more details.
23739 + *
23740 + * You should have received a copy of the GNU General Public License
23741 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23742 + */
23743 +
23744 +/*
23745 + * inode operation (rename entry)
23746 + * todo: this is crazy monster
23747 + */
23748 +
23749 +#include <linux/iversion.h>
23750 +#include "aufs.h"
23751 +
23752 +enum { AuSRC, AuDST, AuSrcDst };
23753 +enum { AuPARENT, AuCHILD, AuParentChild };
23754 +
23755 +#define AuRen_ISDIR_SRC                1
23756 +#define AuRen_ISDIR_DST                (1 << 1)
23757 +#define AuRen_ISSAMEDIR                (1 << 2)
23758 +#define AuRen_WHSRC            (1 << 3)
23759 +#define AuRen_WHDST            (1 << 4)
23760 +#define AuRen_MNT_WRITE                (1 << 5)
23761 +#define AuRen_DT_DSTDIR                (1 << 6)
23762 +#define AuRen_DIROPQ_SRC       (1 << 7)
23763 +#define AuRen_DIROPQ_DST       (1 << 8)
23764 +#define AuRen_DIRREN           (1 << 9)
23765 +#define AuRen_DROPPED_SRC      (1 << 10)
23766 +#define AuRen_DROPPED_DST      (1 << 11)
23767 +#define au_ftest_ren(flags, name)      ((flags) & AuRen_##name)
23768 +#define au_fset_ren(flags, name) \
23769 +       do { (flags) |= AuRen_##name; } while (0)
23770 +#define au_fclr_ren(flags, name) \
23771 +       do { (flags) &= ~AuRen_##name; } while (0)
23772 +
23773 +#ifndef CONFIG_AUFS_DIRREN
23774 +#undef AuRen_DIRREN
23775 +#define AuRen_DIRREN           0
23776 +#endif
23777 +
23778 +struct au_ren_args {
23779 +       struct {
23780 +               struct dentry *dentry, *h_dentry, *parent, *h_parent,
23781 +                       *wh_dentry;
23782 +               struct inode *dir, *inode;
23783 +               struct au_hinode *hdir, *hinode;
23784 +               struct au_dtime dt[AuParentChild];
23785 +               aufs_bindex_t btop, bdiropq;
23786 +       } sd[AuSrcDst];
23787 +
23788 +#define src_dentry     sd[AuSRC].dentry
23789 +#define src_dir                sd[AuSRC].dir
23790 +#define src_inode      sd[AuSRC].inode
23791 +#define src_h_dentry   sd[AuSRC].h_dentry
23792 +#define src_parent     sd[AuSRC].parent
23793 +#define src_h_parent   sd[AuSRC].h_parent
23794 +#define src_wh_dentry  sd[AuSRC].wh_dentry
23795 +#define src_hdir       sd[AuSRC].hdir
23796 +#define src_hinode     sd[AuSRC].hinode
23797 +#define src_h_dir      sd[AuSRC].hdir->hi_inode
23798 +#define src_dt         sd[AuSRC].dt
23799 +#define src_btop       sd[AuSRC].btop
23800 +#define src_bdiropq    sd[AuSRC].bdiropq
23801 +
23802 +#define dst_dentry     sd[AuDST].dentry
23803 +#define dst_dir                sd[AuDST].dir
23804 +#define dst_inode      sd[AuDST].inode
23805 +#define dst_h_dentry   sd[AuDST].h_dentry
23806 +#define dst_parent     sd[AuDST].parent
23807 +#define dst_h_parent   sd[AuDST].h_parent
23808 +#define dst_wh_dentry  sd[AuDST].wh_dentry
23809 +#define dst_hdir       sd[AuDST].hdir
23810 +#define dst_hinode     sd[AuDST].hinode
23811 +#define dst_h_dir      sd[AuDST].hdir->hi_inode
23812 +#define dst_dt         sd[AuDST].dt
23813 +#define dst_btop       sd[AuDST].btop
23814 +#define dst_bdiropq    sd[AuDST].bdiropq
23815 +
23816 +       struct dentry *h_trap;
23817 +       struct au_branch *br;
23818 +       struct path h_path;
23819 +       struct au_nhash whlist;
23820 +       aufs_bindex_t btgt, src_bwh;
23821 +
23822 +       struct {
23823 +               unsigned short auren_flags;
23824 +               unsigned char flags;    /* syscall parameter */
23825 +               unsigned char exchange;
23826 +       } __packed;
23827 +
23828 +       struct au_whtmp_rmdir *thargs;
23829 +       struct dentry *h_dst;
23830 +       struct au_hinode *h_root;
23831 +};
23832 +
23833 +/* ---------------------------------------------------------------------- */
23834 +
23835 +/*
23836 + * functions for reverting.
23837 + * when an error happened in a single rename systemcall, we should revert
23838 + * everything as if nothing happened.
23839 + * we don't need to revert the copied-up/down the parent dir since they are
23840 + * harmless.
23841 + */
23842 +
23843 +#define RevertFailure(fmt, ...) do { \
23844 +       AuIOErr("revert failure: " fmt " (%d, %d)\n", \
23845 +               ##__VA_ARGS__, err, rerr); \
23846 +       err = -EIO; \
23847 +} while (0)
23848 +
23849 +static void au_ren_do_rev_diropq(int err, struct au_ren_args *a, int idx)
23850 +{
23851 +       int rerr;
23852 +       struct dentry *d;
23853 +#define src_or_dst(member) a->sd[idx].member
23854 +
23855 +       d = src_or_dst(dentry); /* {src,dst}_dentry */
23856 +       au_hn_inode_lock_nested(src_or_dst(hinode), AuLsc_I_CHILD);
23857 +       rerr = au_diropq_remove(d, a->btgt);
23858 +       au_hn_inode_unlock(src_or_dst(hinode));
23859 +       au_set_dbdiropq(d, src_or_dst(bdiropq));
23860 +       if (rerr)
23861 +               RevertFailure("remove diropq %pd", d);
23862 +
23863 +#undef src_or_dst_
23864 +}
23865 +
23866 +static void au_ren_rev_diropq(int err, struct au_ren_args *a)
23867 +{
23868 +       if (au_ftest_ren(a->auren_flags, DIROPQ_SRC))
23869 +               au_ren_do_rev_diropq(err, a, AuSRC);
23870 +       if (au_ftest_ren(a->auren_flags, DIROPQ_DST))
23871 +               au_ren_do_rev_diropq(err, a, AuDST);
23872 +}
23873 +
23874 +static void au_ren_rev_rename(int err, struct au_ren_args *a)
23875 +{
23876 +       int rerr;
23877 +       struct inode *delegated;
23878 +       struct path h_ppath = {
23879 +               .dentry = a->src_h_parent,
23880 +               .mnt    = a->h_path.mnt
23881 +       };
23882 +
23883 +       a->h_path.dentry = vfsub_lkup_one(&a->src_dentry->d_name, &h_ppath);
23884 +       rerr = PTR_ERR(a->h_path.dentry);
23885 +       if (IS_ERR(a->h_path.dentry)) {
23886 +               RevertFailure("lkup one %pd", a->src_dentry);
23887 +               return;
23888 +       }
23889 +
23890 +       delegated = NULL;
23891 +       rerr = vfsub_rename(a->dst_h_dir,
23892 +                           au_h_dptr(a->src_dentry, a->btgt),
23893 +                           a->src_h_dir, &a->h_path, &delegated, a->flags);
23894 +       if (unlikely(rerr == -EWOULDBLOCK)) {
23895 +               pr_warn("cannot retry for NFSv4 delegation"
23896 +                       " for an internal rename\n");
23897 +               iput(delegated);
23898 +       }
23899 +       d_drop(a->h_path.dentry);
23900 +       dput(a->h_path.dentry);
23901 +       /* au_set_h_dptr(a->src_dentry, a->btgt, NULL); */
23902 +       if (rerr)
23903 +               RevertFailure("rename %pd", a->src_dentry);
23904 +}
23905 +
23906 +static void au_ren_rev_whtmp(int err, struct au_ren_args *a)
23907 +{
23908 +       int rerr;
23909 +       struct inode *delegated;
23910 +       struct path h_ppath = {
23911 +               .dentry = a->dst_h_parent,
23912 +               .mnt    = a->h_path.mnt
23913 +       };
23914 +
23915 +       a->h_path.dentry = vfsub_lkup_one(&a->dst_dentry->d_name, &h_ppath);
23916 +       rerr = PTR_ERR(a->h_path.dentry);
23917 +       if (IS_ERR(a->h_path.dentry)) {
23918 +               RevertFailure("lkup one %pd", a->dst_dentry);
23919 +               return;
23920 +       }
23921 +       if (d_is_positive(a->h_path.dentry)) {
23922 +               d_drop(a->h_path.dentry);
23923 +               dput(a->h_path.dentry);
23924 +               return;
23925 +       }
23926 +
23927 +       delegated = NULL;
23928 +       rerr = vfsub_rename(a->dst_h_dir, a->h_dst, a->dst_h_dir, &a->h_path,
23929 +                           &delegated, a->flags);
23930 +       if (unlikely(rerr == -EWOULDBLOCK)) {
23931 +               pr_warn("cannot retry for NFSv4 delegation"
23932 +                       " for an internal rename\n");
23933 +               iput(delegated);
23934 +       }
23935 +       d_drop(a->h_path.dentry);
23936 +       dput(a->h_path.dentry);
23937 +       if (!rerr)
23938 +               au_set_h_dptr(a->dst_dentry, a->btgt, dget(a->h_dst));
23939 +       else
23940 +               RevertFailure("rename %pd", a->h_dst);
23941 +}
23942 +
23943 +static void au_ren_rev_whsrc(int err, struct au_ren_args *a)
23944 +{
23945 +       int rerr;
23946 +
23947 +       a->h_path.dentry = a->src_wh_dentry;
23948 +       rerr = au_wh_unlink_dentry(a->src_h_dir, &a->h_path, a->src_dentry);
23949 +       au_set_dbwh(a->src_dentry, a->src_bwh);
23950 +       if (rerr)
23951 +               RevertFailure("unlink %pd", a->src_wh_dentry);
23952 +}
23953 +#undef RevertFailure
23954 +
23955 +/* ---------------------------------------------------------------------- */
23956 +
23957 +/*
23958 + * when we have to copyup the renaming entry, do it with the rename-target name
23959 + * in order to minimize the cost (the later actual rename is unnecessary).
23960 + * otherwise rename it on the target branch.
23961 + */
23962 +static int au_ren_or_cpup(struct au_ren_args *a)
23963 +{
23964 +       int err;
23965 +       struct dentry *d;
23966 +       struct inode *delegated;
23967 +
23968 +       d = a->src_dentry;
23969 +       if (au_dbtop(d) == a->btgt) {
23970 +               a->h_path.dentry = a->dst_h_dentry;
23971 +               AuDebugOn(au_dbtop(d) != a->btgt);
23972 +               delegated = NULL;
23973 +               err = vfsub_rename(a->src_h_dir, au_h_dptr(d, a->btgt),
23974 +                                  a->dst_h_dir, &a->h_path, &delegated,
23975 +                                  a->flags);
23976 +               if (unlikely(err == -EWOULDBLOCK)) {
23977 +                       pr_warn("cannot retry for NFSv4 delegation"
23978 +                               " for an internal rename\n");
23979 +                       iput(delegated);
23980 +               }
23981 +       } else
23982 +               BUG();
23983 +
23984 +       if (!err && a->h_dst)
23985 +               /* it will be set to dinfo later */
23986 +               dget(a->h_dst);
23987 +
23988 +       return err;
23989 +}
23990 +
23991 +/* cf. aufs_rmdir() */
23992 +static int au_ren_del_whtmp(struct au_ren_args *a)
23993 +{
23994 +       int err;
23995 +       struct inode *dir;
23996 +
23997 +       dir = a->dst_dir;
23998 +       SiMustAnyLock(dir->i_sb);
23999 +       if (!au_nhash_test_longer_wh(&a->whlist, a->btgt,
24000 +                                    au_sbi(dir->i_sb)->si_dirwh)
24001 +           || au_test_fs_remote(a->h_dst->d_sb)) {
24002 +               err = au_whtmp_rmdir(dir, a->btgt, a->h_dst, &a->whlist);
24003 +               if (unlikely(err))
24004 +                       pr_warn("failed removing whtmp dir %pd (%d), "
24005 +                               "ignored.\n", a->h_dst, err);
24006 +       } else {
24007 +               au_nhash_wh_free(&a->thargs->whlist);
24008 +               a->thargs->whlist = a->whlist;
24009 +               a->whlist.nh_num = 0;
24010 +               au_whtmp_kick_rmdir(dir, a->btgt, a->h_dst, a->thargs);
24011 +               dput(a->h_dst);
24012 +               a->thargs = NULL;
24013 +       }
24014 +
24015 +       return 0;
24016 +}
24017 +
24018 +/* make it 'opaque' dir. */
24019 +static int au_ren_do_diropq(struct au_ren_args *a, int idx)
24020 +{
24021 +       int err;
24022 +       struct dentry *d, *diropq;
24023 +#define src_or_dst(member) a->sd[idx].member
24024 +
24025 +       err = 0;
24026 +       d = src_or_dst(dentry); /* {src,dst}_dentry */
24027 +       src_or_dst(bdiropq) = au_dbdiropq(d);
24028 +       src_or_dst(hinode) = au_hi(src_or_dst(inode), a->btgt);
24029 +       au_hn_inode_lock_nested(src_or_dst(hinode), AuLsc_I_CHILD);
24030 +       diropq = au_diropq_create(d, a->btgt);
24031 +       au_hn_inode_unlock(src_or_dst(hinode));
24032 +       if (IS_ERR(diropq))
24033 +               err = PTR_ERR(diropq);
24034 +       else
24035 +               dput(diropq);
24036 +
24037 +#undef src_or_dst_
24038 +       return err;
24039 +}
24040 +
24041 +static int au_ren_diropq(struct au_ren_args *a)
24042 +{
24043 +       int err;
24044 +       unsigned char always;
24045 +       struct dentry *d;
24046 +
24047 +       err = 0;
24048 +       d = a->dst_dentry; /* already renamed on the branch */
24049 +       always = !!au_opt_test(au_mntflags(d->d_sb), ALWAYS_DIROPQ);
24050 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC)
24051 +           && !au_ftest_ren(a->auren_flags, DIRREN)
24052 +           && a->btgt != au_dbdiropq(a->src_dentry)
24053 +           && (a->dst_wh_dentry
24054 +               || a->btgt <= au_dbdiropq(d)
24055 +               /* hide the lower to keep xino */
24056 +               /* the lowers may not be a dir, but we hide them anyway */
24057 +               || a->btgt < au_dbbot(d)
24058 +               || always)) {
24059 +               AuDbg("here\n");
24060 +               err = au_ren_do_diropq(a, AuSRC);
24061 +               if (unlikely(err))
24062 +                       goto out;
24063 +               au_fset_ren(a->auren_flags, DIROPQ_SRC);
24064 +       }
24065 +       if (!a->exchange)
24066 +               goto out; /* success */
24067 +
24068 +       d = a->src_dentry; /* already renamed on the branch */
24069 +       if (au_ftest_ren(a->auren_flags, ISDIR_DST)
24070 +           && a->btgt != au_dbdiropq(a->dst_dentry)
24071 +           && (a->btgt < au_dbdiropq(d)
24072 +               || a->btgt < au_dbbot(d)
24073 +               || always)) {
24074 +               AuDbgDentry(a->src_dentry);
24075 +               AuDbgDentry(a->dst_dentry);
24076 +               err = au_ren_do_diropq(a, AuDST);
24077 +               if (unlikely(err))
24078 +                       goto out_rev_src;
24079 +               au_fset_ren(a->auren_flags, DIROPQ_DST);
24080 +       }
24081 +       goto out; /* success */
24082 +
24083 +out_rev_src:
24084 +       AuDbg("err %d, reverting src\n", err);
24085 +       au_ren_rev_diropq(err, a);
24086 +out:
24087 +       return err;
24088 +}
24089 +
24090 +static int do_rename(struct au_ren_args *a)
24091 +{
24092 +       int err;
24093 +       struct dentry *d, *h_d;
24094 +
24095 +       if (!a->exchange) {
24096 +               /* prepare workqueue args for asynchronous rmdir */
24097 +               h_d = a->dst_h_dentry;
24098 +               if (au_ftest_ren(a->auren_flags, ISDIR_DST)
24099 +                   /* && !au_ftest_ren(a->auren_flags, DIRREN) */
24100 +                   && d_is_positive(h_d)) {
24101 +                       err = -ENOMEM;
24102 +                       a->thargs = au_whtmp_rmdir_alloc(a->src_dentry->d_sb,
24103 +                                                        GFP_NOFS);
24104 +                       if (unlikely(!a->thargs))
24105 +                               goto out;
24106 +                       a->h_dst = dget(h_d);
24107 +               }
24108 +
24109 +               /* create whiteout for src_dentry */
24110 +               if (au_ftest_ren(a->auren_flags, WHSRC)) {
24111 +                       a->src_bwh = au_dbwh(a->src_dentry);
24112 +                       AuDebugOn(a->src_bwh >= 0);
24113 +                       a->src_wh_dentry = au_wh_create(a->src_dentry, a->btgt,
24114 +                                                       a->src_h_parent);
24115 +                       err = PTR_ERR(a->src_wh_dentry);
24116 +                       if (IS_ERR(a->src_wh_dentry))
24117 +                               goto out_thargs;
24118 +               }
24119 +
24120 +               /* lookup whiteout for dentry */
24121 +               if (au_ftest_ren(a->auren_flags, WHDST)) {
24122 +                       h_d = au_wh_lkup(a->dst_h_parent,
24123 +                                        &a->dst_dentry->d_name, a->br);
24124 +                       err = PTR_ERR(h_d);
24125 +                       if (IS_ERR(h_d))
24126 +                               goto out_whsrc;
24127 +                       if (d_is_negative(h_d))
24128 +                               dput(h_d);
24129 +                       else
24130 +                               a->dst_wh_dentry = h_d;
24131 +               }
24132 +
24133 +               /* rename dentry to tmpwh */
24134 +               if (a->thargs) {
24135 +                       err = au_whtmp_ren(a->dst_h_dentry, a->br);
24136 +                       if (unlikely(err))
24137 +                               goto out_whdst;
24138 +
24139 +                       d = a->dst_dentry;
24140 +                       au_set_h_dptr(d, a->btgt, NULL);
24141 +                       err = au_lkup_neg(d, a->btgt, /*wh*/0);
24142 +                       if (unlikely(err))
24143 +                               goto out_whtmp;
24144 +                       a->dst_h_dentry = au_h_dptr(d, a->btgt);
24145 +               }
24146 +       }
24147 +
24148 +       BUG_ON(d_is_positive(a->dst_h_dentry) && a->src_btop != a->btgt);
24149 +#if 0 /* debugging */
24150 +       BUG_ON(!au_ftest_ren(a->auren_flags, DIRREN)
24151 +              && d_is_positive(a->dst_h_dentry)
24152 +              && a->src_btop != a->btgt);
24153 +#endif
24154 +
24155 +       /* rename by vfs_rename or cpup */
24156 +       err = au_ren_or_cpup(a);
24157 +       if (unlikely(err))
24158 +               /* leave the copied-up one */
24159 +               goto out_whtmp;
24160 +
24161 +       /* make dir opaque */
24162 +       err = au_ren_diropq(a);
24163 +       if (unlikely(err))
24164 +               goto out_rename;
24165 +
24166 +       /* update target timestamps */
24167 +       if (a->exchange) {
24168 +               AuDebugOn(au_dbtop(a->dst_dentry) != a->btgt);
24169 +               a->h_path.dentry = au_h_dptr(a->dst_dentry, a->btgt);
24170 +               vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
24171 +               a->dst_inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
24172 +       }
24173 +       AuDebugOn(au_dbtop(a->src_dentry) != a->btgt);
24174 +       a->h_path.dentry = au_h_dptr(a->src_dentry, a->btgt);
24175 +       vfsub_update_h_iattr(&a->h_path, /*did*/NULL); /*ignore*/
24176 +       a->src_inode->i_ctime = d_inode(a->h_path.dentry)->i_ctime;
24177 +
24178 +       if (!a->exchange) {
24179 +               /* remove whiteout for dentry */
24180 +               if (a->dst_wh_dentry) {
24181 +                       a->h_path.dentry = a->dst_wh_dentry;
24182 +                       err = au_wh_unlink_dentry(a->dst_h_dir, &a->h_path,
24183 +                                                 a->dst_dentry);
24184 +                       if (unlikely(err))
24185 +                               goto out_diropq;
24186 +               }
24187 +
24188 +               /* remove whtmp */
24189 +               if (a->thargs)
24190 +                       au_ren_del_whtmp(a); /* ignore this error */
24191 +
24192 +               au_fhsm_wrote(a->src_dentry->d_sb, a->btgt, /*force*/0);
24193 +       }
24194 +       err = 0;
24195 +       goto out_success;
24196 +
24197 +out_diropq:
24198 +       au_ren_rev_diropq(err, a);
24199 +out_rename:
24200 +       au_ren_rev_rename(err, a);
24201 +       dput(a->h_dst);
24202 +out_whtmp:
24203 +       if (a->thargs)
24204 +               au_ren_rev_whtmp(err, a);
24205 +out_whdst:
24206 +       dput(a->dst_wh_dentry);
24207 +       a->dst_wh_dentry = NULL;
24208 +out_whsrc:
24209 +       if (a->src_wh_dentry)
24210 +               au_ren_rev_whsrc(err, a);
24211 +out_success:
24212 +       dput(a->src_wh_dentry);
24213 +       dput(a->dst_wh_dentry);
24214 +out_thargs:
24215 +       if (a->thargs) {
24216 +               dput(a->h_dst);
24217 +               au_whtmp_rmdir_free(a->thargs);
24218 +               a->thargs = NULL;
24219 +       }
24220 +out:
24221 +       return err;
24222 +}
24223 +
24224 +/* ---------------------------------------------------------------------- */
24225 +
24226 +/*
24227 + * test if @dentry dir can be rename destination or not.
24228 + * success means, it is a logically empty dir.
24229 + */
24230 +static int may_rename_dstdir(struct dentry *dentry, struct au_nhash *whlist)
24231 +{
24232 +       return au_test_empty(dentry, whlist);
24233 +}
24234 +
24235 +/*
24236 + * test if @a->src_dentry dir can be rename source or not.
24237 + * if it can, return 0.
24238 + * success means,
24239 + * - it is a logically empty dir.
24240 + * - or, it exists on writable branch and has no children including whiteouts
24241 + *   on the lower branch unless DIRREN is on.
24242 + */
24243 +static int may_rename_srcdir(struct au_ren_args *a)
24244 +{
24245 +       int err;
24246 +       unsigned int rdhash;
24247 +       aufs_bindex_t btop, btgt;
24248 +       struct dentry *dentry;
24249 +       struct super_block *sb;
24250 +       struct au_sbinfo *sbinfo;
24251 +
24252 +       dentry = a->src_dentry;
24253 +       sb = dentry->d_sb;
24254 +       sbinfo = au_sbi(sb);
24255 +       if (au_opt_test(sbinfo->si_mntflags, DIRREN))
24256 +               au_fset_ren(a->auren_flags, DIRREN);
24257 +
24258 +       btgt = a->btgt;
24259 +       btop = au_dbtop(dentry);
24260 +       if (btop != btgt) {
24261 +               struct au_nhash whlist;
24262 +
24263 +               SiMustAnyLock(sb);
24264 +               rdhash = sbinfo->si_rdhash;
24265 +               if (!rdhash)
24266 +                       rdhash = au_rdhash_est(au_dir_size(/*file*/NULL,
24267 +                                                          dentry));
24268 +               err = au_nhash_alloc(&whlist, rdhash, GFP_NOFS);
24269 +               if (unlikely(err))
24270 +                       goto out;
24271 +               err = au_test_empty(dentry, &whlist);
24272 +               au_nhash_wh_free(&whlist);
24273 +               goto out;
24274 +       }
24275 +
24276 +       if (btop == au_dbtaildir(dentry))
24277 +               return 0; /* success */
24278 +
24279 +       err = au_test_empty_lower(dentry);
24280 +
24281 +out:
24282 +       if (err == -ENOTEMPTY) {
24283 +               if (au_ftest_ren(a->auren_flags, DIRREN)) {
24284 +                       err = 0;
24285 +               } else {
24286 +                       AuWarn1("renaming dir who has child(ren) on multiple "
24287 +                               "branches, is not supported\n");
24288 +                       err = -EXDEV;
24289 +               }
24290 +       }
24291 +       return err;
24292 +}
24293 +
24294 +/* side effect: sets whlist and h_dentry */
24295 +static int au_ren_may_dir(struct au_ren_args *a)
24296 +{
24297 +       int err;
24298 +       unsigned int rdhash;
24299 +       struct dentry *d;
24300 +
24301 +       d = a->dst_dentry;
24302 +       SiMustAnyLock(d->d_sb);
24303 +
24304 +       err = 0;
24305 +       if (au_ftest_ren(a->auren_flags, ISDIR_DST) && a->dst_inode) {
24306 +               rdhash = au_sbi(d->d_sb)->si_rdhash;
24307 +               if (!rdhash)
24308 +                       rdhash = au_rdhash_est(au_dir_size(/*file*/NULL, d));
24309 +               err = au_nhash_alloc(&a->whlist, rdhash, GFP_NOFS);
24310 +               if (unlikely(err))
24311 +                       goto out;
24312 +
24313 +               if (!a->exchange) {
24314 +                       au_set_dbtop(d, a->dst_btop);
24315 +                       err = may_rename_dstdir(d, &a->whlist);
24316 +                       au_set_dbtop(d, a->btgt);
24317 +               } else
24318 +                       err = may_rename_srcdir(a);
24319 +       }
24320 +       a->dst_h_dentry = au_h_dptr(d, au_dbtop(d));
24321 +       if (unlikely(err))
24322 +               goto out;
24323 +
24324 +       d = a->src_dentry;
24325 +       a->src_h_dentry = au_h_dptr(d, au_dbtop(d));
24326 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC)) {
24327 +               err = may_rename_srcdir(a);
24328 +               if (unlikely(err)) {
24329 +                       au_nhash_wh_free(&a->whlist);
24330 +                       a->whlist.nh_num = 0;
24331 +               }
24332 +       }
24333 +out:
24334 +       return err;
24335 +}
24336 +
24337 +/* ---------------------------------------------------------------------- */
24338 +
24339 +/*
24340 + * simple tests for rename.
24341 + * following the checks in vfs, plus the parent-child relationship.
24342 + */
24343 +static int au_may_ren(struct au_ren_args *a)
24344 +{
24345 +       int err, isdir;
24346 +       struct inode *h_inode;
24347 +
24348 +       if (a->src_btop == a->btgt) {
24349 +               err = au_may_del(a->src_dentry, a->btgt, a->src_h_parent,
24350 +                                au_ftest_ren(a->auren_flags, ISDIR_SRC));
24351 +               if (unlikely(err))
24352 +                       goto out;
24353 +               err = -EINVAL;
24354 +               if (unlikely(a->src_h_dentry == a->h_trap))
24355 +                       goto out;
24356 +       }
24357 +
24358 +       err = 0;
24359 +       if (a->dst_btop != a->btgt)
24360 +               goto out;
24361 +
24362 +       err = -ENOTEMPTY;
24363 +       if (unlikely(a->dst_h_dentry == a->h_trap))
24364 +               goto out;
24365 +
24366 +       err = -EIO;
24367 +       isdir = !!au_ftest_ren(a->auren_flags, ISDIR_DST);
24368 +       if (d_really_is_negative(a->dst_dentry)) {
24369 +               if (d_is_negative(a->dst_h_dentry))
24370 +                       err = au_may_add(a->dst_dentry, a->btgt,
24371 +                                        a->dst_h_parent, isdir);
24372 +       } else {
24373 +               if (unlikely(d_is_negative(a->dst_h_dentry)))
24374 +                       goto out;
24375 +               h_inode = d_inode(a->dst_h_dentry);
24376 +               if (h_inode->i_nlink)
24377 +                       err = au_may_del(a->dst_dentry, a->btgt,
24378 +                                        a->dst_h_parent, isdir);
24379 +       }
24380 +
24381 +out:
24382 +       if (unlikely(err == -ENOENT || err == -EEXIST))
24383 +               err = -EIO;
24384 +       AuTraceErr(err);
24385 +       return err;
24386 +}
24387 +
24388 +/* ---------------------------------------------------------------------- */
24389 +
24390 +/*
24391 + * locking order
24392 + * (VFS)
24393 + * - src_dir and dir by lock_rename()
24394 + * - inode if exists
24395 + * (aufs)
24396 + * - lock all
24397 + *   + src_dentry and dentry by aufs_read_and_write_lock2() which calls,
24398 + *     + si_read_lock
24399 + *     + di_write_lock2_child()
24400 + *       + di_write_lock_child()
24401 + *        + ii_write_lock_child()
24402 + *       + di_write_lock_child2()
24403 + *        + ii_write_lock_child2()
24404 + *     + src_parent and parent
24405 + *       + di_write_lock_parent()
24406 + *        + ii_write_lock_parent()
24407 + *       + di_write_lock_parent2()
24408 + *        + ii_write_lock_parent2()
24409 + *   + lower src_dir and dir by vfsub_lock_rename()
24410 + *   + verify the every relationships between child and parent. if any
24411 + *     of them failed, unlock all and return -EBUSY.
24412 + */
24413 +static void au_ren_unlock(struct au_ren_args *a)
24414 +{
24415 +       vfsub_unlock_rename(a->src_h_parent, a->src_hdir,
24416 +                           a->dst_h_parent, a->dst_hdir);
24417 +       if (au_ftest_ren(a->auren_flags, DIRREN)
24418 +           && a->h_root)
24419 +               au_hn_inode_unlock(a->h_root);
24420 +       if (au_ftest_ren(a->auren_flags, MNT_WRITE))
24421 +               vfsub_mnt_drop_write(au_br_mnt(a->br));
24422 +}
24423 +
24424 +static int au_ren_lock(struct au_ren_args *a)
24425 +{
24426 +       int err;
24427 +       unsigned int udba;
24428 +
24429 +       err = 0;
24430 +       a->src_h_parent = au_h_dptr(a->src_parent, a->btgt);
24431 +       a->src_hdir = au_hi(a->src_dir, a->btgt);
24432 +       a->dst_h_parent = au_h_dptr(a->dst_parent, a->btgt);
24433 +       a->dst_hdir = au_hi(a->dst_dir, a->btgt);
24434 +
24435 +       err = vfsub_mnt_want_write(au_br_mnt(a->br));
24436 +       if (unlikely(err))
24437 +               goto out;
24438 +       au_fset_ren(a->auren_flags, MNT_WRITE);
24439 +       if (au_ftest_ren(a->auren_flags, DIRREN)) {
24440 +               struct dentry *root;
24441 +               struct inode *dir;
24442 +
24443 +               /*
24444 +                * sbinfo is already locked, so this ii_read_lock is
24445 +                * unnecessary. but our debugging feature checks it.
24446 +                */
24447 +               root = a->src_inode->i_sb->s_root;
24448 +               if (root != a->src_parent && root != a->dst_parent) {
24449 +                       dir = d_inode(root);
24450 +                       ii_read_lock_parent3(dir);
24451 +                       a->h_root = au_hi(dir, a->btgt);
24452 +                       ii_read_unlock(dir);
24453 +                       au_hn_inode_lock_nested(a->h_root, AuLsc_I_PARENT3);
24454 +               }
24455 +       }
24456 +       a->h_trap = vfsub_lock_rename(a->src_h_parent, a->src_hdir,
24457 +                                     a->dst_h_parent, a->dst_hdir);
24458 +       udba = au_opt_udba(a->src_dentry->d_sb);
24459 +       if (unlikely(a->src_hdir->hi_inode != d_inode(a->src_h_parent)
24460 +                    || a->dst_hdir->hi_inode != d_inode(a->dst_h_parent)))
24461 +               err = au_busy_or_stale();
24462 +       if (!err && au_dbtop(a->src_dentry) == a->btgt)
24463 +               err = au_h_verify(a->src_h_dentry, udba,
24464 +                                 d_inode(a->src_h_parent), a->src_h_parent,
24465 +                                 a->br);
24466 +       if (!err && au_dbtop(a->dst_dentry) == a->btgt)
24467 +               err = au_h_verify(a->dst_h_dentry, udba,
24468 +                                 d_inode(a->dst_h_parent), a->dst_h_parent,
24469 +                                 a->br);
24470 +       if (!err)
24471 +               goto out; /* success */
24472 +
24473 +       err = au_busy_or_stale();
24474 +       au_ren_unlock(a);
24475 +
24476 +out:
24477 +       return err;
24478 +}
24479 +
24480 +/* ---------------------------------------------------------------------- */
24481 +
24482 +static void au_ren_refresh_dir(struct au_ren_args *a)
24483 +{
24484 +       struct inode *dir;
24485 +
24486 +       dir = a->dst_dir;
24487 +       inode_inc_iversion(dir);
24488 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC)) {
24489 +               /* is this updating defined in POSIX? */
24490 +               au_cpup_attr_timesizes(a->src_inode);
24491 +               au_cpup_attr_nlink(dir, /*force*/1);
24492 +       }
24493 +       au_dir_ts(dir, a->btgt);
24494 +
24495 +       if (a->exchange) {
24496 +               dir = a->src_dir;
24497 +               inode_inc_iversion(dir);
24498 +               if (au_ftest_ren(a->auren_flags, ISDIR_DST)) {
24499 +                       /* is this updating defined in POSIX? */
24500 +                       au_cpup_attr_timesizes(a->dst_inode);
24501 +                       au_cpup_attr_nlink(dir, /*force*/1);
24502 +               }
24503 +               au_dir_ts(dir, a->btgt);
24504 +       }
24505 +
24506 +       if (au_ftest_ren(a->auren_flags, ISSAMEDIR))
24507 +               return;
24508 +
24509 +       dir = a->src_dir;
24510 +       inode_inc_iversion(dir);
24511 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC))
24512 +               au_cpup_attr_nlink(dir, /*force*/1);
24513 +       au_dir_ts(dir, a->btgt);
24514 +}
24515 +
24516 +static void au_ren_refresh(struct au_ren_args *a)
24517 +{
24518 +       aufs_bindex_t bbot, bindex;
24519 +       struct dentry *d, *h_d;
24520 +       struct inode *i, *h_i;
24521 +       struct super_block *sb;
24522 +
24523 +       d = a->dst_dentry;
24524 +       d_drop(d);
24525 +       if (a->h_dst)
24526 +               /* already dget-ed by au_ren_or_cpup() */
24527 +               au_set_h_dptr(d, a->btgt, a->h_dst);
24528 +
24529 +       i = a->dst_inode;
24530 +       if (i) {
24531 +               if (!a->exchange) {
24532 +                       if (!au_ftest_ren(a->auren_flags, ISDIR_DST))
24533 +                               vfsub_drop_nlink(i);
24534 +                       else {
24535 +                               vfsub_dead_dir(i);
24536 +                               au_cpup_attr_timesizes(i);
24537 +                       }
24538 +                       au_update_dbrange(d, /*do_put_zero*/1);
24539 +               } else
24540 +                       au_cpup_attr_nlink(i, /*force*/1);
24541 +       } else {
24542 +               bbot = a->btgt;
24543 +               for (bindex = au_dbtop(d); bindex < bbot; bindex++)
24544 +                       au_set_h_dptr(d, bindex, NULL);
24545 +               bbot = au_dbbot(d);
24546 +               for (bindex = a->btgt + 1; bindex <= bbot; bindex++)
24547 +                       au_set_h_dptr(d, bindex, NULL);
24548 +               au_update_dbrange(d, /*do_put_zero*/0);
24549 +       }
24550 +
24551 +       if (a->exchange
24552 +           || au_ftest_ren(a->auren_flags, DIRREN)) {
24553 +               d_drop(a->src_dentry);
24554 +               if (au_ftest_ren(a->auren_flags, DIRREN))
24555 +                       au_set_dbwh(a->src_dentry, -1);
24556 +               return;
24557 +       }
24558 +
24559 +       d = a->src_dentry;
24560 +       au_set_dbwh(d, -1);
24561 +       bbot = au_dbbot(d);
24562 +       for (bindex = a->btgt + 1; bindex <= bbot; bindex++) {
24563 +               h_d = au_h_dptr(d, bindex);
24564 +               if (h_d)
24565 +                       au_set_h_dptr(d, bindex, NULL);
24566 +       }
24567 +       au_set_dbbot(d, a->btgt);
24568 +
24569 +       sb = d->d_sb;
24570 +       i = a->src_inode;
24571 +       if (au_opt_test(au_mntflags(sb), PLINK) && au_plink_test(i))
24572 +               return; /* success */
24573 +
24574 +       bbot = au_ibbot(i);
24575 +       for (bindex = a->btgt + 1; bindex <= bbot; bindex++) {
24576 +               h_i = au_h_iptr(i, bindex);
24577 +               if (h_i) {
24578 +                       au_xino_write(sb, bindex, h_i->i_ino, /*ino*/0);
24579 +                       /* ignore this error */
24580 +                       au_set_h_iptr(i, bindex, NULL, 0);
24581 +               }
24582 +       }
24583 +       au_set_ibbot(i, a->btgt);
24584 +}
24585 +
24586 +/* ---------------------------------------------------------------------- */
24587 +
24588 +/* mainly for link(2) and rename(2) */
24589 +int au_wbr(struct dentry *dentry, aufs_bindex_t btgt)
24590 +{
24591 +       aufs_bindex_t bdiropq, bwh;
24592 +       struct dentry *parent;
24593 +       struct au_branch *br;
24594 +
24595 +       parent = dentry->d_parent;
24596 +       IMustLock(d_inode(parent)); /* dir is locked */
24597 +
24598 +       bdiropq = au_dbdiropq(parent);
24599 +       bwh = au_dbwh(dentry);
24600 +       br = au_sbr(dentry->d_sb, btgt);
24601 +       if (au_br_rdonly(br)
24602 +           || (0 <= bdiropq && bdiropq < btgt)
24603 +           || (0 <= bwh && bwh < btgt))
24604 +               btgt = -1;
24605 +
24606 +       AuDbg("btgt %d\n", btgt);
24607 +       return btgt;
24608 +}
24609 +
24610 +/* sets src_btop, dst_btop and btgt */
24611 +static int au_ren_wbr(struct au_ren_args *a)
24612 +{
24613 +       int err;
24614 +       struct au_wr_dir_args wr_dir_args = {
24615 +               /* .force_btgt  = -1, */
24616 +               .flags          = AuWrDir_ADD_ENTRY
24617 +       };
24618 +
24619 +       a->src_btop = au_dbtop(a->src_dentry);
24620 +       a->dst_btop = au_dbtop(a->dst_dentry);
24621 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC)
24622 +           || au_ftest_ren(a->auren_flags, ISDIR_DST))
24623 +               au_fset_wrdir(wr_dir_args.flags, ISDIR);
24624 +       wr_dir_args.force_btgt = a->src_btop;
24625 +       if (a->dst_inode && a->dst_btop < a->src_btop)
24626 +               wr_dir_args.force_btgt = a->dst_btop;
24627 +       wr_dir_args.force_btgt = au_wbr(a->dst_dentry, wr_dir_args.force_btgt);
24628 +       err = au_wr_dir(a->dst_dentry, a->src_dentry, &wr_dir_args);
24629 +       a->btgt = err;
24630 +       if (a->exchange)
24631 +               au_update_dbtop(a->dst_dentry);
24632 +
24633 +       return err;
24634 +}
24635 +
24636 +static void au_ren_dt(struct au_ren_args *a)
24637 +{
24638 +       a->h_path.dentry = a->src_h_parent;
24639 +       au_dtime_store(a->src_dt + AuPARENT, a->src_parent, &a->h_path);
24640 +       if (!au_ftest_ren(a->auren_flags, ISSAMEDIR)) {
24641 +               a->h_path.dentry = a->dst_h_parent;
24642 +               au_dtime_store(a->dst_dt + AuPARENT, a->dst_parent, &a->h_path);
24643 +       }
24644 +
24645 +       au_fclr_ren(a->auren_flags, DT_DSTDIR);
24646 +       if (!au_ftest_ren(a->auren_flags, ISDIR_SRC)
24647 +           && !a->exchange)
24648 +               return;
24649 +
24650 +       a->h_path.dentry = a->src_h_dentry;
24651 +       au_dtime_store(a->src_dt + AuCHILD, a->src_dentry, &a->h_path);
24652 +       if (d_is_positive(a->dst_h_dentry)) {
24653 +               au_fset_ren(a->auren_flags, DT_DSTDIR);
24654 +               a->h_path.dentry = a->dst_h_dentry;
24655 +               au_dtime_store(a->dst_dt + AuCHILD, a->dst_dentry, &a->h_path);
24656 +       }
24657 +}
24658 +
24659 +static void au_ren_rev_dt(int err, struct au_ren_args *a)
24660 +{
24661 +       struct dentry *h_d;
24662 +       struct inode *h_inode;
24663 +
24664 +       au_dtime_revert(a->src_dt + AuPARENT);
24665 +       if (!au_ftest_ren(a->auren_flags, ISSAMEDIR))
24666 +               au_dtime_revert(a->dst_dt + AuPARENT);
24667 +
24668 +       if (au_ftest_ren(a->auren_flags, ISDIR_SRC) && err != -EIO) {
24669 +               h_d = a->src_dt[AuCHILD].dt_h_path.dentry;
24670 +               h_inode = d_inode(h_d);
24671 +               inode_lock_nested(h_inode, AuLsc_I_CHILD);
24672 +               au_dtime_revert(a->src_dt + AuCHILD);
24673 +               inode_unlock(h_inode);
24674 +
24675 +               if (au_ftest_ren(a->auren_flags, DT_DSTDIR)) {
24676 +                       h_d = a->dst_dt[AuCHILD].dt_h_path.dentry;
24677 +                       h_inode = d_inode(h_d);
24678 +                       inode_lock_nested(h_inode, AuLsc_I_CHILD);
24679 +                       au_dtime_revert(a->dst_dt + AuCHILD);
24680 +                       inode_unlock(h_inode);
24681 +               }
24682 +       }
24683 +}
24684 +
24685 +/* ---------------------------------------------------------------------- */
24686 +
24687 +int aufs_rename(struct user_namespace *userns,
24688 +               struct inode *_src_dir, struct dentry *_src_dentry,
24689 +               struct inode *_dst_dir, struct dentry *_dst_dentry,
24690 +               unsigned int _flags)
24691 +{
24692 +       int err, lock_flags;
24693 +       void *rev;
24694 +       /* reduce stack space */
24695 +       struct au_ren_args *a;
24696 +       struct au_pin pin;
24697 +
24698 +       AuDbg("%pd, %pd, 0x%x\n", _src_dentry, _dst_dentry, _flags);
24699 +       IMustLock(_src_dir);
24700 +       IMustLock(_dst_dir);
24701 +
24702 +       err = -EINVAL;
24703 +       if (unlikely(_flags & RENAME_WHITEOUT))
24704 +               goto out;
24705 +
24706 +       err = -ENOMEM;
24707 +       BUILD_BUG_ON(sizeof(*a) > PAGE_SIZE);
24708 +       a = kzalloc(sizeof(*a), GFP_NOFS);
24709 +       if (unlikely(!a))
24710 +               goto out;
24711 +
24712 +       a->flags = _flags;
24713 +       BUILD_BUG_ON(sizeof(a->exchange) == sizeof(u8)
24714 +                    && RENAME_EXCHANGE > U8_MAX);
24715 +       a->exchange = _flags & RENAME_EXCHANGE;
24716 +       a->src_dir = _src_dir;
24717 +       a->src_dentry = _src_dentry;
24718 +       a->src_inode = NULL;
24719 +       if (d_really_is_positive(a->src_dentry))
24720 +               a->src_inode = d_inode(a->src_dentry);
24721 +       a->src_parent = a->src_dentry->d_parent; /* dir inode is locked */
24722 +       a->dst_dir = _dst_dir;
24723 +       a->dst_dentry = _dst_dentry;
24724 +       a->dst_inode = NULL;
24725 +       if (d_really_is_positive(a->dst_dentry))
24726 +               a->dst_inode = d_inode(a->dst_dentry);
24727 +       a->dst_parent = a->dst_dentry->d_parent; /* dir inode is locked */
24728 +       if (a->dst_inode) {
24729 +               /*
24730 +                * if EXCHANGE && src is non-dir && dst is dir,
24731 +                * dst is not locked.
24732 +                */
24733 +               /* IMustLock(a->dst_inode); */
24734 +               au_igrab(a->dst_inode);
24735 +       }
24736 +
24737 +       err = -ENOTDIR;
24738 +       lock_flags = AuLock_FLUSH | AuLock_NOPLM | AuLock_GEN;
24739 +       if (d_is_dir(a->src_dentry)) {
24740 +               au_fset_ren(a->auren_flags, ISDIR_SRC);
24741 +               if (unlikely(!a->exchange
24742 +                            && d_really_is_positive(a->dst_dentry)
24743 +                            && !d_is_dir(a->dst_dentry)))
24744 +                       goto out_free;
24745 +               lock_flags |= AuLock_DIRS;
24746 +       }
24747 +       if (a->dst_inode && d_is_dir(a->dst_dentry)) {
24748 +               au_fset_ren(a->auren_flags, ISDIR_DST);
24749 +               if (unlikely(!a->exchange
24750 +                            && d_really_is_positive(a->src_dentry)
24751 +                            && !d_is_dir(a->src_dentry)))
24752 +                       goto out_free;
24753 +               lock_flags |= AuLock_DIRS;
24754 +       }
24755 +       err = aufs_read_and_write_lock2(a->dst_dentry, a->src_dentry,
24756 +                                       lock_flags);
24757 +       if (unlikely(err))
24758 +               goto out_free;
24759 +
24760 +       err = au_d_hashed_positive(a->src_dentry);
24761 +       if (unlikely(err))
24762 +               goto out_unlock;
24763 +       err = -ENOENT;
24764 +       if (a->dst_inode) {
24765 +               /*
24766 +                * If it is a dir, VFS unhash it before this
24767 +                * function. It means we cannot rely upon d_unhashed().
24768 +                */
24769 +               if (unlikely(!a->dst_inode->i_nlink))
24770 +                       goto out_unlock;
24771 +               if (!au_ftest_ren(a->auren_flags, ISDIR_DST)) {
24772 +                       err = au_d_hashed_positive(a->dst_dentry);
24773 +                       if (unlikely(err && !a->exchange))
24774 +                               goto out_unlock;
24775 +               } else if (unlikely(IS_DEADDIR(a->dst_inode)))
24776 +                       goto out_unlock;
24777 +       } else if (unlikely(d_unhashed(a->dst_dentry)))
24778 +               goto out_unlock;
24779 +
24780 +       /*
24781 +        * is it possible?
24782 +        * yes, it happened (in linux-3.3-rcN) but I don't know why.
24783 +        * there may exist a problem somewhere else.
24784 +        */
24785 +       err = -EINVAL;
24786 +       if (unlikely(d_inode(a->dst_parent) == d_inode(a->src_dentry)))
24787 +               goto out_unlock;
24788 +
24789 +       au_fset_ren(a->auren_flags, ISSAMEDIR); /* temporary */
24790 +       di_write_lock_parent(a->dst_parent);
24791 +
24792 +       /* which branch we process */
24793 +       err = au_ren_wbr(a);
24794 +       if (unlikely(err < 0))
24795 +               goto out_parent;
24796 +       a->br = au_sbr(a->dst_dentry->d_sb, a->btgt);
24797 +       a->h_path.mnt = au_br_mnt(a->br);
24798 +
24799 +       /* are they available to be renamed */
24800 +       err = au_ren_may_dir(a);
24801 +       if (unlikely(err))
24802 +               goto out_children;
24803 +
24804 +       /* prepare the writable parent dir on the same branch */
24805 +       if (a->dst_btop == a->btgt) {
24806 +               au_fset_ren(a->auren_flags, WHDST);
24807 +       } else {
24808 +               err = au_cpup_dirs(a->dst_dentry, a->btgt);
24809 +               if (unlikely(err))
24810 +                       goto out_children;
24811 +       }
24812 +
24813 +       err = 0;
24814 +       if (!a->exchange) {
24815 +               if (a->src_dir != a->dst_dir) {
24816 +                       /*
24817 +                        * this temporary unlock is safe,
24818 +                        * because both dir->i_mutex are locked.
24819 +                        */
24820 +                       di_write_unlock(a->dst_parent);
24821 +                       di_write_lock_parent(a->src_parent);
24822 +                       err = au_wr_dir_need_wh(a->src_dentry,
24823 +                                               au_ftest_ren(a->auren_flags,
24824 +                                                            ISDIR_SRC),
24825 +                                               &a->btgt);
24826 +                       di_write_unlock(a->src_parent);
24827 +                       di_write_lock2_parent(a->src_parent, a->dst_parent,
24828 +                                             /*isdir*/1);
24829 +                       au_fclr_ren(a->auren_flags, ISSAMEDIR);
24830 +               } else
24831 +                       err = au_wr_dir_need_wh(a->src_dentry,
24832 +                                               au_ftest_ren(a->auren_flags,
24833 +                                                            ISDIR_SRC),
24834 +                                               &a->btgt);
24835 +       }
24836 +       if (unlikely(err < 0))
24837 +               goto out_children;
24838 +       if (err)
24839 +               au_fset_ren(a->auren_flags, WHSRC);
24840 +
24841 +       /* cpup src */
24842 +       if (a->src_btop != a->btgt) {
24843 +               err = au_pin(&pin, a->src_dentry, a->btgt,
24844 +                            au_opt_udba(a->src_dentry->d_sb),
24845 +                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
24846 +               if (!err) {
24847 +                       struct au_cp_generic cpg = {
24848 +                               .dentry = a->src_dentry,
24849 +                               .bdst   = a->btgt,
24850 +                               .bsrc   = a->src_btop,
24851 +                               .len    = -1,
24852 +                               .pin    = &pin,
24853 +                               .flags  = AuCpup_DTIME | AuCpup_HOPEN
24854 +                       };
24855 +                       AuDebugOn(au_dbtop(a->src_dentry) != a->src_btop);
24856 +                       err = au_sio_cpup_simple(&cpg);
24857 +                       au_unpin(&pin);
24858 +               }
24859 +               if (unlikely(err))
24860 +                       goto out_children;
24861 +               a->src_btop = a->btgt;
24862 +               a->src_h_dentry = au_h_dptr(a->src_dentry, a->btgt);
24863 +               if (!a->exchange)
24864 +                       au_fset_ren(a->auren_flags, WHSRC);
24865 +       }
24866 +
24867 +       /* cpup dst */
24868 +       if (a->exchange && a->dst_inode
24869 +           && a->dst_btop != a->btgt) {
24870 +               err = au_pin(&pin, a->dst_dentry, a->btgt,
24871 +                            au_opt_udba(a->dst_dentry->d_sb),
24872 +                            AuPin_DI_LOCKED | AuPin_MNT_WRITE);
24873 +               if (!err) {
24874 +                       struct au_cp_generic cpg = {
24875 +                               .dentry = a->dst_dentry,
24876 +                               .bdst   = a->btgt,
24877 +                               .bsrc   = a->dst_btop,
24878 +                               .len    = -1,
24879 +                               .pin    = &pin,
24880 +                               .flags  = AuCpup_DTIME | AuCpup_HOPEN
24881 +                       };
24882 +                       err = au_sio_cpup_simple(&cpg);
24883 +                       au_unpin(&pin);
24884 +               }
24885 +               if (unlikely(err))
24886 +                       goto out_children;
24887 +               a->dst_btop = a->btgt;
24888 +               a->dst_h_dentry = au_h_dptr(a->dst_dentry, a->btgt);
24889 +       }
24890 +
24891 +       /* lock them all */
24892 +       err = au_ren_lock(a);
24893 +       if (unlikely(err))
24894 +               /* leave the copied-up one */
24895 +               goto out_children;
24896 +
24897 +       if (!a->exchange) {
24898 +               if (!au_opt_test(au_mntflags(a->dst_dir->i_sb), UDBA_NONE))
24899 +                       err = au_may_ren(a);
24900 +               else if (unlikely(a->dst_dentry->d_name.len > AUFS_MAX_NAMELEN))
24901 +                       err = -ENAMETOOLONG;
24902 +               if (unlikely(err))
24903 +                       goto out_hdir;
24904 +       }
24905 +
24906 +       /* store timestamps to be revertible */
24907 +       au_ren_dt(a);
24908 +
24909 +       /* store dirren info */
24910 +       if (au_ftest_ren(a->auren_flags, DIRREN)) {
24911 +               err = au_dr_rename(a->src_dentry, a->btgt,
24912 +                                  &a->dst_dentry->d_name, &rev);
24913 +               AuTraceErr(err);
24914 +               if (unlikely(err))
24915 +                       goto out_dt;
24916 +       }
24917 +
24918 +       /* here we go */
24919 +       err = do_rename(a);
24920 +       if (unlikely(err))
24921 +               goto out_dirren;
24922 +
24923 +       if (au_ftest_ren(a->auren_flags, DIRREN))
24924 +               au_dr_rename_fin(a->src_dentry, a->btgt, rev);
24925 +
24926 +       /* update dir attributes */
24927 +       au_ren_refresh_dir(a);
24928 +
24929 +       /* dput/iput all lower dentries */
24930 +       au_ren_refresh(a);
24931 +
24932 +       goto out_hdir; /* success */
24933 +
24934 +out_dirren:
24935 +       if (au_ftest_ren(a->auren_flags, DIRREN))
24936 +               au_dr_rename_rev(a->src_dentry, a->btgt, rev);
24937 +out_dt:
24938 +       au_ren_rev_dt(err, a);
24939 +out_hdir:
24940 +       au_ren_unlock(a);
24941 +out_children:
24942 +       au_nhash_wh_free(&a->whlist);
24943 +       if (err && a->dst_inode && a->dst_btop != a->btgt) {
24944 +               AuDbg("btop %d, btgt %d\n", a->dst_btop, a->btgt);
24945 +               au_set_h_dptr(a->dst_dentry, a->btgt, NULL);
24946 +               au_set_dbtop(a->dst_dentry, a->dst_btop);
24947 +       }
24948 +out_parent:
24949 +       if (!err) {
24950 +               if (d_unhashed(a->src_dentry))
24951 +                       au_fset_ren(a->auren_flags, DROPPED_SRC);
24952 +               if (d_unhashed(a->dst_dentry))
24953 +                       au_fset_ren(a->auren_flags, DROPPED_DST);
24954 +               if (!a->exchange)
24955 +                       d_move(a->src_dentry, a->dst_dentry);
24956 +               else {
24957 +                       d_exchange(a->src_dentry, a->dst_dentry);
24958 +                       if (au_ftest_ren(a->auren_flags, DROPPED_DST))
24959 +                               d_drop(a->dst_dentry);
24960 +               }
24961 +               if (au_ftest_ren(a->auren_flags, DROPPED_SRC))
24962 +                       d_drop(a->src_dentry);
24963 +       } else {
24964 +               au_update_dbtop(a->dst_dentry);
24965 +               if (!a->dst_inode)
24966 +                       d_drop(a->dst_dentry);
24967 +       }
24968 +       if (au_ftest_ren(a->auren_flags, ISSAMEDIR))
24969 +               di_write_unlock(a->dst_parent);
24970 +       else
24971 +               di_write_unlock2(a->src_parent, a->dst_parent);
24972 +out_unlock:
24973 +       aufs_read_and_write_unlock2(a->dst_dentry, a->src_dentry);
24974 +out_free:
24975 +       iput(a->dst_inode);
24976 +       if (a->thargs)
24977 +               au_whtmp_rmdir_free(a->thargs);
24978 +       au_kfree_rcu(a);
24979 +out:
24980 +       AuTraceErr(err);
24981 +       return err;
24982 +}
24983 diff -urN /usr/share/empty/fs/aufs/Kconfig linux/fs/aufs/Kconfig
24984 --- /usr/share/empty/fs/aufs/Kconfig    1970-01-01 01:00:00.000000000 +0100
24985 +++ linux/fs/aufs/Kconfig       2022-03-21 14:49:05.716633010 +0100
24986 @@ -0,0 +1,199 @@
24987 +# SPDX-License-Identifier: GPL-2.0
24988 +config AUFS_FS
24989 +       tristate "Aufs (Advanced multi layered unification filesystem) support"
24990 +       help
24991 +       Aufs is a stackable unification filesystem such as Unionfs,
24992 +       which unifies several directories and provides a merged single
24993 +       directory.
24994 +       In the early days, aufs was entirely re-designed and
24995 +       re-implemented Unionfs Version 1.x series. Introducing many
24996 +       original ideas, approaches and improvements, it becomes totally
24997 +       different from Unionfs while keeping the basic features.
24998 +
24999 +if AUFS_FS
25000 +choice
25001 +       prompt "Maximum number of branches"
25002 +       default AUFS_BRANCH_MAX_127
25003 +       help
25004 +       Specifies the maximum number of branches (or member directories)
25005 +       in a single aufs. The larger value consumes more system
25006 +       resources and has a minor impact to performance.
25007 +config AUFS_BRANCH_MAX_127
25008 +       bool "127"
25009 +       help
25010 +       Specifies the maximum number of branches (or member directories)
25011 +       in a single aufs. The larger value consumes more system
25012 +       resources and has a minor impact to performance.
25013 +config AUFS_BRANCH_MAX_511
25014 +       bool "511"
25015 +       help
25016 +       Specifies the maximum number of branches (or member directories)
25017 +       in a single aufs. The larger value consumes more system
25018 +       resources and has a minor impact to performance.
25019 +config AUFS_BRANCH_MAX_1023
25020 +       bool "1023"
25021 +       help
25022 +       Specifies the maximum number of branches (or member directories)
25023 +       in a single aufs. The larger value consumes more system
25024 +       resources and has a minor impact to performance.
25025 +config AUFS_BRANCH_MAX_32767
25026 +       bool "32767"
25027 +       help
25028 +       Specifies the maximum number of branches (or member directories)
25029 +       in a single aufs. The larger value consumes more system
25030 +       resources and has a minor impact to performance.
25031 +endchoice
25032 +
25033 +config AUFS_SBILIST
25034 +       bool
25035 +       depends on AUFS_MAGIC_SYSRQ || PROC_FS
25036 +       default y
25037 +       help
25038 +       Automatic configuration for internal use.
25039 +       When aufs supports Magic SysRq or /proc, enabled automatically.
25040 +
25041 +config AUFS_HNOTIFY
25042 +       bool "Detect direct branch access (bypassing aufs)"
25043 +       help
25044 +       If you want to modify files on branches directly, eg. bypassing aufs,
25045 +       and want aufs to detect the changes of them fully, then enable this
25046 +       option and use 'udba=notify' mount option.
25047 +       Currently there is only one available configuration, "fsnotify".
25048 +       It will have a negative impact to the performance.
25049 +       See detail in aufs.5.
25050 +
25051 +choice
25052 +       prompt "method" if AUFS_HNOTIFY
25053 +       default AUFS_HFSNOTIFY
25054 +config AUFS_HFSNOTIFY
25055 +       bool "fsnotify"
25056 +       select FSNOTIFY
25057 +endchoice
25058 +
25059 +config AUFS_EXPORT
25060 +       bool "NFS-exportable aufs"
25061 +       depends on EXPORTFS
25062 +       help
25063 +       If you want to export your mounted aufs via NFS, then enable this
25064 +       option. There are several requirements for this configuration.
25065 +       See detail in aufs.5.
25066 +
25067 +config AUFS_INO_T_64
25068 +       bool
25069 +       depends on AUFS_EXPORT
25070 +       depends on 64BIT && !(ALPHA || S390)
25071 +       default y
25072 +       help
25073 +       Automatic configuration for internal use.
25074 +       /* typedef unsigned long/int __kernel_ino_t */
25075 +       /* alpha and s390x are int */
25076 +
25077 +config AUFS_XATTR
25078 +       bool "support for XATTR/EA (including Security Labels)"
25079 +       help
25080 +       If your branch fs supports XATTR/EA and you want to make them
25081 +       available in aufs too, then enable this opsion and specify the
25082 +       branch attributes for EA.
25083 +       See detail in aufs.5.
25084 +
25085 +config AUFS_FHSM
25086 +       bool "File-based Hierarchical Storage Management"
25087 +       help
25088 +       Hierarchical Storage Management (or HSM) is a well-known feature
25089 +       in the storage world. Aufs provides this feature as file-based.
25090 +       with multiple branches.
25091 +       These multiple branches are prioritized, ie. the topmost one
25092 +       should be the fastest drive and be used heavily.
25093 +
25094 +config AUFS_RDU
25095 +       bool "Readdir in userspace"
25096 +       help
25097 +       Aufs has two methods to provide a merged view for a directory,
25098 +       by a user-space library and by kernel-space natively. The latter
25099 +       is always enabled but sometimes large and slow.
25100 +       If you enable this option, install the library in aufs2-util
25101 +       package, and set some environment variables for your readdir(3),
25102 +       then the work will be handled in user-space which generally
25103 +       shows better performance in most cases.
25104 +       See detail in aufs.5.
25105 +
25106 +config AUFS_DIRREN
25107 +       bool "Workaround for rename(2)-ing a directory"
25108 +       help
25109 +       By default, aufs returns EXDEV error in renameing a dir who has
25110 +       his child on the lower branch, since it is a bad idea to issue
25111 +       rename(2) internally for every lower branch. But user may not
25112 +       accept this behaviour. So here is a workaround to allow such
25113 +       rename(2) and store some extra infromation on the writable
25114 +       branch. Obviously this costs high (and I don't like it).
25115 +       To use this feature, you need to enable this configuration AND
25116 +       to specify the mount option `dirren.'
25117 +       See details in aufs.5 and the design documents.
25118 +
25119 +config AUFS_SHWH
25120 +       bool "Show whiteouts"
25121 +       help
25122 +       If you want to make the whiteouts in aufs visible, then enable
25123 +       this option and specify 'shwh' mount option. Although it may
25124 +       sounds like philosophy or something, but in technically it
25125 +       simply shows the name of whiteout with keeping its behaviour.
25126 +
25127 +config AUFS_BR_RAMFS
25128 +       bool "Ramfs (initramfs/rootfs) as an aufs branch"
25129 +       help
25130 +       If you want to use ramfs as an aufs branch fs, then enable this
25131 +       option. Generally tmpfs is recommended.
25132 +       Aufs prohibited them to be a branch fs by default, because
25133 +       initramfs becomes unusable after switch_root or something
25134 +       generally. If you sets initramfs as an aufs branch and boot your
25135 +       system by switch_root, you will meet a problem easily since the
25136 +       files in initramfs may be inaccessible.
25137 +       Unless you are going to use ramfs as an aufs branch fs without
25138 +       switch_root or something, leave it N.
25139 +
25140 +config AUFS_BR_FUSE
25141 +       bool "Fuse fs as an aufs branch"
25142 +       depends on FUSE_FS
25143 +       select AUFS_POLL
25144 +       help
25145 +       If you want to use fuse-based userspace filesystem as an aufs
25146 +       branch fs, then enable this option.
25147 +       It implements the internal poll(2) operation which is
25148 +       implemented by fuse only (curretnly).
25149 +
25150 +config AUFS_POLL
25151 +       bool
25152 +       help
25153 +       Automatic configuration for internal use.
25154 +
25155 +config AUFS_BR_HFSPLUS
25156 +       bool "Hfsplus as an aufs branch"
25157 +       depends on HFSPLUS_FS
25158 +       default y
25159 +       help
25160 +       If you want to use hfsplus fs as an aufs branch fs, then enable
25161 +       this option. This option introduces a small overhead at
25162 +       copying-up a file on hfsplus.
25163 +
25164 +config AUFS_BDEV_LOOP
25165 +       bool
25166 +       depends on BLK_DEV_LOOP
25167 +       default y
25168 +       help
25169 +       Automatic configuration for internal use.
25170 +       Convert =[ym] into =y.
25171 +
25172 +config AUFS_DEBUG
25173 +       bool "Debug aufs"
25174 +       help
25175 +       Enable this to compile aufs internal debug code.
25176 +       It will have a negative impact to the performance.
25177 +
25178 +config AUFS_MAGIC_SYSRQ
25179 +       bool
25180 +       depends on AUFS_DEBUG && MAGIC_SYSRQ
25181 +       default y
25182 +       help
25183 +       Automatic configuration for internal use.
25184 +       When aufs supports Magic SysRq, enabled automatically.
25185 +endif
25186 diff -urN /usr/share/empty/fs/aufs/lcnt.h linux/fs/aufs/lcnt.h
25187 --- /usr/share/empty/fs/aufs/lcnt.h     1970-01-01 01:00:00.000000000 +0100
25188 +++ linux/fs/aufs/lcnt.h        2022-03-21 14:49:05.726633010 +0100
25189 @@ -0,0 +1,186 @@
25190 +/* SPDX-License-Identifier: GPL-2.0 */
25191 +/*
25192 + * Copyright (C) 2018-2021 Junjiro R. Okajima
25193 + *
25194 + * This program, aufs is free software; you can redistribute it and/or modify
25195 + * it under the terms of the GNU General Public License as published by
25196 + * the Free Software Foundation; either version 2 of the License, or
25197 + * (at your option) any later version.
25198 + *
25199 + * This program is distributed in the hope that it will be useful,
25200 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
25201 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25202 + * GNU General Public License for more details.
25203 + *
25204 + * You should have received a copy of the GNU General Public License
25205 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25206 + */
25207 +
25208 +/*
25209 + * simple long counter wrapper
25210 + */
25211 +
25212 +#ifndef __AUFS_LCNT_H__
25213 +#define __AUFS_LCNT_H__
25214 +
25215 +#ifdef __KERNEL__
25216 +
25217 +#include "debug.h"
25218 +
25219 +#define AuLCntATOMIC   1
25220 +#define AuLCntPCPUCNT  2
25221 +/*
25222 + * why does percpu_refcount require extra synchronize_rcu()s in
25223 + * au_br_do_free()
25224 + */
25225 +#define AuLCntPCPUREF  3
25226 +
25227 +/* #define AuLCntChosen        AuLCntATOMIC */
25228 +#define AuLCntChosen   AuLCntPCPUCNT
25229 +/* #define AuLCntChosen        AuLCntPCPUREF */
25230 +
25231 +#if AuLCntChosen == AuLCntATOMIC
25232 +#include <linux/atomic.h>
25233 +
25234 +typedef atomic_long_t au_lcnt_t;
25235 +
25236 +static inline int au_lcnt_init(au_lcnt_t *cnt, void *release __maybe_unused)
25237 +{
25238 +       atomic_long_set(cnt, 0);
25239 +       return 0;
25240 +}
25241 +
25242 +static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
25243 +{
25244 +       /* empty */
25245 +}
25246 +
25247 +static inline void au_lcnt_fin(au_lcnt_t *cnt __maybe_unused,
25248 +                              int do_sync __maybe_unused)
25249 +{
25250 +       /* empty */
25251 +}
25252 +
25253 +static inline void au_lcnt_inc(au_lcnt_t *cnt)
25254 +{
25255 +       atomic_long_inc(cnt);
25256 +}
25257 +
25258 +static inline void au_lcnt_dec(au_lcnt_t *cnt)
25259 +{
25260 +       atomic_long_dec(cnt);
25261 +}
25262 +
25263 +static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev __maybe_unused)
25264 +{
25265 +       return atomic_long_read(cnt);
25266 +}
25267 +#endif
25268 +
25269 +#if AuLCntChosen == AuLCntPCPUCNT
25270 +#include <linux/percpu_counter.h>
25271 +
25272 +typedef struct percpu_counter au_lcnt_t;
25273 +
25274 +static inline int au_lcnt_init(au_lcnt_t *cnt, void *release __maybe_unused)
25275 +{
25276 +       return percpu_counter_init(cnt, 0, GFP_NOFS);
25277 +}
25278 +
25279 +static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
25280 +{
25281 +       /* empty */
25282 +}
25283 +
25284 +static inline void au_lcnt_fin(au_lcnt_t *cnt, int do_sync __maybe_unused)
25285 +{
25286 +       percpu_counter_destroy(cnt);
25287 +}
25288 +
25289 +static inline void au_lcnt_inc(au_lcnt_t *cnt)
25290 +{
25291 +       percpu_counter_inc(cnt);
25292 +}
25293 +
25294 +static inline void au_lcnt_dec(au_lcnt_t *cnt)
25295 +{
25296 +       percpu_counter_dec(cnt);
25297 +}
25298 +
25299 +static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev __maybe_unused)
25300 +{
25301 +       s64 n;
25302 +
25303 +       n = percpu_counter_sum(cnt);
25304 +       BUG_ON(n < 0);
25305 +       if (LONG_MAX != LLONG_MAX
25306 +           && n > LONG_MAX)
25307 +               AuWarn1("%s\n", "wrap-around");
25308 +
25309 +       return n;
25310 +}
25311 +#endif
25312 +
25313 +#if AuLCntChosen == AuLCntPCPUREF
25314 +#include <linux/percpu-refcount.h>
25315 +
25316 +typedef struct percpu_ref au_lcnt_t;
25317 +
25318 +static inline int au_lcnt_init(au_lcnt_t *cnt, percpu_ref_func_t *release)
25319 +{
25320 +       if (!release)
25321 +               release = percpu_ref_exit;
25322 +       return percpu_ref_init(cnt, release, /*percpu mode*/0, GFP_NOFS);
25323 +}
25324 +
25325 +static inline void au_lcnt_wait_for_fin(au_lcnt_t *cnt __maybe_unused)
25326 +{
25327 +       synchronize_rcu();
25328 +}
25329 +
25330 +static inline void au_lcnt_fin(au_lcnt_t *cnt, int do_sync)
25331 +{
25332 +       percpu_ref_kill(cnt);
25333 +       if (do_sync)
25334 +               au_lcnt_wait_for_fin(cnt);
25335 +}
25336 +
25337 +static inline void au_lcnt_inc(au_lcnt_t *cnt)
25338 +{
25339 +       percpu_ref_get(cnt);
25340 +}
25341 +
25342 +static inline void au_lcnt_dec(au_lcnt_t *cnt)
25343 +{
25344 +       percpu_ref_put(cnt);
25345 +}
25346 +
25347 +/*
25348 + * avoid calling this func as possible.
25349 + */
25350 +static inline long au_lcnt_read(au_lcnt_t *cnt, int do_rev)
25351 +{
25352 +       long l;
25353 +
25354 +       percpu_ref_switch_to_atomic_sync(cnt);
25355 +       l = atomic_long_read(&cnt->count);
25356 +       if (do_rev)
25357 +               percpu_ref_switch_to_percpu(cnt);
25358 +
25359 +       /* percpu_ref is initialized by 1 instead of 0 */
25360 +       return l - 1;
25361 +}
25362 +#endif
25363 +
25364 +#ifdef CONFIG_AUFS_DEBUG
25365 +#define AuLCntZero(val) do {                   \
25366 +       long l = val;                           \
25367 +       if (l)                                  \
25368 +               AuDbg("%s = %ld\n", #val, l);   \
25369 +} while (0)
25370 +#else
25371 +#define AuLCntZero(val)                do {} while (0)
25372 +#endif
25373 +
25374 +#endif /* __KERNEL__ */
25375 +#endif /* __AUFS_LCNT_H__ */
25376 diff -urN /usr/share/empty/fs/aufs/loop.c linux/fs/aufs/loop.c
25377 --- /usr/share/empty/fs/aufs/loop.c     1970-01-01 01:00:00.000000000 +0100
25378 +++ linux/fs/aufs/loop.c        2022-03-21 14:49:05.726633010 +0100
25379 @@ -0,0 +1,148 @@
25380 +// SPDX-License-Identifier: GPL-2.0
25381 +/*
25382 + * Copyright (C) 2005-2021 Junjiro R. Okajima
25383 + *
25384 + * This program, aufs is free software; you can redistribute it and/or modify
25385 + * it under the terms of the GNU General Public License as published by
25386 + * the Free Software Foundation; either version 2 of the License, or
25387 + * (at your option) any later version.
25388 + *
25389 + * This program is distributed in the hope that it will be useful,
25390 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
25391 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25392 + * GNU General Public License for more details.
25393 + *
25394 + * You should have received a copy of the GNU General Public License
25395 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25396 + */
25397 +
25398 +/*
25399 + * support for loopback block device as a branch
25400 + */
25401 +
25402 +#include "aufs.h"
25403 +
25404 +/* added into drivers/block/loop.c */
25405 +static struct file *(*backing_file_func)(struct super_block *sb);
25406 +
25407 +/*
25408 + * test if two lower dentries have overlapping branches.
25409 + */
25410 +int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding)
25411 +{
25412 +       struct super_block *h_sb;
25413 +       struct file *backing_file;
25414 +
25415 +       if (unlikely(!backing_file_func)) {
25416 +               /* don't load "loop" module here */
25417 +               backing_file_func = symbol_get(loop_backing_file);
25418 +               if (unlikely(!backing_file_func))
25419 +                       /* "loop" module is not loaded */
25420 +                       return 0;
25421 +       }
25422 +
25423 +       h_sb = h_adding->d_sb;
25424 +       backing_file = backing_file_func(h_sb);
25425 +       if (!backing_file)
25426 +               return 0;
25427 +
25428 +       h_adding = backing_file->f_path.dentry;
25429 +       /*
25430 +        * h_adding can be local NFS.
25431 +        * in this case aufs cannot detect the loop.
25432 +        */
25433 +       if (unlikely(h_adding->d_sb == sb))
25434 +               return 1;
25435 +       return !!au_test_subdir(h_adding, sb->s_root);
25436 +}
25437 +
25438 +/* true if a kernel thread named 'loop[0-9].*' accesses a file */
25439 +int au_test_loopback_kthread(void)
25440 +{
25441 +       int ret;
25442 +       struct task_struct *tsk = current;
25443 +       char c, comm[sizeof(tsk->comm)];
25444 +
25445 +       ret = 0;
25446 +       if (tsk->flags & PF_KTHREAD) {
25447 +               get_task_comm(comm, tsk);
25448 +               c = comm[4];
25449 +               ret = ('0' <= c && c <= '9'
25450 +                      && !strncmp(comm, "loop", 4));
25451 +       }
25452 +
25453 +       return ret;
25454 +}
25455 +
25456 +/* ---------------------------------------------------------------------- */
25457 +
25458 +#define au_warn_loopback_step  16
25459 +static int au_warn_loopback_nelem = au_warn_loopback_step;
25460 +static unsigned long *au_warn_loopback_array;
25461 +
25462 +void au_warn_loopback(struct super_block *h_sb)
25463 +{
25464 +       int i, new_nelem;
25465 +       unsigned long *a, magic;
25466 +       static DEFINE_SPINLOCK(spin);
25467 +
25468 +       magic = h_sb->s_magic;
25469 +       spin_lock(&spin);
25470 +       a = au_warn_loopback_array;
25471 +       for (i = 0; i < au_warn_loopback_nelem && *a; i++)
25472 +               if (a[i] == magic) {
25473 +                       spin_unlock(&spin);
25474 +                       return;
25475 +               }
25476 +
25477 +       /* h_sb is new to us, print it */
25478 +       if (i < au_warn_loopback_nelem) {
25479 +               a[i] = magic;
25480 +               goto pr;
25481 +       }
25482 +
25483 +       /* expand the array */
25484 +       new_nelem = au_warn_loopback_nelem + au_warn_loopback_step;
25485 +       a = au_kzrealloc(au_warn_loopback_array,
25486 +                        au_warn_loopback_nelem * sizeof(unsigned long),
25487 +                        new_nelem * sizeof(unsigned long), GFP_ATOMIC,
25488 +                        /*may_shrink*/0);
25489 +       if (a) {
25490 +               au_warn_loopback_nelem = new_nelem;
25491 +               au_warn_loopback_array = a;
25492 +               a[i] = magic;
25493 +               goto pr;
25494 +       }
25495 +
25496 +       spin_unlock(&spin);
25497 +       AuWarn1("realloc failed, ignored\n");
25498 +       return;
25499 +
25500 +pr:
25501 +       spin_unlock(&spin);
25502 +       pr_warn("you may want to try another patch for loopback file "
25503 +               "on %s(0x%lx) branch\n", au_sbtype(h_sb), magic);
25504 +}
25505 +
25506 +int au_loopback_init(void)
25507 +{
25508 +       int err;
25509 +       struct super_block *sb __maybe_unused;
25510 +
25511 +       BUILD_BUG_ON(sizeof(sb->s_magic) != sizeof(*au_warn_loopback_array));
25512 +
25513 +       err = 0;
25514 +       au_warn_loopback_array = kcalloc(au_warn_loopback_step,
25515 +                                        sizeof(unsigned long), GFP_NOFS);
25516 +       if (unlikely(!au_warn_loopback_array))
25517 +               err = -ENOMEM;
25518 +
25519 +       return err;
25520 +}
25521 +
25522 +void au_loopback_fin(void)
25523 +{
25524 +       if (backing_file_func)
25525 +               symbol_put(loop_backing_file);
25526 +       au_kfree_try_rcu(au_warn_loopback_array);
25527 +}
25528 diff -urN /usr/share/empty/fs/aufs/loop.h linux/fs/aufs/loop.h
25529 --- /usr/share/empty/fs/aufs/loop.h     1970-01-01 01:00:00.000000000 +0100
25530 +++ linux/fs/aufs/loop.h        2022-03-21 14:49:05.726633010 +0100
25531 @@ -0,0 +1,55 @@
25532 +/* SPDX-License-Identifier: GPL-2.0 */
25533 +/*
25534 + * Copyright (C) 2005-2021 Junjiro R. Okajima
25535 + *
25536 + * This program, aufs is free software; you can redistribute it and/or modify
25537 + * it under the terms of the GNU General Public License as published by
25538 + * the Free Software Foundation; either version 2 of the License, or
25539 + * (at your option) any later version.
25540 + *
25541 + * This program is distributed in the hope that it will be useful,
25542 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
25543 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25544 + * GNU General Public License for more details.
25545 + *
25546 + * You should have received a copy of the GNU General Public License
25547 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25548 + */
25549 +
25550 +/*
25551 + * support for loopback mount as a branch
25552 + */
25553 +
25554 +#ifndef __AUFS_LOOP_H__
25555 +#define __AUFS_LOOP_H__
25556 +
25557 +#ifdef __KERNEL__
25558 +
25559 +struct dentry;
25560 +struct super_block;
25561 +
25562 +#ifdef CONFIG_AUFS_BDEV_LOOP
25563 +/* drivers/block/loop.c */
25564 +struct file *loop_backing_file(struct super_block *sb);
25565 +
25566 +/* loop.c */
25567 +int au_test_loopback_overlap(struct super_block *sb, struct dentry *h_adding);
25568 +int au_test_loopback_kthread(void);
25569 +void au_warn_loopback(struct super_block *h_sb);
25570 +
25571 +int au_loopback_init(void);
25572 +void au_loopback_fin(void);
25573 +#else
25574 +AuStub(struct file *, loop_backing_file, return NULL, struct super_block *sb)
25575 +
25576 +AuStubInt0(au_test_loopback_overlap, struct super_block *sb,
25577 +          struct dentry *h_adding)
25578 +AuStubInt0(au_test_loopback_kthread, void)
25579 +AuStubVoid(au_warn_loopback, struct super_block *h_sb)
25580 +
25581 +AuStubInt0(au_loopback_init, void)
25582 +AuStubVoid(au_loopback_fin, void)
25583 +#endif /* BLK_DEV_LOOP */
25584 +
25585 +#endif /* __KERNEL__ */
25586 +#endif /* __AUFS_LOOP_H__ */
25587 diff -urN /usr/share/empty/fs/aufs/magic.mk linux/fs/aufs/magic.mk
25588 --- /usr/share/empty/fs/aufs/magic.mk   1970-01-01 01:00:00.000000000 +0100
25589 +++ linux/fs/aufs/magic.mk      2022-03-21 14:49:05.726633010 +0100
25590 @@ -0,0 +1,31 @@
25591 +# SPDX-License-Identifier: GPL-2.0
25592 +
25593 +# defined in ${srctree}/fs/fuse/inode.c
25594 +# tristate
25595 +ifdef CONFIG_FUSE_FS
25596 +ccflags-y += -DFUSE_SUPER_MAGIC=0x65735546
25597 +endif
25598 +
25599 +# defined in ${srctree}/fs/xfs/xfs_sb.h
25600 +# tristate
25601 +ifdef CONFIG_XFS_FS
25602 +ccflags-y += -DXFS_SB_MAGIC=0x58465342
25603 +endif
25604 +
25605 +# defined in ${srctree}/fs/configfs/mount.c
25606 +# tristate
25607 +ifdef CONFIG_CONFIGFS_FS
25608 +ccflags-y += -DCONFIGFS_MAGIC=0x62656570
25609 +endif
25610 +
25611 +# defined in ${srctree}/fs/ubifs/ubifs.h
25612 +# tristate
25613 +ifdef CONFIG_UBIFS_FS
25614 +ccflags-y += -DUBIFS_SUPER_MAGIC=0x24051905
25615 +endif
25616 +
25617 +# defined in ${srctree}/fs/hfsplus/hfsplus_raw.h
25618 +# tristate
25619 +ifdef CONFIG_HFSPLUS_FS
25620 +ccflags-y += -DHFSPLUS_SUPER_MAGIC=0x482b
25621 +endif
25622 diff -urN /usr/share/empty/fs/aufs/Makefile linux/fs/aufs/Makefile
25623 --- /usr/share/empty/fs/aufs/Makefile   1970-01-01 01:00:00.000000000 +0100
25624 +++ linux/fs/aufs/Makefile      2022-03-21 14:49:05.716633010 +0100
25625 @@ -0,0 +1,46 @@
25626 +# SPDX-License-Identifier: GPL-2.0
25627 +
25628 +include ${src}/magic.mk
25629 +ifeq (${CONFIG_AUFS_FS},m)
25630 +include ${src}/conf.mk
25631 +endif
25632 +-include ${src}/priv_def.mk
25633 +
25634 +# cf. include/linux/kernel.h
25635 +# enable pr_debug
25636 +ccflags-y += -DDEBUG
25637 +# sparse requires the full pathname
25638 +ifdef M
25639 +ccflags-y += -include ${M}/../../include/uapi/linux/aufs_type.h
25640 +else
25641 +ccflags-y += -include ${srctree}/include/uapi/linux/aufs_type.h
25642 +endif
25643 +
25644 +obj-$(CONFIG_AUFS_FS) += aufs.o
25645 +aufs-y := module.o sbinfo.o super.o branch.o xino.o sysaufs.o opts.o fsctx.o \
25646 +       wkq.o vfsub.o dcsub.o \
25647 +       cpup.o whout.o wbr_policy.o \
25648 +       dinfo.o dentry.o \
25649 +       dynop.o \
25650 +       finfo.o file.o f_op.o \
25651 +       dir.o vdir.o \
25652 +       iinfo.o inode.o i_op.o i_op_add.o i_op_del.o i_op_ren.o \
25653 +       mvdown.o ioctl.o
25654 +
25655 +# all are boolean
25656 +aufs-$(CONFIG_PROC_FS) += procfs.o plink.o
25657 +aufs-$(CONFIG_SYSFS) += sysfs.o
25658 +aufs-$(CONFIG_DEBUG_FS) += dbgaufs.o
25659 +aufs-$(CONFIG_AUFS_BDEV_LOOP) += loop.o
25660 +aufs-$(CONFIG_AUFS_HNOTIFY) += hnotify.o
25661 +aufs-$(CONFIG_AUFS_HFSNOTIFY) += hfsnotify.o
25662 +aufs-$(CONFIG_AUFS_EXPORT) += export.o
25663 +aufs-$(CONFIG_AUFS_XATTR) += xattr.o
25664 +aufs-$(CONFIG_FS_POSIX_ACL) += posix_acl.o
25665 +aufs-$(CONFIG_AUFS_DIRREN) += dirren.o
25666 +aufs-$(CONFIG_AUFS_FHSM) += fhsm.o
25667 +aufs-$(CONFIG_AUFS_POLL) += poll.o
25668 +aufs-$(CONFIG_AUFS_RDU) += rdu.o
25669 +aufs-$(CONFIG_AUFS_BR_HFSPLUS) += hfsplus.o
25670 +aufs-$(CONFIG_AUFS_DEBUG) += debug.o
25671 +aufs-$(CONFIG_AUFS_MAGIC_SYSRQ) += sysrq.o
25672 diff -urN /usr/share/empty/fs/aufs/module.c linux/fs/aufs/module.c
25673 --- /usr/share/empty/fs/aufs/module.c   1970-01-01 01:00:00.000000000 +0100
25674 +++ linux/fs/aufs/module.c      2022-03-21 14:49:05.726633010 +0100
25675 @@ -0,0 +1,273 @@
25676 +// SPDX-License-Identifier: GPL-2.0
25677 +/*
25678 + * Copyright (C) 2005-2021 Junjiro R. Okajima
25679 + *
25680 + * This program, aufs is free software; you can redistribute it and/or modify
25681 + * it under the terms of the GNU General Public License as published by
25682 + * the Free Software Foundation; either version 2 of the License, or
25683 + * (at your option) any later version.
25684 + *
25685 + * This program is distributed in the hope that it will be useful,
25686 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
25687 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25688 + * GNU General Public License for more details.
25689 + *
25690 + * You should have received a copy of the GNU General Public License
25691 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25692 + */
25693 +
25694 +/*
25695 + * module global variables and operations
25696 + */
25697 +
25698 +#include <linux/module.h>
25699 +#include <linux/seq_file.h>
25700 +#include "aufs.h"
25701 +
25702 +/* shrinkable realloc */
25703 +void *au_krealloc(void *p, unsigned int new_sz, gfp_t gfp, int may_shrink)
25704 +{
25705 +       size_t sz;
25706 +       int diff;
25707 +
25708 +       sz = 0;
25709 +       diff = -1;
25710 +       if (p) {
25711 +#if 0 /* unused */
25712 +               if (!new_sz) {
25713 +                       au_kfree_rcu(p);
25714 +                       p = NULL;
25715 +                       goto out;
25716 +               }
25717 +#else
25718 +               AuDebugOn(!new_sz);
25719 +#endif
25720 +               sz = ksize(p);
25721 +               diff = au_kmidx_sub(sz, new_sz);
25722 +       }
25723 +       if (sz && !diff)
25724 +               goto out;
25725 +
25726 +       if (sz < new_sz)
25727 +               /* expand or SLOB */
25728 +               p = krealloc(p, new_sz, gfp);
25729 +       else if (new_sz < sz && may_shrink) {
25730 +               /* shrink */
25731 +               void *q;
25732 +
25733 +               q = kmalloc(new_sz, gfp);
25734 +               if (q) {
25735 +                       if (p) {
25736 +                               memcpy(q, p, new_sz);
25737 +                               au_kfree_try_rcu(p);
25738 +                       }
25739 +                       p = q;
25740 +               } else
25741 +                       p = NULL;
25742 +       }
25743 +
25744 +out:
25745 +       return p;
25746 +}
25747 +
25748 +void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp,
25749 +                  int may_shrink)
25750 +{
25751 +       p = au_krealloc(p, new_sz, gfp, may_shrink);
25752 +       if (p && new_sz > nused)
25753 +               memset(p + nused, 0, new_sz - nused);
25754 +       return p;
25755 +}
25756 +
25757 +/* ---------------------------------------------------------------------- */
25758 +/*
25759 + * aufs caches
25760 + */
25761 +struct kmem_cache *au_cache[AuCache_Last];
25762 +
25763 +static void au_cache_fin(void)
25764 +{
25765 +       int i;
25766 +
25767 +       /*
25768 +        * Make sure all delayed rcu free inodes are flushed before we
25769 +        * destroy cache.
25770 +        */
25771 +       rcu_barrier();
25772 +
25773 +       /* excluding AuCache_HNOTIFY */
25774 +       BUILD_BUG_ON(AuCache_HNOTIFY + 1 != AuCache_Last);
25775 +       for (i = 0; i < AuCache_HNOTIFY; i++) {
25776 +               kmem_cache_destroy(au_cache[i]);
25777 +               au_cache[i] = NULL;
25778 +       }
25779 +}
25780 +
25781 +static int __init au_cache_init(void)
25782 +{
25783 +       au_cache[AuCache_DINFO] = AuCacheCtor(au_dinfo, au_di_init_once);
25784 +       if (au_cache[AuCache_DINFO])
25785 +               /* SLAB_DESTROY_BY_RCU */
25786 +               au_cache[AuCache_ICNTNR] = AuCacheCtor(au_icntnr,
25787 +                                                      au_icntnr_init_once);
25788 +       if (au_cache[AuCache_ICNTNR])
25789 +               au_cache[AuCache_FINFO] = AuCacheCtor(au_finfo,
25790 +                                                     au_fi_init_once);
25791 +       if (au_cache[AuCache_FINFO])
25792 +               au_cache[AuCache_VDIR] = AuCache(au_vdir);
25793 +       if (au_cache[AuCache_VDIR])
25794 +               au_cache[AuCache_DEHSTR] = AuCache(au_vdir_dehstr);
25795 +       if (au_cache[AuCache_DEHSTR])
25796 +               return 0;
25797 +
25798 +       au_cache_fin();
25799 +       return -ENOMEM;
25800 +}
25801 +
25802 +/* ---------------------------------------------------------------------- */
25803 +
25804 +int au_dir_roflags;
25805 +
25806 +#ifdef CONFIG_AUFS_SBILIST
25807 +/*
25808 + * iterate_supers_type() doesn't protect us from
25809 + * remounting (branch management)
25810 + */
25811 +struct hlist_bl_head au_sbilist;
25812 +#endif
25813 +
25814 +/*
25815 + * functions for module interface.
25816 + */
25817 +MODULE_LICENSE("GPL");
25818 +/* MODULE_LICENSE("GPL v2"); */
25819 +MODULE_AUTHOR("Junjiro R. Okajima <aufs-users@lists.sourceforge.net>");
25820 +MODULE_DESCRIPTION(AUFS_NAME
25821 +       " -- Advanced multi layered unification filesystem");
25822 +MODULE_VERSION(AUFS_VERSION);
25823 +MODULE_ALIAS_FS(AUFS_NAME);
25824 +
25825 +/* this module parameter has no meaning when SYSFS is disabled */
25826 +int sysaufs_brs = 1;
25827 +MODULE_PARM_DESC(brs, "use <sysfs>/fs/aufs/si_*/brN");
25828 +module_param_named(brs, sysaufs_brs, int, 0444);
25829 +
25830 +/* this module parameter has no meaning when USER_NS is disabled */
25831 +bool au_userns;
25832 +MODULE_PARM_DESC(allow_userns, "allow unprivileged to mount under userns");
25833 +module_param_named(allow_userns, au_userns, bool, 0444);
25834 +
25835 +/* ---------------------------------------------------------------------- */
25836 +
25837 +static char au_esc_chars[0x20 + 3]; /* 0x01-0x20, backslash, del, and NULL */
25838 +
25839 +int au_seq_path(struct seq_file *seq, struct path *path)
25840 +{
25841 +       int err;
25842 +
25843 +       err = seq_path(seq, path, au_esc_chars);
25844 +       if (err >= 0)
25845 +               err = 0;
25846 +       else
25847 +               err = -ENOMEM;
25848 +
25849 +       return err;
25850 +}
25851 +
25852 +/* ---------------------------------------------------------------------- */
25853 +
25854 +static int __init aufs_init(void)
25855 +{
25856 +       int err, i;
25857 +       char *p;
25858 +
25859 +       p = au_esc_chars;
25860 +       for (i = 1; i <= ' '; i++)
25861 +               *p++ = i;
25862 +       *p++ = '\\';
25863 +       *p++ = '\x7f';
25864 +       *p = 0;
25865 +
25866 +       au_dir_roflags = au_file_roflags(O_DIRECTORY | O_LARGEFILE);
25867 +
25868 +       memcpy(aufs_iop_nogetattr, aufs_iop, sizeof(aufs_iop));
25869 +       for (i = 0; i < AuIop_Last; i++)
25870 +               aufs_iop_nogetattr[i].getattr = NULL;
25871 +
25872 +       memset(au_cache, 0, sizeof(au_cache));  /* including hnotify */
25873 +
25874 +       au_sbilist_init();
25875 +       sysaufs_brs_init();
25876 +       au_debug_init();
25877 +       au_dy_init();
25878 +       err = sysaufs_init();
25879 +       if (unlikely(err))
25880 +               goto out;
25881 +       err = dbgaufs_init();
25882 +       if (unlikely(err))
25883 +               goto out_sysaufs;
25884 +       err = au_procfs_init();
25885 +       if (unlikely(err))
25886 +               goto out_dbgaufs;
25887 +       err = au_wkq_init();
25888 +       if (unlikely(err))
25889 +               goto out_procfs;
25890 +       err = au_loopback_init();
25891 +       if (unlikely(err))
25892 +               goto out_wkq;
25893 +       err = au_hnotify_init();
25894 +       if (unlikely(err))
25895 +               goto out_loopback;
25896 +       err = au_sysrq_init();
25897 +       if (unlikely(err))
25898 +               goto out_hin;
25899 +       err = au_cache_init();
25900 +       if (unlikely(err))
25901 +               goto out_sysrq;
25902 +
25903 +       aufs_fs_type.fs_flags |= au_userns ? FS_USERNS_MOUNT : 0;
25904 +       err = register_filesystem(&aufs_fs_type);
25905 +       if (unlikely(err))
25906 +               goto out_cache;
25907 +
25908 +       /* since we define pr_fmt, call printk directly */
25909 +       printk(KERN_INFO AUFS_NAME " " AUFS_VERSION "\n");
25910 +       goto out; /* success */
25911 +
25912 +out_cache:
25913 +       au_cache_fin();
25914 +out_sysrq:
25915 +       au_sysrq_fin();
25916 +out_hin:
25917 +       au_hnotify_fin();
25918 +out_loopback:
25919 +       au_loopback_fin();
25920 +out_wkq:
25921 +       au_wkq_fin();
25922 +out_procfs:
25923 +       au_procfs_fin();
25924 +out_dbgaufs:
25925 +       dbgaufs_fin();
25926 +out_sysaufs:
25927 +       sysaufs_fin();
25928 +       au_dy_fin();
25929 +out:
25930 +       return err;
25931 +}
25932 +
25933 +static void __exit aufs_exit(void)
25934 +{
25935 +       unregister_filesystem(&aufs_fs_type);
25936 +       au_cache_fin();
25937 +       au_sysrq_fin();
25938 +       au_hnotify_fin();
25939 +       au_loopback_fin();
25940 +       au_wkq_fin();
25941 +       au_procfs_fin();
25942 +       dbgaufs_fin();
25943 +       sysaufs_fin();
25944 +       au_dy_fin();
25945 +}
25946 +
25947 +module_init(aufs_init);
25948 +module_exit(aufs_exit);
25949 diff -urN /usr/share/empty/fs/aufs/module.h linux/fs/aufs/module.h
25950 --- /usr/share/empty/fs/aufs/module.h   1970-01-01 01:00:00.000000000 +0100
25951 +++ linux/fs/aufs/module.h      2022-05-23 09:44:37.729839166 +0200
25952 @@ -0,0 +1,180 @@
25953 +/* SPDX-License-Identifier: GPL-2.0 */
25954 +/*
25955 + * Copyright (C) 2005-2021 Junjiro R. Okajima
25956 + *
25957 + * This program, aufs is free software; you can redistribute it and/or modify
25958 + * it under the terms of the GNU General Public License as published by
25959 + * the Free Software Foundation; either version 2 of the License, or
25960 + * (at your option) any later version.
25961 + *
25962 + * This program is distributed in the hope that it will be useful,
25963 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
25964 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25965 + * GNU General Public License for more details.
25966 + *
25967 + * You should have received a copy of the GNU General Public License
25968 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
25969 + */
25970 +
25971 +/*
25972 + * module initialization and module-global
25973 + */
25974 +
25975 +#ifndef __AUFS_MODULE_H__
25976 +#define __AUFS_MODULE_H__
25977 +
25978 +#ifdef __KERNEL__
25979 +
25980 +#include <linux/slab.h>
25981 +#include "debug.h"
25982 +#include "dentry.h"
25983 +#include "dir.h"
25984 +#include "file.h"
25985 +#include "inode.h"
25986 +
25987 +struct path;
25988 +struct seq_file;
25989 +
25990 +/* module parameters */
25991 +extern int sysaufs_brs;
25992 +extern bool au_userns;
25993 +
25994 +/* ---------------------------------------------------------------------- */
25995 +
25996 +extern int au_dir_roflags;
25997 +
25998 +void *au_krealloc(void *p, unsigned int new_sz, gfp_t gfp, int may_shrink);
25999 +void *au_kzrealloc(void *p, unsigned int nused, unsigned int new_sz, gfp_t gfp,
26000 +                  int may_shrink);
26001 +
26002 +/*
26003 + * Comparing the size of the object with sizeof(struct rcu_head)
26004 + * case 1: object is always larger
26005 + *     --> au_kfree_rcu() or au_kfree_do_rcu()
26006 + * case 2: object is always smaller
26007 + *     --> au_kfree_small()
26008 + * case 3: object can be any size
26009 + *     --> au_kfree_try_rcu()
26010 + */
26011 +
26012 +static inline void au_kfree_do_rcu(const void *p)
26013 +{
26014 +       struct {
26015 +               struct rcu_head rcu;
26016 +       } *a = (void *)p;
26017 +
26018 +       kfree_rcu(a, rcu);
26019 +}
26020 +
26021 +#define au_kfree_rcu(_p) do {                                          \
26022 +               typeof(_p) p = (_p);                                    \
26023 +               BUILD_BUG_ON(sizeof(*p) < sizeof(struct rcu_head));     \
26024 +               if (p)                                                  \
26025 +                       au_kfree_do_rcu(p);                             \
26026 +       } while (0)
26027 +
26028 +#define au_kfree_do_sz_test(sz)        (sz >= sizeof(struct rcu_head))
26029 +#define au_kfree_sz_test(p)    (p && au_kfree_do_sz_test(ksize(p)))
26030 +
26031 +static inline void au_kfree_try_rcu(const void *p)
26032 +{
26033 +       if (!p)
26034 +               return;
26035 +       if (au_kfree_sz_test(p))
26036 +               au_kfree_do_rcu(p);
26037 +       else
26038 +               kfree(p);
26039 +}
26040 +
26041 +static inline void au_kfree_small(const void *p)
26042 +{
26043 +       if (!p)
26044 +               return;
26045 +       AuDebugOn(au_kfree_sz_test(p));
26046 +       kfree(p);
26047 +}
26048 +
26049 +static inline int au_kmidx_sub(size_t sz, size_t new_sz)
26050 +{
26051 +#ifndef CONFIG_SLOB
26052 +       return __kmalloc_index(sz, false) - __kmalloc_index(new_sz, false);
26053 +#else
26054 +       return -1; /* SLOB is untested */
26055 +#endif
26056 +}
26057 +
26058 +int au_seq_path(struct seq_file *seq, struct path *path);
26059 +
26060 +#ifdef CONFIG_PROC_FS
26061 +/* procfs.c */
26062 +int __init au_procfs_init(void);
26063 +void au_procfs_fin(void);
26064 +#else
26065 +AuStubInt0(au_procfs_init, void);
26066 +AuStubVoid(au_procfs_fin, void);
26067 +#endif
26068 +
26069 +/* ---------------------------------------------------------------------- */
26070 +
26071 +/* kmem cache */
26072 +enum {
26073 +       AuCache_DINFO,
26074 +       AuCache_ICNTNR,
26075 +       AuCache_FINFO,
26076 +       AuCache_VDIR,
26077 +       AuCache_DEHSTR,
26078 +       AuCache_HNOTIFY, /* must be last */
26079 +       AuCache_Last
26080 +};
26081 +
26082 +extern struct kmem_cache *au_cache[AuCache_Last];
26083 +
26084 +#define AuCacheFlags           (SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD)
26085 +#define AuCache(type)          KMEM_CACHE(type, AuCacheFlags)
26086 +#define AuCacheCtor(type, ctor)        \
26087 +       kmem_cache_create(#type, sizeof(struct type), \
26088 +                         __alignof__(struct type), AuCacheFlags, ctor)
26089 +
26090 +#define AuCacheFuncAlloc(name, index)                                  \
26091 +       static inline struct au_##name *au_cache_alloc_##name(void)     \
26092 +       { return kmem_cache_alloc(au_cache[AuCache_##index], GFP_NOFS); }
26093 +
26094 +#define AuCacheFuncs(name, index)                                      \
26095 +       static inline void au_cache_free_##name##_norcu(struct au_##name *p) \
26096 +       { kmem_cache_free(au_cache[AuCache_##index], p); }              \
26097 +                                                                       \
26098 +       static inline void au_cache_free_##name##_rcu_cb(struct rcu_head *rcu) \
26099 +       { void *p = rcu;                                                \
26100 +               p -= offsetof(struct au_##name, rcu);                   \
26101 +               kmem_cache_free(au_cache[AuCache_##index], p); }        \
26102 +       static inline void au_cache_free_##name##_rcu(struct au_##name *p) \
26103 +       { BUILD_BUG_ON(sizeof(struct au_##name) < sizeof(struct rcu_head)); \
26104 +               call_rcu(&p->rcu, au_cache_free_##name##_rcu_cb); }     \
26105 +                                                                       \
26106 +       static inline void au_cache_free_##name(struct au_##name *p)    \
26107 +       { /* au_cache_free_##name##_norcu(p); */                        \
26108 +               au_cache_free_##name##_rcu(p); }
26109 +
26110 +AuCacheFuncs(dinfo, DINFO);
26111 +AuCacheFuncAlloc(dinfo, DINFO);
26112 +
26113 +AuCacheFuncs(icntnr, ICNTNR);
26114 +static inline struct au_icntnr *au_cache_alloc_icntnr(struct super_block *sb)
26115 +{ return alloc_inode_sb(sb, au_cache[AuCache_ICNTNR], GFP_NOFS); }
26116 +
26117 +AuCacheFuncs(finfo, FINFO);
26118 +AuCacheFuncAlloc(finfo, FINFO);
26119 +
26120 +AuCacheFuncs(vdir, VDIR);
26121 +AuCacheFuncAlloc(vdir, VDIR);
26122 +
26123 +AuCacheFuncs(vdir_dehstr, DEHSTR);
26124 +AuCacheFuncAlloc(vdir_dehstr, DEHSTR);
26125 +
26126 +#ifdef CONFIG_AUFS_HNOTIFY
26127 +AuCacheFuncs(hnotify, HNOTIFY);
26128 +AuCacheFuncAlloc(hnotify, HNOTIFY);
26129 +#endif
26130 +
26131 +#endif /* __KERNEL__ */
26132 +#endif /* __AUFS_MODULE_H__ */
26133 diff -urN /usr/share/empty/fs/aufs/mvdown.c linux/fs/aufs/mvdown.c
26134 --- /usr/share/empty/fs/aufs/mvdown.c   1970-01-01 01:00:00.000000000 +0100
26135 +++ linux/fs/aufs/mvdown.c      2022-03-21 14:49:05.729966343 +0100
26136 @@ -0,0 +1,706 @@
26137 +// SPDX-License-Identifier: GPL-2.0
26138 +/*
26139 + * Copyright (C) 2011-2021 Junjiro R. Okajima
26140 + *
26141 + * This program, aufs is free software; you can redistribute it and/or modify
26142 + * it under the terms of the GNU General Public License as published by
26143 + * the Free Software Foundation; either version 2 of the License, or
26144 + * (at your option) any later version.
26145 + *
26146 + * This program is distributed in the hope that it will be useful,
26147 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
26148 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26149 + * GNU General Public License for more details.
26150 + *
26151 + * You should have received a copy of the GNU General Public License
26152 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
26153 + */
26154 +
26155 +/*
26156 + * move-down, opposite of copy-up
26157 + */
26158 +
26159 +#include "aufs.h"
26160 +
26161 +struct au_mvd_args {
26162 +       struct {
26163 +               struct super_block *h_sb;
26164 +               struct dentry *h_parent;
26165 +               struct au_hinode *hdir;
26166 +               struct inode *h_dir, *h_inode;
26167 +               struct au_pin pin;
26168 +       } info[AUFS_MVDOWN_NARRAY];
26169 +
26170 +       struct aufs_mvdown mvdown;
26171 +       struct dentry *dentry, *parent;
26172 +       struct inode *inode, *dir;
26173 +       struct super_block *sb;
26174 +       aufs_bindex_t bopq, bwh, bfound;
26175 +       unsigned char rename_lock;
26176 +};
26177 +
26178 +#define mvd_errno              mvdown.au_errno
26179 +#define mvd_bsrc               mvdown.stbr[AUFS_MVDOWN_UPPER].bindex
26180 +#define mvd_src_brid           mvdown.stbr[AUFS_MVDOWN_UPPER].brid
26181 +#define mvd_bdst               mvdown.stbr[AUFS_MVDOWN_LOWER].bindex
26182 +#define mvd_dst_brid           mvdown.stbr[AUFS_MVDOWN_LOWER].brid
26183 +
26184 +#define mvd_h_src_sb           info[AUFS_MVDOWN_UPPER].h_sb
26185 +#define mvd_h_src_parent       info[AUFS_MVDOWN_UPPER].h_parent
26186 +#define mvd_hdir_src           info[AUFS_MVDOWN_UPPER].hdir
26187 +#define mvd_h_src_dir          info[AUFS_MVDOWN_UPPER].h_dir
26188 +#define mvd_h_src_inode                info[AUFS_MVDOWN_UPPER].h_inode
26189 +#define mvd_pin_src            info[AUFS_MVDOWN_UPPER].pin
26190 +
26191 +#define mvd_h_dst_sb           info[AUFS_MVDOWN_LOWER].h_sb
26192 +#define mvd_h_dst_parent       info[AUFS_MVDOWN_LOWER].h_parent
26193 +#define mvd_hdir_dst           info[AUFS_MVDOWN_LOWER].hdir
26194 +#define mvd_h_dst_dir          info[AUFS_MVDOWN_LOWER].h_dir
26195 +#define mvd_h_dst_inode                info[AUFS_MVDOWN_LOWER].h_inode
26196 +#define mvd_pin_dst            info[AUFS_MVDOWN_LOWER].pin
26197 +
26198 +#define AU_MVD_PR(flag, ...) do {                      \
26199 +               if (flag)                               \
26200 +                       pr_err(__VA_ARGS__);            \
26201 +       } while (0)
26202 +
26203 +static int find_lower_writable(struct au_mvd_args *a)
26204 +{
26205 +       struct super_block *sb;
26206 +       aufs_bindex_t bindex, bbot;
26207 +       struct au_branch *br;
26208 +
26209 +       sb = a->sb;
26210 +       bindex = a->mvd_bsrc;
26211 +       bbot = au_sbbot(sb);
26212 +       if (a->mvdown.flags & AUFS_MVDOWN_FHSM_LOWER)
26213 +               for (bindex++; bindex <= bbot; bindex++) {
26214 +                       br = au_sbr(sb, bindex);
26215 +                       if (au_br_fhsm(br->br_perm)
26216 +                           && !sb_rdonly(au_br_sb(br)))
26217 +                               return bindex;
26218 +               }
26219 +       else if (!(a->mvdown.flags & AUFS_MVDOWN_ROLOWER))
26220 +               for (bindex++; bindex <= bbot; bindex++) {
26221 +                       br = au_sbr(sb, bindex);
26222 +                       if (!au_br_rdonly(br))
26223 +                               return bindex;
26224 +               }
26225 +       else
26226 +               for (bindex++; bindex <= bbot; bindex++) {
26227 +                       br = au_sbr(sb, bindex);
26228 +                       if (!sb_rdonly(au_br_sb(br))) {
26229 +                               if (au_br_rdonly(br))
26230 +                                       a->mvdown.flags
26231 +                                               |= AUFS_MVDOWN_ROLOWER_R;
26232 +                               return bindex;
26233 +                       }
26234 +               }
26235 +
26236 +       return -1;
26237 +}
26238 +
26239 +/* make the parent dir on bdst */
26240 +static int au_do_mkdir(const unsigned char dmsg, struct au_mvd_args *a)
26241 +{
26242 +       int err;
26243 +
26244 +       err = 0;
26245 +       a->mvd_hdir_src = au_hi(a->dir, a->mvd_bsrc);
26246 +       a->mvd_hdir_dst = au_hi(a->dir, a->mvd_bdst);
26247 +       a->mvd_h_src_parent = au_h_dptr(a->parent, a->mvd_bsrc);
26248 +       a->mvd_h_dst_parent = NULL;
26249 +       if (au_dbbot(a->parent) >= a->mvd_bdst)
26250 +               a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
26251 +       if (!a->mvd_h_dst_parent) {
26252 +               err = au_cpdown_dirs(a->dentry, a->mvd_bdst);
26253 +               if (unlikely(err)) {
26254 +                       AU_MVD_PR(dmsg, "cpdown_dirs failed\n");
26255 +                       goto out;
26256 +               }
26257 +               a->mvd_h_dst_parent = au_h_dptr(a->parent, a->mvd_bdst);
26258 +       }
26259 +
26260 +out:
26261 +       AuTraceErr(err);
26262 +       return err;
26263 +}
26264 +
26265 +/* lock them all */
26266 +static int au_do_lock(const unsigned char dmsg, struct au_mvd_args *a)
26267 +{
26268 +       int err;
26269 +       struct dentry *h_trap;
26270 +
26271 +       a->mvd_h_src_sb = au_sbr_sb(a->sb, a->mvd_bsrc);
26272 +       a->mvd_h_dst_sb = au_sbr_sb(a->sb, a->mvd_bdst);
26273 +       err = au_pin(&a->mvd_pin_dst, a->dentry, a->mvd_bdst,
26274 +                    au_opt_udba(a->sb),
26275 +                    AuPin_MNT_WRITE | AuPin_DI_LOCKED);
26276 +       AuTraceErr(err);
26277 +       if (unlikely(err)) {
26278 +               AU_MVD_PR(dmsg, "pin_dst failed\n");
26279 +               goto out;
26280 +       }
26281 +
26282 +       if (a->mvd_h_src_sb != a->mvd_h_dst_sb) {
26283 +               a->rename_lock = 0;
26284 +               au_pin_init(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
26285 +                           AuLsc_DI_PARENT, AuLsc_I_PARENT3,
26286 +                           au_opt_udba(a->sb),
26287 +                           AuPin_MNT_WRITE | AuPin_DI_LOCKED);
26288 +               err = au_do_pin(&a->mvd_pin_src);
26289 +               AuTraceErr(err);
26290 +               a->mvd_h_src_dir = d_inode(a->mvd_h_src_parent);
26291 +               if (unlikely(err)) {
26292 +                       AU_MVD_PR(dmsg, "pin_src failed\n");
26293 +                       goto out_dst;
26294 +               }
26295 +               goto out; /* success */
26296 +       }
26297 +
26298 +       a->rename_lock = 1;
26299 +       au_pin_hdir_unlock(&a->mvd_pin_dst);
26300 +       err = au_pin(&a->mvd_pin_src, a->dentry, a->mvd_bsrc,
26301 +                    au_opt_udba(a->sb),
26302 +                    AuPin_MNT_WRITE | AuPin_DI_LOCKED);
26303 +       AuTraceErr(err);
26304 +       a->mvd_h_src_dir = d_inode(a->mvd_h_src_parent);
26305 +       if (unlikely(err)) {
26306 +               AU_MVD_PR(dmsg, "pin_src failed\n");
26307 +               au_pin_hdir_lock(&a->mvd_pin_dst);
26308 +               goto out_dst;
26309 +       }
26310 +       au_pin_hdir_unlock(&a->mvd_pin_src);
26311 +       h_trap = vfsub_lock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
26312 +                                  a->mvd_h_dst_parent, a->mvd_hdir_dst);
26313 +       if (h_trap) {
26314 +               err = (h_trap != a->mvd_h_src_parent);
26315 +               if (err)
26316 +                       err = (h_trap != a->mvd_h_dst_parent);
26317 +       }
26318 +       BUG_ON(err); /* it should never happen */
26319 +       if (unlikely(a->mvd_h_src_dir != au_pinned_h_dir(&a->mvd_pin_src))) {
26320 +               err = -EBUSY;
26321 +               AuTraceErr(err);
26322 +               vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
26323 +                                   a->mvd_h_dst_parent, a->mvd_hdir_dst);
26324 +               au_pin_hdir_lock(&a->mvd_pin_src);
26325 +               au_unpin(&a->mvd_pin_src);
26326 +               au_pin_hdir_lock(&a->mvd_pin_dst);
26327 +               goto out_dst;
26328 +       }
26329 +       goto out; /* success */
26330 +
26331 +out_dst:
26332 +       au_unpin(&a->mvd_pin_dst);
26333 +out:
26334 +       AuTraceErr(err);
26335 +       return err;
26336 +}
26337 +
26338 +static void au_do_unlock(const unsigned char dmsg, struct au_mvd_args *a)
26339 +{
26340 +       if (!a->rename_lock)
26341 +               au_unpin(&a->mvd_pin_src);
26342 +       else {
26343 +               vfsub_unlock_rename(a->mvd_h_src_parent, a->mvd_hdir_src,
26344 +                                   a->mvd_h_dst_parent, a->mvd_hdir_dst);
26345 +               au_pin_hdir_lock(&a->mvd_pin_src);
26346 +               au_unpin(&a->mvd_pin_src);
26347 +               au_pin_hdir_lock(&a->mvd_pin_dst);
26348 +       }
26349 +       au_unpin(&a->mvd_pin_dst);
26350 +}
26351 +
26352 +/* copy-down the file */
26353 +static int au_do_cpdown(const unsigned char dmsg, struct au_mvd_args *a)
26354 +{
26355 +       int err;
26356 +       struct au_cp_generic cpg = {
26357 +               .dentry = a->dentry,
26358 +               .bdst   = a->mvd_bdst,
26359 +               .bsrc   = a->mvd_bsrc,
26360 +               .len    = -1,
26361 +               .pin    = &a->mvd_pin_dst,
26362 +               .flags  = AuCpup_DTIME | AuCpup_HOPEN
26363 +       };
26364 +
26365 +       AuDbg("b%d, b%d\n", cpg.bsrc, cpg.bdst);
26366 +       if (a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
26367 +               au_fset_cpup(cpg.flags, OVERWRITE);
26368 +       if (a->mvdown.flags & AUFS_MVDOWN_ROLOWER)
26369 +               au_fset_cpup(cpg.flags, RWDST);
26370 +       err = au_sio_cpdown_simple(&cpg);
26371 +       if (unlikely(err))
26372 +               AU_MVD_PR(dmsg, "cpdown failed\n");
26373 +
26374 +       AuTraceErr(err);
26375 +       return err;
26376 +}
26377 +
26378 +/*
26379 + * unlink the whiteout on bdst if exist which may be created by UDBA while we
26380 + * were sleeping
26381 + */
26382 +static int au_do_unlink_wh(const unsigned char dmsg, struct au_mvd_args *a)
26383 +{
26384 +       int err;
26385 +       struct path h_path;
26386 +       struct au_branch *br;
26387 +       struct inode *delegated;
26388 +
26389 +       br = au_sbr(a->sb, a->mvd_bdst);
26390 +       h_path.dentry = au_wh_lkup(a->mvd_h_dst_parent, &a->dentry->d_name, br);
26391 +       err = PTR_ERR(h_path.dentry);
26392 +       if (IS_ERR(h_path.dentry)) {
26393 +               AU_MVD_PR(dmsg, "wh_lkup failed\n");
26394 +               goto out;
26395 +       }
26396 +
26397 +       err = 0;
26398 +       if (d_is_positive(h_path.dentry)) {
26399 +               h_path.mnt = au_br_mnt(br);
26400 +               delegated = NULL;
26401 +               err = vfsub_unlink(d_inode(a->mvd_h_dst_parent), &h_path,
26402 +                                  &delegated, /*force*/0);
26403 +               if (unlikely(err == -EWOULDBLOCK)) {
26404 +                       pr_warn("cannot retry for NFSv4 delegation"
26405 +                               " for an internal unlink\n");
26406 +                       iput(delegated);
26407 +               }
26408 +               if (unlikely(err))
26409 +                       AU_MVD_PR(dmsg, "wh_unlink failed\n");
26410 +       }
26411 +       dput(h_path.dentry);
26412 +
26413 +out:
26414 +       AuTraceErr(err);
26415 +       return err;
26416 +}
26417 +
26418 +/*
26419 + * unlink the topmost h_dentry
26420 + */
26421 +static int au_do_unlink(const unsigned char dmsg, struct au_mvd_args *a)
26422 +{
26423 +       int err;
26424 +       struct path h_path;
26425 +       struct inode *delegated;
26426 +
26427 +       h_path.mnt = au_sbr_mnt(a->sb, a->mvd_bsrc);
26428 +       h_path.dentry = au_h_dptr(a->dentry, a->mvd_bsrc);
26429 +       delegated = NULL;
26430 +       err = vfsub_unlink(a->mvd_h_src_dir, &h_path, &delegated, /*force*/0);
26431 +       if (unlikely(err == -EWOULDBLOCK)) {
26432 +               pr_warn("cannot retry for NFSv4 delegation"
26433 +                       " for an internal unlink\n");
26434 +               iput(delegated);
26435 +       }
26436 +       if (unlikely(err))
26437 +               AU_MVD_PR(dmsg, "unlink failed\n");
26438 +
26439 +       AuTraceErr(err);
26440 +       return err;
26441 +}
26442 +
26443 +/* Since mvdown succeeded, we ignore an error of this function */
26444 +static void au_do_stfs(const unsigned char dmsg, struct au_mvd_args *a)
26445 +{
26446 +       int err;
26447 +       struct au_branch *br;
26448 +
26449 +       a->mvdown.flags |= AUFS_MVDOWN_STFS_FAILED;
26450 +       br = au_sbr(a->sb, a->mvd_bsrc);
26451 +       err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_UPPER].stfs);
26452 +       if (!err) {
26453 +               br = au_sbr(a->sb, a->mvd_bdst);
26454 +               a->mvdown.stbr[AUFS_MVDOWN_LOWER].brid = br->br_id;
26455 +               err = au_br_stfs(br, &a->mvdown.stbr[AUFS_MVDOWN_LOWER].stfs);
26456 +       }
26457 +       if (!err)
26458 +               a->mvdown.flags &= ~AUFS_MVDOWN_STFS_FAILED;
26459 +       else
26460 +               AU_MVD_PR(dmsg, "statfs failed (%d), ignored\n", err);
26461 +}
26462 +
26463 +/*
26464 + * copy-down the file and unlink the bsrc file.
26465 + * - unlink the bdst whout if exist
26466 + * - copy-down the file (with whtmp name and rename)
26467 + * - unlink the bsrc file
26468 + */
26469 +static int au_do_mvdown(const unsigned char dmsg, struct au_mvd_args *a)
26470 +{
26471 +       int err;
26472 +
26473 +       err = au_do_mkdir(dmsg, a);
26474 +       if (!err)
26475 +               err = au_do_lock(dmsg, a);
26476 +       if (unlikely(err))
26477 +               goto out;
26478 +
26479 +       /*
26480 +        * do not revert the activities we made on bdst since they should be
26481 +        * harmless in aufs.
26482 +        */
26483 +
26484 +       err = au_do_cpdown(dmsg, a);
26485 +       if (!err)
26486 +               err = au_do_unlink_wh(dmsg, a);
26487 +       if (!err && !(a->mvdown.flags & AUFS_MVDOWN_KUPPER))
26488 +               err = au_do_unlink(dmsg, a);
26489 +       if (unlikely(err))
26490 +               goto out_unlock;
26491 +
26492 +       AuDbg("%pd2, 0x%x, %d --> %d\n",
26493 +             a->dentry, a->mvdown.flags, a->mvd_bsrc, a->mvd_bdst);
26494 +       if (find_lower_writable(a) < 0)
26495 +               a->mvdown.flags |= AUFS_MVDOWN_BOTTOM;
26496 +
26497 +       if (a->mvdown.flags & AUFS_MVDOWN_STFS)
26498 +               au_do_stfs(dmsg, a);
26499 +
26500 +       /* maintain internal array */
26501 +       if (!(a->mvdown.flags & AUFS_MVDOWN_KUPPER)) {
26502 +               au_set_h_dptr(a->dentry, a->mvd_bsrc, NULL);
26503 +               au_set_dbtop(a->dentry, a->mvd_bdst);
26504 +               au_set_h_iptr(a->inode, a->mvd_bsrc, NULL, /*flags*/0);
26505 +               au_set_ibtop(a->inode, a->mvd_bdst);
26506 +       } else {
26507 +               /* hide the lower */
26508 +               au_set_h_dptr(a->dentry, a->mvd_bdst, NULL);
26509 +               au_set_dbbot(a->dentry, a->mvd_bsrc);
26510 +               au_set_h_iptr(a->inode, a->mvd_bdst, NULL, /*flags*/0);
26511 +               au_set_ibbot(a->inode, a->mvd_bsrc);
26512 +       }
26513 +       if (au_dbbot(a->dentry) < a->mvd_bdst)
26514 +               au_set_dbbot(a->dentry, a->mvd_bdst);
26515 +       if (au_ibbot(a->inode) < a->mvd_bdst)
26516 +               au_set_ibbot(a->inode, a->mvd_bdst);
26517 +
26518 +out_unlock:
26519 +       au_do_unlock(dmsg, a);
26520 +out:
26521 +       AuTraceErr(err);
26522 +       return err;
26523 +}
26524 +
26525 +/* ---------------------------------------------------------------------- */
26526 +
26527 +/* make sure the file is idle */
26528 +static int au_mvd_args_busy(const unsigned char dmsg, struct au_mvd_args *a)
26529 +{
26530 +       int err, plinked;
26531 +
26532 +       err = 0;
26533 +       plinked = !!au_opt_test(au_mntflags(a->sb), PLINK);
26534 +       if (au_dbtop(a->dentry) == a->mvd_bsrc
26535 +           && au_dcount(a->dentry) == 1
26536 +           && atomic_read(&a->inode->i_count) == 1
26537 +           /* && a->mvd_h_src_inode->i_nlink == 1 */
26538 +           && (!plinked || !au_plink_test(a->inode))
26539 +           && a->inode->i_nlink == 1)
26540 +               goto out;
26541 +
26542 +       err = -EBUSY;
26543 +       AU_MVD_PR(dmsg,
26544 +                 "b%d, d{b%d, c%d?}, i{c%d?, l%u}, hi{l%u}, p{%d, %d}\n",
26545 +                 a->mvd_bsrc, au_dbtop(a->dentry), au_dcount(a->dentry),
26546 +                 atomic_read(&a->inode->i_count), a->inode->i_nlink,
26547 +                 a->mvd_h_src_inode->i_nlink,
26548 +                 plinked, plinked ? au_plink_test(a->inode) : 0);
26549 +
26550 +out:
26551 +       AuTraceErr(err);
26552 +       return err;
26553 +}
26554 +
26555 +/* make sure the parent dir is fine */
26556 +static int au_mvd_args_parent(const unsigned char dmsg,
26557 +                             struct au_mvd_args *a)
26558 +{
26559 +       int err;
26560 +       aufs_bindex_t bindex;
26561 +
26562 +       err = 0;
26563 +       if (unlikely(au_alive_dir(a->parent))) {
26564 +               err = -ENOENT;
26565 +               AU_MVD_PR(dmsg, "parent dir is dead\n");
26566 +               goto out;
26567 +       }
26568 +
26569 +       a->bopq = au_dbdiropq(a->parent);
26570 +       bindex = au_wbr_nonopq(a->dentry, a->mvd_bdst);
26571 +       AuDbg("b%d\n", bindex);
26572 +       if (unlikely((bindex >= 0 && bindex < a->mvd_bdst)
26573 +                    || (a->bopq != -1 && a->bopq < a->mvd_bdst))) {
26574 +               err = -EINVAL;
26575 +               a->mvd_errno = EAU_MVDOWN_OPAQUE;
26576 +               AU_MVD_PR(dmsg, "ancestor is opaque b%d, b%d\n",
26577 +                         a->bopq, a->mvd_bdst);
26578 +       }
26579 +
26580 +out:
26581 +       AuTraceErr(err);
26582 +       return err;
26583 +}
26584 +
26585 +static int au_mvd_args_intermediate(const unsigned char dmsg,
26586 +                                   struct au_mvd_args *a)
26587 +{
26588 +       int err;
26589 +       struct au_dinfo *dinfo, *tmp;
26590 +
26591 +       /* lookup the next lower positive entry */
26592 +       err = -ENOMEM;
26593 +       tmp = au_di_alloc(a->sb, AuLsc_DI_TMP);
26594 +       if (unlikely(!tmp))
26595 +               goto out;
26596 +
26597 +       a->bfound = -1;
26598 +       a->bwh = -1;
26599 +       dinfo = au_di(a->dentry);
26600 +       au_di_cp(tmp, dinfo);
26601 +       au_di_swap(tmp, dinfo);
26602 +
26603 +       /* returns the number of positive dentries */
26604 +       err = au_lkup_dentry(a->dentry, a->mvd_bsrc + 1,
26605 +                            /* AuLkup_IGNORE_PERM */ 0);
26606 +       if (!err)
26607 +               a->bwh = au_dbwh(a->dentry);
26608 +       else if (err > 0)
26609 +               a->bfound = au_dbtop(a->dentry);
26610 +
26611 +       au_di_swap(tmp, dinfo);
26612 +       au_rw_write_unlock(&tmp->di_rwsem);
26613 +       au_di_free(tmp);
26614 +       if (unlikely(err < 0))
26615 +               AU_MVD_PR(dmsg, "failed look-up lower\n");
26616 +
26617 +       /*
26618 +        * here, we have these cases.
26619 +        * bfound == -1
26620 +        *      no positive dentry under bsrc. there are more sub-cases.
26621 +        *      bwh < 0
26622 +        *              there no whiteout, we can safely move-down.
26623 +        *      bwh <= bsrc
26624 +        *              impossible
26625 +        *      bsrc < bwh && bwh < bdst
26626 +        *              there is a whiteout on RO branch. cannot proceed.
26627 +        *      bwh == bdst
26628 +        *              there is a whiteout on the RW target branch. it should
26629 +        *              be removed.
26630 +        *      bdst < bwh
26631 +        *              there is a whiteout somewhere unrelated branch.
26632 +        * -1 < bfound && bfound <= bsrc
26633 +        *      impossible.
26634 +        * bfound < bdst
26635 +        *      found, but it is on RO branch between bsrc and bdst. cannot
26636 +        *      proceed.
26637 +        * bfound == bdst
26638 +        *      found, replace it if AUFS_MVDOWN_FORCE is set. otherwise return
26639 +        *      error.
26640 +        * bdst < bfound
26641 +        *      found, after we create the file on bdst, it will be hidden.
26642 +        */
26643 +
26644 +       AuDebugOn(a->bfound == -1
26645 +                 && a->bwh != -1
26646 +                 && a->bwh <= a->mvd_bsrc);
26647 +       AuDebugOn(-1 < a->bfound
26648 +                 && a->bfound <= a->mvd_bsrc);
26649 +
26650 +       err = -EINVAL;
26651 +       if (a->bfound == -1
26652 +           && a->mvd_bsrc < a->bwh
26653 +           && a->bwh != -1
26654 +           && a->bwh < a->mvd_bdst) {
26655 +               a->mvd_errno = EAU_MVDOWN_WHITEOUT;
26656 +               AU_MVD_PR(dmsg, "bsrc %d, bdst %d, bfound %d, bwh %d\n",
26657 +                         a->mvd_bsrc, a->mvd_bdst, a->bfound, a->bwh);
26658 +               goto out;
26659 +       } else if (a->bfound != -1 && a->bfound < a->mvd_bdst) {
26660 +               a->mvd_errno = EAU_MVDOWN_UPPER;
26661 +               AU_MVD_PR(dmsg, "bdst %d, bfound %d\n",
26662 +                         a->mvd_bdst, a->bfound);
26663 +               goto out;
26664 +       }
26665 +
26666 +       err = 0; /* success */
26667 +
26668 +out:
26669 +       AuTraceErr(err);
26670 +       return err;
26671 +}
26672 +
26673 +static int au_mvd_args_exist(const unsigned char dmsg, struct au_mvd_args *a)
26674 +{
26675 +       int err;
26676 +
26677 +       err = 0;
26678 +       if (!(a->mvdown.flags & AUFS_MVDOWN_OWLOWER)
26679 +           && a->bfound == a->mvd_bdst)
26680 +               err = -EEXIST;
26681 +       AuTraceErr(err);
26682 +       return err;
26683 +}
26684 +
26685 +static int au_mvd_args(const unsigned char dmsg, struct au_mvd_args *a)
26686 +{
26687 +       int err;
26688 +       struct au_branch *br;
26689 +
26690 +       err = -EISDIR;
26691 +       if (unlikely(S_ISDIR(a->inode->i_mode)))
26692 +               goto out;
26693 +
26694 +       err = -EINVAL;
26695 +       if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_UPPER))
26696 +               a->mvd_bsrc = au_ibtop(a->inode);
26697 +       else {
26698 +               a->mvd_bsrc = au_br_index(a->sb, a->mvd_src_brid);
26699 +               if (unlikely(a->mvd_bsrc < 0
26700 +                            || (a->mvd_bsrc < au_dbtop(a->dentry)
26701 +                                || au_dbbot(a->dentry) < a->mvd_bsrc
26702 +                                || !au_h_dptr(a->dentry, a->mvd_bsrc))
26703 +                            || (a->mvd_bsrc < au_ibtop(a->inode)
26704 +                                || au_ibbot(a->inode) < a->mvd_bsrc
26705 +                                || !au_h_iptr(a->inode, a->mvd_bsrc)))) {
26706 +                       a->mvd_errno = EAU_MVDOWN_NOUPPER;
26707 +                       AU_MVD_PR(dmsg, "no upper\n");
26708 +                       goto out;
26709 +               }
26710 +       }
26711 +       if (unlikely(a->mvd_bsrc == au_sbbot(a->sb))) {
26712 +               a->mvd_errno = EAU_MVDOWN_BOTTOM;
26713 +               AU_MVD_PR(dmsg, "on the bottom\n");
26714 +               goto out;
26715 +       }
26716 +       a->mvd_h_src_inode = au_h_iptr(a->inode, a->mvd_bsrc);
26717 +       br = au_sbr(a->sb, a->mvd_bsrc);
26718 +       err = au_br_rdonly(br);
26719 +       if (!(a->mvdown.flags & AUFS_MVDOWN_ROUPPER)) {
26720 +               if (unlikely(err))
26721 +                       goto out;
26722 +       } else if (!(vfsub_native_ro(a->mvd_h_src_inode)
26723 +                    || IS_APPEND(a->mvd_h_src_inode))) {
26724 +               if (err)
26725 +                       a->mvdown.flags |= AUFS_MVDOWN_ROUPPER_R;
26726 +               /* go on */
26727 +       } else
26728 +               goto out;
26729 +
26730 +       err = -EINVAL;
26731 +       if (!(a->mvdown.flags & AUFS_MVDOWN_BRID_LOWER)) {
26732 +               a->mvd_bdst = find_lower_writable(a);
26733 +               if (unlikely(a->mvd_bdst < 0)) {
26734 +                       a->mvd_errno = EAU_MVDOWN_BOTTOM;
26735 +                       AU_MVD_PR(dmsg, "no writable lower branch\n");
26736 +                       goto out;
26737 +               }
26738 +       } else {
26739 +               a->mvd_bdst = au_br_index(a->sb, a->mvd_dst_brid);
26740 +               if (unlikely(a->mvd_bdst < 0
26741 +                            || au_sbbot(a->sb) < a->mvd_bdst)) {
26742 +                       a->mvd_errno = EAU_MVDOWN_NOLOWERBR;
26743 +                       AU_MVD_PR(dmsg, "no lower brid\n");
26744 +                       goto out;
26745 +               }
26746 +       }
26747 +
26748 +       err = au_mvd_args_busy(dmsg, a);
26749 +       if (!err)
26750 +               err = au_mvd_args_parent(dmsg, a);
26751 +       if (!err)
26752 +               err = au_mvd_args_intermediate(dmsg, a);
26753 +       if (!err)
26754 +               err = au_mvd_args_exist(dmsg, a);
26755 +       if (!err)
26756 +               AuDbg("b%d, b%d\n", a->mvd_bsrc, a->mvd_bdst);
26757 +
26758 +out:
26759 +       AuTraceErr(err);
26760 +       return err;
26761 +}
26762 +
26763 +int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *uarg)
26764 +{
26765 +       int err, e;
26766 +       unsigned char dmsg;
26767 +       struct au_mvd_args *args;
26768 +       struct inode *inode;
26769 +
26770 +       inode = d_inode(dentry);
26771 +       err = -EPERM;
26772 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
26773 +               goto out;
26774 +
26775 +       err = -ENOMEM;
26776 +       args = kmalloc(sizeof(*args), GFP_NOFS);
26777 +       if (unlikely(!args))
26778 +               goto out;
26779 +
26780 +       err = copy_from_user(&args->mvdown, uarg, sizeof(args->mvdown));
26781 +       if (!err)
26782 +               /* VERIFY_WRITE */
26783 +               err = !access_ok(uarg, sizeof(*uarg));
26784 +       if (unlikely(err)) {
26785 +               err = -EFAULT;
26786 +               AuTraceErr(err);
26787 +               goto out_free;
26788 +       }
26789 +       AuDbg("flags 0x%x\n", args->mvdown.flags);
26790 +       args->mvdown.flags &= ~(AUFS_MVDOWN_ROLOWER_R | AUFS_MVDOWN_ROUPPER_R);
26791 +       args->mvdown.au_errno = 0;
26792 +       args->dentry = dentry;
26793 +       args->inode = inode;
26794 +       args->sb = dentry->d_sb;
26795 +
26796 +       err = -ENOENT;
26797 +       dmsg = !!(args->mvdown.flags & AUFS_MVDOWN_DMSG);
26798 +       args->parent = dget_parent(dentry);
26799 +       args->dir = d_inode(args->parent);
26800 +       inode_lock_nested(args->dir, I_MUTEX_PARENT);
26801 +       dput(args->parent);
26802 +       if (unlikely(args->parent != dentry->d_parent)) {
26803 +               AU_MVD_PR(dmsg, "parent dir is moved\n");
26804 +               goto out_dir;
26805 +       }
26806 +
26807 +       inode_lock_nested(inode, I_MUTEX_CHILD);
26808 +       err = aufs_read_lock(dentry, AuLock_DW | AuLock_FLUSH | AuLock_NOPLMW);
26809 +       if (unlikely(err))
26810 +               goto out_inode;
26811 +
26812 +       di_write_lock_parent(args->parent);
26813 +       err = au_mvd_args(dmsg, args);
26814 +       if (unlikely(err))
26815 +               goto out_parent;
26816 +
26817 +       err = au_do_mvdown(dmsg, args);
26818 +       if (unlikely(err))
26819 +               goto out_parent;
26820 +
26821 +       au_cpup_attr_timesizes(args->dir);
26822 +       au_cpup_attr_timesizes(inode);
26823 +       if (!(args->mvdown.flags & AUFS_MVDOWN_KUPPER))
26824 +               au_cpup_igen(inode, au_h_iptr(inode, args->mvd_bdst));
26825 +       /* au_digen_dec(dentry); */
26826 +
26827 +out_parent:
26828 +       di_write_unlock(args->parent);
26829 +       aufs_read_unlock(dentry, AuLock_DW);
26830 +out_inode:
26831 +       inode_unlock(inode);
26832 +out_dir:
26833 +       inode_unlock(args->dir);
26834 +out_free:
26835 +       e = copy_to_user(uarg, &args->mvdown, sizeof(args->mvdown));
26836 +       if (unlikely(e))
26837 +               err = -EFAULT;
26838 +       au_kfree_rcu(args);
26839 +out:
26840 +       AuTraceErr(err);
26841 +       return err;
26842 +}
26843 diff -urN /usr/share/empty/fs/aufs/opts.c linux/fs/aufs/opts.c
26844 --- /usr/share/empty/fs/aufs/opts.c     1970-01-01 01:00:00.000000000 +0100
26845 +++ linux/fs/aufs/opts.c        2022-03-21 14:49:05.729966343 +0100
26846 @@ -0,0 +1,1032 @@
26847 +// SPDX-License-Identifier: GPL-2.0
26848 +/*
26849 + * Copyright (C) 2005-2021 Junjiro R. Okajima
26850 + *
26851 + * This program, aufs is free software; you can redistribute it and/or modify
26852 + * it under the terms of the GNU General Public License as published by
26853 + * the Free Software Foundation; either version 2 of the License, or
26854 + * (at your option) any later version.
26855 + *
26856 + * This program is distributed in the hope that it will be useful,
26857 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
26858 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26859 + * GNU General Public License for more details.
26860 + *
26861 + * You should have received a copy of the GNU General Public License
26862 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
26863 + */
26864 +
26865 +/*
26866 + * mount options/flags
26867 + */
26868 +
26869 +#include <linux/types.h> /* a distribution requires */
26870 +#include <linux/parser.h>
26871 +#include "aufs.h"
26872 +
26873 +/* ---------------------------------------------------------------------- */
26874 +
26875 +static const char *au_parser_pattern(int val, match_table_t tbl)
26876 +{
26877 +       struct match_token *p;
26878 +
26879 +       p = tbl;
26880 +       while (p->pattern) {
26881 +               if (p->token == val)
26882 +                       return p->pattern;
26883 +               p++;
26884 +       }
26885 +       BUG();
26886 +       return "??";
26887 +}
26888 +
26889 +static const char *au_optstr(int *val, match_table_t tbl)
26890 +{
26891 +       struct match_token *p;
26892 +       int v;
26893 +
26894 +       v = *val;
26895 +       if (!v)
26896 +               goto out;
26897 +       p = tbl;
26898 +       while (p->pattern) {
26899 +               if (p->token
26900 +                   && (v & p->token) == p->token) {
26901 +                       *val &= ~p->token;
26902 +                       return p->pattern;
26903 +               }
26904 +               p++;
26905 +       }
26906 +
26907 +out:
26908 +       return NULL;
26909 +}
26910 +
26911 +/* ---------------------------------------------------------------------- */
26912 +
26913 +static match_table_t brperm = {
26914 +       {AuBrPerm_RO, AUFS_BRPERM_RO},
26915 +       {AuBrPerm_RR, AUFS_BRPERM_RR},
26916 +       {AuBrPerm_RW, AUFS_BRPERM_RW},
26917 +       {0, NULL}
26918 +};
26919 +
26920 +static match_table_t brattr = {
26921 +       /* general */
26922 +       {AuBrAttr_COO_REG, AUFS_BRATTR_COO_REG},
26923 +       {AuBrAttr_COO_ALL, AUFS_BRATTR_COO_ALL},
26924 +       /* 'unpin' attrib is meaningless since linux-3.18-rc1 */
26925 +       {AuBrAttr_UNPIN, AUFS_BRATTR_UNPIN},
26926 +#ifdef CONFIG_AUFS_FHSM
26927 +       {AuBrAttr_FHSM, AUFS_BRATTR_FHSM},
26928 +#endif
26929 +#ifdef CONFIG_AUFS_XATTR
26930 +       {AuBrAttr_ICEX, AUFS_BRATTR_ICEX},
26931 +       {AuBrAttr_ICEX_SEC, AUFS_BRATTR_ICEX_SEC},
26932 +       {AuBrAttr_ICEX_SYS, AUFS_BRATTR_ICEX_SYS},
26933 +       {AuBrAttr_ICEX_TR, AUFS_BRATTR_ICEX_TR},
26934 +       {AuBrAttr_ICEX_USR, AUFS_BRATTR_ICEX_USR},
26935 +       {AuBrAttr_ICEX_OTH, AUFS_BRATTR_ICEX_OTH},
26936 +#endif
26937 +
26938 +       /* ro/rr branch */
26939 +       {AuBrRAttr_WH, AUFS_BRRATTR_WH},
26940 +
26941 +       /* rw branch */
26942 +       {AuBrWAttr_MOO, AUFS_BRWATTR_MOO},
26943 +       {AuBrWAttr_NoLinkWH, AUFS_BRWATTR_NLWH},
26944 +
26945 +       {0, NULL}
26946 +};
26947 +
26948 +static int br_attr_val(char *str, match_table_t table, substring_t args[])
26949 +{
26950 +       int attr, v;
26951 +       char *p;
26952 +
26953 +       attr = 0;
26954 +       do {
26955 +               p = strchr(str, '+');
26956 +               if (p)
26957 +                       *p = 0;
26958 +               v = match_token(str, table, args);
26959 +               if (v) {
26960 +                       if (v & AuBrAttr_CMOO_Mask)
26961 +                               attr &= ~AuBrAttr_CMOO_Mask;
26962 +                       attr |= v;
26963 +               } else {
26964 +                       if (p)
26965 +                               *p = '+';
26966 +                       pr_warn("ignored branch attribute %s\n", str);
26967 +                       break;
26968 +               }
26969 +               if (p)
26970 +                       str = p + 1;
26971 +       } while (p);
26972 +
26973 +       return attr;
26974 +}
26975 +
26976 +static int au_do_optstr_br_attr(au_br_perm_str_t *str, int perm)
26977 +{
26978 +       int sz;
26979 +       const char *p;
26980 +       char *q;
26981 +
26982 +       q = str->a;
26983 +       *q = 0;
26984 +       p = au_optstr(&perm, brattr);
26985 +       if (p) {
26986 +               sz = strlen(p);
26987 +               memcpy(q, p, sz + 1);
26988 +               q += sz;
26989 +       } else
26990 +               goto out;
26991 +
26992 +       do {
26993 +               p = au_optstr(&perm, brattr);
26994 +               if (p) {
26995 +                       *q++ = '+';
26996 +                       sz = strlen(p);
26997 +                       memcpy(q, p, sz + 1);
26998 +                       q += sz;
26999 +               }
27000 +       } while (p);
27001 +
27002 +out:
27003 +       return q - str->a;
27004 +}
27005 +
27006 +int au_br_perm_val(char *perm)
27007 +{
27008 +       int val, bad, sz;
27009 +       char *p;
27010 +       substring_t args[MAX_OPT_ARGS];
27011 +       au_br_perm_str_t attr;
27012 +
27013 +       p = strchr(perm, '+');
27014 +       if (p)
27015 +               *p = 0;
27016 +       val = match_token(perm, brperm, args);
27017 +       if (!val) {
27018 +               if (p)
27019 +                       *p = '+';
27020 +               pr_warn("ignored branch permission %s\n", perm);
27021 +               val = AuBrPerm_RO;
27022 +               goto out;
27023 +       }
27024 +       if (!p)
27025 +               goto out;
27026 +
27027 +       val |= br_attr_val(p + 1, brattr, args);
27028 +
27029 +       bad = 0;
27030 +       switch (val & AuBrPerm_Mask) {
27031 +       case AuBrPerm_RO:
27032 +       case AuBrPerm_RR:
27033 +               bad = val & AuBrWAttr_Mask;
27034 +               val &= ~AuBrWAttr_Mask;
27035 +               break;
27036 +       case AuBrPerm_RW:
27037 +               bad = val & AuBrRAttr_Mask;
27038 +               val &= ~AuBrRAttr_Mask;
27039 +               break;
27040 +       }
27041 +
27042 +       /*
27043 +        * 'unpin' attrib becomes meaningless since linux-3.18-rc1, but aufs
27044 +        * does not treat it as an error, just warning.
27045 +        * this is a tiny guard for the user operation.
27046 +        */
27047 +       if (val & AuBrAttr_UNPIN) {
27048 +               bad |= AuBrAttr_UNPIN;
27049 +               val &= ~AuBrAttr_UNPIN;
27050 +       }
27051 +
27052 +       if (unlikely(bad)) {
27053 +               sz = au_do_optstr_br_attr(&attr, bad);
27054 +               AuDebugOn(!sz);
27055 +               pr_warn("ignored branch attribute %s\n", attr.a);
27056 +       }
27057 +
27058 +out:
27059 +       return val;
27060 +}
27061 +
27062 +void au_optstr_br_perm(au_br_perm_str_t *str, int perm)
27063 +{
27064 +       au_br_perm_str_t attr;
27065 +       const char *p;
27066 +       char *q;
27067 +       int sz;
27068 +
27069 +       q = str->a;
27070 +       p = au_optstr(&perm, brperm);
27071 +       AuDebugOn(!p || !*p);
27072 +       sz = strlen(p);
27073 +       memcpy(q, p, sz + 1);
27074 +       q += sz;
27075 +
27076 +       sz = au_do_optstr_br_attr(&attr, perm);
27077 +       if (sz) {
27078 +               *q++ = '+';
27079 +               memcpy(q, attr.a, sz + 1);
27080 +       }
27081 +
27082 +       AuDebugOn(strlen(str->a) >= sizeof(str->a));
27083 +}
27084 +
27085 +/* ---------------------------------------------------------------------- */
27086 +
27087 +static match_table_t udbalevel = {
27088 +       {AuOpt_UDBA_REVAL, "reval"},
27089 +       {AuOpt_UDBA_NONE, "none"},
27090 +#ifdef CONFIG_AUFS_HNOTIFY
27091 +       {AuOpt_UDBA_HNOTIFY, "notify"}, /* abstraction */
27092 +#ifdef CONFIG_AUFS_HFSNOTIFY
27093 +       {AuOpt_UDBA_HNOTIFY, "fsnotify"},
27094 +#endif
27095 +#endif
27096 +       {-1, NULL}
27097 +};
27098 +
27099 +int au_udba_val(char *str)
27100 +{
27101 +       substring_t args[MAX_OPT_ARGS];
27102 +
27103 +       return match_token(str, udbalevel, args);
27104 +}
27105 +
27106 +const char *au_optstr_udba(int udba)
27107 +{
27108 +       return au_parser_pattern(udba, udbalevel);
27109 +}
27110 +
27111 +/* ---------------------------------------------------------------------- */
27112 +
27113 +static match_table_t au_wbr_create_policy = {
27114 +       {AuWbrCreate_TDP, "tdp"},
27115 +       {AuWbrCreate_TDP, "top-down-parent"},
27116 +       {AuWbrCreate_RR, "rr"},
27117 +       {AuWbrCreate_RR, "round-robin"},
27118 +       {AuWbrCreate_MFS, "mfs"},
27119 +       {AuWbrCreate_MFS, "most-free-space"},
27120 +       {AuWbrCreate_MFSV, "mfs:%d"},
27121 +       {AuWbrCreate_MFSV, "most-free-space:%d"},
27122 +
27123 +       /* top-down regardless the parent, and then mfs */
27124 +       {AuWbrCreate_TDMFS, "tdmfs:%d"},
27125 +       {AuWbrCreate_TDMFSV, "tdmfs:%d:%d"},
27126 +
27127 +       {AuWbrCreate_MFSRR, "mfsrr:%d"},
27128 +       {AuWbrCreate_MFSRRV, "mfsrr:%d:%d"},
27129 +       {AuWbrCreate_PMFS, "pmfs"},
27130 +       {AuWbrCreate_PMFSV, "pmfs:%d"},
27131 +       {AuWbrCreate_PMFSRR, "pmfsrr:%d"},
27132 +       {AuWbrCreate_PMFSRRV, "pmfsrr:%d:%d"},
27133 +
27134 +       {-1, NULL}
27135 +};
27136 +
27137 +static int au_wbr_mfs_wmark(substring_t *arg, char *str,
27138 +                           struct au_opt_wbr_create *create)
27139 +{
27140 +       int err;
27141 +       unsigned long long ull;
27142 +
27143 +       err = 0;
27144 +       if (!match_u64(arg, &ull))
27145 +               create->mfsrr_watermark = ull;
27146 +       else {
27147 +               pr_err("bad integer in %s\n", str);
27148 +               err = -EINVAL;
27149 +       }
27150 +
27151 +       return err;
27152 +}
27153 +
27154 +static int au_wbr_mfs_sec(substring_t *arg, char *str,
27155 +                         struct au_opt_wbr_create *create)
27156 +{
27157 +       int n, err;
27158 +
27159 +       err = 0;
27160 +       if (!match_int(arg, &n) && 0 <= n && n <= AUFS_MFS_MAX_SEC)
27161 +               create->mfs_second = n;
27162 +       else {
27163 +               pr_err("bad integer in %s\n", str);
27164 +               err = -EINVAL;
27165 +       }
27166 +
27167 +       return err;
27168 +}
27169 +
27170 +int au_wbr_create_val(char *str, struct au_opt_wbr_create *create)
27171 +{
27172 +       int err, e;
27173 +       substring_t args[MAX_OPT_ARGS];
27174 +
27175 +       err = match_token(str, au_wbr_create_policy, args);
27176 +       create->wbr_create = err;
27177 +       switch (err) {
27178 +       case AuWbrCreate_MFSRRV:
27179 +       case AuWbrCreate_TDMFSV:
27180 +       case AuWbrCreate_PMFSRRV:
27181 +               e = au_wbr_mfs_wmark(&args[0], str, create);
27182 +               if (!e)
27183 +                       e = au_wbr_mfs_sec(&args[1], str, create);
27184 +               if (unlikely(e))
27185 +                       err = e;
27186 +               break;
27187 +       case AuWbrCreate_MFSRR:
27188 +       case AuWbrCreate_TDMFS:
27189 +       case AuWbrCreate_PMFSRR:
27190 +               e = au_wbr_mfs_wmark(&args[0], str, create);
27191 +               if (unlikely(e)) {
27192 +                       err = e;
27193 +                       break;
27194 +               }
27195 +               fallthrough;
27196 +       case AuWbrCreate_MFS:
27197 +       case AuWbrCreate_PMFS:
27198 +               create->mfs_second = AUFS_MFS_DEF_SEC;
27199 +               break;
27200 +       case AuWbrCreate_MFSV:
27201 +       case AuWbrCreate_PMFSV:
27202 +               e = au_wbr_mfs_sec(&args[0], str, create);
27203 +               if (unlikely(e))
27204 +                       err = e;
27205 +               break;
27206 +       }
27207 +
27208 +       return err;
27209 +}
27210 +
27211 +const char *au_optstr_wbr_create(int wbr_create)
27212 +{
27213 +       return au_parser_pattern(wbr_create, au_wbr_create_policy);
27214 +}
27215 +
27216 +static match_table_t au_wbr_copyup_policy = {
27217 +       {AuWbrCopyup_TDP, "tdp"},
27218 +       {AuWbrCopyup_TDP, "top-down-parent"},
27219 +       {AuWbrCopyup_BUP, "bup"},
27220 +       {AuWbrCopyup_BUP, "bottom-up-parent"},
27221 +       {AuWbrCopyup_BU, "bu"},
27222 +       {AuWbrCopyup_BU, "bottom-up"},
27223 +       {-1, NULL}
27224 +};
27225 +
27226 +int au_wbr_copyup_val(char *str)
27227 +{
27228 +       substring_t args[MAX_OPT_ARGS];
27229 +
27230 +       return match_token(str, au_wbr_copyup_policy, args);
27231 +}
27232 +
27233 +const char *au_optstr_wbr_copyup(int wbr_copyup)
27234 +{
27235 +       return au_parser_pattern(wbr_copyup, au_wbr_copyup_policy);
27236 +}
27237 +
27238 +/* ---------------------------------------------------------------------- */
27239 +
27240 +int au_opt_add(struct au_opt *opt, char *opt_str, unsigned long sb_flags,
27241 +              aufs_bindex_t bindex)
27242 +{
27243 +       int err;
27244 +       struct au_opt_add *add = &opt->add;
27245 +       char *p;
27246 +
27247 +       add->bindex = bindex;
27248 +       add->perm = AuBrPerm_RO;
27249 +       add->pathname = opt_str;
27250 +       p = strchr(opt_str, '=');
27251 +       if (p) {
27252 +               *p++ = 0;
27253 +               if (*p)
27254 +                       add->perm = au_br_perm_val(p);
27255 +       }
27256 +
27257 +       err = vfsub_kern_path(add->pathname, AuOpt_LkupDirFlags, &add->path);
27258 +       if (!err) {
27259 +               if (!p) {
27260 +                       add->perm = AuBrPerm_RO;
27261 +                       if (au_test_fs_rr(add->path.dentry->d_sb))
27262 +                               add->perm = AuBrPerm_RR;
27263 +                       else if (!bindex && !(sb_flags & SB_RDONLY))
27264 +                               add->perm = AuBrPerm_RW;
27265 +               }
27266 +               opt->type = Opt_add;
27267 +               goto out;
27268 +       }
27269 +       pr_err("lookup failed %s (%d)\n", add->pathname, err);
27270 +       err = -EINVAL;
27271 +
27272 +out:
27273 +       return err;
27274 +}
27275 +
27276 +static int au_opt_wbr_create(struct super_block *sb,
27277 +                            struct au_opt_wbr_create *create)
27278 +{
27279 +       int err;
27280 +       struct au_sbinfo *sbinfo;
27281 +
27282 +       SiMustWriteLock(sb);
27283 +
27284 +       err = 1; /* handled */
27285 +       sbinfo = au_sbi(sb);
27286 +       if (sbinfo->si_wbr_create_ops->fin) {
27287 +               err = sbinfo->si_wbr_create_ops->fin(sb);
27288 +               if (!err)
27289 +                       err = 1;
27290 +       }
27291 +
27292 +       sbinfo->si_wbr_create = create->wbr_create;
27293 +       sbinfo->si_wbr_create_ops = au_wbr_create_ops + create->wbr_create;
27294 +       switch (create->wbr_create) {
27295 +       case AuWbrCreate_MFSRRV:
27296 +       case AuWbrCreate_MFSRR:
27297 +       case AuWbrCreate_TDMFS:
27298 +       case AuWbrCreate_TDMFSV:
27299 +       case AuWbrCreate_PMFSRR:
27300 +       case AuWbrCreate_PMFSRRV:
27301 +               sbinfo->si_wbr_mfs.mfsrr_watermark = create->mfsrr_watermark;
27302 +               fallthrough;
27303 +       case AuWbrCreate_MFS:
27304 +       case AuWbrCreate_MFSV:
27305 +       case AuWbrCreate_PMFS:
27306 +       case AuWbrCreate_PMFSV:
27307 +               sbinfo->si_wbr_mfs.mfs_expire
27308 +                       = msecs_to_jiffies(create->mfs_second * MSEC_PER_SEC);
27309 +               break;
27310 +       }
27311 +
27312 +       if (sbinfo->si_wbr_create_ops->init)
27313 +               sbinfo->si_wbr_create_ops->init(sb); /* ignore */
27314 +
27315 +       return err;
27316 +}
27317 +
27318 +/*
27319 + * returns,
27320 + * plus: processed without an error
27321 + * zero: unprocessed
27322 + */
27323 +static int au_opt_simple(struct super_block *sb, struct au_opt *opt,
27324 +                        struct au_opts *opts)
27325 +{
27326 +       int err;
27327 +       struct au_sbinfo *sbinfo;
27328 +
27329 +       SiMustWriteLock(sb);
27330 +
27331 +       err = 1; /* handled */
27332 +       sbinfo = au_sbi(sb);
27333 +       switch (opt->type) {
27334 +       case Opt_udba:
27335 +               sbinfo->si_mntflags &= ~AuOptMask_UDBA;
27336 +               sbinfo->si_mntflags |= opt->udba;
27337 +               opts->given_udba |= opt->udba;
27338 +               break;
27339 +
27340 +       case Opt_plink:
27341 +               if (opt->tf)
27342 +                       au_opt_set(sbinfo->si_mntflags, PLINK);
27343 +               else {
27344 +                       if (au_opt_test(sbinfo->si_mntflags, PLINK))
27345 +                               au_plink_put(sb, /*verbose*/1);
27346 +                       au_opt_clr(sbinfo->si_mntflags, PLINK);
27347 +               }
27348 +               break;
27349 +       case Opt_list_plink:
27350 +               if (au_opt_test(sbinfo->si_mntflags, PLINK))
27351 +                       au_plink_list(sb);
27352 +               break;
27353 +
27354 +       case Opt_dio:
27355 +               if (opt->tf) {
27356 +                       au_opt_set(sbinfo->si_mntflags, DIO);
27357 +                       au_fset_opts(opts->flags, REFRESH_DYAOP);
27358 +               } else {
27359 +                       au_opt_clr(sbinfo->si_mntflags, DIO);
27360 +                       au_fset_opts(opts->flags, REFRESH_DYAOP);
27361 +               }
27362 +               break;
27363 +
27364 +       case Opt_fhsm_sec:
27365 +               au_fhsm_set(sbinfo, opt->fhsm_second);
27366 +               break;
27367 +
27368 +       case Opt_diropq_a:
27369 +               au_opt_set(sbinfo->si_mntflags, ALWAYS_DIROPQ);
27370 +               break;
27371 +       case Opt_diropq_w:
27372 +               au_opt_clr(sbinfo->si_mntflags, ALWAYS_DIROPQ);
27373 +               break;
27374 +
27375 +       case Opt_warn_perm:
27376 +               if (opt->tf)
27377 +                       au_opt_set(sbinfo->si_mntflags, WARN_PERM);
27378 +               else
27379 +                       au_opt_clr(sbinfo->si_mntflags, WARN_PERM);
27380 +               break;
27381 +
27382 +       case Opt_verbose:
27383 +               if (opt->tf)
27384 +                       au_opt_set(sbinfo->si_mntflags, VERBOSE);
27385 +               else
27386 +                       au_opt_clr(sbinfo->si_mntflags, VERBOSE);
27387 +               break;
27388 +
27389 +       case Opt_sum:
27390 +               if (opt->tf)
27391 +                       au_opt_set(sbinfo->si_mntflags, SUM);
27392 +               else {
27393 +                       au_opt_clr(sbinfo->si_mntflags, SUM);
27394 +                       au_opt_clr(sbinfo->si_mntflags, SUM_W);
27395 +               }
27396 +               break;
27397 +       case Opt_wsum:
27398 +               au_opt_clr(sbinfo->si_mntflags, SUM);
27399 +               au_opt_set(sbinfo->si_mntflags, SUM_W);
27400 +               break;
27401 +
27402 +       case Opt_wbr_create:
27403 +               err = au_opt_wbr_create(sb, &opt->wbr_create);
27404 +               break;
27405 +       case Opt_wbr_copyup:
27406 +               sbinfo->si_wbr_copyup = opt->wbr_copyup;
27407 +               sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + opt->wbr_copyup;
27408 +               break;
27409 +
27410 +       case Opt_dirwh:
27411 +               sbinfo->si_dirwh = opt->dirwh;
27412 +               break;
27413 +
27414 +       case Opt_rdcache:
27415 +               sbinfo->si_rdcache
27416 +                       = msecs_to_jiffies(opt->rdcache * MSEC_PER_SEC);
27417 +               break;
27418 +       case Opt_rdblk:
27419 +               sbinfo->si_rdblk = opt->rdblk;
27420 +               break;
27421 +       case Opt_rdhash:
27422 +               sbinfo->si_rdhash = opt->rdhash;
27423 +               break;
27424 +
27425 +       case Opt_shwh:
27426 +               if (opt->tf)
27427 +                       au_opt_set(sbinfo->si_mntflags, SHWH);
27428 +               else
27429 +                       au_opt_clr(sbinfo->si_mntflags, SHWH);
27430 +               break;
27431 +
27432 +       case Opt_dirperm1:
27433 +               if (opt->tf)
27434 +                       au_opt_set(sbinfo->si_mntflags, DIRPERM1);
27435 +               else
27436 +                       au_opt_clr(sbinfo->si_mntflags, DIRPERM1);
27437 +               break;
27438 +
27439 +       case Opt_trunc_xino:
27440 +               if (opt->tf)
27441 +                       au_opt_set(sbinfo->si_mntflags, TRUNC_XINO);
27442 +               else
27443 +                       au_opt_clr(sbinfo->si_mntflags, TRUNC_XINO);
27444 +               break;
27445 +
27446 +       case Opt_trunc_xino_path:
27447 +       case Opt_itrunc_xino:
27448 +               err = au_xino_trunc(sb, opt->xino_itrunc.bindex,
27449 +                                   /*idx_begin*/0);
27450 +               if (!err)
27451 +                       err = 1;
27452 +               break;
27453 +
27454 +       case Opt_trunc_xib:
27455 +               if (opt->tf)
27456 +                       au_fset_opts(opts->flags, TRUNC_XIB);
27457 +               else
27458 +                       au_fclr_opts(opts->flags, TRUNC_XIB);
27459 +               break;
27460 +
27461 +       case Opt_dirren:
27462 +               err = 1;
27463 +               if (opt->tf) {
27464 +                       if (!au_opt_test(sbinfo->si_mntflags, DIRREN)) {
27465 +                               err = au_dr_opt_set(sb);
27466 +                               if (!err)
27467 +                                       err = 1;
27468 +                       }
27469 +                       if (err == 1)
27470 +                               au_opt_set(sbinfo->si_mntflags, DIRREN);
27471 +               } else {
27472 +                       if (au_opt_test(sbinfo->si_mntflags, DIRREN)) {
27473 +                               err = au_dr_opt_clr(sb, au_ftest_opts(opts->flags,
27474 +                                                                     DR_FLUSHED));
27475 +                               if (!err)
27476 +                                       err = 1;
27477 +                       }
27478 +                       if (err == 1)
27479 +                               au_opt_clr(sbinfo->si_mntflags, DIRREN);
27480 +               }
27481 +               break;
27482 +
27483 +       case Opt_acl:
27484 +               if (opt->tf)
27485 +                       sb->s_flags |= SB_POSIXACL;
27486 +               else
27487 +                       sb->s_flags &= ~SB_POSIXACL;
27488 +               break;
27489 +
27490 +       default:
27491 +               err = 0;
27492 +               break;
27493 +       }
27494 +
27495 +       return err;
27496 +}
27497 +
27498 +/*
27499 + * returns tri-state.
27500 + * plus: processed without an error
27501 + * zero: unprocessed
27502 + * minus: error
27503 + */
27504 +static int au_opt_br(struct super_block *sb, struct au_opt *opt,
27505 +                    struct au_opts *opts)
27506 +{
27507 +       int err, do_refresh;
27508 +
27509 +       err = 0;
27510 +       switch (opt->type) {
27511 +       case Opt_append:
27512 +               opt->add.bindex = au_sbbot(sb) + 1;
27513 +               if (opt->add.bindex < 0)
27514 +                       opt->add.bindex = 0;
27515 +               goto add;
27516 +               /* Always goto add, not fallthrough */
27517 +       case Opt_prepend:
27518 +               opt->add.bindex = 0;
27519 +               fallthrough;
27520 +       add: /* indented label */
27521 +       case Opt_add:
27522 +               err = au_br_add(sb, &opt->add,
27523 +                               au_ftest_opts(opts->flags, REMOUNT));
27524 +               if (!err) {
27525 +                       err = 1;
27526 +                       au_fset_opts(opts->flags, REFRESH);
27527 +               }
27528 +               break;
27529 +
27530 +       case Opt_del:
27531 +       case Opt_idel:
27532 +               err = au_br_del(sb, &opt->del,
27533 +                               au_ftest_opts(opts->flags, REMOUNT));
27534 +               if (!err) {
27535 +                       err = 1;
27536 +                       au_fset_opts(opts->flags, TRUNC_XIB);
27537 +                       au_fset_opts(opts->flags, REFRESH);
27538 +               }
27539 +               break;
27540 +
27541 +       case Opt_mod:
27542 +       case Opt_imod:
27543 +               err = au_br_mod(sb, &opt->mod,
27544 +                               au_ftest_opts(opts->flags, REMOUNT),
27545 +                               &do_refresh);
27546 +               if (!err) {
27547 +                       err = 1;
27548 +                       if (do_refresh)
27549 +                               au_fset_opts(opts->flags, REFRESH);
27550 +               }
27551 +               break;
27552 +       }
27553 +       return err;
27554 +}
27555 +
27556 +static int au_opt_xino(struct super_block *sb, struct au_opt *opt,
27557 +                      struct au_opt_xino **opt_xino,
27558 +                      struct au_opts *opts)
27559 +{
27560 +       int err;
27561 +
27562 +       err = 0;
27563 +       switch (opt->type) {
27564 +       case Opt_xino:
27565 +               err = au_xino_set(sb, &opt->xino,
27566 +                                 !!au_ftest_opts(opts->flags, REMOUNT));
27567 +               if (!err)
27568 +                       *opt_xino = &opt->xino;
27569 +               break;
27570 +       case Opt_noxino:
27571 +               au_xino_clr(sb);
27572 +               *opt_xino = (void *)-1;
27573 +               break;
27574 +       }
27575 +
27576 +       return err;
27577 +}
27578 +
27579 +int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
27580 +                  unsigned int pending)
27581 +{
27582 +       int err, fhsm;
27583 +       aufs_bindex_t bindex, bbot;
27584 +       unsigned char do_plink, skip, do_free, can_no_dreval;
27585 +       struct au_branch *br;
27586 +       struct au_wbr *wbr;
27587 +       struct dentry *root, *dentry;
27588 +       struct inode *dir, *h_dir;
27589 +       struct au_sbinfo *sbinfo;
27590 +       struct au_hinode *hdir;
27591 +
27592 +       SiMustAnyLock(sb);
27593 +
27594 +       sbinfo = au_sbi(sb);
27595 +       AuDebugOn(!(sbinfo->si_mntflags & AuOptMask_UDBA));
27596 +
27597 +       if (!(sb_flags & SB_RDONLY)) {
27598 +               if (unlikely(!au_br_writable(au_sbr_perm(sb, 0))))
27599 +                       pr_warn("first branch should be rw\n");
27600 +               if (unlikely(au_opt_test(sbinfo->si_mntflags, SHWH)))
27601 +                       pr_warn_once("shwh should be used with ro\n");
27602 +       }
27603 +
27604 +       if (au_opt_test((sbinfo->si_mntflags | pending), UDBA_HNOTIFY)
27605 +           && !au_opt_test(sbinfo->si_mntflags, XINO))
27606 +               pr_warn_once("udba=*notify requires xino\n");
27607 +
27608 +       if (au_opt_test(sbinfo->si_mntflags, DIRPERM1))
27609 +               pr_warn_once("dirperm1 breaks the protection"
27610 +                            " by the permission bits on the lower branch\n");
27611 +
27612 +       err = 0;
27613 +       fhsm = 0;
27614 +       root = sb->s_root;
27615 +       dir = d_inode(root);
27616 +       do_plink = !!au_opt_test(sbinfo->si_mntflags, PLINK);
27617 +       can_no_dreval = !!au_opt_test((sbinfo->si_mntflags | pending),
27618 +                                     UDBA_NONE);
27619 +       bbot = au_sbbot(sb);
27620 +       for (bindex = 0; !err && bindex <= bbot; bindex++) {
27621 +               skip = 0;
27622 +               h_dir = au_h_iptr(dir, bindex);
27623 +               br = au_sbr(sb, bindex);
27624 +
27625 +               if ((br->br_perm & AuBrAttr_ICEX)
27626 +                   && !h_dir->i_op->listxattr)
27627 +                       br->br_perm &= ~AuBrAttr_ICEX;
27628 +#if 0 /* untested */
27629 +               if ((br->br_perm & AuBrAttr_ICEX_SEC)
27630 +                   && (au_br_sb(br)->s_flags & SB_NOSEC))
27631 +                       br->br_perm &= ~AuBrAttr_ICEX_SEC;
27632 +#endif
27633 +
27634 +               do_free = 0;
27635 +               wbr = br->br_wbr;
27636 +               if (wbr)
27637 +                       wbr_wh_read_lock(wbr);
27638 +
27639 +               if (!au_br_writable(br->br_perm)) {
27640 +                       do_free = !!wbr;
27641 +                       skip = (!wbr
27642 +                               || (!wbr->wbr_whbase
27643 +                                   && !wbr->wbr_plink
27644 +                                   && !wbr->wbr_orph));
27645 +               } else if (!au_br_wh_linkable(br->br_perm)) {
27646 +                       /* skip = (!br->br_whbase && !br->br_orph); */
27647 +                       skip = (!wbr || !wbr->wbr_whbase);
27648 +                       if (skip && wbr) {
27649 +                               if (do_plink)
27650 +                                       skip = !!wbr->wbr_plink;
27651 +                               else
27652 +                                       skip = !wbr->wbr_plink;
27653 +                       }
27654 +               } else {
27655 +                       /* skip = (br->br_whbase && br->br_ohph); */
27656 +                       skip = (wbr && wbr->wbr_whbase);
27657 +                       if (skip) {
27658 +                               if (do_plink)
27659 +                                       skip = !!wbr->wbr_plink;
27660 +                               else
27661 +                                       skip = !wbr->wbr_plink;
27662 +                       }
27663 +               }
27664 +               if (wbr)
27665 +                       wbr_wh_read_unlock(wbr);
27666 +
27667 +               if (can_no_dreval) {
27668 +                       dentry = br->br_path.dentry;
27669 +                       spin_lock(&dentry->d_lock);
27670 +                       if (dentry->d_flags &
27671 +                           (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE))
27672 +                               can_no_dreval = 0;
27673 +                       spin_unlock(&dentry->d_lock);
27674 +               }
27675 +
27676 +               if (au_br_fhsm(br->br_perm)) {
27677 +                       fhsm++;
27678 +                       AuDebugOn(!br->br_fhsm);
27679 +               }
27680 +
27681 +               if (skip)
27682 +                       continue;
27683 +
27684 +               hdir = au_hi(dir, bindex);
27685 +               au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
27686 +               if (wbr)
27687 +                       wbr_wh_write_lock(wbr);
27688 +               err = au_wh_init(br, sb);
27689 +               if (wbr)
27690 +                       wbr_wh_write_unlock(wbr);
27691 +               au_hn_inode_unlock(hdir);
27692 +
27693 +               if (!err && do_free) {
27694 +                       au_kfree_rcu(wbr);
27695 +                       br->br_wbr = NULL;
27696 +               }
27697 +       }
27698 +
27699 +       if (can_no_dreval)
27700 +               au_fset_si(sbinfo, NO_DREVAL);
27701 +       else
27702 +               au_fclr_si(sbinfo, NO_DREVAL);
27703 +
27704 +       if (fhsm >= 2) {
27705 +               au_fset_si(sbinfo, FHSM);
27706 +               for (bindex = bbot; bindex >= 0; bindex--) {
27707 +                       br = au_sbr(sb, bindex);
27708 +                       if (au_br_fhsm(br->br_perm)) {
27709 +                               au_fhsm_set_bottom(sb, bindex);
27710 +                               break;
27711 +                       }
27712 +               }
27713 +       } else {
27714 +               au_fclr_si(sbinfo, FHSM);
27715 +               au_fhsm_set_bottom(sb, -1);
27716 +       }
27717 +
27718 +       return err;
27719 +}
27720 +
27721 +int au_opts_mount(struct super_block *sb, struct au_opts *opts)
27722 +{
27723 +       int err;
27724 +       unsigned int tmp;
27725 +       aufs_bindex_t bindex, bbot;
27726 +       struct au_opt *opt;
27727 +       struct au_opt_xino *opt_xino, xino;
27728 +       struct au_sbinfo *sbinfo;
27729 +       struct au_branch *br;
27730 +       struct inode *dir;
27731 +
27732 +       SiMustWriteLock(sb);
27733 +
27734 +       err = 0;
27735 +       opt_xino = NULL;
27736 +       opt = opts->opt;
27737 +       while (err >= 0 && opt->type != Opt_tail)
27738 +               err = au_opt_simple(sb, opt++, opts);
27739 +       if (err > 0)
27740 +               err = 0;
27741 +       else if (unlikely(err < 0))
27742 +               goto out;
27743 +
27744 +       /* disable xino and udba temporary */
27745 +       sbinfo = au_sbi(sb);
27746 +       tmp = sbinfo->si_mntflags;
27747 +       au_opt_clr(sbinfo->si_mntflags, XINO);
27748 +       au_opt_set_udba(sbinfo->si_mntflags, UDBA_REVAL);
27749 +
27750 +       opt = opts->opt;
27751 +       while (err >= 0 && opt->type != Opt_tail)
27752 +               err = au_opt_br(sb, opt++, opts);
27753 +       if (err > 0)
27754 +               err = 0;
27755 +       else if (unlikely(err < 0))
27756 +               goto out;
27757 +
27758 +       bbot = au_sbbot(sb);
27759 +       if (unlikely(bbot < 0)) {
27760 +               err = -EINVAL;
27761 +               pr_err("no branches\n");
27762 +               goto out;
27763 +       }
27764 +
27765 +       if (au_opt_test(tmp, XINO))
27766 +               au_opt_set(sbinfo->si_mntflags, XINO);
27767 +       opt = opts->opt;
27768 +       while (!err && opt->type != Opt_tail)
27769 +               err = au_opt_xino(sb, opt++, &opt_xino, opts);
27770 +       if (unlikely(err))
27771 +               goto out;
27772 +
27773 +       err = au_opts_verify(sb, sb->s_flags, tmp);
27774 +       if (unlikely(err))
27775 +               goto out;
27776 +
27777 +       /* restore xino */
27778 +       if (au_opt_test(tmp, XINO) && !opt_xino) {
27779 +               xino.file = au_xino_def(sb);
27780 +               err = PTR_ERR(xino.file);
27781 +               if (IS_ERR(xino.file))
27782 +                       goto out;
27783 +
27784 +               err = au_xino_set(sb, &xino, /*remount*/0);
27785 +               fput(xino.file);
27786 +               if (unlikely(err))
27787 +                       goto out;
27788 +       }
27789 +
27790 +       /* restore udba */
27791 +       tmp &= AuOptMask_UDBA;
27792 +       sbinfo->si_mntflags &= ~AuOptMask_UDBA;
27793 +       sbinfo->si_mntflags |= tmp;
27794 +       bbot = au_sbbot(sb);
27795 +       for (bindex = 0; bindex <= bbot; bindex++) {
27796 +               br = au_sbr(sb, bindex);
27797 +               err = au_hnotify_reset_br(tmp, br, br->br_perm);
27798 +               if (unlikely(err))
27799 +                       AuIOErr("hnotify failed on br %d, %d, ignored\n",
27800 +                               bindex, err);
27801 +               /* go on even if err */
27802 +       }
27803 +       if (au_opt_test(tmp, UDBA_HNOTIFY)) {
27804 +               dir = d_inode(sb->s_root);
27805 +               au_hn_reset(dir, au_hi_flags(dir, /*isdir*/1) & ~AuHi_XINO);
27806 +       }
27807 +
27808 +out:
27809 +       return err;
27810 +}
27811 +
27812 +int au_opts_remount(struct super_block *sb, struct au_opts *opts)
27813 +{
27814 +       int err, rerr;
27815 +       unsigned char no_dreval;
27816 +       struct inode *dir;
27817 +       struct au_opt_xino *opt_xino;
27818 +       struct au_opt *opt;
27819 +       struct au_sbinfo *sbinfo;
27820 +
27821 +       SiMustWriteLock(sb);
27822 +
27823 +       err = au_dr_opt_flush(sb);
27824 +       if (unlikely(err))
27825 +               goto out;
27826 +       au_fset_opts(opts->flags, DR_FLUSHED);
27827 +
27828 +       dir = d_inode(sb->s_root);
27829 +       sbinfo = au_sbi(sb);
27830 +       opt_xino = NULL;
27831 +       opt = opts->opt;
27832 +       while (err >= 0 && opt->type != Opt_tail) {
27833 +               err = au_opt_simple(sb, opt, opts);
27834 +               if (!err)
27835 +                       err = au_opt_br(sb, opt, opts);
27836 +               if (!err)
27837 +                       err = au_opt_xino(sb, opt, &opt_xino, opts);
27838 +               opt++;
27839 +       }
27840 +       if (err > 0)
27841 +               err = 0;
27842 +       AuTraceErr(err);
27843 +       /* go on even err */
27844 +
27845 +       no_dreval = !!au_ftest_si(sbinfo, NO_DREVAL);
27846 +       rerr = au_opts_verify(sb, opts->sb_flags, /*pending*/0);
27847 +       if (unlikely(rerr && !err))
27848 +               err = rerr;
27849 +
27850 +       if (no_dreval != !!au_ftest_si(sbinfo, NO_DREVAL))
27851 +               au_fset_opts(opts->flags, REFRESH_IDOP);
27852 +
27853 +       if (au_ftest_opts(opts->flags, TRUNC_XIB)) {
27854 +               rerr = au_xib_trunc(sb);
27855 +               if (unlikely(rerr && !err))
27856 +                       err = rerr;
27857 +       }
27858 +
27859 +       /* will be handled by the caller */
27860 +       if (!au_ftest_opts(opts->flags, REFRESH)
27861 +           && (opts->given_udba
27862 +               || au_opt_test(sbinfo->si_mntflags, XINO)
27863 +               || au_ftest_opts(opts->flags, REFRESH_IDOP)
27864 +                   ))
27865 +               au_fset_opts(opts->flags, REFRESH);
27866 +
27867 +       AuDbg("status 0x%x\n", opts->flags);
27868 +
27869 +out:
27870 +       return err;
27871 +}
27872 +
27873 +/* ---------------------------------------------------------------------- */
27874 +
27875 +unsigned int au_opt_udba(struct super_block *sb)
27876 +{
27877 +       return au_mntflags(sb) & AuOptMask_UDBA;
27878 +}
27879 diff -urN /usr/share/empty/fs/aufs/opts.h linux/fs/aufs/opts.h
27880 --- /usr/share/empty/fs/aufs/opts.h     1970-01-01 01:00:00.000000000 +0100
27881 +++ linux/fs/aufs/opts.h        2022-03-21 14:49:05.729966343 +0100
27882 @@ -0,0 +1,263 @@
27883 +/* SPDX-License-Identifier: GPL-2.0 */
27884 +/*
27885 + * Copyright (C) 2005-2021 Junjiro R. Okajima
27886 + *
27887 + * This program, aufs is free software; you can redistribute it and/or modify
27888 + * it under the terms of the GNU General Public License as published by
27889 + * the Free Software Foundation; either version 2 of the License, or
27890 + * (at your option) any later version.
27891 + *
27892 + * This program is distributed in the hope that it will be useful,
27893 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
27894 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27895 + * GNU General Public License for more details.
27896 + *
27897 + * You should have received a copy of the GNU General Public License
27898 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
27899 + */
27900 +
27901 +/*
27902 + * mount options/flags
27903 + */
27904 +
27905 +#ifndef __AUFS_OPTS_H__
27906 +#define __AUFS_OPTS_H__
27907 +
27908 +#ifdef __KERNEL__
27909 +
27910 +#include <linux/fs_parser.h>
27911 +#include <linux/namei.h>
27912 +#include <linux/path.h>
27913 +
27914 +enum {
27915 +       Opt_br,
27916 +       Opt_add, Opt_del, Opt_mod, Opt_append, Opt_prepend,
27917 +       Opt_idel, Opt_imod,
27918 +       Opt_dirwh, Opt_rdcache, Opt_rdblk, Opt_rdhash,
27919 +       Opt_xino, Opt_noxino,
27920 +       Opt_trunc_xino, Opt_trunc_xino_v,
27921 +       Opt_trunc_xino_path, Opt_itrunc_xino,
27922 +       Opt_trunc_xib,
27923 +       Opt_shwh,
27924 +       Opt_plink, Opt_list_plink,
27925 +       Opt_udba,
27926 +       Opt_dio,
27927 +       Opt_diropq, Opt_diropq_a, Opt_diropq_w,
27928 +       Opt_warn_perm,
27929 +       Opt_wbr_copyup, Opt_wbr_create,
27930 +       Opt_fhsm_sec,
27931 +       Opt_verbose, Opt_noverbose,
27932 +       Opt_sum, Opt_wsum,
27933 +       Opt_dirperm1,
27934 +       Opt_dirren,
27935 +       Opt_acl,
27936 +       Opt_tail, Opt_ignore, Opt_ignore_silent, Opt_err
27937 +};
27938 +
27939 +/* ---------------------------------------------------------------------- */
27940 +
27941 +/* mount flags */
27942 +#define AuOpt_XINO             1               /* external inode number bitmap
27943 +                                                  and translation table */
27944 +#define AuOpt_TRUNC_XINO       (1 << 1)        /* truncate xino files */
27945 +#define AuOpt_UDBA_NONE                (1 << 2)        /* users direct branch access */
27946 +#define AuOpt_UDBA_REVAL       (1 << 3)
27947 +#define AuOpt_UDBA_HNOTIFY     (1 << 4)
27948 +#define AuOpt_SHWH             (1 << 5)        /* show whiteout */
27949 +#define AuOpt_PLINK            (1 << 6)        /* pseudo-link */
27950 +#define AuOpt_DIRPERM1         (1 << 7)        /* ignore the lower dir's perm
27951 +                                                  bits */
27952 +#define AuOpt_ALWAYS_DIROPQ    (1 << 9)        /* policy to creating diropq */
27953 +#define AuOpt_SUM              (1 << 10)       /* summation for statfs(2) */
27954 +#define AuOpt_SUM_W            (1 << 11)       /* unimplemented */
27955 +#define AuOpt_WARN_PERM                (1 << 12)       /* warn when add-branch */
27956 +#define AuOpt_VERBOSE          (1 << 13)       /* print the cause of error */
27957 +#define AuOpt_DIO              (1 << 14)       /* direct io */
27958 +#define AuOpt_DIRREN           (1 << 15)       /* directory rename */
27959 +
27960 +#ifndef CONFIG_AUFS_HNOTIFY
27961 +#undef AuOpt_UDBA_HNOTIFY
27962 +#define AuOpt_UDBA_HNOTIFY     0
27963 +#endif
27964 +#ifndef CONFIG_AUFS_DIRREN
27965 +#undef AuOpt_DIRREN
27966 +#define AuOpt_DIRREN           0
27967 +#endif
27968 +#ifndef CONFIG_AUFS_SHWH
27969 +#undef AuOpt_SHWH
27970 +#define AuOpt_SHWH             0
27971 +#endif
27972 +
27973 +#define AuOpt_Def      (AuOpt_XINO \
27974 +                        | AuOpt_UDBA_REVAL \
27975 +                        | AuOpt_PLINK \
27976 +                        /* | AuOpt_DIRPERM1 */ \
27977 +                        | AuOpt_WARN_PERM)
27978 +#define AuOptMask_UDBA (AuOpt_UDBA_NONE \
27979 +                        | AuOpt_UDBA_REVAL \
27980 +                        | AuOpt_UDBA_HNOTIFY)
27981 +
27982 +#define AuOpt_LkupDirFlags     (LOOKUP_FOLLOW | LOOKUP_DIRECTORY)
27983 +
27984 +#define au_opt_test(flags, name)       (flags & AuOpt_##name)
27985 +#define au_opt_set(flags, name) do { \
27986 +       BUILD_BUG_ON(AuOpt_##name & AuOptMask_UDBA); \
27987 +       ((flags) |= AuOpt_##name); \
27988 +} while (0)
27989 +#define au_opt_set_udba(flags, name) do { \
27990 +       (flags) &= ~AuOptMask_UDBA; \
27991 +       ((flags) |= AuOpt_##name); \
27992 +} while (0)
27993 +#define au_opt_clr(flags, name) do { \
27994 +       ((flags) &= ~AuOpt_##name); \
27995 +} while (0)
27996 +
27997 +static inline unsigned int au_opts_plink(unsigned int mntflags)
27998 +{
27999 +#ifdef CONFIG_PROC_FS
28000 +       return mntflags;
28001 +#else
28002 +       return mntflags & ~AuOpt_PLINK;
28003 +#endif
28004 +}
28005 +
28006 +/* ---------------------------------------------------------------------- */
28007 +
28008 +/* policies to select one among multiple writable branches */
28009 +enum {
28010 +       AuWbrCreate_TDP,        /* top down parent */
28011 +       AuWbrCreate_RR,         /* round robin */
28012 +       AuWbrCreate_MFS,        /* most free space */
28013 +       AuWbrCreate_MFSV,       /* mfs with seconds */
28014 +       AuWbrCreate_MFSRR,      /* mfs then rr */
28015 +       AuWbrCreate_MFSRRV,     /* mfs then rr with seconds */
28016 +       AuWbrCreate_TDMFS,      /* top down regardless parent and mfs */
28017 +       AuWbrCreate_TDMFSV,     /* top down regardless parent and mfs */
28018 +       AuWbrCreate_PMFS,       /* parent and mfs */
28019 +       AuWbrCreate_PMFSV,      /* parent and mfs with seconds */
28020 +       AuWbrCreate_PMFSRR,     /* parent, mfs and round-robin */
28021 +       AuWbrCreate_PMFSRRV,    /* plus seconds */
28022 +
28023 +       AuWbrCreate_Def = AuWbrCreate_TDP
28024 +};
28025 +
28026 +enum {
28027 +       AuWbrCopyup_TDP,        /* top down parent */
28028 +       AuWbrCopyup_BUP,        /* bottom up parent */
28029 +       AuWbrCopyup_BU,         /* bottom up */
28030 +
28031 +       AuWbrCopyup_Def = AuWbrCopyup_TDP
28032 +};
28033 +
28034 +/* ---------------------------------------------------------------------- */
28035 +
28036 +struct file;
28037 +
28038 +struct au_opt_add {
28039 +       aufs_bindex_t   bindex;
28040 +       char            *pathname;
28041 +       int             perm;
28042 +       struct path     path;
28043 +};
28044 +
28045 +struct au_opt_del {
28046 +       char            *pathname;
28047 +       struct path     h_path;
28048 +};
28049 +
28050 +struct au_opt_mod {
28051 +       char            *path;
28052 +       int             perm;
28053 +       struct dentry   *h_root;
28054 +};
28055 +
28056 +struct au_opt_xino {
28057 +       char            *path;
28058 +       struct file     *file;
28059 +};
28060 +
28061 +struct au_opt_xino_itrunc {
28062 +       aufs_bindex_t   bindex;
28063 +};
28064 +
28065 +struct au_opt_wbr_create {
28066 +       int                     wbr_create;
28067 +       int                     mfs_second;
28068 +       unsigned long long      mfsrr_watermark;
28069 +};
28070 +
28071 +struct au_opt {
28072 +       int type;
28073 +       union {
28074 +               struct au_opt_xino      xino;
28075 +               struct au_opt_xino_itrunc xino_itrunc;
28076 +               struct au_opt_add       add;
28077 +               struct au_opt_del       del;
28078 +               struct au_opt_mod       mod;
28079 +               int                     dirwh;
28080 +               int                     rdcache;
28081 +               unsigned int            rdblk;
28082 +               unsigned int            rdhash;
28083 +               int                     udba;
28084 +               struct au_opt_wbr_create wbr_create;
28085 +               int                     wbr_copyup;
28086 +               unsigned int            fhsm_second;
28087 +               bool                    tf; /* generic flag, true or false */
28088 +       };
28089 +};
28090 +
28091 +/* opts flags */
28092 +#define AuOpts_REMOUNT         1
28093 +#define AuOpts_REFRESH         (1 << 1)
28094 +#define AuOpts_TRUNC_XIB       (1 << 2)
28095 +#define AuOpts_REFRESH_DYAOP   (1 << 3)
28096 +#define AuOpts_REFRESH_IDOP    (1 << 4)
28097 +#define AuOpts_DR_FLUSHED      (1 << 5)
28098 +#define au_ftest_opts(flags, name)     ((flags) & AuOpts_##name)
28099 +#define au_fset_opts(flags, name) \
28100 +       do { (flags) |= AuOpts_##name; } while (0)
28101 +#define au_fclr_opts(flags, name) \
28102 +       do { (flags) &= ~AuOpts_##name; } while (0)
28103 +
28104 +#ifndef CONFIG_AUFS_DIRREN
28105 +#undef AuOpts_DR_FLUSHED
28106 +#define AuOpts_DR_FLUSHED      0
28107 +#endif
28108 +
28109 +struct au_opts {
28110 +       struct au_opt   *opt;
28111 +       int             max_opt;
28112 +
28113 +       unsigned int    given_udba;
28114 +       unsigned int    flags;
28115 +       unsigned long   sb_flags;
28116 +};
28117 +
28118 +/* ---------------------------------------------------------------------- */
28119 +
28120 +/* opts.c */
28121 +int au_br_perm_val(char *perm);
28122 +void au_optstr_br_perm(au_br_perm_str_t *str, int perm);
28123 +int au_udba_val(char *str);
28124 +const char *au_optstr_udba(int udba);
28125 +int au_wbr_create_val(char *str, struct au_opt_wbr_create *create);
28126 +const char *au_optstr_wbr_create(int wbr_create);
28127 +int au_wbr_copyup_val(char *str);
28128 +const char *au_optstr_wbr_copyup(int wbr_copyup);
28129 +
28130 +int au_opt_add(struct au_opt *opt, char *opt_str, unsigned long sb_flags,
28131 +              aufs_bindex_t bindex);
28132 +struct super_block;
28133 +int au_opts_verify(struct super_block *sb, unsigned long sb_flags,
28134 +                  unsigned int pending);
28135 +int au_opts_mount(struct super_block *sb, struct au_opts *opts);
28136 +int au_opts_remount(struct super_block *sb, struct au_opts *opts);
28137 +
28138 +unsigned int au_opt_udba(struct super_block *sb);
28139 +
28140 +/* fsctx.c */
28141 +int aufs_fsctx_init(struct fs_context *fc);
28142 +extern const struct fs_parameter_spec aufs_fsctx_paramspec[];
28143 +
28144 +#endif /* __KERNEL__ */
28145 +#endif /* __AUFS_OPTS_H__ */
28146 diff -urN /usr/share/empty/fs/aufs/plink.c linux/fs/aufs/plink.c
28147 --- /usr/share/empty/fs/aufs/plink.c    1970-01-01 01:00:00.000000000 +0100
28148 +++ linux/fs/aufs/plink.c       2022-03-21 14:49:05.729966343 +0100
28149 @@ -0,0 +1,516 @@
28150 +// SPDX-License-Identifier: GPL-2.0
28151 +/*
28152 + * Copyright (C) 2005-2021 Junjiro R. Okajima
28153 + *
28154 + * This program, aufs is free software; you can redistribute it and/or modify
28155 + * it under the terms of the GNU General Public License as published by
28156 + * the Free Software Foundation; either version 2 of the License, or
28157 + * (at your option) any later version.
28158 + *
28159 + * This program is distributed in the hope that it will be useful,
28160 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28161 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28162 + * GNU General Public License for more details.
28163 + *
28164 + * You should have received a copy of the GNU General Public License
28165 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28166 + */
28167 +
28168 +/*
28169 + * pseudo-link
28170 + */
28171 +
28172 +#include "aufs.h"
28173 +
28174 +/*
28175 + * the pseudo-link maintenance mode.
28176 + * during a user process maintains the pseudo-links,
28177 + * prohibit adding a new plink and branch manipulation.
28178 + *
28179 + * Flags
28180 + * NOPLM:
28181 + *     For entry functions which will handle plink, and i_mutex is already held
28182 + *     in VFS.
28183 + *     They cannot wait and should return an error at once.
28184 + *     Callers has to check the error.
28185 + * NOPLMW:
28186 + *     For entry functions which will handle plink, but i_mutex is not held
28187 + *     in VFS.
28188 + *     They can wait the plink maintenance mode to finish.
28189 + *
28190 + * They behave like F_SETLK and F_SETLKW.
28191 + * If the caller never handle plink, then both flags are unnecessary.
28192 + */
28193 +
28194 +int au_plink_maint(struct super_block *sb, int flags)
28195 +{
28196 +       int err;
28197 +       pid_t pid, ppid;
28198 +       struct task_struct *parent, *prev;
28199 +       struct au_sbinfo *sbi;
28200 +
28201 +       SiMustAnyLock(sb);
28202 +
28203 +       err = 0;
28204 +       if (!au_opt_test(au_mntflags(sb), PLINK))
28205 +               goto out;
28206 +
28207 +       sbi = au_sbi(sb);
28208 +       pid = sbi->si_plink_maint_pid;
28209 +       if (!pid || pid == current->pid)
28210 +               goto out;
28211 +
28212 +       /* todo: it highly depends upon /sbin/mount.aufs */
28213 +       prev = NULL;
28214 +       parent = current;
28215 +       ppid = 0;
28216 +       rcu_read_lock();
28217 +       while (1) {
28218 +               parent = rcu_dereference(parent->real_parent);
28219 +               if (parent == prev)
28220 +                       break;
28221 +               ppid = task_pid_vnr(parent);
28222 +               if (pid == ppid) {
28223 +                       rcu_read_unlock();
28224 +                       goto out;
28225 +               }
28226 +               prev = parent;
28227 +       }
28228 +       rcu_read_unlock();
28229 +
28230 +       if (au_ftest_lock(flags, NOPLMW)) {
28231 +               /* if there is no i_mutex lock in VFS, we don't need to wait */
28232 +               /* AuDebugOn(!lockdep_depth(current)); */
28233 +               while (sbi->si_plink_maint_pid) {
28234 +                       si_read_unlock(sb);
28235 +                       /* gave up wake_up_bit() */
28236 +                       wait_event(sbi->si_plink_wq, !sbi->si_plink_maint_pid);
28237 +
28238 +                       if (au_ftest_lock(flags, FLUSH))
28239 +                               au_nwt_flush(&sbi->si_nowait);
28240 +                       si_noflush_read_lock(sb);
28241 +               }
28242 +       } else if (au_ftest_lock(flags, NOPLM)) {
28243 +               AuDbg("ppid %d, pid %d\n", ppid, pid);
28244 +               err = -EAGAIN;
28245 +       }
28246 +
28247 +out:
28248 +       return err;
28249 +}
28250 +
28251 +void au_plink_maint_leave(struct au_sbinfo *sbinfo)
28252 +{
28253 +       spin_lock(&sbinfo->si_plink_maint_lock);
28254 +       sbinfo->si_plink_maint_pid = 0;
28255 +       spin_unlock(&sbinfo->si_plink_maint_lock);
28256 +       wake_up_all(&sbinfo->si_plink_wq);
28257 +}
28258 +
28259 +int au_plink_maint_enter(struct super_block *sb)
28260 +{
28261 +       int err;
28262 +       struct au_sbinfo *sbinfo;
28263 +
28264 +       err = 0;
28265 +       sbinfo = au_sbi(sb);
28266 +       /* make sure i am the only one in this fs */
28267 +       si_write_lock(sb, AuLock_FLUSH);
28268 +       if (au_opt_test(au_mntflags(sb), PLINK)) {
28269 +               spin_lock(&sbinfo->si_plink_maint_lock);
28270 +               if (!sbinfo->si_plink_maint_pid)
28271 +                       sbinfo->si_plink_maint_pid = current->pid;
28272 +               else
28273 +                       err = -EBUSY;
28274 +               spin_unlock(&sbinfo->si_plink_maint_lock);
28275 +       }
28276 +       si_write_unlock(sb);
28277 +
28278 +       return err;
28279 +}
28280 +
28281 +/* ---------------------------------------------------------------------- */
28282 +
28283 +#ifdef CONFIG_AUFS_DEBUG
28284 +void au_plink_list(struct super_block *sb)
28285 +{
28286 +       int 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 +       SiMustAnyLock(sb);
28293 +
28294 +       sbinfo = au_sbi(sb);
28295 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
28296 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
28297 +
28298 +       for (i = 0; i < AuPlink_NHASH; i++) {
28299 +               hbl = sbinfo->si_plink + i;
28300 +               hlist_bl_lock(hbl);
28301 +               hlist_bl_for_each_entry(icntnr, pos, hbl, plink)
28302 +                       AuDbg("%lu\n", icntnr->vfs_inode.i_ino);
28303 +               hlist_bl_unlock(hbl);
28304 +       }
28305 +}
28306 +#endif
28307 +
28308 +/* is the inode pseudo-linked? */
28309 +int au_plink_test(struct inode *inode)
28310 +{
28311 +       int found, i;
28312 +       struct au_sbinfo *sbinfo;
28313 +       struct hlist_bl_head *hbl;
28314 +       struct hlist_bl_node *pos;
28315 +       struct au_icntnr *icntnr;
28316 +
28317 +       sbinfo = au_sbi(inode->i_sb);
28318 +       AuRwMustAnyLock(&sbinfo->si_rwsem);
28319 +       AuDebugOn(!au_opt_test(au_mntflags(inode->i_sb), PLINK));
28320 +       AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
28321 +
28322 +       found = 0;
28323 +       i = au_plink_hash(inode->i_ino);
28324 +       hbl =  sbinfo->si_plink + i;
28325 +       hlist_bl_lock(hbl);
28326 +       hlist_bl_for_each_entry(icntnr, pos, hbl, plink)
28327 +               if (&icntnr->vfs_inode == inode) {
28328 +                       found = 1;
28329 +                       break;
28330 +               }
28331 +       hlist_bl_unlock(hbl);
28332 +       return found;
28333 +}
28334 +
28335 +/* ---------------------------------------------------------------------- */
28336 +
28337 +/*
28338 + * generate a name for plink.
28339 + * the file will be stored under AUFS_WH_PLINKDIR.
28340 + */
28341 +/* 20 is max digits length of ulong 64 */
28342 +#define PLINK_NAME_LEN ((20 + 1) * 2)
28343 +
28344 +static int plink_name(char *name, int len, struct inode *inode,
28345 +                     aufs_bindex_t bindex)
28346 +{
28347 +       int rlen;
28348 +       struct inode *h_inode;
28349 +
28350 +       h_inode = au_h_iptr(inode, bindex);
28351 +       rlen = snprintf(name, len, "%lu.%lu", inode->i_ino, h_inode->i_ino);
28352 +       return rlen;
28353 +}
28354 +
28355 +struct au_do_plink_lkup_args {
28356 +       struct dentry **errp;
28357 +       struct qstr *tgtname;
28358 +       struct path *h_ppath;
28359 +};
28360 +
28361 +static struct dentry *au_do_plink_lkup(struct qstr *tgtname,
28362 +                                      struct path *h_ppath)
28363 +{
28364 +       struct dentry *h_dentry;
28365 +       struct inode *h_inode;
28366 +
28367 +       h_inode = d_inode(h_ppath->dentry);
28368 +       inode_lock_shared_nested(h_inode, AuLsc_I_CHILD2);
28369 +       h_dentry = vfsub_lkup_one(tgtname, h_ppath);
28370 +       inode_unlock_shared(h_inode);
28371 +
28372 +       return h_dentry;
28373 +}
28374 +
28375 +static void au_call_do_plink_lkup(void *args)
28376 +{
28377 +       struct au_do_plink_lkup_args *a = args;
28378 +       *a->errp = au_do_plink_lkup(a->tgtname, a->h_ppath);
28379 +}
28380 +
28381 +/* lookup the plink-ed @inode under the branch at @bindex */
28382 +struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex)
28383 +{
28384 +       struct dentry *h_dentry;
28385 +       struct au_branch *br;
28386 +       struct path h_ppath;
28387 +       int wkq_err;
28388 +       char a[PLINK_NAME_LEN];
28389 +       struct qstr tgtname = QSTR_INIT(a, 0);
28390 +
28391 +       AuDebugOn(au_plink_maint(inode->i_sb, AuLock_NOPLM));
28392 +
28393 +       br = au_sbr(inode->i_sb, bindex);
28394 +       h_ppath.dentry = br->br_wbr->wbr_plink;
28395 +       h_ppath.mnt = au_br_mnt(br);
28396 +       tgtname.len = plink_name(a, sizeof(a), inode, bindex);
28397 +
28398 +       if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
28399 +               struct au_do_plink_lkup_args args = {
28400 +                       .errp           = &h_dentry,
28401 +                       .tgtname        = &tgtname,
28402 +                       .h_ppath        = &h_ppath
28403 +               };
28404 +
28405 +               wkq_err = au_wkq_wait(au_call_do_plink_lkup, &args);
28406 +               if (unlikely(wkq_err))
28407 +                       h_dentry = ERR_PTR(wkq_err);
28408 +       } else
28409 +               h_dentry = au_do_plink_lkup(&tgtname, &h_ppath);
28410 +
28411 +       return h_dentry;
28412 +}
28413 +
28414 +/* create a pseudo-link */
28415 +static int do_whplink(struct qstr *tgt, struct path *h_ppath,
28416 +                     struct dentry *h_dentry)
28417 +{
28418 +       int err;
28419 +       struct path h_path;
28420 +       struct inode *h_dir, *delegated;
28421 +
28422 +       h_dir = d_inode(h_ppath->dentry);
28423 +       inode_lock_nested(h_dir, AuLsc_I_CHILD2);
28424 +       h_path.mnt = h_ppath->mnt;
28425 +again:
28426 +       h_path.dentry = vfsub_lkup_one(tgt, h_ppath);
28427 +       err = PTR_ERR(h_path.dentry);
28428 +       if (IS_ERR(h_path.dentry))
28429 +               goto out;
28430 +
28431 +       err = 0;
28432 +       /* wh.plink dir is not monitored */
28433 +       /* todo: is it really safe? */
28434 +       if (d_is_positive(h_path.dentry)
28435 +           && d_inode(h_path.dentry) != d_inode(h_dentry)) {
28436 +               delegated = NULL;
28437 +               err = vfsub_unlink(h_dir, &h_path, &delegated, /*force*/0);
28438 +               if (unlikely(err == -EWOULDBLOCK)) {
28439 +                       pr_warn("cannot retry for NFSv4 delegation"
28440 +                               " for an internal unlink\n");
28441 +                       iput(delegated);
28442 +               }
28443 +               dput(h_path.dentry);
28444 +               h_path.dentry = NULL;
28445 +               if (!err)
28446 +                       goto again;
28447 +       }
28448 +       if (!err && d_is_negative(h_path.dentry)) {
28449 +               delegated = NULL;
28450 +               err = vfsub_link(h_dentry, h_dir, &h_path, &delegated);
28451 +               if (unlikely(err == -EWOULDBLOCK)) {
28452 +                       pr_warn("cannot retry for NFSv4 delegation"
28453 +                               " for an internal link\n");
28454 +                       iput(delegated);
28455 +               }
28456 +       }
28457 +       dput(h_path.dentry);
28458 +
28459 +out:
28460 +       inode_unlock(h_dir);
28461 +       return err;
28462 +}
28463 +
28464 +struct do_whplink_args {
28465 +       int *errp;
28466 +       struct qstr *tgt;
28467 +       struct path *h_ppath;
28468 +       struct dentry *h_dentry;
28469 +};
28470 +
28471 +static void call_do_whplink(void *args)
28472 +{
28473 +       struct do_whplink_args *a = args;
28474 +       *a->errp = do_whplink(a->tgt, a->h_ppath, a->h_dentry);
28475 +}
28476 +
28477 +static int whplink(struct dentry *h_dentry, struct inode *inode,
28478 +                  aufs_bindex_t bindex)
28479 +{
28480 +       int err, wkq_err;
28481 +       struct au_branch *br;
28482 +       struct au_wbr *wbr;
28483 +       struct path h_ppath;
28484 +       char a[PLINK_NAME_LEN];
28485 +       struct qstr tgtname = QSTR_INIT(a, 0);
28486 +
28487 +       br = au_sbr(inode->i_sb, bindex);
28488 +       wbr = br->br_wbr;
28489 +       h_ppath.dentry = wbr->wbr_plink;
28490 +       h_ppath.mnt = au_br_mnt(br);
28491 +       tgtname.len = plink_name(a, sizeof(a), inode, bindex);
28492 +
28493 +       /* always superio. */
28494 +       if (!uid_eq(current_fsuid(), GLOBAL_ROOT_UID)) {
28495 +               struct do_whplink_args args = {
28496 +                       .errp           = &err,
28497 +                       .tgt            = &tgtname,
28498 +                       .h_ppath        = &h_ppath,
28499 +                       .h_dentry       = h_dentry
28500 +               };
28501 +               wkq_err = au_wkq_wait(call_do_whplink, &args);
28502 +               if (unlikely(wkq_err))
28503 +                       err = wkq_err;
28504 +       } else
28505 +               err = do_whplink(&tgtname, &h_ppath, h_dentry);
28506 +
28507 +       return err;
28508 +}
28509 +
28510 +/*
28511 + * create a new pseudo-link for @h_dentry on @bindex.
28512 + * the linked inode is held in aufs @inode.
28513 + */
28514 +void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
28515 +                    struct dentry *h_dentry)
28516 +{
28517 +       struct super_block *sb;
28518 +       struct au_sbinfo *sbinfo;
28519 +       struct hlist_bl_head *hbl;
28520 +       struct hlist_bl_node *pos;
28521 +       struct au_icntnr *icntnr;
28522 +       int found, err, cnt, i;
28523 +
28524 +       sb = inode->i_sb;
28525 +       sbinfo = au_sbi(sb);
28526 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
28527 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
28528 +
28529 +       found = au_plink_test(inode);
28530 +       if (found)
28531 +               return;
28532 +
28533 +       i = au_plink_hash(inode->i_ino);
28534 +       hbl = sbinfo->si_plink + i;
28535 +       au_igrab(inode);
28536 +
28537 +       hlist_bl_lock(hbl);
28538 +       hlist_bl_for_each_entry(icntnr, pos, hbl, plink) {
28539 +               if (&icntnr->vfs_inode == inode) {
28540 +                       found = 1;
28541 +                       break;
28542 +               }
28543 +       }
28544 +       if (!found) {
28545 +               icntnr = container_of(inode, struct au_icntnr, vfs_inode);
28546 +               hlist_bl_add_head(&icntnr->plink, hbl);
28547 +       }
28548 +       hlist_bl_unlock(hbl);
28549 +       if (!found) {
28550 +               cnt = au_hbl_count(hbl);
28551 +#define msg "unexpectedly unbalanced or too many pseudo-links"
28552 +               if (cnt > AUFS_PLINK_WARN)
28553 +                       AuWarn1(msg ", %d\n", cnt);
28554 +#undef msg
28555 +               err = whplink(h_dentry, inode, bindex);
28556 +               if (unlikely(err)) {
28557 +                       pr_warn("err %d, damaged pseudo link.\n", err);
28558 +                       au_hbl_del(&icntnr->plink, hbl);
28559 +                       iput(&icntnr->vfs_inode);
28560 +               }
28561 +       } else
28562 +               iput(&icntnr->vfs_inode);
28563 +}
28564 +
28565 +/* free all plinks */
28566 +void au_plink_put(struct super_block *sb, int verbose)
28567 +{
28568 +       int i, warned;
28569 +       struct au_sbinfo *sbinfo;
28570 +       struct hlist_bl_head *hbl;
28571 +       struct hlist_bl_node *pos, *tmp;
28572 +       struct au_icntnr *icntnr;
28573 +
28574 +       SiMustWriteLock(sb);
28575 +
28576 +       sbinfo = au_sbi(sb);
28577 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
28578 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
28579 +
28580 +       /* no spin_lock since sbinfo is write-locked */
28581 +       warned = 0;
28582 +       for (i = 0; i < AuPlink_NHASH; i++) {
28583 +               hbl = sbinfo->si_plink + i;
28584 +               if (!warned && verbose && !hlist_bl_empty(hbl)) {
28585 +                       pr_warn("pseudo-link is not flushed");
28586 +                       warned = 1;
28587 +               }
28588 +               hlist_bl_for_each_entry_safe(icntnr, pos, tmp, hbl, plink)
28589 +                       iput(&icntnr->vfs_inode);
28590 +               INIT_HLIST_BL_HEAD(hbl);
28591 +       }
28592 +}
28593 +
28594 +void au_plink_clean(struct super_block *sb, int verbose)
28595 +{
28596 +       struct dentry *root;
28597 +
28598 +       root = sb->s_root;
28599 +       aufs_write_lock(root);
28600 +       if (au_opt_test(au_mntflags(sb), PLINK))
28601 +               au_plink_put(sb, verbose);
28602 +       aufs_write_unlock(root);
28603 +}
28604 +
28605 +static int au_plink_do_half_refresh(struct inode *inode, aufs_bindex_t br_id)
28606 +{
28607 +       int do_put;
28608 +       aufs_bindex_t btop, bbot, bindex;
28609 +
28610 +       do_put = 0;
28611 +       btop = au_ibtop(inode);
28612 +       bbot = au_ibbot(inode);
28613 +       if (btop >= 0) {
28614 +               for (bindex = btop; bindex <= bbot; bindex++) {
28615 +                       if (!au_h_iptr(inode, bindex)
28616 +                           || au_ii_br_id(inode, bindex) != br_id)
28617 +                               continue;
28618 +                       au_set_h_iptr(inode, bindex, NULL, 0);
28619 +                       do_put = 1;
28620 +                       break;
28621 +               }
28622 +               if (do_put)
28623 +                       for (bindex = btop; bindex <= bbot; bindex++)
28624 +                               if (au_h_iptr(inode, bindex)) {
28625 +                                       do_put = 0;
28626 +                                       break;
28627 +                               }
28628 +       } else
28629 +               do_put = 1;
28630 +
28631 +       return do_put;
28632 +}
28633 +
28634 +/* free the plinks on a branch specified by @br_id */
28635 +void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id)
28636 +{
28637 +       struct au_sbinfo *sbinfo;
28638 +       struct hlist_bl_head *hbl;
28639 +       struct hlist_bl_node *pos, *tmp;
28640 +       struct au_icntnr *icntnr;
28641 +       struct inode *inode;
28642 +       int i, do_put;
28643 +
28644 +       SiMustWriteLock(sb);
28645 +
28646 +       sbinfo = au_sbi(sb);
28647 +       AuDebugOn(!au_opt_test(au_mntflags(sb), PLINK));
28648 +       AuDebugOn(au_plink_maint(sb, AuLock_NOPLM));
28649 +
28650 +       /* no bit_lock since sbinfo is write-locked */
28651 +       for (i = 0; i < AuPlink_NHASH; i++) {
28652 +               hbl = sbinfo->si_plink + i;
28653 +               hlist_bl_for_each_entry_safe(icntnr, pos, tmp, hbl, plink) {
28654 +                       inode = au_igrab(&icntnr->vfs_inode);
28655 +                       ii_write_lock_child(inode);
28656 +                       do_put = au_plink_do_half_refresh(inode, br_id);
28657 +                       if (do_put) {
28658 +                               hlist_bl_del(&icntnr->plink);
28659 +                               iput(inode);
28660 +                       }
28661 +                       ii_write_unlock(inode);
28662 +                       iput(inode);
28663 +               }
28664 +       }
28665 +}
28666 diff -urN /usr/share/empty/fs/aufs/poll.c linux/fs/aufs/poll.c
28667 --- /usr/share/empty/fs/aufs/poll.c     1970-01-01 01:00:00.000000000 +0100
28668 +++ linux/fs/aufs/poll.c        2022-03-21 14:49:05.729966343 +0100
28669 @@ -0,0 +1,51 @@
28670 +// SPDX-License-Identifier: GPL-2.0
28671 +/*
28672 + * Copyright (C) 2005-2021 Junjiro R. Okajima
28673 + *
28674 + * This program, aufs is free software; you can redistribute it and/or modify
28675 + * it under the terms of the GNU General Public License as published by
28676 + * the Free Software Foundation; either version 2 of the License, or
28677 + * (at your option) any later version.
28678 + *
28679 + * This program is distributed in the hope that it will be useful,
28680 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28681 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28682 + * GNU General Public License for more details.
28683 + *
28684 + * You should have received a copy of the GNU General Public License
28685 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28686 + */
28687 +
28688 +/*
28689 + * poll operation
28690 + * There is only one filesystem which implements ->poll operation, currently.
28691 + */
28692 +
28693 +#include "aufs.h"
28694 +
28695 +__poll_t aufs_poll(struct file *file, struct poll_table_struct *pt)
28696 +{
28697 +       __poll_t mask;
28698 +       struct file *h_file;
28699 +       struct super_block *sb;
28700 +
28701 +       /* We should pretend an error happened. */
28702 +       mask = EPOLLERR /* | EPOLLIN | EPOLLOUT */;
28703 +       sb = file->f_path.dentry->d_sb;
28704 +       si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLMW);
28705 +
28706 +       h_file = au_read_pre(file, /*keep_fi*/0, /*lsc*/0);
28707 +       if (IS_ERR(h_file)) {
28708 +               AuDbg("h_file %ld\n", PTR_ERR(h_file));
28709 +               goto out;
28710 +       }
28711 +
28712 +       mask = vfs_poll(h_file, pt);
28713 +       fput(h_file); /* instead of au_read_post() */
28714 +
28715 +out:
28716 +       si_read_unlock(sb);
28717 +       if (mask & EPOLLERR)
28718 +               AuDbg("mask 0x%x\n", mask);
28719 +       return mask;
28720 +}
28721 diff -urN /usr/share/empty/fs/aufs/posix_acl.c linux/fs/aufs/posix_acl.c
28722 --- /usr/share/empty/fs/aufs/posix_acl.c        1970-01-01 01:00:00.000000000 +0100
28723 +++ linux/fs/aufs/posix_acl.c   2022-03-21 14:49:05.729966343 +0100
28724 @@ -0,0 +1,111 @@
28725 +// SPDX-License-Identifier: GPL-2.0
28726 +/*
28727 + * Copyright (C) 2014-2021 Junjiro R. Okajima
28728 + *
28729 + * This program, aufs is free software; you can redistribute it and/or modify
28730 + * it under the terms of the GNU General Public License as published by
28731 + * the Free Software Foundation; either version 2 of the License, or
28732 + * (at your option) any later version.
28733 + *
28734 + * This program is distributed in the hope that it will be useful,
28735 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28736 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28737 + * GNU General Public License for more details.
28738 + *
28739 + * You should have received a copy of the GNU General Public License
28740 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28741 + */
28742 +
28743 +/*
28744 + * posix acl operations
28745 + */
28746 +
28747 +#include <linux/fs.h>
28748 +#include "aufs.h"
28749 +
28750 +struct posix_acl *aufs_get_acl(struct inode *inode, int type, bool rcu)
28751 +{
28752 +       struct posix_acl *acl;
28753 +       int err;
28754 +       aufs_bindex_t bindex;
28755 +       struct inode *h_inode;
28756 +       struct super_block *sb;
28757 +
28758 +       acl = ERR_PTR(-ECHILD);
28759 +       if (rcu)
28760 +               goto out;
28761 +
28762 +       acl = NULL;
28763 +       sb = inode->i_sb;
28764 +       si_read_lock(sb, AuLock_FLUSH);
28765 +       ii_read_lock_child(inode);
28766 +       if (!(sb->s_flags & SB_POSIXACL))
28767 +               goto unlock;
28768 +
28769 +       bindex = au_ibtop(inode);
28770 +       h_inode = au_h_iptr(inode, bindex);
28771 +       if (unlikely(!h_inode
28772 +                    || ((h_inode->i_mode & S_IFMT)
28773 +                        != (inode->i_mode & S_IFMT)))) {
28774 +               err = au_busy_or_stale();
28775 +               acl = ERR_PTR(err);
28776 +               goto unlock;
28777 +       }
28778 +
28779 +       /* always topmost only */
28780 +       acl = get_acl(h_inode, type);
28781 +       if (IS_ERR(acl))
28782 +               forget_cached_acl(inode, type);
28783 +       else
28784 +               set_cached_acl(inode, type, acl);
28785 +
28786 +unlock:
28787 +       ii_read_unlock(inode);
28788 +       si_read_unlock(sb);
28789 +
28790 +out:
28791 +       AuTraceErrPtr(acl);
28792 +       return acl;
28793 +}
28794 +
28795 +int aufs_set_acl(struct user_namespace *userns, struct inode *inode,
28796 +                struct posix_acl *acl, int type)
28797 +{
28798 +       int err;
28799 +       ssize_t ssz;
28800 +       struct dentry *dentry;
28801 +       struct au_sxattr arg = {
28802 +               .type = AU_ACL_SET,
28803 +               .u.acl_set = {
28804 +                       .acl    = acl,
28805 +                       .type   = type
28806 +               },
28807 +       };
28808 +
28809 +       IMustLock(inode);
28810 +
28811 +       if (inode->i_ino == AUFS_ROOT_INO)
28812 +               dentry = dget(inode->i_sb->s_root);
28813 +       else {
28814 +               dentry = d_find_alias(inode);
28815 +               if (!dentry)
28816 +                       dentry = d_find_any_alias(inode);
28817 +               if (!dentry) {
28818 +                       pr_warn("cannot handle this inode, "
28819 +                               "please report to aufs-users ML\n");
28820 +                       err = -ENOENT;
28821 +                       goto out;
28822 +               }
28823 +       }
28824 +
28825 +       ssz = au_sxattr(dentry, inode, &arg);
28826 +       /* forget even it if succeeds since the branch might set differently */
28827 +       forget_cached_acl(inode, type);
28828 +       dput(dentry);
28829 +       err = ssz;
28830 +       if (ssz >= 0)
28831 +               err = 0;
28832 +
28833 +out:
28834 +       return err;
28835 +}
28836 diff -urN /usr/share/empty/fs/aufs/procfs.c linux/fs/aufs/procfs.c
28837 --- /usr/share/empty/fs/aufs/procfs.c   1970-01-01 01:00:00.000000000 +0100
28838 +++ linux/fs/aufs/procfs.c      2022-03-21 14:49:05.729966343 +0100
28839 @@ -0,0 +1,170 @@
28840 +// SPDX-License-Identifier: GPL-2.0
28841 +/*
28842 + * Copyright (C) 2010-2021 Junjiro R. Okajima
28843 + *
28844 + * This program, aufs is free software; you can redistribute it and/or modify
28845 + * it under the terms of the GNU General Public License as published by
28846 + * the Free Software Foundation; either version 2 of the License, or
28847 + * (at your option) any later version.
28848 + *
28849 + * This program is distributed in the hope that it will be useful,
28850 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
28851 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
28852 + * GNU General Public License for more details.
28853 + *
28854 + * You should have received a copy of the GNU General Public License
28855 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28856 + */
28857 +
28858 +/*
28859 + * procfs interfaces
28860 + */
28861 +
28862 +#include <linux/proc_fs.h>
28863 +#include "aufs.h"
28864 +
28865 +static int au_procfs_plm_release(struct inode *inode, struct file *file)
28866 +{
28867 +       struct au_sbinfo *sbinfo;
28868 +
28869 +       sbinfo = file->private_data;
28870 +       if (sbinfo) {
28871 +               au_plink_maint_leave(sbinfo);
28872 +               kobject_put(&sbinfo->si_kobj);
28873 +       }
28874 +
28875 +       return 0;
28876 +}
28877 +
28878 +static void au_procfs_plm_write_clean(struct file *file)
28879 +{
28880 +       struct au_sbinfo *sbinfo;
28881 +
28882 +       sbinfo = file->private_data;
28883 +       if (sbinfo)
28884 +               au_plink_clean(sbinfo->si_sb, /*verbose*/0);
28885 +}
28886 +
28887 +static int au_procfs_plm_write_si(struct file *file, unsigned long id)
28888 +{
28889 +       int err;
28890 +       struct super_block *sb;
28891 +       struct au_sbinfo *sbinfo;
28892 +       struct hlist_bl_node *pos;
28893 +
28894 +       err = -EBUSY;
28895 +       if (unlikely(file->private_data))
28896 +               goto out;
28897 +
28898 +       sb = NULL;
28899 +       /* don't use au_sbilist_lock() here */
28900 +       hlist_bl_lock(&au_sbilist);
28901 +       hlist_bl_for_each_entry(sbinfo, pos, &au_sbilist, si_list)
28902 +               if (id == sysaufs_si_id(sbinfo)) {
28903 +                       if (kobject_get_unless_zero(&sbinfo->si_kobj))
28904 +                               sb = sbinfo->si_sb;
28905 +                       break;
28906 +               }
28907 +       hlist_bl_unlock(&au_sbilist);
28908 +
28909 +       err = -EINVAL;
28910 +       if (unlikely(!sb))
28911 +               goto out;
28912 +
28913 +       err = au_plink_maint_enter(sb);
28914 +       if (!err)
28915 +               /* keep kobject_get() */
28916 +               file->private_data = sbinfo;
28917 +       else
28918 +               kobject_put(&sbinfo->si_kobj);
28919 +out:
28920 +       return err;
28921 +}
28922 +
28923 +/*
28924 + * Accept a valid "si=xxxx" only.
28925 + * Once it is accepted successfully, accept "clean" too.
28926 + */
28927 +static ssize_t au_procfs_plm_write(struct file *file, const char __user *ubuf,
28928 +                                  size_t count, loff_t *ppos)
28929 +{
28930 +       ssize_t err;
28931 +       unsigned long id;
28932 +       /* last newline is allowed */
28933 +       char buf[3 + sizeof(unsigned long) * 2 + 1];
28934 +
28935 +       err = -EACCES;
28936 +       if (unlikely(!capable(CAP_SYS_ADMIN)))
28937 +               goto out;
28938 +
28939 +       err = -EINVAL;
28940 +       if (unlikely(count > sizeof(buf)))
28941 +               goto out;
28942 +
28943 +       err = copy_from_user(buf, ubuf, count);
28944 +       if (unlikely(err)) {
28945 +               err = -EFAULT;
28946 +               goto out;
28947 +       }
28948 +       buf[count] = 0;
28949 +
28950 +       err = -EINVAL;
28951 +       if (!strcmp("clean", buf)) {
28952 +               au_procfs_plm_write_clean(file);
28953 +               goto out_success;
28954 +       } else if (unlikely(strncmp("si=", buf, 3)))
28955 +               goto out;
28956 +
28957 +       err = kstrtoul(buf + 3, 16, &id);
28958 +       if (unlikely(err))
28959 +               goto out;
28960 +
28961 +       err = au_procfs_plm_write_si(file, id);
28962 +       if (unlikely(err))
28963 +               goto out;
28964 +
28965 +out_success:
28966 +       err = count; /* success */
28967 +out:
28968 +       return err;
28969 +}
28970 +
28971 +static const struct proc_ops au_procfs_plm_op = {
28972 +       .proc_write     = au_procfs_plm_write,
28973 +       .proc_release   = au_procfs_plm_release
28974 +};
28975 +
28976 +/* ---------------------------------------------------------------------- */
28977 +
28978 +static struct proc_dir_entry *au_procfs_dir;
28979 +
28980 +void au_procfs_fin(void)
28981 +{
28982 +       remove_proc_entry(AUFS_PLINK_MAINT_NAME, au_procfs_dir);
28983 +       remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
28984 +}
28985 +
28986 +int __init au_procfs_init(void)
28987 +{
28988 +       int err;
28989 +       struct proc_dir_entry *entry;
28990 +
28991 +       err = -ENOMEM;
28992 +       au_procfs_dir = proc_mkdir(AUFS_PLINK_MAINT_DIR, NULL);
28993 +       if (unlikely(!au_procfs_dir))
28994 +               goto out;
28995 +
28996 +       entry = proc_create(AUFS_PLINK_MAINT_NAME, S_IFREG | 0200,
28997 +                           au_procfs_dir, &au_procfs_plm_op);
28998 +       if (unlikely(!entry))
28999 +               goto out_dir;
29000 +
29001 +       err = 0;
29002 +       goto out; /* success */
29003 +
29004 +
29005 +out_dir:
29006 +       remove_proc_entry(AUFS_PLINK_MAINT_DIR, NULL);
29007 +out:
29008 +       return err;
29009 +}
29010 diff -urN /usr/share/empty/fs/aufs/rdu.c linux/fs/aufs/rdu.c
29011 --- /usr/share/empty/fs/aufs/rdu.c      1970-01-01 01:00:00.000000000 +0100
29012 +++ linux/fs/aufs/rdu.c 2022-03-21 14:49:05.729966343 +0100
29013 @@ -0,0 +1,384 @@
29014 +// SPDX-License-Identifier: GPL-2.0
29015 +/*
29016 + * Copyright (C) 2005-2021 Junjiro R. Okajima
29017 + *
29018 + * This program, aufs is free software; you can redistribute it and/or modify
29019 + * it under the terms of the GNU General Public License as published by
29020 + * the Free Software Foundation; either version 2 of the License, or
29021 + * (at your option) any later version.
29022 + *
29023 + * This program is distributed in the hope that it will be useful,
29024 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
29025 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29026 + * GNU General Public License for more details.
29027 + *
29028 + * You should have received a copy of the GNU General Public License
29029 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
29030 + */
29031 +
29032 +/*
29033 + * readdir in userspace.
29034 + */
29035 +
29036 +#include <linux/compat.h>
29037 +#include <linux/fs_stack.h>
29038 +#include <linux/security.h>
29039 +#include "aufs.h"
29040 +
29041 +/* bits for struct aufs_rdu.flags */
29042 +#define        AuRdu_CALLED    1
29043 +#define        AuRdu_CONT      (1 << 1)
29044 +#define        AuRdu_FULL      (1 << 2)
29045 +#define au_ftest_rdu(flags, name)      ((flags) & AuRdu_##name)
29046 +#define au_fset_rdu(flags, name) \
29047 +       do { (flags) |= AuRdu_##name; } while (0)
29048 +#define au_fclr_rdu(flags, name) \
29049 +       do { (flags) &= ~AuRdu_##name; } while (0)
29050 +
29051 +struct au_rdu_arg {
29052 +       struct dir_context              ctx;
29053 +       struct aufs_rdu                 *rdu;
29054 +       union au_rdu_ent_ul             ent;
29055 +       unsigned long                   end;
29056 +
29057 +       struct super_block              *sb;
29058 +       int                             err;
29059 +};
29060 +
29061 +static int au_rdu_fill(struct dir_context *ctx, const char *name, int nlen,
29062 +                      loff_t offset, u64 h_ino, unsigned int d_type)
29063 +{
29064 +       int err, len;
29065 +       struct au_rdu_arg *arg = container_of(ctx, struct au_rdu_arg, ctx);
29066 +       struct aufs_rdu *rdu = arg->rdu;
29067 +       struct au_rdu_ent ent;
29068 +
29069 +       err = 0;
29070 +       arg->err = 0;
29071 +       au_fset_rdu(rdu->cookie.flags, CALLED);
29072 +       len = au_rdu_len(nlen);
29073 +       if (arg->ent.ul + len  < arg->end) {
29074 +               ent.ino = h_ino;
29075 +               ent.bindex = rdu->cookie.bindex;
29076 +               ent.type = d_type;
29077 +               ent.nlen = nlen;
29078 +               if (unlikely(nlen > AUFS_MAX_NAMELEN))
29079 +                       ent.type = DT_UNKNOWN;
29080 +
29081 +               /* unnecessary to support mmap_sem since this is a dir */
29082 +               err = -EFAULT;
29083 +               if (copy_to_user(arg->ent.e, &ent, sizeof(ent)))
29084 +                       goto out;
29085 +               if (copy_to_user(arg->ent.e->name, name, nlen))
29086 +                       goto out;
29087 +               /* the terminating NULL */
29088 +               if (__put_user(0, arg->ent.e->name + nlen))
29089 +                       goto out;
29090 +               err = 0;
29091 +               /* AuDbg("%p, %.*s\n", arg->ent.p, nlen, name); */
29092 +               arg->ent.ul += len;
29093 +               rdu->rent++;
29094 +       } else {
29095 +               err = -EFAULT;
29096 +               au_fset_rdu(rdu->cookie.flags, FULL);
29097 +               rdu->full = 1;
29098 +               rdu->tail = arg->ent;
29099 +       }
29100 +
29101 +out:
29102 +       /* AuTraceErr(err); */
29103 +       return err;
29104 +}
29105 +
29106 +static int au_rdu_do(struct file *h_file, struct au_rdu_arg *arg)
29107 +{
29108 +       int err;
29109 +       loff_t offset;
29110 +       struct au_rdu_cookie *cookie = &arg->rdu->cookie;
29111 +
29112 +       /* we don't have to care (FMODE_32BITHASH | FMODE_64BITHASH) for ext4 */
29113 +       offset = vfsub_llseek(h_file, cookie->h_pos, SEEK_SET);
29114 +       err = offset;
29115 +       if (unlikely(offset != cookie->h_pos))
29116 +               goto out;
29117 +
29118 +       err = 0;
29119 +       do {
29120 +               arg->err = 0;
29121 +               au_fclr_rdu(cookie->flags, CALLED);
29122 +               /* smp_mb(); */
29123 +               err = vfsub_iterate_dir(h_file, &arg->ctx);
29124 +               if (err >= 0)
29125 +                       err = arg->err;
29126 +       } while (!err
29127 +                && au_ftest_rdu(cookie->flags, CALLED)
29128 +                && !au_ftest_rdu(cookie->flags, FULL));
29129 +       cookie->h_pos = h_file->f_pos;
29130 +
29131 +out:
29132 +       AuTraceErr(err);
29133 +       return err;
29134 +}
29135 +
29136 +static int au_rdu(struct file *file, struct aufs_rdu *rdu)
29137 +{
29138 +       int err;
29139 +       aufs_bindex_t bbot;
29140 +       struct au_rdu_arg arg = {
29141 +               .ctx = {
29142 +                       .actor = au_rdu_fill
29143 +               }
29144 +       };
29145 +       struct dentry *dentry;
29146 +       struct inode *inode;
29147 +       struct file *h_file;
29148 +       struct au_rdu_cookie *cookie = &rdu->cookie;
29149 +
29150 +       /* VERIFY_WRITE */
29151 +       err = !access_ok(rdu->ent.e, rdu->sz);
29152 +       if (unlikely(err)) {
29153 +               err = -EFAULT;
29154 +               AuTraceErr(err);
29155 +               goto out;
29156 +       }
29157 +       rdu->rent = 0;
29158 +       rdu->tail = rdu->ent;
29159 +       rdu->full = 0;
29160 +       arg.rdu = rdu;
29161 +       arg.ent = rdu->ent;
29162 +       arg.end = arg.ent.ul;
29163 +       arg.end += rdu->sz;
29164 +
29165 +       err = -ENOTDIR;
29166 +       if (unlikely(!file->f_op->iterate && !file->f_op->iterate_shared))
29167 +               goto out;
29168 +
29169 +       err = security_file_permission(file, MAY_READ);
29170 +       AuTraceErr(err);
29171 +       if (unlikely(err))
29172 +               goto out;
29173 +
29174 +       dentry = file->f_path.dentry;
29175 +       inode = d_inode(dentry);
29176 +       inode_lock_shared(inode);
29177 +
29178 +       arg.sb = inode->i_sb;
29179 +       err = si_read_lock(arg.sb, AuLock_FLUSH | AuLock_NOPLM);
29180 +       if (unlikely(err))
29181 +               goto out_mtx;
29182 +       err = au_alive_dir(dentry);
29183 +       if (unlikely(err))
29184 +               goto out_si;
29185 +       /* todo: reval? */
29186 +       fi_read_lock(file);
29187 +
29188 +       err = -EAGAIN;
29189 +       if (unlikely(au_ftest_rdu(cookie->flags, CONT)
29190 +                    && cookie->generation != au_figen(file)))
29191 +               goto out_unlock;
29192 +
29193 +       err = 0;
29194 +       if (!rdu->blk) {
29195 +               rdu->blk = au_sbi(arg.sb)->si_rdblk;
29196 +               if (!rdu->blk)
29197 +                       rdu->blk = au_dir_size(file, /*dentry*/NULL);
29198 +       }
29199 +       bbot = au_fbtop(file);
29200 +       if (cookie->bindex < bbot)
29201 +               cookie->bindex = bbot;
29202 +       bbot = au_fbbot_dir(file);
29203 +       /* AuDbg("b%d, b%d\n", cookie->bindex, bbot); */
29204 +       for (; !err && cookie->bindex <= bbot;
29205 +            cookie->bindex++, cookie->h_pos = 0) {
29206 +               h_file = au_hf_dir(file, cookie->bindex);
29207 +               if (!h_file)
29208 +                       continue;
29209 +
29210 +               au_fclr_rdu(cookie->flags, FULL);
29211 +               err = au_rdu_do(h_file, &arg);
29212 +               AuTraceErr(err);
29213 +               if (unlikely(au_ftest_rdu(cookie->flags, FULL) || err))
29214 +                       break;
29215 +       }
29216 +       AuDbg("rent %llu\n", rdu->rent);
29217 +
29218 +       if (!err && !au_ftest_rdu(cookie->flags, CONT)) {
29219 +               rdu->shwh = !!au_opt_test(au_sbi(arg.sb)->si_mntflags, SHWH);
29220 +               au_fset_rdu(cookie->flags, CONT);
29221 +               cookie->generation = au_figen(file);
29222 +       }
29223 +
29224 +       ii_read_lock_child(inode);
29225 +       fsstack_copy_attr_atime(inode, au_h_iptr(inode, au_ibtop(inode)));
29226 +       ii_read_unlock(inode);
29227 +
29228 +out_unlock:
29229 +       fi_read_unlock(file);
29230 +out_si:
29231 +       si_read_unlock(arg.sb);
29232 +out_mtx:
29233 +       inode_unlock_shared(inode);
29234 +out:
29235 +       AuTraceErr(err);
29236 +       return err;
29237 +}
29238 +
29239 +static int au_rdu_ino(struct file *file, struct aufs_rdu *rdu)
29240 +{
29241 +       int err;
29242 +       ino_t ino;
29243 +       unsigned long long nent;
29244 +       union au_rdu_ent_ul *u;
29245 +       struct au_rdu_ent ent;
29246 +       struct super_block *sb;
29247 +
29248 +       err = 0;
29249 +       nent = rdu->nent;
29250 +       u = &rdu->ent;
29251 +       sb = file->f_path.dentry->d_sb;
29252 +       si_read_lock(sb, AuLock_FLUSH);
29253 +       while (nent-- > 0) {
29254 +               /* unnecessary to support mmap_sem since this is a dir */
29255 +               err = copy_from_user(&ent, u->e, sizeof(ent));
29256 +               if (!err)
29257 +                       /* VERIFY_WRITE */
29258 +                       err = !access_ok(&u->e->ino, sizeof(ino));
29259 +               if (unlikely(err)) {
29260 +                       err = -EFAULT;
29261 +                       AuTraceErr(err);
29262 +                       break;
29263 +               }
29264 +
29265 +               /* AuDbg("b%d, i%llu\n", ent.bindex, ent.ino); */
29266 +               if (!ent.wh)
29267 +                       err = au_ino(sb, ent.bindex, ent.ino, ent.type, &ino);
29268 +               else
29269 +                       err = au_wh_ino(sb, ent.bindex, ent.ino, ent.type,
29270 +                                       &ino);
29271 +               if (unlikely(err)) {
29272 +                       AuTraceErr(err);
29273 +                       break;
29274 +               }
29275 +
29276 +               err = __put_user(ino, &u->e->ino);
29277 +               if (unlikely(err)) {
29278 +                       err = -EFAULT;
29279 +                       AuTraceErr(err);
29280 +                       break;
29281 +               }
29282 +               u->ul += au_rdu_len(ent.nlen);
29283 +       }
29284 +       si_read_unlock(sb);
29285 +
29286 +       return err;
29287 +}
29288 +
29289 +/* ---------------------------------------------------------------------- */
29290 +
29291 +static int au_rdu_verify(struct aufs_rdu *rdu)
29292 +{
29293 +       AuDbg("rdu{%llu, %p, %u | %u | %llu, %u, %u | "
29294 +             "%llu, b%d, 0x%x, g%u}\n",
29295 +             rdu->sz, rdu->ent.e, rdu->verify[AufsCtlRduV_SZ],
29296 +             rdu->blk,
29297 +             rdu->rent, rdu->shwh, rdu->full,
29298 +             rdu->cookie.h_pos, rdu->cookie.bindex, rdu->cookie.flags,
29299 +             rdu->cookie.generation);
29300 +
29301 +       if (rdu->verify[AufsCtlRduV_SZ] == sizeof(*rdu))
29302 +               return 0;
29303 +
29304 +       AuDbg("%u:%u\n",
29305 +             rdu->verify[AufsCtlRduV_SZ], (unsigned int)sizeof(*rdu));
29306 +       return -EINVAL;
29307 +}
29308 +
29309 +long au_rdu_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
29310 +{
29311 +       long err, e;
29312 +       struct aufs_rdu rdu;
29313 +       void __user *p = (void __user *)arg;
29314 +
29315 +       err = copy_from_user(&rdu, p, sizeof(rdu));
29316 +       if (unlikely(err)) {
29317 +               err = -EFAULT;
29318 +               AuTraceErr(err);
29319 +               goto out;
29320 +       }
29321 +       err = au_rdu_verify(&rdu);
29322 +       if (unlikely(err))
29323 +               goto out;
29324 +
29325 +       switch (cmd) {
29326 +       case AUFS_CTL_RDU:
29327 +               err = au_rdu(file, &rdu);
29328 +               if (unlikely(err))
29329 +                       break;
29330 +
29331 +               e = copy_to_user(p, &rdu, sizeof(rdu));
29332 +               if (unlikely(e)) {
29333 +                       err = -EFAULT;
29334 +                       AuTraceErr(err);
29335 +               }
29336 +               break;
29337 +       case AUFS_CTL_RDU_INO:
29338 +               err = au_rdu_ino(file, &rdu);
29339 +               break;
29340 +
29341 +       default:
29342 +               /* err = -ENOTTY; */
29343 +               err = -EINVAL;
29344 +       }
29345 +
29346 +out:
29347 +       AuTraceErr(err);
29348 +       return err;
29349 +}
29350 +
29351 +#ifdef CONFIG_COMPAT
29352 +long au_rdu_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
29353 +{
29354 +       long err, e;
29355 +       struct aufs_rdu rdu;
29356 +       void __user *p = compat_ptr(arg);
29357 +
29358 +       /* todo: get_user()? */
29359 +       err = copy_from_user(&rdu, p, sizeof(rdu));
29360 +       if (unlikely(err)) {
29361 +               err = -EFAULT;
29362 +               AuTraceErr(err);
29363 +               goto out;
29364 +       }
29365 +       rdu.ent.e = compat_ptr(rdu.ent.ul);
29366 +       err = au_rdu_verify(&rdu);
29367 +       if (unlikely(err))
29368 +               goto out;
29369 +
29370 +       switch (cmd) {
29371 +       case AUFS_CTL_RDU:
29372 +               err = au_rdu(file, &rdu);
29373 +               if (unlikely(err))
29374 +                       break;
29375 +
29376 +               rdu.ent.ul = ptr_to_compat(rdu.ent.e);
29377 +               rdu.tail.ul = ptr_to_compat(rdu.tail.e);
29378 +               e = copy_to_user(p, &rdu, sizeof(rdu));
29379 +               if (unlikely(e)) {
29380 +                       err = -EFAULT;
29381 +                       AuTraceErr(err);
29382 +               }
29383 +               break;
29384 +       case AUFS_CTL_RDU_INO:
29385 +               err = au_rdu_ino(file, &rdu);
29386 +               break;
29387 +
29388 +       default:
29389 +               /* err = -ENOTTY; */
29390 +               err = -EINVAL;
29391 +       }
29392 +
29393 +out:
29394 +       AuTraceErr(err);
29395 +       return err;
29396 +}
29397 +#endif
29398 diff -urN /usr/share/empty/fs/aufs/rwsem.h linux/fs/aufs/rwsem.h
29399 --- /usr/share/empty/fs/aufs/rwsem.h    1970-01-01 01:00:00.000000000 +0100
29400 +++ linux/fs/aufs/rwsem.h       2022-03-21 14:49:05.729966343 +0100
29401 @@ -0,0 +1,85 @@
29402 +/* SPDX-License-Identifier: GPL-2.0 */
29403 +/*
29404 + * Copyright (C) 2005-2021 Junjiro R. Okajima
29405 + *
29406 + * This program, aufs is free software; you can redistribute it and/or modify
29407 + * it under the terms of the GNU General Public License as published by
29408 + * the Free Software Foundation; either version 2 of the License, or
29409 + * (at your option) any later version.
29410 + *
29411 + * This program is distributed in the hope that it will be useful,
29412 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
29413 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29414 + * GNU General Public License for more details.
29415 + *
29416 + * You should have received a copy of the GNU General Public License
29417 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
29418 + */
29419 +
29420 +/*
29421 + * simple read-write semaphore wrappers
29422 + */
29423 +
29424 +#ifndef __AUFS_RWSEM_H__
29425 +#define __AUFS_RWSEM_H__
29426 +
29427 +#ifdef __KERNEL__
29428 +
29429 +#include "debug.h"
29430 +
29431 +/* in the future, the name 'au_rwsem' will be totally gone */
29432 +#define au_rwsem       rw_semaphore
29433 +
29434 +/* to debug easier, do not make them inlined functions */
29435 +#define AuRwMustNoWaiters(rw)  AuDebugOn(rwsem_is_contended(rw))
29436 +
29437 +#ifdef CONFIG_LOCKDEP
29438 +/* rwsem_is_locked() is unusable */
29439 +#define AuRwMustReadLock(rw)   AuDebugOn(IS_ENABLED(CONFIG_LOCKDEP)    \
29440 +                                         && !lockdep_recursing(current) \
29441 +                                         && debug_locks                \
29442 +                                         && !lockdep_is_held_type(rw, 1))
29443 +#define AuRwMustWriteLock(rw)  AuDebugOn(IS_ENABLED(CONFIG_LOCKDEP)    \
29444 +                                         && !lockdep_recursing(current) \
29445 +                                         && debug_locks                \
29446 +                                         && !lockdep_is_held_type(rw, 0))
29447 +#define AuRwMustAnyLock(rw)    AuDebugOn(IS_ENABLED(CONFIG_LOCKDEP)    \
29448 +                                         && !lockdep_recursing(current) \
29449 +                                         && debug_locks                \
29450 +                                         && !lockdep_is_held(rw))
29451 +#define AuRwDestroy(rw)                AuDebugOn(IS_ENABLED(CONFIG_LOCKDEP)    \
29452 +                                         && !lockdep_recursing(current) \
29453 +                                         && debug_locks                \
29454 +                                         && lockdep_is_held(rw))
29455 +#else
29456 +#define AuRwMustReadLock(rw)   do {} while (0)
29457 +#define AuRwMustWriteLock(rw)  do {} while (0)
29458 +#define AuRwMustAnyLock(rw)    do {} while (0)
29459 +#define AuRwDestroy(rw)                do {} while (0)
29460 +#endif
29461 +
29462 +#define au_rw_init(rw) init_rwsem(rw)
29463 +
29464 +#define au_rw_init_wlock(rw) do {              \
29465 +               au_rw_init(rw);                 \
29466 +               down_write(rw);                 \
29467 +       } while (0)
29468 +
29469 +#define au_rw_init_wlock_nested(rw, lsc) do {  \
29470 +               au_rw_init(rw);                 \
29471 +               down_write_nested(rw, lsc);     \
29472 +       } while (0)
29473 +
29474 +#define au_rw_read_lock(rw)            down_read(rw)
29475 +#define au_rw_read_lock_nested(rw, lsc)        down_read_nested(rw, lsc)
29476 +#define au_rw_read_unlock(rw)          up_read(rw)
29477 +#define au_rw_dgrade_lock(rw)          downgrade_write(rw)
29478 +#define au_rw_write_lock(rw)           down_write(rw)
29479 +#define au_rw_write_lock_nested(rw, lsc) down_write_nested(rw, lsc)
29480 +#define au_rw_write_unlock(rw)         up_write(rw)
29481 +/* why is not _nested version defined? */
29482 +#define au_rw_read_trylock(rw)         down_read_trylock(rw)
29483 +#define au_rw_write_trylock(rw)                down_write_trylock(rw)
29484 +
29485 +#endif /* __KERNEL__ */
29486 +#endif /* __AUFS_RWSEM_H__ */
29487 diff -urN /usr/share/empty/fs/aufs/sbinfo.c linux/fs/aufs/sbinfo.c
29488 --- /usr/share/empty/fs/aufs/sbinfo.c   1970-01-01 01:00:00.000000000 +0100
29489 +++ linux/fs/aufs/sbinfo.c      2022-03-21 14:49:05.729966343 +0100
29490 @@ -0,0 +1,316 @@
29491 +// SPDX-License-Identifier: GPL-2.0
29492 +/*
29493 + * Copyright (C) 2005-2021 Junjiro R. Okajima
29494 + *
29495 + * This program, aufs is free software; you can redistribute it and/or modify
29496 + * it under the terms of the GNU General Public License as published by
29497 + * the Free Software Foundation; either version 2 of the License, or
29498 + * (at your option) any later version.
29499 + *
29500 + * This program is distributed in the hope that it will be useful,
29501 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
29502 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29503 + * GNU General Public License for more details.
29504 + *
29505 + * You should have received a copy of the GNU General Public License
29506 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
29507 + */
29508 +
29509 +/*
29510 + * superblock private data
29511 + */
29512 +
29513 +#include <linux/iversion.h>
29514 +#include "aufs.h"
29515 +
29516 +/*
29517 + * they are necessary regardless sysfs is disabled.
29518 + */
29519 +void au_si_free(struct kobject *kobj)
29520 +{
29521 +       int i;
29522 +       struct au_sbinfo *sbinfo;
29523 +       char *locked __maybe_unused; /* debug only */
29524 +
29525 +       sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
29526 +       for (i = 0; i < AuPlink_NHASH; i++)
29527 +               AuDebugOn(!hlist_bl_empty(sbinfo->si_plink + i));
29528 +       AuDebugOn(atomic_read(&sbinfo->si_nowait.nw_len));
29529 +
29530 +       AuLCntZero(au_lcnt_read(&sbinfo->si_ninodes, /*do_rev*/0));
29531 +       au_lcnt_fin(&sbinfo->si_ninodes, /*do_sync*/0);
29532 +       AuLCntZero(au_lcnt_read(&sbinfo->si_nfiles, /*do_rev*/0));
29533 +       au_lcnt_fin(&sbinfo->si_nfiles, /*do_sync*/0);
29534 +
29535 +       dbgaufs_si_fin(sbinfo);
29536 +       au_rw_write_lock(&sbinfo->si_rwsem);
29537 +       au_br_free(sbinfo);
29538 +       au_rw_write_unlock(&sbinfo->si_rwsem);
29539 +
29540 +       au_kfree_try_rcu(sbinfo->si_branch);
29541 +       mutex_destroy(&sbinfo->si_xib_mtx);
29542 +       AuRwDestroy(&sbinfo->si_rwsem);
29543 +
29544 +       au_lcnt_wait_for_fin(&sbinfo->si_ninodes);
29545 +       /* si_nfiles is waited too */
29546 +       au_kfree_rcu(sbinfo);
29547 +}
29548 +
29549 +struct au_sbinfo *au_si_alloc(struct super_block *sb)
29550 +{
29551 +       struct au_sbinfo *sbinfo;
29552 +       int err, i;
29553 +
29554 +       err = -ENOMEM;
29555 +       sbinfo = kzalloc(sizeof(*sbinfo), GFP_NOFS);
29556 +       if (unlikely(!sbinfo))
29557 +               goto out;
29558 +
29559 +       /* will be reallocated separately */
29560 +       sbinfo->si_branch = kzalloc(sizeof(*sbinfo->si_branch), GFP_NOFS);
29561 +       if (unlikely(!sbinfo->si_branch))
29562 +               goto out_sbinfo;
29563 +
29564 +       err = sysaufs_si_init(sbinfo);
29565 +       if (!err) {
29566 +               dbgaufs_si_null(sbinfo);
29567 +               err = dbgaufs_si_init(sbinfo);
29568 +               if (unlikely(err))
29569 +                       kobject_put(&sbinfo->si_kobj);
29570 +       }
29571 +       if (unlikely(err))
29572 +               goto out_br;
29573 +
29574 +       au_nwt_init(&sbinfo->si_nowait);
29575 +       au_rw_init_wlock(&sbinfo->si_rwsem);
29576 +
29577 +       au_lcnt_init(&sbinfo->si_ninodes, /*release*/NULL);
29578 +       au_lcnt_init(&sbinfo->si_nfiles, /*release*/NULL);
29579 +
29580 +       sbinfo->si_bbot = -1;
29581 +       sbinfo->si_last_br_id = AUFS_BRANCH_MAX / 2;
29582 +
29583 +       sbinfo->si_wbr_copyup = AuWbrCopyup_Def;
29584 +       sbinfo->si_wbr_create = AuWbrCreate_Def;
29585 +       sbinfo->si_wbr_copyup_ops = au_wbr_copyup_ops + sbinfo->si_wbr_copyup;
29586 +       sbinfo->si_wbr_create_ops = au_wbr_create_ops + sbinfo->si_wbr_create;
29587 +
29588 +       au_fhsm_init(sbinfo);
29589 +
29590 +       sbinfo->si_mntflags = au_opts_plink(AuOpt_Def);
29591 +
29592 +       sbinfo->si_xino_jiffy = jiffies;
29593 +       sbinfo->si_xino_expire
29594 +               = msecs_to_jiffies(AUFS_XINO_DEF_SEC * MSEC_PER_SEC);
29595 +       mutex_init(&sbinfo->si_xib_mtx);
29596 +       /* leave si_xib_last_pindex and si_xib_next_bit */
29597 +
29598 +       INIT_HLIST_BL_HEAD(&sbinfo->si_aopen);
29599 +
29600 +       sbinfo->si_rdcache = msecs_to_jiffies(AUFS_RDCACHE_DEF * MSEC_PER_SEC);
29601 +       sbinfo->si_rdblk = AUFS_RDBLK_DEF;
29602 +       sbinfo->si_rdhash = AUFS_RDHASH_DEF;
29603 +       sbinfo->si_dirwh = AUFS_DIRWH_DEF;
29604 +
29605 +       for (i = 0; i < AuPlink_NHASH; i++)
29606 +               INIT_HLIST_BL_HEAD(sbinfo->si_plink + i);
29607 +       init_waitqueue_head(&sbinfo->si_plink_wq);
29608 +       spin_lock_init(&sbinfo->si_plink_maint_lock);
29609 +
29610 +       INIT_HLIST_BL_HEAD(&sbinfo->si_files);
29611 +
29612 +       /* with getattr by default */
29613 +       sbinfo->si_iop_array = aufs_iop;
29614 +
29615 +       /* leave other members for sysaufs and si_mnt. */
29616 +       sbinfo->si_sb = sb;
29617 +       if (sb) {
29618 +               sb->s_fs_info = sbinfo;
29619 +               si_pid_set(sb);
29620 +       }
29621 +       return sbinfo; /* success */
29622 +
29623 +out_br:
29624 +       au_kfree_try_rcu(sbinfo->si_branch);
29625 +out_sbinfo:
29626 +       au_kfree_rcu(sbinfo);
29627 +out:
29628 +       return ERR_PTR(err);
29629 +}
29630 +
29631 +int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr, int may_shrink)
29632 +{
29633 +       int err, sz;
29634 +       struct au_branch **brp;
29635 +
29636 +       AuRwMustWriteLock(&sbinfo->si_rwsem);
29637 +
29638 +       err = -ENOMEM;
29639 +       sz = sizeof(*brp) * (sbinfo->si_bbot + 1);
29640 +       if (unlikely(!sz))
29641 +               sz = sizeof(*brp);
29642 +       brp = au_kzrealloc(sbinfo->si_branch, sz, sizeof(*brp) * nbr, GFP_NOFS,
29643 +                          may_shrink);
29644 +       if (brp) {
29645 +               sbinfo->si_branch = brp;
29646 +               err = 0;
29647 +       }
29648 +
29649 +       return err;
29650 +}
29651 +
29652 +/* ---------------------------------------------------------------------- */
29653 +
29654 +unsigned int au_sigen_inc(struct super_block *sb)
29655 +{
29656 +       unsigned int gen;
29657 +       struct inode *inode;
29658 +
29659 +       SiMustWriteLock(sb);
29660 +
29661 +       gen = ++au_sbi(sb)->si_generation;
29662 +       au_update_digen(sb->s_root);
29663 +       inode = d_inode(sb->s_root);
29664 +       au_update_iigen(inode, /*half*/0);
29665 +       inode_inc_iversion(inode);
29666 +       return gen;
29667 +}
29668 +
29669 +aufs_bindex_t au_new_br_id(struct super_block *sb)
29670 +{
29671 +       aufs_bindex_t br_id;
29672 +       int i;
29673 +       struct au_sbinfo *sbinfo;
29674 +
29675 +       SiMustWriteLock(sb);
29676 +
29677 +       sbinfo = au_sbi(sb);
29678 +       for (i = 0; i <= AUFS_BRANCH_MAX; i++) {
29679 +               br_id = ++sbinfo->si_last_br_id;
29680 +               AuDebugOn(br_id < 0);
29681 +               if (br_id && au_br_index(sb, br_id) < 0)
29682 +                       return br_id;
29683 +       }
29684 +
29685 +       return -1;
29686 +}
29687 +
29688 +/* ---------------------------------------------------------------------- */
29689 +
29690 +/* it is ok that new 'nwt' tasks are appended while we are sleeping */
29691 +int si_read_lock(struct super_block *sb, int flags)
29692 +{
29693 +       int err;
29694 +
29695 +       err = 0;
29696 +       if (au_ftest_lock(flags, FLUSH))
29697 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
29698 +
29699 +       si_noflush_read_lock(sb);
29700 +       err = au_plink_maint(sb, flags);
29701 +       if (unlikely(err))
29702 +               si_read_unlock(sb);
29703 +
29704 +       return err;
29705 +}
29706 +
29707 +int si_write_lock(struct super_block *sb, int flags)
29708 +{
29709 +       int err;
29710 +
29711 +       if (au_ftest_lock(flags, FLUSH))
29712 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
29713 +
29714 +       si_noflush_write_lock(sb);
29715 +       err = au_plink_maint(sb, flags);
29716 +       if (unlikely(err))
29717 +               si_write_unlock(sb);
29718 +
29719 +       return err;
29720 +}
29721 +
29722 +/* dentry and super_block lock. call at entry point */
29723 +int aufs_read_lock(struct dentry *dentry, int flags)
29724 +{
29725 +       int err;
29726 +       struct super_block *sb;
29727 +
29728 +       sb = dentry->d_sb;
29729 +       err = si_read_lock(sb, flags);
29730 +       if (unlikely(err))
29731 +               goto out;
29732 +
29733 +       if (au_ftest_lock(flags, DW))
29734 +               di_write_lock_child(dentry);
29735 +       else
29736 +               di_read_lock_child(dentry, flags);
29737 +
29738 +       if (au_ftest_lock(flags, GEN)) {
29739 +               err = au_digen_test(dentry, au_sigen(sb));
29740 +               if (!au_opt_test(au_mntflags(sb), UDBA_NONE))
29741 +                       AuDebugOn(!err && au_dbrange_test(dentry));
29742 +               else if (!err)
29743 +                       err = au_dbrange_test(dentry);
29744 +               if (unlikely(err))
29745 +                       aufs_read_unlock(dentry, flags);
29746 +       }
29747 +
29748 +out:
29749 +       return err;
29750 +}
29751 +
29752 +void aufs_read_unlock(struct dentry *dentry, int flags)
29753 +{
29754 +       if (au_ftest_lock(flags, DW))
29755 +               di_write_unlock(dentry);
29756 +       else
29757 +               di_read_unlock(dentry, flags);
29758 +       si_read_unlock(dentry->d_sb);
29759 +}
29760 +
29761 +void aufs_write_lock(struct dentry *dentry)
29762 +{
29763 +       si_write_lock(dentry->d_sb, AuLock_FLUSH | AuLock_NOPLMW);
29764 +       di_write_lock_child(dentry);
29765 +}
29766 +
29767 +void aufs_write_unlock(struct dentry *dentry)
29768 +{
29769 +       di_write_unlock(dentry);
29770 +       si_write_unlock(dentry->d_sb);
29771 +}
29772 +
29773 +int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags)
29774 +{
29775 +       int err;
29776 +       unsigned int sigen;
29777 +       struct super_block *sb;
29778 +
29779 +       sb = d1->d_sb;
29780 +       err = si_read_lock(sb, flags);
29781 +       if (unlikely(err))
29782 +               goto out;
29783 +
29784 +       di_write_lock2_child(d1, d2, au_ftest_lock(flags, DIRS));
29785 +
29786 +       if (au_ftest_lock(flags, GEN)) {
29787 +               sigen = au_sigen(sb);
29788 +               err = au_digen_test(d1, sigen);
29789 +               AuDebugOn(!err && au_dbrange_test(d1));
29790 +               if (!err) {
29791 +                       err = au_digen_test(d2, sigen);
29792 +                       AuDebugOn(!err && au_dbrange_test(d2));
29793 +               }
29794 +               if (unlikely(err))
29795 +                       aufs_read_and_write_unlock2(d1, d2);
29796 +       }
29797 +
29798 +out:
29799 +       return err;
29800 +}
29801 +
29802 +void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2)
29803 +{
29804 +       di_write_unlock2(d1, d2);
29805 +       si_read_unlock(d1->d_sb);
29806 +}
29807 diff -urN /usr/share/empty/fs/aufs/super.c linux/fs/aufs/super.c
29808 --- /usr/share/empty/fs/aufs/super.c    1970-01-01 01:00:00.000000000 +0100
29809 +++ linux/fs/aufs/super.c       2022-05-23 09:44:37.729839166 +0200
29810 @@ -0,0 +1,871 @@
29811 +// SPDX-License-Identifier: GPL-2.0
29812 +/*
29813 + * Copyright (C) 2005-2021 Junjiro R. Okajima
29814 + *
29815 + * This program, aufs is free software; you can redistribute it and/or modify
29816 + * it under the terms of the GNU General Public License as published by
29817 + * the Free Software Foundation; either version 2 of the License, or
29818 + * (at your option) any later version.
29819 + *
29820 + * This program is distributed in the hope that it will be useful,
29821 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
29822 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
29823 + * GNU General Public License for more details.
29824 + *
29825 + * You should have received a copy of the GNU General Public License
29826 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
29827 + */
29828 +
29829 +/*
29830 + * mount and super_block operations
29831 + */
29832 +
29833 +#include <linux/iversion.h>
29834 +#include <linux/mm.h>
29835 +#include <linux/seq_file.h>
29836 +#include <linux/statfs.h>
29837 +#include <linux/vmalloc.h>
29838 +#include "aufs.h"
29839 +
29840 +/*
29841 + * super_operations
29842 + */
29843 +static struct inode *aufs_alloc_inode(struct super_block *sb __maybe_unused)
29844 +{
29845 +       struct au_icntnr *c;
29846 +
29847 +       c = au_cache_alloc_icntnr(sb);
29848 +       if (c) {
29849 +               au_icntnr_init(c);
29850 +               inode_set_iversion(&c->vfs_inode, 1); /* sigen(sb); */
29851 +               c->iinfo.ii_hinode = NULL;
29852 +               return &c->vfs_inode;
29853 +       }
29854 +       return NULL;
29855 +}
29856 +
29857 +static void aufs_destroy_inode(struct inode *inode)
29858 +{
29859 +       if (!au_is_bad_inode(inode))
29860 +               au_iinfo_fin(inode);
29861 +}
29862 +
29863 +static void aufs_free_inode(struct inode *inode)
29864 +{
29865 +       au_cache_free_icntnr(container_of(inode, struct au_icntnr, vfs_inode));
29866 +}
29867 +
29868 +struct inode *au_iget_locked(struct super_block *sb, ino_t ino)
29869 +{
29870 +       struct inode *inode;
29871 +       int err;
29872 +
29873 +       inode = iget_locked(sb, ino);
29874 +       if (unlikely(!inode)) {
29875 +               inode = ERR_PTR(-ENOMEM);
29876 +               goto out;
29877 +       }
29878 +       if (!(inode->i_state & I_NEW))
29879 +               goto out;
29880 +
29881 +       err = au_xigen_new(inode);
29882 +       if (!err)
29883 +               err = au_iinfo_init(inode);
29884 +       if (!err)
29885 +               inode_inc_iversion(inode);
29886 +       else {
29887 +               iget_failed(inode);
29888 +               inode = ERR_PTR(err);
29889 +       }
29890 +
29891 +out:
29892 +       /* never return NULL */
29893 +       AuDebugOn(!inode);
29894 +       AuTraceErrPtr(inode);
29895 +       return inode;
29896 +}
29897 +
29898 +/* lock free root dinfo */
29899 +static int au_show_brs(struct seq_file *seq, struct super_block *sb)
29900 +{
29901 +       int err;
29902 +       aufs_bindex_t bindex, bbot;
29903 +       struct path path;
29904 +       struct au_hdentry *hdp;
29905 +       struct au_branch *br;
29906 +       au_br_perm_str_t perm;
29907 +
29908 +       err = 0;
29909 +       bbot = au_sbbot(sb);
29910 +       bindex = 0;
29911 +       hdp = au_hdentry(au_di(sb->s_root), bindex);
29912 +       for (; !err && bindex <= bbot; bindex++, hdp++) {
29913 +               br = au_sbr(sb, bindex);
29914 +               path.mnt = au_br_mnt(br);
29915 +               path.dentry = hdp->hd_dentry;
29916 +               err = au_seq_path(seq, &path);
29917 +               if (!err) {
29918 +                       au_optstr_br_perm(&perm, br->br_perm);
29919 +                       seq_printf(seq, "=%s", perm.a);
29920 +                       if (bindex != bbot)
29921 +                               seq_putc(seq, ':');
29922 +               }
29923 +       }
29924 +       if (unlikely(err || seq_has_overflowed(seq)))
29925 +               err = -E2BIG;
29926 +
29927 +       return err;
29928 +}
29929 +
29930 +static void au_gen_fmt(char *fmt, int len __maybe_unused, const char *pat,
29931 +                      const char *append)
29932 +{
29933 +       char *p;
29934 +
29935 +       p = fmt;
29936 +       while (*pat != ':')
29937 +               *p++ = *pat++;
29938 +       *p++ = *pat++;
29939 +       strcpy(p, append);
29940 +       AuDebugOn(strlen(fmt) >= len);
29941 +}
29942 +
29943 +static void au_show_wbr_create(struct seq_file *m, int v,
29944 +                              struct au_sbinfo *sbinfo)
29945 +{
29946 +       const char *pat;
29947 +       char fmt[32];
29948 +       struct au_wbr_mfs *mfs;
29949 +
29950 +       AuRwMustAnyLock(&sbinfo->si_rwsem);
29951 +
29952 +       seq_puts(m, ",create=");
29953 +       pat = au_optstr_wbr_create(v);
29954 +       mfs = &sbinfo->si_wbr_mfs;
29955 +       switch (v) {
29956 +       case AuWbrCreate_TDP:
29957 +       case AuWbrCreate_RR:
29958 +       case AuWbrCreate_MFS:
29959 +       case AuWbrCreate_PMFS:
29960 +               seq_puts(m, pat);
29961 +               break;
29962 +       case AuWbrCreate_MFSRR:
29963 +       case AuWbrCreate_TDMFS:
29964 +       case AuWbrCreate_PMFSRR:
29965 +               au_gen_fmt(fmt, sizeof(fmt), pat, "%llu");
29966 +               seq_printf(m, fmt, mfs->mfsrr_watermark);
29967 +               break;
29968 +       case AuWbrCreate_MFSV:
29969 +       case AuWbrCreate_PMFSV:
29970 +               au_gen_fmt(fmt, sizeof(fmt), pat, "%lu");
29971 +               seq_printf(m, fmt,
29972 +                          jiffies_to_msecs(mfs->mfs_expire)
29973 +                          / MSEC_PER_SEC);
29974 +               break;
29975 +       case AuWbrCreate_MFSRRV:
29976 +       case AuWbrCreate_TDMFSV:
29977 +       case AuWbrCreate_PMFSRRV:
29978 +               au_gen_fmt(fmt, sizeof(fmt), pat, "%llu:%lu");
29979 +               seq_printf(m, fmt, mfs->mfsrr_watermark,
29980 +                          jiffies_to_msecs(mfs->mfs_expire) / MSEC_PER_SEC);
29981 +               break;
29982 +       default:
29983 +               BUG();
29984 +       }
29985 +}
29986 +
29987 +static int au_show_xino(struct seq_file *seq, struct super_block *sb)
29988 +{
29989 +#ifdef CONFIG_SYSFS
29990 +       return 0;
29991 +#else
29992 +       int err;
29993 +       const int len = sizeof(AUFS_XINO_FNAME) - 1;
29994 +       aufs_bindex_t bindex, brid;
29995 +       struct qstr *name;
29996 +       struct file *f;
29997 +       struct dentry *d, *h_root;
29998 +       struct au_branch *br;
29999 +
30000 +       AuRwMustAnyLock(&sbinfo->si_rwsem);
30001 +
30002 +       err = 0;
30003 +       f = au_sbi(sb)->si_xib;
30004 +       if (!f)
30005 +               goto out;
30006 +
30007 +       /* stop printing the default xino path on the first writable branch */
30008 +       h_root = NULL;
30009 +       bindex = au_xi_root(sb, f->f_path.dentry);
30010 +       if (bindex >= 0) {
30011 +               br = au_sbr_sb(sb, bindex);
30012 +               h_root = au_br_dentry(br);
30013 +       }
30014 +
30015 +       d = f->f_path.dentry;
30016 +       name = &d->d_name;
30017 +       /* safe ->d_parent because the file is unlinked */
30018 +       if (d->d_parent == h_root
30019 +           && name->len == len
30020 +           && !memcmp(name->name, AUFS_XINO_FNAME, len))
30021 +               goto out;
30022 +
30023 +       seq_puts(seq, ",xino=");
30024 +       err = au_xino_path(seq, f);
30025 +
30026 +out:
30027 +       return err;
30028 +#endif
30029 +}
30030 +
30031 +/* seq_file will re-call me in case of too long string */
30032 +static int aufs_show_options(struct seq_file *m, struct dentry *dentry)
30033 +{
30034 +       int err;
30035 +       unsigned int mnt_flags, v;
30036 +       struct super_block *sb;
30037 +       struct au_sbinfo *sbinfo;
30038 +
30039 +#define AuBool(name, str) do { \
30040 +       v = au_opt_test(mnt_flags, name); \
30041 +       if (v != au_opt_test(AuOpt_Def, name)) \
30042 +               seq_printf(m, ",%s" #str, v ? "" : "no"); \
30043 +} while (0)
30044 +
30045 +#define AuStr(name, str) do { \
30046 +       v = mnt_flags & AuOptMask_##name; \
30047 +       if (v != (AuOpt_Def & AuOptMask_##name)) \
30048 +               seq_printf(m, "," #str "=%s", au_optstr_##str(v)); \
30049 +} while (0)
30050 +
30051 +#define AuUInt(name, str, val) do { \
30052 +       if (val != AUFS_##name##_DEF) \
30053 +               seq_printf(m, "," #str "=%u", val); \
30054 +} while (0)
30055 +
30056 +       sb = dentry->d_sb;
30057 +       if (sb->s_flags & SB_POSIXACL)
30058 +               seq_puts(m, ",acl");
30059 +#if 0 /* reserved for future use */
30060 +       if (sb->s_flags & SB_I_VERSION)
30061 +               seq_puts(m, ",i_version");
30062 +#endif
30063 +
30064 +       /* lock free root dinfo */
30065 +       si_noflush_read_lock(sb);
30066 +       sbinfo = au_sbi(sb);
30067 +       seq_printf(m, ",si=%lx", sysaufs_si_id(sbinfo));
30068 +
30069 +       mnt_flags = au_mntflags(sb);
30070 +       if (au_opt_test(mnt_flags, XINO)) {
30071 +               err = au_show_xino(m, sb);
30072 +               if (unlikely(err))
30073 +                       goto out;
30074 +       } else
30075 +               seq_puts(m, ",noxino");
30076 +
30077 +       AuBool(TRUNC_XINO, trunc_xino);
30078 +       AuStr(UDBA, udba);
30079 +       AuBool(SHWH, shwh);
30080 +       AuBool(PLINK, plink);
30081 +       AuBool(DIO, dio);
30082 +       AuBool(DIRPERM1, dirperm1);
30083 +
30084 +       v = sbinfo->si_wbr_create;
30085 +       if (v != AuWbrCreate_Def)
30086 +               au_show_wbr_create(m, v, sbinfo);
30087 +
30088 +       v = sbinfo->si_wbr_copyup;
30089 +       if (v != AuWbrCopyup_Def)
30090 +               seq_printf(m, ",cpup=%s", au_optstr_wbr_copyup(v));
30091 +
30092 +       v = au_opt_test(mnt_flags, ALWAYS_DIROPQ);
30093 +       if (v != au_opt_test(AuOpt_Def, ALWAYS_DIROPQ))
30094 +               seq_printf(m, ",diropq=%c", v ? 'a' : 'w');
30095 +
30096 +       AuUInt(DIRWH, dirwh, sbinfo->si_dirwh);
30097 +
30098 +       v = jiffies_to_msecs(sbinfo->si_rdcache) / MSEC_PER_SEC;
30099 +       AuUInt(RDCACHE, rdcache, v);
30100 +
30101 +       AuUInt(RDBLK, rdblk, sbinfo->si_rdblk);
30102 +       AuUInt(RDHASH, rdhash, sbinfo->si_rdhash);
30103 +
30104 +       au_fhsm_show(m, sbinfo);
30105 +
30106 +       AuBool(DIRREN, dirren);
30107 +       AuBool(SUM, sum);
30108 +       /* AuBool(SUM_W, wsum); */
30109 +       AuBool(WARN_PERM, warn_perm);
30110 +       AuBool(VERBOSE, verbose);
30111 +
30112 +out:
30113 +       /* be sure to print "br:" last */
30114 +       if (!sysaufs_brs) {
30115 +               seq_puts(m, ",br:");
30116 +               au_show_brs(m, sb);
30117 +       }
30118 +       si_read_unlock(sb);
30119 +       return 0;
30120 +
30121 +#undef AuBool
30122 +#undef AuStr
30123 +#undef AuUInt
30124 +}
30125 +
30126 +/* ---------------------------------------------------------------------- */
30127 +
30128 +/* sum mode which returns the summation for statfs(2) */
30129 +
30130 +static u64 au_add_till_max(u64 a, u64 b)
30131 +{
30132 +       u64 old;
30133 +
30134 +       old = a;
30135 +       a += b;
30136 +       if (old <= a)
30137 +               return a;
30138 +       return ULLONG_MAX;
30139 +}
30140 +
30141 +static u64 au_mul_till_max(u64 a, long mul)
30142 +{
30143 +       u64 old;
30144 +
30145 +       old = a;
30146 +       a *= mul;
30147 +       if (old <= a)
30148 +               return a;
30149 +       return ULLONG_MAX;
30150 +}
30151 +
30152 +static int au_statfs_sum(struct super_block *sb, struct kstatfs *buf)
30153 +{
30154 +       int err;
30155 +       long bsize, factor;
30156 +       u64 blocks, bfree, bavail, files, ffree;
30157 +       aufs_bindex_t bbot, bindex, i;
30158 +       unsigned char shared;
30159 +       struct path h_path;
30160 +       struct super_block *h_sb;
30161 +
30162 +       err = 0;
30163 +       bsize = LONG_MAX;
30164 +       files = 0;
30165 +       ffree = 0;
30166 +       blocks = 0;
30167 +       bfree = 0;
30168 +       bavail = 0;
30169 +       bbot = au_sbbot(sb);
30170 +       for (bindex = 0; bindex <= bbot; bindex++) {
30171 +               h_path.mnt = au_sbr_mnt(sb, bindex);
30172 +               h_sb = h_path.mnt->mnt_sb;
30173 +               shared = 0;
30174 +               for (i = 0; !shared && i < bindex; i++)
30175 +                       shared = (au_sbr_sb(sb, i) == h_sb);
30176 +               if (shared)
30177 +                       continue;
30178 +
30179 +               /* sb->s_root for NFS is unreliable */
30180 +               h_path.dentry = h_path.mnt->mnt_root;
30181 +               err = vfs_statfs(&h_path, buf);
30182 +               if (unlikely(err))
30183 +                       goto out;
30184 +
30185 +               if (bsize > buf->f_bsize) {
30186 +                       /*
30187 +                        * we will reduce bsize, so we have to expand blocks
30188 +                        * etc. to match them again
30189 +                        */
30190 +                       factor = (bsize / buf->f_bsize);
30191 +                       blocks = au_mul_till_max(blocks, factor);
30192 +                       bfree = au_mul_till_max(bfree, factor);
30193 +                       bavail = au_mul_till_max(bavail, factor);
30194 +                       bsize = buf->f_bsize;
30195 +               }
30196 +
30197 +               factor = (buf->f_bsize / bsize);
30198 +               blocks = au_add_till_max(blocks,
30199 +                               au_mul_till_max(buf->f_blocks, factor));
30200 +               bfree = au_add_till_max(bfree,
30201 +                               au_mul_till_max(buf->f_bfree, factor));
30202 +               bavail = au_add_till_max(bavail,
30203 +                               au_mul_till_max(buf->f_bavail, factor));
30204 +               files = au_add_till_max(files, buf->f_files);
30205 +               ffree = au_add_till_max(ffree, buf->f_ffree);
30206 +       }
30207 +
30208 +       buf->f_bsize = bsize;
30209 +       buf->f_blocks = blocks;
30210 +       buf->f_bfree = bfree;
30211 +       buf->f_bavail = bavail;
30212 +       buf->f_files = files;
30213 +       buf->f_ffree = ffree;
30214 +       buf->f_frsize = 0;
30215 +
30216 +out:
30217 +       return err;
30218 +}
30219 +
30220 +static int aufs_statfs(struct dentry *dentry, struct kstatfs *buf)
30221 +{
30222 +       int err;
30223 +       struct path h_path;
30224 +       struct super_block *sb;
30225 +
30226 +       /* lock free root dinfo */
30227 +       sb = dentry->d_sb;
30228 +       si_noflush_read_lock(sb);
30229 +       if (!au_opt_test(au_mntflags(sb), SUM)) {
30230 +               /* sb->s_root for NFS is unreliable */
30231 +               h_path.mnt = au_sbr_mnt(sb, 0);
30232 +               h_path.dentry = h_path.mnt->mnt_root;
30233 +               err = vfs_statfs(&h_path, buf);
30234 +       } else
30235 +               err = au_statfs_sum(sb, buf);
30236 +       si_read_unlock(sb);
30237 +
30238 +       if (!err) {
30239 +               buf->f_type = AUFS_SUPER_MAGIC;
30240 +               buf->f_namelen = AUFS_MAX_NAMELEN;
30241 +               memset(&buf->f_fsid, 0, sizeof(buf->f_fsid));
30242 +       }
30243 +       /* buf->f_bsize = buf->f_blocks = buf->f_bfree = buf->f_bavail = -1; */
30244 +
30245 +       return err;
30246 +}
30247 +
30248 +/* ---------------------------------------------------------------------- */
30249 +
30250 +static int aufs_sync_fs(struct super_block *sb, int wait)
30251 +{
30252 +       int err, e;
30253 +       aufs_bindex_t bbot, bindex;
30254 +       struct au_branch *br;
30255 +       struct super_block *h_sb;
30256 +
30257 +       err = 0;
30258 +       si_noflush_read_lock(sb);
30259 +       bbot = au_sbbot(sb);
30260 +       for (bindex = 0; bindex <= bbot; bindex++) {
30261 +               br = au_sbr(sb, bindex);
30262 +               if (!au_br_writable(br->br_perm))
30263 +                       continue;
30264 +
30265 +               h_sb = au_sbr_sb(sb, bindex);
30266 +               e = vfsub_sync_filesystem(h_sb);
30267 +               if (unlikely(e && !err))
30268 +                       err = e;
30269 +               /* go on even if an error happens */
30270 +       }
30271 +       si_read_unlock(sb);
30272 +
30273 +       return err;
30274 +}
30275 +
30276 +/* ---------------------------------------------------------------------- */
30277 +
30278 +/* final actions when unmounting a file system */
30279 +static void aufs_put_super(struct super_block *sb)
30280 +{
30281 +       struct au_sbinfo *sbinfo;
30282 +
30283 +       sbinfo = au_sbi(sb);
30284 +       if (sbinfo)
30285 +               kobject_put(&sbinfo->si_kobj);
30286 +}
30287 +
30288 +/* ---------------------------------------------------------------------- */
30289 +
30290 +void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb,
30291 +                    struct super_block *sb, void *arg)
30292 +{
30293 +       void *array;
30294 +       unsigned long long n, sz;
30295 +
30296 +       array = NULL;
30297 +       n = 0;
30298 +       if (!*hint)
30299 +               goto out;
30300 +
30301 +       if (*hint > ULLONG_MAX / sizeof(array)) {
30302 +               array = ERR_PTR(-EMFILE);
30303 +               pr_err("hint %llu\n", *hint);
30304 +               goto out;
30305 +       }
30306 +
30307 +       sz = sizeof(array) * *hint;
30308 +       array = kzalloc(sz, GFP_NOFS);
30309 +       if (unlikely(!array))
30310 +               array = vzalloc(sz);
30311 +       if (unlikely(!array)) {
30312 +               array = ERR_PTR(-ENOMEM);
30313 +               goto out;
30314 +       }
30315 +
30316 +       n = cb(sb, array, *hint, arg);
30317 +       AuDebugOn(n > *hint);
30318 +
30319 +out:
30320 +       *hint = n;
30321 +       return array;
30322 +}
30323 +
30324 +static unsigned long long au_iarray_cb(struct super_block *sb, void *a,
30325 +                                      unsigned long long max __maybe_unused,
30326 +                                      void *arg)
30327 +{
30328 +       unsigned long long n;
30329 +       struct inode **p, *inode;
30330 +       struct list_head *head;
30331 +
30332 +       n = 0;
30333 +       p = a;
30334 +       head = arg;
30335 +       spin_lock(&sb->s_inode_list_lock);
30336 +       list_for_each_entry(inode, head, i_sb_list) {
30337 +               if (!au_is_bad_inode(inode)
30338 +                   && au_ii(inode)->ii_btop >= 0) {
30339 +                       spin_lock(&inode->i_lock);
30340 +                       if (atomic_read(&inode->i_count)) {
30341 +                               au_igrab(inode);
30342 +                               *p++ = inode;
30343 +                               n++;
30344 +                               AuDebugOn(n > max);
30345 +                       }
30346 +                       spin_unlock(&inode->i_lock);
30347 +               }
30348 +       }
30349 +       spin_unlock(&sb->s_inode_list_lock);
30350 +
30351 +       return n;
30352 +}
30353 +
30354 +struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max)
30355 +{
30356 +       struct au_sbinfo *sbi;
30357 +
30358 +       sbi = au_sbi(sb);
30359 +       *max = au_lcnt_read(&sbi->si_ninodes, /*do_rev*/1);
30360 +       return au_array_alloc(max, au_iarray_cb, sb, &sb->s_inodes);
30361 +}
30362 +
30363 +void au_iarray_free(struct inode **a, unsigned long long max)
30364 +{
30365 +       unsigned long long ull;
30366 +
30367 +       for (ull = 0; ull < max; ull++)
30368 +               iput(a[ull]);
30369 +       kvfree(a);
30370 +}
30371 +
30372 +/* ---------------------------------------------------------------------- */
30373 +
30374 +/*
30375 + * refresh dentry and inode at remount time.
30376 + */
30377 +/* todo: consolidate with simple_reval_dpath() and au_reval_for_attr() */
30378 +static int au_do_refresh(struct dentry *dentry, unsigned int dir_flags,
30379 +                     struct dentry *parent)
30380 +{
30381 +       int err;
30382 +
30383 +       di_write_lock_child(dentry);
30384 +       di_read_lock_parent(parent, AuLock_IR);
30385 +       err = au_refresh_dentry(dentry, parent);
30386 +       if (!err && dir_flags)
30387 +               au_hn_reset(d_inode(dentry), dir_flags);
30388 +       di_read_unlock(parent, AuLock_IR);
30389 +       di_write_unlock(dentry);
30390 +
30391 +       return err;
30392 +}
30393 +
30394 +static int au_do_refresh_d(struct dentry *dentry, unsigned int sigen,
30395 +                          struct au_sbinfo *sbinfo,
30396 +                          const unsigned int dir_flags, unsigned int do_idop)
30397 +{
30398 +       int err;
30399 +       struct dentry *parent;
30400 +
30401 +       err = 0;
30402 +       parent = dget_parent(dentry);
30403 +       if (!au_digen_test(parent, sigen) && au_digen_test(dentry, sigen)) {
30404 +               if (d_really_is_positive(dentry)) {
30405 +                       if (!d_is_dir(dentry))
30406 +                               err = au_do_refresh(dentry, /*dir_flags*/0,
30407 +                                                parent);
30408 +                       else {
30409 +                               err = au_do_refresh(dentry, dir_flags, parent);
30410 +                               if (unlikely(err))
30411 +                                       au_fset_si(sbinfo, FAILED_REFRESH_DIR);
30412 +                       }
30413 +               } else
30414 +                       err = au_do_refresh(dentry, /*dir_flags*/0, parent);
30415 +               AuDbgDentry(dentry);
30416 +       }
30417 +       dput(parent);
30418 +
30419 +       if (!err) {
30420 +               if (do_idop)
30421 +                       au_refresh_dop(dentry, /*force_reval*/0);
30422 +       } else
30423 +               au_refresh_dop(dentry, /*force_reval*/1);
30424 +
30425 +       AuTraceErr(err);
30426 +       return err;
30427 +}
30428 +
30429 +static int au_refresh_d(struct super_block *sb, unsigned int do_idop)
30430 +{
30431 +       int err, i, j, ndentry, e;
30432 +       unsigned int sigen;
30433 +       struct au_dcsub_pages dpages;
30434 +       struct au_dpage *dpage;
30435 +       struct dentry **dentries, *d;
30436 +       struct au_sbinfo *sbinfo;
30437 +       struct dentry *root = sb->s_root;
30438 +       const unsigned int dir_flags = au_hi_flags(d_inode(root), /*isdir*/1);
30439 +
30440 +       if (do_idop)
30441 +               au_refresh_dop(root, /*force_reval*/0);
30442 +
30443 +       err = au_dpages_init(&dpages, GFP_NOFS);
30444 +       if (unlikely(err))
30445 +               goto out;
30446 +       err = au_dcsub_pages(&dpages, root, NULL, NULL);
30447 +       if (unlikely(err))
30448 +               goto out_dpages;
30449 +
30450 +       sigen = au_sigen(sb);
30451 +       sbinfo = au_sbi(sb);
30452 +       for (i = 0; i < dpages.ndpage; i++) {
30453 +               dpage = dpages.dpages + i;
30454 +               dentries = dpage->dentries;
30455 +               ndentry = dpage->ndentry;
30456 +               for (j = 0; j < ndentry; j++) {
30457 +                       d = dentries[j];
30458 +                       e = au_do_refresh_d(d, sigen, sbinfo, dir_flags,
30459 +                                           do_idop);
30460 +                       if (unlikely(e && !err))
30461 +                               err = e;
30462 +                       /* go on even err */
30463 +               }
30464 +       }
30465 +
30466 +out_dpages:
30467 +       au_dpages_free(&dpages);
30468 +out:
30469 +       return err;
30470 +}
30471 +
30472 +static int au_refresh_i(struct super_block *sb, unsigned int do_idop)
30473 +{
30474 +       int err, e;
30475 +       unsigned int sigen;
30476 +       unsigned long long max, ull;
30477 +       struct inode *inode, **array;
30478 +
30479 +       array = au_iarray_alloc(sb, &max);
30480 +       err = PTR_ERR(array);
30481 +       if (IS_ERR(array))
30482 +               goto out;
30483 +
30484 +       err = 0;
30485 +       sigen = au_sigen(sb);
30486 +       for (ull = 0; ull < max; ull++) {
30487 +               inode = array[ull];
30488 +               if (unlikely(!inode))
30489 +                       break;
30490 +
30491 +               e = 0;
30492 +               ii_write_lock_child(inode);
30493 +               if (au_iigen(inode, NULL) != sigen) {
30494 +                       e = au_refresh_hinode_self(inode);
30495 +                       if (unlikely(e)) {
30496 +                               au_refresh_iop(inode, /*force_getattr*/1);
30497 +                               pr_err("error %d, i%lu\n", e, inode->i_ino);
30498 +                               if (!err)
30499 +                                       err = e;
30500 +                               /* go on even if err */
30501 +                       }
30502 +               }
30503 +               if (!e && do_idop)
30504 +                       au_refresh_iop(inode, /*force_getattr*/0);
30505 +               ii_write_unlock(inode);
30506 +       }
30507 +
30508 +       au_iarray_free(array, max);
30509 +
30510 +out:
30511 +       return err;
30512 +}
30513 +
30514 +void au_remount_refresh(struct super_block *sb, unsigned int do_idop)
30515 +{
30516 +       int err, e;
30517 +       unsigned int udba;
30518 +       aufs_bindex_t bindex, bbot;
30519 +       struct dentry *root;
30520 +       struct inode *inode;
30521 +       struct au_branch *br;
30522 +       struct au_sbinfo *sbi;
30523 +
30524 +       au_sigen_inc(sb);
30525 +       sbi = au_sbi(sb);
30526 +       au_fclr_si(sbi, FAILED_REFRESH_DIR);
30527 +
30528 +       root = sb->s_root;
30529 +       DiMustNoWaiters(root);
30530 +       inode = d_inode(root);
30531 +       IiMustNoWaiters(inode);
30532 +
30533 +       udba = au_opt_udba(sb);
30534 +       bbot = au_sbbot(sb);
30535 +       for (bindex = 0; bindex <= bbot; bindex++) {
30536 +               br = au_sbr(sb, bindex);
30537 +               err = au_hnotify_reset_br(udba, br, br->br_perm);
30538 +               if (unlikely(err))
30539 +                       AuIOErr("hnotify failed on br %d, %d, ignored\n",
30540 +                               bindex, err);
30541 +               /* go on even if err */
30542 +       }
30543 +       au_hn_reset(inode, au_hi_flags(inode, /*isdir*/1));
30544 +
30545 +       if (do_idop) {
30546 +               if (au_ftest_si(sbi, NO_DREVAL)) {
30547 +                       AuDebugOn(sb->s_d_op == &aufs_dop_noreval);
30548 +                       sb->s_d_op = &aufs_dop_noreval;
30549 +                       AuDebugOn(sbi->si_iop_array == aufs_iop_nogetattr);
30550 +                       sbi->si_iop_array = aufs_iop_nogetattr;
30551 +               } else {
30552 +                       AuDebugOn(sb->s_d_op == &aufs_dop);
30553 +                       sb->s_d_op = &aufs_dop;
30554 +                       AuDebugOn(sbi->si_iop_array == aufs_iop);
30555 +                       sbi->si_iop_array = aufs_iop;
30556 +               }
30557 +               pr_info("reset to %ps and %ps\n",
30558 +                       sb->s_d_op, sbi->si_iop_array);
30559 +       }
30560 +
30561 +       di_write_unlock(root);
30562 +       err = au_refresh_d(sb, do_idop);
30563 +       e = au_refresh_i(sb, do_idop);
30564 +       if (unlikely(e && !err))
30565 +               err = e;
30566 +       /* aufs_write_lock() calls ..._child() */
30567 +       di_write_lock_child(root);
30568 +
30569 +       au_cpup_attr_all(inode, /*force*/1);
30570 +
30571 +       if (unlikely(err))
30572 +               AuIOErr("refresh failed, ignored, %d\n", err);
30573 +}
30574 +
30575 +const struct super_operations aufs_sop = {
30576 +       .alloc_inode    = aufs_alloc_inode,
30577 +       .destroy_inode  = aufs_destroy_inode,
30578 +       .free_inode     = aufs_free_inode,
30579 +       /* always deleting, no clearing */
30580 +       .drop_inode     = generic_delete_inode,
30581 +       .show_options   = aufs_show_options,
30582 +       .statfs         = aufs_statfs,
30583 +       .put_super      = aufs_put_super,
30584 +       .sync_fs        = aufs_sync_fs
30585 +};
30586 +
30587 +/* ---------------------------------------------------------------------- */
30588 +
30589 +int au_alloc_root(struct super_block *sb)
30590 +{
30591 +       int err;
30592 +       struct inode *inode;
30593 +       struct dentry *root;
30594 +
30595 +       err = -ENOMEM;
30596 +       inode = au_iget_locked(sb, AUFS_ROOT_INO);
30597 +       err = PTR_ERR(inode);
30598 +       if (IS_ERR(inode))
30599 +               goto out;
30600 +
30601 +       inode->i_op = aufs_iop + AuIop_DIR; /* with getattr by default */
30602 +       inode->i_fop = &aufs_dir_fop;
30603 +       inode->i_mode = S_IFDIR;
30604 +       set_nlink(inode, 2);
30605 +       unlock_new_inode(inode);
30606 +
30607 +       root = d_make_root(inode);
30608 +       if (unlikely(!root))
30609 +               goto out;
30610 +       err = PTR_ERR(root);
30611 +       if (IS_ERR(root))
30612 +               goto out;
30613 +
30614 +       err = au_di_init(root);
30615 +       if (!err) {
30616 +               sb->s_root = root;
30617 +               return 0; /* success */
30618 +       }
30619 +       dput(root);
30620 +
30621 +out:
30622 +       return err;
30623 +}
30624 +
30625 +/* ---------------------------------------------------------------------- */
30626 +
30627 +static void aufs_kill_sb(struct super_block *sb)
30628 +{
30629 +       struct au_sbinfo *sbinfo;
30630 +       struct dentry *root;
30631 +
30632 +       sbinfo = au_sbi(sb);
30633 +       if (!sbinfo)
30634 +               goto out;
30635 +
30636 +       au_sbilist_del(sb);
30637 +
30638 +       root = sb->s_root;
30639 +       if (root)
30640 +               aufs_write_lock(root);
30641 +       else
30642 +               __si_write_lock(sb);
30643 +
30644 +       au_fhsm_fin(sb);
30645 +       if (sbinfo->si_wbr_create_ops->fin)
30646 +               sbinfo->si_wbr_create_ops->fin(sb);
30647 +       if (au_opt_test(sbinfo->si_mntflags, UDBA_HNOTIFY)) {
30648 +               au_opt_set_udba(sbinfo->si_mntflags, UDBA_NONE);
30649 +               au_remount_refresh(sb, /*do_idop*/0);
30650 +       }
30651 +       if (au_opt_test(sbinfo->si_mntflags, PLINK))
30652 +               au_plink_put(sb, /*verbose*/1);
30653 +       au_xino_clr(sb);
30654 +       if (root)
30655 +               au_dr_opt_flush(sb);
30656 +
30657 +       if (root)
30658 +               aufs_write_unlock(root);
30659 +       else
30660 +               __si_write_unlock(sb);
30661 +
30662 +       sbinfo->si_sb = NULL;
30663 +       au_nwt_flush(&sbinfo->si_nowait);
30664 +
30665 +out:
30666 +       kill_anon_super(sb);
30667 +}
30668 +
30669 +struct file_system_type aufs_fs_type = {
30670 +       .name           = AUFS_FSTYPE,
30671 +       /* a race between rename and others */
30672 +       .fs_flags       = FS_RENAME_DOES_D_MOVE
30673 +                               /* untested */
30674 +                               /*| FS_ALLOW_IDMAP*/
30675 +                               ,
30676 +       .init_fs_context = aufs_fsctx_init,
30677 +       .parameters     = aufs_fsctx_paramspec,
30678 +       .kill_sb        = aufs_kill_sb,
30679 +       /* no need to __module_get() and module_put(). */
30680 +       .owner          = THIS_MODULE,
30681 +};
30682 diff -urN /usr/share/empty/fs/aufs/super.h linux/fs/aufs/super.h
30683 --- /usr/share/empty/fs/aufs/super.h    1970-01-01 01:00:00.000000000 +0100
30684 +++ linux/fs/aufs/super.h       2022-03-21 14:49:05.729966343 +0100
30685 @@ -0,0 +1,592 @@
30686 +/* SPDX-License-Identifier: GPL-2.0 */
30687 +/*
30688 + * Copyright (C) 2005-2021 Junjiro R. Okajima
30689 + *
30690 + * This program, aufs is free software; you can redistribute it and/or modify
30691 + * it under the terms of the GNU General Public License as published by
30692 + * the Free Software Foundation; either version 2 of the License, or
30693 + * (at your option) any later version.
30694 + *
30695 + * This program is distributed in the hope that it will be useful,
30696 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
30697 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30698 + * GNU General Public License for more details.
30699 + *
30700 + * You should have received a copy of the GNU General Public License
30701 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
30702 + */
30703 +
30704 +/*
30705 + * super_block operations
30706 + */
30707 +
30708 +#ifndef __AUFS_SUPER_H__
30709 +#define __AUFS_SUPER_H__
30710 +
30711 +#ifdef __KERNEL__
30712 +
30713 +#include <linux/fs.h>
30714 +#include <linux/kobject.h>
30715 +#include "hbl.h"
30716 +#include "lcnt.h"
30717 +#include "rwsem.h"
30718 +#include "wkq.h"
30719 +
30720 +/* policies to select one among multiple writable branches */
30721 +struct au_wbr_copyup_operations {
30722 +       int (*copyup)(struct dentry *dentry);
30723 +};
30724 +
30725 +#define AuWbr_DIR      1               /* target is a dir */
30726 +#define AuWbr_PARENT   (1 << 1)        /* always require a parent */
30727 +
30728 +#define au_ftest_wbr(flags, name)      ((flags) & AuWbr_##name)
30729 +#define au_fset_wbr(flags, name)       { (flags) |= AuWbr_##name; }
30730 +#define au_fclr_wbr(flags, name)       { (flags) &= ~AuWbr_##name; }
30731 +
30732 +struct au_wbr_create_operations {
30733 +       int (*create)(struct dentry *dentry, unsigned int flags);
30734 +       int (*init)(struct super_block *sb);
30735 +       int (*fin)(struct super_block *sb);
30736 +};
30737 +
30738 +struct au_wbr_mfs {
30739 +       struct mutex    mfs_lock; /* protect this structure */
30740 +       unsigned long   mfs_jiffy;
30741 +       unsigned long   mfs_expire;
30742 +       aufs_bindex_t   mfs_bindex;
30743 +
30744 +       unsigned long long      mfsrr_bytes;
30745 +       unsigned long long      mfsrr_watermark;
30746 +};
30747 +
30748 +#define AuPlink_NHASH 100
30749 +static inline int au_plink_hash(ino_t ino)
30750 +{
30751 +       return ino % AuPlink_NHASH;
30752 +}
30753 +
30754 +/* File-based Hierarchical Storage Management */
30755 +struct au_fhsm {
30756 +#ifdef CONFIG_AUFS_FHSM
30757 +       /* allow only one process who can receive the notification */
30758 +       spinlock_t              fhsm_spin;
30759 +       pid_t                   fhsm_pid;
30760 +       wait_queue_head_t       fhsm_wqh;
30761 +       atomic_t                fhsm_readable;
30762 +
30763 +       /* these are protected by si_rwsem */
30764 +       unsigned long           fhsm_expire;
30765 +       aufs_bindex_t           fhsm_bottom;
30766 +#endif
30767 +};
30768 +
30769 +struct au_branch;
30770 +struct au_sbinfo {
30771 +       /* nowait tasks in the system-wide workqueue */
30772 +       struct au_nowait_tasks  si_nowait;
30773 +
30774 +       /*
30775 +        * tried sb->s_umount, but failed due to the dependency between i_mutex.
30776 +        * rwsem for au_sbinfo is necessary.
30777 +        */
30778 +       struct au_rwsem         si_rwsem;
30779 +
30780 +       /*
30781 +        * dirty approach to protect sb->sb_inodes and ->s_files (gone) from
30782 +        * remount.
30783 +        */
30784 +       au_lcnt_t               si_ninodes, si_nfiles;
30785 +
30786 +       /* branch management */
30787 +       unsigned int            si_generation;
30788 +
30789 +       /* see AuSi_ flags */
30790 +       unsigned char           au_si_status;
30791 +
30792 +       aufs_bindex_t           si_bbot;
30793 +
30794 +       /* dirty trick to keep br_id plus */
30795 +       unsigned int            si_last_br_id :
30796 +                               sizeof(aufs_bindex_t) * BITS_PER_BYTE - 1;
30797 +       struct au_branch        **si_branch;
30798 +
30799 +       /* policy to select a writable branch */
30800 +       unsigned char           si_wbr_copyup;
30801 +       unsigned char           si_wbr_create;
30802 +       struct au_wbr_copyup_operations *si_wbr_copyup_ops;
30803 +       struct au_wbr_create_operations *si_wbr_create_ops;
30804 +
30805 +       /* round robin */
30806 +       atomic_t                si_wbr_rr_next;
30807 +
30808 +       /* most free space */
30809 +       struct au_wbr_mfs       si_wbr_mfs;
30810 +
30811 +       /* File-based Hierarchical Storage Management */
30812 +       struct au_fhsm          si_fhsm;
30813 +
30814 +       /* mount flags */
30815 +       /* include/asm-ia64/siginfo.h defines a macro named si_flags */
30816 +       unsigned int            si_mntflags;
30817 +
30818 +       /* external inode number (bitmap and translation table) */
30819 +       loff_t                  si_ximaxent;    /* max entries in a xino */
30820 +
30821 +       struct file             *si_xib;
30822 +       struct mutex            si_xib_mtx; /* protect xib members */
30823 +       unsigned long           *si_xib_buf;
30824 +       unsigned long           si_xib_last_pindex;
30825 +       int                     si_xib_next_bit;
30826 +
30827 +       unsigned long           si_xino_jiffy;
30828 +       unsigned long           si_xino_expire;
30829 +       /* reserved for future use */
30830 +       /* unsigned long long   si_xib_limit; */        /* Max xib file size */
30831 +
30832 +#ifdef CONFIG_AUFS_EXPORT
30833 +       /* i_generation */
30834 +       /* todo: make xigen file an array to support many inode numbers */
30835 +       struct file             *si_xigen;
30836 +       atomic_t                si_xigen_next;
30837 +#endif
30838 +
30839 +       /* dirty trick to support atomic_open */
30840 +       struct hlist_bl_head    si_aopen;
30841 +
30842 +       /* vdir parameters */
30843 +       unsigned long           si_rdcache;     /* max cache time in jiffies */
30844 +       unsigned int            si_rdblk;       /* deblk size */
30845 +       unsigned int            si_rdhash;      /* hash size */
30846 +
30847 +       /*
30848 +        * If the number of whiteouts are larger than si_dirwh, leave all of
30849 +        * them after au_whtmp_ren to reduce the cost of rmdir(2).
30850 +        * future fsck.aufs or kernel thread will remove them later.
30851 +        * Otherwise, remove all whiteouts and the dir in rmdir(2).
30852 +        */
30853 +       unsigned int            si_dirwh;
30854 +
30855 +       /* pseudo_link list */
30856 +       struct hlist_bl_head    si_plink[AuPlink_NHASH];
30857 +       wait_queue_head_t       si_plink_wq;
30858 +       spinlock_t              si_plink_maint_lock;
30859 +       pid_t                   si_plink_maint_pid;
30860 +
30861 +       /* file list */
30862 +       struct hlist_bl_head    si_files;
30863 +
30864 +       /* with/without getattr, brother of sb->s_d_op */
30865 +       const struct inode_operations *si_iop_array;
30866 +
30867 +       /*
30868 +        * sysfs and lifetime management.
30869 +        * this is not a small structure and it may be a waste of memory in case
30870 +        * of sysfs is disabled, particularly when many aufs-es are mounted.
30871 +        * but using sysfs is majority.
30872 +        */
30873 +       struct kobject          si_kobj;
30874 +#ifdef CONFIG_DEBUG_FS
30875 +       struct dentry            *si_dbgaufs;
30876 +       struct dentry            *si_dbgaufs_plink;
30877 +       struct dentry            *si_dbgaufs_xib;
30878 +#ifdef CONFIG_AUFS_EXPORT
30879 +       struct dentry            *si_dbgaufs_xigen;
30880 +#endif
30881 +#endif
30882 +
30883 +#ifdef CONFIG_AUFS_SBILIST
30884 +       struct hlist_bl_node    si_list;
30885 +#endif
30886 +
30887 +       /* dirty, necessary for unmounting, sysfs and sysrq */
30888 +       struct super_block      *si_sb;
30889 +};
30890 +
30891 +/* sbinfo status flags */
30892 +/*
30893 + * set true when refresh_dirs() failed at remount time.
30894 + * then try refreshing dirs at access time again.
30895 + * if it is false, refreshing dirs at access time is unnecessary
30896 + */
30897 +#define AuSi_FAILED_REFRESH_DIR        1
30898 +#define AuSi_FHSM              (1 << 1)        /* fhsm is active now */
30899 +#define AuSi_NO_DREVAL         (1 << 2)        /* disable all d_revalidate */
30900 +
30901 +#ifndef CONFIG_AUFS_FHSM
30902 +#undef AuSi_FHSM
30903 +#define AuSi_FHSM              0
30904 +#endif
30905 +
30906 +static inline unsigned char au_do_ftest_si(struct au_sbinfo *sbi,
30907 +                                          unsigned int flag)
30908 +{
30909 +       AuRwMustAnyLock(&sbi->si_rwsem);
30910 +       return sbi->au_si_status & flag;
30911 +}
30912 +#define au_ftest_si(sbinfo, name)      au_do_ftest_si(sbinfo, AuSi_##name)
30913 +#define au_fset_si(sbinfo, name) do { \
30914 +       AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
30915 +       (sbinfo)->au_si_status |= AuSi_##name; \
30916 +} while (0)
30917 +#define au_fclr_si(sbinfo, name) do { \
30918 +       AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
30919 +       (sbinfo)->au_si_status &= ~AuSi_##name; \
30920 +} while (0)
30921 +
30922 +/* ---------------------------------------------------------------------- */
30923 +
30924 +/* policy to select one among writable branches */
30925 +#define AuWbrCopyup(sbinfo, ...) \
30926 +       ((sbinfo)->si_wbr_copyup_ops->copyup(__VA_ARGS__))
30927 +#define AuWbrCreate(sbinfo, ...) \
30928 +       ((sbinfo)->si_wbr_create_ops->create(__VA_ARGS__))
30929 +
30930 +/* flags for si_read_lock()/aufs_read_lock()/di_read_lock() */
30931 +#define AuLock_DW              1               /* write-lock dentry */
30932 +#define AuLock_IR              (1 << 1)        /* read-lock inode */
30933 +#define AuLock_IW              (1 << 2)        /* write-lock inode */
30934 +#define AuLock_FLUSH           (1 << 3)        /* wait for 'nowait' tasks */
30935 +#define AuLock_DIRS            (1 << 4)        /* target is a pair of dirs */
30936 +                                               /* except RENAME_EXCHANGE */
30937 +#define AuLock_NOPLM           (1 << 5)        /* return err in plm mode */
30938 +#define AuLock_NOPLMW          (1 << 6)        /* wait for plm mode ends */
30939 +#define AuLock_GEN             (1 << 7)        /* test digen/iigen */
30940 +#define au_ftest_lock(flags, name)     ((flags) & AuLock_##name)
30941 +#define au_fset_lock(flags, name) \
30942 +       do { (flags) |= AuLock_##name; } while (0)
30943 +#define au_fclr_lock(flags, name) \
30944 +       do { (flags) &= ~AuLock_##name; } while (0)
30945 +
30946 +/* ---------------------------------------------------------------------- */
30947 +
30948 +/* super.c */
30949 +struct inode *au_iget_locked(struct super_block *sb, ino_t ino);
30950 +
30951 +typedef unsigned long long (*au_arraycb_t)(struct super_block *sb, void *array,
30952 +                                          unsigned long long max, void *arg);
30953 +void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb,
30954 +                    struct super_block *sb, void *arg);
30955 +struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max);
30956 +void au_iarray_free(struct inode **a, unsigned long long max);
30957 +
30958 +void au_remount_refresh(struct super_block *sb, unsigned int do_idop);
30959 +extern const struct super_operations aufs_sop;
30960 +int au_alloc_root(struct super_block *sb);
30961 +extern struct file_system_type aufs_fs_type;
30962 +
30963 +/* sbinfo.c */
30964 +void au_si_free(struct kobject *kobj);
30965 +struct au_sbinfo *au_si_alloc(struct super_block *sb);
30966 +int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr, int may_shrink);
30967 +
30968 +unsigned int au_sigen_inc(struct super_block *sb);
30969 +aufs_bindex_t au_new_br_id(struct super_block *sb);
30970 +
30971 +int si_read_lock(struct super_block *sb, int flags);
30972 +int si_write_lock(struct super_block *sb, int flags);
30973 +int aufs_read_lock(struct dentry *dentry, int flags);
30974 +void aufs_read_unlock(struct dentry *dentry, int flags);
30975 +void aufs_write_lock(struct dentry *dentry);
30976 +void aufs_write_unlock(struct dentry *dentry);
30977 +int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags);
30978 +void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2);
30979 +
30980 +/* wbr_policy.c */
30981 +extern struct au_wbr_copyup_operations au_wbr_copyup_ops[];
30982 +extern struct au_wbr_create_operations au_wbr_create_ops[];
30983 +int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst);
30984 +int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex);
30985 +int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t btop);
30986 +
30987 +/* mvdown.c */
30988 +int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *arg);
30989 +
30990 +#ifdef CONFIG_AUFS_FHSM
30991 +/* fhsm.c */
30992 +
30993 +static inline pid_t au_fhsm_pid(struct au_fhsm *fhsm)
30994 +{
30995 +       pid_t pid;
30996 +
30997 +       spin_lock(&fhsm->fhsm_spin);
30998 +       pid = fhsm->fhsm_pid;
30999 +       spin_unlock(&fhsm->fhsm_spin);
31000 +
31001 +       return pid;
31002 +}
31003 +
31004 +void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force);
31005 +void au_fhsm_wrote_all(struct super_block *sb, int force);
31006 +int au_fhsm_fd(struct super_block *sb, int oflags);
31007 +int au_fhsm_br_alloc(struct au_branch *br);
31008 +void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex);
31009 +void au_fhsm_fin(struct super_block *sb);
31010 +void au_fhsm_init(struct au_sbinfo *sbinfo);
31011 +void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec);
31012 +void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo);
31013 +#else
31014 +AuStubVoid(au_fhsm_wrote, struct super_block *sb, aufs_bindex_t bindex,
31015 +          int force)
31016 +AuStubVoid(au_fhsm_wrote_all, struct super_block *sb, int force)
31017 +AuStub(int, au_fhsm_fd, return -EOPNOTSUPP, struct super_block *sb, int oflags)
31018 +AuStub(pid_t, au_fhsm_pid, return 0, struct au_fhsm *fhsm)
31019 +AuStubInt0(au_fhsm_br_alloc, struct au_branch *br)
31020 +AuStubVoid(au_fhsm_set_bottom, struct super_block *sb, aufs_bindex_t bindex)
31021 +AuStubVoid(au_fhsm_fin, struct super_block *sb)
31022 +AuStubVoid(au_fhsm_init, struct au_sbinfo *sbinfo)
31023 +AuStubVoid(au_fhsm_set, struct au_sbinfo *sbinfo, unsigned int sec)
31024 +AuStubVoid(au_fhsm_show, struct seq_file *seq, struct au_sbinfo *sbinfo)
31025 +#endif
31026 +
31027 +/* ---------------------------------------------------------------------- */
31028 +
31029 +static inline struct au_sbinfo *au_sbi(struct super_block *sb)
31030 +{
31031 +       return sb->s_fs_info;
31032 +}
31033 +
31034 +/* ---------------------------------------------------------------------- */
31035 +
31036 +#ifdef CONFIG_AUFS_EXPORT
31037 +int au_test_nfsd(void);
31038 +void au_export_init(struct super_block *sb);
31039 +void au_xigen_inc(struct inode *inode);
31040 +int au_xigen_new(struct inode *inode);
31041 +int au_xigen_set(struct super_block *sb, struct path *path);
31042 +void au_xigen_clr(struct super_block *sb);
31043 +
31044 +static inline int au_busy_or_stale(void)
31045 +{
31046 +       if (!au_test_nfsd())
31047 +               return -EBUSY;
31048 +       return -ESTALE;
31049 +}
31050 +#else
31051 +AuStubInt0(au_test_nfsd, void)
31052 +AuStubVoid(au_export_init, struct super_block *sb)
31053 +AuStubVoid(au_xigen_inc, struct inode *inode)
31054 +AuStubInt0(au_xigen_new, struct inode *inode)
31055 +AuStubInt0(au_xigen_set, struct super_block *sb, struct path *path)
31056 +AuStubVoid(au_xigen_clr, struct super_block *sb)
31057 +AuStub(int, au_busy_or_stale, return -EBUSY, void)
31058 +#endif /* CONFIG_AUFS_EXPORT */
31059 +
31060 +/* ---------------------------------------------------------------------- */
31061 +
31062 +#ifdef CONFIG_AUFS_SBILIST
31063 +/* module.c */
31064 +extern struct hlist_bl_head au_sbilist;
31065 +
31066 +static inline void au_sbilist_init(void)
31067 +{
31068 +       INIT_HLIST_BL_HEAD(&au_sbilist);
31069 +}
31070 +
31071 +static inline void au_sbilist_add(struct super_block *sb)
31072 +{
31073 +       au_hbl_add(&au_sbi(sb)->si_list, &au_sbilist);
31074 +}
31075 +
31076 +static inline void au_sbilist_del(struct super_block *sb)
31077 +{
31078 +       au_hbl_del(&au_sbi(sb)->si_list, &au_sbilist);
31079 +}
31080 +
31081 +#ifdef CONFIG_AUFS_MAGIC_SYSRQ
31082 +static inline void au_sbilist_lock(void)
31083 +{
31084 +       hlist_bl_lock(&au_sbilist);
31085 +}
31086 +
31087 +static inline void au_sbilist_unlock(void)
31088 +{
31089 +       hlist_bl_unlock(&au_sbilist);
31090 +}
31091 +#define AuGFP_SBILIST  GFP_ATOMIC
31092 +#else
31093 +AuStubVoid(au_sbilist_lock, void)
31094 +AuStubVoid(au_sbilist_unlock, void)
31095 +#define AuGFP_SBILIST  GFP_NOFS
31096 +#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
31097 +#else
31098 +AuStubVoid(au_sbilist_init, void)
31099 +AuStubVoid(au_sbilist_add, struct super_block *sb)
31100 +AuStubVoid(au_sbilist_del, struct super_block *sb)
31101 +AuStubVoid(au_sbilist_lock, void)
31102 +AuStubVoid(au_sbilist_unlock, void)
31103 +#define AuGFP_SBILIST  GFP_NOFS
31104 +#endif
31105 +
31106 +/* ---------------------------------------------------------------------- */
31107 +
31108 +static inline void dbgaufs_si_null(struct au_sbinfo *sbinfo)
31109 +{
31110 +       /*
31111 +        * This function is a dynamic '__init' function actually,
31112 +        * so the tiny check for si_rwsem is unnecessary.
31113 +        */
31114 +       /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
31115 +#ifdef CONFIG_DEBUG_FS
31116 +       sbinfo->si_dbgaufs = NULL;
31117 +       sbinfo->si_dbgaufs_plink = NULL;
31118 +       sbinfo->si_dbgaufs_xib = NULL;
31119 +#ifdef CONFIG_AUFS_EXPORT
31120 +       sbinfo->si_dbgaufs_xigen = NULL;
31121 +#endif
31122 +#endif
31123 +}
31124 +
31125 +/* ---------------------------------------------------------------------- */
31126 +
31127 +/* current->atomic_flags */
31128 +/* this value should never corrupt the ones defined in linux/sched.h */
31129 +#define PFA_AUFS       0x10
31130 +
31131 +TASK_PFA_TEST(AUFS, test_aufs) /* task_test_aufs */
31132 +TASK_PFA_SET(AUFS, aufs)       /* task_set_aufs */
31133 +TASK_PFA_CLEAR(AUFS, aufs)     /* task_clear_aufs */
31134 +
31135 +static inline int si_pid_test(struct super_block *sb)
31136 +{
31137 +       return !!task_test_aufs(current);
31138 +}
31139 +
31140 +static inline void si_pid_clr(struct super_block *sb)
31141 +{
31142 +       AuDebugOn(!task_test_aufs(current));
31143 +       task_clear_aufs(current);
31144 +}
31145 +
31146 +static inline void si_pid_set(struct super_block *sb)
31147 +{
31148 +       AuDebugOn(task_test_aufs(current));
31149 +       task_set_aufs(current);
31150 +}
31151 +
31152 +/* ---------------------------------------------------------------------- */
31153 +
31154 +/* lock superblock. mainly for entry point functions */
31155 +#define __si_read_lock(sb)     au_rw_read_lock(&au_sbi(sb)->si_rwsem)
31156 +#define __si_write_lock(sb)    au_rw_write_lock(&au_sbi(sb)->si_rwsem)
31157 +#define __si_read_trylock(sb)  au_rw_read_trylock(&au_sbi(sb)->si_rwsem)
31158 +#define __si_write_trylock(sb) au_rw_write_trylock(&au_sbi(sb)->si_rwsem)
31159 +/*
31160 +#define __si_read_trylock_nested(sb) \
31161 +       au_rw_read_trylock_nested(&au_sbi(sb)->si_rwsem)
31162 +#define __si_write_trylock_nested(sb) \
31163 +       au_rw_write_trylock_nested(&au_sbi(sb)->si_rwsem)
31164 +*/
31165 +
31166 +#define __si_read_unlock(sb)   au_rw_read_unlock(&au_sbi(sb)->si_rwsem)
31167 +#define __si_write_unlock(sb)  au_rw_write_unlock(&au_sbi(sb)->si_rwsem)
31168 +#define __si_downgrade_lock(sb)        au_rw_dgrade_lock(&au_sbi(sb)->si_rwsem)
31169 +
31170 +#define SiMustNoWaiters(sb)    AuRwMustNoWaiters(&au_sbi(sb)->si_rwsem)
31171 +#define SiMustAnyLock(sb)      AuRwMustAnyLock(&au_sbi(sb)->si_rwsem)
31172 +#define SiMustWriteLock(sb)    AuRwMustWriteLock(&au_sbi(sb)->si_rwsem)
31173 +
31174 +static inline void si_noflush_read_lock(struct super_block *sb)
31175 +{
31176 +       __si_read_lock(sb);
31177 +       si_pid_set(sb);
31178 +}
31179 +
31180 +static inline int si_noflush_read_trylock(struct super_block *sb)
31181 +{
31182 +       int locked;
31183 +
31184 +       locked = __si_read_trylock(sb);
31185 +       if (locked)
31186 +               si_pid_set(sb);
31187 +       return locked;
31188 +}
31189 +
31190 +static inline void si_noflush_write_lock(struct super_block *sb)
31191 +{
31192 +       __si_write_lock(sb);
31193 +       si_pid_set(sb);
31194 +}
31195 +
31196 +static inline int si_noflush_write_trylock(struct super_block *sb)
31197 +{
31198 +       int locked;
31199 +
31200 +       locked = __si_write_trylock(sb);
31201 +       if (locked)
31202 +               si_pid_set(sb);
31203 +       return locked;
31204 +}
31205 +
31206 +#if 0 /* reserved */
31207 +static inline int si_read_trylock(struct super_block *sb, int flags)
31208 +{
31209 +       if (au_ftest_lock(flags, FLUSH))
31210 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
31211 +       return si_noflush_read_trylock(sb);
31212 +}
31213 +#endif
31214 +
31215 +static inline void si_read_unlock(struct super_block *sb)
31216 +{
31217 +       si_pid_clr(sb);
31218 +       __si_read_unlock(sb);
31219 +}
31220 +
31221 +#if 0 /* reserved */
31222 +static inline int si_write_trylock(struct super_block *sb, int flags)
31223 +{
31224 +       if (au_ftest_lock(flags, FLUSH))
31225 +               au_nwt_flush(&au_sbi(sb)->si_nowait);
31226 +       return si_noflush_write_trylock(sb);
31227 +}
31228 +#endif
31229 +
31230 +static inline void si_write_unlock(struct super_block *sb)
31231 +{
31232 +       si_pid_clr(sb);
31233 +       __si_write_unlock(sb);
31234 +}
31235 +
31236 +#if 0 /* reserved */
31237 +static inline void si_downgrade_lock(struct super_block *sb)
31238 +{
31239 +       __si_downgrade_lock(sb);
31240 +}
31241 +#endif
31242 +
31243 +/* ---------------------------------------------------------------------- */
31244 +
31245 +static inline aufs_bindex_t au_sbbot(struct super_block *sb)
31246 +{
31247 +       SiMustAnyLock(sb);
31248 +       return au_sbi(sb)->si_bbot;
31249 +}
31250 +
31251 +static inline unsigned int au_mntflags(struct super_block *sb)
31252 +{
31253 +       SiMustAnyLock(sb);
31254 +       return au_sbi(sb)->si_mntflags;
31255 +}
31256 +
31257 +static inline unsigned int au_sigen(struct super_block *sb)
31258 +{
31259 +       SiMustAnyLock(sb);
31260 +       return au_sbi(sb)->si_generation;
31261 +}
31262 +
31263 +static inline struct au_branch *au_sbr(struct super_block *sb,
31264 +                                      aufs_bindex_t bindex)
31265 +{
31266 +       SiMustAnyLock(sb);
31267 +       return au_sbi(sb)->si_branch[0 + bindex];
31268 +}
31269 +
31270 +static inline loff_t au_xi_maxent(struct super_block *sb)
31271 +{
31272 +       SiMustAnyLock(sb);
31273 +       return au_sbi(sb)->si_ximaxent;
31274 +}
31275 +
31276 +#endif /* __KERNEL__ */
31277 +#endif /* __AUFS_SUPER_H__ */
31278 diff -urN /usr/share/empty/fs/aufs/sysaufs.c linux/fs/aufs/sysaufs.c
31279 --- /usr/share/empty/fs/aufs/sysaufs.c  1970-01-01 01:00:00.000000000 +0100
31280 +++ linux/fs/aufs/sysaufs.c     2022-05-23 09:44:37.729839166 +0200
31281 @@ -0,0 +1,94 @@
31282 +// SPDX-License-Identifier: GPL-2.0
31283 +/*
31284 + * Copyright (C) 2005-2021 Junjiro R. Okajima
31285 + *
31286 + * This program, aufs is free software; you can redistribute it and/or modify
31287 + * it under the terms of the GNU General Public License as published by
31288 + * the Free Software Foundation; either version 2 of the License, or
31289 + * (at your option) any later version.
31290 + *
31291 + * This program is distributed in the hope that it will be useful,
31292 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31293 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31294 + * GNU General Public License for more details.
31295 + *
31296 + * You should have received a copy of the GNU General Public License
31297 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31298 + */
31299 +
31300 +/*
31301 + * sysfs interface and lifetime management
31302 + * they are necessary regardless sysfs is disabled.
31303 + */
31304 +
31305 +#include <linux/random.h>
31306 +#include "aufs.h"
31307 +
31308 +unsigned long sysaufs_si_mask;
31309 +struct kset *sysaufs_kset;
31310 +
31311 +#define AuSiAttr(_name) { \
31312 +       .attr   = { .name = __stringify(_name), .mode = 0444 }, \
31313 +       .show   = sysaufs_si_##_name,                           \
31314 +}
31315 +
31316 +static struct sysaufs_si_attr sysaufs_si_attr_xi_path = AuSiAttr(xi_path);
31317 +struct attribute *sysaufs_si_attrs[] = {
31318 +       &sysaufs_si_attr_xi_path.attr,
31319 +       NULL,
31320 +};
31321 +ATTRIBUTE_GROUPS(sysaufs_si);
31322 +
31323 +static const struct sysfs_ops au_sbi_ops = {
31324 +       .show   = sysaufs_si_show
31325 +};
31326 +
31327 +static struct kobj_type au_sbi_ktype = {
31328 +       .release        = au_si_free,
31329 +       .sysfs_ops      = &au_sbi_ops,
31330 +       .default_groups = sysaufs_si_groups
31331 +};
31332 +
31333 +/* ---------------------------------------------------------------------- */
31334 +
31335 +int sysaufs_si_init(struct au_sbinfo *sbinfo)
31336 +{
31337 +       int err;
31338 +
31339 +       sbinfo->si_kobj.kset = sysaufs_kset;
31340 +       /* cf. sysaufs_name() */
31341 +       err = kobject_init_and_add
31342 +               (&sbinfo->si_kobj, &au_sbi_ktype, /*&sysaufs_kset->kobj*/NULL,
31343 +                SysaufsSiNamePrefix "%lx", sysaufs_si_id(sbinfo));
31344 +
31345 +       return err;
31346 +}
31347 +
31348 +void sysaufs_fin(void)
31349 +{
31350 +       sysfs_remove_group(&sysaufs_kset->kobj, sysaufs_attr_group);
31351 +       kset_unregister(sysaufs_kset);
31352 +}
31353 +
31354 +int __init sysaufs_init(void)
31355 +{
31356 +       int err;
31357 +
31358 +       do {
31359 +               get_random_bytes(&sysaufs_si_mask, sizeof(sysaufs_si_mask));
31360 +       } while (!sysaufs_si_mask);
31361 +
31362 +       err = -EINVAL;
31363 +       sysaufs_kset = kset_create_and_add(AUFS_NAME, NULL, fs_kobj);
31364 +       if (unlikely(!sysaufs_kset))
31365 +               goto out;
31366 +       err = PTR_ERR(sysaufs_kset);
31367 +       if (IS_ERR(sysaufs_kset))
31368 +               goto out;
31369 +       err = sysfs_create_group(&sysaufs_kset->kobj, sysaufs_attr_group);
31370 +       if (unlikely(err))
31371 +               kset_unregister(sysaufs_kset);
31372 +
31373 +out:
31374 +       return err;
31375 +}
31376 diff -urN /usr/share/empty/fs/aufs/sysaufs.h linux/fs/aufs/sysaufs.h
31377 --- /usr/share/empty/fs/aufs/sysaufs.h  1970-01-01 01:00:00.000000000 +0100
31378 +++ linux/fs/aufs/sysaufs.h     2022-03-21 14:49:05.729966343 +0100
31379 @@ -0,0 +1,102 @@
31380 +/* SPDX-License-Identifier: GPL-2.0 */
31381 +/*
31382 + * Copyright (C) 2005-2021 Junjiro R. Okajima
31383 + *
31384 + * This program, aufs is free software; you can redistribute it and/or modify
31385 + * it under the terms of the GNU General Public License as published by
31386 + * the Free Software Foundation; either version 2 of the License, or
31387 + * (at your option) any later version.
31388 + *
31389 + * This program is distributed in the hope that it will be useful,
31390 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31391 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31392 + * GNU General Public License for more details.
31393 + *
31394 + * You should have received a copy of the GNU General Public License
31395 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31396 + */
31397 +
31398 +/*
31399 + * sysfs interface and mount lifetime management
31400 + */
31401 +
31402 +#ifndef __SYSAUFS_H__
31403 +#define __SYSAUFS_H__
31404 +
31405 +#ifdef __KERNEL__
31406 +
31407 +#include <linux/sysfs.h>
31408 +#include "module.h"
31409 +
31410 +struct super_block;
31411 +struct au_sbinfo;
31412 +
31413 +struct sysaufs_si_attr {
31414 +       struct attribute attr;
31415 +       int (*show)(struct seq_file *seq, struct super_block *sb);
31416 +};
31417 +
31418 +/* ---------------------------------------------------------------------- */
31419 +
31420 +/* sysaufs.c */
31421 +extern unsigned long sysaufs_si_mask;
31422 +extern struct kset *sysaufs_kset;
31423 +extern struct attribute *sysaufs_si_attrs[];
31424 +int sysaufs_si_init(struct au_sbinfo *sbinfo);
31425 +int __init sysaufs_init(void);
31426 +void sysaufs_fin(void);
31427 +
31428 +/* ---------------------------------------------------------------------- */
31429 +
31430 +/* some people doesn't like to show a pointer in kernel */
31431 +static inline unsigned long sysaufs_si_id(struct au_sbinfo *sbinfo)
31432 +{
31433 +       return sysaufs_si_mask ^ (unsigned long)sbinfo;
31434 +}
31435 +
31436 +#define SysaufsSiNamePrefix    "si_"
31437 +#define SysaufsSiNameLen       (sizeof(SysaufsSiNamePrefix) + 16)
31438 +static inline void sysaufs_name(struct au_sbinfo *sbinfo, char *name)
31439 +{
31440 +       snprintf(name, SysaufsSiNameLen, SysaufsSiNamePrefix "%lx",
31441 +                sysaufs_si_id(sbinfo));
31442 +}
31443 +
31444 +struct au_branch;
31445 +#ifdef CONFIG_SYSFS
31446 +/* sysfs.c */
31447 +extern struct attribute_group *sysaufs_attr_group;
31448 +
31449 +int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb);
31450 +ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
31451 +                        char *buf);
31452 +long au_brinfo_ioctl(struct file *file, unsigned long arg);
31453 +#ifdef CONFIG_COMPAT
31454 +long au_brinfo_compat_ioctl(struct file *file, unsigned long arg);
31455 +#endif
31456 +
31457 +void sysaufs_br_init(struct au_branch *br);
31458 +void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex);
31459 +void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex);
31460 +
31461 +#define sysaufs_brs_init()     do {} while (0)
31462 +
31463 +#else
31464 +#define sysaufs_attr_group     NULL
31465 +
31466 +AuStubInt0(sysaufs_si_xi_path, struct seq_file *seq, struct super_block *sb)
31467 +AuStub(ssize_t, sysaufs_si_show, return 0, struct kobject *kobj,
31468 +       struct attribute *attr, char *buf)
31469 +AuStubVoid(sysaufs_br_init, struct au_branch *br)
31470 +AuStubVoid(sysaufs_brs_add, struct super_block *sb, aufs_bindex_t bindex)
31471 +AuStubVoid(sysaufs_brs_del, struct super_block *sb, aufs_bindex_t bindex)
31472 +
31473 +static inline void sysaufs_brs_init(void)
31474 +{
31475 +       sysaufs_brs = 0;
31476 +}
31477 +
31478 +#endif /* CONFIG_SYSFS */
31479 +
31480 +#endif /* __KERNEL__ */
31481 +#endif /* __SYSAUFS_H__ */
31482 diff -urN /usr/share/empty/fs/aufs/sysfs.c linux/fs/aufs/sysfs.c
31483 --- /usr/share/empty/fs/aufs/sysfs.c    1970-01-01 01:00:00.000000000 +0100
31484 +++ linux/fs/aufs/sysfs.c       2022-03-21 14:49:05.729966343 +0100
31485 @@ -0,0 +1,374 @@
31486 +// SPDX-License-Identifier: GPL-2.0
31487 +/*
31488 + * Copyright (C) 2005-2021 Junjiro R. Okajima
31489 + *
31490 + * This program, aufs is free software; you can redistribute it and/or modify
31491 + * it under the terms of the GNU General Public License as published by
31492 + * the Free Software Foundation; either version 2 of the License, or
31493 + * (at your option) any later version.
31494 + *
31495 + * This program is distributed in the hope that it will be useful,
31496 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31497 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31498 + * GNU General Public License for more details.
31499 + *
31500 + * You should have received a copy of the GNU General Public License
31501 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31502 + */
31503 +
31504 +/*
31505 + * sysfs interface
31506 + */
31507 +
31508 +#include <linux/compat.h>
31509 +#include <linux/seq_file.h>
31510 +#include "aufs.h"
31511 +
31512 +#ifdef CONFIG_AUFS_FS_MODULE
31513 +/* this entry violates the "one line per file" policy of sysfs */
31514 +static ssize_t config_show(struct kobject *kobj, struct kobj_attribute *attr,
31515 +                          char *buf)
31516 +{
31517 +       ssize_t err;
31518 +       static char *conf =
31519 +/* this file is generated at compiling */
31520 +#include "conf.str"
31521 +               ;
31522 +
31523 +       err = snprintf(buf, PAGE_SIZE, conf);
31524 +       if (unlikely(err >= PAGE_SIZE))
31525 +               err = -EFBIG;
31526 +       return err;
31527 +}
31528 +
31529 +static struct kobj_attribute au_config_attr = __ATTR_RO(config);
31530 +#endif
31531 +
31532 +static struct attribute *au_attr[] = {
31533 +#ifdef CONFIG_AUFS_FS_MODULE
31534 +       &au_config_attr.attr,
31535 +#endif
31536 +       NULL,   /* need to NULL terminate the list of attributes */
31537 +};
31538 +
31539 +static struct attribute_group sysaufs_attr_group_body = {
31540 +       .attrs = au_attr
31541 +};
31542 +
31543 +struct attribute_group *sysaufs_attr_group = &sysaufs_attr_group_body;
31544 +
31545 +/* ---------------------------------------------------------------------- */
31546 +
31547 +int sysaufs_si_xi_path(struct seq_file *seq, struct super_block *sb)
31548 +{
31549 +       int err;
31550 +
31551 +       SiMustAnyLock(sb);
31552 +
31553 +       err = 0;
31554 +       if (au_opt_test(au_mntflags(sb), XINO)) {
31555 +               err = au_xino_path(seq, au_sbi(sb)->si_xib);
31556 +               seq_putc(seq, '\n');
31557 +       }
31558 +       return err;
31559 +}
31560 +
31561 +/*
31562 + * the lifetime of branch is independent from the entry under sysfs.
31563 + * sysfs handles the lifetime of the entry, and never call ->show() after it is
31564 + * unlinked.
31565 + */
31566 +static int sysaufs_si_br(struct seq_file *seq, struct super_block *sb,
31567 +                        aufs_bindex_t bindex, int idx)
31568 +{
31569 +       int err;
31570 +       struct path path;
31571 +       struct dentry *root;
31572 +       struct au_branch *br;
31573 +       au_br_perm_str_t perm;
31574 +
31575 +       AuDbg("b%d\n", bindex);
31576 +
31577 +       err = 0;
31578 +       root = sb->s_root;
31579 +       di_read_lock_parent(root, !AuLock_IR);
31580 +       br = au_sbr(sb, bindex);
31581 +
31582 +       switch (idx) {
31583 +       case AuBrSysfs_BR:
31584 +               path.mnt = au_br_mnt(br);
31585 +               path.dentry = au_h_dptr(root, bindex);
31586 +               err = au_seq_path(seq, &path);
31587 +               if (!err) {
31588 +                       au_optstr_br_perm(&perm, br->br_perm);
31589 +                       seq_printf(seq, "=%s\n", perm.a);
31590 +               }
31591 +               break;
31592 +       case AuBrSysfs_BRID:
31593 +               seq_printf(seq, "%d\n", br->br_id);
31594 +               break;
31595 +       }
31596 +       di_read_unlock(root, !AuLock_IR);
31597 +       if (unlikely(err || seq_has_overflowed(seq)))
31598 +               err = -E2BIG;
31599 +
31600 +       return err;
31601 +}
31602 +
31603 +/* ---------------------------------------------------------------------- */
31604 +
31605 +static struct seq_file *au_seq(char *p, ssize_t len)
31606 +{
31607 +       struct seq_file *seq;
31608 +
31609 +       seq = kzalloc(sizeof(*seq), GFP_NOFS);
31610 +       if (seq) {
31611 +               /* mutex_init(&seq.lock); */
31612 +               seq->buf = p;
31613 +               seq->size = len;
31614 +               return seq; /* success */
31615 +       }
31616 +
31617 +       seq = ERR_PTR(-ENOMEM);
31618 +       return seq;
31619 +}
31620 +
31621 +#define SysaufsBr_PREFIX       "br"
31622 +#define SysaufsBrid_PREFIX     "brid"
31623 +
31624 +/* todo: file size may exceed PAGE_SIZE */
31625 +ssize_t sysaufs_si_show(struct kobject *kobj, struct attribute *attr,
31626 +                       char *buf)
31627 +{
31628 +       ssize_t err;
31629 +       int idx;
31630 +       long l;
31631 +       aufs_bindex_t bbot;
31632 +       struct au_sbinfo *sbinfo;
31633 +       struct super_block *sb;
31634 +       struct seq_file *seq;
31635 +       char *name;
31636 +       struct attribute **cattr;
31637 +
31638 +       sbinfo = container_of(kobj, struct au_sbinfo, si_kobj);
31639 +       sb = sbinfo->si_sb;
31640 +
31641 +       /*
31642 +        * prevent a race condition between sysfs and aufs.
31643 +        * for instance, sysfs_file_read() calls sysfs_get_active_two() which
31644 +        * prohibits maintaining the sysfs entries.
31645 +        * hew we acquire read lock after sysfs_get_active_two().
31646 +        * on the other hand, the remount process may maintain the sysfs/aufs
31647 +        * entries after acquiring write lock.
31648 +        * it can cause a deadlock.
31649 +        * simply we gave up processing read here.
31650 +        */
31651 +       err = -EBUSY;
31652 +       if (unlikely(!si_noflush_read_trylock(sb)))
31653 +               goto out;
31654 +
31655 +       seq = au_seq(buf, PAGE_SIZE);
31656 +       err = PTR_ERR(seq);
31657 +       if (IS_ERR(seq))
31658 +               goto out_unlock;
31659 +
31660 +       name = (void *)attr->name;
31661 +       cattr = sysaufs_si_attrs;
31662 +       while (*cattr) {
31663 +               if (!strcmp(name, (*cattr)->name)) {
31664 +                       err = container_of(*cattr, struct sysaufs_si_attr, attr)
31665 +                               ->show(seq, sb);
31666 +                       goto out_seq;
31667 +               }
31668 +               cattr++;
31669 +       }
31670 +
31671 +       if (!strncmp(name, SysaufsBrid_PREFIX,
31672 +                    sizeof(SysaufsBrid_PREFIX) - 1)) {
31673 +               idx = AuBrSysfs_BRID;
31674 +               name += sizeof(SysaufsBrid_PREFIX) - 1;
31675 +       } else if (!strncmp(name, SysaufsBr_PREFIX,
31676 +                           sizeof(SysaufsBr_PREFIX) - 1)) {
31677 +               idx = AuBrSysfs_BR;
31678 +               name += sizeof(SysaufsBr_PREFIX) - 1;
31679 +       } else
31680 +                 BUG();
31681 +
31682 +       err = kstrtol(name, 10, &l);
31683 +       if (!err) {
31684 +               bbot = au_sbbot(sb);
31685 +               if (l <= bbot)
31686 +                       err = sysaufs_si_br(seq, sb, (aufs_bindex_t)l, idx);
31687 +               else
31688 +                       err = -ENOENT;
31689 +       }
31690 +
31691 +out_seq:
31692 +       if (!err) {
31693 +               err = seq->count;
31694 +               /* sysfs limit */
31695 +               if (unlikely(err == PAGE_SIZE))
31696 +                       err = -EFBIG;
31697 +       }
31698 +       au_kfree_rcu(seq);
31699 +out_unlock:
31700 +       si_read_unlock(sb);
31701 +out:
31702 +       return err;
31703 +}
31704 +
31705 +/* ---------------------------------------------------------------------- */
31706 +
31707 +static int au_brinfo(struct super_block *sb, union aufs_brinfo __user *arg)
31708 +{
31709 +       int err;
31710 +       int16_t brid;
31711 +       aufs_bindex_t bindex, bbot;
31712 +       size_t sz;
31713 +       char *buf;
31714 +       struct seq_file *seq;
31715 +       struct au_branch *br;
31716 +
31717 +       si_read_lock(sb, AuLock_FLUSH);
31718 +       bbot = au_sbbot(sb);
31719 +       err = bbot + 1;
31720 +       if (!arg)
31721 +               goto out;
31722 +
31723 +       err = -ENOMEM;
31724 +       buf = (void *)__get_free_page(GFP_NOFS);
31725 +       if (unlikely(!buf))
31726 +               goto out;
31727 +
31728 +       seq = au_seq(buf, PAGE_SIZE);
31729 +       err = PTR_ERR(seq);
31730 +       if (IS_ERR(seq))
31731 +               goto out_buf;
31732 +
31733 +       sz = sizeof(*arg) - offsetof(union aufs_brinfo, path);
31734 +       for (bindex = 0; bindex <= bbot; bindex++, arg++) {
31735 +               /* VERIFY_WRITE */
31736 +               err = !access_ok(arg, sizeof(*arg));
31737 +               if (unlikely(err))
31738 +                       break;
31739 +
31740 +               br = au_sbr(sb, bindex);
31741 +               brid = br->br_id;
31742 +               BUILD_BUG_ON(sizeof(brid) != sizeof(arg->id));
31743 +               err = __put_user(brid, &arg->id);
31744 +               if (unlikely(err))
31745 +                       break;
31746 +
31747 +               BUILD_BUG_ON(sizeof(br->br_perm) != sizeof(arg->perm));
31748 +               err = __put_user(br->br_perm, &arg->perm);
31749 +               if (unlikely(err))
31750 +                       break;
31751 +
31752 +               err = au_seq_path(seq, &br->br_path);
31753 +               if (unlikely(err))
31754 +                       break;
31755 +               seq_putc(seq, '\0');
31756 +               if (!seq_has_overflowed(seq)) {
31757 +                       err = copy_to_user(arg->path, seq->buf, seq->count);
31758 +                       seq->count = 0;
31759 +                       if (unlikely(err))
31760 +                               break;
31761 +               } else {
31762 +                       err = -E2BIG;
31763 +                       goto out_seq;
31764 +               }
31765 +       }
31766 +       if (unlikely(err))
31767 +               err = -EFAULT;
31768 +
31769 +out_seq:
31770 +       au_kfree_rcu(seq);
31771 +out_buf:
31772 +       free_page((unsigned long)buf);
31773 +out:
31774 +       si_read_unlock(sb);
31775 +       return err;
31776 +}
31777 +
31778 +long au_brinfo_ioctl(struct file *file, unsigned long arg)
31779 +{
31780 +       return au_brinfo(file->f_path.dentry->d_sb, (void __user *)arg);
31781 +}
31782 +
31783 +#ifdef CONFIG_COMPAT
31784 +long au_brinfo_compat_ioctl(struct file *file, unsigned long arg)
31785 +{
31786 +       return au_brinfo(file->f_path.dentry->d_sb, compat_ptr(arg));
31787 +}
31788 +#endif
31789 +
31790 +/* ---------------------------------------------------------------------- */
31791 +
31792 +void sysaufs_br_init(struct au_branch *br)
31793 +{
31794 +       int i;
31795 +       struct au_brsysfs *br_sysfs;
31796 +       struct attribute *attr;
31797 +
31798 +       br_sysfs = br->br_sysfs;
31799 +       for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31800 +               attr = &br_sysfs->attr;
31801 +               sysfs_attr_init(attr);
31802 +               attr->name = br_sysfs->name;
31803 +               attr->mode = 0444;
31804 +               br_sysfs++;
31805 +       }
31806 +}
31807 +
31808 +void sysaufs_brs_del(struct super_block *sb, aufs_bindex_t bindex)
31809 +{
31810 +       struct au_branch *br;
31811 +       struct kobject *kobj;
31812 +       struct au_brsysfs *br_sysfs;
31813 +       int i;
31814 +       aufs_bindex_t bbot;
31815 +
31816 +       if (!sysaufs_brs)
31817 +               return;
31818 +
31819 +       kobj = &au_sbi(sb)->si_kobj;
31820 +       bbot = au_sbbot(sb);
31821 +       for (; bindex <= bbot; bindex++) {
31822 +               br = au_sbr(sb, bindex);
31823 +               br_sysfs = br->br_sysfs;
31824 +               for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31825 +                       sysfs_remove_file(kobj, &br_sysfs->attr);
31826 +                       br_sysfs++;
31827 +               }
31828 +       }
31829 +}
31830 +
31831 +void sysaufs_brs_add(struct super_block *sb, aufs_bindex_t bindex)
31832 +{
31833 +       int err, i;
31834 +       aufs_bindex_t bbot;
31835 +       struct kobject *kobj;
31836 +       struct au_branch *br;
31837 +       struct au_brsysfs *br_sysfs;
31838 +
31839 +       if (!sysaufs_brs)
31840 +               return;
31841 +
31842 +       kobj = &au_sbi(sb)->si_kobj;
31843 +       bbot = au_sbbot(sb);
31844 +       for (; bindex <= bbot; bindex++) {
31845 +               br = au_sbr(sb, bindex);
31846 +               br_sysfs = br->br_sysfs;
31847 +               snprintf(br_sysfs[AuBrSysfs_BR].name, sizeof(br_sysfs->name),
31848 +                        SysaufsBr_PREFIX "%d", bindex);
31849 +               snprintf(br_sysfs[AuBrSysfs_BRID].name, sizeof(br_sysfs->name),
31850 +                        SysaufsBrid_PREFIX "%d", bindex);
31851 +               for (i = 0; i < ARRAY_SIZE(br->br_sysfs); i++) {
31852 +                       err = sysfs_create_file(kobj, &br_sysfs->attr);
31853 +                       if (unlikely(err))
31854 +                               pr_warn("failed %s under sysfs(%d)\n",
31855 +                                       br_sysfs->name, err);
31856 +                       br_sysfs++;
31857 +               }
31858 +       }
31859 +}
31860 diff -urN /usr/share/empty/fs/aufs/sysrq.c linux/fs/aufs/sysrq.c
31861 --- /usr/share/empty/fs/aufs/sysrq.c    1970-01-01 01:00:00.000000000 +0100
31862 +++ linux/fs/aufs/sysrq.c       2022-03-21 14:49:05.729966343 +0100
31863 @@ -0,0 +1,149 @@
31864 +// SPDX-License-Identifier: GPL-2.0
31865 +/*
31866 + * Copyright (C) 2005-2021 Junjiro R. Okajima
31867 + *
31868 + * This program, aufs is free software; you can redistribute it and/or modify
31869 + * it under the terms of the GNU General Public License as published by
31870 + * the Free Software Foundation; either version 2 of the License, or
31871 + * (at your option) any later version.
31872 + *
31873 + * This program is distributed in the hope that it will be useful,
31874 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
31875 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31876 + * GNU General Public License for more details.
31877 + *
31878 + * You should have received a copy of the GNU General Public License
31879 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
31880 + */
31881 +
31882 +/*
31883 + * magic sysrq handler
31884 + */
31885 +
31886 +/* #include <linux/sysrq.h> */
31887 +#include <linux/writeback.h>
31888 +#include "aufs.h"
31889 +
31890 +/* ---------------------------------------------------------------------- */
31891 +
31892 +static void sysrq_sb(struct super_block *sb)
31893 +{
31894 +       char *plevel;
31895 +       struct au_sbinfo *sbinfo;
31896 +       struct file *file;
31897 +       struct hlist_bl_head *files;
31898 +       struct hlist_bl_node *pos;
31899 +       struct au_finfo *finfo;
31900 +       struct inode *i;
31901 +
31902 +       plevel = au_plevel;
31903 +       au_plevel = KERN_WARNING;
31904 +
31905 +       /* since we define pr_fmt, call printk directly */
31906 +#define pr(str) printk(KERN_WARNING AUFS_NAME ": " str)
31907 +
31908 +       sbinfo = au_sbi(sb);
31909 +       printk(KERN_WARNING "si=%lx\n", sysaufs_si_id(sbinfo));
31910 +       pr("superblock\n");
31911 +       au_dpri_sb(sb);
31912 +
31913 +#if 0 /* reserved */
31914 +       do {
31915 +               int err, i, j, ndentry;
31916 +               struct au_dcsub_pages dpages;
31917 +               struct au_dpage *dpage;
31918 +
31919 +               err = au_dpages_init(&dpages, GFP_ATOMIC);
31920 +               if (unlikely(err))
31921 +                       break;
31922 +               err = au_dcsub_pages(&dpages, sb->s_root, NULL, NULL);
31923 +               if (!err)
31924 +                       for (i = 0; i < dpages.ndpage; i++) {
31925 +                               dpage = dpages.dpages + i;
31926 +                               ndentry = dpage->ndentry;
31927 +                               for (j = 0; j < ndentry; j++)
31928 +                                       au_dpri_dentry(dpage->dentries[j]);
31929 +                       }
31930 +               au_dpages_free(&dpages);
31931 +       } while (0);
31932 +#endif
31933 +
31934 +       pr("isolated inode\n");
31935 +       spin_lock(&sb->s_inode_list_lock);
31936 +       list_for_each_entry(i, &sb->s_inodes, i_sb_list) {
31937 +               spin_lock(&i->i_lock);
31938 +               if (hlist_empty(&i->i_dentry))
31939 +                       au_dpri_inode(i);
31940 +               spin_unlock(&i->i_lock);
31941 +       }
31942 +       spin_unlock(&sb->s_inode_list_lock);
31943 +
31944 +       pr("files\n");
31945 +       files = &au_sbi(sb)->si_files;
31946 +       hlist_bl_lock(files);
31947 +       hlist_bl_for_each_entry(finfo, pos, files, fi_hlist) {
31948 +               umode_t mode;
31949 +
31950 +               file = finfo->fi_file;
31951 +               mode = file_inode(file)->i_mode;
31952 +               if (!special_file(mode))
31953 +                       au_dpri_file(file);
31954 +       }
31955 +       hlist_bl_unlock(files);
31956 +       pr("done\n");
31957 +
31958 +#undef pr
31959 +       au_plevel = plevel;
31960 +}
31961 +
31962 +/* ---------------------------------------------------------------------- */
31963 +
31964 +/* module parameter */
31965 +static char *aufs_sysrq_key = "a";
31966 +module_param_named(sysrq, aufs_sysrq_key, charp, 0444);
31967 +MODULE_PARM_DESC(sysrq, "MagicSysRq key for " AUFS_NAME);
31968 +
31969 +static void au_sysrq(int key __maybe_unused)
31970 +{
31971 +       struct au_sbinfo *sbinfo;
31972 +       struct hlist_bl_node *pos;
31973 +
31974 +       lockdep_off();
31975 +       au_sbilist_lock();
31976 +       hlist_bl_for_each_entry(sbinfo, pos, &au_sbilist, si_list)
31977 +               sysrq_sb(sbinfo->si_sb);
31978 +       au_sbilist_unlock();
31979 +       lockdep_on();
31980 +}
31981 +
31982 +static struct sysrq_key_op au_sysrq_op = {
31983 +       .handler        = au_sysrq,
31984 +       .help_msg       = "Aufs",
31985 +       .action_msg     = "Aufs",
31986 +       .enable_mask    = SYSRQ_ENABLE_DUMP
31987 +};
31988 +
31989 +/* ---------------------------------------------------------------------- */
31990 +
31991 +int __init au_sysrq_init(void)
31992 +{
31993 +       int err;
31994 +       char key;
31995 +
31996 +       err = -1;
31997 +       key = *aufs_sysrq_key;
31998 +       if ('a' <= key && key <= 'z')
31999 +               err = register_sysrq_key(key, &au_sysrq_op);
32000 +       if (unlikely(err))
32001 +               pr_err("err %d, sysrq=%c\n", err, key);
32002 +       return err;
32003 +}
32004 +
32005 +void au_sysrq_fin(void)
32006 +{
32007 +       int err;
32008 +
32009 +       err = unregister_sysrq_key(*aufs_sysrq_key, &au_sysrq_op);
32010 +       if (unlikely(err))
32011 +               pr_err("err %d (ignored)\n", err);
32012 +}
32013 diff -urN /usr/share/empty/fs/aufs/vdir.c linux/fs/aufs/vdir.c
32014 --- /usr/share/empty/fs/aufs/vdir.c     1970-01-01 01:00:00.000000000 +0100
32015 +++ linux/fs/aufs/vdir.c        2022-03-21 14:49:05.733299677 +0100
32016 @@ -0,0 +1,896 @@
32017 +// SPDX-License-Identifier: GPL-2.0
32018 +/*
32019 + * Copyright (C) 2005-2021 Junjiro R. Okajima
32020 + *
32021 + * This program, aufs is free software; you can redistribute it and/or modify
32022 + * it under the terms of the GNU General Public License as published by
32023 + * the Free Software Foundation; either version 2 of the License, or
32024 + * (at your option) any later version.
32025 + *
32026 + * This program is distributed in the hope that it will be useful,
32027 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
32028 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
32029 + * GNU General Public License for more details.
32030 + *
32031 + * You should have received a copy of the GNU General Public License
32032 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
32033 + */
32034 +
32035 +/*
32036 + * virtual or vertical directory
32037 + */
32038 +
32039 +#include <linux/iversion.h>
32040 +#include "aufs.h"
32041 +
32042 +static unsigned int calc_size(int nlen)
32043 +{
32044 +       return ALIGN(sizeof(struct au_vdir_de) + nlen, sizeof(ino_t));
32045 +}
32046 +
32047 +static int set_deblk_end(union au_vdir_deblk_p *p,
32048 +                        union au_vdir_deblk_p *deblk_end)
32049 +{
32050 +       if (calc_size(0) <= deblk_end->deblk - p->deblk) {
32051 +               p->de->de_str.len = 0;
32052 +               /* smp_mb(); */
32053 +               return 0;
32054 +       }
32055 +       return -1; /* error */
32056 +}
32057 +
32058 +/* returns true or false */
32059 +static int is_deblk_end(union au_vdir_deblk_p *p,
32060 +                       union au_vdir_deblk_p *deblk_end)
32061 +{
32062 +       if (calc_size(0) <= deblk_end->deblk - p->deblk)
32063 +               return !p->de->de_str.len;
32064 +       return 1;
32065 +}
32066 +
32067 +static unsigned char *last_deblk(struct au_vdir *vdir)
32068 +{
32069 +       return vdir->vd_deblk[vdir->vd_nblk - 1];
32070 +}
32071 +
32072 +/* ---------------------------------------------------------------------- */
32073 +
32074 +/* estimate the appropriate size for name hash table */
32075 +unsigned int au_rdhash_est(loff_t sz)
32076 +{
32077 +       unsigned int n;
32078 +
32079 +       n = UINT_MAX;
32080 +       sz >>= 10;
32081 +       if (sz < n)
32082 +               n = sz;
32083 +       if (sz < AUFS_RDHASH_DEF)
32084 +               n = AUFS_RDHASH_DEF;
32085 +       /* pr_info("n %u\n", n); */
32086 +       return n;
32087 +}
32088 +
32089 +/*
32090 + * the allocated memory has to be freed by
32091 + * au_nhash_wh_free() or au_nhash_de_free().
32092 + */
32093 +int au_nhash_alloc(struct au_nhash *nhash, unsigned int num_hash, gfp_t gfp)
32094 +{
32095 +       struct hlist_head *head;
32096 +       unsigned int u;
32097 +       size_t sz;
32098 +
32099 +       sz = sizeof(*nhash->nh_head) * num_hash;
32100 +       head = kmalloc(sz, gfp);
32101 +       if (head) {
32102 +               nhash->nh_num = num_hash;
32103 +               nhash->nh_head = head;
32104 +               for (u = 0; u < num_hash; u++)
32105 +                       INIT_HLIST_HEAD(head++);
32106 +               return 0; /* success */
32107 +       }
32108 +
32109 +       return -ENOMEM;
32110 +}
32111 +
32112 +static void nhash_count(struct hlist_head *head)
32113 +{
32114 +#if 0 /* debugging */
32115 +       unsigned long n;
32116 +       struct hlist_node *pos;
32117 +
32118 +       n = 0;
32119 +       hlist_for_each(pos, head)
32120 +               n++;
32121 +       pr_info("%lu\n", n);
32122 +#endif
32123 +}
32124 +
32125 +static void au_nhash_wh_do_free(struct hlist_head *head)
32126 +{
32127 +       struct au_vdir_wh *pos;
32128 +       struct hlist_node *node;
32129 +
32130 +       hlist_for_each_entry_safe(pos, node, head, wh_hash)
32131 +               au_kfree_rcu(pos);
32132 +}
32133 +
32134 +static void au_nhash_de_do_free(struct hlist_head *head)
32135 +{
32136 +       struct au_vdir_dehstr *pos;
32137 +       struct hlist_node *node;
32138 +
32139 +       hlist_for_each_entry_safe(pos, node, head, hash)
32140 +               au_cache_free_vdir_dehstr(pos);
32141 +}
32142 +
32143 +static void au_nhash_do_free(struct au_nhash *nhash,
32144 +                            void (*free)(struct hlist_head *head))
32145 +{
32146 +       unsigned int n;
32147 +       struct hlist_head *head;
32148 +
32149 +       n = nhash->nh_num;
32150 +       if (!n)
32151 +               return;
32152 +
32153 +       head = nhash->nh_head;
32154 +       while (n-- > 0) {
32155 +               nhash_count(head);
32156 +               free(head++);
32157 +       }
32158 +       au_kfree_try_rcu(nhash->nh_head);
32159 +}
32160 +
32161 +void au_nhash_wh_free(struct au_nhash *whlist)
32162 +{
32163 +       au_nhash_do_free(whlist, au_nhash_wh_do_free);
32164 +}
32165 +
32166 +static void au_nhash_de_free(struct au_nhash *delist)
32167 +{
32168 +       au_nhash_do_free(delist, au_nhash_de_do_free);
32169 +}
32170 +
32171 +/* ---------------------------------------------------------------------- */
32172 +
32173 +int au_nhash_test_longer_wh(struct au_nhash *whlist, aufs_bindex_t btgt,
32174 +                           int limit)
32175 +{
32176 +       int num;
32177 +       unsigned int u, n;
32178 +       struct hlist_head *head;
32179 +       struct au_vdir_wh *pos;
32180 +
32181 +       num = 0;
32182 +       n = whlist->nh_num;
32183 +       head = whlist->nh_head;
32184 +       for (u = 0; u < n; u++, head++)
32185 +               hlist_for_each_entry(pos, head, wh_hash)
32186 +                       if (pos->wh_bindex == btgt && ++num > limit)
32187 +                               return 1;
32188 +       return 0;
32189 +}
32190 +
32191 +static struct hlist_head *au_name_hash(struct au_nhash *nhash,
32192 +                                      unsigned char *name,
32193 +                                      unsigned int len)
32194 +{
32195 +       unsigned int v;
32196 +       /* const unsigned int magic_bit = 12; */
32197 +
32198 +       AuDebugOn(!nhash->nh_num || !nhash->nh_head);
32199 +
32200 +       v = 0;
32201 +       if (len > 8)
32202 +               len = 8;
32203 +       while (len--)
32204 +               v += *name++;
32205 +       /* v = hash_long(v, magic_bit); */
32206 +       v %= nhash->nh_num;
32207 +       return nhash->nh_head + v;
32208 +}
32209 +
32210 +static int au_nhash_test_name(struct au_vdir_destr *str, const char *name,
32211 +                             int nlen)
32212 +{
32213 +       return str->len == nlen && !memcmp(str->name, name, nlen);
32214 +}
32215 +
32216 +/* returns found or not */
32217 +int au_nhash_test_known_wh(struct au_nhash *whlist, char *name, int nlen)
32218 +{
32219 +       struct hlist_head *head;
32220 +       struct au_vdir_wh *pos;
32221 +       struct au_vdir_destr *str;
32222 +
32223 +       head = au_name_hash(whlist, name, nlen);
32224 +       hlist_for_each_entry(pos, head, wh_hash) {
32225 +               str = &pos->wh_str;
32226 +               AuDbg("%.*s\n", str->len, str->name);
32227 +               if (au_nhash_test_name(str, name, nlen))
32228 +                       return 1;
32229 +       }
32230 +       return 0;
32231 +}
32232 +
32233 +/* returns found(true) or not */
32234 +static int test_known(struct au_nhash *delist, char *name, int nlen)
32235 +{
32236 +       struct hlist_head *head;
32237 +       struct au_vdir_dehstr *pos;
32238 +       struct au_vdir_destr *str;
32239 +
32240 +       head = au_name_hash(delist, name, nlen);
32241 +       hlist_for_each_entry(pos, head, hash) {
32242 +               str = pos->str;
32243 +               AuDbg("%.*s\n", str->len, str->name);
32244 +               if (au_nhash_test_name(str, name, nlen))
32245 +                       return 1;
32246 +       }
32247 +       return 0;
32248 +}
32249 +
32250 +static void au_shwh_init_wh(struct au_vdir_wh *wh, ino_t ino,
32251 +                           unsigned char d_type)
32252 +{
32253 +#ifdef CONFIG_AUFS_SHWH
32254 +       wh->wh_ino = ino;
32255 +       wh->wh_type = d_type;
32256 +#endif
32257 +}
32258 +
32259 +/* ---------------------------------------------------------------------- */
32260 +
32261 +int au_nhash_append_wh(struct au_nhash *whlist, char *name, int nlen, ino_t ino,
32262 +                      unsigned int d_type, aufs_bindex_t bindex,
32263 +                      unsigned char shwh)
32264 +{
32265 +       int err;
32266 +       struct au_vdir_destr *str;
32267 +       struct au_vdir_wh *wh;
32268 +
32269 +       AuDbg("%.*s\n", nlen, name);
32270 +       AuDebugOn(!whlist->nh_num || !whlist->nh_head);
32271 +
32272 +       err = -ENOMEM;
32273 +       wh = kmalloc(sizeof(*wh) + nlen, GFP_NOFS);
32274 +       if (unlikely(!wh))
32275 +               goto out;
32276 +
32277 +       err = 0;
32278 +       wh->wh_bindex = bindex;
32279 +       if (shwh)
32280 +               au_shwh_init_wh(wh, ino, d_type);
32281 +       str = &wh->wh_str;
32282 +       str->len = nlen;
32283 +       memcpy(str->name, name, nlen);
32284 +       hlist_add_head(&wh->wh_hash, au_name_hash(whlist, name, nlen));
32285 +       /* smp_mb(); */
32286 +
32287 +out:
32288 +       return err;
32289 +}
32290 +
32291 +static int append_deblk(struct au_vdir *vdir)
32292 +{
32293 +       int err;
32294 +       unsigned long ul;
32295 +       const unsigned int deblk_sz = vdir->vd_deblk_sz;
32296 +       union au_vdir_deblk_p p, deblk_end;
32297 +       unsigned char **o;
32298 +
32299 +       err = -ENOMEM;
32300 +       o = au_krealloc(vdir->vd_deblk, sizeof(*o) * (vdir->vd_nblk + 1),
32301 +                       GFP_NOFS, /*may_shrink*/0);
32302 +       if (unlikely(!o))
32303 +               goto out;
32304 +
32305 +       vdir->vd_deblk = o;
32306 +       p.deblk = kmalloc(deblk_sz, GFP_NOFS);
32307 +       if (p.deblk) {
32308 +               ul = vdir->vd_nblk++;
32309 +               vdir->vd_deblk[ul] = p.deblk;
32310 +               vdir->vd_last.ul = ul;
32311 +               vdir->vd_last.p.deblk = p.deblk;
32312 +               deblk_end.deblk = p.deblk + deblk_sz;
32313 +               err = set_deblk_end(&p, &deblk_end);
32314 +       }
32315 +
32316 +out:
32317 +       return err;
32318 +}
32319 +
32320 +static int append_de(struct au_vdir *vdir, char *name, int nlen, ino_t ino,
32321 +                    unsigned int d_type, struct au_nhash *delist)
32322 +{
32323 +       int err;
32324 +       unsigned int sz;
32325 +       const unsigned int deblk_sz = vdir->vd_deblk_sz;
32326 +       union au_vdir_deblk_p p, *room, deblk_end;
32327 +       struct au_vdir_dehstr *dehstr;
32328 +
32329 +       p.deblk = last_deblk(vdir);
32330 +       deblk_end.deblk = p.deblk + deblk_sz;
32331 +       room = &vdir->vd_last.p;
32332 +       AuDebugOn(room->deblk < p.deblk || deblk_end.deblk <= room->deblk
32333 +                 || !is_deblk_end(room, &deblk_end));
32334 +
32335 +       sz = calc_size(nlen);
32336 +       if (unlikely(sz > deblk_end.deblk - room->deblk)) {
32337 +               err = append_deblk(vdir);
32338 +               if (unlikely(err))
32339 +                       goto out;
32340 +
32341 +               p.deblk = last_deblk(vdir);
32342 +               deblk_end.deblk = p.deblk + deblk_sz;
32343 +               /* smp_mb(); */
32344 +               AuDebugOn(room->deblk != p.deblk);
32345 +       }
32346 +
32347 +       err = -ENOMEM;
32348 +       dehstr = au_cache_alloc_vdir_dehstr();
32349 +       if (unlikely(!dehstr))
32350 +               goto out;
32351 +
32352 +       dehstr->str = &room->de->de_str;
32353 +       hlist_add_head(&dehstr->hash, au_name_hash(delist, name, nlen));
32354 +       room->de->de_ino = ino;
32355 +       room->de->de_type = d_type;
32356 +       room->de->de_str.len = nlen;
32357 +       memcpy(room->de->de_str.name, name, nlen);
32358 +
32359 +       err = 0;
32360 +       room->deblk += sz;
32361 +       if (unlikely(set_deblk_end(room, &deblk_end)))
32362 +               err = append_deblk(vdir);
32363 +       /* smp_mb(); */
32364 +
32365 +out:
32366 +       return err;
32367 +}
32368 +
32369 +/* ---------------------------------------------------------------------- */
32370 +
32371 +void au_vdir_free(struct au_vdir *vdir)
32372 +{
32373 +       unsigned char **deblk;
32374 +
32375 +       deblk = vdir->vd_deblk;
32376 +       while (vdir->vd_nblk--)
32377 +               au_kfree_try_rcu(*deblk++);
32378 +       au_kfree_try_rcu(vdir->vd_deblk);
32379 +       au_cache_free_vdir(vdir);
32380 +}
32381 +
32382 +static struct au_vdir *alloc_vdir(struct file *file)
32383 +{
32384 +       struct au_vdir *vdir;
32385 +       struct super_block *sb;
32386 +       int err;
32387 +
32388 +       sb = file->f_path.dentry->d_sb;
32389 +       SiMustAnyLock(sb);
32390 +
32391 +       err = -ENOMEM;
32392 +       vdir = au_cache_alloc_vdir();
32393 +       if (unlikely(!vdir))
32394 +               goto out;
32395 +
32396 +       vdir->vd_deblk = kzalloc(sizeof(*vdir->vd_deblk), GFP_NOFS);
32397 +       if (unlikely(!vdir->vd_deblk))
32398 +               goto out_free;
32399 +
32400 +       vdir->vd_deblk_sz = au_sbi(sb)->si_rdblk;
32401 +       if (!vdir->vd_deblk_sz) {
32402 +               /* estimate the appropriate size for deblk */
32403 +               vdir->vd_deblk_sz = au_dir_size(file, /*dentry*/NULL);
32404 +               /* pr_info("vd_deblk_sz %u\n", vdir->vd_deblk_sz); */
32405 +       }
32406 +       vdir->vd_nblk = 0;
32407 +       vdir->vd_version = 0;
32408 +       vdir->vd_jiffy = 0;
32409 +       err = append_deblk(vdir);
32410 +       if (!err)
32411 +               return vdir; /* success */
32412 +
32413 +       au_kfree_try_rcu(vdir->vd_deblk);
32414 +
32415 +out_free:
32416 +       au_cache_free_vdir(vdir);
32417 +out:
32418 +       vdir = ERR_PTR(err);
32419 +       return vdir;
32420 +}
32421 +
32422 +static int reinit_vdir(struct au_vdir *vdir)
32423 +{
32424 +       int err;
32425 +       union au_vdir_deblk_p p, deblk_end;
32426 +
32427 +       while (vdir->vd_nblk > 1) {
32428 +               au_kfree_try_rcu(vdir->vd_deblk[vdir->vd_nblk - 1]);
32429 +               /* vdir->vd_deblk[vdir->vd_nblk - 1] = NULL; */
32430 +               vdir->vd_nblk--;
32431 +       }
32432 +       p.deblk = vdir->vd_deblk[0];
32433 +       deblk_end.deblk = p.deblk + vdir->vd_deblk_sz;
32434 +       err = set_deblk_end(&p, &deblk_end);
32435 +       /* keep vd_dblk_sz */
32436 +       vdir->vd_last.ul = 0;
32437 +       vdir->vd_last.p.deblk = vdir->vd_deblk[0];
32438 +       vdir->vd_version = 0;
32439 +       vdir->vd_jiffy = 0;
32440 +       /* smp_mb(); */
32441 +       return err;
32442 +}
32443 +
32444 +/* ---------------------------------------------------------------------- */
32445 +
32446 +#define AuFillVdir_CALLED      1
32447 +#define AuFillVdir_WHABLE      (1 << 1)
32448 +#define AuFillVdir_SHWH                (1 << 2)
32449 +#define au_ftest_fillvdir(flags, name) ((flags) & AuFillVdir_##name)
32450 +#define au_fset_fillvdir(flags, name) \
32451 +       do { (flags) |= AuFillVdir_##name; } while (0)
32452 +#define au_fclr_fillvdir(flags, name) \
32453 +       do { (flags) &= ~AuFillVdir_##name; } while (0)
32454 +
32455 +#ifndef CONFIG_AUFS_SHWH
32456 +#undef AuFillVdir_SHWH
32457 +#define AuFillVdir_SHWH                0
32458 +#endif
32459 +
32460 +struct fillvdir_arg {
32461 +       struct dir_context      ctx;
32462 +       struct file             *file;
32463 +       struct au_vdir          *vdir;
32464 +       struct au_nhash         delist;
32465 +       struct au_nhash         whlist;
32466 +       aufs_bindex_t           bindex;
32467 +       unsigned int            flags;
32468 +       int                     err;
32469 +};
32470 +
32471 +static int fillvdir(struct dir_context *ctx, const char *__name, int nlen,
32472 +                   loff_t offset __maybe_unused, u64 h_ino,
32473 +                   unsigned int d_type)
32474 +{
32475 +       struct fillvdir_arg *arg = container_of(ctx, struct fillvdir_arg, ctx);
32476 +       char *name = (void *)__name;
32477 +       struct super_block *sb;
32478 +       ino_t ino;
32479 +       const unsigned char shwh = !!au_ftest_fillvdir(arg->flags, SHWH);
32480 +
32481 +       arg->err = 0;
32482 +       sb = arg->file->f_path.dentry->d_sb;
32483 +       au_fset_fillvdir(arg->flags, CALLED);
32484 +       /* smp_mb(); */
32485 +       if (nlen <= AUFS_WH_PFX_LEN
32486 +           || memcmp(name, AUFS_WH_PFX, AUFS_WH_PFX_LEN)) {
32487 +               if (test_known(&arg->delist, name, nlen)
32488 +                   || au_nhash_test_known_wh(&arg->whlist, name, nlen))
32489 +                       goto out; /* already exists or whiteouted */
32490 +
32491 +               arg->err = au_ino(sb, arg->bindex, h_ino, d_type, &ino);
32492 +               if (!arg->err) {
32493 +                       if (unlikely(nlen > AUFS_MAX_NAMELEN))
32494 +                               d_type = DT_UNKNOWN;
32495 +                       arg->err = append_de(arg->vdir, name, nlen, ino,
32496 +                                            d_type, &arg->delist);
32497 +               }
32498 +       } else if (au_ftest_fillvdir(arg->flags, WHABLE)) {
32499 +               name += AUFS_WH_PFX_LEN;
32500 +               nlen -= AUFS_WH_PFX_LEN;
32501 +               if (au_nhash_test_known_wh(&arg->whlist, name, nlen))
32502 +                       goto out; /* already whiteouted */
32503 +
32504 +               ino = 0; /* just to suppress a warning */
32505 +               if (shwh)
32506 +                       arg->err = au_wh_ino(sb, arg->bindex, h_ino, d_type,
32507 +                                            &ino);
32508 +               if (!arg->err) {
32509 +                       if (nlen <= AUFS_MAX_NAMELEN + AUFS_WH_PFX_LEN)
32510 +                               d_type = DT_UNKNOWN;
32511 +                       arg->err = au_nhash_append_wh
32512 +                               (&arg->whlist, name, nlen, ino, d_type,
32513 +                                arg->bindex, shwh);
32514 +               }
32515 +       }
32516 +
32517 +out:
32518 +       if (!arg->err)
32519 +               arg->vdir->vd_jiffy = jiffies;
32520 +       /* smp_mb(); */
32521 +       AuTraceErr(arg->err);
32522 +       return arg->err;
32523 +}
32524 +
32525 +static int au_handle_shwh(struct super_block *sb, struct au_vdir *vdir,
32526 +                         struct au_nhash *whlist, struct au_nhash *delist)
32527 +{
32528 +#ifdef CONFIG_AUFS_SHWH
32529 +       int err;
32530 +       unsigned int nh, u;
32531 +       struct hlist_head *head;
32532 +       struct au_vdir_wh *pos;
32533 +       struct hlist_node *n;
32534 +       char *p, *o;
32535 +       struct au_vdir_destr *destr;
32536 +
32537 +       AuDebugOn(!au_opt_test(au_mntflags(sb), SHWH));
32538 +
32539 +       err = -ENOMEM;
32540 +       o = p = (void *)__get_free_page(GFP_NOFS);
32541 +       if (unlikely(!p))
32542 +               goto out;
32543 +
32544 +       err = 0;
32545 +       nh = whlist->nh_num;
32546 +       memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
32547 +       p += AUFS_WH_PFX_LEN;
32548 +       for (u = 0; u < nh; u++) {
32549 +               head = whlist->nh_head + u;
32550 +               hlist_for_each_entry_safe(pos, n, head, wh_hash) {
32551 +                       destr = &pos->wh_str;
32552 +                       memcpy(p, destr->name, destr->len);
32553 +                       err = append_de(vdir, o, destr->len + AUFS_WH_PFX_LEN,
32554 +                                       pos->wh_ino, pos->wh_type, delist);
32555 +                       if (unlikely(err))
32556 +                               break;
32557 +               }
32558 +       }
32559 +
32560 +       free_page((unsigned long)o);
32561 +
32562 +out:
32563 +       AuTraceErr(err);
32564 +       return err;
32565 +#else
32566 +       return 0;
32567 +#endif
32568 +}
32569 +
32570 +static int au_do_read_vdir(struct fillvdir_arg *arg)
32571 +{
32572 +       int err;
32573 +       unsigned int rdhash;
32574 +       loff_t offset;
32575 +       aufs_bindex_t bbot, bindex, btop;
32576 +       unsigned char shwh;
32577 +       struct file *hf, *file;
32578 +       struct super_block *sb;
32579 +
32580 +       file = arg->file;
32581 +       sb = file->f_path.dentry->d_sb;
32582 +       SiMustAnyLock(sb);
32583 +
32584 +       rdhash = au_sbi(sb)->si_rdhash;
32585 +       if (!rdhash)
32586 +               rdhash = au_rdhash_est(au_dir_size(file, /*dentry*/NULL));
32587 +       err = au_nhash_alloc(&arg->delist, rdhash, GFP_NOFS);
32588 +       if (unlikely(err))
32589 +               goto out;
32590 +       err = au_nhash_alloc(&arg->whlist, rdhash, GFP_NOFS);
32591 +       if (unlikely(err))
32592 +               goto out_delist;
32593 +
32594 +       err = 0;
32595 +       arg->flags = 0;
32596 +       shwh = 0;
32597 +       if (au_opt_test(au_mntflags(sb), SHWH)) {
32598 +               shwh = 1;
32599 +               au_fset_fillvdir(arg->flags, SHWH);
32600 +       }
32601 +       btop = au_fbtop(file);
32602 +       bbot = au_fbbot_dir(file);
32603 +       for (bindex = btop; !err && bindex <= bbot; bindex++) {
32604 +               hf = au_hf_dir(file, bindex);
32605 +               if (!hf)
32606 +                       continue;
32607 +
32608 +               offset = vfsub_llseek(hf, 0, SEEK_SET);
32609 +               err = offset;
32610 +               if (unlikely(offset))
32611 +                       break;
32612 +
32613 +               arg->bindex = bindex;
32614 +               au_fclr_fillvdir(arg->flags, WHABLE);
32615 +               if (shwh
32616 +                   || (bindex != bbot
32617 +                       && au_br_whable(au_sbr_perm(sb, bindex))))
32618 +                       au_fset_fillvdir(arg->flags, WHABLE);
32619 +               do {
32620 +                       arg->err = 0;
32621 +                       au_fclr_fillvdir(arg->flags, CALLED);
32622 +                       /* smp_mb(); */
32623 +                       err = vfsub_iterate_dir(hf, &arg->ctx);
32624 +                       if (err >= 0)
32625 +                               err = arg->err;
32626 +               } while (!err && au_ftest_fillvdir(arg->flags, CALLED));
32627 +
32628 +               /*
32629 +                * dir_relax() may be good for concurrency, but aufs should not
32630 +                * use it since it will cause a lockdep problem.
32631 +                */
32632 +       }
32633 +
32634 +       if (!err && shwh)
32635 +               err = au_handle_shwh(sb, arg->vdir, &arg->whlist, &arg->delist);
32636 +
32637 +       au_nhash_wh_free(&arg->whlist);
32638 +
32639 +out_delist:
32640 +       au_nhash_de_free(&arg->delist);
32641 +out:
32642 +       return err;
32643 +}
32644 +
32645 +static int read_vdir(struct file *file, int may_read)
32646 +{
32647 +       int err;
32648 +       unsigned long expire;
32649 +       unsigned char do_read;
32650 +       struct fillvdir_arg arg = {
32651 +               .ctx = {
32652 +                       .actor = fillvdir
32653 +               }
32654 +       };
32655 +       struct inode *inode;
32656 +       struct au_vdir *vdir, *allocated;
32657 +
32658 +       err = 0;
32659 +       inode = file_inode(file);
32660 +       IMustLock(inode);
32661 +       IiMustWriteLock(inode);
32662 +       SiMustAnyLock(inode->i_sb);
32663 +
32664 +       allocated = NULL;
32665 +       do_read = 0;
32666 +       expire = au_sbi(inode->i_sb)->si_rdcache;
32667 +       vdir = au_ivdir(inode);
32668 +       if (!vdir) {
32669 +               do_read = 1;
32670 +               vdir = alloc_vdir(file);
32671 +               err = PTR_ERR(vdir);
32672 +               if (IS_ERR(vdir))
32673 +                       goto out;
32674 +               err = 0;
32675 +               allocated = vdir;
32676 +       } else if (may_read
32677 +                  && (!inode_eq_iversion(inode, vdir->vd_version)
32678 +                      || time_after(jiffies, vdir->vd_jiffy + expire))) {
32679 +               do_read = 1;
32680 +               err = reinit_vdir(vdir);
32681 +               if (unlikely(err))
32682 +                       goto out;
32683 +       }
32684 +
32685 +       if (!do_read)
32686 +               return 0; /* success */
32687 +
32688 +       arg.file = file;
32689 +       arg.vdir = vdir;
32690 +       err = au_do_read_vdir(&arg);
32691 +       if (!err) {
32692 +               /* file->f_pos = 0; */ /* todo: ctx->pos? */
32693 +               vdir->vd_version = inode_query_iversion(inode);
32694 +               vdir->vd_last.ul = 0;
32695 +               vdir->vd_last.p.deblk = vdir->vd_deblk[0];
32696 +               if (allocated)
32697 +                       au_set_ivdir(inode, allocated);
32698 +       } else if (allocated)
32699 +               au_vdir_free(allocated);
32700 +
32701 +out:
32702 +       return err;
32703 +}
32704 +
32705 +static int copy_vdir(struct au_vdir *tgt, struct au_vdir *src)
32706 +{
32707 +       int err, rerr;
32708 +       unsigned long ul, n;
32709 +       const unsigned int deblk_sz = src->vd_deblk_sz;
32710 +
32711 +       AuDebugOn(tgt->vd_nblk != 1);
32712 +
32713 +       err = -ENOMEM;
32714 +       if (tgt->vd_nblk < src->vd_nblk) {
32715 +               unsigned char **p;
32716 +
32717 +               p = au_krealloc(tgt->vd_deblk, sizeof(*p) * src->vd_nblk,
32718 +                               GFP_NOFS, /*may_shrink*/0);
32719 +               if (unlikely(!p))
32720 +                       goto out;
32721 +               tgt->vd_deblk = p;
32722 +       }
32723 +
32724 +       if (tgt->vd_deblk_sz != deblk_sz) {
32725 +               unsigned char *p;
32726 +
32727 +               tgt->vd_deblk_sz = deblk_sz;
32728 +               p = au_krealloc(tgt->vd_deblk[0], deblk_sz, GFP_NOFS,
32729 +                               /*may_shrink*/1);
32730 +               if (unlikely(!p))
32731 +                       goto out;
32732 +               tgt->vd_deblk[0] = p;
32733 +       }
32734 +       memcpy(tgt->vd_deblk[0], src->vd_deblk[0], deblk_sz);
32735 +       tgt->vd_version = src->vd_version;
32736 +       tgt->vd_jiffy = src->vd_jiffy;
32737 +
32738 +       n = src->vd_nblk;
32739 +       for (ul = 1; ul < n; ul++) {
32740 +               tgt->vd_deblk[ul] = kmemdup(src->vd_deblk[ul], deblk_sz,
32741 +                                           GFP_NOFS);
32742 +               if (unlikely(!tgt->vd_deblk[ul]))
32743 +                       goto out;
32744 +               tgt->vd_nblk++;
32745 +       }
32746 +       tgt->vd_nblk = n;
32747 +       tgt->vd_last.ul = tgt->vd_last.ul;
32748 +       tgt->vd_last.p.deblk = tgt->vd_deblk[tgt->vd_last.ul];
32749 +       tgt->vd_last.p.deblk += src->vd_last.p.deblk
32750 +               - src->vd_deblk[src->vd_last.ul];
32751 +       /* smp_mb(); */
32752 +       return 0; /* success */
32753 +
32754 +out:
32755 +       rerr = reinit_vdir(tgt);
32756 +       BUG_ON(rerr);
32757 +       return err;
32758 +}
32759 +
32760 +int au_vdir_init(struct file *file)
32761 +{
32762 +       int err;
32763 +       struct inode *inode;
32764 +       struct au_vdir *vdir_cache, *allocated;
32765 +
32766 +       /* test file->f_pos here instead of ctx->pos */
32767 +       err = read_vdir(file, !file->f_pos);
32768 +       if (unlikely(err))
32769 +               goto out;
32770 +
32771 +       allocated = NULL;
32772 +       vdir_cache = au_fvdir_cache(file);
32773 +       if (!vdir_cache) {
32774 +               vdir_cache = alloc_vdir(file);
32775 +               err = PTR_ERR(vdir_cache);
32776 +               if (IS_ERR(vdir_cache))
32777 +                       goto out;
32778 +               allocated = vdir_cache;
32779 +       } else if (!file->f_pos && vdir_cache->vd_version != file->f_version) {
32780 +               /* test file->f_pos here instead of ctx->pos */
32781 +               err = reinit_vdir(vdir_cache);
32782 +               if (unlikely(err))
32783 +                       goto out;
32784 +       } else
32785 +               return 0; /* success */
32786 +
32787 +       inode = file_inode(file);
32788 +       err = copy_vdir(vdir_cache, au_ivdir(inode));
32789 +       if (!err) {
32790 +               file->f_version = inode_query_iversion(inode);
32791 +               if (allocated)
32792 +                       au_set_fvdir_cache(file, allocated);
32793 +       } else if (allocated)
32794 +               au_vdir_free(allocated);
32795 +
32796 +out:
32797 +       return err;
32798 +}
32799 +
32800 +static loff_t calc_offset(struct au_vdir *vdir)
32801 +{
32802 +       loff_t offset;
32803 +       union au_vdir_deblk_p p;
32804 +
32805 +       p.deblk = vdir->vd_deblk[vdir->vd_last.ul];
32806 +       offset = vdir->vd_last.p.deblk - p.deblk;
32807 +       offset += vdir->vd_deblk_sz * vdir->vd_last.ul;
32808 +       return offset;
32809 +}
32810 +
32811 +/* returns true or false */
32812 +static int seek_vdir(struct file *file, struct dir_context *ctx)
32813 +{
32814 +       int valid;
32815 +       unsigned int deblk_sz;
32816 +       unsigned long ul, n;
32817 +       loff_t offset;
32818 +       union au_vdir_deblk_p p, deblk_end;
32819 +       struct au_vdir *vdir_cache;
32820 +
32821 +       valid = 1;
32822 +       vdir_cache = au_fvdir_cache(file);
32823 +       offset = calc_offset(vdir_cache);
32824 +       AuDbg("offset %lld\n", offset);
32825 +       if (ctx->pos == offset)
32826 +               goto out;
32827 +
32828 +       vdir_cache->vd_last.ul = 0;
32829 +       vdir_cache->vd_last.p.deblk = vdir_cache->vd_deblk[0];
32830 +       if (!ctx->pos)
32831 +               goto out;
32832 +
32833 +       valid = 0;
32834 +       deblk_sz = vdir_cache->vd_deblk_sz;
32835 +       ul = div64_u64(ctx->pos, deblk_sz);
32836 +       AuDbg("ul %lu\n", ul);
32837 +       if (ul >= vdir_cache->vd_nblk)
32838 +               goto out;
32839 +
32840 +       n = vdir_cache->vd_nblk;
32841 +       for (; ul < n; ul++) {
32842 +               p.deblk = vdir_cache->vd_deblk[ul];
32843 +               deblk_end.deblk = p.deblk + deblk_sz;
32844 +               offset = ul;
32845 +               offset *= deblk_sz;
32846 +               while (!is_deblk_end(&p, &deblk_end) && offset < ctx->pos) {
32847 +                       unsigned int l;
32848 +
32849 +                       l = calc_size(p.de->de_str.len);
32850 +                       offset += l;
32851 +                       p.deblk += l;
32852 +               }
32853 +               if (!is_deblk_end(&p, &deblk_end)) {
32854 +                       valid = 1;
32855 +                       vdir_cache->vd_last.ul = ul;
32856 +                       vdir_cache->vd_last.p = p;
32857 +                       break;
32858 +               }
32859 +       }
32860 +
32861 +out:
32862 +       /* smp_mb(); */
32863 +       if (!valid)
32864 +               AuDbg("valid %d\n", !valid);
32865 +       return valid;
32866 +}
32867 +
32868 +int au_vdir_fill_de(struct file *file, struct dir_context *ctx)
32869 +{
32870 +       unsigned int l, deblk_sz;
32871 +       union au_vdir_deblk_p deblk_end;
32872 +       struct au_vdir *vdir_cache;
32873 +       struct au_vdir_de *de;
32874 +
32875 +       if (!seek_vdir(file, ctx))
32876 +               return 0;
32877 +
32878 +       vdir_cache = au_fvdir_cache(file);
32879 +       deblk_sz = vdir_cache->vd_deblk_sz;
32880 +       while (1) {
32881 +               deblk_end.deblk = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
32882 +               deblk_end.deblk += deblk_sz;
32883 +               while (!is_deblk_end(&vdir_cache->vd_last.p, &deblk_end)) {
32884 +                       de = vdir_cache->vd_last.p.de;
32885 +                       AuDbg("%.*s, off%lld, i%lu, dt%d\n",
32886 +                             de->de_str.len, de->de_str.name, ctx->pos,
32887 +                             (unsigned long)de->de_ino, de->de_type);
32888 +                       if (unlikely(!dir_emit(ctx, de->de_str.name,
32889 +                                              de->de_str.len, de->de_ino,
32890 +                                              de->de_type))) {
32891 +                               /* todo: ignore the error caused by udba? */
32892 +                               /* return err; */
32893 +                               return 0;
32894 +                       }
32895 +
32896 +                       l = calc_size(de->de_str.len);
32897 +                       vdir_cache->vd_last.p.deblk += l;
32898 +                       ctx->pos += l;
32899 +               }
32900 +               if (vdir_cache->vd_last.ul < vdir_cache->vd_nblk - 1) {
32901 +                       vdir_cache->vd_last.ul++;
32902 +                       vdir_cache->vd_last.p.deblk
32903 +                               = vdir_cache->vd_deblk[vdir_cache->vd_last.ul];
32904 +                       ctx->pos = deblk_sz * vdir_cache->vd_last.ul;
32905 +                       continue;
32906 +               }
32907 +               break;
32908 +       }
32909 +
32910 +       /* smp_mb(); */
32911 +       return 0;
32912 +}
32913 diff -urN /usr/share/empty/fs/aufs/vfsub.c linux/fs/aufs/vfsub.c
32914 --- /usr/share/empty/fs/aufs/vfsub.c    1970-01-01 01:00:00.000000000 +0100
32915 +++ linux/fs/aufs/vfsub.c       2022-03-21 14:49:05.733299677 +0100
32916 @@ -0,0 +1,918 @@
32917 +// SPDX-License-Identifier: GPL-2.0
32918 +/*
32919 + * Copyright (C) 2005-2021 Junjiro R. Okajima
32920 + *
32921 + * This program, aufs is free software; you can redistribute it and/or modify
32922 + * it under the terms of the GNU General Public License as published by
32923 + * the Free Software Foundation; either version 2 of the License, or
32924 + * (at your option) any later version.
32925 + *
32926 + * This program is distributed in the hope that it will be useful,
32927 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
32928 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
32929 + * GNU General Public License for more details.
32930 + *
32931 + * You should have received a copy of the GNU General Public License
32932 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
32933 + */
32934 +
32935 +/*
32936 + * sub-routines for VFS
32937 + */
32938 +
32939 +#include <linux/mnt_namespace.h>
32940 +#include <linux/nsproxy.h>
32941 +#include <linux/security.h>
32942 +#include <linux/splice.h>
32943 +#include "aufs.h"
32944 +
32945 +#ifdef CONFIG_AUFS_BR_FUSE
32946 +int vfsub_test_mntns(struct vfsmount *mnt, struct super_block *h_sb)
32947 +{
32948 +       if (!au_test_fuse(h_sb) || !au_userns)
32949 +               return 0;
32950 +
32951 +       return is_current_mnt_ns(mnt) ? 0 : -EACCES;
32952 +}
32953 +#endif
32954 +
32955 +int vfsub_sync_filesystem(struct super_block *h_sb)
32956 +{
32957 +       int err;
32958 +
32959 +       lockdep_off();
32960 +       down_read(&h_sb->s_umount);
32961 +       err = sync_filesystem(h_sb);
32962 +       up_read(&h_sb->s_umount);
32963 +       lockdep_on();
32964 +
32965 +       return err;
32966 +}
32967 +
32968 +/* ---------------------------------------------------------------------- */
32969 +
32970 +int vfsub_update_h_iattr(struct path *h_path, int *did)
32971 +{
32972 +       int err;
32973 +       struct kstat st;
32974 +       struct super_block *h_sb;
32975 +
32976 +       /*
32977 +        * Always needs h_path->mnt for LSM or FUSE branch.
32978 +        */
32979 +       AuDebugOn(!h_path->mnt);
32980 +
32981 +       /* for remote fs, leave work for its getattr or d_revalidate */
32982 +       /* for bad i_attr fs, handle them in aufs_getattr() */
32983 +       /* still some fs may acquire i_mutex. we need to skip them */
32984 +       err = 0;
32985 +       if (!did)
32986 +               did = &err;
32987 +       h_sb = h_path->dentry->d_sb;
32988 +       *did = (!au_test_fs_remote(h_sb) && au_test_fs_refresh_iattr(h_sb));
32989 +       if (*did)
32990 +               err = vfsub_getattr(h_path, &st);
32991 +
32992 +       return err;
32993 +}
32994 +
32995 +/* ---------------------------------------------------------------------- */
32996 +
32997 +struct file *vfsub_dentry_open(struct path *path, int flags)
32998 +{
32999 +       return dentry_open(path, flags /* | __FMODE_NONOTIFY */,
33000 +                          current_cred());
33001 +}
33002 +
33003 +struct file *vfsub_filp_open(const char *path, int oflags, int mode)
33004 +{
33005 +       struct file *file;
33006 +
33007 +       lockdep_off();
33008 +       file = filp_open(path,
33009 +                        oflags /* | __FMODE_NONOTIFY */,
33010 +                        mode);
33011 +       lockdep_on();
33012 +       if (IS_ERR(file))
33013 +               goto out;
33014 +       vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33015 +
33016 +out:
33017 +       return file;
33018 +}
33019 +
33020 +/*
33021 + * Ideally this function should call VFS:do_last() in order to keep all its
33022 + * checkings. But it is very hard for aufs to regenerate several VFS internal
33023 + * structure such as nameidata. This is a second (or third) best approach.
33024 + * cf. linux/fs/namei.c:do_last(), lookup_open() and atomic_open().
33025 + */
33026 +int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
33027 +                     struct vfsub_aopen_args *args)
33028 +{
33029 +       int err;
33030 +       struct au_branch *br = args->br;
33031 +       struct file *file = args->file;
33032 +       /* copied from linux/fs/namei.c:atomic_open() */
33033 +       struct dentry *const DENTRY_NOT_SET = (void *)-1UL;
33034 +
33035 +       IMustLock(dir);
33036 +       AuDebugOn(!dir->i_op->atomic_open);
33037 +
33038 +       err = au_br_test_oflag(args->open_flag, br);
33039 +       if (unlikely(err))
33040 +               goto out;
33041 +
33042 +       au_lcnt_inc(&br->br_nfiles);
33043 +       file->f_path.dentry = DENTRY_NOT_SET;
33044 +       file->f_path.mnt = au_br_mnt(br);
33045 +       AuDbg("%ps\n", dir->i_op->atomic_open);
33046 +       err = dir->i_op->atomic_open(dir, dentry, file, args->open_flag,
33047 +                                    args->create_mode);
33048 +       if (unlikely(err < 0)) {
33049 +               au_lcnt_dec(&br->br_nfiles);
33050 +               goto out;
33051 +       }
33052 +
33053 +       /* temporary workaround for nfsv4 branch */
33054 +       if (au_test_nfs(dir->i_sb))
33055 +               nfs_mark_for_revalidate(dir);
33056 +
33057 +       if (file->f_mode & FMODE_CREATED)
33058 +               fsnotify_create(dir, dentry);
33059 +       if (!(file->f_mode & FMODE_OPENED)) {
33060 +               au_lcnt_dec(&br->br_nfiles);
33061 +               goto out;
33062 +       }
33063 +
33064 +       /* todo: call VFS:may_open() here */
33065 +       /* todo: ima_file_check() too? */
33066 +       if (!err && (args->open_flag & __FMODE_EXEC))
33067 +               err = deny_write_access(file);
33068 +       if (!err)
33069 +               fsnotify_open(file);
33070 +       else
33071 +               au_lcnt_dec(&br->br_nfiles);
33072 +       /* note that the file is created and still opened */
33073 +
33074 +out:
33075 +       return err;
33076 +}
33077 +
33078 +int vfsub_kern_path(const char *name, unsigned int flags, struct path *path)
33079 +{
33080 +       int err;
33081 +
33082 +       err = kern_path(name, flags, path);
33083 +       if (!err && d_is_positive(path->dentry))
33084 +               vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
33085 +       return err;
33086 +}
33087 +
33088 +struct dentry *vfsub_lookup_one_len_unlocked(const char *name,
33089 +                                            struct path *ppath, int len)
33090 +{
33091 +       struct path path;
33092 +
33093 +       path.dentry = lookup_one_len_unlocked(name, ppath->dentry, len);
33094 +       if (IS_ERR(path.dentry))
33095 +               goto out;
33096 +       if (d_is_positive(path.dentry)) {
33097 +               path.mnt = ppath->mnt;
33098 +               vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
33099 +       }
33100 +
33101 +out:
33102 +       AuTraceErrPtr(path.dentry);
33103 +       return path.dentry;
33104 +}
33105 +
33106 +struct dentry *vfsub_lookup_one_len(const char *name, struct path *ppath,
33107 +                                   int len)
33108 +{
33109 +       struct path path;
33110 +
33111 +       /* VFS checks it too, but by WARN_ON_ONCE() */
33112 +       IMustLock(d_inode(ppath->dentry));
33113 +
33114 +       path.dentry = lookup_one_len(name, ppath->dentry, len);
33115 +       if (IS_ERR(path.dentry))
33116 +               goto out;
33117 +       if (d_is_positive(path.dentry)) {
33118 +               path.mnt = ppath->mnt;
33119 +               vfsub_update_h_iattr(&path, /*did*/NULL); /*ignore*/
33120 +       }
33121 +
33122 +out:
33123 +       AuTraceErrPtr(path.dentry);
33124 +       return path.dentry;
33125 +}
33126 +
33127 +void vfsub_call_lkup_one(void *args)
33128 +{
33129 +       struct vfsub_lkup_one_args *a = args;
33130 +       *a->errp = vfsub_lkup_one(a->name, a->ppath);
33131 +}
33132 +
33133 +/* ---------------------------------------------------------------------- */
33134 +
33135 +struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
33136 +                                struct dentry *d2, struct au_hinode *hdir2)
33137 +{
33138 +       struct dentry *d;
33139 +
33140 +       lockdep_off();
33141 +       d = lock_rename(d1, d2);
33142 +       lockdep_on();
33143 +       au_hn_suspend(hdir1);
33144 +       if (hdir1 != hdir2)
33145 +               au_hn_suspend(hdir2);
33146 +
33147 +       return d;
33148 +}
33149 +
33150 +void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
33151 +                        struct dentry *d2, struct au_hinode *hdir2)
33152 +{
33153 +       au_hn_resume(hdir1);
33154 +       if (hdir1 != hdir2)
33155 +               au_hn_resume(hdir2);
33156 +       lockdep_off();
33157 +       unlock_rename(d1, d2);
33158 +       lockdep_on();
33159 +}
33160 +
33161 +/* ---------------------------------------------------------------------- */
33162 +
33163 +int vfsub_create(struct inode *dir, struct path *path, int mode, bool want_excl)
33164 +{
33165 +       int err;
33166 +       struct dentry *d;
33167 +       struct user_namespace *userns;
33168 +
33169 +       IMustLock(dir);
33170 +
33171 +       d = path->dentry;
33172 +       path->dentry = d->d_parent;
33173 +       err = security_path_mknod(path, d, mode, 0);
33174 +       path->dentry = d;
33175 +       if (unlikely(err))
33176 +               goto out;
33177 +       userns = mnt_user_ns(path->mnt);
33178 +
33179 +       lockdep_off();
33180 +       err = vfs_create(userns, dir, path->dentry, mode, want_excl);
33181 +       lockdep_on();
33182 +       if (!err) {
33183 +               struct path tmp = *path;
33184 +               int did;
33185 +
33186 +               vfsub_update_h_iattr(&tmp, &did);
33187 +               if (did) {
33188 +                       tmp.dentry = path->dentry->d_parent;
33189 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33190 +               }
33191 +               /*ignore*/
33192 +       }
33193 +
33194 +out:
33195 +       return err;
33196 +}
33197 +
33198 +int vfsub_symlink(struct inode *dir, struct path *path, const char *symname)
33199 +{
33200 +       int err;
33201 +       struct dentry *d;
33202 +       struct user_namespace *userns;
33203 +
33204 +       IMustLock(dir);
33205 +
33206 +       d = path->dentry;
33207 +       path->dentry = d->d_parent;
33208 +       err = security_path_symlink(path, d, symname);
33209 +       path->dentry = d;
33210 +       if (unlikely(err))
33211 +               goto out;
33212 +       userns = mnt_user_ns(path->mnt);
33213 +
33214 +       lockdep_off();
33215 +       err = vfs_symlink(userns, dir, path->dentry, symname);
33216 +       lockdep_on();
33217 +       if (!err) {
33218 +               struct path tmp = *path;
33219 +               int did;
33220 +
33221 +               vfsub_update_h_iattr(&tmp, &did);
33222 +               if (did) {
33223 +                       tmp.dentry = path->dentry->d_parent;
33224 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33225 +               }
33226 +               /*ignore*/
33227 +       }
33228 +
33229 +out:
33230 +       return err;
33231 +}
33232 +
33233 +int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev)
33234 +{
33235 +       int err;
33236 +       struct dentry *d;
33237 +       struct user_namespace *userns;
33238 +
33239 +       IMustLock(dir);
33240 +
33241 +       d = path->dentry;
33242 +       path->dentry = d->d_parent;
33243 +       err = security_path_mknod(path, d, mode, new_encode_dev(dev));
33244 +       path->dentry = d;
33245 +       if (unlikely(err))
33246 +               goto out;
33247 +       userns = mnt_user_ns(path->mnt);
33248 +
33249 +       lockdep_off();
33250 +       err = vfs_mknod(userns, dir, path->dentry, mode, dev);
33251 +       lockdep_on();
33252 +       if (!err) {
33253 +               struct path tmp = *path;
33254 +               int did;
33255 +
33256 +               vfsub_update_h_iattr(&tmp, &did);
33257 +               if (did) {
33258 +                       tmp.dentry = path->dentry->d_parent;
33259 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33260 +               }
33261 +               /*ignore*/
33262 +       }
33263 +
33264 +out:
33265 +       return err;
33266 +}
33267 +
33268 +static int au_test_nlink(struct inode *inode)
33269 +{
33270 +       const unsigned int link_max = UINT_MAX >> 1; /* rough margin */
33271 +
33272 +       if (!au_test_fs_no_limit_nlink(inode->i_sb)
33273 +           || inode->i_nlink < link_max)
33274 +               return 0;
33275 +       return -EMLINK;
33276 +}
33277 +
33278 +int vfsub_link(struct dentry *src_dentry, struct inode *dir, struct path *path,
33279 +              struct inode **delegated_inode)
33280 +{
33281 +       int err;
33282 +       struct dentry *d;
33283 +       struct user_namespace *userns;
33284 +
33285 +       IMustLock(dir);
33286 +
33287 +       err = au_test_nlink(d_inode(src_dentry));
33288 +       if (unlikely(err))
33289 +               return err;
33290 +
33291 +       /* we don't call may_linkat() */
33292 +       d = path->dentry;
33293 +       path->dentry = d->d_parent;
33294 +       err = security_path_link(src_dentry, path, d);
33295 +       path->dentry = d;
33296 +       if (unlikely(err))
33297 +               goto out;
33298 +       userns = mnt_user_ns(path->mnt);
33299 +
33300 +       lockdep_off();
33301 +       err = vfs_link(src_dentry, userns, dir, path->dentry, delegated_inode);
33302 +       lockdep_on();
33303 +       if (!err) {
33304 +               struct path tmp = *path;
33305 +               int did;
33306 +
33307 +               /* fuse has different memory inode for the same inumber */
33308 +               vfsub_update_h_iattr(&tmp, &did);
33309 +               if (did) {
33310 +                       tmp.dentry = path->dentry->d_parent;
33311 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33312 +                       tmp.dentry = src_dentry;
33313 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33314 +               }
33315 +               /*ignore*/
33316 +       }
33317 +
33318 +out:
33319 +       return err;
33320 +}
33321 +
33322 +int vfsub_rename(struct inode *src_dir, struct dentry *src_dentry,
33323 +                struct inode *dir, struct path *path,
33324 +                struct inode **delegated_inode, unsigned int flags)
33325 +{
33326 +       int err;
33327 +       struct renamedata rd;
33328 +       struct path tmp = {
33329 +               .mnt    = path->mnt
33330 +       };
33331 +       struct dentry *d;
33332 +
33333 +       IMustLock(dir);
33334 +       IMustLock(src_dir);
33335 +
33336 +       d = path->dentry;
33337 +       path->dentry = d->d_parent;
33338 +       tmp.dentry = src_dentry->d_parent;
33339 +       err = security_path_rename(&tmp, src_dentry, path, d, /*flags*/0);
33340 +       path->dentry = d;
33341 +       if (unlikely(err))
33342 +               goto out;
33343 +
33344 +       rd.old_mnt_userns = mnt_user_ns(path->mnt);
33345 +       rd.old_dir = src_dir;
33346 +       rd.old_dentry = src_dentry;
33347 +       rd.new_mnt_userns = rd.old_mnt_userns;
33348 +       rd.new_dir = dir;
33349 +       rd.new_dentry = path->dentry;
33350 +       rd.delegated_inode = delegated_inode;
33351 +       rd.flags = flags;
33352 +       lockdep_off();
33353 +       err = vfs_rename(&rd);
33354 +       lockdep_on();
33355 +       if (!err) {
33356 +               int did;
33357 +
33358 +               tmp.dentry = d->d_parent;
33359 +               vfsub_update_h_iattr(&tmp, &did);
33360 +               if (did) {
33361 +                       tmp.dentry = src_dentry;
33362 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33363 +                       tmp.dentry = src_dentry->d_parent;
33364 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33365 +               }
33366 +               /*ignore*/
33367 +       }
33368 +
33369 +out:
33370 +       return err;
33371 +}
33372 +
33373 +int vfsub_mkdir(struct inode *dir, struct path *path, int mode)
33374 +{
33375 +       int err;
33376 +       struct dentry *d;
33377 +       struct user_namespace *userns;
33378 +
33379 +       IMustLock(dir);
33380 +
33381 +       d = path->dentry;
33382 +       path->dentry = d->d_parent;
33383 +       err = security_path_mkdir(path, d, mode);
33384 +       path->dentry = d;
33385 +       if (unlikely(err))
33386 +               goto out;
33387 +       userns = mnt_user_ns(path->mnt);
33388 +
33389 +       lockdep_off();
33390 +       err = vfs_mkdir(userns, dir, path->dentry, mode);
33391 +       lockdep_on();
33392 +       if (!err) {
33393 +               struct path tmp = *path;
33394 +               int did;
33395 +
33396 +               vfsub_update_h_iattr(&tmp, &did);
33397 +               if (did) {
33398 +                       tmp.dentry = path->dentry->d_parent;
33399 +                       vfsub_update_h_iattr(&tmp, /*did*/NULL);
33400 +               }
33401 +               /*ignore*/
33402 +       }
33403 +
33404 +out:
33405 +       return err;
33406 +}
33407 +
33408 +int vfsub_rmdir(struct inode *dir, struct path *path)
33409 +{
33410 +       int err;
33411 +       struct dentry *d;
33412 +       struct user_namespace *userns;
33413 +
33414 +       IMustLock(dir);
33415 +
33416 +       d = path->dentry;
33417 +       path->dentry = d->d_parent;
33418 +       err = security_path_rmdir(path, d);
33419 +       path->dentry = d;
33420 +       if (unlikely(err))
33421 +               goto out;
33422 +       userns = mnt_user_ns(path->mnt);
33423 +
33424 +       lockdep_off();
33425 +       err = vfs_rmdir(userns, dir, path->dentry);
33426 +       lockdep_on();
33427 +       if (!err) {
33428 +               struct path tmp = {
33429 +                       .dentry = path->dentry->d_parent,
33430 +                       .mnt    = path->mnt
33431 +               };
33432 +
33433 +               vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
33434 +       }
33435 +
33436 +out:
33437 +       return err;
33438 +}
33439 +
33440 +/* ---------------------------------------------------------------------- */
33441 +
33442 +/* todo: support mmap_sem? */
33443 +ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
33444 +                    loff_t *ppos)
33445 +{
33446 +       ssize_t err;
33447 +
33448 +       lockdep_off();
33449 +       err = vfs_read(file, ubuf, count, ppos);
33450 +       lockdep_on();
33451 +       if (err >= 0)
33452 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33453 +       return err;
33454 +}
33455 +
33456 +ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
33457 +                    loff_t *ppos)
33458 +{
33459 +       ssize_t err;
33460 +
33461 +       lockdep_off();
33462 +       err = kernel_read(file, kbuf, count, ppos);
33463 +       lockdep_on();
33464 +       AuTraceErr(err);
33465 +       if (err >= 0)
33466 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33467 +       return err;
33468 +}
33469 +
33470 +ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
33471 +                     loff_t *ppos)
33472 +{
33473 +       ssize_t err;
33474 +
33475 +       lockdep_off();
33476 +       err = vfs_write(file, ubuf, count, ppos);
33477 +       lockdep_on();
33478 +       if (err >= 0)
33479 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33480 +       return err;
33481 +}
33482 +
33483 +ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count, loff_t *ppos)
33484 +{
33485 +       ssize_t err;
33486 +
33487 +       lockdep_off();
33488 +       err = kernel_write(file, kbuf, count, ppos);
33489 +       lockdep_on();
33490 +       if (err >= 0)
33491 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33492 +       return err;
33493 +}
33494 +
33495 +int vfsub_flush(struct file *file, fl_owner_t id)
33496 +{
33497 +       int err;
33498 +
33499 +       err = 0;
33500 +       if (file->f_op->flush) {
33501 +               if (!au_test_nfs(file->f_path.dentry->d_sb))
33502 +                       err = file->f_op->flush(file, id);
33503 +               else {
33504 +                       lockdep_off();
33505 +                       err = file->f_op->flush(file, id);
33506 +                       lockdep_on();
33507 +               }
33508 +               if (!err)
33509 +                       vfsub_update_h_iattr(&file->f_path, /*did*/NULL);
33510 +               /*ignore*/
33511 +       }
33512 +       return err;
33513 +}
33514 +
33515 +int vfsub_iterate_dir(struct file *file, struct dir_context *ctx)
33516 +{
33517 +       int err;
33518 +
33519 +       AuDbg("%pD, ctx{%ps, %llu}\n", file, ctx->actor, ctx->pos);
33520 +
33521 +       lockdep_off();
33522 +       err = iterate_dir(file, ctx);
33523 +       lockdep_on();
33524 +       if (err >= 0)
33525 +               vfsub_update_h_iattr(&file->f_path, /*did*/NULL); /*ignore*/
33526 +
33527 +       return err;
33528 +}
33529 +
33530 +long vfsub_splice_to(struct file *in, loff_t *ppos,
33531 +                    struct pipe_inode_info *pipe, size_t len,
33532 +                    unsigned int flags)
33533 +{
33534 +       long err;
33535 +
33536 +       lockdep_off();
33537 +       err = do_splice_to(in, ppos, pipe, len, flags);
33538 +       lockdep_on();
33539 +       file_accessed(in);
33540 +       if (err >= 0)
33541 +               vfsub_update_h_iattr(&in->f_path, /*did*/NULL); /*ignore*/
33542 +       return err;
33543 +}
33544 +
33545 +long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
33546 +                      loff_t *ppos, size_t len, unsigned int flags)
33547 +{
33548 +       long err;
33549 +
33550 +       lockdep_off();
33551 +       err = do_splice_from(pipe, out, ppos, len, flags);
33552 +       lockdep_on();
33553 +       if (err >= 0)
33554 +               vfsub_update_h_iattr(&out->f_path, /*did*/NULL); /*ignore*/
33555 +       return err;
33556 +}
33557 +
33558 +int vfsub_fsync(struct file *file, struct path *path, int datasync)
33559 +{
33560 +       int err;
33561 +
33562 +       /* file can be NULL */
33563 +       lockdep_off();
33564 +       err = vfs_fsync(file, datasync);
33565 +       lockdep_on();
33566 +       if (!err) {
33567 +               if (!path) {
33568 +                       AuDebugOn(!file);
33569 +                       path = &file->f_path;
33570 +               }
33571 +               vfsub_update_h_iattr(path, /*did*/NULL); /*ignore*/
33572 +       }
33573 +       return err;
33574 +}
33575 +
33576 +/* cf. open.c:do_sys_truncate() and do_sys_ftruncate() */
33577 +int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
33578 +               struct file *h_file)
33579 +{
33580 +       int err;
33581 +       struct inode *h_inode;
33582 +       struct super_block *h_sb;
33583 +       struct user_namespace *h_userns;
33584 +
33585 +       if (!h_file) {
33586 +               err = vfsub_truncate(h_path, length);
33587 +               goto out;
33588 +       }
33589 +
33590 +       h_inode = d_inode(h_path->dentry);
33591 +       h_sb = h_inode->i_sb;
33592 +       lockdep_off();
33593 +       sb_start_write(h_sb);
33594 +       lockdep_on();
33595 +       err = security_path_truncate(h_path);
33596 +       if (!err) {
33597 +               h_userns = mnt_user_ns(h_path->mnt);
33598 +               lockdep_off();
33599 +               err = do_truncate(h_userns, h_path->dentry, length, attr,
33600 +                                 h_file);
33601 +               lockdep_on();
33602 +       }
33603 +       lockdep_off();
33604 +       sb_end_write(h_sb);
33605 +       lockdep_on();
33606 +
33607 +out:
33608 +       return err;
33609 +}
33610 +
33611 +/* ---------------------------------------------------------------------- */
33612 +
33613 +struct au_vfsub_mkdir_args {
33614 +       int *errp;
33615 +       struct inode *dir;
33616 +       struct path *path;
33617 +       int mode;
33618 +};
33619 +
33620 +static void au_call_vfsub_mkdir(void *args)
33621 +{
33622 +       struct au_vfsub_mkdir_args *a = args;
33623 +       *a->errp = vfsub_mkdir(a->dir, a->path, a->mode);
33624 +}
33625 +
33626 +int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode)
33627 +{
33628 +       int err, do_sio, wkq_err;
33629 +       struct user_namespace *userns;
33630 +
33631 +       userns = mnt_user_ns(path->mnt);
33632 +       do_sio = au_test_h_perm_sio(userns, dir, MAY_EXEC | MAY_WRITE);
33633 +       if (!do_sio) {
33634 +               lockdep_off();
33635 +               err = vfsub_mkdir(dir, path, mode);
33636 +               lockdep_on();
33637 +       } else {
33638 +               struct au_vfsub_mkdir_args args = {
33639 +                       .errp   = &err,
33640 +                       .dir    = dir,
33641 +                       .path   = path,
33642 +                       .mode   = mode
33643 +               };
33644 +               wkq_err = au_wkq_wait(au_call_vfsub_mkdir, &args);
33645 +               if (unlikely(wkq_err))
33646 +                       err = wkq_err;
33647 +       }
33648 +
33649 +       return err;
33650 +}
33651 +
33652 +struct au_vfsub_rmdir_args {
33653 +       int *errp;
33654 +       struct inode *dir;
33655 +       struct path *path;
33656 +};
33657 +
33658 +static void au_call_vfsub_rmdir(void *args)
33659 +{
33660 +       struct au_vfsub_rmdir_args *a = args;
33661 +       *a->errp = vfsub_rmdir(a->dir, a->path);
33662 +}
33663 +
33664 +int vfsub_sio_rmdir(struct inode *dir, struct path *path)
33665 +{
33666 +       int err, do_sio, wkq_err;
33667 +       struct user_namespace *userns;
33668 +
33669 +       userns = mnt_user_ns(path->mnt);
33670 +       do_sio = au_test_h_perm_sio(userns, dir, MAY_EXEC | MAY_WRITE);
33671 +       if (!do_sio) {
33672 +               lockdep_off();
33673 +               err = vfsub_rmdir(dir, path);
33674 +               lockdep_on();
33675 +       } else {
33676 +               struct au_vfsub_rmdir_args args = {
33677 +                       .errp   = &err,
33678 +                       .dir    = dir,
33679 +                       .path   = path
33680 +               };
33681 +               wkq_err = au_wkq_wait(au_call_vfsub_rmdir, &args);
33682 +               if (unlikely(wkq_err))
33683 +                       err = wkq_err;
33684 +       }
33685 +
33686 +       return err;
33687 +}
33688 +
33689 +/* ---------------------------------------------------------------------- */
33690 +
33691 +struct notify_change_args {
33692 +       int *errp;
33693 +       struct path *path;
33694 +       struct iattr *ia;
33695 +       struct inode **delegated_inode;
33696 +};
33697 +
33698 +static void call_notify_change(void *args)
33699 +{
33700 +       struct notify_change_args *a = args;
33701 +       struct inode *h_inode;
33702 +       struct user_namespace *userns;
33703 +
33704 +       h_inode = d_inode(a->path->dentry);
33705 +       IMustLock(h_inode);
33706 +
33707 +       *a->errp = -EPERM;
33708 +       if (!IS_IMMUTABLE(h_inode) && !IS_APPEND(h_inode)) {
33709 +               userns = mnt_user_ns(a->path->mnt);
33710 +               lockdep_off();
33711 +               *a->errp = notify_change(userns, a->path->dentry, a->ia,
33712 +                                        a->delegated_inode);
33713 +               lockdep_on();
33714 +               if (!*a->errp)
33715 +                       vfsub_update_h_iattr(a->path, /*did*/NULL); /*ignore*/
33716 +       }
33717 +       AuTraceErr(*a->errp);
33718 +}
33719 +
33720 +int vfsub_notify_change(struct path *path, struct iattr *ia,
33721 +                       struct inode **delegated_inode)
33722 +{
33723 +       int err;
33724 +       struct notify_change_args args = {
33725 +               .errp                   = &err,
33726 +               .path                   = path,
33727 +               .ia                     = ia,
33728 +               .delegated_inode        = delegated_inode
33729 +       };
33730 +
33731 +       call_notify_change(&args);
33732 +
33733 +       return err;
33734 +}
33735 +
33736 +int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
33737 +                           struct inode **delegated_inode)
33738 +{
33739 +       int err, wkq_err;
33740 +       struct notify_change_args args = {
33741 +               .errp                   = &err,
33742 +               .path                   = path,
33743 +               .ia                     = ia,
33744 +               .delegated_inode        = delegated_inode
33745 +       };
33746 +
33747 +       wkq_err = au_wkq_wait(call_notify_change, &args);
33748 +       if (unlikely(wkq_err))
33749 +               err = wkq_err;
33750 +
33751 +       return err;
33752 +}
33753 +
33754 +/* ---------------------------------------------------------------------- */
33755 +
33756 +struct unlink_args {
33757 +       int *errp;
33758 +       struct inode *dir;
33759 +       struct path *path;
33760 +       struct inode **delegated_inode;
33761 +};
33762 +
33763 +static void call_unlink(void *args)
33764 +{
33765 +       struct unlink_args *a = args;
33766 +       struct dentry *d = a->path->dentry;
33767 +       struct inode *h_inode;
33768 +       struct user_namespace *userns;
33769 +       const int stop_sillyrename = (au_test_nfs(d->d_sb)
33770 +                                     && au_dcount(d) == 1);
33771 +
33772 +       IMustLock(a->dir);
33773 +
33774 +       a->path->dentry = d->d_parent;
33775 +       *a->errp = security_path_unlink(a->path, d);
33776 +       a->path->dentry = d;
33777 +       if (unlikely(*a->errp))
33778 +               return;
33779 +
33780 +       if (!stop_sillyrename)
33781 +               dget(d);
33782 +       h_inode = NULL;
33783 +       if (d_is_positive(d)) {
33784 +               h_inode = d_inode(d);
33785 +               ihold(h_inode);
33786 +       }
33787 +
33788 +       userns = mnt_user_ns(a->path->mnt);
33789 +       lockdep_off();
33790 +       *a->errp = vfs_unlink(userns, a->dir, d, a->delegated_inode);
33791 +       lockdep_on();
33792 +       if (!*a->errp) {
33793 +               struct path tmp = {
33794 +                       .dentry = d->d_parent,
33795 +                       .mnt    = a->path->mnt
33796 +               };
33797 +               vfsub_update_h_iattr(&tmp, /*did*/NULL); /*ignore*/
33798 +       }
33799 +
33800 +       if (!stop_sillyrename)
33801 +               dput(d);
33802 +       if (h_inode)
33803 +               iput(h_inode);
33804 +
33805 +       AuTraceErr(*a->errp);
33806 +}
33807 +
33808 +/*
33809 + * @dir: must be locked.
33810 + * @dentry: target dentry.
33811 + */
33812 +int vfsub_unlink(struct inode *dir, struct path *path,
33813 +                struct inode **delegated_inode, int force)
33814 +{
33815 +       int err;
33816 +       struct unlink_args args = {
33817 +               .errp                   = &err,
33818 +               .dir                    = dir,
33819 +               .path                   = path,
33820 +               .delegated_inode        = delegated_inode
33821 +       };
33822 +
33823 +       if (!force)
33824 +               call_unlink(&args);
33825 +       else {
33826 +               int wkq_err;
33827 +
33828 +               wkq_err = au_wkq_wait(call_unlink, &args);
33829 +               if (unlikely(wkq_err))
33830 +                       err = wkq_err;
33831 +       }
33832 +
33833 +       return err;
33834 +}
33835 diff -urN /usr/share/empty/fs/aufs/vfsub.h linux/fs/aufs/vfsub.h
33836 --- /usr/share/empty/fs/aufs/vfsub.h    1970-01-01 01:00:00.000000000 +0100
33837 +++ linux/fs/aufs/vfsub.h       2022-03-21 14:49:05.733299677 +0100
33838 @@ -0,0 +1,358 @@
33839 +/* SPDX-License-Identifier: GPL-2.0 */
33840 +/*
33841 + * Copyright (C) 2005-2021 Junjiro R. Okajima
33842 + *
33843 + * This program, aufs is free software; you can redistribute it and/or modify
33844 + * it under the terms of the GNU General Public License as published by
33845 + * the Free Software Foundation; either version 2 of the License, or
33846 + * (at your option) any later version.
33847 + *
33848 + * This program is distributed in the hope that it will be useful,
33849 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
33850 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33851 + * GNU General Public License for more details.
33852 + *
33853 + * You should have received a copy of the GNU General Public License
33854 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
33855 + */
33856 +
33857 +/*
33858 + * sub-routines for VFS
33859 + */
33860 +
33861 +#ifndef __AUFS_VFSUB_H__
33862 +#define __AUFS_VFSUB_H__
33863 +
33864 +#ifdef __KERNEL__
33865 +
33866 +#include <linux/fs.h>
33867 +#include <linux/mount.h>
33868 +#include <linux/posix_acl.h>
33869 +#include <linux/xattr.h>
33870 +#include "debug.h"
33871 +
33872 +/* copied from linux/fs/internal.h */
33873 +/* todo: BAD approach!! */
33874 +extern void __mnt_drop_write(struct vfsmount *);
33875 +extern struct file *alloc_empty_file(int, const struct cred *);
33876 +
33877 +/* ---------------------------------------------------------------------- */
33878 +
33879 +/* lock subclass for lower inode */
33880 +/* default MAX_LOCKDEP_SUBCLASSES(8) is not enough */
33881 +/* reduce? gave up. */
33882 +enum {
33883 +       AuLsc_I_Begin = I_MUTEX_PARENT2, /* 5 */
33884 +       AuLsc_I_PARENT,         /* lower inode, parent first */
33885 +       AuLsc_I_PARENT2,        /* copyup dirs */
33886 +       AuLsc_I_PARENT3,        /* copyup wh */
33887 +       AuLsc_I_CHILD,
33888 +       AuLsc_I_CHILD2,
33889 +       AuLsc_I_End
33890 +};
33891 +
33892 +/* to debug easier, do not make them inlined functions */
33893 +#define MtxMustLock(mtx)       AuDebugOn(!mutex_is_locked(mtx))
33894 +#define IMustLock(i)           AuDebugOn(!inode_is_locked(i))
33895 +
33896 +/* ---------------------------------------------------------------------- */
33897 +
33898 +static inline void vfsub_drop_nlink(struct inode *inode)
33899 +{
33900 +       AuDebugOn(!inode->i_nlink);
33901 +       drop_nlink(inode);
33902 +}
33903 +
33904 +static inline void vfsub_dead_dir(struct inode *inode)
33905 +{
33906 +       AuDebugOn(!S_ISDIR(inode->i_mode));
33907 +       inode->i_flags |= S_DEAD;
33908 +       clear_nlink(inode);
33909 +}
33910 +
33911 +static inline int vfsub_native_ro(struct inode *inode)
33912 +{
33913 +       return sb_rdonly(inode->i_sb)
33914 +               || IS_RDONLY(inode)
33915 +               /* || IS_APPEND(inode) */
33916 +               || IS_IMMUTABLE(inode);
33917 +}
33918 +
33919 +#ifdef CONFIG_AUFS_BR_FUSE
33920 +int vfsub_test_mntns(struct vfsmount *mnt, struct super_block *h_sb);
33921 +#else
33922 +AuStubInt0(vfsub_test_mntns, struct vfsmount *mnt, struct super_block *h_sb);
33923 +#endif
33924 +
33925 +int vfsub_sync_filesystem(struct super_block *h_sb);
33926 +
33927 +/* ---------------------------------------------------------------------- */
33928 +
33929 +int vfsub_update_h_iattr(struct path *h_path, int *did);
33930 +struct file *vfsub_dentry_open(struct path *path, int flags);
33931 +struct file *vfsub_filp_open(const char *path, int oflags, int mode);
33932 +struct au_branch;
33933 +struct vfsub_aopen_args {
33934 +       struct file             *file;
33935 +       unsigned int            open_flag;
33936 +       umode_t                 create_mode;
33937 +       struct au_branch        *br;
33938 +};
33939 +int vfsub_atomic_open(struct inode *dir, struct dentry *dentry,
33940 +                     struct vfsub_aopen_args *args);
33941 +int vfsub_kern_path(const char *name, unsigned int flags, struct path *path);
33942 +
33943 +struct dentry *vfsub_lookup_one_len_unlocked(const char *name,
33944 +                                            struct path *ppath, int len);
33945 +struct dentry *vfsub_lookup_one_len(const char *name, struct path *ppath,
33946 +                                   int len);
33947 +
33948 +struct vfsub_lkup_one_args {
33949 +       struct dentry **errp;
33950 +       struct qstr *name;
33951 +       struct path *ppath;
33952 +};
33953 +
33954 +static inline struct dentry *vfsub_lkup_one(struct qstr *name,
33955 +                                           struct path *ppath)
33956 +{
33957 +       return vfsub_lookup_one_len(name->name, ppath, name->len);
33958 +}
33959 +
33960 +void vfsub_call_lkup_one(void *args);
33961 +
33962 +/* ---------------------------------------------------------------------- */
33963 +
33964 +static inline int vfsub_mnt_want_write(struct vfsmount *mnt)
33965 +{
33966 +       int err;
33967 +
33968 +       lockdep_off();
33969 +       err = mnt_want_write(mnt);
33970 +       lockdep_on();
33971 +       return err;
33972 +}
33973 +
33974 +static inline void vfsub_mnt_drop_write(struct vfsmount *mnt)
33975 +{
33976 +       lockdep_off();
33977 +       mnt_drop_write(mnt);
33978 +       lockdep_on();
33979 +}
33980 +
33981 +#if 0 /* reserved */
33982 +static inline void vfsub_mnt_drop_write_file(struct file *file)
33983 +{
33984 +       lockdep_off();
33985 +       mnt_drop_write_file(file);
33986 +       lockdep_on();
33987 +}
33988 +#endif
33989 +
33990 +/* ---------------------------------------------------------------------- */
33991 +
33992 +struct au_hinode;
33993 +struct dentry *vfsub_lock_rename(struct dentry *d1, struct au_hinode *hdir1,
33994 +                                struct dentry *d2, struct au_hinode *hdir2);
33995 +void vfsub_unlock_rename(struct dentry *d1, struct au_hinode *hdir1,
33996 +                        struct dentry *d2, struct au_hinode *hdir2);
33997 +
33998 +int vfsub_create(struct inode *dir, struct path *path, int mode,
33999 +                bool want_excl);
34000 +int vfsub_symlink(struct inode *dir, struct path *path,
34001 +                 const char *symname);
34002 +int vfsub_mknod(struct inode *dir, struct path *path, int mode, dev_t dev);
34003 +int vfsub_link(struct dentry *src_dentry, struct inode *dir,
34004 +              struct path *path, struct inode **delegated_inode);
34005 +int vfsub_rename(struct inode *src_hdir, struct dentry *src_dentry,
34006 +                struct inode *hdir, struct path *path,
34007 +                struct inode **delegated_inode, unsigned int flags);
34008 +int vfsub_mkdir(struct inode *dir, struct path *path, int mode);
34009 +int vfsub_rmdir(struct inode *dir, struct path *path);
34010 +
34011 +/* ---------------------------------------------------------------------- */
34012 +
34013 +ssize_t vfsub_read_u(struct file *file, char __user *ubuf, size_t count,
34014 +                    loff_t *ppos);
34015 +ssize_t vfsub_read_k(struct file *file, void *kbuf, size_t count,
34016 +                       loff_t *ppos);
34017 +ssize_t vfsub_write_u(struct file *file, const char __user *ubuf, size_t count,
34018 +                     loff_t *ppos);
34019 +ssize_t vfsub_write_k(struct file *file, void *kbuf, size_t count,
34020 +                     loff_t *ppos);
34021 +int vfsub_flush(struct file *file, fl_owner_t id);
34022 +int vfsub_iterate_dir(struct file *file, struct dir_context *ctx);
34023 +
34024 +static inline loff_t vfsub_f_size_read(struct file *file)
34025 +{
34026 +       return i_size_read(file_inode(file));
34027 +}
34028 +
34029 +static inline unsigned int vfsub_file_flags(struct file *file)
34030 +{
34031 +       unsigned int flags;
34032 +
34033 +       spin_lock(&file->f_lock);
34034 +       flags = file->f_flags;
34035 +       spin_unlock(&file->f_lock);
34036 +
34037 +       return flags;
34038 +}
34039 +
34040 +static inline int vfsub_file_execed(struct file *file)
34041 +{
34042 +       /* todo: direct access f_flags */
34043 +       return !!(vfsub_file_flags(file) & __FMODE_EXEC);
34044 +}
34045 +
34046 +#if 0 /* reserved */
34047 +static inline void vfsub_file_accessed(struct file *h_file)
34048 +{
34049 +       file_accessed(h_file);
34050 +       vfsub_update_h_iattr(&h_file->f_path, /*did*/NULL); /*ignore*/
34051 +}
34052 +#endif
34053 +
34054 +#if 0 /* reserved */
34055 +static inline void vfsub_touch_atime(struct vfsmount *h_mnt,
34056 +                                    struct dentry *h_dentry)
34057 +{
34058 +       struct path h_path = {
34059 +               .dentry = h_dentry,
34060 +               .mnt    = h_mnt
34061 +       };
34062 +       touch_atime(&h_path);
34063 +       vfsub_update_h_iattr(&h_path, /*did*/NULL); /*ignore*/
34064 +}
34065 +#endif
34066 +
34067 +static inline int vfsub_update_time(struct inode *h_inode,
34068 +                                   struct timespec64 *ts, int flags)
34069 +{
34070 +       return inode_update_time(h_inode, ts, flags);
34071 +       /* no vfsub_update_h_iattr() since we don't have struct path */
34072 +}
34073 +
34074 +#ifdef CONFIG_FS_POSIX_ACL
34075 +static inline int vfsub_acl_chmod(struct user_namespace *h_userns,
34076 +                                 struct inode *h_inode, umode_t h_mode)
34077 +{
34078 +       int err;
34079 +
34080 +       err = posix_acl_chmod(h_userns, h_inode, h_mode);
34081 +       if (err == -EOPNOTSUPP)
34082 +               err = 0;
34083 +       return err;
34084 +}
34085 +#else
34086 +AuStubInt0(vfsub_acl_chmod, struct user_namespace *h_userns,
34087 +          struct inode *h_inode, umode_t h_mode);
34088 +#endif
34089 +
34090 +long vfsub_splice_to(struct file *in, loff_t *ppos,
34091 +                    struct pipe_inode_info *pipe, size_t len,
34092 +                    unsigned int flags);
34093 +long vfsub_splice_from(struct pipe_inode_info *pipe, struct file *out,
34094 +                      loff_t *ppos, size_t len, unsigned int flags);
34095 +
34096 +static inline long vfsub_truncate(struct path *path, loff_t length)
34097 +{
34098 +       long err;
34099 +
34100 +       lockdep_off();
34101 +       err = vfs_truncate(path, length);
34102 +       lockdep_on();
34103 +       return err;
34104 +}
34105 +
34106 +int vfsub_trunc(struct path *h_path, loff_t length, unsigned int attr,
34107 +               struct file *h_file);
34108 +int vfsub_fsync(struct file *file, struct path *path, int datasync);
34109 +
34110 +/*
34111 + * re-use branch fs's ioctl(FICLONE) while aufs itself doesn't support such
34112 + * ioctl.
34113 + */
34114 +static inline loff_t vfsub_clone_file_range(struct file *src, struct file *dst,
34115 +                                           loff_t len)
34116 +{
34117 +       loff_t err;
34118 +
34119 +       lockdep_off();
34120 +       err = vfs_clone_file_range(src, 0, dst, 0, len, /*remap_flags*/0);
34121 +       lockdep_on();
34122 +
34123 +       return err;
34124 +}
34125 +
34126 +/* copy_file_range(2) is a systemcall */
34127 +static inline ssize_t vfsub_copy_file_range(struct file *src, loff_t src_pos,
34128 +                                           struct file *dst, loff_t dst_pos,
34129 +                                           size_t len, unsigned int flags)
34130 +{
34131 +       ssize_t ssz;
34132 +
34133 +       lockdep_off();
34134 +       ssz = vfs_copy_file_range(src, src_pos, dst, dst_pos, len, flags);
34135 +       lockdep_on();
34136 +
34137 +       return ssz;
34138 +}
34139 +
34140 +/* ---------------------------------------------------------------------- */
34141 +
34142 +static inline loff_t vfsub_llseek(struct file *file, loff_t offset, int origin)
34143 +{
34144 +       loff_t err;
34145 +
34146 +       lockdep_off();
34147 +       err = vfs_llseek(file, offset, origin);
34148 +       lockdep_on();
34149 +       return err;
34150 +}
34151 +
34152 +/* ---------------------------------------------------------------------- */
34153 +
34154 +int vfsub_sio_mkdir(struct inode *dir, struct path *path, int mode);
34155 +int vfsub_sio_rmdir(struct inode *dir, struct path *path);
34156 +int vfsub_sio_notify_change(struct path *path, struct iattr *ia,
34157 +                           struct inode **delegated_inode);
34158 +int vfsub_notify_change(struct path *path, struct iattr *ia,
34159 +                       struct inode **delegated_inode);
34160 +int vfsub_unlink(struct inode *dir, struct path *path,
34161 +                struct inode **delegated_inode, int force);
34162 +
34163 +static inline int vfsub_getattr(const struct path *path, struct kstat *st)
34164 +{
34165 +       return vfs_getattr(path, st, STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
34166 +}
34167 +
34168 +/* ---------------------------------------------------------------------- */
34169 +
34170 +static inline int vfsub_setxattr(struct user_namespace *userns,
34171 +                                struct dentry *dentry, const char *name,
34172 +                                const void *value, size_t size, int flags)
34173 +{
34174 +       int err;
34175 +
34176 +       lockdep_off();
34177 +       err = vfs_setxattr(userns, dentry, name, value, size, flags);
34178 +       lockdep_on();
34179 +
34180 +       return err;
34181 +}
34182 +
34183 +static inline int vfsub_removexattr(struct user_namespace *userns,
34184 +                                   struct dentry *dentry, const char *name)
34185 +{
34186 +       int err;
34187 +
34188 +       lockdep_off();
34189 +       err = vfs_removexattr(userns, dentry, name);
34190 +       lockdep_on();
34191 +
34192 +       return err;
34193 +}
34194 +
34195 +#endif /* __KERNEL__ */
34196 +#endif /* __AUFS_VFSUB_H__ */
34197 diff -urN /usr/share/empty/fs/aufs/wbr_policy.c linux/fs/aufs/wbr_policy.c
34198 --- /usr/share/empty/fs/aufs/wbr_policy.c       1970-01-01 01:00:00.000000000 +0100
34199 +++ linux/fs/aufs/wbr_policy.c  2022-03-21 14:49:05.733299677 +0100
34200 @@ -0,0 +1,830 @@
34201 +// SPDX-License-Identifier: GPL-2.0
34202 +/*
34203 + * Copyright (C) 2005-2021 Junjiro R. Okajima
34204 + *
34205 + * This program, aufs is free software; you can redistribute it and/or modify
34206 + * it under the terms of the GNU General Public License as published by
34207 + * the Free Software Foundation; either version 2 of the License, or
34208 + * (at your option) any later version.
34209 + *
34210 + * This program is distributed in the hope that it will be useful,
34211 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
34212 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
34213 + * GNU General Public License for more details.
34214 + *
34215 + * You should have received a copy of the GNU General Public License
34216 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
34217 + */
34218 +
34219 +/*
34220 + * policies for selecting one among multiple writable branches
34221 + */
34222 +
34223 +#include <linux/statfs.h>
34224 +#include "aufs.h"
34225 +
34226 +/* subset of cpup_attr() */
34227 +static noinline_for_stack
34228 +int au_cpdown_attr(struct path *h_path, struct dentry *h_src)
34229 +{
34230 +       int err, sbits;
34231 +       struct iattr ia;
34232 +       struct inode *h_isrc;
34233 +
34234 +       h_isrc = d_inode(h_src);
34235 +       ia.ia_valid = ATTR_FORCE | ATTR_MODE | ATTR_UID | ATTR_GID;
34236 +       ia.ia_mode = h_isrc->i_mode;
34237 +       ia.ia_uid = h_isrc->i_uid;
34238 +       ia.ia_gid = h_isrc->i_gid;
34239 +       sbits = !!(ia.ia_mode & (S_ISUID | S_ISGID));
34240 +       au_cpup_attr_flags(d_inode(h_path->dentry), h_isrc->i_flags);
34241 +       /* no delegation since it is just created */
34242 +       err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
34243 +
34244 +       /* is this nfs only? */
34245 +       if (!err && sbits && au_test_nfs(h_path->dentry->d_sb)) {
34246 +               ia.ia_valid = ATTR_FORCE | ATTR_MODE;
34247 +               ia.ia_mode = h_isrc->i_mode;
34248 +               err = vfsub_sio_notify_change(h_path, &ia, /*delegated*/NULL);
34249 +       }
34250 +
34251 +       return err;
34252 +}
34253 +
34254 +#define AuCpdown_PARENT_OPQ    1
34255 +#define AuCpdown_WHED          (1 << 1)
34256 +#define AuCpdown_MADE_DIR      (1 << 2)
34257 +#define AuCpdown_DIROPQ                (1 << 3)
34258 +#define au_ftest_cpdown(flags, name)   ((flags) & AuCpdown_##name)
34259 +#define au_fset_cpdown(flags, name) \
34260 +       do { (flags) |= AuCpdown_##name; } while (0)
34261 +#define au_fclr_cpdown(flags, name) \
34262 +       do { (flags) &= ~AuCpdown_##name; } while (0)
34263 +
34264 +static int au_cpdown_dir_opq(struct dentry *dentry, aufs_bindex_t bdst,
34265 +                            unsigned int *flags)
34266 +{
34267 +       int err;
34268 +       struct dentry *opq_dentry;
34269 +
34270 +       opq_dentry = au_diropq_create(dentry, bdst);
34271 +       err = PTR_ERR(opq_dentry);
34272 +       if (IS_ERR(opq_dentry))
34273 +               goto out;
34274 +       dput(opq_dentry);
34275 +       au_fset_cpdown(*flags, DIROPQ);
34276 +
34277 +out:
34278 +       return err;
34279 +}
34280 +
34281 +static int au_cpdown_dir_wh(struct dentry *dentry, struct dentry *h_parent,
34282 +                           struct inode *dir, aufs_bindex_t bdst)
34283 +{
34284 +       int err;
34285 +       struct path h_path;
34286 +       struct au_branch *br;
34287 +
34288 +       br = au_sbr(dentry->d_sb, bdst);
34289 +       h_path.dentry = au_wh_lkup(h_parent, &dentry->d_name, br);
34290 +       err = PTR_ERR(h_path.dentry);
34291 +       if (IS_ERR(h_path.dentry))
34292 +               goto out;
34293 +
34294 +       err = 0;
34295 +       if (d_is_positive(h_path.dentry)) {
34296 +               h_path.mnt = au_br_mnt(br);
34297 +               err = au_wh_unlink_dentry(au_h_iptr(dir, bdst), &h_path,
34298 +                                         dentry);
34299 +       }
34300 +       dput(h_path.dentry);
34301 +
34302 +out:
34303 +       return err;
34304 +}
34305 +
34306 +static int au_cpdown_dir(struct dentry *dentry, aufs_bindex_t bdst,
34307 +                        struct au_pin *pin,
34308 +                        struct dentry *h_parent, void *arg)
34309 +{
34310 +       int err, rerr;
34311 +       aufs_bindex_t bopq, btop;
34312 +       struct path h_path;
34313 +       struct dentry *parent;
34314 +       struct inode *h_dir, *h_inode, *inode, *dir;
34315 +       unsigned int *flags = arg;
34316 +
34317 +       btop = au_dbtop(dentry);
34318 +       /* dentry is di-locked */
34319 +       parent = dget_parent(dentry);
34320 +       dir = d_inode(parent);
34321 +       h_dir = d_inode(h_parent);
34322 +       AuDebugOn(h_dir != au_h_iptr(dir, bdst));
34323 +       IMustLock(h_dir);
34324 +
34325 +       err = au_lkup_neg(dentry, bdst, /*wh*/0);
34326 +       if (unlikely(err < 0))
34327 +               goto out;
34328 +       h_path.dentry = au_h_dptr(dentry, bdst);
34329 +       h_path.mnt = au_sbr_mnt(dentry->d_sb, bdst);
34330 +       err = vfsub_sio_mkdir(au_h_iptr(dir, bdst), &h_path, 0755);
34331 +       if (unlikely(err))
34332 +               goto out_put;
34333 +       au_fset_cpdown(*flags, MADE_DIR);
34334 +
34335 +       bopq = au_dbdiropq(dentry);
34336 +       au_fclr_cpdown(*flags, WHED);
34337 +       au_fclr_cpdown(*flags, DIROPQ);
34338 +       if (au_dbwh(dentry) == bdst)
34339 +               au_fset_cpdown(*flags, WHED);
34340 +       if (!au_ftest_cpdown(*flags, PARENT_OPQ) && bopq <= bdst)
34341 +               au_fset_cpdown(*flags, PARENT_OPQ);
34342 +       h_inode = d_inode(h_path.dentry);
34343 +       inode_lock_nested(h_inode, AuLsc_I_CHILD);
34344 +       if (au_ftest_cpdown(*flags, WHED)) {
34345 +               err = au_cpdown_dir_opq(dentry, bdst, flags);
34346 +               if (unlikely(err)) {
34347 +                       inode_unlock(h_inode);
34348 +                       goto out_dir;
34349 +               }
34350 +       }
34351 +
34352 +       err = au_cpdown_attr(&h_path, au_h_dptr(dentry, btop));
34353 +       inode_unlock(h_inode);
34354 +       if (unlikely(err))
34355 +               goto out_opq;
34356 +
34357 +       if (au_ftest_cpdown(*flags, WHED)) {
34358 +               err = au_cpdown_dir_wh(dentry, h_parent, dir, bdst);
34359 +               if (unlikely(err))
34360 +                       goto out_opq;
34361 +       }
34362 +
34363 +       inode = d_inode(dentry);
34364 +       if (au_ibbot(inode) < bdst)
34365 +               au_set_ibbot(inode, bdst);
34366 +       au_set_h_iptr(inode, bdst, au_igrab(h_inode),
34367 +                     au_hi_flags(inode, /*isdir*/1));
34368 +       au_fhsm_wrote(dentry->d_sb, bdst, /*force*/0);
34369 +       goto out; /* success */
34370 +
34371 +       /* revert */
34372 +out_opq:
34373 +       if (au_ftest_cpdown(*flags, DIROPQ)) {
34374 +               inode_lock_nested(h_inode, AuLsc_I_CHILD);
34375 +               rerr = au_diropq_remove(dentry, bdst);
34376 +               inode_unlock(h_inode);
34377 +               if (unlikely(rerr)) {
34378 +                       AuIOErr("failed removing diropq for %pd b%d (%d)\n",
34379 +                               dentry, bdst, rerr);
34380 +                       err = -EIO;
34381 +                       goto out;
34382 +               }
34383 +       }
34384 +out_dir:
34385 +       if (au_ftest_cpdown(*flags, MADE_DIR)) {
34386 +               rerr = vfsub_sio_rmdir(au_h_iptr(dir, bdst), &h_path);
34387 +               if (unlikely(rerr)) {
34388 +                       AuIOErr("failed removing %pd b%d (%d)\n",
34389 +                               dentry, bdst, rerr);
34390 +                       err = -EIO;
34391 +               }
34392 +       }
34393 +out_put:
34394 +       au_set_h_dptr(dentry, bdst, NULL);
34395 +       if (au_dbbot(dentry) == bdst)
34396 +               au_update_dbbot(dentry);
34397 +out:
34398 +       dput(parent);
34399 +       return err;
34400 +}
34401 +
34402 +int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst)
34403 +{
34404 +       int err;
34405 +       unsigned int flags;
34406 +
34407 +       flags = 0;
34408 +       err = au_cp_dirs(dentry, bdst, au_cpdown_dir, &flags);
34409 +
34410 +       return err;
34411 +}
34412 +
34413 +/* ---------------------------------------------------------------------- */
34414 +
34415 +/* policies for create */
34416 +
34417 +int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex)
34418 +{
34419 +       int err, i, j, ndentry;
34420 +       aufs_bindex_t bopq;
34421 +       struct au_dcsub_pages dpages;
34422 +       struct au_dpage *dpage;
34423 +       struct dentry **dentries, *parent, *d;
34424 +
34425 +       err = au_dpages_init(&dpages, GFP_NOFS);
34426 +       if (unlikely(err))
34427 +               goto out;
34428 +       parent = dget_parent(dentry);
34429 +       err = au_dcsub_pages_rev_aufs(&dpages, parent, /*do_include*/0);
34430 +       if (unlikely(err))
34431 +               goto out_free;
34432 +
34433 +       err = bindex;
34434 +       for (i = 0; i < dpages.ndpage; i++) {
34435 +               dpage = dpages.dpages + i;
34436 +               dentries = dpage->dentries;
34437 +               ndentry = dpage->ndentry;
34438 +               for (j = 0; j < ndentry; j++) {
34439 +                       d = dentries[j];
34440 +                       di_read_lock_parent2(d, !AuLock_IR);
34441 +                       bopq = au_dbdiropq(d);
34442 +                       di_read_unlock(d, !AuLock_IR);
34443 +                       if (bopq >= 0 && bopq < err)
34444 +                               err = bopq;
34445 +               }
34446 +       }
34447 +
34448 +out_free:
34449 +       dput(parent);
34450 +       au_dpages_free(&dpages);
34451 +out:
34452 +       return err;
34453 +}
34454 +
34455 +static int au_wbr_bu(struct super_block *sb, aufs_bindex_t bindex)
34456 +{
34457 +       for (; bindex >= 0; bindex--)
34458 +               if (!au_br_rdonly(au_sbr(sb, bindex)))
34459 +                       return bindex;
34460 +       return -EROFS;
34461 +}
34462 +
34463 +/* top down parent */
34464 +static int au_wbr_create_tdp(struct dentry *dentry,
34465 +                            unsigned int flags __maybe_unused)
34466 +{
34467 +       int err;
34468 +       aufs_bindex_t btop, bindex;
34469 +       struct super_block *sb;
34470 +       struct dentry *parent, *h_parent;
34471 +
34472 +       sb = dentry->d_sb;
34473 +       btop = au_dbtop(dentry);
34474 +       err = btop;
34475 +       if (!au_br_rdonly(au_sbr(sb, btop)))
34476 +               goto out;
34477 +
34478 +       err = -EROFS;
34479 +       parent = dget_parent(dentry);
34480 +       for (bindex = au_dbtop(parent); bindex < btop; bindex++) {
34481 +               h_parent = au_h_dptr(parent, bindex);
34482 +               if (!h_parent || d_is_negative(h_parent))
34483 +                       continue;
34484 +
34485 +               if (!au_br_rdonly(au_sbr(sb, bindex))) {
34486 +                       err = bindex;
34487 +                       break;
34488 +               }
34489 +       }
34490 +       dput(parent);
34491 +
34492 +       /* bottom up here */
34493 +       if (unlikely(err < 0)) {
34494 +               err = au_wbr_bu(sb, btop - 1);
34495 +               if (err >= 0)
34496 +                       err = au_wbr_nonopq(dentry, err);
34497 +       }
34498 +
34499 +out:
34500 +       AuDbg("b%d\n", err);
34501 +       return err;
34502 +}
34503 +
34504 +/* ---------------------------------------------------------------------- */
34505 +
34506 +/* an exception for the policy other than tdp */
34507 +static int au_wbr_create_exp(struct dentry *dentry)
34508 +{
34509 +       int err;
34510 +       aufs_bindex_t bwh, bdiropq;
34511 +       struct dentry *parent;
34512 +
34513 +       err = -1;
34514 +       bwh = au_dbwh(dentry);
34515 +       parent = dget_parent(dentry);
34516 +       bdiropq = au_dbdiropq(parent);
34517 +       if (bwh >= 0) {
34518 +               if (bdiropq >= 0)
34519 +                       err = min(bdiropq, bwh);
34520 +               else
34521 +                       err = bwh;
34522 +               AuDbg("%d\n", err);
34523 +       } else if (bdiropq >= 0) {
34524 +               err = bdiropq;
34525 +               AuDbg("%d\n", err);
34526 +       }
34527 +       dput(parent);
34528 +
34529 +       if (err >= 0)
34530 +               err = au_wbr_nonopq(dentry, err);
34531 +
34532 +       if (err >= 0 && au_br_rdonly(au_sbr(dentry->d_sb, err)))
34533 +               err = -1;
34534 +
34535 +       AuDbg("%d\n", err);
34536 +       return err;
34537 +}
34538 +
34539 +/* ---------------------------------------------------------------------- */
34540 +
34541 +/* round robin */
34542 +static int au_wbr_create_init_rr(struct super_block *sb)
34543 +{
34544 +       int err;
34545 +
34546 +       err = au_wbr_bu(sb, au_sbbot(sb));
34547 +       atomic_set(&au_sbi(sb)->si_wbr_rr_next, -err); /* less important */
34548 +       /* smp_mb(); */
34549 +
34550 +       AuDbg("b%d\n", err);
34551 +       return err;
34552 +}
34553 +
34554 +static int au_wbr_create_rr(struct dentry *dentry, unsigned int flags)
34555 +{
34556 +       int err, nbr;
34557 +       unsigned int u;
34558 +       aufs_bindex_t bindex, bbot;
34559 +       struct super_block *sb;
34560 +       atomic_t *next;
34561 +
34562 +       err = au_wbr_create_exp(dentry);
34563 +       if (err >= 0)
34564 +               goto out;
34565 +
34566 +       sb = dentry->d_sb;
34567 +       next = &au_sbi(sb)->si_wbr_rr_next;
34568 +       bbot = au_sbbot(sb);
34569 +       nbr = bbot + 1;
34570 +       for (bindex = 0; bindex <= bbot; bindex++) {
34571 +               if (!au_ftest_wbr(flags, DIR)) {
34572 +                       err = atomic_dec_return(next) + 1;
34573 +                       /* modulo for 0 is meaningless */
34574 +                       if (unlikely(!err))
34575 +                               err = atomic_dec_return(next) + 1;
34576 +               } else
34577 +                       err = atomic_read(next);
34578 +               AuDbg("%d\n", err);
34579 +               u = err;
34580 +               err = u % nbr;
34581 +               AuDbg("%d\n", err);
34582 +               if (!au_br_rdonly(au_sbr(sb, err)))
34583 +                       break;
34584 +               err = -EROFS;
34585 +       }
34586 +
34587 +       if (err >= 0)
34588 +               err = au_wbr_nonopq(dentry, err);
34589 +
34590 +out:
34591 +       AuDbg("%d\n", err);
34592 +       return err;
34593 +}
34594 +
34595 +/* ---------------------------------------------------------------------- */
34596 +
34597 +/* most free space */
34598 +static void au_mfs(struct dentry *dentry, struct dentry *parent)
34599 +{
34600 +       struct super_block *sb;
34601 +       struct au_branch *br;
34602 +       struct au_wbr_mfs *mfs;
34603 +       struct dentry *h_parent;
34604 +       aufs_bindex_t bindex, bbot;
34605 +       int err;
34606 +       unsigned long long b, bavail;
34607 +       struct path h_path;
34608 +       /* reduce the stack usage */
34609 +       struct kstatfs *st;
34610 +
34611 +       st = kmalloc(sizeof(*st), GFP_NOFS);
34612 +       if (unlikely(!st)) {
34613 +               AuWarn1("failed updating mfs(%d), ignored\n", -ENOMEM);
34614 +               return;
34615 +       }
34616 +
34617 +       bavail = 0;
34618 +       sb = dentry->d_sb;
34619 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34620 +       MtxMustLock(&mfs->mfs_lock);
34621 +       mfs->mfs_bindex = -EROFS;
34622 +       mfs->mfsrr_bytes = 0;
34623 +       if (!parent) {
34624 +               bindex = 0;
34625 +               bbot = au_sbbot(sb);
34626 +       } else {
34627 +               bindex = au_dbtop(parent);
34628 +               bbot = au_dbtaildir(parent);
34629 +       }
34630 +
34631 +       for (; bindex <= bbot; bindex++) {
34632 +               if (parent) {
34633 +                       h_parent = au_h_dptr(parent, bindex);
34634 +                       if (!h_parent || d_is_negative(h_parent))
34635 +                               continue;
34636 +               }
34637 +               br = au_sbr(sb, bindex);
34638 +               if (au_br_rdonly(br))
34639 +                       continue;
34640 +
34641 +               /* sb->s_root for NFS is unreliable */
34642 +               h_path.mnt = au_br_mnt(br);
34643 +               h_path.dentry = h_path.mnt->mnt_root;
34644 +               err = vfs_statfs(&h_path, st);
34645 +               if (unlikely(err)) {
34646 +                       AuWarn1("failed statfs, b%d, %d\n", bindex, err);
34647 +                       continue;
34648 +               }
34649 +
34650 +               /* when the available size is equal, select the lower one */
34651 +               BUILD_BUG_ON(sizeof(b) < sizeof(st->f_bavail)
34652 +                            || sizeof(b) < sizeof(st->f_bsize));
34653 +               b = st->f_bavail * st->f_bsize;
34654 +               br->br_wbr->wbr_bytes = b;
34655 +               if (b >= bavail) {
34656 +                       bavail = b;
34657 +                       mfs->mfs_bindex = bindex;
34658 +                       mfs->mfs_jiffy = jiffies;
34659 +               }
34660 +       }
34661 +
34662 +       mfs->mfsrr_bytes = bavail;
34663 +       AuDbg("b%d\n", mfs->mfs_bindex);
34664 +       au_kfree_rcu(st);
34665 +}
34666 +
34667 +static int au_wbr_create_mfs(struct dentry *dentry, unsigned int flags)
34668 +{
34669 +       int err;
34670 +       struct dentry *parent;
34671 +       struct super_block *sb;
34672 +       struct au_wbr_mfs *mfs;
34673 +
34674 +       err = au_wbr_create_exp(dentry);
34675 +       if (err >= 0)
34676 +               goto out;
34677 +
34678 +       sb = dentry->d_sb;
34679 +       parent = NULL;
34680 +       if (au_ftest_wbr(flags, PARENT))
34681 +               parent = dget_parent(dentry);
34682 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34683 +       mutex_lock(&mfs->mfs_lock);
34684 +       if (time_after(jiffies, mfs->mfs_jiffy + mfs->mfs_expire)
34685 +           || mfs->mfs_bindex < 0
34686 +           || au_br_rdonly(au_sbr(sb, mfs->mfs_bindex)))
34687 +               au_mfs(dentry, parent);
34688 +       mutex_unlock(&mfs->mfs_lock);
34689 +       err = mfs->mfs_bindex;
34690 +       dput(parent);
34691 +
34692 +       if (err >= 0)
34693 +               err = au_wbr_nonopq(dentry, err);
34694 +
34695 +out:
34696 +       AuDbg("b%d\n", err);
34697 +       return err;
34698 +}
34699 +
34700 +static int au_wbr_create_init_mfs(struct super_block *sb)
34701 +{
34702 +       struct au_wbr_mfs *mfs;
34703 +
34704 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34705 +       mutex_init(&mfs->mfs_lock);
34706 +       mfs->mfs_jiffy = 0;
34707 +       mfs->mfs_bindex = -EROFS;
34708 +
34709 +       return 0;
34710 +}
34711 +
34712 +static int au_wbr_create_fin_mfs(struct super_block *sb __maybe_unused)
34713 +{
34714 +       mutex_destroy(&au_sbi(sb)->si_wbr_mfs.mfs_lock);
34715 +       return 0;
34716 +}
34717 +
34718 +/* ---------------------------------------------------------------------- */
34719 +
34720 +/* top down regardless parent, and then mfs */
34721 +static int au_wbr_create_tdmfs(struct dentry *dentry,
34722 +                              unsigned int flags __maybe_unused)
34723 +{
34724 +       int err;
34725 +       aufs_bindex_t bwh, btail, bindex, bfound, bmfs;
34726 +       unsigned long long watermark;
34727 +       struct super_block *sb;
34728 +       struct au_wbr_mfs *mfs;
34729 +       struct au_branch *br;
34730 +       struct dentry *parent;
34731 +
34732 +       sb = dentry->d_sb;
34733 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34734 +       mutex_lock(&mfs->mfs_lock);
34735 +       if (time_after(jiffies, mfs->mfs_jiffy + mfs->mfs_expire)
34736 +           || mfs->mfs_bindex < 0)
34737 +               au_mfs(dentry, /*parent*/NULL);
34738 +       watermark = mfs->mfsrr_watermark;
34739 +       bmfs = mfs->mfs_bindex;
34740 +       mutex_unlock(&mfs->mfs_lock);
34741 +
34742 +       /* another style of au_wbr_create_exp() */
34743 +       bwh = au_dbwh(dentry);
34744 +       parent = dget_parent(dentry);
34745 +       btail = au_dbtaildir(parent);
34746 +       if (bwh >= 0 && bwh < btail)
34747 +               btail = bwh;
34748 +
34749 +       err = au_wbr_nonopq(dentry, btail);
34750 +       if (unlikely(err < 0))
34751 +               goto out;
34752 +       btail = err;
34753 +       bfound = -1;
34754 +       for (bindex = 0; bindex <= btail; bindex++) {
34755 +               br = au_sbr(sb, bindex);
34756 +               if (au_br_rdonly(br))
34757 +                       continue;
34758 +               if (br->br_wbr->wbr_bytes > watermark) {
34759 +                       bfound = bindex;
34760 +                       break;
34761 +               }
34762 +       }
34763 +       err = bfound;
34764 +       if (err < 0)
34765 +               err = bmfs;
34766 +
34767 +out:
34768 +       dput(parent);
34769 +       AuDbg("b%d\n", err);
34770 +       return err;
34771 +}
34772 +
34773 +/* ---------------------------------------------------------------------- */
34774 +
34775 +/* most free space and then round robin */
34776 +static int au_wbr_create_mfsrr(struct dentry *dentry, unsigned int flags)
34777 +{
34778 +       int err;
34779 +       struct au_wbr_mfs *mfs;
34780 +
34781 +       err = au_wbr_create_mfs(dentry, flags);
34782 +       if (err >= 0) {
34783 +               mfs = &au_sbi(dentry->d_sb)->si_wbr_mfs;
34784 +               mutex_lock(&mfs->mfs_lock);
34785 +               if (mfs->mfsrr_bytes < mfs->mfsrr_watermark)
34786 +                       err = au_wbr_create_rr(dentry, flags);
34787 +               mutex_unlock(&mfs->mfs_lock);
34788 +       }
34789 +
34790 +       AuDbg("b%d\n", err);
34791 +       return err;
34792 +}
34793 +
34794 +static int au_wbr_create_init_mfsrr(struct super_block *sb)
34795 +{
34796 +       int err;
34797 +
34798 +       au_wbr_create_init_mfs(sb); /* ignore */
34799 +       err = au_wbr_create_init_rr(sb);
34800 +
34801 +       return err;
34802 +}
34803 +
34804 +/* ---------------------------------------------------------------------- */
34805 +
34806 +/* top down parent and most free space */
34807 +static int au_wbr_create_pmfs(struct dentry *dentry, unsigned int flags)
34808 +{
34809 +       int err, e2;
34810 +       unsigned long long b;
34811 +       aufs_bindex_t bindex, btop, bbot;
34812 +       struct super_block *sb;
34813 +       struct dentry *parent, *h_parent;
34814 +       struct au_branch *br;
34815 +
34816 +       err = au_wbr_create_tdp(dentry, flags);
34817 +       if (unlikely(err < 0))
34818 +               goto out;
34819 +       parent = dget_parent(dentry);
34820 +       btop = au_dbtop(parent);
34821 +       bbot = au_dbtaildir(parent);
34822 +       if (btop == bbot)
34823 +               goto out_parent; /* success */
34824 +
34825 +       e2 = au_wbr_create_mfs(dentry, flags);
34826 +       if (e2 < 0)
34827 +               goto out_parent; /* success */
34828 +
34829 +       /* when the available size is equal, select upper one */
34830 +       sb = dentry->d_sb;
34831 +       br = au_sbr(sb, err);
34832 +       b = br->br_wbr->wbr_bytes;
34833 +       AuDbg("b%d, %llu\n", err, b);
34834 +
34835 +       for (bindex = btop; bindex <= bbot; bindex++) {
34836 +               h_parent = au_h_dptr(parent, bindex);
34837 +               if (!h_parent || d_is_negative(h_parent))
34838 +                       continue;
34839 +
34840 +               br = au_sbr(sb, bindex);
34841 +               if (!au_br_rdonly(br) && br->br_wbr->wbr_bytes > b) {
34842 +                       b = br->br_wbr->wbr_bytes;
34843 +                       err = bindex;
34844 +                       AuDbg("b%d, %llu\n", err, b);
34845 +               }
34846 +       }
34847 +
34848 +       if (err >= 0)
34849 +               err = au_wbr_nonopq(dentry, err);
34850 +
34851 +out_parent:
34852 +       dput(parent);
34853 +out:
34854 +       AuDbg("b%d\n", err);
34855 +       return err;
34856 +}
34857 +
34858 +/* ---------------------------------------------------------------------- */
34859 +
34860 +/*
34861 + * - top down parent
34862 + * - most free space with parent
34863 + * - most free space round-robin regardless parent
34864 + */
34865 +static int au_wbr_create_pmfsrr(struct dentry *dentry, unsigned int flags)
34866 +{
34867 +       int err;
34868 +       unsigned long long watermark;
34869 +       struct super_block *sb;
34870 +       struct au_branch *br;
34871 +       struct au_wbr_mfs *mfs;
34872 +
34873 +       err = au_wbr_create_pmfs(dentry, flags | AuWbr_PARENT);
34874 +       if (unlikely(err < 0))
34875 +               goto out;
34876 +
34877 +       sb = dentry->d_sb;
34878 +       br = au_sbr(sb, err);
34879 +       mfs = &au_sbi(sb)->si_wbr_mfs;
34880 +       mutex_lock(&mfs->mfs_lock);
34881 +       watermark = mfs->mfsrr_watermark;
34882 +       mutex_unlock(&mfs->mfs_lock);
34883 +       if (br->br_wbr->wbr_bytes < watermark)
34884 +               /* regardless the parent dir */
34885 +               err = au_wbr_create_mfsrr(dentry, flags);
34886 +
34887 +out:
34888 +       AuDbg("b%d\n", err);
34889 +       return err;
34890 +}
34891 +
34892 +/* ---------------------------------------------------------------------- */
34893 +
34894 +/* policies for copyup */
34895 +
34896 +/* top down parent */
34897 +static int au_wbr_copyup_tdp(struct dentry *dentry)
34898 +{
34899 +       return au_wbr_create_tdp(dentry, /*flags, anything is ok*/0);
34900 +}
34901 +
34902 +/* bottom up parent */
34903 +static int au_wbr_copyup_bup(struct dentry *dentry)
34904 +{
34905 +       int err;
34906 +       aufs_bindex_t bindex, btop;
34907 +       struct dentry *parent, *h_parent;
34908 +       struct super_block *sb;
34909 +
34910 +       err = -EROFS;
34911 +       sb = dentry->d_sb;
34912 +       parent = dget_parent(dentry);
34913 +       btop = au_dbtop(parent);
34914 +       for (bindex = au_dbtop(dentry); bindex >= btop; bindex--) {
34915 +               h_parent = au_h_dptr(parent, bindex);
34916 +               if (!h_parent || d_is_negative(h_parent))
34917 +                       continue;
34918 +
34919 +               if (!au_br_rdonly(au_sbr(sb, bindex))) {
34920 +                       err = bindex;
34921 +                       break;
34922 +               }
34923 +       }
34924 +       dput(parent);
34925 +
34926 +       /* bottom up here */
34927 +       if (unlikely(err < 0))
34928 +               err = au_wbr_bu(sb, btop - 1);
34929 +
34930 +       AuDbg("b%d\n", err);
34931 +       return err;
34932 +}
34933 +
34934 +/* bottom up */
34935 +int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t btop)
34936 +{
34937 +       int err;
34938 +
34939 +       err = au_wbr_bu(dentry->d_sb, btop);
34940 +       AuDbg("b%d\n", err);
34941 +       if (err > btop)
34942 +               err = au_wbr_nonopq(dentry, err);
34943 +
34944 +       AuDbg("b%d\n", err);
34945 +       return err;
34946 +}
34947 +
34948 +static int au_wbr_copyup_bu(struct dentry *dentry)
34949 +{
34950 +       int err;
34951 +       aufs_bindex_t btop;
34952 +
34953 +       btop = au_dbtop(dentry);
34954 +       err = au_wbr_do_copyup_bu(dentry, btop);
34955 +       return err;
34956 +}
34957 +
34958 +/* ---------------------------------------------------------------------- */
34959 +
34960 +struct au_wbr_copyup_operations au_wbr_copyup_ops[] = {
34961 +       [AuWbrCopyup_TDP] = {
34962 +               .copyup = au_wbr_copyup_tdp
34963 +       },
34964 +       [AuWbrCopyup_BUP] = {
34965 +               .copyup = au_wbr_copyup_bup
34966 +       },
34967 +       [AuWbrCopyup_BU] = {
34968 +               .copyup = au_wbr_copyup_bu
34969 +       }
34970 +};
34971 +
34972 +struct au_wbr_create_operations au_wbr_create_ops[] = {
34973 +       [AuWbrCreate_TDP] = {
34974 +               .create = au_wbr_create_tdp
34975 +       },
34976 +       [AuWbrCreate_RR] = {
34977 +               .create = au_wbr_create_rr,
34978 +               .init   = au_wbr_create_init_rr
34979 +       },
34980 +       [AuWbrCreate_MFS] = {
34981 +               .create = au_wbr_create_mfs,
34982 +               .init   = au_wbr_create_init_mfs,
34983 +               .fin    = au_wbr_create_fin_mfs
34984 +       },
34985 +       [AuWbrCreate_MFSV] = {
34986 +               .create = au_wbr_create_mfs,
34987 +               .init   = au_wbr_create_init_mfs,
34988 +               .fin    = au_wbr_create_fin_mfs
34989 +       },
34990 +       [AuWbrCreate_MFSRR] = {
34991 +               .create = au_wbr_create_mfsrr,
34992 +               .init   = au_wbr_create_init_mfsrr,
34993 +               .fin    = au_wbr_create_fin_mfs
34994 +       },
34995 +       [AuWbrCreate_MFSRRV] = {
34996 +               .create = au_wbr_create_mfsrr,
34997 +               .init   = au_wbr_create_init_mfsrr,
34998 +               .fin    = au_wbr_create_fin_mfs
34999 +       },
35000 +       [AuWbrCreate_TDMFS] = {
35001 +               .create = au_wbr_create_tdmfs,
35002 +               .init   = au_wbr_create_init_mfs,
35003 +               .fin    = au_wbr_create_fin_mfs
35004 +       },
35005 +       [AuWbrCreate_TDMFSV] = {
35006 +               .create = au_wbr_create_tdmfs,
35007 +               .init   = au_wbr_create_init_mfs,
35008 +               .fin    = au_wbr_create_fin_mfs
35009 +       },
35010 +       [AuWbrCreate_PMFS] = {
35011 +               .create = au_wbr_create_pmfs,
35012 +               .init   = au_wbr_create_init_mfs,
35013 +               .fin    = au_wbr_create_fin_mfs
35014 +       },
35015 +       [AuWbrCreate_PMFSV] = {
35016 +               .create = au_wbr_create_pmfs,
35017 +               .init   = au_wbr_create_init_mfs,
35018 +               .fin    = au_wbr_create_fin_mfs
35019 +       },
35020 +       [AuWbrCreate_PMFSRR] = {
35021 +               .create = au_wbr_create_pmfsrr,
35022 +               .init   = au_wbr_create_init_mfsrr,
35023 +               .fin    = au_wbr_create_fin_mfs
35024 +       },
35025 +       [AuWbrCreate_PMFSRRV] = {
35026 +               .create = au_wbr_create_pmfsrr,
35027 +               .init   = au_wbr_create_init_mfsrr,
35028 +               .fin    = au_wbr_create_fin_mfs
35029 +       }
35030 +};
35031 diff -urN /usr/share/empty/fs/aufs/whout.c linux/fs/aufs/whout.c
35032 --- /usr/share/empty/fs/aufs/whout.c    1970-01-01 01:00:00.000000000 +0100
35033 +++ linux/fs/aufs/whout.c       2022-03-21 14:49:05.733299677 +0100
35034 @@ -0,0 +1,1072 @@
35035 +// SPDX-License-Identifier: GPL-2.0
35036 +/*
35037 + * Copyright (C) 2005-2021 Junjiro R. Okajima
35038 + *
35039 + * This program, aufs is free software; you can redistribute it and/or modify
35040 + * it under the terms of the GNU General Public License as published by
35041 + * the Free Software Foundation; either version 2 of the License, or
35042 + * (at your option) any later version.
35043 + *
35044 + * This program is distributed in the hope that it will be useful,
35045 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
35046 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
35047 + * GNU General Public License for more details.
35048 + *
35049 + * You should have received a copy of the GNU General Public License
35050 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
35051 + */
35052 +
35053 +/*
35054 + * whiteout for logical deletion and opaque directory
35055 + */
35056 +
35057 +#include "aufs.h"
35058 +
35059 +#define WH_MASK                        0444
35060 +
35061 +/*
35062 + * If a directory contains this file, then it is opaque.  We start with the
35063 + * .wh. flag so that it is blocked by lookup.
35064 + */
35065 +static struct qstr diropq_name = QSTR_INIT(AUFS_WH_DIROPQ,
35066 +                                          sizeof(AUFS_WH_DIROPQ) - 1);
35067 +
35068 +/*
35069 + * generate whiteout name, which is NOT terminated by NULL.
35070 + * @name: original d_name.name
35071 + * @len: original d_name.len
35072 + * @wh: whiteout qstr
35073 + * returns zero when succeeds, otherwise error.
35074 + * succeeded value as wh->name should be freed by kfree().
35075 + */
35076 +int au_wh_name_alloc(struct qstr *wh, const struct qstr *name)
35077 +{
35078 +       char *p;
35079 +
35080 +       if (unlikely(name->len > PATH_MAX - AUFS_WH_PFX_LEN))
35081 +               return -ENAMETOOLONG;
35082 +
35083 +       wh->len = name->len + AUFS_WH_PFX_LEN;
35084 +       p = kmalloc(wh->len, GFP_NOFS);
35085 +       wh->name = p;
35086 +       if (p) {
35087 +               memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
35088 +               memcpy(p + AUFS_WH_PFX_LEN, name->name, name->len);
35089 +               /* smp_mb(); */
35090 +               return 0;
35091 +       }
35092 +       return -ENOMEM;
35093 +}
35094 +
35095 +/* ---------------------------------------------------------------------- */
35096 +
35097 +/*
35098 + * test if the @wh_name exists under @h_ppath.
35099 + * @try_sio specifies the necessary of super-io.
35100 + */
35101 +int au_wh_test(struct user_namespace *h_userns, struct path *h_ppath,
35102 +              struct qstr *wh_name, int try_sio)
35103 +{
35104 +       int err;
35105 +       struct dentry *wh_dentry;
35106 +
35107 +       if (!try_sio)
35108 +               wh_dentry = vfsub_lkup_one(wh_name, h_ppath);
35109 +       else
35110 +               wh_dentry = au_sio_lkup_one(h_userns, wh_name, h_ppath);
35111 +       err = PTR_ERR(wh_dentry);
35112 +       if (IS_ERR(wh_dentry)) {
35113 +               if (err == -ENAMETOOLONG)
35114 +                       err = 0;
35115 +               goto out;
35116 +       }
35117 +
35118 +       err = 0;
35119 +       if (d_is_negative(wh_dentry))
35120 +               goto out_wh; /* success */
35121 +
35122 +       err = 1;
35123 +       if (d_is_reg(wh_dentry))
35124 +               goto out_wh; /* success */
35125 +
35126 +       err = -EIO;
35127 +       AuIOErr("%pd Invalid whiteout entry type 0%o.\n",
35128 +               wh_dentry, d_inode(wh_dentry)->i_mode);
35129 +
35130 +out_wh:
35131 +       dput(wh_dentry);
35132 +out:
35133 +       return err;
35134 +}
35135 +
35136 +/*
35137 + * test if the @h_path->dentry sets opaque or not.
35138 + */
35139 +int au_diropq_test(struct user_namespace *h_userns, struct path *h_path)
35140 +{
35141 +       int err;
35142 +       struct inode *h_dir;
35143 +
35144 +       h_dir = d_inode(h_path->dentry);
35145 +       err = au_wh_test(h_userns, h_path, &diropq_name,
35146 +                        au_test_h_perm_sio(h_userns, h_dir, MAY_EXEC));
35147 +       return err;
35148 +}
35149 +
35150 +/*
35151 + * returns a negative dentry whose name is unique and temporary.
35152 + */
35153 +struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
35154 +                            struct qstr *prefix)
35155 +{
35156 +       struct dentry *dentry;
35157 +       int i;
35158 +       char defname[NAME_MAX - AUFS_MAX_NAMELEN + DNAME_INLINE_LEN + 1],
35159 +               *name, *p;
35160 +       /* strict atomic_t is unnecessary here */
35161 +       static unsigned short cnt;
35162 +       struct qstr qs;
35163 +       struct path h_ppath;
35164 +       struct user_namespace *h_userns;
35165 +
35166 +       BUILD_BUG_ON(sizeof(cnt) * 2 > AUFS_WH_TMP_LEN);
35167 +
35168 +       name = defname;
35169 +       qs.len = sizeof(defname) - DNAME_INLINE_LEN + prefix->len - 1;
35170 +       if (unlikely(prefix->len > DNAME_INLINE_LEN)) {
35171 +               dentry = ERR_PTR(-ENAMETOOLONG);
35172 +               if (unlikely(qs.len > NAME_MAX))
35173 +                       goto out;
35174 +               dentry = ERR_PTR(-ENOMEM);
35175 +               name = kmalloc(qs.len + 1, GFP_NOFS);
35176 +               if (unlikely(!name))
35177 +                       goto out;
35178 +       }
35179 +
35180 +       /* doubly whiteout-ed */
35181 +       memcpy(name, AUFS_WH_PFX AUFS_WH_PFX, AUFS_WH_PFX_LEN * 2);
35182 +       p = name + AUFS_WH_PFX_LEN * 2;
35183 +       memcpy(p, prefix->name, prefix->len);
35184 +       p += prefix->len;
35185 +       *p++ = '.';
35186 +       AuDebugOn(name + qs.len + 1 - p <= AUFS_WH_TMP_LEN);
35187 +
35188 +       h_ppath.dentry = h_parent;
35189 +       h_ppath.mnt = au_br_mnt(br);
35190 +       h_userns = au_br_userns(br);
35191 +       qs.name = name;
35192 +       for (i = 0; i < 3; i++) {
35193 +               sprintf(p, "%.*x", AUFS_WH_TMP_LEN, cnt++);
35194 +               dentry = au_sio_lkup_one(h_userns, &qs, &h_ppath);
35195 +               if (IS_ERR(dentry) || d_is_negative(dentry))
35196 +                       goto out_name;
35197 +               dput(dentry);
35198 +       }
35199 +       /* pr_warn("could not get random name\n"); */
35200 +       dentry = ERR_PTR(-EEXIST);
35201 +       AuDbg("%.*s\n", AuLNPair(&qs));
35202 +       BUG();
35203 +
35204 +out_name:
35205 +       if (name != defname)
35206 +               au_kfree_try_rcu(name);
35207 +out:
35208 +       AuTraceErrPtr(dentry);
35209 +       return dentry;
35210 +}
35211 +
35212 +/*
35213 + * rename the @h_dentry on @br to the whiteouted temporary name.
35214 + */
35215 +int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br)
35216 +{
35217 +       int err;
35218 +       struct path h_path = {
35219 +               .mnt = au_br_mnt(br)
35220 +       };
35221 +       struct inode *h_dir, *delegated;
35222 +       struct dentry *h_parent;
35223 +
35224 +       h_parent = h_dentry->d_parent; /* dir inode is locked */
35225 +       h_dir = d_inode(h_parent);
35226 +       IMustLock(h_dir);
35227 +
35228 +       h_path.dentry = au_whtmp_lkup(h_parent, br, &h_dentry->d_name);
35229 +       err = PTR_ERR(h_path.dentry);
35230 +       if (IS_ERR(h_path.dentry))
35231 +               goto out;
35232 +
35233 +       /* under the same dir, no need to lock_rename() */
35234 +       delegated = NULL;
35235 +       err = vfsub_rename(h_dir, h_dentry, h_dir, &h_path, &delegated,
35236 +                          /*flags*/0);
35237 +       AuTraceErr(err);
35238 +       if (unlikely(err == -EWOULDBLOCK)) {
35239 +               pr_warn("cannot retry for NFSv4 delegation"
35240 +                       " for an internal rename\n");
35241 +               iput(delegated);
35242 +       }
35243 +       dput(h_path.dentry);
35244 +
35245 +out:
35246 +       AuTraceErr(err);
35247 +       return err;
35248 +}
35249 +
35250 +/* ---------------------------------------------------------------------- */
35251 +/*
35252 + * functions for removing a whiteout
35253 + */
35254 +
35255 +static int do_unlink_wh(struct inode *h_dir, struct path *h_path)
35256 +{
35257 +       int err, force;
35258 +       struct inode *delegated;
35259 +
35260 +       /*
35261 +        * forces superio when the dir has a sticky bit.
35262 +        * this may be a violation of unix fs semantics.
35263 +        */
35264 +       force = (h_dir->i_mode & S_ISVTX)
35265 +               && !uid_eq(current_fsuid(), d_inode(h_path->dentry)->i_uid);
35266 +       delegated = NULL;
35267 +       err = vfsub_unlink(h_dir, h_path, &delegated, force);
35268 +       if (unlikely(err == -EWOULDBLOCK)) {
35269 +               pr_warn("cannot retry for NFSv4 delegation"
35270 +                       " for an internal unlink\n");
35271 +               iput(delegated);
35272 +       }
35273 +       return err;
35274 +}
35275 +
35276 +int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
35277 +                       struct dentry *dentry)
35278 +{
35279 +       int err;
35280 +
35281 +       err = do_unlink_wh(h_dir, h_path);
35282 +       if (!err && dentry)
35283 +               au_set_dbwh(dentry, -1);
35284 +
35285 +       return err;
35286 +}
35287 +
35288 +static int unlink_wh_name(struct path *h_ppath, struct qstr *wh)
35289 +{
35290 +       int err;
35291 +       struct path h_path;
35292 +
35293 +       err = 0;
35294 +       h_path.dentry = vfsub_lkup_one(wh, h_ppath);
35295 +       if (IS_ERR(h_path.dentry))
35296 +               err = PTR_ERR(h_path.dentry);
35297 +       else {
35298 +               if (d_is_reg(h_path.dentry)) {
35299 +                       h_path.mnt = h_ppath->mnt;
35300 +                       err = do_unlink_wh(d_inode(h_ppath->dentry), &h_path);
35301 +               }
35302 +               dput(h_path.dentry);
35303 +       }
35304 +
35305 +       return err;
35306 +}
35307 +
35308 +/* ---------------------------------------------------------------------- */
35309 +/*
35310 + * initialize/clean whiteout for a branch
35311 + */
35312 +
35313 +static void au_wh_clean(struct inode *h_dir, struct path *whpath,
35314 +                       const int isdir)
35315 +{
35316 +       int err;
35317 +       struct inode *delegated;
35318 +
35319 +       if (d_is_negative(whpath->dentry))
35320 +               return;
35321 +
35322 +       if (isdir)
35323 +               err = vfsub_rmdir(h_dir, whpath);
35324 +       else {
35325 +               delegated = NULL;
35326 +               err = vfsub_unlink(h_dir, whpath, &delegated, /*force*/0);
35327 +               if (unlikely(err == -EWOULDBLOCK)) {
35328 +                       pr_warn("cannot retry for NFSv4 delegation"
35329 +                               " for an internal unlink\n");
35330 +                       iput(delegated);
35331 +               }
35332 +       }
35333 +       if (unlikely(err))
35334 +               pr_warn("failed removing %pd (%d), ignored.\n",
35335 +                       whpath->dentry, err);
35336 +}
35337 +
35338 +static int test_linkable(struct dentry *h_root)
35339 +{
35340 +       struct inode *h_dir = d_inode(h_root);
35341 +
35342 +       if (h_dir->i_op->link)
35343 +               return 0;
35344 +
35345 +       pr_err("%pd (%s) doesn't support link(2), use noplink and rw+nolwh\n",
35346 +              h_root, au_sbtype(h_root->d_sb));
35347 +       return -ENOSYS; /* the branch doesn't have its ->link() */
35348 +}
35349 +
35350 +/* todo: should this mkdir be done in /sbin/mount.aufs helper? */
35351 +static int au_whdir(struct inode *h_dir, struct path *path)
35352 +{
35353 +       int err;
35354 +
35355 +       err = -EEXIST;
35356 +       if (d_is_negative(path->dentry)) {
35357 +               int mode = 0700;
35358 +
35359 +               if (au_test_nfs(path->dentry->d_sb))
35360 +                       mode |= 0111;
35361 +               err = vfsub_mkdir(h_dir, path, mode);
35362 +       } else if (d_is_dir(path->dentry))
35363 +               err = 0;
35364 +       else
35365 +               pr_err("unknown %pd exists\n", path->dentry);
35366 +
35367 +       return err;
35368 +}
35369 +
35370 +struct au_wh_base {
35371 +       const struct qstr *name;
35372 +       struct dentry *dentry;
35373 +};
35374 +
35375 +static void au_wh_init_ro(struct inode *h_dir, struct au_wh_base base[],
35376 +                         struct path *h_path)
35377 +{
35378 +       h_path->dentry = base[AuBrWh_BASE].dentry;
35379 +       au_wh_clean(h_dir, h_path, /*isdir*/0);
35380 +       h_path->dentry = base[AuBrWh_PLINK].dentry;
35381 +       au_wh_clean(h_dir, h_path, /*isdir*/1);
35382 +       h_path->dentry = base[AuBrWh_ORPH].dentry;
35383 +       au_wh_clean(h_dir, h_path, /*isdir*/1);
35384 +}
35385 +
35386 +/*
35387 + * returns tri-state,
35388 + * minus: error, caller should print the message
35389 + * zero: success
35390 + * plus: error, caller should NOT print the message
35391 + */
35392 +static int au_wh_init_rw_nolink(struct dentry *h_root, struct au_wbr *wbr,
35393 +                               int do_plink, struct au_wh_base base[],
35394 +                               struct path *h_path)
35395 +{
35396 +       int err;
35397 +       struct inode *h_dir;
35398 +
35399 +       h_dir = d_inode(h_root);
35400 +       h_path->dentry = base[AuBrWh_BASE].dentry;
35401 +       au_wh_clean(h_dir, h_path, /*isdir*/0);
35402 +       h_path->dentry = base[AuBrWh_PLINK].dentry;
35403 +       if (do_plink) {
35404 +               err = test_linkable(h_root);
35405 +               if (unlikely(err)) {
35406 +                       err = 1;
35407 +                       goto out;
35408 +               }
35409 +
35410 +               err = au_whdir(h_dir, h_path);
35411 +               if (unlikely(err))
35412 +                       goto out;
35413 +               wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
35414 +       } else
35415 +               au_wh_clean(h_dir, h_path, /*isdir*/1);
35416 +       h_path->dentry = base[AuBrWh_ORPH].dentry;
35417 +       err = au_whdir(h_dir, h_path);
35418 +       if (unlikely(err))
35419 +               goto out;
35420 +       wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
35421 +
35422 +out:
35423 +       return err;
35424 +}
35425 +
35426 +/*
35427 + * for the moment, aufs supports the branch filesystem which does not support
35428 + * link(2). testing on FAT which does not support i_op->setattr() fully either,
35429 + * copyup failed. finally, such filesystem will not be used as the writable
35430 + * branch.
35431 + *
35432 + * returns tri-state, see above.
35433 + */
35434 +static int au_wh_init_rw(struct dentry *h_root, struct au_wbr *wbr,
35435 +                        int do_plink, struct au_wh_base base[],
35436 +                        struct path *h_path)
35437 +{
35438 +       int err;
35439 +       struct inode *h_dir;
35440 +
35441 +       WbrWhMustWriteLock(wbr);
35442 +
35443 +       err = test_linkable(h_root);
35444 +       if (unlikely(err)) {
35445 +               err = 1;
35446 +               goto out;
35447 +       }
35448 +
35449 +       /*
35450 +        * todo: should this create be done in /sbin/mount.aufs helper?
35451 +        */
35452 +       err = -EEXIST;
35453 +       h_dir = d_inode(h_root);
35454 +       if (d_is_negative(base[AuBrWh_BASE].dentry)) {
35455 +               h_path->dentry = base[AuBrWh_BASE].dentry;
35456 +               err = vfsub_create(h_dir, h_path, WH_MASK, /*want_excl*/true);
35457 +       } else if (d_is_reg(base[AuBrWh_BASE].dentry))
35458 +               err = 0;
35459 +       else
35460 +               pr_err("unknown %pd2 exists\n", base[AuBrWh_BASE].dentry);
35461 +       if (unlikely(err))
35462 +               goto out;
35463 +
35464 +       h_path->dentry = base[AuBrWh_PLINK].dentry;
35465 +       if (do_plink) {
35466 +               err = au_whdir(h_dir, h_path);
35467 +               if (unlikely(err))
35468 +                       goto out;
35469 +               wbr->wbr_plink = dget(base[AuBrWh_PLINK].dentry);
35470 +       } else
35471 +               au_wh_clean(h_dir, h_path, /*isdir*/1);
35472 +       wbr->wbr_whbase = dget(base[AuBrWh_BASE].dentry);
35473 +
35474 +       h_path->dentry = base[AuBrWh_ORPH].dentry;
35475 +       err = au_whdir(h_dir, h_path);
35476 +       if (unlikely(err))
35477 +               goto out;
35478 +       wbr->wbr_orph = dget(base[AuBrWh_ORPH].dentry);
35479 +
35480 +out:
35481 +       return err;
35482 +}
35483 +
35484 +/*
35485 + * initialize the whiteout base file/dir for @br.
35486 + */
35487 +int au_wh_init(struct au_branch *br, struct super_block *sb)
35488 +{
35489 +       int err, i;
35490 +       const unsigned char do_plink
35491 +               = !!au_opt_test(au_mntflags(sb), PLINK);
35492 +       struct inode *h_dir;
35493 +       struct path path = br->br_path;
35494 +       struct dentry *h_root = path.dentry;
35495 +       struct au_wbr *wbr = br->br_wbr;
35496 +       static const struct qstr base_name[] = {
35497 +               [AuBrWh_BASE] = QSTR_INIT(AUFS_BASE_NAME,
35498 +                                         sizeof(AUFS_BASE_NAME) - 1),
35499 +               [AuBrWh_PLINK] = QSTR_INIT(AUFS_PLINKDIR_NAME,
35500 +                                          sizeof(AUFS_PLINKDIR_NAME) - 1),
35501 +               [AuBrWh_ORPH] = QSTR_INIT(AUFS_ORPHDIR_NAME,
35502 +                                         sizeof(AUFS_ORPHDIR_NAME) - 1)
35503 +       };
35504 +       struct au_wh_base base[] = {
35505 +               [AuBrWh_BASE] = {
35506 +                       .name   = base_name + AuBrWh_BASE,
35507 +                       .dentry = NULL
35508 +               },
35509 +               [AuBrWh_PLINK] = {
35510 +                       .name   = base_name + AuBrWh_PLINK,
35511 +                       .dentry = NULL
35512 +               },
35513 +               [AuBrWh_ORPH] = {
35514 +                       .name   = base_name + AuBrWh_ORPH,
35515 +                       .dentry = NULL
35516 +               }
35517 +       };
35518 +
35519 +       if (wbr)
35520 +               WbrWhMustWriteLock(wbr);
35521 +
35522 +       for (i = 0; i < AuBrWh_Last; i++) {
35523 +               /* doubly whiteouted */
35524 +               struct dentry *d;
35525 +
35526 +               d = au_wh_lkup(h_root, (void *)base[i].name, br);
35527 +               err = PTR_ERR(d);
35528 +               if (IS_ERR(d))
35529 +                       goto out;
35530 +
35531 +               base[i].dentry = d;
35532 +               AuDebugOn(wbr
35533 +                         && wbr->wbr_wh[i]
35534 +                         && wbr->wbr_wh[i] != base[i].dentry);
35535 +       }
35536 +
35537 +       if (wbr)
35538 +               for (i = 0; i < AuBrWh_Last; i++) {
35539 +                       dput(wbr->wbr_wh[i]);
35540 +                       wbr->wbr_wh[i] = NULL;
35541 +               }
35542 +
35543 +       err = 0;
35544 +       if (!au_br_writable(br->br_perm)) {
35545 +               h_dir = d_inode(h_root);
35546 +               au_wh_init_ro(h_dir, base, &path);
35547 +       } else if (!au_br_wh_linkable(br->br_perm)) {
35548 +               err = au_wh_init_rw_nolink(h_root, wbr, do_plink, base, &path);
35549 +               if (err > 0)
35550 +                       goto out;
35551 +               else if (err)
35552 +                       goto out_err;
35553 +       } else {
35554 +               err = au_wh_init_rw(h_root, wbr, do_plink, base, &path);
35555 +               if (err > 0)
35556 +                       goto out;
35557 +               else if (err)
35558 +                       goto out_err;
35559 +       }
35560 +       goto out; /* success */
35561 +
35562 +out_err:
35563 +       pr_err("an error(%d) on the writable branch %pd(%s)\n",
35564 +              err, h_root, au_sbtype(h_root->d_sb));
35565 +out:
35566 +       for (i = 0; i < AuBrWh_Last; i++)
35567 +               dput(base[i].dentry);
35568 +       return err;
35569 +}
35570 +
35571 +/* ---------------------------------------------------------------------- */
35572 +/*
35573 + * whiteouts are all hard-linked usually.
35574 + * when its link count reaches a ceiling, we create a new whiteout base
35575 + * asynchronously.
35576 + */
35577 +
35578 +struct reinit_br_wh {
35579 +       struct super_block *sb;
35580 +       struct au_branch *br;
35581 +};
35582 +
35583 +static void reinit_br_wh(void *arg)
35584 +{
35585 +       int err;
35586 +       aufs_bindex_t bindex;
35587 +       struct path h_path;
35588 +       struct reinit_br_wh *a = arg;
35589 +       struct au_wbr *wbr;
35590 +       struct inode *dir, *delegated;
35591 +       struct dentry *h_root;
35592 +       struct au_hinode *hdir;
35593 +
35594 +       err = 0;
35595 +       wbr = a->br->br_wbr;
35596 +       /* big aufs lock */
35597 +       si_noflush_write_lock(a->sb);
35598 +       if (!au_br_writable(a->br->br_perm))
35599 +               goto out;
35600 +       bindex = au_br_index(a->sb, a->br->br_id);
35601 +       if (unlikely(bindex < 0))
35602 +               goto out;
35603 +
35604 +       di_read_lock_parent(a->sb->s_root, AuLock_IR);
35605 +       dir = d_inode(a->sb->s_root);
35606 +       hdir = au_hi(dir, bindex);
35607 +       h_root = au_h_dptr(a->sb->s_root, bindex);
35608 +       AuDebugOn(h_root != au_br_dentry(a->br));
35609 +
35610 +       au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
35611 +       wbr_wh_write_lock(wbr);
35612 +       err = au_h_verify(wbr->wbr_whbase, au_opt_udba(a->sb), hdir->hi_inode,
35613 +                         h_root, a->br);
35614 +       if (!err) {
35615 +               h_path.dentry = wbr->wbr_whbase;
35616 +               h_path.mnt = au_br_mnt(a->br);
35617 +               delegated = NULL;
35618 +               err = vfsub_unlink(hdir->hi_inode, &h_path, &delegated,
35619 +                                  /*force*/0);
35620 +               if (unlikely(err == -EWOULDBLOCK)) {
35621 +                       pr_warn("cannot retry for NFSv4 delegation"
35622 +                               " for an internal unlink\n");
35623 +                       iput(delegated);
35624 +               }
35625 +       } else {
35626 +               pr_warn("%pd is moved, ignored\n", wbr->wbr_whbase);
35627 +               err = 0;
35628 +       }
35629 +       dput(wbr->wbr_whbase);
35630 +       wbr->wbr_whbase = NULL;
35631 +       if (!err)
35632 +               err = au_wh_init(a->br, a->sb);
35633 +       wbr_wh_write_unlock(wbr);
35634 +       au_hn_inode_unlock(hdir);
35635 +       di_read_unlock(a->sb->s_root, AuLock_IR);
35636 +       if (!err)
35637 +               au_fhsm_wrote(a->sb, bindex, /*force*/0);
35638 +
35639 +out:
35640 +       if (wbr)
35641 +               atomic_dec(&wbr->wbr_wh_running);
35642 +       au_lcnt_dec(&a->br->br_count);
35643 +       si_write_unlock(a->sb);
35644 +       au_nwt_done(&au_sbi(a->sb)->si_nowait);
35645 +       au_kfree_rcu(a);
35646 +       if (unlikely(err))
35647 +               AuIOErr("err %d\n", err);
35648 +}
35649 +
35650 +static void kick_reinit_br_wh(struct super_block *sb, struct au_branch *br)
35651 +{
35652 +       int do_dec, wkq_err;
35653 +       struct reinit_br_wh *arg;
35654 +
35655 +       do_dec = 1;
35656 +       if (atomic_inc_return(&br->br_wbr->wbr_wh_running) != 1)
35657 +               goto out;
35658 +
35659 +       /* ignore ENOMEM */
35660 +       arg = kmalloc(sizeof(*arg), GFP_NOFS);
35661 +       if (arg) {
35662 +               /*
35663 +                * dec(wh_running), kfree(arg) and dec(br_count)
35664 +                * in reinit function
35665 +                */
35666 +               arg->sb = sb;
35667 +               arg->br = br;
35668 +               au_lcnt_inc(&br->br_count);
35669 +               wkq_err = au_wkq_nowait(reinit_br_wh, arg, sb, /*flags*/0);
35670 +               if (unlikely(wkq_err)) {
35671 +                       atomic_dec(&br->br_wbr->wbr_wh_running);
35672 +                       au_lcnt_dec(&br->br_count);
35673 +                       au_kfree_rcu(arg);
35674 +               }
35675 +               do_dec = 0;
35676 +       }
35677 +
35678 +out:
35679 +       if (do_dec)
35680 +               atomic_dec(&br->br_wbr->wbr_wh_running);
35681 +}
35682 +
35683 +/* ---------------------------------------------------------------------- */
35684 +
35685 +/*
35686 + * create the whiteout @wh.
35687 + */
35688 +static int link_or_create_wh(struct super_block *sb, aufs_bindex_t bindex,
35689 +                            struct dentry *wh)
35690 +{
35691 +       int err;
35692 +       struct path h_path = {
35693 +               .dentry = wh
35694 +       };
35695 +       struct au_branch *br;
35696 +       struct au_wbr *wbr;
35697 +       struct dentry *h_parent;
35698 +       struct inode *h_dir, *delegated;
35699 +
35700 +       h_parent = wh->d_parent; /* dir inode is locked */
35701 +       h_dir = d_inode(h_parent);
35702 +       IMustLock(h_dir);
35703 +
35704 +       br = au_sbr(sb, bindex);
35705 +       h_path.mnt = au_br_mnt(br);
35706 +       wbr = br->br_wbr;
35707 +       wbr_wh_read_lock(wbr);
35708 +       if (wbr->wbr_whbase) {
35709 +               delegated = NULL;
35710 +               err = vfsub_link(wbr->wbr_whbase, h_dir, &h_path, &delegated);
35711 +               if (unlikely(err == -EWOULDBLOCK)) {
35712 +                       pr_warn("cannot retry for NFSv4 delegation"
35713 +                               " for an internal link\n");
35714 +                       iput(delegated);
35715 +               }
35716 +               if (!err || err != -EMLINK)
35717 +                       goto out;
35718 +
35719 +               /* link count full. re-initialize br_whbase. */
35720 +               kick_reinit_br_wh(sb, br);
35721 +       }
35722 +
35723 +       /* return this error in this context */
35724 +       err = vfsub_create(h_dir, &h_path, WH_MASK, /*want_excl*/true);
35725 +       if (!err)
35726 +               au_fhsm_wrote(sb, bindex, /*force*/0);
35727 +
35728 +out:
35729 +       wbr_wh_read_unlock(wbr);
35730 +       return err;
35731 +}
35732 +
35733 +/* ---------------------------------------------------------------------- */
35734 +
35735 +/*
35736 + * create or remove the diropq.
35737 + */
35738 +static struct dentry *do_diropq(struct dentry *dentry, aufs_bindex_t bindex,
35739 +                               unsigned int flags)
35740 +{
35741 +       struct dentry *opq_dentry;
35742 +       struct super_block *sb;
35743 +       struct au_branch *br;
35744 +       struct path h_path;
35745 +       int err;
35746 +
35747 +       sb = dentry->d_sb;
35748 +       br = au_sbr(sb, bindex);
35749 +       h_path.dentry = au_h_dptr(dentry, bindex);
35750 +       h_path.mnt = au_br_mnt(br);
35751 +       opq_dentry = vfsub_lkup_one(&diropq_name, &h_path);
35752 +       if (IS_ERR(opq_dentry))
35753 +               goto out;
35754 +
35755 +       if (au_ftest_diropq(flags, CREATE)) {
35756 +               err = link_or_create_wh(sb, bindex, opq_dentry);
35757 +               if (!err) {
35758 +                       au_set_dbdiropq(dentry, bindex);
35759 +                       goto out; /* success */
35760 +               }
35761 +       } else {
35762 +               h_path.dentry = opq_dentry;
35763 +               err = do_unlink_wh(au_h_iptr(d_inode(dentry), bindex), &h_path);
35764 +               if (!err)
35765 +                       au_set_dbdiropq(dentry, -1);
35766 +       }
35767 +       dput(opq_dentry);
35768 +       opq_dentry = ERR_PTR(err);
35769 +
35770 +out:
35771 +       return opq_dentry;
35772 +}
35773 +
35774 +struct do_diropq_args {
35775 +       struct dentry **errp;
35776 +       struct dentry *dentry;
35777 +       aufs_bindex_t bindex;
35778 +       unsigned int flags;
35779 +};
35780 +
35781 +static void call_do_diropq(void *args)
35782 +{
35783 +       struct do_diropq_args *a = args;
35784 +       *a->errp = do_diropq(a->dentry, a->bindex, a->flags);
35785 +}
35786 +
35787 +struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
35788 +                            unsigned int flags)
35789 +{
35790 +       struct dentry *diropq, *h_dentry;
35791 +       struct user_namespace *h_userns;
35792 +
35793 +       h_userns = au_sbr_userns(dentry->d_sb, bindex);
35794 +       h_dentry = au_h_dptr(dentry, bindex);
35795 +       if (!au_test_h_perm_sio(h_userns, d_inode(h_dentry),
35796 +                               MAY_EXEC | MAY_WRITE))
35797 +               diropq = do_diropq(dentry, bindex, flags);
35798 +       else {
35799 +               int wkq_err;
35800 +               struct do_diropq_args args = {
35801 +                       .errp           = &diropq,
35802 +                       .dentry         = dentry,
35803 +                       .bindex         = bindex,
35804 +                       .flags          = flags
35805 +               };
35806 +
35807 +               wkq_err = au_wkq_wait(call_do_diropq, &args);
35808 +               if (unlikely(wkq_err))
35809 +                       diropq = ERR_PTR(wkq_err);
35810 +       }
35811 +
35812 +       return diropq;
35813 +}
35814 +
35815 +/* ---------------------------------------------------------------------- */
35816 +
35817 +/*
35818 + * lookup whiteout dentry.
35819 + * @h_parent: lower parent dentry which must exist and be locked
35820 + * @base_name: name of dentry which will be whiteouted
35821 + * returns dentry for whiteout.
35822 + */
35823 +struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
35824 +                         struct au_branch *br)
35825 +{
35826 +       int err;
35827 +       struct qstr wh_name;
35828 +       struct dentry *wh_dentry;
35829 +       struct path h_path;
35830 +
35831 +       err = au_wh_name_alloc(&wh_name, base_name);
35832 +       wh_dentry = ERR_PTR(err);
35833 +       if (!err) {
35834 +               h_path.dentry = h_parent;
35835 +               h_path.mnt = au_br_mnt(br);
35836 +               wh_dentry = vfsub_lkup_one(&wh_name, &h_path);
35837 +               au_kfree_try_rcu(wh_name.name);
35838 +       }
35839 +       return wh_dentry;
35840 +}
35841 +
35842 +/*
35843 + * link/create a whiteout for @dentry on @bindex.
35844 + */
35845 +struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
35846 +                           struct dentry *h_parent)
35847 +{
35848 +       struct dentry *wh_dentry;
35849 +       struct super_block *sb;
35850 +       int err;
35851 +
35852 +       sb = dentry->d_sb;
35853 +       wh_dentry = au_wh_lkup(h_parent, &dentry->d_name, au_sbr(sb, bindex));
35854 +       if (!IS_ERR(wh_dentry) && d_is_negative(wh_dentry)) {
35855 +               err = link_or_create_wh(sb, bindex, wh_dentry);
35856 +               if (!err) {
35857 +                       au_set_dbwh(dentry, bindex);
35858 +                       au_fhsm_wrote(sb, bindex, /*force*/0);
35859 +               } else {
35860 +                       dput(wh_dentry);
35861 +                       wh_dentry = ERR_PTR(err);
35862 +               }
35863 +       }
35864 +
35865 +       return wh_dentry;
35866 +}
35867 +
35868 +/* ---------------------------------------------------------------------- */
35869 +
35870 +/* Delete all whiteouts in this directory on branch bindex. */
35871 +static int del_wh_children(struct path *h_path, struct au_nhash *whlist,
35872 +                          aufs_bindex_t bindex)
35873 +{
35874 +       int err;
35875 +       unsigned long ul, n;
35876 +       struct qstr wh_name;
35877 +       char *p;
35878 +       struct hlist_head *head;
35879 +       struct au_vdir_wh *pos;
35880 +       struct au_vdir_destr *str;
35881 +
35882 +       err = -ENOMEM;
35883 +       p = (void *)__get_free_page(GFP_NOFS);
35884 +       wh_name.name = p;
35885 +       if (unlikely(!wh_name.name))
35886 +               goto out;
35887 +
35888 +       err = 0;
35889 +       memcpy(p, AUFS_WH_PFX, AUFS_WH_PFX_LEN);
35890 +       p += AUFS_WH_PFX_LEN;
35891 +       n = whlist->nh_num;
35892 +       head = whlist->nh_head;
35893 +       for (ul = 0; !err && ul < n; ul++, head++) {
35894 +               hlist_for_each_entry(pos, head, wh_hash) {
35895 +                       if (pos->wh_bindex != bindex)
35896 +                               continue;
35897 +
35898 +                       str = &pos->wh_str;
35899 +                       if (str->len + AUFS_WH_PFX_LEN <= PATH_MAX) {
35900 +                               memcpy(p, str->name, str->len);
35901 +                               wh_name.len = AUFS_WH_PFX_LEN + str->len;
35902 +                               err = unlink_wh_name(h_path, &wh_name);
35903 +                               if (!err)
35904 +                                       continue;
35905 +                               break;
35906 +                       }
35907 +                       AuIOErr("whiteout name too long %.*s\n",
35908 +                               str->len, str->name);
35909 +                       err = -EIO;
35910 +                       break;
35911 +               }
35912 +       }
35913 +       free_page((unsigned long)wh_name.name);
35914 +
35915 +out:
35916 +       return err;
35917 +}
35918 +
35919 +struct del_wh_children_args {
35920 +       int *errp;
35921 +       struct path *h_path;
35922 +       struct au_nhash *whlist;
35923 +       aufs_bindex_t bindex;
35924 +};
35925 +
35926 +static void call_del_wh_children(void *args)
35927 +{
35928 +       struct del_wh_children_args *a = args;
35929 +       *a->errp = del_wh_children(a->h_path, a->whlist, a->bindex);
35930 +}
35931 +
35932 +/* ---------------------------------------------------------------------- */
35933 +
35934 +struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp)
35935 +{
35936 +       struct au_whtmp_rmdir *whtmp;
35937 +       int err;
35938 +       unsigned int rdhash;
35939 +
35940 +       SiMustAnyLock(sb);
35941 +
35942 +       whtmp = kzalloc(sizeof(*whtmp), gfp);
35943 +       if (unlikely(!whtmp)) {
35944 +               whtmp = ERR_PTR(-ENOMEM);
35945 +               goto out;
35946 +       }
35947 +
35948 +       /* no estimation for dir size */
35949 +       rdhash = au_sbi(sb)->si_rdhash;
35950 +       if (!rdhash)
35951 +               rdhash = AUFS_RDHASH_DEF;
35952 +       err = au_nhash_alloc(&whtmp->whlist, rdhash, gfp);
35953 +       if (unlikely(err)) {
35954 +               au_kfree_rcu(whtmp);
35955 +               whtmp = ERR_PTR(err);
35956 +       }
35957 +
35958 +out:
35959 +       return whtmp;
35960 +}
35961 +
35962 +void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp)
35963 +{
35964 +       if (whtmp->br)
35965 +               au_lcnt_dec(&whtmp->br->br_count);
35966 +       dput(whtmp->wh_dentry);
35967 +       iput(whtmp->dir);
35968 +       au_nhash_wh_free(&whtmp->whlist);
35969 +       au_kfree_rcu(whtmp);
35970 +}
35971 +
35972 +/*
35973 + * rmdir the whiteouted temporary named dir @h_dentry.
35974 + * @whlist: whiteouted children.
35975 + */
35976 +int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
35977 +                  struct dentry *wh_dentry, struct au_nhash *whlist)
35978 +{
35979 +       int err;
35980 +       unsigned int h_nlink;
35981 +       struct path wh_path;
35982 +       struct inode *wh_inode, *h_dir;
35983 +       struct au_branch *br;
35984 +       struct user_namespace *h_userns;
35985 +
35986 +       h_dir = d_inode(wh_dentry->d_parent); /* dir inode is locked */
35987 +       IMustLock(h_dir);
35988 +
35989 +       br = au_sbr(dir->i_sb, bindex);
35990 +       wh_path.dentry = wh_dentry;
35991 +       wh_path.mnt = au_br_mnt(br);
35992 +       h_userns = au_br_userns(br);
35993 +       wh_inode = d_inode(wh_dentry);
35994 +       inode_lock_nested(wh_inode, AuLsc_I_CHILD);
35995 +
35996 +       /*
35997 +        * someone else might change some whiteouts while we were sleeping.
35998 +        * it means this whlist may have an obsoleted entry.
35999 +        */
36000 +       if (!au_test_h_perm_sio(h_userns, wh_inode, MAY_EXEC | MAY_WRITE))
36001 +               err = del_wh_children(&wh_path, whlist, bindex);
36002 +       else {
36003 +               int wkq_err;
36004 +               struct del_wh_children_args args = {
36005 +                       .errp           = &err,
36006 +                       .h_path         = &wh_path,
36007 +                       .whlist         = whlist,
36008 +                       .bindex         = bindex
36009 +               };
36010 +
36011 +               wkq_err = au_wkq_wait(call_del_wh_children, &args);
36012 +               if (unlikely(wkq_err))
36013 +                       err = wkq_err;
36014 +       }
36015 +       inode_unlock(wh_inode);
36016 +
36017 +       if (!err) {
36018 +               h_nlink = h_dir->i_nlink;
36019 +               err = vfsub_rmdir(h_dir, &wh_path);
36020 +               /* some fs doesn't change the parent nlink in some cases */
36021 +               h_nlink -= h_dir->i_nlink;
36022 +       }
36023 +
36024 +       if (!err) {
36025 +               if (au_ibtop(dir) == bindex) {
36026 +                       /* todo: dir->i_mutex is necessary */
36027 +                       au_cpup_attr_timesizes(dir);
36028 +                       if (h_nlink)
36029 +                               vfsub_drop_nlink(dir);
36030 +               }
36031 +               return 0; /* success */
36032 +       }
36033 +
36034 +       pr_warn("failed removing %pd(%d), ignored\n", wh_dentry, err);
36035 +       return err;
36036 +}
36037 +
36038 +static void call_rmdir_whtmp(void *args)
36039 +{
36040 +       int err;
36041 +       aufs_bindex_t bindex;
36042 +       struct au_whtmp_rmdir *a = args;
36043 +       struct super_block *sb;
36044 +       struct dentry *h_parent;
36045 +       struct inode *h_dir;
36046 +       struct au_hinode *hdir;
36047 +
36048 +       /* rmdir by nfsd may cause deadlock with this i_mutex */
36049 +       /* inode_lock(a->dir); */
36050 +       err = -EROFS;
36051 +       sb = a->dir->i_sb;
36052 +       si_read_lock(sb, !AuLock_FLUSH);
36053 +       if (!au_br_writable(a->br->br_perm))
36054 +               goto out;
36055 +       bindex = au_br_index(sb, a->br->br_id);
36056 +       if (unlikely(bindex < 0))
36057 +               goto out;
36058 +
36059 +       err = -EIO;
36060 +       ii_write_lock_parent(a->dir);
36061 +       h_parent = dget_parent(a->wh_dentry);
36062 +       h_dir = d_inode(h_parent);
36063 +       hdir = au_hi(a->dir, bindex);
36064 +       err = vfsub_mnt_want_write(au_br_mnt(a->br));
36065 +       if (unlikely(err))
36066 +               goto out_mnt;
36067 +       au_hn_inode_lock_nested(hdir, AuLsc_I_PARENT);
36068 +       err = au_h_verify(a->wh_dentry, au_opt_udba(sb), h_dir, h_parent,
36069 +                         a->br);
36070 +       if (!err)
36071 +               err = au_whtmp_rmdir(a->dir, bindex, a->wh_dentry, &a->whlist);
36072 +       au_hn_inode_unlock(hdir);
36073 +       vfsub_mnt_drop_write(au_br_mnt(a->br));
36074 +
36075 +out_mnt:
36076 +       dput(h_parent);
36077 +       ii_write_unlock(a->dir);
36078 +out:
36079 +       /* inode_unlock(a->dir); */
36080 +       au_whtmp_rmdir_free(a);
36081 +       si_read_unlock(sb);
36082 +       au_nwt_done(&au_sbi(sb)->si_nowait);
36083 +       if (unlikely(err))
36084 +               AuIOErr("err %d\n", err);
36085 +}
36086 +
36087 +void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
36088 +                        struct dentry *wh_dentry, struct au_whtmp_rmdir *args)
36089 +{
36090 +       int wkq_err;
36091 +       struct super_block *sb;
36092 +
36093 +       IMustLock(dir);
36094 +
36095 +       /* all post-process will be done in do_rmdir_whtmp(). */
36096 +       sb = dir->i_sb;
36097 +       args->dir = au_igrab(dir);
36098 +       args->br = au_sbr(sb, bindex);
36099 +       au_lcnt_inc(&args->br->br_count);
36100 +       args->wh_dentry = dget(wh_dentry);
36101 +       wkq_err = au_wkq_nowait(call_rmdir_whtmp, args, sb, /*flags*/0);
36102 +       if (unlikely(wkq_err)) {
36103 +               pr_warn("rmdir error %pd (%d), ignored\n", wh_dentry, wkq_err);
36104 +               au_whtmp_rmdir_free(args);
36105 +       }
36106 +}
36107 diff -urN /usr/share/empty/fs/aufs/whout.h linux/fs/aufs/whout.h
36108 --- /usr/share/empty/fs/aufs/whout.h    1970-01-01 01:00:00.000000000 +0100
36109 +++ linux/fs/aufs/whout.h       2022-03-21 14:49:05.733299677 +0100
36110 @@ -0,0 +1,87 @@
36111 +/* SPDX-License-Identifier: GPL-2.0 */
36112 +/*
36113 + * Copyright (C) 2005-2021 Junjiro R. Okajima
36114 + *
36115 + * This program, aufs is free software; you can redistribute it and/or modify
36116 + * it under the terms of the GNU General Public License as published by
36117 + * the Free Software Foundation; either version 2 of the License, or
36118 + * (at your option) any later version.
36119 + *
36120 + * This program is distributed in the hope that it will be useful,
36121 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
36122 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36123 + * GNU General Public License for more details.
36124 + *
36125 + * You should have received a copy of the GNU General Public License
36126 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
36127 + */
36128 +
36129 +/*
36130 + * whiteout for logical deletion and opaque directory
36131 + */
36132 +
36133 +#ifndef __AUFS_WHOUT_H__
36134 +#define __AUFS_WHOUT_H__
36135 +
36136 +#ifdef __KERNEL__
36137 +
36138 +#include "dir.h"
36139 +
36140 +/* whout.c */
36141 +int au_wh_name_alloc(struct qstr *wh, const struct qstr *name);
36142 +int au_wh_test(struct user_namespace *h_userns, struct path *h_ppath,
36143 +              struct qstr *wh_name, int try_sio);
36144 +int au_diropq_test(struct user_namespace *h_userns, struct path *h_path);
36145 +struct au_branch;
36146 +struct dentry *au_whtmp_lkup(struct dentry *h_parent, struct au_branch *br,
36147 +                            struct qstr *prefix);
36148 +int au_whtmp_ren(struct dentry *h_dentry, struct au_branch *br);
36149 +int au_wh_unlink_dentry(struct inode *h_dir, struct path *h_path,
36150 +                       struct dentry *dentry);
36151 +int au_wh_init(struct au_branch *br, struct super_block *sb);
36152 +
36153 +/* diropq flags */
36154 +#define AuDiropq_CREATE        1
36155 +#define au_ftest_diropq(flags, name)   ((flags) & AuDiropq_##name)
36156 +#define au_fset_diropq(flags, name) \
36157 +       do { (flags) |= AuDiropq_##name; } while (0)
36158 +#define au_fclr_diropq(flags, name) \
36159 +       do { (flags) &= ~AuDiropq_##name; } while (0)
36160 +
36161 +struct dentry *au_diropq_sio(struct dentry *dentry, aufs_bindex_t bindex,
36162 +                            unsigned int flags);
36163 +struct dentry *au_wh_lkup(struct dentry *h_parent, struct qstr *base_name,
36164 +                         struct au_branch *br);
36165 +struct dentry *au_wh_create(struct dentry *dentry, aufs_bindex_t bindex,
36166 +                           struct dentry *h_parent);
36167 +
36168 +/* real rmdir for the whiteout-ed dir */
36169 +struct au_whtmp_rmdir {
36170 +       struct inode *dir;
36171 +       struct au_branch *br;
36172 +       struct dentry *wh_dentry;
36173 +       struct au_nhash whlist;
36174 +};
36175 +
36176 +struct au_whtmp_rmdir *au_whtmp_rmdir_alloc(struct super_block *sb, gfp_t gfp);
36177 +void au_whtmp_rmdir_free(struct au_whtmp_rmdir *whtmp);
36178 +int au_whtmp_rmdir(struct inode *dir, aufs_bindex_t bindex,
36179 +                  struct dentry *wh_dentry, struct au_nhash *whlist);
36180 +void au_whtmp_kick_rmdir(struct inode *dir, aufs_bindex_t bindex,
36181 +                        struct dentry *wh_dentry, struct au_whtmp_rmdir *args);
36182 +
36183 +/* ---------------------------------------------------------------------- */
36184 +
36185 +static inline struct dentry *au_diropq_create(struct dentry *dentry,
36186 +                                             aufs_bindex_t bindex)
36187 +{
36188 +       return au_diropq_sio(dentry, bindex, AuDiropq_CREATE);
36189 +}
36190 +
36191 +static inline int au_diropq_remove(struct dentry *dentry, aufs_bindex_t bindex)
36192 +{
36193 +       return PTR_ERR(au_diropq_sio(dentry, bindex, !AuDiropq_CREATE));
36194 +}
36195 +
36196 +#endif /* __KERNEL__ */
36197 +#endif /* __AUFS_WHOUT_H__ */
36198 diff -urN /usr/share/empty/fs/aufs/wkq.c linux/fs/aufs/wkq.c
36199 --- /usr/share/empty/fs/aufs/wkq.c      1970-01-01 01:00:00.000000000 +0100
36200 +++ linux/fs/aufs/wkq.c 2022-03-21 14:49:05.733299677 +0100
36201 @@ -0,0 +1,372 @@
36202 +// SPDX-License-Identifier: GPL-2.0
36203 +/*
36204 + * Copyright (C) 2005-2021 Junjiro R. Okajima
36205 + *
36206 + * This program, aufs is free software; you can redistribute it and/or modify
36207 + * it under the terms of the GNU General Public License as published by
36208 + * the Free Software Foundation; either version 2 of the License, or
36209 + * (at your option) any later version.
36210 + *
36211 + * This program is distributed in the hope that it will be useful,
36212 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
36213 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36214 + * GNU General Public License for more details.
36215 + *
36216 + * You should have received a copy of the GNU General Public License
36217 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
36218 + */
36219 +
36220 +/*
36221 + * workqueue for asynchronous/super-io operations
36222 + * todo: try new credential scheme
36223 + */
36224 +
36225 +#include <linux/module.h>
36226 +#include "aufs.h"
36227 +
36228 +/* internal workqueue named AUFS_WKQ_NAME */
36229 +
36230 +static struct workqueue_struct *au_wkq;
36231 +
36232 +struct au_wkinfo {
36233 +       struct work_struct wk;
36234 +       struct kobject *kobj;
36235 +
36236 +       unsigned int flags; /* see wkq.h */
36237 +
36238 +       au_wkq_func_t func;
36239 +       void *args;
36240 +
36241 +#ifdef CONFIG_LOCKDEP
36242 +       int dont_check;
36243 +       struct held_lock **hlock;
36244 +#endif
36245 +
36246 +       struct completion *comp;
36247 +};
36248 +
36249 +/* ---------------------------------------------------------------------- */
36250 +/*
36251 + * Aufs passes some operations to the workqueue such as the internal copyup.
36252 + * This scheme looks rather unnatural for LOCKDEP debugging feature, since the
36253 + * job run by workqueue depends upon the locks acquired in the other task.
36254 + * Delegating a small operation to the workqueue, aufs passes its lockdep
36255 + * information too. And the job in the workqueue restores the info in order to
36256 + * pretend as if it acquired those locks. This is just to make LOCKDEP work
36257 + * correctly and expectedly.
36258 + */
36259 +
36260 +#ifndef CONFIG_LOCKDEP
36261 +AuStubInt0(au_wkq_lockdep_alloc, struct au_wkinfo *wkinfo);
36262 +AuStubVoid(au_wkq_lockdep_free, struct au_wkinfo *wkinfo);
36263 +AuStubVoid(au_wkq_lockdep_pre, struct au_wkinfo *wkinfo);
36264 +AuStubVoid(au_wkq_lockdep_post, struct au_wkinfo *wkinfo);
36265 +AuStubVoid(au_wkq_lockdep_init, struct au_wkinfo *wkinfo);
36266 +#else
36267 +static void au_wkq_lockdep_init(struct au_wkinfo *wkinfo)
36268 +{
36269 +       wkinfo->hlock = NULL;
36270 +       wkinfo->dont_check = 0;
36271 +}
36272 +
36273 +/*
36274 + * 1: matched
36275 + * 0: unmatched
36276 + */
36277 +static int au_wkq_lockdep_test(struct lock_class_key *key, const char *name)
36278 +{
36279 +       static DEFINE_SPINLOCK(spin);
36280 +       static struct {
36281 +               char *name;
36282 +               struct lock_class_key *key;
36283 +       } a[] = {
36284 +               { .name = "&sbinfo->si_rwsem" },
36285 +               { .name = "&finfo->fi_rwsem" },
36286 +               { .name = "&dinfo->di_rwsem" },
36287 +               { .name = "&iinfo->ii_rwsem" }
36288 +       };
36289 +       static int set;
36290 +       int i;
36291 +
36292 +       /* lockless read from 'set.' see below */
36293 +       if (set == ARRAY_SIZE(a)) {
36294 +               for (i = 0; i < ARRAY_SIZE(a); i++)
36295 +                       if (a[i].key == key)
36296 +                               goto match;
36297 +               goto unmatch;
36298 +       }
36299 +
36300 +       spin_lock(&spin);
36301 +       if (set)
36302 +               for (i = 0; i < ARRAY_SIZE(a); i++)
36303 +                       if (a[i].key == key) {
36304 +                               spin_unlock(&spin);
36305 +                               goto match;
36306 +                       }
36307 +       for (i = 0; i < ARRAY_SIZE(a); i++) {
36308 +               if (a[i].key) {
36309 +                       if (unlikely(a[i].key == key)) { /* rare but possible */
36310 +                               spin_unlock(&spin);
36311 +                               goto match;
36312 +                       } else
36313 +                               continue;
36314 +               }
36315 +               if (strstr(a[i].name, name)) {
36316 +                       /*
36317 +                        * the order of these three lines is important for the
36318 +                        * lockless read above.
36319 +                        */
36320 +                       a[i].key = key;
36321 +                       spin_unlock(&spin);
36322 +                       set++;
36323 +                       /* AuDbg("%d, %s\n", set, name); */
36324 +                       goto match;
36325 +               }
36326 +       }
36327 +       spin_unlock(&spin);
36328 +       goto unmatch;
36329 +
36330 +match:
36331 +       return 1;
36332 +unmatch:
36333 +       return 0;
36334 +}
36335 +
36336 +static int au_wkq_lockdep_alloc(struct au_wkinfo *wkinfo)
36337 +{
36338 +       int err, n;
36339 +       struct task_struct *curr;
36340 +       struct held_lock **hl, *held_locks, *p;
36341 +
36342 +       err = 0;
36343 +       curr = current;
36344 +       wkinfo->dont_check = lockdep_recursing(curr);
36345 +       if (wkinfo->dont_check)
36346 +               goto out;
36347 +       n = curr->lockdep_depth;
36348 +       if (!n)
36349 +               goto out;
36350 +
36351 +       err = -ENOMEM;
36352 +       wkinfo->hlock = kmalloc_array(n + 1, sizeof(*wkinfo->hlock), GFP_NOFS);
36353 +       if (unlikely(!wkinfo->hlock))
36354 +               goto out;
36355 +
36356 +       err = 0;
36357 +#if 0 /* left for debugging */
36358 +       if (0 && au_debug_test())
36359 +               lockdep_print_held_locks(curr);
36360 +#endif
36361 +       held_locks = curr->held_locks;
36362 +       hl = wkinfo->hlock;
36363 +       while (n--) {
36364 +               p = held_locks++;
36365 +               if (au_wkq_lockdep_test(p->instance->key, p->instance->name))
36366 +                       *hl++ = p;
36367 +       }
36368 +       *hl = NULL;
36369 +
36370 +out:
36371 +       return err;
36372 +}
36373 +
36374 +static void au_wkq_lockdep_free(struct au_wkinfo *wkinfo)
36375 +{
36376 +       au_kfree_try_rcu(wkinfo->hlock);
36377 +}
36378 +
36379 +static void au_wkq_lockdep_pre(struct au_wkinfo *wkinfo)
36380 +{
36381 +       struct held_lock *p, **hl = wkinfo->hlock;
36382 +       int subclass;
36383 +
36384 +       if (wkinfo->dont_check)
36385 +               lockdep_off();
36386 +       if (!hl)
36387 +               return;
36388 +       while ((p = *hl++)) { /* assignment */
36389 +               subclass = lockdep_hlock_class(p)->subclass;
36390 +               /* AuDbg("%s, %d\n", p->instance->name, subclass); */
36391 +               if (p->read)
36392 +                       rwsem_acquire_read(p->instance, subclass, 0,
36393 +                                          /*p->acquire_ip*/_RET_IP_);
36394 +               else
36395 +                       rwsem_acquire(p->instance, subclass, 0,
36396 +                                     /*p->acquire_ip*/_RET_IP_);
36397 +       }
36398 +}
36399 +
36400 +static void au_wkq_lockdep_post(struct au_wkinfo *wkinfo)
36401 +{
36402 +       struct held_lock *p, **hl = wkinfo->hlock;
36403 +
36404 +       if (wkinfo->dont_check)
36405 +               lockdep_on();
36406 +       if (!hl)
36407 +               return;
36408 +       while ((p = *hl++)) /* assignment */
36409 +               rwsem_release(p->instance, /*p->acquire_ip*/_RET_IP_);
36410 +}
36411 +#endif
36412 +
36413 +static void wkq_func(struct work_struct *wk)
36414 +{
36415 +       struct au_wkinfo *wkinfo = container_of(wk, struct au_wkinfo, wk);
36416 +
36417 +       AuDebugOn(!uid_eq(current_fsuid(), GLOBAL_ROOT_UID));
36418 +       AuDebugOn(rlimit(RLIMIT_FSIZE) != RLIM_INFINITY);
36419 +
36420 +       au_wkq_lockdep_pre(wkinfo);
36421 +       wkinfo->func(wkinfo->args);
36422 +       au_wkq_lockdep_post(wkinfo);
36423 +       if (au_ftest_wkq(wkinfo->flags, WAIT))
36424 +               complete(wkinfo->comp);
36425 +       else {
36426 +               kobject_put(wkinfo->kobj);
36427 +               module_put(THIS_MODULE); /* todo: ?? */
36428 +               au_kfree_rcu(wkinfo);
36429 +       }
36430 +}
36431 +
36432 +/*
36433 + * Since struct completion is large, try allocating it dynamically.
36434 + */
36435 +#define AuWkqCompDeclare(name) struct completion *comp = NULL
36436 +
36437 +static int au_wkq_comp_alloc(struct au_wkinfo *wkinfo, struct completion **comp)
36438 +{
36439 +       *comp = kmalloc(sizeof(**comp), GFP_NOFS);
36440 +       if (*comp) {
36441 +               init_completion(*comp);
36442 +               wkinfo->comp = *comp;
36443 +               return 0;
36444 +       }
36445 +       return -ENOMEM;
36446 +}
36447 +
36448 +static void au_wkq_comp_free(struct completion *comp)
36449 +{
36450 +       au_kfree_rcu(comp);
36451 +}
36452 +
36453 +static void au_wkq_run(struct au_wkinfo *wkinfo)
36454 +{
36455 +       if (au_ftest_wkq(wkinfo->flags, NEST)) {
36456 +               if (au_wkq_test()) {
36457 +                       AuWarn1("wkq from wkq, unless silly-rename on NFS,"
36458 +                               " due to a dead dir by UDBA,"
36459 +                               " or async xino write?\n");
36460 +                       AuDebugOn(au_ftest_wkq(wkinfo->flags, WAIT));
36461 +               }
36462 +       } else
36463 +               au_dbg_verify_kthread();
36464 +
36465 +       if (au_ftest_wkq(wkinfo->flags, WAIT)) {
36466 +               INIT_WORK_ONSTACK(&wkinfo->wk, wkq_func);
36467 +               queue_work(au_wkq, &wkinfo->wk);
36468 +       } else {
36469 +               INIT_WORK(&wkinfo->wk, wkq_func);
36470 +               schedule_work(&wkinfo->wk);
36471 +       }
36472 +}
36473 +
36474 +/*
36475 + * Be careful. It is easy to make deadlock happen.
36476 + * processA: lock, wkq and wait
36477 + * processB: wkq and wait, lock in wkq
36478 + * --> deadlock
36479 + */
36480 +int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args)
36481 +{
36482 +       int err;
36483 +       AuWkqCompDeclare(comp);
36484 +       struct au_wkinfo wkinfo = {
36485 +               .flags  = flags,
36486 +               .func   = func,
36487 +               .args   = args
36488 +       };
36489 +
36490 +       err = au_wkq_comp_alloc(&wkinfo, &comp);
36491 +       if (unlikely(err))
36492 +               goto out;
36493 +       err = au_wkq_lockdep_alloc(&wkinfo);
36494 +       if (unlikely(err))
36495 +               goto out_comp;
36496 +       if (!err) {
36497 +               au_wkq_run(&wkinfo);
36498 +               /* no timeout, no interrupt */
36499 +               wait_for_completion(wkinfo.comp);
36500 +       }
36501 +       au_wkq_lockdep_free(&wkinfo);
36502 +
36503 +out_comp:
36504 +       au_wkq_comp_free(comp);
36505 +out:
36506 +       destroy_work_on_stack(&wkinfo.wk);
36507 +       return err;
36508 +}
36509 +
36510 +/*
36511 + * Note: dget/dput() in func for aufs dentries are not supported. It will be a
36512 + * problem in a concurrent umounting.
36513 + */
36514 +int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
36515 +                 unsigned int flags)
36516 +{
36517 +       int err;
36518 +       struct au_wkinfo *wkinfo;
36519 +
36520 +       atomic_inc(&au_sbi(sb)->si_nowait.nw_len);
36521 +
36522 +       /*
36523 +        * wkq_func() must free this wkinfo.
36524 +        * it highly depends upon the implementation of workqueue.
36525 +        */
36526 +       err = 0;
36527 +       wkinfo = kmalloc(sizeof(*wkinfo), GFP_NOFS);
36528 +       if (wkinfo) {
36529 +               wkinfo->kobj = &au_sbi(sb)->si_kobj;
36530 +               wkinfo->flags = flags & ~AuWkq_WAIT;
36531 +               wkinfo->func = func;
36532 +               wkinfo->args = args;
36533 +               wkinfo->comp = NULL;
36534 +               au_wkq_lockdep_init(wkinfo);
36535 +               kobject_get(wkinfo->kobj);
36536 +               __module_get(THIS_MODULE); /* todo: ?? */
36537 +
36538 +               au_wkq_run(wkinfo);
36539 +       } else {
36540 +               err = -ENOMEM;
36541 +               au_nwt_done(&au_sbi(sb)->si_nowait);
36542 +       }
36543 +
36544 +       return err;
36545 +}
36546 +
36547 +/* ---------------------------------------------------------------------- */
36548 +
36549 +void au_nwt_init(struct au_nowait_tasks *nwt)
36550 +{
36551 +       atomic_set(&nwt->nw_len, 0);
36552 +       /* smp_mb(); */ /* atomic_set */
36553 +       init_waitqueue_head(&nwt->nw_wq);
36554 +}
36555 +
36556 +void au_wkq_fin(void)
36557 +{
36558 +       destroy_workqueue(au_wkq);
36559 +}
36560 +
36561 +int __init au_wkq_init(void)
36562 +{
36563 +       int err;
36564 +
36565 +       err = 0;
36566 +       au_wkq = alloc_workqueue(AUFS_WKQ_NAME, 0, WQ_DFL_ACTIVE);
36567 +       if (IS_ERR(au_wkq))
36568 +               err = PTR_ERR(au_wkq);
36569 +       else if (!au_wkq)
36570 +               err = -ENOMEM;
36571 +
36572 +       return err;
36573 +}
36574 diff -urN /usr/share/empty/fs/aufs/wkq.h linux/fs/aufs/wkq.h
36575 --- /usr/share/empty/fs/aufs/wkq.h      1970-01-01 01:00:00.000000000 +0100
36576 +++ linux/fs/aufs/wkq.h 2022-03-21 14:49:05.733299677 +0100
36577 @@ -0,0 +1,89 @@
36578 +/* SPDX-License-Identifier: GPL-2.0 */
36579 +/*
36580 + * Copyright (C) 2005-2021 Junjiro R. Okajima
36581 + *
36582 + * This program, aufs is free software; you can redistribute it and/or modify
36583 + * it under the terms of the GNU General Public License as published by
36584 + * the Free Software Foundation; either version 2 of the License, or
36585 + * (at your option) any later version.
36586 + *
36587 + * This program is distributed in the hope that it will be useful,
36588 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
36589 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36590 + * GNU General Public License for more details.
36591 + *
36592 + * You should have received a copy of the GNU General Public License
36593 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
36594 + */
36595 +
36596 +/*
36597 + * workqueue for asynchronous/super-io operations
36598 + * todo: try new credentials management scheme
36599 + */
36600 +
36601 +#ifndef __AUFS_WKQ_H__
36602 +#define __AUFS_WKQ_H__
36603 +
36604 +#ifdef __KERNEL__
36605 +
36606 +#include <linux/wait.h>
36607 +
36608 +struct super_block;
36609 +
36610 +/* ---------------------------------------------------------------------- */
36611 +
36612 +/*
36613 + * in the next operation, wait for the 'nowait' tasks in system-wide workqueue
36614 + */
36615 +struct au_nowait_tasks {
36616 +       atomic_t                nw_len;
36617 +       wait_queue_head_t       nw_wq;
36618 +};
36619 +
36620 +/* ---------------------------------------------------------------------- */
36621 +
36622 +typedef void (*au_wkq_func_t)(void *args);
36623 +
36624 +/* wkq flags */
36625 +#define AuWkq_WAIT     1
36626 +#define AuWkq_NEST     (1 << 1)
36627 +#define au_ftest_wkq(flags, name)      ((flags) & AuWkq_##name)
36628 +#define au_fset_wkq(flags, name) \
36629 +       do { (flags) |= AuWkq_##name; } while (0)
36630 +#define au_fclr_wkq(flags, name) \
36631 +       do { (flags) &= ~AuWkq_##name; } while (0)
36632 +
36633 +/* wkq.c */
36634 +int au_wkq_do_wait(unsigned int flags, au_wkq_func_t func, void *args);
36635 +int au_wkq_nowait(au_wkq_func_t func, void *args, struct super_block *sb,
36636 +                 unsigned int flags);
36637 +void au_nwt_init(struct au_nowait_tasks *nwt);
36638 +int __init au_wkq_init(void);
36639 +void au_wkq_fin(void);
36640 +
36641 +/* ---------------------------------------------------------------------- */
36642 +
36643 +static inline int au_wkq_test(void)
36644 +{
36645 +       return current->flags & PF_WQ_WORKER;
36646 +}
36647 +
36648 +static inline int au_wkq_wait(au_wkq_func_t func, void *args)
36649 +{
36650 +       return au_wkq_do_wait(AuWkq_WAIT, func, args);
36651 +}
36652 +
36653 +static inline void au_nwt_done(struct au_nowait_tasks *nwt)
36654 +{
36655 +       if (atomic_dec_and_test(&nwt->nw_len))
36656 +               wake_up_all(&nwt->nw_wq);
36657 +}
36658 +
36659 +static inline int au_nwt_flush(struct au_nowait_tasks *nwt)
36660 +{
36661 +       wait_event(nwt->nw_wq, !atomic_read(&nwt->nw_len));
36662 +       return 0;
36663 +}
36664 +
36665 +#endif /* __KERNEL__ */
36666 +#endif /* __AUFS_WKQ_H__ */
36667 diff -urN /usr/share/empty/fs/aufs/xattr.c linux/fs/aufs/xattr.c
36668 --- /usr/share/empty/fs/aufs/xattr.c    1970-01-01 01:00:00.000000000 +0100
36669 +++ linux/fs/aufs/xattr.c       2022-03-21 14:49:05.733299677 +0100
36670 @@ -0,0 +1,368 @@
36671 +// SPDX-License-Identifier: GPL-2.0
36672 +/*
36673 + * Copyright (C) 2014-2021 Junjiro R. Okajima
36674 + *
36675 + * This program, aufs is free software; you can redistribute it and/or modify
36676 + * it under the terms of the GNU General Public License as published by
36677 + * the Free Software Foundation; either version 2 of the License, or
36678 + * (at your option) any later version.
36679 + *
36680 + * This program is distributed in the hope that it will be useful,
36681 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
36682 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36683 + * GNU General Public License for more details.
36684 + *
36685 + * You should have received a copy of the GNU General Public License
36686 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
36687 + */
36688 +
36689 +/*
36690 + * handling xattr functions
36691 + */
36692 +
36693 +#include <linux/fs.h>
36694 +#include <linux/posix_acl_xattr.h>
36695 +#include <linux/xattr.h>
36696 +#include "aufs.h"
36697 +
36698 +static int au_xattr_ignore(int err, char *name, unsigned int ignore_flags)
36699 +{
36700 +       if (!ignore_flags)
36701 +               goto out;
36702 +       switch (err) {
36703 +       case -ENOMEM:
36704 +       case -EDQUOT:
36705 +               goto out;
36706 +       }
36707 +
36708 +       if ((ignore_flags & AuBrAttr_ICEX) == AuBrAttr_ICEX) {
36709 +               err = 0;
36710 +               goto out;
36711 +       }
36712 +
36713 +#define cmp(brattr, prefix) do {                                       \
36714 +               if (!strncmp(name, XATTR_##prefix##_PREFIX,             \
36715 +                            XATTR_##prefix##_PREFIX_LEN)) {            \
36716 +                       if (ignore_flags & AuBrAttr_ICEX_##brattr)      \
36717 +                               err = 0;                                \
36718 +                       goto out;                                       \
36719 +               }                                                       \
36720 +       } while (0)
36721 +
36722 +       cmp(SEC, SECURITY);
36723 +       cmp(SYS, SYSTEM);
36724 +       cmp(TR, TRUSTED);
36725 +       cmp(USR, USER);
36726 +#undef cmp
36727 +
36728 +       if (ignore_flags & AuBrAttr_ICEX_OTH)
36729 +               err = 0;
36730 +
36731 +out:
36732 +       return err;
36733 +}
36734 +
36735 +static const int au_xattr_out_of_list = AuBrAttr_ICEX_OTH << 1;
36736 +
36737 +static int au_do_cpup_xattr(struct path *h_dst, struct path *h_src,
36738 +                           char *name, char **buf, unsigned int ignore_flags,
36739 +                           unsigned int verbose)
36740 +{
36741 +       int err;
36742 +       ssize_t ssz;
36743 +       struct inode *h_idst;
36744 +       struct dentry *h_dst_dentry, *h_src_dentry;
36745 +       struct user_namespace *h_dst_userns, *h_src_userns;
36746 +
36747 +       h_src_userns = mnt_user_ns(h_src->mnt);
36748 +       h_src_dentry = h_src->dentry;
36749 +       ssz = vfs_getxattr_alloc(h_src_userns, h_src_dentry, name, buf, 0,
36750 +                                GFP_NOFS);
36751 +       err = ssz;
36752 +       if (unlikely(err <= 0)) {
36753 +               if (err == -ENODATA
36754 +                   || (err == -EOPNOTSUPP
36755 +                       && ((ignore_flags & au_xattr_out_of_list)
36756 +                           || (au_test_nfs_noacl(d_inode(h_src_dentry))
36757 +                               && (!strcmp(name, XATTR_NAME_POSIX_ACL_ACCESS)
36758 +                                   || !strcmp(name,
36759 +                                              XATTR_NAME_POSIX_ACL_DEFAULT))))
36760 +                           ))
36761 +                       err = 0;
36762 +               if (err && (verbose || au_debug_test()))
36763 +                       pr_err("%s, err %d\n", name, err);
36764 +               goto out;
36765 +       }
36766 +
36767 +       /* unlock it temporary */
36768 +       h_dst_userns = mnt_user_ns(h_dst->mnt);
36769 +       h_dst_dentry = h_dst->dentry;
36770 +       h_idst = d_inode(h_dst_dentry);
36771 +       inode_unlock(h_idst);
36772 +       err = vfsub_setxattr(h_dst_userns, h_dst_dentry, name, *buf, ssz,
36773 +                            /*flags*/0);
36774 +       inode_lock_nested(h_idst, AuLsc_I_CHILD2);
36775 +       if (unlikely(err)) {
36776 +               if (verbose || au_debug_test())
36777 +                       pr_err("%s, err %d\n", name, err);
36778 +               err = au_xattr_ignore(err, name, ignore_flags);
36779 +       }
36780 +
36781 +out:
36782 +       return err;
36783 +}
36784 +
36785 +int au_cpup_xattr(struct path *h_dst, struct path *h_src, int ignore_flags,
36786 +                 unsigned int verbose)
36787 +{
36788 +       int err, unlocked, acl_access, acl_default;
36789 +       ssize_t ssz;
36790 +       struct dentry *h_dst_dentry, *h_src_dentry;
36791 +       struct inode *h_isrc, *h_idst;
36792 +       char *value, *p, *o, *e;
36793 +
36794 +       /* try stopping to update the source inode while we are referencing */
36795 +       /* there should not be the parent-child relationship between them */
36796 +       h_dst_dentry = h_dst->dentry;
36797 +       h_idst = d_inode(h_dst_dentry);
36798 +       h_src_dentry = h_src->dentry;
36799 +       h_isrc = d_inode(h_src_dentry);
36800 +       inode_unlock(h_idst);
36801 +       inode_lock_shared_nested(h_isrc, AuLsc_I_CHILD);
36802 +       inode_lock_nested(h_idst, AuLsc_I_CHILD2);
36803 +       unlocked = 0;
36804 +
36805 +       /* some filesystems don't list POSIX ACL, for example tmpfs */
36806 +       ssz = vfs_listxattr(h_src_dentry, NULL, 0);
36807 +       err = ssz;
36808 +       if (unlikely(err < 0)) {
36809 +               AuTraceErr(err);
36810 +               if (err == -ENODATA
36811 +                   || err == -EOPNOTSUPP)
36812 +                       err = 0;        /* ignore */
36813 +               goto out;
36814 +       }
36815 +
36816 +       err = 0;
36817 +       p = NULL;
36818 +       o = NULL;
36819 +       if (ssz) {
36820 +               err = -ENOMEM;
36821 +               p = kmalloc(ssz, GFP_NOFS);
36822 +               o = p;
36823 +               if (unlikely(!p))
36824 +                       goto out;
36825 +               err = vfs_listxattr(h_src_dentry, p, ssz);
36826 +       }
36827 +       inode_unlock_shared(h_isrc);
36828 +       unlocked = 1;
36829 +       AuDbg("err %d, ssz %zd\n", err, ssz);
36830 +       if (unlikely(err < 0))
36831 +               goto out_free;
36832 +
36833 +       err = 0;
36834 +       e = p + ssz;
36835 +       value = NULL;
36836 +       acl_access = 0;
36837 +       acl_default = 0;
36838 +       while (!err && p < e) {
36839 +               acl_access |= !strncmp(p, XATTR_NAME_POSIX_ACL_ACCESS,
36840 +                                      sizeof(XATTR_NAME_POSIX_ACL_ACCESS) - 1);
36841 +               acl_default |= !strncmp(p, XATTR_NAME_POSIX_ACL_DEFAULT,
36842 +                                       sizeof(XATTR_NAME_POSIX_ACL_DEFAULT)
36843 +                                       - 1);
36844 +               err = au_do_cpup_xattr(h_dst, h_src, p, &value, ignore_flags,
36845 +                                      verbose);
36846 +               p += strlen(p) + 1;
36847 +       }
36848 +       AuTraceErr(err);
36849 +       ignore_flags |= au_xattr_out_of_list;
36850 +       if (!err && !acl_access) {
36851 +               err = au_do_cpup_xattr(h_dst, h_src,
36852 +                                      XATTR_NAME_POSIX_ACL_ACCESS, &value,
36853 +                                      ignore_flags, verbose);
36854 +               AuTraceErr(err);
36855 +       }
36856 +       if (!err && !acl_default) {
36857 +               err = au_do_cpup_xattr(h_dst, h_src,
36858 +                                      XATTR_NAME_POSIX_ACL_DEFAULT, &value,
36859 +                                      ignore_flags, verbose);
36860 +               AuTraceErr(err);
36861 +       }
36862 +
36863 +       au_kfree_try_rcu(value);
36864 +
36865 +out_free:
36866 +       au_kfree_try_rcu(o);
36867 +out:
36868 +       if (!unlocked)
36869 +               inode_unlock_shared(h_isrc);
36870 +       AuTraceErr(err);
36871 +       return err;
36872 +}
36873 +
36874 +/* ---------------------------------------------------------------------- */
36875 +
36876 +static int au_smack_reentering(struct super_block *sb)
36877 +{
36878 +#if IS_ENABLED(CONFIG_SECURITY_SMACK) || IS_ENABLED(CONFIG_SECURITY_SELINUX)
36879 +       /*
36880 +        * as a part of lookup, smack_d_instantiate() is called, and it calls
36881 +        * i_op->getxattr(). ouch.
36882 +        */
36883 +       return si_pid_test(sb);
36884 +#else
36885 +       return 0;
36886 +#endif
36887 +}
36888 +
36889 +enum {
36890 +       AU_XATTR_LIST,
36891 +       AU_XATTR_GET
36892 +};
36893 +
36894 +struct au_lgxattr {
36895 +       int type;
36896 +       union {
36897 +               struct {
36898 +                       char    *list;
36899 +                       size_t  size;
36900 +               } list;
36901 +               struct {
36902 +                       const char      *name;
36903 +                       void            *value;
36904 +                       size_t          size;
36905 +               } get;
36906 +       } u;
36907 +};
36908 +
36909 +static ssize_t au_lgxattr(struct dentry *dentry, struct inode *inode,
36910 +                         struct au_lgxattr *arg)
36911 +{
36912 +       ssize_t err;
36913 +       int reenter;
36914 +       struct path h_path;
36915 +       struct super_block *sb;
36916 +
36917 +       sb = dentry->d_sb;
36918 +       reenter = au_smack_reentering(sb);
36919 +       if (!reenter) {
36920 +               err = si_read_lock(sb, AuLock_FLUSH | AuLock_NOPLM);
36921 +               if (unlikely(err))
36922 +                       goto out;
36923 +       }
36924 +       err = au_h_path_getattr(dentry, inode, /*force*/1, &h_path, reenter);
36925 +       if (unlikely(err))
36926 +               goto out_si;
36927 +       if (unlikely(!h_path.dentry))
36928 +               /* illegally overlapped or something */
36929 +               goto out_di; /* pretending success */
36930 +
36931 +       /* always topmost entry only */
36932 +       switch (arg->type) {
36933 +       case AU_XATTR_LIST:
36934 +               err = vfs_listxattr(h_path.dentry,
36935 +                                   arg->u.list.list, arg->u.list.size);
36936 +               break;
36937 +       case AU_XATTR_GET:
36938 +               AuDebugOn(d_is_negative(h_path.dentry));
36939 +               err = vfs_getxattr(mnt_user_ns(h_path.mnt), h_path.dentry,
36940 +                                  arg->u.get.name, arg->u.get.value,
36941 +                                  arg->u.get.size);
36942 +               break;
36943 +       }
36944 +
36945 +out_di:
36946 +       if (!reenter)
36947 +               di_read_unlock(dentry, AuLock_IR);
36948 +out_si:
36949 +       if (!reenter)
36950 +               si_read_unlock(sb);
36951 +out:
36952 +       AuTraceErr(err);
36953 +       return err;
36954 +}
36955 +
36956 +ssize_t aufs_listxattr(struct dentry *dentry, char *list, size_t size)
36957 +{
36958 +       struct au_lgxattr arg = {
36959 +               .type = AU_XATTR_LIST,
36960 +               .u.list = {
36961 +                       .list   = list,
36962 +                       .size   = size
36963 +               },
36964 +       };
36965 +
36966 +       return au_lgxattr(dentry, /*inode*/NULL, &arg);
36967 +}
36968 +
36969 +static ssize_t au_getxattr(struct dentry *dentry, struct inode *inode,
36970 +                          const char *name, void *value, size_t size)
36971 +{
36972 +       struct au_lgxattr arg = {
36973 +               .type = AU_XATTR_GET,
36974 +               .u.get = {
36975 +                       .name   = name,
36976 +                       .value  = value,
36977 +                       .size   = size
36978 +               },
36979 +       };
36980 +
36981 +       return au_lgxattr(dentry, inode, &arg);
36982 +}
36983 +
36984 +static int au_setxattr(struct dentry *dentry, struct inode *inode,
36985 +                      const char *name, const void *value, size_t size,
36986 +                      int flags)
36987 +{
36988 +       struct au_sxattr arg = {
36989 +               .type = AU_XATTR_SET,
36990 +               .u.set = {
36991 +                       .name   = name,
36992 +                       .value  = value,
36993 +                       .size   = size,
36994 +                       .flags  = flags
36995 +               },
36996 +       };
36997 +
36998 +       return au_sxattr(dentry, inode, &arg);
36999 +}
37000 +
37001 +/* ---------------------------------------------------------------------- */
37002 +
37003 +static int au_xattr_get(const struct xattr_handler *handler,
37004 +                       struct dentry *dentry, struct inode *inode,
37005 +                       const char *name, void *buffer, size_t size)
37006 +{
37007 +       return au_getxattr(dentry, inode, name, buffer, size);
37008 +}
37009 +
37010 +static int au_xattr_set(const struct xattr_handler *handler,
37011 +                       struct user_namespace *userns,
37012 +                       struct dentry *dentry, struct inode *inode,
37013 +                       const char *name, const void *value, size_t size,
37014 +                       int flags)
37015 +{
37016 +       return au_setxattr(dentry, inode, name, value, size, flags);
37017 +}
37018 +
37019 +static const struct xattr_handler au_xattr_handler = {
37020 +       .name   = "",
37021 +       .prefix = "",
37022 +       .get    = au_xattr_get,
37023 +       .set    = au_xattr_set
37024 +};
37025 +
37026 +static const struct xattr_handler *au_xattr_handlers[] = {
37027 +#ifdef CONFIG_FS_POSIX_ACL
37028 +       &posix_acl_access_xattr_handler,
37029 +       &posix_acl_default_xattr_handler,
37030 +#endif
37031 +       &au_xattr_handler, /* must be last */
37032 +       NULL
37033 +};
37034 +
37035 +void au_xattr_init(struct super_block *sb)
37036 +{
37037 +       sb->s_xattr = au_xattr_handlers;
37038 +}
37039 diff -urN /usr/share/empty/fs/aufs/xino.c linux/fs/aufs/xino.c
37040 --- /usr/share/empty/fs/aufs/xino.c     1970-01-01 01:00:00.000000000 +0100
37041 +++ linux/fs/aufs/xino.c        2022-03-21 14:49:05.733299677 +0100
37042 @@ -0,0 +1,1926 @@
37043 +// SPDX-License-Identifier: GPL-2.0
37044 +/*
37045 + * Copyright (C) 2005-2021 Junjiro R. Okajima
37046 + *
37047 + * This program, aufs is free software; you can redistribute it and/or modify
37048 + * it under the terms of the GNU General Public License as published by
37049 + * the Free Software Foundation; either version 2 of the License, or
37050 + * (at your option) any later version.
37051 + *
37052 + * This program is distributed in the hope that it will be useful,
37053 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
37054 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
37055 + * GNU General Public License for more details.
37056 + *
37057 + * You should have received a copy of the GNU General Public License
37058 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
37059 + */
37060 +
37061 +/*
37062 + * external inode number translation table and bitmap
37063 + *
37064 + * things to consider
37065 + * - the lifetime
37066 + *   + au_xino object
37067 + *   + XINO files (xino, xib, xigen)
37068 + *   + dynamic debugfs entries (xiN)
37069 + *   + static debugfs entries (xib, xigen)
37070 + *   + static sysfs entry (xi_path)
37071 + * - several entry points to handle them.
37072 + *   + mount(2) without xino option (default)
37073 + *   + mount(2) with xino option
37074 + *   + mount(2) with noxino option
37075 + *   + umount(2)
37076 + *   + remount with add/del branches
37077 + *   + remount with xino/noxino options
37078 + */
37079 +
37080 +#include <linux/seq_file.h>
37081 +#include <linux/statfs.h>
37082 +#include "aufs.h"
37083 +
37084 +static aufs_bindex_t sbr_find_shared(struct super_block *sb, aufs_bindex_t btop,
37085 +                                    aufs_bindex_t bbot,
37086 +                                    struct super_block *h_sb)
37087 +{
37088 +       /* todo: try binary-search if the branches are many */
37089 +       for (; btop <= bbot; btop++)
37090 +               if (h_sb == au_sbr_sb(sb, btop))
37091 +                       return btop;
37092 +       return -1;
37093 +}
37094 +
37095 +/*
37096 + * find another branch who is on the same filesystem of the specified
37097 + * branch{@btgt}. search until @bbot.
37098 + */
37099 +static aufs_bindex_t is_sb_shared(struct super_block *sb, aufs_bindex_t btgt,
37100 +                                 aufs_bindex_t bbot)
37101 +{
37102 +       aufs_bindex_t bindex;
37103 +       struct super_block *tgt_sb;
37104 +
37105 +       tgt_sb = au_sbr_sb(sb, btgt);
37106 +       bindex = sbr_find_shared(sb, /*btop*/0, btgt - 1, tgt_sb);
37107 +       if (bindex < 0)
37108 +               bindex = sbr_find_shared(sb, btgt + 1, bbot, tgt_sb);
37109 +
37110 +       return bindex;
37111 +}
37112 +
37113 +/* ---------------------------------------------------------------------- */
37114 +
37115 +/*
37116 + * stop unnecessary notify events at creating xino files
37117 + */
37118 +
37119 +aufs_bindex_t au_xi_root(struct super_block *sb, struct dentry *dentry)
37120 +{
37121 +       aufs_bindex_t bfound, bindex, bbot;
37122 +       struct dentry *parent;
37123 +       struct au_branch *br;
37124 +
37125 +       bfound = -1;
37126 +       parent = dentry->d_parent; /* safe d_parent access */
37127 +       bbot = au_sbbot(sb);
37128 +       for (bindex = 0; bindex <= bbot; bindex++) {
37129 +               br = au_sbr(sb, bindex);
37130 +               if (au_br_dentry(br) == parent) {
37131 +                       bfound = bindex;
37132 +                       break;
37133 +               }
37134 +       }
37135 +
37136 +       AuDbg("bfound b%d\n", bfound);
37137 +       return bfound;
37138 +}
37139 +
37140 +struct au_xino_lock_dir {
37141 +       struct au_hinode *hdir;
37142 +       struct dentry *parent;
37143 +       struct inode *dir;
37144 +};
37145 +
37146 +static struct dentry *au_dget_parent_lock(struct dentry *dentry,
37147 +                                         unsigned int lsc)
37148 +{
37149 +       struct dentry *parent;
37150 +       struct inode *dir;
37151 +
37152 +       parent = dget_parent(dentry);
37153 +       dir = d_inode(parent);
37154 +       inode_lock_nested(dir, lsc);
37155 +#if 0 /* it should not happen */
37156 +       spin_lock(&dentry->d_lock);
37157 +       if (unlikely(dentry->d_parent != parent)) {
37158 +               spin_unlock(&dentry->d_lock);
37159 +               inode_unlock(dir);
37160 +               dput(parent);
37161 +               parent = NULL;
37162 +               goto out;
37163 +       }
37164 +       spin_unlock(&dentry->d_lock);
37165 +
37166 +out:
37167 +#endif
37168 +       return parent;
37169 +}
37170 +
37171 +static void au_xino_lock_dir(struct super_block *sb, struct path *xipath,
37172 +                            struct au_xino_lock_dir *ldir)
37173 +{
37174 +       aufs_bindex_t bindex;
37175 +
37176 +       ldir->hdir = NULL;
37177 +       bindex = au_xi_root(sb, xipath->dentry);
37178 +       if (bindex >= 0) {
37179 +               /* rw branch root */
37180 +               ldir->hdir = au_hi(d_inode(sb->s_root), bindex);
37181 +               au_hn_inode_lock_nested(ldir->hdir, AuLsc_I_PARENT);
37182 +       } else {
37183 +               /* other */
37184 +               ldir->parent = au_dget_parent_lock(xipath->dentry,
37185 +                                                  AuLsc_I_PARENT);
37186 +               ldir->dir = d_inode(ldir->parent);
37187 +       }
37188 +}
37189 +
37190 +static void au_xino_unlock_dir(struct au_xino_lock_dir *ldir)
37191 +{
37192 +       if (ldir->hdir)
37193 +               au_hn_inode_unlock(ldir->hdir);
37194 +       else {
37195 +               inode_unlock(ldir->dir);
37196 +               dput(ldir->parent);
37197 +       }
37198 +}
37199 +
37200 +/* ---------------------------------------------------------------------- */
37201 +
37202 +/*
37203 + * create and set a new xino file
37204 + */
37205 +struct file *au_xino_create(struct super_block *sb, char *fpath, int silent,
37206 +                           int wbrtop)
37207 +{
37208 +       struct file *file;
37209 +       struct dentry *h_parent, *d;
37210 +       struct inode *h_dir, *inode;
37211 +       int err;
37212 +       static DEFINE_MUTEX(mtx);
37213 +
37214 +       /*
37215 +        * at mount-time, and the xino file is the default path,
37216 +        * hnotify is disabled so we have no notify events to ignore.
37217 +        * when a user specified the xino, we cannot get au_hdir to be ignored.
37218 +        */
37219 +       if (!wbrtop)
37220 +               mutex_lock(&mtx);
37221 +       file = vfsub_filp_open(fpath, O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
37222 +                              /* | __FMODE_NONOTIFY */,
37223 +                              0666);
37224 +       if (IS_ERR(file)) {
37225 +               if (!wbrtop)
37226 +                       mutex_unlock(&mtx);
37227 +               if (!silent)
37228 +                       pr_err("open %s(%ld)\n", fpath, PTR_ERR(file));
37229 +               return file;
37230 +       }
37231 +
37232 +       /* keep file count */
37233 +       err = 0;
37234 +       d = file->f_path.dentry;
37235 +       h_parent = au_dget_parent_lock(d, AuLsc_I_PARENT);
37236 +       if (!wbrtop)
37237 +               mutex_unlock(&mtx);
37238 +       /* mnt_want_write() is unnecessary here */
37239 +       h_dir = d_inode(h_parent);
37240 +       inode = file_inode(file);
37241 +       /* no delegation since it is just created */
37242 +       if (inode->i_nlink)
37243 +               err = vfsub_unlink(h_dir, &file->f_path, /*delegated*/NULL,
37244 +                                  /*force*/0);
37245 +       inode_unlock(h_dir);
37246 +       dput(h_parent);
37247 +       if (unlikely(err)) {
37248 +               if (!silent)
37249 +                       pr_err("unlink %s(%d)\n", fpath, err);
37250 +               goto out;
37251 +       }
37252 +
37253 +       err = -EINVAL;
37254 +       if (unlikely(sb && sb == d->d_sb)) {
37255 +               if (!silent)
37256 +                       pr_err("%s must be outside\n", fpath);
37257 +               goto out;
37258 +       }
37259 +       if (unlikely(au_test_fs_bad_xino(d->d_sb))) {
37260 +               if (!silent)
37261 +                       pr_err("xino doesn't support %s(%s)\n",
37262 +                              fpath, au_sbtype(d->d_sb));
37263 +               goto out;
37264 +       }
37265 +       return file; /* success */
37266 +
37267 +out:
37268 +       fput(file);
37269 +       file = ERR_PTR(err);
37270 +       return file;
37271 +}
37272 +
37273 +/*
37274 + * create a new xinofile at the same place/path as @base.
37275 + */
37276 +struct file *au_xino_create2(struct super_block *sb, struct path *base,
37277 +                            struct file *copy_src)
37278 +{
37279 +       struct file *file;
37280 +       struct dentry *dentry;
37281 +       struct inode *dir, *delegated;
37282 +       struct qstr *name;
37283 +       struct path ppath, path;
37284 +       int err, do_unlock;
37285 +       struct au_xino_lock_dir ldir;
37286 +
37287 +       do_unlock = 1;
37288 +       au_xino_lock_dir(sb, base, &ldir);
37289 +       dentry = base->dentry;
37290 +       ppath.dentry = dentry->d_parent; /* dir inode is locked */
37291 +       ppath.mnt = base->mnt;
37292 +       dir = d_inode(ppath.dentry);
37293 +       IMustLock(dir);
37294 +
37295 +       name = &dentry->d_name;
37296 +       path.dentry = vfsub_lookup_one_len(name->name, &ppath, name->len);
37297 +       if (IS_ERR(path.dentry)) {
37298 +               file = (void *)path.dentry;
37299 +               pr_err("%pd lookup err %ld\n", dentry, PTR_ERR(path.dentry));
37300 +               goto out;
37301 +       }
37302 +
37303 +       /* no need to mnt_want_write() since we call dentry_open() later */
37304 +       err = vfs_create(mnt_user_ns(base->mnt), dir, path.dentry, 0666, NULL);
37305 +       if (unlikely(err)) {
37306 +               file = ERR_PTR(err);
37307 +               pr_err("%pd create err %d\n", dentry, err);
37308 +               goto out_dput;
37309 +       }
37310 +
37311 +       path.mnt = base->mnt;
37312 +       file = vfsub_dentry_open(&path,
37313 +                                O_RDWR | O_CREAT | O_EXCL | O_LARGEFILE
37314 +                                /* | __FMODE_NONOTIFY */);
37315 +       if (IS_ERR(file)) {
37316 +               pr_err("%pd open err %ld\n", dentry, PTR_ERR(file));
37317 +               goto out_dput;
37318 +       }
37319 +
37320 +       delegated = NULL;
37321 +       err = vfsub_unlink(dir, &file->f_path, &delegated, /*force*/0);
37322 +       au_xino_unlock_dir(&ldir);
37323 +       do_unlock = 0;
37324 +       if (unlikely(err == -EWOULDBLOCK)) {
37325 +               pr_warn("cannot retry for NFSv4 delegation"
37326 +                       " for an internal unlink\n");
37327 +               iput(delegated);
37328 +       }
37329 +       if (unlikely(err)) {
37330 +               pr_err("%pd unlink err %d\n", dentry, err);
37331 +               goto out_fput;
37332 +       }
37333 +
37334 +       if (copy_src) {
37335 +               /* no one can touch copy_src xino */
37336 +               err = au_copy_file(file, copy_src, vfsub_f_size_read(copy_src));
37337 +               if (unlikely(err)) {
37338 +                       pr_err("%pd copy err %d\n", dentry, err);
37339 +                       goto out_fput;
37340 +               }
37341 +       }
37342 +       goto out_dput; /* success */
37343 +
37344 +out_fput:
37345 +       fput(file);
37346 +       file = ERR_PTR(err);
37347 +out_dput:
37348 +       dput(path.dentry);
37349 +out:
37350 +       if (do_unlock)
37351 +               au_xino_unlock_dir(&ldir);
37352 +       return file;
37353 +}
37354 +
37355 +struct file *au_xino_file1(struct au_xino *xi)
37356 +{
37357 +       struct file *file;
37358 +       unsigned int u, nfile;
37359 +
37360 +       file = NULL;
37361 +       nfile = xi->xi_nfile;
37362 +       for (u = 0; u < nfile; u++) {
37363 +               file = xi->xi_file[u];
37364 +               if (file)
37365 +                       break;
37366 +       }
37367 +
37368 +       return file;
37369 +}
37370 +
37371 +static int au_xino_file_set(struct au_xino *xi, int idx, struct file *file)
37372 +{
37373 +       int err;
37374 +       struct file *f;
37375 +       void *p;
37376 +
37377 +       if (file)
37378 +               get_file(file);
37379 +
37380 +       err = 0;
37381 +       f = NULL;
37382 +       if (idx < xi->xi_nfile) {
37383 +               f = xi->xi_file[idx];
37384 +               if (f)
37385 +                       fput(f);
37386 +       } else {
37387 +               p = au_kzrealloc(xi->xi_file,
37388 +                                sizeof(*xi->xi_file) * xi->xi_nfile,
37389 +                                sizeof(*xi->xi_file) * (idx + 1),
37390 +                                GFP_NOFS, /*may_shrink*/0);
37391 +               if (p) {
37392 +                       MtxMustLock(&xi->xi_mtx);
37393 +                       xi->xi_file = p;
37394 +                       xi->xi_nfile = idx + 1;
37395 +               } else {
37396 +                       err = -ENOMEM;
37397 +                       if (file)
37398 +                               fput(file);
37399 +                       goto out;
37400 +               }
37401 +       }
37402 +       xi->xi_file[idx] = file;
37403 +
37404 +out:
37405 +       return err;
37406 +}
37407 +
37408 +/*
37409 + * if @xinew->xi is not set, then create new xigen file.
37410 + */
37411 +struct file *au_xi_new(struct super_block *sb, struct au_xi_new *xinew)
37412 +{
37413 +       struct file *file;
37414 +       int err;
37415 +
37416 +       SiMustAnyLock(sb);
37417 +
37418 +       file = au_xino_create2(sb, xinew->base, xinew->copy_src);
37419 +       if (IS_ERR(file)) {
37420 +               err = PTR_ERR(file);
37421 +               pr_err("%s[%d], err %d\n",
37422 +                      xinew->xi ? "xino" : "xigen",
37423 +                      xinew->idx, err);
37424 +               goto out;
37425 +       }
37426 +
37427 +       if (xinew->xi)
37428 +               err = au_xino_file_set(xinew->xi, xinew->idx, file);
37429 +       else {
37430 +               BUG();
37431 +               /* todo: make xigen file an array */
37432 +               /* err = au_xigen_file_set(sb, xinew->idx, file); */
37433 +       }
37434 +       fput(file);
37435 +       if (unlikely(err))
37436 +               file = ERR_PTR(err);
37437 +
37438 +out:
37439 +       return file;
37440 +}
37441 +
37442 +/* ---------------------------------------------------------------------- */
37443 +
37444 +/*
37445 + * truncate xino files
37446 + */
37447 +static int au_xino_do_trunc(struct super_block *sb, aufs_bindex_t bindex,
37448 +                           int idx, struct kstatfs *st)
37449 +{
37450 +       int err;
37451 +       blkcnt_t blocks;
37452 +       struct file *file, *new_xino;
37453 +       struct au_xi_new xinew = {
37454 +               .idx = idx
37455 +       };
37456 +
37457 +       err = 0;
37458 +       xinew.xi = au_sbr(sb, bindex)->br_xino;
37459 +       file = au_xino_file(xinew.xi, idx);
37460 +       if (!file)
37461 +               goto out;
37462 +
37463 +       xinew.base = &file->f_path;
37464 +       err = vfs_statfs(xinew.base, st);
37465 +       if (unlikely(err)) {
37466 +               AuErr1("statfs err %d, ignored\n", err);
37467 +               err = 0;
37468 +               goto out;
37469 +       }
37470 +
37471 +       blocks = file_inode(file)->i_blocks;
37472 +       pr_info("begin truncating xino(b%d-%d), ib%llu, %llu/%llu free blks\n",
37473 +               bindex, idx, (u64)blocks, st->f_bfree, st->f_blocks);
37474 +
37475 +       xinew.copy_src = file;
37476 +       new_xino = au_xi_new(sb, &xinew);
37477 +       if (IS_ERR(new_xino)) {
37478 +               err = PTR_ERR(new_xino);
37479 +               pr_err("xino(b%d-%d), err %d, ignored\n", bindex, idx, err);
37480 +               goto out;
37481 +       }
37482 +
37483 +       err = vfs_statfs(&new_xino->f_path, st);
37484 +       if (!err)
37485 +               pr_info("end truncating xino(b%d-%d), ib%llu, %llu/%llu free blks\n",
37486 +                       bindex, idx, (u64)file_inode(new_xino)->i_blocks,
37487 +                       st->f_bfree, st->f_blocks);
37488 +       else {
37489 +               AuErr1("statfs err %d, ignored\n", err);
37490 +               err = 0;
37491 +       }
37492 +
37493 +out:
37494 +       return err;
37495 +}
37496 +
37497 +int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex, int idx_begin)
37498 +{
37499 +       int err, i;
37500 +       unsigned long jiffy;
37501 +       aufs_bindex_t bbot;
37502 +       struct kstatfs *st;
37503 +       struct au_branch *br;
37504 +       struct au_xino *xi;
37505 +
37506 +       err = -ENOMEM;
37507 +       st = kmalloc(sizeof(*st), GFP_NOFS);
37508 +       if (unlikely(!st))
37509 +               goto out;
37510 +
37511 +       err = -EINVAL;
37512 +       bbot = au_sbbot(sb);
37513 +       if (unlikely(bindex < 0 || bbot < bindex))
37514 +               goto out_st;
37515 +
37516 +       err = 0;
37517 +       jiffy = jiffies;
37518 +       br = au_sbr(sb, bindex);
37519 +       xi = br->br_xino;
37520 +       for (i = idx_begin; !err && i < xi->xi_nfile; i++)
37521 +               err = au_xino_do_trunc(sb, bindex, i, st);
37522 +       if (!err)
37523 +               au_sbi(sb)->si_xino_jiffy = jiffy;
37524 +
37525 +out_st:
37526 +       au_kfree_rcu(st);
37527 +out:
37528 +       return err;
37529 +}
37530 +
37531 +struct xino_do_trunc_args {
37532 +       struct super_block *sb;
37533 +       struct au_branch *br;
37534 +       int idx;
37535 +};
37536 +
37537 +static void xino_do_trunc(void *_args)
37538 +{
37539 +       struct xino_do_trunc_args *args = _args;
37540 +       struct super_block *sb;
37541 +       struct au_branch *br;
37542 +       struct inode *dir;
37543 +       int err, idx;
37544 +       aufs_bindex_t bindex;
37545 +
37546 +       err = 0;
37547 +       sb = args->sb;
37548 +       dir = d_inode(sb->s_root);
37549 +       br = args->br;
37550 +       idx = args->idx;
37551 +
37552 +       si_noflush_write_lock(sb);
37553 +       ii_read_lock_parent(dir);
37554 +       bindex = au_br_index(sb, br->br_id);
37555 +       err = au_xino_trunc(sb, bindex, idx);
37556 +       ii_read_unlock(dir);
37557 +       if (unlikely(err))
37558 +               pr_warn("err b%d, (%d)\n", bindex, err);
37559 +       atomic_dec(&br->br_xino->xi_truncating);
37560 +       au_lcnt_dec(&br->br_count);
37561 +       si_write_unlock(sb);
37562 +       au_nwt_done(&au_sbi(sb)->si_nowait);
37563 +       au_kfree_rcu(args);
37564 +}
37565 +
37566 +/*
37567 + * returns the index in the xi_file array whose corresponding file is necessary
37568 + * to truncate, or -1 which means no need to truncate.
37569 + */
37570 +static int xino_trunc_test(struct super_block *sb, struct au_branch *br)
37571 +{
37572 +       int err;
37573 +       unsigned int u;
37574 +       struct kstatfs st;
37575 +       struct au_sbinfo *sbinfo;
37576 +       struct au_xino *xi;
37577 +       struct file *file;
37578 +
37579 +       /* todo: si_xino_expire and the ratio should be customizable */
37580 +       sbinfo = au_sbi(sb);
37581 +       if (time_before(jiffies,
37582 +                       sbinfo->si_xino_jiffy + sbinfo->si_xino_expire))
37583 +               return -1;
37584 +
37585 +       /* truncation border */
37586 +       xi = br->br_xino;
37587 +       for (u = 0; u < xi->xi_nfile; u++) {
37588 +               file = au_xino_file(xi, u);
37589 +               if (!file)
37590 +                       continue;
37591 +
37592 +               err = vfs_statfs(&file->f_path, &st);
37593 +               if (unlikely(err)) {
37594 +                       AuErr1("statfs err %d, ignored\n", err);
37595 +                       return -1;
37596 +               }
37597 +               if (div64_u64(st.f_bfree * 100, st.f_blocks)
37598 +                   >= AUFS_XINO_DEF_TRUNC)
37599 +                       return u;
37600 +       }
37601 +
37602 +       return -1;
37603 +}
37604 +
37605 +static void xino_try_trunc(struct super_block *sb, struct au_branch *br)
37606 +{
37607 +       int idx;
37608 +       struct xino_do_trunc_args *args;
37609 +       int wkq_err;
37610 +
37611 +       idx = xino_trunc_test(sb, br);
37612 +       if (idx < 0)
37613 +               return;
37614 +
37615 +       if (atomic_inc_return(&br->br_xino->xi_truncating) > 1)
37616 +               goto out;
37617 +
37618 +       /* lock and kfree() will be called in trunc_xino() */
37619 +       args = kmalloc(sizeof(*args), GFP_NOFS);
37620 +       if (unlikely(!args)) {
37621 +               AuErr1("no memory\n");
37622 +               goto out;
37623 +       }
37624 +
37625 +       au_lcnt_inc(&br->br_count);
37626 +       args->sb = sb;
37627 +       args->br = br;
37628 +       args->idx = idx;
37629 +       wkq_err = au_wkq_nowait(xino_do_trunc, args, sb, /*flags*/0);
37630 +       if (!wkq_err)
37631 +               return; /* success */
37632 +
37633 +       pr_err("wkq %d\n", wkq_err);
37634 +       au_lcnt_dec(&br->br_count);
37635 +       au_kfree_rcu(args);
37636 +
37637 +out:
37638 +       atomic_dec(&br->br_xino->xi_truncating);
37639 +}
37640 +
37641 +/* ---------------------------------------------------------------------- */
37642 +
37643 +struct au_xi_calc {
37644 +       int idx;
37645 +       loff_t pos;
37646 +};
37647 +
37648 +static void au_xi_calc(struct super_block *sb, ino_t h_ino,
37649 +                      struct au_xi_calc *calc)
37650 +{
37651 +       loff_t maxent;
37652 +
37653 +       maxent = au_xi_maxent(sb);
37654 +       calc->idx = div64_u64_rem(h_ino, maxent, &calc->pos);
37655 +       calc->pos *= sizeof(ino_t);
37656 +}
37657 +
37658 +static int au_xino_do_new_async(struct super_block *sb, struct au_branch *br,
37659 +                               struct au_xi_calc *calc)
37660 +{
37661 +       int err;
37662 +       struct file *file;
37663 +       struct au_xino *xi = br->br_xino;
37664 +       struct au_xi_new xinew = {
37665 +               .xi = xi
37666 +       };
37667 +
37668 +       SiMustAnyLock(sb);
37669 +
37670 +       err = 0;
37671 +       if (!xi)
37672 +               goto out;
37673 +
37674 +       mutex_lock(&xi->xi_mtx);
37675 +       file = au_xino_file(xi, calc->idx);
37676 +       if (file)
37677 +               goto out_mtx;
37678 +
37679 +       file = au_xino_file(xi, /*idx*/-1);
37680 +       AuDebugOn(!file);
37681 +       xinew.idx = calc->idx;
37682 +       xinew.base = &file->f_path;
37683 +       /* xinew.copy_src = NULL; */
37684 +       file = au_xi_new(sb, &xinew);
37685 +       if (IS_ERR(file))
37686 +               err = PTR_ERR(file);
37687 +
37688 +out_mtx:
37689 +       mutex_unlock(&xi->xi_mtx);
37690 +out:
37691 +       return err;
37692 +}
37693 +
37694 +struct au_xino_do_new_async_args {
37695 +       struct super_block *sb;
37696 +       struct au_branch *br;
37697 +       struct au_xi_calc calc;
37698 +       ino_t ino;
37699 +};
37700 +
37701 +struct au_xi_writing {
37702 +       struct hlist_bl_node node;
37703 +       ino_t h_ino, ino;
37704 +};
37705 +
37706 +static int au_xino_do_write(struct file *file, struct au_xi_calc *calc,
37707 +                           ino_t ino);
37708 +
37709 +static void au_xino_call_do_new_async(void *args)
37710 +{
37711 +       struct au_xino_do_new_async_args *a = args;
37712 +       struct au_branch *br;
37713 +       struct super_block *sb;
37714 +       struct au_sbinfo *sbi;
37715 +       struct inode *root;
37716 +       struct file *file;
37717 +       struct au_xi_writing *del, *p;
37718 +       struct hlist_bl_head *hbl;
37719 +       struct hlist_bl_node *pos;
37720 +       int err;
37721 +
37722 +       br = a->br;
37723 +       sb = a->sb;
37724 +       sbi = au_sbi(sb);
37725 +       si_noflush_read_lock(sb);
37726 +       root = d_inode(sb->s_root);
37727 +       ii_read_lock_child(root);
37728 +       err = au_xino_do_new_async(sb, br, &a->calc);
37729 +       if (unlikely(err)) {
37730 +               AuIOErr("err %d\n", err);
37731 +               goto out;
37732 +       }
37733 +
37734 +       file = au_xino_file(br->br_xino, a->calc.idx);
37735 +       AuDebugOn(!file);
37736 +       err = au_xino_do_write(file, &a->calc, a->ino);
37737 +       if (unlikely(err)) {
37738 +               AuIOErr("err %d\n", err);
37739 +               goto out;
37740 +       }
37741 +
37742 +       del = NULL;
37743 +       hbl = &br->br_xino->xi_writing;
37744 +       hlist_bl_lock(hbl);
37745 +       au_hbl_for_each(pos, hbl) {
37746 +               p = container_of(pos, struct au_xi_writing, node);
37747 +               if (p->ino == a->ino) {
37748 +                       del = p;
37749 +                       hlist_bl_del(&p->node);
37750 +                       break;
37751 +               }
37752 +       }
37753 +       hlist_bl_unlock(hbl);
37754 +       au_kfree_rcu(del);
37755 +
37756 +out:
37757 +       au_lcnt_dec(&br->br_count);
37758 +       ii_read_unlock(root);
37759 +       si_read_unlock(sb);
37760 +       au_nwt_done(&sbi->si_nowait);
37761 +       au_kfree_rcu(a);
37762 +}
37763 +
37764 +/*
37765 + * create a new xino file asynchronously
37766 + */
37767 +static int au_xino_new_async(struct super_block *sb, struct au_branch *br,
37768 +                            struct au_xi_calc *calc, ino_t ino)
37769 +{
37770 +       int err;
37771 +       struct au_xino_do_new_async_args *arg;
37772 +
37773 +       err = -ENOMEM;
37774 +       arg = kmalloc(sizeof(*arg), GFP_NOFS);
37775 +       if (unlikely(!arg))
37776 +               goto out;
37777 +
37778 +       arg->sb = sb;
37779 +       arg->br = br;
37780 +       arg->calc = *calc;
37781 +       arg->ino = ino;
37782 +       au_lcnt_inc(&br->br_count);
37783 +       err = au_wkq_nowait(au_xino_call_do_new_async, arg, sb, AuWkq_NEST);
37784 +       if (unlikely(err)) {
37785 +               pr_err("wkq %d\n", err);
37786 +               au_lcnt_dec(&br->br_count);
37787 +               au_kfree_rcu(arg);
37788 +       }
37789 +
37790 +out:
37791 +       return err;
37792 +}
37793 +
37794 +/*
37795 + * read @ino from xinofile for the specified branch{@sb, @bindex}
37796 + * at the position of @h_ino.
37797 + */
37798 +int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
37799 +                ino_t *ino)
37800 +{
37801 +       int err;
37802 +       ssize_t sz;
37803 +       struct au_xi_calc calc;
37804 +       struct au_sbinfo *sbinfo;
37805 +       struct file *file;
37806 +       struct au_xino *xi;
37807 +       struct hlist_bl_head *hbl;
37808 +       struct hlist_bl_node *pos;
37809 +       struct au_xi_writing *p;
37810 +
37811 +       *ino = 0;
37812 +       if (!au_opt_test(au_mntflags(sb), XINO))
37813 +               return 0; /* no xino */
37814 +
37815 +       err = 0;
37816 +       au_xi_calc(sb, h_ino, &calc);
37817 +       xi = au_sbr(sb, bindex)->br_xino;
37818 +       file = au_xino_file(xi, calc.idx);
37819 +       if (!file) {
37820 +               hbl = &xi->xi_writing;
37821 +               hlist_bl_lock(hbl);
37822 +               au_hbl_for_each(pos, hbl) {
37823 +                       p = container_of(pos, struct au_xi_writing, node);
37824 +                       if (p->h_ino == h_ino) {
37825 +                               AuDbg("hi%llu, i%llu, found\n",
37826 +                                     (u64)p->h_ino, (u64)p->ino);
37827 +                               *ino = p->ino;
37828 +                               break;
37829 +                       }
37830 +               }
37831 +               hlist_bl_unlock(hbl);
37832 +               return 0;
37833 +       } else if (vfsub_f_size_read(file) < calc.pos + sizeof(*ino))
37834 +               return 0; /* no xino */
37835 +
37836 +       sbinfo = au_sbi(sb);
37837 +       sz = xino_fread(file, ino, sizeof(*ino), &calc.pos);
37838 +       if (sz == sizeof(*ino))
37839 +               return 0; /* success */
37840 +
37841 +       err = sz;
37842 +       if (unlikely(sz >= 0)) {
37843 +               err = -EIO;
37844 +               AuIOErr("xino read error (%zd)\n", sz);
37845 +       }
37846 +       return err;
37847 +}
37848 +
37849 +static int au_xino_do_write(struct file *file, struct au_xi_calc *calc,
37850 +                           ino_t ino)
37851 +{
37852 +       ssize_t sz;
37853 +
37854 +       sz = xino_fwrite(file, &ino, sizeof(ino), &calc->pos);
37855 +       if (sz == sizeof(ino))
37856 +               return 0; /* success */
37857 +
37858 +       AuIOErr("write failed (%zd)\n", sz);
37859 +       return -EIO;
37860 +}
37861 +
37862 +/*
37863 + * write @ino to the xinofile for the specified branch{@sb, @bindex}
37864 + * at the position of @h_ino.
37865 + * even if @ino is zero, it is written to the xinofile and means no entry.
37866 + * if the size of the xino file on a specific filesystem exceeds the watermark,
37867 + * try truncating it.
37868 + */
37869 +int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
37870 +                 ino_t ino)
37871 +{
37872 +       int err;
37873 +       unsigned int mnt_flags;
37874 +       struct au_xi_calc calc;
37875 +       struct file *file;
37876 +       struct au_branch *br;
37877 +       struct au_xino *xi;
37878 +       struct au_xi_writing *p;
37879 +
37880 +       SiMustAnyLock(sb);
37881 +
37882 +       mnt_flags = au_mntflags(sb);
37883 +       if (!au_opt_test(mnt_flags, XINO))
37884 +               return 0;
37885 +
37886 +       au_xi_calc(sb, h_ino, &calc);
37887 +       br = au_sbr(sb, bindex);
37888 +       xi = br->br_xino;
37889 +       file = au_xino_file(xi, calc.idx);
37890 +       if (!file) {
37891 +               /* store the inum pair into the list */
37892 +               p = kmalloc(sizeof(*p), GFP_NOFS | __GFP_NOFAIL);
37893 +               p->h_ino = h_ino;
37894 +               p->ino = ino;
37895 +               au_hbl_add(&p->node, &xi->xi_writing);
37896 +
37897 +               /* create and write a new xino file asynchronously */
37898 +               err = au_xino_new_async(sb, br, &calc, ino);
37899 +               if (!err)
37900 +                       return 0; /* success */
37901 +               goto out;
37902 +       }
37903 +
37904 +       err = au_xino_do_write(file, &calc, ino);
37905 +       if (!err) {
37906 +               br = au_sbr(sb, bindex);
37907 +               if (au_opt_test(mnt_flags, TRUNC_XINO)
37908 +                   && au_test_fs_trunc_xino(au_br_sb(br)))
37909 +                       xino_try_trunc(sb, br);
37910 +               return 0; /* success */
37911 +       }
37912 +
37913 +out:
37914 +       AuIOErr("write failed (%d)\n", err);
37915 +       return -EIO;
37916 +}
37917 +
37918 +static ssize_t xino_fread_wkq(struct file *file, void *buf, size_t size,
37919 +                             loff_t *pos);
37920 +
37921 +/* todo: unnecessary to support mmap_sem since kernel-space? */
37922 +ssize_t xino_fread(struct file *file, void *kbuf, size_t size, loff_t *pos)
37923 +{
37924 +       ssize_t err;
37925 +       int i;
37926 +       const int prevent_endless = 10;
37927 +
37928 +       i = 0;
37929 +       do {
37930 +               err = vfsub_read_k(file, kbuf, size, pos);
37931 +               if (err == -EINTR
37932 +                   && !au_wkq_test()
37933 +                   && fatal_signal_pending(current)) {
37934 +                       err = xino_fread_wkq(file, kbuf, size, pos);
37935 +                       BUG_ON(err == -EINTR);
37936 +               }
37937 +       } while (i++ < prevent_endless
37938 +                && (err == -EAGAIN || err == -EINTR));
37939 +
37940 +#if 0 /* reserved for future use */
37941 +       if (err > 0)
37942 +               fsnotify_access(file->f_path.dentry);
37943 +#endif
37944 +
37945 +       return err;
37946 +}
37947 +
37948 +struct xino_fread_args {
37949 +       ssize_t *errp;
37950 +       struct file *file;
37951 +       void *buf;
37952 +       size_t size;
37953 +       loff_t *pos;
37954 +};
37955 +
37956 +static void call_xino_fread(void *args)
37957 +{
37958 +       struct xino_fread_args *a = args;
37959 +       *a->errp = xino_fread(a->file, a->buf, a->size, a->pos);
37960 +}
37961 +
37962 +static ssize_t xino_fread_wkq(struct file *file, void *buf, size_t size,
37963 +                             loff_t *pos)
37964 +{
37965 +       ssize_t err;
37966 +       int wkq_err;
37967 +       struct xino_fread_args args = {
37968 +               .errp   = &err,
37969 +               .file   = file,
37970 +               .buf    = buf,
37971 +               .size   = size,
37972 +               .pos    = pos
37973 +       };
37974 +
37975 +       wkq_err = au_wkq_wait(call_xino_fread, &args);
37976 +       if (unlikely(wkq_err))
37977 +               err = wkq_err;
37978 +
37979 +       return err;
37980 +}
37981 +
37982 +static ssize_t xino_fwrite_wkq(struct file *file, void *buf, size_t size,
37983 +                              loff_t *pos);
37984 +
37985 +static ssize_t do_xino_fwrite(struct file *file, void *kbuf, size_t size,
37986 +                             loff_t *pos)
37987 +{
37988 +       ssize_t err;
37989 +       int i;
37990 +       const int prevent_endless = 10;
37991 +
37992 +       i = 0;
37993 +       do {
37994 +               err = vfsub_write_k(file, kbuf, size, pos);
37995 +               if (err == -EINTR
37996 +                   && !au_wkq_test()
37997 +                   && fatal_signal_pending(current)) {
37998 +                       err = xino_fwrite_wkq(file, kbuf, size, pos);
37999 +                       BUG_ON(err == -EINTR);
38000 +               }
38001 +       } while (i++ < prevent_endless
38002 +                && (err == -EAGAIN || err == -EINTR));
38003 +
38004 +#if 0 /* reserved for future use */
38005 +       if (err > 0)
38006 +               fsnotify_modify(file->f_path.dentry);
38007 +#endif
38008 +
38009 +       return err;
38010 +}
38011 +
38012 +struct do_xino_fwrite_args {
38013 +       ssize_t *errp;
38014 +       struct file *file;
38015 +       void *buf;
38016 +       size_t size;
38017 +       loff_t *pos;
38018 +};
38019 +
38020 +static void call_do_xino_fwrite(void *args)
38021 +{
38022 +       struct do_xino_fwrite_args *a = args;
38023 +       *a->errp = do_xino_fwrite(a->file, a->buf, a->size, a->pos);
38024 +}
38025 +
38026 +static ssize_t xino_fwrite_wkq(struct file *file, void *buf, size_t size,
38027 +                              loff_t *pos)
38028 +{
38029 +       ssize_t err;
38030 +       int wkq_err;
38031 +       struct do_xino_fwrite_args args = {
38032 +               .errp   = &err,
38033 +               .file   = file,
38034 +               .buf    = buf,
38035 +               .size   = size,
38036 +               .pos    = pos
38037 +       };
38038 +
38039 +       /*
38040 +        * it breaks RLIMIT_FSIZE and normal user's limit,
38041 +        * users should care about quota and real 'filesystem full.'
38042 +        */
38043 +       wkq_err = au_wkq_wait(call_do_xino_fwrite, &args);
38044 +       if (unlikely(wkq_err))
38045 +               err = wkq_err;
38046 +
38047 +       return err;
38048 +}
38049 +
38050 +ssize_t xino_fwrite(struct file *file, void *buf, size_t size, loff_t *pos)
38051 +{
38052 +       ssize_t err;
38053 +
38054 +       if (rlimit(RLIMIT_FSIZE) == RLIM_INFINITY) {
38055 +               lockdep_off();
38056 +               err = do_xino_fwrite(file, buf, size, pos);
38057 +               lockdep_on();
38058 +       } else {
38059 +               lockdep_off();
38060 +               err = xino_fwrite_wkq(file, buf, size, pos);
38061 +               lockdep_on();
38062 +       }
38063 +
38064 +       return err;
38065 +}
38066 +
38067 +/* ---------------------------------------------------------------------- */
38068 +
38069 +/*
38070 + * inode number bitmap
38071 + */
38072 +static const int page_bits = (int)PAGE_SIZE * BITS_PER_BYTE;
38073 +static ino_t xib_calc_ino(unsigned long pindex, int bit)
38074 +{
38075 +       ino_t ino;
38076 +
38077 +       AuDebugOn(bit < 0 || page_bits <= bit);
38078 +       ino = AUFS_FIRST_INO + pindex * page_bits + bit;
38079 +       return ino;
38080 +}
38081 +
38082 +static void xib_calc_bit(ino_t ino, unsigned long *pindex, int *bit)
38083 +{
38084 +       AuDebugOn(ino < AUFS_FIRST_INO);
38085 +       ino -= AUFS_FIRST_INO;
38086 +       *pindex = ino / page_bits;
38087 +       *bit = ino % page_bits;
38088 +}
38089 +
38090 +static int xib_pindex(struct super_block *sb, unsigned long pindex)
38091 +{
38092 +       int err;
38093 +       loff_t pos;
38094 +       ssize_t sz;
38095 +       struct au_sbinfo *sbinfo;
38096 +       struct file *xib;
38097 +       unsigned long *p;
38098 +
38099 +       sbinfo = au_sbi(sb);
38100 +       MtxMustLock(&sbinfo->si_xib_mtx);
38101 +       AuDebugOn(pindex > ULONG_MAX / PAGE_SIZE
38102 +                 || !au_opt_test(sbinfo->si_mntflags, XINO));
38103 +
38104 +       if (pindex == sbinfo->si_xib_last_pindex)
38105 +               return 0;
38106 +
38107 +       xib = sbinfo->si_xib;
38108 +       p = sbinfo->si_xib_buf;
38109 +       pos = sbinfo->si_xib_last_pindex;
38110 +       pos *= PAGE_SIZE;
38111 +       sz = xino_fwrite(xib, p, PAGE_SIZE, &pos);
38112 +       if (unlikely(sz != PAGE_SIZE))
38113 +               goto out;
38114 +
38115 +       pos = pindex;
38116 +       pos *= PAGE_SIZE;
38117 +       if (vfsub_f_size_read(xib) >= pos + PAGE_SIZE)
38118 +               sz = xino_fread(xib, p, PAGE_SIZE, &pos);
38119 +       else {
38120 +               memset(p, 0, PAGE_SIZE);
38121 +               sz = xino_fwrite(xib, p, PAGE_SIZE, &pos);
38122 +       }
38123 +       if (sz == PAGE_SIZE) {
38124 +               sbinfo->si_xib_last_pindex = pindex;
38125 +               return 0; /* success */
38126 +       }
38127 +
38128 +out:
38129 +       AuIOErr1("write failed (%zd)\n", sz);
38130 +       err = sz;
38131 +       if (sz >= 0)
38132 +               err = -EIO;
38133 +       return err;
38134 +}
38135 +
38136 +static void au_xib_clear_bit(struct inode *inode)
38137 +{
38138 +       int err, bit;
38139 +       unsigned long pindex;
38140 +       struct super_block *sb;
38141 +       struct au_sbinfo *sbinfo;
38142 +
38143 +       AuDebugOn(inode->i_nlink);
38144 +
38145 +       sb = inode->i_sb;
38146 +       xib_calc_bit(inode->i_ino, &pindex, &bit);
38147 +       AuDebugOn(page_bits <= bit);
38148 +       sbinfo = au_sbi(sb);
38149 +       mutex_lock(&sbinfo->si_xib_mtx);
38150 +       err = xib_pindex(sb, pindex);
38151 +       if (!err) {
38152 +               clear_bit(bit, sbinfo->si_xib_buf);
38153 +               sbinfo->si_xib_next_bit = bit;
38154 +       }
38155 +       mutex_unlock(&sbinfo->si_xib_mtx);
38156 +}
38157 +
38158 +/* ---------------------------------------------------------------------- */
38159 +
38160 +/*
38161 + * truncate a xino bitmap file
38162 + */
38163 +
38164 +/* todo: slow */
38165 +static int do_xib_restore(struct super_block *sb, struct file *file, void *page)
38166 +{
38167 +       int err, bit;
38168 +       ssize_t sz;
38169 +       unsigned long pindex;
38170 +       loff_t pos, pend;
38171 +       struct au_sbinfo *sbinfo;
38172 +       ino_t *ino;
38173 +       unsigned long *p;
38174 +
38175 +       err = 0;
38176 +       sbinfo = au_sbi(sb);
38177 +       MtxMustLock(&sbinfo->si_xib_mtx);
38178 +       p = sbinfo->si_xib_buf;
38179 +       pend = vfsub_f_size_read(file);
38180 +       pos = 0;
38181 +       while (pos < pend) {
38182 +               sz = xino_fread(file, page, PAGE_SIZE, &pos);
38183 +               err = sz;
38184 +               if (unlikely(sz <= 0))
38185 +                       goto out;
38186 +
38187 +               err = 0;
38188 +               for (ino = page; sz > 0; ino++, sz -= sizeof(ino)) {
38189 +                       if (unlikely(*ino < AUFS_FIRST_INO))
38190 +                               continue;
38191 +
38192 +                       xib_calc_bit(*ino, &pindex, &bit);
38193 +                       AuDebugOn(page_bits <= bit);
38194 +                       err = xib_pindex(sb, pindex);
38195 +                       if (!err)
38196 +                               set_bit(bit, p);
38197 +                       else
38198 +                               goto out;
38199 +               }
38200 +       }
38201 +
38202 +out:
38203 +       return err;
38204 +}
38205 +
38206 +static int xib_restore(struct super_block *sb)
38207 +{
38208 +       int err, i;
38209 +       unsigned int nfile;
38210 +       aufs_bindex_t bindex, bbot;
38211 +       void *page;
38212 +       struct au_branch *br;
38213 +       struct au_xino *xi;
38214 +       struct file *file;
38215 +
38216 +       err = -ENOMEM;
38217 +       page = (void *)__get_free_page(GFP_NOFS);
38218 +       if (unlikely(!page))
38219 +               goto out;
38220 +
38221 +       err = 0;
38222 +       bbot = au_sbbot(sb);
38223 +       for (bindex = 0; !err && bindex <= bbot; bindex++)
38224 +               if (!bindex || is_sb_shared(sb, bindex, bindex - 1) < 0) {
38225 +                       br = au_sbr(sb, bindex);
38226 +                       xi = br->br_xino;
38227 +                       nfile = xi->xi_nfile;
38228 +                       for (i = 0; i < nfile; i++) {
38229 +                               file = au_xino_file(xi, i);
38230 +                               if (file)
38231 +                                       err = do_xib_restore(sb, file, page);
38232 +                       }
38233 +               } else
38234 +                       AuDbg("skip shared b%d\n", bindex);
38235 +       free_page((unsigned long)page);
38236 +
38237 +out:
38238 +       return err;
38239 +}
38240 +
38241 +int au_xib_trunc(struct super_block *sb)
38242 +{
38243 +       int err;
38244 +       ssize_t sz;
38245 +       loff_t pos;
38246 +       struct au_sbinfo *sbinfo;
38247 +       unsigned long *p;
38248 +       struct file *file;
38249 +
38250 +       SiMustWriteLock(sb);
38251 +
38252 +       err = 0;
38253 +       sbinfo = au_sbi(sb);
38254 +       if (!au_opt_test(sbinfo->si_mntflags, XINO))
38255 +               goto out;
38256 +
38257 +       file = sbinfo->si_xib;
38258 +       if (vfsub_f_size_read(file) <= PAGE_SIZE)
38259 +               goto out;
38260 +
38261 +       file = au_xino_create2(sb, &sbinfo->si_xib->f_path, NULL);
38262 +       err = PTR_ERR(file);
38263 +       if (IS_ERR(file))
38264 +               goto out;
38265 +       fput(sbinfo->si_xib);
38266 +       sbinfo->si_xib = file;
38267 +
38268 +       p = sbinfo->si_xib_buf;
38269 +       memset(p, 0, PAGE_SIZE);
38270 +       pos = 0;
38271 +       sz = xino_fwrite(sbinfo->si_xib, p, PAGE_SIZE, &pos);
38272 +       if (unlikely(sz != PAGE_SIZE)) {
38273 +               err = sz;
38274 +               AuIOErr("err %d\n", err);
38275 +               if (sz >= 0)
38276 +                       err = -EIO;
38277 +               goto out;
38278 +       }
38279 +
38280 +       mutex_lock(&sbinfo->si_xib_mtx);
38281 +       /* mnt_want_write() is unnecessary here */
38282 +       err = xib_restore(sb);
38283 +       mutex_unlock(&sbinfo->si_xib_mtx);
38284 +
38285 +out:
38286 +       return err;
38287 +}
38288 +
38289 +/* ---------------------------------------------------------------------- */
38290 +
38291 +struct au_xino *au_xino_alloc(unsigned int nfile)
38292 +{
38293 +       struct au_xino *xi;
38294 +
38295 +       xi = kzalloc(sizeof(*xi), GFP_NOFS);
38296 +       if (unlikely(!xi))
38297 +               goto out;
38298 +       xi->xi_nfile = nfile;
38299 +       xi->xi_file = kcalloc(nfile, sizeof(*xi->xi_file), GFP_NOFS);
38300 +       if (unlikely(!xi->xi_file))
38301 +               goto out_free;
38302 +
38303 +       xi->xi_nondir.total = 8; /* initial size */
38304 +       xi->xi_nondir.array = kcalloc(xi->xi_nondir.total, sizeof(ino_t),
38305 +                                     GFP_NOFS);
38306 +       if (unlikely(!xi->xi_nondir.array))
38307 +               goto out_file;
38308 +
38309 +       spin_lock_init(&xi->xi_nondir.spin);
38310 +       init_waitqueue_head(&xi->xi_nondir.wqh);
38311 +       mutex_init(&xi->xi_mtx);
38312 +       INIT_HLIST_BL_HEAD(&xi->xi_writing);
38313 +       atomic_set(&xi->xi_truncating, 0);
38314 +       kref_init(&xi->xi_kref);
38315 +       goto out; /* success */
38316 +
38317 +out_file:
38318 +       au_kfree_try_rcu(xi->xi_file);
38319 +out_free:
38320 +       au_kfree_rcu(xi);
38321 +       xi = NULL;
38322 +out:
38323 +       return xi;
38324 +}
38325 +
38326 +static int au_xino_init(struct au_branch *br, int idx, struct file *file)
38327 +{
38328 +       int err;
38329 +       struct au_xino *xi;
38330 +
38331 +       err = 0;
38332 +       xi = au_xino_alloc(idx + 1);
38333 +       if (unlikely(!xi)) {
38334 +               err = -ENOMEM;
38335 +               goto out;
38336 +       }
38337 +
38338 +       if (file)
38339 +               get_file(file);
38340 +       xi->xi_file[idx] = file;
38341 +       AuDebugOn(br->br_xino);
38342 +       br->br_xino = xi;
38343 +
38344 +out:
38345 +       return err;
38346 +}
38347 +
38348 +static void au_xino_release(struct kref *kref)
38349 +{
38350 +       struct au_xino *xi;
38351 +       int i;
38352 +       unsigned long ul;
38353 +       struct hlist_bl_head *hbl;
38354 +       struct hlist_bl_node *pos, *n;
38355 +       struct au_xi_writing *p;
38356 +
38357 +       xi = container_of(kref, struct au_xino, xi_kref);
38358 +       for (i = 0; i < xi->xi_nfile; i++)
38359 +               if (xi->xi_file[i])
38360 +                       fput(xi->xi_file[i]);
38361 +       for (i = xi->xi_nondir.total - 1; i >= 0; i--)
38362 +               AuDebugOn(xi->xi_nondir.array[i]);
38363 +       mutex_destroy(&xi->xi_mtx);
38364 +       hbl = &xi->xi_writing;
38365 +       ul = au_hbl_count(hbl);
38366 +       if (unlikely(ul)) {
38367 +               pr_warn("xi_writing %lu\n", ul);
38368 +               hlist_bl_lock(hbl);
38369 +               hlist_bl_for_each_entry_safe(p, pos, n, hbl, node) {
38370 +                       hlist_bl_del(&p->node);
38371 +                       /* kmemleak reported au_kfree_rcu() doesn't free it */
38372 +                       kfree(p);
38373 +               }
38374 +               hlist_bl_unlock(hbl);
38375 +       }
38376 +       au_kfree_try_rcu(xi->xi_file);
38377 +       au_kfree_try_rcu(xi->xi_nondir.array);
38378 +       au_kfree_rcu(xi);
38379 +}
38380 +
38381 +int au_xino_put(struct au_branch *br)
38382 +{
38383 +       int ret;
38384 +       struct au_xino *xi;
38385 +
38386 +       ret = 0;
38387 +       xi = br->br_xino;
38388 +       if (xi) {
38389 +               br->br_xino = NULL;
38390 +               ret = kref_put(&xi->xi_kref, au_xino_release);
38391 +       }
38392 +
38393 +       return ret;
38394 +}
38395 +
38396 +/* ---------------------------------------------------------------------- */
38397 +
38398 +/*
38399 + * xino mount option handlers
38400 + */
38401 +
38402 +/* xino bitmap */
38403 +static void xino_clear_xib(struct super_block *sb)
38404 +{
38405 +       struct au_sbinfo *sbinfo;
38406 +
38407 +       SiMustWriteLock(sb);
38408 +
38409 +       sbinfo = au_sbi(sb);
38410 +       if (sbinfo->si_xib)
38411 +               fput(sbinfo->si_xib);
38412 +       sbinfo->si_xib = NULL;
38413 +       if (sbinfo->si_xib_buf)
38414 +               free_page((unsigned long)sbinfo->si_xib_buf);
38415 +       sbinfo->si_xib_buf = NULL;
38416 +}
38417 +
38418 +static int au_xino_set_xib(struct super_block *sb, struct path *path)
38419 +{
38420 +       int err;
38421 +       loff_t pos;
38422 +       struct au_sbinfo *sbinfo;
38423 +       struct file *file;
38424 +       struct super_block *xi_sb;
38425 +
38426 +       SiMustWriteLock(sb);
38427 +
38428 +       sbinfo = au_sbi(sb);
38429 +       file = au_xino_create2(sb, path, sbinfo->si_xib);
38430 +       err = PTR_ERR(file);
38431 +       if (IS_ERR(file))
38432 +               goto out;
38433 +       if (sbinfo->si_xib)
38434 +               fput(sbinfo->si_xib);
38435 +       sbinfo->si_xib = file;
38436 +       xi_sb = file_inode(file)->i_sb;
38437 +       sbinfo->si_ximaxent = xi_sb->s_maxbytes;
38438 +       if (unlikely(sbinfo->si_ximaxent < PAGE_SIZE)) {
38439 +               err = -EIO;
38440 +               pr_err("s_maxbytes(%llu) on %s is too small\n",
38441 +                      (u64)sbinfo->si_ximaxent, au_sbtype(xi_sb));
38442 +               goto out_unset;
38443 +       }
38444 +       sbinfo->si_ximaxent /= sizeof(ino_t);
38445 +
38446 +       err = -ENOMEM;
38447 +       if (!sbinfo->si_xib_buf)
38448 +               sbinfo->si_xib_buf = (void *)get_zeroed_page(GFP_NOFS);
38449 +       if (unlikely(!sbinfo->si_xib_buf))
38450 +               goto out_unset;
38451 +
38452 +       sbinfo->si_xib_last_pindex = 0;
38453 +       sbinfo->si_xib_next_bit = 0;
38454 +       if (vfsub_f_size_read(file) < PAGE_SIZE) {
38455 +               pos = 0;
38456 +               err = xino_fwrite(file, sbinfo->si_xib_buf, PAGE_SIZE, &pos);
38457 +               if (unlikely(err != PAGE_SIZE))
38458 +                       goto out_free;
38459 +       }
38460 +       err = 0;
38461 +       goto out; /* success */
38462 +
38463 +out_free:
38464 +       if (sbinfo->si_xib_buf)
38465 +               free_page((unsigned long)sbinfo->si_xib_buf);
38466 +       sbinfo->si_xib_buf = NULL;
38467 +       if (err >= 0)
38468 +               err = -EIO;
38469 +out_unset:
38470 +       fput(sbinfo->si_xib);
38471 +       sbinfo->si_xib = NULL;
38472 +out:
38473 +       AuTraceErr(err);
38474 +       return err;
38475 +}
38476 +
38477 +/* xino for each branch */
38478 +static void xino_clear_br(struct super_block *sb)
38479 +{
38480 +       aufs_bindex_t bindex, bbot;
38481 +       struct au_branch *br;
38482 +
38483 +       bbot = au_sbbot(sb);
38484 +       for (bindex = 0; bindex <= bbot; bindex++) {
38485 +               br = au_sbr(sb, bindex);
38486 +               AuDebugOn(!br);
38487 +               au_xino_put(br);
38488 +       }
38489 +}
38490 +
38491 +static void au_xino_set_br_shared(struct super_block *sb, struct au_branch *br,
38492 +                                 aufs_bindex_t bshared)
38493 +{
38494 +       struct au_branch *brshared;
38495 +
38496 +       brshared = au_sbr(sb, bshared);
38497 +       AuDebugOn(!brshared->br_xino);
38498 +       AuDebugOn(!brshared->br_xino->xi_file);
38499 +       if (br->br_xino != brshared->br_xino) {
38500 +               au_xino_get(brshared);
38501 +               au_xino_put(br);
38502 +               br->br_xino = brshared->br_xino;
38503 +       }
38504 +}
38505 +
38506 +struct au_xino_do_set_br {
38507 +       struct au_branch *br;
38508 +       ino_t h_ino;
38509 +       aufs_bindex_t bshared;
38510 +};
38511 +
38512 +static int au_xino_do_set_br(struct super_block *sb, struct path *path,
38513 +                            struct au_xino_do_set_br *args)
38514 +{
38515 +       int err;
38516 +       struct au_xi_calc calc;
38517 +       struct file *file;
38518 +       struct au_branch *br;
38519 +       struct au_xi_new xinew = {
38520 +               .base = path
38521 +       };
38522 +
38523 +       br = args->br;
38524 +       xinew.xi = br->br_xino;
38525 +       au_xi_calc(sb, args->h_ino, &calc);
38526 +       xinew.copy_src = au_xino_file(xinew.xi, calc.idx);
38527 +       if (args->bshared >= 0)
38528 +               /* shared xino */
38529 +               au_xino_set_br_shared(sb, br, args->bshared);
38530 +       else if (!xinew.xi) {
38531 +               /* new xino */
38532 +               err = au_xino_init(br, calc.idx, xinew.copy_src);
38533 +               if (unlikely(err))
38534 +                       goto out;
38535 +       }
38536 +
38537 +       /* force re-creating */
38538 +       xinew.xi = br->br_xino;
38539 +       xinew.idx = calc.idx;
38540 +       mutex_lock(&xinew.xi->xi_mtx);
38541 +       file = au_xi_new(sb, &xinew);
38542 +       mutex_unlock(&xinew.xi->xi_mtx);
38543 +       err = PTR_ERR(file);
38544 +       if (IS_ERR(file))
38545 +               goto out;
38546 +       AuDebugOn(!file);
38547 +
38548 +       err = au_xino_do_write(file, &calc, AUFS_ROOT_INO);
38549 +       if (unlikely(err))
38550 +               au_xino_put(br);
38551 +
38552 +out:
38553 +       AuTraceErr(err);
38554 +       return err;
38555 +}
38556 +
38557 +static int au_xino_set_br(struct super_block *sb, struct path *path)
38558 +{
38559 +       int err;
38560 +       aufs_bindex_t bindex, bbot;
38561 +       struct au_xino_do_set_br args;
38562 +       struct inode *inode;
38563 +
38564 +       SiMustWriteLock(sb);
38565 +
38566 +       bbot = au_sbbot(sb);
38567 +       inode = d_inode(sb->s_root);
38568 +       for (bindex = 0; bindex <= bbot; bindex++) {
38569 +               args.h_ino = au_h_iptr(inode, bindex)->i_ino;
38570 +               args.br = au_sbr(sb, bindex);
38571 +               args.bshared = is_sb_shared(sb, bindex, bindex - 1);
38572 +               err = au_xino_do_set_br(sb, path, &args);
38573 +               if (unlikely(err))
38574 +                       break;
38575 +       }
38576 +
38577 +       AuTraceErr(err);
38578 +       return err;
38579 +}
38580 +
38581 +void au_xino_clr(struct super_block *sb)
38582 +{
38583 +       struct au_sbinfo *sbinfo;
38584 +
38585 +       au_xigen_clr(sb);
38586 +       xino_clear_xib(sb);
38587 +       xino_clear_br(sb);
38588 +       dbgaufs_brs_del(sb, 0);
38589 +       sbinfo = au_sbi(sb);
38590 +       /* lvalue, do not call au_mntflags() */
38591 +       au_opt_clr(sbinfo->si_mntflags, XINO);
38592 +}
38593 +
38594 +int au_xino_set(struct super_block *sb, struct au_opt_xino *xiopt, int remount)
38595 +{
38596 +       int err, skip;
38597 +       struct dentry *dentry, *parent, *cur_dentry, *cur_parent;
38598 +       struct qstr *dname, *cur_name;
38599 +       struct file *cur_xino;
38600 +       struct au_sbinfo *sbinfo;
38601 +       struct path *path, *cur_path;
38602 +
38603 +       SiMustWriteLock(sb);
38604 +
38605 +       err = 0;
38606 +       sbinfo = au_sbi(sb);
38607 +       path = &xiopt->file->f_path;
38608 +       dentry = path->dentry;
38609 +       parent = dget_parent(dentry);
38610 +       if (remount) {
38611 +               skip = 0;
38612 +               cur_xino = sbinfo->si_xib;
38613 +               if (cur_xino) {
38614 +                       cur_path = &cur_xino->f_path;
38615 +                       cur_dentry = cur_path->dentry;
38616 +                       cur_parent = dget_parent(cur_dentry);
38617 +                       cur_name = &cur_dentry->d_name;
38618 +                       dname = &dentry->d_name;
38619 +                       skip = (cur_parent == parent
38620 +                               && au_qstreq(dname, cur_name));
38621 +                       dput(cur_parent);
38622 +               }
38623 +               if (skip)
38624 +                       goto out;
38625 +       }
38626 +
38627 +       au_opt_set(sbinfo->si_mntflags, XINO);
38628 +       err = au_xino_set_xib(sb, path);
38629 +       /* si_x{read,write} are set */
38630 +       if (!err)
38631 +               err = au_xigen_set(sb, path);
38632 +       if (!err)
38633 +               err = au_xino_set_br(sb, path);
38634 +       if (!err) {
38635 +               dbgaufs_brs_add(sb, 0, /*topdown*/1);
38636 +               goto out; /* success */
38637 +       }
38638 +
38639 +       /* reset all */
38640 +       AuIOErr("failed setting xino(%d).\n", err);
38641 +       au_xino_clr(sb);
38642 +
38643 +out:
38644 +       dput(parent);
38645 +       return err;
38646 +}
38647 +
38648 +/*
38649 + * create a xinofile at the default place/path.
38650 + */
38651 +struct file *au_xino_def(struct super_block *sb)
38652 +{
38653 +       struct file *file;
38654 +       char *page, *p;
38655 +       struct au_branch *br;
38656 +       struct super_block *h_sb;
38657 +       struct path path;
38658 +       aufs_bindex_t bbot, bindex, bwr;
38659 +
38660 +       br = NULL;
38661 +       bbot = au_sbbot(sb);
38662 +       bwr = -1;
38663 +       for (bindex = 0; bindex <= bbot; bindex++) {
38664 +               br = au_sbr(sb, bindex);
38665 +               if (au_br_writable(br->br_perm)
38666 +                   && !au_test_fs_bad_xino(au_br_sb(br))) {
38667 +                       bwr = bindex;
38668 +                       break;
38669 +               }
38670 +       }
38671 +
38672 +       if (bwr >= 0) {
38673 +               file = ERR_PTR(-ENOMEM);
38674 +               page = (void *)__get_free_page(GFP_NOFS);
38675 +               if (unlikely(!page))
38676 +                       goto out;
38677 +               path.mnt = au_br_mnt(br);
38678 +               path.dentry = au_h_dptr(sb->s_root, bwr);
38679 +               p = d_path(&path, page, PATH_MAX - sizeof(AUFS_XINO_FNAME));
38680 +               file = (void *)p;
38681 +               if (!IS_ERR(p)) {
38682 +                       strcat(p, "/" AUFS_XINO_FNAME);
38683 +                       AuDbg("%s\n", p);
38684 +                       file = au_xino_create(sb, p, /*silent*/0, /*wbrtop*/1);
38685 +               }
38686 +               free_page((unsigned long)page);
38687 +       } else {
38688 +               file = au_xino_create(sb, AUFS_XINO_DEFPATH, /*silent*/0,
38689 +                                     /*wbrtop*/0);
38690 +               if (IS_ERR(file))
38691 +                       goto out;
38692 +               h_sb = file->f_path.dentry->d_sb;
38693 +               if (unlikely(au_test_fs_bad_xino(h_sb))) {
38694 +                       pr_err("xino doesn't support %s(%s)\n",
38695 +                              AUFS_XINO_DEFPATH, au_sbtype(h_sb));
38696 +                       fput(file);
38697 +                       file = ERR_PTR(-EINVAL);
38698 +               }
38699 +       }
38700 +
38701 +out:
38702 +       return file;
38703 +}
38704 +
38705 +/* ---------------------------------------------------------------------- */
38706 +
38707 +/*
38708 + * initialize the xinofile for the specified branch @br
38709 + * at the place/path where @base_file indicates.
38710 + * test whether another branch is on the same filesystem or not,
38711 + * if found then share the xinofile with another branch.
38712 + */
38713 +int au_xino_init_br(struct super_block *sb, struct au_branch *br, ino_t h_ino,
38714 +                   struct path *base)
38715 +{
38716 +       int err;
38717 +       struct au_xino_do_set_br args = {
38718 +               .h_ino  = h_ino,
38719 +               .br     = br
38720 +       };
38721 +
38722 +       args.bshared = sbr_find_shared(sb, /*btop*/0, au_sbbot(sb),
38723 +                                      au_br_sb(br));
38724 +       err = au_xino_do_set_br(sb, base, &args);
38725 +       if (unlikely(err))
38726 +               au_xino_put(br);
38727 +
38728 +       return err;
38729 +}
38730 +
38731 +/* ---------------------------------------------------------------------- */
38732 +
38733 +/*
38734 + * get an unused inode number from bitmap
38735 + */
38736 +ino_t au_xino_new_ino(struct super_block *sb)
38737 +{
38738 +       ino_t ino;
38739 +       unsigned long *p, pindex, ul, pend;
38740 +       struct au_sbinfo *sbinfo;
38741 +       struct file *file;
38742 +       int free_bit, err;
38743 +
38744 +       if (!au_opt_test(au_mntflags(sb), XINO))
38745 +               return iunique(sb, AUFS_FIRST_INO);
38746 +
38747 +       sbinfo = au_sbi(sb);
38748 +       mutex_lock(&sbinfo->si_xib_mtx);
38749 +       p = sbinfo->si_xib_buf;
38750 +       free_bit = sbinfo->si_xib_next_bit;
38751 +       if (free_bit < page_bits && !test_bit(free_bit, p))
38752 +               goto out; /* success */
38753 +       free_bit = find_first_zero_bit(p, page_bits);
38754 +       if (free_bit < page_bits)
38755 +               goto out; /* success */
38756 +
38757 +       pindex = sbinfo->si_xib_last_pindex;
38758 +       for (ul = pindex - 1; ul < ULONG_MAX; ul--) {
38759 +               err = xib_pindex(sb, ul);
38760 +               if (unlikely(err))
38761 +                       goto out_err;
38762 +               free_bit = find_first_zero_bit(p, page_bits);
38763 +               if (free_bit < page_bits)
38764 +                       goto out; /* success */
38765 +       }
38766 +
38767 +       file = sbinfo->si_xib;
38768 +       pend = vfsub_f_size_read(file) / PAGE_SIZE;
38769 +       for (ul = pindex + 1; ul <= pend; ul++) {
38770 +               err = xib_pindex(sb, ul);
38771 +               if (unlikely(err))
38772 +                       goto out_err;
38773 +               free_bit = find_first_zero_bit(p, page_bits);
38774 +               if (free_bit < page_bits)
38775 +                       goto out; /* success */
38776 +       }
38777 +       BUG();
38778 +
38779 +out:
38780 +       set_bit(free_bit, p);
38781 +       sbinfo->si_xib_next_bit = free_bit + 1;
38782 +       pindex = sbinfo->si_xib_last_pindex;
38783 +       mutex_unlock(&sbinfo->si_xib_mtx);
38784 +       ino = xib_calc_ino(pindex, free_bit);
38785 +       AuDbg("i%lu\n", (unsigned long)ino);
38786 +       return ino;
38787 +out_err:
38788 +       mutex_unlock(&sbinfo->si_xib_mtx);
38789 +       AuDbg("i0\n");
38790 +       return 0;
38791 +}
38792 +
38793 +/* for s_op->delete_inode() */
38794 +void au_xino_delete_inode(struct inode *inode, const int unlinked)
38795 +{
38796 +       int err;
38797 +       unsigned int mnt_flags;
38798 +       aufs_bindex_t bindex, bbot, bi;
38799 +       unsigned char try_trunc;
38800 +       struct au_iinfo *iinfo;
38801 +       struct super_block *sb;
38802 +       struct au_hinode *hi;
38803 +       struct inode *h_inode;
38804 +       struct au_branch *br;
38805 +       struct au_xi_calc calc;
38806 +       struct file *file;
38807 +
38808 +       AuDebugOn(au_is_bad_inode(inode));
38809 +
38810 +       sb = inode->i_sb;
38811 +       mnt_flags = au_mntflags(sb);
38812 +       if (!au_opt_test(mnt_flags, XINO)
38813 +           || inode->i_ino == AUFS_ROOT_INO)
38814 +               return;
38815 +
38816 +       if (unlinked) {
38817 +               au_xigen_inc(inode);
38818 +               au_xib_clear_bit(inode);
38819 +       }
38820 +
38821 +       iinfo = au_ii(inode);
38822 +       bindex = iinfo->ii_btop;
38823 +       if (bindex < 0)
38824 +               return;
38825 +
38826 +       try_trunc = !!au_opt_test(mnt_flags, TRUNC_XINO);
38827 +       hi = au_hinode(iinfo, bindex);
38828 +       bbot = iinfo->ii_bbot;
38829 +       for (; bindex <= bbot; bindex++, hi++) {
38830 +               h_inode = hi->hi_inode;
38831 +               if (!h_inode
38832 +                   || (!unlinked && h_inode->i_nlink))
38833 +                       continue;
38834 +
38835 +               /* inode may not be revalidated */
38836 +               bi = au_br_index(sb, hi->hi_id);
38837 +               if (bi < 0)
38838 +                       continue;
38839 +
38840 +               br = au_sbr(sb, bi);
38841 +               au_xi_calc(sb, h_inode->i_ino, &calc);
38842 +               file = au_xino_file(br->br_xino, calc.idx);
38843 +               if (IS_ERR_OR_NULL(file))
38844 +                       continue;
38845 +
38846 +               err = au_xino_do_write(file, &calc, /*ino*/0);
38847 +               if (!err && try_trunc
38848 +                   && au_test_fs_trunc_xino(au_br_sb(br)))
38849 +                       xino_try_trunc(sb, br);
38850 +       }
38851 +}
38852 +
38853 +/* ---------------------------------------------------------------------- */
38854 +
38855 +static int au_xinondir_find(struct au_xino *xi, ino_t h_ino)
38856 +{
38857 +       int found, total, i;
38858 +
38859 +       found = -1;
38860 +       total = xi->xi_nondir.total;
38861 +       for (i = 0; i < total; i++) {
38862 +               if (xi->xi_nondir.array[i] != h_ino)
38863 +                       continue;
38864 +               found = i;
38865 +               break;
38866 +       }
38867 +
38868 +       return found;
38869 +}
38870 +
38871 +static int au_xinondir_expand(struct au_xino *xi)
38872 +{
38873 +       int err, sz;
38874 +       ino_t *p;
38875 +
38876 +       BUILD_BUG_ON(KMALLOC_MAX_SIZE > INT_MAX);
38877 +
38878 +       err = -ENOMEM;
38879 +       sz = xi->xi_nondir.total * sizeof(ino_t);
38880 +       if (unlikely(sz > KMALLOC_MAX_SIZE / 2))
38881 +               goto out;
38882 +       p = au_kzrealloc(xi->xi_nondir.array, sz, sz << 1, GFP_ATOMIC,
38883 +                        /*may_shrink*/0);
38884 +       if (p) {
38885 +               xi->xi_nondir.array = p;
38886 +               xi->xi_nondir.total <<= 1;
38887 +               AuDbg("xi_nondir.total %d\n", xi->xi_nondir.total);
38888 +               err = 0;
38889 +       }
38890 +
38891 +out:
38892 +       return err;
38893 +}
38894 +
38895 +void au_xinondir_leave(struct super_block *sb, aufs_bindex_t bindex,
38896 +                      ino_t h_ino, int idx)
38897 +{
38898 +       struct au_xino *xi;
38899 +
38900 +       AuDebugOn(!au_opt_test(au_mntflags(sb), XINO));
38901 +       xi = au_sbr(sb, bindex)->br_xino;
38902 +       AuDebugOn(idx < 0 || xi->xi_nondir.total <= idx);
38903 +
38904 +       spin_lock(&xi->xi_nondir.spin);
38905 +       AuDebugOn(xi->xi_nondir.array[idx] != h_ino);
38906 +       xi->xi_nondir.array[idx] = 0;
38907 +       spin_unlock(&xi->xi_nondir.spin);
38908 +       wake_up_all(&xi->xi_nondir.wqh);
38909 +}
38910 +
38911 +int au_xinondir_enter(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
38912 +                     int *idx)
38913 +{
38914 +       int err, found, empty;
38915 +       struct au_xino *xi;
38916 +
38917 +       err = 0;
38918 +       *idx = -1;
38919 +       if (!au_opt_test(au_mntflags(sb), XINO))
38920 +               goto out; /* no xino */
38921 +
38922 +       xi = au_sbr(sb, bindex)->br_xino;
38923 +
38924 +again:
38925 +       spin_lock(&xi->xi_nondir.spin);
38926 +       found = au_xinondir_find(xi, h_ino);
38927 +       if (found == -1) {
38928 +               empty = au_xinondir_find(xi, /*h_ino*/0);
38929 +               if (empty == -1) {
38930 +                       empty = xi->xi_nondir.total;
38931 +                       err = au_xinondir_expand(xi);
38932 +                       if (unlikely(err))
38933 +                               goto out_unlock;
38934 +               }
38935 +               xi->xi_nondir.array[empty] = h_ino;
38936 +               *idx = empty;
38937 +       } else {
38938 +               spin_unlock(&xi->xi_nondir.spin);
38939 +               wait_event(xi->xi_nondir.wqh,
38940 +                          xi->xi_nondir.array[found] != h_ino);
38941 +               goto again;
38942 +       }
38943 +
38944 +out_unlock:
38945 +       spin_unlock(&xi->xi_nondir.spin);
38946 +out:
38947 +       return err;
38948 +}
38949 +
38950 +/* ---------------------------------------------------------------------- */
38951 +
38952 +int au_xino_path(struct seq_file *seq, struct file *file)
38953 +{
38954 +       int err;
38955 +
38956 +       err = au_seq_path(seq, &file->f_path);
38957 +       if (unlikely(err))
38958 +               goto out;
38959 +
38960 +#define Deleted "\\040(deleted)"
38961 +       seq->count -= sizeof(Deleted) - 1;
38962 +       AuDebugOn(memcmp(seq->buf + seq->count, Deleted,
38963 +                        sizeof(Deleted) - 1));
38964 +#undef Deleted
38965 +
38966 +out:
38967 +       return err;
38968 +}
38969 diff -urN /usr/share/empty/include/uapi/linux/aufs_type.h linux/include/uapi/linux/aufs_type.h
38970 --- /usr/share/empty/include/uapi/linux/aufs_type.h     1970-01-01 01:00:00.000000000 +0100
38971 +++ linux/include/uapi/linux/aufs_type.h        2022-05-23 09:44:37.729839166 +0200
38972 @@ -0,0 +1,452 @@
38973 +/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
38974 +/*
38975 + * Copyright (C) 2005-2021 Junjiro R. Okajima
38976 + *
38977 + * This program, aufs is free software; you can redistribute it and/or modify
38978 + * it under the terms of the GNU General Public License as published by
38979 + * the Free Software Foundation; either version 2 of the License, or
38980 + * (at your option) any later version.
38981 + *
38982 + * This program is distributed in the hope that it will be useful,
38983 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
38984 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
38985 + * GNU General Public License for more details.
38986 + *
38987 + * You should have received a copy of the GNU General Public License
38988 + * along with this program.  If not, see <http://www.gnu.org/licenses/>.
38989 + */
38990 +
38991 +#ifndef __AUFS_TYPE_H__
38992 +#define __AUFS_TYPE_H__
38993 +
38994 +#define AUFS_NAME      "aufs"
38995 +
38996 +#ifdef __KERNEL__
38997 +/*
38998 + * define it before including all other headers.
38999 + * sched.h may use pr_* macros before defining "current", so define the
39000 + * no-current version first, and re-define later.
39001 + */
39002 +#define pr_fmt(fmt)    AUFS_NAME " %s:%d: " fmt, __func__, __LINE__
39003 +#include <linux/sched.h>
39004 +#undef pr_fmt
39005 +#define pr_fmt(fmt) \
39006 +               AUFS_NAME " %s:%d:%.*s[%d]: " fmt, __func__, __LINE__, \
39007 +               (int)sizeof(current->comm), current->comm, current->pid
39008 +#include <linux/limits.h>
39009 +#else
39010 +#include <stdint.h>
39011 +#include <sys/types.h>
39012 +#include <limits.h>
39013 +#endif /* __KERNEL__ */
39014 +
39015 +#define AUFS_VERSION   "5.x-rcN-20220418"
39016 +
39017 +/* todo? move this to linux-2.6.19/include/magic.h */
39018 +#define AUFS_SUPER_MAGIC       ('a' << 24 | 'u' << 16 | 'f' << 8 | 's')
39019 +
39020 +/* ---------------------------------------------------------------------- */
39021 +
39022 +#ifdef __KERNEL__
39023 +#ifdef CONFIG_AUFS_BRANCH_MAX_127
39024 +typedef int8_t aufs_bindex_t;
39025 +#define AUFS_BRANCH_MAX 127
39026 +#else
39027 +typedef int16_t aufs_bindex_t;
39028 +#ifdef CONFIG_AUFS_BRANCH_MAX_511
39029 +#define AUFS_BRANCH_MAX 511
39030 +#elif defined(CONFIG_AUFS_BRANCH_MAX_1023)
39031 +#define AUFS_BRANCH_MAX 1023
39032 +#elif defined(CONFIG_AUFS_BRANCH_MAX_32767)
39033 +#define AUFS_BRANCH_MAX 32767
39034 +#endif
39035 +#endif
39036 +
39037 +#ifndef AUFS_BRANCH_MAX
39038 +#error unknown CONFIG_AUFS_BRANCH_MAX value
39039 +#endif
39040 +#endif /* __KERNEL__ */
39041 +
39042 +/* ---------------------------------------------------------------------- */
39043 +
39044 +#define AUFS_FSTYPE            AUFS_NAME
39045 +
39046 +#define AUFS_ROOT_INO          2
39047 +#define AUFS_FIRST_INO         11
39048 +
39049 +#define AUFS_WH_PFX            ".wh."
39050 +#define AUFS_WH_PFX_LEN                ((int)sizeof(AUFS_WH_PFX) - 1)
39051 +#define AUFS_WH_TMP_LEN                4
39052 +/* a limit for rmdir/rename a dir and copyup */
39053 +#define AUFS_MAX_NAMELEN       (NAME_MAX \
39054 +                               - AUFS_WH_PFX_LEN * 2   /* doubly whiteouted */\
39055 +                               - 1                     /* dot */\
39056 +                               - AUFS_WH_TMP_LEN)      /* hex */
39057 +#define AUFS_XINO_FNAME                "." AUFS_NAME ".xino"
39058 +#define AUFS_XINO_DEFPATH      "/tmp/" AUFS_XINO_FNAME
39059 +#define AUFS_XINO_DEF_SEC      30 /* seconds */
39060 +#define AUFS_XINO_DEF_TRUNC    45 /* percentage */
39061 +#define AUFS_DIRWH_DEF         3
39062 +#define AUFS_RDCACHE_DEF       10 /* seconds */
39063 +#define AUFS_RDCACHE_MAX       3600 /* seconds */
39064 +#define AUFS_RDBLK_DEF         512 /* bytes */
39065 +#define AUFS_RDHASH_DEF                32
39066 +#define AUFS_WKQ_NAME          AUFS_NAME "d"
39067 +#define AUFS_MFS_DEF_SEC       30 /* seconds */
39068 +#define AUFS_MFS_MAX_SEC       3600 /* seconds */
39069 +#define AUFS_FHSM_CACHE_DEF_SEC        30 /* seconds */
39070 +#define AUFS_PLINK_WARN                50 /* number of plinks in a single bucket */
39071 +
39072 +/* pseudo-link maintenace under /proc */
39073 +#define AUFS_PLINK_MAINT_NAME  "plink_maint"
39074 +#define AUFS_PLINK_MAINT_DIR   "fs/" AUFS_NAME
39075 +#define AUFS_PLINK_MAINT_PATH  AUFS_PLINK_MAINT_DIR "/" AUFS_PLINK_MAINT_NAME
39076 +
39077 +/* dirren, renamed dir */
39078 +#define AUFS_DR_INFO_PFX       AUFS_WH_PFX ".dr."
39079 +#define AUFS_DR_BRHINO_NAME    AUFS_WH_PFX "hino"
39080 +/* whiteouted doubly */
39081 +#define AUFS_WH_DR_INFO_PFX    AUFS_WH_PFX AUFS_DR_INFO_PFX
39082 +#define AUFS_WH_DR_BRHINO      AUFS_WH_PFX AUFS_DR_BRHINO_NAME
39083 +
39084 +#define AUFS_DIROPQ_NAME       AUFS_WH_PFX ".opq" /* whiteouted doubly */
39085 +#define AUFS_WH_DIROPQ         AUFS_WH_PFX AUFS_DIROPQ_NAME
39086 +
39087 +#define AUFS_BASE_NAME         AUFS_WH_PFX AUFS_NAME
39088 +#define AUFS_PLINKDIR_NAME     AUFS_WH_PFX "plnk"
39089 +#define AUFS_ORPHDIR_NAME      AUFS_WH_PFX "orph"
39090 +
39091 +/* doubly whiteouted */
39092 +#define AUFS_WH_BASE           AUFS_WH_PFX AUFS_BASE_NAME
39093 +#define AUFS_WH_PLINKDIR       AUFS_WH_PFX AUFS_PLINKDIR_NAME
39094 +#define AUFS_WH_ORPHDIR                AUFS_WH_PFX AUFS_ORPHDIR_NAME
39095 +
39096 +/* branch permissions and attributes */
39097 +#define AUFS_BRPERM_RW         "rw"
39098 +#define AUFS_BRPERM_RO         "ro"
39099 +#define AUFS_BRPERM_RR         "rr"
39100 +#define AUFS_BRATTR_COO_REG    "coo_reg"
39101 +#define AUFS_BRATTR_COO_ALL    "coo_all"
39102 +#define AUFS_BRATTR_FHSM       "fhsm"
39103 +#define AUFS_BRATTR_UNPIN      "unpin"
39104 +#define AUFS_BRATTR_ICEX       "icex"
39105 +#define AUFS_BRATTR_ICEX_SEC   "icexsec"
39106 +#define AUFS_BRATTR_ICEX_SYS   "icexsys"
39107 +#define AUFS_BRATTR_ICEX_TR    "icextr"
39108 +#define AUFS_BRATTR_ICEX_USR   "icexusr"
39109 +#define AUFS_BRATTR_ICEX_OTH   "icexoth"
39110 +#define AUFS_BRRATTR_WH                "wh"
39111 +#define AUFS_BRWATTR_NLWH      "nolwh"
39112 +#define AUFS_BRWATTR_MOO       "moo"
39113 +
39114 +#define AuBrPerm_RW            1               /* writable, hardlinkable wh */
39115 +#define AuBrPerm_RO            (1 << 1)        /* readonly */
39116 +#define AuBrPerm_RR            (1 << 2)        /* natively readonly */
39117 +#define AuBrPerm_Mask          (AuBrPerm_RW | AuBrPerm_RO | AuBrPerm_RR)
39118 +
39119 +#define AuBrAttr_COO_REG       (1 << 3)        /* copy-up on open */
39120 +#define AuBrAttr_COO_ALL       (1 << 4)
39121 +#define AuBrAttr_COO_Mask      (AuBrAttr_COO_REG | AuBrAttr_COO_ALL)
39122 +
39123 +#define AuBrAttr_FHSM          (1 << 5)        /* file-based hsm */
39124 +#define AuBrAttr_UNPIN         (1 << 6)        /* rename-able top dir of
39125 +                                                  branch. meaningless since
39126 +                                                  linux-3.18-rc1 */
39127 +
39128 +/* ignore error in copying XATTR */
39129 +#define AuBrAttr_ICEX_SEC      (1 << 7)
39130 +#define AuBrAttr_ICEX_SYS      (1 << 8)
39131 +#define AuBrAttr_ICEX_TR       (1 << 9)
39132 +#define AuBrAttr_ICEX_USR      (1 << 10)
39133 +#define AuBrAttr_ICEX_OTH      (1 << 11)
39134 +#define AuBrAttr_ICEX          (AuBrAttr_ICEX_SEC      \
39135 +                                | AuBrAttr_ICEX_SYS    \
39136 +                                | AuBrAttr_ICEX_TR     \
39137 +                                | AuBrAttr_ICEX_USR    \
39138 +                                | AuBrAttr_ICEX_OTH)
39139 +
39140 +#define AuBrRAttr_WH           (1 << 12)       /* whiteout-able */
39141 +#define AuBrRAttr_Mask         AuBrRAttr_WH
39142 +
39143 +#define AuBrWAttr_NoLinkWH     (1 << 13)       /* un-hardlinkable whiteouts */
39144 +#define AuBrWAttr_MOO          (1 << 14)       /* move-up on open */
39145 +#define AuBrWAttr_Mask         (AuBrWAttr_NoLinkWH | AuBrWAttr_MOO)
39146 +
39147 +#define AuBrAttr_CMOO_Mask     (AuBrAttr_COO_Mask | AuBrWAttr_MOO)
39148 +
39149 +/* #warning test userspace */
39150 +#ifdef __KERNEL__
39151 +#ifndef CONFIG_AUFS_FHSM
39152 +#undef AuBrAttr_FHSM
39153 +#define AuBrAttr_FHSM          0
39154 +#endif
39155 +#ifndef CONFIG_AUFS_XATTR
39156 +#undef AuBrAttr_ICEX
39157 +#define AuBrAttr_ICEX          0
39158 +#undef AuBrAttr_ICEX_SEC
39159 +#define AuBrAttr_ICEX_SEC      0
39160 +#undef AuBrAttr_ICEX_SYS
39161 +#define AuBrAttr_ICEX_SYS      0
39162 +#undef AuBrAttr_ICEX_TR
39163 +#define AuBrAttr_ICEX_TR       0
39164 +#undef AuBrAttr_ICEX_USR
39165 +#define AuBrAttr_ICEX_USR      0
39166 +#undef AuBrAttr_ICEX_OTH
39167 +#define AuBrAttr_ICEX_OTH      0
39168 +#endif
39169 +#endif
39170 +
39171 +/* the longest combination */
39172 +/* AUFS_BRATTR_ICEX and AUFS_BRATTR_ICEX_TR don't affect here */
39173 +#define AuBrPermStrSz  sizeof(AUFS_BRPERM_RW                   \
39174 +                              "+" AUFS_BRATTR_COO_REG          \
39175 +                              "+" AUFS_BRATTR_FHSM             \
39176 +                              "+" AUFS_BRATTR_UNPIN            \
39177 +                              "+" AUFS_BRATTR_ICEX_SEC         \
39178 +                              "+" AUFS_BRATTR_ICEX_SYS         \
39179 +                              "+" AUFS_BRATTR_ICEX_USR         \
39180 +                              "+" AUFS_BRATTR_ICEX_OTH         \
39181 +                              "+" AUFS_BRWATTR_NLWH)
39182 +
39183 +typedef struct {
39184 +       char a[AuBrPermStrSz];
39185 +} au_br_perm_str_t;
39186 +
39187 +static inline int au_br_writable(int brperm)
39188 +{
39189 +       return brperm & AuBrPerm_RW;
39190 +}
39191 +
39192 +static inline int au_br_whable(int brperm)
39193 +{
39194 +       return brperm & (AuBrPerm_RW | AuBrRAttr_WH);
39195 +}
39196 +
39197 +static inline int au_br_wh_linkable(int brperm)
39198 +{
39199 +       return !(brperm & AuBrWAttr_NoLinkWH);
39200 +}
39201 +
39202 +static inline int au_br_cmoo(int brperm)
39203 +{
39204 +       return brperm & AuBrAttr_CMOO_Mask;
39205 +}
39206 +
39207 +static inline int au_br_fhsm(int brperm)
39208 +{
39209 +       return brperm & AuBrAttr_FHSM;
39210 +}
39211 +
39212 +/* ---------------------------------------------------------------------- */
39213 +
39214 +/* ioctl */
39215 +enum {
39216 +       /* readdir in userspace */
39217 +       AuCtl_RDU,
39218 +       AuCtl_RDU_INO,
39219 +
39220 +       AuCtl_WBR_FD,   /* pathconf wrapper */
39221 +       AuCtl_IBUSY,    /* busy inode */
39222 +       AuCtl_MVDOWN,   /* move-down */
39223 +       AuCtl_BR,       /* info about branches */
39224 +       AuCtl_FHSM_FD   /* connection for fhsm */
39225 +};
39226 +
39227 +/* borrowed from linux/include/linux/kernel.h */
39228 +#ifndef ALIGN
39229 +#ifdef _GNU_SOURCE
39230 +#define ALIGN(x, a)            __ALIGN_MASK(x, (typeof(x))(a)-1)
39231 +#else
39232 +#define ALIGN(x, a)            (((x) + (a) - 1) & ~((a) - 1))
39233 +#endif
39234 +#define __ALIGN_MASK(x, mask)  (((x)+(mask))&~(mask))
39235 +#endif
39236 +
39237 +/* borrowed from linux/include/linux/compiler-gcc3.h */
39238 +#ifndef __aligned
39239 +#define __aligned(x)                   __attribute__((aligned(x)))
39240 +#endif
39241 +
39242 +#ifdef __KERNEL__
39243 +#ifndef __packed
39244 +#define __packed                       __attribute__((packed))
39245 +#endif
39246 +#endif
39247 +
39248 +struct au_rdu_cookie {
39249 +       uint64_t        h_pos;
39250 +       int16_t         bindex;
39251 +       uint8_t         flags;
39252 +       uint8_t         pad;
39253 +       uint32_t        generation;
39254 +} __aligned(8);
39255 +
39256 +struct au_rdu_ent {
39257 +       uint64_t        ino;
39258 +       int16_t         bindex;
39259 +       uint8_t         type;
39260 +       uint8_t         nlen;
39261 +       uint8_t         wh;
39262 +       char            name[];
39263 +} __aligned(8);
39264 +
39265 +static inline int au_rdu_len(int nlen)
39266 +{
39267 +       /* include the terminating NULL */
39268 +       return ALIGN(sizeof(struct au_rdu_ent) + nlen + 1,
39269 +                    sizeof(uint64_t));
39270 +}
39271 +
39272 +union au_rdu_ent_ul {
39273 +       struct au_rdu_ent __user        *e;
39274 +       uint64_t                        ul;
39275 +};
39276 +
39277 +enum {
39278 +       AufsCtlRduV_SZ,
39279 +       AufsCtlRduV_End
39280 +};
39281 +
39282 +struct aufs_rdu {
39283 +       /* input */
39284 +       union {
39285 +               uint64_t        sz;     /* AuCtl_RDU */
39286 +               uint64_t        nent;   /* AuCtl_RDU_INO */
39287 +       };
39288 +       union au_rdu_ent_ul     ent;
39289 +       uint16_t                verify[AufsCtlRduV_End];
39290 +
39291 +       /* input/output */
39292 +       uint32_t                blk;
39293 +
39294 +       /* output */
39295 +       union au_rdu_ent_ul     tail;
39296 +       /* number of entries which were added in a single call */
39297 +       uint64_t                rent;
39298 +       uint8_t                 full;
39299 +       uint8_t                 shwh;
39300 +
39301 +       struct au_rdu_cookie    cookie;
39302 +} __aligned(8);
39303 +
39304 +/* ---------------------------------------------------------------------- */
39305 +
39306 +/* dirren. the branch is identified by the filename who contains this */
39307 +struct au_drinfo {
39308 +       uint64_t ino;
39309 +       union {
39310 +               uint8_t oldnamelen;
39311 +               uint64_t _padding;
39312 +       };
39313 +       uint8_t oldname[];
39314 +} __aligned(8);
39315 +
39316 +struct au_drinfo_fdata {
39317 +       uint32_t magic;
39318 +       struct au_drinfo drinfo;
39319 +} __aligned(8);
39320 +
39321 +#define AUFS_DRINFO_MAGIC_V1   ('a' << 24 | 'd' << 16 | 'r' << 8 | 0x01)
39322 +/* future */
39323 +#define AUFS_DRINFO_MAGIC_V2   ('a' << 24 | 'd' << 16 | 'r' << 8 | 0x02)
39324 +
39325 +/* ---------------------------------------------------------------------- */
39326 +
39327 +struct aufs_wbr_fd {
39328 +       uint32_t        oflags;
39329 +       int16_t         brid;
39330 +} __aligned(8);
39331 +
39332 +/* ---------------------------------------------------------------------- */
39333 +
39334 +struct aufs_ibusy {
39335 +       uint64_t        ino, h_ino;
39336 +       int16_t         bindex;
39337 +} __aligned(8);
39338 +
39339 +/* ---------------------------------------------------------------------- */
39340 +
39341 +/* error code for move-down */
39342 +/* the actual message strings are implemented in aufs-util.git */
39343 +enum {
39344 +       EAU_MVDOWN_OPAQUE = 1,
39345 +       EAU_MVDOWN_WHITEOUT,
39346 +       EAU_MVDOWN_UPPER,
39347 +       EAU_MVDOWN_BOTTOM,
39348 +       EAU_MVDOWN_NOUPPER,
39349 +       EAU_MVDOWN_NOLOWERBR,
39350 +       EAU_Last
39351 +};
39352 +
39353 +/* flags for move-down */
39354 +#define AUFS_MVDOWN_DMSG       1
39355 +#define AUFS_MVDOWN_OWLOWER    (1 << 1)        /* overwrite lower */
39356 +#define AUFS_MVDOWN_KUPPER     (1 << 2)        /* keep upper */
39357 +#define AUFS_MVDOWN_ROLOWER    (1 << 3)        /* do even if lower is RO */
39358 +#define AUFS_MVDOWN_ROLOWER_R  (1 << 4)        /* did on lower RO */
39359 +#define AUFS_MVDOWN_ROUPPER    (1 << 5)        /* do even if upper is RO */
39360 +#define AUFS_MVDOWN_ROUPPER_R  (1 << 6)        /* did on upper RO */
39361 +#define AUFS_MVDOWN_BRID_UPPER (1 << 7)        /* upper brid */
39362 +#define AUFS_MVDOWN_BRID_LOWER (1 << 8)        /* lower brid */
39363 +#define AUFS_MVDOWN_FHSM_LOWER (1 << 9)        /* find fhsm attr for lower */
39364 +#define AUFS_MVDOWN_STFS       (1 << 10)       /* req. stfs */
39365 +#define AUFS_MVDOWN_STFS_FAILED        (1 << 11)       /* output: stfs is unusable */
39366 +#define AUFS_MVDOWN_BOTTOM     (1 << 12)       /* output: no more lowers */
39367 +
39368 +/* index for move-down */
39369 +enum {
39370 +       AUFS_MVDOWN_UPPER,
39371 +       AUFS_MVDOWN_LOWER,
39372 +       AUFS_MVDOWN_NARRAY
39373 +};
39374 +
39375 +/*
39376 + * additional info of move-down
39377 + * number of free blocks and inodes.
39378 + * subset of struct kstatfs, but smaller and always 64bit.
39379 + */
39380 +struct aufs_stfs {
39381 +       uint64_t        f_blocks;
39382 +       uint64_t        f_bavail;
39383 +       uint64_t        f_files;
39384 +       uint64_t        f_ffree;
39385 +};
39386 +
39387 +struct aufs_stbr {
39388 +       int16_t                 brid;   /* optional input */
39389 +       int16_t                 bindex; /* output */
39390 +       struct aufs_stfs        stfs;   /* output when AUFS_MVDOWN_STFS set */
39391 +} __aligned(8);
39392 +
39393 +struct aufs_mvdown {
39394 +       uint32_t                flags;                  /* input/output */
39395 +       struct aufs_stbr        stbr[AUFS_MVDOWN_NARRAY]; /* input/output */
39396 +       int8_t                  au_errno;               /* output */
39397 +} __aligned(8);
39398 +
39399 +/* ---------------------------------------------------------------------- */
39400 +
39401 +union aufs_brinfo {
39402 +       /* PATH_MAX may differ between kernel-space and user-space */
39403 +       char    _spacer[4096];
39404 +       struct {
39405 +               int16_t id;
39406 +               int     perm;
39407 +               char    path[];
39408 +       };
39409 +} __aligned(8);
39410 +
39411 +/* ---------------------------------------------------------------------- */
39412 +
39413 +#define AuCtlType              'A'
39414 +#define AUFS_CTL_RDU           _IOWR(AuCtlType, AuCtl_RDU, struct aufs_rdu)
39415 +#define AUFS_CTL_RDU_INO       _IOWR(AuCtlType, AuCtl_RDU_INO, struct aufs_rdu)
39416 +#define AUFS_CTL_WBR_FD                _IOW(AuCtlType, AuCtl_WBR_FD, \
39417 +                                    struct aufs_wbr_fd)
39418 +#define AUFS_CTL_IBUSY         _IOWR(AuCtlType, AuCtl_IBUSY, struct aufs_ibusy)
39419 +#define AUFS_CTL_MVDOWN                _IOWR(AuCtlType, AuCtl_MVDOWN, \
39420 +                                     struct aufs_mvdown)
39421 +#define AUFS_CTL_BRINFO                _IOW(AuCtlType, AuCtl_BR, union aufs_brinfo)
39422 +#define AUFS_CTL_FHSM_FD       _IOW(AuCtlType, AuCtl_FHSM_FD, int)
39423 +
39424 +#endif /* __AUFS_TYPE_H__ */
39425 SPDX-License-Identifier: GPL-2.0
39426 aufs5.x-rcN loopback patch
39427
39428 diff --git a/drivers/block/loop.c b/drivers/block/loop.c
39429 index ba35d0d6847a..63e256b40842 100644
39430 --- a/drivers/block/loop.c
39431 +++ b/drivers/block/loop.c
39432 @@ -509,6 +509,15 @@ static inline void loop_update_dio(struct loop_device *lo)
39433                                 lo->use_dio);
39434  }
39435  
39436 +static struct file *loop_real_file(struct file *file)
39437 +{
39438 +       struct file *f = NULL;
39439 +
39440 +       if (file->f_path.dentry->d_sb->s_op->real_loop)
39441 +               f = file->f_path.dentry->d_sb->s_op->real_loop(file);
39442 +       return f;
39443 +}
39444 +
39445  static void loop_reread_partitions(struct loop_device *lo)
39446  {
39447         int rc;
39448 @@ -566,6 +575,7 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39449  {
39450         struct file *file = fget(arg);
39451         struct file *old_file;
39452 +       struct file *f, *virt_file = NULL, *old_virt_file;
39453         int error;
39454         bool partscan;
39455         bool is_loop;
39456 @@ -585,11 +595,19 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39457         if (!(lo->lo_flags & LO_FLAGS_READ_ONLY))
39458                 goto out_err;
39459  
39460 +       f = loop_real_file(file);
39461 +       if (f) {
39462 +               virt_file = file;
39463 +               file = f;
39464 +               get_file(file);
39465 +       }
39466 +
39467         error = loop_validate_file(file, bdev);
39468         if (error)
39469                 goto out_err;
39470  
39471         old_file = lo->lo_backing_file;
39472 +       old_virt_file = lo->lo_backing_virt_file;
39473  
39474         error = -EINVAL;
39475  
39476 @@ -602,6 +620,7 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39477         blk_mq_freeze_queue(lo->lo_queue);
39478         mapping_set_gfp_mask(old_file->f_mapping, lo->old_gfp_mask);
39479         lo->lo_backing_file = file;
39480 +       lo->lo_backing_virt_file = virt_file;
39481         lo->old_gfp_mask = mapping_gfp_mask(file->f_mapping);
39482         mapping_set_gfp_mask(file->f_mapping,
39483                              lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
39484 @@ -624,6 +643,8 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39485          * dependency.
39486          */
39487         fput(old_file);
39488 +       if (old_virt_file)
39489 +               fput(old_virt_file);
39490         if (partscan)
39491                 loop_reread_partitions(lo);
39492         return 0;
39493 @@ -632,6 +653,8 @@ static int loop_change_fd(struct loop_device *lo, struct block_device *bdev,
39494         loop_global_unlock(lo, is_loop);
39495  out_putf:
39496         fput(file);
39497 +       if (virt_file)
39498 +               fput(virt_file);
39499         return error;
39500  }
39501  
39502 @@ -970,6 +993,7 @@ static int loop_configure(struct loop_device *lo, fmode_t mode,
39503                           const struct loop_config *config)
39504  {
39505         struct file *file = fget(config->fd);
39506 +       struct file *f, *virt_file = NULL;
39507         struct inode *inode;
39508         struct address_space *mapping;
39509         int error;
39510 @@ -985,6 +1009,13 @@ static int loop_configure(struct loop_device *lo, fmode_t mode,
39511         /* This is safe, since we have a reference from open(). */
39512         __module_get(THIS_MODULE);
39513  
39514 +       f = loop_real_file(file);
39515 +       if (f) {
39516 +               virt_file = file;
39517 +               file = f;
39518 +               get_file(file);
39519 +       }
39520 +
39521         /*
39522          * If we don't hold exclusive handle for the device, upgrade to it
39523          * here to avoid changing device under exclusive owner.
39524 @@ -1050,6 +1081,7 @@ static int loop_configure(struct loop_device *lo, fmode_t mode,
39525         lo->use_dio = lo->lo_flags & LO_FLAGS_DIRECT_IO;
39526         lo->lo_device = bdev;
39527         lo->lo_backing_file = file;
39528 +       lo->lo_backing_virt_file = virt_file;
39529         lo->old_gfp_mask = mapping_gfp_mask(mapping);
39530         mapping_set_gfp_mask(mapping, lo->old_gfp_mask & ~(__GFP_IO|__GFP_FS));
39531  
39532 @@ -1100,6 +1132,8 @@ static int loop_configure(struct loop_device *lo, fmode_t mode,
39533                 bd_abort_claiming(bdev, loop_configure);
39534  out_putf:
39535         fput(file);
39536 +       if (virt_file)
39537 +               fput(virt_file);
39538         /* This is safe: open() is still holding a reference. */
39539         module_put(THIS_MODULE);
39540         return error;
39541 @@ -1108,6 +1142,7 @@ static int loop_configure(struct loop_device *lo, fmode_t mode,
39542  static void __loop_clr_fd(struct loop_device *lo, bool release)
39543  {
39544         struct file *filp;
39545 +       struct file *virt_filp = lo->lo_backing_virt_file;
39546         gfp_t gfp = lo->old_gfp_mask;
39547         struct loop_worker *pos, *worker;
39548  
39549 @@ -1153,6 +1188,7 @@ static void __loop_clr_fd(struct loop_device *lo, bool release)
39550         spin_lock_irq(&lo->lo_lock);
39551         filp = lo->lo_backing_file;
39552         lo->lo_backing_file = NULL;
39553 +       lo->lo_backing_virt_file = NULL;
39554         spin_unlock_irq(&lo->lo_lock);
39555  
39556         lo->lo_device = NULL;
39557 @@ -1214,6 +1250,8 @@ static void __loop_clr_fd(struct loop_device *lo, bool release)
39558          * fput can take open_mutex which is usually taken before lo_mutex.
39559          */
39560         fput(filp);
39561 +       if (virt_filp)
39562 +               fput(virt_filp);
39563  }
39564  
39565  static int loop_clr_fd(struct loop_device *lo)
39566 diff --git a/drivers/block/loop.h b/drivers/block/loop.h
39567 index 082d4b6bfc6a..61feca64688d 100644
39568 --- a/drivers/block/loop.h
39569 +++ b/drivers/block/loop.h
39570 @@ -34,7 +34,7 @@ struct loop_device {
39571         int             lo_flags;
39572         char            lo_file_name[LO_NAME_SIZE];
39573  
39574 -       struct file *   lo_backing_file;
39575 +       struct file     *lo_backing_file, *lo_backing_virt_file;
39576         struct block_device *lo_device;
39577  
39578         gfp_t           old_gfp_mask;
39579 diff --git a/fs/aufs/f_op.c b/fs/aufs/f_op.c
39580 index 99ceca144044..e49dfe855032 100644
39581 --- a/fs/aufs/f_op.c
39582 +++ b/fs/aufs/f_op.c
39583 @@ -304,7 +304,7 @@ static ssize_t aufs_read_iter(struct kiocb *kio, struct iov_iter *iov_iter)
39584         if (IS_ERR(h_file))
39585                 goto out;
39586  
39587 -       if (au_test_loopback_kthread()) {
39588 +       if (0 && au_test_loopback_kthread()) {
39589                 au_warn_loopback(h_file->f_path.dentry->d_sb);
39590                 if (file->f_mapping != h_file->f_mapping) {
39591                         file->f_mapping = h_file->f_mapping;
39592 diff --git a/fs/aufs/loop.c b/fs/aufs/loop.c
39593 index 74347bd75b38..5ef888a1d53f 100644
39594 --- a/fs/aufs/loop.c
39595 +++ b/fs/aufs/loop.c
39596 @@ -133,3 +133,19 @@ void au_loopback_fin(void)
39597                 symbol_put(loop_backing_file);
39598         au_kfree_try_rcu(au_warn_loopback_array);
39599  }
39600 +
39601 +/* ---------------------------------------------------------------------- */
39602 +
39603 +/* support the loopback block device insude aufs */
39604 +
39605 +struct file *aufs_real_loop(struct file *file)
39606 +{
39607 +       struct file *f;
39608 +
39609 +       BUG_ON(!au_test_aufs(file->f_path.dentry->d_sb));
39610 +       fi_read_lock(file);
39611 +       f = au_hf_top(file);
39612 +       fi_read_unlock(file);
39613 +       AuDebugOn(!f);
39614 +       return f;
39615 +}
39616 diff --git a/fs/aufs/loop.h b/fs/aufs/loop.h
39617 index 7293bee427f9..3345c098d0d4 100644
39618 --- a/fs/aufs/loop.h
39619 +++ b/fs/aufs/loop.h
39620 @@ -26,6 +26,8 @@ void au_warn_loopback(struct super_block *h_sb);
39621  
39622  int au_loopback_init(void);
39623  void au_loopback_fin(void);
39624 +
39625 +struct file *aufs_real_loop(struct file *file);
39626  #else
39627  AuStub(struct file *, loop_backing_file, return NULL, struct super_block *sb)
39628  
39629 @@ -36,6 +38,8 @@ AuStubVoid(au_warn_loopback, struct super_block *h_sb)
39630  
39631  AuStubInt0(au_loopback_init, void)
39632  AuStubVoid(au_loopback_fin, void)
39633 +
39634 +AuStub(struct file *, aufs_real_loop, return NULL, struct file *file)
39635  #endif /* BLK_DEV_LOOP */
39636  
39637  #endif /* __KERNEL__ */
39638 diff --git a/fs/aufs/super.c b/fs/aufs/super.c
39639 index 7d9bcc074c87..0eefe350c01c 100644
39640 --- a/fs/aufs/super.c
39641 +++ b/fs/aufs/super.c
39642 @@ -758,7 +758,10 @@ const struct super_operations aufs_sop = {
39643         .show_options   = aufs_show_options,
39644         .statfs         = aufs_statfs,
39645         .put_super      = aufs_put_super,
39646 -       .sync_fs        = aufs_sync_fs
39647 +       .sync_fs        = aufs_sync_fs,
39648 +#ifdef CONFIG_AUFS_BDEV_LOOP
39649 +       .real_loop      = aufs_real_loop
39650 +#endif
39651  };
39652  
39653  /* ---------------------------------------------------------------------- */
39654 diff --git a/include/linux/fs.h b/include/linux/fs.h
39655 index 06c9242932a2..9190ce754010 100644
39656 --- a/include/linux/fs.h
39657 +++ b/include/linux/fs.h
39658 @@ -2113,6 +2113,10 @@ struct super_operations {
39659                                   struct shrink_control *);
39660         long (*free_cached_objects)(struct super_block *,
39661                                     struct shrink_control *);
39662 +#if IS_ENABLED(CONFIG_BLK_DEV_LOOP) || IS_ENABLED(CONFIG_BLK_DEV_LOOP_MODULE)
39663 +       /* and aufs */
39664 +       struct file *(*real_loop)(struct file *);
39665 +#endif
39666  };
39667  
39668  /*
This page took 3.956358 seconds and 3 git commands to generate.